ladybird/Userland/Libraries/LibJS/Script.cpp
Andreas Kling 10c489713d LibJS+LibWeb: Let JS::Script::parse() return a list of errors (on error)
These are really supposed to be a list of SyntaxError objects, but for
now we simply return all the Parser::Error objects we got from Parser.
2021-09-14 21:41:51 +02:00

46 lines
1.2 KiB
C++

/*
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibCore/ElapsedTimer.h>
#include <LibJS/AST.h>
#include <LibJS/Lexer.h>
#include <LibJS/Parser.h>
#include <LibJS/Script.h>
namespace JS {
// 16.1.5 ParseScript ( sourceText, realm, hostDefined ), https://tc39.es/ecma262/#sec-parse-script
Result<NonnullRefPtr<Script>, Vector<Parser::Error>> Script::parse(StringView source_text, Realm& realm, StringView filename)
{
auto timer = Core::ElapsedTimer::start_new();
ScopeGuard timer_guard([&] {
dbgln("JS::Script: Parsed {} in {}ms", filename, timer.elapsed());
});
// 1. Let body be ParseText(sourceText, Script).
auto parser = Parser(Lexer(source_text, filename));
auto body = parser.parse_program();
// 2. If body is a List of errors, return body.
if (parser.has_errors())
return parser.errors();
// 3. Return Script Record { [[Realm]]: realm, [[ECMAScriptCode]]: body, [[HostDefined]]: hostDefined }.
return adopt_ref(*new Script(realm, move(body)));
}
Script::Script(Realm& realm, NonnullRefPtr<Program> parse_node)
: m_vm(realm.vm())
, m_realm(make_handle(&realm))
, m_parse_node(move(parse_node))
{
}
Script::~Script()
{
}
}