@@ -216,15 +216,26 @@ class CanonicalOptions:
216
216
realloc: Callable[[int ,int ,int ,int ],int ]
217
217
post_return: Callable[[],None ]
218
218
219
+ class ComponentInstance :
220
+ may_leave = True
221
+ may_enter = True
222
+ # ...
223
+
219
224
class Context :
220
225
opts: CanonicalOptions
226
+ inst: ComponentInstance
221
227
```
222
228
Going through the fields of ` Context ` :
223
229
224
230
The ` opts ` field represents the [ ` canonopt ` ] values supplied to
225
231
currently-executing ` canon lift ` or ` canon lower ` .
226
232
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
+
228
239
229
240
### Loading
230
241
@@ -1191,31 +1202,19 @@ export. In any case, `canon lift` specifies how these variously-produced values
1191
1202
are consumed as parameters (and produced as results) by a * single host-agnostic
1192
1203
component* .
1193
1204
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
-
1206
1205
Given the above closure arguments, ` canon_lift ` is defined:
1207
1206
``` 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 ):
1209
1208
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
1212
1211
else :
1213
- assert (not callee_instance .may_enter)
1212
+ assert (not callee_cx.inst .may_enter)
1214
1213
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
1217
1216
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
1219
1218
1220
1219
try :
1221
1220
flat_results = callee(flat_args)
@@ -1227,7 +1226,7 @@ def canon_lift(callee_cx, callee_instance, callee, ft, args, called_as_export):
1227
1226
if callee_cx.opts.post_return is not None :
1228
1227
callee_cx.opts.post_return(flat_results)
1229
1228
if called_as_export:
1230
- callee_instance .may_enter = True
1229
+ callee_cx.inst .may_enter = True
1231
1230
1232
1231
return (results, post_return)
1233
1232
```
@@ -1273,17 +1272,17 @@ Thus, from the perspective of Core WebAssembly, `$f` is a [function instance]
1273
1272
containing a ` hostfunc ` that closes over ` $opts ` , ` $inst ` , ` $callee ` and ` $ft `
1274
1273
and, when called from Core WebAssembly code, calls ` canon_lower ` , which is defined as:
1275
1274
``` 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)
1278
1277
1279
1278
flat_args = ValueIter(flat_args)
1280
1279
args = lift_values(caller_cx, MAX_FLAT_PARAMS , flat_args, ft.param_types())
1281
1280
1282
1281
results, post_return = callee(args)
1283
1282
1284
- caller_instance .may_leave = False
1283
+ caller_cx.inst .may_leave = False
1285
1284
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
1287
1286
1288
1287
post_return()
1289
1288
0 commit comments