diff options
author | Andreas Kling <kling@serenityos.org> | 2021-09-14 20:56:57 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-09-14 21:41:51 +0200 |
commit | 10c489713d828e002e1577a4fcdfafc37741afa0 (patch) | |
tree | f7dae4fdc4051a189b77156f907e81ae40dbe9c7 | |
parent | 5fa02b8a9e41f680a7996fee3f5f5f708d0ec638 (diff) | |
download | serenity-10c489713d828e002e1577a4fcdfafc37741afa0.zip |
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.
-rw-r--r-- | Userland/Libraries/LibJS/Script.cpp | 9 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Script.h | 3 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.cpp | 13 |
3 files changed, 17 insertions, 8 deletions
diff --git a/Userland/Libraries/LibJS/Script.cpp b/Userland/Libraries/LibJS/Script.cpp index 9a4b5460a4..2df2dc2a63 100644 --- a/Userland/Libraries/LibJS/Script.cpp +++ b/Userland/Libraries/LibJS/Script.cpp @@ -13,7 +13,7 @@ namespace JS { // 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(); ScopeGuard timer_guard([&] { @@ -21,9 +21,12 @@ NonnullRefPtr<Script> Script::parse(StringView source_text, Realm& realm, String }); // 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 }. return adopt_ref(*new Script(realm, move(body))); diff --git a/Userland/Libraries/LibJS/Script.h b/Userland/Libraries/LibJS/Script.h index 037dea4629..c58ddf956f 100644 --- a/Userland/Libraries/LibJS/Script.h +++ b/Userland/Libraries/LibJS/Script.h @@ -10,6 +10,7 @@ #include <AK/RefCounted.h> #include <LibJS/AST.h> #include <LibJS/Heap/Handle.h> +#include <LibJS/Parser.h> #include <LibJS/Runtime/Realm.h> namespace JS { @@ -18,7 +19,7 @@ namespace JS { class Script : public RefCounted<Script> { public: ~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(); } Program const& parse_node() const { return *m_parse_node; } diff --git a/Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.cpp b/Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.cpp index da2022b2b8..c5a14b98a6 100644 --- a/Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.cpp +++ b/Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.cpp @@ -38,12 +38,17 @@ NonnullRefPtr<ClassicScript> ClassicScript::create(String filename, StringView s // 10. Let result be ParseScript(source, settings's Realm, script). auto result = JS::Script::parse(source, realm, script->filename()); - // FIXME: 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. + // 11. If result is a list of errors, then: + + 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. - script->m_script_record = move(result); + script->m_script_record = result.release_value(); // 13. Return script. return script; |