Skip to content

Commit f25a7db

Browse files
committed
Inline rep into handles, remove explicit Resource object in CABI
1 parent 379a2e7 commit f25a7db

File tree

3 files changed

+36
-43
lines changed

3 files changed

+36
-43
lines changed

design/mvp/CanonicalABI.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -257,22 +257,24 @@ class ComponentInstance:
257257
The `HandleTable` class is defined in terms of a collection of supporting
258258
runtime bookkeeping classes that we'll go through first.
259259

260-
The `Resource` class represents a runtime instance of a resource type, storing
261-
the core representation value (which is currently fixed to `i32`) and the
262-
component instance that is implementing this resource.
260+
The `ResourceType` class represents a resource type that has been defined by
261+
the specific component instance pointed to by `impl` with a particular
262+
function closure as the `dtor`.
263263
```python
264264
@dataclass
265-
class Resource:
266-
rep: int
265+
class ResourceType(Type):
267266
impl: ComponentInstance
267+
dtor: Optional[Callable[[int],None]]
268268
```
269269

270-
The `OwnHandle` and `BorrowHandle` classes represent runtime handle values of
271-
`own` and `borrow` type, resp:
270+
The `Handle` class and its subclasses represent handle values referring to
271+
resources. The `rep` field of `Handle` stores the representation value
272+
(currently fixed to `i32`) pass to `resource.new` for the resource that this
273+
handle refers to.
272274
```python
273275
@dataclass
274276
class Handle:
275-
resource: Resource
277+
rep: int
276278
rt: ResourceType
277279
lend_count: int
278280

@@ -284,8 +286,7 @@ class OwnHandle(Handle):
284286
class BorrowHandle(Handle):
285287
scope: BorrowScope
286288
```
287-
The `resource` field points to the resource instance this handle refers to. The
288-
`rt` field points to a runtime value representing the static
289+
The `rt` field points to a runtime value representing the static
289290
[`resourcetype`](Explainer.md#type-definitions) of this handle and is used by
290291
dynamic type checking below. Lastly, the `lend_count` field maintains a count
291292
of the outstanding handles that were lent from this handle (by calls to
@@ -962,13 +963,13 @@ current component instance's `HandleTable`:
962963
```python
963964
def lower_own(cx, src, rt):
964965
assert(isinstance(src, OwnHandle))
965-
h = OwnHandle(src.resource, rt, 0)
966+
h = OwnHandle(src.rep, rt, 0)
966967
return cx.inst.handles.insert(h)
967968

968969
def lower_borrow(cx, src, rt):
969970
assert(isinstance(src, Handle))
970971
cx.borrow_scope.add(src)
971-
h = BorrowHandle(src.resource, rt, 0, cx.borrow_scope)
972+
h = BorrowHandle(src.rep, rt, 0, cx.borrow_scope)
972973
return cx.inst.handles.insert(h)
973974
```
974975
Note that the `rt` value that is stored in the runtime `Handle` captures what
@@ -1542,7 +1543,7 @@ Calling `$f` invokes the following function, which creates a resource object
15421543
and inserts it into the current instance's handle table:
15431544
```python
15441545
def canon_resource_new(inst, rt, rep):
1545-
h = OwnHandle(Resource(rep, inst), rt, 0)
1546+
h = OwnHandle(rep, rt, 0)
15461547
return inst.handles.insert(h)
15471548
```
15481549

@@ -1563,8 +1564,8 @@ optional destructor.
15631564
def canon_resource_drop(inst, t, i):
15641565
h = inst.handles.remove(i, t)
15651566
if isinstance(t, Own) and t.rt.dtor:
1566-
trap_if(not h.resource.impl.may_enter)
1567-
t.rt.dtor(h.resource.rep)
1567+
trap_if(not t.rt.impl.may_enter)
1568+
t.rt.dtor(h.rep)
15681569
```
15691570
The `may_enter` guard ensures the non-reentrance [component invariant], since
15701571
a destructor call is analogous to a call to an export.
@@ -1586,8 +1587,7 @@ matches. Note that the "locally-defined" requirement above ensures that only
15861587
the component instance defining a resource can access its representation.
15871588
```python
15881589
def canon_resource_rep(inst, rt, i):
1589-
h = inst.handles.get(i, rt)
1590-
return h.resource.rep
1590+
return inst.handles.get(i, rt).rep
15911591
```
15921592

15931593

design/mvp/canonical-abi/definitions.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,6 @@ class Result(ValType):
159159
class Flags(ValType):
160160
labels: [str]
161161

162-
@dataclass
163-
class ResourceType(Type):
164-
dtor: Optional[Callable[[int],None]]
165-
166162
@dataclass
167163
class Own(ValType):
168164
rt: ResourceType
@@ -320,15 +316,15 @@ def __init__(self):
320316
#
321317

322318
@dataclass
323-
class Resource:
324-
rep: int
319+
class ResourceType(Type):
325320
impl: ComponentInstance
321+
dtor: Optional[Callable[[int],None]]
326322

327323
#
328324

329325
@dataclass
330326
class Handle:
331-
resource: Resource
327+
rep: int
332328
rt: ResourceType
333329
lend_count: int
334330

@@ -843,13 +839,13 @@ def pack_flags_into_int(v, labels):
843839

844840
def lower_own(cx, src, rt):
845841
assert(isinstance(src, OwnHandle))
846-
h = OwnHandle(src.resource, rt, 0)
842+
h = OwnHandle(src.rep, rt, 0)
847843
return cx.inst.handles.insert(h)
848844

849845
def lower_borrow(cx, src, rt):
850846
assert(isinstance(src, Handle))
851847
cx.borrow_scope.add(src)
852-
h = BorrowHandle(src.resource, rt, 0, cx.borrow_scope)
848+
h = BorrowHandle(src.rep, rt, 0, cx.borrow_scope)
853849
return cx.inst.handles.insert(h)
854850

855851
### Flattening
@@ -1204,19 +1200,18 @@ def canon_lower(opts, inst, callee, calling_import, ft, flat_args):
12041200
### `resource.new`
12051201

12061202
def canon_resource_new(inst, rt, rep):
1207-
h = OwnHandle(Resource(rep, inst), rt, 0)
1203+
h = OwnHandle(rep, rt, 0)
12081204
return inst.handles.insert(h)
12091205

12101206
### `resource.drop`
12111207

12121208
def canon_resource_drop(inst, t, i):
12131209
h = inst.handles.remove(i, t)
12141210
if isinstance(t, Own) and t.rt.dtor:
1215-
trap_if(not h.resource.impl.may_enter)
1216-
t.rt.dtor(h.resource.rep)
1211+
trap_if(not t.rt.impl.may_enter)
1212+
t.rt.dtor(h.rep)
12171213

12181214
### `resource.rep`
12191215

12201216
def canon_resource_rep(inst, rt, i):
1221-
h = inst.handles.get(i, rt)
1222-
return h.resource.rep
1217+
return inst.handles.get(i, rt).rep

design/mvp/canonical-abi/run_tests.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -383,21 +383,19 @@ def test_handles():
383383
def dtor(x):
384384
nonlocal dtor_value
385385
dtor_value = x
386-
rt = ResourceType(dtor)
387386

388-
opts = mk_opts()
387+
rt = ResourceType(ComponentInstance(), dtor)
388+
389389
inst = ComponentInstance()
390-
r1 = Resource(42, inst)
391-
r2 = Resource(43, inst)
392-
r3 = Resource(44, inst)
390+
opts = mk_opts()
393391

394392
def host_import(args):
395393
nonlocal opts
396394
nonlocal inst
397395
assert(len(args) == 2)
398-
assert(args[0].resource is r1)
399-
assert(args[1].resource is r3)
400-
return ([OwnHandle(Resource(45, inst), rt, 0)], lambda:())
396+
assert(args[0].rep == 42)
397+
assert(args[1].rep == 44)
398+
return ([OwnHandle(45, rt, 0)], lambda:())
401399

402400
def core_wasm(args):
403401
nonlocal dtor_value
@@ -439,13 +437,13 @@ def core_wasm(args):
439437
return [Value('i32', 0), Value('i32', 1), Value('i32', 3)]
440438

441439
ft = FuncType([Own(rt),Own(rt),Borrow(rt)],[Own(rt),Own(rt),Own(rt)])
442-
args = [OwnHandle(r1, rt, 0), OwnHandle(r2, rt, 0), OwnHandle(r3, rt, 0)]
440+
args = [OwnHandle(42, rt, 0), OwnHandle(43, rt, 0), OwnHandle(44, rt, 0)]
443441
got,post_return = canon_lift(opts, inst, core_wasm, ft, args)
444442

445443
assert(len(got) == 3)
446-
assert(got[0].resource.rep == 46)
447-
assert(got[1].resource is r2)
448-
assert(got[2].resource.rep == 45)
444+
assert(got[0].rep == 46)
445+
assert(got[1].rep == 43)
446+
assert(got[2].rep == 45)
449447
assert(len(inst.handles.array) == 4)
450448
assert(all(inst.handles.array[i] is None for i in range(3)))
451449
assert(len(inst.handles.free) == 4)

0 commit comments

Comments
 (0)