GlobalObject.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Format.h>
  7. #include <LibJS/Contrib/Test262/$262Object.h>
  8. #include <LibJS/Contrib/Test262/AgentObject.h>
  9. #include <LibJS/Contrib/Test262/GlobalObject.h>
  10. #include <LibJS/Heap/Cell.h>
  11. #include <LibJS/Runtime/GlobalObject.h>
  12. #include <LibJS/Runtime/VM.h>
  13. namespace JS::Test262 {
  14. ThrowCompletionOr<void> GlobalObject::initialize(Realm& realm)
  15. {
  16. MUST_OR_THROW_OOM(Base::initialize(realm));
  17. m_$262 = MUST_OR_THROW_OOM(vm().heap().allocate<$262Object>(realm, realm));
  18. // https://github.com/tc39/test262/blob/master/INTERPRETING.md#host-defined-functions
  19. u8 attr = Attribute::Writable | Attribute::Configurable;
  20. define_native_function(realm, "print", print, 1, attr);
  21. define_direct_property("$262", m_$262, attr);
  22. return {};
  23. }
  24. void GlobalObject::visit_edges(Cell::Visitor& visitor)
  25. {
  26. Base::visit_edges(visitor);
  27. visitor.visit(m_$262);
  28. }
  29. JS_DEFINE_NATIVE_FUNCTION(GlobalObject::print)
  30. {
  31. auto string = TRY(vm.argument(0).to_deprecated_string(vm));
  32. outln("{}", string);
  33. return js_undefined();
  34. }
  35. }