GlobalObject.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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/Utf8View.h>
  28. #include <LibJS/Console.h>
  29. #include <LibJS/Heap/DeferGC.h>
  30. #include <LibJS/Runtime/ArrayBufferConstructor.h>
  31. #include <LibJS/Runtime/ArrayBufferPrototype.h>
  32. #include <LibJS/Runtime/ArrayConstructor.h>
  33. #include <LibJS/Runtime/ArrayIteratorPrototype.h>
  34. #include <LibJS/Runtime/ArrayPrototype.h>
  35. #include <LibJS/Runtime/BigIntConstructor.h>
  36. #include <LibJS/Runtime/BigIntPrototype.h>
  37. #include <LibJS/Runtime/BooleanConstructor.h>
  38. #include <LibJS/Runtime/BooleanPrototype.h>
  39. #include <LibJS/Runtime/ConsoleObject.h>
  40. #include <LibJS/Runtime/DateConstructor.h>
  41. #include <LibJS/Runtime/DatePrototype.h>
  42. #include <LibJS/Runtime/ErrorConstructor.h>
  43. #include <LibJS/Runtime/ErrorPrototype.h>
  44. #include <LibJS/Runtime/FunctionConstructor.h>
  45. #include <LibJS/Runtime/FunctionPrototype.h>
  46. #include <LibJS/Runtime/GlobalObject.h>
  47. #include <LibJS/Runtime/IteratorPrototype.h>
  48. #include <LibJS/Runtime/JSONObject.h>
  49. #include <LibJS/Runtime/MathObject.h>
  50. #include <LibJS/Runtime/NativeFunction.h>
  51. #include <LibJS/Runtime/NumberConstructor.h>
  52. #include <LibJS/Runtime/NumberPrototype.h>
  53. #include <LibJS/Runtime/Object.h>
  54. #include <LibJS/Runtime/ObjectConstructor.h>
  55. #include <LibJS/Runtime/ObjectPrototype.h>
  56. #include <LibJS/Runtime/ProxyConstructor.h>
  57. #include <LibJS/Runtime/ReflectObject.h>
  58. #include <LibJS/Runtime/RegExpConstructor.h>
  59. #include <LibJS/Runtime/RegExpPrototype.h>
  60. #include <LibJS/Runtime/Shape.h>
  61. #include <LibJS/Runtime/StringConstructor.h>
  62. #include <LibJS/Runtime/StringIteratorPrototype.h>
  63. #include <LibJS/Runtime/StringPrototype.h>
  64. #include <LibJS/Runtime/SymbolConstructor.h>
  65. #include <LibJS/Runtime/SymbolPrototype.h>
  66. #include <LibJS/Runtime/TypedArray.h>
  67. #include <LibJS/Runtime/TypedArrayConstructor.h>
  68. #include <LibJS/Runtime/TypedArrayPrototype.h>
  69. #include <LibJS/Runtime/Value.h>
  70. #include <ctype.h>
  71. namespace JS {
  72. GlobalObject::GlobalObject()
  73. : ScopeObject(GlobalObjectTag::Tag)
  74. , m_console(make<Console>(*this))
  75. {
  76. }
  77. void GlobalObject::initialize()
  78. {
  79. auto& vm = this->vm();
  80. ensure_shape_is_unique();
  81. // These are done first since other prototypes depend on their presence.
  82. m_empty_object_shape = heap().allocate_without_global_object<Shape>(*this);
  83. m_object_prototype = heap().allocate_without_global_object<ObjectPrototype>(*this);
  84. m_function_prototype = heap().allocate_without_global_object<FunctionPrototype>(*this);
  85. m_new_object_shape = vm.heap().allocate_without_global_object<Shape>(*this);
  86. m_new_object_shape->set_prototype_without_transition(m_object_prototype);
  87. m_new_script_function_prototype_object_shape = vm.heap().allocate_without_global_object<Shape>(*this);
  88. m_new_script_function_prototype_object_shape->set_prototype_without_transition(m_object_prototype);
  89. m_new_script_function_prototype_object_shape->add_property_without_transition(vm.names.constructor, Attribute::Writable | Attribute::Configurable);
  90. static_cast<FunctionPrototype*>(m_function_prototype)->initialize(*this);
  91. static_cast<ObjectPrototype*>(m_object_prototype)->initialize(*this);
  92. set_prototype(m_object_prototype);
  93. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  94. if (!m_##snake_name##_prototype) \
  95. m_##snake_name##_prototype = heap().allocate<PrototypeName>(*this, *this);
  96. JS_ENUMERATE_BUILTIN_TYPES
  97. #undef __JS_ENUMERATE
  98. #define __JS_ENUMERATE(ClassName, snake_name) \
  99. if (!m_##snake_name##_prototype) \
  100. m_##snake_name##_prototype = heap().allocate<ClassName##Prototype>(*this, *this);
  101. JS_ENUMERATE_ITERATOR_PROTOTYPES
  102. #undef __JS_ENUMERATE
  103. u8 attr = Attribute::Writable | Attribute::Configurable;
  104. define_native_function(vm.names.gc, gc, 0, attr);
  105. define_native_function(vm.names.isNaN, is_nan, 1, attr);
  106. define_native_function(vm.names.isFinite, is_finite, 1, attr);
  107. define_native_function(vm.names.parseFloat, parse_float, 1, attr);
  108. define_native_function(vm.names.parseInt, parse_int, 1, attr);
  109. define_property(vm.names.NaN, js_nan(), 0);
  110. define_property(vm.names.Infinity, js_infinity(), 0);
  111. define_property(vm.names.undefined, js_undefined(), 0);
  112. define_property(vm.names.globalThis, this, attr);
  113. define_property(vm.names.console, heap().allocate<ConsoleObject>(*this, *this), attr);
  114. define_property(vm.names.Math, heap().allocate<MathObject>(*this, *this), attr);
  115. define_property(vm.names.JSON, heap().allocate<JSONObject>(*this, *this), attr);
  116. define_property(vm.names.Reflect, heap().allocate<ReflectObject>(*this, *this), attr);
  117. add_constructor(vm.names.Array, m_array_constructor, m_array_prototype);
  118. add_constructor(vm.names.ArrayBuffer, m_array_buffer_constructor, m_array_buffer_prototype);
  119. add_constructor(vm.names.BigInt, m_bigint_constructor, m_bigint_prototype);
  120. add_constructor(vm.names.Boolean, m_boolean_constructor, m_boolean_prototype);
  121. add_constructor(vm.names.Date, m_date_constructor, m_date_prototype);
  122. add_constructor(vm.names.Error, m_error_constructor, m_error_prototype);
  123. add_constructor(vm.names.Function, m_function_constructor, m_function_prototype);
  124. add_constructor(vm.names.Number, m_number_constructor, m_number_prototype);
  125. add_constructor(vm.names.Object, m_object_constructor, m_object_prototype);
  126. add_constructor(vm.names.Proxy, m_proxy_constructor, nullptr);
  127. add_constructor(vm.names.RegExp, m_regexp_constructor, m_regexp_prototype);
  128. add_constructor(vm.names.String, m_string_constructor, m_string_prototype);
  129. add_constructor(vm.names.Symbol, m_symbol_constructor, m_symbol_prototype);
  130. initialize_constructor(vm.names.TypedArray, m_typed_array_constructor, m_typed_array_prototype);
  131. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  132. add_constructor(vm.names.ClassName, m_##snake_name##_constructor, m_##snake_name##_prototype);
  133. JS_ENUMERATE_ERROR_SUBCLASSES
  134. JS_ENUMERATE_TYPED_ARRAYS
  135. #undef __JS_ENUMERATE
  136. }
  137. GlobalObject::~GlobalObject()
  138. {
  139. }
  140. void GlobalObject::visit_edges(Visitor& visitor)
  141. {
  142. Base::visit_edges(visitor);
  143. visitor.visit(m_empty_object_shape);
  144. visitor.visit(m_new_object_shape);
  145. visitor.visit(m_new_script_function_prototype_object_shape);
  146. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  147. visitor.visit(m_##snake_name##_constructor);
  148. JS_ENUMERATE_ERROR_SUBCLASSES
  149. #undef __JS_ENUMERATE
  150. #define __JS_ENUMERATE(ClassName, snake_name) \
  151. visitor.visit(m_##snake_name##_prototype);
  152. JS_ENUMERATE_ITERATOR_PROTOTYPES
  153. #undef __JS_ENUMERATE
  154. }
  155. JS_DEFINE_NATIVE_FUNCTION(GlobalObject::gc)
  156. {
  157. dbgln("Forced garbage collection requested!");
  158. vm.heap().collect_garbage();
  159. return js_undefined();
  160. }
  161. JS_DEFINE_NATIVE_FUNCTION(GlobalObject::is_nan)
  162. {
  163. auto number = vm.argument(0).to_number(global_object);
  164. if (vm.exception())
  165. return {};
  166. return Value(number.is_nan());
  167. }
  168. JS_DEFINE_NATIVE_FUNCTION(GlobalObject::is_finite)
  169. {
  170. auto number = vm.argument(0).to_number(global_object);
  171. if (vm.exception())
  172. return {};
  173. return Value(number.is_finite_number());
  174. }
  175. JS_DEFINE_NATIVE_FUNCTION(GlobalObject::parse_float)
  176. {
  177. if (vm.argument(0).is_number())
  178. return vm.argument(0);
  179. auto string = vm.argument(0).to_string(global_object);
  180. if (vm.exception())
  181. return {};
  182. for (size_t length = string.length(); length > 0; --length) {
  183. // This can't throw, so no exception check is fine.
  184. auto number = Value(js_string(vm, string.substring(0, length))).to_number(global_object);
  185. if (!number.is_nan())
  186. return number;
  187. }
  188. return js_nan();
  189. }
  190. JS_DEFINE_NATIVE_FUNCTION(GlobalObject::parse_int)
  191. {
  192. // 18.2.5 parseInt ( string, radix )
  193. auto input_string = vm.argument(0).to_string(global_object);
  194. if (vm.exception())
  195. return {};
  196. // FIXME: There's a bunch of unnecessary string copying here.
  197. double sign = 1;
  198. auto s = input_string.trim_whitespace(TrimMode::Left);
  199. if (!s.is_empty() && s[0] == '-')
  200. sign = -1;
  201. if (!s.is_empty() && (s[0] == '+' || s[0] == '-'))
  202. s = s.substring(1, s.length() - 1);
  203. auto radix = vm.argument(1).to_i32(global_object);
  204. if (vm.exception())
  205. return {};
  206. bool strip_prefix = true;
  207. if (radix != 0) {
  208. if (radix < 2 || radix > 36)
  209. return js_nan();
  210. if (radix != 16)
  211. strip_prefix = false;
  212. } else {
  213. radix = 10;
  214. }
  215. if (strip_prefix) {
  216. if (s.length() >= 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) {
  217. s = s.substring(2, s.length() - 2);
  218. radix = 16;
  219. }
  220. }
  221. auto parse_digit = [&](u32 codepoint, i32 radix) -> Optional<i32> {
  222. i32 digit = -1;
  223. if (isdigit(codepoint))
  224. digit = codepoint - '0';
  225. else if (islower(codepoint))
  226. digit = 10 + (codepoint - 'a');
  227. else if (isupper(codepoint))
  228. digit = 10 + (codepoint - 'A');
  229. if (digit == -1 || digit >= radix)
  230. return {};
  231. return digit;
  232. };
  233. bool had_digits = false;
  234. double number = 0;
  235. for (auto codepoint : Utf8View(s)) {
  236. auto digit = parse_digit(codepoint, radix);
  237. if (!digit.has_value())
  238. break;
  239. had_digits = true;
  240. number *= radix;
  241. number += digit.value();
  242. }
  243. if (!had_digits)
  244. return js_nan();
  245. return Value(sign * number);
  246. }
  247. Optional<Variable> GlobalObject::get_from_scope(const FlyString& name) const
  248. {
  249. auto value = get(name);
  250. if (value.is_empty())
  251. return {};
  252. return Variable { value, DeclarationKind::Var };
  253. }
  254. void GlobalObject::put_to_scope(const FlyString& name, Variable variable)
  255. {
  256. put(name, variable.value);
  257. }
  258. bool GlobalObject::has_this_binding() const
  259. {
  260. return true;
  261. }
  262. Value GlobalObject::get_this_binding(GlobalObject&) const
  263. {
  264. return Value(this);
  265. }
  266. }