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
58 changes: 58 additions & 0 deletions Cargo.lock

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

15 changes: 14 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ members = [
"pd-vm-nostd",
"pd-vm-wasm",
"crates/rustscript",
"crates/pd-host-schema",
]
resolver = "2"

Expand All @@ -27,6 +28,7 @@ name = "vm"
[features]
default = ["runtime", "cli", "cranelift-jit"]
runtime = []
async = ["runtime", "dep:tokio"]
sqlite = ["runtime", "dep:rusqlite"]
edge-abi = [
"dep:edge_abi",
Expand Down Expand Up @@ -62,11 +64,12 @@ 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"
regex = "1"
serde = "1"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
rt-format = "0.3.1"
self_cell = "1"
Expand All @@ -87,5 +90,15 @@ name = "host_binding_generation_tests"
path = "tests/host_binding_generation_tests.rs"
required-features = ["cranelift-jit"]

[[test]]
name = "host_sdk_tests"
path = "tests/host_sdk_tests.rs"
required-features = ["runtime"]

[[test]]
name = "host_context_arch_tests"
path = "tests/host_context_arch_tests.rs"
required-features = ["runtime"]

[build-dependencies]
syn = { version = "2", features = ["full"] }
67 changes: 62 additions & 5 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,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 @@ -271,6 +282,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 @@ -296,6 +310,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 Down Expand Up @@ -979,6 +996,7 @@ fn render_builtin_catalog(
&actual_builtin_by_variant,
);
render_builtin_signature_method(&mut out, &builtin_variant_order);
render_builtin_capability_method(&mut out, builtin_callables);
writeln!(
&mut out,
" pub fn from_namespaced_name(name: &str) -> Option<Self> {{"
Expand Down Expand Up @@ -1553,6 +1571,35 @@ fn required_param_count(params: &[CallableParamDecl]) -> usize {
params.iter().take_while(|param| !param.optional).count()
}

fn render_builtin_capability_method(out: &mut String, builtin_callables: &[CallableDecl]) {
let mut capability_variants = Vec::new();
for callable in builtin_callables {
let variant = builtin_variant_name(&callable.name);
if !capability_variants.contains(&variant) {
capability_variants.push(variant);
}
}
capability_variants.sort();
writeln!(out, " #[cfg(feature = \"runtime\")]").unwrap();
writeln!(
out,
" pub(crate) const fn requires_explicit_host_capability(self) -> bool {{"
)
.unwrap();
if capability_variants.is_empty() {
writeln!(out, " false").unwrap();
} else {
let patterns = capability_variants
.iter()
.map(|variant| format!("BuiltinFunction::{variant}"))
.collect::<Vec<_>>()
.join(" | ");
writeln!(out, " matches!(self, {patterns})").unwrap();
}
writeln!(out, " }}").unwrap();
writeln!(out).unwrap();
}

fn stable_groups<F>(callables: &[CallableDecl], mut key_fn: F) -> Vec<Group<'_>>
where
F: FnMut(&CallableDecl) -> String,
Expand Down Expand Up @@ -1863,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 @@ -1889,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 Down Expand Up @@ -2055,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
12 changes: 12 additions & 0 deletions crates/pd-host-schema/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "pd-host-schema"
version.workspace = true
edition.workspace = true
description = "Shared host-schema parsing for the pd-host-function proc macro and the pd-vm build script"
license = "MIT"
homepage = "https://rustscript.org/"
repository = "https://github.com/rustscript-lang/rustscript"

[dependencies]
proc-macro2 = "1"
syn = { version = "2", features = ["full", "extra-traits"] }
Loading
Loading