Skip to content

Commit 2028231

Browse files
authored
[js-api] Extend with reference types support. (WebAssembly#79)
* [js-api] Remove spurious argument to ToWebAssemblyValue(). Fixes WebAssembly#51. * [js-api] Extend the WebAssembly.Table API. Fixes WebAssembly#22. Fixes WebAssembly#67.
1 parent 7cfa199 commit 2028231

File tree

1 file changed

+40
-21
lines changed

1 file changed

+40
-21
lines changed

document/js-api/index.bs

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -722,10 +722,10 @@ dictionary TableDescriptor {
722722

723723
[LegacyNamespace=WebAssembly, Exposed=(Window,Worker,Worklet)]
724724
interface Table {
725-
constructor(TableDescriptor descriptor);
726-
unsigned long grow([EnforceRange] unsigned long delta);
727-
Function? get([EnforceRange] unsigned long index);
728-
void set([EnforceRange] unsigned long index, Function? value);
725+
constructor(TableDescriptor descriptor, optional any value);
726+
unsigned long grow([EnforceRange] unsigned long delta, optional any value);
727+
any get([EnforceRange] unsigned long index);
728+
void set([EnforceRange] unsigned long index, optional any value);
729729
readonly attribute unsigned long length;
730730
};
731731
</pre>
@@ -736,7 +736,7 @@ which can be simultaneously referenced by multiple {{Instance}} objects. Each
736736
{{Table}} object has the following internal slots:
737737

738738
* \[[Table]] : a [=table address=]
739-
* \[[Values]] : a [=list=] whose elements are either null or [=Exported Function=]s.
739+
* \[[Values]] : a [=list=] whose elements depend on the element type of \[[Table]]'s [=table type=].
740740
</div>
741741

742742
<div algorithm>
@@ -761,29 +761,41 @@ which can be simultaneously referenced by multiple {{Instance}} objects. Each
761761
</div>
762762

763763
<div algorithm>
764-
The <dfn constructor for="Table">Table(|descriptor|)</dfn> constructor, when invoked, performs the following steps:
764+
The <dfn constructor for="Table">Table(|descriptor|, |value|)</dfn> constructor, when invoked, performs the following steps:
765+
1. Let |elementType| be ToValueType(descriptor|["element"]).
765766
1. let |initial| be |descriptor|["initial"].
766767
1. If |descriptor|["maximum"] is [=present=], let |maximum| be |descriptor|["maximum"]; otherwise, let |maximum| be empty.
767768
1. If |maximum| is not empty and |maximum| &lt; |initial|, throw a {{RangeError}} exception.
768-
1. Let |type| be the [=table type=] {[=table type|𝗆𝗂𝗇=] n, [=table type|𝗆𝖺𝗑=] |maximum|} [=table type|funcref=].
769+
1. If |value| is missing,
770+
1. Let |ref| be [=DefaultValue=](|elementType|).
771+
1. Otherwise,
772+
1. Let |ref| be ? [=ToWebAssemblyValue=](|value|, |elementType|).
773+
1. Let |type| be the [=table type=] {[=table type|𝗆𝗂𝗇=] |initial|, [=table type|𝗆𝖺𝗑=] |maximum|} |elementType|.
769774
1. Let |store| be the [=surrounding agent=]'s [=associated store=].
770-
1. Let (|store|, |tableaddr|) be [=table_alloc=](|store|, |type|, [=ref.null=]). <!-- TODO(littledan): Report allocation failure https://github.com/WebAssembly/spec/issues/584 -->
775+
1. Let (|store|, |tableaddr|) be [=table_alloc=](|store|, |type|, |ref|). <!-- TODO(littledan): Report allocation failure https://github.com/WebAssembly/spec/issues/584 -->
771776
1. Set the [=surrounding agent=]'s [=associated store=] to |store|.
772777
1. [=initialize a table object|Initialize=] **this** from |tableaddr|.
778+
1. [=list/Empty=] **this**.\[[Values]].
779+
1. [=list/Append=] |value| to **this**.\[[Values]] |initial| times.
773780
</div>
774781

775782
<div algorithm=dom-Table-grow>
776-
The <dfn method for="Table">grow(|delta|)</dfn> method, when invoked, performs the following steps:
783+
The <dfn method for="Table">grow(|delta|, |value|)</dfn> method, when invoked, performs the following steps:
777784
1. Let |tableaddr| be **this**.\[[Table]].
778785
1. Let |initialSize| be the length of **this**.\[[Values]].
779786
1. Let |store| be the [=surrounding agent=]'s [=associated store=].
780-
1. Let |result| be [=table_grow=](|store|, |tableaddr|, |delta|, [=ref.null=]).
787+
1. Let (<var ignore>limits</var>, |elementType|) be [=table_type=](|tableaddr|).
788+
1. If |value| is missing,
789+
1. Let |ref| be [=DefaultValue=](|elementType|).
790+
1. Otherwise,
791+
1. Let |ref| be ? [=ToWebAssemblyValue=](|value|, |elementType|).
792+
1. Let |result| be [=table_grow=](|store|, |tableaddr|, |delta|, |ref|).
781793
1. If |result| is [=error=], throw a {{RangeError}} exception.
782794

783795
Note: The above exception may happen due to either insufficient memory or an invalid size parameter.
784796

785797
1. Set the [=surrounding agent=]'s [=associated store=] to |result|.
786-
1. [=list/Append=] null to **this**.\[[Values]] |delta| times.
798+
1. [=list/Append=] |value| to **this**.\[[Values]] |delta| times.
787799
1. Return |initialSize|.
788800
</div>
789801

@@ -803,10 +815,11 @@ which can be simultaneously referenced by multiple {{Instance}} objects. Each
803815
The <dfn method for="Table">set(|index|, |value|)</dfn> method, when invoked, performs the following steps:
804816
1. Let |tableaddr| be **this**.\[[Table]].
805817
1. Let |values| be **this**.\[[Values]].
806-
1. If |value| is null, let |ref| be [=ref.null=].
818+
1. Let (<var ignore>limits</var>, |elementType|) be [=table_type=](|tableaddr|).
819+
1. If |value| is missing,
820+
1. Let |ref| be [=DefaultValue=](|elementType|).
807821
1. Otherwise,
808-
1. If |value| does not have a \[[FunctionAddress]] internal slot, throw a {{TypeError}} exception.
809-
1. Let |ref| be [=ref.func=] |value|.\[[FunctionAddress]].
822+
1. Let |ref| be ? [=ToWebAssemblyValue=](|value|, |elementType|).
810823
1. Let |store| be the [=surrounding agent=]'s [=associated store=].
811824
1. Let |store| be [=table_write=](|store|, |tableaddr|, |index|, |ref|).
812825
1. If |store| is [=error=], throw a {{RangeError}} exception.
@@ -822,7 +835,10 @@ enum ValueType {
822835
"i32",
823836
"i64",
824837
"f32",
825-
"f64"
838+
"f64",
839+
"nullref",
840+
"anyref",
841+
"anyfunc",
826842
};
827843
</pre>
828844

@@ -886,14 +902,17 @@ which can be simultaneously referenced by multiple {{Instance}} objects. Each
886902
1. If |valuetype| equals [=𝗂𝟨𝟦=], return [=𝗂𝟨𝟦.𝖼𝗈𝗇𝗌𝗍=] 0.
887903
1. If |valuetype| equals [=𝖿𝟥𝟤=], return [=𝖿𝟥𝟤.𝖼𝗈𝗇𝗌𝗍=] 0.
888904
1. If |valuetype| equals [=𝖿𝟨𝟦=], return [=𝖿𝟨𝟦.𝖼𝗈𝗇𝗌𝗍=] 0.
889-
1. Else, return [=ref.null=].
905+
1. If |valuetype| equals [=nullref=], return [=ref.null=].
906+
1. If |valuetype| equals [=anyref=], return [=ToWebAssemblyValue=](undefined, |valuetype|).
907+
1. If |valuetype| equals [=nullref=], return [=ref.null=].
908+
1. Assert: This step is not reached.
890909
</div>
891910

892911
<div algorithm>
893912
The <dfn constructor for="Global">Global(|descriptor|, |v|)</dfn> constructor, when invoked, performs the following steps:
894913
1. Let |mutable| be |descriptor|["mutable"].
895914
1. Let |valuetype| be [=ToValueType=](|descriptor|["value"]).
896-
1. If |v| is undefined,
915+
1. If |v| is missing,
897916
1. let |value| be [=DefaultValue=](|valuetype|).
898917
1. Otherwise,
899918
1. If |valuetype| is [=𝗂𝟨𝟦=], throw a {{TypeError}} exception.
@@ -993,7 +1012,7 @@ This slot holds a [=function address=] relative to the [=surrounding agent=]'s [
9931012
1. For each type |t| of |parameters|,
9941013
1. If the length of |argValues| &gt; |i|, let |arg| be |argValues|[|i|].
9951014
1. Otherwise, let |arg| be undefined.
996-
1. [=list/Append=] [=ToWebAssemblyValue=](|arg|, |t|, {{TypeError}}) to |args|.
1015+
1. [=list/Append=] [=ToWebAssemblyValue=](|arg|, |t|) to |args|.
9971016
1. Set |i| to |i| + 1.
9981017
1. Let |argsSeq| be a WebAssembly [=sequence=] containing the elements of |args|.
9991018
1. Let (|store|, |ret|) be the result of [=func_invoke=](|store|, |funcaddr|, |argsSeq|).
@@ -1060,7 +1079,7 @@ Note: Number values which are equal to NaN may have various observable NaN paylo
10601079
</div>
10611080

10621081
<div algorithm>
1063-
The algorithm <dfn>ToWebAssemblyValue</dfn>(|v|, |type|, |error|) coerces a JavaScript value to a [=WebAssembly value=] performs the following steps:
1082+
The algorithm <dfn>ToWebAssemblyValue</dfn>(|v|, |type|) coerces a JavaScript value to a [=WebAssembly value=] performs the following steps:
10641083

10651084

10661085
1. Assert: |type| is not [=𝗂𝟨𝟦=].
@@ -1077,9 +1096,9 @@ The algorithm <dfn>ToWebAssemblyValue</dfn>(|v|, |type|, |error|) coerces a Java
10771096
1. If |type| is [=anyref=],
10781097
1. Do nothing.
10791098
1. If |type| is [=funcref=],
1080-
1. If |v| is not an [=Exported function=] or null, throw |error|.
1099+
1. If |v| is not an [=Exported function=] or null, throw a {{TypeError}}.
10811100
1. If |type| is [=nullref=],
1082-
1. If |v| is not null, throw |error|.
1101+
1. If |v| is not null, throw a {{TypeError}}.
10831102
1. Return the result of [=allocating a host address=] for |v|.
10841103

10851104
</div>

0 commit comments

Comments
 (0)