Skip to content

Commit c0ec7fd

Browse files
authored
Merge pull request #22 from OctoSense-org/vm-unify/core-on-ported
Build octoscript-core against the ported makepad VM
2 parents 9cf5add + 4ce5ba7 commit c0ec7fd

5 files changed

Lines changed: 91 additions & 60 deletions

File tree

‎Cargo.lock‎

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/octoscript-capabilities/Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ workspace = true
1313
blake3 = { version = "1.8.5", features = ["pure"] }
1414
getrandom = { version = "0.3.4", features = ["wasm_js"] }
1515
keyring = { version = "3.6.3", default-features = false, features = ["apple-native", "windows-native"], optional = true }
16-
makepad-script = { path = "../../vendor/makepad/platform/script" }
16+
makepad-script = { git = "https://github.com/OctoSense-org/makepad.git", rev = "1fdce273bf888239079754a1834734635eaed34f" }
1717
serde = { version = "1.0", features = ["derive"] }
1818
serde_json = "1.0.150"
1919
octoscript-core = { path = "../octoscript-core" }

‎crates/octoscript-capabilities/src/lib.rs‎

Lines changed: 56 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use std::time::{Duration, Instant};
1818

1919
use makepad_script::{
2020
id, id_lut, script_args_def, script_err_not_allowed, script_err_unexpected, script_value,
21-
LiveId, ScriptHandle, ScriptHandleGc, ScriptHandleType, ScriptIp, ScriptThreadId, ScriptValue,
22-
NIL,
21+
LiveId, ScriptHandle, ScriptHandleGc, ScriptHandleType, ScriptIp, ScriptStringSink,
22+
ScriptThreadId, ScriptValue, NIL,
2323
};
2424
use serde::{de::DeserializeOwned, Deserialize, Serialize};
2525
pub use serde_json::{json, Value as JsonValue};
@@ -5793,7 +5793,7 @@ fn install_capability_module(
57935793
)
57945794
}
57955795
};
5796-
let output = match vm.host.downcast_mut::<CapabilityHost>() {
5796+
let output = match vm.host.as_any_mut().downcast_mut::<CapabilityHost>() {
57975797
Some(host) => host.call_json(&tool, &input),
57985798
None => {
57995799
return script_err_unexpected!(
@@ -5841,7 +5841,7 @@ fn install_capability_module(
58415841
)
58425842
}
58435843
};
5844-
let result = match vm.host.downcast_mut::<CapabilityHost>() {
5844+
let result = match vm.host.as_any_mut().downcast_mut::<CapabilityHost>() {
58455845
Some(host) => host.begin_async_json(&tool, &input, max_pending_tools),
58465846
None => {
58475847
return script_err_unexpected!(
@@ -5904,7 +5904,7 @@ fn install_tool_module(runtime: &mut Runtime<CapabilityHost, ()>, max_pending_to
59045904
)
59055905
}
59065906
};
5907-
let pending = match vm.host.downcast_ref::<CapabilityHost>() {
5907+
let pending = match vm.host.as_any().downcast_ref::<CapabilityHost>() {
59085908
Some(host) => host.pending(),
59095909
None => {
59105910
return script_err_unexpected!(
@@ -5951,7 +5951,9 @@ fn install_tool_module(runtime: &mut Runtime<CapabilityHost, ()>, max_pending_to
59515951
match ready {
59525952
Some(Ok(output)) => match promise_output {
59535953
ToolPromiseOutput::Text => {
5954-
vm.new_string_with(|_, destination| destination.push_str(&output))
5954+
vm.bx.heap.new_bounded_string_with(|_, destination| {
5955+
destination.append_str(&output)
5956+
})
59555957
}
59565958
ToolPromiseOutput::DecodedJson {
59575959
max_output_bytes,
@@ -5985,22 +5987,25 @@ fn install_tool_module(runtime: &mut Runtime<CapabilityHost, ()>, max_pending_to
59855987
let name = script_text(vm, script_value!(vm, args.name));
59865988
let input = script_text(vm, script_value!(vm, args.input));
59875989
let result = match (name, input) {
5988-
(Ok(name), Ok(input)) => match vm.host.downcast_mut::<CapabilityHost>() {
5989-
Some(host) => host.call(&name, &input),
5990-
None => {
5991-
return script_err_unexpected!(
5992-
vm.bx.threads.cur_ref().trap,
5993-
"invalid Octoscript capability host"
5994-
)
5990+
(Ok(name), Ok(input)) => {
5991+
match vm.host.as_any_mut().downcast_mut::<CapabilityHost>() {
5992+
Some(host) => host.call(&name, &input),
5993+
None => {
5994+
return script_err_unexpected!(
5995+
vm.bx.threads.cur_ref().trap,
5996+
"invalid Octoscript capability host"
5997+
)
5998+
}
59955999
}
5996-
},
6000+
}
59976001
(Err(error), _) | (_, Err(error)) => Err(error),
59986002
};
59996003

60006004
match result {
6001-
Ok(output) => {
6002-
vm.new_string_with(|_, destination| destination.push_str(&output))
6003-
}
6005+
Ok(output) => vm
6006+
.bx
6007+
.heap
6008+
.new_bounded_string_with(|_, destination| destination.append_str(&output)),
60046009
Err(error) => {
60056010
script_err_not_allowed!(vm.bx.threads.cur_ref().trap, "{}", error)
60066011
}
@@ -6016,22 +6021,25 @@ fn install_tool_module(runtime: &mut Runtime<CapabilityHost, ()>, max_pending_to
60166021
let name = script_text(vm, script_value!(vm, args.name));
60176022
let input = script_json(vm, script_value!(vm, args.input));
60186023
let result = match (name, input) {
6019-
(Ok(name), Ok(input)) => match vm.host.downcast_mut::<CapabilityHost>() {
6020-
Some(host) => host.call_json(&name, &input),
6021-
None => {
6022-
return script_err_unexpected!(
6023-
vm.bx.threads.cur_ref().trap,
6024-
"invalid Octoscript capability host"
6025-
)
6024+
(Ok(name), Ok(input)) => {
6025+
match vm.host.as_any_mut().downcast_mut::<CapabilityHost>() {
6026+
Some(host) => host.call_json(&name, &input),
6027+
None => {
6028+
return script_err_unexpected!(
6029+
vm.bx.threads.cur_ref().trap,
6030+
"invalid Octoscript capability host"
6031+
)
6032+
}
60266033
}
6027-
},
6034+
}
60286035
(Err(error), _) | (_, Err(error)) => Err(error),
60296036
};
60306037

60316038
match result {
6032-
Ok(output) => {
6033-
vm.new_string_with(|_, destination| destination.push_str(&output))
6034-
}
6039+
Ok(output) => vm
6040+
.bx
6041+
.heap
6042+
.new_bounded_string_with(|_, destination| destination.append_str(&output)),
60356043
Err(error) => {
60366044
script_err_not_allowed!(vm.bx.threads.cur_ref().trap, "{}", error)
60376045
}
@@ -6047,15 +6055,17 @@ fn install_tool_module(runtime: &mut Runtime<CapabilityHost, ()>, max_pending_to
60476055
let name = script_text(vm, script_value!(vm, args.name));
60486056
let input = script_text(vm, script_value!(vm, args.input));
60496057
let result = match (name, input) {
6050-
(Ok(name), Ok(input)) => match vm.host.downcast_mut::<CapabilityHost>() {
6051-
Some(host) => host.begin_async(&name, &input, max_pending_tools),
6052-
None => {
6053-
return script_err_unexpected!(
6054-
vm.bx.threads.cur_ref().trap,
6055-
"invalid Octoscript capability host"
6056-
)
6058+
(Ok(name), Ok(input)) => {
6059+
match vm.host.as_any_mut().downcast_mut::<CapabilityHost>() {
6060+
Some(host) => host.begin_async(&name, &input, max_pending_tools),
6061+
None => {
6062+
return script_err_unexpected!(
6063+
vm.bx.threads.cur_ref().trap,
6064+
"invalid Octoscript capability host"
6065+
)
6066+
}
60576067
}
6058-
},
6068+
}
60596069
(Err(error), _) | (_, Err(error)) => Err(error),
60606070
};
60616071

@@ -6084,15 +6094,17 @@ fn install_tool_module(runtime: &mut Runtime<CapabilityHost, ()>, max_pending_to
60846094
let name = script_text(vm, script_value!(vm, args.name));
60856095
let input = script_json(vm, script_value!(vm, args.input));
60866096
let result = match (name, input) {
6087-
(Ok(name), Ok(input)) => match vm.host.downcast_mut::<CapabilityHost>() {
6088-
Some(host) => host.begin_async_json(&name, &input, max_pending_tools),
6089-
None => {
6090-
return script_err_unexpected!(
6091-
vm.bx.threads.cur_ref().trap,
6092-
"invalid Octoscript capability host"
6093-
)
6097+
(Ok(name), Ok(input)) => {
6098+
match vm.host.as_any_mut().downcast_mut::<CapabilityHost>() {
6099+
Some(host) => host.begin_async_json(&name, &input, max_pending_tools),
6100+
None => {
6101+
return script_err_unexpected!(
6102+
vm.bx.threads.cur_ref().trap,
6103+
"invalid Octoscript capability host"
6104+
)
6105+
}
60946106
}
6095-
},
6107+
}
60966108
(Err(error), _) | (_, Err(error)) => Err(error),
60976109
};
60986110

‎crates/octoscript-core/Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ workspace = true
1111

1212
[dependencies]
1313
octoscript-ui-l0 = { path = "../octoscript-ui-l0" }
14-
makepad-script = { path = "../../vendor/makepad/platform/script" }
14+
makepad-script = { git = "https://github.com/OctoSense-org/makepad.git", rev = "1fdce273bf888239079754a1834734635eaed34f" }
1515
serde_json = "1.0.150"

‎crates/octoscript-core/src/lib.rs‎

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -799,8 +799,7 @@ impl Evaluation {
799799
/// install native bindings through [`Runtime::configure`]; scripts only see
800800
/// the bindings that configuration creates.
801801
pub struct Runtime<H: Any = (), S: Any = ()> {
802-
host: H,
803-
std: S,
802+
host: vm::ScriptVmHost<H, S>,
804803
vm: Box<vm::ScriptVmBase>,
805804
limits: ExecutionLimits,
806805
json_method_limits: Rc<Cell<ScriptJsonMethodLimits>>,
@@ -818,8 +817,7 @@ impl<H: Any, S: Any> Runtime<H, S> {
818817
)));
819818
let installed_limits = json_method_limits.clone();
820819
let mut runtime = Self {
821-
host,
822-
std,
820+
host: vm::ScriptVmHost::new(host, std),
823821
vm: Box::new(vm::ScriptVmBase::new()),
824822
limits,
825823
json_method_limits,
@@ -894,11 +892,11 @@ impl<H: Any, S: Any> Runtime<H, S> {
894892
}
895893

896894
pub fn host(&self) -> &H {
897-
&self.host
895+
&self.host.host
898896
}
899897

900898
pub fn host_mut(&mut self) -> &mut H {
901-
&mut self.host
899+
&mut self.host.host
902900
}
903901

904902
/// Installs trusted native bindings. The standalone `std` module is frozen,
@@ -1215,7 +1213,6 @@ impl<H: Any, S: Any> Runtime<H, S> {
12151213
let previous_vm = std::mem::replace(&mut self.vm, Box::new(vm::ScriptVmBase::new()));
12161214
let mut vm = vm::ScriptVm {
12171215
host: &mut self.host,
1218-
std: &mut self.std,
12191216
bx: previous_vm,
12201217
};
12211218
let result = operation(&mut vm);
@@ -5499,13 +5496,15 @@ mod tests {
54995496

55005497
#[test]
55015498
fn canonical_newline_boundaries_are_lowered_for_vm_compatibility() {
5502-
// The inherited tokenizer sees the newline as whitespace and would
5503-
// otherwise parse `(42)` as a call on the imported module field.
5499+
// The ported VM (upstream makepad #1139) treats a `(` on a new line as
5500+
// the start of a new statement, so the inherited tokenizer no longer
5501+
// parses `(42)` as a call on the imported module field. Both the
5502+
// compatibility preflight and canonical lowering now agree.
55045503
let source = "use mod.std.a\n(42)\n";
55055504
let compatibility = check_vm_compatibility(source).unwrap();
55065505
let canonical = check_syntax(source).unwrap();
55075506

5508-
assert!(!compatibility.valid);
5507+
assert!(compatibility.valid, "{:?}", compatibility.diagnostics);
55095508
assert!(canonical.valid, "{:?}", canonical.diagnostics);
55105509
}
55115510

@@ -7720,10 +7719,12 @@ compute(outer, 2)
77207719
.iter()
77217720
.any(|diagnostic| diagnostic.contains("heap allocation limit")));
77227721

7723-
assert!(matches!(
7724-
runtime.eval("2"),
7725-
Err(RuntimeError::HeapLimitExceeded { maximum, .. }) if maximum == limits.max_heap_bytes
7726-
));
7722+
// The ported heap preflights the charge and never allocates the
7723+
// over-limit string, so the runtime is not left above its cap after
7724+
// the failure: the next evaluation runs, and stays within the limit.
7725+
let next = runtime.eval("2").unwrap();
7726+
assert!(next.completed(), "{:?}", next.diagnostics);
7727+
assert!(runtime.accounted_heap_bytes() <= limits.max_heap_bytes);
77277728
}
77287729

77297730
#[test]

0 commit comments

Comments
 (0)