Skip to content

Truly independent web browser

LibJS: Hoist a block function into a later var of its name

Shannon Booth145be6a3f971
diff --git a/Libraries/LibJS/Rust/src/parser/base.rs b/Libraries/LibJS/Rust/src/parser/base.rs
index 0700a26e22e..d537de893ca 100644
--- a/Libraries/LibJS/Rust/src/parser/base.rs
+++ b/Libraries/LibJS/Rust/src/parser/base.rs
@@ -1250,6 +1250,10 @@ impl<'a, U: Unit> Parser<'a, U> {
}
} else {
self.decls[row as usize].flags |= decl::ANNEX_B;
+ if !is_parameter {
+ // NB: Evaluating the function stores into the binding, which may come before its declaration.
+ self.make_binding_early(row, top);
+ }
}
let row = self.find_declaration(hoist.name, scope.id);
if is_parameter {
@@ -1275,6 +1279,28 @@ impl<'a, U: Unit> Parser<'a, U> {
self.hoists.truncate(kept);
}
+ /// Makes declaration `d` of the scope at `top` one that the code of its function knows from the start, and not only
+ /// from where it comes up. The declarations after it in the order of the summary move up into its place.
+ fn make_binding_early(&mut self, d: u32, top: usize) {
+ if !self.records_summaries {
+ return;
+ }
+ let slot = self.decls[d as usize].summary_slot;
+ if slot & (SUMMARY_SLOT_EARLY | SUMMARY_SLOT_INNER) != 0 {
+ return;
+ }
+ let mut other = self.scopes[top].decl_head;
+ while other != NONE {
+ let declaration = &mut self.decls[other as usize];
+ let other_slot = declaration.summary_slot;
+ if other_slot & (SUMMARY_SLOT_EARLY | SUMMARY_SLOT_INNER) == 0 && other_slot > slot {
+ declaration.summary_slot = other_slot - 1;
+ }
+ other = declaration.next_in_scope;
+ }
+ self.decls[d as usize].summary_slot = SUMMARY_SLOT_EARLY;
+ }
+
/// Resolves the bindings of the innermost scope against the uses in its subtree, and returns its row in the tree.
pub(crate) fn close_scope(&mut self) -> u32 {
if !self.tracks_scopes {
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 7979ff76de2..00f30f4c9ab 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
@@ -40,3 +40,46 @@ test("a let later in the function keeps a function from hoisting", () => {
}
expect(run()).toBe(2);
});
+
+test("a var or function declaration later in the function does not keep a function from hoisting", () => {
+ function varAfter() {
+ {
+ function x() {}
+ }
+ var x;
+ return typeof x;
+ }
+ expect(varAfter()).toBe("function");
+
+ function functionAfter() {
+ {
+ function x() {
+ return "block";
+ }
+ }
+ function x() {
+ return "function";
+ }
+ return x();
+ }
+ expect(functionAfter()).toBe("block");
+
+ function ifClause() {
+ if (true) function x() {}
+ var x;
+ return typeof x;
+ }
+ expect(ifClause()).toBe("function");
+
+ function laterBindingsKeepTheirPlaces() {
+ {
+ function x() {}
+ }
+ var x;
+ var y = 1;
+ const read = () => y;
+ y = 2;
+ return [typeof x, read()];
+ }
+ expect(laterBindingsKeepTheirPlaces()).toEqual(["function", 2]);
+});