Skip to content
This repository was archived by the owner on Nov 3, 2021. It is now read-only.

Commit a219a03

Browse files
authored
[spec] JS API changes (#8)
1 parent 10c465a commit a219a03

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

document/js-api/index.bs

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ urlPrefix: https://webassembly.github.io/spec/core/; spec: WebAssembly; type: df
100100
text: 𝗂𝟥𝟤.𝖼𝗈𝗇𝗌𝗍
101101
text: 𝖿𝟥𝟤.𝖼𝗈𝗇𝗌𝗍
102102
text: 𝖿𝟨𝟦.𝖼𝗈𝗇𝗌𝗍
103+
text: ref.null
104+
text: ref.func
105+
text: ref.host
103106
text: function index; url: syntax/modules.html#syntax-funcidx
104107
text: function instance; url: exec/runtime.html#function-instances
105108
text: init_store; url: appendix/embedding.html#embed-init-store
@@ -132,11 +135,16 @@ urlPrefix: https://webassembly.github.io/spec/core/; spec: WebAssembly; type: df
132135
text: table address; url: exec/runtime.html#syntax-tableaddr
133136
text: function address; url: exec/runtime.html#syntax-funcaddr
134137
text: memory address; url: exec/runtime.html#syntax-memaddr
135-
url: syntax/types.html#syntax-valtype
138+
text: host address; url: exec/runtime.html#syntax-hostaddr
139+
url: syntax/types.html#syntax-numtype
136140
text: 𝗂𝟥𝟤
137141
text: 𝗂𝟨𝟦
138142
text: 𝖿𝟥𝟤
139143
text: 𝖿𝟨𝟦
144+
url: syntax/types.html#syntax-reftype
145+
text: anyref
146+
text: anyeqref
147+
text: anyfunc
140148
text: function element; url: exec/runtime.html#syntax-funcelem
141149
text: import component; url: syntax/modules.html#imports
142150
text: external value; url: exec/runtime.html#syntax-externval
@@ -223,6 +231,7 @@ Each [=agent=] is associated with the following [=ordered map=]s:
223231
* The <dfn>Memory object cache</dfn>, mapping [=memory address=]es to {{Memory}} objects.
224232
* The <dfn>Table object cache</dfn>, mapping [=table address=]es to {{Table}} objects.
225233
* The <dfn>Exported Function cache</dfn>, mapping [=function address=]es to [=Exported Function=] objects.
234+
* The <dfn>Host value cache</dfn>, mapping [=host address=]es to values.
226235

227236
<h2 id="webassembly-namespace">The WebAssembly Namespace</h2>
228237

@@ -326,7 +335,7 @@ A {{Module}} object represents a single WebAssembly module. Each {{Module}} obje
326335
1. [=Append=] |externfunc| to |imports|.
327336
1. If |externtype| is of the form [=𝗀𝗅𝗈𝖻𝖺𝗅=] |globaltype|,
328337
1. If |globaltype| is [=𝗂𝟨𝟦=] or [=Type=](|v|) is not [=Number=], throw a {{LinkError}} exception.
329-
1. Let |value| be [=ToWebAssemblyValue=](|v|, |globaltype|.<em>[=global type|valtype=]</em>)
338+
1. Let |value| be [=ToWebAssemblyValue=](|v|, |globaltype|.<em>[=global type|valtype=]</em>, {{LinkError}})
330339
1. Assert: |globaltype|.<em>[=global type|mut=]</em> is [=global type|𝖼𝗈𝗇𝗌𝗍=], as verified by WebAssembly validation.
331340
1. Let |store| be the [=surrounding agent=]'s [=associated store=].
332341
1. Let (|store|, |globaladdr|) be [=alloc_global=](|store|, |globaltype|, |value|).
@@ -590,6 +599,8 @@ which can be simultaneously referenced by multiple {{Instance}} objects. Each
590599

591600
<pre class="idl">
592601
enum TableKind {
602+
"anyref",
603+
"anyeqref",
593604
"anyfunc",
594605
// Note: More values may be added in future iterations,
595606
// e.g., typed function references, typed GC references
@@ -742,7 +753,7 @@ This slot holds a [=function address=] relative to the [=surrounding agent=]'s [
742753
1. For each type |t| of |parameters|,
743754
1. If the length of |argValues| &gt; |i|, let |arg| be |argValues|[i].
744755
1. Otherwise, let |arg| be undefined.
745-
1. [=Append=] [=ToWebAssemblyValue=](|arg|, |t|) to |args|.
756+
1. [=Append=] [=ToWebAssemblyValue=](|arg|, |t|, {{TypeError}}) to |args|.
746757
1. Set |i| to |i| + 1.
747758
1. Let (|store|, |ret|) be the result of [=invoke_func=](|store|, |funcaddr|, |args|).
748759
1. Set the [=surrounding agent=]'s [=associated store=] to |store|.
@@ -767,7 +778,7 @@ Note: Exported Functions do not have a \[[Construct]] method and thus it is not
767778
1. Let |ret| be ? [=Call=](|func|, undefined, |jsArguments|). If an exception is thrown, trigger a WebAssembly trap, and propagate the exception to the enclosing JavaScript.
768779
1. Let [|parameters|][|results|] be |functype|.
769780
1. If |results| is empty, return undefined.
770-
1. Otherwise, return [=ToWebAssemblyValue=](|ret|, |results|[0]).
781+
1. Otherwise, return [=ToWebAssemblyValue=](|ret|, |results|[0], {{TypeError}}).
771782
1. Let |store| be the [=surrounding agent=]'s [=associated store=].
772783
1. Let (|store|, |funcaddr|) be [=alloc_func=](|store|, |functype|, |hostfunc|).
773784
1. Set the [=surrounding agent=]'s [=associated store=] to |store|.
@@ -781,14 +792,24 @@ Assert: |w| is not of the form [=𝗂𝟨𝟦.𝖼𝗈𝗇𝗌𝗍=] |i64|.
781792
1. If |w| is of the form [=𝗂𝟥𝟤.𝖼𝗈𝗇𝗌𝗍=] |i32|, return [=the Number value=] for [=signed_32=](|i32|).
782793
1. If |w| is of the form [=𝖿𝟥𝟤.𝖼𝗈𝗇𝗌𝗍=] |f32|, return [=the Number value=] for |f32|.
783794
1. If |w| is of the form [=𝖿𝟨𝟦.𝖼𝗈𝗇𝗌𝗍=] |f64|, return [=the Number value=] for |f64|.
795+
1. If |w| is of the form [=ref.null=], return null.
796+
1. If |w| is of the form [=ref.func=] |funcaddr|, return the result of creating [=a new Exported Function=] from |funcaddr|.
797+
1. If |w| is of the form [=ref.host=] |hostaddr|, return the result of [=retrieving a host value=] from |hostaddr|.
784798

785799
<!-- If the WebAssembly value is optional, then given `None`, return JavaScript value `undefined`. -->
786800

787801
Note: Implementations may optionally replace the NaN payload with any other NaN payload at this point in the f32 or f64 cases; such a change would not be observable through [=NumberToRawBytes=].
788802
</div>
789803

790804
<div algorithm>
791-
The algorithm <dfn>ToWebAssemblyValue</dfn>(|v|, |type|) coerce a JavaScript value to a [=WebAssembly value=] performs the following steps:
805+
For <dfn>retrieving a host value</dfn> from a [=host address=] |hostaddr|, perform the following steps:
806+
1. Let |map| be the [=surrounding agent=]'s associated [=host value cache=].
807+
1. Assert: |map|[|hostaddr|] [=map/exists=].
808+
1. Return |map|[|hostaddr|].
809+
</div>
810+
811+
<div algorithm>
812+
The algorithm <dfn>ToWebAssemblyValue</dfn>(|v|, |type|, |error|) coerces a JavaScript value to a [=WebAssembly value=] performs the following steps:
792813

793814

794815
Assert: |type| is not [=𝗂𝟨𝟦=].
@@ -801,9 +822,34 @@ Assert: |type| is not [=𝗂𝟨𝟦=].
801822
1. If |type| is [=𝖿𝟨𝟦=],
802823
1. Let |f64| be ? [=ToNumber=](|v|).
803824
1. Return [=𝖿𝟨𝟦.𝖼𝗈𝗇𝗌𝗍=] |f64|.
825+
1. If |type| is [=anyref=],
826+
1. If |v| is a primitive value but not a string, symbol, or null, throw |error|.
827+
1. Return the result of [=allocating a host address=] for |v|.
828+
1. If |type| is [=anyeqref=],
829+
1. If |v| is a primitive value but not a symbol or null, throw |error|.
830+
1. Return the result of [=allocating a host address=] for |v|.
831+
1. If |type| is [=anyfunc=],
832+
1. If |v| is not an [=Exported function=] or null, throw |error|.
833+
1. Return the result of [=allocating a host address=] for |v|.
804834

805835
</div>
806836

837+
<div algorithm>
838+
For <dfn>allocating a host address</dfn> for a value |v|, perform the following steps:
839+
1. If |v| is null,
840+
1. Return [=ref.null=].
841+
1. If |v| is an [=Exported Function=],
842+
1. Let |funcaddr| be the value of |v|'s \[[FunctionAddress]] internal slot.
843+
1. Return [=ref.func=] |funcaddr|.
844+
1. Let |map| be the [=surrounding agent=]'s associated [=host value cache=].
845+
1. If a [=host address=] |hostaddr| exists such that |map|[|hostaddr|] is the same as |v|,
846+
1. Return [=ref.host=] |hostaddr|.
847+
1. Let [=host address=] |hostaddr| be the smallest address such that |map|[|hostaddr|] [=map/exists=] is false.
848+
1. [=map/Set=] |map|[|hostaddr|] to |v|.
849+
1. Return [=ref.host=] |hostaddr|.
850+
</div>
851+
852+
807853
<h3 id="error-objects">Error Objects</h3>
808854

809855
WebAssembly defines the following Error classes: {{CompileError}}, {{LinkError}}, and {{RuntimeError}}. WebAssembly errors have the following custom bindings:

proposals/reference-types/Overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ API extensions:
100100
* Any JS object (non-primitive value) or string or symbol or `null` can be passed as `anyref` to a Wasm function, stored in a global, or in a table.
101101
- It may be possible to allow all other non-primitive values as well, depending on details of existing engines.
102102

103-
* Any JS function object or `null` can be passed as `anyfunc` to a Wasm function, stored in a global, or in a table.
103+
* Any Wasm exported function object or `null` can be passed as `anyfunc` to a Wasm function, stored in a global, or in a table.
104104

105105

106106
## Possible Future Extensions

0 commit comments

Comments
 (0)