GlobalObject.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 <AK/String.h>
  29. #include <LibJS/Heap/Heap.h>
  30. #include <LibJS/Interpreter.h>
  31. #include <LibJS/Runtime/ArrayConstructor.h>
  32. #include <LibJS/Runtime/ArrayPrototype.h>
  33. #include <LibJS/Runtime/BooleanConstructor.h>
  34. #include <LibJS/Runtime/BooleanPrototype.h>
  35. #include <LibJS/Runtime/ConsoleObject.h>
  36. #include <LibJS/Runtime/DateConstructor.h>
  37. #include <LibJS/Runtime/DatePrototype.h>
  38. #include <LibJS/Runtime/Error.h>
  39. #include <LibJS/Runtime/ErrorConstructor.h>
  40. #include <LibJS/Runtime/ErrorPrototype.h>
  41. #include <LibJS/Runtime/FunctionConstructor.h>
  42. #include <LibJS/Runtime/FunctionPrototype.h>
  43. #include <LibJS/Runtime/GlobalObject.h>
  44. #include <LibJS/Runtime/LexicalEnvironment.h>
  45. #include <LibJS/Runtime/MathObject.h>
  46. #include <LibJS/Runtime/NativeFunction.h>
  47. #include <LibJS/Runtime/NumberConstructor.h>
  48. #include <LibJS/Runtime/NumberPrototype.h>
  49. #include <LibJS/Runtime/Object.h>
  50. #include <LibJS/Runtime/ObjectConstructor.h>
  51. #include <LibJS/Runtime/ObjectPrototype.h>
  52. #include <LibJS/Runtime/ReflectObject.h>
  53. #include <LibJS/Runtime/Shape.h>
  54. #include <LibJS/Runtime/StringConstructor.h>
  55. #include <LibJS/Runtime/StringPrototype.h>
  56. #include <LibJS/Runtime/SymbolConstructor.h>
  57. #include <LibJS/Runtime/SymbolPrototype.h>
  58. #include <LibJS/Runtime/Value.h>
  59. namespace JS {
  60. GlobalObject::GlobalObject()
  61. : Object(nullptr)
  62. {
  63. }
  64. void GlobalObject::initialize()
  65. {
  66. // These are done first since other prototypes depend on their presence.
  67. m_empty_object_shape = heap().allocate<Shape>();
  68. m_object_prototype = heap().allocate<ObjectPrototype>();
  69. m_function_prototype = heap().allocate<FunctionPrototype>();
  70. static_cast<FunctionPrototype*>(m_function_prototype)->initialize();
  71. static_cast<ObjectPrototype*>(m_object_prototype)->initialize();
  72. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  73. if (!m_##snake_name##_prototype) \
  74. m_##snake_name##_prototype = heap().allocate<PrototypeName>();
  75. JS_ENUMERATE_BUILTIN_TYPES
  76. #undef __JS_ENUMERATE
  77. u8 attr = Attribute::Writable | Attribute::Configurable;
  78. put_native_function("gc", gc, 0, attr);
  79. put_native_function("isNaN", is_nan, 1, attr);
  80. put_native_function("isFinite", is_finite, 1, attr);
  81. put_native_function("parseFloat", parse_float, 1, attr);
  82. put("NaN", js_nan(), 0);
  83. put("Infinity", js_infinity(), 0);
  84. put("undefined", js_undefined(), 0);
  85. put("globalThis", this, attr);
  86. put("console", heap().allocate<ConsoleObject>(), attr);
  87. put("Math", heap().allocate<MathObject>(), attr);
  88. put("Reflect", heap().allocate<ReflectObject>(), attr);
  89. add_constructor("Array", m_array_constructor, *m_array_prototype);
  90. add_constructor("Boolean", m_boolean_constructor, *m_boolean_prototype);
  91. add_constructor("Date", m_date_constructor, *m_date_prototype);
  92. add_constructor("Error", m_error_constructor, *m_error_prototype);
  93. add_constructor("Function", m_function_constructor, *m_function_prototype);
  94. add_constructor("Number", m_number_constructor, *m_number_prototype);
  95. add_constructor("Object", m_object_constructor, *m_object_prototype);
  96. add_constructor("String", m_string_constructor, *m_string_prototype);
  97. add_constructor("Symbol", m_symbol_constructor, *m_symbol_prototype);
  98. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  99. add_constructor(#ClassName, m_##snake_name##_constructor, *m_##snake_name##_prototype);
  100. JS_ENUMERATE_ERROR_SUBCLASSES
  101. #undef __JS_ENUMERATE
  102. }
  103. GlobalObject::~GlobalObject()
  104. {
  105. }
  106. void GlobalObject::visit_children(Visitor& visitor)
  107. {
  108. Object::visit_children(visitor);
  109. visitor.visit(m_empty_object_shape);
  110. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  111. visitor.visit(m_##snake_name##_constructor);
  112. JS_ENUMERATE_ERROR_SUBCLASSES
  113. #undef __JS_ENUMERATE
  114. }
  115. Value GlobalObject::gc(Interpreter& interpreter)
  116. {
  117. dbg() << "Forced garbage collection requested!";
  118. interpreter.heap().collect_garbage();
  119. return js_undefined();
  120. }
  121. Value GlobalObject::is_nan(Interpreter& interpreter)
  122. {
  123. auto number = interpreter.argument(0).to_number(interpreter);
  124. if (interpreter.exception())
  125. return {};
  126. return Value(number.is_nan());
  127. }
  128. Value GlobalObject::is_finite(Interpreter& interpreter)
  129. {
  130. auto number = interpreter.argument(0).to_number(interpreter);
  131. if (interpreter.exception())
  132. return {};
  133. return Value(number.is_finite_number());
  134. }
  135. Value GlobalObject::parse_float(Interpreter& interpreter)
  136. {
  137. if (interpreter.argument(0).is_number())
  138. return interpreter.argument(0);
  139. auto string = interpreter.argument(0).to_string(interpreter);
  140. if (interpreter.exception())
  141. return {};
  142. for (size_t length = string.length(); length > 0; --length) {
  143. // This can't throw, so no exception check is fine.
  144. auto number = Value(js_string(interpreter, string.substring(0, length))).to_number(interpreter);
  145. if (!number.is_nan())
  146. return number;
  147. }
  148. return js_nan();
  149. }
  150. }