Skip to content

Truly independent web browser

LibJS: Give a named function expression a var for its block function

Shannon Boothff6586b141bc
diff --git a/Libraries/LibJS/Rust/src/parser/base.rs b/Libraries/LibJS/Rust/src/parser/base.rs
index d537de893ca..ed27b50b574 100644
--- a/Libraries/LibJS/Rust/src/parser/base.rs
+++ b/Libraries/LibJS/Rust/src/parser/base.rs
@@ -1248,6 +1248,12 @@ impl<'a, U: Unit> Parser<'a, U> {
let d = self.decls.len() - 1;
self.decls[d].summary_slot = SUMMARY_SLOT_EARLY;
}
+ } else if flags & !decl::FUNCTION_NAME == 0 {
+ // NB: The name of a function expression is bound outside of the function, which gets a `var` of its own.
+ self.decls[row as usize].flags |= decl::ANNEX_B;
+ if self.records_summaries {
+ self.decls[row as usize].summary_slot = SUMMARY_SLOT_EARLY;
+ }
} else {
self.decls[row as usize].flags |= decl::ANNEX_B;
if !is_parameter {
diff --git a/Tests/LibJS/Runtime/functions/annex-b-function-after-blocking-let.js b/Tests/LibJS/Runtime/functions/annex-b-function-after-blocking-let.js
index 00f30f4c9ab..163c2545500 100644
--- a/Tests/LibJS/Runtime/functions/annex-b-function-after-blocking-let.js
+++ b/Tests/LibJS/Runtime/functions/annex-b-function-after-blocking-let.js
@@ -83,3 +83,18 @@ test("a var or function declaration later in the function does not keep a functi
}
expect(laterBindingsKeepTheirPlaces()).toEqual(["function", 2]);
});
+
+test("a function expression's own name does not keep a function of that name from hoisting", () => {
+ var depth = 0;
+ var f = function x() {
+ var before = typeof x;
+ if (++depth > 1) return "called the function expression again";
+ {
+ function x() {
+ return "block";
+ }
+ }
+ return [before, x()];
+ };
+ expect(f()).toEqual(["undefined", "block"]);
+});