@@ -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`:
963963def 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
968968def 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```
974974Note that the ` rt ` value that is stored in the runtime ` Handle ` captures what
975975is statically known about the handle right before losing this information in
@@ -1406,16 +1406,14 @@ component*.
14061406
14071407Given 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-
14381432Uncaught Core WebAssembly [ exceptions] result in a trap at component
14391433boundaries. Thus, if a component wishes to signal an error, it must use some
14401434sort 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]
14651459containing a ` hostfunc ` that closes over ` $opts ` , ` $inst ` , ` $callee ` and ` $ft `
14661460and, 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:
15461541Calling ` $f ` invokes the following function, which creates a resource object
15471542and 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
15651560be of type ` $t ` from the handle table and then, for an ` own ` handle, calls the
15661561optional 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
15901585matches. Note that the "locally-defined" requirement above ensures that only
15911586the 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
0 commit comments