WrappedFunction.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. GC_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<GC::Ref<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 = realm.create<WrappedFunction>(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();
  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 Completion(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. // 6. Return ? result.
  60. return result;
  61. }
  62. // 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
  63. ThrowCompletionOr<Value> ordinary_wrapped_function_call(WrappedFunction const& function, Value this_argument, ReadonlySpan<Value> arguments_list)
  64. {
  65. auto& vm = function.vm();
  66. // 1. Let target be F.[[WrappedTargetFunction]].
  67. auto const& target = function.wrapped_target_function();
  68. // 2. Assert: IsCallable(target) is true.
  69. VERIFY(Value(&target).is_function());
  70. // 3. Let callerRealm be F.[[Realm]].
  71. auto* caller_realm = function.realm();
  72. // 4. NOTE: Any exception objects produced after this point are associated with callerRealm.
  73. VERIFY(vm.current_realm() == caller_realm);
  74. // 5. Let targetRealm be ? GetFunctionRealm(target).
  75. auto* target_realm = TRY(get_function_realm(vm, target));
  76. // 6. Let wrappedArgs be a new empty List.
  77. auto wrapped_args = GC::RootVector<Value> { vm.heap() };
  78. wrapped_args.ensure_capacity(arguments_list.size());
  79. // 7. For each element arg of argumentsList, do
  80. for (auto const& arg : arguments_list) {
  81. // a. Let wrappedValue be ? GetWrappedValue(targetRealm, arg).
  82. auto wrapped_value = TRY(get_wrapped_value(vm, *target_realm, arg));
  83. // b. Append wrappedValue to wrappedArgs.
  84. wrapped_args.append(wrapped_value);
  85. }
  86. // 8. Let wrappedThisArgument to ? GetWrappedValue(targetRealm, thisArgument).
  87. auto wrapped_this_argument = TRY(get_wrapped_value(vm, *target_realm, this_argument));
  88. // 9. Let result be the Completion Record of Call(target, wrappedThisArgument, wrappedArgs).
  89. auto result = call(vm, &target, wrapped_this_argument, wrapped_args.span());
  90. // 10. If result.[[Type]] is normal or result.[[Type]] is return, then
  91. if (!result.is_throw_completion()) {
  92. // a. Return ? GetWrappedValue(callerRealm, result.[[Value]]).
  93. return get_wrapped_value(vm, *caller_realm, result.value());
  94. }
  95. // 11. Else,
  96. else {
  97. // a. Throw a TypeError exception.
  98. return vm.throw_completion<TypeError>(ErrorType::WrappedFunctionCallThrowCompletion);
  99. }
  100. // NOTE: Also see "Editor's Note" in the spec regarding the TypeError above.
  101. }
  102. // 2.3 PrepareForWrappedFunctionCall ( F: a wrapped function exotic object, ), https://tc39.es/proposal-shadowrealm/#sec-prepare-for-wrapped-function-call
  103. void prepare_for_wrapped_function_call(WrappedFunction const& function, ExecutionContext& callee_context)
  104. {
  105. auto& vm = function.vm();
  106. // 1. Let callerContext be the running execution context.
  107. auto const& caller_context = vm.running_execution_context();
  108. // 2. Let calleeContext be a new execution context.
  109. // NOTE: In the specification, PrepareForWrappedFunctionCall "returns" a new callee execution context.
  110. // To avoid heap allocations, we put our ExecutionContext objects on the C++ stack instead.
  111. // Whoever calls us should put an ExecutionContext on their stack and pass that as the `callee_context`.
  112. // 3. Set the Function of calleeContext to F.
  113. callee_context.function = &const_cast<WrappedFunction&>(function);
  114. // 4. Let calleeRealm be F.[[Realm]].
  115. auto* callee_realm = function.realm();
  116. // 5. Set the Realm of calleeContext to calleeRealm.
  117. callee_context.realm = callee_realm;
  118. // 6. Set the ScriptOrModule of calleeContext to null.
  119. callee_context.script_or_module = {};
  120. // 7. If callerContext is not already suspended, suspend callerContext.
  121. // NOTE: We don't support this concept yet.
  122. (void)caller_context;
  123. // 8. Push calleeContext onto the execution context stack; calleeContext is now the running execution context.
  124. vm.push_execution_context(callee_context);
  125. // 9. NOTE: Any exception objects produced after this point are associated with calleeRealm.
  126. // 10. Return calleeContext.
  127. // NOTE: No-op, see NOTE after step 2.
  128. }
  129. }