diff --git a/implants/lib/eldritchv2/eldritch-core/src/interpreter/core.rs b/implants/lib/eldritchv2/eldritch-core/src/interpreter/core.rs index 7d084b78e..078cf5b6b 100644 --- a/implants/lib/eldritchv2/eldritch-core/src/interpreter/core.rs +++ b/implants/lib/eldritchv2/eldritch-core/src/interpreter/core.rs @@ -33,6 +33,18 @@ pub struct Interpreter { pub depth: usize, pub call_stack: Vec, pub current_func_name: String, + pub is_scope_owner: bool, +} + +impl Drop for Interpreter { + fn drop(&mut self) { + if self.is_scope_owner { + // Break reference cycles by clearing the environment values. + // This drops all variables including functions, which may hold references back to the environment. + self.env.write().values.clear(); + self.env.write().parent = None; + } + } } impl Default for Interpreter { @@ -60,6 +72,7 @@ impl Interpreter { depth: 0, call_stack: Vec::new(), current_func_name: "".to_string(), + is_scope_owner: true, }; interpreter.load_builtins(); diff --git a/implants/lib/eldritchv2/eldritch-core/src/interpreter/eval.rs b/implants/lib/eldritchv2/eldritch-core/src/interpreter/eval.rs index b1461857d..024c4eaf3 100644 --- a/implants/lib/eldritchv2/eldritch-core/src/interpreter/eval.rs +++ b/implants/lib/eldritchv2/eldritch-core/src/interpreter/eval.rs @@ -868,6 +868,7 @@ fn builtin_eval( depth: interp.depth + 1, call_stack: interp.call_stack.clone(), current_func_name: "".to_string(), + is_scope_owner: false, }; match temp_interp.interpret(&code) {