1 // Copyright 2014 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.
7 #include "src/arguments.h"
8 #include "src/frames-inl.h"
9 #include "src/runtime/runtime-utils.h"
14 RUNTIME_FUNCTION(Runtime_CreateJSGeneratorObject) {
15 HandleScope scope(isolate);
16 DCHECK(args.length() == 0);
18 JavaScriptFrameIterator it(isolate);
19 JavaScriptFrame* frame = it.frame();
20 Handle<JSFunction> function(frame->function());
21 RUNTIME_ASSERT(function->shared()->is_generator());
23 Handle<JSGeneratorObject> generator;
24 if (frame->IsConstructor()) {
25 generator = handle(JSGeneratorObject::cast(frame->receiver()));
27 generator = isolate->factory()->NewJSGeneratorObject(function);
29 generator->set_function(*function);
30 generator->set_context(Context::cast(frame->context()));
31 generator->set_receiver(frame->receiver());
32 generator->set_continuation(0);
33 generator->set_operand_stack(isolate->heap()->empty_fixed_array());
34 generator->set_stack_handler_index(-1);
40 RUNTIME_FUNCTION(Runtime_SuspendJSGeneratorObject) {
41 HandleScope handle_scope(isolate);
42 DCHECK(args.length() == 1);
43 CONVERT_ARG_HANDLE_CHECKED(JSGeneratorObject, generator_object, 0);
45 JavaScriptFrameIterator stack_iterator(isolate);
46 JavaScriptFrame* frame = stack_iterator.frame();
47 RUNTIME_ASSERT(frame->function()->shared()->is_generator());
48 DCHECK_EQ(frame->function(), generator_object->function());
50 // The caller should have saved the context and continuation already.
51 DCHECK_EQ(generator_object->context(), Context::cast(frame->context()));
52 DCHECK_LT(0, generator_object->continuation());
54 // We expect there to be at least two values on the operand stack: the return
55 // value of the yield expression, and the argument to this runtime call.
56 // Neither of those should be saved.
57 int operands_count = frame->ComputeOperandsCount();
58 DCHECK_GE(operands_count, 2);
61 if (operands_count == 0) {
62 // Although it's semantically harmless to call this function with an
63 // operands_count of zero, it is also unnecessary.
64 DCHECK_EQ(generator_object->operand_stack(),
65 isolate->heap()->empty_fixed_array());
66 DCHECK_EQ(generator_object->stack_handler_index(), -1);
67 // If there are no operands on the stack, there shouldn't be a handler
69 DCHECK(!frame->HasHandler());
71 int stack_handler_index = -1;
72 Handle<FixedArray> operand_stack =
73 isolate->factory()->NewFixedArray(operands_count);
74 frame->SaveOperandStack(*operand_stack, &stack_handler_index);
75 generator_object->set_operand_stack(*operand_stack);
76 generator_object->set_stack_handler_index(stack_handler_index);
79 return isolate->heap()->undefined_value();
83 // Note that this function is the slow path for resuming generators. It is only
84 // called if the suspended activation had operands on the stack, stack handlers
85 // needing rewinding, or if the resume should throw an exception. The fast path
86 // is handled directly in FullCodeGenerator::EmitGeneratorResume(), which is
87 // inlined into GeneratorNext and GeneratorThrow. EmitGeneratorResumeResume is
88 // called in any case, as it needs to reconstruct the stack frame and make space
89 // for arguments and operands.
90 RUNTIME_FUNCTION(Runtime_ResumeJSGeneratorObject) {
91 SealHandleScope shs(isolate);
92 DCHECK(args.length() == 3);
93 CONVERT_ARG_CHECKED(JSGeneratorObject, generator_object, 0);
94 CONVERT_ARG_CHECKED(Object, value, 1);
95 CONVERT_SMI_ARG_CHECKED(resume_mode_int, 2);
96 JavaScriptFrameIterator stack_iterator(isolate);
97 JavaScriptFrame* frame = stack_iterator.frame();
99 DCHECK_EQ(frame->function(), generator_object->function());
100 DCHECK(frame->function()->is_compiled());
102 STATIC_ASSERT(JSGeneratorObject::kGeneratorExecuting < 0);
103 STATIC_ASSERT(JSGeneratorObject::kGeneratorClosed == 0);
105 Address pc = generator_object->function()->code()->instruction_start();
106 int offset = generator_object->continuation();
108 frame->set_pc(pc + offset);
109 if (FLAG_enable_ool_constant_pool) {
110 frame->set_constant_pool(
111 generator_object->function()->code()->constant_pool());
113 generator_object->set_continuation(JSGeneratorObject::kGeneratorExecuting);
115 FixedArray* operand_stack = generator_object->operand_stack();
116 int operands_count = operand_stack->length();
117 if (operands_count != 0) {
118 frame->RestoreOperandStack(operand_stack,
119 generator_object->stack_handler_index());
120 generator_object->set_operand_stack(isolate->heap()->empty_fixed_array());
121 generator_object->set_stack_handler_index(-1);
124 JSGeneratorObject::ResumeMode resume_mode =
125 static_cast<JSGeneratorObject::ResumeMode>(resume_mode_int);
126 switch (resume_mode) {
127 case JSGeneratorObject::NEXT:
129 case JSGeneratorObject::THROW:
130 return isolate->Throw(value);
134 return isolate->ThrowIllegalOperation();
138 RUNTIME_FUNCTION(Runtime_GeneratorClose) {
139 HandleScope scope(isolate);
140 DCHECK(args.length() == 1);
141 CONVERT_ARG_HANDLE_CHECKED(JSGeneratorObject, generator, 0);
143 generator->set_continuation(JSGeneratorObject::kGeneratorClosed);
145 return isolate->heap()->undefined_value();
149 // Returns function of generator activation.
150 RUNTIME_FUNCTION(Runtime_GeneratorGetFunction) {
151 HandleScope scope(isolate);
152 DCHECK(args.length() == 1);
153 CONVERT_ARG_HANDLE_CHECKED(JSGeneratorObject, generator, 0);
155 return generator->function();
159 // Returns context of generator activation.
160 RUNTIME_FUNCTION(Runtime_GeneratorGetContext) {
161 HandleScope scope(isolate);
162 DCHECK(args.length() == 1);
163 CONVERT_ARG_HANDLE_CHECKED(JSGeneratorObject, generator, 0);
165 return generator->context();
169 // Returns receiver of generator activation.
170 RUNTIME_FUNCTION(Runtime_GeneratorGetReceiver) {
171 HandleScope scope(isolate);
172 DCHECK(args.length() == 1);
173 CONVERT_ARG_HANDLE_CHECKED(JSGeneratorObject, generator, 0);
175 return generator->receiver();
179 // Returns generator continuation as a PC offset, or the magic -1 or 0 values.
180 RUNTIME_FUNCTION(Runtime_GeneratorGetContinuation) {
181 HandleScope scope(isolate);
182 DCHECK(args.length() == 1);
183 CONVERT_ARG_HANDLE_CHECKED(JSGeneratorObject, generator, 0);
185 return Smi::FromInt(generator->continuation());
189 RUNTIME_FUNCTION(Runtime_GeneratorGetSourcePosition) {
190 HandleScope scope(isolate);
191 DCHECK(args.length() == 1);
192 CONVERT_ARG_HANDLE_CHECKED(JSGeneratorObject, generator, 0);
194 if (generator->is_suspended()) {
195 Handle<Code> code(generator->function()->code(), isolate);
196 int offset = generator->continuation();
198 RUNTIME_ASSERT(0 <= offset && offset < code->Size());
199 Address pc = code->address() + offset;
201 return Smi::FromInt(code->SourcePosition(pc));
204 return isolate->heap()->undefined_value();
208 RUNTIME_FUNCTION(Runtime_FunctionIsGenerator) {
209 SealHandleScope shs(isolate);
210 DCHECK(args.length() == 1);
211 CONVERT_ARG_CHECKED(JSFunction, f, 0);
212 return isolate->heap()->ToBoolean(f->shared()->is_generator());
216 RUNTIME_FUNCTION(RuntimeReference_GeneratorNext) {
217 UNREACHABLE(); // Optimization disabled in SetUpGenerators().
222 RUNTIME_FUNCTION(RuntimeReference_GeneratorThrow) {
223 UNREACHABLE(); // Optimization disabled in SetUpGenerators().
227 } // namespace v8::internal