Intrinsics.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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/IteratorPrototype.h>
  67. #include <LibJS/Runtime/JSONObject.h>
  68. #include <LibJS/Runtime/MapConstructor.h>
  69. #include <LibJS/Runtime/MapIteratorPrototype.h>
  70. #include <LibJS/Runtime/MapPrototype.h>
  71. #include <LibJS/Runtime/MathObject.h>
  72. #include <LibJS/Runtime/NativeFunction.h>
  73. #include <LibJS/Runtime/NumberConstructor.h>
  74. #include <LibJS/Runtime/NumberPrototype.h>
  75. #include <LibJS/Runtime/ObjectConstructor.h>
  76. #include <LibJS/Runtime/ObjectPrototype.h>
  77. #include <LibJS/Runtime/PromiseConstructor.h>
  78. #include <LibJS/Runtime/PromisePrototype.h>
  79. #include <LibJS/Runtime/ProxyConstructor.h>
  80. #include <LibJS/Runtime/Realm.h>
  81. #include <LibJS/Runtime/ReflectObject.h>
  82. #include <LibJS/Runtime/RegExpConstructor.h>
  83. #include <LibJS/Runtime/RegExpPrototype.h>
  84. #include <LibJS/Runtime/RegExpStringIteratorPrototype.h>
  85. #include <LibJS/Runtime/SetConstructor.h>
  86. #include <LibJS/Runtime/SetIteratorPrototype.h>
  87. #include <LibJS/Runtime/SetPrototype.h>
  88. #include <LibJS/Runtime/ShadowRealmConstructor.h>
  89. #include <LibJS/Runtime/ShadowRealmPrototype.h>
  90. #include <LibJS/Runtime/Shape.h>
  91. #include <LibJS/Runtime/StringConstructor.h>
  92. #include <LibJS/Runtime/StringIteratorPrototype.h>
  93. #include <LibJS/Runtime/StringPrototype.h>
  94. #include <LibJS/Runtime/SuppressedErrorConstructor.h>
  95. #include <LibJS/Runtime/SuppressedErrorPrototype.h>
  96. #include <LibJS/Runtime/SymbolConstructor.h>
  97. #include <LibJS/Runtime/SymbolPrototype.h>
  98. #include <LibJS/Runtime/Temporal/CalendarConstructor.h>
  99. #include <LibJS/Runtime/Temporal/CalendarPrototype.h>
  100. #include <LibJS/Runtime/Temporal/DurationConstructor.h>
  101. #include <LibJS/Runtime/Temporal/DurationPrototype.h>
  102. #include <LibJS/Runtime/Temporal/InstantConstructor.h>
  103. #include <LibJS/Runtime/Temporal/InstantPrototype.h>
  104. #include <LibJS/Runtime/Temporal/PlainDateConstructor.h>
  105. #include <LibJS/Runtime/Temporal/PlainDatePrototype.h>
  106. #include <LibJS/Runtime/Temporal/PlainDateTimeConstructor.h>
  107. #include <LibJS/Runtime/Temporal/PlainDateTimePrototype.h>
  108. #include <LibJS/Runtime/Temporal/PlainMonthDayConstructor.h>
  109. #include <LibJS/Runtime/Temporal/PlainMonthDayPrototype.h>
  110. #include <LibJS/Runtime/Temporal/PlainTimeConstructor.h>
  111. #include <LibJS/Runtime/Temporal/PlainTimePrototype.h>
  112. #include <LibJS/Runtime/Temporal/PlainYearMonthConstructor.h>
  113. #include <LibJS/Runtime/Temporal/PlainYearMonthPrototype.h>
  114. #include <LibJS/Runtime/Temporal/Temporal.h>
  115. #include <LibJS/Runtime/Temporal/TimeZoneConstructor.h>
  116. #include <LibJS/Runtime/Temporal/TimeZonePrototype.h>
  117. #include <LibJS/Runtime/Temporal/ZonedDateTimeConstructor.h>
  118. #include <LibJS/Runtime/Temporal/ZonedDateTimePrototype.h>
  119. #include <LibJS/Runtime/TypedArray.h>
  120. #include <LibJS/Runtime/TypedArrayConstructor.h>
  121. #include <LibJS/Runtime/TypedArrayPrototype.h>
  122. #include <LibJS/Runtime/WeakMapConstructor.h>
  123. #include <LibJS/Runtime/WeakMapPrototype.h>
  124. #include <LibJS/Runtime/WeakRefConstructor.h>
  125. #include <LibJS/Runtime/WeakRefPrototype.h>
  126. #include <LibJS/Runtime/WeakSetConstructor.h>
  127. #include <LibJS/Runtime/WeakSetPrototype.h>
  128. #include <LibJS/Runtime/WrapForValidIteratorPrototype.h>
  129. namespace JS {
  130. static void initialize_constructor(VM& vm, PropertyKey const& property_key, Object& constructor, Object* prototype, PropertyAttributes constructor_property_attributes = Attribute::Writable | Attribute::Configurable)
  131. {
  132. constructor.define_direct_property(vm.names.name, PrimitiveString::create(vm, property_key.as_string()), Attribute::Configurable);
  133. if (prototype)
  134. prototype->define_direct_property(vm.names.constructor, &constructor, constructor_property_attributes);
  135. }
  136. // 9.3.2 CreateIntrinsics ( realmRec ), https://tc39.es/ecma262/#sec-createintrinsics
  137. ThrowCompletionOr<NonnullGCPtr<Intrinsics>> Intrinsics::create(Realm& realm)
  138. {
  139. auto& vm = realm.vm();
  140. // 1. Set realmRec.[[Intrinsics]] to a new Record.
  141. auto intrinsics = vm.heap().allocate_without_realm<Intrinsics>(realm);
  142. realm.set_intrinsics({}, intrinsics);
  143. // 2. Set fields of realmRec.[[Intrinsics]] with the values listed in Table 6.
  144. // The field names are the names listed in column one of the table.
  145. // The value of each field is a new object value fully and recursively populated
  146. // with property values as defined by the specification of each object in
  147. // clauses 19 through 28. All object property values are newly created object
  148. // values. All values that are built-in function objects are created by performing
  149. // CreateBuiltinFunction(steps, length, name, slots, realmRec, prototype)
  150. // where steps is the definition of that function provided by this specification,
  151. // name is the initial value of the function's "name" property, length is the
  152. // initial value of the function's "length" property, slots is a list of the
  153. // names, if any, of the function's specified internal slots, and prototype
  154. // is the specified value of the function's [[Prototype]] internal slot. The
  155. // creation of the intrinsics and their properties must be ordered to avoid
  156. // any dependencies upon objects that have not yet been created.
  157. MUST_OR_THROW_OOM(intrinsics->initialize_intrinsics(realm));
  158. // 3. Perform AddRestrictedFunctionProperties(realmRec.[[Intrinsics]].[[%Function.prototype%]], realmRec).
  159. add_restricted_function_properties(static_cast<FunctionObject&>(*realm.intrinsics().function_prototype()), realm);
  160. // 4. Return unused.
  161. return *intrinsics;
  162. }
  163. ThrowCompletionOr<void> Intrinsics::initialize_intrinsics(Realm& realm)
  164. {
  165. auto& vm = this->vm();
  166. // These are done first since other prototypes depend on their presence.
  167. m_empty_object_shape = heap().allocate_without_realm<Shape>(realm);
  168. m_object_prototype = heap().allocate_without_realm<ObjectPrototype>(realm);
  169. m_function_prototype = heap().allocate_without_realm<FunctionPrototype>(realm);
  170. m_new_object_shape = heap().allocate_without_realm<Shape>(realm);
  171. m_new_object_shape->set_prototype_without_transition(m_object_prototype);
  172. m_new_ordinary_function_prototype_object_shape = heap().allocate_without_realm<Shape>(realm);
  173. m_new_ordinary_function_prototype_object_shape->set_prototype_without_transition(m_object_prototype);
  174. m_new_ordinary_function_prototype_object_shape->add_property_without_transition(vm.names.constructor, Attribute::Writable | Attribute::Configurable);
  175. // Normally Heap::allocate() takes care of this, but these are allocated via allocate_without_realm().
  176. MUST_OR_THROW_OOM(m_function_prototype->initialize(realm));
  177. MUST_OR_THROW_OOM(m_object_prototype->initialize(realm));
  178. #define __JS_ENUMERATE(ClassName, snake_name) \
  179. VERIFY(!m_##snake_name##_prototype); \
  180. m_##snake_name##_prototype = heap().allocate<ClassName##Prototype>(realm, realm).release_allocated_value_but_fixme_should_propagate_errors();
  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 = heap().allocate<AsyncFromSyncIteratorPrototype>(realm, realm).release_allocated_value_but_fixme_should_propagate_errors();
  185. m_async_generator_prototype = heap().allocate<AsyncGeneratorPrototype>(realm, realm).release_allocated_value_but_fixme_should_propagate_errors();
  186. m_generator_prototype = heap().allocate<GeneratorPrototype>(realm, realm).release_allocated_value_but_fixme_should_propagate_errors();
  187. m_intl_segments_prototype = heap().allocate<Intl::SegmentsPrototype>(realm, realm).release_allocated_value_but_fixme_should_propagate_errors();
  188. m_wrap_for_valid_iterator_prototype = heap().allocate<WrapForValidIteratorPrototype>(realm, realm).release_allocated_value_but_fixme_should_propagate_errors();
  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 = heap().allocate<ErrorPrototype>(realm, realm).release_allocated_value_but_fixme_should_propagate_errors();
  194. m_error_constructor = heap().allocate<ErrorConstructor>(realm, realm).release_allocated_value_but_fixme_should_propagate_errors();
  195. m_function_constructor = heap().allocate<FunctionConstructor>(realm, realm).release_allocated_value_but_fixme_should_propagate_errors();
  196. // Not included in JS_ENUMERATE_NATIVE_OBJECTS due to missing distinct prototype
  197. m_proxy_constructor = heap().allocate<ProxyConstructor>(realm, realm).release_allocated_value_but_fixme_should_propagate_errors();
  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 = heap().allocate<ObjectConstructor>(realm, realm).release_allocated_value_but_fixme_should_propagate_errors();
  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 = heap().allocate<Namespace::PrototypeName>(m_realm, *typed_array_prototype()).release_allocated_value_but_fixme_should_propagate_errors(); \
  254. 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(); \
  255. } else { \
  256. m_##snake_namespace##snake_name##_prototype = heap().allocate<Namespace::PrototypeName>(m_realm, m_realm).release_allocated_value_but_fixme_should_propagate_errors(); \
  257. m_##snake_namespace##snake_name##_constructor = heap().allocate<Namespace::ConstructorName>(m_realm, m_realm).release_allocated_value_but_fixme_should_propagate_errors(); \
  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, 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. NonnullGCPtr<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. NonnullGCPtr<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. NonnullGCPtr<ClassName> Intrinsics::snake_name##_object() \
  307. { \
  308. if (!m_##snake_name##_object) \
  309. m_##snake_name##_object = heap().allocate<ClassName>(m_realm, m_realm).release_allocated_value_but_fixme_should_propagate_errors(); \
  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_new_ordinary_function_prototype_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. }