Intrinsics.h 8.9 KB

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