@@ -216,15 +216,26 @@ class CanonicalOptions:
216216 realloc: Callable[[int ,int ,int ,int ],int ]
217217 post_return: Callable[[],None ]
218218
219+ class ComponentInstance :
220+ may_leave = True
221+ may_enter = True
222+ # ...
223+
219224class Context :
220225 opts: CanonicalOptions
226+ inst: ComponentInstance
221227```
222228Going through the fields of ` Context ` :
223229
224230The ` opts ` field represents the [ ` canonopt ` ] values supplied to
225231currently-executing ` canon lift ` or ` canon lower ` .
226232
227- (Others will be added shortly.)
233+ The ` inst ` field represents the component instance that the currently-executing
234+ canonical definition is closed over. The ` may_enter ` and ` may_leave ` fields are
235+ used to enforce the [ component invariants] : ` may_leave ` indicates whether the
236+ instance may call out to an import and the ` may_enter ` state indicates whether
237+ the instance may be called from the outside world through an export.
238+
228239
229240### Loading
230241
@@ -1191,31 +1202,19 @@ export. In any case, `canon lift` specifies how these variously-produced values
11911202are consumed as parameters (and produced as results) by a * single host-agnostic
11921203component* .
11931204
1194- The ` $inst ` captured above is assumed to have at least the following two fields,
1195- which are used to implement the [ component invariants] :
1196- ``` python
1197- class Instance :
1198- may_leave = True
1199- may_enter = True
1200- # ...
1201- ```
1202- The ` may_leave ` state indicates whether the instance may call out to an import
1203- and the ` may_enter ` state indicates whether the instance may be called from
1204- the outside world through an export.
1205-
12061205Given the above closure arguments, ` canon_lift ` is defined:
12071206``` python
1208- def canon_lift (callee_cx , callee_instance , callee , ft , args , called_as_export ):
1207+ def canon_lift (callee_cx , callee , ft , args , called_as_export ):
12091208 if called_as_export:
1210- trap_if(not callee_instance .may_enter)
1211- callee_instance .may_enter = False
1209+ trap_if(not callee_cx.inst .may_enter)
1210+ callee_cx.inst .may_enter = False
12121211 else :
1213- assert (not callee_instance .may_enter)
1212+ assert (not callee_cx.inst .may_enter)
12141213
1215- assert (callee_instance .may_leave)
1216- callee_instance .may_leave = False
1214+ assert (callee_cx.inst .may_leave)
1215+ callee_cx.inst .may_leave = False
12171216 flat_args = lower_values(callee_cx, MAX_FLAT_PARAMS , args, ft.param_types())
1218- callee_instance .may_leave = True
1217+ callee_cx.inst .may_leave = True
12191218
12201219 try :
12211220 flat_results = callee(flat_args)
@@ -1227,7 +1226,7 @@ def canon_lift(callee_cx, callee_instance, callee, ft, args, called_as_export):
12271226 if callee_cx.opts.post_return is not None :
12281227 callee_cx.opts.post_return(flat_results)
12291228 if called_as_export:
1230- callee_instance .may_enter = True
1229+ callee_cx.inst .may_enter = True
12311230
12321231 return (results, post_return)
12331232```
@@ -1273,17 +1272,17 @@ Thus, from the perspective of Core WebAssembly, `$f` is a [function instance]
12731272containing a ` hostfunc ` that closes over ` $opts ` , ` $inst ` , ` $callee ` and ` $ft `
12741273and, when called from Core WebAssembly code, calls ` canon_lower ` , which is defined as:
12751274``` python
1276- def canon_lower (caller_cx , caller_instance , callee , ft , flat_args ):
1277- trap_if(not caller_instance .may_leave)
1275+ def canon_lower (caller_cx , callee , ft , flat_args ):
1276+ trap_if(not caller_cx.inst .may_leave)
12781277
12791278 flat_args = ValueIter(flat_args)
12801279 args = lift_values(caller_cx, MAX_FLAT_PARAMS , flat_args, ft.param_types())
12811280
12821281 results, post_return = callee(args)
12831282
1284- caller_instance .may_leave = False
1283+ caller_cx.inst .may_leave = False
12851284 flat_results = lower_values(caller_cx, MAX_FLAT_RESULTS , results, ft.result_types(), flat_args)
1286- caller_instance .may_leave = True
1285+ caller_cx.inst .may_leave = True
12871286
12881287 post_return()
12891288
0 commit comments