Przeglądaj źródła

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.
Andreas Kling 3 lat temu
rodzic
commit
10c489713d

+ 6 - 3
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)));

+ 2 - 1
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; }

+ 9 - 4
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;