2021-09-09 16:02:31 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
|
|
|
* 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 {
|
|
|
|
|
2021-09-09 17:02:40 +00:00
|
|
|
// 16.1.5 ParseScript ( sourceText, realm, hostDefined ), https://tc39.es/ecma262/#sec-parse-script
|
2022-09-05 12:31:25 +00:00
|
|
|
Result<NonnullGCPtr<Script>, Vector<Parser::Error>> 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 }.
|
2022-09-05 12:31:25 +00:00
|
|
|
return NonnullGCPtr(*realm.heap().allocate_without_realm<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);
|
|
|
|
}
|
|
|
|
|
2021-09-09 16:02:31 +00:00
|
|
|
}
|