Skip to content

Truly independent web browser

LibJS: Give class static blocks an arguments object

Shannon Boothbf8c54e21556
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs
index 72760e0e6ba..e9af413d296 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs
@@ -578,6 +578,7 @@ impl Generator<'_> {
/// A static block, which runs as a function of its own.
pub(super) fn sweep_static_block(&mut self, block: NodeId) -> Result<(), OutOfStack> {
+ self.create_arguments_object_of_static_block();
self.sweep_rows(FIRST_ROW_OF_REPARSED_FUNCTION, block.index())?;
self.end_disposal();
Ok(())
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs
index 7ed43abad30..976ddb41eed 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs
@@ -1143,6 +1143,15 @@ impl Generator<'_> {
});
}
+ /// The arguments object of a static block, which FunctionDeclarationInstantiation gives it like any other function.
+ /// `arguments` is an early error in the block, so only an `eval` can see it.
+ pub(super) fn create_arguments_object_of_static_block(&mut self) {
+ let flags = self.context.scope.flags;
+ if flags & function_flag::CONTAINS_EVAL != 0 {
+ self.create_arguments_object(flags, false, false);
+ }
+ }
+
fn create_arguments_object(&mut self, flags: u32, is_mapped: bool, creates_bindings: bool) {
let name = Name(N_ARGUMENTS);
let is_captured = flags & (function_flag::CAPTURES_ARGUMENTS | function_flag::CONTAINS_EVAL) != 0
diff --git a/Tests/LibJS/Runtime/classes/class-static-initializers.js b/Tests/LibJS/Runtime/classes/class-static-initializers.js
index ca929c61898..71a1cf9d01e 100644
--- a/Tests/LibJS/Runtime/classes/class-static-initializers.js
+++ b/Tests/LibJS/Runtime/classes/class-static-initializers.js
@@ -148,3 +148,29 @@ describe("static blocks inside a destructuring declaration", () => {
expect(f()).toBe(1);
});
});
+
+test("a direct eval in a static block sees the block's own arguments object", () => {
+ class A {
+ static {
+ this.result = eval("typeof arguments + ',' + arguments.length");
+ }
+ }
+ expect(A.result).toBe("object,0");
+
+ class B {
+ static {
+ this.result = (() => eval("arguments.length"))();
+ }
+ }
+ expect(B.result).toBe(0);
+
+ function outer() {
+ class C {
+ static {
+ this.result = eval("arguments.length");
+ }
+ }
+ return [C.result, arguments.length];
+ }
+ expect(outer(1, 2, 3)).toEqual([0, 3]);
+});