2021-09-09 16:02:31 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2021, Andreas Kling <andreas@ladybird.org>
|
2021-09-09 16:02:31 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2021-09-09 17:02:40 +00:00
|
|
|
#include <LibJS/AST.h>
|
|
|
|
#include <LibJS/Lexer.h>
|
|
|
|
#include <LibJS/Parser.h>
|
2022-02-07 17:51:58 +00:00
|
|
|
#include <LibJS/Runtime/VM.h>
|
2021-09-09 16:02:31 +00:00
|
|
|
#include <LibJS/Script.h>
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2023-11-19 08:45:05 +00:00
|
|
|
JS_DEFINE_ALLOCATOR(Script);
|
|
|
|
|
2021-09-09 17:02:40 +00:00
|
|
|
// 16.1.5 ParseScript ( sourceText, realm, hostDefined ), https://tc39.es/ecma262/#sec-parse-script
|
2022-11-23 11:39:23 +00:00
|
|
|
Result<NonnullGCPtr<Script>, Vector<ParserError>> Script::parse(StringView source_text, Realm& realm, StringView filename, HostDefined* host_defined, size_t line_number_offset)
|
2021-09-09 16:02:31 +00:00
|
|
|
{
|
2022-04-30 23:13:33 +00:00
|
|
|
// 1. Let script be ParseText(sourceText, Script).
|
2022-03-13 21:17:35 +00:00
|
|
|
auto parser = Parser(Lexer(source_text, filename, line_number_offset));
|
2022-04-30 23:13:33 +00:00
|
|
|
auto script = parser.parse_program();
|
2021-09-09 17:02:40 +00:00
|
|
|
|
2022-04-30 23:13:33 +00:00
|
|
|
// 2. If script is a List of errors, return body.
|
2021-09-14 18:56:57 +00:00
|
|
|
if (parser.has_errors())
|
|
|
|
return parser.errors();
|
2021-09-09 17:02:40 +00:00
|
|
|
|
2022-04-30 23:13:33 +00:00
|
|
|
// 3. Return Script Record { [[Realm]]: realm, [[ECMAScriptCode]]: script, [[HostDefined]]: hostDefined }.
|
2024-11-13 17:13:46 +00:00
|
|
|
return realm.heap().allocate<Script>(realm, filename, move(script), host_defined);
|
2021-09-09 16:02:31 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 17:25:39 +00:00
|
|
|
Script::Script(Realm& realm, StringView filename, NonnullRefPtr<Program> parse_node, HostDefined* host_defined)
|
2022-09-05 12:31:25 +00:00
|
|
|
: m_realm(realm)
|
2021-09-09 16:02:31 +00:00
|
|
|
, m_parse_node(move(parse_node))
|
2022-01-18 18:21:42 +00:00
|
|
|
, m_filename(filename)
|
2022-02-07 17:25:39 +00:00
|
|
|
, m_host_defined(host_defined)
|
2021-09-09 16:02:31 +00:00
|
|
|
{
|
|
|
|
}
|
2022-04-17 20:59:52 +00:00
|
|
|
|
2022-09-05 12:31:25 +00:00
|
|
|
Script::~Script()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Script::visit_edges(Cell::Visitor& visitor)
|
|
|
|
{
|
|
|
|
Base::visit_edges(visitor);
|
|
|
|
visitor.visit(m_realm);
|
2022-09-05 23:19:58 +00:00
|
|
|
if (m_host_defined)
|
|
|
|
m_host_defined->visit_host_defined_self(visitor);
|
2023-10-28 21:56:15 +00:00
|
|
|
for (auto const& loaded_module : m_loaded_modules)
|
|
|
|
visitor.visit(loaded_module.module);
|
2022-09-05 12:31:25 +00:00
|
|
|
}
|
|
|
|
|
2021-09-09 16:02:31 +00:00
|
|
|
}
|