diff options
author | Luke Wilde <lukew@serenityos.org> | 2022-01-16 13:16:04 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-01-22 01:21:18 +0000 |
commit | 631bbcd00a32773aa22ece270ba00628c1240d4c (patch) | |
tree | a12959a306d8a47ffe3f343942e69b9f4abb2a82 /Userland/Applications/Spreadsheet | |
parent | 232a8432b7f879140c50df8114b7467f45e99349 (diff) | |
download | serenity-631bbcd00a32773aa22ece270ba00628c1240d4c.zip |
LibJS: Refactor interpreter to use Script and Source Text Modules
This also refactors interpreter creation to follow
InitializeHostDefinedRealm, but I couldn't fit it in the title :^)
This allows us to follow the spec much more closely rather than being
completely ad-hoc with just the parse node instead of having all the
surrounding data such as the realm of the parse node.
The interpreter creation refactor creates the global execution context
once and doesn't take it off the stack. This allows LibWeb to take the
global execution context and manually handle it, following the HTML
spec. The HTML spec calls this the "realm execution context" of the
environment settings object.
It also allows us to specify the globalThis type, as it can be
different from the global object type. For example, on the web, Window
global objects use a WindowProxy global this value to enforce the same
origin policy on operations like [[GetOwnProperty]].
Finally, it allows us to directly call Program::execute in perform_eval
and perform_shadow_realm_eval as this moves
global_declaration_instantiation into Interpreter::run
(ScriptEvaluation) as per the spec.
Note that this doesn't evalulate Source Text Modules yet or refactor
the bytecode interpreter, that's work for future us :^)
This patch was originally build by Luke for the environment settings
object change but was also needed for modules. So I (davidot) have
modified it with the new completion changes and setup for that.
Co-authored-by: davidot <davidot@serenityos.org>
Diffstat (limited to 'Userland/Applications/Spreadsheet')
4 files changed, 18 insertions, 14 deletions
diff --git a/Userland/Applications/Spreadsheet/JSIntegration.cpp b/Userland/Applications/Spreadsheet/JSIntegration.cpp index a14d4d7fd9..5ddc2ffb46 100644 --- a/Userland/Applications/Spreadsheet/JSIntegration.cpp +++ b/Userland/Applications/Spreadsheet/JSIntegration.cpp @@ -355,8 +355,8 @@ JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::get_column_bound) return JS::Value(bounds.row); } -WorkbookObject::WorkbookObject(Workbook& workbook) - : JS::Object(*JS::Object::create(workbook.vm().interpreter().global_object(), workbook.vm().interpreter().global_object().object_prototype())) +WorkbookObject::WorkbookObject(Workbook& workbook, JS::GlobalObject& global_object) + : JS::Object(*JS::Object::create(global_object, global_object.object_prototype())) , m_workbook(workbook) { } diff --git a/Userland/Applications/Spreadsheet/JSIntegration.h b/Userland/Applications/Spreadsheet/JSIntegration.h index 24ed2e8bd7..67eb5fcec5 100644 --- a/Userland/Applications/Spreadsheet/JSIntegration.h +++ b/Userland/Applications/Spreadsheet/JSIntegration.h @@ -49,7 +49,7 @@ class WorkbookObject final : public JS::Object { JS_OBJECT(WorkbookObject, JS::Object); public: - WorkbookObject(Workbook&); + WorkbookObject(Workbook&, JS::GlobalObject&); virtual ~WorkbookObject() override; diff --git a/Userland/Applications/Spreadsheet/Spreadsheet.cpp b/Userland/Applications/Spreadsheet/Spreadsheet.cpp index 6e60d5afef..a514921805 100644 --- a/Userland/Applications/Spreadsheet/Spreadsheet.cpp +++ b/Userland/Applications/Spreadsheet/Spreadsheet.cpp @@ -47,15 +47,19 @@ Sheet::Sheet(Workbook& workbook) global_object().define_direct_property("thisSheet", &global_object(), JS::default_attributes); // Self-reference is unfortunate, but required. // Sadly, these have to be evaluated once per sheet. - auto file_or_error = Core::File::open("/res/js/Spreadsheet/runtime.js", Core::OpenMode::ReadOnly); + constexpr StringView runtime_file_path = "/res/js/Spreadsheet/runtime.js"; + auto file_or_error = Core::File::open(runtime_file_path, Core::OpenMode::ReadOnly); if (!file_or_error.is_error()) { auto buffer = file_or_error.value()->read_all(); - JS::Parser parser { JS::Lexer(buffer) }; - if (parser.has_errors()) { + auto script_or_error = JS::Script::parse(buffer, interpreter().realm(), runtime_file_path); + if (script_or_error.is_error()) { warnln("Spreadsheet: Failed to parse runtime code"); - parser.print_errors(); + for (auto& error : script_or_error.error()) { + // FIXME: This doesn't print hints anymore + warnln("SyntaxError: {}", error.to_string()); + } } else { - (void)interpreter().run(global_object(), parser.parse_program()); + (void)interpreter().run(script_or_error.value()); if (auto* exception = interpreter().exception()) { warnln("Spreadsheet: Failed to run runtime code:"); for (auto& traceback_frame : exception->traceback()) { @@ -159,14 +163,14 @@ Sheet::ValueAndException Sheet::evaluate(StringView source, Cell* on_behalf_of) TemporaryChange cell_change { m_current_cell_being_evaluated, on_behalf_of }; ScopeGuard clear_exception { [&] { interpreter().vm().clear_exception(); } }; - auto parser = JS::Parser(JS::Lexer(source)); - auto program = parser.parse_program(); - if (parser.has_errors() || interpreter().exception()) + auto script_or_error = JS::Script::parse(source, interpreter().realm()); + // FIXME: Convert parser errors to exceptions to show them to the user. + if (script_or_error.is_error() || interpreter().exception()) return { JS::js_undefined(), interpreter().exception() }; - auto result = interpreter().run(global_object(), program); + auto result = interpreter().run(script_or_error.value()); if (result.is_error()) { - auto exc = interpreter().exception(); + auto* exc = interpreter().exception(); return { JS::js_undefined(), exc }; } return { result.value(), {} }; diff --git a/Userland/Applications/Spreadsheet/Workbook.cpp b/Userland/Applications/Spreadsheet/Workbook.cpp index c79b872119..cf27699373 100644 --- a/Userland/Applications/Spreadsheet/Workbook.cpp +++ b/Userland/Applications/Spreadsheet/Workbook.cpp @@ -29,7 +29,7 @@ Workbook::Workbook(NonnullRefPtrVector<Sheet>&& sheets, GUI::Window& parent_wind , m_main_execution_context(m_vm->heap()) , m_parent_window(parent_window) { - m_workbook_object = m_vm->heap().allocate<WorkbookObject>(m_interpreter->global_object(), *this); + m_workbook_object = m_vm->heap().allocate<WorkbookObject>(m_interpreter->global_object(), *this, m_interpreter->global_object()); m_interpreter->global_object().define_direct_property("workbook", workbook_object(), JS::default_attributes); m_main_execution_context.current_node = nullptr; |