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-03-13 21:17:35 +00:00
|
|
|
Result<NonnullRefPtr<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
|
|
|
{
|
2021-09-09 17:02:40 +00:00
|
|
|
// 1. Let body be ParseText(sourceText, Script).
|
2022-03-13 21:17:35 +00:00
|
|
|
auto parser = Parser(Lexer(source_text, filename, line_number_offset));
|
2021-09-14 18:56:57 +00:00
|
|
|
auto body = parser.parse_program();
|
2021-09-09 17:02:40 +00:00
|
|
|
|
2021-09-14 18:56:57 +00:00
|
|
|
// 2. If body is a List of errors, return body.
|
|
|
|
if (parser.has_errors())
|
|
|
|
return parser.errors();
|
2021-09-09 17:02:40 +00:00
|
|
|
|
|
|
|
// 3. Return Script Record { [[Realm]]: realm, [[ECMAScriptCode]]: body, [[HostDefined]]: hostDefined }.
|
2022-02-07 17:25:39 +00:00
|
|
|
return adopt_ref(*new Script(realm, filename, move(body), 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)
|
2021-09-14 18:30:04 +00:00
|
|
|
: m_vm(realm.vm())
|
|
|
|
, m_realm(make_handle(&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
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|