Skip to content

Instantly share code, notes, and snippets.

@x-labz
Last active January 12, 2022 16:20
Show Gist options
  • Select an option

  • Save x-labz/c2fec3da2eaa2984e4ace064b6f7ff67 to your computer and use it in GitHub Desktop.

Select an option

Save x-labz/c2fec3da2eaa2984e4ace064b6f7ff67 to your computer and use it in GitHub Desktop.

Revisions

  1. x-labz revised this gist Jan 12, 2022. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions init-wasm.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,10 @@
    const WASM_FILE = "./main.wasm";

    const W = 640;
    const H = 360;

    let mem_in, mem_out;
    let mem_bg_img;
    let instance = null;
    let memoryStates = new WeakMap();

  2. x-labz revised this gist Jan 12, 2022. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions init-wasm.js
    Original file line number Diff line number Diff line change
    @@ -71,10 +71,12 @@ const initWASM = () =>
    .then((results) => {
    instance = results.instance;

    // instruct the module to allocate the shared buffers
    const in_p = instance.exports.init_in(W, H);
    const out_p = instance.exports.init_out();
    const bg_p = instance.exports.init_bg_img();
    console.log("init:", in_p, out_p);

    // create byte type view buffers
    mem_in = new Uint8Array(
    instance.exports.memory.buffer,
    in_p,
    @@ -90,8 +92,5 @@ const initWASM = () =>
    bg_p,
    W * H * 4
    );
    // console.log("mem", mem_in, mem_out);
    // instance.exports.reset_buffers();
    // console.log("mem after reset", mem_in, mem_out);
    })
    .catch(console.error);
  3. x-labz created this gist Jan 12, 2022.
    97 changes: 97 additions & 0 deletions init-wasm.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,97 @@
    const WASM_FILE = "./main.wasm";

    let instance = null;
    let memoryStates = new WeakMap();

    // basic system calls from the wasm module

    function syscall(instance, n, args) {
    switch (n) {
    default:
    // console.log("Syscall " + n + " NYI.");
    break;
    case /* brk */ 45:
    return 0;
    case /* writev */ 146:
    return instance.exports.writev_c(args[0], args[1], args[2]);
    case /* mmap2 */ 192:
    debugger;
    const memory = instance.exports.memory;
    let memoryState = memoryStates.get(instance);
    const requested = args[1];
    if (!memoryState) {
    memoryState = {
    object: memory,
    currentPosition: memory.buffer.byteLength,
    };
    memoryStates.set(instance, memoryState);
    }
    let cur = memoryState.currentPosition;
    if (cur + requested > memory.buffer.byteLength) {
    const need = Math.ceil(
    (cur + requested - memory.buffer.byteLength) / 65536
    );
    memory.grow(need);
    }
    memoryState.currentPosition += requested;
    return cur;
    }
    }

    const initWASM = () =>
    fetch(WASM_FILE)
    .then((response) => response.arrayBuffer())
    .then((bytes) =>
    WebAssembly.instantiate(bytes, {
    env: {
    __syscall0: function __syscall0(n) {
    return syscall(instance, n, []);
    },
    __syscall1: function __syscall1(n, a) {
    return syscall(instance, n, [a]);
    },
    __syscall2: function __syscall2(n, a, b) {
    return syscall(instance, n, [a, b]);
    },
    __syscall3: function __syscall3(n, a, b, c) {
    return syscall(instance, n, [a, b, c]);
    },
    __syscall4: function __syscall4(n, a, b, c, d) {
    return syscall(instance, n, [a, b, c, d]);
    },
    __syscall5: function __syscall5(n, a, b, c, d, e) {
    return syscall(instance, n, [a, b, c, d, e]);
    },
    __syscall6: function __syscall6(n, a, b, c, d, e, f) {
    return syscall(instance, n, [a, b, c, d, e, f]);
    }
    }
    })
    )
    .then((results) => {
    instance = results.instance;

    const in_p = instance.exports.init_in(W, H);
    const out_p = instance.exports.init_out();
    const bg_p = instance.exports.init_bg_img();
    console.log("init:", in_p, out_p);
    mem_in = new Uint8Array(
    instance.exports.memory.buffer,
    in_p,
    W * H * 1.5
    );
    mem_out = new Uint8Array(
    instance.exports.memory.buffer,
    out_p,
    W * H * 4
    );
    mem_bg_img = new Uint8Array(
    instance.exports.memory.buffer,
    bg_p,
    W * H * 4
    );
    // console.log("mem", mem_in, mem_out);
    // instance.exports.reset_buffers();
    // console.log("mem after reset", mem_in, mem_out);
    })
    .catch(console.error);