@@ -220,9 +220,9 @@ class Context:
220
220
inst: ComponentInstance
221
221
borrow_scope: BorrowScope
222
222
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
226
226
self .borrow_scope = BorrowScope()
227
227
```
228
228
@@ -336,7 +336,7 @@ class HandleTable:
336
336
self .array = []
337
337
self .free = []
338
338
339
- def insert (self , cx , h ):
339
+ def insert (self , h ):
340
340
if self .free:
341
341
i = self .free.pop()
342
342
assert (self .array[i] is None )
@@ -963,13 +963,13 @@ current component instance's `HandleTable`:
963
963
def lower_own (cx , src , rt ):
964
964
assert (isinstance (src, OwnHandle))
965
965
h = OwnHandle(src.resource, rt, 0 )
966
- return cx.inst.handles.insert(cx, h)
966
+ return cx.inst.handles.insert(h)
967
967
968
968
def lower_borrow (cx , src , rt ):
969
969
assert (isinstance (src, Handle))
970
970
cx.borrow_scope.add(src)
971
971
h = BorrowHandle(src.resource, rt, 0 , cx.borrow_scope)
972
- return cx.inst.handles.insert(cx, h)
972
+ return cx.inst.handles.insert(h)
973
973
```
974
974
Note that the ` rt ` value that is stored in the runtime ` Handle ` captures what
975
975
is statically known about the handle right before losing this information in
@@ -1406,16 +1406,14 @@ component*.
1406
1406
1407
1407
Given the above closure arguments, ` canon_lift ` is defined:
1408
1408
``` 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)
1414
1412
1415
- assert (cx. inst.may_leave)
1416
- cx. inst.may_leave = False
1413
+ assert (inst.may_leave)
1414
+ inst.may_leave = False
1417
1415
flat_args = lower_values(cx, MAX_FLAT_PARAMS , args, ft.param_types())
1418
- cx. inst.may_leave = True
1416
+ inst.may_leave = True
1419
1417
1420
1418
try :
1421
1419
flat_results = callee(flat_args)
@@ -1425,16 +1423,12 @@ def canon_lift(cx, callee, ft, args):
1425
1423
results = lift_values(cx, MAX_FLAT_RESULTS , ValueIter(flat_results), ft.result_types())
1426
1424
1427
1425
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)
1431
1428
cx.borrow_scope.exit()
1432
- cx.borrow_scope = outer_borrow_scope
1433
1429
1434
1430
return (results, post_return)
1435
1431
```
1436
- There are a number of things to note about this definition:
1437
-
1438
1432
Uncaught Core WebAssembly [ exceptions] result in a trap at component
1439
1433
boundaries. Thus, if a component wishes to signal an error, it must use some
1440
1434
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]
1465
1459
containing a ` hostfunc ` that closes over ` $opts ` , ` $inst ` , ` $callee ` and ` $ft `
1466
1460
and, when called from Core WebAssembly code, calls ` canon_lower ` , which is defined as:
1467
1461
``` 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)
1470
1465
1471
- assert (cx. inst.may_enter)
1466
+ assert (inst.may_enter)
1472
1467
if calling_import:
1473
- cx. inst.may_enter = False
1468
+ inst.may_enter = False
1474
1469
1475
1470
flat_args = ValueIter(flat_args)
1476
1471
args = lift_values(cx, MAX_FLAT_PARAMS , flat_args, ft.param_types())
1477
1472
1478
1473
results, post_return = callee(args)
1479
1474
1480
- cx. inst.may_leave = False
1475
+ inst.may_leave = False
1481
1476
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
1483
1478
1484
1479
post_return()
1485
1480
1486
1481
if calling_import:
1487
- cx. inst.may_enter = True
1482
+ inst.may_enter = True
1488
1483
1489
1484
return flat_results
1490
1485
```
@@ -1546,9 +1541,9 @@ validation specifies:
1546
1541
Calling ` $f ` invokes the following function, which creates a resource object
1547
1542
and inserts it into the current instance's handle table:
1548
1543
``` 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)
1552
1547
```
1553
1548
1554
1549
### ` canon resource.drop `
@@ -1565,8 +1560,8 @@ Calling `$f` invokes the following function, which removes a handle guarded to
1565
1560
be of type ` $t ` from the handle table and then, for an ` own ` handle, calls the
1566
1561
optional destructor.
1567
1562
``` 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)
1570
1565
if isinstance (t, Own) and t.rt.dtor:
1571
1566
trap_if(not h.resource.impl.may_enter)
1572
1567
t.rt.dtor(h.resource.rep)
@@ -1590,8 +1585,8 @@ representation of the indexed handle after checking that the resource type
1590
1585
matches. Note that the "locally-defined" requirement above ensures that only
1591
1586
the component instance defining a resource can access its representation.
1592
1587
``` 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)
1595
1590
return h.resource.rep
1596
1591
```
1597
1592
0 commit comments