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