mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 22:07:35 +00:00
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>
This commit is contained in:
parent
232a8432b7
commit
631bbcd00a
20 changed files with 293 additions and 129 deletions
|
@ -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(), {} };
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue