@@ -45,50 +45,43 @@ instruction. Thrown exceptions are handled as follows:
45
45
1 . If the call stack is exhausted without any enclosing try blocks, the embedder
46
46
defines how to handle the uncaught exception.
47
47
48
- ### Event handling
48
+ ### Exception handling
49
49
50
50
This proposal adds exception handling to WebAssembly. Part of this proposal is
51
51
to define a new section to declare exceptions. However, rather than limiting
52
52
this new section to just defining exceptions, it defines a more general format
53
- that allows the declaration of other forms of events .
53
+ ` tag ` that allows the declaration of other forms of typed tags in future .
54
54
55
- In general, an event handler allows one to process an event generated by a block
56
- of code. Events suspend the current execution and look for a corresponding event
57
- handler. If found, the corresponding event handler is run. Some event handlers
58
- may send values back to the suspended instruction, allowing the originating code
59
- to resume.
55
+ WebAssembly tags are defined in a new ` tag ` section of a WebAssembly module. The
56
+ tag section is a list of declared tags that are created fresh each time the
57
+ module is instantiated.
60
58
61
- Exceptions are a special case of an event in that they never resume. Similarly,
62
- a ` throw ` instruction is the suspending event of an exception. The catch block
63
- associated with a try block defines how to handle the throw.
64
-
65
- WebAssembly events (i.e. exceptions) are defined by a new ` event ` section of a
66
- WebAssembly module. The event section is a list of declared events associated
67
- with the module.
68
-
69
- Each event has an ` attribute ` and a ` type ` . Currently, the attribute can only
70
- specify that the event is an exception. In the future, additional attribute
71
- values may be added when other events are added to WebAssembly.
59
+ Each tag has an ` attribute ` and a ` type ` . Currently, the attribute can only
60
+ specify that the tag is for an exception. In the future, additional attribute
61
+ values may be added when other kinds of tags are added to WebAssembly.
72
62
73
63
To allow for such a future extension possibility, we reserve a byte in the
74
64
binary format of an exception definition, set to 0 to denote an exception
75
- attribute, but for the moment we won't use the term event in the formal spec .
65
+ attribute.
76
66
77
67
### Exceptions
78
68
79
- An ` exception ` is an internal construct in WebAssembly. WebAssembly exceptions
80
- are defined in the event and import sections of a module.
69
+ An ` exception tag ` is a value to distinguish different exceptions, while an
70
+ ` exception tag index ` is a numeric name to refer to an (imported or defined)
71
+ exception tag within a module (see [ tag index space] ( #tag-index-space ) for
72
+ details). Exception tags are declared in the tag and import sections of a
73
+ module.
81
74
82
- The type of an exception is denoted by an index to a function signature defined
83
- in the ` type ` section. The parameters of the function signature define the list
84
- of values associated with the exception. The result type must be empty .
75
+ An ` exception ` is an internal construct in WebAssembly that represents a runtime
76
+ object that can be thrown. A WebAssembly exception consists of an exception tag
77
+ and its runtime arguments .
85
78
86
- An ` exception tag ` is a value to distinguish different exceptions, while an
87
- ` exception index ` is a numeric name to refer to an (imported or defined)
88
- exception tag within a module (see [ exception index
89
- space ] ( #exception-index-space ) for details) .
79
+ The type of an exception tag is denoted by an index to a function signature
80
+ defined in the ` type ` section. The parameters of the function signature define
81
+ the list of argument values associated with the tag. The result type must be
82
+ empty .
90
83
91
- Exception indices are used by:
84
+ Exception tag indices are used by:
92
85
93
86
1 . The ` throw ` instruction which creates a WebAssembly exception with the
94
87
corresponding exception tag, and then throws it.
@@ -138,9 +131,9 @@ are considered _tagged_ catch blocks.
138
131
139
132
The last catching instruction of a try-catch block can be the ` catch_all `
140
133
instruction. If it begins with the ` catch_all ` instruction, it defines the
141
- _ default_ catch block. The default catch block has no exception type , and is
142
- used to catch all exceptions not caught by any of the tagged catch blocks. The
143
- term 'catch block' refers to both ` catch ` and ` catch_all ` blocks.
134
+ _ default_ catch block. The default catch block has no tag index , and is used to
135
+ catch all exceptions not caught by any of the tagged catch blocks. The term
136
+ 'catch block' refers to both ` catch ` and ` catch_all ` blocks.
144
137
145
138
When the program runs ` br ` within ` catch ` or ` catch_all ` blocks, the rest of
146
139
the catching blocks will not run and the program control will branch to the
@@ -154,13 +147,13 @@ targets for branches (`br` and `br_if`) as well.
154
147
155
148
### Throwing an exception
156
149
157
- The ` throw ` instruction takes an exception index as an immediate argument. That
158
- index is used to identify the exception tag to use to create and throw the
150
+ The ` throw ` instruction takes an exception tag index as an immediate argument.
151
+ That index is used to identify the exception tag to use to create and throw the
159
152
corresponding exception.
160
153
161
154
The values on top of the stack must correspond to the type associated with the
162
- exception. These values are popped off the stack and are used (along with the
163
- corresponding exception tag) to create the corresponding exception. That
155
+ exception tag . These values are popped off the stack and are used (along with
156
+ the corresponding exception tag) to create the corresponding exception. That
164
157
exception is then thrown.
165
158
166
159
When an exception is thrown, the embedder searches for the nearest enclosing try
@@ -187,7 +180,7 @@ after possible block parameters were popped.
187
180
Then in case of a try-catch block, tagged catch blocks are tried in the order
188
181
they appear in the catching try block, until one matches. If a matched tagged
189
182
catch block is found, control is transferred to the body of the catch block, and
190
- the data fields of the exception are pushed back onto the stack.
183
+ the arguments of the exception are pushed back onto the stack.
191
184
192
185
Otherwise, control is transferred to the body of the ` catch_all ` block, if any.
193
186
However, unlike tagged catch blocks, the constructor arguments are not copied
@@ -358,38 +351,36 @@ document](https://github.com/WebAssembly/spec/blob/master/document/core/text/ins
358
351
The following rules are added to * instructions* :
359
352
360
353
```
361
- try blocktype instruction* (catch instruction*)* (catch_all instruction*)? end |
354
+ try blocktype instruction* (catch tag_index instruction*)* (catch_all instruction*)? end |
362
355
try blocktype instruction* delegate label |
363
- throw (exception except_index) |
356
+ throw tag_index argument* |
364
357
rethrow label |
365
358
```
366
359
367
360
Like the ` block ` , ` loop ` , and ` if ` instructions, the ` try ` instruction is
368
361
* structured* control flow instruction, and can be labeled. This allows branch
369
362
instructions to exit try blocks.
370
363
371
- The ` except_index ` of the ` throw ` and ` catch ` instructions defines the exception
372
- (and hence, exception tag) to create /extract from. See [ exception index
373
- space] ( #exception -index-space ) for further clarification of exception tags.
364
+ The ` tag_index ` of the ` throw ` and ` catch ` instructions denotes the exception
365
+ tag to use when creating /extract from an exception . See [ tag index
366
+ space] ( #tag -index-space ) for further clarification of exception tags.
374
367
375
368
## Changes to Modules document
376
369
377
370
This section describes change in the [ Modules
378
371
document] ( https://github.com/WebAssembly/design/blob/master/Modules.md ) .
379
372
380
- ### Exception index space
373
+ ### Tag index space
381
374
382
- The ` exception index space ` indexes all imported and internally-defined
383
- exceptions, assigning monotonically-increasing indices based on the order
384
- defined in the import and exception sections. Thus, the index space starts at
385
- zero with imported exceptions, followed by internally-defined exceptions in the
386
- [ exception section] ( #exception-section ) .
375
+ The ` tag index space ` indexes all imported and internally-defined exception
376
+ tags, assigning monotonically-increasing indices based on the order defined in
377
+ the import and tag sections. Thus, the index space starts at zero with imported
378
+ tags, followed by internally-defined tags in the [ tag section] ( #tag-section ) .
387
379
388
- The exception index space defines the (module) static version of runtime
389
- exception tags. For exception indices that are not imported/exported, the
390
- corresponding exception tag is guaranteed to be unique over all loaded modules.
391
- Exceptions that are imported or exported alias the respective exceptions defined
392
- elsewhere, and use the same tag.
380
+ For tag indices that are not imported/exported, the corresponding exception tag
381
+ is guaranteed to be unique over all loaded modules. Exceptions that are imported
382
+ or exported alias the respective exceptions defined elsewhere, and use the same
383
+ tag.
393
384
394
385
## Changes to the binary model
395
386
@@ -398,48 +389,39 @@ document](https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md).
398
389
399
390
#### Other Types
400
391
401
- ##### exception_type
392
+ ##### tag_type
402
393
403
394
We reserve a bit to denote the exception attribute:
404
395
405
396
| Name | Value |
406
397
| -----------| -------|
407
398
| Exception | 0 |
408
399
409
- Each exception type has the fields:
400
+ Each tag type has the fields:
410
401
411
402
| Field | Type | Description |
412
403
| -------| ------| -------------|
413
- | ` attribute ` | ` varuint32 ` | The attribute of an exception . |
404
+ | ` attribute ` | ` varuint32 ` | The attribute of a tag . |
414
405
| ` type ` | ` varuint32 ` | The type index for its corresponding type signature |
415
406
416
407
##### external_kind
417
408
418
409
A single-byte unsigned integer indicating the kind of definition being imported
419
410
or defined:
420
411
421
- * ` 0 ` indicating a ` Function ` [ import] ( Modules.md#imports ) or
422
- [ definition] ( Modules.md#function-and-code-sections )
423
- * ` 1 ` indicating a ` Table ` [ import] ( Modules.md#imports ) or
424
- [ definition] ( Modules.md#table-section )
425
- * ` 2 ` indicating a ` Memory ` [ import] ( Modules.md#imports ) or
426
- [ definition] ( Modules.md#linear-memory-section )
427
- * ` 3 ` indicating a ` Global ` [ import] ( Modules.md#imports ) or
428
- [ definition] ( Modules.md#global-section )
429
- * ` 4 ` indicating an ` Event ` [ import] ( #import-section ) or
430
- [ definition] ( #event-section )
412
+ * ` 4 ` indicating a ` Tag `
413
+ [ import] ( https://github.com/WebAssembly/design/blob/main/BinaryEncoding.md#import-section ) or
414
+ [ definition] ( #tag-section )
431
415
432
416
### Module structure
433
417
434
418
#### High-level structure
435
419
436
- A new ` event ` section is introduced and is named ` event ` . If included, it must
437
- appear immediately after the memory section.
420
+ A new ` tag ` section is introduced.
438
421
439
- ##### Exception section
422
+ ##### Tag section
440
423
441
- The ` event ` section is the named section 'exception'. For ease of validation,
442
- this section comes after the [ memory
424
+ The ` tag ` section comes after the [ memory
443
425
section] ( https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md#memory-section )
444
426
and before the [ global
445
427
section] ( https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md#global-section ) .
@@ -452,42 +434,42 @@ So the list of all sections will be:
452
434
| Function | ` 3 ` | Function declarations |
453
435
| Table | ` 4 ` | Indirect function table and other tables |
454
436
| Memory | ` 5 ` | Memory attributes |
455
- | Event | ` 13 ` | Event declarations |
437
+ | Tag | ` 13 ` | Tag declarations |
456
438
| Global | ` 6 ` | Global declarations |
457
439
| Export | ` 7 ` | Exports |
458
440
| Start | ` 8 ` | Start function declaration |
459
441
| Element | ` 9 ` | Elements section |
460
442
| Code | ` 10 ` | Function bodies (code) |
461
443
| Data | ` 11 ` | Data segments |
462
444
463
- The event section declares a list of event types as follows:
445
+ The tag section declares a list of tag types as follows:
464
446
465
447
| Field | Type | Description |
466
448
| -------| ------| -------------|
467
- | count | ` varuint32 ` | count of the number of event to follow |
468
- | type | ` event_type *` | The definitions of the event types |
449
+ | count | ` varuint32 ` | count of the number of tags to follow |
450
+ | type | ` tag_type *` | The definitions of the tag types |
469
451
470
452
##### Import section
471
453
472
- The import section is extended to include exception definitions by extending an
454
+ The import section is extended to include tag definitions by extending an
473
455
` import_entry ` as follows:
474
456
475
- If the ` kind ` is ` Event ` :
457
+ If the ` kind ` is ` Tag ` :
476
458
477
459
| Field | Type | Description |
478
460
| -------| ------| -------------|
479
- | ` type ` | ` event_type ` | the event being imported |
461
+ | ` type ` | ` tag_type ` | the tag being imported |
480
462
481
463
##### Export section
482
464
483
- The export section is extended to reference exception types by extending an
465
+ The export section is extended to reference tag types by extending an
484
466
` export_entry ` as follows:
485
467
486
- If the ` kind ` is ` Event ` :
468
+ If the ` kind ` is ` Tag ` :
487
469
488
470
| Field | Type | Description |
489
471
| -------| ------| -------------|
490
- | ` index ` | ` varuint32 ` | the index into the corresponding event index space |
472
+ | ` index ` | ` varuint32 ` | the index into the corresponding tag index space |
491
473
492
474
##### Name section
493
475
@@ -498,12 +480,12 @@ follows:
498
480
| --------- | ---- | ----------- |
499
481
| [ Function] ( #function-names ) | ` 1 ` | Assigns names to functions |
500
482
| [ Local] ( #local-names ) | ` 2 ` | Assigns names to locals in functions |
501
- | [ Event ] ( #event -names ) | ` 3 ` | Assigns names to event types |
483
+ | [ Tag ] ( #tag -names ) | ` 3 ` | Assigns names to tag types |
502
484
503
- ###### Event names
485
+ ###### Tag names
504
486
505
- The event names subsection is a ` name_map ` which assigns names to a subset of
506
- the exception indices (Used for both imports and module-defined).
487
+ The tag names subsection is a ` name_map ` which assigns names to a subset of
488
+ the tag indices (Used for both imports and module-defined).
507
489
508
490
### Control flow operators
509
491
@@ -516,7 +498,7 @@ throws, and rethrows as follows:
516
498
| ` catch ` | ` 0x07 ` | index : ` varint32 ` | begins the catch block of the try block |
517
499
| ` catch_all ` | ` 0x19 ` | | begins the catch_all block of the try block |
518
500
| ` delegate ` | ` 0x18 ` | relative_depth : ` varuint32 ` | begins the delegate block of the try block |
519
- | ` throw ` | ` 0x08 ` | index : ` varint32 ` | Creates an exception defined by the exception ` index ` and then throws it |
501
+ | ` throw ` | ` 0x08 ` | index : ` varint32 ` | Creates an exception defined by the tag and then throws it |
520
502
| ` rethrow ` | ` 0x09 ` | relative_depth : ` varuint32 ` | Pops the ` exnref ` on top of the stack and throws it |
521
503
522
504
The * sig* fields of ` block ` , ` if ` , and ` try ` operators are block signatures
0 commit comments