Skip to content

Commit 4d1030b

Browse files
committed
Rename start/return_thunk to on_start/return in CABI
1 parent 6d00d6b commit 4d1030b

File tree

3 files changed

+67
-67
lines changed

3 files changed

+67
-67
lines changed

design/mvp/CanonicalABI.md

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -681,16 +681,16 @@ async calls (until some active calls finish):
681681
```python
682682
class AsyncTask(Task):
683683
ft: FuncType
684-
start_thunk: Callable
685-
return_thunk: Callable
684+
on_start: Callable
685+
on_return: Callable
686686
state: AsyncCallState
687687
unblock_next_pending: bool
688688

689-
def __init__(self, opts, inst, caller, ft, start_thunk, return_thunk):
689+
def __init__(self, opts, inst, caller, ft, on_start, on_return):
690690
super().__init__(opts, inst, caller)
691691
self.ft = ft
692-
self.start_thunk = start_thunk
693-
self.return_thunk = return_thunk
692+
self.on_start = on_start
693+
self.on_return = on_return
694694
self.state = AsyncCallState.STARTING
695695
self.unblock_next_pending = False
696696

@@ -1925,12 +1925,12 @@ When instantiating component instance `$inst`:
19251925
The resulting function `$f` takes 3 runtime arguments:
19261926
* `caller`: the caller's `Task` or, if this lifted function is being called by
19271927
the host, `None`
1928-
* `start_thunk`: a nullary function that must be called to return the caller's
1928+
* `on_start`: a nullary function that must be called to return the caller's
19291929
arguments as a list of component-level values
1930-
* `return_thunk`: a unary function that must be called after `start_thunk`,
1930+
* `on_return`: a unary function that must be called after `on_start`,
19311931
passing the list of component-level return values
19321932

1933-
The indirection of `start_thunk` and `return_thunk` are used to model the
1933+
The indirection of `on_start` and `on_return` are used to model the
19341934
interleaving of reading arguments out of the caller's stack and memory and
19351935
writing results back into the caller's stack and memory, which will vary in
19361936
async calls.
@@ -1949,21 +1949,21 @@ by a *single host-agnostic component*.
19491949

19501950
Based on this, `canon_lift` is defined:
19511951
```python
1952-
async def canon_lift(opts, inst, callee, ft, caller, start_thunk, return_thunk):
1952+
async def canon_lift(opts, inst, callee, ft, caller, on_start, on_return):
19531953
if opts.sync:
19541954
task = SyncTask(opts, inst, caller)
19551955
await task.enter()
19561956

1957-
flat_args = lower_sync_values(task, MAX_FLAT_PARAMS, start_thunk(), ft.param_types())
1957+
flat_args = lower_sync_values(task, MAX_FLAT_PARAMS, on_start(), ft.param_types())
19581958
flat_results = await call_and_trap_on_throw(callee, task, flat_args)
1959-
return_thunk(lift_sync_values(task, MAX_FLAT_RESULTS, CoreValueIter(flat_results), ft.result_types()))
1959+
on_return(lift_sync_values(task, MAX_FLAT_RESULTS, CoreValueIter(flat_results), ft.result_types()))
19601960

19611961
if opts.post_return is not None:
19621962
[] = await call_and_trap_on_throw(opts.post_return, task, flat_results)
19631963

19641964
task.exit()
19651965
else:
1966-
task = AsyncTask(opts, inst, caller, ft, start_thunk, return_thunk)
1966+
task = AsyncTask(opts, inst, caller, ft, on_start, on_return)
19671967
await task.enter()
19681968

19691969
if not opts.callback:
@@ -1991,8 +1991,8 @@ async def call_and_trap_on_throw(callee, task, args):
19911991
```
19921992
The only fundamental difference between sync and async lifting is whether
19931993
parameters/results are automatically lowered/lifted (with `canon_lift` calling
1994-
`start_thunk` and `return_thunk`) or whether the `callee` explicitly triggers
1995-
`start_thunk`/`return_thunk` via `task.start`/`task.return` (defined below).
1994+
`on_start` and `on_return`) or whether the `callee` explicitly triggers
1995+
`on_start`/`on_return` via `task.start`/`task.return` (defined below).
19961996
The latter gives the callee the ability to explicitly apply backpressure (by
19971997
waiting before calling `task.start`) whereas the former applies "backpressure"
19981998
immediately if a sync call is already running. In both cases, backpressure is
@@ -2050,29 +2050,29 @@ async def canon_lower(opts, callee, ft, task, flat_args):
20502050
if opts.sync:
20512051
subtask = Subtask(opts, task.inst)
20522052

2053-
def start_thunk():
2053+
def on_start():
20542054
return lift_sync_values(subtask, MAX_FLAT_PARAMS, flat_args, ft.param_types())
20552055

2056-
def return_thunk(results):
2056+
def on_return(results):
20572057
nonlocal flat_results
20582058
flat_results = lower_sync_values(subtask, MAX_FLAT_RESULTS, results, ft.result_types(), flat_args)
20592059

2060-
await callee(task, start_thunk, return_thunk)
2060+
await callee(task, on_start, on_return)
20612061

20622062
subtask.finish()
20632063
else:
20642064
subtask = AsyncSubtask(opts, task.inst)
20652065

20662066
async def do_call():
2067-
def start_thunk():
2067+
def on_start():
20682068
subtask.start()
20692069
return lift_async_values(subtask, flat_args, ft.param_types())
20702070

2071-
def return_thunk(results):
2071+
def on_return(results):
20722072
subtask.return_()
20732073
lower_async_values(subtask, results, ft.result_types(), flat_args)
20742074

2075-
await callee(task, start_thunk, return_thunk)
2075+
await callee(task, on_start, on_return)
20762076
subtask.finish()
20772077

20782078
asyncio.get_event_loop().set_task_factory(asyncio.eager_task_factory)
@@ -2091,7 +2091,7 @@ async def canon_lower(opts, callee, ft, task, flat_args):
20912091
In the async case, the combination of `asyncio.create_task` with
20922092
`eager_task_factory` immediately start executing `do_call` without `await`ing
20932093
it. Following `create_task`, `do_call` has either completed eagerly (with
2094-
`return_thunk` having been called to write the return values into the
2094+
`on_return` having been called to write the return values into the
20952095
caller-supplied memory buffer), in which case the lowered function can simply
20962096
return `0` to indicate "done". Otherwise, the coroutine is still running, in
20972097
which case it is added to an instance-wide table of active async subtasks,
@@ -2246,7 +2246,7 @@ async def canon_task_start(task, core_ft, flat_args):
22462246
trap_if(task.opts.sync)
22472247
trap_if(core_ft != flatten_functype(CanonicalOptions(), FuncType([], task.ft.params), 'lower'))
22482248
task.start()
2249-
args = task.start_thunk()
2249+
args = task.on_start()
22502250
flat_results = lower_sync_values(task, MAX_FLAT_RESULTS, args, task.ft.param_types(), CoreValueIter(flat_args))
22512251
assert(len(core_ft.results) == len(flat_results))
22522252
return flat_results
@@ -2277,7 +2277,7 @@ async def canon_task_return(task, core_ft, flat_args):
22772277
trap_if(core_ft != flatten_functype(CanonicalOptions(), FuncType(task.ft.results, []), 'lower'))
22782278
task.return_()
22792279
results = lift_sync_values(task, MAX_FLAT_PARAMS, CoreValueIter(flat_args), task.ft.result_types())
2280-
task.return_thunk(results)
2280+
task.on_return(results)
22812281
assert(len(core_ft.results) == 0)
22822282
return []
22832283
```

design/mvp/canonical-abi/definitions.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -506,16 +506,16 @@ def exit(self):
506506

507507
class AsyncTask(Task):
508508
ft: FuncType
509-
start_thunk: Callable
510-
return_thunk: Callable
509+
on_start: Callable
510+
on_return: Callable
511511
state: AsyncCallState
512512
unblock_next_pending: bool
513513

514-
def __init__(self, opts, inst, caller, ft, start_thunk, return_thunk):
514+
def __init__(self, opts, inst, caller, ft, on_start, on_return):
515515
super().__init__(opts, inst, caller)
516516
self.ft = ft
517-
self.start_thunk = start_thunk
518-
self.return_thunk = return_thunk
517+
self.on_start = on_start
518+
self.on_return = on_return
519519
self.state = AsyncCallState.STARTING
520520
self.unblock_next_pending = False
521521

@@ -1357,21 +1357,21 @@ def lower_heap_values(cx, vs, ts, out_param):
13571357

13581358
### `canon lift`
13591359

1360-
async def canon_lift(opts, inst, callee, ft, caller, start_thunk, return_thunk):
1360+
async def canon_lift(opts, inst, callee, ft, caller, on_start, on_return):
13611361
if opts.sync:
13621362
task = SyncTask(opts, inst, caller)
13631363
await task.enter()
13641364

1365-
flat_args = lower_sync_values(task, MAX_FLAT_PARAMS, start_thunk(), ft.param_types())
1365+
flat_args = lower_sync_values(task, MAX_FLAT_PARAMS, on_start(), ft.param_types())
13661366
flat_results = await call_and_trap_on_throw(callee, task, flat_args)
1367-
return_thunk(lift_sync_values(task, MAX_FLAT_RESULTS, CoreValueIter(flat_results), ft.result_types()))
1367+
on_return(lift_sync_values(task, MAX_FLAT_RESULTS, CoreValueIter(flat_results), ft.result_types()))
13681368

13691369
if opts.post_return is not None:
13701370
[] = await call_and_trap_on_throw(opts.post_return, task, flat_results)
13711371

13721372
task.exit()
13731373
else:
1374-
task = AsyncTask(opts, inst, caller, ft, start_thunk, return_thunk)
1374+
task = AsyncTask(opts, inst, caller, ft, on_start, on_return)
13751375
await task.enter()
13761376

13771377
if not opts.callback:
@@ -1408,29 +1408,29 @@ async def canon_lower(opts, callee, ft, task, flat_args):
14081408
if opts.sync:
14091409
subtask = Subtask(opts, task.inst)
14101410

1411-
def start_thunk():
1411+
def on_start():
14121412
return lift_sync_values(subtask, MAX_FLAT_PARAMS, flat_args, ft.param_types())
14131413

1414-
def return_thunk(results):
1414+
def on_return(results):
14151415
nonlocal flat_results
14161416
flat_results = lower_sync_values(subtask, MAX_FLAT_RESULTS, results, ft.result_types(), flat_args)
14171417

1418-
await callee(task, start_thunk, return_thunk)
1418+
await callee(task, on_start, on_return)
14191419

14201420
subtask.finish()
14211421
else:
14221422
subtask = AsyncSubtask(opts, task.inst)
14231423

14241424
async def do_call():
1425-
def start_thunk():
1425+
def on_start():
14261426
subtask.start()
14271427
return lift_async_values(subtask, flat_args, ft.param_types())
14281428

1429-
def return_thunk(results):
1429+
def on_return(results):
14301430
subtask.return_()
14311431
lower_async_values(subtask, results, ft.result_types(), flat_args)
14321432

1433-
await callee(task, start_thunk, return_thunk)
1433+
await callee(task, on_start, on_return)
14341434
subtask.finish()
14351435

14361436
asyncio.get_event_loop().set_task_factory(asyncio.eager_task_factory)
@@ -1491,7 +1491,7 @@ async def canon_task_start(task, core_ft, flat_args):
14911491
trap_if(task.opts.sync)
14921492
trap_if(core_ft != flatten_functype(CanonicalOptions(), FuncType([], task.ft.params), 'lower'))
14931493
task.start()
1494-
args = task.start_thunk()
1494+
args = task.on_start()
14951495
flat_results = lower_sync_values(task, MAX_FLAT_RESULTS, args, task.ft.param_types(), CoreValueIter(flat_args))
14961496
assert(len(core_ft.results) == len(flat_results))
14971497
return flat_results
@@ -1504,7 +1504,7 @@ async def canon_task_return(task, core_ft, flat_args):
15041504
trap_if(core_ft != flatten_functype(CanonicalOptions(), FuncType(task.ft.results, []), 'lower'))
15051505
task.return_()
15061506
results = lift_sync_values(task, MAX_FLAT_PARAMS, CoreValueIter(flat_args), task.ft.result_types())
1507-
task.return_thunk(results)
1507+
task.on_return(results)
15081508
assert(len(core_ft.results) == 0)
15091509
return []
15101510

design/mvp/canonical-abi/run_tests.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -391,12 +391,12 @@ async def dtor(task, args):
391391
rt2 = ResourceType(inst, dtor) # only usable in exports
392392
opts = mk_opts()
393393

394-
async def host_import(entered, start_thunk, return_thunk):
395-
args = start_thunk()
394+
async def host_import(entered, on_start, on_return):
395+
args = on_start()
396396
assert(len(args) == 2)
397397
assert(args[0] == 42)
398398
assert(args[1] == 44)
399-
return_thunk([45])
399+
on_return([45])
400400

401401
async def core_wasm(task, args):
402402
nonlocal dtor_value
@@ -460,15 +460,15 @@ async def core_wasm(task, args):
460460
Own(rt)
461461
])
462462

463-
def arg_thunk():
463+
def on_start():
464464
return [ 42, 43, 44, 13 ]
465465

466466
got = None
467-
def return_thunk(results):
467+
def on_return(results):
468468
nonlocal got
469469
got = results
470470

471-
asyncio.run(canon_lift(opts, inst, core_wasm, ft, None, arg_thunk, return_thunk))
471+
asyncio.run(canon_lift(opts, inst, core_wasm, ft, None, on_start, on_return))
472472

473473
assert(len(got) == 3)
474474
assert(got[0] == 46)
@@ -498,13 +498,13 @@ async def core_eager_producer(task, args):
498498

499499
fut1, fut2, fut3 = asyncio.Future(), asyncio.Future(), asyncio.Future()
500500
blocking_ft = FuncType([U8(),U8()], [U8()])
501-
async def blocking_callee(entered, start_thunk, return_thunk):
501+
async def blocking_callee(entered, on_start, on_return):
502502
await fut1
503-
[x,y] = start_thunk()
503+
[x,y] = on_start()
504504
assert(x == 83)
505505
assert(y == 84)
506506
await fut2
507-
return_thunk([44])
507+
on_return([44])
508508
await fut3
509509

510510
consumer_heap = Heap(10)
@@ -577,16 +577,16 @@ async def dtor(task, args):
577577

578578
ft = FuncType([Bool()],[U8()])
579579

580-
def arg_thunk():
580+
def on_start():
581581
return [ True ]
582582

583583
got = None
584-
def return_thunk(results):
584+
def on_return(results):
585585
nonlocal got
586586
got = results
587587

588588
consumer_inst = ComponentInstance()
589-
await canon_lift(consumer_opts, consumer_inst, consumer, ft, None, arg_thunk, return_thunk)
589+
await canon_lift(consumer_opts, consumer_inst, consumer, ft, None, on_start, on_return)
590590
assert(len(got) == 1)
591591
assert(got[0] == 42)
592592

@@ -596,10 +596,10 @@ async def test_async_callback():
596596
producer_inst = ComponentInstance()
597597

598598
producer_ft = FuncType([], [])
599-
async def producer(fut, entered, start_thunk, return_thunk):
600-
start_thunk()
599+
async def producer(fut, entered, on_start, on_return):
600+
on_start()
601601
await fut
602-
return_thunk([])
602+
on_return([])
603603

604604
fut1 = asyncio.Future()
605605
producer1 = partial(producer, fut1)
@@ -639,18 +639,18 @@ async def callback(task, args):
639639
return [0]
640640

641641
consumer_inst = ComponentInstance()
642-
def arg_thunk(): return []
642+
def on_start(): return []
643643

644644
got = None
645-
def return_thunk(results):
645+
def on_return(results):
646646
nonlocal got
647647
got = results
648648

649649
opts = mk_opts()
650650
opts.sync = False
651651
opts.callback = callback
652652

653-
await canon_lift(opts, consumer_inst, consumer, consumer_ft, None, arg_thunk, return_thunk)
653+
await canon_lift(opts, consumer_inst, consumer, consumer_ft, None, on_start, on_return)
654654
assert(got[0] == 83)
655655

656656
asyncio.run(test_async_callback())
@@ -717,14 +717,14 @@ async def consumer(task, args):
717717
return []
718718

719719
consumer_inst = ComponentInstance()
720-
def arg_thunk(): return []
720+
def on_start(): return []
721721

722722
got = None
723-
def return_thunk(results):
723+
def on_return(results):
724724
nonlocal got
725725
got = results
726726

727-
await canon_lift(consumer_opts, consumer_inst, consumer, consumer_ft, None, arg_thunk, return_thunk)
727+
await canon_lift(consumer_opts, consumer_inst, consumer, consumer_ft, None, on_start, on_return)
728728
assert(got[0] == 83)
729729

730730
asyncio.run(test_async_to_sync())
@@ -733,10 +733,10 @@ async def test_sync_using_wait():
733733
inst = ComponentInstance()
734734
ft = FuncType([], [])
735735

736-
async def hostcall(fut, entered, start_thunk, return_thunk):
737-
start_thunk()
736+
async def hostcall(fut, entered, on_start, on_return):
737+
on_start()
738738
await fut
739-
return_thunk([])
739+
on_return([])
740740
fut1 = asyncio.Future()
741741
hostcall1 = partial(hostcall, fut1)
742742
fut2 = asyncio.Future()
@@ -761,9 +761,9 @@ async def core_func(task, args):
761761
assert(callidx == 2)
762762
return []
763763

764-
def arg_thunk(): return []
765-
def return_thunk(results): pass
766-
await canon_lift(mk_opts(), inst, core_func, ft, None, arg_thunk, return_thunk)
764+
def on_start(): return []
765+
def on_return(results): pass
766+
await canon_lift(mk_opts(), inst, core_func, ft, None, on_start, on_return)
767767

768768
asyncio.run(test_sync_using_wait())
769769

0 commit comments

Comments
 (0)