WrappedFunction.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AbstractOperations.h>
  7. #include <LibJS/Runtime/ShadowRealm.h>
  8. #include <LibJS/Runtime/WrappedFunction.h>
  9. namespace JS {
  10. // 3.1.1 WrappedFunctionCreate ( callerRealm: a Realm Record, Target: a function object, ), https://tc39.es/proposal-shadowrealm/#sec-wrappedfunctioncreate
  11. ThrowCompletionOr<WrappedFunction*> WrappedFunction::create(GlobalObject& global_object, Realm& caller_realm, FunctionObject& target)
  12. {
  13. auto& vm = global_object.vm();
  14. // 1. Let internalSlotsList be the internal slots listed in Table 2, plus [[Prototype]] and [[Extensible]].
  15. // 2. Let wrapped be ! MakeBasicObject(internalSlotsList).
  16. // 3. Set wrapped.[[Prototype]] to callerRealm.[[Intrinsics]].[[%Function.prototype%]].
  17. // 4. Set wrapped.[[Call]] as described in 2.1.
  18. // 5. Set wrapped.[[WrappedTargetFunction]] to Target.
  19. // 6. Set wrapped.[[Realm]] to callerRealm.
  20. auto& prototype = *caller_realm.global_object().function_prototype();
  21. auto* wrapped = global_object.heap().allocate<WrappedFunction>(global_object, caller_realm, target, prototype);
  22. // 7. Let result be CopyNameAndLength(wrapped, Target, "wrapped").
  23. auto result = copy_name_and_length(global_object, *wrapped, target, "wrapped"sv);
  24. // 8. If result is an Abrupt Completion, throw a TypeError exception.
  25. if (result.is_throw_completion())
  26. return vm.throw_completion<TypeError>(global_object, ErrorType::WrappedFunctionCopyNameAndLengthThrowCompletion);
  27. // 9. Return wrapped.
  28. return wrapped;
  29. }
  30. // 2 Wrapped Function Exotic Objects, https://tc39.es/proposal-shadowrealm/#sec-wrapped-function-exotic-objects
  31. WrappedFunction::WrappedFunction(Realm& realm, FunctionObject& wrapped_target_function, Object& prototype)
  32. : FunctionObject(prototype)
  33. , m_wrapped_target_function(wrapped_target_function)
  34. , m_realm(realm)
  35. {
  36. }
  37. // 2.1 [[Call]] ( thisArgument, argumentsList ), https://tc39.es/proposal-shadowrealm/#sec-wrapped-function-exotic-objects-call-thisargument-argumentslist
  38. ThrowCompletionOr<Value> WrappedFunction::internal_call(Value this_argument, MarkedVector<Value> arguments_list)
  39. {
  40. auto& vm = this->vm();
  41. auto& global_object = this->global_object();
  42. // 1. Let target be F.[[WrappedTargetFunction]].
  43. auto& target = m_wrapped_target_function;
  44. // 2. Assert: IsCallable(target) is true.
  45. VERIFY(Value(&target).is_function());
  46. // 3. Let targetRealm be ? GetFunctionRealm(target).
  47. auto* target_realm = TRY(get_function_realm(global_object, target));
  48. // 4. Let callerRealm be ? GetFunctionRealm(F).
  49. auto* caller_realm = TRY(get_function_realm(global_object, *this));
  50. // 5. NOTE: Any exception objects produced after this point are associated with callerRealm.
  51. // 6. Let wrappedArgs be a new empty List.
  52. auto wrapped_args = MarkedVector<Value> { vm.heap() };
  53. wrapped_args.ensure_capacity(arguments_list.size());
  54. // 7. For each element arg of argumentsList, do
  55. for (auto& arg : arguments_list) {
  56. // a. Let wrappedValue be ? GetWrappedValue(targetRealm, arg).
  57. auto wrapped_value = TRY(get_wrapped_value(global_object, *target_realm, arg));
  58. // b. Append wrappedValue to wrappedArgs.
  59. wrapped_args.append(wrapped_value);
  60. }
  61. // 8. Let wrappedThisArgument to ? GetWrappedValue(targetRealm, thisArgument).
  62. auto wrapped_this_argument = TRY(get_wrapped_value(global_object, *target_realm, this_argument));
  63. // 9. Let result be the Completion Record of Call(target, wrappedThisArgument, wrappedArgs).
  64. auto result = call(global_object, &target, wrapped_this_argument, move(wrapped_args));
  65. // 10. If result.[[Type]] is normal or result.[[Type]] is return, then
  66. if (!result.is_throw_completion()) {
  67. // a. Return ? GetWrappedValue(callerRealm, result.[[Value]]).
  68. return get_wrapped_value(global_object, *caller_realm, result.value());
  69. }
  70. // 11. Else,
  71. else {
  72. // a. Throw a TypeError exception.
  73. return vm.throw_completion<TypeError>(caller_realm->global_object(), ErrorType::WrappedFunctionCallThrowCompletion);
  74. }
  75. // NOTE: Also see "Editor's Note" in the spec regarding the TypeError above.
  76. }
  77. void WrappedFunction::visit_edges(Visitor& visitor)
  78. {
  79. Base::visit_edges(visitor);
  80. visitor.visit(&m_wrapped_target_function);
  81. visitor.visit(&m_realm);
  82. }
  83. }