$262Object.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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(Realm& realm)
  23. {
  24. Base::initialize(realm);
  25. m_agent = vm().heap().allocate<AgentObject>(realm, realm);
  26. m_is_htmldda = vm().heap().allocate<IsHTMLDDA>(realm, realm);
  27. u8 attr = Attribute::Writable | Attribute::Configurable;
  28. define_native_function(realm, "clearKeptObjects", clear_kept_objects, 0, attr);
  29. define_native_function(realm, "createRealm", create_realm, 0, attr);
  30. define_native_function(realm, "detachArrayBuffer", detach_array_buffer, 1, attr);
  31. define_native_function(realm, "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_realm<GlobalObject>(*realm);
  53. VERIFY(realm_global_object);
  54. realm->set_global_object(realm_global_object, nullptr);
  55. set_default_global_bindings(*realm);
  56. realm_global_object->initialize(*realm);
  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>();
  64. auto& array_buffer_object = static_cast<ArrayBuffer&>(array_buffer.as_object());
  65. TRY(JS::detach_array_buffer(vm, array_buffer_object, vm.argument(1)));
  66. return js_null();
  67. }
  68. JS_DEFINE_NATIVE_FUNCTION($262Object::eval_script)
  69. {
  70. auto source_text = TRY(vm.argument(0).to_string(vm));
  71. // 1. Let hostDefined be any host-defined values for the provided sourceText (obtained in an implementation dependent manner)
  72. // 2. Let realm be the current Realm Record.
  73. auto& realm = *vm.current_realm();
  74. // 3. Let s be ParseScript(sourceText, realm, hostDefined).
  75. auto script_or_error = Script::parse(source_text, realm);
  76. // 4. If s is a List of errors, then
  77. if (script_or_error.is_error()) {
  78. // a. Let error be the first element of s.
  79. auto& error = script_or_error.error()[0];
  80. // b. Return Completion { [[Type]]: throw, [[Value]]: error, [[Target]]: empty }.
  81. return vm.throw_completion<SyntaxError>(error.to_string());
  82. }
  83. // 5. Let status be ScriptEvaluation(s).
  84. auto status = vm.interpreter().run(script_or_error.value());
  85. // 6. Return Completion(status).
  86. return status;
  87. }
  88. }