text
stringlengths
0
1.99k
using WebAssembly.instantiate(), specifying an importObject containing a
helper function that performs the actual logging:
const importObject = {
ns: {
logger: (num) => {
console.log(`The answer is: ${num}`)
}
}
};
fetch("logger.wasm")
.then((response) => response.arrayBuffer())
.then((bytes) => WebAssembly.instantiate(bytes, importObject));
A single import is given ("logger"), which resides in the "ns" namespace:
imports are required to have a namespace, but notably this is restricted to
be exactly one level (no more and no less).
Instantiating the module will invoke its designated "start" method (if
specified), in our case the $main function. It logs the following to the
console:
The answer is: 42
---[ 2 - A "feature" of the specification
WASM modules statically specify the imports they require. During
instantiation, the runtime maps each of these imports to the corresponding
JavaScript object (it does not have to be a function). The W3C's
"WebAssembly JavaScript Interface" specification details exactly how this
mapping should occur [0]:
1. If module.imports is not empty, and importObject is undefined, throw a
TypeError exception.
2. Let imports be << >>.
3. For each (moduleName, componentName, externtype) of module_imports,
1. Let o be ? Get(importObject, moduleName).
2. If o is not an Object, throw a TypeError exception.
3. Let v be ? Get(o, componentName).
4. If externtype is of the form func functype,
... (snip) ...
4. Let externfunc be the external value func funcaddr.
5. Append externfunc to imports.
... (snip) ...
In other words, for each of the module's specified imports, the runtime
attempts to use Get(importObject, moduleName) to obtain the specified
namespace as a key (property) of the importObject, and then again uses
Get(o, componentName) to reference the import as a key of that namespace
object.
What is Get() in this specification language? Well, if we follow its
definition in the "ECMAScript 2026 Language Specification", we end up at
OrdinaryGet(), which is defined to perform the following steps [1]:
1. Let desc be ? O.[[GetOwnProperty]](P).
2. If desc is undefined, then
a. Let parent be ? O.[[GetPrototypeOf]]().
b. If parent is null, return undefined.
c. Return ? parent.[[Get]](P, Receiver).
3. If IsDataDescriptor(desc) is true, return desc.[[Value]].
4. Assert: IsAccessorDescriptor(desc) is true.
5. Let getter be desc.[[Get]].
6. If getter is undefined, return undefined.
7. Return ? Call(getter, Receiver).
This recursive mechanism (step 2.c) follows what is known as the prototype
chain: a form of inheritance which is core to the JavaScript language. It
is why you can call .toString() on almost any object, for example. While it
may be normal that this occurs here (almost all property-lookups in
JavaScript use this mechanism), I believe that WASM import lookups are
implicit enough that almost nobody fully thinks this through.
---[ 3 - Importing from the prototype
Why do I say this? Well, consider our importObject from before:
const importObject = {
ns: {
logger: (num) => {
console.log(`The answer is: ${num}`)
}
}
};
If we use tab-completion in a JavaScript REPL, we see that it "inherits" a
bunch of properties from the Object prototype:
> importObject.<tab>
importObject.__proto__ importObject.constructor
importObject.hasOwnProperty importObject.isPrototypeOf
importObject.propertyIsEnumerable importObject.toLocaleString
importObject.toString importObject.valueOf
importObject.ns
So does this mean that besides "ns", all of these other properties can also
be imported as WASM namespaces?! Yes :)