Skip to content

Link AssemblyScript with a module in c/cpp #741

Description

@geloizi

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.

Activity

  1. jtenner commented on Aug 4, 2019

    @jtenner
    Contributor

    This requires linking. I think.

    Also, when this feature is supported by wasm runtimes, you'll be able to pass in @unmanaged class references to c api functions.

    This is my understanding. Maybe I'm mistaken?

  2. MaxGraey commented on Aug 4, 2019

    @MaxGraey
    Member

    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.

  3. geloizi commented on Aug 4, 2019

    @geloizi
    Author

    The 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, or Array) cannot be passed from AssemblyScript to C, but basic ones (numbers) should work.

  4. jtenner commented on Aug 4, 2019

    @jtenner
    Contributor

    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.

  5. dcodeIO commented on Aug 5, 2019

    @dcodeIO
    Member

    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 declare for 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.

  6. stale commented on Sep 4, 2019

    @stale

    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.

  7. willemneal commented on Sep 11, 2019

    @willemneal
    Contributor

    This seems like the way to go forward with this:

    https://github.com/WebAssembly/interface-types/blob/master/proposals/interface-types/Explainer.md#walkthrough

    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.

  8. J-Rojas commented on Jan 31, 2020

    @J-Rojas

    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.

  9. munrocket commented on Jan 19, 2022

    @munrocket

    It'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.

  10. adv-sw commented on Jun 23, 2025

    @adv-sw

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions