GlobalObject.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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/ReflectObject.h>
  56. #include <LibJS/Runtime/RegExpConstructor.h>
  57. #include <LibJS/Runtime/RegExpPrototype.h>
  58. #include <LibJS/Runtime/Shape.h>
  59. #include <LibJS/Runtime/StringConstructor.h>
  60. #include <LibJS/Runtime/StringIteratorPrototype.h>
  61. #include <LibJS/Runtime/StringPrototype.h>
  62. #include <LibJS/Runtime/SymbolConstructor.h>
  63. #include <LibJS/Runtime/SymbolPrototype.h>
  64. #include <LibJS/Runtime/TypedArray.h>
  65. #include <LibJS/Runtime/TypedArrayConstructor.h>
  66. #include <LibJS/Runtime/TypedArrayPrototype.h>
  67. #include <LibJS/Runtime/Value.h>
  68. namespace JS {
  69. GlobalObject::GlobalObject()
  70. : ScopeObject(GlobalObjectTag::Tag)
  71. , m_console(make<Console>(*this))
  72. {
  73. }
  74. void GlobalObject::initialize()
  75. {
  76. auto& vm = this->vm();
  77. ensure_shape_is_unique();
  78. // These are done first since other prototypes depend on their presence.
  79. m_empty_object_shape = heap().allocate_without_global_object<Shape>(*this);
  80. m_object_prototype = heap().allocate_without_global_object<ObjectPrototype>(*this);
  81. m_function_prototype = heap().allocate_without_global_object<FunctionPrototype>(*this);
  82. m_new_object_shape = vm.heap().allocate_without_global_object<Shape>(*this);
  83. m_new_object_shape->set_prototype_without_transition(m_object_prototype);
  84. m_new_script_function_prototype_object_shape = vm.heap().allocate_without_global_object<Shape>(*this);
  85. m_new_script_function_prototype_object_shape->set_prototype_without_transition(m_object_prototype);
  86. m_new_script_function_prototype_object_shape->add_property_without_transition(vm.names.constructor, Attribute::Writable | Attribute::Configurable);
  87. static_cast<FunctionPrototype*>(m_function_prototype)->initialize(*this);
  88. static_cast<ObjectPrototype*>(m_object_prototype)->initialize(*this);
  89. set_prototype(m_object_prototype);
  90. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  91. if (!m_##snake_name##_prototype) \
  92. m_##snake_name##_prototype = heap().allocate<PrototypeName>(*this, *this);
  93. JS_ENUMERATE_BUILTIN_TYPES
  94. #undef __JS_ENUMERATE
  95. #define __JS_ENUMERATE(ClassName, snake_name) \
  96. if (!m_##snake_name##_prototype) \
  97. m_##snake_name##_prototype = heap().allocate<ClassName##Prototype>(*this, *this);
  98. JS_ENUMERATE_ITERATOR_PROTOTYPES
  99. #undef __JS_ENUMERATE
  100. u8 attr = Attribute::Writable | Attribute::Configurable;
  101. define_native_function(vm.names.gc, gc, 0, attr);
  102. define_native_function(vm.names.isNaN, is_nan, 1, attr);
  103. define_native_function(vm.names.isFinite, is_finite, 1, attr);
  104. define_native_function(vm.names.parseFloat, parse_float, 1, attr);
  105. define_property(vm.names.NaN, js_nan(), 0);
  106. define_property(vm.names.Infinity, js_infinity(), 0);
  107. define_property(vm.names.undefined, js_undefined(), 0);
  108. define_property(vm.names.globalThis, this, attr);
  109. define_property(vm.names.console, heap().allocate<ConsoleObject>(*this, *this), attr);
  110. define_property(vm.names.Math, heap().allocate<MathObject>(*this, *this), attr);
  111. define_property(vm.names.JSON, heap().allocate<JSONObject>(*this, *this), attr);
  112. define_property(vm.names.Reflect, heap().allocate<ReflectObject>(*this, *this), attr);
  113. add_constructor(vm.names.Array, m_array_constructor, m_array_prototype);
  114. add_constructor(vm.names.BigInt, m_bigint_constructor, m_bigint_prototype);
  115. add_constructor(vm.names.Boolean, m_boolean_constructor, m_boolean_prototype);
  116. add_constructor(vm.names.Date, m_date_constructor, m_date_prototype);
  117. add_constructor(vm.names.Error, m_error_constructor, m_error_prototype);
  118. add_constructor(vm.names.Function, m_function_constructor, m_function_prototype);
  119. add_constructor(vm.names.Number, m_number_constructor, m_number_prototype);
  120. add_constructor(vm.names.Object, m_object_constructor, m_object_prototype);
  121. add_constructor(vm.names.Proxy, m_proxy_constructor, nullptr);
  122. add_constructor(vm.names.RegExp, m_regexp_constructor, m_regexp_prototype);
  123. add_constructor(vm.names.String, m_string_constructor, m_string_prototype);
  124. add_constructor(vm.names.Symbol, m_symbol_constructor, m_symbol_prototype);
  125. initialize_constructor(vm.names.TypedArray, m_typed_array_constructor, m_typed_array_prototype);
  126. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  127. add_constructor(vm.names.ClassName, m_##snake_name##_constructor, m_##snake_name##_prototype);
  128. JS_ENUMERATE_ERROR_SUBCLASSES
  129. JS_ENUMERATE_TYPED_ARRAYS
  130. #undef __JS_ENUMERATE
  131. }
  132. GlobalObject::~GlobalObject()
  133. {
  134. }
  135. void GlobalObject::visit_edges(Visitor& visitor)
  136. {
  137. Base::visit_edges(visitor);
  138. visitor.visit(m_empty_object_shape);
  139. visitor.visit(m_new_object_shape);
  140. visitor.visit(m_new_script_function_prototype_object_shape);
  141. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  142. visitor.visit(m_##snake_name##_constructor);
  143. JS_ENUMERATE_ERROR_SUBCLASSES
  144. #undef __JS_ENUMERATE
  145. #define __JS_ENUMERATE(ClassName, snake_name) \
  146. visitor.visit(m_##snake_name##_prototype);
  147. JS_ENUMERATE_ITERATOR_PROTOTYPES
  148. #undef __JS_ENUMERATE
  149. }
  150. JS_DEFINE_NATIVE_FUNCTION(GlobalObject::gc)
  151. {
  152. dbg() << "Forced garbage collection requested!";
  153. vm.heap().collect_garbage();
  154. return js_undefined();
  155. }
  156. JS_DEFINE_NATIVE_FUNCTION(GlobalObject::is_nan)
  157. {
  158. auto number = vm.argument(0).to_number(global_object);
  159. if (vm.exception())
  160. return {};
  161. return Value(number.is_nan());
  162. }
  163. JS_DEFINE_NATIVE_FUNCTION(GlobalObject::is_finite)
  164. {
  165. auto number = vm.argument(0).to_number(global_object);
  166. if (vm.exception())
  167. return {};
  168. return Value(number.is_finite_number());
  169. }
  170. JS_DEFINE_NATIVE_FUNCTION(GlobalObject::parse_float)
  171. {
  172. if (vm.argument(0).is_number())
  173. return vm.argument(0);
  174. auto string = vm.argument(0).to_string(global_object);
  175. if (vm.exception())
  176. return {};
  177. for (size_t length = string.length(); length > 0; --length) {
  178. // This can't throw, so no exception check is fine.
  179. auto number = Value(js_string(vm, string.substring(0, length))).to_number(global_object);
  180. if (!number.is_nan())
  181. return number;
  182. }
  183. return js_nan();
  184. }
  185. Optional<Variable> GlobalObject::get_from_scope(const FlyString& name) const
  186. {
  187. auto value = get(name);
  188. if (value.is_empty())
  189. return {};
  190. return Variable { value, DeclarationKind::Var };
  191. }
  192. void GlobalObject::put_to_scope(const FlyString& name, Variable variable)
  193. {
  194. put(name, variable.value);
  195. }
  196. bool GlobalObject::has_this_binding() const
  197. {
  198. return true;
  199. }
  200. Value GlobalObject::get_this_binding(GlobalObject&) const
  201. {
  202. return Value(this);
  203. }
  204. }