GlobalObject.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/LogStream.h>
  27. #include <AK/String.h>
  28. #include <LibJS/Heap/Heap.h>
  29. #include <LibJS/Interpreter.h>
  30. #include <LibJS/Runtime/ArrayConstructor.h>
  31. #include <LibJS/Runtime/ArrayPrototype.h>
  32. #include <LibJS/Runtime/BooleanConstructor.h>
  33. #include <LibJS/Runtime/BooleanPrototype.h>
  34. #include <LibJS/Runtime/ConsoleObject.h>
  35. #include <LibJS/Runtime/DateConstructor.h>
  36. #include <LibJS/Runtime/DatePrototype.h>
  37. #include <LibJS/Runtime/Error.h>
  38. #include <LibJS/Runtime/ErrorConstructor.h>
  39. #include <LibJS/Runtime/ErrorPrototype.h>
  40. #include <LibJS/Runtime/FunctionConstructor.h>
  41. #include <LibJS/Runtime/FunctionPrototype.h>
  42. #include <LibJS/Runtime/GlobalObject.h>
  43. #include <LibJS/Runtime/LexicalEnvironment.h>
  44. #include <LibJS/Runtime/MathObject.h>
  45. #include <LibJS/Runtime/NativeFunction.h>
  46. #include <LibJS/Runtime/NumberConstructor.h>
  47. #include <LibJS/Runtime/NumberPrototype.h>
  48. #include <LibJS/Runtime/Object.h>
  49. #include <LibJS/Runtime/ObjectConstructor.h>
  50. #include <LibJS/Runtime/ObjectPrototype.h>
  51. #include <LibJS/Runtime/Shape.h>
  52. #include <LibJS/Runtime/StringConstructor.h>
  53. #include <LibJS/Runtime/StringPrototype.h>
  54. #include <LibJS/Runtime/Value.h>
  55. namespace JS {
  56. template<typename ConstructorType>
  57. void GlobalObject::add_constructor(const FlyString& property_name, ConstructorType*& constructor, Object& prototype)
  58. {
  59. constructor = heap().allocate<ConstructorType>();
  60. prototype.put("constructor", constructor);
  61. put(property_name, constructor);
  62. }
  63. GlobalObject::GlobalObject()
  64. : Object(nullptr)
  65. {
  66. }
  67. void GlobalObject::initialize()
  68. {
  69. // These are done first since other prototypes depend on their presence.
  70. m_object_prototype = heap().allocate<ObjectPrototype>();
  71. m_function_prototype = heap().allocate<FunctionPrototype>();
  72. static_cast<FunctionPrototype*>(m_function_prototype)->initialize();
  73. static_cast<ObjectPrototype*>(m_object_prototype)->initialize();
  74. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  75. if (!m_##snake_name##_prototype) \
  76. m_##snake_name##_prototype = heap().allocate<PrototypeName>();
  77. JS_ENUMERATE_BUILTIN_TYPES
  78. #undef __JS_ENUMERATE
  79. put_native_function("gc", gc);
  80. put_native_function("isNaN", is_nan, 1);
  81. // FIXME: These are read-only in ES5
  82. put("NaN", js_nan());
  83. put("Infinity", js_infinity());
  84. put("undefined", js_undefined());
  85. put("globalThis", this);
  86. put("console", heap().allocate<ConsoleObject>());
  87. put("Math", heap().allocate<MathObject>());
  88. add_constructor("Array", m_array_constructor, *m_array_prototype);
  89. add_constructor("Boolean", m_boolean_constructor, *m_boolean_prototype);
  90. add_constructor("Date", m_date_constructor, *m_date_prototype);
  91. add_constructor("Error", m_error_constructor, *m_error_prototype);
  92. add_constructor("Function", m_function_constructor, *m_function_prototype);
  93. add_constructor("Number", m_number_constructor, *m_number_prototype);
  94. add_constructor("Object", m_object_constructor, *m_object_prototype);
  95. add_constructor("String", m_string_constructor, *m_string_prototype);
  96. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  97. add_constructor(#ClassName, m_##snake_name##_constructor, *m_##snake_name##_prototype);
  98. JS_ENUMERATE_ERROR_SUBCLASSES
  99. #undef __JS_ENUMERATE
  100. }
  101. GlobalObject::~GlobalObject()
  102. {
  103. }
  104. void GlobalObject::visit_children(Visitor& visitor)
  105. {
  106. Object::visit_children(visitor);
  107. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  108. visitor.visit(m_##snake_name##_constructor);
  109. JS_ENUMERATE_ERROR_SUBCLASSES
  110. #undef __JS_ENUMERATE
  111. }
  112. Value GlobalObject::gc(Interpreter& interpreter)
  113. {
  114. dbg() << "Forced garbage collection requested!";
  115. interpreter.heap().collect_garbage();
  116. return js_undefined();
  117. }
  118. Value GlobalObject::is_nan(Interpreter& interpreter)
  119. {
  120. if (interpreter.argument_count() < 1)
  121. return js_undefined();
  122. return Value(interpreter.argument(0).to_number().is_nan());
  123. }
  124. }