Intrinsics.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibJS/Forward.h>
  8. #include <LibJS/Heap/Cell.h>
  9. namespace JS {
  10. class Intrinsics final : public Cell {
  11. GC_CELL(Intrinsics, Cell);
  12. GC_DECLARE_ALLOCATOR(Intrinsics);
  13. public:
  14. static ThrowCompletionOr<GC::Ref<Intrinsics>> create(Realm&);
  15. GC::Ref<Shape> empty_object_shape() { return *m_empty_object_shape; }
  16. GC::Ref<Shape> new_object_shape() { return *m_new_object_shape; }
  17. [[nodiscard]] GC::Ref<Shape> iterator_result_object_shape() { return *m_iterator_result_object_shape; }
  18. [[nodiscard]] u32 iterator_result_object_value_offset() { return m_iterator_result_object_value_offset; }
  19. [[nodiscard]] u32 iterator_result_object_done_offset() { return m_iterator_result_object_done_offset; }
  20. // Not included in JS_ENUMERATE_NATIVE_OBJECTS due to missing distinct prototype
  21. GC::Ref<ProxyConstructor> proxy_constructor() { return *m_proxy_constructor; }
  22. // Not included in JS_ENUMERATE_NATIVE_OBJECTS due to missing distinct constructor
  23. GC::Ref<Object> async_from_sync_iterator_prototype() { return *m_async_from_sync_iterator_prototype; }
  24. GC::Ref<Object> async_generator_prototype() { return *m_async_generator_prototype; }
  25. GC::Ref<Object> generator_prototype() { return *m_generator_prototype; }
  26. GC::Ref<Object> wrap_for_valid_iterator_prototype() { return *m_wrap_for_valid_iterator_prototype; }
  27. // Alias for the AsyncGenerator Prototype Object used by the spec (%AsyncGeneratorFunction.prototype.prototype%)
  28. GC::Ref<Object> async_generator_function_prototype_prototype() { return *m_async_generator_prototype; }
  29. // Alias for the Generator Prototype Object used by the spec (%GeneratorFunction.prototype.prototype%)
  30. GC::Ref<Object> generator_function_prototype_prototype() { return *m_generator_prototype; }
  31. // Not included in JS_ENUMERATE_INTL_OBJECTS due to missing distinct constructor
  32. GC::Ref<Object> intl_segments_prototype() { return *m_intl_segments_prototype; }
  33. // Global object functions
  34. GC::Ref<FunctionObject> eval_function() const { return *m_eval_function; }
  35. GC::Ref<FunctionObject> is_finite_function() const { return *m_is_finite_function; }
  36. GC::Ref<FunctionObject> is_nan_function() const { return *m_is_nan_function; }
  37. GC::Ref<FunctionObject> parse_float_function() const { return *m_parse_float_function; }
  38. GC::Ref<FunctionObject> parse_int_function() const { return *m_parse_int_function; }
  39. GC::Ref<FunctionObject> decode_uri_function() const { return *m_decode_uri_function; }
  40. GC::Ref<FunctionObject> decode_uri_component_function() const { return *m_decode_uri_component_function; }
  41. GC::Ref<FunctionObject> encode_uri_function() const { return *m_encode_uri_function; }
  42. GC::Ref<FunctionObject> encode_uri_component_function() const { return *m_encode_uri_component_function; }
  43. GC::Ref<FunctionObject> escape_function() const { return *m_escape_function; }
  44. GC::Ref<FunctionObject> unescape_function() const { return *m_unescape_function; }
  45. // Namespace/constructor object functions
  46. GC::Ref<FunctionObject> array_prototype_values_function() const { return *m_array_prototype_values_function; }
  47. GC::Ref<FunctionObject> date_constructor_now_function() const { return *m_date_constructor_now_function; }
  48. GC::Ref<FunctionObject> json_parse_function() const { return *m_json_parse_function; }
  49. GC::Ref<FunctionObject> json_stringify_function() const { return *m_json_stringify_function; }
  50. GC::Ref<FunctionObject> object_prototype_to_string_function() const { return *m_object_prototype_to_string_function; }
  51. GC::Ref<FunctionObject> throw_type_error_function() const { return *m_throw_type_error_function; }
  52. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  53. GC::Ref<ConstructorName> snake_name##_constructor(); \
  54. GC::Ref<Object> snake_name##_prototype();
  55. JS_ENUMERATE_BUILTIN_TYPES
  56. #undef __JS_ENUMERATE
  57. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  58. GC::Ref<Intl::ConstructorName> intl_##snake_name##_constructor(); \
  59. GC::Ref<Object> intl_##snake_name##_prototype();
  60. JS_ENUMERATE_INTL_OBJECTS
  61. #undef __JS_ENUMERATE
  62. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  63. GC::Ref<Temporal::ConstructorName> temporal_##snake_name##_constructor(); \
  64. GC::Ref<Object> temporal_##snake_name##_prototype();
  65. JS_ENUMERATE_TEMPORAL_OBJECTS
  66. #undef __JS_ENUMERATE
  67. #define __JS_ENUMERATE(ClassName, snake_name) \
  68. GC::Ref<ClassName> snake_name##_object();
  69. JS_ENUMERATE_BUILTIN_NAMESPACE_OBJECTS
  70. #undef __JS_ENUMERATE
  71. #define __JS_ENUMERATE(ClassName, snake_name) \
  72. GC::Ref<Object> snake_name##_prototype() \
  73. { \
  74. return *m_##snake_name##_prototype; \
  75. }
  76. JS_ENUMERATE_ITERATOR_PROTOTYPES
  77. #undef __JS_ENUMERATE
  78. private:
  79. Intrinsics(Realm& realm)
  80. : m_realm(realm)
  81. {
  82. }
  83. virtual void visit_edges(Visitor&) override;
  84. ThrowCompletionOr<void> initialize_intrinsics(Realm&);
  85. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  86. void initialize_##snake_name();
  87. JS_ENUMERATE_BUILTIN_TYPES
  88. #undef __JS_ENUMERATE
  89. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  90. void initialize_intl_##snake_name();
  91. JS_ENUMERATE_INTL_OBJECTS
  92. #undef __JS_ENUMERATE
  93. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  94. void initialize_temporal_##snake_name();
  95. JS_ENUMERATE_TEMPORAL_OBJECTS
  96. #undef __JS_ENUMERATE
  97. GC::Ref<Realm> m_realm;
  98. GC::Ptr<Shape> m_empty_object_shape;
  99. GC::Ptr<Shape> m_new_object_shape;
  100. GC::Ptr<Shape> m_iterator_result_object_shape;
  101. u32 m_iterator_result_object_value_offset { 0 };
  102. u32 m_iterator_result_object_done_offset { 0 };
  103. // Not included in JS_ENUMERATE_NATIVE_OBJECTS due to missing distinct prototype
  104. GC::Ptr<ProxyConstructor> m_proxy_constructor;
  105. // Not included in JS_ENUMERATE_NATIVE_OBJECTS due to missing distinct constructor
  106. GC::Ptr<Object> m_async_from_sync_iterator_prototype;
  107. GC::Ptr<Object> m_async_generator_prototype;
  108. GC::Ptr<Object> m_generator_prototype;
  109. GC::Ptr<Object> m_wrap_for_valid_iterator_prototype;
  110. // Not included in JS_ENUMERATE_INTL_OBJECTS due to missing distinct constructor
  111. GC::Ptr<Object> m_intl_segments_prototype;
  112. // Global object functions
  113. GC::Ptr<FunctionObject> m_eval_function;
  114. GC::Ptr<FunctionObject> m_is_finite_function;
  115. GC::Ptr<FunctionObject> m_is_nan_function;
  116. GC::Ptr<FunctionObject> m_parse_float_function;
  117. GC::Ptr<FunctionObject> m_parse_int_function;
  118. GC::Ptr<FunctionObject> m_decode_uri_function;
  119. GC::Ptr<FunctionObject> m_decode_uri_component_function;
  120. GC::Ptr<FunctionObject> m_encode_uri_function;
  121. GC::Ptr<FunctionObject> m_encode_uri_component_function;
  122. GC::Ptr<FunctionObject> m_escape_function;
  123. GC::Ptr<FunctionObject> m_unescape_function;
  124. // Namespace/constructor object functions
  125. GC::Ptr<FunctionObject> m_array_prototype_values_function;
  126. GC::Ptr<FunctionObject> m_date_constructor_now_function;
  127. GC::Ptr<FunctionObject> m_json_parse_function;
  128. GC::Ptr<FunctionObject> m_json_stringify_function;
  129. GC::Ptr<FunctionObject> m_object_prototype_to_string_function;
  130. GC::Ptr<FunctionObject> m_throw_type_error_function;
  131. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  132. GC::Ptr<ConstructorName> m_##snake_name##_constructor; \
  133. GC::Ptr<Object> m_##snake_name##_prototype;
  134. JS_ENUMERATE_BUILTIN_TYPES
  135. #undef __JS_ENUMERATE
  136. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  137. GC::Ptr<Intl::ConstructorName> m_intl_##snake_name##_constructor; \
  138. GC::Ptr<Object> m_intl_##snake_name##_prototype;
  139. JS_ENUMERATE_INTL_OBJECTS
  140. #undef __JS_ENUMERATE
  141. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  142. GC::Ptr<Temporal::ConstructorName> m_temporal_##snake_name##_constructor; \
  143. GC::Ptr<Object> m_temporal_##snake_name##_prototype;
  144. JS_ENUMERATE_TEMPORAL_OBJECTS
  145. #undef __JS_ENUMERATE
  146. #define __JS_ENUMERATE(ClassName, snake_name) \
  147. GC::Ptr<ClassName> m_##snake_name##_object;
  148. JS_ENUMERATE_BUILTIN_NAMESPACE_OBJECTS
  149. #undef __JS_ENUMERATE
  150. #define __JS_ENUMERATE(ClassName, snake_name) \
  151. GC::Ptr<Object> m_##snake_name##_prototype;
  152. JS_ENUMERATE_ITERATOR_PROTOTYPES
  153. #undef __JS_ENUMERATE
  154. };
  155. void add_restricted_function_properties(FunctionObject&, Realm&);
  156. }