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.
This commit is contained in:
Andreas Kling 2020-06-08 12:25:45 +02:00
parent ff8bb962b6
commit affc479e83
Notes: sideshowbarker 2024-07-19 05:45:09 +09:00
12 changed files with 21 additions and 20 deletions

View file

@ -51,7 +51,7 @@ Value BooleanConstructor::call(Interpreter& interpreter)
Value BooleanConstructor::construct(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());
} }
} }

View file

@ -55,14 +55,14 @@ Value DateConstructor::call(Interpreter& interpreter)
return js_string(interpreter, static_cast<Date&>(date.as_object()).string()); return js_string(interpreter, static_cast<Date&>(date.as_object()).string());
} }
Value DateConstructor::construct(Interpreter& interpreter) Value DateConstructor::construct(Interpreter&)
{ {
// TODO: Support args // TODO: Support args
struct timeval tv; struct timeval tv;
gettimeofday(&tv, nullptr); gettimeofday(&tv, nullptr);
auto datetime = Core::DateTime::now(); auto datetime = Core::DateTime::now();
auto milliseconds = static_cast<u16>(tv.tv_usec / 1000); auto milliseconds = static_cast<u16>(tv.tv_usec / 1000);
return Date::create(interpreter.global_object(), datetime, milliseconds); return Date::create(global_object(), datetime, milliseconds);
} }
Value DateConstructor::now(Interpreter&) Value DateConstructor::now(Interpreter&)

View file

@ -55,7 +55,7 @@ Value ErrorConstructor::construct(Interpreter& interpreter)
if (interpreter.exception()) if (interpreter.exception())
return {}; return {};
} }
return Error::create(interpreter.global_object(), "Error", message); return Error::create(global_object(), "Error", message);
} }
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \ #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \

View file

@ -59,7 +59,7 @@ BoundFunction* Function::bind(Value bound_this_value, Vector<Value> arguments)
case Value::Type::Null: case Value::Type::Null:
if (interpreter().in_strict_mode()) if (interpreter().in_strict_mode())
return bound_this_value; return bound_this_value;
return &interpreter().global_object(); return &global_object();
default: default:
return bound_this_value.to_object(interpreter()); return bound_this_value.to_object(interpreter());
} }

View file

@ -75,7 +75,7 @@ Value NumberConstructor::construct(Interpreter& interpreter)
if (interpreter.exception()) if (interpreter.exception())
return {}; return {};
} }
return NumberObject::create(interpreter.global_object(), number); return NumberObject::create(global_object(), number);
} }
Value NumberConstructor::is_finite(Interpreter& interpreter) Value NumberConstructor::is_finite(Interpreter& interpreter)

View file

@ -91,7 +91,8 @@ Object::Object(Object* prototype)
m_shape = interpreter().global_object().empty_object_shape(); m_shape = interpreter().global_object().empty_object_shape();
set_prototype(prototype); set_prototype(prototype);
} else { } else {
m_shape = interpreter().heap().allocate<Shape>(interpreter().global_object()); // This is the global object
m_shape = interpreter().heap().allocate<Shape>(static_cast<GlobalObject&>(*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 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 // FIXME: Support generic iterables
if (this_object.is_string_object()) { 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) { } else if (kind == GetOwnPropertyMode::Value) {
properties_array->define_property(i, js_string(interpreter(), String::format("%c", str[i]))); properties_array->define_property(i, js_string(interpreter(), String::format("%c", str[i])));
} else { } 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))); entry_array->define_property(0, js_string(interpreter(), String::number(i)));
if (interpreter().exception()) if (interpreter().exception())
return {}; return {};
@ -206,7 +207,7 @@ Value Object::get_own_properties(const Object& this_object, GetOwnPropertyMode k
} else if (kind == GetOwnPropertyMode::Value) { } else if (kind == GetOwnPropertyMode::Value) {
properties_array->define_property(property_index, value_and_attributes.value); properties_array->define_property(property_index, value_and_attributes.value);
} else { } 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()))); entry_array->define_property(0, js_string(interpreter(), String::number(entry.index())));
if (interpreter().exception()) if (interpreter().exception())
return {}; return {};
@ -232,7 +233,7 @@ Value Object::get_own_properties(const Object& this_object, GetOwnPropertyMode k
} else if (kind == GetOwnPropertyMode::Value) { } else if (kind == GetOwnPropertyMode::Value) {
properties_array->define_property(offset, this_object.get(it.key)); properties_array->define_property(offset, this_object.get(it.key));
} else { } 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)); entry_array->define_property(0, js_string(interpreter(), it.key));
if (interpreter().exception()) if (interpreter().exception())
return {}; return {};
@ -294,7 +295,7 @@ Value Object::get_own_property_descriptor_object(PropertyName property_name) con
return js_undefined(); return js_undefined();
auto descriptor = descriptor_opt.value(); 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())); descriptor_object->define_property("enumerable", Value(descriptor.attributes.is_enumerable()));
if (interpreter().exception()) if (interpreter().exception())
return {}; return {};
@ -685,7 +686,7 @@ bool Object::put(PropertyName property_name, Value value)
bool Object::define_native_function(const FlyString& property_name, AK::Function<Value(Interpreter&)> native_function, i32 length, PropertyAttributes attribute) bool Object::define_native_function(const FlyString& property_name, AK::Function<Value(Interpreter&)> 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); function->define_property("length", Value(length), Attribute::Configurable);
if (interpreter().exception()) if (interpreter().exception())
return {}; return {};

View file

@ -61,7 +61,7 @@ ObjectConstructor::~ObjectConstructor()
Value ObjectConstructor::call(Interpreter& interpreter) 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) Value ObjectConstructor::construct(Interpreter& interpreter)

View file

@ -62,7 +62,7 @@ Value ProxyConstructor::construct(Interpreter& interpreter)
if (!handler.is_object()) if (!handler.is_object())
return interpreter.throw_exception<TypeError>(String::format("Expected handler argument of Proxy constructor to be object, got %s", handler.to_string_without_side_effects().characters())); return interpreter.throw_exception<TypeError>(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());
} }
} }

View file

@ -51,14 +51,14 @@ Value RegExpConstructor::call(Interpreter& interpreter)
Value RegExpConstructor::construct(Interpreter& interpreter) Value RegExpConstructor::construct(Interpreter& interpreter)
{ {
if (!interpreter.argument_count()) if (!interpreter.argument_count())
return RegExpObject::create(interpreter.global_object(), "(?:)", ""); return RegExpObject::create(global_object(), "(?:)", "");
auto contents = interpreter.argument(0).to_string(interpreter); auto contents = interpreter.argument(0).to_string(interpreter);
if (interpreter.exception()) if (interpreter.exception())
return {}; return {};
auto flags = interpreter.argument_count() > 1 ? interpreter.argument(1).to_string(interpreter) : ""; auto flags = interpreter.argument_count() > 1 ? interpreter.argument(1).to_string(interpreter) : "";
if (interpreter.exception()) if (interpreter.exception())
return {}; return {};
return RegExpObject::create(interpreter.global_object(), contents, flags); return RegExpObject::create(global_object(), contents, flags);
} }
} }

View file

@ -104,7 +104,7 @@ Value ScriptFunction::call(Interpreter& interpreter)
auto parameter = parameters()[i]; auto parameter = parameters()[i];
auto value = js_undefined(); auto value = js_undefined();
if (parameter.is_rest) { 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) for (size_t rest_index = i; rest_index < argument_values.size(); ++rest_index)
array->indexed_properties().append(argument_values[rest_index]); array->indexed_properties().append(argument_values[rest_index]);
value = Value(array); value = Value(array);

View file

@ -69,7 +69,7 @@ Value StringConstructor::construct(Interpreter& interpreter)
primitive_string = interpreter.argument(0).to_primitive_string(interpreter); primitive_string = interpreter.argument(0).to_primitive_string(interpreter);
if (!primitive_string) if (!primitive_string)
return {}; return {};
return StringObject::create(interpreter.global_object(), *primitive_string); return StringObject::create(global_object(), *primitive_string);
} }
Value StringConstructor::raw(Interpreter& interpreter) Value StringConstructor::raw(Interpreter& interpreter)

View file

@ -59,7 +59,7 @@ JS::Value XMLHttpRequestConstructor::call(JS::Interpreter& interpreter)
JS::Value XMLHttpRequestConstructor::construct(JS::Interpreter& interpreter) JS::Value XMLHttpRequestConstructor::construct(JS::Interpreter& interpreter)
{ {
auto& window = static_cast<WindowObject&>(interpreter.global_object()); auto& window = static_cast<WindowObject&>(global_object());
return interpreter.heap().allocate<XMLHttpRequestWrapper>(XMLHttpRequest::create(window.impl())); return interpreter.heap().allocate<XMLHttpRequestWrapper>(XMLHttpRequest::create(window.impl()));
} }