@@ -213,6 +213,7 @@ definitions via the `cx` parameter:
213213class Context :
214214 opts: CanonicalOptions
215215 inst: ComponentInstance
216+ called_as_export: bool
216217```
217218
218219The ` opts ` field represents the [ ` canonopt ` ] values supplied to
@@ -238,6 +239,11 @@ class ComponentInstance:
238239 # ...
239240```
240241
242+ Lastly, the ` called_as_export ` field indicates whether the lifted function is
243+ being called through a component export or whether this is an internal call,
244+ (for example, when a child component calls an import that is defined by its
245+ parent component).
246+
241247
242248### Loading
243249
@@ -1206,29 +1212,29 @@ component*.
12061212
12071213Given the above closure arguments, ` canon_lift ` is defined:
12081214``` python
1209- def canon_lift (callee_cx , callee , ft , args , called_as_export ):
1210- if called_as_export:
1211- trap_if(not callee_cx .inst.may_enter)
1212- callee_cx .inst.may_enter = False
1215+ def canon_lift (cx , callee , ft , args ):
1216+ if cx. called_as_export:
1217+ trap_if(not cx .inst.may_enter)
1218+ cx .inst.may_enter = False
12131219 else :
1214- assert (not callee_cx .inst.may_enter)
1220+ assert (not cx .inst.may_enter)
12151221
1216- assert (callee_cx .inst.may_leave)
1217- callee_cx .inst.may_leave = False
1218- flat_args = lower_values(callee_cx , MAX_FLAT_PARAMS , args, ft.param_types())
1219- callee_cx .inst.may_leave = True
1222+ assert (cx .inst.may_leave)
1223+ cx .inst.may_leave = False
1224+ flat_args = lower_values(cx , MAX_FLAT_PARAMS , args, ft.param_types())
1225+ cx .inst.may_leave = True
12201226
12211227 try :
12221228 flat_results = callee(flat_args)
12231229 except CoreWebAssemblyException:
12241230 trap()
12251231
1226- results = lift_values(callee_cx , MAX_FLAT_RESULTS , ValueIter(flat_results), ft.result_types())
1232+ results = lift_values(cx , MAX_FLAT_RESULTS , ValueIter(flat_results), ft.result_types())
12271233 def post_return ():
1228- if callee_cx .opts.post_return is not None :
1229- callee_cx .opts.post_return(flat_results)
1230- if called_as_export:
1231- callee_cx .inst.may_enter = True
1234+ if cx .opts.post_return is not None :
1235+ cx .opts.post_return(flat_results)
1236+ if cx. called_as_export:
1237+ cx .inst.may_enter = True
12321238
12331239 return (results, post_return)
12341240```
@@ -1239,15 +1245,13 @@ boundaries. Thus, if a component wishes to signal an error, it must use some
12391245sort of explicit type such as ` result ` (whose ` error ` case particular language
12401246bindings may choose to map to and from exceptions).
12411247
1242- The ` called_as_export ` parameter indicates whether ` canon_lift ` is being called
1243- as part of a component export or whether this ` canon_lift ` is being called
1244- internally (for example, by a child component instance). By clearing
1245- ` may_enter ` for the duration of ` canon_lift ` when called as an export, the
1246- dynamic traps ensure that components cannot be reentered, which is a [ component
1247- invariant] . Furthermore, because ` may_enter ` is not cleared on the exceptional
1248- exit path taken by ` trap() ` , if there is a trap during Core WebAssembly
1249- execution or lifting/lowering, the component is left permanently un-enterable,
1250- ensuring the lockdown-after-trap [ component invariant] .
1248+ By clearing ` may_enter ` for the duration of ` canon_lift ` when the function is
1249+ called as an export, the dynamic traps ensure that components cannot be
1250+ reentered, ensuring the non-reentrance [ component invariant] . Furthermore,
1251+ because ` may_enter ` is not cleared on the exceptional exit path taken by
1252+ ` trap() ` , if there is a trap during Core WebAssembly execution of lifting or
1253+ lowering, the component is left permanently un-enterable, ensuring the
1254+ lockdown-after-trap [ component invariant] .
12511255
12521256The contract assumed by ` canon_lift ` (and ensured by ` canon_lower ` below) is
12531257that the caller of ` canon_lift ` * must* call ` post_return ` right after lowering
@@ -1274,17 +1278,17 @@ Thus, from the perspective of Core WebAssembly, `$f` is a [function instance]
12741278containing a ` hostfunc ` that closes over ` $opts ` , ` $inst ` , ` $callee ` and ` $ft `
12751279and, when called from Core WebAssembly code, calls ` canon_lower ` , which is defined as:
12761280``` python
1277- def canon_lower (caller_cx , callee , ft , flat_args ):
1278- trap_if(not caller_cx .inst.may_leave)
1281+ def canon_lower (cx , callee , ft , flat_args ):
1282+ trap_if(not cx .inst.may_leave)
12791283
12801284 flat_args = ValueIter(flat_args)
1281- args = lift_values(caller_cx , MAX_FLAT_PARAMS , flat_args, ft.param_types())
1285+ args = lift_values(cx , MAX_FLAT_PARAMS , flat_args, ft.param_types())
12821286
12831287 results, post_return = callee(args)
12841288
1285- caller_cx .inst.may_leave = False
1286- flat_results = lower_values(caller_cx , MAX_FLAT_RESULTS , results, ft.result_types(), flat_args)
1287- caller_cx .inst.may_leave = True
1289+ cx .inst.may_leave = False
1290+ flat_results = lower_values(cx , MAX_FLAT_RESULTS , results, ft.result_types(), flat_args)
1291+ cx .inst.may_leave = True
12881292
12891293 post_return()
12901294
0 commit comments