Serenity Operating System
at hosted 108 lines 4.1 kB view raw
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 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/BooleanConstructor.h> 33#include <LibJS/Runtime/ConsoleObject.h> 34#include <LibJS/Runtime/DateConstructor.h> 35#include <LibJS/Runtime/ErrorConstructor.h> 36#include <LibJS/Runtime/FunctionConstructor.h> 37#include <LibJS/Runtime/GlobalObject.h> 38#include <LibJS/Runtime/MathObject.h> 39#include <LibJS/Runtime/NativeFunction.h> 40#include <LibJS/Runtime/NumberConstructor.h> 41#include <LibJS/Runtime/ObjectConstructor.h> 42#include <LibJS/Runtime/Value.h> 43 44namespace JS { 45 46template<typename ConstructorType> 47void GlobalObject::add_constructor(const FlyString& property_name, ConstructorType*& constructor, Object& prototype) 48{ 49 constructor = heap().allocate<ConstructorType>(); 50 prototype.put("constructor", constructor); 51 put(property_name, constructor); 52} 53 54GlobalObject::GlobalObject() 55{ 56 put_native_function("gc", gc); 57 put_native_function("isNaN", is_nan, 1); 58 59 // FIXME: These are read-only in ES5 60 put("NaN", js_nan()); 61 put("Infinity", js_infinity()); 62 put("undefined", js_undefined()); 63 64 put("globalThis", this); 65 put("console", heap().allocate<ConsoleObject>()); 66 put("Math", heap().allocate<MathObject>()); 67 68 add_constructor("Array", m_array_constructor, *interpreter().array_prototype()); 69 add_constructor("Boolean", m_boolean_constructor, *interpreter().boolean_prototype()); 70 add_constructor("Date", m_date_constructor, *interpreter().date_prototype()); 71 add_constructor("Error", m_error_constructor, *interpreter().error_prototype()); 72 add_constructor("Function", m_function_constructor, *interpreter().function_prototype()); 73 add_constructor("Number", m_number_constructor, *interpreter().number_prototype()); 74 add_constructor("Object", m_object_constructor, *interpreter().object_prototype()); 75} 76 77GlobalObject::~GlobalObject() 78{ 79} 80 81void GlobalObject::visit_children(Visitor& visitor) 82{ 83 Object::visit_children(visitor); 84 85 visitor.visit(m_array_constructor); 86 visitor.visit(m_boolean_constructor); 87 visitor.visit(m_date_constructor); 88 visitor.visit(m_error_constructor); 89 visitor.visit(m_function_constructor); 90 visitor.visit(m_number_constructor); 91 visitor.visit(m_object_constructor); 92} 93 94Value GlobalObject::gc(Interpreter& interpreter) 95{ 96 dbg() << "Forced garbage collection requested!"; 97 interpreter.heap().collect_garbage(); 98 return js_undefined(); 99} 100 101Value GlobalObject::is_nan(Interpreter& interpreter) 102{ 103 if (interpreter.argument_count() < 1) 104 return js_undefined(); 105 return Value(interpreter.argument(0).to_number().is_nan()); 106} 107 108}