Skip to content

Commit e9c8b16

Browse files
authored
Merge pull request WebAssembly#55 from WebAssembly/rtt-expand
Expand explanation of RTTs
2 parents fae291d + 27beff3 commit e9c8b16

File tree

1 file changed

+38
-14
lines changed

1 file changed

+38
-14
lines changed

proposals/gc/MVP.md

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Based on [reference types proposal](https://github.com/WebAssembly/reference-typ
2828
* `i31ref` is a new reference type
2929
- `reftype ::= ... | i31ref`
3030

31-
* `rtt <typeuse>` is a new reference type that is a runtime representation of type `<typeuse>` (see [overview](Overview.md#casting-and-runtime-types))
31+
* `rtt <typeuse>` is a new reference type that is a runtime representation of type `<typeuse>` (see [Runtime types](#runtime-types))
3232
- `reftype ::= ... | rtt <typeuse>`
3333
- `rtt t ok` iff `t ok`
3434

@@ -159,12 +159,35 @@ In addition to the rules for basic reference types:
159159
* Table definitions with non-zero minimum size must have an element type that is defaultable. (Imports are not affected.)
160160

161161

162-
### Values
162+
### Runtime
163163

164-
* Each reference value has an associated *runtime type*, which is a runtime description of its type:
165-
- For structures or arrays, it is determined by an [RTT](#runtime-types) value provided upon creation, or `anyref` if none.
166-
- For `i31ref` references it is `i31ref`.
167-
- For `null` it is `nullref`.
164+
#### Runtime Types
165+
166+
* Runtime types (RTTs) are explicit values representing types at runtime; a value of type `rtt <t>` is a dynamic representative of static type `<t>`.
167+
168+
* All RTTs are explicitly created and all operations involving dynamic type information (like casts) operate on explicit RTT operands.
169+
170+
* There is a runtime subtyping hierarchy on RTTs; creating an RTT requires providing a *parent type* in the form of an existing RTT; the RTT for `anyref` is the root of this hierarchy.
171+
172+
* An RTT t1 is a *sub-RTT* of another RTT t2 iff either of the following holds:
173+
- t1 and t2 represent the same static type, or
174+
- t1 has a parent that is a sub-RTT of t2.
175+
176+
* Validation requires that each parent type is a representative of a static supertype of its child; runtime subtyping hence is a sub-relation of static subtyping (a graph with fewer nodes and edges).
177+
178+
* At the same time, runtime subtyping forms a linear hierarchy such that the relation can be checked efficiently using standard implementation techniques (it is a tree-shaped graph).
179+
180+
181+
#### Values
182+
183+
* Creating a structure or array optionally allows supplying a suitable RTT to represent its runtime type; it defaults to `anyref` if none is given.
184+
185+
* Each reference value has an associated runtime type:
186+
- For structures or arrays, it is the RTT provided upon creation, or `anyref` if none.
187+
- For `i31ref` references it is the RTT for `i31ref`.
188+
- For `null` it is the RTT for `nullref`.
189+
190+
* The so-defined runtime type is the only type information that can be discovered about a reference value at runtime; a structure or array with RTT `anyref` thereby is fully opaque to runtime type checks (and an implementation may choose to optimize away its RTT).
168191

169192

170193
### Instructions
@@ -265,7 +288,7 @@ Perhaps also the following short-hands:
265288
- traps on `null`
266289

267290

268-
#### Integer references
291+
#### Integer References
269292

270293
Tentatively, support a type of guaranteed unboxed scalars.
271294

@@ -290,7 +313,7 @@ Perhaps also the following short-hands:
290313
- equivalent to `(rtt.i31ref) (ref.cast anyref i31ref)`
291314

292315

293-
#### Optional references
316+
#### Optional References
294317

295318
* `ref.as_nonnull` converts an optional reference to a non-optional one
296319
- `ref.as_nonnull : [(optref $t)] -> [(ref $t)]`
@@ -307,33 +330,34 @@ Perhaps also the following short-hands:
307330

308331
#### Runtime Types
309332

310-
* `rtt.anyref` returns the RTT of type `anyref` as a subtype of only itself
333+
* `rtt.anyref` returns the RTT of type `anyref` as a sub-RTT of only itself
311334
- `rtt.anyref : [] -> [(rtt anyref)]`
312335

313-
* `rtt.new <typeuse> <typeuse>` returns the RTT of the specified type as a subtype of a given RTT operand
336+
* `rtt.new <typeuse> <typeuse>` returns the RTT of the specified type as a sub-RTT of a given parent RTT operand
314337
- `rtt.new t t' : [(rtt t')] -> [(rtt t)]`
315338
- iff `t <: t'`
316-
- multiple invocations of this instruction with the same operand yield the same RTTs
339+
- multiple invocations of this instruction with the same operand yield the same observable RTTs
317340

318341
* All RTT instructions are considered *constant expressions*.
319342

320343

321344
#### Casts
322345

323-
* `ref.test <typeuse> <typeuse>` tests whether a reference value's [runtime type](#values) matches a type given by a RTT representation
346+
* `ref.test <typeuse> <typeuse>` tests whether a reference value's [runtime type](#values) is a [runtime subtype](#runtime) of a given RTT
324347
- `ref.test t t' : [t (rtt t')] -> [i32]`
325348
- iff `t' <: t <: anyref`
326-
- returns 1 if the operand's runtime type is defined to be a (transitive) subtype of `t`, 0 otherwise
349+
- returns 1 if the first operand's runtime type is a sub-RTT of the RTT operand, 0 otherwise
327350

328351
* `ref.cast <typeuse> <typeuse>` casts a reference value down to a type given by a RTT representation
329352
- `ref.cast t t' : [t (rtt t')] -> [t']`
330353
- iff `t' <: t <: anyref`
331-
- traps if the operand's runtime type is not defined to be a (transitive) subtype of `t`
354+
- traps if the first operand's runtime type is not a sub-RTT of the RTT operand
332355

333356
* `br_on_cast <labelidx> <typeuse> <typeuse>` branches if a value can be cast down to a given reference type
334357
- `br_on_cast $l t t' : [t (rtt t')] -> [t]`
335358
- iff `t' <: t <: anyref`
336359
- and `$l : [t']`
360+
- branches iff the first operand's runtime type is a sub-RTT of the RTT operand
337361
- passes cast operand along with branch
338362

339363

0 commit comments

Comments
 (0)