WrappedFunction.cpp 7.3 KB

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