Truly independent web browser
LibJS: Look a name up through its environments in a loop
Shannon Booth6e9eb614a493
diff --git a/Libraries/LibJS/Rust/src/interpreter/vm.rs b/Libraries/LibJS/Rust/src/interpreter/vm.rsindex 7c76ce6750d..18caf64f5d4 100644--- a/Libraries/LibJS/Rust/src/interpreter/vm.rs+++ b/Libraries/LibJS/Rust/src/interpreter/vm.rs@@ -2389,48 +2389,52 @@ impl Vm {self.running_realm().global_declarative_environment()}- // 9.1.2.1 GetIdentifierReference ( env, name, strict ), https://tc39.es/ecma262/#sec-getidentifierreference+ // 9.1.2.1 GetIdentifierReference ( envRecord, name, strict ), https://tc39.es/ecma262/#sec-getidentifierreferencepub fn get_identifier_reference(&self,- environment: Option<Gc<Environment>>,+ mut environment: Option<Gc<Environment>>,name: Utf16FlyString,strict: Strict,- hops: usize,+ mut hops: usize,) -> ThrowCompletionOr<Reference> {- // 1. If env is the value null, then- let Some(environment) = environment else {- // a. Return the Reference Record { [[Base]]: unresolvable, [[ReferencedName]]: name, [[Strict]]: strict, [[ThisValue]]: empty }.- return Ok(Reference::with_base_type(- BaseType::Unresolvable,- PropertyKey::from(name),- strict,- ));- };-- // 2. Let exists be ? env.HasBinding(name).- let mut index = None;- let exists = environment.has_binding(self, &name, Some(&mut index))?;+ // NB: The recursion of step 5 is a loop, so that a long chain of environments does not grow the stack.+ loop {+ // 1. If envRecord is null, then+ let Some(current) = environment else {+ // a. Return the Reference Record { [[Base]]: unresolvable, [[ReferencedName]]: name, [[Strict]]: strict, [[ThisValue]]: empty }.+ return Ok(Reference::with_base_type(+ BaseType::Unresolvable,+ PropertyKey::from(name),+ strict,+ ));+ };- // Note: This is an optimization for looking up the same reference.- let environment_coordinate = index.map(|index| EnvironmentCoordinate {- hops: u32::try_from(hops).expect("the hop count fits in u32"),- index: u32::try_from(index).expect("the binding index fits in u32"),- });+ // 2. Let exists be ? envRecord.HasBinding(name).+ let mut index = None;+ let exists = current.has_binding(self, &name, Some(&mut index))?;++ // 3. If exists is true, then+ if exists {+ // Note: This is an optimization for looking up the same reference.+ let environment_coordinate = index.map(|index| EnvironmentCoordinate {+ hops: u32::try_from(hops).expect("the hop count fits in u32"),+ index: u32::try_from(index).expect("the binding index fits in u32"),+ });++ // a. Return the Reference Record { [[Base]]: envRecord, [[ReferencedName]]: name, [[Strict]]: strict, [[ThisValue]]: empty }.+ return Ok(Reference::with_base_environment(+ current,+ name,+ strict,+ environment_coordinate,+ ));+ }- // 3. If exists is true, then- if exists {- // a. Return the Reference Record { [[Base]]: env, [[ReferencedName]]: name, [[Strict]]: strict, [[ThisValue]]: empty }.- return Ok(Reference::with_base_environment(- environment,- name,- strict,- environment_coordinate,- ));+ // 4. Let outer be envRecord.[[OuterEnv]].+ // 5. Return ? GetIdentifierReference(outer, name, strict).+ environment = current.outer_environment();+ hops += 1;}- // 4. Else,- // a. Let outer be env.[[OuterEnv]].- // b. Return ? GetIdentifierReference(outer, name, strict).- self.get_identifier_reference(environment.outer_environment(), name, strict, hops + 1)}// 9.4.2 ResolveBinding ( name [ , env ] ), https://tc39.es/ecma262/#sec-resolvebindingdiff --git a/Tests/LibJS/Runtime/long-environment-chain-lookups.js b/Tests/LibJS/Runtime/long-environment-chain-lookups.jsnew file mode 100644index 00000000000..ed22e7c773b--- /dev/null+++ b/Tests/LibJS/Runtime/long-environment-chain-lookups.js@@ -0,0 +1,27 @@+test("a global looked up through a very long chain of function environments", () => {+ let next = function (code, eval) {+ return eval(code);+ };+ for (let i = 0; i < 50000; i++) next = next("(function (code, eval) { return eval(code); })", eval);+ expect(next("typeof Array", eval)).toBe("function");+});++test("an assignment through more block scopes than a coordinate holds, near the call stack size limit", () => {+ let source = "let v = 7; (() => v);";+ for (let i = 0; i < 300; i++) source += `{ let b${i} = 0; (() => b${i});`;+ source += "v = 8; return v;" + "}".repeat(300);+ const deep = new Function(source);+ expect(deep()).toBe(8);++ const results = new Set();+ function recurse() {+ try {+ Reflect.apply(recurse, null, []);+ } catch {}+ try {+ results.add(deep());+ } catch {}+ }+ recurse();+ expect([...results]).toEqual([8]);+});