WrappedFunction.cpp 4.0 KB

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