Truly independent web browser
LibJS: Keep the functions of a static block out of the block around it
Shannon Booth170b7df4108a
diff --git a/Libraries/LibJS/Parser/Rust/src/stmt.rs b/Libraries/LibJS/Parser/Rust/src/stmt.rsindex f854030cc7f..d2ad1f06998 100644--- a/Libraries/LibJS/Parser/Rust/src/stmt.rs+++ b/Libraries/LibJS/Parser/Rust/src/stmt.rs@@ -1665,9 +1665,16 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {if scope_flags & scope_flag::STATIC_BLOCK != 0 {self.in_arrow = false;}+ // 15.7.11 Runtime Semantics: ClassStaticBlockDefinitionEvaluation ( homeObj ), https://tc39.es/ecma262/#sec-runtime-semantics-classstaticblockdefinitionevaluation+ // NB: A static block is the body of a function of its own, so its function declarations are not those of a+ // block around the class.+ let saved_block_mark = (self.block_mark, self.last_function_of_block);+ self.block_mark = NONE;+ self.last_function_of_block = 0;self.open_scope_with(scope_flags, func);let id = self.cur_scope;let node = parse(self);+ (self.block_mark, self.last_function_of_block) = saved_block_mark;let scope = *self.scopes.last().expect("the scope is still there");let has_eval = scope.has_eval;self.last_pseudo_function_row = self.close_scope();diff --git a/Tests/LibJS/Runtime/classes/class-static-block-function-declarations.js b/Tests/LibJS/Runtime/classes/class-static-block-function-declarations.jsnew file mode 100644index 00000000000..10b21476c39--- /dev/null+++ b/Tests/LibJS/Runtime/classes/class-static-block-function-declarations.js@@ -0,0 +1,90 @@+test("function declarations of a static block are not bindings of the block around the class", () => {+ function inBlock() {+ var b = "outer";+ {+ class C {+ static {+ function b() {}+ }+ }+ return typeof b;+ }+ }+ function inCatch() {+ try {+ throw 0;+ } catch (e) {+ class C {+ static {+ function e() {}+ }+ }+ return typeof e;+ }+ }+ function inSwitch() {+ switch (1) {+ case 1:+ class C {+ static {+ function b() {}+ }+ }+ return typeof b;+ }+ }+ expect(inBlock()).toBe("string");+ expect(inCatch()).toBe("number");+ expect(inSwitch()).toBe("undefined");+});++test("function declarations of a static block can use private names", () => {+ let result;+ {+ class C {+ static #p = 7;+ static {+ function read(o) {+ return o.#p;+ }+ result = read(C);+ }+ }+ expect(() => read(C)).toThrowWithMessage(ReferenceError, "'read' is not defined");+ }+ expect(result).toBe(7);+});++test("function declarations of a static block see the bindings of the block", () => {+ let result;+ {+ class C {+ static {+ var q = 1;+ function b() {+ return q;+ }+ result = b();+ }+ }+ }+ expect(result).toBe(1);+});++test("function declarations in a block of a static block", () => {+ let result;+ {+ class C {+ static {+ {+ function inner() {+ return 2;+ }+ result = inner();+ }+ }+ }+ expect(typeof inner).toBe("undefined");+ }+ expect(result).toBe(2);+});