Intrinsics.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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/IteratorConstructor.h>
  66. #include <LibJS/Runtime/IteratorHelperPrototype.h>
  67. #include <LibJS/Runtime/IteratorPrototype.h>
  68. #include <LibJS/Runtime/JSONObject.h>
  69. #include <LibJS/Runtime/MapConstructor.h>
  70. #include <LibJS/Runtime/MapIteratorPrototype.h>
  71. #include <LibJS/Runtime/MapPrototype.h>
  72. #include <LibJS/Runtime/MathObject.h>
  73. #include <LibJS/Runtime/NativeFunction.h>
  74. #include <LibJS/Runtime/NumberConstructor.h>
  75. #include <LibJS/Runtime/NumberPrototype.h>
  76. #include <LibJS/Runtime/ObjectConstructor.h>
  77. #include <LibJS/Runtime/ObjectPrototype.h>
  78. #include <LibJS/Runtime/PromiseConstructor.h>
  79. #include <LibJS/Runtime/PromisePrototype.h>
  80. #include <LibJS/Runtime/ProxyConstructor.h>
  81. #include <LibJS/Runtime/Realm.h>
  82. #include <LibJS/Runtime/ReflectObject.h>
  83. #include <LibJS/Runtime/RegExpConstructor.h>
  84. #include <LibJS/Runtime/RegExpPrototype.h>
  85. #include <LibJS/Runtime/RegExpStringIteratorPrototype.h>
  86. #include <LibJS/Runtime/SetConstructor.h>
  87. #include <LibJS/Runtime/SetIteratorPrototype.h>
  88. #include <LibJS/Runtime/SetPrototype.h>
  89. #include <LibJS/Runtime/ShadowRealmConstructor.h>
  90. #include <LibJS/Runtime/ShadowRealmPrototype.h>
  91. #include <LibJS/Runtime/Shape.h>
  92. #include <LibJS/Runtime/SharedArrayBufferConstructor.h>
  93. #include <LibJS/Runtime/SharedArrayBufferPrototype.h>
  94. #include <LibJS/Runtime/StringConstructor.h>
  95. #include <LibJS/Runtime/StringIteratorPrototype.h>
  96. #include <LibJS/Runtime/StringPrototype.h>
  97. #include <LibJS/Runtime/SuppressedErrorConstructor.h>
  98. #include <LibJS/Runtime/SuppressedErrorPrototype.h>
  99. #include <LibJS/Runtime/SymbolConstructor.h>
  100. #include <LibJS/Runtime/SymbolPrototype.h>
  101. #include <LibJS/Runtime/Temporal/DurationConstructor.h>
  102. #include <LibJS/Runtime/Temporal/DurationPrototype.h>
  103. #include <LibJS/Runtime/Temporal/PlainDateConstructor.h>
  104. #include <LibJS/Runtime/Temporal/PlainDatePrototype.h>
  105. #include <LibJS/Runtime/Temporal/PlainMonthDayConstructor.h>
  106. #include <LibJS/Runtime/Temporal/PlainMonthDayPrototype.h>
  107. #include <LibJS/Runtime/Temporal/PlainYearMonthConstructor.h>
  108. #include <LibJS/Runtime/Temporal/PlainYearMonthPrototype.h>
  109. #include <LibJS/Runtime/Temporal/Temporal.h>
  110. #include <LibJS/Runtime/TypedArray.h>
  111. #include <LibJS/Runtime/TypedArrayConstructor.h>
  112. #include <LibJS/Runtime/TypedArrayPrototype.h>
  113. #include <LibJS/Runtime/WeakMapConstructor.h>
  114. #include <LibJS/Runtime/WeakMapPrototype.h>
  115. #include <LibJS/Runtime/WeakRefConstructor.h>
  116. #include <LibJS/Runtime/WeakRefPrototype.h>
  117. #include <LibJS/Runtime/WeakSetConstructor.h>
  118. #include <LibJS/Runtime/WeakSetPrototype.h>
  119. #include <LibJS/Runtime/WrapForValidIteratorPrototype.h>
  120. namespace JS {
  121. GC_DEFINE_ALLOCATOR(Intrinsics);
  122. static void initialize_constructor(VM& vm, PropertyKey const& property_key, Object& constructor, Object* prototype, PropertyAttributes constructor_property_attributes = Attribute::Writable | Attribute::Configurable)
  123. {
  124. constructor.define_direct_property(vm.names.name, PrimitiveString::create(vm, property_key.as_string()), Attribute::Configurable);
  125. if (prototype)
  126. prototype->define_direct_property(vm.names.constructor, &constructor, constructor_property_attributes);
  127. }
  128. // 9.3.2 CreateIntrinsics ( realmRec ), https://tc39.es/ecma262/#sec-createintrinsics
  129. ThrowCompletionOr<GC::Ref<Intrinsics>> Intrinsics::create(Realm& realm)
  130. {
  131. auto& vm = realm.vm();
  132. // 1. Set realmRec.[[Intrinsics]] to a new Record.
  133. auto intrinsics = vm.heap().allocate<Intrinsics>(realm);
  134. realm.set_intrinsics({}, intrinsics);
  135. // 2. Set fields of realmRec.[[Intrinsics]] with the values listed in Table 6.
  136. // The field names are the names listed in column one of the table.
  137. // The value of each field is a new object value fully and recursively populated
  138. // with property values as defined by the specification of each object in
  139. // clauses 19 through 28. All object property values are newly created object
  140. // values. All values that are built-in function objects are created by performing
  141. // CreateBuiltinFunction(steps, length, name, slots, realmRec, prototype)
  142. // where steps is the definition of that function provided by this specification,
  143. // name is the initial value of the function's "name" property, length is the
  144. // initial value of the function's "length" property, slots is a list of the
  145. // names, if any, of the function's specified internal slots, and prototype
  146. // is the specified value of the function's [[Prototype]] internal slot. The
  147. // creation of the intrinsics and their properties must be ordered to avoid
  148. // any dependencies upon objects that have not yet been created.
  149. MUST_OR_THROW_OOM(intrinsics->initialize_intrinsics(realm));
  150. // 3. Perform AddRestrictedFunctionProperties(realmRec.[[Intrinsics]].[[%Function.prototype%]], realmRec).
  151. add_restricted_function_properties(static_cast<FunctionObject&>(*realm.intrinsics().function_prototype()), realm);
  152. // 4. Return unused.
  153. return *intrinsics;
  154. }
  155. ThrowCompletionOr<void> Intrinsics::initialize_intrinsics(Realm& realm)
  156. {
  157. auto& vm = this->vm();
  158. // These are done first since other prototypes depend on their presence.
  159. m_empty_object_shape = heap().allocate<Shape>(realm);
  160. m_object_prototype = heap().allocate<ObjectPrototype>(realm);
  161. m_object_prototype->convert_to_prototype_if_needed();
  162. m_function_prototype = heap().allocate<FunctionPrototype>(realm);
  163. m_function_prototype->convert_to_prototype_if_needed();
  164. m_new_object_shape = heap().allocate<Shape>(realm);
  165. m_new_object_shape->set_prototype_without_transition(m_object_prototype);
  166. // OPTIMIZATION: A lot of runtime algorithms create an "iterator result" object.
  167. // We pre-bake a shape for these objects and remember the property offsets.
  168. // This allows us to construct them very quickly.
  169. m_iterator_result_object_shape = heap().allocate<Shape>(realm);
  170. m_iterator_result_object_shape->set_prototype_without_transition(m_object_prototype);
  171. m_iterator_result_object_shape->add_property_without_transition(vm.names.value, Attribute::Writable | Attribute::Configurable | Attribute::Enumerable);
  172. m_iterator_result_object_shape->add_property_without_transition(vm.names.done, Attribute::Writable | Attribute::Configurable | Attribute::Enumerable);
  173. m_iterator_result_object_value_offset = m_iterator_result_object_shape->lookup(vm.names.value.to_string_or_symbol()).value().offset;
  174. m_iterator_result_object_done_offset = m_iterator_result_object_shape->lookup(vm.names.done.to_string_or_symbol()).value().offset;
  175. // Normally Realm::create() takes care of this, but these are allocated via Heap::allocate().
  176. m_function_prototype->initialize(realm);
  177. m_object_prototype->initialize(realm);
  178. #define __JS_ENUMERATE(ClassName, snake_name) \
  179. VERIFY(!m_##snake_name##_prototype); \
  180. m_##snake_name##_prototype = realm.create<ClassName##Prototype>(realm);
  181. JS_ENUMERATE_ITERATOR_PROTOTYPES
  182. #undef __JS_ENUMERATE
  183. // These must be initialized separately as they have no companion constructor
  184. m_async_from_sync_iterator_prototype = realm.create<AsyncFromSyncIteratorPrototype>(realm);
  185. m_async_generator_prototype = realm.create<AsyncGeneratorPrototype>(realm);
  186. m_generator_prototype = realm.create<GeneratorPrototype>(realm);
  187. m_intl_segments_prototype = realm.create<Intl::SegmentsPrototype>(realm);
  188. m_wrap_for_valid_iterator_prototype = realm.create<WrapForValidIteratorPrototype>(realm);
  189. // These must be initialized before allocating...
  190. // - AggregateErrorPrototype, which uses ErrorPrototype as its prototype
  191. // - AggregateErrorConstructor, which uses ErrorConstructor as its prototype
  192. // - AsyncFunctionConstructor, which uses FunctionConstructor as its prototype
  193. m_error_prototype = realm.create<ErrorPrototype>(realm);
  194. m_error_constructor = realm.create<ErrorConstructor>(realm);
  195. m_function_constructor = realm.create<FunctionConstructor>(realm);
  196. // Not included in JS_ENUMERATE_NATIVE_OBJECTS due to missing distinct prototype
  197. m_proxy_constructor = realm.create<ProxyConstructor>(realm);
  198. // Global object functions
  199. m_eval_function = NativeFunction::create(realm, GlobalObject::eval, 1, vm.names.eval, &realm);
  200. m_is_finite_function = NativeFunction::create(realm, GlobalObject::is_finite, 1, vm.names.isFinite, &realm);
  201. m_is_nan_function = NativeFunction::create(realm, GlobalObject::is_nan, 1, vm.names.isNaN, &realm);
  202. m_parse_float_function = NativeFunction::create(realm, GlobalObject::parse_float, 1, vm.names.parseFloat, &realm);
  203. m_parse_int_function = NativeFunction::create(realm, GlobalObject::parse_int, 2, vm.names.parseInt, &realm);
  204. m_decode_uri_function = NativeFunction::create(realm, GlobalObject::decode_uri, 1, vm.names.decodeURI, &realm);
  205. m_decode_uri_component_function = NativeFunction::create(realm, GlobalObject::decode_uri_component, 1, vm.names.decodeURIComponent, &realm);
  206. m_encode_uri_function = NativeFunction::create(realm, GlobalObject::encode_uri, 1, vm.names.encodeURI, &realm);
  207. m_encode_uri_component_function = NativeFunction::create(realm, GlobalObject::encode_uri_component, 1, vm.names.encodeURIComponent, &realm);
  208. m_escape_function = NativeFunction::create(realm, GlobalObject::escape, 1, vm.names.escape, &realm);
  209. m_unescape_function = NativeFunction::create(realm, GlobalObject::unescape, 1, vm.names.unescape, &realm);
  210. m_object_constructor = realm.create<ObjectConstructor>(realm);
  211. // 10.2.4.1 %ThrowTypeError% ( ), https://tc39.es/ecma262/#sec-%throwtypeerror%
  212. m_throw_type_error_function = NativeFunction::create(
  213. realm, [](VM& vm) {
  214. return vm.throw_completion<TypeError>(ErrorType::RestrictedFunctionPropertiesAccess);
  215. },
  216. 0, "", &realm);
  217. m_throw_type_error_function->define_direct_property(vm.names.length, Value(0), 0);
  218. m_throw_type_error_function->define_direct_property(vm.names.name, PrimitiveString::create(vm, String {}), 0);
  219. MUST(m_throw_type_error_function->internal_prevent_extensions());
  220. initialize_constructor(vm, vm.names.Error, *m_error_constructor, m_error_prototype);
  221. initialize_constructor(vm, vm.names.Function, *m_function_constructor, m_function_prototype);
  222. initialize_constructor(vm, vm.names.Object, *m_object_constructor, m_object_prototype);
  223. initialize_constructor(vm, vm.names.Proxy, *m_proxy_constructor, nullptr);
  224. initialize_constructor(vm, vm.names.GeneratorFunction, *generator_function_constructor(), generator_function_prototype(), Attribute::Configurable);
  225. initialize_constructor(vm, vm.names.AsyncGeneratorFunction, *async_generator_function_constructor(), async_generator_function_prototype(), Attribute::Configurable);
  226. initialize_constructor(vm, vm.names.AsyncFunction, *async_function_constructor(), async_function_prototype(), Attribute::Configurable);
  227. // 27.5.1.1 Generator.prototype.constructor, https://tc39.es/ecma262/#sec-generator.prototype.constructor
  228. m_generator_prototype->define_direct_property(vm.names.constructor, m_generator_function_prototype, Attribute::Configurable);
  229. // 27.6.1.1 AsyncGenerator.prototype.constructor, https://tc39.es/ecma262/#sec-asyncgenerator-prototype-constructor
  230. m_async_generator_prototype->define_direct_property(vm.names.constructor, m_async_generator_function_prototype, Attribute::Configurable);
  231. m_array_prototype_values_function = &array_prototype()->get_without_side_effects(vm.names.values).as_function();
  232. m_date_constructor_now_function = &date_constructor()->get_without_side_effects(vm.names.now).as_function();
  233. m_json_parse_function = &json_object()->get_without_side_effects(vm.names.parse).as_function();
  234. m_json_stringify_function = &json_object()->get_without_side_effects(vm.names.stringify).as_function();
  235. m_object_prototype_to_string_function = &object_prototype()->get_without_side_effects(vm.names.toString).as_function();
  236. return {};
  237. }
  238. template<typename T>
  239. constexpr inline bool IsTypedArrayConstructor = false;
  240. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  241. template<> \
  242. constexpr inline bool IsTypedArrayConstructor<ConstructorName> = true;
  243. JS_ENUMERATE_TYPED_ARRAYS
  244. #undef __JS_ENUMERATE
  245. #define __JS_ENUMERATE_INNER(ClassName, snake_name, PrototypeName, ConstructorName, Namespace, snake_namespace) \
  246. void Intrinsics::initialize_##snake_namespace##snake_name() \
  247. { \
  248. auto& vm = this->vm(); \
  249. \
  250. VERIFY(!m_##snake_namespace##snake_name##_prototype); \
  251. VERIFY(!m_##snake_namespace##snake_name##_constructor); \
  252. if constexpr (IsTypedArrayConstructor<Namespace::ConstructorName>) { \
  253. m_##snake_namespace##snake_name##_prototype = m_realm->create<Namespace::PrototypeName>(*typed_array_prototype()); \
  254. m_##snake_namespace##snake_name##_constructor = m_realm->create<Namespace::ConstructorName>(m_realm, *typed_array_constructor()); \
  255. } else { \
  256. m_##snake_namespace##snake_name##_prototype = m_realm->create<Namespace::PrototypeName>(m_realm); \
  257. m_##snake_namespace##snake_name##_constructor = m_realm->create<Namespace::ConstructorName>(m_realm); \
  258. } \
  259. \
  260. /* FIXME: Add these special cases to JS_ENUMERATE_NATIVE_OBJECTS */ \
  261. if constexpr (IsSame<Namespace::ConstructorName, BigIntConstructor>) \
  262. initialize_constructor(vm, vm.names.BigInt, *m_##snake_namespace##snake_name##_constructor, m_##snake_namespace##snake_name##_prototype); \
  263. else if constexpr (IsSame<Namespace::ConstructorName, BooleanConstructor>) \
  264. initialize_constructor(vm, vm.names.Boolean, *m_##snake_namespace##snake_name##_constructor, m_##snake_namespace##snake_name##_prototype); \
  265. else if constexpr (IsSame<Namespace::ConstructorName, FunctionConstructor>) \
  266. initialize_constructor(vm, vm.names.Function, *m_##snake_namespace##snake_name##_constructor, m_##snake_namespace##snake_name##_prototype); \
  267. else if constexpr (IsSame<Namespace::ConstructorName, IteratorConstructor>) \
  268. initialize_constructor(vm, vm.names.Iterator, *m_##snake_namespace##snake_name##_constructor, nullptr); \
  269. else if constexpr (IsSame<Namespace::ConstructorName, NumberConstructor>) \
  270. initialize_constructor(vm, vm.names.Number, *m_##snake_namespace##snake_name##_constructor, m_##snake_namespace##snake_name##_prototype); \
  271. else if constexpr (IsSame<Namespace::ConstructorName, RegExpConstructor>) \
  272. initialize_constructor(vm, vm.names.RegExp, *m_##snake_namespace##snake_name##_constructor, m_##snake_namespace##snake_name##_prototype); \
  273. else if constexpr (IsSame<Namespace::ConstructorName, StringConstructor>) \
  274. initialize_constructor(vm, vm.names.String, *m_##snake_namespace##snake_name##_constructor, m_##snake_namespace##snake_name##_prototype); \
  275. else if constexpr (IsSame<Namespace::ConstructorName, SymbolConstructor>) \
  276. initialize_constructor(vm, vm.names.Symbol, *m_##snake_namespace##snake_name##_constructor, m_##snake_namespace##snake_name##_prototype); \
  277. else \
  278. initialize_constructor(vm, vm.names.ClassName, *m_##snake_namespace##snake_name##_constructor, m_##snake_namespace##snake_name##_prototype); \
  279. } \
  280. \
  281. GC::Ref<Namespace::ConstructorName> Intrinsics::snake_namespace##snake_name##_constructor() \
  282. { \
  283. if (!m_##snake_namespace##snake_name##_constructor) \
  284. initialize_##snake_namespace##snake_name(); \
  285. return *m_##snake_namespace##snake_name##_constructor; \
  286. } \
  287. \
  288. GC::Ref<Object> Intrinsics::snake_namespace##snake_name##_prototype() \
  289. { \
  290. if (!m_##snake_namespace##snake_name##_prototype) \
  291. initialize_##snake_namespace##snake_name(); \
  292. return *m_##snake_namespace##snake_name##_prototype; \
  293. }
  294. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  295. __JS_ENUMERATE_INNER(ClassName, snake_name, PrototypeName, ConstructorName, JS, )
  296. JS_ENUMERATE_BUILTIN_TYPES
  297. #undef __JS_ENUMERATE
  298. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  299. __JS_ENUMERATE_INNER(ClassName, snake_name, PrototypeName, ConstructorName, JS::Intl, intl_)
  300. JS_ENUMERATE_INTL_OBJECTS
  301. #undef __JS_ENUMERATE
  302. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  303. __JS_ENUMERATE_INNER(ClassName, snake_name, PrototypeName, ConstructorName, JS::Temporal, temporal_)
  304. JS_ENUMERATE_TEMPORAL_OBJECTS
  305. #undef __JS_ENUMERATE
  306. #undef __JS_ENUMERATE_INNER
  307. #define __JS_ENUMERATE(ClassName, snake_name) \
  308. GC::Ref<ClassName> Intrinsics::snake_name##_object() \
  309. { \
  310. if (!m_##snake_name##_object) \
  311. m_##snake_name##_object = m_realm->create<ClassName>(m_realm); \
  312. return *m_##snake_name##_object; \
  313. }
  314. JS_ENUMERATE_BUILTIN_NAMESPACE_OBJECTS
  315. #undef __JS_ENUMERATE
  316. void Intrinsics::visit_edges(Visitor& visitor)
  317. {
  318. Base::visit_edges(visitor);
  319. visitor.visit(m_realm);
  320. visitor.visit(m_empty_object_shape);
  321. visitor.visit(m_new_object_shape);
  322. visitor.visit(m_iterator_result_object_shape);
  323. visitor.visit(m_proxy_constructor);
  324. visitor.visit(m_async_from_sync_iterator_prototype);
  325. visitor.visit(m_async_generator_prototype);
  326. visitor.visit(m_generator_prototype);
  327. visitor.visit(m_intl_segments_prototype);
  328. visitor.visit(m_wrap_for_valid_iterator_prototype);
  329. visitor.visit(m_eval_function);
  330. visitor.visit(m_is_finite_function);
  331. visitor.visit(m_is_nan_function);
  332. visitor.visit(m_parse_float_function);
  333. visitor.visit(m_parse_int_function);
  334. visitor.visit(m_decode_uri_function);
  335. visitor.visit(m_decode_uri_component_function);
  336. visitor.visit(m_encode_uri_function);
  337. visitor.visit(m_encode_uri_component_function);
  338. visitor.visit(m_escape_function);
  339. visitor.visit(m_unescape_function);
  340. visitor.visit(m_array_prototype_values_function);
  341. visitor.visit(m_date_constructor_now_function);
  342. visitor.visit(m_eval_function);
  343. visitor.visit(m_json_parse_function);
  344. visitor.visit(m_json_stringify_function);
  345. visitor.visit(m_object_prototype_to_string_function);
  346. visitor.visit(m_throw_type_error_function);
  347. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  348. visitor.visit(m_##snake_name##_constructor); \
  349. visitor.visit(m_##snake_name##_prototype);
  350. JS_ENUMERATE_BUILTIN_TYPES
  351. #undef __JS_ENUMERATE
  352. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  353. visitor.visit(m_intl_##snake_name##_constructor); \
  354. visitor.visit(m_intl_##snake_name##_prototype);
  355. JS_ENUMERATE_INTL_OBJECTS
  356. #undef __JS_ENUMERATE
  357. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  358. visitor.visit(m_temporal_##snake_name##_constructor); \
  359. visitor.visit(m_temporal_##snake_name##_prototype);
  360. JS_ENUMERATE_TEMPORAL_OBJECTS
  361. #undef __JS_ENUMERATE
  362. #define __JS_ENUMERATE(ClassName, snake_name) \
  363. visitor.visit(m_##snake_name##_object);
  364. JS_ENUMERATE_BUILTIN_NAMESPACE_OBJECTS
  365. #undef __JS_ENUMERATE
  366. #define __JS_ENUMERATE(ClassName, snake_name) \
  367. visitor.visit(m_##snake_name##_prototype);
  368. JS_ENUMERATE_ITERATOR_PROTOTYPES
  369. #undef __JS_ENUMERATE
  370. }
  371. // 10.2.4 AddRestrictedFunctionProperties ( F, realm ), https://tc39.es/ecma262/#sec-addrestrictedfunctionproperties
  372. void add_restricted_function_properties(FunctionObject& function, Realm& realm)
  373. {
  374. auto& vm = realm.vm();
  375. // 1. Assert: realm.[[Intrinsics]].[[%ThrowTypeError%]] exists and has been initialized.
  376. // NOTE: This is ensured by dereferencing the GCPtr in the getter.
  377. // 2. Let thrower be realm.[[Intrinsics]].[[%ThrowTypeError%]].
  378. auto thrower = realm.intrinsics().throw_type_error_function();
  379. // 3. Perform ! DefinePropertyOrThrow(F, "caller", PropertyDescriptor { [[Get]]: thrower, [[Set]]: thrower, [[Enumerable]]: false, [[Configurable]]: true }).
  380. function.define_direct_accessor(vm.names.caller, thrower, thrower, Attribute::Configurable);
  381. // 4. Perform ! DefinePropertyOrThrow(F, "arguments", PropertyDescriptor { [[Get]]: thrower, [[Set]]: thrower, [[Enumerable]]: false, [[Configurable]]: true }).
  382. function.define_direct_accessor(vm.names.arguments, thrower, thrower, Attribute::Configurable);
  383. // 5. Return unused.
  384. }
  385. }