Skip to content

Commit 23c6751

Browse files
authored
Add index constructor argument for WebAssembly.Table (WebAssembly#64)
Similar to WebAssembly.Memory (WebAssembly#39), the WebAssembly.Table constructor also needs an `index` argument.
1 parent d75e87e commit 23c6751

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

document/js-api/index.bs

+9-7
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,11 @@ enum ImportExportKind {
482482
"global"
483483
};
484484

485+
enum IndexType {
486+
"i32",
487+
"i64",
488+
};
489+
485490
dictionary ModuleExportDescriptor {
486491
required USVString name;
487492
required ImportExportKind kind;
@@ -584,15 +589,10 @@ Note: The use of this synchronous API is discouraged, as some implementations so
584589
<h3 id="memories">Memories</h3>
585590

586591
<pre class="idl">
587-
enum MemoryIndexType {
588-
"i32",
589-
"i64",
590-
};
591-
592592
dictionary MemoryDescriptor {
593593
required [EnforceRange] unsigned long initial;
594594
[EnforceRange] unsigned long maximum;
595-
MemoryIndexType index;
595+
IndexType index;
596596
};
597597

598598
[LegacyNamespace=WebAssembly, Exposed=*]
@@ -783,6 +783,7 @@ dictionary TableDescriptor {
783783
required TableKind element;
784784
required [EnforceRange] unsigned long initial;
785785
[EnforceRange] unsigned long maximum;
786+
IndexType index;
786787
};
787788

788789
[LegacyNamespace=WebAssembly, Exposed=*]
@@ -829,7 +830,8 @@ Each {{Table}} object has a \[[Table]] internal slot, which is a [=table address
829830
1. Let |ref| be [=DefaultValue=](|elementType|).
830831
1. Otherwise,
831832
1. Let |ref| be [=?=] [=ToWebAssemblyValue=](|value|, |elementType|).
832-
1. Let |type| be the [=table type=] {[=table type|min=] |initial|, [=table type|max=] |maximum|} |elementType|.
833+
1. If |descriptior|["index"] [=map/exists=], let |index| be |descriptor|["index"]; otherwise, let |index| be "i32".
834+
1. Let |type| be the [=table type=] [=table type|index=] |index| {[=table type|min=] |initial|, [=table type|max=] |maximum|} [=table type|refType=] |elementType|.
833835
1. Let |store| be the [=surrounding agent=]'s [=associated store=].
834836
1. Let (|store|, |tableaddr|) be [=table_alloc=](|store|, |type|, |ref|). <!-- TODO(littledan): Report allocation failure https://github.com/WebAssembly/spec/issues/584 -->
835837
1. Set the [=surrounding agent=]'s [=associated store=] to |store|.

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

+33
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,16 @@ test(() => {
157157
},
158158
};
159159
},
160+
161+
get index() {
162+
order.push("index");
163+
return {
164+
valueOf() {
165+
order.push("index valueOf");
166+
return "i32";
167+
},
168+
};
169+
},
160170
});
161171

162172
assert_array_equals(order, [
@@ -166,6 +176,8 @@ test(() => {
166176
"initial valueOf",
167177
"maximum",
168178
"maximum valueOf",
179+
"index",
180+
"index valueOf",
169181
]);
170182
}, "Order of evaluation for descriptor");
171183

@@ -206,3 +218,24 @@ test(() => {
206218
assert_throws_js(TypeError, () => new WebAssembly.Table(argument, "cannot be used as a wasm function"));
207219
assert_throws_js(TypeError, () => new WebAssembly.Table(argument, 37));
208220
}, "initialize anyfunc table with a bad default value");
221+
222+
test(() => {
223+
const argument = { "element": "i32", "initial": 3, "index": "i32" };
224+
const table = new WebAssembly.Table(argument);
225+
// Once this is merged with the type reflection proposal we should check the
226+
// index type of `table`.
227+
assert_equals(table.length, 3);
228+
}, "Table with i32 index constructor");
229+
230+
test(() => {
231+
const argument = { "element": "i32", "initial": 3, "index": "i64" };
232+
const table = new WebAssembly.Table(argument);
233+
// Once this is merged with the type reflection proposal we should check the
234+
// index type of `table`.
235+
assert_equals(table.length, 3);
236+
}, "Table with i64 index constructor");
237+
238+
test(() => {
239+
const argument = { "element": "i32", "initial": 3, "index": "unknown" };
240+
assert_throws_js(TypeError, () => new WebAssembly.Table(argument));
241+
}, "Unknown table index");

0 commit comments

Comments
 (0)