Skip to content

Commit

Permalink
WebAssembly.Memory "index: u32/u64" constructor parameter (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
dakenf authored Aug 4, 2023
1 parent d1bcd3d commit 7cfa894
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
9 changes: 8 additions & 1 deletion document/js-api/index.bs
Original file line number Diff line number Diff line change
Expand Up @@ -607,9 +607,15 @@ interface Instance {
<h3 id="memories">Memories</h3>

<pre class="idl">
enum MemoryIndexType {
"u32",
"u64",
};

dictionary MemoryDescriptor {
required [EnforceRange] unsigned long initial;
[EnforceRange] unsigned long maximum;
MemoryIndexType index;
};

[LegacyNamespace=WebAssembly, Exposed=(Window,Worker,Worklet)]
Expand Down Expand Up @@ -662,7 +668,8 @@ which can be simultaneously referenced by multiple {{Instance}} objects. Each
1. Let |initial| be |descriptor|["initial"].
1. If |descriptor|["maximum"] [=map/exists=], let |maximum| be |descriptor|["maximum"]; otherwise, let |maximum| be empty.
1. If |maximum| is not empty and |maximum| &lt; |initial|, throw a {{RangeError}} exception.
1. Let |memtype| be { min |initial|, max |maximum| }.
1. If |descriptior|["index"] [=map/exists=], let |index| be |descriptor|["index"]; otherwise, let |index| be "u32".
1. Let |memtype| be { min |initial|, max |maximum|, index |index| }.
1. Let |store| be the [=surrounding agent=]'s [=associated store=].
1. Let (|store|, |memaddr|) be [=mem_alloc=](|store|, |memtype|). If allocation fails, throw a {{RangeError}} exception.
1. Set the [=surrounding agent=]'s [=associated store=] to |store|.
Expand Down
7 changes: 6 additions & 1 deletion test/js-api/memory/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@ function assert_ArrayBuffer(actual, { size=0, shared=false, detached=false }, me
assert_equals(Object.isExtensible(actual), !shared, "buffer extensibility");
}

function assert_Memory(memory, { size=0, shared=false }) {
function assert_Memory(memory, { size=0, shared=false, index="u32" }) {
assert_equals(Object.getPrototypeOf(memory), WebAssembly.Memory.prototype,
"prototype");
assert_true(Object.isExtensible(memory), "extensible");

// https://github.com/WebAssembly/spec/issues/840
assert_equals(memory.buffer, memory.buffer, "buffer should be idempotent");
assert_ArrayBuffer(memory.buffer, { size, shared });

// this depends on js-types proposal implementation
if (typeof memory.type == "function") {
assert_equals(memory.type().index, index, "memory index");
}
}
25 changes: 25 additions & 0 deletions test/js-api/memory/constructor.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ test(() => {
assert_unreached(`Should not call [[HasProperty]] with ${x}`);
},
get(o, x) {
if (x === "index") {
return "u32";
}
return 0;
},
});
Expand Down Expand Up @@ -128,3 +131,25 @@ test(() => {
const memory = new WebAssembly.Memory(argument, {});
assert_Memory(memory, { "size": 0 });
}, "Stray argument");

test(() => {
const argument = { "initial": 1 };
const memory = new WebAssembly.Memory(argument);
assert_Memory(memory, { "size": 1, "index": "u32" });
}, "Memory with index parameter omitted");

test(() => {
const argument = { "initial": 1, "index": "u32" };
const memory = new WebAssembly.Memory(argument);
assert_Memory(memory, { "size": 1, "index": "u32" });
}, "Memory with u32 index constructor");

test(() => {
const argument = { "initial": 1, "index": "u64" };
const memory = new WebAssembly.Memory(argument);
assert_Memory(memory, { "size": 1, "index": "u64" });
}, "Memory with u64 index constructor");

test(() => {
assert_throws_js(TypeError, () => new WebAssembly.Memory({ "initial": 1, "index": "none" }));
}, "Unknown memory index");

0 comments on commit 7cfa894

Please # to comment.