mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 20:47:45 +00:00
LibJS+LibWeb: Restore type safety of Realm::set_global_object()
The changes from 8a03b17
to allow any JS::Value aren't a good fit, as
shown by the excessive amount of verify_cast needed :^)
This commit is contained in:
parent
64b29eb459
commit
c8f1651761
10 changed files with 27 additions and 36 deletions
|
@ -61,7 +61,7 @@ JS_DEFINE_NATIVE_FUNCTION($262Object::create_realm)
|
||||||
VERIFY(realm);
|
VERIFY(realm);
|
||||||
auto* realm_global_object = vm.heap().allocate_without_global_object<GlobalObject>(*realm);
|
auto* realm_global_object = vm.heap().allocate_without_global_object<GlobalObject>(*realm);
|
||||||
VERIFY(realm_global_object);
|
VERIFY(realm_global_object);
|
||||||
realm->set_global_object(realm_global_object, js_undefined());
|
realm->set_global_object(realm_global_object, nullptr);
|
||||||
realm_global_object->set_associated_realm(*realm);
|
realm_global_object->set_associated_realm(*realm);
|
||||||
realm_global_object->initialize_global_object();
|
realm_global_object->initialize_global_object();
|
||||||
return Value(realm_global_object->$262());
|
return Value(realm_global_object->$262());
|
||||||
|
|
|
@ -47,13 +47,11 @@ public:
|
||||||
|
|
||||||
interpreter->m_global_execution_context = MUST(Realm::initialize_host_defined_realm(
|
interpreter->m_global_execution_context = MUST(Realm::initialize_host_defined_realm(
|
||||||
vm,
|
vm,
|
||||||
[&](Realm& realm) -> Value {
|
[&](Realm& realm) -> GlobalObject* {
|
||||||
global_object = interpreter->heap().allocate_without_global_object<GlobalObjectType>(realm, forward<Args>(args)...);
|
global_object = interpreter->heap().allocate_without_global_object<GlobalObjectType>(realm, forward<Args>(args)...);
|
||||||
return global_object;
|
return global_object;
|
||||||
},
|
},
|
||||||
[](Realm&) -> Value {
|
nullptr));
|
||||||
return js_undefined();
|
|
||||||
}));
|
|
||||||
|
|
||||||
// NOTE: These are not in the spec.
|
// NOTE: These are not in the spec.
|
||||||
static FlyString global_execution_context_name = "(global execution context)";
|
static FlyString global_execution_context_name = "(global execution context)";
|
||||||
|
|
|
@ -21,7 +21,7 @@ Realm* Realm::create(VM& vm)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 9.6 InitializeHostDefinedRealm ( ), https://tc39.es/ecma262/#sec-initializehostdefinedrealm
|
// 9.6 InitializeHostDefinedRealm ( ), https://tc39.es/ecma262/#sec-initializehostdefinedrealm
|
||||||
ThrowCompletionOr<NonnullOwnPtr<ExecutionContext>> Realm::initialize_host_defined_realm(VM& vm, Function<Value(Realm&)> create_global_object, Function<Value(Realm&)> create_global_this_value)
|
ThrowCompletionOr<NonnullOwnPtr<ExecutionContext>> Realm::initialize_host_defined_realm(VM& vm, Function<GlobalObject*(Realm&)> create_global_object, Function<GlobalObject*(Realm&)> create_global_this_value)
|
||||||
{
|
{
|
||||||
DeferGC defer_gc(vm.heap());
|
DeferGC defer_gc(vm.heap());
|
||||||
|
|
||||||
|
@ -46,20 +46,16 @@ ThrowCompletionOr<NonnullOwnPtr<ExecutionContext>> Realm::initialize_host_define
|
||||||
// 7. If the host requires use of an exotic object to serve as realm's global object,
|
// 7. If the host requires use of an exotic object to serve as realm's global object,
|
||||||
// let global be such an object created in a host-defined manner.
|
// let global be such an object created in a host-defined manner.
|
||||||
// Otherwise, let global be undefined, indicating that an ordinary object should be created as the global object.
|
// Otherwise, let global be undefined, indicating that an ordinary object should be created as the global object.
|
||||||
Value global;
|
GlobalObject* global = nullptr;
|
||||||
if (create_global_object)
|
if (create_global_object)
|
||||||
global = create_global_object(*realm);
|
global = create_global_object(*realm);
|
||||||
else
|
|
||||||
global = js_undefined();
|
|
||||||
|
|
||||||
// 8. If the host requires that the this binding in realm's global scope return an object other than the global object,
|
// 8. If the host requires that the this binding in realm's global scope return an object other than the global object,
|
||||||
// let thisValue be such an object created in a host-defined manner.
|
// let thisValue be such an object created in a host-defined manner.
|
||||||
// Otherwise, let thisValue be undefined, indicating that realm's global this binding should be the global object.
|
// Otherwise, let thisValue be undefined, indicating that realm's global this binding should be the global object.
|
||||||
Value this_value;
|
GlobalObject* this_value = nullptr;
|
||||||
if (create_global_this_value)
|
if (create_global_this_value)
|
||||||
this_value = create_global_this_value(*realm);
|
this_value = create_global_this_value(*realm);
|
||||||
else
|
|
||||||
this_value = js_undefined();
|
|
||||||
|
|
||||||
// 9. Perform SetRealmGlobalObject(realm, global, thisValue).
|
// 9. Perform SetRealmGlobalObject(realm, global, thisValue).
|
||||||
realm->set_global_object(global, this_value);
|
realm->set_global_object(global, this_value);
|
||||||
|
@ -75,34 +71,33 @@ ThrowCompletionOr<NonnullOwnPtr<ExecutionContext>> Realm::initialize_host_define
|
||||||
}
|
}
|
||||||
|
|
||||||
// 9.3.3 SetRealmGlobalObject ( realmRec, globalObj, thisValue ), https://tc39.es/ecma262/#sec-setrealmglobalobject
|
// 9.3.3 SetRealmGlobalObject ( realmRec, globalObj, thisValue ), https://tc39.es/ecma262/#sec-setrealmglobalobject
|
||||||
void Realm::set_global_object(Value global_object, Value this_value)
|
void Realm::set_global_object(GlobalObject* global_object, GlobalObject* this_value)
|
||||||
{
|
{
|
||||||
// 1. If globalObj is undefined, then
|
// 1. If globalObj is undefined, then
|
||||||
if (global_object.is_undefined()) {
|
if (global_object == nullptr) {
|
||||||
// NOTE: Step 1 is not supported, the global object must be allocated elsewhere.
|
// NOTE: Step 1 is not supported, the global object must be allocated elsewhere.
|
||||||
VERIFY_NOT_REACHED();
|
VERIFY_NOT_REACHED();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Assert: Type(globalObj) is Object.
|
// 2. Assert: Type(globalObj) is Object.
|
||||||
VERIFY(global_object.is_object());
|
VERIFY(global_object);
|
||||||
VERIFY(is<GlobalObject>(global_object.as_object()));
|
|
||||||
|
|
||||||
// Non-standard
|
// Non-standard
|
||||||
verify_cast<GlobalObject>(global_object.as_object()).set_associated_realm(*this);
|
global_object->set_associated_realm(*this);
|
||||||
|
|
||||||
// 3. If thisValue is undefined, set thisValue to globalObj.
|
// 3. If thisValue is undefined, set thisValue to globalObj.
|
||||||
if (this_value.is_undefined())
|
if (this_value == nullptr)
|
||||||
this_value = global_object;
|
this_value = global_object;
|
||||||
|
|
||||||
// Non-standard
|
// Non-standard
|
||||||
VERIFY(this_value.is_object());
|
VERIFY(this_value);
|
||||||
|
|
||||||
// 4. Set realmRec.[[GlobalObject]] to globalObj.
|
// 4. Set realmRec.[[GlobalObject]] to globalObj.
|
||||||
m_global_object = &verify_cast<GlobalObject>(global_object.as_object());
|
m_global_object = global_object;
|
||||||
|
|
||||||
// 5. Let newGlobalEnv be NewGlobalEnvironment(globalObj, thisValue).
|
// 5. Let newGlobalEnv be NewGlobalEnvironment(globalObj, thisValue).
|
||||||
// 6. Set realmRec.[[GlobalEnv]] to newGlobalEnv.
|
// 6. Set realmRec.[[GlobalEnv]] to newGlobalEnv.
|
||||||
m_global_environment = m_global_object->heap().allocate_without_global_object<GlobalEnvironment>(verify_cast<GlobalObject>(global_object.as_object()), this_value.as_object());
|
m_global_environment = m_global_object->heap().allocate_without_global_object<GlobalEnvironment>(*global_object, *this_value);
|
||||||
|
|
||||||
// 7. Return unused.
|
// 7. Return unused.
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,9 +26,9 @@ public:
|
||||||
Realm() = default;
|
Realm() = default;
|
||||||
|
|
||||||
static Realm* create(VM&);
|
static Realm* create(VM&);
|
||||||
static ThrowCompletionOr<NonnullOwnPtr<ExecutionContext>> initialize_host_defined_realm(VM&, Function<Value(Realm&)> create_global_object, Function<Value(Realm&)> create_global_this_value);
|
static ThrowCompletionOr<NonnullOwnPtr<ExecutionContext>> initialize_host_defined_realm(VM&, Function<GlobalObject*(Realm&)> create_global_object, Function<GlobalObject*(Realm&)> create_global_this_value);
|
||||||
|
|
||||||
void set_global_object(Value global_object, Value this_value);
|
void set_global_object(GlobalObject* global_object, GlobalObject* this_value);
|
||||||
|
|
||||||
[[nodiscard]] GlobalObject& global_object() const { return *m_global_object; }
|
[[nodiscard]] GlobalObject& global_object() const { return *m_global_object; }
|
||||||
[[nodiscard]] GlobalEnvironment& global_environment() const { return *m_global_environment; }
|
[[nodiscard]] GlobalEnvironment& global_environment() const { return *m_global_environment; }
|
||||||
|
|
|
@ -64,7 +64,7 @@ ThrowCompletionOr<Object*> ShadowRealmConstructor::construct(FunctionObject& new
|
||||||
|
|
||||||
// 10. Perform ? SetRealmGlobalObject(realmRec, undefined, undefined).
|
// 10. Perform ? SetRealmGlobalObject(realmRec, undefined, undefined).
|
||||||
auto* new_global_object = vm.heap().allocate_without_global_object<GlobalObject>(*realm);
|
auto* new_global_object = vm.heap().allocate_without_global_object<GlobalObject>(*realm);
|
||||||
realm->set_global_object(new_global_object, js_undefined());
|
realm->set_global_object(new_global_object, nullptr);
|
||||||
new_global_object->initialize_global_object();
|
new_global_object->initialize_global_object();
|
||||||
|
|
||||||
// TODO: I don't think we should have these exactly like this, that doesn't work well with how
|
// TODO: I don't think we should have these exactly like this, that doesn't work well with how
|
||||||
|
|
|
@ -367,7 +367,7 @@ void queue_mutation_observer_microtask(DOM::Document& document)
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://html.spec.whatwg.org/multipage/webappapis.html#creating-a-new-javascript-realm
|
// https://html.spec.whatwg.org/multipage/webappapis.html#creating-a-new-javascript-realm
|
||||||
NonnullOwnPtr<JS::ExecutionContext> create_a_new_javascript_realm(JS::VM& vm, Function<JS::Value(JS::Realm&)> create_global_object, Function<JS::Value(JS::Realm&)> create_global_this_value)
|
NonnullOwnPtr<JS::ExecutionContext> create_a_new_javascript_realm(JS::VM& vm, Function<JS::GlobalObject*(JS::Realm&)> create_global_object, Function<JS::GlobalObject*(JS::Realm&)> create_global_this_value)
|
||||||
{
|
{
|
||||||
// 1. Perform InitializeHostDefinedRealm() with the provided customizations for creating the global object and the global this binding.
|
// 1. Perform InitializeHostDefinedRealm() with the provided customizations for creating the global object and the global this binding.
|
||||||
// 2. Let realm execution context be the running JavaScript execution context.
|
// 2. Let realm execution context be the running JavaScript execution context.
|
||||||
|
|
|
@ -49,6 +49,6 @@ struct WebEngineCustomJobCallbackData final : public JS::JobCallback::CustomData
|
||||||
HTML::ClassicScript* active_script();
|
HTML::ClassicScript* active_script();
|
||||||
JS::VM& main_thread_vm();
|
JS::VM& main_thread_vm();
|
||||||
void queue_mutation_observer_microtask(DOM::Document&);
|
void queue_mutation_observer_microtask(DOM::Document&);
|
||||||
NonnullOwnPtr<JS::ExecutionContext> create_a_new_javascript_realm(JS::VM&, Function<JS::Value(JS::Realm&)> create_global_object, Function<JS::Value(JS::Realm&)> create_global_this_value);
|
NonnullOwnPtr<JS::ExecutionContext> create_a_new_javascript_realm(JS::VM&, Function<JS::GlobalObject*(JS::Realm&)> create_global_object, Function<JS::GlobalObject*(JS::Realm&)> create_global_this_value);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -168,16 +168,16 @@ NonnullRefPtr<Document> Document::create_and_initialize(Type type, String conten
|
||||||
// 5. Let realm execution context be the result of creating a new JavaScript realm given agent and the following customizations:
|
// 5. Let realm execution context be the result of creating a new JavaScript realm given agent and the following customizations:
|
||||||
auto realm_execution_context = Bindings::create_a_new_javascript_realm(
|
auto realm_execution_context = Bindings::create_a_new_javascript_realm(
|
||||||
Bindings::main_thread_vm(),
|
Bindings::main_thread_vm(),
|
||||||
[&](JS::Realm& realm) -> JS::Value {
|
[&](JS::Realm& realm) -> JS::GlobalObject* {
|
||||||
// - For the global object, create a new Window object.
|
// - For the global object, create a new Window object.
|
||||||
window = HTML::Window::create();
|
window = HTML::Window::create();
|
||||||
auto* global_object = realm.heap().allocate_without_global_object<Bindings::WindowObject>(realm, *window);
|
auto* global_object = realm.heap().allocate_without_global_object<Bindings::WindowObject>(realm, *window);
|
||||||
VERIFY(window->wrapper() == global_object);
|
VERIFY(window->wrapper() == global_object);
|
||||||
return global_object;
|
return global_object;
|
||||||
},
|
},
|
||||||
[](JS::Realm&) -> JS::Value {
|
[](JS::Realm&) -> JS::GlobalObject* {
|
||||||
// FIXME: - For the global this binding, use browsingContext's WindowProxy object.
|
// FIXME: - For the global this binding, use browsingContext's WindowProxy object.
|
||||||
return JS::js_undefined();
|
return nullptr;
|
||||||
});
|
});
|
||||||
|
|
||||||
// 6. Let topLevelCreationURL be creationURL.
|
// 6. Let topLevelCreationURL be creationURL.
|
||||||
|
|
|
@ -121,16 +121,16 @@ NonnullRefPtr<BrowsingContext> BrowsingContext::create_a_new_browsing_context(Pa
|
||||||
// 8. Let realm execution context be the result of creating a new JavaScript realm given agent and the following customizations:
|
// 8. Let realm execution context be the result of creating a new JavaScript realm given agent and the following customizations:
|
||||||
auto realm_execution_context = Bindings::create_a_new_javascript_realm(
|
auto realm_execution_context = Bindings::create_a_new_javascript_realm(
|
||||||
Bindings::main_thread_vm(),
|
Bindings::main_thread_vm(),
|
||||||
[&](JS::Realm& realm) -> JS::Value {
|
[&](JS::Realm& realm) -> JS::GlobalObject* {
|
||||||
// - For the global object, create a new Window object.
|
// - For the global object, create a new Window object.
|
||||||
window = HTML::Window::create();
|
window = HTML::Window::create();
|
||||||
auto* global_object = realm.heap().allocate_without_global_object<Bindings::WindowObject>(realm, *window);
|
auto* global_object = realm.heap().allocate_without_global_object<Bindings::WindowObject>(realm, *window);
|
||||||
VERIFY(window->wrapper() == global_object);
|
VERIFY(window->wrapper() == global_object);
|
||||||
return global_object;
|
return global_object;
|
||||||
},
|
},
|
||||||
[](JS::Realm&) -> JS::Value {
|
[](JS::Realm&) -> JS::GlobalObject* {
|
||||||
// FIXME: - For the global this binding, use browsingContext's WindowProxy object.
|
// FIXME: - For the global this binding, use browsingContext's WindowProxy object.
|
||||||
return JS::js_undefined();
|
return nullptr;
|
||||||
});
|
});
|
||||||
|
|
||||||
// 9. Let topLevelCreationURL be about:blank if embedder is null; otherwise embedder's relevant settings object's top-level creation URL.
|
// 9. Let topLevelCreationURL be about:blank if embedder is null; otherwise embedder's relevant settings object's top-level creation URL.
|
||||||
|
|
|
@ -102,7 +102,7 @@ void Worker::run_a_worker(AK::URL& url, EnvironmentSettingsObject& outside_setti
|
||||||
// 7. Let realm execution context be the result of creating a new JavaScript realm given agent and the following customizations:
|
// 7. Let realm execution context be the result of creating a new JavaScript realm given agent and the following customizations:
|
||||||
auto realm_execution_context = Bindings::create_a_new_javascript_realm(
|
auto realm_execution_context = Bindings::create_a_new_javascript_realm(
|
||||||
*m_worker_vm,
|
*m_worker_vm,
|
||||||
[&](JS::Realm& realm) -> JS::Value {
|
[&](JS::Realm& realm) -> JS::GlobalObject* {
|
||||||
// 7a. For the global object, if is shared is true, create a new SharedWorkerGlobalScope object.
|
// 7a. For the global object, if is shared is true, create a new SharedWorkerGlobalScope object.
|
||||||
// 7b. Otherwise, create a new DedicatedWorkerGlobalScope object.
|
// 7b. Otherwise, create a new DedicatedWorkerGlobalScope object.
|
||||||
// FIXME: Proper support for both SharedWorkerGlobalScope and DedicatedWorkerGlobalScope
|
// FIXME: Proper support for both SharedWorkerGlobalScope and DedicatedWorkerGlobalScope
|
||||||
|
@ -113,9 +113,7 @@ void Worker::run_a_worker(AK::URL& url, EnvironmentSettingsObject& outside_setti
|
||||||
m_worker_scope = m_worker_vm->heap().allocate_without_global_object<JS::GlobalObject>(realm);
|
m_worker_scope = m_worker_vm->heap().allocate_without_global_object<JS::GlobalObject>(realm);
|
||||||
return m_worker_scope;
|
return m_worker_scope;
|
||||||
},
|
},
|
||||||
[&](JS::Realm&) -> JS::Value {
|
nullptr);
|
||||||
return JS::js_undefined();
|
|
||||||
});
|
|
||||||
m_worker_realm = realm_execution_context->realm;
|
m_worker_realm = realm_execution_context->realm;
|
||||||
|
|
||||||
m_console = adopt_ref(*new WorkerDebugConsoleClient(m_worker_scope->console()));
|
m_console = adopt_ref(*new WorkerDebugConsoleClient(m_worker_scope->console()));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue