MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

add wasm demo

+59
+41
examples/demo/build.js
··· 1 + export function buildWasm({ types, funcs, exports }) { 2 + const str = s => [s.length, ...Buffer.from(s)]; 3 + const section = (id, data) => [id, data.length, ...data]; 4 + 5 + return new Uint8Array([ 6 + 0x00, 7 + 0x61, 8 + 0x73, 9 + 0x6d, // magic 10 + 0x01, 11 + 0x00, 12 + 0x00, 13 + 0x00, // version 14 + 15 + ...section(0x01, [ 16 + // types 17 + types.length, 18 + ...types.flatMap(([params, results]) => [0x60, params.length, ...params, results.length, ...results]) 19 + ]), 20 + 21 + ...section(0x03, [ 22 + // funcs -> type index 23 + funcs.length, 24 + ...funcs.map((_, i) => i) 25 + ]), 26 + 27 + ...section(0x07, [ 28 + // exports 29 + exports.length, 30 + ...exports.flatMap(([name, kind, idx]) => [...str(name), kind, idx]) 31 + ]), 32 + 33 + ...section(0x0a, [ 34 + funcs.length, 35 + ...funcs.flatMap(({ locals = [], body }) => { 36 + const code = [locals.length, ...body, 0x0b]; 37 + return [code.length, ...code]; 38 + }) 39 + ]) 40 + ]); 41 + }
+17
examples/demo/wasm.js
··· 1 + import { buildWasm } from './build'; 2 + 3 + const i32 = 0x7f; 4 + const local_get = i => [0x20, i]; 5 + const i32_add = [0x6a]; 6 + 7 + const wasmBytes = buildWasm({ 8 + types: [[[i32, i32], [i32]]], 9 + funcs: [{ body: [...local_get(0), ...local_get(1), ...i32_add] }], 10 + exports: [['add', 0x00, 0]] 11 + }); 12 + 13 + console.log(`compiled to ${wasmBytes.length} bytes\n`); 14 + const { instance } = await WebAssembly.instantiate(wasmBytes); 15 + 16 + console.log('add(2, 3) =', instance.exports.add(2, 3)); 17 + console.log('add(100, 200) =', instance.exports.add(100, 200));
+1
src/modules/napi.c
··· 1344 1344 if (argv) { 1345 1345 size_t ncopy = requested < info->argc ? requested : info->argc; 1346 1346 if (ncopy > 0) memcpy(argv, info->argv, ncopy * sizeof(napi_value)); 1347 + for (size_t i = ncopy; i < requested; i++) argv[i] = (napi_value)js_mkundef(); 1347 1348 } 1348 1349 *argc = info->argc; 1349 1350 }