Skip to content

Commit 76f0b8d

Browse files
committed
feat(host): submit async functions through generic drivers
1 parent 2ed098c commit 76f0b8d

15 files changed

Lines changed: 718 additions & 1762 deletions

File tree

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ name = "vm"
2727
[features]
2828
default = ["runtime", "cli", "cranelift-jit"]
2929
runtime = []
30-
http-client = ["runtime", "dep:reqwest", "dep:url", "dep:tokio", "dep:futures-util"]
30+
async = ["runtime", "dep:reqwest", "dep:url", "dep:tokio", "dep:futures-util"]
31+
http-client = ["async"]
3132
sqlite = ["runtime", "dep:rusqlite"]
3233
edge-abi = [
3334
"dep:edge_abi",

build.rs

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ struct CallableDecl {
115115
wrapper: Option<WrapperDecl>,
116116
host_binding_kind: HostBindingKind,
117117
host_execution: HostExecutionKind,
118+
runtime_owned_pending: bool,
118119
}
119120

120121
#[derive(Clone, Debug)]
@@ -155,17 +156,19 @@ fn main() {
155156
module: "host".to_string(),
156157
category: SourceCategory::DefaultHost,
157158
},
158-
SourceSpec {
159-
path: "src/builtins/runtime/http.rs".to_string(),
160-
module: "http".to_string(),
161-
category: SourceCategory::DefaultHost,
162-
},
163159
SourceSpec {
164160
path: "src/builtins/runtime/context_host.rs".to_string(),
165161
module: "context_host".to_string(),
166162
category: SourceCategory::DefaultHost,
167163
},
168164
];
165+
if env::var_os("CARGO_FEATURE_ASYNC").is_some() {
166+
host_sources.push(SourceSpec {
167+
path: "src/builtins/runtime/http.rs".to_string(),
168+
module: "http".to_string(),
169+
category: SourceCategory::DefaultHost,
170+
});
171+
}
169172
if env::var_os("CARGO_FEATURE_SQLITE").is_some() {
170173
host_sources.push(SourceSpec {
171174
path: "src/builtins/runtime/sqlite.rs".to_string(),
@@ -273,6 +276,9 @@ fn parse_sources(
273276
}
274277

275278
pub(crate) fn classify_host_binding(function: &ItemFn) -> HostBindingKind {
279+
if function.sig.asyncness.is_some() {
280+
return HostBindingKind::StaticStack;
281+
}
276282
if function.sig.inputs.iter().any(|input| match input {
277283
FnArg::Typed(pat_type) => is_vm_context_type(&pat_type.ty),
278284
_ => false,
@@ -298,6 +304,9 @@ pub(crate) fn classify_host_binding(function: &ItemFn) -> HostBindingKind {
298304
}
299305

300306
pub(crate) fn infer_host_execution(function: &ItemFn) -> HostExecutionKind {
307+
if function.sig.asyncness.is_some() {
308+
return HostExecutionKind::MaySuspend;
309+
}
301310
let return_type = normalized_return_type(&function.sig.output);
302311
if contains_host_call_result(&return_type) {
303312
HostExecutionKind::MaySuspend
@@ -444,6 +453,8 @@ fn parse_source_file(path: &Path, spec: &SourceSpec, _order_offset: usize) -> Ve
444453
wrapper,
445454
host_binding_kind: classify_host_binding(function),
446455
host_execution: infer_host_execution(function),
456+
runtime_owned_pending: function.sig.asyncness.is_none()
457+
&& contains_host_call_result(&normalized_return_type(&function.sig.output)),
447458
});
448459
}
449460
out
@@ -1095,12 +1106,14 @@ fn render_builtin_runtime_dispatch(
10951106
)
10961107
.unwrap();
10971108
}
1098-
writeln!(
1099-
&mut out,
1100-
" registry.mark_runtime_owned_pending({:?});",
1101-
callable.name
1102-
)
1103-
.unwrap();
1109+
if callable.runtime_owned_pending {
1110+
writeln!(
1111+
&mut out,
1112+
" registry.mark_runtime_owned_pending({:?});",
1113+
callable.name
1114+
)
1115+
.unwrap();
1116+
}
11041117
}
11051118
writeln!(&mut out, "}}").unwrap();
11061119
writeln!(&mut out).unwrap();
@@ -1117,12 +1130,14 @@ fn render_builtin_runtime_dispatch(
11171130
.render_bind_static_call(&callable.name, &host_wrapper_adapter_name(callable));
11181131
writeln!(&mut out, " {:?} => {{", callable.name).unwrap();
11191132
writeln!(&mut out, " {bind_call}").unwrap();
1120-
writeln!(
1121-
&mut out,
1122-
" vm.mark_runtime_owned_pending_binding({:?});",
1123-
callable.name
1124-
)
1125-
.unwrap();
1133+
if callable.runtime_owned_pending {
1134+
writeln!(
1135+
&mut out,
1136+
" vm.mark_runtime_owned_pending_binding({:?});",
1137+
callable.name
1138+
)
1139+
.unwrap();
1140+
}
11261141
writeln!(&mut out, " true").unwrap();
11271142
writeln!(&mut out, " }}").unwrap();
11281143
}
@@ -1891,6 +1906,9 @@ fn host_wrapper_adapter_name(callable: &CallableDecl) -> String {
18911906

18921907
fn generated_wrapper_decl(function: &ItemFn) -> WrapperDecl {
18931908
let mut params = Vec::new();
1909+
if function.sig.asyncness.is_some() {
1910+
params.push(WrapperParamKind::Vm);
1911+
}
18941912
for input in &function.sig.inputs {
18951913
let FnArg::Typed(pat_type) = input else {
18961914
panic!("methods are not supported in #[pd_host_function] declarations");
@@ -1917,6 +1935,13 @@ fn parse_callable_params(function: &ItemFn) -> Vec<CallableParamDecl> {
19171935
let FnArg::Typed(pat_type) = input else {
19181936
panic!("methods are not supported in #[pd_host_function] declarations");
19191937
};
1938+
if pat_type
1939+
.attrs
1940+
.iter()
1941+
.any(|attr| attr.path().is_ident("pd_host_context"))
1942+
{
1943+
return None;
1944+
}
19201945
if is_vm_context_type(&pat_type.ty) {
19211946
return None;
19221947
}

0 commit comments

Comments
 (0)