Intrinsics.h 8.3 KB

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