GlobalObject.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include <AK/LogStream.h>
  2. #include <AK/String.h>
  3. #include <LibJS/Heap/Heap.h>
  4. #include <LibJS/Interpreter.h>
  5. #include <LibJS/Runtime/ConsoleObject.h>
  6. #include <LibJS/Runtime/DateConstructor.h>
  7. #include <LibJS/Runtime/ErrorConstructor.h>
  8. #include <LibJS/Runtime/FunctionConstructor.h>
  9. #include <LibJS/Runtime/GlobalObject.h>
  10. #include <LibJS/Runtime/MathObject.h>
  11. #include <LibJS/Runtime/NativeFunction.h>
  12. #include <LibJS/Runtime/ObjectConstructor.h>
  13. #include <LibJS/Runtime/Value.h>
  14. namespace JS {
  15. GlobalObject::GlobalObject()
  16. {
  17. put_native_function("gc", gc);
  18. put_native_function("isNaN", is_nan, 1);
  19. // FIXME: These are read-only in ES5
  20. put("NaN", js_nan());
  21. put("Infinity", js_infinity());
  22. put("undefined", js_undefined());
  23. put("console", heap().allocate<ConsoleObject>());
  24. put("Date", heap().allocate<DateConstructor>());
  25. put("Error", heap().allocate<ErrorConstructor>());
  26. put("Function", heap().allocate<FunctionConstructor>());
  27. put("Math", heap().allocate<MathObject>());
  28. put("Object", heap().allocate<ObjectConstructor>());
  29. }
  30. GlobalObject::~GlobalObject()
  31. {
  32. }
  33. Value GlobalObject::gc(Interpreter& interpreter)
  34. {
  35. dbg() << "Forced garbage collection requested!";
  36. interpreter.heap().collect_garbage();
  37. return js_undefined();
  38. }
  39. Value GlobalObject::is_nan(Interpreter& interpreter)
  40. {
  41. if (interpreter.argument_count() < 1)
  42. return js_undefined();
  43. return Value(interpreter.argument(0).to_number().is_nan());
  44. }
  45. }