| 1 | // Copyright (C) 2016 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 "qv4object_p.h" |
| 5 | #include "qv4function_p.h" |
| 6 | #include "qv4symbol_p.h" |
| 7 | #include <private/qv4mm_p.h> |
| 8 | |
| 9 | #include "qv4scopedvalue_p.h" |
| 10 | #include "qv4argumentsobject_p.h" |
| 11 | |
| 12 | #include <private/qqmljsengine_p.h> |
| 13 | #include <private/qqmljslexer_p.h> |
| 14 | #include <private/qqmljsparser_p.h> |
| 15 | #include <private/qqmljsast_p.h> |
| 16 | #include <private/qqmljavascriptexpression_p.h> |
| 17 | #include <private/qqmlengine_p.h> |
| 18 | #include <qv4runtimecodegen_p.h> |
| 19 | #include "private/qlocale_tools_p.h" |
| 20 | #include "private/qqmlbuiltinfunctions_p.h" |
| 21 | #include <private/qv4jscall_p.h> |
| 22 | #include <private/qv4vme_moth_p.h> |
| 23 | #include <private/qv4alloca_p.h> |
| 24 | |
| 25 | #include <QtCore/QDebug> |
| 26 | #include <algorithm> |
| 27 | |
| 28 | using namespace QV4; |
| 29 | |
| 30 | |
| 31 | DEFINE_OBJECT_VTABLE(FunctionObject); |
| 32 | |
| 33 | void Heap::FunctionObject::init(QV4::ExecutionEngine *engine, QV4::String *name) |
| 34 | { |
| 35 | Object::init(); |
| 36 | Scope s(engine); |
| 37 | ScopedFunctionObject f(s, this); |
| 38 | if (name) |
| 39 | f->setName(name); |
| 40 | } |
| 41 | |
| 42 | void Heap::FunctionObject::init(QV4::ExecutionEngine *engine, const QString &name) |
| 43 | { |
| 44 | Scope valueScope(engine); |
| 45 | ScopedString s(valueScope, engine->newString(s: name)); |
| 46 | init(engine, name: s); |
| 47 | } |
| 48 | |
| 49 | void Heap::FunctionObject::init() |
| 50 | { |
| 51 | init(engine: internalClass->engine, name: static_cast<QV4::String *>(nullptr)); |
| 52 | } |
| 53 | |
| 54 | void Heap::JavaScriptFunctionObject::init( |
| 55 | QV4::ExecutionContext *scope, Function *function, QV4::String *n) |
| 56 | { |
| 57 | Q_ASSERT(n || function); |
| 58 | Scope s(scope->engine()); |
| 59 | ScopedString name(s, n ? n->d() : function->name()); |
| 60 | FunctionObject::init(engine: s.engine, name); |
| 61 | this->scope.set(e: s.engine, newVal: scope->d()); |
| 62 | setFunction(function); |
| 63 | } |
| 64 | |
| 65 | void Heap::JavaScriptFunctionObject::setFunction(Function *f) |
| 66 | { |
| 67 | if (f) { |
| 68 | function = f; |
| 69 | function->executableCompilationUnit()->addref(); |
| 70 | } |
| 71 | } |
| 72 | void Heap::JavaScriptFunctionObject::destroy() |
| 73 | { |
| 74 | if (function) |
| 75 | function->executableCompilationUnit()->release(); |
| 76 | FunctionObject::destroy(); |
| 77 | } |
| 78 | |
| 79 | void Heap::DynamicFunctionObject::init( |
| 80 | QV4::ExecutionEngine *engine, QV4::String *name, VTable::Call call) |
| 81 | { |
| 82 | FunctionObject::init(engine, name); |
| 83 | jsCall = call; |
| 84 | } |
| 85 | |
| 86 | void FunctionObject::createDefaultPrototypeProperty(uint protoConstructorSlot) |
| 87 | { |
| 88 | Scope s(this); |
| 89 | |
| 90 | Q_ASSERT(s.engine->internalClasses(EngineBase::Class_ObjectProto)->verifyIndex(s.engine->id_constructor()->propertyKey(), protoConstructorSlot)); |
| 91 | |
| 92 | ScopedObject proto(s, s.engine->newObject(internalClass: s.engine->internalClasses(icType: EngineBase::Class_ObjectProto))); |
| 93 | proto->setProperty(index: protoConstructorSlot, b: d()); |
| 94 | defineDefaultProperty(name: s.engine->id_prototype(), value: proto, attributes: Attr_NotEnumerable|Attr_NotConfigurable); |
| 95 | } |
| 96 | |
| 97 | ReturnedValue FunctionObject::name() const |
| 98 | { |
| 99 | return get(name: engine()->id_name()); |
| 100 | } |
| 101 | |
| 102 | ReturnedValue FunctionObject::failCall() const |
| 103 | { |
| 104 | return engine()->throwTypeError(QStringLiteral("Function can only be called with |new|." )); |
| 105 | } |
| 106 | |
| 107 | ReturnedValue FunctionObject::failCallAsConstructor() const |
| 108 | { |
| 109 | return engine()->throwTypeError(QStringLiteral("Function is not a constructor." )); |
| 110 | } |
| 111 | |
| 112 | void FunctionObject::virtualConvertAndCall( |
| 113 | const FunctionObject *f, QObject *thisObject, |
| 114 | void **argv, const QMetaType *types, int argc) |
| 115 | { |
| 116 | QV4::convertAndCall(engine: f->engine(), thisObject, a: argv, types, argc, |
| 117 | call: [f](const Value *thisObject, const Value *argv, int argc) { |
| 118 | return f->call(thisObject, argv, argc); |
| 119 | }); |
| 120 | } |
| 121 | |
| 122 | Heap::FunctionObject *FunctionObject::createScriptFunction(ExecutionContext *scope, Function *function) |
| 123 | { |
| 124 | if (function->isArrowFunction()) |
| 125 | return scope->engine()->memoryManager->allocate<ArrowFunction>(args&: scope, args&: function); |
| 126 | return scope->engine()->memoryManager->allocate<ScriptFunction>(args&: scope, args&: function); |
| 127 | } |
| 128 | |
| 129 | Heap::FunctionObject *FunctionObject::createConstructorFunction( |
| 130 | ExecutionContext *scope, Function *function, Object *homeObject, bool isDerivedConstructor) |
| 131 | { |
| 132 | QV4::ExecutionEngine *engine = scope->engine(); |
| 133 | if (!function) { |
| 134 | Heap::DefaultClassConstructorFunction *c |
| 135 | = engine->memoryManager->allocate<DefaultClassConstructorFunction>(args&: scope); |
| 136 | c->isDerivedConstructor = isDerivedConstructor; |
| 137 | return c; |
| 138 | } |
| 139 | |
| 140 | Heap::ConstructorFunction *c |
| 141 | = engine->memoryManager->allocate<ConstructorFunction>(args&: scope, args&: function); |
| 142 | c->homeObject.set(e: engine, newVal: homeObject->d()); |
| 143 | c->isDerivedConstructor = isDerivedConstructor; |
| 144 | return c; |
| 145 | } |
| 146 | |
| 147 | Heap::FunctionObject *FunctionObject::createMemberFunction(ExecutionContext *scope, Function *function, Object *homeObject, QV4::String *name) |
| 148 | { |
| 149 | Heap::MemberFunction *m = scope->engine()->memoryManager->allocate<MemberFunction>(args&: scope, args&: function, args&: name); |
| 150 | m->homeObject.set(e: scope->engine(), newVal: homeObject->d()); |
| 151 | return m; |
| 152 | } |
| 153 | |
| 154 | Heap::FunctionObject *FunctionObject::createBuiltinFunction(ExecutionEngine *engine, StringOrSymbol *nameOrSymbol, VTable::Call code, int argumentCount) |
| 155 | { |
| 156 | Scope scope(engine); |
| 157 | ScopedString name(scope, nameOrSymbol); |
| 158 | if (!name) |
| 159 | name = engine->newString(s: QChar::fromLatin1(c: '[') + QStringView{nameOrSymbol->toQString()}.mid(pos: 1) + QChar::fromLatin1(c: ']')); |
| 160 | |
| 161 | ScopedFunctionObject function( |
| 162 | scope, engine->memoryManager->allocate<DynamicFunctionObject>(args&: engine, args&: name, args&: code)); |
| 163 | function->defineReadonlyConfigurableProperty(name: engine->id_length(), value: Value::fromInt32(i: argumentCount)); |
| 164 | return function->d(); |
| 165 | } |
| 166 | |
| 167 | ReturnedValue FunctionObject::getHomeObject() const |
| 168 | { |
| 169 | const MemberFunction *m = as<MemberFunction>(); |
| 170 | if (m) |
| 171 | return m->d()->homeObject->asReturnedValue(); |
| 172 | const ConstructorFunction *c = as<ConstructorFunction>(); |
| 173 | if (c) |
| 174 | return c->d()->homeObject->asReturnedValue(); |
| 175 | return Encode::undefined(); |
| 176 | } |
| 177 | |
| 178 | DEFINE_OBJECT_VTABLE(JavaScriptFunctionObject); |
| 179 | |
| 180 | QQmlSourceLocation JavaScriptFunctionObject::sourceLocation() const |
| 181 | { |
| 182 | return d()->function->sourceLocation(); |
| 183 | } |
| 184 | |
| 185 | DEFINE_OBJECT_VTABLE(DynamicFunctionObject); |
| 186 | |
| 187 | ReturnedValue DynamicFunctionObject::virtualCall( |
| 188 | const FunctionObject *f, const Value *thisObject, const Value *argv, int argc) { |
| 189 | Heap::DynamicFunctionObject *d = static_cast<const DynamicFunctionObject *>(f)->d(); |
| 190 | if (d->jsCall) |
| 191 | return d->jsCall(f, thisObject, argv, argc); |
| 192 | return d->internalClass->engine->throwTypeError( |
| 193 | QStringLiteral("Function can only be called with |new|." )); |
| 194 | } |
| 195 | |
| 196 | DEFINE_OBJECT_VTABLE(FunctionCtor); |
| 197 | |
| 198 | void Heap::FunctionCtor::init(QV4::ExecutionEngine *engine) |
| 199 | { |
| 200 | Heap::FunctionObject::init(engine, QStringLiteral("Function" )); |
| 201 | } |
| 202 | |
| 203 | // 15.3.2 |
| 204 | QQmlRefPointer<ExecutableCompilationUnit> FunctionCtor::parse(ExecutionEngine *engine, const Value *argv, int argc, Type t) |
| 205 | { |
| 206 | QString arguments; |
| 207 | QString body; |
| 208 | if (argc > 0) { |
| 209 | for (int i = 0, ei = argc - 1; i < ei; ++i) { |
| 210 | if (i) |
| 211 | arguments += QLatin1String(", " ); |
| 212 | arguments += argv[i].toQString(); |
| 213 | } |
| 214 | body = argv[argc - 1].toQString(); |
| 215 | } |
| 216 | if (engine->hasException) |
| 217 | return nullptr; |
| 218 | |
| 219 | QString function = (t == Type_Function ? QLatin1String("function anonymous(" ) : QLatin1String("function* anonymous(" )) + arguments + QLatin1String("\n){" ) + body + QLatin1String("\n}" ); |
| 220 | |
| 221 | QQmlJS::Engine ee; |
| 222 | QQmlJS::Lexer lexer(&ee); |
| 223 | lexer.setCode(code: function, lineno: 1, qmlMode: false); |
| 224 | QQmlJS::Parser parser(&ee); |
| 225 | |
| 226 | const bool parsed = parser.parseExpression(); |
| 227 | |
| 228 | if (!parsed) { |
| 229 | engine->throwSyntaxError(message: QLatin1String("Parse error" )); |
| 230 | return nullptr; |
| 231 | } |
| 232 | |
| 233 | QQmlJS::AST::FunctionExpression *fe = QQmlJS::AST::cast<QQmlJS::AST::FunctionExpression *>(parser.rootNode()); |
| 234 | if (!fe) { |
| 235 | engine->throwSyntaxError(message: QLatin1String("Parse error" )); |
| 236 | return nullptr; |
| 237 | } |
| 238 | |
| 239 | Compiler::Module module(QString(), QString(), engine->debugger() != nullptr); |
| 240 | |
| 241 | Compiler::JSUnitGenerator jsGenerator(&module); |
| 242 | RuntimeCodegen cg(engine, &jsGenerator, false); |
| 243 | cg.generateFromFunctionExpression(sourceCode: function, ast: fe, module: &module); |
| 244 | |
| 245 | if (engine->hasException) |
| 246 | return nullptr; |
| 247 | |
| 248 | return engine->insertCompilationUnit(unit: cg.generateCompilationUnit()); |
| 249 | } |
| 250 | |
| 251 | ReturnedValue FunctionCtor::virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *newTarget) |
| 252 | { |
| 253 | ExecutionEngine *engine = f->engine(); |
| 254 | |
| 255 | QQmlRefPointer<ExecutableCompilationUnit> compilationUnit |
| 256 | = parse(engine, argv, argc, t: Type_Function); |
| 257 | if (engine->hasException) |
| 258 | return Encode::undefined(); |
| 259 | |
| 260 | Function *vmf = compilationUnit->rootFunction(); |
| 261 | ExecutionContext *global = engine->scriptContext(); |
| 262 | ReturnedValue o = Encode(FunctionObject::createScriptFunction(scope: global, function: vmf)); |
| 263 | |
| 264 | if (!newTarget) |
| 265 | return o; |
| 266 | Scope scope(engine); |
| 267 | ScopedObject obj(scope, o); |
| 268 | obj->setProtoFromNewTarget(newTarget); |
| 269 | return obj->asReturnedValue(); |
| 270 | } |
| 271 | |
| 272 | // 15.3.1: This is equivalent to new Function(...) |
| 273 | ReturnedValue FunctionCtor::virtualCall(const FunctionObject *f, const Value *, const Value *argv, int argc) |
| 274 | { |
| 275 | return virtualCallAsConstructor(f, argv, argc, newTarget: f); |
| 276 | } |
| 277 | |
| 278 | DEFINE_OBJECT_VTABLE(FunctionPrototype); |
| 279 | |
| 280 | void Heap::FunctionPrototype::init() |
| 281 | { |
| 282 | Heap::FunctionObject::init(); |
| 283 | } |
| 284 | |
| 285 | void FunctionPrototype::init(ExecutionEngine *engine, Object *ctor) |
| 286 | { |
| 287 | Scope scope(engine); |
| 288 | ScopedObject o(scope); |
| 289 | |
| 290 | ctor->defineReadonlyConfigurableProperty(name: engine->id_length(), value: Value::fromInt32(i: 1)); |
| 291 | ctor->defineReadonlyProperty(name: engine->id_prototype(), value: (o = this)); |
| 292 | |
| 293 | defineReadonlyConfigurableProperty(name: engine->id_name(), value: *engine->id_empty()); |
| 294 | defineReadonlyConfigurableProperty(name: engine->id_length(), value: Value::fromInt32(i: 0)); |
| 295 | defineDefaultProperty(QStringLiteral("constructor" ), value: (o = ctor)); |
| 296 | defineDefaultProperty(name: engine->id_toString(), code: method_toString, argumentCount: 0); |
| 297 | defineDefaultProperty(QStringLiteral("apply" ), code: method_apply, argumentCount: 2); |
| 298 | defineDefaultProperty(QStringLiteral("call" ), code: method_call, argumentCount: 1); |
| 299 | defineDefaultProperty(QStringLiteral("bind" ), code: method_bind, argumentCount: 1); |
| 300 | defineDefaultProperty(name: engine->symbol_hasInstance(), code: method_hasInstance, argumentCount: 1, attributes: Attr_ReadOnly); |
| 301 | } |
| 302 | |
| 303 | ReturnedValue FunctionPrototype::virtualCall( |
| 304 | const FunctionObject *, const Value *, const Value *, int) |
| 305 | { |
| 306 | return Encode::undefined(); |
| 307 | } |
| 308 | |
| 309 | ReturnedValue FunctionPrototype::method_toString(const FunctionObject *b, const Value *thisObject, const Value *, int) |
| 310 | { |
| 311 | ExecutionEngine *v4 = b->engine(); |
| 312 | const FunctionObject *fun = thisObject->as<FunctionObject>(); |
| 313 | if (!fun) |
| 314 | return v4->throwTypeError(); |
| 315 | |
| 316 | const Scope scope(fun->engine()); |
| 317 | const ScopedString scopedFunctionName(scope, fun->name()); |
| 318 | const QString functionName(scopedFunctionName ? scopedFunctionName->toQString() : QString()); |
| 319 | QString functionAsString = QStringLiteral("function" ); |
| 320 | |
| 321 | // If fun->name() is empty, then there is no function name |
| 322 | // to append because the function is anonymous. |
| 323 | if (!functionName.isEmpty()) |
| 324 | functionAsString.append(s: QLatin1Char(' ') + functionName); |
| 325 | |
| 326 | functionAsString.append(QStringLiteral("() { [native code] }" )); |
| 327 | |
| 328 | return Encode(v4->newString(s: functionAsString)); |
| 329 | } |
| 330 | |
| 331 | ReturnedValue FunctionPrototype::method_apply(const QV4::FunctionObject *b, const Value *thisObject, const Value *argv, int argc) |
| 332 | { |
| 333 | ExecutionEngine *v4 = b->engine(); |
| 334 | const FunctionObject *f = thisObject->as<FunctionObject>(); |
| 335 | if (!f) |
| 336 | return v4->throwTypeError(); |
| 337 | thisObject = argc ? argv : nullptr; |
| 338 | if (argc < 2 || argv[1].isNullOrUndefined()) |
| 339 | return checkedResult(v4, result: f->call(thisObject, argv, argc: 0)); |
| 340 | |
| 341 | Object *arr = argv[1].objectValue(); |
| 342 | if (!arr) |
| 343 | return v4->throwTypeError(); |
| 344 | |
| 345 | Scope scope(v4); |
| 346 | const int len = v4->safeForAllocLength(len64: arr->getLength()); |
| 347 | CHECK_EXCEPTION(); |
| 348 | |
| 349 | Value *arguments = scope.alloc<Scope::Uninitialized>(nValues: len); |
| 350 | if (len) { |
| 351 | if (ArgumentsObject::isNonStrictArgumentsObject(m: arr) && !arr->cast<ArgumentsObject>()->fullyCreated()) { |
| 352 | QV4::ArgumentsObject *a = arr->cast<ArgumentsObject>(); |
| 353 | int l = qMin(a: len, b: a->d()->context->argc()); |
| 354 | memcpy(dest: arguments, src: a->d()->context->args(), n: l*sizeof(Value)); |
| 355 | for (int i = l; i < len; ++i) |
| 356 | arguments[i] = Value::undefinedValue(); |
| 357 | } else if (arr->arrayType() == Heap::ArrayData::Simple && !arr->protoHasArray()) { |
| 358 | auto sad = static_cast<Heap::SimpleArrayData *>(arr->arrayData()); |
| 359 | int alen = sad ? sad->values.size : 0; |
| 360 | if (alen > len) |
| 361 | alen = len; |
| 362 | for (int i = 0; i < alen; ++i) |
| 363 | arguments[i] = sad->data(index: i); |
| 364 | for (int i = alen; i < len; ++i) |
| 365 | arguments[i] = Value::undefinedValue(); |
| 366 | } else { |
| 367 | // need to init the arguments array, as the get() calls below can have side effects |
| 368 | memset(s: arguments, c: 0, n: len*sizeof(Value)); |
| 369 | for (int i = 0; i < len; ++i) |
| 370 | arguments[i] = arr->get(idx: i); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | return checkedResult(v4, result: f->call(thisObject, argv: arguments, argc: len)); |
| 375 | } |
| 376 | |
| 377 | ReturnedValue FunctionPrototype::method_call(const QV4::FunctionObject *b, const Value *thisObject, const Value *argv, int argc) |
| 378 | { |
| 379 | QV4::ExecutionEngine *v4 = b->engine(); |
| 380 | if (!thisObject->isFunctionObject()) |
| 381 | return v4->throwTypeError(); |
| 382 | |
| 383 | const FunctionObject *f = static_cast<const FunctionObject *>(thisObject); |
| 384 | |
| 385 | thisObject = argc ? argv : nullptr; |
| 386 | if (argc) { |
| 387 | ++argv; |
| 388 | --argc; |
| 389 | } |
| 390 | return checkedResult(v4, result: f->call(thisObject, argv, argc)); |
| 391 | } |
| 392 | |
| 393 | ReturnedValue FunctionPrototype::method_bind(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc) |
| 394 | { |
| 395 | QV4::Scope scope(b); |
| 396 | ScopedFunctionObject target(scope, thisObject); |
| 397 | if (!target || target->isBinding()) |
| 398 | return scope.engine->throwTypeError(); |
| 399 | |
| 400 | ScopedValue boundThis(scope, argc ? argv[0] : Value::undefinedValue()); |
| 401 | Scoped<MemberData> boundArgs(scope, (Heap::MemberData *)nullptr); |
| 402 | |
| 403 | int nArgs = (argc - 1 >= 0) ? argc - 1 : 0; |
| 404 | if (target->isBoundFunction()) { |
| 405 | BoundFunction *bound = static_cast<BoundFunction *>(target.getPointer()); |
| 406 | Scoped<MemberData> oldArgs(scope, bound->boundArgs()); |
| 407 | boundThis = bound->boundThis(); |
| 408 | int oldSize = !oldArgs ? 0 : oldArgs->size(); |
| 409 | if (oldSize + nArgs) { |
| 410 | boundArgs = MemberData::allocate(e: scope.engine, n: oldSize + nArgs); |
| 411 | boundArgs->d()->values.size = oldSize + nArgs; |
| 412 | for (uint i = 0; i < static_cast<uint>(oldSize); ++i) |
| 413 | boundArgs->set(e: scope.engine, index: i, v: oldArgs->data()[i]); |
| 414 | for (uint i = 0; i < static_cast<uint>(nArgs); ++i) |
| 415 | boundArgs->set(e: scope.engine, index: oldSize + i, v: argv[i + 1]); |
| 416 | } |
| 417 | target = bound->target(); |
| 418 | } else if (nArgs) { |
| 419 | boundArgs = MemberData::allocate(e: scope.engine, n: nArgs); |
| 420 | boundArgs->d()->values.size = nArgs; |
| 421 | for (uint i = 0, ei = static_cast<uint>(nArgs); i < ei; ++i) |
| 422 | boundArgs->set(e: scope.engine, index: i, v: argv[i + 1]); |
| 423 | } |
| 424 | |
| 425 | if (target->isConstructor()) { |
| 426 | return scope.engine->memoryManager->allocate<BoundConstructor>(args&: target, args&: boundThis, args&: boundArgs) |
| 427 | ->asReturnedValue(); |
| 428 | } |
| 429 | |
| 430 | return scope.engine->memoryManager->allocate<BoundFunction>(args&: target, args&: boundThis, args&: boundArgs) |
| 431 | ->asReturnedValue(); |
| 432 | } |
| 433 | |
| 434 | ReturnedValue FunctionPrototype::method_hasInstance(const FunctionObject *, const Value *thisObject, const Value *argv, int argc) |
| 435 | { |
| 436 | if (!argc) |
| 437 | return Encode(false); |
| 438 | const Object *o = thisObject->as<Object>(); |
| 439 | if (!o) |
| 440 | return Encode(false); |
| 441 | |
| 442 | return Object::virtualInstanceOf(typeObject: o, var: argv[0]); |
| 443 | } |
| 444 | |
| 445 | DEFINE_OBJECT_VTABLE(ScriptFunction); |
| 446 | |
| 447 | ReturnedValue ScriptFunction::virtualCallAsConstructor(const FunctionObject *fo, const Value *argv, int argc, const Value *newTarget) |
| 448 | { |
| 449 | ExecutionEngine *v4 = fo->engine(); |
| 450 | const ScriptFunction *f = static_cast<const ScriptFunction *>(fo); |
| 451 | Q_ASSERT(newTarget->isFunctionObject()); |
| 452 | const FunctionObject *nt = static_cast<const FunctionObject *>(newTarget); |
| 453 | |
| 454 | Scope scope(v4); |
| 455 | Scoped<InternalClass> ic(scope); |
| 456 | if (nt->d() == f->d()) { |
| 457 | ic = f->classForConstructor(); |
| 458 | } else { |
| 459 | ScopedObject o(scope, nt->protoProperty()); |
| 460 | ic = scope.engine->internalClasses(icType: EngineBase::Class_Object); |
| 461 | if (o) |
| 462 | ic = ic->changePrototype(proto: o->d()); |
| 463 | } |
| 464 | ScopedValue thisObject(scope, v4->memoryManager->allocObject<Object>(ic)); |
| 465 | |
| 466 | JSTypesStackFrame frame; |
| 467 | frame.init(v4Function: f->function(), argv, argc); |
| 468 | frame.setupJSFrame(stackSpace: v4->jsStackTop, function: *f, scope: f->scope(), |
| 469 | thisObject, |
| 470 | newTarget: newTarget ? *newTarget : Value::undefinedValue()); |
| 471 | |
| 472 | frame.push(engine: v4); |
| 473 | v4->jsStackTop += frame.requiredJSStackFrameSize(); |
| 474 | |
| 475 | ReturnedValue result = Moth::VME::exec(frame: &frame, engine: v4); |
| 476 | |
| 477 | frame.pop(engine: v4); |
| 478 | |
| 479 | if (Q_UNLIKELY(v4->hasException)) |
| 480 | return Encode::undefined(); |
| 481 | else if (!Value::fromReturnedValue(val: result).isObject()) |
| 482 | return thisObject->asReturnedValue(); |
| 483 | return result; |
| 484 | } |
| 485 | |
| 486 | DEFINE_OBJECT_VTABLE(ArrowFunction); |
| 487 | |
| 488 | void ArrowFunction::virtualCallWithMetaTypes(const FunctionObject *fo, QObject *thisObject, |
| 489 | void **a, const QMetaType *types, int argc) |
| 490 | { |
| 491 | const ArrowFunction *self = static_cast<const ArrowFunction *>(fo); |
| 492 | Function *function = self->function(); |
| 493 | if (function->kind != Function::AotCompiled) { |
| 494 | QV4::convertAndCall(engine: fo->engine(), thisObject, a, types, argc, |
| 495 | call: [fo](const Value *thisObject, const Value *argv, int argc) { |
| 496 | return ArrowFunction::virtualCall(f: fo, thisObject, argv, argc); |
| 497 | }); |
| 498 | return; |
| 499 | } |
| 500 | |
| 501 | QV4::Scope scope(fo->engine()); |
| 502 | QV4::Scoped<ExecutionContext> context(scope, self->scope()); |
| 503 | MetaTypesStackFrame frame; |
| 504 | frame.init(v4Function: function, thisObject, context, returnAndArgs: a, metaTypes: types, argc); |
| 505 | frame.push(engine: scope.engine); |
| 506 | Moth::VME::exec(frame: &frame, engine: scope.engine); |
| 507 | frame.pop(engine: scope.engine); |
| 508 | } |
| 509 | |
| 510 | static ReturnedValue qfoDoCall( |
| 511 | const QV4::JavaScriptFunctionObject *fo, const QV4::Value *thisObject, |
| 512 | const QV4::Value *argv, int argc) |
| 513 | { |
| 514 | ExecutionEngine *engine = fo->engine(); |
| 515 | JSTypesStackFrame frame; |
| 516 | frame.init(v4Function: fo->function(), argv, argc, callerCanHandleTailCall: true); |
| 517 | frame.setupJSFrame(stackSpace: engine->jsStackTop, function: *fo, scope: fo->scope(), |
| 518 | thisObject: thisObject ? *thisObject : Value::undefinedValue()); |
| 519 | |
| 520 | frame.push(engine); |
| 521 | engine->jsStackTop += frame.requiredJSStackFrameSize(); |
| 522 | |
| 523 | ReturnedValue result; |
| 524 | |
| 525 | do { |
| 526 | frame.setPendingTailCall(false); |
| 527 | result = Moth::VME::exec(frame: &frame, engine); |
| 528 | frame.setTailCalling(true); |
| 529 | } while (frame.pendingTailCall()); |
| 530 | |
| 531 | frame.pop(engine); |
| 532 | |
| 533 | return result; |
| 534 | } |
| 535 | |
| 536 | ReturnedValue ArrowFunction::virtualCall(const QV4::FunctionObject *fo, const Value *thisObject, |
| 537 | const QV4::Value *argv, int argc) |
| 538 | { |
| 539 | const ArrowFunction *self = static_cast<const ArrowFunction *>(fo); |
| 540 | Function *function = self->function(); |
| 541 | switch (function->kind) { |
| 542 | case Function::AotCompiled: |
| 543 | return QV4::convertAndCall( |
| 544 | engine: fo->engine(), aotFunction: &function->aotCompiledFunction, thisObject, argv, argc, |
| 545 | call: [fo](QObject *thisObject, void **a, const QMetaType *types, int argc) { |
| 546 | ArrowFunction::virtualCallWithMetaTypes(fo, thisObject, a, types, argc); |
| 547 | }); |
| 548 | case Function::JsTyped: |
| 549 | return QV4::coerceAndCall( |
| 550 | engine: fo->engine(), typedFunction: &function->jsTypedFunction, compiledFunction: function->compiledFunction, argv, argc, |
| 551 | call: [self, thisObject](const Value *argv, int argc) { |
| 552 | return qfoDoCall(fo: self, thisObject, argv, argc); |
| 553 | }); |
| 554 | default: |
| 555 | break; |
| 556 | } |
| 557 | |
| 558 | return qfoDoCall(fo: self, thisObject, argv, argc); |
| 559 | } |
| 560 | |
| 561 | void Heap::ArrowFunction::init(QV4::ExecutionContext *scope, Function *function, QV4::String *n) |
| 562 | { |
| 563 | Q_ASSERT(function); |
| 564 | JavaScriptFunctionObject::init(scope, function, n); |
| 565 | |
| 566 | Scope s(scope); |
| 567 | Q_ASSERT(internalClass && internalClass->verifyIndex(s.engine->id_length()->propertyKey(), Index_Length)); |
| 568 | setProperty(e: s.engine, index: Index_Length, v: Value::fromInt32(i: int(function->compiledFunction->length))); |
| 569 | } |
| 570 | |
| 571 | void Heap::ScriptFunction::init(QV4::ExecutionContext *scope, Function *function) |
| 572 | { |
| 573 | ArrowFunction::init(scope, function); |
| 574 | Q_ASSERT(!function->isArrowFunction()); |
| 575 | |
| 576 | Scope s(scope); |
| 577 | ScopedFunctionObject f(s, this); |
| 578 | f->createDefaultPrototypeProperty(protoConstructorSlot: Heap::FunctionObject::Index_ProtoConstructor); |
| 579 | } |
| 580 | |
| 581 | void Heap::DefaultClassConstructorFunction::init(QV4::ExecutionContext *scope) |
| 582 | { |
| 583 | Scope s(scope->engine()); |
| 584 | FunctionObject::init(engine: s.engine, name: nullptr); |
| 585 | this->scope.set(e: s.engine, newVal: scope->d()); |
| 586 | } |
| 587 | |
| 588 | Heap::InternalClass *ScriptFunction::classForConstructor() const |
| 589 | { |
| 590 | Scope scope(engine()); |
| 591 | ScopedValue o(scope, protoProperty()); |
| 592 | if (d()->cachedClassForConstructor && d()->cachedClassForConstructor->prototype == o->heapObject()) |
| 593 | return d()->cachedClassForConstructor; |
| 594 | |
| 595 | Scoped<InternalClass> ic(scope, engine()->internalClasses(icType: EngineBase::Class_Object)); |
| 596 | ScopedObject p(scope, o); |
| 597 | if (p) |
| 598 | ic = ic->changePrototype(proto: p->d()); |
| 599 | d()->cachedClassForConstructor.set(e: scope.engine, newVal: ic->d()); |
| 600 | |
| 601 | return ic->d(); |
| 602 | } |
| 603 | |
| 604 | DEFINE_OBJECT_VTABLE(ConstructorFunction); |
| 605 | |
| 606 | ReturnedValue ConstructorFunction::virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *newTarget) |
| 607 | { |
| 608 | const ConstructorFunction *c = static_cast<const ConstructorFunction *>(f); |
| 609 | if (!c->d()->isDerivedConstructor) |
| 610 | return ScriptFunction::virtualCallAsConstructor(fo: f, argv, argc, newTarget); |
| 611 | |
| 612 | ExecutionEngine *v4 = f->engine(); |
| 613 | |
| 614 | JSTypesStackFrame frame; |
| 615 | frame.init(v4Function: c->function(), argv, argc); |
| 616 | frame.setupJSFrame(stackSpace: v4->jsStackTop, function: *f, scope: c->scope(), |
| 617 | thisObject: Value::emptyValue(), |
| 618 | newTarget: newTarget ? *newTarget : Value::undefinedValue()); |
| 619 | |
| 620 | frame.push(engine: v4); |
| 621 | v4->jsStackTop += frame.requiredJSStackFrameSize(); |
| 622 | |
| 623 | ReturnedValue result = Moth::VME::exec(frame: &frame, engine: v4); |
| 624 | ReturnedValue thisObject = frame.jsFrame->thisObject.asReturnedValue(); |
| 625 | |
| 626 | frame.pop(engine: v4); |
| 627 | |
| 628 | if (Q_UNLIKELY(v4->hasException)) |
| 629 | return Encode::undefined(); |
| 630 | else if (Value::fromReturnedValue(val: result).isObject()) |
| 631 | return result; |
| 632 | else if (!Value::fromReturnedValue(val: result).isUndefined()) |
| 633 | return v4->throwTypeError(); |
| 634 | else if (Value::fromReturnedValue(val: thisObject).isEmpty()) { |
| 635 | Scope scope(v4); |
| 636 | ScopedString s(scope, v4->newString(QStringLiteral("this" ))); |
| 637 | return v4->throwReferenceError(value: s); |
| 638 | } |
| 639 | return thisObject; |
| 640 | } |
| 641 | |
| 642 | ReturnedValue ConstructorFunction::virtualCall(const FunctionObject *f, const Value *, const Value *, int) |
| 643 | { |
| 644 | return f->engine()->throwTypeError(QStringLiteral("Cannot call a class constructor without |new|" )); |
| 645 | } |
| 646 | |
| 647 | DEFINE_OBJECT_VTABLE(MemberFunction); |
| 648 | |
| 649 | DEFINE_OBJECT_VTABLE(DefaultClassConstructorFunction); |
| 650 | |
| 651 | ReturnedValue DefaultClassConstructorFunction::virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *newTarget) |
| 652 | { |
| 653 | const DefaultClassConstructorFunction *c = static_cast<const DefaultClassConstructorFunction *>(f); |
| 654 | ExecutionEngine *v4 = f->engine(); |
| 655 | |
| 656 | Scope scope(v4); |
| 657 | |
| 658 | if (!c->d()->isDerivedConstructor) { |
| 659 | ScopedObject proto(scope, static_cast<const Object *>(newTarget)->get(name: scope.engine->id_prototype())); |
| 660 | ScopedObject c(scope, scope.engine->newObject()); |
| 661 | c->setPrototypeUnchecked(proto); |
| 662 | return c->asReturnedValue(); |
| 663 | } |
| 664 | |
| 665 | ScopedFunctionObject super(scope, f->getPrototypeOf()); |
| 666 | Q_ASSERT(super->isFunctionObject()); |
| 667 | |
| 668 | JSTypesStackFrame frame; |
| 669 | frame.init(v4Function: nullptr, argv, argc); |
| 670 | frame.setupJSFrame(stackSpace: v4->jsStackTop, function: *f, scope: c->scope(), |
| 671 | thisObject: Value::undefinedValue(), |
| 672 | newTarget: newTarget ? *newTarget : Value::undefinedValue(), nFormals: argc, nRegisters: argc); |
| 673 | |
| 674 | frame.push(engine: v4); |
| 675 | v4->jsStackTop += frame.requiredJSStackFrameSize(nRegisters: argc); |
| 676 | |
| 677 | // Do a super call |
| 678 | ReturnedValue result = super->callAsConstructor(argv, argc, newTarget); |
| 679 | ReturnedValue thisObject = frame.jsFrame->thisObject.asReturnedValue(); |
| 680 | |
| 681 | frame.pop(engine: v4); |
| 682 | |
| 683 | if (Q_UNLIKELY(v4->hasException)) |
| 684 | return Encode::undefined(); |
| 685 | else if (Value::fromReturnedValue(val: result).isObject()) |
| 686 | return result; |
| 687 | else if (!Value::fromReturnedValue(val: result).isUndefined()) |
| 688 | return v4->throwTypeError(); |
| 689 | else if (Value::fromReturnedValue(val: thisObject).isEmpty()) { |
| 690 | Scope scope(v4); |
| 691 | ScopedString s(scope, v4->newString(QStringLiteral("this" ))); |
| 692 | return v4->throwReferenceError(value: s); |
| 693 | } |
| 694 | |
| 695 | return thisObject; |
| 696 | } |
| 697 | |
| 698 | ReturnedValue DefaultClassConstructorFunction::virtualCall(const FunctionObject *f, const Value *, const Value *, int) |
| 699 | { |
| 700 | return f->engine()->throwTypeError(QStringLiteral("Cannot call a class constructor without |new|" )); |
| 701 | } |
| 702 | |
| 703 | DEFINE_OBJECT_VTABLE(IndexedBuiltinFunction); |
| 704 | |
| 705 | DEFINE_OBJECT_VTABLE(BoundFunction); |
| 706 | |
| 707 | void Heap::BoundFunction::init( |
| 708 | QV4::FunctionObject *target, const Value &boundThis, QV4::MemberData *boundArgs) |
| 709 | { |
| 710 | ExecutionEngine *engine = target->engine(); |
| 711 | Scope s(engine); |
| 712 | ScopedString name(s, engine->newString(QStringLiteral("__bound function__" ))); |
| 713 | if (auto *js = target->as<QV4::JavaScriptFunctionObject>()) { |
| 714 | ScopedContext ctx(s, js->scope()); |
| 715 | JavaScriptFunctionObject::init(scope: ctx, function: js->function(), n: name); |
| 716 | } else { |
| 717 | Q_ASSERT(name); |
| 718 | JavaScriptFunctionObject::init(scope: engine->rootContext(), function: nullptr, n: name); |
| 719 | } |
| 720 | |
| 721 | this->target.set(e: s.engine, newVal: target->d()); |
| 722 | this->boundArgs.set(e: s.engine, newVal: boundArgs ? boundArgs->d() : nullptr); |
| 723 | this->boundThis.set(e: s.engine, newVal: boundThis); |
| 724 | |
| 725 | ScopedObject f(s, this); |
| 726 | |
| 727 | ScopedValue l(s, target->get(name: engine->id_length())); |
| 728 | int len = l->toUInt32(); |
| 729 | if (boundArgs) |
| 730 | len -= boundArgs->size(); |
| 731 | if (len < 0) |
| 732 | len = 0; |
| 733 | f->defineReadonlyConfigurableProperty(name: engine->id_length(), value: Value::fromInt32(i: len)); |
| 734 | |
| 735 | ScopedProperty pd(s); |
| 736 | pd->value = engine->thrower(); |
| 737 | pd->set = engine->thrower(); |
| 738 | f->insertMember(s: engine->id_arguments(), p: pd, attributes: Attr_Accessor|Attr_NotConfigurable|Attr_NotEnumerable); |
| 739 | f->insertMember(s: engine->id_caller(), p: pd, attributes: Attr_Accessor|Attr_NotConfigurable|Attr_NotEnumerable); |
| 740 | } |
| 741 | |
| 742 | ReturnedValue BoundFunction::virtualCall(const FunctionObject *fo, const Value *, const Value *argv, int argc) |
| 743 | { |
| 744 | QV4::ExecutionEngine *v4 = fo->engine(); |
| 745 | if (v4->hasException) |
| 746 | return Encode::undefined(); |
| 747 | |
| 748 | const BoundFunction *f = static_cast<const BoundFunction *>(fo); |
| 749 | Scope scope(v4); |
| 750 | Scoped<MemberData> boundArgs(scope, f->boundArgs()); |
| 751 | ScopedFunctionObject target(scope, f->target()); |
| 752 | JSCallArguments jsCallData(scope, (boundArgs ? boundArgs->size() : 0) + argc); |
| 753 | *jsCallData.thisObject = f->boundThis(); |
| 754 | Value *argp = jsCallData.args; |
| 755 | if (boundArgs) { |
| 756 | memcpy(dest: argp, src: boundArgs->data(), n: boundArgs->size()*sizeof(Value)); |
| 757 | argp += boundArgs->size(); |
| 758 | } |
| 759 | memcpy(dest: argp, src: argv, n: argc*sizeof(Value)); |
| 760 | return checkedResult(v4, result: target->call(data: jsCallData)); |
| 761 | } |
| 762 | |
| 763 | DEFINE_OBJECT_VTABLE(BoundConstructor); |
| 764 | |
| 765 | ReturnedValue BoundConstructor::virtualCallAsConstructor( |
| 766 | const FunctionObject *fo, const Value *argv, int argc, const Value *) |
| 767 | { |
| 768 | const BoundFunction *f = static_cast<const BoundFunction *>(fo); |
| 769 | Scope scope(f->engine()); |
| 770 | |
| 771 | if (scope.hasException()) |
| 772 | return Encode::undefined(); |
| 773 | |
| 774 | Scoped<MemberData> boundArgs(scope, f->boundArgs()); |
| 775 | ScopedFunctionObject target(scope, f->target()); |
| 776 | JSCallArguments jsCallData(scope, (boundArgs ? boundArgs->size() : 0) + argc); |
| 777 | Value *argp = jsCallData.args; |
| 778 | if (boundArgs) { |
| 779 | memcpy(dest: argp, src: boundArgs->data(), n: boundArgs->size()*sizeof(Value)); |
| 780 | argp += boundArgs->size(); |
| 781 | } |
| 782 | memcpy(dest: argp, src: argv, n: argc*sizeof(Value)); |
| 783 | return target->callAsConstructor(data: jsCallData); |
| 784 | } |
| 785 | |