Skip to content

Commit

Permalink
Fix setting WebAssembly.Global value before being "bound" to a WASM…
Browse files Browse the repository at this point in the history
… module
  • Loading branch information
TooTallNate committed Oct 5, 2023
1 parent 59779a7 commit 0e3264e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/small-crabs-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'nxjs-runtime': patch
---

Fix setting `WebAssembly.Global` value before being "bound" to a WASM module
7 changes: 5 additions & 2 deletions apps/tests/src/wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ test('global.wasm', async () => {
const g = new WebAssembly.Global({ value: 'i32', mutable: true }, 6);
assert.equal(g.value, 6, 'getting initial value from JS');

g.value = 12;
assert.equal(g.value, 12, 'reading updated value from JS');

const { module, instance } = await WebAssembly.instantiateStreaming(
fetch('global.wasm'),
{
Expand All @@ -121,8 +124,8 @@ test('global.wasm', async () => {
const incGlobal = instance.exports.incGlobal as Function;
assert.type(incGlobal, 'function');

assert.equal(g.value, 6, 'getting initial value from JS after init');
assert.equal(getGlobal(), 6, 'getting initial value from wasm');
assert.equal(g.value, 12, 'getting initial value from JS after init');
assert.equal(getGlobal(), 12, 'getting initial value from wasm');

g.value = 42;
assert.equal(getGlobal(), 42, 'getting JS-updated value from wasm');
Expand Down
6 changes: 5 additions & 1 deletion packages/runtime/src/wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ export class Global<T extends ValueType = ValueType>

set value(v: ValueTypeMap[T]) {
const i = globalInternalsMap.get(this)!;
i.opaque ? Switch.native.wasmGlobalSet(i.opaque, v) : i.value;
if (i.opaque) {
Switch.native.wasmGlobalSet(i.opaque, v);
} else {
i.value = v;
}
}

/**
Expand Down

0 comments on commit 0e3264e

Please sign in to comment.