Skip to content

Commit a36f1be

Browse files
committed
add component-model-async/lower.wast test
This is another piece of bytecodealliance#9582 which I'm splitting out to make review easier. This test includes a minimal component which lowers an import with the `async` option. The rest of the changes fill in some TODOs to make the test pass. Signed-off-by: Joel Dice <[email protected]>
1 parent 54d77a4 commit a36f1be

File tree

6 files changed

+41
-5
lines changed

6 files changed

+41
-5
lines changed

crates/cranelift/src/compiler/component.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,6 @@ impl<'a> TrampolineCompiler<'a> {
280280
async_,
281281
} = *options;
282282

283-
if async_ {
284-
todo!()
285-
}
286-
287283
// vmctx: *mut VMComponentContext
288284
host_sig.params.push(ir::AbiParam::new(pointer_type));
289285
callee_args.push(vmctx);
@@ -350,6 +346,14 @@ impl<'a> TrampolineCompiler<'a> {
350346
.iconst(ir::types::I8, i64::from(string_encoding as u8)),
351347
);
352348

349+
// async_: bool
350+
host_sig.params.push(ir::AbiParam::new(ir::types::I8));
351+
callee_args.push(
352+
self.builder
353+
.ins()
354+
.iconst(ir::types::I8, if async_ { 1 } else { 0 }),
355+
);
356+
353357
// storage: *mut ValRaw
354358
host_sig.params.push(ir::AbiParam::new(pointer_type));
355359
callee_args.push(values_vec_ptr);

crates/wasmtime/src/runtime/component/func/host.rs

+14
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ impl HostFunc {
4646
memory: *mut VMMemoryDefinition,
4747
realloc: *mut VMFuncRef,
4848
string_encoding: u8,
49+
async_: u8,
4950
storage: NonNull<MaybeUninit<ValRaw>>,
5051
storage_len: usize,
5152
) -> bool
@@ -66,6 +67,7 @@ impl HostFunc {
6667
memory,
6768
realloc,
6869
StringEncoding::from_u8(string_encoding).unwrap(),
70+
async_ != 0,
6971
NonNull::slice_from_raw_parts(storage, storage_len).as_mut(),
7072
|store, args| (*data)(store, args),
7173
)
@@ -142,6 +144,7 @@ unsafe fn call_host<T, Params, Return, F>(
142144
memory: *mut VMMemoryDefinition,
143145
realloc: *mut VMFuncRef,
144146
string_encoding: StringEncoding,
147+
async_: bool,
145148
storage: &mut [MaybeUninit<ValRaw>],
146149
closure: F,
147150
) -> Result<()>
@@ -150,6 +153,10 @@ where
150153
Return: Lower,
151154
F: FnOnce(StoreContextMut<'_, T>, Params) -> Result<Return>,
152155
{
156+
if async_ {
157+
todo!()
158+
}
159+
153160
/// Representation of arguments to this function when a return pointer is in
154161
/// use, namely the argument list is followed by a single value which is the
155162
/// return pointer.
@@ -320,12 +327,17 @@ unsafe fn call_host_dynamic<T, F>(
320327
memory: *mut VMMemoryDefinition,
321328
realloc: *mut VMFuncRef,
322329
string_encoding: StringEncoding,
330+
async_: bool,
323331
storage: &mut [MaybeUninit<ValRaw>],
324332
closure: F,
325333
) -> Result<()>
326334
where
327335
F: FnOnce(StoreContextMut<'_, T>, &[Val], &mut [Val]) -> Result<()>,
328336
{
337+
if async_ {
338+
todo!()
339+
}
340+
329341
let options = Options::new(
330342
store.0.id(),
331343
NonNull::new(memory),
@@ -429,6 +441,7 @@ extern "C" fn dynamic_entrypoint<T, F>(
429441
memory: *mut VMMemoryDefinition,
430442
realloc: *mut VMFuncRef,
431443
string_encoding: u8,
444+
async_: u8,
432445
storage: NonNull<MaybeUninit<ValRaw>>,
433446
storage_len: usize,
434447
) -> bool
@@ -447,6 +460,7 @@ where
447460
memory,
448461
realloc,
449462
StringEncoding::from_u8(string_encoding).unwrap(),
463+
async_ != 0,
450464
NonNull::slice_from_raw_parts(storage, storage_len).as_mut(),
451465
|store, params, results| (*data)(store, params, results),
452466
)

crates/wasmtime/src/runtime/vm/component.rs

+2
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ pub struct ComponentInstance {
9494
/// option for the canonical ABI options.
9595
/// * `string_encoding` - this is the configured string encoding for the
9696
/// canonical ABI this lowering corresponds to.
97+
/// * `async_` - whether the caller is using the async ABI.
9798
/// * `args_and_results` - pointer to stack-allocated space in the caller where
9899
/// all the arguments are stored as well as where the results will be written
99100
/// to. The size and initialized bytes of this depends on the core wasm type
@@ -117,6 +118,7 @@ pub type VMLoweringCallee = extern "C" fn(
117118
opt_memory: *mut VMMemoryDefinition,
118119
opt_realloc: *mut VMFuncRef,
119120
string_encoding: u8,
121+
async_: u8,
120122
args_and_results: NonNull<mem::MaybeUninit<ValRaw>>,
121123
nargs_and_results: usize,
122124
) -> bool;

crates/wasmtime/src/runtime/vm/interpreter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ impl InterpreterRef<'_> {
379379
use wasmtime_environ::component::ComponentBuiltinFunctionIndex;
380380

381381
if id == const { HostCall::ComponentLowerImport.index() } {
382-
call!(@host VMLoweringCallee(nonnull, nonnull, u32, nonnull, ptr, ptr, u8, nonnull, size) -> bool);
382+
call!(@host VMLoweringCallee(nonnull, nonnull, u32, nonnull, ptr, ptr, u8, u8, nonnull, size) -> bool);
383383
}
384384

385385
macro_rules! component {

crates/wast/src/spectest.rs

+3
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ pub fn link_component_spectest<T>(linker: &mut component::Linker<T>) -> Result<(
9494
use wasmtime::component::{Resource, ResourceType};
9595

9696
let engine = linker.engine().clone();
97+
linker
98+
.root()
99+
.func_wrap("host-echo-u32", |_, v: (u32,)| Ok(v))?;
97100
linker
98101
.root()
99102
.func_wrap("host-return-two", |_, _: ()| Ok((2u32,)))?;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
;;! component_model_async = true
2+
3+
;; async lower
4+
(component
5+
(import "host-echo-u32" (func $foo (param "p1" u32) (result u32)))
6+
(core module $libc (memory (export "memory") 1))
7+
(core instance $libc (instantiate $libc))
8+
(core func $foo (canon lower (func $foo) async (memory $libc "memory")))
9+
(core module $m
10+
(func (import "" "foo") (param i32 i32) (result i32))
11+
)
12+
(core instance $i (instantiate $m (with "" (instance (export "foo" (func $foo))))))
13+
)

0 commit comments

Comments
 (0)