Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -27,6 +27,7 @@ name = "vm"
[features]
default = ["runtime", "cli", "cranelift-jit"]
runtime = []
async = ["runtime", "dep:tokio"]
sqlite = ["runtime", "dep:rusqlite"]
edge-abi = [
"dep:edge_abi",
Expand DownExpand Up@@ -62,6 +63,7 @@ cranelift-module = { version = "0.129.1", optional = true }
cranelift-native = { version = "0.129.1", optional = true }
pd-host-function = { path = "./pd-host-function", version = "0.1.0" }
rusqlite = { version = "0.32", default-features = false, features = ["bundled", "hooks", "limits"], optional = true }
tokio = { version = "1", features = ["rt-multi-thread", "net", "time", "sync", "fs", "io-util", "process"], optional = true }
edge_abi = { package = "pd-edge-abi", version = "0.1.1", default-features = false, optional = true }
futures-channel = "0.3"
paste = "1"
Expand Down
68 changes: 51 additions & 17 deletions build.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -115,6 +115,7 @@ struct CallableDecl {
wrapper: Option<WrapperDecl>,
host_binding_kind: HostBindingKind,
host_execution: HostExecutionKind,
runtime_owned_pending: bool,
}

#[derive(Clone, Debug)]
Expand DownExpand Up@@ -243,10 +244,21 @@ fn write_generated_file(path: &Path, contents: &str) {
fn builtin_source_specs(namespaces: &[NamespaceDecl]) -> Vec<SourceSpec> {
namespaces
.iter()
.map(|namespace| SourceSpec {
path: format!("src/builtins/runtime/{}.rs", namespace.module),
module: namespace.module.clone(),
category: SourceCategory::NamespacedBuiltin,
.map(|namespace| {
let path = if namespace.module == "io" {
if cfg!(feature = "async") {
"src/builtins/runtime/io/async_io.rs".to_string()
} else {
"src/builtins/runtime/io/blocking.rs".to_string()
}
} else {
format!("src/builtins/runtime/{}.rs", namespace.module)
};
SourceSpec {
path,
module: namespace.module.clone(),
category: SourceCategory::NamespacedBuiltin,
}
})
.collect()
}
Expand All@@ -268,6 +280,9 @@ fn parse_sources(
}

pub(crate) fn classify_host_binding(function: &ItemFn) -> HostBindingKind {
if function.sig.asyncness.is_some() {
return HostBindingKind::StaticStack;
}
if function.sig.inputs.iter().any(|input| match input {
FnArg::Typed(pat_type) => is_vm_context_type(&pat_type.ty),
_ => false,
Expand All@@ -293,6 +308,9 @@ pub(crate) fn classify_host_binding(function: &ItemFn) -> HostBindingKind {
}

pub(crate) fn infer_host_execution(function: &ItemFn) -> HostExecutionKind {
if function.sig.asyncness.is_some() {
return HostExecutionKind::MaySuspend;
}
let return_type = normalized_return_type(&function.sig.output);
if contains_host_call_result(&return_type) {
HostExecutionKind::MaySuspend
Expand DownExpand Up@@ -439,6 +457,8 @@ fn parse_source_file(path: &Path, spec: &SourceSpec, _order_offset: usize) -> Ve
wrapper,
host_binding_kind: classify_host_binding(function),
host_execution: infer_host_execution(function),
runtime_owned_pending: function.sig.asyncness.is_none()
&& contains_host_call_result(&normalized_return_type(&function.sig.output)),
});
}
out
Expand DownExpand Up@@ -1090,12 +1110,14 @@ fn render_builtin_runtime_dispatch(
)
.unwrap();
}
writeln!(
&mut out,
" registry.mark_runtime_owned_pending({:?});",
callable.name
)
.unwrap();
if callable.runtime_owned_pending {
writeln!(
&mut out,
" registry.mark_runtime_owned_pending({:?});",
callable.name
)
.unwrap();
}
}
writeln!(&mut out, "}}").unwrap();
writeln!(&mut out).unwrap();
Expand All@@ -1112,12 +1134,14 @@ fn render_builtin_runtime_dispatch(
.render_bind_static_call(&callable.name, &host_wrapper_adapter_name(callable));
writeln!(&mut out, " {:?} => {{", callable.name).unwrap();
writeln!(&mut out, " {bind_call}").unwrap();
writeln!(
&mut out,
" vm.mark_runtime_owned_pending_binding({:?});",
callable.name
)
.unwrap();
if callable.runtime_owned_pending {
writeln!(
&mut out,
" vm.mark_runtime_owned_pending_binding({:?});",
callable.name
)
.unwrap();
}
writeln!(&mut out, " true").unwrap();
writeln!(&mut out, " }}").unwrap();
}
Expand DownExpand Up@@ -1886,6 +1910,9 @@ fn host_wrapper_adapter_name(callable: &CallableDecl) -> String {

fn generated_wrapper_decl(function: &ItemFn) -> WrapperDecl {
let mut params = Vec::new();
if function.sig.asyncness.is_some() {
params.push(WrapperParamKind::Vm);
}
for input in &function.sig.inputs {
let FnArg::Typed(pat_type) = input else {
panic!("methods are not supported in #[pd_host_function] declarations");
Expand All@@ -1912,6 +1939,13 @@ fn parse_callable_params(function: &ItemFn) -> Vec<CallableParamDecl> {
let FnArg::Typed(pat_type) = input else {
panic!("methods are not supported in #[pd_host_function] declarations");
};
if pat_type
.attrs
.iter()
.any(|attr| attr.path().is_ident("pd_host_context"))
{
return None;
}
if is_vm_context_type(&pat_type.ty) {
return None;
}
Expand DownExpand Up@@ -2078,7 +2112,7 @@ fn type_label(ty: &Type) -> String {
};
format!("{} | null", type_label(inner))
}
"VmResult" | "HostCallResult" => {
"VmResult" | "HostCallResult" | "HostFutureOutput" => {
let syn::PathArguments::AngleBracketed(args) = &segment.arguments else {
panic!("{ident}<T> requires one generic argument");
};
Expand Down
3 changes: 3 additions & 0 deletions crates/rustscript/tests/alias_smoke.rs
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
#[cfg(feature = "sqlite")]
use rustscript::SqliteHostExt;

/// Verify that the `rustscript` alias crate re-exports the same API as `pd-vm`.
#[test]
fn alias_exports_compile_source() {
Expand Down
Loading
Loading