Repository navigation
Link AssemblyScript with a module in c/cpp #741
Description
Activity
This requires linking. I think.
Also, when this feature is supported by wasm runtimes, you'll be able to pass in
@unmanagedclass references to c api functions.This is my understanding. Maybe I'm mistaken?
cross-language communication or FFI in WebAssembly is one of fundamental problems. I guess only WebIDL-binding (declaration) could resolve this issue in general case.
Reacted by Joshua TennerThe question is - how to produce a static library using AssemblyScript compiler? This library could be linked with something else. I do understand that "complicate" data types (like
string, orArray) cannot be passed from AssemblyScript to C, but basic ones (numbers) should work.Yep. You will just need to know c layout, and use the assemblyscript builtins to your advantage. All of us are waiting for web assembly features to be standardized like webidl, anyref, gc etc.
The challenges with proper linking are described at tool-conventions/Linking. In particular, Wasm elements must be renumbered and memory and table offsets relocated in a way that the individual parts don't conflict. My expectation is that Binaryen will support static linking eventually, and we'd reuse what it provides by generating the respective reloc sections. Not there yet, though.
The best one can do currently is to build two modules and let them interact through imports and exports. The interface of the C module can be described as imports by ambient declarations using
declarefor example so the AS module can call its exports (looking a bit like this or this). If memory must be read/written, a bit of glue code on the JS side will be necessary as well, like read something from one module's memory and write it to another module's memory.This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This seems like the way to go forward with this:
Basically have a special section in each binary for how to convert export and imports to and from abstract interface types like strings, arrays, etc.
Would love to use AssemblyScript but much of the code that I need to adapt is written in C/C++ and I wouldn't want to write multiple WASM modules that need JS glue to pass data between them. It would be great to have binaryen be able to link module interfaces written in different languages on the back end.
Reacted by Zhang ZhuoIt's possible right now if you have really simple C/C++ code and you can compile it with clang without runtime and memory. Here concatenation problem described better. Basically we need reference types and multi memories for linking more complex code in one wasm sandbox.
I'm fairly new to AssemblyScript, but as I understand it, asc can only output a .wasm file, not an object file, so there's no opportunity to link with code compiled from other sources. I use llvm wasm-ld for linking multiple object files together. For regular use, you'd just need your imports & exports configured correctly in the various modules & it should just work, unless I'm missing something.
So, we need an option in asc to output an obj file instead of a wasm. If such an option already exists, details welcome.
Is it possible to link AssemblScript with modules written in other languages, primarily C/C++? The use case - there is a library in C that's I'd like to call from AssemblScript.