Skip to content

Commit 8d89078

Browse files
committed
Move the component instance into the context
1 parent 6417d2c commit 8d89078

File tree

3 files changed

+47
-50
lines changed

3 files changed

+47
-50
lines changed

Diff for: design/mvp/CanonicalABI.md

+24-25
Original file line numberDiff line numberDiff line change
@@ -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+
219224
class Context:
220225
opts: CanonicalOptions
226+
inst: ComponentInstance
221227
```
222228
Going through the fields of `Context`:
223229

224230
The `opts` field represents the [`canonopt`] values supplied to
225231
currently-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
11911202
are consumed as parameters (and produced as results) by a *single host-agnostic
11921203
component*.
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-
12061205
Given 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]
12731272
containing a `hostfunc` that closes over `$opts`, `$inst`, `$callee` and `$ft`
12741273
and, 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

Diff for: design/mvp/canonical-abi/definitions.py

+18-19
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,14 @@ class CanonicalOptions:
277277
realloc: Callable[[int,int,int,int],int]
278278
post_return: Callable[[],None]
279279

280+
class ComponentInstance:
281+
may_leave = True
282+
may_enter = True
283+
# ...
284+
280285
class Context:
281286
opts: CanonicalOptions
287+
inst: ComponentInstance
282288

283289
### Loading
284290

@@ -996,24 +1002,17 @@ def lower_values(cx, max_flat, vs, ts, out_param = None):
9961002

9971003
### `lift`
9981004

999-
class Instance:
1000-
may_leave = True
1001-
may_enter = True
1002-
# ...
1003-
1004-
#
1005-
1006-
def canon_lift(callee_cx, callee_instance, callee, ft, args, called_as_export):
1005+
def canon_lift(callee_cx, callee, ft, args, called_as_export):
10071006
if called_as_export:
1008-
trap_if(not callee_instance.may_enter)
1009-
callee_instance.may_enter = False
1007+
trap_if(not callee_cx.inst.may_enter)
1008+
callee_cx.inst.may_enter = False
10101009
else:
1011-
assert(not callee_instance.may_enter)
1010+
assert(not callee_cx.inst.may_enter)
10121011

1013-
assert(callee_instance.may_leave)
1014-
callee_instance.may_leave = False
1012+
assert(callee_cx.inst.may_leave)
1013+
callee_cx.inst.may_leave = False
10151014
flat_args = lower_values(callee_cx, MAX_FLAT_PARAMS, args, ft.param_types())
1016-
callee_instance.may_leave = True
1015+
callee_cx.inst.may_leave = True
10171016

10181017
try:
10191018
flat_results = callee(flat_args)
@@ -1025,23 +1024,23 @@ def post_return():
10251024
if callee_cx.opts.post_return is not None:
10261025
callee_cx.opts.post_return(flat_results)
10271026
if called_as_export:
1028-
callee_instance.may_enter = True
1027+
callee_cx.inst.may_enter = True
10291028

10301029
return (results, post_return)
10311030

10321031
### `lower`
10331032

1034-
def canon_lower(caller_cx, caller_instance, callee, ft, flat_args):
1035-
trap_if(not caller_instance.may_leave)
1033+
def canon_lower(caller_cx, callee, ft, flat_args):
1034+
trap_if(not caller_cx.inst.may_leave)
10361035

10371036
flat_args = ValueIter(flat_args)
10381037
args = lift_values(caller_cx, MAX_FLAT_PARAMS, flat_args, ft.param_types())
10391038

10401039
results, post_return = callee(args)
10411040

1042-
caller_instance.may_leave = False
1041+
caller_cx.inst.may_leave = False
10431042
flat_results = lower_values(caller_cx, MAX_FLAT_RESULTS, results, ft.result_types(), flat_args)
1044-
caller_instance.may_leave = True
1043+
caller_cx.inst.may_leave = True
10451044

10461045
post_return()
10471046

Diff for: design/mvp/canonical-abi/run_tests.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def mk_cx(memory, encoding = None, realloc = None, post_return = None):
3939
cx.opts.string_encoding = encoding
4040
cx.opts.realloc = realloc
4141
cx.opts.post_return = post_return
42+
cx.inst = ComponentInstance()
4243
return cx
4344

4445
def mk_str(s):
@@ -343,26 +344,24 @@ def test_roundtrip(t, v):
343344
definitions.MAX_FLAT_RESULTS = 16
344345

345346
ft = FuncType([t],[t])
346-
callee_instance = Instance()
347347
callee = lambda x: x
348348

349349
callee_heap = Heap(1000)
350350
callee_cx = mk_cx(callee_heap.memory, 'utf8', callee_heap.realloc, lambda x: () )
351-
lifted_callee = lambda args: canon_lift(callee_cx, callee_instance, callee, ft, args, True)
351+
lifted_callee = lambda args: canon_lift(callee_cx, callee, ft, args, True)
352352

353353
caller_heap = Heap(1000)
354-
caller_instance = Instance()
355354
caller_cx = mk_cx(caller_heap.memory, 'utf8', caller_heap.realloc, None)
356355

357356
flat_args = lower_flat(caller_cx, v, t)
358-
flat_results = canon_lower(caller_cx, caller_instance, lifted_callee, ft, flat_args)
357+
flat_results = canon_lower(caller_cx, lifted_callee, ft, flat_args)
359358
got = lift_flat(caller_cx, ValueIter(flat_results), t)
360359

361360
if got != v:
362361
fail("test_roundtrip({},{},{}) got {}".format(t, v, caller_args, got))
363362

364-
assert(caller_instance.may_leave and caller_instance.may_enter)
365-
assert(callee_instance.may_leave and callee_instance.may_enter)
363+
assert(caller_cx.inst.may_leave and caller_cx.inst.may_enter)
364+
assert(callee_cx.inst.may_leave and callee_cx.inst.may_enter)
366365
definitions.MAX_FLAT_RESULTS = before
367366

368367
test_roundtrip(S8(), -1)

0 commit comments

Comments
 (0)