| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#include "vm/AsyncFunction.h" |
| 8 |
|
| 9 |
#include "mozilla/Maybe.h" |
| 10 |
|
| 11 |
#include "jsapi.h" |
| 12 |
|
| 13 |
#include "builtin/ModuleObject.h" |
| 14 |
#include "builtin/Promise.h" |
| 15 |
#include "js/Wrapper.h" |
| 16 |
#include "proxy/DeadObjectProxy.h" |
| 17 |
#include "vm/FunctionFlags.h" // js::FunctionFlags |
| 18 |
#include "vm/GeneratorObject.h" |
| 19 |
#include "vm/GlobalObject.h" |
| 20 |
#include "vm/Interpreter.h" |
| 21 |
#include "vm/Modules.h" |
| 22 |
#include "vm/NativeObject.h" |
| 23 |
#include "vm/PromiseObject.h" // js::PromiseObject |
| 24 |
#include "vm/Realm.h" |
| 25 |
#include "vm/SelfHosting.h" |
| 26 |
|
| 27 |
#include "vm/JSContext-inl.h" |
| 28 |
#include "vm/JSObject-inl.h" |
| 29 |
|
| 30 |
using namespace js; |
| 31 |
|
| 32 |
using mozilla::Maybe; |
| 33 |
|
| 34 |
static JSObject* CreateAsyncFunction(JSContext* cx, JSProtoKey key) { |
| 35 |
RootedObject proto(cx, &cx->global()->getFunctionConstructor()); |
| 36 |
Handle<PropertyName*> name = cx->names().AsyncFunction; |
| 37 |
return NewFunctionWithProto(cx, AsyncFunctionConstructor, 1, |
| 38 |
FunctionFlags::NATIVE_CTOR, nullptr, name, proto, |
| 39 |
gc::AllocKind::FUNCTION, TenuredObject); |
| 40 |
} |
| 41 |
|
| 42 |
static JSObject* CreateAsyncFunctionPrototype(JSContext* cx, JSProtoKey key) { |
| 43 |
return NewTenuredObjectWithFunctionPrototype(cx, cx->global()); |
| 44 |
} |
| 45 |
|
| 46 |
static bool AsyncFunctionClassFinish(JSContext* cx, HandleObject asyncFunction, |
| 47 |
HandleObject asyncFunctionProto) { |
| 48 |
// Change the "constructor" property to non-writable before adding any other |
| 49 |
// properties, so it's still the last property and can be modified without a |
| 50 |
// dictionary-mode transition. |
| 51 |
MOZ_ASSERT(asyncFunctionProto->as<NativeObject>().getLastProperty().key() == |
| 52 |
NameToId(cx->names().constructor)); |
| 53 |
MOZ_ASSERT(!asyncFunctionProto->as<NativeObject>().inDictionaryMode()); |
| 54 |
|
| 55 |
RootedValue asyncFunctionVal(cx, ObjectValue(*asyncFunction)); |
| 56 |
if (!DefineDataProperty(cx, asyncFunctionProto, cx->names().constructor, |
| 57 |
asyncFunctionVal, JSPROP_READONLY)) { |
| 58 |
return false; |
| 59 |
} |
| 60 |
MOZ_ASSERT(!asyncFunctionProto->as<NativeObject>().inDictionaryMode()); |
| 61 |
|
| 62 |
return DefineToStringTag(cx, asyncFunctionProto, cx->names().AsyncFunction); |
| 63 |
} |
| 64 |
|
| 65 |
static const ClassSpec AsyncFunctionClassSpec = { |
| 66 |
CreateAsyncFunction, |
| 67 |
CreateAsyncFunctionPrototype, |
| 68 |
nullptr, |
| 69 |
nullptr, |
| 70 |
nullptr, |
| 71 |
nullptr, |
| 72 |
AsyncFunctionClassFinish, |
| 73 |
ClassSpec::DontDefineConstructor, |
| 74 |
}; |
| 75 |
|
| 76 |
const JSClass js::AsyncFunctionClass = { |
| 77 |
"AsyncFunction", |
| 78 |
0, |
| 79 |
JS_NULL_CLASS_OPS, |
| 80 |
&AsyncFunctionClassSpec, |
| 81 |
}; |
| 82 |
|
| 83 |
enum class ResumeKind { Normal, Throw }; |
| 84 |
|
| 85 |
/** |
| 86 |
* ES2022 draft rev d03c1ec6e235a5180fa772b6178727c17974cb14 |
| 87 |
* |
| 88 |
* Await in async function |
| 89 |
* https://tc39.es/ecma262/#await |
| 90 |
* |
| 91 |
* Unified implementation of |
| 92 |
* |
| 93 |
* Step 3. fulfilledClosure Abstract Closure. |
| 94 |
* Step 5. rejectedClosure Abstract Closure. |
| 95 |
*/ |
| 96 |
static bool AsyncFunctionResume(JSContext* cx, |
| 97 |
Handle<AsyncFunctionGeneratorObject*> generator, |
| 98 |
ResumeKind kind, HandleValue valueOrReason) { |
| 99 |
// We're enqueuing the promise job for Await before suspending the execution |
| 100 |
// of the async function. So when either the debugger or OOM errors terminate |
| 101 |
// the execution after JSOp::AsyncAwait, but before JSOp::Await, we're in an |
| 102 |
// inconsistent state, because we don't have a resume index set and therefore |
| 103 |
// don't know where to resume the async function. Return here in that case. |
| 104 |
if (generator->isClosed()) { |
| 105 |
return true; |
| 106 |
} |
| 107 |
|
| 108 |
// The debugger sets the async function's generator object into the "running" |
| 109 |
// state while firing debugger events to ensure the debugger can't re-enter |
| 110 |
// the async function, cf. |AutoSetGeneratorRunning| in Debugger.cpp. Catch |
| 111 |
// this case here by checking if the generator is already runnning. |
| 112 |
if (generator->isRunning()) { |
| 113 |
return true; |
| 114 |
} |
| 115 |
|
| 116 |
Rooted<PromiseObject*> resultPromise(cx, generator->promise()); |
| 117 |
|
| 118 |
RootedObject stack(cx); |
| 119 |
Maybe<JS::AutoSetAsyncStackForNewCalls> asyncStack; |
| 120 |
if (JSObject* allocationSite = resultPromise->allocationSite()) { |
| 121 |
// The promise is created within the activation of the async function, so |
| 122 |
// use the parent frame as the starting point for async stacks. |
| 123 |
stack = allocationSite->as<SavedFrame>().getParent(); |
| 124 |
if (stack) { |
| 125 |
asyncStack.emplace( |
| 126 |
cx, stack, "async", |
| 127 |
JS::AutoSetAsyncStackForNewCalls::AsyncCallKind::EXPLICIT); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
MOZ_ASSERT(generator->isSuspended(), |
| 132 |
"non-suspended generator when resuming async function"); |
| 133 |
|
| 134 |
// Step {3,5}.a. Let prevContext be the running execution context. |
| 135 |
// Step {3,5}.b. Suspend prevContext. |
| 136 |
// Step {3,5}.c. Push asyncContext onto the execution context stack; |
| 137 |
// asyncContext is now the running execution context. |
| 138 |
// |
| 139 |
// fulfilledClosure |
| 140 |
// Step 3.d. Resume the suspended evaluation of asyncContext using |
| 141 |
// NormalCompletion(value) as the result of the operation that |
| 142 |
// suspended it. |
| 143 |
// |
| 144 |
// rejectedClosure |
| 145 |
// Step 5.d. Resume the suspended evaluation of asyncContext using |
| 146 |
// ThrowCompletion(reason) as the result of the operation that |
| 147 |
// suspended it. |
| 148 |
// |
| 149 |
// Execution context switching is handled in generator. |
| 150 |
Handle<PropertyName*> funName = kind == ResumeKind::Normal |
| 151 |
? cx->names().AsyncFunctionNext |
| 152 |
: cx->names().AsyncFunctionThrow; |
| 153 |
FixedInvokeArgs<1> args(cx); |
| 154 |
args[0].set(valueOrReason); |
| 155 |
RootedValue generatorOrValue(cx, ObjectValue(*generator)); |
| 156 |
if (!CallSelfHostedFunction(cx, funName, generatorOrValue, args, |
| 157 |
&generatorOrValue)) { |
| 158 |
if (!generator->isClosed()) { |
| 159 |
generator->setClosed(cx); |
| 160 |
} |
| 161 |
|
| 162 |
// Handle the OOM case mentioned above. |
| 163 |
if (resultPromise->state() == JS::PromiseState::Pending && |
| 164 |
cx->isExceptionPending()) { |
| 165 |
RootedValue exn(cx); |
| 166 |
if (!GetAndClearException(cx, &exn)) { |
| 167 |
return false; |
| 168 |
} |
| 169 |
return AsyncFunctionThrown(cx, resultPromise, exn); |
| 170 |
} |
| 171 |
return false; |
| 172 |
} |
| 173 |
|
| 174 |
// Step {3,f}.e. Assert: When we reach this step, asyncContext has already |
| 175 |
// been removed from the execution context stack and |
| 176 |
// prevContext is the currently running execution context. |
| 177 |
// Step {3,f}.f. Return undefined. |
| 178 |
MOZ_ASSERT_IF(generator->isClosed(), generatorOrValue.isObject()); |
| 179 |
MOZ_ASSERT_IF(generator->isClosed(), |
| 180 |
&generatorOrValue.toObject() == resultPromise); |
| 181 |
MOZ_ASSERT_IF(!generator->isClosed(), generator->isAfterAwait()); |
| 182 |
|
| 183 |
return true; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* ES2022 draft rev d03c1ec6e235a5180fa772b6178727c17974cb14 |
| 188 |
* |
| 189 |
* Await in async function |
| 190 |
* https://tc39.es/ecma262/#await |
| 191 |
* |
| 192 |
* Step 3. fulfilledClosure Abstract Closure. |
| 193 |
*/ |
| 194 |
[[nodiscard]] bool js::AsyncFunctionAwaitedFulfilled( |
| 195 |
JSContext* cx, Handle<AsyncFunctionGeneratorObject*> generator, |
| 196 |
HandleValue value) { |
| 197 |
return AsyncFunctionResume(cx, generator, ResumeKind::Normal, value); |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* ES2022 draft rev d03c1ec6e235a5180fa772b6178727c17974cb14 |
| 202 |
* |
| 203 |
* Await in async function |
| 204 |
* https://tc39.es/ecma262/#await |
| 205 |
* |
| 206 |
* Step 5. rejectedClosure Abstract Closure. |
| 207 |
*/ |
| 208 |
[[nodiscard]] bool js::AsyncFunctionAwaitedRejected( |
| 209 |
JSContext* cx, Handle<AsyncFunctionGeneratorObject*> generator, |
| 210 |
HandleValue reason) { |
| 211 |
return AsyncFunctionResume(cx, generator, ResumeKind::Throw, reason); |
| 212 |
} |
| 213 |
|
| 214 |
JSObject* js::AsyncFunctionResolve( |
| 215 |
JSContext* cx, Handle<AsyncFunctionGeneratorObject*> generator, |
| 216 |
HandleValue value) { |
| 217 |
Rooted<PromiseObject*> promise(cx, generator->promise()); |
| 218 |
if (!AsyncFunctionReturned(cx, promise, value)) { |
| 219 |
return nullptr; |
| 220 |
} |
| 221 |
return promise; |
| 222 |
} |
| 223 |
|
| 224 |
JSObject* js::AsyncFunctionReject( |
| 225 |
JSContext* cx, Handle<AsyncFunctionGeneratorObject*> generator, |
| 226 |
HandleValue reason, HandleValue stack) { |
| 227 |
MOZ_ASSERT(stack.isObjectOrNull()); |
| 228 |
Rooted<PromiseObject*> promise(cx, generator->promise()); |
| 229 |
Rooted<SavedFrame*> unwrappedRejectionStack(cx); |
| 230 |
if (stack.isObject()) { |
| 231 |
MOZ_ASSERT(UncheckedUnwrap(&stack.toObject())->is<SavedFrame>() || |
| 232 |
IsDeadProxyObject(&stack.toObject())); |
| 233 |
unwrappedRejectionStack = stack.toObject().maybeUnwrapIf<SavedFrame>(); |
| 234 |
} |
| 235 |
if (!AsyncFunctionThrown(cx, promise, reason, unwrappedRejectionStack)) { |
| 236 |
return nullptr; |
| 237 |
} |
| 238 |
return promise; |
| 239 |
} |
| 240 |
|
| 241 |
const JSClass AsyncFunctionGeneratorObject::class_ = { |
| 242 |
"AsyncFunctionGenerator", |
| 243 |
JSCLASS_HAS_RESERVED_SLOTS(AsyncFunctionGeneratorObject::RESERVED_SLOTS), |
| 244 |
&classOps_, |
| 245 |
}; |
| 246 |
|
| 247 |
const JSClassOps AsyncFunctionGeneratorObject::classOps_ = { |
| 248 |
nullptr, // addProperty |
| 249 |
nullptr, // delProperty |
| 250 |
nullptr, // enumerate |
| 251 |
nullptr, // newEnumerate |
| 252 |
nullptr, // resolve |
| 253 |
nullptr, // mayResolve |
| 254 |
nullptr, // finalize |
| 255 |
nullptr, // call |
| 256 |
nullptr, // construct |
| 257 |
CallTraceMethod<AbstractGeneratorObject>, // trace |
| 258 |
}; |
| 259 |
|
| 260 |
AsyncFunctionGeneratorObject* AsyncFunctionGeneratorObject::create( |
| 261 |
JSContext* cx, HandleFunction fun) { |
| 262 |
MOZ_ASSERT(fun->isAsync() && !fun->isGenerator()); |
| 263 |
|
| 264 |
Rooted<PromiseObject*> resultPromise(cx, CreatePromiseObjectForAsync(cx)); |
| 265 |
if (!resultPromise) { |
| 266 |
return nullptr; |
| 267 |
} |
| 268 |
|
| 269 |
auto* obj = NewBuiltinClassInstance<AsyncFunctionGeneratorObject>(cx); |
| 270 |
if (!obj) { |
| 271 |
return nullptr; |
| 272 |
} |
| 273 |
obj->initFixedSlot(PROMISE_SLOT, ObjectValue(*resultPromise)); |
| 274 |
|
| 275 |
// Starts in the running state. |
| 276 |
obj->setResumeIndex(AbstractGeneratorObject::RESUME_INDEX_RUNNING); |
| 277 |
|
| 278 |
return obj; |
| 279 |
} |
| 280 |
|
| 281 |
JSFunction* NewHandler(JSContext* cx, Native handler, |
| 282 |
JS::Handle<JSObject*> target) { |
| 283 |
cx->check(target); |
| 284 |
|
| 285 |
JS::Handle<PropertyName*> funName = cx->names().empty_; |
| 286 |
JS::Rooted<JSFunction*> handlerFun( |
| 287 |
cx, NewNativeFunction(cx, handler, 0, funName, |
| 288 |
gc::AllocKind::FUNCTION_EXTENDED, GenericObject)); |
| 289 |
if (!handlerFun) { |
| 290 |
return nullptr; |
| 291 |
} |
| 292 |
handlerFun->setExtendedSlot(FunctionExtended::MODULE_SLOT, |
| 293 |
JS::ObjectValue(*target)); |
| 294 |
return handlerFun; |
| 295 |
} |
| 296 |
|
| 297 |
static bool AsyncModuleExecutionFulfilledHandler(JSContext* cx, unsigned argc, |
| 298 |
Value* vp) { |
| 299 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 300 |
JSFunction& func = args.callee().as<JSFunction>(); |
| 301 |
|
| 302 |
Rooted<ModuleObject*> module( |
| 303 |
cx, &func.getExtendedSlot(FunctionExtended::MODULE_SLOT) |
| 304 |
.toObject() |
| 305 |
.as<ModuleObject>()); |
| 306 |
AsyncModuleExecutionFulfilled(cx, module); |
| 307 |
args.rval().setUndefined(); |
| 308 |
return true; |
| 309 |
} |
| 310 |
|
| 311 |
static bool AsyncModuleExecutionRejectedHandler(JSContext* cx, unsigned argc, |
| 312 |
Value* vp) { |
| 313 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 314 |
JSFunction& func = args.callee().as<JSFunction>(); |
| 315 |
Rooted<ModuleObject*> module( |
| 316 |
cx, &func.getExtendedSlot(FunctionExtended::MODULE_SLOT) |
| 317 |
.toObject() |
| 318 |
.as<ModuleObject>()); |
| 319 |
args.rval().setUndefined(); |
| 320 |
return AsyncModuleExecutionRejected(cx, module, args.get(0)); |
| 321 |
} |
| 322 |
|
| 323 |
AsyncFunctionGeneratorObject* AsyncFunctionGeneratorObject::create( |
| 324 |
JSContext* cx, Handle<ModuleObject*> module) { |
| 325 |
// TODO: Module is currently hitching a ride with |
| 326 |
// AsyncFunctionGeneratorObject. The reason for this is we have some work in |
| 327 |
// the JITs that make use of this object when we hit AsyncAwait bytecode. At |
| 328 |
// the same time, top level await shares a lot of it's implementation with |
| 329 |
// AsyncFunction. I am not sure if the best thing to do here is inherit, |
| 330 |
// override, or do something else. Comments appreciated. |
| 331 |
MOZ_ASSERT(module->script()->isAsync()); |
| 332 |
|
| 333 |
Rooted<PromiseObject*> resultPromise(cx, CreatePromiseObjectForAsync(cx)); |
| 334 |
if (!resultPromise) { |
| 335 |
return nullptr; |
| 336 |
} |
| 337 |
|
| 338 |
Rooted<AsyncFunctionGeneratorObject*> obj( |
| 339 |
cx, NewBuiltinClassInstance<AsyncFunctionGeneratorObject>(cx)); |
| 340 |
if (!obj) { |
| 341 |
return nullptr; |
| 342 |
} |
| 343 |
obj->initFixedSlot(PROMISE_SLOT, ObjectValue(*resultPromise)); |
| 344 |
|
| 345 |
RootedObject onFulfilled( |
| 346 |
cx, NewHandler(cx, AsyncModuleExecutionFulfilledHandler, module)); |
| 347 |
if (!onFulfilled) { |
| 348 |
return nullptr; |
| 349 |
} |
| 350 |
|
| 351 |
RootedObject onRejected( |
| 352 |
cx, NewHandler(cx, AsyncModuleExecutionRejectedHandler, module)); |
| 353 |
if (!onRejected) { |
| 354 |
return nullptr; |
| 355 |
} |
| 356 |
|
| 357 |
if (!JS::AddPromiseReactionsIgnoringUnhandledRejection( |
| 358 |
cx, resultPromise, onFulfilled, onRejected)) { |
| 359 |
return nullptr; |
| 360 |
} |
| 361 |
|
| 362 |
// Starts in the running state. |
| 363 |
obj->setResumeIndex(AbstractGeneratorObject::RESUME_INDEX_RUNNING); |
| 364 |
|
| 365 |
return obj; |
| 366 |
} |