Skip to content

Commit

Permalink
Support "memory" type in WebAssembly.Module.imports() and `WebAssem…
Browse files Browse the repository at this point in the history
…bly.Module.exports()`

By using TooTallNate's fork of wasm3.

See: TooTallNate/pacman-packages@7f7a72b
  • Loading branch information
TooTallNate committed Oct 5, 2023
1 parent 23d9189 commit af4273d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .changeset/short-pears-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'nxjs-tests': patch
---

Support "memory" type in `WebAssembly.Module.imports()` and `WebAssembly.Module.exports()`
16 changes: 7 additions & 9 deletions apps/tests/src/wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,10 @@ test('fib.wasm', async () => {
);
assert.equal(WebAssembly.Module.imports(module).length, 0);

// TODO: "memory" kind not yet supported in `exports()`
//assert.equal(WebAssembly.Module.exports(module), [
// { name: 'memory', kind: 'memory' },
// { name: 'fib', kind: 'function' }
//]);
assert.equal(WebAssembly.Module.exports(module), [
{ name: 'fib', kind: 'function' },
{ name: 'memory', kind: 'memory' },
]);

const fib = instance.exports.fib as (i: number) => number;
assert.type(fib, 'function');
Expand Down Expand Up @@ -170,10 +169,9 @@ test('memory-export.wasm', async () => {
);
assert.equal(WebAssembly.Module.imports(module).length, 0);

// TODO: "memory" kind not yet supported in `exports()`
//assert.equal(WebAssembly.Module.exports(module), [
// { name: 'memory', kind: 'memory' },
//]);
assert.equal(WebAssembly.Module.exports(module), [
{ name: 'memory', kind: 'memory' },
]);

const memory = instance.exports.memory as WebAssembly.Memory;
assert.instance(memory, WebAssembly.Memory);
Expand Down
43 changes: 18 additions & 25 deletions source/wasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ static JSValue nx_wasm_global_value_get(JSContext *ctx, JSValueConst this_val, i
return nx_throw_wasm_error(ctx, "LinkError", r);
}

return JS_NewInt32(ctx, val.value.i32);
return nx__wasm_tojsvalue(ctx, val.type, &val.value);
}

static JSValue nx_wasm_global_value_set(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
Expand All @@ -155,14 +155,9 @@ static JSValue nx_wasm_global_value_set(JSContext *ctx, JSValueConst this_val, i
if (!global)
{
// Not bound
// TODO: throw error
return JS_ThrowTypeError(ctx, "Global not defined");
}

// M3TaggedValue val;
// val.type = global->type;
// val.value.i32 = 123;

JSValue new_val = argv[1];
switch (global->type)
{
Expand Down Expand Up @@ -193,12 +188,6 @@ static JSValue nx_wasm_global_value_set(JSContext *ctx, JSValueConst this_val, i
};
}

// M3Result r = m3_SetGlobal(global, &val);
// if (r)
//{
// return nx_throw_wasm_error(ctx, "LinkError", r);
// }

return JS_UNDEFINED;
}

Expand Down Expand Up @@ -533,12 +522,9 @@ static JSValue nx_wasm_new_instance(JSContext *ctx, JSValueConst this_val, int a
return buf;
}

// TODO: Seems like the exported name is not saved within wasm3 - assume "memory" for now
const char *name = "memory";

JSValue item = JS_NewObject(ctx);
JS_DefinePropertyValueStr(ctx, item, "kind", JS_NewString(ctx, "memory"), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, item, "name", JS_NewString(ctx, name), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, item, "name", JS_NewString(ctx, instance->module->memoryExportName), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, item, "val", buf, JS_PROP_C_W_E);
// TODO: maximum / shared?
JS_DefinePropertyValueUint32(ctx, exports_array, exports_index++, item, JS_PROP_C_W_E);
Expand Down Expand Up @@ -589,15 +575,15 @@ static JSValue nx_wasm_module_imports(JSContext *ctx, JSValueConst this_val, int
}
}

// if (m->module->memoryImported) {
// JSValue item = JS_NewObject(ctx);
// JS_DefinePropertyValueStr(ctx, item, "kind", JS_NewString(ctx, "memory"), JS_PROP_C_W_E);
// JS_DefinePropertyValueStr(ctx, item, "module", JS_NewString(ctx, "?"), JS_PROP_C_W_E);
// JS_DefinePropertyValueStr(ctx, item, "name", JS_NewString(ctx, "?"), JS_PROP_C_W_E);
// JS_DefinePropertyValueUint32(ctx, imports, index++, item, JS_PROP_C_W_E);
// }
if (m->module->memoryImported) {
JSValue item = JS_NewObject(ctx);
JS_DefinePropertyValueStr(ctx, item, "kind", JS_NewString(ctx, "memory"), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, item, "module", JS_NewString(ctx, m->module->memoryImport.moduleUtf8), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, item, "name", JS_NewString(ctx, m->module->memoryImport.fieldUtf8), JS_PROP_C_W_E);
JS_DefinePropertyValueUint32(ctx, imports, index++, item, JS_PROP_C_W_E);
}

// TODO: "table" import types (seems that perhaps wasm3 doesn't currently support?)
// TODO: "table" import types (wasm3 doesn't currently support)

return imports;
}
Expand Down Expand Up @@ -637,7 +623,14 @@ static JSValue nx_wasm_module_exports(JSContext *ctx, JSValueConst this_val, int
}
}

// TODO: other export types.
if (!m->module->memoryImported && m->module->memoryExportName) {
JSValue item = JS_NewObject(ctx);
JS_DefinePropertyValueStr(ctx, item, "kind", JS_NewString(ctx, "memory"), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, item, "name", JS_NewString(ctx, m->module->memoryExportName), JS_PROP_C_W_E);
JS_DefinePropertyValueUint32(ctx, exports, index++, item, JS_PROP_C_W_E);
}

// TODO: "table" import types (wasm3 doesn't currently support)

return exports;
}
Expand Down

0 comments on commit af4273d

Please sign in to comment.