Skip to content

Commit 5998d34

Browse files
committed
Use (rep i64) in example
1 parent 00aca66 commit 5998d34

1 file changed

Lines changed: 27 additions & 24 deletions

File tree

‎design/mvp/Explainer.md‎

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -718,19 +718,19 @@ section's example component:
718718
(component
719719
(core module $Alloc ...)
720720
(core instance $alloc (instantiate $Alloc))
721-
(type $file (resource (rep i32) (dtor (core func $alloc "free"))))
721+
(type $file (resource (rep i64) (dtor (core func $alloc "free"))))
722722
(core func $file-new (canon resource.new $file))
723723
(core module $Main
724-
(import "alloc" "mem" (memory 1))
725-
(import "alloc" "malloc" (func $malloc (param i32) (result i32)))
726-
(import "file" "new" (func $new (param i32) (result i32)))
727-
(func (export "open") (param $pathPtr i32) (param $pathLen i32) (result i32)
728-
(local $ptr i32)
729-
(local.set $ptr (call $malloc (i32.const ...)))
724+
(import "alloc" "mem" (memory i64 1))
725+
(import "alloc" "malloc" (func $malloc (param i64) (result i64)))
726+
(import "file" "new" (func $new (param i64) (result i32)))
727+
(func (export "open") (param $pathPtr i64) (param $pathLen i64) (result i32)
728+
(local $ptr i64)
729+
(local.set $ptr (call $malloc (i64.const ...)))
730730
... initialize $ptr
731731
(call $new (local.get $ptr))
732732
)
733-
(func (export "read") (param $ptr i32) (param $n i32) (result i32)
733+
(func (export "read") (param $ptr i64) (param $n i32) (result i64)
734734
... use $ptr ... return (list u8) ...
735735
)
736736
)
@@ -749,12 +749,13 @@ section's example component:
749749
```
750750
Walking through the definitions in order, starting after `$alloc`:
751751

752-
The `(type $file (resource ...))` defines a new resource type with an `i32`
752+
The `(type $file (resource ...))` defines a new resource type with an `i64`
753753
representation type that, when destroyed, calls the `free` function exported by
754754
`$alloc` to free the memory allocation. Currently, `i32` and `i64` [🐘] are the
755755
only allowed representation types but, in the future, as part of [wasm-gc]
756-
support, reference types could be added. In any case, the general idea is that
757-
a resource's representation value is pointer-like and pointer-sized.
756+
support, reference types could be added. In any case, the general idea is that a
757+
resource's representation value is pointer-like and pointer-sized. This example
758+
uses a 64-bit `memory` and thus specifies an `i64` resource representation.
758759

759760
Next, the `(core func $file-new (canon resource.new $file))` definition appends
760761
a new `core func` index for the `resource.new` ABI built-in. As this definition
@@ -766,7 +767,7 @@ was defined in the same component, ensuring that components are always able to
766767
encapsulate the construction and representation of their own resource types.
767768

768769
Next, the `$Main` module shows the lowered function type of `resource.new`
769-
which is imported as `file` `new`. The `i32` parameter of `new` matches the
770+
which is imported as `file` `new`. The `i64` parameter of `new` matches the
770771
`rep` type of `$file`. Regardless of `rep`, the result is always the `i32` index
771772
of a new `own` handle added to the caller's handle table. Thus, when a component
772773
implements a resource type, it starts out as the owner of all new resources of
@@ -779,14 +780,14 @@ the callee to the caller.
779780

780781
The `read` function exported by `$Main` is `lift`ed to implement the
781782
`[method]file.read` function exported by the component. The component-level
782-
first parameter of `[method]file.read` is `(param "self" (borrow $file))` and
783-
so the corresponding first parameter of `read` is `(param i32)`. As an ABI-level
784-
optimization, this `i32` is *not* the index of a `borrow` handle but, rather,
785-
it's the unboxed representation value of the resource pointed to by the given
786-
`borrow` handle. This optimization applies only when passing a `(borrow $R)`
787-
handle to the component instance that defined `$R` and it skips the overhead of
788-
adding a handle, calling `resource.rep`, and then calling `resource.drop` to
789-
drop the handle.
783+
first parameter of `[method]file.read` is `(param "self" (borrow $File))` which
784+
would normally be lowered to an `i32` handle index. However, as an ABI-level
785+
optimization, because this `canon lift` definition is inside the same component
786+
that defines `$file`, the first lowered parameter type is `i64`, matching the
787+
`(rep i64)` of `$file`. This optimization skips the per-call overhead of adding
788+
a borrowed handle to the handle table, calling `resource.rep` to get the `i64`,
789+
and then calling `resource.drop` to remove the borrowed handle from the handle
790+
table.
790791

791792
Because of all the static and dynamic checks described above, `open` and `read`
792793
can rely on the following useful guarantees:
@@ -822,10 +823,12 @@ after replacing `import` with `export`):
822823
(param "self" (borrow $File)) (param "n" u32) (result (list u8))))
823824
))
824825
```
825-
Notably, the `i32` `rep` type of `file` is kept an encapsulated implementation
826-
detail of the component and thus, e.g., a component can shift from `(rep i32)`
827-
to `(rep i64)` (and later, with [wasm-gc], `(rep (ref $T))`) without changing
828-
its public interface.
826+
Notably, the `i64` address type of `memory` and representation type of `file`
827+
are kept an encapsulated implementation detail of the component and thus this
828+
64-bit component can be imported and called by the 32-bit component shown in the
829+
previous section without a problem. This encapsulation also means that
830+
components can change their memory and resource representation types over time
831+
without breaking clients.
829832

830833
### Structured names and attributes
831834

0 commit comments

Comments
 (0)