Prechádzať zdrojové kódy

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.
Linus Groh 4 rokov pred
rodič
commit
1c1354db07

+ 2 - 8
Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp

@@ -435,10 +435,7 @@ Object* create_unmapped_arguments_object(GlobalObject& global_object, Vector<Val
     }
 
     // 7. 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());
 
@@ -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());
 

+ 8 - 6
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<GeneratorFunctionConstructor>(*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)

+ 5 - 4
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<typename ConstructorType>