| 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 | |
| 10 | using namespace QV4; |
| 11 | |
| 12 | DEFINE_OBJECT_VTABLE(GeneratorFunctionCtor); |
| 13 | DEFINE_OBJECT_VTABLE(GeneratorFunction); |
| 14 | DEFINE_OBJECT_VTABLE(GeneratorObject); |
| 15 | |
| 16 | void Heap::GeneratorFunctionCtor::init(QV4::ExecutionEngine *engine) |
| 17 | { |
| 18 | Heap::FunctionObject::init(engine, QStringLiteral("GeneratorFunction" )); |
| 19 | } |
| 20 | |
| 21 | ReturnedValue 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, t: 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(scope: global, function: 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(...) |
| 42 | ReturnedValue GeneratorFunctionCtor::virtualCall(const FunctionObject *f, const Value *, const Value *argv, int argc) |
| 43 | { |
| 44 | return virtualCallAsConstructor(f, argv, argc, newTarget: f); |
| 45 | } |
| 46 | |
| 47 | Heap::FunctionObject *GeneratorFunction::create(ExecutionContext *context, Function *function) |
| 48 | { |
| 49 | Scope scope(context); |
| 50 | Scoped<GeneratorFunction> g(scope, context->engine()->memoryManager->allocate<GeneratorFunction>(args&: context, args&: function)); |
| 51 | ScopedObject proto(scope, scope.engine->newObject()); |
| 52 | proto->setPrototypeOf(scope.engine->generatorPrototype()); |
| 53 | g->defineDefaultProperty(name: scope.engine->id_prototype(), value: proto, attributes: Attr_NotConfigurable|Attr_NotEnumerable); |
| 54 | g->setPrototypeOf(ScopedObject(scope, scope.engine->generatorFunctionCtor()->get(name: scope.engine->id_prototype()))); |
| 55 | return g->d(); |
| 56 | } |
| 57 | |
| 58 | ReturnedValue 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>(ic: engine->classes[EngineBase::Class_GeneratorObject])); |
| 66 | g->setPrototypeOf(ScopedObject(scope, gf->get(name: 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(e: engine, newVal: engine->newArrayObject(count: argc)); |
| 71 | gp->jsFrame.set(e: engine, newVal: engine->newArrayObject( |
| 72 | count: JSTypesStackFrame::requiredJSStackFrameSize(v4Function: function))); |
| 73 | |
| 74 | // copy original arguments |
| 75 | for (int i = 0; i < argc; i++) |
| 76 | gp->values->arrayData->setArrayData(e: engine, index: i, newVal: argv[i]); |
| 77 | |
| 78 | gp->cppFrame.init(v4Function: function, argv: gp->values->arrayData->values.values, argc); |
| 79 | gp->cppFrame.setupJSFrame(stackSpace: gp->jsFrame->arrayData->values.values, function: *gf, scope: gf->scope(), |
| 80 | thisObject: thisObject ? *thisObject : Value::undefinedValue(), |
| 81 | newTarget: Value::undefinedValue()); |
| 82 | |
| 83 | gp->cppFrame.push(engine); |
| 84 | |
| 85 | CHECK_STACK_LIMITS(scope.engine) |
| 86 | Moth::VME::interpret(frame: &gp->cppFrame, engine, codeEntry: function->codeData); |
| 87 | |
| 88 | gp->state = GeneratorState::SuspendedStart; |
| 89 | |
| 90 | gp->cppFrame.pop(engine); |
| 91 | return g->asReturnedValue(); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | void Heap::GeneratorPrototype::init() |
| 96 | { |
| 97 | Heap::FunctionObject::init(); |
| 98 | } |
| 99 | |
| 100 | |
| 101 | void GeneratorPrototype::init(ExecutionEngine *engine, Object *ctor) |
| 102 | { |
| 103 | Scope scope(engine); |
| 104 | ScopedValue v(scope); |
| 105 | |
| 106 | Scoped<InternalClass> ic(scope, engine->newInternalClass( |
| 107 | vtable: Object::staticVTable(), prototype: engine->functionPrototype())); |
| 108 | ScopedObject ctorProto(scope, engine->newObject(internalClass: ic->d())); |
| 109 | |
| 110 | ctor->defineReadonlyConfigurableProperty(name: engine->id_length(), value: Value::fromInt32(i: 1)); |
| 111 | ctor->defineReadonlyProperty(name: engine->id_prototype(), value: ctorProto); |
| 112 | |
| 113 | ctorProto->defineDefaultProperty(QStringLiteral("constructor" ), value: (v = ctor), attributes: Attr_ReadOnly_ButConfigurable); |
| 114 | ctorProto->defineDefaultProperty(name: engine->symbol_toStringTag(), value: (v = engine->newIdentifier(QStringLiteral("GeneratorFunction" ))), attributes: Attr_ReadOnly_ButConfigurable); |
| 115 | ctorProto->defineDefaultProperty(name: engine->id_prototype(), value: (v = this), attributes: Attr_ReadOnly_ButConfigurable); |
| 116 | |
| 117 | setPrototypeOf(engine->iteratorPrototype()); |
| 118 | defineDefaultProperty(QStringLiteral("constructor" ), value: ctorProto, attributes: Attr_ReadOnly_ButConfigurable); |
| 119 | defineDefaultProperty(QStringLiteral("next" ), code: method_next, argumentCount: 1); |
| 120 | defineDefaultProperty(QStringLiteral("return" ), code: method_return, argumentCount: 1); |
| 121 | defineDefaultProperty(QStringLiteral("throw" ), code: method_throw, argumentCount: 1); |
| 122 | defineDefaultProperty(name: engine->symbol_toStringTag(), value: (v = engine->newString(QStringLiteral("Generator" ))), attributes: Attr_ReadOnly_ButConfigurable); |
| 123 | } |
| 124 | |
| 125 | ReturnedValue 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: Value::undefinedValue(), done: true); |
| 135 | |
| 136 | return g->resume(engine, arg: argc ? argv[0] : Value::undefinedValue(), {}); |
| 137 | } |
| 138 | |
| 139 | ReturnedValue 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, value: argc ? argv[0] : Value::undefinedValue(), done: true); |
| 153 | |
| 154 | return g->resume(engine, arg: argc ? argv[0] : Value::undefinedValue(), Value::emptyValue()); |
| 155 | } |
| 156 | |
| 157 | ReturnedValue 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(value: argc ? argv[0] : Value::undefinedValue()); |
| 170 | return Encode::undefined(); |
| 171 | } |
| 172 | |
| 173 | return g->resume(engine, arg: Value::undefinedValue(), argc ? argv[0] : Value::undefinedValue()); |
| 174 | } |
| 175 | |
| 176 | ReturnedValue 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(value: *exception); |
| 203 | ScopedValue result(scope, Moth::VME::interpret(frame: &gp->cppFrame, engine, codeEntry: 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, value: result, done); |
| 214 | } |
| 215 | |
| 216 | DEFINE_OBJECT_VTABLE(MemberGeneratorFunction); |
| 217 | |
| 218 | Heap::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>(args&: context, args&: function, args&: name)); |
| 222 | g->d()->homeObject.set(e: scope.engine, newVal: homeObject->d()); |
| 223 | ScopedObject proto(scope, scope.engine->newObject()); |
| 224 | proto->setPrototypeOf(scope.engine->generatorPrototype()); |
| 225 | g->defineDefaultProperty(name: scope.engine->id_prototype(), value: proto, attributes: Attr_NotConfigurable|Attr_NotEnumerable); |
| 226 | g->setPrototypeOf(ScopedObject(scope, scope.engine->generatorFunctionCtor()->get(name: scope.engine->id_prototype()))); |
| 227 | return g->d(); |
| 228 | } |
| 229 | |
| 230 | ReturnedValue MemberGeneratorFunction::virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc) |
| 231 | { |
| 232 | return GeneratorFunction::virtualCall(f, thisObject, argv, argc); |
| 233 | } |
| 234 | |