Skip to content
Merged
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
50 changes: 10 additions & 40 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ insta = { version = "1.46.3", features = ["yaml"] }
divan = "0.1.21"

# Python bindings
pyo3 = { version = "0.24", features = ["extension-module"] }
pyo3 = { version = "0.29", features = ["extension-module"] }

# WASM bindings
wasm-bindgen = "0.2"
Expand Down
30 changes: 29 additions & 1 deletion crates/zapcode-core/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,35 @@ fn wrap_trailing_object(source: &str) -> String {
return source.to_string();
}
// If preceded by a keyword that takes a block, don't wrap
let last_word = before
// Strip trailing parenthesized groups before extracting last_word
// e.g. "if (true)" → "if", "for (let i=0; i<10; i++)" → "for"
let before_for_keyword = {
let mut s = before.as_bytes();
while s.last() == Some(&b')') {
if s.len() < 2 {
break;
}
let mut depth = 1;
let mut i = s.len() - 2;
while depth > 0 && i > 0 {
match s[i] {
b')' => depth += 1,
b'(' => depth -= 1,
_ => {}
}
if depth > 0 {
i -= 1;
}
}
if depth > 0 {
break;
}
s = &s[..i];
}
std::str::from_utf8(s).unwrap_or(before).trim_end()
};

let last_word = before_for_keyword
.rsplit(|c: char| !c.is_alphanumeric() && c != '_')
.next()
.unwrap_or("");
Expand Down
59 changes: 59 additions & 0 deletions crates/zapcode-core/tests/objects_arrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,62 @@ fn test_trailing_object_after_semicolon() {
other => panic!("expected object, got {:?}", other),
}
}

// --- Bug fix: keyword+paren+block constructs with object literal args ---

#[test]
fn test_if_block_with_object_arg() {
let result = eval_ts("if (true) { Promise.resolve({ a: 1 }); }").unwrap();
assert_eq!(result, Value::Undefined);
}

#[test]
fn test_for_block_with_object_arg() {
let result = eval_ts("let sum = 0; for (let i = 0; i < 3; i++) { sum += i; } sum").unwrap();
assert_eq!(result, Value::Int(3));
}

#[test]
fn test_while_block_with_object_arg() {
let result = eval_ts("let x = 0; while (x < 3) { x++; } x").unwrap();
assert_eq!(result, Value::Int(3));
}

#[test]
fn test_catch_block_with_object_arg() {
let result = eval_ts(
"let caught = false; try { throw new Error('test'); } catch (e) { caught = true; } caught",
)
.unwrap();
assert_eq!(result, Value::Bool(true));
}

#[test]
fn test_nested_if_block_with_object_arg() {
let result = eval_ts("let x = 0; if (true) { if (true) { x = 42; } } x").unwrap();
assert_eq!(result, Value::Int(42));
}

#[test]
fn test_else_if_block_with_object_arg() {
let result = eval_ts("let x = 0; if (false) { x = 1; } else if (true) { x = 2; } x").unwrap();
assert_eq!(result, Value::Int(2));
}

#[test]
fn test_for_of_block_with_object_arg() {
let result = eval_ts("let sum = 0; for (const x of [1, 2, 3]) { sum += x; } sum").unwrap();
assert_eq!(result, Value::Int(6));
}

#[test]
fn test_if_block_with_await_and_object_arg() {
let result = eval_ts("if (true) { await Promise.resolve({ a: 1 }); }").unwrap();
assert_eq!(result, Value::Undefined);
}

#[test]
fn test_if_block_with_user_function_and_object_arg() {
let result = eval_ts("function f(x){ return x; }\nif (true) { f({ a: 1 }); }").unwrap();
assert_eq!(result, Value::Undefined);
}
14 changes: 8 additions & 6 deletions crates/zapcode-py/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use pyo3::exceptions::PyRuntimeError;
use pyo3::prelude::*;
use pyo3::types::{PyBool, PyDict, PyFloat, PyInt, PyList, PyString};

type PyObject = Py<PyAny>;

use zapcode_core::{
ExecutionTrace, ResourceLimits, TraceSpan as CoreTraceSpan, TraceStatus, Value, VmState,
ZapcodeError, ZapcodeSnapshot as CoreSnapshot,
Expand All @@ -17,21 +19,21 @@ use zapcode_core::{
fn py_to_value(obj: &Bound<'_, PyAny>) -> PyResult<Value> {
if obj.is_none() {
Ok(Value::Null)
} else if let Ok(b) = obj.downcast::<PyBool>() {
} else if let Ok(b) = obj.cast::<PyBool>() {
Ok(Value::Bool(b.is_true()))
} else if let Ok(i) = obj.downcast::<PyInt>() {
} else if let Ok(i) = obj.cast::<PyInt>() {
let val: i64 = i.extract()?;
Ok(Value::Int(val))
} else if let Ok(f) = obj.downcast::<PyFloat>() {
} else if let Ok(f) = obj.cast::<PyFloat>() {
let val: f64 = f.extract()?;
Ok(Value::Float(val))
} else if let Ok(s) = obj.downcast::<PyString>() {
} else if let Ok(s) = obj.cast::<PyString>() {
let val: String = s.extract()?;
Ok(Value::String(Arc::from(val.as_str())))
} else if let Ok(list) = obj.downcast::<PyList>() {
} else if let Ok(list) = obj.cast::<PyList>() {
let items: PyResult<Vec<Value>> = list.iter().map(|item| py_to_value(&item)).collect();
Ok(Value::Array(items?))
} else if let Ok(dict) = obj.downcast::<PyDict>() {
} else if let Ok(dict) = obj.cast::<PyDict>() {
let mut map = indexmap::IndexMap::new();
for (k, v) in dict.iter() {
let key: String = k.extract()?;
Expand Down
Loading