GlobalObject.cpp 1.4 KB

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