Skip to content

Truly independent web browser

3.0 KB
Raw Blame
<!doctype html>
<script src="../include.js"></script>
<script>
    // Build (module (type (func (param T...) (result T...))) (type (func))
    //   (import "m" "hook" (func (type 1)))
    //   (func (export "echo") (type 0) (call 0) (local.get 0) ...)).
    // These small sections and indices fit in single-byte LEB128 values.
    function echoFunction(types, hook = () => {}) {
        const section = (id, bytes) => [id, bytes.length, ...bytes];
        const body = [0, 0x10, 0, ...types.flatMap((_, i) => [0x20, i]), 0x0b];
        const bytes = new Uint8Array([
            0, 97, 115, 109, 1, 0, 0, 0,
            ...section(1, [2, 0x60, types.length, ...types, types.length, ...types, 0x60, 0, 0]),
            ...section(2, [1, 1, 109, 4, 104, 111, 111, 107, 0, 1]),
            ...section(3, [1, 0]),
            ...section(7, [1, 4, 101, 99, 104, 111, 0, 1]),
            ...section(10, [1, body.length, ...body]),
        ]);
        return new WebAssembly.Instance(new WebAssembly.Module(bytes), { m: { hook } }).exports.echo;
    }

    test(() => {
        const i32 = 0x7f, i64 = 0x7e, f32 = 0x7d, f64 = 0x7c;
        const mixed = echoFunction([i32, i64, f32, f64, i32, i64, f32, f64], () => internals.gc());
        const inputs = [4294967297, (1n << 64n) + 2n, 1 / 3, -0, -3, -4n, Infinity, NaN];
        const expected = [1, 2n, Math.fround(1 / 3), -0, -3, -4n, Infinity, NaN];
        const order = [];
        const converted = inputs.map((value, index) => ({
            valueOf() {
                order.push(index);
                internals.gc();
                // Reenter wasm while the outer call is still converting its arguments.
                if (echoFunction([i32])(17) !== 17)
                    throw new Error("reentry failed");
                return value;
            },
        }));
        const result = mixed(...converted);
        println(`conversion order: ${order.join(", ")}`);
        println(`mixed results: ${result.every((v, i) => Object.is(v, expected[i]))}`);

        const sentinel = {};
        try {
            mixed(1, { valueOf() { throw sentinel; } }, { valueOf() { throw new Error("unreachable"); } });
        } catch (e) {
            println(`conversion exception: ${e === sentinel}`);
        }
        try {
            echoFunction([], () => { throw sentinel; })();
        } catch (e) {
            println(`import exception: ${e === sentinel}`);
        }
        println(`empty result: ${echoFunction([])()}`);
        println(`missing integer: ${echoFunction([i32])()}`);
        println(`missing float: ${Number.isNaN(echoFunction([f64])())}`);
        try {
            echoFunction([i64])(1);
        } catch (e) {
            println(`invalid BigInt: ${e.name}`);
        }
        println(`extra argument: ${echoFunction([i32])(42, { valueOf() { throw sentinel; } })}`);
        println(`wide results: ${echoFunction(Array(9).fill(i32))(1, 2, 3, 4, 5, 6, 7, 8, 9).join(", ")}`);
        const object = {};
        println(`reference result: ${echoFunction([0x6f], () => internals.gc())(object) === object}`);
    });
</script>