2021-09-09 13:02:31 -03:00
|
|
|
/*
|
2024-10-04 08:19:50 -03:00
|
|
|
* Copyright (c) 2021, Andreas Kling <andreas@ladybird.org>
|
2021-09-09 13:02:31 -03:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2021-09-09 14:02:40 -03:00
|
|
|
#include <LibJS/AST.h>
|
|
|
|
|
#include <LibJS/Lexer.h>
|
|
|
|
|
#include <LibJS/Parser.h>
|
2026-02-10 21:45:55 -03:00
|
|
|
#include <LibJS/Runtime/SharedFunctionInstanceData.h>
|
2022-02-07 14:51:58 -03:00
|
|
|
#include <LibJS/Runtime/VM.h>
|
2021-09-09 13:02:31 -03:00
|
|
|
#include <LibJS/Script.h>
|
|
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
GC_DEFINE_ALLOCATOR(Script);
|
2023-11-19 05:45:05 -03:00
|
|
|
|
2021-09-09 14:02:40 -03:00
|
|
|
// 16.1.5 ParseScript ( sourceText, realm, hostDefined ), https://tc39.es/ecma262/#sec-parse-script
|
2024-11-14 12:01:23 -03:00
|
|
|
Result<GC::Ref<Script>, Vector<ParserError>> Script::parse(StringView source_text, Realm& realm, StringView filename, HostDefined* host_defined, size_t line_number_offset)
|
2021-09-09 13:02:31 -03:00
|
|
|
{
|
2022-04-30 20:13:33 -03:00
|
|
|
// 1. Let script be ParseText(sourceText, Script).
|
2025-11-08 17:21:52 -03:00
|
|
|
auto parser = Parser(Lexer(SourceCode::create(String::from_utf8(filename).release_value_but_fixme_should_propagate_errors(), Utf16String::from_utf8(source_text)), line_number_offset));
|
2022-04-30 20:13:33 -03:00
|
|
|
auto script = parser.parse_program();
|
2021-09-09 14:02:40 -03:00
|
|
|
|
2022-04-30 20:13:33 -03:00
|
|
|
// 2. If script is a List of errors, return body.
|
2021-09-14 15:56:57 -03:00
|
|
|
if (parser.has_errors())
|
|
|
|
|
return parser.errors();
|
2021-09-09 14:02:40 -03:00
|
|
|
|
2022-04-30 20:13:33 -03:00
|
|
|
// 3. Return Script Record { [[Realm]]: realm, [[ECMAScriptCode]]: script, [[HostDefined]]: hostDefined }.
|
2025-10-28 17:16:35 -03:00
|
|
|
return realm.heap().allocate<Script>(realm, filename, move(script), host_defined);
|
2021-09-09 13:02:31 -03:00
|
|
|
}
|
|
|
|
|
|
2025-10-28 17:16:35 -03:00
|
|
|
Script::Script(Realm& realm, StringView filename, NonnullRefPtr<Program> parse_node, HostDefined* host_defined)
|
2022-09-05 09:31:25 -03:00
|
|
|
: m_realm(realm)
|
2021-09-09 13:02:31 -03:00
|
|
|
, m_parse_node(move(parse_node))
|
2022-01-18 15:21:42 -03:00
|
|
|
, m_filename(filename)
|
2022-02-07 14:25:39 -03:00
|
|
|
, m_host_defined(host_defined)
|
2021-09-09 13:02:31 -03:00
|
|
|
{
|
2026-02-10 21:45:55 -03:00
|
|
|
auto& vm = realm.vm();
|
|
|
|
|
auto& program = *m_parse_node;
|
|
|
|
|
|
|
|
|
|
m_is_strict_mode = program.is_strict_mode();
|
|
|
|
|
|
|
|
|
|
// Pre-compute lexically declared names (GDI step 3).
|
|
|
|
|
MUST(program.for_each_lexically_declared_identifier([&](Identifier const& identifier) -> ThrowCompletionOr<void> {
|
|
|
|
|
m_lexical_names.append(identifier.string());
|
|
|
|
|
return {};
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
// Pre-compute var declared names (GDI step 4).
|
|
|
|
|
MUST(program.for_each_var_declared_identifier([&](Identifier const& identifier) -> ThrowCompletionOr<void> {
|
|
|
|
|
m_var_names.append(identifier.string());
|
|
|
|
|
return {};
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
// Pre-compute functions to initialize and declared function names (GDI steps 7-8).
|
|
|
|
|
MUST(program.for_each_var_function_declaration_in_reverse_order([&](FunctionDeclaration const& function) -> ThrowCompletionOr<void> {
|
|
|
|
|
auto function_name = function.name();
|
|
|
|
|
if (m_declared_function_names.set(function_name) != AK::HashSetResult::InsertedNewEntry)
|
|
|
|
|
return {};
|
|
|
|
|
m_functions_to_initialize.append({ function.ensure_shared_data(vm), function_name });
|
|
|
|
|
return {};
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
// Pre-compute var scoped variable names (GDI step 10).
|
|
|
|
|
MUST(program.for_each_var_scoped_variable_declaration([&](VariableDeclaration const& declaration) {
|
|
|
|
|
return declaration.for_each_bound_identifier([&](Identifier const& identifier) -> ThrowCompletionOr<void> {
|
|
|
|
|
m_var_scoped_names.append(identifier.string());
|
|
|
|
|
return {};
|
|
|
|
|
});
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
// Pre-compute AnnexB candidates (GDI step 13).
|
|
|
|
|
if (!m_is_strict_mode) {
|
|
|
|
|
MUST(program.for_each_function_hoistable_with_annexB_extension([&](FunctionDeclaration& function_declaration) -> ThrowCompletionOr<void> {
|
|
|
|
|
m_annex_b_candidates.append(function_declaration);
|
|
|
|
|
return {};
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Pre-compute lexical bindings (GDI step 15).
|
|
|
|
|
MUST(program.for_each_lexically_scoped_declaration([&](Declaration const& declaration) {
|
|
|
|
|
return declaration.for_each_bound_identifier([&](Identifier const& identifier) -> ThrowCompletionOr<void> {
|
|
|
|
|
m_lexical_bindings.append({ identifier.string(), declaration.is_constant_declaration() });
|
|
|
|
|
return {};
|
|
|
|
|
});
|
|
|
|
|
}));
|
2021-09-09 13:02:31 -03:00
|
|
|
}
|
2022-04-17 17:59:52 -03:00
|
|
|
|
2022-09-05 09:31:25 -03:00
|
|
|
Script::~Script()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Script::visit_edges(Cell::Visitor& visitor)
|
|
|
|
|
{
|
|
|
|
|
Base::visit_edges(visitor);
|
|
|
|
|
visitor.visit(m_realm);
|
2026-02-10 21:42:41 -03:00
|
|
|
visitor.visit(m_executable);
|
2026-02-10 21:45:55 -03:00
|
|
|
for (auto const& function : m_functions_to_initialize)
|
|
|
|
|
visitor.visit(function.shared_data);
|
2022-09-05 20:19:58 -03:00
|
|
|
if (m_host_defined)
|
|
|
|
|
m_host_defined->visit_host_defined_self(visitor);
|
2023-10-28 18:56:15 -03:00
|
|
|
for (auto const& loaded_module : m_loaded_modules)
|
|
|
|
|
visitor.visit(loaded_module.module);
|
2022-09-05 09:31:25 -03:00
|
|
|
}
|
|
|
|
|
|
2021-09-09 13:02:31 -03:00
|
|
|
}
|