Skip to content

Truly independent web browser

4.3 KB
Raw Blame
<!doctype html>
<script src="../include.js"></script>
<iframe id="frame"></iframe>
<script>
    // (module
    //   (type $answer (func (result i32)))
    //   (import "m" "memory" (memory 1))
    //   (import "m" "table" (table 1 funcref))
    //   (import "m" "global" (global $global (mut i32)))
    //   (import "m" "f" (func $f (result i32)))
    //   (func (export "readMemory") (result i32) (i32.load8_u (i32.const 0)))
    //   (func (export "growMemory") (result i32) (memory.grow (i32.const 1)))
    //   (func (export "callTable") (result i32) (call_indirect (type $answer) (i32.const 0)))
    //   (func (export "getGlobal") (result i32) (global.get $global))
    //   (func (export "setGlobal") (param i32) (global.set $global (local.get 0)))
    //   (func (export "callF") (result i32) (call $f))
    //   (export "f" (func $f)))
    const mainBytes = new Uint8Array([
        0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x09, 0x02, 0x60, 0x00, 0x01, 0x7f, 0x60,
        0x01, 0x7f, 0x00, 0x02, 0x2b, 0x04, 0x01, 0x6d, 0x06, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x02,
        0x00, 0x01, 0x01, 0x6d, 0x05, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x01, 0x70, 0x00, 0x01, 0x01, 0x6d,
        0x06, 0x67, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x03, 0x7f, 0x01, 0x01, 0x6d, 0x01, 0x66, 0x00, 0x00,
        0x03, 0x07, 0x06, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x07, 0x4b, 0x07, 0x0a, 0x72, 0x65, 0x61,
        0x64, 0x4d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x00, 0x01, 0x0a, 0x67, 0x72, 0x6f, 0x77, 0x4d, 0x65,
        0x6d, 0x6f, 0x72, 0x79, 0x00, 0x02, 0x09, 0x63, 0x61, 0x6c, 0x6c, 0x54, 0x61, 0x62, 0x6c, 0x65,
        0x00, 0x03, 0x09, 0x67, 0x65, 0x74, 0x47, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x00, 0x04, 0x09, 0x73,
        0x65, 0x74, 0x47, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x00, 0x05, 0x05, 0x63, 0x61, 0x6c, 0x6c, 0x46,
        0x00, 0x06, 0x01, 0x66, 0x00, 0x00, 0x0a, 0x29, 0x06, 0x07, 0x00, 0x41, 0x00, 0x2d, 0x00, 0x00,
        0x0b, 0x06, 0x00, 0x41, 0x01, 0x40, 0x00, 0x0b, 0x07, 0x00, 0x41, 0x00, 0x11, 0x00, 0x00, 0x0b,
        0x04, 0x00, 0x23, 0x00, 0x0b, 0x06, 0x00, 0x20, 0x00, 0x24, 0x00, 0x0b, 0x04, 0x00, 0x10, 0x00,
        0x0b, 0x00, 0x21, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x01, 0x04, 0x01, 0x00, 0x01, 0x66, 0x04, 0x09,
        0x01, 0x00, 0x06, 0x61, 0x6e, 0x73, 0x77, 0x65, 0x72, 0x07, 0x09, 0x01, 0x00, 0x06, 0x67, 0x6c,
        0x6f, 0x62, 0x61, 0x6c,
    ]);

    // (module (func (export "answer") (result i32) (i32.const 42)))
    const frameBytes = new Uint8Array([
        0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x05, 0x01, 0x60, 0x00, 0x01, 0x7f, 0x03,
        0x02, 0x01, 0x00, 0x07, 0x0a, 0x01, 0x06, 0x61, 0x6e, 0x73, 0x77, 0x65, 0x72, 0x00, 0x00, 0x0a,
        0x06, 0x01, 0x04, 0x00, 0x41, 0x2a, 0x0b,
    ]);

    test(() => {
        const frameWindow = document.getElementById("frame").contentWindow;
        const FrameWebAssembly = frameWindow.WebAssembly;
        const answer = new FrameWebAssembly.Instance(new FrameWebAssembly.Module(frameBytes)).exports.answer;
        const memory = new FrameWebAssembly.Memory({ initial: 1, maximum: 4 });
        const table = new FrameWebAssembly.Table({ element: "anyfunc", initial: 1 });
        const global = new FrameWebAssembly.Global({ value: "i32", mutable: true }, 7);
        table.set(0, answer);

        const instance = new WebAssembly.Instance(new WebAssembly.Module(mainBytes), { m: { memory, table, global, f: answer } });
        const exports = instance.exports;

        new frameWindow.Uint8Array(memory.buffer)[0] = 123;
        println(`memory of another realm: ${exports.readMemory()}`);
        const oldBuffer = memory.buffer;
        println(`grown from WebAssembly: ${exports.growMemory()}, old buffer detached: ${oldBuffer.byteLength === 0}, new length: ${memory.buffer.byteLength}`);

        println(`table of another realm: ${exports.callTable()}`);

        println(`global of another realm: ${exports.getGlobal()}`);
        global.value = 9;
        exports.setGlobal(exports.getGlobal() + 1);
        println(`global written from both sides: ${global.value}`);

        println(`function of another realm: ${exports.callF()}`);
        println(`re-exported function is the same object: ${exports.f === answer}`);
        println(`re-exported function's realm: ${Object.getPrototypeOf(exports.f) === frameWindow.Function.prototype}`);
    });
</script>