Vedant Roy via llvm-dev
2020-Sep-14 02:21 UTC
[llvm-dev] WebAssembly - Import functions from runtime
Hi, Is there a method for importing functions from the Web Assembly runtime when writing C? I know Emscripten has support for calling JS functions from C code, but I was wondering if there's an easy way to do this without Emscripten (pure Clang)? Best, Ved -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200913/307babb9/attachment.html>
Thomas Lively via llvm-dev
2020-Sep-14 07:04 UTC
[llvm-dev] WebAssembly - Import functions from runtime
Hi Ved, Yes, the trick is to declare the functions you want to import in your C source, then pass the `--allow-undefined` flag to wasm-ld when linking your wasm module. Here's a complete example: hello.c: ``` int print_int(int x); int do_work(int x) { print_int(x); return x; } ``` clang -target wasm32 -c hello.c -o hello.o wasm-ld hello.o -o hello.wasm --no-entry --allow-undefined --export=do_work This will give you a wasm module that imports function "env" "print_int" and exports function "do_work". Note that Wasm uses a two-level namespace for imports, but the first level will always be "env" unless you annotate the function declaration with `__attribute__((import_module("my_other_namespace")))`. Best, Thomas On Sun, Sep 13, 2020 at 8:04 PM Vedant Roy via llvm-dev < llvm-dev at lists.llvm.org> wrote:> Hi, > > Is there a method for importing functions from the Web Assembly runtime > when writing C? I know Emscripten has support for calling JS functions from > C code, but I was wondering if there's an easy way to do this without > Emscripten (pure Clang)? > > Best, > Ved > > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200914/b68f28e2/attachment.html>