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

Commit 789acfa

Browse files
committed
[js-api] Add support for exceptions.
1 parent 4d3526b commit 789acfa

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

document/js-api/index.bs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,12 @@ urlPrefix: https://webassembly.github.io/spec/core/; spec: WebAssembly; type: df
181181
urlPrefix: https://heycam.github.io/webidl/; spec: WebIDL
182182
type: dfn
183183
text: create a namespace object; url: create-a-namespace-object
184+
185+
urlPrefix: https://webassembly.github.io/reference-types/core/; spec: WebAssembly (reference types); type: dfn
186+
url: exec/runtime.html#values
187+
text: ref.extern
188+
urlPrefix: https://webassembly.github.io/reference-types/js-api/; spec: WebAssembly JS API (reference types); type: dfn
189+
text: retrieving an extern value; url: #retrieving-an-extern-value
184190
</pre>
185191

186192
<pre class='link-defaults'>
@@ -727,6 +733,8 @@ interface Table {
727733
};
728734
</pre>
729735

736+
Issue: Should we support putting [=exnref=] values in tables?
737+
730738
<div>
731739
A {{Table}} object represents a single [=table instance=]
732740
which can be simultaneously referenced by multiple {{Instance}} objects. Each
@@ -995,6 +1003,14 @@ This slot holds a [=function address=] relative to the [=surrounding agent=]'s [
9951003
1. Let (|store|, |ret|) be the result of [=func_invoke=](|store|, |funcaddr|, |argsSeq|).
9961004
1. Set the [=surrounding agent=]'s [=associated store=] to |store|.
9971005
1. If |ret| is [=error=], throw an exception. This exception should be a WebAssembly {{RuntimeError}} exception, unless otherwise indicated by <a href="#errors">the WebAssembly error mapping</a>.
1006+
1. If |ret| is exception [=exnref=] |exception|,
1007+
1. If |exception|'s [=exnref/exception tag=] is the [=JavaScript exception tag=], then
1008+
1. Let [=ref.extern=] |externaddr| be |exception|'s [=exnref/payload=].
1009+
1. Let |jsException| be the result of [=retrieving an extern value=] from |externaddr|.
1010+
1. Throw |jsException|.
1011+
1. Throw a {{RuntimeError}} exception.
1012+
1013+
Issue: Probably shouldn't be a {{RuntimeError}}.
9981014
1. If |ret| is empty, return undefined.
9991015
1. Otherwise, return [=ToJSValue=](|v|), where |v| is the singular element of |ret|.
10001016
</div>
@@ -1024,7 +1040,9 @@ Note: Exported Functions do not have a \[[Construct]] method and thus it is not
10241040
1. Let |hostfunc| be a [=host function=] which performs the following steps when called with arguments |arguments|:
10251041
1. Let |result| be the result of [=run a host function|running a host function=] from |func|, |functype|, and |arguments|.
10261042
1. Assert: |result|.\[[Type]] is <emu-const>throw</emu-const> or <emu-const>return</emu-const>.
1027-
1. If |result|.\[[Type]] is <emu-const>throw</emu-const>, then trigger a WebAssembly trap, and propagate |result|.\[[Value]] to the enclosing JavaScript.
1043+
1. If |result|.\[[Type]] is <emu-const>throw</emu-const>, then:
1044+
1. Let |exception| be [=ToWebAssemblyValue=](|result|.\[[Value]], [=anyref=]).
1045+
1. [=WebAssembly/Throw=] |exception| with the [=JavaScript exception tag=].
10281046
1. Otherwise, return |result|.\[[Value]].
10291047
1. Let |store| be the [=surrounding agent=]'s [=associated store=].
10301048
1. Let (|store|, |funcaddr|) be [=func_alloc=](|store|, |functype|, |hostfunc|).
@@ -1096,6 +1114,32 @@ The algorithm <dfn>ToWebAssemblyValue</dfn>(|v|, |type|, |error|) coerces a Java
10961114
</div>
10971115

10981116

1117+
<h3 id="exceptions">Exceptions</h3>
1118+
1119+
A new type <dfn>exnref</dfn> is introduced for references to exceptions.
1120+
Values of this type have an <dfn for=exnref>exception tag</dfn> and a <dfn for=exnref>payload</dfn>
1121+
(whose type is dependent on the value of the [=exnref/exception tag=]).
1122+
The concrete representation of values of this type is undefined.
1123+
1124+
The <dfn>JavaScript exception tag</dfn> is an [=exnref/exception tag=] reserved by this
1125+
specification to distinguish exceptions originating from JavaScript.
1126+
1127+
Issue: Should it be possible for `br_on_exn` to extract the payload from an exception with this tag.
1128+
1129+
<div algorithm>
1130+
1131+
To <dfn for=WebAssembly>throw</dfn> a WebAssembly value |payload| given an [=exception tag=] |tag|,
1132+
perform the following steps:
1133+
1134+
1. Let |exception| be a new [=exnref=] value with |payload| and |tag|.
1135+
1. Unwind the stack until reaching the *catching try block*.
1136+
1. Push |exception| onto the operand stack.
1137+
1. Transfer control to the catch block.
1138+
1139+
Note: This algorithm is expected to be moved into the core specification.
1140+
1141+
</div>
1142+
10991143
<h3 id="error-objects">Error Objects</h3>
11001144

11011145
WebAssembly defines the following Error classes: <dfn exception>CompileError</dfn>, <dfn exception>LinkError</dfn>, and <dfn exception>RuntimeError</dfn>.

0 commit comments

Comments
 (0)