Skip to content

Commit fa9ecc5

Browse files
committed
Refactor canonical-abi/definitions.py to not reuse Context objects
1 parent d6af626 commit fa9ecc5

File tree

3 files changed

+108
-107
lines changed

3 files changed

+108
-107
lines changed

design/mvp/CanonicalABI.md

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,9 @@ class Context:
220220
inst: ComponentInstance
221221
borrow_scope: BorrowScope
222222

223-
def __init__(self):
224-
self.opts = CanonicalOptions()
225-
self.inst = ComponentInstance()
223+
def __init__(self, opts, inst):
224+
self.opts = opts
225+
self.inst = inst
226226
self.borrow_scope = BorrowScope()
227227
```
228228

@@ -336,7 +336,7 @@ class HandleTable:
336336
self.array = []
337337
self.free = []
338338

339-
def insert(self, cx, h):
339+
def insert(self, h):
340340
if self.free:
341341
i = self.free.pop()
342342
assert(self.array[i] is None)
@@ -963,13 +963,13 @@ current component instance's `HandleTable`:
963963
def lower_own(cx, src, rt):
964964
assert(isinstance(src, OwnHandle))
965965
h = OwnHandle(src.resource, rt, 0)
966-
return cx.inst.handles.insert(cx, h)
966+
return cx.inst.handles.insert(h)
967967

968968
def lower_borrow(cx, src, rt):
969969
assert(isinstance(src, Handle))
970970
cx.borrow_scope.add(src)
971971
h = BorrowHandle(src.resource, rt, 0, cx.borrow_scope)
972-
return cx.inst.handles.insert(cx, h)
972+
return cx.inst.handles.insert(h)
973973
```
974974
Note that the `rt` value that is stored in the runtime `Handle` captures what
975975
is statically known about the handle right before losing this information in
@@ -1406,16 +1406,14 @@ component*.
14061406

14071407
Given the above closure arguments, `canon_lift` is defined:
14081408
```python
1409-
def canon_lift(cx, callee, ft, args):
1410-
trap_if(not cx.inst.may_enter)
1411-
1412-
outer_borrow_scope = cx.borrow_scope
1413-
cx.borrow_scope = BorrowScope()
1409+
def canon_lift(opts, inst, callee, ft, args):
1410+
cx = Context(opts, inst)
1411+
trap_if(not inst.may_enter)
14141412

1415-
assert(cx.inst.may_leave)
1416-
cx.inst.may_leave = False
1413+
assert(inst.may_leave)
1414+
inst.may_leave = False
14171415
flat_args = lower_values(cx, MAX_FLAT_PARAMS, args, ft.param_types())
1418-
cx.inst.may_leave = True
1416+
inst.may_leave = True
14191417

14201418
try:
14211419
flat_results = callee(flat_args)
@@ -1425,16 +1423,12 @@ def canon_lift(cx, callee, ft, args):
14251423
results = lift_values(cx, MAX_FLAT_RESULTS, ValueIter(flat_results), ft.result_types())
14261424

14271425
def post_return():
1428-
if cx.opts.post_return is not None:
1429-
cx.opts.post_return(flat_results)
1430-
1426+
if opts.post_return is not None:
1427+
opts.post_return(flat_results)
14311428
cx.borrow_scope.exit()
1432-
cx.borrow_scope = outer_borrow_scope
14331429

14341430
return (results, post_return)
14351431
```
1436-
There are a number of things to note about this definition:
1437-
14381432
Uncaught Core WebAssembly [exceptions] result in a trap at component
14391433
boundaries. Thus, if a component wishes to signal an error, it must use some
14401434
sort of explicit type such as `result` (whose `error` case particular language
@@ -1465,26 +1459,27 @@ Thus, from the perspective of Core WebAssembly, `$f` is a [function instance]
14651459
containing a `hostfunc` that closes over `$opts`, `$inst`, `$callee` and `$ft`
14661460
and, when called from Core WebAssembly code, calls `canon_lower`, which is defined as:
14671461
```python
1468-
def canon_lower(cx, callee, calling_import, ft, flat_args):
1469-
trap_if(not cx.inst.may_leave)
1462+
def canon_lower(opts, inst, callee, calling_import, ft, flat_args):
1463+
cx = Context(opts, inst)
1464+
trap_if(not inst.may_leave)
14701465

1471-
assert(cx.inst.may_enter)
1466+
assert(inst.may_enter)
14721467
if calling_import:
1473-
cx.inst.may_enter = False
1468+
inst.may_enter = False
14741469

14751470
flat_args = ValueIter(flat_args)
14761471
args = lift_values(cx, MAX_FLAT_PARAMS, flat_args, ft.param_types())
14771472

14781473
results, post_return = callee(args)
14791474

1480-
cx.inst.may_leave = False
1475+
inst.may_leave = False
14811476
flat_results = lower_values(cx, MAX_FLAT_RESULTS, results, ft.result_types(), flat_args)
1482-
cx.inst.may_leave = True
1477+
inst.may_leave = True
14831478

14841479
post_return()
14851480

14861481
if calling_import:
1487-
cx.inst.may_enter = True
1482+
inst.may_enter = True
14881483

14891484
return flat_results
14901485
```
@@ -1546,9 +1541,9 @@ validation specifies:
15461541
Calling `$f` invokes the following function, which creates a resource object
15471542
and inserts it into the current instance's handle table:
15481543
```python
1549-
def canon_resource_new(cx, rt, rep):
1550-
h = OwnHandle(Resource(rep, cx.inst), rt, 0)
1551-
return cx.inst.handles.insert(cx, h)
1544+
def canon_resource_new(inst, rt, rep):
1545+
h = OwnHandle(Resource(rep, inst), rt, 0)
1546+
return inst.handles.insert(h)
15521547
```
15531548

15541549
### `canon resource.drop`
@@ -1565,8 +1560,8 @@ Calling `$f` invokes the following function, which removes a handle guarded to
15651560
be of type `$t` from the handle table and then, for an `own` handle, calls the
15661561
optional destructor.
15671562
```python
1568-
def canon_resource_drop(cx, t, i):
1569-
h = cx.inst.handles.remove(i, t)
1563+
def canon_resource_drop(inst, t, i):
1564+
h = inst.handles.remove(i, t)
15701565
if isinstance(t, Own) and t.rt.dtor:
15711566
trap_if(not h.resource.impl.may_enter)
15721567
t.rt.dtor(h.resource.rep)
@@ -1590,8 +1585,8 @@ representation of the indexed handle after checking that the resource type
15901585
matches. Note that the "locally-defined" requirement above ensures that only
15911586
the component instance defining a resource can access its representation.
15921587
```python
1593-
def canon_resource_rep(cx, rt, i):
1594-
h = cx.inst.handles.get(i, rt)
1588+
def canon_resource_rep(inst, rt, i):
1589+
h = inst.handles.get(i, rt)
15951590
return h.resource.rep
15961591
```
15971592

design/mvp/canonical-abi/definitions.py

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,9 @@ class Context:
292292
inst: ComponentInstance
293293
borrow_scope: BorrowScope
294294

295-
def __init__(self):
296-
self.opts = CanonicalOptions()
297-
self.inst = ComponentInstance()
295+
def __init__(self, opts, inst):
296+
self.opts = opts
297+
self.inst = inst
298298
self.borrow_scope = BorrowScope()
299299

300300
#
@@ -373,7 +373,7 @@ def __init__(self):
373373
self.array = []
374374
self.free = []
375375

376-
def insert(self, cx, h):
376+
def insert(self, h):
377377
if self.free:
378378
i = self.free.pop()
379379
assert(self.array[i] is None)
@@ -844,13 +844,13 @@ def pack_flags_into_int(v, labels):
844844
def lower_own(cx, src, rt):
845845
assert(isinstance(src, OwnHandle))
846846
h = OwnHandle(src.resource, rt, 0)
847-
return cx.inst.handles.insert(cx, h)
847+
return cx.inst.handles.insert(h)
848848

849849
def lower_borrow(cx, src, rt):
850850
assert(isinstance(src, Handle))
851851
cx.borrow_scope.add(src)
852852
h = BorrowHandle(src.resource, rt, 0, cx.borrow_scope)
853-
return cx.inst.handles.insert(cx, h)
853+
return cx.inst.handles.insert(h)
854854

855855
### Flattening
856856

@@ -1152,16 +1152,14 @@ def lower_values(cx, max_flat, vs, ts, out_param = None):
11521152

11531153
### `lift`
11541154

1155-
def canon_lift(cx, callee, ft, args):
1156-
trap_if(not cx.inst.may_enter)
1157-
1158-
outer_borrow_scope = cx.borrow_scope
1159-
cx.borrow_scope = BorrowScope()
1155+
def canon_lift(opts, inst, callee, ft, args):
1156+
cx = Context(opts, inst)
1157+
trap_if(not inst.may_enter)
11601158

1161-
assert(cx.inst.may_leave)
1162-
cx.inst.may_leave = False
1159+
assert(inst.may_leave)
1160+
inst.may_leave = False
11631161
flat_args = lower_values(cx, MAX_FLAT_PARAMS, args, ft.param_types())
1164-
cx.inst.may_leave = True
1162+
inst.may_leave = True
11651163

11661164
try:
11671165
flat_results = callee(flat_args)
@@ -1171,55 +1169,54 @@ def canon_lift(cx, callee, ft, args):
11711169
results = lift_values(cx, MAX_FLAT_RESULTS, ValueIter(flat_results), ft.result_types())
11721170

11731171
def post_return():
1174-
if cx.opts.post_return is not None:
1175-
cx.opts.post_return(flat_results)
1176-
1172+
if opts.post_return is not None:
1173+
opts.post_return(flat_results)
11771174
cx.borrow_scope.exit()
1178-
cx.borrow_scope = outer_borrow_scope
11791175

11801176
return (results, post_return)
11811177

11821178
### `lower`
11831179

1184-
def canon_lower(cx, callee, calling_import, ft, flat_args):
1185-
trap_if(not cx.inst.may_leave)
1180+
def canon_lower(opts, inst, callee, calling_import, ft, flat_args):
1181+
cx = Context(opts, inst)
1182+
trap_if(not inst.may_leave)
11861183

1187-
assert(cx.inst.may_enter)
1184+
assert(inst.may_enter)
11881185
if calling_import:
1189-
cx.inst.may_enter = False
1186+
inst.may_enter = False
11901187

11911188
flat_args = ValueIter(flat_args)
11921189
args = lift_values(cx, MAX_FLAT_PARAMS, flat_args, ft.param_types())
11931190

11941191
results, post_return = callee(args)
11951192

1196-
cx.inst.may_leave = False
1193+
inst.may_leave = False
11971194
flat_results = lower_values(cx, MAX_FLAT_RESULTS, results, ft.result_types(), flat_args)
1198-
cx.inst.may_leave = True
1195+
inst.may_leave = True
11991196

12001197
post_return()
12011198

12021199
if calling_import:
1203-
cx.inst.may_enter = True
1200+
inst.may_enter = True
12041201

12051202
return flat_results
12061203

12071204
### `resource.new`
12081205

1209-
def canon_resource_new(cx, rt, rep):
1210-
h = OwnHandle(Resource(rep, cx.inst), rt, 0)
1211-
return cx.inst.handles.insert(cx, h)
1206+
def canon_resource_new(inst, rt, rep):
1207+
h = OwnHandle(Resource(rep, inst), rt, 0)
1208+
return inst.handles.insert(h)
12121209

12131210
### `resource.drop`
12141211

1215-
def canon_resource_drop(cx, t, i):
1216-
h = cx.inst.handles.remove(i, t)
1212+
def canon_resource_drop(inst, t, i):
1213+
h = inst.handles.remove(i, t)
12171214
if isinstance(t, Own) and t.rt.dtor:
12181215
trap_if(not h.resource.impl.may_enter)
12191216
t.rt.dtor(h.resource.rep)
12201217

12211218
### `resource.rep`
12221219

1223-
def canon_resource_rep(cx, rt, i):
1224-
h = cx.inst.handles.get(i, rt)
1220+
def canon_resource_rep(inst, rt, i):
1221+
h = inst.handles.get(i, rt)
12251222
return h.resource.rep

0 commit comments

Comments
 (0)