mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 13:17:34 +00:00
LibJS+LibWeb: Replace GlobalObject with Realm in create() functions
This is a continuation of the previous two commits. As allocating a JS cell already primarily involves a realm instead of a global object, and we'll need to pass one to the allocate() function itself eventually (it's bridged via the global object right now), the create() functions need to receive a realm as well. The plan is for this to be the highest-level function that actually receives a realm and passes it around, AOs on an even higher level will use the "current realm" concept via VM::current_realm() as that's what the spec assumes; passing around realms (or global objects, for that matter) on higher AO levels is pointless and unlike for allocating individual objects, which may happen outside of regular JS execution, we don't need control over the specific realm that is being used there.
This commit is contained in:
parent
5dd5896588
commit
b99cc7d050
178 changed files with 883 additions and 609 deletions
|
@ -16,6 +16,7 @@
|
|||
#include <LibWeb/Bindings/CrossOriginAbstractOperations.h>
|
||||
#include <LibWeb/Bindings/DOMExceptionWrapper.h>
|
||||
#include <LibWeb/Bindings/LocationObject.h>
|
||||
#include <LibWeb/Bindings/MainThreadVM.h>
|
||||
#include <LibWeb/Bindings/WindowObject.h>
|
||||
#include <LibWeb/DOM/DOMException.h>
|
||||
#include <LibWeb/HTML/Scripting/Environments.h>
|
||||
|
@ -93,6 +94,7 @@ bool is_platform_object_same_origin(JS::Object const& object)
|
|||
// 7.2.3.4 CrossOriginGetOwnPropertyHelper ( O, P ), https://html.spec.whatwg.org/multipage/browsers.html#crossorigingetownpropertyhelper-(-o,-p-)
|
||||
Optional<JS::PropertyDescriptor> cross_origin_get_own_property_helper(Variant<LocationObject*, WindowObject*> const& object, JS::PropertyKey const& property_key)
|
||||
{
|
||||
auto& realm = *main_thread_vm().current_realm();
|
||||
auto const* object_ptr = object.visit([](auto* o) { return static_cast<JS::Object const*>(o); });
|
||||
auto const object_const_variant = object.visit([](auto* o) { return Variant<LocationObject const*, WindowObject const*> { o }; });
|
||||
|
||||
|
@ -126,7 +128,7 @@ Optional<JS::PropertyDescriptor> cross_origin_get_own_property_helper(Variant<Lo
|
|||
// 2. If IsCallable(value) is true, then set value to an anonymous built-in function, created in the current Realm Record, that performs the same steps as the IDL operation P on object O.
|
||||
if (value->is_function()) {
|
||||
value = JS::NativeFunction::create(
|
||||
HTML::current_global_object(), [function = JS::make_handle(*value)](auto&, auto& global_object) {
|
||||
realm, [function = JS::make_handle(*value)](auto&, auto& global_object) {
|
||||
return JS::call(global_object, function.value(), JS::js_undefined());
|
||||
},
|
||||
0, "");
|
||||
|
@ -143,7 +145,7 @@ Optional<JS::PropertyDescriptor> cross_origin_get_own_property_helper(Variant<Lo
|
|||
// 2. If e.[[NeedsGet]] is true, then set crossOriginGet to an anonymous built-in function, created in the current Realm Record, that performs the same steps as the getter of the IDL attribute P on object O.
|
||||
if (*entry.needs_get) {
|
||||
cross_origin_get = JS::NativeFunction::create(
|
||||
HTML::current_global_object(), [object_ptr, getter = JS::make_handle(*original_descriptor->get)](auto&, auto& global_object) {
|
||||
realm, [object_ptr, getter = JS::make_handle(*original_descriptor->get)](auto&, auto& global_object) {
|
||||
return JS::call(global_object, getter.cell(), object_ptr);
|
||||
},
|
||||
0, "");
|
||||
|
@ -155,7 +157,7 @@ Optional<JS::PropertyDescriptor> cross_origin_get_own_property_helper(Variant<Lo
|
|||
// If e.[[NeedsSet]] is true, then set crossOriginSet to an anonymous built-in function, created in the current Realm Record, that performs the same steps as the setter of the IDL attribute P on object O.
|
||||
if (*entry.needs_set) {
|
||||
cross_origin_set = JS::NativeFunction::create(
|
||||
HTML::current_global_object(), [object_ptr, setter = JS::make_handle(*original_descriptor->set)](auto&, auto& global_object) {
|
||||
realm, [object_ptr, setter = JS::make_handle(*original_descriptor->set)](auto&, auto& global_object) {
|
||||
return JS::call(global_object, setter.cell(), object_ptr);
|
||||
},
|
||||
0, "");
|
||||
|
|
|
@ -299,7 +299,6 @@ JS::VM& main_thread_vm()
|
|||
// https://dom.spec.whatwg.org/#queue-a-mutation-observer-compound-microtask
|
||||
void queue_mutation_observer_microtask(DOM::Document& document)
|
||||
{
|
||||
// FIXME: Is this the correct VM?
|
||||
auto& vm = main_thread_vm();
|
||||
auto& custom_data = verify_cast<WebEngineCustomData>(*vm.custom_data());
|
||||
|
||||
|
@ -345,8 +344,9 @@ void queue_mutation_observer_microtask(DOM::Document& document)
|
|||
if (!records.is_empty()) {
|
||||
auto& callback = mutation_observer.callback();
|
||||
auto& global_object = callback.callback_context.global_object();
|
||||
auto& realm = callback.callback_context.realm();
|
||||
|
||||
auto* wrapped_records = MUST(JS::Array::create(global_object, 0));
|
||||
auto* wrapped_records = MUST(JS::Array::create(realm, 0));
|
||||
for (size_t i = 0; i < records.size(); ++i) {
|
||||
auto& record = records.at(i);
|
||||
auto* wrapped_record = Bindings::wrap(global_object, record);
|
||||
|
|
|
@ -21,7 +21,7 @@ NavigatorObject::NavigatorObject(JS::Realm& realm)
|
|||
void NavigatorObject::initialize(JS::Realm& realm)
|
||||
{
|
||||
auto& heap = this->heap();
|
||||
auto* languages = MUST(JS::Array::create(realm.global_object(), 0));
|
||||
auto* languages = MUST(JS::Array::create(realm, 0));
|
||||
languages->indexed_properties().append(js_string(heap, "en-US"));
|
||||
|
||||
// FIXME: All of these should be in Navigator's prototype and be native accessors
|
||||
|
|
|
@ -618,6 +618,8 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::scroll)
|
|||
// https://www.w3.org/TR/cssom-view/#dom-window-scrollby
|
||||
JS_DEFINE_NATIVE_FUNCTION(WindowObject::scroll_by)
|
||||
{
|
||||
auto& realm = *global_object.associated_realm();
|
||||
|
||||
auto* impl = TRY(impl_from(vm, global_object));
|
||||
if (!impl->page())
|
||||
return JS::js_undefined();
|
||||
|
@ -626,12 +628,12 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::scroll_by)
|
|||
JS::Object* options = nullptr;
|
||||
|
||||
if (vm.argument_count() == 0) {
|
||||
options = JS::Object::create(global_object, nullptr);
|
||||
options = JS::Object::create(realm, nullptr);
|
||||
} else if (vm.argument_count() == 1) {
|
||||
options = TRY(vm.argument(0).to_object(global_object));
|
||||
} else if (vm.argument_count() >= 2) {
|
||||
// We ignore arguments 2+ in line with behavior of Chrome and Firefox
|
||||
options = JS::Object::create(global_object, nullptr);
|
||||
options = JS::Object::create(realm, nullptr);
|
||||
MUST(options->set("left", vm.argument(0), ShouldThrowExceptions::No));
|
||||
MUST(options->set("top", vm.argument(1), ShouldThrowExceptions::No));
|
||||
MUST(options->set("behavior", JS::js_string(vm, "auto"), ShouldThrowExceptions::No));
|
||||
|
|
|
@ -30,7 +30,7 @@ inline Wrapper* wrap_impl(JS::GlobalObject& global_object, NativeObject& native_
|
|||
{
|
||||
auto& realm = *global_object.associated_realm();
|
||||
if (!native_object.wrapper()) {
|
||||
native_object.set_wrapper(*global_object.heap().allocate<typename NativeObject::WrapperType>(global_object, realm, native_object));
|
||||
native_object.set_wrapper(*realm.heap().allocate<typename NativeObject::WrapperType>(realm.global_object(), realm, native_object));
|
||||
}
|
||||
return native_object.wrapper();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue