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.
4
5 #include "src/runtime/runtime-utils.h"
6
7 #include "src/arguments.h"
8 #include "src/debug/debug.h"
9 #include "src/factory.h"
10 #include "src/frames-inl.h"
11 #include "src/objects-inl.h"
12
13 namespace v8 {
14 namespace internal {
15
RUNTIME_FUNCTION(Runtime_CreateJSGeneratorObject)16 RUNTIME_FUNCTION(Runtime_CreateJSGeneratorObject) {
17 HandleScope scope(isolate);
18 DCHECK(args.length() == 2);
19 CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 0);
20 CONVERT_ARG_HANDLE_CHECKED(Object, receiver, 1);
21 CHECK(IsResumableFunction(function->shared()->kind()));
22
23 Handle<FixedArray> operand_stack;
24 if (function->shared()->HasBytecodeArray()) {
25 // New-style generators.
26 DCHECK(!function->shared()->HasBaselineCode());
27 int size = function->shared()->bytecode_array()->register_count();
28 operand_stack = isolate->factory()->NewFixedArray(size);
29 } else {
30 // Old-style generators.
31 DCHECK(function->shared()->HasBaselineCode());
32 operand_stack = isolate->factory()->empty_fixed_array();
33 }
34
35 Handle<JSGeneratorObject> generator =
36 isolate->factory()->NewJSGeneratorObject(function);
37 generator->set_function(*function);
38 generator->set_context(isolate->context());
39 generator->set_receiver(*receiver);
40 generator->set_operand_stack(*operand_stack);
41 generator->set_continuation(JSGeneratorObject::kGeneratorExecuting);
42 return *generator;
43 }
44
RUNTIME_FUNCTION(Runtime_SuspendJSGeneratorObject)45 RUNTIME_FUNCTION(Runtime_SuspendJSGeneratorObject) {
46 HandleScope handle_scope(isolate);
47 DCHECK(args.length() == 1);
48 CONVERT_ARG_HANDLE_CHECKED(JSGeneratorObject, generator_object, 0);
49
50 JavaScriptFrameIterator stack_iterator(isolate);
51 JavaScriptFrame* frame = stack_iterator.frame();
52 CHECK(IsResumableFunction(frame->function()->shared()->kind()));
53 DCHECK_EQ(frame->function(), generator_object->function());
54 DCHECK(frame->function()->shared()->is_compiled());
55 DCHECK(!frame->function()->IsOptimized());
56
57 isolate->debug()->RecordAsyncFunction(generator_object);
58
59 // The caller should have saved the context and continuation already.
60 DCHECK_EQ(generator_object->context(), Context::cast(frame->context()));
61 DCHECK_LT(0, generator_object->continuation());
62
63 // We expect there to be at least two values on the operand stack: the return
64 // value of the yield expression, and the arguments to this runtime call.
65 // Neither of those should be saved.
66 int operands_count = frame->ComputeOperandsCount();
67 DCHECK_GE(operands_count, 1 + args.length());
68 operands_count -= 1 + args.length();
69
70 if (operands_count == 0) {
71 // Although it's semantically harmless to call this function with an
72 // operands_count of zero, it is also unnecessary.
73 DCHECK_EQ(generator_object->operand_stack(),
74 isolate->heap()->empty_fixed_array());
75 } else {
76 Handle<FixedArray> operand_stack =
77 isolate->factory()->NewFixedArray(operands_count);
78 frame->SaveOperandStack(*operand_stack);
79 generator_object->set_operand_stack(*operand_stack);
80 }
81
82 return isolate->heap()->undefined_value();
83 }
84
RUNTIME_FUNCTION(Runtime_GeneratorClose)85 RUNTIME_FUNCTION(Runtime_GeneratorClose) {
86 HandleScope scope(isolate);
87 DCHECK(args.length() == 1);
88 CONVERT_ARG_HANDLE_CHECKED(JSGeneratorObject, generator, 0);
89
90 generator->set_continuation(JSGeneratorObject::kGeneratorClosed);
91
92 return isolate->heap()->undefined_value();
93 }
94
RUNTIME_FUNCTION(Runtime_GeneratorGetFunction)95 RUNTIME_FUNCTION(Runtime_GeneratorGetFunction) {
96 HandleScope scope(isolate);
97 DCHECK(args.length() == 1);
98 CONVERT_ARG_HANDLE_CHECKED(JSGeneratorObject, generator, 0);
99
100 return generator->function();
101 }
102
RUNTIME_FUNCTION(Runtime_GeneratorGetReceiver)103 RUNTIME_FUNCTION(Runtime_GeneratorGetReceiver) {
104 HandleScope scope(isolate);
105 DCHECK(args.length() == 1);
106 CONVERT_ARG_HANDLE_CHECKED(JSGeneratorObject, generator, 0);
107
108 return generator->receiver();
109 }
110
RUNTIME_FUNCTION(Runtime_GeneratorGetInputOrDebugPos)111 RUNTIME_FUNCTION(Runtime_GeneratorGetInputOrDebugPos) {
112 HandleScope scope(isolate);
113 DCHECK(args.length() == 1);
114 CONVERT_ARG_HANDLE_CHECKED(JSGeneratorObject, generator, 0);
115
116 return generator->input_or_debug_pos();
117 }
118
RUNTIME_FUNCTION(Runtime_GeneratorGetResumeMode)119 RUNTIME_FUNCTION(Runtime_GeneratorGetResumeMode) {
120 HandleScope scope(isolate);
121 DCHECK(args.length() == 1);
122 CONVERT_ARG_HANDLE_CHECKED(JSGeneratorObject, generator, 0);
123
124 return Smi::FromInt(generator->resume_mode());
125 }
126
RUNTIME_FUNCTION(Runtime_GeneratorGetContinuation)127 RUNTIME_FUNCTION(Runtime_GeneratorGetContinuation) {
128 HandleScope scope(isolate);
129 DCHECK(args.length() == 1);
130 CONVERT_ARG_HANDLE_CHECKED(JSGeneratorObject, generator, 0);
131
132 return Smi::FromInt(generator->continuation());
133 }
134
RUNTIME_FUNCTION(Runtime_GeneratorGetSourcePosition)135 RUNTIME_FUNCTION(Runtime_GeneratorGetSourcePosition) {
136 HandleScope scope(isolate);
137 DCHECK(args.length() == 1);
138 CONVERT_ARG_HANDLE_CHECKED(JSGeneratorObject, generator, 0);
139
140 if (!generator->is_suspended()) return isolate->heap()->undefined_value();
141 return Smi::FromInt(generator->source_position());
142 }
143
144 } // namespace internal
145 } // namespace v8
146