LibJS+LibWeb: Make JS::Script and Web::HTML::ClassicScript use Realms

The spec wants Script Records to have a Realm, not a GlobalObject.
This commit is contained in:
Linus Groh 2021-09-12 12:02:20 +01:00 committed by Andreas Kling
parent 673fc02ac5
commit 106f295916
Notes: sideshowbarker 2024-07-18 04:09:49 +09:00
5 changed files with 17 additions and 16 deletions

View File

@ -12,7 +12,7 @@
namespace JS { namespace JS {
// 16.1.5 ParseScript ( sourceText, realm, hostDefined ), https://tc39.es/ecma262/#sec-parse-script // 16.1.5 ParseScript ( sourceText, realm, hostDefined ), https://tc39.es/ecma262/#sec-parse-script
NonnullRefPtr<Script> Script::parse(StringView source_text, GlobalObject& global_object, StringView filename) NonnullRefPtr<Script> Script::parse(StringView source_text, Realm& realm, StringView filename)
{ {
// 1. Let body be ParseText(sourceText, Script). // 1. Let body be ParseText(sourceText, Script).
auto body = Parser(Lexer(source_text, filename)).parse_program(); auto body = Parser(Lexer(source_text, filename)).parse_program();
@ -20,11 +20,11 @@ NonnullRefPtr<Script> Script::parse(StringView source_text, GlobalObject& global
// FIXME: 2. If body is a List of errors, return body. // FIXME: 2. If body is a List of errors, return body.
// 3. Return Script Record { [[Realm]]: realm, [[ECMAScriptCode]]: body, [[HostDefined]]: hostDefined }. // 3. Return Script Record { [[Realm]]: realm, [[ECMAScriptCode]]: body, [[HostDefined]]: hostDefined }.
return adopt_ref(*new Script(global_object, move(body))); return adopt_ref(*new Script(realm, move(body)));
} }
Script::Script(GlobalObject& global_object, NonnullRefPtr<Program> parse_node) Script::Script(Realm& realm, NonnullRefPtr<Program> parse_node)
: m_global_object(make_handle(&global_object)) : m_realm(make_handle(&realm))
, m_parse_node(move(parse_node)) , m_parse_node(move(parse_node))
{ {
} }

View File

@ -10,7 +10,7 @@
#include <AK/RefCounted.h> #include <AK/RefCounted.h>
#include <LibJS/AST.h> #include <LibJS/AST.h>
#include <LibJS/Heap/Handle.h> #include <LibJS/Heap/Handle.h>
#include <LibJS/Runtime/GlobalObject.h> #include <LibJS/Runtime/Realm.h>
namespace JS { namespace JS {
@ -18,16 +18,16 @@ namespace JS {
class Script : public RefCounted<Script> { class Script : public RefCounted<Script> {
public: public:
~Script(); ~Script();
static NonnullRefPtr<Script> parse(StringView source_text, GlobalObject&, StringView filename = {}); static NonnullRefPtr<Script> parse(StringView source_text, Realm&, StringView filename = {});
GlobalObject& global_object() { return *m_global_object.cell(); } Realm& realm() { return *m_realm.cell(); }
Program const& parse_node() const { return *m_parse_node; } Program const& parse_node() const { return *m_parse_node; }
private: private:
Script(GlobalObject&, NonnullRefPtr<Program>); Script(Realm&, NonnullRefPtr<Program>);
Handle<GlobalObject> m_global_object; Handle<Realm> m_realm; // [[Realm]]
NonnullRefPtr<Program> m_parse_node; NonnullRefPtr<Program> m_parse_node; // [[ECMAScriptCode]]
}; };
} }

View File

@ -6,6 +6,7 @@
#include <AK/Debug.h> #include <AK/Debug.h>
#include <AK/StringBuilder.h> #include <AK/StringBuilder.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Parser.h> #include <LibJS/Parser.h>
#include <LibTextCodec/Decoder.h> #include <LibTextCodec/Decoder.h>
#include <LibWeb/DOM/Document.h> #include <LibWeb/DOM/Document.h>
@ -303,7 +304,7 @@ void HTMLScriptElement::prepare_script()
document().interpreter(); document().interpreter();
// FIXME: This is all ad-hoc and needs work. // FIXME: This is all ad-hoc and needs work.
auto script = ClassicScript::create(url.to_string(), data, *document().window().wrapper(), URL()); auto script = ClassicScript::create(url.to_string(), data, document().interpreter().realm(), URL());
// When the chosen algorithm asynchronously completes, set the script's script to the result. At that time, the script is ready. // When the chosen algorithm asynchronously completes, set the script's script to the result. At that time, the script is ready.
m_script = script; m_script = script;
@ -330,7 +331,7 @@ void HTMLScriptElement::prepare_script()
document().interpreter(); document().interpreter();
// FIXME: Pass settings, base URL and options. // FIXME: Pass settings, base URL and options.
auto script = ClassicScript::create(m_document->url().to_string(), source_text, *document().window().wrapper(), URL()); auto script = ClassicScript::create(m_document->url().to_string(), source_text, document().interpreter().realm(), URL());
// 2. Set the script's script to script. // 2. Set the script's script to script.
m_script = script; m_script = script;

View File

@ -10,7 +10,7 @@
namespace Web::HTML { namespace Web::HTML {
// https://html.spec.whatwg.org/multipage/webappapis.html#creating-a-classic-script // https://html.spec.whatwg.org/multipage/webappapis.html#creating-a-classic-script
NonnullRefPtr<ClassicScript> ClassicScript::create(String filename, StringView source, JS::GlobalObject& global_object, URL base_url, MutedErrors muted_errors) NonnullRefPtr<ClassicScript> ClassicScript::create(String filename, StringView source, JS::Realm& realm, URL base_url, MutedErrors muted_errors)
{ {
// 1. If muted errors was not provided, let it be false. (NOTE: This is taken care of by the default argument.) // 1. If muted errors was not provided, let it be false. (NOTE: This is taken care of by the default argument.)
@ -35,7 +35,7 @@ NonnullRefPtr<ClassicScript> ClassicScript::create(String filename, StringView s
// FIXME: 9. Set script's parse error and error to rethrow to null. // FIXME: 9. Set script's parse error and error to rethrow to null.
// 10. Let result be ParseScript(source, settings's Realm, script). // 10. Let result be ParseScript(source, settings's Realm, script).
auto result = JS::Script::parse(source, global_object, script->filename()); auto result = JS::Script::parse(source, realm, script->filename());
// FIXME: 11. If result is a list of errors, then: // FIXME: 11. If result is a list of errors, then:
// 1. Set script's parse error and its error to rethrow to result[0]. // 1. Set script's parse error and its error to rethrow to result[0].
@ -53,7 +53,7 @@ JS::Value ClassicScript::run(RethrowErrors rethrow_errors)
{ {
(void)rethrow_errors; (void)rethrow_errors;
auto interpreter = JS::Interpreter::create_with_existing_global_object(m_script_record->global_object()); auto interpreter = JS::Interpreter::create_with_existing_global_object(m_script_record->realm().global_object());
interpreter->run(interpreter->global_object(), m_script_record->parse_node()); interpreter->run(interpreter->global_object(), m_script_record->parse_node());
auto& vm = interpreter->vm(); auto& vm = interpreter->vm();
if (vm.exception()) if (vm.exception())

View File

@ -20,7 +20,7 @@ public:
No, No,
Yes, Yes,
}; };
static NonnullRefPtr<ClassicScript> create(String filename, StringView source, JS::GlobalObject&, URL base_url, MutedErrors = MutedErrors::No); static NonnullRefPtr<ClassicScript> create(String filename, StringView source, JS::Realm&, URL base_url, MutedErrors = MutedErrors::No);
JS::Script* script_record() { return m_script_record; } JS::Script* script_record() { return m_script_record; }
JS::Script const* script_record() const { return m_script_record; } JS::Script const* script_record() const { return m_script_record; }