Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qv4generatorobject.cpp
Go to the documentation of this file.
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
15
16void Heap::GeneratorFunctionCtor::init(QV4::ExecutionEngine *engine)
17{
18 Heap::FunctionObject::init(engine, 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, Type_Generator);
26 if (engine->hasException)
27 return Encode::undefined();
28
29 Function *vmf = compilationUnit->rootFunction();
30 ExecutionContext *global = engine->scriptContext();
31 ReturnedValue o = Encode(GeneratorFunction::create(global, 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, 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>(context, function));
51 ScopedObject proto(scope, scope.engine->newObject());
52 proto->setPrototypeOf(scope.engine->generatorPrototype());
53 g->defineDefaultProperty(scope.engine->id_prototype(), proto, Attr_NotConfigurable|Attr_NotEnumerable);
54 g->setPrototypeOf(ScopedObject(scope, scope.engine->generatorFunctionCtor()->get(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>(engine->classes[EngineBase::Class_GeneratorObject]));
66 g->setPrototypeOf(ScopedObject(scope, gf->get(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(engine, engine->newArrayObject(argc));
71 gp->jsFrame.set(engine, engine->newArrayObject(
72 JSTypesStackFrame::requiredJSStackFrameSize(function)));
73
74 // copy original arguments
75 for (int i = 0; i < argc; i++)
76 gp->values->arrayData->setArrayData(engine, i, argv[i]);
77
78 gp->cppFrame.init(function, gp->values->arrayData->values.values, argc);
79 gp->cppFrame.setupJSFrame(gp->jsFrame->arrayData->values.values, *gf, gf->scope(),
80 thisObject ? *thisObject : Value::undefinedValue(),
81 Value::undefinedValue());
82
83 gp->cppFrame.push(engine);
84
85 CHECK_STACK_LIMITS(scope.engine)
86 Moth::VME::interpret(&gp->cppFrame, engine, function->codeData);
87
88 gp->state = GeneratorState::SuspendedStart;
89
90 gp->cppFrame.pop(engine);
91 return g->asReturnedValue();
92}
93
94
95void Heap::GeneratorPrototype::init()
96{
97 Heap::FunctionObject::init();
98}
99
100
101void GeneratorPrototype::init(ExecutionEngine *engine, Object *ctor)
102{
103 Scope scope(engine);
104 ScopedValue v(scope);
105
106 Scoped<InternalClass> ic(scope, engine->newInternalClass(
107 Object::staticVTable(), engine->functionPrototype()));
108 ScopedObject ctorProto(scope, engine->newObject(ic->d()));
109
110 ctor->defineReadonlyConfigurableProperty(engine->id_length(), Value::fromInt32(1));
111 ctor->defineReadonlyProperty(engine->id_prototype(), ctorProto);
112
113 ctorProto->defineDefaultProperty(QStringLiteral("constructor"), (v = ctor), Attr_ReadOnly_ButConfigurable);
114 ctorProto->defineDefaultProperty(engine->symbol_toStringTag(), (v = engine->newIdentifier(QStringLiteral("GeneratorFunction"))), Attr_ReadOnly_ButConfigurable);
115 ctorProto->defineDefaultProperty(engine->id_prototype(), (v = this), Attr_ReadOnly_ButConfigurable);
116
117 setPrototypeOf(engine->iteratorPrototype());
118 defineDefaultProperty(QStringLiteral("constructor"), ctorProto, Attr_ReadOnly_ButConfigurable);
119 defineDefaultProperty(QStringLiteral("next"), method_next, 1);
120 defineDefaultProperty(QStringLiteral("return"), method_return, 1);
121 defineDefaultProperty(QStringLiteral("throw"), method_throw, 1);
122 defineDefaultProperty(engine->symbol_toStringTag(), (v = engine->newString(QStringLiteral("Generator"))), Attr_ReadOnly_ButConfigurable);
123}
124
125ReturnedValue GeneratorPrototype::method_next(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc)
126{
127 ExecutionEngine *engine = f->engine();
128 const GeneratorObject *g = thisObject->as<GeneratorObject>();
129 if (!g || g->d()->state == GeneratorState::Executing)
130 return engine->throwTypeError();
131 Heap::GeneratorObject *gp = g->d();
132
133 if (gp->state == GeneratorState::Completed)
134 return IteratorPrototype::createIterResultObject(engine, Value::undefinedValue(), true);
135
136 return g->resume(engine, argc ? argv[0] : Value::undefinedValue(), {});
137}
138
139ReturnedValue GeneratorPrototype::method_return(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc)
140{
141 ExecutionEngine *engine = f->engine();
142 const GeneratorObject *g = thisObject->as<GeneratorObject>();
143 if (!g || g->d()->state == GeneratorState::Executing)
144 return engine->throwTypeError();
145
146 Heap::GeneratorObject *gp = g->d();
147
148 if (gp->state == GeneratorState::SuspendedStart)
149 gp->state = GeneratorState::Completed;
150
151 if (gp->state == GeneratorState::Completed)
152 return IteratorPrototype::createIterResultObject(engine, argc ? argv[0] : Value::undefinedValue(), true);
153
154 return g->resume(engine, argc ? argv[0] : Value::undefinedValue(), Value::emptyValue());
155}
156
157ReturnedValue GeneratorPrototype::method_throw(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc)
158{
159 ExecutionEngine *engine = f->engine();
160 const GeneratorObject *g = thisObject->as<GeneratorObject>();
161 if (!g || g->d()->state == GeneratorState::Executing)
162 return engine->throwTypeError();
163
164 Heap::GeneratorObject *gp = g->d();
165
166
167 if (gp->state == GeneratorState::SuspendedStart || gp->state == GeneratorState::Completed) {
168 gp->state = GeneratorState::Completed;
169 engine->throwError(argc ? argv[0] : Value::undefinedValue());
170 return Encode::undefined();
171 }
172
173 return g->resume(engine, Value::undefinedValue(), argc ? argv[0] : Value::undefinedValue());
174}
175
176ReturnedValue GeneratorObject::resume(ExecutionEngine *engine, const Value &arg, std::optional<Value> exception) const
177{
178 Heap::GeneratorObject *gp = d();
179 gp->state = GeneratorState::Executing;
180 gp->cppFrame.setParentFrame(engine->currentStackFrame);
181 engine->currentStackFrame = &gp->cppFrame;
182
183 Q_ASSERT(gp->cppFrame.yield() != nullptr);
184 const char *code = gp->cppFrame.yield();
185 gp->cppFrame.setYield(nullptr);
186 gp->cppFrame.jsFrame->accumulator = arg;
187 gp->cppFrame.setYieldIsIterator(false);
188
189 Scope scope(engine);
190
191 CHECK_STACK_LIMITS(scope.engine)
192
193 // A value to be thrown will be passed in by `method_throw` or
194 // `method_return` when they need to resume the generator.
195 // For `method_throw` this will be the value that was passed to
196 // `throw` itself.
197 // For `method_return` this will be an `emptyValue`.
198 // The empty value will be used as a signal that `return` was
199 // called and managed in the execution of a `Resume` instruction
200 // during `interpret`.
201 if (exception)
202 engine->throwError(*exception);
203 ScopedValue result(scope, Moth::VME::interpret(&gp->cppFrame, engine, code));
204
205 engine->currentStackFrame = gp->cppFrame.parentFrame();
206
207 bool done = (gp->cppFrame.yield() == nullptr);
208 gp->state = done ? GeneratorState::Completed : GeneratorState::SuspendedYield;
209 if (engine->hasException)
210 return Encode::undefined();
211 if (gp->cppFrame.yieldIsIterator())
212 return result->asReturnedValue();
213 return IteratorPrototype::createIterResultObject(engine, result, done);
214}
215
217
218Heap::FunctionObject *MemberGeneratorFunction::create(ExecutionContext *context, Function *function, Object *homeObject, String *name)
219{
220 Scope scope(context);
221 Scoped<MemberGeneratorFunction> g(scope, context->engine()->memoryManager->allocate<MemberGeneratorFunction>(context, function, name));
222 g->d()->homeObject.set(scope.engine, homeObject->d());
223 ScopedObject proto(scope, scope.engine->newObject());
224 proto->setPrototypeOf(scope.engine->generatorPrototype());
225 g->defineDefaultProperty(scope.engine->id_prototype(), proto, Attr_NotConfigurable|Attr_NotEnumerable);
226 g->setPrototypeOf(ScopedObject(scope, scope.engine->generatorFunctionCtor()->get(scope.engine->id_prototype())));
227 return g->d();
228}
229
230ReturnedValue MemberGeneratorFunction::virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc)
231{
232 return GeneratorFunction::virtualCall(f, thisObject, argv, argc);
233}
#define CHECK_STACK_LIMITS(v4)
DEFINE_OBJECT_VTABLE(MemberGeneratorFunction)
DEFINE_OBJECT_VTABLE(GeneratorObject)
DEFINE_OBJECT_VTABLE(GeneratorFunctionCtor)
DEFINE_OBJECT_VTABLE(GeneratorFunction)