$262Object.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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(Realm& realm)
  19. : Object(Object::ConstructWithoutPrototypeTag::Tag, realm)
  20. {
  21. }
  22. void $262Object::initialize(JS::Realm& realm)
  23. {
  24. Base::initialize(realm);
  25. m_agent = vm().heap().allocate<AgentObject>(realm.global_object(), realm);
  26. m_is_htmldda = vm().heap().allocate<IsHTMLDDA>(realm.global_object(), realm);
  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", realm.global_object().get_without_side_effects("gc"), attr);
  34. define_direct_property("global", &realm.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 = Realm::create(vm);
  51. VERIFY(realm);
  52. auto* realm_global_object = vm.heap().allocate_without_global_object<GlobalObject>(*realm);
  53. VERIFY(realm_global_object);
  54. realm->set_global_object(realm_global_object, nullptr);
  55. realm_global_object->set_associated_realm(*realm);
  56. realm_global_object->initialize_global_object();
  57. return Value(realm_global_object->$262());
  58. }
  59. JS_DEFINE_NATIVE_FUNCTION($262Object::detach_array_buffer)
  60. {
  61. auto array_buffer = vm.argument(0);
  62. if (!array_buffer.is_object() || !is<ArrayBuffer>(array_buffer.as_object()))
  63. return vm.throw_completion<TypeError>(global_object);
  64. auto& array_buffer_object = static_cast<ArrayBuffer&>(array_buffer.as_object());
  65. TRY(JS::detach_array_buffer(global_object, array_buffer_object, vm.argument(1)));
  66. return js_null();
  67. }
  68. JS_DEFINE_NATIVE_FUNCTION($262Object::eval_script)
  69. {
  70. auto source = TRY(vm.argument(0).to_string(global_object));
  71. auto script_or_error = Script::parse(source, *vm.current_realm());
  72. if (script_or_error.is_error())
  73. return vm.throw_completion<SyntaxError>(global_object, script_or_error.error()[0].to_string());
  74. TRY(vm.interpreter().run(script_or_error.value()));
  75. return js_undefined();
  76. }
  77. }