$262Object.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/Contrib/Test262/$262Object.h>
  8. #include <LibJS/Contrib/Test262/AgentObject.h>
  9. #include <LibJS/Contrib/Test262/GlobalObject.h>
  10. #include <LibJS/Contrib/Test262/IsHTMLDDA.h>
  11. #include <LibJS/Heap/Cell.h>
  12. #include <LibJS/Interpreter.h>
  13. #include <LibJS/Runtime/ArrayBuffer.h>
  14. #include <LibJS/Runtime/GlobalObject.h>
  15. #include <LibJS/Runtime/Object.h>
  16. #include <LibJS/Script.h>
  17. namespace JS::Test262 {
  18. $262Object::$262Object(JS::GlobalObject& global_object)
  19. : Object(Object::ConstructWithoutPrototypeTag::Tag, global_object)
  20. {
  21. }
  22. void $262Object::initialize(JS::GlobalObject& global_object)
  23. {
  24. Base::initialize(global_object);
  25. m_agent = vm().heap().allocate<AgentObject>(global_object, global_object);
  26. m_is_htmldda = vm().heap().allocate<IsHTMLDDA>(global_object, global_object);
  27. u8 attr = Attribute::Writable | Attribute::Configurable;
  28. define_native_function("clearKeptObjects", clear_kept_objects, 0, attr);
  29. define_native_function("createRealm", create_realm, 0, attr);
  30. define_native_function("detachArrayBuffer", detach_array_buffer, 1, attr);
  31. define_native_function("evalScript", eval_script, 1, attr);
  32. define_direct_property("agent", m_agent, attr);
  33. define_direct_property("gc", global_object.get_without_side_effects("gc"), attr);
  34. define_direct_property("global", &global_object, attr);
  35. define_direct_property("IsHTMLDDA", m_is_htmldda, attr);
  36. }
  37. void $262Object::visit_edges(Cell::Visitor& visitor)
  38. {
  39. Base::visit_edges(visitor);
  40. visitor.visit(m_agent);
  41. visitor.visit(m_is_htmldda);
  42. }
  43. JS_DEFINE_NATIVE_FUNCTION($262Object::clear_kept_objects)
  44. {
  45. vm.finish_execution_generation();
  46. return js_undefined();
  47. }
  48. JS_DEFINE_NATIVE_FUNCTION($262Object::create_realm)
  49. {
  50. auto realm = vm.heap().allocate_without_global_object<GlobalObject>();
  51. realm->initialize_global_object();
  52. return Value(realm->$262());
  53. }
  54. JS_DEFINE_NATIVE_FUNCTION($262Object::detach_array_buffer)
  55. {
  56. auto array_buffer = vm.argument(0);
  57. if (!array_buffer.is_object() || !is<ArrayBuffer>(array_buffer.as_object()))
  58. return vm.throw_completion<TypeError>(global_object);
  59. auto& array_buffer_object = static_cast<ArrayBuffer&>(array_buffer.as_object());
  60. TRY(JS::detach_array_buffer(global_object, array_buffer_object, vm.argument(1)));
  61. return js_null();
  62. }
  63. JS_DEFINE_NATIVE_FUNCTION($262Object::eval_script)
  64. {
  65. auto source = TRY(vm.argument(0).to_string(global_object));
  66. auto script_or_error = Script::parse(source, *vm.current_realm());
  67. if (script_or_error.is_error())
  68. return vm.throw_completion<SyntaxError>(global_object, script_or_error.error()[0].to_string());
  69. TRY(vm.interpreter().run(script_or_error.value()));
  70. return js_undefined();
  71. }
  72. }