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

Commit e841f94

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

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

document/js-api/index.bs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,9 @@ This slot holds a [=function address=] relative to the [=surrounding agent=]'s [
995995
1. Let (|store|, |ret|) be the result of [=func_invoke=](|store|, |funcaddr|, |argsSeq|).
996996
1. Set the [=surrounding agent=]'s [=associated store=] to |store|.
997997
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>.
998+
1. If |ret| is an exception,
999+
1. If the exception is a value that was thrown by JavaScript code called by the Exported Function, throw the exception itself.
1000+
1. Else, throw a {{RuntimeError}} exception.
9981001
1. If |ret| is empty, return undefined.
9991002
1. Otherwise, return [=ToJSValue=](|v|), where |v| is the singular element of |ret|.
10001003
</div>
@@ -1024,7 +1027,9 @@ Note: Exported Functions do not have a \[[Construct]] method and thus it is not
10241027
1. Let |hostfunc| be a [=host function=] which performs the following steps when called with arguments |arguments|:
10251028
1. Let |result| be the result of [=run a host function|running a host function=] from |func|, |functype|, and |arguments|.
10261029
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.
1030+
1. If |result|.\[[Type]] is <emu-const>throw</emu-const>, then:
1031+
1. Let |exception| be [=ToWebAssemblyValue=](|result|.\[[Value]], [=anyref=]).
1032+
1. [=WebAssembly/Throw=] |exception| with the [=JavaScript exception tag=].
10281033
1. Otherwise, return |result|.\[[Value]].
10291034
1. Let |store| be the [=surrounding agent=]'s [=associated store=].
10301035
1. Let (|store|, |funcaddr|) be [=func_alloc=](|store|, |functype|, |hostfunc|).
@@ -1096,6 +1101,22 @@ The algorithm <dfn>ToWebAssemblyValue</dfn>(|v|, |type|, |error|) coerces a Java
10961101
</div>
10971102

10981103

1104+
<h3 id="exceptions">Exceptions</h3>
1105+
1106+
<div algorithm>
1107+
1108+
To <dfn for=WebAssembly>throw</dfn> a WebAssembly value |payload| given an exception tag |tag|,
1109+
perform the following steps:
1110+
1111+
1. Let |exception| be a new [=exnref=] value with |payload| and |tag|.
1112+
1. Unwind the stack until reaching the *catching try block*.
1113+
1. Push |exception| onto the operand stack.
1114+
1. Transfer control to the catch block.
1115+
1116+
Note: This algorithm is expected to be moved into the core specification.
1117+
1118+
</div>
1119+
10991120
<h3 id="error-objects">Error Objects</h3>
11001121

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

0 commit comments

Comments
 (0)