ladybird/Libraries/LibJS/Runtime/GlobalObject.cpp
Andreas Kling 7c4e53f31e LibJS: Rework how native functions are called to improve |this| value
Native functions now only get the Interpreter& as an argument. They can
then extract |this| along with any indexed arguments it wants from it.

This forces functions that want |this| to actually deal with calling
interpreter.this_value().to_object(), and dealing with the possibility
of a non-object |this|.

This is still not great but let's keep massaging it forward.
2020-03-28 22:51:09 +01:00

35 lines
1 KiB
C++

#include <AK/LogStream.h>
#include <AK/String.h>
#include <LibJS/Heap/Heap.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/ConsoleObject.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/MathObject.h>
#include <LibJS/Runtime/NativeFunction.h>
#include <LibJS/Runtime/ObjectConstructor.h>
#include <LibJS/Runtime/Value.h>
namespace JS {
GlobalObject::GlobalObject()
{
put("console", heap().allocate<ConsoleObject>());
put_native_function("gc", [](Interpreter& interpreter) -> Value {
dbg() << "Forced garbage collection requested!";
interpreter.heap().collect_garbage();
return js_undefined();
});
put_native_function("isNaN", [](Interpreter& interpreter) -> Value {
if (interpreter.call_frame().arguments.size() < 1)
return js_undefined();
return Value(interpreter.call_frame().arguments[0].to_number().is_nan());
});
put("Math", heap().allocate<MathObject>());
put("Object", heap().allocate<ObjectConstructor>());
}
GlobalObject::~GlobalObject()
{
}
}