Intrinsics.h 8.4 KB

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