$262Object.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 <AK/TypeCasts.h>
  8. #include <LibJS/Contrib/Test262/$262Object.h>
  9. #include <LibJS/Contrib/Test262/AgentObject.h>
  10. #include <LibJS/Contrib/Test262/GlobalObject.h>
  11. #include <LibJS/Contrib/Test262/IsHTMLDDA.h>
  12. #include <LibJS/Heap/Cell.h>
  13. #include <LibJS/Interpreter.h>
  14. #include <LibJS/Runtime/ArrayBuffer.h>
  15. #include <LibJS/Runtime/GlobalObject.h>
  16. #include <LibJS/Runtime/Object.h>
  17. #include <LibJS/Script.h>
  18. namespace JS::Test262 {
  19. $262Object::$262Object(Realm& realm)
  20. : Object(Object::ConstructWithoutPrototypeTag::Tag, realm)
  21. {
  22. }
  23. ThrowCompletionOr<void> $262Object::initialize(Realm& realm)
  24. {
  25. MUST_OR_THROW_OOM(Base::initialize(realm));
  26. m_agent = MUST_OR_THROW_OOM(vm().heap().allocate<AgentObject>(realm, realm));
  27. m_is_htmldda = MUST_OR_THROW_OOM(vm().heap().allocate<IsHTMLDDA>(realm, realm));
  28. u8 attr = Attribute::Writable | Attribute::Configurable;
  29. define_native_function(realm, "clearKeptObjects", clear_kept_objects, 0, attr);
  30. define_native_function(realm, "createRealm", create_realm, 0, attr);
  31. define_native_function(realm, "detachArrayBuffer", detach_array_buffer, 1, attr);
  32. define_native_function(realm, "evalScript", eval_script, 1, attr);
  33. define_direct_property("agent", m_agent, attr);
  34. define_direct_property("gc", realm.global_object().get_without_side_effects("gc"), attr);
  35. define_direct_property("global", &realm.global_object(), attr);
  36. define_direct_property("IsHTMLDDA", m_is_htmldda, attr);
  37. return {};
  38. }
  39. void $262Object::visit_edges(Cell::Visitor& visitor)
  40. {
  41. Base::visit_edges(visitor);
  42. visitor.visit(m_agent);
  43. visitor.visit(m_is_htmldda);
  44. }
  45. JS_DEFINE_NATIVE_FUNCTION($262Object::clear_kept_objects)
  46. {
  47. vm.finish_execution_generation();
  48. return js_undefined();
  49. }
  50. JS_DEFINE_NATIVE_FUNCTION($262Object::create_realm)
  51. {
  52. auto realm = MUST_OR_THROW_OOM(Realm::create(vm));
  53. auto realm_global_object = vm.heap().allocate_without_realm<GlobalObject>(*realm);
  54. VERIFY(realm_global_object);
  55. realm->set_global_object(realm_global_object, nullptr);
  56. set_default_global_bindings(*realm);
  57. MUST_OR_THROW_OOM(realm_global_object->initialize(*realm));
  58. return Value(realm_global_object->$262());
  59. }
  60. JS_DEFINE_NATIVE_FUNCTION($262Object::detach_array_buffer)
  61. {
  62. auto array_buffer = vm.argument(0);
  63. if (!array_buffer.is_object() || !is<ArrayBuffer>(array_buffer.as_object()))
  64. return vm.throw_completion<TypeError>();
  65. auto& array_buffer_object = static_cast<ArrayBuffer&>(array_buffer.as_object());
  66. TRY(JS::detach_array_buffer(vm, array_buffer_object, vm.argument(1)));
  67. return js_null();
  68. }
  69. JS_DEFINE_NATIVE_FUNCTION($262Object::eval_script)
  70. {
  71. auto source_text = TRY(vm.argument(0).to_deprecated_string(vm));
  72. // 1. Let hostDefined be any host-defined values for the provided sourceText (obtained in an implementation dependent manner)
  73. // 2. Let realm be the current Realm Record.
  74. auto& realm = *vm.current_realm();
  75. // 3. Let s be ParseScript(sourceText, realm, hostDefined).
  76. auto script_or_error = Script::parse(source_text, realm);
  77. // 4. If s is a List of errors, then
  78. if (script_or_error.is_error()) {
  79. // a. Let error be the first element of s.
  80. auto& error = script_or_error.error()[0];
  81. // b. Return Completion { [[Type]]: throw, [[Value]]: error, [[Target]]: empty }.
  82. return vm.throw_completion<SyntaxError>(TRY_OR_THROW_OOM(vm, error.to_string()));
  83. }
  84. // 5. Let status be ScriptEvaluation(s).
  85. auto status = vm.interpreter().run(script_or_error.value());
  86. // 6. Return Completion(status).
  87. return status;
  88. }
  89. }