Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 the V8 project authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "src/builtins/builtins-async.h" | |
| 6 #include "src/builtins/builtins-utils.h" | |
| 7 #include "src/builtins/builtins.h" | |
| 8 #include "src/code-factory.h" | |
| 9 #include "src/code-stub-assembler.h" | |
| 10 #include "src/frames-inl.h" | |
| 11 | |
| 12 namespace v8 { | |
| 13 namespace internal { | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 // Describe fields of Context associated with the AsyncIterator unwrap closure. | |
| 18 class ValueUnwrapContext { | |
| 19 public: | |
| 20 enum Fields { kDoneSlot = Context::MIN_CONTEXT_SLOTS, kLength }; | |
| 21 }; | |
| 22 | |
| 23 class AsyncFromSyncBuiltinsAssembler : public AsyncBuiltinsAssembler { | |
| 24 public: | |
| 25 explicit AsyncFromSyncBuiltinsAssembler(CodeAssemblerState* state) | |
| 26 : AsyncBuiltinsAssembler(state) {} | |
| 27 | |
| 28 std::pair<Node*, Node*> LoadIteratorResult(Node* const context, | |
|
jgruber
2017/02/17 12:28:23
Let's document what this returns.
caitp
2017/02/17 14:56:10
Done.
| |
| 29 Node* const native_context, | |
| 30 Node* const iter_result, | |
| 31 Label* if_exception, | |
| 32 Variable* var_exception); | |
| 33 | |
| 34 Node* CreateUnwrapClosure(Node* native_context, Node* done); | |
|
jgruber
2017/02/17 12:28:23
Nit: I think it'd help readability if we stay cons
| |
| 35 }; | |
| 36 | |
| 37 std::pair<Node*, Node*> AsyncFromSyncBuiltinsAssembler::LoadIteratorResult( | |
| 38 Node* const context, Node* const native_context, Node* const iter_result, | |
| 39 Label* if_exception, Variable* var_exception) { | |
| 40 Label if_fastpath(this), if_slowpath(this), merge(this), to_boolean(this), | |
| 41 done(this); | |
| 42 GotoIf(TaggedIsSmi(iter_result), &if_slowpath); | |
| 43 | |
| 44 Node* const fast_iter_result_map = | |
| 45 LoadContextElement(native_context, Context::ITERATOR_RESULT_MAP_INDEX); | |
| 46 Node* const iter_result_map = LoadMap(iter_result); | |
| 47 | |
| 48 Variable var_value(this, MachineRepresentation::kTagged); | |
| 49 Variable var_done(this, MachineRepresentation::kTagged); | |
| 50 Branch(WordEqual(iter_result_map, fast_iter_result_map), &if_fastpath, | |
| 51 &if_slowpath); | |
| 52 | |
| 53 Bind(&if_fastpath); | |
| 54 { | |
| 55 var_value.Bind( | |
| 56 LoadObjectField(iter_result, JSIteratorResult::kValueOffset)); | |
| 57 var_done.Bind(LoadObjectField(iter_result, JSIteratorResult::kDoneOffset)); | |
| 58 Goto(&merge); | |
| 59 } | |
| 60 | |
| 61 Bind(&if_slowpath); | |
| 62 { | |
| 63 // Let nextValue be IteratorValue(nextResult). | |
| 64 // IfAbruptRejectPromise(nextValue, promiseCapability). | |
| 65 Node* const value = | |
| 66 GetProperty(context, iter_result, factory()->value_string()); | |
| 67 GotoIfException(value, if_exception, var_exception); | |
| 68 | |
| 69 // Let nextDone be IteratorComplete(nextResult). | |
| 70 // IfAbruptRejectPromise(nextDone, promiseCapability). | |
| 71 Node* const done = | |
| 72 GetProperty(context, iter_result, factory()->done_string()); | |
| 73 GotoIfException(done, if_exception, var_exception); | |
| 74 | |
| 75 var_value.Bind(value); | |
| 76 var_done.Bind(done); | |
| 77 Goto(&merge); | |
| 78 } | |
| 79 | |
| 80 Bind(&merge); | |
| 81 // Ensure `iterResult.done` is a Boolean. | |
| 82 GotoIf(TaggedIsSmi(var_done.value()), &to_boolean); | |
| 83 Branch(IsBooleanMap(LoadMap(var_done.value())), &done, &to_boolean); | |
|
jgruber
2017/02/17 12:28:23
Nit: Looks like we have IsBoolean() now.
caitp
2017/02/17 14:56:10
Done.
| |
| 84 | |
| 85 Bind(&to_boolean); | |
| 86 { | |
| 87 Node* const result = | |
| 88 CallStub(CodeFactory::ToBoolean(isolate()), context, var_done.value()); | |
| 89 var_done.Bind(result); | |
| 90 Goto(&done); | |
| 91 } | |
| 92 | |
| 93 Bind(&done); | |
| 94 return std::make_pair(var_value.value(), var_done.value()); | |
| 95 } | |
| 96 | |
| 97 Node* AsyncFromSyncBuiltinsAssembler::CreateUnwrapClosure(Node* native_context, | |
| 98 Node* done) { | |
| 99 Node* const map = LoadContextElement( | |
| 100 native_context, Context::STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX); | |
| 101 Node* const on_resolve_shared = LoadContextElement( | |
| 102 native_context, Context::ASYNC_ITERATOR_VALUE_UNWRAP_SHARED_FUN); | |
| 103 CSA_ASSERT(this, | |
| 104 HasInstanceType(on_resolve_shared, SHARED_FUNCTION_INFO_TYPE)); | |
| 105 Node* const closure_context = | |
| 106 AllocateAsyncIteratorValueUnwrapContext(native_context, done); | |
| 107 return AllocateFunctionWithMapAndContext(map, on_resolve_shared, | |
| 108 closure_context); | |
| 109 } | |
| 110 } // namespace | |
| 111 | |
| 112 // https://tc39.github.io/proposal-async-iteration/ | |
| 113 // Section #sec-%asyncfromsynciteratorprototype%.next | |
| 114 TF_BUILTIN(AsyncFromSyncIteratorPrototypeNext, AsyncFromSyncBuiltinsAssembler) { | |
| 115 const char* method_name = "[Async-from-Sync Iterator].prototype.next"; | |
| 116 | |
| 117 Node* const iterator = Parameter(0); | |
| 118 Node* const parameter = Parameter(1); | |
| 119 Node* const context = Parameter(4); | |
| 120 | |
| 121 Node* const native_context = LoadNativeContext(context); | |
| 122 Node* const promise = AllocateAndInitJSPromise(context); | |
| 123 | |
| 124 Variable var_exception(this, MachineRepresentation::kTagged); | |
|
jgruber
2017/02/17 12:28:23
Nit: var_exception(this, MR::kTagged, UndefinedCon
caitp
2017/02/17 14:56:10
Done.
| |
| 125 var_exception.Bind(UndefinedConstant()); | |
| 126 Label reject_promise(this, Label::kDeferred); | |
| 127 Label if_receiverisincompatible(this, Label::kDeferred); | |
| 128 | |
| 129 GotoIf(TaggedIsSmi(iterator), &if_receiverisincompatible); | |
| 130 GotoUnless(HasInstanceType(iterator, JS_ASYNC_FROM_SYNC_ITERATOR_TYPE), | |
| 131 &if_receiverisincompatible); | |
| 132 | |
| 133 Node* const sync_iterator = | |
| 134 LoadObjectField(iterator, JSAsyncFromSyncIterator::kSyncIteratorOffset); | |
| 135 | |
| 136 // Let nextResult be IteratorNext(syncIterator, value). | |
| 137 // IfAbruptRejectPromise(nextResult, promiseCapability). | |
| 138 Node* next_method = | |
| 139 GetProperty(context, sync_iterator, factory()->next_string()); | |
| 140 | |
| 141 Node* const iter_result = CallJS(CodeFactory::Call(isolate()), context, | |
| 142 next_method, sync_iterator, parameter); | |
| 143 GotoIfException(iter_result, &reject_promise, &var_exception); | |
| 144 | |
| 145 Node* value; | |
| 146 Node* done; | |
| 147 std::tie(value, done) = LoadIteratorResult( | |
| 148 context, native_context, iter_result, &reject_promise, &var_exception); | |
| 149 | |
| 150 // Let valueWrapperCapability be ! NewPromiseCapability(%Promise%). | |
| 151 Node* const wrapper = AllocateAndInitJSPromise(context); | |
| 152 | |
| 153 // Perform ! Call(valueWrapperCapability.[[Resolve]], undefined, | |
| 154 // « nextValue »). | |
| 155 InternalResolvePromise(context, wrapper, value); | |
| 156 | |
| 157 // Let onFulfilled be a new built-in function object as defined in | |
| 158 // Async Iterator Value Unwrap Functions. | |
| 159 // Set onFulfilled.[[Done]] to nextDone. | |
| 160 Node* const on_resolve = CreateUnwrapClosure(native_context, done); | |
| 161 | |
| 162 // Perform ! PerformPromiseThen(valueWrapperCapability.[[Promise]], | |
| 163 // onFulfilled, undefined, promiseCapability). | |
| 164 Node* const undefined = UndefinedConstant(); | |
| 165 InternalPerformPromiseThen(context, wrapper, on_resolve, undefined, promise, | |
| 166 undefined, undefined); | |
| 167 Return(promise); | |
| 168 | |
| 169 Bind(&if_receiverisincompatible); | |
| 170 { | |
| 171 // If Type(O) is not Object, or if O does not have a [[SyncIterator]] | |
| 172 // internal slot, then | |
| 173 | |
| 174 // Let badIteratorError be a new TypeError exception. | |
| 175 Node* const error = | |
| 176 MakeTypeError(MessageTemplate::kIncompatibleMethodReceiver, context, | |
| 177 CStringConstant(method_name), iterator); | |
| 178 | |
| 179 // Perform ! Call(promiseCapability.[[Reject]], undefined, | |
| 180 // « badIteratorError »). | |
| 181 var_exception.Bind(error); | |
| 182 Goto(&reject_promise); | |
| 183 } | |
| 184 | |
| 185 Bind(&reject_promise); | |
| 186 { | |
| 187 Node* const promise = AllocateAndInitJSPromise(context); | |
|
jgruber
2017/02/17 12:28:23
Doesn't the proposal say to reject promiseCapabili
caitp
2017/02/17 14:56:10
You're right, and I have no idea why there was a n
| |
| 188 Node* const exception = var_exception.value(); | |
| 189 InternalPromiseReject(context, promise, exception, TrueConstant()); | |
| 190 | |
| 191 // Return promiseCapability.[[Promise]]. | |
| 192 Return(promise); | |
| 193 } | |
| 194 } | |
| 195 | |
| 196 // https://tc39.github.io/proposal-async-iteration/ | |
| 197 // Section #sec-%asyncfromsynciteratorprototype%.return | |
| 198 TF_BUILTIN(AsyncFromSyncIteratorPrototypeReturn, | |
| 199 AsyncFromSyncBuiltinsAssembler) { | |
| 200 const char* method_name = "[Async-from-Sync Iterator].prototype.return"; | |
| 201 | |
| 202 Node* const iterator = Parameter(0); | |
| 203 Node* const parameter = Parameter(1); | |
| 204 Node* const context = Parameter(4); | |
| 205 | |
| 206 Node* const native_context = LoadNativeContext(context); | |
| 207 Node* const promise = AllocateAndInitJSPromise(context); | |
| 208 | |
| 209 Variable var_exception(this, MachineRepresentation::kTagged); | |
|
jgruber
2017/02/17 12:28:23
Ditto
caitp
2017/02/17 14:56:10
Done.
| |
| 210 var_exception.Bind(UndefinedConstant()); | |
| 211 Label reject_promise(this, Label::kDeferred); | |
| 212 Label if_receiverisincompatible(this, Label::kDeferred); | |
| 213 | |
| 214 GotoIf(TaggedIsSmi(iterator), &if_receiverisincompatible); | |
| 215 GotoUnless(HasInstanceType(iterator, JS_ASYNC_FROM_SYNC_ITERATOR_TYPE), | |
| 216 &if_receiverisincompatible); | |
| 217 | |
| 218 Node* const sync_iterator = | |
| 219 LoadObjectField(iterator, JSAsyncFromSyncIterator::kSyncIteratorOffset); | |
| 220 | |
| 221 // Let return be GetMethod(syncIterator, "return"). | |
| 222 // IfAbruptRejectPromise(return, promiseCapability). | |
| 223 Node* const return_method = | |
| 224 GetProperty(context, sync_iterator, factory()->return_string()); | |
| 225 GotoIfException(return_method, &reject_promise, &var_exception); | |
| 226 | |
| 227 Variable var_value(this, MachineRepresentation::kTagged); | |
| 228 Variable var_done(this, MachineRepresentation::kTagged); | |
| 229 | |
| 230 Label if_isundefined(this), if_isnotundefined(this), resolve_promise(this); | |
| 231 | |
| 232 Branch(IsUndefined(return_method), &if_isundefined, &if_isnotundefined); | |
| 233 Bind(&if_isundefined); | |
| 234 { | |
| 235 // If return is undefined, then | |
| 236 // Let iterResult be ! CreateIterResultObject(value, true) | |
| 237 Node* const iter_result = | |
| 238 CallStub(CodeFactory::CreateIterResultObject(isolate()), context, | |
| 239 parameter, TrueConstant()); | |
| 240 | |
| 241 // Perform ! Call(promiseCapability.[[Resolve]], undefined, « iterResult »). | |
| 242 // IfAbruptRejectPromise(nextDone, promiseCapability). | |
| 243 // Return promiseCapability.[[Promise]]. | |
| 244 PromiseFulfill(context, promise, iter_result, v8::Promise::kFulfilled); | |
| 245 Return(promise); | |
| 246 } | |
| 247 | |
| 248 Bind(&if_isnotundefined); | |
| 249 { | |
| 250 // Let returnResult be Call(return, syncIterator, « value »). | |
| 251 Node* const iter_result = CallJS(CodeFactory::Call(isolate()), context, | |
| 252 return_method, sync_iterator, parameter); | |
| 253 // IfAbruptRejectPromise(returnResult, promiseCapability). | |
| 254 GotoIfException(iter_result, &reject_promise, &var_exception); | |
| 255 | |
| 256 Node* value; | |
| 257 Node* done; | |
| 258 std::tie(value, done) = LoadIteratorResult( | |
| 259 context, native_context, iter_result, &reject_promise, &var_exception); | |
| 260 | |
| 261 var_value.Bind(value); | |
| 262 var_done.Bind(done); | |
| 263 Goto(&resolve_promise); | |
| 264 } | |
| 265 | |
| 266 Bind(&resolve_promise); | |
| 267 { | |
| 268 // Let valueWrapperCapability be ! NewPromiseCapability(%Promise%). | |
| 269 Node* const wrapper = AllocateAndInitJSPromise(context); | |
| 270 Node* const value = var_value.value(); | |
| 271 Node* const done = var_done.value(); | |
| 272 | |
| 273 // Perform ! Call(valueWrapperCapability.[[Resolve]], undefined, | |
| 274 // « nextValue »). | |
| 275 InternalResolvePromise(context, wrapper, value); | |
| 276 | |
| 277 // Let onFulfilled be a new built-in function object as defined in | |
| 278 // Async Iterator Value Unwrap Functions. | |
| 279 // Set onFulfilled.[[Done]] to nextDone. | |
| 280 Node* const on_resolve = CreateUnwrapClosure(native_context, done); | |
| 281 | |
| 282 // Perform ! PerformPromiseThen(valueWrapperCapability.[[Promise]], | |
| 283 // onFulfilled, undefined, promiseCapability). | |
| 284 Node* const undefined = UndefinedConstant(); | |
| 285 InternalPerformPromiseThen(context, wrapper, on_resolve, undefined, promise, | |
| 286 undefined, undefined); | |
| 287 Return(promise); | |
| 288 } | |
| 289 | |
| 290 Bind(&if_receiverisincompatible); | |
|
jgruber
2017/02/17 12:28:23
I think you could refactor this into a ThrowIfNotA
caitp
2017/02/17 14:56:10
Done.
| |
| 291 { | |
| 292 // If Type(O) is not Object, or if O does not have a [[SyncIterator]] | |
| 293 // internal slot, then | |
| 294 | |
| 295 // Let badIteratorError be a new TypeError exception. | |
| 296 Node* const error = | |
| 297 MakeTypeError(MessageTemplate::kIncompatibleMethodReceiver, context, | |
| 298 CStringConstant(method_name), iterator); | |
| 299 | |
| 300 // Perform ! Call(promiseCapability.[[Reject]], undefined, | |
| 301 // « badIteratorError »). | |
| 302 var_exception.Bind(error); | |
| 303 Goto(&reject_promise); | |
| 304 } | |
| 305 | |
| 306 Bind(&reject_promise); | |
| 307 { | |
| 308 Node* const exception = var_exception.value(); | |
| 309 InternalPromiseReject(context, promise, exception, TrueConstant()); | |
| 310 | |
| 311 // Return promiseCapability.[[Promise]]. | |
| 312 Return(promise); | |
| 313 } | |
| 314 } | |
| 315 | |
| 316 // https://tc39.github.io/proposal-async-iteration/ | |
| 317 // Section #sec-%asyncfromsynciteratorprototype%.throw | |
| 318 TF_BUILTIN(AsyncFromSyncIteratorPrototypeThrow, | |
| 319 AsyncFromSyncBuiltinsAssembler) { | |
| 320 const char* method_name = "[Async-from-Sync Iterator].prototype.throw"; | |
| 321 | |
| 322 Node* const iterator = Parameter(0); | |
| 323 Node* const parameter = Parameter(1); | |
| 324 Node* const context = Parameter(4); | |
| 325 | |
| 326 Node* const native_context = LoadNativeContext(context); | |
| 327 Node* const promise = AllocateAndInitJSPromise(context); | |
| 328 | |
| 329 Variable var_exception(this, MachineRepresentation::kTagged); | |
| 330 var_exception.Bind(parameter); | |
|
jgruber
2017/02/17 12:28:23
Ditto
caitp
2017/02/17 14:56:10
Done.
| |
| 331 Variable var_value(this, MachineRepresentation::kTagged); | |
| 332 Variable var_done(this, MachineRepresentation::kTagged); | |
| 333 Label reject_promise(this, Label::kDeferred); | |
| 334 Label if_receiverisincompatible(this, Label::kDeferred); | |
| 335 | |
| 336 GotoIf(TaggedIsSmi(iterator), &if_receiverisincompatible); | |
| 337 GotoUnless(HasInstanceType(iterator, JS_ASYNC_FROM_SYNC_ITERATOR_TYPE), | |
| 338 &if_receiverisincompatible); | |
| 339 | |
| 340 Node* const sync_iterator = | |
| 341 LoadObjectField(iterator, JSAsyncFromSyncIterator::kSyncIteratorOffset); | |
| 342 | |
| 343 // Let throw be GetMethod(syncIterator, "throw"). | |
| 344 // IfAbruptRejectPromise(nextResult, promiseCapability). | |
| 345 // IfAbruptRejectPromise(throw, promiseCapability). | |
| 346 Node* const throw_method = | |
| 347 GetProperty(context, sync_iterator, factory()->throw_string()); | |
| 348 GotoIfException(throw_method, &reject_promise, &var_exception); | |
| 349 | |
| 350 Label if_isnotundefined(this), resolve_promise(this); | |
| 351 | |
| 352 // If throw is undefined, then | |
| 353 // Perform ! Call(promiseCapability.[[Reject]], undefined, « value »). | |
| 354 Branch(IsUndefined(throw_method), &reject_promise, &if_isnotundefined); | |
| 355 | |
| 356 Bind(&if_isnotundefined); | |
| 357 { | |
| 358 // Let throwResult be Call(throw, syncIterator, « value »). | |
| 359 Node* const iter_result = CallJS(CodeFactory::Call(isolate()), context, | |
| 360 throw_method, sync_iterator, parameter); | |
| 361 GotoIfException(iter_result, &reject_promise, &var_exception); | |
| 362 | |
| 363 Node* value; | |
| 364 Node* done; | |
| 365 std::tie(value, done) = LoadIteratorResult( | |
| 366 context, native_context, iter_result, &reject_promise, &var_exception); | |
| 367 | |
| 368 // Let valueWrapperCapability be ! NewPromiseCapability(%Promise%). | |
| 369 Node* const wrapper = AllocateAndInitJSPromise(context); | |
| 370 | |
| 371 // Perform ! Call(valueWrapperCapability.[[Resolve]], undefined, « | |
| 372 // throwValue »). | |
| 373 InternalResolvePromise(context, wrapper, value); | |
| 374 | |
| 375 // Let onFulfilled be a new built-in function object as defined in | |
| 376 // Async Iterator Value Unwrap Functions. | |
| 377 // Set onFulfilled.[[Done]] to throwDone. | |
| 378 Node* const on_resolve = CreateUnwrapClosure(native_context, done); | |
| 379 | |
| 380 // Perform ! PerformPromiseThen(valueWrapperCapability.[[Promise]], | |
| 381 // onFulfilled, undefined, promiseCapability). | |
| 382 Node* const undefined = UndefinedConstant(); | |
| 383 InternalPerformPromiseThen(context, wrapper, on_resolve, undefined, promise, | |
| 384 undefined, undefined); | |
| 385 Return(promise); | |
| 386 } | |
| 387 | |
| 388 Bind(&if_receiverisincompatible); | |
| 389 { | |
| 390 // If Type(O) is not Object, or if O does not have a [[SyncIterator]] | |
| 391 // internal slot, then | |
| 392 | |
| 393 // Let badIteratorError be a new TypeError exception. | |
| 394 Node* const error = | |
| 395 MakeTypeError(MessageTemplate::kIncompatibleMethodReceiver, context, | |
| 396 CStringConstant(method_name), iterator); | |
| 397 | |
| 398 // Perform ! Call(promiseCapability.[[Reject]], undefined, | |
| 399 // « badIteratorError »). | |
| 400 var_exception.Bind(error); | |
| 401 Goto(&reject_promise); | |
| 402 } | |
| 403 | |
| 404 Bind(&reject_promise); | |
| 405 { | |
| 406 Node* const exception = var_exception.value(); | |
| 407 InternalPromiseReject(context, promise, exception, TrueConstant()); | |
| 408 | |
| 409 // Return promiseCapability.[[Promise]]. | |
| 410 Return(promise); | |
| 411 } | |
| 412 } | |
| 413 | |
| 414 TF_BUILTIN(AsyncIteratorValueUnwrap, AsyncBuiltinsAssembler) { | |
| 415 Node* const value = Parameter(1); | |
| 416 Node* const context = Parameter(4); | |
| 417 | |
| 418 Node* const done = LoadContextElement(context, ValueUnwrapContext::kDoneSlot); | |
|
jgruber
2017/02/17 12:28:23
CSA_ASSERT(IsBoolean)
caitp
2017/02/17 14:56:10
Done.
| |
| 419 Node* const unwrapped_value = CallStub( | |
| 420 CodeFactory::CreateIterResultObject(isolate()), context, value, done); | |
| 421 | |
| 422 Return(unwrapped_value); | |
| 423 } | |
| 424 | |
| 425 Node* AsyncBuiltinsAssembler::AllocateAsyncIteratorValueUnwrapContext( | |
| 426 Node* native_context, Node* done) { | |
|
jgruber
2017/02/17 12:28:23
Please add a CSA_ASSERT(IsNativeContext && IsBoole
caitp
2017/02/17 14:56:10
Done.
| |
| 427 Node* const context = | |
| 428 CreatePromiseContext(native_context, ValueUnwrapContext::kLength); | |
| 429 StoreContextElementNoWriteBarrier(context, ValueUnwrapContext::kDoneSlot, | |
| 430 done); | |
| 431 return context; | |
| 432 } | |
| 433 | |
| 434 } // namespace internal | |
| 435 } // namespace v8 | |
| OLD | NEW |