$262Object.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright (c) 2021, 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. // 25.1.2.3 DetachArrayBuffer, https://tc39.es/ecma262/#sec-detacharraybuffer
  55. JS_DEFINE_NATIVE_FUNCTION($262Object::detach_array_buffer)
  56. {
  57. auto array_buffer = vm.argument(0);
  58. if (!array_buffer.is_object() || !is<ArrayBuffer>(array_buffer.as_object()))
  59. return vm.throw_completion<TypeError>(global_object);
  60. auto& array_buffer_object = static_cast<ArrayBuffer&>(array_buffer.as_object());
  61. if (!same_value(array_buffer_object.detach_key(), vm.argument(1)))
  62. return vm.throw_completion<TypeError>(global_object);
  63. array_buffer_object.detach_buffer();
  64. return js_null();
  65. }
  66. JS_DEFINE_NATIVE_FUNCTION($262Object::eval_script)
  67. {
  68. auto source = TRY(vm.argument(0).to_string(global_object));
  69. auto script_or_error = Script::parse(source, *vm.current_realm());
  70. if (script_or_error.is_error())
  71. return vm.throw_completion<SyntaxError>(global_object, script_or_error.error()[0].to_string());
  72. TRY(vm.interpreter().run(script_or_error.value()));
  73. return js_undefined();
  74. }
  75. }