WrappedFunction.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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<NonnullGCPtr<WrappedFunction>> WrappedFunction::create(Realm& realm, Realm& caller_realm, FunctionObject& target)
  12. {
  13. auto& vm = realm.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.intrinsics().function_prototype();
  21. auto wrapped = vm.heap().allocate<WrappedFunction>(realm, caller_realm, target, prototype);
  22. // 7. Let result be CopyNameAndLength(wrapped, Target).
  23. auto result = copy_name_and_length(vm, *wrapped, target);
  24. // 8. If result is an Abrupt Completion, throw a TypeError exception.
  25. if (result.is_throw_completion())
  26. return vm.throw_completion<TypeError>(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. void WrappedFunction::visit_edges(Visitor& visitor)
  38. {
  39. Base::visit_edges(visitor);
  40. visitor.visit(m_wrapped_target_function);
  41. visitor.visit(m_realm);
  42. }
  43. // 2.1 [[Call]] ( thisArgument, argumentsList ), https://tc39.es/proposal-shadowrealm/#sec-wrapped-function-exotic-objects-call-thisargument-argumentslist
  44. ThrowCompletionOr<Value> WrappedFunction::internal_call(Value this_argument, MarkedVector<Value> arguments_list)
  45. {
  46. auto& vm = this->vm();
  47. // 1. Let callerContext be the running execution context.
  48. // NOTE: No-op, kept by the VM in its execution context stack.
  49. // 2. Let calleeContext be PrepareForWrappedFunctionCall(F).
  50. ExecutionContext callee_context { vm.heap() };
  51. prepare_for_wrapped_function_call(*this, callee_context);
  52. // 3. Assert: calleeContext is now the running execution context.
  53. VERIFY(&vm.running_execution_context() == &callee_context);
  54. // 4. Let result be OrdinaryWrappedFunctionCall(F, thisArgument, argumentsList).
  55. auto result = ordinary_wrapped_function_call(*this, this_argument, arguments_list);
  56. // 5. Remove calleeContext from the execution context stack and restore callerContext as the running execution context.
  57. vm.pop_execution_context();
  58. // NOTE: I think the spec isn't fully correct here, see https://github.com/tc39/proposal-shadowrealm/issues/371.
  59. // 6. If result.[[Type]] is return, return result.[[Value]].
  60. // 7. ReturnIfAbrupt(result).
  61. // 8. Return undefined.
  62. return result;
  63. }
  64. // 2.2 OrdinaryWrappedFunctionCall ( F: a wrapped function exotic object, thisArgument: an ECMAScript language value, argumentsList: a List of ECMAScript language values, ), https://tc39.es/proposal-shadowrealm/#sec-ordinary-wrapped-function-call
  65. ThrowCompletionOr<Value> ordinary_wrapped_function_call(WrappedFunction const& function, Value this_argument, MarkedVector<Value> const& arguments_list)
  66. {
  67. auto& vm = function.vm();
  68. // 1. Let target be F.[[WrappedTargetFunction]].
  69. auto const& target = function.wrapped_target_function();
  70. // 2. Assert: IsCallable(target) is true.
  71. VERIFY(Value(&target).is_function());
  72. // 3. Let callerRealm be F.[[Realm]].
  73. auto* caller_realm = function.realm();
  74. // 4. NOTE: Any exception objects produced after this point are associated with callerRealm.
  75. VERIFY(vm.current_realm() == caller_realm);
  76. // 5. Let targetRealm be ? GetFunctionRealm(target).
  77. auto* target_realm = TRY(get_function_realm(vm, target));
  78. // 6. Let wrappedArgs be a new empty List.
  79. auto wrapped_args = MarkedVector<Value> { vm.heap() };
  80. wrapped_args.ensure_capacity(arguments_list.size());
  81. // 7. For each element arg of argumentsList, do
  82. for (auto const& arg : arguments_list) {
  83. // a. Let wrappedValue be ? GetWrappedValue(targetRealm, arg).
  84. auto wrapped_value = TRY(get_wrapped_value(vm, *target_realm, arg));
  85. // b. Append wrappedValue to wrappedArgs.
  86. wrapped_args.append(wrapped_value);
  87. }
  88. // 8. Let wrappedThisArgument to ? GetWrappedValue(targetRealm, thisArgument).
  89. auto wrapped_this_argument = TRY(get_wrapped_value(vm, *target_realm, this_argument));
  90. // 9. Let result be the Completion Record of Call(target, wrappedThisArgument, wrappedArgs).
  91. auto result = call(vm, &target, wrapped_this_argument, move(wrapped_args));
  92. // 10. If result.[[Type]] is normal or result.[[Type]] is return, then
  93. if (!result.is_throw_completion()) {
  94. // a. Return ? GetWrappedValue(callerRealm, result.[[Value]]).
  95. return get_wrapped_value(vm, *caller_realm, result.value());
  96. }
  97. // 11. Else,
  98. else {
  99. // a. Throw a TypeError exception.
  100. return vm.throw_completion<TypeError>(ErrorType::WrappedFunctionCallThrowCompletion);
  101. }
  102. // NOTE: Also see "Editor's Note" in the spec regarding the TypeError above.
  103. }
  104. // 2.3 PrepareForWrappedFunctionCall ( F: a wrapped function exotic object, ), https://tc39.es/proposal-shadowrealm/#sec-prepare-for-wrapped-function-call
  105. void prepare_for_wrapped_function_call(WrappedFunction const& function, ExecutionContext& callee_context)
  106. {
  107. auto& vm = function.vm();
  108. // 1. Let callerContext be the running execution context.
  109. auto const& caller_context = vm.running_execution_context();
  110. // 2. Let calleeContext be a new execution context.
  111. // NOTE: In the specification, PrepareForWrappedFunctionCall "returns" a new callee execution context.
  112. // To avoid heap allocations, we put our ExecutionContext objects on the C++ stack instead.
  113. // Whoever calls us should put an ExecutionContext on their stack and pass that as the `callee_context`.
  114. // 3. Set the Function of calleeContext to F.
  115. callee_context.function = &const_cast<WrappedFunction&>(function);
  116. // 4. Let calleeRealm be F.[[Realm]].
  117. auto* callee_realm = function.realm();
  118. // 5. Set the Realm of calleeContext to calleeRealm.
  119. callee_context.realm = callee_realm;
  120. // 6. Set the ScriptOrModule of calleeContext to null.
  121. callee_context.script_or_module = {};
  122. // 7. If callerContext is not already suspended, suspend callerContext.
  123. // NOTE: We don't support this concept yet.
  124. (void)caller_context;
  125. // 8. Push calleeContext onto the execution context stack; calleeContext is now the running execution context.
  126. vm.push_execution_context(callee_context);
  127. // 9. NOTE: Any exception objects produced after this point are associated with calleeRealm.
  128. // 10. Return calleeContext.
  129. // NOTE: No-op, see NOTE after step 2.
  130. }
  131. }