Skip to content

Commit 5aa690d

Browse files
authored
Replace 'event' with 'tag' in explainer (#161)
It was suggested to change the term 'event' to 'tag' to reference the section name and the entries within the section. The suggested reasons were they can be used something other than events in future, and the term event can be confusing with other concepts on the web.
1 parent f489a73 commit 5aa690d

File tree

1 file changed

+68
-86
lines changed

1 file changed

+68
-86
lines changed

proposals/exception-handling/Exceptions.md

+68-86
Original file line numberDiff line numberDiff line change
@@ -45,50 +45,43 @@ instruction. Thrown exceptions are handled as follows:
4545
1. If the call stack is exhausted without any enclosing try blocks, the embedder
4646
defines how to handle the uncaught exception.
4747

48-
### Event handling
48+
### Exception handling
4949

5050
This proposal adds exception handling to WebAssembly. Part of this proposal is
5151
to define a new section to declare exceptions. However, rather than limiting
5252
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.
5454

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.
6058

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.
7262

7363
To allow for such a future extension possibility, we reserve a byte in the
7464
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.
7666

7767
### Exceptions
7868

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.
8174

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.
8578

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.
9083

91-
Exception indices are used by:
84+
Exception tag indices are used by:
9285

9386
1. The `throw` instruction which creates a WebAssembly exception with the
9487
corresponding exception tag, and then throws it.
@@ -138,9 +131,9 @@ are considered _tagged_ catch blocks.
138131

139132
The last catching instruction of a try-catch block can be the `catch_all`
140133
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.
144137

145138
When the program runs `br` within `catch` or `catch_all` blocks, the rest of
146139
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.
154147

155148
### Throwing an exception
156149

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
159152
corresponding exception.
160153

161154
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
164157
exception is then thrown.
165158

166159
When an exception is thrown, the embedder searches for the nearest enclosing try
@@ -187,7 +180,7 @@ after possible block parameters were popped.
187180
Then in case of a try-catch block, tagged catch blocks are tried in the order
188181
they appear in the catching try block, until one matches. If a matched tagged
189182
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.
191184

192185
Otherwise, control is transferred to the body of the `catch_all` block, if any.
193186
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
358351
The following rules are added to *instructions*:
359352

360353
```
361-
try blocktype instruction* (catch instruction*)* (catch_all instruction*)? end |
354+
try blocktype instruction* (catch tag_index instruction*)* (catch_all instruction*)? end |
362355
try blocktype instruction* delegate label |
363-
throw (exception except_index) |
356+
throw tag_index argument* |
364357
rethrow label |
365358
```
366359

367360
Like the `block`, `loop`, and `if` instructions, the `try` instruction is
368361
*structured* control flow instruction, and can be labeled. This allows branch
369362
instructions to exit try blocks.
370363

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.
374367

375368
## Changes to Modules document
376369

377370
This section describes change in the [Modules
378371
document](https://github.com/WebAssembly/design/blob/master/Modules.md).
379372

380-
### Exception index space
373+
### Tag index space
381374

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).
387379

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.
393384

394385
## Changes to the binary model
395386

@@ -398,48 +389,39 @@ document](https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md).
398389

399390
#### Other Types
400391

401-
##### exception_type
392+
##### tag_type
402393

403394
We reserve a bit to denote the exception attribute:
404395

405396
| Name | Value |
406397
|-----------|-------|
407398
| Exception | 0 |
408399

409-
Each exception type has the fields:
400+
Each tag type has the fields:
410401

411402
| Field | Type | Description |
412403
|-------|------|-------------|
413-
| `attribute` | `varuint32` | The attribute of an exception. |
404+
| `attribute` | `varuint32` | The attribute of a tag. |
414405
| `type` | `varuint32` | The type index for its corresponding type signature |
415406

416407
##### external_kind
417408

418409
A single-byte unsigned integer indicating the kind of definition being imported
419410
or defined:
420411

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)
431415

432416
### Module structure
433417

434418
#### High-level structure
435419

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.
438421

439-
##### Exception section
422+
##### Tag section
440423

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
443425
section](https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md#memory-section)
444426
and before the [global
445427
section](https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md#global-section).
@@ -452,42 +434,42 @@ So the list of all sections will be:
452434
| Function | `3` | Function declarations |
453435
| Table | `4` | Indirect function table and other tables |
454436
| Memory | `5` | Memory attributes |
455-
| Event | `13` | Event declarations |
437+
| Tag | `13` | Tag declarations |
456438
| Global | `6` | Global declarations |
457439
| Export | `7` | Exports |
458440
| Start | `8` | Start function declaration |
459441
| Element | `9` | Elements section |
460442
| Code | `10` | Function bodies (code) |
461443
| Data | `11` | Data segments |
462444

463-
The event section declares a list of event types as follows:
445+
The tag section declares a list of tag types as follows:
464446

465447
| Field | Type | Description |
466448
|-------|------|-------------|
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 |
469451

470452
##### Import section
471453

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
473455
`import_entry` as follows:
474456

475-
If the `kind` is `Event`:
457+
If the `kind` is `Tag`:
476458

477459
| Field | Type | Description |
478460
|-------|------|-------------|
479-
| `type` | `event_type` | the event being imported |
461+
| `type` | `tag_type` | the tag being imported |
480462

481463
##### Export section
482464

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
484466
`export_entry` as follows:
485467

486-
If the `kind` is `Event`:
468+
If the `kind` is `Tag`:
487469

488470
| Field | Type | Description |
489471
|-------|------|-------------|
490-
| `index` | `varuint32` | the index into the corresponding event index space |
472+
| `index` | `varuint32` | the index into the corresponding tag index space |
491473

492474
##### Name section
493475

@@ -498,12 +480,12 @@ follows:
498480
| --------- | ---- | ----------- |
499481
| [Function](#function-names) | `1` | Assigns names to functions |
500482
| [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 |
502484

503-
###### Event names
485+
###### Tag names
504486

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).
507489

508490
### Control flow operators
509491

@@ -516,7 +498,7 @@ throws, and rethrows as follows:
516498
| `catch` | `0x07` | index : `varint32` | begins the catch block of the try block |
517499
| `catch_all` | `0x19` | | begins the catch_all block of the try block |
518500
| `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 |
520502
| `rethrow` | `0x09` | relative_depth : `varuint32` | Pops the `exnref` on top of the stack and throws it |
521503

522504
The *sig* fields of `block`, `if`, and `try` operators are block signatures

0 commit comments

Comments
 (0)