Truly independent web browser
LibJS: Only name anonymous definitions from a pending name
Shannon Booth5b9b695dd2bb
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rsindex 0b81b283ebc..6a0c5bbff78 100644--- a/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs@@ -27,7 +27,6 @@ use crate::bytecode::instruction::typed as op;use crate::bytecode::operand::*;use crate::bytecode::source_map::SourceOffset;use crate::parser::OutOfStack;-use crate::parser::columns::NONE;use crate::parser::columns::tag;use crate::parser::literals::EscapeMode;use crate::u32_from_usize;@@ -50,7 +49,7 @@ use super::stream::OpenIterator;use super::stream::ScopeEnvironment;use super::stream::ValKind;use super::sweep::declaration_kind_of_mark;-use super::sweep::is_anonymous;+use super::sweep::is_anonymous_function_definition;use super::sweep::next_tag;use super::sweep::resource_of_mark;use crate::parser::literals::raw_template_value;@@ -515,22 +514,15 @@ impl Generator<'_> {let initializer = NodeId(tree.node_a(NodeId(tree.node_b(field))));let is_computed = tree.node_tag(field) == tag::COMPUTED_CLASS_FIELD;let first_row = key.index() + 1 + usize::from(is_computed || tree.node_tag(key) == tag::BIGINT);- let bare_initializer = tree.skip_parens(initializer);- let initializer_tag = tree.node_tag(bare_initializer);- let is_anonymous_definition = match initializer_tag {- tag::FUNCTION => is_anonymous(tree, bare_initializer),- tag::CLASS => tree.node_a(bare_initializer) == NONE,- _ => false,- };let value = if let Some(name) = class_field_name(tree, field) {let name = self.intern_identifier_text(crate::utf16::Utf16View::of_string(&name));self.set_pending_function_name(initializer, Some(name));self.sweep_value(first_row, initializer)?- } else if is_anonymous_definition {+ } else if is_anonymous_function_definition(tree, initializer) {// NB: A computed key is only known once the class is defined, so the function reads it.let name = self.temporary();self.put(op::GetClassFieldInitializerName { dst: name });- if initializer_tag == tag::CLASS {+ if tree.node_tag(tree.skip_parens(initializer)) == tag::CLASS {self.context.stream.pending_computed_class_name = Some(name);self.sweep_value(first_row, initializer)?} else {@@ -1669,8 +1661,7 @@ impl Generator<'_> {/// Whatever anonymous function or class `value` is takes `name` as its name.fn set_pending_function_name(&mut self, value: NodeId, name: Option<IdentifierTableIndex>) {- let tree = self.tree;- if matches!(tree.node_tag(tree.skip_parens(value)), tag::FUNCTION | tag::CLASS) {+ if is_anonymous_function_definition(self.tree, value) {self.context.stream.pending_function_name = name;}}diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/sweep.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/sweep.rsindex 650d4e9143c..e8025816b0b 100644--- a/Libraries/LibJS/Rust/src/bytecode/codegen/sweep.rs+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/sweep.rs@@ -638,6 +638,42 @@ pub(super) fn is_anonymous(tree: &SyntaxTree, function: NodeId) -> bool {tree.script.index.name[tree.node_b(function) as usize] == NONE}+/// https://tc39.es/ecma262/#sec-static-semantics-isfunctiondefinition+#[inline(always)]+fn is_function_definition(tree: &SyntaxTree, expression: NodeId) -> bool {+ matches!(tree.node_tag(expression), tag::FUNCTION | tag::CLASS)+}++/// https://tc39.es/ecma262/#sec-static-semantics-hasname+#[inline(always)]+fn has_name(tree: &SyntaxTree, expression: NodeId) -> bool {+ match tree.node_tag(expression) {+ tag::FUNCTION => !is_anonymous(tree, expression),+ tag::CLASS => tree.node_a(expression) != NONE,+ _ => false,+ }+}++/// https://tc39.es/ecma262/#sec-isanonymousfunctiondefinition+pub(super) fn is_anonymous_function_definition(tree: &SyntaxTree, expression: NodeId) -> bool {+ // NB: IsFunctionDefinition and HasName of a ParenthesizedExpression are those of the expression in it.+ let expression = tree.skip_parens(expression);++ // 1. If IsFunctionDefinition of expr is false, return false.+ if !is_function_definition(tree, expression) {+ return false;+ }++ // 2. Let hasName be HasName of expr.+ // 3. If hasName is true, return false.+ if has_name(tree, expression) {+ return false;+ }++ // 4. Return true.+ true+}+/// The operator of a logical assignment, by the token that it is.fn logical_operator_of_assignment(token: u8) -> crate::ast::LogicalOp {use crate::parser::token::T;diff --git a/Tests/LibJS/Runtime/functions/function-name.js b/Tests/LibJS/Runtime/functions/function-name.jsindex 014cbd40a37..3e64707f92c 100644--- a/Tests/LibJS/Runtime/functions/function-name.js+++ b/Tests/LibJS/Runtime/functions/function-name.js@@ -97,6 +97,40 @@ test("computed property name inference only applies to anonymous definitions", (expect(object[key].name).toBe("");});+test("a named class does not pass on the name it would have got to the functions in it", () => {+ const names = [];+ function mixin(fn) {+ names.push(fn.name);+ return class {};+ }++ var [a = class N extends mixin(() => {}) {}] = [];+ function withDefault(p = class N extends mixin(function () {}) {}) {}+ withDefault();+ class WithField {+ x = class N extends mixin(() => {}) {};+ }+ new WithField();+ var {+ b = class N {+ static [mixin(() => {}) && "k"]() {}+ },+ } = {};+ expect(names).toEqual(["", "", "", ""]);++ var g;+ function withAssignment(p = class C extends ((g = () => {}), Object) {}) {}+ withAssignment();+ expect(g.name).toBe("g");++ var {+ c = class C {+ [function () {}.name || "empty"]() {}+ },+ } = {};+ expect(Object.getOwnPropertyNames(c.prototype)).toEqual(["constructor", "empty"]);+});+test("names of native functions", () => {expect(console.debug.name).toBe("debug");expect((console.debug.name = "warn")).toBe("warn");