Skip to content

Truly independent web browser

4.8 KB
Raw Blame
<!DOCTYPE html>
<script src="../include.js"></script>
<script>
    promiseTest(async () => {
        const WASM_FILE = "../../data/greeter.wasm";

        let wasm;

        const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder;

        let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true });

        cachedTextDecoder.decode();

        function getUint8Memory0() {
            return  new Uint8Array(wasm.memory.buffer);
        }

        function getStringFromWasm0(ptr, len) {
            ptr = ptr >>> 0;
            return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
        }

        const exports = {
            "./wasm_test_bg.js": {
                __wbg_println_95d984b86202de7b(arg0, arg1) {
                    println(getStringFromWasm0(arg0, arg1));
                },
                greet() {
                    wasm.greet();
                },
            },
        };

        function runTest(module) {
            wasm = module.instance?.exports ?? module.exports;
            try {
                wasm.greet();
            } catch (e) {
                println(`FAILED: ${e.message}`);
            }
        }

        println("WebAssembly.instantiateStreaming");
        let module = await WebAssembly.instantiateStreaming(fetch(WASM_FILE), exports).catch(e => {
            println(`FAILED: ${e.message}`);
            return Promise.resolve();
        });
        if (module) {
            runTest(module);
        }

        println("WebAssembly.compileStreaming");
        let compiled = await WebAssembly.compileStreaming(fetch(WASM_FILE)).catch(e => {
            println(`FAILED: ${e.message}`);
            return Promise.resolve();
        });
        if (compiled) {
            let module = await WebAssembly.instantiate(compiled, exports).catch(e => {
                println(`FAILED: ${e.message}`);
                return Promise.resolve();
            });
            runTest(module);
        }

        // Sanity check after running meat of the test
        println("Sanity check");
        const arrayBuffer = new Uint8Array([
            0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x09, 0x02, 0x60, 0x02, 0x7f, 0x7f, 0x00,
            0x60, 0x00, 0x00, 0x02, 0x34, 0x01, 0x11, 0x2e, 0x2f, 0x77, 0x61, 0x73, 0x6d, 0x5f, 0x74, 0x65,
            0x73, 0x74, 0x5f, 0x62, 0x67, 0x2e, 0x6a, 0x73, 0x1e, 0x5f, 0x5f, 0x77, 0x62, 0x67, 0x5f, 0x70,
            0x72, 0x69, 0x6e, 0x74, 0x6c, 0x6e, 0x5f, 0x39, 0x35, 0x64, 0x39, 0x38, 0x34, 0x62, 0x38, 0x36,
            0x32, 0x30, 0x32, 0x64, 0x65, 0x37, 0x62, 0x00, 0x00, 0x03, 0x02, 0x01, 0x01, 0x05, 0x03, 0x01,
            0x00, 0x11, 0x07, 0x12, 0x02, 0x06, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x02, 0x00, 0x05, 0x67,
            0x72, 0x65, 0x65, 0x74, 0x00, 0x01, 0x0a, 0x0d, 0x01, 0x0b, 0x00, 0x41, 0x80, 0x80, 0xc0, 0x00,
            0x41, 0x15, 0x10, 0x00, 0x0b, 0x0b, 0x1e, 0x01, 0x00, 0x41, 0x80, 0x80, 0xc0, 0x00, 0x0b, 0x15,
            0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x66, 0x72, 0x6f, 0x6d, 0x20, 0x77, 0x61, 0x73, 0x6d, 0x21,
            0x21, 0x21, 0x21, 0x21, 0x21, 0x00, 0x7b, 0x09, 0x70, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x65, 0x72,
            0x73, 0x02, 0x08, 0x6c, 0x61, 0x6e, 0x67, 0x75, 0x61, 0x67, 0x65, 0x01, 0x04, 0x52, 0x75, 0x73,
            0x74, 0x00, 0x0c, 0x70, 0x72, 0x6f, 0x63, 0x65, 0x73, 0x73, 0x65, 0x64, 0x2d, 0x62, 0x79, 0x03,
            0x05, 0x72, 0x75, 0x73, 0x74, 0x63, 0x1d, 0x31, 0x2e, 0x37, 0x33, 0x2e, 0x30, 0x20, 0x28, 0x63,
            0x63, 0x36, 0x36, 0x61, 0x64, 0x34, 0x36, 0x38, 0x20, 0x32, 0x30, 0x32, 0x33, 0x2d, 0x31, 0x30,
            0x2d, 0x30, 0x33, 0x29, 0x06, 0x77, 0x61, 0x6c, 0x72, 0x75, 0x73, 0x06, 0x30, 0x2e, 0x31, 0x39,
            0x2e, 0x30, 0x0c, 0x77, 0x61, 0x73, 0x6d, 0x2d, 0x62, 0x69, 0x6e, 0x64, 0x67, 0x65, 0x6e, 0x12,
            0x30, 0x2e, 0x32, 0x2e, 0x38, 0x38, 0x20, 0x28, 0x30, 0x62, 0x35, 0x66, 0x30, 0x65, 0x65, 0x63,
            0x32, 0x29, 0x00, 0x2c, 0x0f, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x5f, 0x66, 0x65, 0x61, 0x74,
            0x75, 0x72, 0x65, 0x73, 0x02, 0x2b, 0x0f, 0x6d, 0x75, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x2d, 0x67,
            0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x73, 0x2b, 0x08, 0x73, 0x69, 0x67, 0x6e, 0x2d, 0x65, 0x78, 0x74
        ]).buffer;
        const fetchedBuffer = new Uint8Array(await (await fetch(WASM_FILE)).arrayBuffer()).buffer;
        if (arrayBuffer.byteLength !== fetchedBuffer.byteLength) {
            println(`FAILED: Mismatch in byte length: ${arrayBuffer.byteLength} !== ${fetchedBuffer.byteLength}`);
        }
        for (let i = 0; i < arrayBuffer.byteLength; i++) {
            if (new Uint8Array(arrayBuffer)[i] !== new Uint8Array(fetchedBuffer)[i]) {
                println(`FAILED: Mismatch at byte ${i}`);
            }
        }
    });
</script>