Skip to content

Commit 7cfa894

Browse files
authored
WebAssembly.Memory "index: u32/u64" constructor parameter (#39)
1 parent d1bcd3d commit 7cfa894

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

document/js-api/index.bs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,9 +607,15 @@ interface Instance {
607607
<h3 id="memories">Memories</h3>
608608

609609
<pre class="idl">
610+
enum MemoryIndexType {
611+
"u32",
612+
"u64",
613+
};
614+
610615
dictionary MemoryDescriptor {
611616
required [EnforceRange] unsigned long initial;
612617
[EnforceRange] unsigned long maximum;
618+
MemoryIndexType index;
613619
};
614620

615621
[LegacyNamespace=WebAssembly, Exposed=(Window,Worker,Worklet)]
@@ -662,7 +668,8 @@ which can be simultaneously referenced by multiple {{Instance}} objects. Each
662668
1. Let |initial| be |descriptor|["initial"].
663669
1. If |descriptor|["maximum"] [=map/exists=], let |maximum| be |descriptor|["maximum"]; otherwise, let |maximum| be empty.
664670
1. If |maximum| is not empty and |maximum| &lt; |initial|, throw a {{RangeError}} exception.
665-
1. Let |memtype| be { min |initial|, max |maximum| }.
671+
1. If |descriptior|["index"] [=map/exists=], let |index| be |descriptor|["index"]; otherwise, let |index| be "u32".
672+
1. Let |memtype| be { min |initial|, max |maximum|, index |index| }.
666673
1. Let |store| be the [=surrounding agent=]'s [=associated store=].
667674
1. Let (|store|, |memaddr|) be [=mem_alloc=](|store|, |memtype|). If allocation fails, throw a {{RangeError}} exception.
668675
1. Set the [=surrounding agent=]'s [=associated store=] to |store|.

test/js-api/memory/assertions.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,17 @@ function assert_ArrayBuffer(actual, { size=0, shared=false, detached=false }, me
2727
assert_equals(Object.isExtensible(actual), !shared, "buffer extensibility");
2828
}
2929

30-
function assert_Memory(memory, { size=0, shared=false }) {
30+
function assert_Memory(memory, { size=0, shared=false, index="u32" }) {
3131
assert_equals(Object.getPrototypeOf(memory), WebAssembly.Memory.prototype,
3232
"prototype");
3333
assert_true(Object.isExtensible(memory), "extensible");
3434

3535
// https://github.com/WebAssembly/spec/issues/840
3636
assert_equals(memory.buffer, memory.buffer, "buffer should be idempotent");
3737
assert_ArrayBuffer(memory.buffer, { size, shared });
38+
39+
// this depends on js-types proposal implementation
40+
if (typeof memory.type == "function") {
41+
assert_equals(memory.type().index, index, "memory index");
42+
}
3843
}

test/js-api/memory/constructor.any.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ test(() => {
7272
assert_unreached(`Should not call [[HasProperty]] with ${x}`);
7373
},
7474
get(o, x) {
75+
if (x === "index") {
76+
return "u32";
77+
}
7578
return 0;
7679
},
7780
});
@@ -128,3 +131,25 @@ test(() => {
128131
const memory = new WebAssembly.Memory(argument, {});
129132
assert_Memory(memory, { "size": 0 });
130133
}, "Stray argument");
134+
135+
test(() => {
136+
const argument = { "initial": 1 };
137+
const memory = new WebAssembly.Memory(argument);
138+
assert_Memory(memory, { "size": 1, "index": "u32" });
139+
}, "Memory with index parameter omitted");
140+
141+
test(() => {
142+
const argument = { "initial": 1, "index": "u32" };
143+
const memory = new WebAssembly.Memory(argument);
144+
assert_Memory(memory, { "size": 1, "index": "u32" });
145+
}, "Memory with u32 index constructor");
146+
147+
test(() => {
148+
const argument = { "initial": 1, "index": "u64" };
149+
const memory = new WebAssembly.Memory(argument);
150+
assert_Memory(memory, { "size": 1, "index": "u64" });
151+
}, "Memory with u64 index constructor");
152+
153+
test(() => {
154+
assert_throws_js(TypeError, () => new WebAssembly.Memory({ "initial": 1, "index": "none" }));
155+
}, "Unknown memory index");

0 commit comments

Comments
 (0)