From 1c1354db07cbc371475fb8a1d1adfe6ead89b189 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sun, 25 Jul 2021 19:37:42 +0100 Subject: [PATCH] LibJS: Store the %Array.prototype.values% intrinsic on the global object Also group the getter functions for all the additional intrinsics (not generated via macros), and initialize the members. --- .../Libraries/LibJS/Runtime/AbstractOperations.cpp | 10 ++-------- Userland/Libraries/LibJS/Runtime/GlobalObject.cpp | 14 ++++++++------ Userland/Libraries/LibJS/Runtime/GlobalObject.h | 9 +++++---- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp index b792abd799..4d384eb276 100644 --- a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp @@ -435,10 +435,7 @@ Object* create_unmapped_arguments_object(GlobalObject& global_object, Vectorget(vm.names.values); - if (vm.exception()) - return {}; + auto* array_prototype_values = global_object.array_prototype_values_function(); object->define_property_or_throw(*vm.well_known_symbol_iterator(), { .value = array_prototype_values, .writable = true, .enumerable = false, .configurable = true }); VERIFY(!vm.exception()); @@ -530,10 +527,7 @@ Object* create_mapped_arguments_object(GlobalObject& global_object, FunctionObje } // 20. Perform ! DefinePropertyOrThrow(obj, @@iterator, PropertyDescriptor { [[Value]]: %Array.prototype.values%, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: true }). - // FIXME: This is not guaranteed to be %Array.prototype.values%! - auto array_prototype_values = global_object.array_prototype()->get(vm.names.values); - if (vm.exception()) - return {}; + auto* array_prototype_values = global_object.array_prototype_values_function(); object->define_property_or_throw(*vm.well_known_symbol_iterator(), { .value = array_prototype_values, .writable = true, .enumerable = false, .configurable = true }); VERIFY(!vm.exception()); diff --git a/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp b/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp index c669a45a51..9db3c782cd 100644 --- a/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp @@ -166,7 +166,6 @@ void GlobalObject::initialize_global_object() define_native_function(vm.names.parseFloat, parse_float, 1, attr); define_native_function(vm.names.parseInt, parse_int, 2, attr); define_native_function(vm.names.eval, eval, 1, attr); - m_eval_function = &get_without_side_effects(vm.names.eval).as_function(); // 10.2.4.1 %ThrowTypeError% ( ), https://tc39.es/ecma262/#sec-%throwtypeerror% m_throw_type_error_function = NativeFunction::create(global_object(), {}, [](VM& vm, GlobalObject& global_object) { @@ -178,8 +177,8 @@ void GlobalObject::initialize_global_object() m_throw_type_error_function->internal_prevent_extensions(); // 10.2.4 AddRestrictedFunctionProperties ( F, realm ), https://tc39.es/ecma262/#sec-addrestrictedfunctionproperties - m_function_prototype->define_direct_accessor(vm.names.caller, throw_type_error_function(), throw_type_error_function(), Attribute::Configurable); - m_function_prototype->define_direct_accessor(vm.names.arguments, throw_type_error_function(), throw_type_error_function(), Attribute::Configurable); + m_function_prototype->define_direct_accessor(vm.names.caller, m_throw_type_error_function, m_throw_type_error_function, Attribute::Configurable); + m_function_prototype->define_direct_accessor(vm.names.arguments, m_throw_type_error_function, m_throw_type_error_function, Attribute::Configurable); define_native_function(vm.names.encodeURI, encode_uri, 1, attr); define_native_function(vm.names.decodeURI, decode_uri, 1, attr); @@ -238,6 +237,9 @@ void GlobalObject::initialize_global_object() m_generator_function_constructor = heap().allocate(*this, *this); // 27.3.3.1 GeneratorFunction.prototype.constructor, https://tc39.es/ecma262/#sec-generatorfunction.prototype.constructor m_generator_function_prototype->define_direct_property(vm.names.constructor, m_generator_function_constructor, Attribute::Configurable); + + m_array_prototype_values_function = &m_array_prototype->get_without_side_effects(vm.names.values).as_function(); + m_eval_function = &get_without_side_effects(vm.names.eval).as_function(); } GlobalObject::~GlobalObject() @@ -254,6 +256,9 @@ void GlobalObject::visit_edges(Visitor& visitor) visitor.visit(m_proxy_constructor); visitor.visit(m_generator_object_prototype); visitor.visit(m_environment); + visitor.visit(m_array_prototype_values_function); + visitor.visit(m_eval_function); + visitor.visit(m_throw_type_error_function); #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \ visitor.visit(m_##snake_name##_constructor); \ @@ -271,9 +276,6 @@ void GlobalObject::visit_edges(Visitor& visitor) visitor.visit(m_##snake_name##_prototype); JS_ENUMERATE_ITERATOR_PROTOTYPES #undef __JS_ENUMERATE - - visitor.visit(m_eval_function); - visitor.visit(m_throw_type_error_function); } JS_DEFINE_NATIVE_FUNCTION(GlobalObject::gc) diff --git a/Userland/Libraries/LibJS/Runtime/GlobalObject.h b/Userland/Libraries/LibJS/Runtime/GlobalObject.h index e470f0a0ae..8a7e2552d5 100644 --- a/Userland/Libraries/LibJS/Runtime/GlobalObject.h +++ b/Userland/Libraries/LibJS/Runtime/GlobalObject.h @@ -36,8 +36,8 @@ public: // Not included in JS_ENUMERATE_NATIVE_OBJECTS due to missing distinct constructor GeneratorObjectPrototype* generator_object_prototype() { return m_generator_object_prototype; } + FunctionObject* array_prototype_values_function() const { return m_array_prototype_values_function; } FunctionObject* eval_function() const { return m_eval_function; } - FunctionObject* throw_type_error_function() const { return m_throw_type_error_function; } #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \ @@ -95,6 +95,10 @@ private: GlobalEnvironment* m_environment { nullptr }; + FunctionObject* m_array_prototype_values_function { nullptr }; + FunctionObject* m_eval_function { nullptr }; + FunctionObject* m_throw_type_error_function { nullptr }; + #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \ ConstructorName* m_##snake_name##_constructor { nullptr }; \ Object* m_##snake_name##_prototype { nullptr }; @@ -111,9 +115,6 @@ private: Object* m_##snake_name##_prototype { nullptr }; JS_ENUMERATE_ITERATOR_PROTOTYPES #undef __JS_ENUMERATE - - FunctionObject* m_eval_function; - FunctionObject* m_throw_type_error_function; }; template