2020-04-06 11:09:01 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2020-03-12 20:11:35 +01:00
|
|
|
#include <AK/LogStream.h>
|
|
|
|
#include <AK/String.h>
|
2020-03-16 14:20:30 +01:00
|
|
|
#include <LibJS/Heap/Heap.h>
|
2020-03-12 20:11:35 +01:00
|
|
|
#include <LibJS/Interpreter.h>
|
2020-04-04 22:28:21 +02:00
|
|
|
#include <LibJS/Runtime/ArrayConstructor.h>
|
2020-03-16 14:20:30 +01:00
|
|
|
#include <LibJS/Runtime/ConsoleObject.h>
|
2020-03-30 00:21:56 +01:00
|
|
|
#include <LibJS/Runtime/DateConstructor.h>
|
2020-04-01 19:42:07 +01:00
|
|
|
#include <LibJS/Runtime/ErrorConstructor.h>
|
2020-04-04 14:34:31 +01:00
|
|
|
#include <LibJS/Runtime/FunctionConstructor.h>
|
2020-03-16 14:20:30 +01:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2020-03-21 17:52:12 +01:00
|
|
|
#include <LibJS/Runtime/MathObject.h>
|
2020-03-16 14:20:30 +01:00
|
|
|
#include <LibJS/Runtime/NativeFunction.h>
|
2020-03-28 17:23:54 +01:00
|
|
|
#include <LibJS/Runtime/ObjectConstructor.h>
|
2020-03-16 14:20:30 +01:00
|
|
|
#include <LibJS/Runtime/Value.h>
|
2020-03-12 20:11:35 +01:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2020-03-13 11:06:32 +01:00
|
|
|
GlobalObject::GlobalObject()
|
2020-03-12 20:11:35 +01:00
|
|
|
{
|
2020-03-28 23:10:37 +01:00
|
|
|
put_native_function("gc", gc);
|
2020-04-04 14:13:53 +01:00
|
|
|
put_native_function("isNaN", is_nan, 1);
|
2020-03-28 23:10:37 +01:00
|
|
|
|
2020-04-02 16:59:01 +01:00
|
|
|
// FIXME: These are read-only in ES5
|
2020-04-01 21:00:09 +01:00
|
|
|
put("NaN", js_nan());
|
2020-04-02 16:59:01 +01:00
|
|
|
put("Infinity", js_infinity());
|
2020-04-02 22:08:14 +01:00
|
|
|
put("undefined", js_undefined());
|
2020-04-02 16:59:01 +01:00
|
|
|
|
2020-03-16 14:58:20 +01:00
|
|
|
put("console", heap().allocate<ConsoleObject>());
|
2020-03-30 00:21:56 +01:00
|
|
|
put("Date", heap().allocate<DateConstructor>());
|
2020-04-01 19:42:07 +01:00
|
|
|
put("Error", heap().allocate<ErrorConstructor>());
|
2020-04-04 14:34:31 +01:00
|
|
|
put("Function", heap().allocate<FunctionConstructor>());
|
2020-03-21 17:52:12 +01:00
|
|
|
put("Math", heap().allocate<MathObject>());
|
2020-03-28 17:23:54 +01:00
|
|
|
put("Object", heap().allocate<ObjectConstructor>());
|
2020-04-04 22:28:21 +02:00
|
|
|
put("Array", heap().allocate<ArrayConstructor>());
|
2020-03-12 20:11:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
GlobalObject::~GlobalObject()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-03-28 23:10:37 +01:00
|
|
|
Value GlobalObject::gc(Interpreter& interpreter)
|
|
|
|
{
|
|
|
|
dbg() << "Forced garbage collection requested!";
|
|
|
|
interpreter.heap().collect_garbage();
|
|
|
|
return js_undefined();
|
|
|
|
}
|
|
|
|
|
|
|
|
Value GlobalObject::is_nan(Interpreter& interpreter)
|
|
|
|
{
|
2020-04-01 22:38:59 +02:00
|
|
|
if (interpreter.argument_count() < 1)
|
2020-03-28 23:10:37 +01:00
|
|
|
return js_undefined();
|
2020-04-01 22:38:59 +02:00
|
|
|
return Value(interpreter.argument(0).to_number().is_nan());
|
2020-03-28 23:10:37 +01:00
|
|
|
}
|
|
|
|
|
2020-03-12 20:11:35 +01:00
|
|
|
}
|