GlobalObject.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <AK/LogStream.h>
  28. #include <LibJS/Console.h>
  29. #include <LibJS/Heap/DeferGC.h>
  30. #include <LibJS/Runtime/ArrayConstructor.h>
  31. #include <LibJS/Runtime/ArrayIteratorPrototype.h>
  32. #include <LibJS/Runtime/ArrayPrototype.h>
  33. #include <LibJS/Runtime/BigIntConstructor.h>
  34. #include <LibJS/Runtime/BigIntPrototype.h>
  35. #include <LibJS/Runtime/BooleanConstructor.h>
  36. #include <LibJS/Runtime/BooleanPrototype.h>
  37. #include <LibJS/Runtime/ConsoleObject.h>
  38. #include <LibJS/Runtime/DateConstructor.h>
  39. #include <LibJS/Runtime/DatePrototype.h>
  40. #include <LibJS/Runtime/ErrorConstructor.h>
  41. #include <LibJS/Runtime/ErrorPrototype.h>
  42. #include <LibJS/Runtime/FunctionConstructor.h>
  43. #include <LibJS/Runtime/FunctionPrototype.h>
  44. #include <LibJS/Runtime/GlobalObject.h>
  45. #include <LibJS/Runtime/IteratorPrototype.h>
  46. #include <LibJS/Runtime/JSONObject.h>
  47. #include <LibJS/Runtime/MathObject.h>
  48. #include <LibJS/Runtime/NativeFunction.h>
  49. #include <LibJS/Runtime/NumberConstructor.h>
  50. #include <LibJS/Runtime/NumberPrototype.h>
  51. #include <LibJS/Runtime/Object.h>
  52. #include <LibJS/Runtime/ObjectConstructor.h>
  53. #include <LibJS/Runtime/ObjectPrototype.h>
  54. #include <LibJS/Runtime/ProxyConstructor.h>
  55. #include <LibJS/Runtime/ProxyPrototype.h>
  56. #include <LibJS/Runtime/ReflectObject.h>
  57. #include <LibJS/Runtime/RegExpConstructor.h>
  58. #include <LibJS/Runtime/RegExpPrototype.h>
  59. #include <LibJS/Runtime/Shape.h>
  60. #include <LibJS/Runtime/StringConstructor.h>
  61. #include <LibJS/Runtime/StringIteratorPrototype.h>
  62. #include <LibJS/Runtime/StringPrototype.h>
  63. #include <LibJS/Runtime/SymbolConstructor.h>
  64. #include <LibJS/Runtime/SymbolPrototype.h>
  65. #include <LibJS/Runtime/Value.h>
  66. namespace JS {
  67. GlobalObject::GlobalObject()
  68. : Object(GlobalObjectTag::Tag)
  69. , m_console(make<Console>(*this))
  70. {
  71. }
  72. void GlobalObject::initialize()
  73. {
  74. ensure_shape_is_unique();
  75. // These are done first since other prototypes depend on their presence.
  76. m_empty_object_shape = heap().allocate<Shape>(*this, *this);
  77. m_object_prototype = heap().allocate_without_global_object<ObjectPrototype>(*this);
  78. m_function_prototype = heap().allocate_without_global_object<FunctionPrototype>(*this);
  79. static_cast<FunctionPrototype*>(m_function_prototype)->initialize(*this);
  80. static_cast<ObjectPrototype*>(m_object_prototype)->initialize(*this);
  81. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  82. if (!m_##snake_name##_prototype) \
  83. m_##snake_name##_prototype = heap().allocate<PrototypeName>(*this, *this);
  84. JS_ENUMERATE_BUILTIN_TYPES
  85. #undef __JS_ENUMERATE
  86. #define __JS_ENUMERATE(ClassName, snake_name) \
  87. if (!m_##snake_name##_prototype) \
  88. m_##snake_name##_prototype = heap().allocate<ClassName##Prototype>(*this, *this);
  89. JS_ENUMERATE_ITERATOR_PROTOTYPES
  90. #undef __JS_ENUMERATE
  91. u8 attr = Attribute::Writable | Attribute::Configurable;
  92. define_native_function("gc", gc, 0, attr);
  93. define_native_function("isNaN", is_nan, 1, attr);
  94. define_native_function("isFinite", is_finite, 1, attr);
  95. define_native_function("parseFloat", parse_float, 1, attr);
  96. define_property("NaN", js_nan(), 0);
  97. define_property("Infinity", js_infinity(), 0);
  98. define_property("undefined", js_undefined(), 0);
  99. define_property("globalThis", this, attr);
  100. define_property("console", heap().allocate<ConsoleObject>(*this, *this), attr);
  101. define_property("Math", heap().allocate<MathObject>(*this, *this), attr);
  102. define_property("JSON", heap().allocate<JSONObject>(*this, *this), attr);
  103. define_property("Reflect", heap().allocate<ReflectObject>(*this, *this), attr);
  104. add_constructor("Array", m_array_constructor, *m_array_prototype);
  105. add_constructor("BigInt", m_bigint_constructor, *m_bigint_prototype);
  106. add_constructor("Boolean", m_boolean_constructor, *m_boolean_prototype);
  107. add_constructor("Date", m_date_constructor, *m_date_prototype);
  108. add_constructor("Error", m_error_constructor, *m_error_prototype);
  109. add_constructor("Function", m_function_constructor, *m_function_prototype);
  110. add_constructor("Number", m_number_constructor, *m_number_prototype);
  111. add_constructor("Object", m_object_constructor, *m_object_prototype);
  112. add_constructor("Proxy", m_proxy_constructor, *m_proxy_prototype);
  113. add_constructor("RegExp", m_regexp_constructor, *m_regexp_prototype);
  114. add_constructor("String", m_string_constructor, *m_string_prototype);
  115. add_constructor("Symbol", m_symbol_constructor, *m_symbol_prototype);
  116. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  117. add_constructor(#ClassName, m_##snake_name##_constructor, *m_##snake_name##_prototype);
  118. JS_ENUMERATE_ERROR_SUBCLASSES
  119. #undef __JS_ENUMERATE
  120. }
  121. GlobalObject::~GlobalObject()
  122. {
  123. }
  124. void GlobalObject::visit_children(Visitor& visitor)
  125. {
  126. Object::visit_children(visitor);
  127. visitor.visit(m_empty_object_shape);
  128. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  129. visitor.visit(m_##snake_name##_constructor);
  130. JS_ENUMERATE_ERROR_SUBCLASSES
  131. #undef __JS_ENUMERATE
  132. #define __JS_ENUMERATE(ClassName, snake_name) \
  133. visitor.visit(m_##snake_name##_prototype);
  134. JS_ENUMERATE_ITERATOR_PROTOTYPES
  135. #undef __JS_ENUMERATE
  136. }
  137. JS_DEFINE_NATIVE_FUNCTION(GlobalObject::gc)
  138. {
  139. dbg() << "Forced garbage collection requested!";
  140. vm.heap().collect_garbage();
  141. return js_undefined();
  142. }
  143. JS_DEFINE_NATIVE_FUNCTION(GlobalObject::is_nan)
  144. {
  145. auto number = vm.argument(0).to_number(global_object);
  146. if (vm.exception())
  147. return {};
  148. return Value(number.is_nan());
  149. }
  150. JS_DEFINE_NATIVE_FUNCTION(GlobalObject::is_finite)
  151. {
  152. auto number = vm.argument(0).to_number(global_object);
  153. if (vm.exception())
  154. return {};
  155. return Value(number.is_finite_number());
  156. }
  157. JS_DEFINE_NATIVE_FUNCTION(GlobalObject::parse_float)
  158. {
  159. if (vm.argument(0).is_number())
  160. return vm.argument(0);
  161. auto string = vm.argument(0).to_string(global_object);
  162. if (vm.exception())
  163. return {};
  164. for (size_t length = string.length(); length > 0; --length) {
  165. // This can't throw, so no exception check is fine.
  166. auto number = Value(js_string(vm, string.substring(0, length))).to_number(global_object);
  167. if (!number.is_nan())
  168. return number;
  169. }
  170. return js_nan();
  171. }
  172. }