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