Truly independent web browser
<!doctype html>
<script src="../include.js"></script>
<script>
// (module
// (global $e (import "m" "e") externref)
// (global $a (import "m" "a") anyref)
// (global $f (import "m" "f") funcref)
// (func (export "e") (result externref) (global.get $e))
// (func (export "a") (result anyref) (global.get $a))
// (func (export "f") (result funcref) (global.get $f)))
const globalsBytes = new Uint8Array([
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x0d, 0x03, 0x60, 0x00, 0x01, 0x6f, 0x60,
0x00, 0x01, 0x6e, 0x60, 0x00, 0x01, 0x70, 0x02, 0x16, 0x03, 0x01, 0x6d, 0x01, 0x65, 0x03, 0x6f,
0x00, 0x01, 0x6d, 0x01, 0x61, 0x03, 0x6e, 0x00, 0x01, 0x6d, 0x01, 0x66, 0x03, 0x70, 0x00, 0x03,
0x04, 0x03, 0x00, 0x01, 0x02, 0x07, 0x0d, 0x03, 0x01, 0x65, 0x00, 0x00, 0x01, 0x61, 0x00, 0x01,
0x01, 0x66, 0x00, 0x02, 0x0a, 0x10, 0x03, 0x04, 0x00, 0x23, 0x00, 0x0b, 0x04, 0x00, 0x23, 0x01,
0x0b, 0x04, 0x00, 0x23, 0x02, 0x0b, 0x00, 0x11, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x07, 0x0a, 0x03,
0x00, 0x01, 0x65, 0x01, 0x01, 0x61, 0x02, 0x01, 0x66,
]);
// (module (global (import "m" "g") (mut externref)))
const mutableBytes = new Uint8Array([
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x02, 0x08, 0x01, 0x01, 0x6d, 0x01, 0x67, 0x03,
0x6f, 0x01,
]);
function tryInstantiate(bytes, imports) {
try {
return new WebAssembly.Instance(new WebAssembly.Module(bytes), imports).exports;
} catch (exception) {
return exception.constructor.name;
}
}
test(() => {
// Reference-typed globals can be imported from plain values, which convert to the global's type.
const object = { name: "object" };
const exports = tryInstantiate(globalsBytes, { m: { e: object, a: 42, f: null } });
println(`externref: ${exports.e() === object}, anyref: ${exports.a()}, funcref: ${exports.f()}`);
println(`a value that does not convert: ${tryInstantiate(globalsBytes, { m: { e: object, a: 42, f: () => {} } })}`);
println(`a value for a mutable global: ${tryInstantiate(mutableBytes, { m: { g: object } })}`);
});
</script>