If you're using Emscripten already (or even if you aren't), their preamble offers some convenient extensions to WebAssembly.Module
on the JavaScript side to make interacting with C FFI compatible functions easier. Namely (abreviated, see links for more information):
ccall
(ident, returnType, argTypes, args, opts)
Call a compiled C function from JavaScript. The function executes a compiled C function from JavaScript and returns the result.
returnType
and argTypes
let you specify the types of parameters and the return value. The possible types are "number"
, "string"
or "array"
, which correspond to the appropriate JavaScript types. Use "number"
for any numeric type or C pointer, "string"
for C char*
that represent strings, and "array"
for JavaScript arrays and typed arrays; for typed arrays, it must be a Uint8Array
or Int8Array
.
cwrap
(ident, returnType, argTypes)
Returns a native JavaScript wrapper for a C function. This is similar to ccall()
, but returns a JavaScript function that can be reused as many time as needed.
Notably, using the "string"
argument/return value handles conversion between JavaScript strings and C char*
(to stack space) strings.
As of current, I do not believe there are any pre-existing libraries for handling FFI for larger types. node-ffi
exists as a library for handling FFI between the NodeJS runtime and dynamic libraries, and some of its machinery may be reusable for interfacing with a WASM runtime.
However, I think you may have misunderstood the purpose of WebAssembly. WebAssembly does not replace JavaScript.
Is WebAssembly trying to replace JavaScript?
No! WebAssembly is designed to be a complement to, not replacement of, JavaScript. While WebAssembly will, over time, allow many languages to be compiled to the Web, JavaScript has an incredible amount of momentum and will remain the single, privileged (as described above) dynamic language of the Web. Furthermore, it is expected that JavaScript and WebAssembly will be used together in a number of configurations:
- Whole, compiled C++ apps that leverage JavaScript to glue things together.
- HTML/CSS/JavaScript UI around a main WebAssembly-controlled center canvas, allowing developers to leverage the power of web frameworks to build accessible, web-native-feeling experiences.
- Mostly HTML/CSS/JavaScript app with a few high-performance WebAssembly modules (e.g., graphing, simulation, image/sound/video processing, visualization, animation, compression, etc., examples which we can already see in asm.js today) allowing developers to reuse popular WebAssembly libraries just like JavaScript libraries today.
- When WebAssembly gains the ability to access garbage-collected objects 🦄, those objects will be shared with JavaScript, and not live in a walled-off world of their own.
Even with that said, all JavaScript libraries are built around the expressiveness of the JavaScript language, and won't translate well into other languages. Libraries like React, Angular, and Vue use JavaScript's dynamic typing to enable key parts of their functionality.
Still, I'm open to see what's possible. WASM is still a young, growing beast, and what's a pipe dream today may be possible in the coming years. If you do something cool, make sure to share it!
.ts
file, and add type machinery incrementally on top as you want to. – CAD97 Dec 6 '17 at 8:12