1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 13:17:34 +00:00

LibJS+LibWeb: Reduce use of GlobalObject as an intermediary

- Prefer VM::current_realm() over GlobalObject::associated_realm()
- Prefer VM::heap() over GlobalObject::heap()
- Prefer Cell::vm() over Cell::global_object()
- Prefer Wrapper::vm() over Wrapper::global_object()
- Inline Realm::global_object() calls used to access intrinsics as they
  will later perform a direct lookup without going through the global
  object
This commit is contained in:
Linus Groh 2022-08-22 19:00:49 +01:00
parent e3895e6c80
commit b345a0acca
58 changed files with 157 additions and 231 deletions

View file

@ -95,9 +95,7 @@ JS_DEFINE_NATIVE_FUNCTION(LocationObject::href_getter)
// https://html.spec.whatwg.org/multipage/history.html#the-location-interface:dom-location-href-2
JS_DEFINE_NATIVE_FUNCTION(LocationObject::href_setter)
{
auto& realm = *vm.current_realm();
auto& global_object = realm.global_object();
auto& window = static_cast<WindowObject&>(global_object);
auto& window = static_cast<WindowObject&>(HTML::current_global_object());
// FIXME: 1. If this's relevant Document is null, then return.
@ -220,9 +218,7 @@ JS_DEFINE_NATIVE_FUNCTION(LocationObject::port_getter)
// https://html.spec.whatwg.org/multipage/history.html#dom-location-reload
JS_DEFINE_NATIVE_FUNCTION(LocationObject::reload)
{
auto& realm = *vm.current_realm();
auto& global_object = realm.global_object();
auto& window = static_cast<WindowObject&>(global_object);
auto& window = static_cast<WindowObject&>(HTML::current_global_object());
window.impl().did_call_location_reload({});
return JS::js_undefined();
}
@ -230,9 +226,7 @@ JS_DEFINE_NATIVE_FUNCTION(LocationObject::reload)
// https://html.spec.whatwg.org/multipage/history.html#dom-location-replace
JS_DEFINE_NATIVE_FUNCTION(LocationObject::replace)
{
auto& realm = *vm.current_realm();
auto& global_object = realm.global_object();
auto& window = static_cast<WindowObject&>(global_object);
auto& window = static_cast<WindowObject&>(HTML::current_global_object());
auto url = TRY(vm.argument(0).to_string(vm));
// FIXME: This needs spec compliance work.
window.impl().did_call_location_replace({}, move(url));
@ -306,8 +300,7 @@ JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> LocationObject::internal
// 7.10.5.6 [[DefineOwnProperty]] ( P, Desc ), https://html.spec.whatwg.org/multipage/history.html#location-defineownproperty
JS::ThrowCompletionOr<bool> LocationObject::internal_define_own_property(JS::PropertyKey const& property_key, JS::PropertyDescriptor const& descriptor)
{
auto& global_object = HTML::current_global_object();
auto& vm = global_object.vm();
auto& vm = this->vm();
// 1. If IsPlatformObjectSameOrigin(this) is true, then:
if (is_platform_object_same_origin(*this)) {
@ -349,8 +342,7 @@ JS::ThrowCompletionOr<bool> LocationObject::internal_set(JS::PropertyKey const&
// 7.10.5.9 [[Delete]] ( P ), https://html.spec.whatwg.org/multipage/history.html#location-delete
JS::ThrowCompletionOr<bool> LocationObject::internal_delete(JS::PropertyKey const& property_key)
{
auto& global_object = HTML::current_global_object();
auto& vm = global_object.vm();
auto& vm = this->vm();
// 1. If IsPlatformObjectSameOrigin(this) is true, then return ? OrdinaryDelete(this, P).
if (is_platform_object_same_origin(*this))

View file

@ -128,8 +128,7 @@ JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> WindowProxy::internal_ge
// 7.4.6 [[DefineOwnProperty]] ( P, Desc ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-defineownproperty
JS::ThrowCompletionOr<bool> WindowProxy::internal_define_own_property(JS::PropertyKey const& property_key, JS::PropertyDescriptor const& descriptor)
{
auto& global_object = HTML::current_global_object();
auto& vm = global_object.vm();
auto& vm = this->vm();
// 1. Let W be the value of the [[Window]] internal slot of this.
@ -196,8 +195,7 @@ JS::ThrowCompletionOr<bool> WindowProxy::internal_set(JS::PropertyKey const& pro
// 7.4.9 [[Delete]] ( P ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-delete
JS::ThrowCompletionOr<bool> WindowProxy::internal_delete(JS::PropertyKey const& property_key)
{
auto& global_object = HTML::current_global_object();
auto& vm = global_object.vm();
auto& vm = this->vm();
// 1. Let W be the value of the [[Window]] internal slot of this.

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -14,8 +14,8 @@ namespace Web::Encoding {
// https://encoding.spec.whatwg.org/#dom-textencoder-encode
JS::Uint8Array* TextEncoder::encode(String const& input) const
{
auto& global_object = wrapper()->global_object();
auto& realm = *global_object.associated_realm();
auto& vm = wrapper()->vm();
auto& realm = *vm.current_realm();
// NOTE: The AK::String returned from PrimitiveString::string() is always UTF-8, regardless of the internal string type, so most of these steps are no-ops.

View file

@ -158,8 +158,7 @@ DOM::ExceptionOr<void> Headers::set(String const& name_string, String const& val
// https://webidl.spec.whatwg.org/#es-iterable, Step 4
JS::ThrowCompletionOr<void> Headers::for_each(ForEachCallback callback)
{
auto& global_object = wrapper()->global_object();
auto& vm = global_object.vm();
auto& vm = wrapper()->vm();
// The value pairs to iterate over are the return value of running sort and combine with thiss header list.
auto value_pairs_to_iterate_over = [&]() -> JS::ThrowCompletionOr<Vector<Fetch::Infrastructure::Header>> {

View file

@ -15,9 +15,8 @@ namespace Web::Fetch {
// https://webidl.spec.whatwg.org/#es-iterable, Step 2
JS::ThrowCompletionOr<JS::Object*> HeadersIterator::next()
{
auto& global_object = wrapper()->global_object();
auto& vm = global_object.vm();
auto& realm = *global_object.associated_realm();
auto& vm = wrapper()->vm();
auto& realm = *vm.current_realm();
// The value pairs to iterate over are the return value of running sort and combine with thiss header list.
auto value_pairs_to_iterate_over = [&]() -> JS::ThrowCompletionOr<Vector<Fetch::Infrastructure::Header>> {

View file

@ -221,8 +221,8 @@ DOM::ExceptionOr<NonnullRefPtr<Blob>> Blob::slice(Optional<i64> start, Optional<
// https://w3c.github.io/FileAPI/#dom-blob-text
JS::Promise* Blob::text()
{
auto& global_object = wrapper()->global_object();
auto& realm = *global_object.associated_realm();
auto& vm = wrapper()->vm();
auto& realm = *vm.current_realm();
// FIXME: 1. Let stream be the result of calling get stream on this.
// FIXME: 2. Let reader be the result of getting a reader from stream. If that threw an exception, return a new promise rejected with that exception.
@ -230,7 +230,7 @@ JS::Promise* Blob::text()
// FIXME: We still need to implement ReadableStream for this step to be fully valid.
// 3. Let promise be the result of reading all bytes from stream with reader
auto* promise = JS::Promise::create(realm);
auto* result = JS::js_string(global_object.heap(), String { m_byte_buffer.bytes() });
auto* result = JS::js_string(vm, String { m_byte_buffer.bytes() });
// 4. Return the result of transforming promise by a fulfillment handler that returns the result of running UTF-8 decode on its first argument.
promise->fulfill(result);
@ -240,8 +240,8 @@ JS::Promise* Blob::text()
// https://w3c.github.io/FileAPI/#dom-blob-arraybuffer
JS::Promise* Blob::array_buffer()
{
auto& global_object = wrapper()->global_object();
auto& realm = *global_object.associated_realm();
auto& vm = wrapper()->vm();
auto& realm = *vm.current_realm();
// FIXME: 1. Let stream be the result of calling get stream on this.
// FIXME: 2. Let reader be the result of getting a reader from stream. If that threw an exception, return a new promise rejected with that exception.

View file

@ -71,8 +71,7 @@ NonnullRefPtr<ClassicScript> ClassicScript::create(String filename, StringView s
// https://html.spec.whatwg.org/multipage/webappapis.html#run-a-classic-script
JS::Completion ClassicScript::run(RethrowErrors rethrow_errors)
{
auto& global_object = settings_object().global_object();
auto& vm = global_object.vm();
auto& vm = settings_object().realm().vm();
// 1. Let settings be the settings object of script.
auto& settings = settings_object();

View file

@ -324,7 +324,7 @@ JS::GlobalObject& current_global_object()
JS::Realm& relevant_realm(JS::Object const& object)
{
// The relevant Realm for a platform object is the value of its [[Realm]] field.
return *object.global_object().associated_realm();
return object.shape().realm();
}
// https://html.spec.whatwg.org/multipage/webappapis.html#relevant-settings-object

View file

@ -14,9 +14,8 @@ namespace Web::URL {
JS::Object* URLSearchParamsIterator::next()
{
auto& global_object = wrapper()->global_object();
auto& vm = global_object.vm();
auto& realm = *global_object.associated_realm();
auto& vm = wrapper()->vm();
auto& realm = *vm.current_realm();
if (m_index >= m_url_search_params.m_list.size())
return create_iterator_result_object(vm, JS::js_undefined(), true);

View file

@ -27,8 +27,7 @@ JS::ThrowCompletionOr<JS::Value> WebAssemblyMemoryConstructor::call()
JS::ThrowCompletionOr<JS::Object*> WebAssemblyMemoryConstructor::construct(FunctionObject&)
{
auto& vm = this->vm();
auto& global_object = this->global_object();
auto& realm = *global_object.associated_realm();
auto& realm = *vm.current_realm();
auto descriptor = TRY(vm.argument(0).to_object(vm));
auto initial_value = TRY(descriptor->get("initial"));

View file

@ -29,8 +29,7 @@ JS::ThrowCompletionOr<JS::Value> WebAssemblyTableConstructor::call()
JS::ThrowCompletionOr<JS::Object*> WebAssemblyTableConstructor::construct(FunctionObject&)
{
auto& vm = this->vm();
auto& global_object = this->global_object();
auto& realm = *global_object.associated_realm();
auto& realm = *vm.current_realm();
auto descriptor = TRY(vm.argument(0).to_object(vm));
auto element_value = TRY(descriptor->get("element"));

View file

@ -213,8 +213,8 @@ void WebSocket::on_message(ByteBuffer message, bool is_text)
TODO();
} else if (m_binary_type == "arraybuffer") {
// type indicates that the data is Binary and binaryType is "arraybuffer"
auto& global_object = wrapper()->global_object();
auto& realm = *global_object.associated_realm();
auto& vm = wrapper()->vm();
auto& realm = *vm.current_realm();
HTML::MessageEventInit event_init;
event_init.data = JS::ArrayBuffer::create(realm, message);
event_init.origin = url();

View file

@ -82,7 +82,6 @@ DOM::ExceptionOr<String> XMLHttpRequest::response_text() const
// https://xhr.spec.whatwg.org/#response
DOM::ExceptionOr<JS::Value> XMLHttpRequest::response()
{
auto& global_object = wrapper()->global_object();
auto& vm = wrapper()->vm();
auto& realm = *vm.current_realm();
@ -144,7 +143,7 @@ DOM::ExceptionOr<JS::Value> XMLHttpRequest::response()
// 3. Let jsonObject be the result of running parse JSON from bytes on thiss received bytes. If that threw an exception, then return null.
TextCodec::UTF8Decoder decoder;
auto json_object_result = JS::call(vm, global_object.json_parse_function(), JS::js_undefined(), JS::js_string(global_object.heap(), decoder.to_utf8({ m_received_bytes.data(), m_received_bytes.size() })));
auto json_object_result = JS::call(vm, realm.global_object().json_parse_function(), JS::js_undefined(), JS::js_string(vm, decoder.to_utf8({ m_received_bytes.data(), m_received_bytes.size() })));
if (json_object_result.is_error())
return JS::Value(JS::js_null());
@ -623,8 +622,7 @@ DOM::ExceptionOr<void> XMLHttpRequest::set_timeout(u32 timeout)
{
// 1. If the current global object is a Window object and thiss synchronous flag is set,
// then throw an "InvalidAccessError" DOMException.
auto& global_object = wrapper()->global_object();
if (global_object.class_name() == "WindowObject" && m_synchronous)
if (is<Bindings::WindowObject>(HTML::current_global_object()) && m_synchronous)
return DOM::InvalidAccessError::create("Use of XMLHttpRequest's timeout attribute is not supported in the synchronous mode in window context.");
// 2. Set thiss timeout to the given value.