use {
crate::{
AsContextMut, ValRaw,
component::{HasData, HasSelf, Instance},
store::StoreInner,
vm::{VMFuncRef, VMMemoryDefinition, VMStore, component::ComponentInstance},
},
anyhow::Result,
futures::{FutureExt, stream::FuturesUnordered},
std::{boxed::Box, future::Future, mem::MaybeUninit, pin::Pin},
wasmtime_environ::component::{
RuntimeComponentInstanceIndex, TypeComponentLocalErrorContextTableIndex,
TypeFutureTableIndex, TypeStreamTableIndex, TypeTupleIndex,
},
};
pub(crate) use futures_and_streams::ResourcePair;
pub use futures_and_streams::{ErrorContext, FutureReader, StreamReader};
mod futures_and_streams;
pub struct Promise<T>(Pin<Box<dyn Future<Output = T> + Send + Sync + 'static>>);
impl<T: 'static> Promise<T> {
pub fn map<U>(self, fun: impl FnOnce(T) -> U + Send + Sync + 'static) -> Promise<U> {
Promise(Box::pin(self.0.map(fun)))
}
pub async fn get<U: Send>(self, store: impl AsContextMut<Data = U>) -> Result<T> {
_ = store;
todo!()
}
pub fn into_future(self) -> Pin<Box<dyn Future<Output = T> + Send + Sync + 'static>> {
self.0
}
}
pub struct PromisesUnordered<T>(
FuturesUnordered<Pin<Box<dyn Future<Output = T> + Send + Sync + 'static>>>,
);
impl<T: 'static> PromisesUnordered<T> {
pub fn new() -> Self {
Self(FuturesUnordered::new())
}
pub fn push(&mut self, promise: Promise<T>) {
self.0.push(promise.0)
}
pub async fn next<U: Send>(&mut self, store: impl AsContextMut<Data = U>) -> Result<Option<T>> {
_ = store;
todo!()
}
}
pub struct Accessor<T: 'static, D = HasSelf<T>>
where
D: HasData,
{
#[expect(dead_code, reason = "to be used in the future")]
get: fn() -> *mut dyn VMStore,
#[expect(dead_code, reason = "to be used in the future")]
get_data: fn(&mut T) -> D::Data<'_>,
#[expect(dead_code, reason = "to be used in the future")]
instance: Instance,
}
impl<T, D> Accessor<T, D>
where
D: HasData,
{
#[doc(hidden)]
pub fn with_data<D2: HasData>(
&mut self,
get_data: fn(&mut T) -> D2::Data<'_>,
) -> Accessor<T, D2> {
let _ = get_data;
todo!()
}
}
pub unsafe trait VMComponentAsyncStore {
fn backpressure_set(
&mut self,
caller_instance: RuntimeComponentInstanceIndex,
enabled: u32,
) -> Result<()>;
fn task_return(
&mut self,
instance: &mut ComponentInstance,
ty: TypeTupleIndex,
storage: *mut ValRaw,
storage_len: usize,
) -> Result<()>;
fn waitable_set_new(
&mut self,
instance: &mut ComponentInstance,
caller_instance: RuntimeComponentInstanceIndex,
) -> Result<u32>;
fn waitable_set_wait(
&mut self,
instance: &mut ComponentInstance,
caller_instance: RuntimeComponentInstanceIndex,
set: u32,
async_: bool,
memory: *mut VMMemoryDefinition,
payload: u32,
) -> Result<u32>;
fn waitable_set_poll(
&mut self,
instance: &mut ComponentInstance,
caller_instance: RuntimeComponentInstanceIndex,
set: u32,
async_: bool,
memory: *mut VMMemoryDefinition,
payload: u32,
) -> Result<u32>;
fn waitable_set_drop(
&mut self,
instance: &mut ComponentInstance,
caller_instance: RuntimeComponentInstanceIndex,
set: u32,
) -> Result<()>;
fn waitable_join(
&mut self,
instance: &mut ComponentInstance,
caller_instance: RuntimeComponentInstanceIndex,
set: u32,
waitable: u32,
) -> Result<()>;
fn yield_(&mut self, instance: &mut ComponentInstance, async_: bool) -> Result<()>;
fn subtask_drop(
&mut self,
instance: &mut ComponentInstance,
caller_instance: RuntimeComponentInstanceIndex,
task_id: u32,
) -> Result<()>;
fn sync_enter(
&mut self,
start: *mut VMFuncRef,
return_: *mut VMFuncRef,
caller_instance: RuntimeComponentInstanceIndex,
task_return_type: TypeTupleIndex,
result_count: u32,
storage: *mut ValRaw,
storage_len: usize,
) -> Result<()>;
fn sync_exit(
&mut self,
instance: &mut ComponentInstance,
callback: *mut VMFuncRef,
caller_instance: RuntimeComponentInstanceIndex,
callee: *mut VMFuncRef,
callee_instance: RuntimeComponentInstanceIndex,
param_count: u32,
storage: *mut MaybeUninit<ValRaw>,
storage_len: usize,
) -> Result<()>;
fn async_enter(
&mut self,
start: *mut VMFuncRef,
return_: *mut VMFuncRef,
caller_instance: RuntimeComponentInstanceIndex,
task_return_type: TypeTupleIndex,
params: u32,
results: u32,
) -> Result<()>;
fn async_exit(
&mut self,
instance: &mut ComponentInstance,
callback: *mut VMFuncRef,
post_return: *mut VMFuncRef,
caller_instance: RuntimeComponentInstanceIndex,
callee: *mut VMFuncRef,
callee_instance: RuntimeComponentInstanceIndex,
param_count: u32,
result_count: u32,
flags: u32,
) -> Result<u32>;
fn future_new(
&mut self,
instance: &mut ComponentInstance,
ty: TypeFutureTableIndex,
) -> Result<u32>;
fn future_write(
&mut self,
instance: &mut ComponentInstance,
memory: *mut VMMemoryDefinition,
realloc: *mut VMFuncRef,
string_encoding: u8,
ty: TypeFutureTableIndex,
future: u32,
address: u32,
) -> Result<u32>;
fn future_read(
&mut self,
instance: &mut ComponentInstance,
memory: *mut VMMemoryDefinition,
realloc: *mut VMFuncRef,
string_encoding: u8,
ty: TypeFutureTableIndex,
future: u32,
address: u32,
) -> Result<u32>;
fn future_cancel_write(
&mut self,
instance: &mut ComponentInstance,
ty: TypeFutureTableIndex,
async_: bool,
writer: u32,
) -> Result<u32>;
fn future_cancel_read(
&mut self,
instance: &mut ComponentInstance,
ty: TypeFutureTableIndex,
async_: bool,
reader: u32,
) -> Result<u32>;
fn future_drop_writable(
&mut self,
instance: &mut ComponentInstance,
ty: TypeFutureTableIndex,
writer: u32,
) -> Result<()>;
fn future_drop_readable(
&mut self,
instance: &mut ComponentInstance,
ty: TypeFutureTableIndex,
reader: u32,
) -> Result<()>;
fn stream_new(
&mut self,
instance: &mut ComponentInstance,
ty: TypeStreamTableIndex,
) -> Result<u32>;
fn stream_write(
&mut self,
instance: &mut ComponentInstance,
memory: *mut VMMemoryDefinition,
realloc: *mut VMFuncRef,
string_encoding: u8,
ty: TypeStreamTableIndex,
stream: u32,
address: u32,
count: u32,
) -> Result<u32>;
fn stream_read(
&mut self,
instance: &mut ComponentInstance,
memory: *mut VMMemoryDefinition,
realloc: *mut VMFuncRef,
string_encoding: u8,
ty: TypeStreamTableIndex,
stream: u32,
address: u32,
count: u32,
) -> Result<u32>;
fn stream_cancel_write(
&mut self,
instance: &mut ComponentInstance,
ty: TypeStreamTableIndex,
async_: bool,
writer: u32,
) -> Result<u32>;
fn stream_cancel_read(
&mut self,
instance: &mut ComponentInstance,
ty: TypeStreamTableIndex,
async_: bool,
reader: u32,
) -> Result<u32>;
fn stream_drop_writable(
&mut self,
instance: &mut ComponentInstance,
ty: TypeStreamTableIndex,
writer: u32,
) -> Result<()>;
fn stream_drop_readable(
&mut self,
instance: &mut ComponentInstance,
ty: TypeStreamTableIndex,
reader: u32,
) -> Result<()>;
fn flat_stream_write(
&mut self,
instance: &mut ComponentInstance,
memory: *mut VMMemoryDefinition,
realloc: *mut VMFuncRef,
ty: TypeStreamTableIndex,
payload_size: u32,
payload_align: u32,
stream: u32,
address: u32,
count: u32,
) -> Result<u32>;
fn flat_stream_read(
&mut self,
instance: &mut ComponentInstance,
memory: *mut VMMemoryDefinition,
realloc: *mut VMFuncRef,
ty: TypeStreamTableIndex,
payload_size: u32,
payload_align: u32,
stream: u32,
address: u32,
count: u32,
) -> Result<u32>;
fn error_context_new(
&mut self,
instance: &mut ComponentInstance,
memory: *mut VMMemoryDefinition,
realloc: *mut VMFuncRef,
string_encoding: u8,
ty: TypeComponentLocalErrorContextTableIndex,
debug_msg_address: u32,
debug_msg_len: u32,
) -> Result<u32>;
fn error_context_debug_message(
&mut self,
instance: &mut ComponentInstance,
memory: *mut VMMemoryDefinition,
realloc: *mut VMFuncRef,
string_encoding: u8,
ty: TypeComponentLocalErrorContextTableIndex,
err_ctx_handle: u32,
debug_msg_address: u32,
) -> Result<()>;
fn error_context_drop(
&mut self,
instance: &mut ComponentInstance,
ty: TypeComponentLocalErrorContextTableIndex,
err_ctx_handle: u32,
) -> Result<()>;
}
unsafe impl<T> VMComponentAsyncStore for StoreInner<T> {
fn backpressure_set(
&mut self,
caller_instance: RuntimeComponentInstanceIndex,
enabled: u32,
) -> Result<()> {
_ = (caller_instance, enabled);
todo!()
}
fn task_return(
&mut self,
instance: &mut ComponentInstance,
ty: TypeTupleIndex,
storage: *mut ValRaw,
storage_len: usize,
) -> Result<()> {
_ = (instance, ty, storage, storage_len);
todo!()
}
fn waitable_set_new(
&mut self,
instance: &mut ComponentInstance,
caller_instance: RuntimeComponentInstanceIndex,
) -> Result<u32> {
_ = (instance, caller_instance);
todo!();
}
fn waitable_set_wait(
&mut self,
instance: &mut ComponentInstance,
caller_instance: RuntimeComponentInstanceIndex,
set: u32,
async_: bool,
memory: *mut VMMemoryDefinition,
payload: u32,
) -> Result<u32> {
_ = (instance, caller_instance, set, async_, memory, payload);
todo!();
}
fn waitable_set_poll(
&mut self,
instance: &mut ComponentInstance,
caller_instance: RuntimeComponentInstanceIndex,
set: u32,
async_: bool,
memory: *mut VMMemoryDefinition,
payload: u32,
) -> Result<u32> {
_ = (instance, caller_instance, set, async_, memory, payload);
todo!();
}
fn waitable_set_drop(
&mut self,
instance: &mut ComponentInstance,
caller_instance: RuntimeComponentInstanceIndex,
set: u32,
) -> Result<()> {
_ = (instance, caller_instance, set);
todo!();
}
fn waitable_join(
&mut self,
instance: &mut ComponentInstance,
caller_instance: RuntimeComponentInstanceIndex,
set: u32,
waitable: u32,
) -> Result<()> {
_ = (instance, caller_instance, set, waitable);
todo!();
}
fn yield_(&mut self, instance: &mut ComponentInstance, async_: bool) -> Result<()> {
_ = (instance, async_);
todo!()
}
fn subtask_drop(
&mut self,
instance: &mut ComponentInstance,
caller_instance: RuntimeComponentInstanceIndex,
task_id: u32,
) -> Result<()> {
_ = (instance, caller_instance, task_id);
todo!()
}
fn sync_enter(
&mut self,
start: *mut VMFuncRef,
return_: *mut VMFuncRef,
caller_instance: RuntimeComponentInstanceIndex,
task_return_type: TypeTupleIndex,
result_count: u32,
storage: *mut ValRaw,
storage_len: usize,
) -> Result<()> {
_ = (
start,
return_,
caller_instance,
task_return_type,
result_count,
storage,
storage_len,
);
todo!()
}
fn sync_exit(
&mut self,
instance: &mut ComponentInstance,
callback: *mut VMFuncRef,
caller_instance: RuntimeComponentInstanceIndex,
callee: *mut VMFuncRef,
callee_instance: RuntimeComponentInstanceIndex,
param_count: u32,
storage: *mut MaybeUninit<ValRaw>,
storage_len: usize,
) -> Result<()> {
_ = (
instance,
callback,
caller_instance,
callee,
callee_instance,
param_count,
storage,
storage_len,
);
todo!()
}
fn async_enter(
&mut self,
start: *mut VMFuncRef,
return_: *mut VMFuncRef,
caller_instance: RuntimeComponentInstanceIndex,
task_return_type: TypeTupleIndex,
params: u32,
results: u32,
) -> Result<()> {
_ = (
start,
return_,
caller_instance,
task_return_type,
params,
results,
);
todo!()
}
fn async_exit(
&mut self,
instance: &mut ComponentInstance,
callback: *mut VMFuncRef,
post_return: *mut VMFuncRef,
caller_instance: RuntimeComponentInstanceIndex,
callee: *mut VMFuncRef,
callee_instance: RuntimeComponentInstanceIndex,
param_count: u32,
result_count: u32,
flags: u32,
) -> Result<u32> {
_ = (
instance,
callback,
post_return,
caller_instance,
callee,
callee_instance,
param_count,
result_count,
flags,
);
todo!()
}
fn future_new(
&mut self,
instance: &mut ComponentInstance,
ty: TypeFutureTableIndex,
) -> Result<u32> {
_ = (instance, ty);
todo!()
}
fn future_write(
&mut self,
instance: &mut ComponentInstance,
memory: *mut VMMemoryDefinition,
realloc: *mut VMFuncRef,
string_encoding: u8,
ty: TypeFutureTableIndex,
future: u32,
address: u32,
) -> Result<u32> {
_ = (
instance,
memory,
realloc,
string_encoding,
ty,
future,
address,
);
todo!()
}
fn future_read(
&mut self,
instance: &mut ComponentInstance,
memory: *mut VMMemoryDefinition,
realloc: *mut VMFuncRef,
string_encoding: u8,
ty: TypeFutureTableIndex,
future: u32,
address: u32,
) -> Result<u32> {
_ = (
instance,
memory,
realloc,
string_encoding,
ty,
future,
address,
);
todo!()
}
fn future_cancel_write(
&mut self,
instance: &mut ComponentInstance,
ty: TypeFutureTableIndex,
async_: bool,
writer: u32,
) -> Result<u32> {
_ = (instance, ty, async_, writer);
todo!()
}
fn future_cancel_read(
&mut self,
instance: &mut ComponentInstance,
ty: TypeFutureTableIndex,
async_: bool,
reader: u32,
) -> Result<u32> {
_ = (instance, ty, async_, reader);
todo!()
}
fn future_drop_writable(
&mut self,
instance: &mut ComponentInstance,
ty: TypeFutureTableIndex,
writer: u32,
) -> Result<()> {
_ = (instance, ty, writer);
todo!()
}
fn future_drop_readable(
&mut self,
instance: &mut ComponentInstance,
ty: TypeFutureTableIndex,
reader: u32,
) -> Result<()> {
_ = (instance, ty, reader);
todo!()
}
fn stream_new(
&mut self,
instance: &mut ComponentInstance,
ty: TypeStreamTableIndex,
) -> Result<u32> {
_ = (instance, ty);
todo!()
}
fn stream_write(
&mut self,
instance: &mut ComponentInstance,
memory: *mut VMMemoryDefinition,
realloc: *mut VMFuncRef,
string_encoding: u8,
ty: TypeStreamTableIndex,
stream: u32,
address: u32,
count: u32,
) -> Result<u32> {
_ = (
instance,
memory,
realloc,
string_encoding,
ty,
stream,
address,
count,
);
todo!()
}
fn stream_read(
&mut self,
instance: &mut ComponentInstance,
memory: *mut VMMemoryDefinition,
realloc: *mut VMFuncRef,
string_encoding: u8,
ty: TypeStreamTableIndex,
stream: u32,
address: u32,
count: u32,
) -> Result<u32> {
_ = (
instance,
memory,
realloc,
string_encoding,
ty,
stream,
address,
count,
);
todo!()
}
fn stream_cancel_write(
&mut self,
instance: &mut ComponentInstance,
ty: TypeStreamTableIndex,
async_: bool,
writer: u32,
) -> Result<u32> {
_ = (instance, ty, async_, writer);
todo!()
}
fn stream_cancel_read(
&mut self,
instance: &mut ComponentInstance,
ty: TypeStreamTableIndex,
async_: bool,
reader: u32,
) -> Result<u32> {
_ = (instance, ty, async_, reader);
todo!()
}
fn stream_drop_writable(
&mut self,
instance: &mut ComponentInstance,
ty: TypeStreamTableIndex,
writer: u32,
) -> Result<()> {
_ = (instance, ty, writer);
todo!()
}
fn stream_drop_readable(
&mut self,
instance: &mut ComponentInstance,
ty: TypeStreamTableIndex,
reader: u32,
) -> Result<()> {
_ = (instance, ty, reader);
todo!()
}
fn flat_stream_write(
&mut self,
instance: &mut ComponentInstance,
memory: *mut VMMemoryDefinition,
realloc: *mut VMFuncRef,
ty: TypeStreamTableIndex,
payload_size: u32,
payload_align: u32,
stream: u32,
address: u32,
count: u32,
) -> Result<u32> {
_ = (
instance,
memory,
realloc,
ty,
payload_size,
payload_align,
stream,
address,
count,
);
todo!()
}
fn flat_stream_read(
&mut self,
instance: &mut ComponentInstance,
memory: *mut VMMemoryDefinition,
realloc: *mut VMFuncRef,
ty: TypeStreamTableIndex,
payload_size: u32,
payload_align: u32,
stream: u32,
address: u32,
count: u32,
) -> Result<u32> {
_ = (
instance,
memory,
realloc,
ty,
payload_size,
payload_align,
stream,
address,
count,
);
todo!()
}
fn error_context_new(
&mut self,
instance: &mut ComponentInstance,
memory: *mut VMMemoryDefinition,
realloc: *mut VMFuncRef,
string_encoding: u8,
ty: TypeComponentLocalErrorContextTableIndex,
debug_msg_address: u32,
debug_msg_len: u32,
) -> Result<u32> {
_ = (
instance,
memory,
realloc,
string_encoding,
ty,
debug_msg_address,
debug_msg_len,
);
todo!()
}
fn error_context_debug_message(
&mut self,
instance: &mut ComponentInstance,
memory: *mut VMMemoryDefinition,
realloc: *mut VMFuncRef,
string_encoding: u8,
ty: TypeComponentLocalErrorContextTableIndex,
err_ctx_handle: u32,
debug_msg_address: u32,
) -> Result<()> {
_ = (
instance,
memory,
realloc,
string_encoding,
ty,
err_ctx_handle,
debug_msg_address,
);
todo!()
}
fn error_context_drop(
&mut self,
instance: &mut ComponentInstance,
ty: TypeComponentLocalErrorContextTableIndex,
err_ctx_handle: u32,
) -> Result<()> {
_ = (instance, ty, err_ctx_handle);
todo!()
}
}