1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:18:11 +00:00

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.
This commit is contained in:
Andreas Kling 2021-09-14 20:56:57 +02:00
parent 5fa02b8a9e
commit 10c489713d
3 changed files with 17 additions and 8 deletions

View file

@ -13,7 +13,7 @@
namespace JS { namespace JS {
// 16.1.5 ParseScript ( sourceText, realm, hostDefined ), https://tc39.es/ecma262/#sec-parse-script // 16.1.5 ParseScript ( sourceText, realm, hostDefined ), https://tc39.es/ecma262/#sec-parse-script
NonnullRefPtr<Script> Script::parse(StringView source_text, Realm& realm, StringView filename) Result<NonnullRefPtr<Script>, Vector<Parser::Error>> Script::parse(StringView source_text, Realm& realm, StringView filename)
{ {
auto timer = Core::ElapsedTimer::start_new(); auto timer = Core::ElapsedTimer::start_new();
ScopeGuard timer_guard([&] { ScopeGuard timer_guard([&] {
@ -21,9 +21,12 @@ NonnullRefPtr<Script> Script::parse(StringView source_text, Realm& realm, String
}); });
// 1. Let body be ParseText(sourceText, Script). // 1. Let body be ParseText(sourceText, Script).
auto body = Parser(Lexer(source_text, filename)).parse_program(); auto parser = Parser(Lexer(source_text, filename));
auto body = parser.parse_program();
// FIXME: 2. If body is a List of errors, return body. // 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 }. // 3. Return Script Record { [[Realm]]: realm, [[ECMAScriptCode]]: body, [[HostDefined]]: hostDefined }.
return adopt_ref(*new Script(realm, move(body))); return adopt_ref(*new Script(realm, move(body)));

View file

@ -10,6 +10,7 @@
#include <AK/RefCounted.h> #include <AK/RefCounted.h>
#include <LibJS/AST.h> #include <LibJS/AST.h>
#include <LibJS/Heap/Handle.h> #include <LibJS/Heap/Handle.h>
#include <LibJS/Parser.h>
#include <LibJS/Runtime/Realm.h> #include <LibJS/Runtime/Realm.h>
namespace JS { namespace JS {
@ -18,7 +19,7 @@ namespace JS {
class Script : public RefCounted<Script> { class Script : public RefCounted<Script> {
public: public:
~Script(); ~Script();
static NonnullRefPtr<Script> parse(StringView source_text, Realm&, StringView filename = {}); static Result<NonnullRefPtr<Script>, Vector<Parser::Error>> parse(StringView source_text, Realm&, StringView filename = {});
Realm& realm() { return *m_realm.cell(); } Realm& realm() { return *m_realm.cell(); }
Program const& parse_node() const { return *m_parse_node; } Program const& parse_node() const { return *m_parse_node; }

View file

@ -38,12 +38,17 @@ NonnullRefPtr<ClassicScript> ClassicScript::create(String filename, StringView s
// 10. Let result be ParseScript(source, settings's Realm, script). // 10. Let result be ParseScript(source, settings's Realm, script).
auto result = JS::Script::parse(source, realm, script->filename()); auto result = JS::Script::parse(source, realm, script->filename());
// FIXME: 11. If result is a list of errors, then: // 11. If result is a list of errors, then:
// 1. Set script's parse error and its error to rethrow to result[0].
// 2. Return script. if (result.is_error()) {
// FIXME: 1. Set script's parse error and its error to rethrow to result[0].
// 2. Return script.
return script;
}
// 12. Set script's record to result. // 12. Set script's record to result.
script->m_script_record = move(result); script->m_script_record = result.release_value();
// 13. Return script. // 13. Return script.
return script; return script;