1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 08:17:45 +00:00

Meta: Port LibJS Wasm repl to the new LibJS architecture

This was left behind when removing the AST interpreter, and the CI not
being around made it bitrot.

Co-Authored-By: Dan Klishch <danilklishch@gmail.com>
This commit is contained in:
Ali Mohammad Pur 2023-10-27 01:58:43 +03:30 committed by Ali Mohammad Pur
parent 072c4eeb50
commit aa4360b9a3

View file

@ -18,6 +18,7 @@
#include <LibJS/Parser.h> #include <LibJS/Parser.h>
#include <LibJS/Print.h> #include <LibJS/Print.h>
#include <LibJS/Runtime/ConsoleObject.h> #include <LibJS/Runtime/ConsoleObject.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/JSONObject.h> #include <LibJS/Runtime/JSONObject.h>
#include <LibJS/Runtime/StringPrototype.h> #include <LibJS/Runtime/StringPrototype.h>
#include <LibJS/SourceTextModule.h> #include <LibJS/SourceTextModule.h>
@ -39,7 +40,7 @@
class ReplConsoleClient; class ReplConsoleClient;
RefPtr<JS::VM> g_vm; RefPtr<JS::VM> g_vm;
OwnPtr<JS::Interpreter> g_interpreter; OwnPtr<JS::ExecutionContext> g_execution_context;
OwnPtr<ReplConsoleClient> g_console_client; OwnPtr<ReplConsoleClient> g_console_client;
JS::Handle<JS::Value> g_last_value = JS::make_handle(JS::js_undefined()); JS::Handle<JS::Value> g_last_value = JS::make_handle(JS::js_undefined());
@ -104,8 +105,10 @@ static bool s_dump_ast = false;
static bool s_as_module = false; static bool s_as_module = false;
static bool s_print_last_result = false; static bool s_print_last_result = false;
static ErrorOr<bool> parse_and_run(JS::Interpreter& interpreter, StringView source, StringView source_name) static ErrorOr<bool> parse_and_run(JS::Realm& realm, StringView source, StringView source_name)
{ {
auto& interpreter = g_vm->bytecode_interpreter();
enum class ReturnEarly { enum class ReturnEarly {
No, No,
Yes, Yes,
@ -123,7 +126,7 @@ static ErrorOr<bool> parse_and_run(JS::Interpreter& interpreter, StringView sour
}; };
if (!s_as_module) { if (!s_as_module) {
auto script_or_error = JS::Script::parse(source, interpreter.realm(), source_name); auto script_or_error = JS::Script::parse(source, realm, source_name);
if (script_or_error.is_error()) { if (script_or_error.is_error()) {
auto error = script_or_error.error()[0]; auto error = script_or_error.error()[0];
auto hint = error.source_location_hint(source); auto hint = error.source_location_hint(source);
@ -132,14 +135,14 @@ static ErrorOr<bool> parse_and_run(JS::Interpreter& interpreter, StringView sour
auto error_string = TRY(error.to_string()); auto error_string = TRY(error.to_string());
displayln("{}", error_string); displayln("{}", error_string);
result = interpreter.vm().throw_completion<JS::SyntaxError>(move(error_string)); result = g_vm->throw_completion<JS::SyntaxError>(move(error_string));
} else { } else {
auto return_early = TRY(run_script_or_module(script_or_error.value())); auto return_early = TRY(run_script_or_module(script_or_error.value()));
if (return_early == ReturnEarly::Yes) if (return_early == ReturnEarly::Yes)
return true; return true;
} }
} else { } else {
auto module_or_error = JS::SourceTextModule::parse(source, interpreter.realm(), source_name); auto module_or_error = JS::SourceTextModule::parse(source, realm, source_name);
if (module_or_error.is_error()) { if (module_or_error.is_error()) {
auto error = module_or_error.error()[0]; auto error = module_or_error.error()[0];
auto hint = error.source_location_hint(source); auto hint = error.source_location_hint(source);
@ -148,7 +151,7 @@ static ErrorOr<bool> parse_and_run(JS::Interpreter& interpreter, StringView sour
auto error_string = TRY(error.to_string()); auto error_string = TRY(error.to_string());
displayln("{}", error_string); displayln("{}", error_string);
result = interpreter.vm().throw_completion<JS::SyntaxError>(move(error_string)); result = g_vm->throw_completion<JS::SyntaxError>(move(error_string));
} else { } else {
auto return_early = TRY(run_script_or_module(module_or_error.value())); auto return_early = TRY(run_script_or_module(module_or_error.value()));
if (return_early == ReturnEarly::Yes) if (return_early == ReturnEarly::Yes)
@ -344,21 +347,20 @@ extern "C" int initialize_repl(char const* time_zone)
(void)print(promise.result()); (void)print(promise.result());
displayln(")"); displayln(")");
}; };
OwnPtr<JS::Interpreter> interpreter;
s_print_last_result = true; s_print_last_result = true;
interpreter = JS::Interpreter::create<ReplObject>(*g_vm); g_execution_context = JS::create_simple_execution_context<ReplObject>(*g_vm);
auto console_object = interpreter->realm().intrinsics().console_object(); auto& realm = *g_execution_context->realm;
auto console_object = realm.intrinsics().console_object();
g_console_client = make<ReplConsoleClient>(console_object->console()); g_console_client = make<ReplConsoleClient>(console_object->console());
console_object->console().set_client(*g_console_client); console_object->console().set_client(*g_console_client);
g_interpreter = move(interpreter);
return 0; return 0;
} }
extern "C" bool execute(char const* source) extern "C" bool execute(char const* source)
{ {
if (auto result = parse_and_run(*g_interpreter, { source, strlen(source) }, "REPL"sv); result.is_error()) { if (auto result = parse_and_run(*g_execution_context->realm, { source, strlen(source) }, "REPL"sv); result.is_error()) {
displayln("{}", result.error()); displayln("{}", result.error());
return false; return false;
} else { } else {