diff --git a/crates/js/src/es2015_tests.rs b/crates/js/src/es2015_tests.rs index 32a6caa1ee..693dcf27f9 100644 --- a/crates/js/src/es2015_tests.rs +++ b/crates/js/src/es2015_tests.rs @@ -223,6 +223,50 @@ fn eval_code_keeps_its_let_and_const_to_itself() { truth("let a; try { eval('var b; var a;'); } catch (e) {} typeof b === 'undefined'"); } +#[test] +fn eval_code_keeps_its_environments_through_collections() { + // With a collection on every heap reservation, eval code's own environment and the scope of + // its `let` and `const` names stay while its declarations are made: the scope of a function + // the code declares leads out to the global scope without coming back to itself. + use crate::heap::{Class, Env, Func, key}; + use crate::value::Value; + let cases = [ + ("function g() { var o = 1; return eval(\"'use strict'; let x = 2; var y = 3; function f() { return x + y + o; } f\"); }", 6.0), + ("function g() { return (0, eval)(\"'use strict'; let x = 5; var y = 6; function f() { return x + y; } f\"); }", 11.0), + ("function g() { return (0, eval)(\"let x = 7; function f() { return x; } f\"); }", 7.0), + ]; + for (setup, want) in cases { + let mut realm = es(); + crate::interp::COLLECT_ON_RESERVE.with(|c| c.set(true)); + let made = realm.run(&format!("{setup} var f = g(); 0")); + crate::interp::COLLECT_ON_RESERVE.with(|c| c.set(false)); + assert_eq!(made, Ok(JsValue::Number(0.0)), "{setup}"); + let interp = realm.interp(); + let global = interp.global; + let f = match interp.get_from(global, &key("f"), &Value::Object(global)) { + Ok(Value::Object(f)) => f, + other => panic!("{setup}: {other:?}"), + }; + let mut cur = match interp.heap.object(f).map(|o| &o.class) { + Some(Class::Function(Func::Closure { env, .. })) => Some(*env), + _ => panic!("{setup}: not a closure"), + }; + let mut seen = std::collections::HashSet::new(); + while let Some(e) = cur { + assert!(seen.insert(e), "{setup}: the scope chain comes back to {e:?}"); + if e == interp.global_lex { + break; + } + cur = match interp.heap.env(e) { + Some(Env::Decl { outer, .. } | Env::Obj { outer, .. }) => *outer, + None => panic!("{setup}: {e:?} was freed"), + }; + } + assert_eq!(cur, Some(interp.global_lex), "{setup}"); + assert_eq!(realm.run("f()"), Ok(JsValue::Number(want)), "{setup}"); + } +} + // ---- arrow functions ---- #[test] diff --git a/crates/js/src/interp.rs b/crates/js/src/interp.rs index eeaf4f4f4c..f4436bb5db 100644 --- a/crates/js/src/interp.rs +++ b/crates/js/src/interp.rs @@ -388,6 +388,9 @@ const NAMED_KEY_UNITS: usize = 256; thread_local! { /// Tests: the [`Interp::finally_sites`] entries that leaving a block or a frame has looked at. pub(crate) static SITE_CHECKS: std::cell::Cell = const { std::cell::Cell::new(0) }; + /// Tests: [`Interp::reserve_heap`] collects garbage on every call, so anything left + /// unreachable from the roots while it runs is freed. + pub(crate) static COLLECT_ON_RESERVE: std::cell::Cell = const { std::cell::Cell::new(false) }; } /// The estimated bytes of the index names `0` to `len - 1` while `for-in` lists them. @@ -696,6 +699,10 @@ impl Interp { /// collection. Everything the caller still needs must be reachable from the roots. pub(crate) fn reserve_heap(&mut self, bytes: usize) -> JsResult<()> { self.settle_held(); + #[cfg(test)] + if COLLECT_ON_RESERVE.with(std::cell::Cell::get) { + self.collect_garbage(); + } if self.heap.bytes.saturating_add(bytes) <= self.limits.heap_bytes { return Ok(()); } @@ -3218,6 +3225,17 @@ impl Interp { /// environment of its `let` and `const` names. A script's `let` and `const` names go in the /// global scope ([`Self::declare_global_lexical`]). fn instantiate_global(&mut self, code: &Rc, env: EnvId, var_env: EnvId, is_eval: bool) -> JsResult { + // Strict eval code's own environment is in no frame yet, so it is a root while the + // declarations reserve heap. + let mark = self.temp_envs.len(); + self.temp_envs.extend([env, var_env]); + let r = self.instantiate_global_rooted(code, env, var_env, is_eval); + self.temp_envs.truncate(mark); + r + } + + /// [`Self::instantiate_global`] with `env` and `var_env` in [`Self::temp_envs`]. + fn instantiate_global_rooted(&mut self, code: &Rc, env: EnvId, var_env: EnvId, is_eval: bool) -> JsResult { let configurable = if is_eval { CONFIGURABLE } else { 0 }; if is_eval && !code.strict && self.options.es2015 { self.check_eval_vars(code, env, var_env)?;