Intrinsics.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AggregateErrorConstructor.h>
  7. #include <LibJS/Runtime/AggregateErrorPrototype.h>
  8. #include <LibJS/Runtime/ArrayBufferConstructor.h>
  9. #include <LibJS/Runtime/ArrayBufferPrototype.h>
  10. #include <LibJS/Runtime/ArrayConstructor.h>
  11. #include <LibJS/Runtime/ArrayIteratorPrototype.h>
  12. #include <LibJS/Runtime/ArrayPrototype.h>
  13. #include <LibJS/Runtime/AsyncFromSyncIteratorPrototype.h>
  14. #include <LibJS/Runtime/AsyncFunctionConstructor.h>
  15. #include <LibJS/Runtime/AsyncFunctionPrototype.h>
  16. #include <LibJS/Runtime/AsyncGeneratorFunctionConstructor.h>
  17. #include <LibJS/Runtime/AsyncGeneratorFunctionPrototype.h>
  18. #include <LibJS/Runtime/AsyncGeneratorPrototype.h>
  19. #include <LibJS/Runtime/AsyncIteratorPrototype.h>
  20. #include <LibJS/Runtime/AtomicsObject.h>
  21. #include <LibJS/Runtime/BigIntConstructor.h>
  22. #include <LibJS/Runtime/BigIntPrototype.h>
  23. #include <LibJS/Runtime/BooleanConstructor.h>
  24. #include <LibJS/Runtime/BooleanPrototype.h>
  25. #include <LibJS/Runtime/ConsoleObject.h>
  26. #include <LibJS/Runtime/DataViewConstructor.h>
  27. #include <LibJS/Runtime/DataViewPrototype.h>
  28. #include <LibJS/Runtime/DateConstructor.h>
  29. #include <LibJS/Runtime/DatePrototype.h>
  30. #include <LibJS/Runtime/DisposableStackConstructor.h>
  31. #include <LibJS/Runtime/DisposableStackPrototype.h>
  32. #include <LibJS/Runtime/ErrorConstructor.h>
  33. #include <LibJS/Runtime/ErrorPrototype.h>
  34. #include <LibJS/Runtime/FinalizationRegistryConstructor.h>
  35. #include <LibJS/Runtime/FinalizationRegistryPrototype.h>
  36. #include <LibJS/Runtime/FunctionConstructor.h>
  37. #include <LibJS/Runtime/FunctionPrototype.h>
  38. #include <LibJS/Runtime/GeneratorFunctionConstructor.h>
  39. #include <LibJS/Runtime/GeneratorFunctionPrototype.h>
  40. #include <LibJS/Runtime/GeneratorPrototype.h>
  41. #include <LibJS/Runtime/Intl/CollatorConstructor.h>
  42. #include <LibJS/Runtime/Intl/CollatorPrototype.h>
  43. #include <LibJS/Runtime/Intl/DateTimeFormatConstructor.h>
  44. #include <LibJS/Runtime/Intl/DateTimeFormatPrototype.h>
  45. #include <LibJS/Runtime/Intl/DisplayNamesConstructor.h>
  46. #include <LibJS/Runtime/Intl/DisplayNamesPrototype.h>
  47. #include <LibJS/Runtime/Intl/DurationFormatConstructor.h>
  48. #include <LibJS/Runtime/Intl/DurationFormatPrototype.h>
  49. #include <LibJS/Runtime/Intl/Intl.h>
  50. #include <LibJS/Runtime/Intl/ListFormatConstructor.h>
  51. #include <LibJS/Runtime/Intl/ListFormatPrototype.h>
  52. #include <LibJS/Runtime/Intl/LocaleConstructor.h>
  53. #include <LibJS/Runtime/Intl/LocalePrototype.h>
  54. #include <LibJS/Runtime/Intl/NumberFormatConstructor.h>
  55. #include <LibJS/Runtime/Intl/NumberFormatPrototype.h>
  56. #include <LibJS/Runtime/Intl/PluralRulesConstructor.h>
  57. #include <LibJS/Runtime/Intl/PluralRulesPrototype.h>
  58. #include <LibJS/Runtime/Intl/RelativeTimeFormatConstructor.h>
  59. #include <LibJS/Runtime/Intl/RelativeTimeFormatPrototype.h>
  60. #include <LibJS/Runtime/Intl/SegmentIteratorPrototype.h>
  61. #include <LibJS/Runtime/Intl/SegmenterConstructor.h>
  62. #include <LibJS/Runtime/Intl/SegmenterPrototype.h>
  63. #include <LibJS/Runtime/Intl/SegmentsPrototype.h>
  64. #include <LibJS/Runtime/Intrinsics.h>
  65. #include <LibJS/Runtime/IteratorPrototype.h>
  66. #include <LibJS/Runtime/JSONObject.h>
  67. #include <LibJS/Runtime/MapConstructor.h>
  68. #include <LibJS/Runtime/MapIteratorPrototype.h>
  69. #include <LibJS/Runtime/MapPrototype.h>
  70. #include <LibJS/Runtime/MathObject.h>
  71. #include <LibJS/Runtime/NativeFunction.h>
  72. #include <LibJS/Runtime/NumberConstructor.h>
  73. #include <LibJS/Runtime/NumberPrototype.h>
  74. #include <LibJS/Runtime/ObjectConstructor.h>
  75. #include <LibJS/Runtime/ObjectPrototype.h>
  76. #include <LibJS/Runtime/PromiseConstructor.h>
  77. #include <LibJS/Runtime/PromisePrototype.h>
  78. #include <LibJS/Runtime/ProxyConstructor.h>
  79. #include <LibJS/Runtime/Realm.h>
  80. #include <LibJS/Runtime/ReflectObject.h>
  81. #include <LibJS/Runtime/RegExpConstructor.h>
  82. #include <LibJS/Runtime/RegExpPrototype.h>
  83. #include <LibJS/Runtime/RegExpStringIteratorPrototype.h>
  84. #include <LibJS/Runtime/SetConstructor.h>
  85. #include <LibJS/Runtime/SetIteratorPrototype.h>
  86. #include <LibJS/Runtime/SetPrototype.h>
  87. #include <LibJS/Runtime/ShadowRealmConstructor.h>
  88. #include <LibJS/Runtime/ShadowRealmPrototype.h>
  89. #include <LibJS/Runtime/Shape.h>
  90. #include <LibJS/Runtime/StringConstructor.h>
  91. #include <LibJS/Runtime/StringIteratorPrototype.h>
  92. #include <LibJS/Runtime/StringPrototype.h>
  93. #include <LibJS/Runtime/SuppressedErrorConstructor.h>
  94. #include <LibJS/Runtime/SuppressedErrorPrototype.h>
  95. #include <LibJS/Runtime/SymbolConstructor.h>
  96. #include <LibJS/Runtime/SymbolPrototype.h>
  97. #include <LibJS/Runtime/Temporal/CalendarConstructor.h>
  98. #include <LibJS/Runtime/Temporal/CalendarPrototype.h>
  99. #include <LibJS/Runtime/Temporal/DurationConstructor.h>
  100. #include <LibJS/Runtime/Temporal/DurationPrototype.h>
  101. #include <LibJS/Runtime/Temporal/InstantConstructor.h>
  102. #include <LibJS/Runtime/Temporal/InstantPrototype.h>
  103. #include <LibJS/Runtime/Temporal/PlainDateConstructor.h>
  104. #include <LibJS/Runtime/Temporal/PlainDatePrototype.h>
  105. #include <LibJS/Runtime/Temporal/PlainDateTimeConstructor.h>
  106. #include <LibJS/Runtime/Temporal/PlainDateTimePrototype.h>
  107. #include <LibJS/Runtime/Temporal/PlainMonthDayConstructor.h>
  108. #include <LibJS/Runtime/Temporal/PlainMonthDayPrototype.h>
  109. #include <LibJS/Runtime/Temporal/PlainTimeConstructor.h>
  110. #include <LibJS/Runtime/Temporal/PlainTimePrototype.h>
  111. #include <LibJS/Runtime/Temporal/PlainYearMonthConstructor.h>
  112. #include <LibJS/Runtime/Temporal/PlainYearMonthPrototype.h>
  113. #include <LibJS/Runtime/Temporal/Temporal.h>
  114. #include <LibJS/Runtime/Temporal/TimeZoneConstructor.h>
  115. #include <LibJS/Runtime/Temporal/TimeZonePrototype.h>
  116. #include <LibJS/Runtime/Temporal/ZonedDateTimeConstructor.h>
  117. #include <LibJS/Runtime/Temporal/ZonedDateTimePrototype.h>
  118. #include <LibJS/Runtime/TypedArray.h>
  119. #include <LibJS/Runtime/TypedArrayConstructor.h>
  120. #include <LibJS/Runtime/TypedArrayPrototype.h>
  121. #include <LibJS/Runtime/WeakMapConstructor.h>
  122. #include <LibJS/Runtime/WeakMapPrototype.h>
  123. #include <LibJS/Runtime/WeakRefConstructor.h>
  124. #include <LibJS/Runtime/WeakRefPrototype.h>
  125. #include <LibJS/Runtime/WeakSetConstructor.h>
  126. #include <LibJS/Runtime/WeakSetPrototype.h>
  127. namespace JS {
  128. static void initialize_constructor(VM& vm, PropertyKey const& property_key, Object& constructor, Object* prototype, PropertyAttributes constructor_property_attributes = Attribute::Writable | Attribute::Configurable)
  129. {
  130. constructor.define_direct_property(vm.names.name, PrimitiveString::create(vm, property_key.as_string()), Attribute::Configurable);
  131. if (prototype)
  132. prototype->define_direct_property(vm.names.constructor, &constructor, constructor_property_attributes);
  133. }
  134. // 9.3.2 CreateIntrinsics ( realmRec ), https://tc39.es/ecma262/#sec-createintrinsics
  135. ThrowCompletionOr<NonnullGCPtr<Intrinsics>> Intrinsics::create(Realm& realm)
  136. {
  137. auto& vm = realm.vm();
  138. // 1. Set realmRec.[[Intrinsics]] to a new Record.
  139. auto intrinsics = vm.heap().allocate_without_realm<Intrinsics>(realm);
  140. realm.set_intrinsics({}, intrinsics);
  141. // 2. Set fields of realmRec.[[Intrinsics]] with the values listed in Table 6.
  142. // The field names are the names listed in column one of the table.
  143. // The value of each field is a new object value fully and recursively populated
  144. // with property values as defined by the specification of each object in
  145. // clauses 19 through 28. All object property values are newly created object
  146. // values. All values that are built-in function objects are created by performing
  147. // CreateBuiltinFunction(steps, length, name, slots, realmRec, prototype)
  148. // where steps is the definition of that function provided by this specification,
  149. // name is the initial value of the function's "name" property, length is the
  150. // initial value of the function's "length" property, slots is a list of the
  151. // names, if any, of the function's specified internal slots, and prototype
  152. // is the specified value of the function's [[Prototype]] internal slot. The
  153. // creation of the intrinsics and their properties must be ordered to avoid
  154. // any dependencies upon objects that have not yet been created.
  155. MUST_OR_THROW_OOM(intrinsics->initialize_intrinsics(realm));
  156. // 3. Perform AddRestrictedFunctionProperties(realmRec.[[Intrinsics]].[[%Function.prototype%]], realmRec).
  157. add_restricted_function_properties(static_cast<FunctionObject&>(*realm.intrinsics().function_prototype()), realm);
  158. // 4. Return unused.
  159. return *intrinsics;
  160. }
  161. ThrowCompletionOr<void> Intrinsics::initialize_intrinsics(Realm& realm)
  162. {
  163. auto& vm = this->vm();
  164. // These are done first since other prototypes depend on their presence.
  165. m_empty_object_shape = heap().allocate_without_realm<Shape>(realm);
  166. m_object_prototype = heap().allocate_without_realm<ObjectPrototype>(realm);
  167. m_function_prototype = heap().allocate_without_realm<FunctionPrototype>(realm);
  168. m_new_object_shape = heap().allocate_without_realm<Shape>(realm);
  169. m_new_object_shape->set_prototype_without_transition(m_object_prototype);
  170. m_new_ordinary_function_prototype_object_shape = heap().allocate_without_realm<Shape>(realm);
  171. m_new_ordinary_function_prototype_object_shape->set_prototype_without_transition(m_object_prototype);
  172. m_new_ordinary_function_prototype_object_shape->add_property_without_transition(vm.names.constructor, Attribute::Writable | Attribute::Configurable);
  173. // Normally Heap::allocate() takes care of this, but these are allocated via allocate_without_realm().
  174. MUST_OR_THROW_OOM(m_function_prototype->initialize(realm));
  175. MUST_OR_THROW_OOM(m_object_prototype->initialize(realm));
  176. #define __JS_ENUMERATE(ClassName, snake_name) \
  177. VERIFY(!m_##snake_name##_prototype); \
  178. m_##snake_name##_prototype = heap().allocate<ClassName##Prototype>(realm, realm).release_allocated_value_but_fixme_should_propagate_errors();
  179. JS_ENUMERATE_ITERATOR_PROTOTYPES
  180. #undef __JS_ENUMERATE
  181. // These must be initialized separately as they have no companion constructor
  182. m_async_from_sync_iterator_prototype = heap().allocate<AsyncFromSyncIteratorPrototype>(realm, realm).release_allocated_value_but_fixme_should_propagate_errors();
  183. m_async_generator_prototype = heap().allocate<AsyncGeneratorPrototype>(realm, realm).release_allocated_value_but_fixme_should_propagate_errors();
  184. m_generator_prototype = heap().allocate<GeneratorPrototype>(realm, realm).release_allocated_value_but_fixme_should_propagate_errors();
  185. m_intl_segments_prototype = heap().allocate<Intl::SegmentsPrototype>(realm, realm).release_allocated_value_but_fixme_should_propagate_errors();
  186. // These must be initialized before allocating...
  187. // - AggregateErrorPrototype, which uses ErrorPrototype as its prototype
  188. // - AggregateErrorConstructor, which uses ErrorConstructor as its prototype
  189. // - AsyncFunctionConstructor, which uses FunctionConstructor as its prototype
  190. m_error_prototype = heap().allocate<ErrorPrototype>(realm, realm).release_allocated_value_but_fixme_should_propagate_errors();
  191. m_error_constructor = heap().allocate<ErrorConstructor>(realm, realm).release_allocated_value_but_fixme_should_propagate_errors();
  192. m_function_constructor = heap().allocate<FunctionConstructor>(realm, realm).release_allocated_value_but_fixme_should_propagate_errors();
  193. // Not included in JS_ENUMERATE_NATIVE_OBJECTS due to missing distinct prototype
  194. m_proxy_constructor = heap().allocate<ProxyConstructor>(realm, realm).release_allocated_value_but_fixme_should_propagate_errors();
  195. // Global object functions
  196. m_eval_function = NativeFunction::create(realm, GlobalObject::eval, 1, vm.names.eval, &realm);
  197. m_is_finite_function = NativeFunction::create(realm, GlobalObject::is_finite, 1, vm.names.isFinite, &realm);
  198. m_is_nan_function = NativeFunction::create(realm, GlobalObject::is_nan, 1, vm.names.isNaN, &realm);
  199. m_parse_float_function = NativeFunction::create(realm, GlobalObject::parse_float, 1, vm.names.parseFloat, &realm);
  200. m_parse_int_function = NativeFunction::create(realm, GlobalObject::parse_int, 2, vm.names.parseInt, &realm);
  201. m_decode_uri_function = NativeFunction::create(realm, GlobalObject::decode_uri, 1, vm.names.decodeURI, &realm);
  202. m_decode_uri_component_function = NativeFunction::create(realm, GlobalObject::decode_uri_component, 1, vm.names.decodeURIComponent, &realm);
  203. m_encode_uri_function = NativeFunction::create(realm, GlobalObject::encode_uri, 1, vm.names.encodeURI, &realm);
  204. m_encode_uri_component_function = NativeFunction::create(realm, GlobalObject::encode_uri_component, 1, vm.names.encodeURIComponent, &realm);
  205. m_escape_function = NativeFunction::create(realm, GlobalObject::escape, 1, vm.names.escape, &realm);
  206. m_unescape_function = NativeFunction::create(realm, GlobalObject::unescape, 1, vm.names.unescape, &realm);
  207. m_object_constructor = heap().allocate<ObjectConstructor>(realm, realm).release_allocated_value_but_fixme_should_propagate_errors();
  208. // 10.2.4.1 %ThrowTypeError% ( ), https://tc39.es/ecma262/#sec-%throwtypeerror%
  209. m_throw_type_error_function = NativeFunction::create(
  210. realm, [](VM& vm) {
  211. return vm.throw_completion<TypeError>(ErrorType::RestrictedFunctionPropertiesAccess);
  212. },
  213. 0, "", &realm);
  214. m_throw_type_error_function->define_direct_property(vm.names.length, Value(0), 0);
  215. m_throw_type_error_function->define_direct_property(vm.names.name, PrimitiveString::create(vm, String {}), 0);
  216. MUST(m_throw_type_error_function->internal_prevent_extensions());
  217. initialize_constructor(vm, vm.names.Error, *m_error_constructor, m_error_prototype);
  218. initialize_constructor(vm, vm.names.Function, *m_function_constructor, m_function_prototype);
  219. initialize_constructor(vm, vm.names.Object, *m_object_constructor, m_object_prototype);
  220. initialize_constructor(vm, vm.names.Proxy, *m_proxy_constructor, nullptr);
  221. initialize_constructor(vm, vm.names.GeneratorFunction, *generator_function_constructor(), generator_function_prototype(), Attribute::Configurable);
  222. initialize_constructor(vm, vm.names.AsyncGeneratorFunction, *async_generator_function_constructor(), async_generator_function_prototype(), Attribute::Configurable);
  223. initialize_constructor(vm, vm.names.AsyncFunction, *async_function_constructor(), async_function_prototype(), Attribute::Configurable);
  224. // 27.5.1.1 Generator.prototype.constructor, https://tc39.es/ecma262/#sec-generator.prototype.constructor
  225. m_generator_prototype->define_direct_property(vm.names.constructor, m_generator_function_prototype, Attribute::Configurable);
  226. // 27.6.1.1 AsyncGenerator.prototype.constructor, https://tc39.es/ecma262/#sec-asyncgenerator-prototype-constructor
  227. m_async_generator_prototype->define_direct_property(vm.names.constructor, m_async_generator_function_prototype, Attribute::Configurable);
  228. m_array_prototype_values_function = &array_prototype()->get_without_side_effects(vm.names.values).as_function();
  229. m_date_constructor_now_function = &date_constructor()->get_without_side_effects(vm.names.now).as_function();
  230. m_json_parse_function = &json_object()->get_without_side_effects(vm.names.parse).as_function();
  231. m_json_stringify_function = &json_object()->get_without_side_effects(vm.names.stringify).as_function();
  232. m_object_prototype_to_string_function = &object_prototype()->get_without_side_effects(vm.names.toString).as_function();
  233. return {};
  234. }
  235. template<typename T>
  236. constexpr inline bool IsTypedArrayConstructor = false;
  237. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  238. template<> \
  239. constexpr inline bool IsTypedArrayConstructor<ConstructorName> = true;
  240. JS_ENUMERATE_TYPED_ARRAYS
  241. #undef __JS_ENUMERATE
  242. #define __JS_ENUMERATE_INNER(ClassName, snake_name, PrototypeName, ConstructorName, Namespace, snake_namespace) \
  243. void Intrinsics::initialize_##snake_namespace##snake_name() \
  244. { \
  245. auto& vm = this->vm(); \
  246. \
  247. VERIFY(!m_##snake_namespace##snake_name##_prototype); \
  248. VERIFY(!m_##snake_namespace##snake_name##_constructor); \
  249. if constexpr (IsTypedArrayConstructor<Namespace::ConstructorName>) { \
  250. m_##snake_namespace##snake_name##_prototype = heap().allocate<Namespace::PrototypeName>(m_realm, *typed_array_prototype()).release_allocated_value_but_fixme_should_propagate_errors(); \
  251. m_##snake_namespace##snake_name##_constructor = heap().allocate<Namespace::ConstructorName>(m_realm, m_realm, *typed_array_constructor()).release_allocated_value_but_fixme_should_propagate_errors(); \
  252. } else { \
  253. m_##snake_namespace##snake_name##_prototype = heap().allocate<Namespace::PrototypeName>(m_realm, m_realm).release_allocated_value_but_fixme_should_propagate_errors(); \
  254. m_##snake_namespace##snake_name##_constructor = heap().allocate<Namespace::ConstructorName>(m_realm, m_realm).release_allocated_value_but_fixme_should_propagate_errors(); \
  255. } \
  256. \
  257. /* FIXME: Add these special cases to JS_ENUMERATE_NATIVE_OBJECTS */ \
  258. if constexpr (IsSame<Namespace::ConstructorName, BigIntConstructor>) \
  259. initialize_constructor(vm, vm.names.BigInt, *m_##snake_namespace##snake_name##_constructor, m_##snake_namespace##snake_name##_prototype); \
  260. else if constexpr (IsSame<Namespace::ConstructorName, BooleanConstructor>) \
  261. initialize_constructor(vm, vm.names.Boolean, *m_##snake_namespace##snake_name##_constructor, m_##snake_namespace##snake_name##_prototype); \
  262. else if constexpr (IsSame<Namespace::ConstructorName, FunctionConstructor>) \
  263. initialize_constructor(vm, vm.names.Function, *m_##snake_namespace##snake_name##_constructor, m_##snake_namespace##snake_name##_prototype); \
  264. else if constexpr (IsSame<Namespace::ConstructorName, NumberConstructor>) \
  265. initialize_constructor(vm, vm.names.Number, *m_##snake_namespace##snake_name##_constructor, m_##snake_namespace##snake_name##_prototype); \
  266. else if constexpr (IsSame<Namespace::ConstructorName, RegExpConstructor>) \
  267. initialize_constructor(vm, vm.names.RegExp, *m_##snake_namespace##snake_name##_constructor, m_##snake_namespace##snake_name##_prototype); \
  268. else if constexpr (IsSame<Namespace::ConstructorName, StringConstructor>) \
  269. initialize_constructor(vm, vm.names.String, *m_##snake_namespace##snake_name##_constructor, m_##snake_namespace##snake_name##_prototype); \
  270. else if constexpr (IsSame<Namespace::ConstructorName, SymbolConstructor>) \
  271. initialize_constructor(vm, vm.names.Symbol, *m_##snake_namespace##snake_name##_constructor, m_##snake_namespace##snake_name##_prototype); \
  272. else \
  273. initialize_constructor(vm, vm.names.ClassName, *m_##snake_namespace##snake_name##_constructor, m_##snake_namespace##snake_name##_prototype); \
  274. } \
  275. \
  276. NonnullGCPtr<Namespace::ConstructorName> Intrinsics::snake_namespace##snake_name##_constructor() \
  277. { \
  278. if (!m_##snake_namespace##snake_name##_constructor) \
  279. initialize_##snake_namespace##snake_name(); \
  280. return *m_##snake_namespace##snake_name##_constructor; \
  281. } \
  282. \
  283. NonnullGCPtr<Object> Intrinsics::snake_namespace##snake_name##_prototype() \
  284. { \
  285. if (!m_##snake_namespace##snake_name##_prototype) \
  286. initialize_##snake_namespace##snake_name(); \
  287. return *m_##snake_namespace##snake_name##_prototype; \
  288. }
  289. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  290. __JS_ENUMERATE_INNER(ClassName, snake_name, PrototypeName, ConstructorName, JS, )
  291. JS_ENUMERATE_BUILTIN_TYPES
  292. #undef __JS_ENUMERATE
  293. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  294. __JS_ENUMERATE_INNER(ClassName, snake_name, PrototypeName, ConstructorName, JS::Intl, intl_)
  295. JS_ENUMERATE_INTL_OBJECTS
  296. #undef __JS_ENUMERATE
  297. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  298. __JS_ENUMERATE_INNER(ClassName, snake_name, PrototypeName, ConstructorName, JS::Temporal, temporal_)
  299. JS_ENUMERATE_TEMPORAL_OBJECTS
  300. #undef __JS_ENUMERATE
  301. #undef __JS_ENUMERATE_INNER
  302. #define __JS_ENUMERATE(ClassName, snake_name) \
  303. NonnullGCPtr<ClassName> Intrinsics::snake_name##_object() \
  304. { \
  305. if (!m_##snake_name##_object) \
  306. m_##snake_name##_object = heap().allocate<ClassName>(m_realm, m_realm).release_allocated_value_but_fixme_should_propagate_errors(); \
  307. return *m_##snake_name##_object; \
  308. }
  309. JS_ENUMERATE_BUILTIN_NAMESPACE_OBJECTS
  310. #undef __JS_ENUMERATE
  311. void Intrinsics::visit_edges(Visitor& visitor)
  312. {
  313. visitor.visit(m_realm);
  314. visitor.visit(m_empty_object_shape);
  315. visitor.visit(m_new_object_shape);
  316. visitor.visit(m_new_ordinary_function_prototype_object_shape);
  317. visitor.visit(m_proxy_constructor);
  318. visitor.visit(m_async_from_sync_iterator_prototype);
  319. visitor.visit(m_async_generator_prototype);
  320. visitor.visit(m_generator_prototype);
  321. visitor.visit(m_intl_segments_prototype);
  322. visitor.visit(m_array_prototype_values_function);
  323. visitor.visit(m_date_constructor_now_function);
  324. visitor.visit(m_eval_function);
  325. visitor.visit(m_json_parse_function);
  326. visitor.visit(m_json_stringify_function);
  327. visitor.visit(m_object_prototype_to_string_function);
  328. visitor.visit(m_throw_type_error_function);
  329. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  330. visitor.visit(m_##snake_name##_constructor); \
  331. visitor.visit(m_##snake_name##_prototype);
  332. JS_ENUMERATE_BUILTIN_TYPES
  333. #undef __JS_ENUMERATE
  334. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  335. visitor.visit(m_intl_##snake_name##_constructor); \
  336. visitor.visit(m_intl_##snake_name##_prototype);
  337. JS_ENUMERATE_INTL_OBJECTS
  338. #undef __JS_ENUMERATE
  339. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  340. visitor.visit(m_temporal_##snake_name##_constructor); \
  341. visitor.visit(m_temporal_##snake_name##_prototype);
  342. JS_ENUMERATE_TEMPORAL_OBJECTS
  343. #undef __JS_ENUMERATE
  344. #define __JS_ENUMERATE(ClassName, snake_name) \
  345. visitor.visit(m_##snake_name##_object);
  346. JS_ENUMERATE_BUILTIN_NAMESPACE_OBJECTS
  347. #undef __JS_ENUMERATE
  348. #define __JS_ENUMERATE(ClassName, snake_name) \
  349. visitor.visit(m_##snake_name##_prototype);
  350. JS_ENUMERATE_ITERATOR_PROTOTYPES
  351. #undef __JS_ENUMERATE
  352. }
  353. // 10.2.4 AddRestrictedFunctionProperties ( F, realm ), https://tc39.es/ecma262/#sec-addrestrictedfunctionproperties
  354. void add_restricted_function_properties(FunctionObject& function, Realm& realm)
  355. {
  356. auto& vm = realm.vm();
  357. // 1. Assert: realm.[[Intrinsics]].[[%ThrowTypeError%]] exists and has been initialized.
  358. // NOTE: This is ensured by dereferencing the GCPtr in the getter.
  359. // 2. Let thrower be realm.[[Intrinsics]].[[%ThrowTypeError%]].
  360. auto thrower = realm.intrinsics().throw_type_error_function();
  361. // 3. Perform ! DefinePropertyOrThrow(F, "caller", PropertyDescriptor { [[Get]]: thrower, [[Set]]: thrower, [[Enumerable]]: false, [[Configurable]]: true }).
  362. function.define_direct_accessor(vm.names.caller, thrower, thrower, Attribute::Configurable);
  363. // 4. Perform ! DefinePropertyOrThrow(F, "arguments", PropertyDescriptor { [[Get]]: thrower, [[Set]]: thrower, [[Enumerable]]: false, [[Configurable]]: true }).
  364. function.define_direct_accessor(vm.names.arguments, thrower, thrower, Attribute::Configurable);
  365. // 5. Return unused.
  366. }
  367. }