Skip to content

Truly independent web browser

2.8 KB
Raw Blame
<!doctype html>
<script src="../include.js"></script>
<script>
    // (module
    //   (import "m" "returnsV128" (func $returnsV128 (result v128)))
    //   (import "m" "returnsExnref" (func $returnsExnref (result exnref)))
    //   (global $calls (export "calls") (mut i32) (i32.const 0))
    //   (func (export "returnV128") (result v128)
    //     (global.set $calls (i32.add (global.get $calls) (i32.const 1)))
    //     (v128.const i64x2 0 0))
    //   (func (export "returnExnref") (result exnref)
    //     (global.set $calls (i32.add (global.get $calls) (i32.const 1)))
    //     (ref.null exn))
    //   (func (export "callReturnsV128") (drop (call $returnsV128)))
    //   (func (export "callReturnsExnref") (drop (call $returnsExnref))))
    const bytes = new Uint8Array([
        0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x0c, 0x03, 0x60, 0x00, 0x01, 0x7b, 0x60,
        0x00, 0x01, 0x69, 0x60, 0x00, 0x00, 0x02, 0x23, 0x02, 0x01, 0x6d, 0x0b, 0x72, 0x65, 0x74, 0x75,
        0x72, 0x6e, 0x73, 0x56, 0x31, 0x32, 0x38, 0x00, 0x00, 0x01, 0x6d, 0x0d, 0x72, 0x65, 0x74, 0x75,
        0x72, 0x6e, 0x73, 0x45, 0x78, 0x6e, 0x72, 0x65, 0x66, 0x00, 0x01, 0x03, 0x05, 0x04, 0x00, 0x01,
        0x02, 0x02, 0x06, 0x06, 0x01, 0x7f, 0x01, 0x41, 0x00, 0x0b, 0x07, 0x4b, 0x05, 0x05, 0x63, 0x61,
        0x6c, 0x6c, 0x73, 0x03, 0x00, 0x0a, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x56, 0x31, 0x32, 0x38,
        0x00, 0x02, 0x0c, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x45, 0x78, 0x6e, 0x72, 0x65, 0x66, 0x00,
        0x03, 0x0f, 0x63, 0x61, 0x6c, 0x6c, 0x52, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x73, 0x56, 0x31, 0x32,
        0x38, 0x00, 0x04, 0x11, 0x63, 0x61, 0x6c, 0x6c, 0x52, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x73, 0x45,
        0x78, 0x6e, 0x72, 0x65, 0x66, 0x00, 0x05, 0x0a, 0x35, 0x04, 0x1b, 0x00, 0x23, 0x00, 0x41, 0x01,
        0x6a, 0x24, 0x00, 0xfd, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x0b, 0x00, 0x23, 0x00, 0x41, 0x01, 0x6a, 0x24, 0x00, 0xd0,
        0x69, 0x0b, 0x05, 0x00, 0x10, 0x00, 0x1a, 0x0b, 0x05, 0x00, 0x10, 0x01, 0x1a, 0x0b,
    ]);

    test(() => {
        let hostCalls = 0;
        const exports = new WebAssembly.Instance(new WebAssembly.Module(bytes), {
            m: {
                returnsV128: () => { ++hostCalls; },
                returnsExnref: () => { ++hostCalls; },
            },
        }).exports;

        for (const name of ["returnV128", "returnExnref", "callReturnsV128", "callReturnsExnref"]) {
            try {
                exports[name]();
                println(`${name}: no exception`);
            } catch (e) {
                println(`${name}: ${e.name}`);
            }
        }
        println(`WebAssembly calls: ${exports.calls.value}, host calls: ${hostCalls}`);
    });
</script>