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