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/Libraries/LibWeb/DOM | |
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/Libraries/LibWeb/DOM')
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Document.cpp | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index 170c0a2e7b..f47bca16cb 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -679,15 +679,17 @@ JS::Interpreter& Document::interpreter() JS::Value Document::run_javascript(StringView source, StringView filename) { - auto parser = JS::Parser(JS::Lexer(source, filename)); - auto program = parser.parse_program(); - if (parser.has_errors()) { - parser.print_errors(false); + // FIXME: The only user of this function now is javascript: URLs. Refactor them to follow the spec: https://html.spec.whatwg.org/multipage/browsing-the-web.html#javascript-protocol + auto& interpreter = document().interpreter(); + auto script_or_error = JS::Script::parse(source, interpreter.realm(), filename); + if (script_or_error.is_error()) { + // FIXME: Add error logging back. return JS::js_undefined(); } - auto& interpreter = document().interpreter(); + + auto result = interpreter.run(script_or_error.value()); + auto& vm = interpreter.vm(); - auto result = interpreter.run(interpreter.global_object(), *program); if (result.is_error()) { // FIXME: I'm sure the spec could tell us something about error propagation here! vm.clear_exception(); |