From affc479e83c66270812876f1537b36c324828a64 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 8 Jun 2020 12:25:45 +0200 Subject: [PATCH] LibJS+LibWeb: Remove a bunch of calls to Interpreter::global_object() Objects should get the GlobalObject from themselves instead. However, it's not yet available during construction so this only switches code that happens after construction. To support multiple global objects, Interpreter needs to stop holding on to "the" global object and let each object graph own their global. --- Libraries/LibJS/Runtime/BooleanConstructor.cpp | 2 +- Libraries/LibJS/Runtime/DateConstructor.cpp | 4 ++-- Libraries/LibJS/Runtime/ErrorConstructor.cpp | 2 +- Libraries/LibJS/Runtime/Function.cpp | 2 +- Libraries/LibJS/Runtime/NumberConstructor.cpp | 2 +- Libraries/LibJS/Runtime/Object.cpp | 15 ++++++++------- Libraries/LibJS/Runtime/ObjectConstructor.cpp | 2 +- Libraries/LibJS/Runtime/ProxyConstructor.cpp | 2 +- Libraries/LibJS/Runtime/RegExpConstructor.cpp | 4 ++-- Libraries/LibJS/Runtime/ScriptFunction.cpp | 2 +- Libraries/LibJS/Runtime/StringConstructor.cpp | 2 +- .../LibWeb/Bindings/XMLHttpRequestConstructor.cpp | 2 +- 12 files changed, 21 insertions(+), 20 deletions(-) diff --git a/Libraries/LibJS/Runtime/BooleanConstructor.cpp b/Libraries/LibJS/Runtime/BooleanConstructor.cpp index 64910080c1..e7b5eb8d7f 100644 --- a/Libraries/LibJS/Runtime/BooleanConstructor.cpp +++ b/Libraries/LibJS/Runtime/BooleanConstructor.cpp @@ -51,7 +51,7 @@ Value BooleanConstructor::call(Interpreter& interpreter) Value BooleanConstructor::construct(Interpreter& interpreter) { - return BooleanObject::create(interpreter.global_object(), interpreter.argument(0).to_boolean()); + return BooleanObject::create(global_object(), interpreter.argument(0).to_boolean()); } } diff --git a/Libraries/LibJS/Runtime/DateConstructor.cpp b/Libraries/LibJS/Runtime/DateConstructor.cpp index cff912c2fa..a76db80b5e 100644 --- a/Libraries/LibJS/Runtime/DateConstructor.cpp +++ b/Libraries/LibJS/Runtime/DateConstructor.cpp @@ -55,14 +55,14 @@ Value DateConstructor::call(Interpreter& interpreter) return js_string(interpreter, static_cast(date.as_object()).string()); } -Value DateConstructor::construct(Interpreter& interpreter) +Value DateConstructor::construct(Interpreter&) { // TODO: Support args struct timeval tv; gettimeofday(&tv, nullptr); auto datetime = Core::DateTime::now(); auto milliseconds = static_cast(tv.tv_usec / 1000); - return Date::create(interpreter.global_object(), datetime, milliseconds); + return Date::create(global_object(), datetime, milliseconds); } Value DateConstructor::now(Interpreter&) diff --git a/Libraries/LibJS/Runtime/ErrorConstructor.cpp b/Libraries/LibJS/Runtime/ErrorConstructor.cpp index 91d96b9bf1..f308f64a2a 100644 --- a/Libraries/LibJS/Runtime/ErrorConstructor.cpp +++ b/Libraries/LibJS/Runtime/ErrorConstructor.cpp @@ -55,7 +55,7 @@ Value ErrorConstructor::construct(Interpreter& interpreter) if (interpreter.exception()) return {}; } - return Error::create(interpreter.global_object(), "Error", message); + return Error::create(global_object(), "Error", message); } #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \ diff --git a/Libraries/LibJS/Runtime/Function.cpp b/Libraries/LibJS/Runtime/Function.cpp index c7c1df8c17..c6329cf2c3 100644 --- a/Libraries/LibJS/Runtime/Function.cpp +++ b/Libraries/LibJS/Runtime/Function.cpp @@ -59,7 +59,7 @@ BoundFunction* Function::bind(Value bound_this_value, Vector arguments) case Value::Type::Null: if (interpreter().in_strict_mode()) return bound_this_value; - return &interpreter().global_object(); + return &global_object(); default: return bound_this_value.to_object(interpreter()); } diff --git a/Libraries/LibJS/Runtime/NumberConstructor.cpp b/Libraries/LibJS/Runtime/NumberConstructor.cpp index a37c913bbf..0733b9c59a 100644 --- a/Libraries/LibJS/Runtime/NumberConstructor.cpp +++ b/Libraries/LibJS/Runtime/NumberConstructor.cpp @@ -75,7 +75,7 @@ Value NumberConstructor::construct(Interpreter& interpreter) if (interpreter.exception()) return {}; } - return NumberObject::create(interpreter.global_object(), number); + return NumberObject::create(global_object(), number); } Value NumberConstructor::is_finite(Interpreter& interpreter) diff --git a/Libraries/LibJS/Runtime/Object.cpp b/Libraries/LibJS/Runtime/Object.cpp index 866f86b65a..dff49c1706 100644 --- a/Libraries/LibJS/Runtime/Object.cpp +++ b/Libraries/LibJS/Runtime/Object.cpp @@ -91,7 +91,8 @@ Object::Object(Object* prototype) m_shape = interpreter().global_object().empty_object_shape(); set_prototype(prototype); } else { - m_shape = interpreter().heap().allocate(interpreter().global_object()); + // This is the global object + m_shape = interpreter().heap().allocate(static_cast(*this)); } } @@ -167,7 +168,7 @@ Value Object::get_own_property(const Object& this_object, PropertyName property_ Value Object::get_own_properties(const Object& this_object, GetOwnPropertyMode kind, bool only_enumerable_properties) const { - auto* properties_array = Array::create(interpreter().global_object()); + auto* properties_array = Array::create(global_object()); // FIXME: Support generic iterables if (this_object.is_string_object()) { @@ -179,7 +180,7 @@ Value Object::get_own_properties(const Object& this_object, GetOwnPropertyMode k } else if (kind == GetOwnPropertyMode::Value) { properties_array->define_property(i, js_string(interpreter(), String::format("%c", str[i]))); } else { - auto* entry_array = Array::create(interpreter().global_object()); + auto* entry_array = Array::create(global_object()); entry_array->define_property(0, js_string(interpreter(), String::number(i))); if (interpreter().exception()) return {}; @@ -206,7 +207,7 @@ Value Object::get_own_properties(const Object& this_object, GetOwnPropertyMode k } else if (kind == GetOwnPropertyMode::Value) { properties_array->define_property(property_index, value_and_attributes.value); } else { - auto* entry_array = Array::create(interpreter().global_object()); + auto* entry_array = Array::create(global_object()); entry_array->define_property(0, js_string(interpreter(), String::number(entry.index()))); if (interpreter().exception()) return {}; @@ -232,7 +233,7 @@ Value Object::get_own_properties(const Object& this_object, GetOwnPropertyMode k } else if (kind == GetOwnPropertyMode::Value) { properties_array->define_property(offset, this_object.get(it.key)); } else { - auto* entry_array = Array::create(interpreter().global_object()); + auto* entry_array = Array::create(global_object()); entry_array->define_property(0, js_string(interpreter(), it.key)); if (interpreter().exception()) return {}; @@ -294,7 +295,7 @@ Value Object::get_own_property_descriptor_object(PropertyName property_name) con return js_undefined(); auto descriptor = descriptor_opt.value(); - auto* descriptor_object = Object::create_empty(interpreter(), interpreter().global_object()); + auto* descriptor_object = Object::create_empty(interpreter(), global_object()); descriptor_object->define_property("enumerable", Value(descriptor.attributes.is_enumerable())); if (interpreter().exception()) return {}; @@ -685,7 +686,7 @@ bool Object::put(PropertyName property_name, Value value) bool Object::define_native_function(const FlyString& property_name, AK::Function native_function, i32 length, PropertyAttributes attribute) { - auto* function = NativeFunction::create(interpreter(), interpreter().global_object(), property_name, move(native_function)); + auto* function = NativeFunction::create(interpreter(), global_object(), property_name, move(native_function)); function->define_property("length", Value(length), Attribute::Configurable); if (interpreter().exception()) return {}; diff --git a/Libraries/LibJS/Runtime/ObjectConstructor.cpp b/Libraries/LibJS/Runtime/ObjectConstructor.cpp index 88ae0832ef..979b26a980 100644 --- a/Libraries/LibJS/Runtime/ObjectConstructor.cpp +++ b/Libraries/LibJS/Runtime/ObjectConstructor.cpp @@ -61,7 +61,7 @@ ObjectConstructor::~ObjectConstructor() Value ObjectConstructor::call(Interpreter& interpreter) { - return Object::create_empty(interpreter, interpreter.global_object()); + return Object::create_empty(interpreter, global_object()); } Value ObjectConstructor::construct(Interpreter& interpreter) diff --git a/Libraries/LibJS/Runtime/ProxyConstructor.cpp b/Libraries/LibJS/Runtime/ProxyConstructor.cpp index 9181f637b7..1d03959705 100644 --- a/Libraries/LibJS/Runtime/ProxyConstructor.cpp +++ b/Libraries/LibJS/Runtime/ProxyConstructor.cpp @@ -62,7 +62,7 @@ Value ProxyConstructor::construct(Interpreter& interpreter) if (!handler.is_object()) return interpreter.throw_exception(String::format("Expected handler argument of Proxy constructor to be object, got %s", handler.to_string_without_side_effects().characters())); - return ProxyObject::create(interpreter.global_object(), target.as_object(), handler.as_object()); + return ProxyObject::create(global_object(), target.as_object(), handler.as_object()); } } diff --git a/Libraries/LibJS/Runtime/RegExpConstructor.cpp b/Libraries/LibJS/Runtime/RegExpConstructor.cpp index 9a2198a3f2..78a81d8f90 100644 --- a/Libraries/LibJS/Runtime/RegExpConstructor.cpp +++ b/Libraries/LibJS/Runtime/RegExpConstructor.cpp @@ -51,14 +51,14 @@ Value RegExpConstructor::call(Interpreter& interpreter) Value RegExpConstructor::construct(Interpreter& interpreter) { if (!interpreter.argument_count()) - return RegExpObject::create(interpreter.global_object(), "(?:)", ""); + return RegExpObject::create(global_object(), "(?:)", ""); auto contents = interpreter.argument(0).to_string(interpreter); if (interpreter.exception()) return {}; auto flags = interpreter.argument_count() > 1 ? interpreter.argument(1).to_string(interpreter) : ""; if (interpreter.exception()) return {}; - return RegExpObject::create(interpreter.global_object(), contents, flags); + return RegExpObject::create(global_object(), contents, flags); } } diff --git a/Libraries/LibJS/Runtime/ScriptFunction.cpp b/Libraries/LibJS/Runtime/ScriptFunction.cpp index fddee25d2a..6a7c6c390d 100644 --- a/Libraries/LibJS/Runtime/ScriptFunction.cpp +++ b/Libraries/LibJS/Runtime/ScriptFunction.cpp @@ -104,7 +104,7 @@ Value ScriptFunction::call(Interpreter& interpreter) auto parameter = parameters()[i]; auto value = js_undefined(); if (parameter.is_rest) { - auto* array = Array::create(interpreter.global_object()); + auto* array = Array::create(global_object()); for (size_t rest_index = i; rest_index < argument_values.size(); ++rest_index) array->indexed_properties().append(argument_values[rest_index]); value = Value(array); diff --git a/Libraries/LibJS/Runtime/StringConstructor.cpp b/Libraries/LibJS/Runtime/StringConstructor.cpp index d5a633306e..f9a5d34631 100644 --- a/Libraries/LibJS/Runtime/StringConstructor.cpp +++ b/Libraries/LibJS/Runtime/StringConstructor.cpp @@ -69,7 +69,7 @@ Value StringConstructor::construct(Interpreter& interpreter) primitive_string = interpreter.argument(0).to_primitive_string(interpreter); if (!primitive_string) return {}; - return StringObject::create(interpreter.global_object(), *primitive_string); + return StringObject::create(global_object(), *primitive_string); } Value StringConstructor::raw(Interpreter& interpreter) diff --git a/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.cpp b/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.cpp index 3ad3150412..413d5b94fd 100644 --- a/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.cpp +++ b/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.cpp @@ -59,7 +59,7 @@ JS::Value XMLHttpRequestConstructor::call(JS::Interpreter& interpreter) JS::Value XMLHttpRequestConstructor::construct(JS::Interpreter& interpreter) { - auto& window = static_cast(interpreter.global_object()); + auto& window = static_cast(global_object()); return interpreter.heap().allocate(XMLHttpRequest::create(window.impl())); }