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
1 change: 1 addition & 0 deletions Cargo.lock

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

15 changes: 15 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ name = "pd-vm-run"
path = "src/bin/pd-vm-run.rs"
required-features = ["cli"]

[[example]]
name = "mini_bench"
path = "examples/mini_bench.rs"
required-features = ["runtime"]

[[example]]
name = "collection_rebind_bench"
path = "examples/collection_rebind_bench.rs"
required-features = ["runtime"]

[[example]]
name = "rustscript_fuzz"
path = "examples/rustscript_fuzz.rs"
required-features = ["runtime"]

[dependencies]
base64 = "0.22"
cranelift-codegen = { version = "0.129.1", optional = true }
Expand Down
78 changes: 63 additions & 15 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1220,9 +1220,9 @@ fn render_callable_consts(callables: &[&CallableDecl]) -> String {
for param in &callable.params {
writeln!(
&mut out,
" CallableParam {{ name: {:?}, ty: CallableParamType::{}, optional: {} }},",
" CallableParam {{ name: {:?}, ty: {}, optional: {} }},",
param.name,
callable_param_variant(&param.ty_label),
callable_param_expr(&param.ty_label),
param.optional
)
.unwrap();
Expand Down Expand Up @@ -1645,20 +1645,39 @@ fn callable_const_base(callable: &CallableDecl) -> String {
to_shouty_snake(&format!("{prefix}_{}", callable.rust_ident))
}

fn callable_param_variant(label: &str) -> &'static str {
let label = label.strip_suffix(" | null").unwrap_or(label);
pub(crate) fn callable_param_expr(label: &str) -> String {
match label {
"any" => "Any",
"null" => "Null",
"int" => "Int",
"float" => "Float",
"bool" => "Bool",
"string" => "String",
"bytes" => "Bytes",
"array" => "Array",
"map" => "Map",
"number" => "Number",
"resource" => "Resource",
"any" => "CallableParamType::Any".to_string(),
"null" => "CallableParamType::Null".to_string(),
"int" => "CallableParamType::Int".to_string(),
"float" => "CallableParamType::Float".to_string(),
"bool" => "CallableParamType::Bool".to_string(),
"string" => "CallableParamType::String".to_string(),
"bytes" => "CallableParamType::Bytes".to_string(),
"array" => "CallableParamType::Array".to_string(),
"map" => "CallableParamType::Map".to_string(),
"number" => "CallableParamType::Number".to_string(),
"resource" => "CallableParamType::Resource".to_string(),
other if other.starts_with("fn(") => {
let (params, result) = other
.strip_prefix("fn(")
.and_then(|value| value.split_once(") -> "))
.unwrap_or_else(|| panic!("invalid callable schema '{other}'"));
let params = if params.is_empty() {
Vec::new()
} else {
params
.split(", ")
.map(callable_param_expr)
.collect::<Vec<_>>()
};
let result = callable_param_expr(result);
format!(
"CallableParamType::Callable(CallableType {{ params: &[{}], return_type: &{} }})",
params.join(", "),
result
)
}
other => panic!("unsupported callable param type '{other}'"),
}
}
Expand Down Expand Up @@ -2280,3 +2299,32 @@ mod tests {
}
}
}

#[cfg(test)]
mod callable_schema_tests {
use super::*;
use syn::parse_quote;

#[test]
fn build_metadata_renders_typed_callable_parameters() {
let ty: Type = parse_quote!(VmCallable<fn(VmMap) -> VmMap>);
assert_eq!(
pd_host_schema::type_label(&ty).expect("callable type should parse"),
"fn(map) -> map"
);
assert_eq!(
callable_param_expr("fn(map) -> map"),
"CallableParamType::Callable(CallableType { params: &[CallableParamType::Map], return_type: &CallableParamType::Map })"
);

let float_ty: Type = parse_quote!(VmCallable<fn(f64) -> f64>);
assert_eq!(
pd_host_schema::type_label(&float_ty).expect("callable type should parse"),
"fn(float) -> float"
);
assert_eq!(
callable_param_expr("fn(float) -> float"),
"CallableParamType::Callable(CallableType { params: &[CallableParamType::Float], return_type: &CallableParamType::Float })"
);
}
}
1 change: 1 addition & 0 deletions crates/rustscript/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ cranelift-jit = ["pd_vm_crate/cranelift-jit"]

[dependencies]
pd_vm_crate = { package = "pd-vm", path = "../..", version = ">=0.1.0, <1.0.0" }
serde_json = "1"
Loading
Loading