Search code examples
apicompiler-constructionwebassembly

Wasm JS api implementation


I'm currently for the fun of it rewriting the back end of a language we used in a programming language course to target wasm instead of MIPS.

So be very clear I'm not talking about using wasm but creating wasm file and getting it all right.

I'm currently looking on import and export and the webassembly specification (as for now) does not cover how the wasm/js api actually works. For example when importing something like JS console.log function or another function does the JS engine just search the import section and looks for the names encoded and match it with a function?

I know that this is not a "code issues", in the usual way but I can't seem to find the right place to look.


Solution

  • I'm currently looking on import and export and the webassembly specification (as for now) does not cover how the wasm/js api actually works.

    That's the beauty of a specification, it can have various different implementations!

    For example when importing something like JS console.log function or another function does the JS engine just search the import section and looks for the names encoded and match it with a function?

    That would seem like a reasonable implementation. When you create a WebAssembly module you supply named import functions, these must match the names in the WebAssembly module's import section.

    webassemblyjs is a JavaScript-based WebAssembly interpreter. You can see how it matches externally provided imports with the module's expected imports here:

    https://github.com/xtuc/webassemblyjs/blob/master/packages/webassemblyjs/src/interpreter/index.js#L57