| Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/qtdeclarative/src/qtdeclarative/src/qml/jsruntime/qv4generatorobject.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||||||||
| 2 | ** | - | ||||||||||||
| 3 | ** Copyright (C) 2018 The Qt Company Ltd. | - | ||||||||||||
| 4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||
| 5 | ** | - | ||||||||||||
| 6 | ** This file is part of the QtQml module of the Qt Toolkit. | - | ||||||||||||
| 7 | ** | - | ||||||||||||
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||||||||
| 9 | ** Commercial License Usage | - | ||||||||||||
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
| 11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
| 12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||||||||
| 16 | ** | - | ||||||||||||
| 17 | ** GNU Lesser General Public License Usage | - | ||||||||||||
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
| 19 | ** General Public License version 3 as published by the Free Software | - | ||||||||||||
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||
| 21 | ** packaging of this file. Please review the following information to | - | ||||||||||||
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||
| 24 | ** | - | ||||||||||||
| 25 | ** GNU General Public License Usage | - | ||||||||||||
| 26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||
| 27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||
| 28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||
| 29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||
| 31 | ** included in the packaging of this file. Please review the following | - | ||||||||||||
| 32 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||
| 35 | ** | - | ||||||||||||
| 36 | ** $QT_END_LICENSE$ | - | ||||||||||||
| 37 | ** | - | ||||||||||||
| 38 | ****************************************************************************/ | - | ||||||||||||
| 39 | - | |||||||||||||
| 40 | #include <qv4generatorobject_p.h> | - | ||||||||||||
| 41 | #include <qv4symbol_p.h> | - | ||||||||||||
| 42 | #include <qv4iterator_p.h> | - | ||||||||||||
| 43 | #include <qv4jscall_p.h> | - | ||||||||||||
| 44 | #include <qv4vme_moth_p.h> | - | ||||||||||||
| 45 | - | |||||||||||||
| 46 | using namespace QV4; | - | ||||||||||||
| 47 | - | |||||||||||||
| 48 | DEFINE_OBJECT_VTABLE(GeneratorFunctionCtor); | - | ||||||||||||
| 49 | DEFINE_OBJECT_VTABLE(GeneratorFunction); | - | ||||||||||||
| 50 | DEFINE_OBJECT_VTABLE(GeneratorObject); | - | ||||||||||||
| 51 | - | |||||||||||||
| 52 | void Heap::GeneratorFunctionCtor::init(QV4::ExecutionContext *scope) | - | ||||||||||||
| 53 | { | - | ||||||||||||
| 54 | Heap::FunctionObject::init(scope, QStringLiteral("GeneratorFunction")); executed 98596 times by 153 tests: return qstring_literal_temp;Executed by:
| 98596 | ||||||||||||
| 55 | } executed 99150 times by 153 tests: end of blockExecuted by:
| 99150 | ||||||||||||
| 56 | - | |||||||||||||
| 57 | ReturnedValue GeneratorFunctionCtor::virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *) | - | ||||||||||||
| 58 | { | - | ||||||||||||
| 59 | ExecutionEngine *engine = f->engine(); | - | ||||||||||||
| 60 | - | |||||||||||||
| 61 | QQmlRefPointer<CompiledData::CompilationUnit> compilationUnit = parse(engine, argv, argc, Type_Generator); | - | ||||||||||||
| 62 | if (engine->hasException)
| 8-111 | ||||||||||||
| 63 | return Encode::undefined(); executed 8 times by 1 test: return Encode::undefined();Executed by:
| 8 | ||||||||||||
| 64 | - | |||||||||||||
| 65 | Function *vmf = compilationUnit->linkToEngine(engine); | - | ||||||||||||
| 66 | ExecutionContext *global = engine->scriptContext(); | - | ||||||||||||
| 67 | return Encode(GeneratorFunction::create(global, vmf)); executed 111 times by 1 test: return Encode(GeneratorFunction::create(global, vmf));Executed by:
| 111 | ||||||||||||
| 68 | } | - | ||||||||||||
| 69 | - | |||||||||||||
| 70 | // 15.3.1: This is equivalent to new Function(...) | - | ||||||||||||
| 71 | ReturnedValue GeneratorFunctionCtor::virtualCall(const FunctionObject *f, const Value *, const Value *argv, int argc) | - | ||||||||||||
| 72 | { | - | ||||||||||||
| 73 | return virtualCallAsConstructor(f, argv, argc, f); executed 92 times by 1 test: return virtualCallAsConstructor(f, argv, argc, f);Executed by:
| 92 | ||||||||||||
| 74 | } | - | ||||||||||||
| 75 | - | |||||||||||||
| 76 | Heap::FunctionObject *GeneratorFunction::create(ExecutionContext *context, Function *function) | - | ||||||||||||
| 77 | { | - | ||||||||||||
| 78 | Scope scope(context); | - | ||||||||||||
| 79 | Scoped<GeneratorFunction> g(scope, context->engine()->memoryManager->allocate<GeneratorFunction>(context, function)); | - | ||||||||||||
| 80 | ScopedObject proto(scope, scope.engine->newObject()); | - | ||||||||||||
| 81 | proto->setPrototypeOf(scope.engine->generatorPrototype()); | - | ||||||||||||
| 82 | g->defineDefaultProperty(scope.engine->id_prototype(), proto, Attr_NotConfigurable|Attr_NotEnumerable); | - | ||||||||||||
| 83 | g->setPrototypeOf(ScopedObject(scope, scope.engine->generatorFunctionCtor()->get(scope.engine->id_prototype()))); | - | ||||||||||||
| 84 | return g->d(); executed 6223 times by 1 test: return g->d();Executed by:
| 6223 | ||||||||||||
| 85 | } | - | ||||||||||||
| 86 | - | |||||||||||||
| 87 | ReturnedValue GeneratorFunction::virtualCallAsConstructor(const FunctionObject *f, const Value *, int, const Value *) | - | ||||||||||||
| 88 | { | - | ||||||||||||
| 89 | return f->engine()->throwTypeError(); executed 16 times by 1 test: return f->engine()->throwTypeError();Executed by:
| 16 | ||||||||||||
| 90 | } | - | ||||||||||||
| 91 | - | |||||||||||||
| 92 | ReturnedValue GeneratorFunction::virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc) | - | ||||||||||||
| 93 | { | - | ||||||||||||
| 94 | const GeneratorFunction *gf = static_cast<const GeneratorFunction *>(f); | - | ||||||||||||
| 95 | Function *function = gf->function(); | - | ||||||||||||
| 96 | ExecutionEngine *engine = gf->engine(); | - | ||||||||||||
| 97 | - | |||||||||||||
| 98 | // We need to set up a separate stack for the generator, as it's being re-entered | - | ||||||||||||
| 99 | uint stackSize = argc // space for the original arguments | - | ||||||||||||
| 100 | + CppStackFrame::requiredJSStackFrameSize(function); // space for the JS stack frame | - | ||||||||||||
| 101 | - | |||||||||||||
| 102 | size_t requiredMemory = sizeof(GeneratorObject::Data) - sizeof(Value) + sizeof(Value) * stackSize; | - | ||||||||||||
| 103 | - | |||||||||||||
| 104 | Scope scope(gf); | - | ||||||||||||
| 105 | Scoped<GeneratorObject> g(scope, scope.engine->memoryManager->allocManaged<GeneratorObject>(requiredMemory, scope.engine->classes[EngineBase::Class_GeneratorObject])); | - | ||||||||||||
| 106 | g->setPrototypeOf(ScopedObject(scope, gf->get(scope.engine->id_prototype()))); | - | ||||||||||||
| 107 | - | |||||||||||||
| 108 | Heap::GeneratorObject *gp = g->d(); | - | ||||||||||||
| 109 | gp->stack.size = stackSize; | - | ||||||||||||
| 110 | gp->stack.alloc = stackSize; | - | ||||||||||||
| 111 | - | |||||||||||||
| 112 | // copy original arguments | - | ||||||||||||
| 113 | memcpy(gp->stack.values, argv, argc*sizeof(Value)); | - | ||||||||||||
| 114 | gp->cppFrame.init(engine, function, gp->stack.values, argc); | - | ||||||||||||
| 115 | gp->cppFrame.setupJSFrame(&gp->stack.values[argc], *gf, gf->scope(), | - | ||||||||||||
| 116 | thisObject ? *thisObject : Primitive::undefinedValue(), | - | ||||||||||||
| 117 | Primitive::undefinedValue()); | - | ||||||||||||
| 118 | - | |||||||||||||
| 119 | gp->cppFrame.push(); | - | ||||||||||||
| 120 | - | |||||||||||||
| 121 | Moth::VME::interpret(&gp->cppFrame, engine, function->codeData); | - | ||||||||||||
| 122 | gp->state = GeneratorState::SuspendedStart; | - | ||||||||||||
| 123 | - | |||||||||||||
| 124 | gp->cppFrame.pop(); | - | ||||||||||||
| 125 | return g->asReturnedValue(); executed 7777 times by 1 test: return g->asReturnedValue();Executed by:
| 7777 | ||||||||||||
| 126 | } | - | ||||||||||||
| 127 | - | |||||||||||||
| 128 | - | |||||||||||||
| 129 | void Heap::GeneratorPrototype::init() | - | ||||||||||||
| 130 | { | - | ||||||||||||
| 131 | Heap::FunctionObject::init(); | - | ||||||||||||
| 132 | } never executed: end of block | 0 | ||||||||||||
| 133 | - | |||||||||||||
| 134 | - | |||||||||||||
| 135 | void GeneratorPrototype::init(ExecutionEngine *engine, Object *ctor) | - | ||||||||||||
| 136 | { | - | ||||||||||||
| 137 | Scope scope(engine); | - | ||||||||||||
| 138 | ScopedValue v(scope); | - | ||||||||||||
| 139 | - | |||||||||||||
| 140 | ScopedObject ctorProto(scope, engine->newObject(engine->newInternalClass(Object::staticVTable(), engine->functionPrototype()))); | - | ||||||||||||
| 141 | - | |||||||||||||
| 142 | ctor->defineReadonlyConfigurableProperty(engine->id_length(), Primitive::fromInt32(1)); | - | ||||||||||||
| 143 | ctor->defineReadonlyProperty(engine->id_prototype(), ctorProto); | - | ||||||||||||
| 144 | - | |||||||||||||
| 145 | ctorProto->defineDefaultProperty(QStringLiteral("constructor"), (v = ctor), Attr_ReadOnly_ButConfigurable); executed 98715 times by 153 tests: return qstring_literal_temp;Executed by:
| 98715 | ||||||||||||
| 146 | ctorProto->defineDefaultProperty(engine->symbol_toStringTag(), (v = engine->newIdentifier(QStringLiteral("GeneratorFunction"))), Attr_ReadOnly_ButConfigurable); executed 98995 times by 153 tests: return qstring_literal_temp;Executed by:
| 98995 | ||||||||||||
| 147 | ctorProto->defineDefaultProperty(engine->id_prototype(), (v = this), Attr_ReadOnly_ButConfigurable); | - | ||||||||||||
| 148 | - | |||||||||||||
| 149 | setPrototypeOf(engine->iteratorPrototype()); | - | ||||||||||||
| 150 | defineDefaultProperty(QStringLiteral("constructor"), ctorProto, Attr_ReadOnly_ButConfigurable); executed 99072 times by 153 tests: return qstring_literal_temp;Executed by:
| 99072 | ||||||||||||
| 151 | defineDefaultProperty(QStringLiteral("next"), method_next, 1); executed 99096 times by 153 tests: return qstring_literal_temp;Executed by:
| 99096 | ||||||||||||
| 152 | defineDefaultProperty(QStringLiteral("return"), method_return, 1); executed 99107 times by 153 tests: return qstring_literal_temp;Executed by:
| 99107 | ||||||||||||
| 153 | defineDefaultProperty(QStringLiteral("throw"), method_throw, 1); executed 99095 times by 153 tests: return qstring_literal_temp;Executed by:
| 99095 | ||||||||||||
| 154 | defineDefaultProperty(engine->symbol_toStringTag(), (v = engine->newString(QStringLiteral("Generator"))), Attr_ReadOnly_ButConfigurable); executed 99061 times by 153 tests: return qstring_literal_temp;Executed by:
| 99061 | ||||||||||||
| 155 | } executed 99017 times by 153 tests: end of blockExecuted by:
| 99017 | ||||||||||||
| 156 | - | |||||||||||||
| 157 | ReturnedValue GeneratorPrototype::method_next(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)
| 8-7766 | ||||||||||||
| 162 | return engine->throwTypeError(); executed 88 times by 1 test: return engine->throwTypeError();Executed by:
| 88 | ||||||||||||
| 163 | Heap::GeneratorObject *gp = g->d(); | - | ||||||||||||
| 164 | - | |||||||||||||
| 165 | if (gp->state == GeneratorState::Completed)
| 716-7045 | ||||||||||||
| 166 | return IteratorPrototype::createIterResultObject(engine, Primitive::undefinedValue(), true); executed 716 times by 1 test: return IteratorPrototype::createIterResultObject(engine, Primitive::undefinedValue(), true);Executed by:
| 716 | ||||||||||||
| 167 | - | |||||||||||||
| 168 | return g->resume(engine, argc ? argv[0] : Primitive::undefinedValue()); executed 7043 times by 1 test: return g->resume(engine, argc ? argv[0] : Primitive::undefinedValue());Executed by:
| 7043 | ||||||||||||
| 169 | } | - | ||||||||||||
| 170 | - | |||||||||||||
| 171 | ReturnedValue GeneratorPrototype::method_return(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc) | - | ||||||||||||
| 172 | { | - | ||||||||||||
| 173 | ExecutionEngine *engine = f->engine(); | - | ||||||||||||
| 174 | const GeneratorObject *g = thisObject->as<GeneratorObject>(); | - | ||||||||||||
| 175 | if (!g || g->d()->state == GeneratorState::Executing)
| 4-500 | ||||||||||||
| 176 | return engine->throwTypeError(); executed 84 times by 1 test: return engine->throwTypeError();Executed by:
| 84 | ||||||||||||
| 177 | - | |||||||||||||
| 178 | Heap::GeneratorObject *gp = g->d(); | - | ||||||||||||
| 179 | - | |||||||||||||
| 180 | if (gp->state == GeneratorState::SuspendedStart)
| 4-492 | ||||||||||||
| 181 | gp->state = GeneratorState::Completed; executed 4 times by 1 test: gp->state = GeneratorState::Completed;Executed by:
| 4 | ||||||||||||
| 182 | - | |||||||||||||
| 183 | if (gp->state == GeneratorState::Completed)
| 8-488 | ||||||||||||
| 184 | return IteratorPrototype::createIterResultObject(engine, argc ? argv[0] : Primitive::undefinedValue(), true); executed 8 times by 1 test: return IteratorPrototype::createIterResultObject(engine, argc ? argv[0] : Primitive::undefinedValue(), true);Executed by:
| 8 | ||||||||||||
| 185 | - | |||||||||||||
| 186 | // the bytecode interpreter interprets an exception with empty value as | - | ||||||||||||
| 187 | // a yield called with return() | - | ||||||||||||
| 188 | engine->throwError(Primitive::emptyValue()); | - | ||||||||||||
| 189 | - | |||||||||||||
| 190 | return g->resume(engine, argc ? argv[0]: Primitive::undefinedValue()); executed 488 times by 1 test: return g->resume(engine, argc ? argv[0]: Primitive::undefinedValue());Executed by:
| 488 | ||||||||||||
| 191 | } | - | ||||||||||||
| 192 | - | |||||||||||||
| 193 | ReturnedValue GeneratorPrototype::method_throw(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc) | - | ||||||||||||
| 194 | { | - | ||||||||||||
| 195 | ExecutionEngine *engine = f->engine(); | - | ||||||||||||
| 196 | const GeneratorObject *g = thisObject->as<GeneratorObject>(); | - | ||||||||||||
| 197 | if (!g || g->d()->state == GeneratorState::Executing)
| 4-80 | ||||||||||||
| 198 | return engine->throwTypeError(); executed 84 times by 1 test: return engine->throwTypeError();Executed by:
| 84 | ||||||||||||
| 199 | - | |||||||||||||
| 200 | Heap::GeneratorObject *gp = g->d(); | - | ||||||||||||
| 201 | - | |||||||||||||
| 202 | engine->throwError(argc ? argv[0]: Primitive::undefinedValue()); | - | ||||||||||||
| 203 | - | |||||||||||||
| 204 | if (gp->state == GeneratorState::SuspendedStart || gp->state == GeneratorState::Completed) {
| 4-56 | ||||||||||||
| 205 | gp->state = GeneratorState::Completed; | - | ||||||||||||
| 206 | return Encode::undefined(); executed 8 times by 1 test: return Encode::undefined();Executed by:
| 8 | ||||||||||||
| 207 | } | - | ||||||||||||
| 208 | - | |||||||||||||
| 209 | return g->resume(engine, Primitive::undefinedValue()); executed 52 times by 1 test: return g->resume(engine, Primitive::undefinedValue());Executed by:
| 52 | ||||||||||||
| 210 | } | - | ||||||||||||
| 211 | - | |||||||||||||
| 212 | ReturnedValue GeneratorObject::resume(ExecutionEngine *engine, const Value &arg) const | - | ||||||||||||
| 213 | { | - | ||||||||||||
| 214 | Heap::GeneratorObject *gp = d(); | - | ||||||||||||
| 215 | gp->state = GeneratorState::Executing; | - | ||||||||||||
| 216 | gp->cppFrame.parent = engine->currentStackFrame; | - | ||||||||||||
| 217 | engine->currentStackFrame = &gp->cppFrame; | - | ||||||||||||
| 218 | - | |||||||||||||
| 219 | Q_ASSERT(gp->cppFrame.yield != nullptr); | - | ||||||||||||
| 220 | const char *code = gp->cppFrame.yield; | - | ||||||||||||
| 221 | gp->cppFrame.yield = nullptr; | - | ||||||||||||
| 222 | gp->cppFrame.jsFrame->accumulator = arg; | - | ||||||||||||
| 223 | - | |||||||||||||
| 224 | Scope scope(engine); | - | ||||||||||||
| 225 | ScopedValue result(scope, Moth::VME::interpret(&gp->cppFrame, engine, code)); | - | ||||||||||||
| 226 | - | |||||||||||||
| 227 | engine->currentStackFrame = gp->cppFrame.parent; | - | ||||||||||||
| 228 | - | |||||||||||||
| 229 | bool done = (gp->cppFrame.yield == nullptr); | - | ||||||||||||
| 230 | gp->state = done ? GeneratorState::Completed : GeneratorState::SuspendedYield;
| 1834-5756 | ||||||||||||
| 231 | if (engine->hasException)
| 612-6985 | ||||||||||||
| 232 | return Encode::undefined(); executed 612 times by 1 test: return Encode::undefined();Executed by:
| 612 | ||||||||||||
| 233 | return IteratorPrototype::createIterResultObject(engine, result, done); executed 6985 times by 1 test: return IteratorPrototype::createIterResultObject(engine, result, done);Executed by:
| 6985 | ||||||||||||
| 234 | } | - | ||||||||||||
| 235 | - | |||||||||||||
| 236 | DEFINE_OBJECT_VTABLE(MemberGeneratorFunction); | - | ||||||||||||
| 237 | - | |||||||||||||
| 238 | Heap::FunctionObject *MemberGeneratorFunction::create(ExecutionContext *context, Function *function) | - | ||||||||||||
| 239 | { | - | ||||||||||||
| 240 | Scope scope(context); | - | ||||||||||||
| 241 | Scoped<GeneratorFunction> g(scope, context->engine()->memoryManager->allocate<MemberGeneratorFunction>(context, function)); | - | ||||||||||||
| 242 | ScopedObject proto(scope, scope.engine->newObject()); | - | ||||||||||||
| 243 | proto->setPrototypeOf(scope.engine->generatorPrototype()); | - | ||||||||||||
| 244 | g->defineDefaultProperty(scope.engine->id_prototype(), proto, Attr_NotConfigurable|Attr_NotEnumerable); | - | ||||||||||||
| 245 | g->setPrototypeOf(ScopedObject(scope, scope.engine->generatorFunctionCtor()->get(scope.engine->id_prototype()))); | - | ||||||||||||
| 246 | return g->d(); executed 3044 times by 1 test: return g->d();Executed by:
| 3044 | ||||||||||||
| 247 | } | - | ||||||||||||
| 248 | - | |||||||||||||
| 249 | ReturnedValue MemberGeneratorFunction::virtualCallAsConstructor(const FunctionObject *f, const Value *, int, const Value *) | - | ||||||||||||
| 250 | { | - | ||||||||||||
| 251 | return f->engine()->throwTypeError(QStringLiteral("Function is not a constructor.")); never executed: return f->engine()->throwTypeError(([]() noexcept -> QString { enum { Size = sizeof(u"" "Function is not a constructor.")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "Function is not a constructor." }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }()));never executed: return qstring_literal_temp; | 0 | ||||||||||||
| 252 | } | - | ||||||||||||
| Source code | Switch to Preprocessed file |