mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
js: Throw a regular SyntaxError for errors from the parser
This commit is contained in:
parent
33defef267
commit
4e0ed34d7e
Notes:
sideshowbarker
2024-07-19 06:38:44 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/4e0ed34d7ec Pull-request: https://github.com/SerenityOS/serenity/pull/2221 Reviewed-by: https://github.com/Dexesttp
1 changed files with 30 additions and 48 deletions
|
@ -269,6 +269,32 @@ bool write_to_file(const StringView& path)
|
|||
return true;
|
||||
}
|
||||
|
||||
bool parse_and_run(JS::Interpreter& interpreter, const StringView& source)
|
||||
{
|
||||
auto parser = JS::Parser(JS::Lexer(source));
|
||||
auto program = parser.parse_program();
|
||||
|
||||
if (s_dump_ast)
|
||||
program->dump(0);
|
||||
|
||||
if (parser.has_errors()) {
|
||||
auto error = parser.errors()[0];
|
||||
interpreter.throw_exception<JS::SyntaxError>(error.to_string());
|
||||
} else {
|
||||
interpreter.run(*program);
|
||||
}
|
||||
|
||||
if (interpreter.exception()) {
|
||||
printf("Uncaught exception: ");
|
||||
print(interpreter.exception()->value());
|
||||
interpreter.clear_exception();
|
||||
return false;
|
||||
}
|
||||
if (s_print_last_result)
|
||||
print(interpreter.last_value());
|
||||
return true;
|
||||
}
|
||||
|
||||
ReplObject::ReplObject()
|
||||
{
|
||||
}
|
||||
|
@ -335,17 +361,7 @@ JS::Value ReplObject::load_file(JS::Interpreter& interpreter)
|
|||
} else {
|
||||
source = file_contents;
|
||||
}
|
||||
auto parser = JS::Parser(JS::Lexer(source));
|
||||
auto program = parser.parse_program();
|
||||
if (s_dump_ast)
|
||||
program->dump(0);
|
||||
|
||||
if (parser.has_errors())
|
||||
continue;
|
||||
|
||||
interpreter.run(*program);
|
||||
if (s_print_last_result)
|
||||
print(interpreter.last_value());
|
||||
parse_and_run(interpreter, source);
|
||||
}
|
||||
return JS::Value(true);
|
||||
}
|
||||
|
@ -357,24 +373,7 @@ void repl(JS::Interpreter& interpreter)
|
|||
if (piece.is_empty())
|
||||
continue;
|
||||
repl_statements.append(piece);
|
||||
auto parser = JS::Parser(JS::Lexer(piece));
|
||||
auto program = parser.parse_program();
|
||||
if (s_dump_ast)
|
||||
program->dump(0);
|
||||
|
||||
if (parser.has_errors()) {
|
||||
printf("Parse error\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
interpreter.run(*program);
|
||||
if (interpreter.exception()) {
|
||||
printf("Uncaught exception: ");
|
||||
print(interpreter.exception()->value());
|
||||
interpreter.clear_exception();
|
||||
} else {
|
||||
print(interpreter.last_value());
|
||||
}
|
||||
parse_and_run(interpreter, piece);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -491,6 +490,7 @@ int main(int argc, char** argv)
|
|||
};
|
||||
|
||||
if (script_path == nullptr) {
|
||||
s_print_last_result = true;
|
||||
interpreter = JS::Interpreter::create<ReplObject>();
|
||||
ReplConsoleClient console_client(interpreter->console());
|
||||
interpreter->console().set_client(console_client);
|
||||
|
@ -742,27 +742,9 @@ int main(int argc, char** argv)
|
|||
} else {
|
||||
source = file_contents;
|
||||
}
|
||||
auto parser = JS::Parser(JS::Lexer(source));
|
||||
auto program = parser.parse_program();
|
||||
|
||||
if (s_dump_ast)
|
||||
program->dump(0);
|
||||
|
||||
if (parser.has_errors()) {
|
||||
printf("Parse Error\n");
|
||||
if (!parse_and_run(*interpreter, source))
|
||||
return 1;
|
||||
}
|
||||
|
||||
auto result = interpreter->run(*program);
|
||||
|
||||
if (interpreter->exception()) {
|
||||
printf("Uncaught exception: ");
|
||||
print(interpreter->exception()->value());
|
||||
interpreter->clear_exception();
|
||||
return 1;
|
||||
}
|
||||
if (s_print_last_result)
|
||||
print(result);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
Loading…
Reference in a new issue