262Object.cpp 3.8 KB

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