You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: proposals/exception-handling/Exceptions.md
+61-57Lines changed: 61 additions & 57 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -36,8 +36,8 @@ succeeding instructions to process the data.
36
36
A WebAssembly exception is created when you throw it with the `throw`
37
37
instruction. Thrown exceptions are handled as follows:
38
38
39
-
1. They can be caught by one of the *catch clauses* in an enclosing try
40
-
block of a function body.
39
+
1. They can be caught by one of the *catch clauses* in an enclosing try block of
40
+
a function body.
41
41
42
42
1. Throws not caught within a function body continue up the call stack, popping
43
43
call frames, until an enclosing try block is found.
@@ -86,21 +86,22 @@ Exception tag indices are used by:
86
86
1. The `throw` instruction which creates a WebAssembly exception with the
87
87
corresponding exception tag, and then throws it.
88
88
89
-
2. Catch clauses use a tag to identify the thrown exception it
90
-
can catch. If it matches, it pushes the corresponding argument values of the
91
-
exception onto the stack.
89
+
2. Catch clauses use a tag to identify the thrown exception it can catch. If it
90
+
matches, it pushes the corresponding argument values of the exception onto
91
+
the stack.
92
92
93
93
### Exception references
94
94
95
-
When caught, an exception is reified into an _exception reference_, a value of the new type `exnref`.
96
-
Exception references can be used to rethrow the caught exception.
95
+
When caught, an exception is reified into an _exception reference_, a value of
96
+
the new type `exnref`. Exception references can be used to rethrow the caught
97
+
exception.
97
98
98
99
### Try blocks
99
100
100
101
A _try block_ defines a list of instructions that may need to process exceptions
101
102
and/or clean up state when an exception is thrown. Like other higher-level
102
-
constructs, a try block begins with a `try_table` instruction, and ends with an`end`
103
-
instruction. That is, a try block is sequence of instructions having the
103
+
constructs, a try block begins with a `try_table` instruction, and ends with an
104
+
`end`instruction. That is, a try block is sequence of instructions having the
104
105
following form:
105
106
106
107
```
@@ -109,10 +110,11 @@ try_table blocktype catch*
109
110
end
110
111
```
111
112
112
-
A try block contains zero or more _catch clauses_. If there are no catch clauses, then the try block does not catch any exceptions.
113
+
A try block contains zero or more _catch clauses_. If there are no catch
114
+
clauses, then the try block does not catch any exceptions.
113
115
114
-
The _body_ of the try block is the list of instructions after the last
115
-
catch clause, if any.
116
+
The _body_ of the try block is the list of instructions after the last catch
117
+
clause, if any.
116
118
117
119
Each `catch` clause can be in one of 4 forms:
118
120
```
@@ -121,16 +123,16 @@ catch_ref tag label
121
123
catch_all label
122
124
catch_all_ref label
123
125
```
124
-
All forms have a label which is branched to when an exception is cought (see below).
125
-
The former two forms have an exception tag associated with it that
126
-
identifies what exceptions it will catch.
127
-
The latter two forms catch any exception, so that they can be used to define a _default_ handler.
126
+
All forms have a label which is branched to when an exception is cought (see
127
+
below). The former two forms have an exception tag associated with it that
128
+
identifies what exceptions it will catch. The latter two forms catch any
129
+
exception, so that they can be used to define a _default_ handler.
128
130
129
131
Try blocks, like control-flow blocks, have a _block type_. The block type of a
130
132
try block defines the values yielded by evaluating the try block when either no
131
-
exception is thrown, or the exception is successfully caught by the catch clause.
132
-
Because `try_table` defines a control-flow block, it can be
133
-
targets for branches (`br` and `br_if`) as well.
133
+
exception is thrown, or the exception is successfully caught by the catch
134
+
clause. Because `try_table` defines a control-flow block, it can be targets for
135
+
branches (`br` and `br_if`) as well.
134
136
135
137
### Throwing an exception
136
138
@@ -160,37 +162,38 @@ Once a catching try block is found for the thrown exception, the operand stack
160
162
is popped back to the size the operand stack had when the try block was entered
161
163
after possible block parameters were popped.
162
164
163
-
Then catch clauses are tried in the order
164
-
they appear in the catching try block, until one matches. If a matching catch clause is found, control is transferred to the label of that catch clause.
165
-
In case of `catch` or `catch_ref`,
166
-
the arguments of the exception are pushed back onto the stack.
167
-
For `catch_ref` and `catch_all_ref`, an exception reference is then pushed to the stack, which represents the caught exception.
165
+
Then catch clauses are tried in the order they appear in the catching try block,
166
+
until one matches. If a matching catch clause is found, control is transferred
167
+
to the label of that catch clause. In case of `catch` or `catch_ref`, the
168
+
arguments of the exception are pushed back onto the stack. For `catch_ref` and
169
+
`catch_all_ref`, an exception reference is then pushed to the stack, which
170
+
represents the caught exception.
168
171
169
172
If no catch clauses were matched, the exception is implicitly rethrown.
170
173
171
-
Note that a caught exception can be rethrown explicitly using the `exnref` and the `throw_ref` instruction.
174
+
Note that a caught exception can be rethrown explicitly using the `exnref` and
175
+
the `throw_ref` instruction.
172
176
173
177
### Rethrowing an exception
174
178
175
-
The `throw_ref` takes an operand of type `exnref` and re-throws the corresponding caught exception.
176
-
If the operand is null, a trap occurs.
179
+
The `throw_ref` takes an operand of type `exnref` and re-throws the
180
+
corresponding caught exception. If the operand is null, a trap occurs.
177
181
178
182
### JS API
179
183
180
184
#### Traps
181
185
182
-
Catch clauses handle exceptions generated by the `throw`
183
-
instruction, but do not catch traps. The rationale for this is that in general
184
-
traps are not locally recoverable and are not needed to be handled in local
185
-
scopes like try blocks.
186
+
Catch clauses handle exceptions generated by the `throw` instruction, but do not
187
+
catch traps. The rationale for this is that in general traps are not locally
188
+
recoverable and are not needed to be handled in local scopes like try blocks.
186
189
187
190
The `try_table` instruction catches foreign exceptions generated from calls to
188
191
function imports as well, including JavaScript exceptions, with a few
189
192
exceptions:
190
193
1. In order to be consistent before and after a trap reaches a JavaScript frame,
191
194
the `try_table` instruction does not catch exceptions generated from traps.
192
-
1. The `try_table` instruction does not catch JavaScript exceptions generated from
193
-
stack overflow and out of memory.
195
+
1. The `try_table` instruction does not catch JavaScript exceptions generated
196
+
from stack overflow and out of memory.
194
197
195
198
Filtering these exceptions should be based on a predicate that is not observable
196
199
by JavaScript. Traps currently generate instances of
@@ -232,22 +235,22 @@ check ensures that without access to a WebAssembly module's exported exception
232
235
tag, the associated data fields cannot be read.
233
236
234
237
The `Exception` constructor can take an optional `ExceptionOptions` argument,
235
-
which can optionally contain `traceStack` entry. When `traceStack` is
236
-
`true`, JavaScript VMs are permitted to attach a stack trace string to
237
-
`Exception.stack`field, as in JavaScript's `Error` class. `traceStack`
238
-
serves as a request to the WebAssembly engine to attach a stack trace; it
239
-
is not necessary to honour if `true`, but `trace` may not be populated if
240
-
`traceStack` is `false`. While `Exception` is not a subclass of JavaScript's
241
-
`Error` and it can be used to represent normal control flow constructs,
242
-
`traceStack` field can be set when we use it to represent errors. The
243
-
format of stack trace strings conform to the [WebAssembly stack trace
238
+
which can optionally contain `traceStack` entry. When `traceStack` is`true`,
239
+
JavaScript VMs are permitted to attach a stack trace string to`Exception.stack`
240
+
field, as in JavaScript's `Error` class. `traceStack` serves as a request to the
241
+
WebAssembly engine to attach a stack trace; it is not necessary to honour if
242
+
`true`, but `trace` may not be populated if`traceStack` is `false`. While
243
+
`Exception` is not a subclass of JavaScript's`Error` and it can be used to
244
+
represent normal control flow constructs, `traceStack` field can be set when we
245
+
use it to represent errors. The format of stack trace strings conform to the
0 commit comments