/* * Copyright (c) 2020-2024, Andreas Kling * Copyright (c) 2020-2023, Linus Groh * Copyright (c) 2021-2022, David Tuin * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include #include #include #include #include #include #include #include #include namespace JS { ASTNode::ASTNode(SourceRange source_range) : m_source_range(move(source_range)) { } ByteString ASTNode::class_name() const { // NOTE: We strip the "JS::" prefix. auto const* typename_ptr = typeid(*this).name(); return demangle({ typename_ptr, strlen(typename_ptr) }).substring(4); } Optional CallExpression::expression_string() const { if (is(*m_callee)) return static_cast(*m_callee).string().to_utf16_string(); if (is(*m_callee)) return static_cast(*m_callee).to_string_approximation(); return {}; } static Optional nullopt_or_private_identifier_description(Expression const& expression) { if (is(expression)) return static_cast(expression).string(); return {}; } Optional ClassField::private_bound_identifier() const { return nullopt_or_private_identifier_description(*m_key); } Optional ClassMethod::private_bound_identifier() const { return nullopt_or_private_identifier_description(*m_key); } ThrowCompletionOr ClassDeclaration::for_each_bound_identifier(ThrowCompletionOrVoidCallback&& callback) const { if (!m_class_expression->m_name) return {}; return callback(*m_class_expression->m_name); } bool BindingPattern::contains_expression() const { for (auto& entry : entries) { if (entry.name.has>()) return true; if (entry.initializer) return true; if (auto binding_ptr = entry.alias.get_pointer>(); binding_ptr && (*binding_ptr)->contains_expression()) return true; } return false; } ThrowCompletionOr BindingPattern::for_each_bound_identifier(ThrowCompletionOrVoidCallback&& callback) const { for (auto const& entry : entries) { auto const& alias = entry.alias; if (alias.has>()) { TRY(callback(alias.get>())); } else if (alias.has>()) { TRY(alias.get>()->for_each_bound_identifier(forward(callback))); } else { auto const& name = entry.name; if (name.has>()) TRY(callback(name.get>())); } } return {}; } FunctionNode::FunctionNode(RefPtr name, Utf16View source_text, NonnullRefPtr body, NonnullRefPtr parameters, i32 function_length, FunctionKind kind, bool is_strict_mode, FunctionParsingInsights parsing_insights, bool is_arrow_function) : m_name(move(name)) , m_source_text(move(source_text)) , m_body(move(body)) , m_parameters(move(parameters)) , m_function_length(function_length) , m_kind(kind) , m_is_strict_mode(is_strict_mode) , m_is_arrow_function(is_arrow_function) , m_parsing_insights(parsing_insights) { if (m_is_arrow_function) VERIFY(!parsing_insights.might_need_arguments_object); } FunctionNode::~FunctionNode() = default; ThrowCompletionOr FunctionDeclaration::for_each_bound_identifier(ThrowCompletionOrVoidCallback&& callback) const { if (!m_name) return {}; return callback(*m_name); } ThrowCompletionOr VariableDeclaration::for_each_bound_identifier(ThrowCompletionOrVoidCallback&& callback) const { for (auto const& entry : declarations()) { TRY(entry->target().visit( [&](NonnullRefPtr const& id) { return callback(id); }, [&](NonnullRefPtr const& binding) { return binding->for_each_bound_identifier([&](auto const& id) { return callback(id); }); })); } return {}; } ThrowCompletionOr UsingDeclaration::for_each_bound_identifier(ThrowCompletionOrVoidCallback&& callback) const { for (auto const& entry : m_declarations) { VERIFY(entry->target().has>()); TRY(callback(entry->target().get>())); } return {}; } static Utf16String expression_to_string_approximation(Expression const& expression) { if (is(expression)) return as(expression).string().to_utf16_string(); if (is(expression)) { auto const& member = as(expression); auto object_string = expression_to_string_approximation(member.object()); if (member.is_computed()) { auto property_string = expression_to_string_approximation(member.property()); return Utf16String::formatted("{}[{}]", object_string, property_string); } if (is(member.property())) return Utf16String::formatted("{}.{}", object_string, as(member.property()).string()); return Utf16String::formatted("{}.{}", object_string, as(member.property()).string()); } if (is(expression)) return Utf16String::formatted("'{}'", as(expression).value()); if (is(expression)) return Utf16String::formatted("{}", as(expression).value().as_double()); if (is(expression)) return "this"_utf16; return ""_utf16; } Utf16String MemberExpression::to_string_approximation() const { return expression_to_string_approximation(*this); } bool MemberExpression::ends_in_private_name() const { if (is_computed()) return false; if (is(*m_property)) return true; if (is(*m_property)) return static_cast(*m_property).ends_in_private_name(); return false; } bool ScopeNode::has_non_local_lexical_declarations() const { bool result = false; MUST(for_each_lexically_declared_identifier([&](Identifier const& identifier) { if (!identifier.is_local()) result = true; })); return result; } ThrowCompletionOr ScopeNode::for_each_lexically_scoped_declaration(ThrowCompletionOrVoidCallback&& callback) const { for (auto& declaration : m_lexical_declarations) TRY(callback(declaration)); return {}; } ThrowCompletionOr ScopeNode::for_each_lexically_declared_identifier(ThrowCompletionOrVoidCallback&& callback) const { for (auto const& declaration : m_lexical_declarations) { TRY(declaration->for_each_bound_identifier([&](auto const& identifier) { return callback(identifier); })); } return {}; } ThrowCompletionOr ScopeNode::for_each_var_declared_identifier(ThrowCompletionOrVoidCallback&& callback) const { for (auto& declaration : m_var_declarations) { TRY(declaration->for_each_bound_identifier([&](auto const& id) { return callback(id); })); } return {}; } ThrowCompletionOr ScopeNode::for_each_var_function_declaration_in_reverse_order(ThrowCompletionOrVoidCallback&& callback) const { for (ssize_t i = m_var_declarations.size() - 1; i >= 0; i--) { auto& declaration = m_var_declarations[i]; if (is(declaration)) TRY(callback(static_cast(*declaration))); } return {}; } ThrowCompletionOr ScopeNode::for_each_var_scoped_variable_declaration(ThrowCompletionOrVoidCallback&& callback) const { for (auto& declaration : m_var_declarations) { if (!is(declaration)) { VERIFY(is(declaration)); TRY(callback(static_cast(*declaration))); } } return {}; } ThrowCompletionOr ScopeNode::for_each_function_hoistable_with_annexB_extension(ThrowCompletionOrVoidCallback&& callback) const { for (auto& function : m_functions_hoistable_with_annexB_extension) { // We need const_cast here since it might have to set a property on function declaration. TRY(callback(const_cast(*function))); } return {}; } void ScopeNode::add_lexical_declaration(NonnullRefPtr declaration) { m_lexical_declarations.append(move(declaration)); } void ScopeNode::add_var_scoped_declaration(NonnullRefPtr declaration) { m_var_declarations.append(move(declaration)); } void ScopeNode::add_hoisted_function(NonnullRefPtr declaration) { m_functions_hoistable_with_annexB_extension.append(move(declaration)); } void ScopeNode::ensure_function_scope_data() const { if (m_function_scope_data) return; auto data = make(); // Extract functions_to_initialize from var-scoped function declarations (in reverse order, deduplicated). HashTable seen_function_names; for (ssize_t i = m_var_declarations.size() - 1; i >= 0; i--) { auto const& declaration = m_var_declarations[i]; if (is(declaration)) { auto& function_decl = static_cast(*declaration); if (seen_function_names.set(function_decl.name()) == AK::HashSetResult::InsertedNewEntry) data->functions_to_initialize.append(static_ptr_cast(declaration)); } } data->has_function_named_arguments = seen_function_names.contains("arguments"_utf16_fly_string); // Check if "arguments" is lexically declared. MUST(for_each_lexically_declared_identifier([&](auto const& identifier) { if (identifier.string() == "arguments"_utf16_fly_string) data->has_lexically_declared_arguments = true; })); // Extract vars_to_initialize from var declarations. HashTable seen_var_names; MUST(for_each_var_declared_identifier([&](Identifier const& identifier) { auto const& name = identifier.string(); if (seen_var_names.set(name) == AK::HashSetResult::InsertedNewEntry) { data->vars_to_initialize.append({ .identifier = identifier, .is_parameter = false, .is_function_name = seen_function_names.contains(name), }); data->var_names.set(name); if (!identifier.is_local()) { data->non_local_var_count++; data->non_local_var_count_for_parameter_expressions++; } } })); m_function_scope_data = move(data); } Utf16FlyString ExportStatement::local_name_for_default = "*default*"_utf16_fly_string; bool ExportStatement::has_export(Utf16FlyString const& export_name) const { return m_entries.contains([&](auto& entry) { // Make sure that empty exported names does not overlap with anything if (entry.kind != ExportEntry::Kind::NamedExport) return false; return entry.export_name == export_name; }); } bool ImportStatement::has_bound_name(Utf16FlyString const& name) const { return m_entries.contains([&](auto& entry) { return entry.local_name == name; }); } ModuleRequest::ModuleRequest(Utf16FlyString module_specifier_, Vector attributes) : module_specifier(move(module_specifier_)) , attributes(move(attributes)) { // 13.3.10.2 EvaluateImportCall ( specifierExpression [ , optionsExpression ] ), https://tc39.es/ecma262/#sec-evaluate-import-call // 16.2.2.4 Static Semantics: WithClauseToAttributes, https://tc39.es/ecma262/#sec-withclausetoattributes // 2. Sort attributes according to the lexicographic order of their [[Key]] field, treating the value of each such // field as a sequence of UTF-16 code unit values. quick_sort(this->attributes, [](ImportAttribute const& lhs, ImportAttribute const& rhs) { return lhs.key < rhs.key; }); } ByteString SourceRange::filename() const { return code->filename().to_byte_string(); } NonnullRefPtr CallExpression::create(SourceRange source_range, NonnullRefPtr callee, ReadonlySpan arguments, InvocationStyleEnum invocation_style, InsideParenthesesEnum inside_parens) { return ASTNodeWithTailArray::create(arguments.size(), move(source_range), move(callee), arguments, invocation_style, inside_parens); } NonnullRefPtr NewExpression::create(SourceRange source_range, NonnullRefPtr callee, ReadonlySpan arguments, InvocationStyleEnum invocation_style, InsideParenthesesEnum inside_parens) { return ASTNodeWithTailArray::create(arguments.size(), move(source_range), move(callee), arguments, invocation_style, inside_parens); } NonnullRefPtr FunctionParameters::empty() { static auto empty = adopt_ref(*new FunctionParameters({})); return empty; } }