1/****************************************************************************
2**
3** Copyright (C) 2018 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQml module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include <qv4generatorobject_p.h>
41#include <qv4symbol_p.h>
42#include <qv4iterator_p.h>
43#include <qv4jscall_p.h>
44#include <qv4vme_moth_p.h>
45
46using namespace QV4;
47
48DEFINE_OBJECT_VTABLE(GeneratorFunctionCtor);
49DEFINE_OBJECT_VTABLE(GeneratorFunction);
50DEFINE_OBJECT_VTABLE(GeneratorObject);
51
52void Heap::GeneratorFunctionCtor::init(QV4::ExecutionContext *scope)
53{
54 Heap::FunctionObject::init(scope, QStringLiteral("GeneratorFunction"));
55}
56
57ReturnedValue GeneratorFunctionCtor::virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *newTarget)
58{
59 ExecutionEngine *engine = f->engine();
60
61 QQmlRefPointer<ExecutableCompilationUnit> compilationUnit = parse(engine, argv, argc, t: Type_Generator);
62 if (engine->hasException)
63 return Encode::undefined();
64
65 Function *vmf = compilationUnit->linkToEngine(engine);
66 ExecutionContext *global = engine->scriptContext();
67 ReturnedValue o = Encode(GeneratorFunction::create(scope: global, function: vmf));
68
69 if (!newTarget)
70 return o;
71 Scope scope(engine);
72 ScopedObject obj(scope, o);
73 obj->setProtoFromNewTarget(newTarget);
74 return obj->asReturnedValue();
75}
76
77// 15.3.1: This is equivalent to new Function(...)
78ReturnedValue GeneratorFunctionCtor::virtualCall(const FunctionObject *f, const Value *, const Value *argv, int argc)
79{
80 return virtualCallAsConstructor(f, argv, argc, newTarget: f);
81}
82
83Heap::FunctionObject *GeneratorFunction::create(ExecutionContext *context, Function *function)
84{
85 Scope scope(context);
86 Scoped<GeneratorFunction> g(scope, context->engine()->memoryManager->allocate<GeneratorFunction>(args: context, args: function));
87 ScopedObject proto(scope, scope.engine->newObject());
88 proto->setPrototypeOf(scope.engine->generatorPrototype());
89 g->defineDefaultProperty(name: scope.engine->id_prototype(), value: proto, attributes: Attr_NotConfigurable|Attr_NotEnumerable);
90 g->setPrototypeOf(ScopedObject(scope, scope.engine->generatorFunctionCtor()->get(name: scope.engine->id_prototype())));
91 return g->d();
92}
93
94ReturnedValue GeneratorFunction::virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc)
95{
96 const GeneratorFunction *gf = static_cast<const GeneratorFunction *>(f);
97 Function *function = gf->function();
98 ExecutionEngine *engine = gf->engine();
99
100 Scope scope(gf);
101 Scoped<GeneratorObject> g(scope, engine->memoryManager->allocManaged<GeneratorObject>(size: sizeof(GeneratorObject::Data), ic: engine->classes[EngineBase::Class_GeneratorObject]));
102 g->setPrototypeOf(ScopedObject(scope, gf->get(name: scope.engine->id_prototype())));
103
104 // We need to set up a separate JSFrame for the generator, as it's being re-entered
105 Heap::GeneratorObject *gp = g->d();
106 gp->values.set(e: engine, newVal: engine->newArrayObject(count: argc));
107 gp->jsFrame.set(e: engine, newVal: engine->newArrayObject(count: CppStackFrame::requiredJSStackFrameSize(v4Function: function)));
108
109 // copy original arguments
110 for (int i = 0; i < argc; i++)
111 gp->values->arrayData->setArrayData(e: engine, index: i, newVal: argv[i]);
112
113 gp->cppFrame.init(engine, v4Function: function, argv: gp->values->arrayData->values.values, argc);
114 gp->cppFrame.setupJSFrame(stackSpace: gp->jsFrame->arrayData->values.values, function: *gf, scope: gf->scope(),
115 thisObject: thisObject ? *thisObject : Value::undefinedValue(),
116 newTarget: Value::undefinedValue());
117
118 gp->cppFrame.push();
119
120 Moth::VME::interpret(frame: &gp->cppFrame, engine, codeEntry: function->codeData);
121 gp->state = GeneratorState::SuspendedStart;
122
123 gp->cppFrame.pop();
124 return g->asReturnedValue();
125}
126
127
128void Heap::GeneratorPrototype::init()
129{
130 Heap::FunctionObject::init();
131}
132
133
134void GeneratorPrototype::init(ExecutionEngine *engine, Object *ctor)
135{
136 Scope scope(engine);
137 ScopedValue v(scope);
138
139 Scoped<InternalClass> ic(scope, engine->newInternalClass(
140 vtable: Object::staticVTable(), prototype: engine->functionPrototype()));
141 ScopedObject ctorProto(scope, engine->newObject(internalClass: ic->d()));
142
143 ctor->defineReadonlyConfigurableProperty(name: engine->id_length(), value: Value::fromInt32(i: 1));
144 ctor->defineReadonlyProperty(name: engine->id_prototype(), value: ctorProto);
145
146 ctorProto->defineDefaultProperty(QStringLiteral("constructor"), value: (v = ctor), attributes: Attr_ReadOnly_ButConfigurable);
147 ctorProto->defineDefaultProperty(name: engine->symbol_toStringTag(), value: (v = engine->newIdentifier(QStringLiteral("GeneratorFunction"))), attributes: Attr_ReadOnly_ButConfigurable);
148 ctorProto->defineDefaultProperty(name: engine->id_prototype(), value: (v = this), attributes: Attr_ReadOnly_ButConfigurable);
149
150 setPrototypeOf(engine->iteratorPrototype());
151 defineDefaultProperty(QStringLiteral("constructor"), value: ctorProto, attributes: Attr_ReadOnly_ButConfigurable);
152 defineDefaultProperty(QStringLiteral("next"), code: method_next, argumentCount: 1);
153 defineDefaultProperty(QStringLiteral("return"), code: method_return, argumentCount: 1);
154 defineDefaultProperty(QStringLiteral("throw"), code: method_throw, argumentCount: 1);
155 defineDefaultProperty(name: engine->symbol_toStringTag(), value: (v = engine->newString(QStringLiteral("Generator"))), attributes: Attr_ReadOnly_ButConfigurable);
156}
157
158ReturnedValue GeneratorPrototype::method_next(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc)
159{
160 ExecutionEngine *engine = f->engine();
161 const GeneratorObject *g = thisObject->as<GeneratorObject>();
162 if (!g || g->d()->state == GeneratorState::Executing)
163 return engine->throwTypeError();
164 Heap::GeneratorObject *gp = g->d();
165
166 if (gp->state == GeneratorState::Completed)
167 return IteratorPrototype::createIterResultObject(engine, value: Value::undefinedValue(), done: true);
168
169 return g->resume(engine, arg: argc ? argv[0] : Value::undefinedValue());
170}
171
172ReturnedValue GeneratorPrototype::method_return(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc)
173{
174 ExecutionEngine *engine = f->engine();
175 const GeneratorObject *g = thisObject->as<GeneratorObject>();
176 if (!g || g->d()->state == GeneratorState::Executing)
177 return engine->throwTypeError();
178
179 Heap::GeneratorObject *gp = g->d();
180
181 if (gp->state == GeneratorState::SuspendedStart)
182 gp->state = GeneratorState::Completed;
183
184 if (gp->state == GeneratorState::Completed)
185 return IteratorPrototype::createIterResultObject(engine, value: argc ? argv[0] : Value::undefinedValue(), done: true);
186
187 // the bytecode interpreter interprets an exception with empty value as
188 // a yield called with return()
189 engine->throwError(value: Value::emptyValue());
190
191 return g->resume(engine, arg: argc ? argv[0]: Value::undefinedValue());
192}
193
194ReturnedValue GeneratorPrototype::method_throw(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc)
195{
196 ExecutionEngine *engine = f->engine();
197 const GeneratorObject *g = thisObject->as<GeneratorObject>();
198 if (!g || g->d()->state == GeneratorState::Executing)
199 return engine->throwTypeError();
200
201 Heap::GeneratorObject *gp = g->d();
202
203 engine->throwError(value: argc ? argv[0]: Value::undefinedValue());
204
205 if (gp->state == GeneratorState::SuspendedStart || gp->state == GeneratorState::Completed) {
206 gp->state = GeneratorState::Completed;
207 return Encode::undefined();
208 }
209
210 return g->resume(engine, arg: Value::undefinedValue());
211}
212
213ReturnedValue GeneratorObject::resume(ExecutionEngine *engine, const Value &arg) const
214{
215 Heap::GeneratorObject *gp = d();
216 gp->state = GeneratorState::Executing;
217 gp->cppFrame.parent = engine->currentStackFrame;
218 engine->currentStackFrame = &gp->cppFrame;
219
220 Q_ASSERT(gp->cppFrame.yield != nullptr);
221 const char *code = gp->cppFrame.yield;
222 gp->cppFrame.yield = nullptr;
223 gp->cppFrame.jsFrame->accumulator = arg;
224 gp->cppFrame.yieldIsIterator = false;
225
226 Scope scope(engine);
227 ScopedValue result(scope, Moth::VME::interpret(frame: &gp->cppFrame, engine, codeEntry: code));
228
229 engine->currentStackFrame = gp->cppFrame.parent;
230
231 bool done = (gp->cppFrame.yield == nullptr);
232 gp->state = done ? GeneratorState::Completed : GeneratorState::SuspendedYield;
233 if (engine->hasException)
234 return Encode::undefined();
235 if (gp->cppFrame.yieldIsIterator)
236 return result->asReturnedValue();
237 return IteratorPrototype::createIterResultObject(engine, value: result, done);
238}
239
240DEFINE_OBJECT_VTABLE(MemberGeneratorFunction);
241
242Heap::FunctionObject *MemberGeneratorFunction::create(ExecutionContext *context, Function *function, Object *homeObject, String *name)
243{
244 Scope scope(context);
245 Scoped<MemberGeneratorFunction> g(scope, context->engine()->memoryManager->allocate<MemberGeneratorFunction>(args: context, args: function, args: name));
246 g->d()->homeObject.set(e: scope.engine, newVal: homeObject->d());
247 ScopedObject proto(scope, scope.engine->newObject());
248 proto->setPrototypeOf(scope.engine->generatorPrototype());
249 g->defineDefaultProperty(name: scope.engine->id_prototype(), value: proto, attributes: Attr_NotConfigurable|Attr_NotEnumerable);
250 g->setPrototypeOf(ScopedObject(scope, scope.engine->generatorFunctionCtor()->get(name: scope.engine->id_prototype())));
251 return g->d();
252}
253
254ReturnedValue MemberGeneratorFunction::virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc)
255{
256 return GeneratorFunction::virtualCall(f, thisObject, argv, argc);
257}
258

source code of qtdeclarative/src/qml/jsruntime/qv4generatorobject.cpp