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