Intrinsics.cpp 27 KB

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