OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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.h" |
| 6 #include "src/builtins/builtins-utils.h" |
| 7 |
| 8 #include "src/code-factory.h" |
| 9 |
| 10 namespace v8 { |
| 11 namespace internal { |
| 12 |
| 13 namespace { |
| 14 |
| 15 void Generate_GeneratorPrototypeResume( |
| 16 CodeStubAssembler* assembler, JSGeneratorObject::ResumeMode resume_mode, |
| 17 char const* const method_name) { |
| 18 typedef CodeStubAssembler::Label Label; |
| 19 typedef compiler::Node Node; |
| 20 |
| 21 Node* receiver = assembler->Parameter(0); |
| 22 Node* value = assembler->Parameter(1); |
| 23 Node* context = assembler->Parameter(4); |
| 24 Node* closed = |
| 25 assembler->SmiConstant(Smi::FromInt(JSGeneratorObject::kGeneratorClosed)); |
| 26 |
| 27 // Check if the {receiver} is actually a JSGeneratorObject. |
| 28 Label if_receiverisincompatible(assembler, Label::kDeferred); |
| 29 assembler->GotoIf(assembler->WordIsSmi(receiver), &if_receiverisincompatible); |
| 30 Node* receiver_instance_type = assembler->LoadInstanceType(receiver); |
| 31 assembler->GotoUnless(assembler->Word32Equal( |
| 32 receiver_instance_type, |
| 33 assembler->Int32Constant(JS_GENERATOR_OBJECT_TYPE)), |
| 34 &if_receiverisincompatible); |
| 35 |
| 36 // Check if the {receiver} is running or already closed. |
| 37 Node* receiver_continuation = assembler->LoadObjectField( |
| 38 receiver, JSGeneratorObject::kContinuationOffset); |
| 39 Label if_receiverisclosed(assembler, Label::kDeferred), |
| 40 if_receiverisrunning(assembler, Label::kDeferred); |
| 41 assembler->GotoIf(assembler->SmiEqual(receiver_continuation, closed), |
| 42 &if_receiverisclosed); |
| 43 DCHECK_LT(JSGeneratorObject::kGeneratorExecuting, |
| 44 JSGeneratorObject::kGeneratorClosed); |
| 45 assembler->GotoIf(assembler->SmiLessThan(receiver_continuation, closed), |
| 46 &if_receiverisrunning); |
| 47 |
| 48 // Resume the {receiver} using our trampoline. |
| 49 Node* result = assembler->CallStub( |
| 50 CodeFactory::ResumeGenerator(assembler->isolate()), context, value, |
| 51 receiver, assembler->SmiConstant(Smi::FromInt(resume_mode))); |
| 52 assembler->Return(result); |
| 53 |
| 54 assembler->Bind(&if_receiverisincompatible); |
| 55 { |
| 56 // The {receiver} is not a valid JSGeneratorObject. |
| 57 Node* result = assembler->CallRuntime( |
| 58 Runtime::kThrowIncompatibleMethodReceiver, context, |
| 59 assembler->HeapConstant(assembler->factory()->NewStringFromAsciiChecked( |
| 60 method_name, TENURED)), |
| 61 receiver); |
| 62 assembler->Return(result); // Never reached. |
| 63 } |
| 64 |
| 65 assembler->Bind(&if_receiverisclosed); |
| 66 { |
| 67 // The {receiver} is closed already. |
| 68 Node* result = nullptr; |
| 69 switch (resume_mode) { |
| 70 case JSGeneratorObject::kNext: |
| 71 result = assembler->CallRuntime(Runtime::kCreateIterResultObject, |
| 72 context, assembler->UndefinedConstant(), |
| 73 assembler->BooleanConstant(true)); |
| 74 break; |
| 75 case JSGeneratorObject::kReturn: |
| 76 result = |
| 77 assembler->CallRuntime(Runtime::kCreateIterResultObject, context, |
| 78 value, assembler->BooleanConstant(true)); |
| 79 break; |
| 80 case JSGeneratorObject::kThrow: |
| 81 result = assembler->CallRuntime(Runtime::kThrow, context, value); |
| 82 break; |
| 83 } |
| 84 assembler->Return(result); |
| 85 } |
| 86 |
| 87 assembler->Bind(&if_receiverisrunning); |
| 88 { |
| 89 Node* result = |
| 90 assembler->CallRuntime(Runtime::kThrowGeneratorRunning, context); |
| 91 assembler->Return(result); // Never reached. |
| 92 } |
| 93 } |
| 94 |
| 95 } // anonymous namespace |
| 96 |
| 97 // ES6 section 25.3.1.2 Generator.prototype.next ( value ) |
| 98 void Builtins::Generate_GeneratorPrototypeNext(CodeStubAssembler* assembler) { |
| 99 Generate_GeneratorPrototypeResume(assembler, JSGeneratorObject::kNext, |
| 100 "[Generator].prototype.next"); |
| 101 } |
| 102 |
| 103 // ES6 section 25.3.1.3 Generator.prototype.return ( value ) |
| 104 void Builtins::Generate_GeneratorPrototypeReturn(CodeStubAssembler* assembler) { |
| 105 Generate_GeneratorPrototypeResume(assembler, JSGeneratorObject::kReturn, |
| 106 "[Generator].prototype.return"); |
| 107 } |
| 108 |
| 109 // ES6 section 25.3.1.4 Generator.prototype.throw ( exception ) |
| 110 void Builtins::Generate_GeneratorPrototypeThrow(CodeStubAssembler* assembler) { |
| 111 Generate_GeneratorPrototypeResume(assembler, JSGeneratorObject::kThrow, |
| 112 "[Generator].prototype.throw"); |
| 113 } |
| 114 |
| 115 } // namespace internal |
| 116 } // namespace v8 |
OLD | NEW |