Skip to content

Commit ae3440d

Browse files
authored
Use proper envs for Rust functions (#7623)
1 parent af658c9 commit ae3440d

File tree

3 files changed

+19
-37
lines changed

3 files changed

+19
-37
lines changed

rust/kcl-lib/src/execution/fn_call.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ impl FunctionDefinition<'_> {
401401
impl FunctionBody<'_> {
402402
fn prep_mem(&self, exec_state: &mut ExecState) {
403403
match self {
404-
FunctionBody::Rust(_) => exec_state.mut_stack().push_new_env_for_rust_call(),
404+
FunctionBody::Rust(_) => exec_state.mut_stack().push_new_root_env(true),
405405
FunctionBody::Kcl(_, memory) => exec_state.mut_stack().push_new_env_for_call(*memory),
406406
}
407407
}

rust/kcl-lib/src/execution/memory.rs

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -541,22 +541,6 @@ impl Stack {
541541
self.push_new_env_for_call(snapshot);
542542
}
543543

544-
/// Push a new stack frame on to the call stack for callees which should not read or write
545-
/// from memory.
546-
///
547-
/// This is suitable for calling standard library functions or other functions written in Rust
548-
/// which will use 'Rust memory' rather than KCL's memory and cannot reach into the wider
549-
/// environment.
550-
///
551-
/// Trying to read or write from this environment will panic with an index out of bounds.
552-
pub fn push_new_env_for_rust_call(&mut self) {
553-
self.call_stack.push(self.current_env);
554-
// Rust functions shouldn't try to set or access anything in their environment, so don't
555-
// waste time and space on a new env. Using usize::MAX means we'll get an overflow if we
556-
// try to access anything rather than a silent error.
557-
self.current_env = EnvironmentRef(usize::MAX, 0);
558-
}
559-
560544
/// Push a new stack frame on to the call stack with no connection to a parent environment.
561545
///
562546
/// Suitable for executing a separate module.
@@ -683,7 +667,7 @@ impl Stack {
683667
env.contains_key(var)
684668
}
685669

686-
/// Get a key from the first KCL (i.e., non-Rust) stack frame on the call stack.
670+
/// Get a key from the first stack frame on the call stack.
687671
pub fn get_from_call_stack(&self, key: &str, source_range: SourceRange) -> Result<(usize, &KclValue), KclError> {
688672
if !self.current_env.skip_env() {
689673
return Ok((self.current_env.1, self.get(key, source_range)?));
@@ -695,7 +679,7 @@ impl Stack {
695679
}
696680
}
697681

698-
unreachable!("It can't be Rust frames all the way down");
682+
unreachable!("No frames on the stack?");
699683
}
700684

701685
/// Iterate over all keys in the current environment which satisfy the provided predicate.
@@ -1217,24 +1201,6 @@ mod test {
12171201
assert_get_from(mem, "c", 5, callee);
12181202
}
12191203

1220-
#[test]
1221-
fn rust_env() {
1222-
let mem = &mut Stack::new_for_tests();
1223-
mem.add("a".to_owned(), val(1), sr()).unwrap();
1224-
mem.add("b".to_owned(), val(3), sr()).unwrap();
1225-
let sn = mem.snapshot();
1226-
1227-
mem.push_new_env_for_rust_call();
1228-
mem.push_new_env_for_call(sn);
1229-
assert_get(mem, "b", 3);
1230-
mem.add("b".to_owned(), val(4), sr()).unwrap();
1231-
assert_get(mem, "b", 4);
1232-
1233-
mem.pop_env();
1234-
mem.pop_env();
1235-
assert_get(mem, "b", 3);
1236-
}
1237-
12381204
#[test]
12391205
fn deep_call_env() {
12401206
let mem = &mut Stack::new_for_tests();

rust/kcl-lib/src/execution/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,6 +1920,22 @@ shape = layer() |> patternTransform(instances = 10, transform = transform)
19201920
);
19211921
}
19221922

1923+
#[tokio::test(flavor = "multi_thread")]
1924+
async fn pass_std_to_std() {
1925+
let ast = r#"sketch001 = startSketchOn(XY)
1926+
profile001 = circle(sketch001, center = [0, 0], radius = 2)
1927+
extrude001 = extrude(profile001, length = 5)
1928+
extrudes = patternLinear3d(
1929+
extrude001,
1930+
instances = 3,
1931+
distance = 5,
1932+
axis = [1, 1, 0],
1933+
)
1934+
clone001 = map(extrudes, f = clone)
1935+
"#;
1936+
parse_execute(ast).await.unwrap();
1937+
}
1938+
19231939
#[tokio::test(flavor = "multi_thread")]
19241940
async fn test_zero_param_fn() {
19251941
let ast = r#"sigmaAllow = 35000 // psi

0 commit comments

Comments
 (0)