Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Add index constructor argument for WebAssembly.Table #64

Merged
merged 2 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions document/js-api/index.bs
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,11 @@ enum ImportExportKind {
"global"
};

enum IndexType {
"i32",
"i64",
};

dictionary ModuleExportDescriptor {
required USVString name;
required ImportExportKind kind;
Expand Down Expand Up @@ -584,15 +589,10 @@ Note: The use of this synchronous API is discouraged, as some implementations so
<h3 id="memories">Memories</h3>

<pre class="idl">
enum MemoryIndexType {
"i32",
"i64",
};

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

[LegacyNamespace=WebAssembly, Exposed=*]
Expand Down Expand Up @@ -783,6 +783,7 @@ dictionary TableDescriptor {
required TableKind element;
required [EnforceRange] unsigned long initial;
[EnforceRange] unsigned long maximum;
IndexType index;
};

[LegacyNamespace=WebAssembly, Exposed=*]
Expand Down Expand Up @@ -829,7 +830,8 @@ Each {{Table}} object has a \[[Table]] internal slot, which is a [=table address
1. Let |ref| be [=DefaultValue=](|elementType|).
1. Otherwise,
1. Let |ref| be [=?=] [=ToWebAssemblyValue=](|value|, |elementType|).
1. Let |type| be the [=table type=] {[=table type|min=] |initial|, [=table type|max=] |maximum|} |elementType|.
1. If |descriptior|["index"] [=map/exists=], let |index| be |descriptor|["index"]; otherwise, let |index| be "i32".
1. Let |type| be the [=table type=] [=table type|index=] |index| {[=table type|min=] |initial|, [=table type|max=] |maximum|} [=table type|refType=] |elementType|.
1. Let |store| be the [=surrounding agent=]'s [=associated store=].
1. Let (|store|, |tableaddr|) be [=table_alloc=](|store|, |type|, |ref|). <!-- TODO(littledan): Report allocation failure https://github.com/WebAssembly/spec/issues/584 -->
1. Set the [=surrounding agent=]'s [=associated store=] to |store|.
Expand Down
33 changes: 33 additions & 0 deletions test/js-api/table/constructor.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@ test(() => {
},
};
},

get index() {
order.push("index");
return {
valueOf() {
order.push("index valueOf");
return "i32";
},
};
},
});

assert_array_equals(order, [
Expand All @@ -166,6 +176,8 @@ test(() => {
"initial valueOf",
"maximum",
"maximum valueOf",
"index",
"index valueOf",
]);
}, "Order of evaluation for descriptor");

Expand Down Expand Up @@ -206,3 +218,24 @@ test(() => {
assert_throws_js(TypeError, () => new WebAssembly.Table(argument, "cannot be used as a wasm function"));
assert_throws_js(TypeError, () => new WebAssembly.Table(argument, 37));
}, "initialize anyfunc table with a bad default value");

test(() => {
const argument = { "element": "i32", "initial": 3, "index": "i32" };
const table = new WebAssembly.Table(argument);
// Once this is merged with the type reflection proposal we should check the
// index type of `table`.
assert_equals(table.length, 3);
}, "Table with i32 index constructor");

test(() => {
const argument = { "element": "i32", "initial": 3, "index": "i64" };
const table = new WebAssembly.Table(argument);
// Once this is merged with the type reflection proposal we should check the
// index type of `table`.
assert_equals(table.length, 3);
}, "Table with i64 index constructor");

test(() => {
const argument = { "element": "i32", "initial": 3, "index": "unknown" };
assert_throws_js(TypeError, () => new WebAssembly.Table(argument));
}, "Unknown table index");
Loading