1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <qv4generatorobject_p.h>
5#include <qv4symbol_p.h>
6#include <qv4iterator_p.h>
7#include <qv4jscall_p.h>
8#include <qv4vme_moth_p.h>
9
10using namespace QV4;
11
12DEFINE_OBJECT_VTABLE(GeneratorFunctionCtor);
13DEFINE_OBJECT_VTABLE(GeneratorFunction);
14DEFINE_OBJECT_VTABLE(GeneratorObject);
15
16void Heap::GeneratorFunctionCtor::init(QV4::ExecutionContext *scope)
17{
18 Heap::FunctionObject::init(scope, QStringLiteral("GeneratorFunction"));
19}
20
21ReturnedValue GeneratorFunctionCtor::virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *newTarget)
22{
23 ExecutionEngine *engine = f->engine();
24
25 QQmlRefPointer<ExecutableCompilationUnit> compilationUnit = parse(engine, argv, argc, t: Type_Generator);
26 if (engine->hasException)
27 return Encode::undefined();
28
29 Function *vmf = compilationUnit->linkToEngine(engine);
30 ExecutionContext *global = engine->scriptContext();
31 ReturnedValue o = Encode(GeneratorFunction::create(scope: global, function: vmf));
32
33 if (!newTarget)
34 return o;
35 Scope scope(engine);
36 ScopedObject obj(scope, o);
37 obj->setProtoFromNewTarget(newTarget);
38 return obj->asReturnedValue();
39}
40
41// 15.3.1: This is equivalent to new Function(...)
42ReturnedValue GeneratorFunctionCtor::virtualCall(const FunctionObject *f, const Value *, const Value *argv, int argc)
43{
44 return virtualCallAsConstructor(f, argv, argc, newTarget: f);
45}
46
47Heap::FunctionObject *GeneratorFunction::create(ExecutionContext *context, Function *function)
48{
49 Scope scope(context);
50 Scoped<GeneratorFunction> g(scope, context->engine()->memoryManager->allocate<GeneratorFunction>(args&: context, args&: function));
51 ScopedObject proto(scope, scope.engine->newObject());
52 proto->setPrototypeOf(scope.engine->generatorPrototype());
53 g->defineDefaultProperty(name: scope.engine->id_prototype(), value: proto, attributes: Attr_NotConfigurable|Attr_NotEnumerable);
54 g->setPrototypeOf(ScopedObject(scope, scope.engine->generatorFunctionCtor()->get(name: scope.engine->id_prototype())));
55 return g->d();
56}
57
58ReturnedValue GeneratorFunction::virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc)
59{
60 const GeneratorFunction *gf = static_cast<const GeneratorFunction *>(f);
61 Function *function = gf->function();
62 ExecutionEngine *engine = gf->engine();
63
64 Scope scope(gf);
65 Scoped<GeneratorObject> g(scope, engine->memoryManager->allocManaged<GeneratorObject>(size: sizeof(GeneratorObject::Data), ic: engine->classes[EngineBase::Class_GeneratorObject]));
66 g->setPrototypeOf(ScopedObject(scope, gf->get(name: scope.engine->id_prototype())));
67
68 // We need to set up a separate JSFrame for the generator, as it's being re-entered
69 Heap::GeneratorObject *gp = g->d();
70 gp->values.set(e: engine, newVal: engine->newArrayObject(count: argc));
71 gp->jsFrame.set(e: engine, newVal: engine->newArrayObject(
72 count: JSTypesStackFrame::requiredJSStackFrameSize(v4Function: function)));
73
74 // copy original arguments
75 for (int i = 0; i < argc; i++)
76 gp->values->arrayData->setArrayData(e: engine, index: i, newVal: argv[i]);
77
78 gp->cppFrame.init(v4Function: function, argv: gp->values->arrayData->values.values, argc);
79 gp->cppFrame.setupJSFrame(stackSpace: gp->jsFrame->arrayData->values.values, function: *gf, scope: gf->scope(),
80 thisObject: thisObject ? *thisObject : Value::undefinedValue(),
81 newTarget: Value::undefinedValue());
82
83 gp->cppFrame.push(engine);
84
85 Moth::VME::interpret(frame: &gp->cppFrame, engine, codeEntry: function->codeData);
86 gp->state = GeneratorState::SuspendedStart;
87
88 gp->cppFrame.pop(engine);
89 return g->asReturnedValue();
90}
91
92
93void Heap::GeneratorPrototype::init()
94{
95 Heap::FunctionObject::init();
96}
97
98
99void GeneratorPrototype::init(ExecutionEngine *engine, Object *ctor)
100{
101 Scope scope(engine);
102 ScopedValue v(scope);
103
104 Scoped<InternalClass> ic(scope, engine->newInternalClass(
105 vtable: Object::staticVTable(), prototype: engine->functionPrototype()));
106 ScopedObject ctorProto(scope, engine->newObject(internalClass: ic->d()));
107
108 ctor->defineReadonlyConfigurableProperty(name: engine->id_length(), value: Value::fromInt32(i: 1));
109 ctor->defineReadonlyProperty(name: engine->id_prototype(), value: ctorProto);
110
111 ctorProto->defineDefaultProperty(QStringLiteral("constructor"), value: (v = ctor), attributes: Attr_ReadOnly_ButConfigurable);
112 ctorProto->defineDefaultProperty(name: engine->symbol_toStringTag(), value: (v = engine->newIdentifier(QStringLiteral("GeneratorFunction"))), attributes: Attr_ReadOnly_ButConfigurable);
113 ctorProto->defineDefaultProperty(name: engine->id_prototype(), value: (v = this), attributes: Attr_ReadOnly_ButConfigurable);
114
115 setPrototypeOf(engine->iteratorPrototype());
116 defineDefaultProperty(QStringLiteral("constructor"), value: ctorProto, attributes: Attr_ReadOnly_ButConfigurable);
117 defineDefaultProperty(QStringLiteral("next"), code: method_next, argumentCount: 1);
118 defineDefaultProperty(QStringLiteral("return"), code: method_return, argumentCount: 1);
119 defineDefaultProperty(QStringLiteral("throw"), code: method_throw, argumentCount: 1);
120 defineDefaultProperty(name: engine->symbol_toStringTag(), value: (v = engine->newString(QStringLiteral("Generator"))), attributes: Attr_ReadOnly_ButConfigurable);
121}
122
123ReturnedValue GeneratorPrototype::method_next(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc)
124{
125 ExecutionEngine *engine = f->engine();
126 const GeneratorObject *g = thisObject->as<GeneratorObject>();
127 if (!g || g->d()->state == GeneratorState::Executing)
128 return engine->throwTypeError();
129 Heap::GeneratorObject *gp = g->d();
130
131 if (gp->state == GeneratorState::Completed)
132 return IteratorPrototype::createIterResultObject(engine, value: Value::undefinedValue(), done: true);
133
134 return g->resume(engine, arg: argc ? argv[0] : Value::undefinedValue());
135}
136
137ReturnedValue GeneratorPrototype::method_return(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc)
138{
139 ExecutionEngine *engine = f->engine();
140 const GeneratorObject *g = thisObject->as<GeneratorObject>();
141 if (!g || g->d()->state == GeneratorState::Executing)
142 return engine->throwTypeError();
143
144 Heap::GeneratorObject *gp = g->d();
145
146 if (gp->state == GeneratorState::SuspendedStart)
147 gp->state = GeneratorState::Completed;
148
149 if (gp->state == GeneratorState::Completed)
150 return IteratorPrototype::createIterResultObject(engine, value: argc ? argv[0] : Value::undefinedValue(), done: true);
151
152 // the bytecode interpreter interprets an exception with empty value as
153 // a yield called with return()
154 engine->throwError(value: Value::emptyValue());
155
156 return g->resume(engine, arg: argc ? argv[0] : Value::undefinedValue());
157}
158
159ReturnedValue GeneratorPrototype::method_throw(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc)
160{
161 ExecutionEngine *engine = f->engine();
162 const GeneratorObject *g = thisObject->as<GeneratorObject>();
163 if (!g || g->d()->state == GeneratorState::Executing)
164 return engine->throwTypeError();
165
166 Heap::GeneratorObject *gp = g->d();
167
168 engine->throwError(value: argc ? argv[0] : Value::undefinedValue());
169
170 if (gp->state == GeneratorState::SuspendedStart || gp->state == GeneratorState::Completed) {
171 gp->state = GeneratorState::Completed;
172 return Encode::undefined();
173 }
174
175 return g->resume(engine, arg: Value::undefinedValue());
176}
177
178ReturnedValue GeneratorObject::resume(ExecutionEngine *engine, const Value &arg) const
179{
180 Heap::GeneratorObject *gp = d();
181 gp->state = GeneratorState::Executing;
182 gp->cppFrame.setParentFrame(engine->currentStackFrame);
183 engine->currentStackFrame = &gp->cppFrame;
184
185 Q_ASSERT(gp->cppFrame.yield() != nullptr);
186 const char *code = gp->cppFrame.yield();
187 gp->cppFrame.setYield(nullptr);
188 gp->cppFrame.jsFrame->accumulator = arg;
189 gp->cppFrame.setYieldIsIterator(false);
190
191 Scope scope(engine);
192 ScopedValue result(scope, Moth::VME::interpret(frame: &gp->cppFrame, engine, codeEntry: code));
193
194 engine->currentStackFrame = gp->cppFrame.parentFrame();
195
196 bool done = (gp->cppFrame.yield() == nullptr);
197 gp->state = done ? GeneratorState::Completed : GeneratorState::SuspendedYield;
198 if (engine->hasException)
199 return Encode::undefined();
200 if (gp->cppFrame.yieldIsIterator())
201 return result->asReturnedValue();
202 return IteratorPrototype::createIterResultObject(engine, value: result, done);
203}
204
205DEFINE_OBJECT_VTABLE(MemberGeneratorFunction);
206
207Heap::FunctionObject *MemberGeneratorFunction::create(ExecutionContext *context, Function *function, Object *homeObject, String *name)
208{
209 Scope scope(context);
210 Scoped<MemberGeneratorFunction> g(scope, context->engine()->memoryManager->allocate<MemberGeneratorFunction>(args&: context, args&: function, args&: name));
211 g->d()->homeObject.set(e: scope.engine, newVal: homeObject->d());
212 ScopedObject proto(scope, scope.engine->newObject());
213 proto->setPrototypeOf(scope.engine->generatorPrototype());
214 g->defineDefaultProperty(name: scope.engine->id_prototype(), value: proto, attributes: Attr_NotConfigurable|Attr_NotEnumerable);
215 g->setPrototypeOf(ScopedObject(scope, scope.engine->generatorFunctionCtor()->get(name: scope.engine->id_prototype())));
216 return g->d();
217}
218
219ReturnedValue MemberGeneratorFunction::virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc)
220{
221 return GeneratorFunction::virtualCall(f, thisObject, argv, argc);
222}
223

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