blob: ff07acd304a235a4b78d684ffd1bdbe726ee1688 [file] [log] [blame]
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001// 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.
4
5#include "src/v8.h"
6
7#include "src/arguments.h"
8#include "src/frames-inl.h"
9#include "src/runtime/runtime-utils.h"
10
11namespace v8 {
12namespace internal {
13
14RUNTIME_FUNCTION(Runtime_CreateJSGeneratorObject) {
15 HandleScope scope(isolate);
16 DCHECK(args.length() == 0);
17
18 JavaScriptFrameIterator it(isolate);
19 JavaScriptFrame* frame = it.frame();
20 Handle<JSFunction> function(frame->function());
21 RUNTIME_ASSERT(function->shared()->is_generator());
22
23 Handle<JSGeneratorObject> generator;
24 if (frame->IsConstructor()) {
25 generator = handle(JSGeneratorObject::cast(frame->receiver()));
26 } else {
27 generator = isolate->factory()->NewJSGeneratorObject(function);
28 }
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);
35
36 return *generator;
37}
38
39
40RUNTIME_FUNCTION(Runtime_SuspendJSGeneratorObject) {
41 HandleScope handle_scope(isolate);
42 DCHECK(args.length() == 1);
43 CONVERT_ARG_HANDLE_CHECKED(JSGeneratorObject, generator_object, 0);
44
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());
49
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());
53
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);
59 operands_count -= 2;
60
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
68 // active either.
69 DCHECK(!frame->HasHandler());
70 } else {
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);
77 }
78
79 return isolate->heap()->undefined_value();
80}
81
82
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.
90RUNTIME_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();
98
99 DCHECK_EQ(frame->function(), generator_object->function());
100 DCHECK(frame->function()->is_compiled());
101
102 STATIC_ASSERT(JSGeneratorObject::kGeneratorExecuting < 0);
103 STATIC_ASSERT(JSGeneratorObject::kGeneratorClosed == 0);
104
105 Address pc = generator_object->function()->code()->instruction_start();
106 int offset = generator_object->continuation();
107 DCHECK(offset > 0);
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());
112 }
113 generator_object->set_continuation(JSGeneratorObject::kGeneratorExecuting);
114
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);
122 }
123
124 JSGeneratorObject::ResumeMode resume_mode =
125 static_cast<JSGeneratorObject::ResumeMode>(resume_mode_int);
126 switch (resume_mode) {
127 case JSGeneratorObject::NEXT:
128 return value;
129 case JSGeneratorObject::THROW:
130 return isolate->Throw(value);
131 }
132
133 UNREACHABLE();
134 return isolate->ThrowIllegalOperation();
135}
136
137
138RUNTIME_FUNCTION(Runtime_GeneratorClose) {
139 HandleScope scope(isolate);
140 DCHECK(args.length() == 1);
141 CONVERT_ARG_HANDLE_CHECKED(JSGeneratorObject, generator, 0);
142
143 generator->set_continuation(JSGeneratorObject::kGeneratorClosed);
144
145 return isolate->heap()->undefined_value();
146}
147
148
149// Returns function of generator activation.
150RUNTIME_FUNCTION(Runtime_GeneratorGetFunction) {
151 HandleScope scope(isolate);
152 DCHECK(args.length() == 1);
153 CONVERT_ARG_HANDLE_CHECKED(JSGeneratorObject, generator, 0);
154
155 return generator->function();
156}
157
158
159// Returns context of generator activation.
160RUNTIME_FUNCTION(Runtime_GeneratorGetContext) {
161 HandleScope scope(isolate);
162 DCHECK(args.length() == 1);
163 CONVERT_ARG_HANDLE_CHECKED(JSGeneratorObject, generator, 0);
164
165 return generator->context();
166}
167
168
169// Returns receiver of generator activation.
170RUNTIME_FUNCTION(Runtime_GeneratorGetReceiver) {
171 HandleScope scope(isolate);
172 DCHECK(args.length() == 1);
173 CONVERT_ARG_HANDLE_CHECKED(JSGeneratorObject, generator, 0);
174
175 return generator->receiver();
176}
177
178
179// Returns generator continuation as a PC offset, or the magic -1 or 0 values.
180RUNTIME_FUNCTION(Runtime_GeneratorGetContinuation) {
181 HandleScope scope(isolate);
182 DCHECK(args.length() == 1);
183 CONVERT_ARG_HANDLE_CHECKED(JSGeneratorObject, generator, 0);
184
185 return Smi::FromInt(generator->continuation());
186}
187
188
189RUNTIME_FUNCTION(Runtime_GeneratorGetSourcePosition) {
190 HandleScope scope(isolate);
191 DCHECK(args.length() == 1);
192 CONVERT_ARG_HANDLE_CHECKED(JSGeneratorObject, generator, 0);
193
194 if (generator->is_suspended()) {
195 Handle<Code> code(generator->function()->code(), isolate);
196 int offset = generator->continuation();
197
198 RUNTIME_ASSERT(0 <= offset && offset < code->Size());
199 Address pc = code->address() + offset;
200
201 return Smi::FromInt(code->SourcePosition(pc));
202 }
203
204 return isolate->heap()->undefined_value();
205}
206
207
208RUNTIME_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());
213}
214
215
216RUNTIME_FUNCTION(RuntimeReference_GeneratorNext) {
217 UNREACHABLE(); // Optimization disabled in SetUpGenerators().
218 return NULL;
219}
220
221
222RUNTIME_FUNCTION(RuntimeReference_GeneratorThrow) {
223 UNREACHABLE(); // Optimization disabled in SetUpGenerators().
224 return NULL;
225}
226}
227} // namespace v8::internal