Skip to content

Commit

Permalink
[js-api] Add support for exceptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ms2ger committed May 22, 2020
1 parent 4d3526b commit e841f94
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion document/js-api/index.bs
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,9 @@ This slot holds a [=function address=] relative to the [=surrounding agent=]'s [
1. Let (|store|, |ret|) be the result of [=func_invoke=](|store|, |funcaddr|, |argsSeq|).
1. Set the [=surrounding agent=]'s [=associated store=] to |store|.
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>.
1. If |ret| is an exception,
1. If the exception is a value that was thrown by JavaScript code called by the Exported Function, throw the exception itself.
1. Else, throw a {{RuntimeError}} exception.
1. If |ret| is empty, return undefined.
1. Otherwise, return [=ToJSValue=](|v|), where |v| is the singular element of |ret|.
</div>
Expand Down Expand Up @@ -1024,7 +1027,9 @@ Note: Exported Functions do not have a \[[Construct]] method and thus it is not
1. Let |hostfunc| be a [=host function=] which performs the following steps when called with arguments |arguments|:
1. Let |result| be the result of [=run a host function|running a host function=] from |func|, |functype|, and |arguments|.
1. Assert: |result|.\[[Type]] is <emu-const>throw</emu-const> or <emu-const>return</emu-const>.
1. If |result|.\[[Type]] is <emu-const>throw</emu-const>, then trigger a WebAssembly trap, and propagate |result|.\[[Value]] to the enclosing JavaScript.
1. If |result|.\[[Type]] is <emu-const>throw</emu-const>, then:
1. Let |exception| be [=ToWebAssemblyValue=](|result|.\[[Value]], [=anyref=]).
1. [=WebAssembly/Throw=] |exception| with the [=JavaScript exception tag=].
1. Otherwise, return |result|.\[[Value]].
1. Let |store| be the [=surrounding agent=]'s [=associated store=].
1. Let (|store|, |funcaddr|) be [=func_alloc=](|store|, |functype|, |hostfunc|).
Expand Down Expand Up @@ -1096,6 +1101,22 @@ The algorithm <dfn>ToWebAssemblyValue</dfn>(|v|, |type|, |error|) coerces a Java
</div>


<h3 id="exceptions">Exceptions</h3>

<div algorithm>

To <dfn for=WebAssembly>throw</dfn> a WebAssembly value |payload| given an exception tag |tag|,
perform the following steps:

1. Let |exception| be a new [=exnref=] value with |payload| and |tag|.
1. Unwind the stack until reaching the *catching try block*.
1. Push |exception| onto the operand stack.
1. Transfer control to the catch block.

Note: This algorithm is expected to be moved into the core specification.

</div>

<h3 id="error-objects">Error Objects</h3>

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

0 comments on commit e841f94

Please sign in to comment.