aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/jsruntime
diff options
context:
space:
mode:
Diffstat (limited to 'src/qml/jsruntime')
-rw-r--r--src/qml/jsruntime/qv4argumentsobject.cpp16
-rw-r--r--src/qml/jsruntime/qv4arraybuffer.cpp8
-rw-r--r--src/qml/jsruntime/qv4arraybuffer_p.h12
-rw-r--r--src/qml/jsruntime/qv4arraydata.cpp37
-rw-r--r--src/qml/jsruntime/qv4arrayobject.cpp74
-rw-r--r--src/qml/jsruntime/qv4arrayobject_p.h2
-rw-r--r--src/qml/jsruntime/qv4booleanobject.cpp4
-rw-r--r--src/qml/jsruntime/qv4booleanobject_p.h2
-rw-r--r--src/qml/jsruntime/qv4compilationunitmapper.cpp29
-rw-r--r--src/qml/jsruntime/qv4compilationunitmapper_p.h3
-rw-r--r--src/qml/jsruntime/qv4compilationunitmapper_unix.cpp23
-rw-r--r--src/qml/jsruntime/qv4compilationunitmapper_win.cpp29
-rw-r--r--src/qml/jsruntime/qv4context.cpp22
-rw-r--r--src/qml/jsruntime/qv4context_p.h2
-rw-r--r--src/qml/jsruntime/qv4dataview.cpp4
-rw-r--r--src/qml/jsruntime/qv4dataview_p.h2
-rw-r--r--src/qml/jsruntime/qv4dateobject.cpp61
-rw-r--r--src/qml/jsruntime/qv4dateobject_p.h10
-rw-r--r--src/qml/jsruntime/qv4engine.cpp743
-rw-r--r--src/qml/jsruntime/qv4engine_p.h68
-rw-r--r--src/qml/jsruntime/qv4enginebase_p.h3
-rw-r--r--src/qml/jsruntime/qv4errorobject.cpp36
-rw-r--r--src/qml/jsruntime/qv4errorobject_p.h16
-rw-r--r--src/qml/jsruntime/qv4estable.cpp23
-rw-r--r--src/qml/jsruntime/qv4estable_p.h11
-rw-r--r--src/qml/jsruntime/qv4executableallocator_p.h2
-rw-r--r--src/qml/jsruntime/qv4executablecompilationunit.cpp466
-rw-r--r--src/qml/jsruntime/qv4executablecompilationunit_p.h315
-rw-r--r--src/qml/jsruntime/qv4function.cpp165
-rw-r--r--src/qml/jsruntime/qv4function_p.h48
-rw-r--r--src/qml/jsruntime/qv4functionobject.cpp256
-rw-r--r--src/qml/jsruntime/qv4functionobject_p.h220
-rw-r--r--src/qml/jsruntime/qv4generatorobject.cpp8
-rw-r--r--src/qml/jsruntime/qv4generatorobject_p.h2
-rw-r--r--src/qml/jsruntime/qv4global_p.h4
-rw-r--r--src/qml/jsruntime/qv4globalobject.cpp37
-rw-r--r--src/qml/jsruntime/qv4globalobject_p.h2
-rw-r--r--src/qml/jsruntime/qv4identifierhash.cpp4
-rw-r--r--src/qml/jsruntime/qv4identifierhash_p.h2
-rw-r--r--src/qml/jsruntime/qv4identifiertable.cpp27
-rw-r--r--src/qml/jsruntime/qv4identifiertable_p.h6
-rw-r--r--src/qml/jsruntime/qv4include.cpp20
-rw-r--r--src/qml/jsruntime/qv4include_p.h1
-rw-r--r--src/qml/jsruntime/qv4internalclass.cpp43
-rw-r--r--src/qml/jsruntime/qv4internalclass_p.h27
-rw-r--r--src/qml/jsruntime/qv4jscall.cpp23
-rw-r--r--src/qml/jsruntime/qv4jscall_p.h477
-rw-r--r--src/qml/jsruntime/qv4jsonobject.cpp27
-rw-r--r--src/qml/jsruntime/qv4jsonobject_p.h7
-rw-r--r--src/qml/jsruntime/qv4lookup.cpp89
-rw-r--r--src/qml/jsruntime/qv4lookup_p.h62
-rw-r--r--src/qml/jsruntime/qv4managed_p.h4
-rw-r--r--src/qml/jsruntime/qv4mapobject.cpp29
-rw-r--r--src/qml/jsruntime/qv4mapobject_p.h8
-rw-r--r--src/qml/jsruntime/qv4math_p.h6
-rw-r--r--src/qml/jsruntime/qv4memberdata.cpp2
-rw-r--r--src/qml/jsruntime/qv4module.cpp24
-rw-r--r--src/qml/jsruntime/qv4numberobject.cpp4
-rw-r--r--src/qml/jsruntime/qv4numberobject_p.h2
-rw-r--r--src/qml/jsruntime/qv4object.cpp43
-rw-r--r--src/qml/jsruntime/qv4object_p.h2
-rw-r--r--src/qml/jsruntime/qv4objectproto.cpp4
-rw-r--r--src/qml/jsruntime/qv4objectproto_p.h4
-rw-r--r--src/qml/jsruntime/qv4persistent.cpp126
-rw-r--r--src/qml/jsruntime/qv4persistent_p.h24
-rw-r--r--src/qml/jsruntime/qv4profiling.cpp4
-rw-r--r--src/qml/jsruntime/qv4profiling_p.h4
-rw-r--r--src/qml/jsruntime/qv4promiseobject.cpp28
-rw-r--r--src/qml/jsruntime/qv4promiseobject_p.h4
-rw-r--r--src/qml/jsruntime/qv4property_p.h2
-rw-r--r--src/qml/jsruntime/qv4propertykey.cpp7
-rw-r--r--src/qml/jsruntime/qv4propertykey_p.h18
-rw-r--r--src/qml/jsruntime/qv4proxy.cpp39
-rw-r--r--src/qml/jsruntime/qv4proxy_p.h17
-rw-r--r--src/qml/jsruntime/qv4qmetaobjectwrapper.cpp111
-rw-r--r--src/qml/jsruntime/qv4qmetaobjectwrapper_p.h120
-rw-r--r--src/qml/jsruntime/qv4qmlcontext.cpp54
-rw-r--r--src/qml/jsruntime/qv4qobjectwrapper.cpp1066
-rw-r--r--src/qml/jsruntime/qv4qobjectwrapper_p.h112
-rw-r--r--src/qml/jsruntime/qv4referenceobject_p.h1
-rw-r--r--src/qml/jsruntime/qv4regexp.cpp93
-rw-r--r--src/qml/jsruntime/qv4regexp_p.h2
-rw-r--r--src/qml/jsruntime/qv4regexpobject.cpp4
-rw-r--r--src/qml/jsruntime/qv4regexpobject_p.h40
-rw-r--r--src/qml/jsruntime/qv4resolvedtypereference.cpp26
-rw-r--r--src/qml/jsruntime/qv4resolvedtypereference_p.h12
-rw-r--r--src/qml/jsruntime/qv4runtime.cpp187
-rw-r--r--src/qml/jsruntime/qv4runtime_p.h2
-rw-r--r--src/qml/jsruntime/qv4runtimeapi_p.h198
-rw-r--r--src/qml/jsruntime/qv4script.cpp20
-rw-r--r--src/qml/jsruntime/qv4script_p.h8
-rw-r--r--src/qml/jsruntime/qv4sequenceobject.cpp336
-rw-r--r--src/qml/jsruntime/qv4sequenceobject_p.h30
-rw-r--r--src/qml/jsruntime/qv4setobject.cpp27
-rw-r--r--src/qml/jsruntime/qv4setobject_p.h8
-rw-r--r--src/qml/jsruntime/qv4sparsearray_p.h44
-rw-r--r--src/qml/jsruntime/qv4stackframe.cpp11
-rw-r--r--src/qml/jsruntime/qv4stackframe_p.h41
-rw-r--r--src/qml/jsruntime/qv4string.cpp31
-rw-r--r--src/qml/jsruntime/qv4string_p.h30
-rw-r--r--src/qml/jsruntime/qv4stringobject.cpp9
-rw-r--r--src/qml/jsruntime/qv4stringobject_p.h2
-rw-r--r--src/qml/jsruntime/qv4symbol.cpp6
-rw-r--r--src/qml/jsruntime/qv4symbol_p.h2
-rw-r--r--src/qml/jsruntime/qv4typedarray.cpp27
-rw-r--r--src/qml/jsruntime/qv4typedarray_p.h6
-rw-r--r--src/qml/jsruntime/qv4urlobject.cpp60
-rw-r--r--src/qml/jsruntime/qv4urlobject_p.h17
-rw-r--r--src/qml/jsruntime/qv4value_p.h20
-rw-r--r--src/qml/jsruntime/qv4variantobject.cpp3
-rw-r--r--src/qml/jsruntime/qv4variantobject_p.h2
-rw-r--r--src/qml/jsruntime/qv4vme_moth.cpp147
-rw-r--r--src/qml/jsruntime/qv4vtable_p.h79
113 files changed, 4189 insertions, 2790 deletions
diff --git a/src/qml/jsruntime/qv4argumentsobject.cpp b/src/qml/jsruntime/qv4argumentsobject.cpp
index 52731ca221..74b79cb400 100644
--- a/src/qml/jsruntime/qv4argumentsobject.cpp
+++ b/src/qml/jsruntime/qv4argumentsobject.cpp
@@ -1,14 +1,16 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
-#include <qv4argumentsobject_p.h>
-#include <qv4arrayobject_p.h>
-#include <qv4scopedvalue_p.h>
-#include <qv4string_p.h>
-#include <qv4function_p.h>
-#include <qv4jscall_p.h>
-#include <qv4symbol_p.h>
+
+#include "qv4argumentsobject_p.h"
#include <private/qv4alloca_p.h>
+#include <private/qv4arrayobject_p.h>
+#include <private/qv4function_p.h>
+#include <private/qv4jscall_p.h>
+#include <private/qv4scopedvalue_p.h>
+#include <private/qv4stackframe_p.h>
+#include <private/qv4string_p.h>
+#include <private/qv4symbol_p.h>
using namespace QV4;
diff --git a/src/qml/jsruntime/qv4arraybuffer.cpp b/src/qml/jsruntime/qv4arraybuffer.cpp
index f2be552cf8..a49bd32d66 100644
--- a/src/qml/jsruntime/qv4arraybuffer.cpp
+++ b/src/qml/jsruntime/qv4arraybuffer.cpp
@@ -12,14 +12,14 @@ DEFINE_OBJECT_VTABLE(ArrayBufferCtor);
DEFINE_OBJECT_VTABLE(SharedArrayBuffer);
DEFINE_OBJECT_VTABLE(ArrayBuffer);
-void Heap::SharedArrayBufferCtor::init(QV4::ExecutionContext *scope)
+void Heap::SharedArrayBufferCtor::init(QV4::ExecutionEngine *engine)
{
- Heap::FunctionObject::init(scope, QStringLiteral("SharedArrayBuffer"));
+ Heap::FunctionObject::init(engine, QStringLiteral("SharedArrayBuffer"));
}
-void Heap::ArrayBufferCtor::init(QV4::ExecutionContext *scope)
+void Heap::ArrayBufferCtor::init(QV4::ExecutionEngine *engine)
{
- Heap::FunctionObject::init(scope, QStringLiteral("ArrayBuffer"));
+ Heap::FunctionObject::init(engine, QStringLiteral("ArrayBuffer"));
}
ReturnedValue SharedArrayBufferCtor::virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *newTarget)
diff --git a/src/qml/jsruntime/qv4arraybuffer_p.h b/src/qml/jsruntime/qv4arraybuffer_p.h
index b955618cbe..af1195a947 100644
--- a/src/qml/jsruntime/qv4arraybuffer_p.h
+++ b/src/qml/jsruntime/qv4arraybuffer_p.h
@@ -25,14 +25,14 @@ namespace QV4 {
namespace Heap {
struct SharedArrayBufferCtor : FunctionObject {
- void init(QV4::ExecutionContext *scope);
+ void init(QV4::ExecutionEngine *engine);
};
struct ArrayBufferCtor : SharedArrayBufferCtor {
- void init(QV4::ExecutionContext *scope);
+ void init(QV4::ExecutionEngine *engine);
};
-struct Q_QML_PRIVATE_EXPORT SharedArrayBuffer : Object {
+struct Q_QML_EXPORT SharedArrayBuffer : Object {
void init(size_t length);
void init(const QByteArray& array);
void destroy();
@@ -68,7 +68,7 @@ private:
bool isShared;
};
-struct Q_QML_PRIVATE_EXPORT ArrayBuffer : SharedArrayBuffer {
+struct Q_QML_EXPORT ArrayBuffer : SharedArrayBuffer {
void init(size_t length) {
SharedArrayBuffer::init(length);
setSharedArrayBuffer(false);
@@ -98,7 +98,7 @@ struct ArrayBufferCtor : SharedArrayBufferCtor
static ReturnedValue method_isView(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
};
-struct Q_QML_PRIVATE_EXPORT SharedArrayBuffer : Object
+struct Q_QML_EXPORT SharedArrayBuffer : Object
{
V4_OBJECT2(SharedArrayBuffer, Object)
V4_NEEDS_DESTROY
@@ -115,7 +115,7 @@ struct Q_QML_PRIVATE_EXPORT SharedArrayBuffer : Object
bool isSharedArrayBuffer() const { return d()->isSharedArrayBuffer(); }
};
-struct Q_QML_PRIVATE_EXPORT ArrayBuffer : SharedArrayBuffer
+struct Q_QML_EXPORT ArrayBuffer : SharedArrayBuffer
{
V4_OBJECT2(ArrayBuffer, SharedArrayBuffer)
V4_NEEDS_DESTROY
diff --git a/src/qml/jsruntime/qv4arraydata.cpp b/src/qml/jsruntime/qv4arraydata.cpp
index 6e01c50048..724f6fbfa3 100644
--- a/src/qml/jsruntime/qv4arraydata.cpp
+++ b/src/qml/jsruntime/qv4arraydata.cpp
@@ -630,11 +630,6 @@ void ArrayData::sort(ExecutionEngine *engine, Object *thisObject, const Value &c
if (!arrayData || !arrayData->length())
return;
- if (!comparefn.isUndefined() && !comparefn.isFunctionObject()) {
- engine->throwTypeError();
- return;
- }
-
// The spec says the sorting goes through a series of get,put and delete operations.
// this implies that the attributes don't get sorted around.
@@ -711,8 +706,36 @@ void ArrayData::sort(ExecutionEngine *engine, Object *thisObject, const Value &c
ArrayElementLessThan lessThan(engine, comparefn);
- Value *begin = thisObject->arrayData()->values.values;
- sortHelper(begin, begin + len, lessThan);
+ const auto thisArrayData = thisObject->arrayData();
+ uint startIndex = thisArrayData->mappedIndex(0);
+ uint endIndex = thisArrayData->mappedIndex(len - 1) + 1;
+ if (startIndex < endIndex) {
+ // Values are contiguous. Sort right away.
+ sortHelper(
+ thisArrayData->values.values + startIndex,
+ thisArrayData->values.values + endIndex,
+ lessThan);
+ } else {
+ // Values wrap around the end of the allocation. Close the gap to form a contiguous array.
+ // We're going to sort anyway. So we don't need to care about order.
+
+ // ArrayElementLessThan sorts empty and undefined to the end of the array anyway, but we
+ // probably shouldn't rely on the unused slots to be actually undefined or empty.
+
+ const uint gap = startIndex - endIndex;
+ const uint allocEnd = thisArrayData->values.alloc - 1;
+ for (uint i = 0; i < gap; ++i) {
+ const uint from = allocEnd - i;
+ const uint to = endIndex + i;
+ if (from < startIndex)
+ break;
+
+ std::swap(thisArrayData->values.values[from], thisArrayData->values.values[to]);
+ }
+
+ thisArrayData->offset = 0;
+ sortHelper(thisArrayData->values.values, thisArrayData->values.values + len, lessThan);
+ }
#ifdef CHECK_SPARSE_ARRAYS
thisObject->initSparseArray();
diff --git a/src/qml/jsruntime/qv4arrayobject.cpp b/src/qml/jsruntime/qv4arrayobject.cpp
index 05e73d0295..40a7123232 100644
--- a/src/qml/jsruntime/qv4arrayobject.cpp
+++ b/src/qml/jsruntime/qv4arrayobject.cpp
@@ -14,9 +14,9 @@ using namespace QV4;
DEFINE_OBJECT_VTABLE(ArrayCtor);
-void Heap::ArrayCtor::init(QV4::ExecutionContext *scope)
+void Heap::ArrayCtor::init(QV4::ExecutionEngine *engine)
{
- Heap::FunctionObject::init(scope, QStringLiteral("Array"));
+ Heap::FunctionObject::init(engine, QStringLiteral("Array"));
}
ReturnedValue ArrayCtor::virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *newTarget)
@@ -194,9 +194,8 @@ ReturnedValue ArrayPrototype::method_from(const FunctionObject *builtin, const V
// sets them into the created array.
forever {
if (k > (static_cast<qint64>(1) << 53) - 1) {
- ScopedValue falsey(scope, Encode(false));
ScopedValue error(scope, scope.engine->throwTypeError());
- return Runtime::IteratorClose::call(scope.engine, iterator, falsey);
+ return Runtime::IteratorClose::call(scope.engine, iterator);
}
// Retrieve the next value. If the iteration ends, we're done here.
@@ -218,7 +217,7 @@ ReturnedValue ArrayPrototype::method_from(const FunctionObject *builtin, const V
mapArguments[1] = Value::fromDouble(k);
mappedValue = mapfn->call(thisArg, mapArguments, 2);
if (scope.hasException())
- return Runtime::IteratorClose::call(scope.engine, iterator, Value::fromBoolean(false));
+ return Runtime::IteratorClose::call(scope.engine, iterator);
} else {
mappedValue = *nextValue;
}
@@ -230,10 +229,8 @@ ReturnedValue ArrayPrototype::method_from(const FunctionObject *builtin, const V
scope.engine->throwTypeError(QString::fromLatin1("Cannot redefine property: %1").arg(k));
}
- if (scope.hasException()) {
- ScopedValue falsey(scope, Encode(false));
- return Runtime::IteratorClose::call(scope.engine, iterator, falsey);
- }
+ if (scope.hasException())
+ return Runtime::IteratorClose::call(scope.engine, iterator);
k++;
}
@@ -851,15 +848,69 @@ ReturnedValue ArrayPrototype::method_slice(const FunctionObject *b, const Value
ReturnedValue ArrayPrototype::method_sort(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc)
{
+ // Based on https://tc39.es/ecma262/#sec-array.prototype.sort
+
Scope scope(b);
+
+ ScopedValue comparefn(scope, argc ? argv[0] : Value::undefinedValue());
+
+ // 1. If comparefn is not undefined and IsCallable(comparefn) is false, throw a TypeError exception.
+ if (!comparefn->isUndefined() && !comparefn->isFunctionObject())
+ return scope.engine->throwTypeError(QStringLiteral("The provided comparison function is not callable."));
+
+ // 2. Let obj be ? ToObject(this value).
ScopedObject instance(scope, thisObject->toObject(scope.engine));
if (!instance)
RETURN_UNDEFINED();
+ // 3. Let len be ? LengthOfArrayLike(obj).
uint len = instance->getLength();
- ScopedValue comparefn(scope, argc ? argv[0] : Value::undefinedValue());
- ArrayData::sort(scope.engine, instance, comparefn, len);
+ if (instance->arrayData() && instance->arrayData()->length()) {
+ ArrayData::sort(scope.engine, instance, comparefn, len);
+ } else {
+ // Generic implementation that does not require a populated
+ // ArrayData, this is used, for example, by `Sequences` which
+ // store their data in a different way.
+
+ // 5. Let sortedList be ? SortIndexedProperties(obj, len, SortCompare, skip-holes)
+ Value* sorted = scope.alloc(scope.engine->safeForAllocLength(len));
+ CHECK_EXCEPTION();
+
+ uint written = 0;
+ for (uint index = 0; index < len; ++index) {
+ bool hasProperty = false;
+ auto element = instance->get(index, &hasProperty);
+
+ if (hasProperty) {
+ sorted[written] = element;
+ ++written;
+ }
+ }
+
+ std::stable_sort(sorted, sorted + written, ArrayElementLessThan(scope.engine, comparefn));
+
+ // [...]
+ // 8. Repeat, while j < itemCount,
+ // a. Perform ? Set(obj, ! ToString(𝔽(j)), sortedList[j], true).
+ // [...]
+ for (uint index = 0; index < written; ++index) {
+ instance->setIndexed(index, sorted[index], QV4::Object::DoThrowOnRejection);
+ CHECK_EXCEPTION();
+ }
+
+ // [...]
+ // 10. Repeat, while j < len,
+ // a. Perform ? DeletePropertyOrThrow(obj, ! ToString(𝔽(j))).
+ // [...]
+ while (written < len) {
+ if (!instance->deleteProperty(PropertyKey::fromArrayIndex(written)))
+ return scope.engine->throwTypeError();
+ ++written;
+ }
+ }
+
+ // 11. Return obj
return thisObject->asReturnedValue();
}
@@ -1493,4 +1544,3 @@ ReturnedValue ArrayPrototype::method_get_species(const FunctionObject *, const V
{
return thisObject->asReturnedValue();
}
-
diff --git a/src/qml/jsruntime/qv4arrayobject_p.h b/src/qml/jsruntime/qv4arrayobject_p.h
index b07e27b24f..a68068937f 100644
--- a/src/qml/jsruntime/qv4arrayobject_p.h
+++ b/src/qml/jsruntime/qv4arrayobject_p.h
@@ -50,7 +50,7 @@ namespace QV4 {
namespace Heap {
struct ArrayCtor : FunctionObject {
- void init(QV4::ExecutionContext *scope);
+ void init(QV4::ExecutionEngine *engine);
};
}
diff --git a/src/qml/jsruntime/qv4booleanobject.cpp b/src/qml/jsruntime/qv4booleanobject.cpp
index 3110ec7992..5c1d50e753 100644
--- a/src/qml/jsruntime/qv4booleanobject.cpp
+++ b/src/qml/jsruntime/qv4booleanobject.cpp
@@ -8,9 +8,9 @@ using namespace QV4;
DEFINE_OBJECT_VTABLE(BooleanCtor);
DEFINE_OBJECT_VTABLE(BooleanObject);
-void Heap::BooleanCtor::init(QV4::ExecutionContext *scope)
+void Heap::BooleanCtor::init(QV4::ExecutionEngine *engine)
{
- Heap::FunctionObject::init(scope, QStringLiteral("Boolean"));
+ Heap::FunctionObject::init(engine, QStringLiteral("Boolean"));
}
ReturnedValue BooleanCtor::virtualCallAsConstructor(const FunctionObject *that, const Value *argv, int argc, const Value *newTarget)
diff --git a/src/qml/jsruntime/qv4booleanobject_p.h b/src/qml/jsruntime/qv4booleanobject_p.h
index e009b0413a..1b2d3914ac 100644
--- a/src/qml/jsruntime/qv4booleanobject_p.h
+++ b/src/qml/jsruntime/qv4booleanobject_p.h
@@ -25,7 +25,7 @@ namespace QV4 {
namespace Heap {
struct BooleanCtor : FunctionObject {
- void init(QV4::ExecutionContext *scope);
+ void init(ExecutionEngine *engine);
};
}
diff --git a/src/qml/jsruntime/qv4compilationunitmapper.cpp b/src/qml/jsruntime/qv4compilationunitmapper.cpp
index 0479b8b8f1..e9915c7d26 100644
--- a/src/qml/jsruntime/qv4compilationunitmapper.cpp
+++ b/src/qml/jsruntime/qv4compilationunitmapper.cpp
@@ -4,7 +4,6 @@
#include "qv4compilationunitmapper_p.h"
#include <private/qv4compileddata_p.h>
-#include <private/qv4executablecompilationunit_p.h>
#include <QtCore/qdatetime.h>
#include <QtCore/qmutex.h>
@@ -29,23 +28,24 @@ public:
s_staticUnits.insert(file, staticUnit);
}
+ void remove(const QString &file)
+ {
+ s_staticUnits.remove(file);
+ }
+
private:
QMutexLocker<QMutex> m_lock;
static QMutex s_mutex;
- // We can copy the mappers around because they're all static, that is the dtors are noops.
+ // We can copy the mappers around because they're all static.
+ // We never unmap the files.
static QHash<QString, CompilationUnitMapper> s_staticUnits;
};
QHash<QString, CompilationUnitMapper> StaticUnitCache::s_staticUnits;
QMutex StaticUnitCache::s_mutex;
-CompilationUnitMapper::~CompilationUnitMapper()
-{
- close();
-}
-
CompiledData::Unit *CompilationUnitMapper::get(
const QString &cacheFilePath, const QDateTime &sourceTimeStamp, QString *errorString)
{
@@ -54,7 +54,7 @@ CompiledData::Unit *CompilationUnitMapper::get(
CompilationUnitMapper mapper = cache.get(cacheFilePath);
if (mapper.dataPtr) {
auto *unit = reinterpret_cast<CompiledData::Unit *>(mapper.dataPtr);
- if (ExecutableCompilationUnit::verifyHeader(unit, sourceTimeStamp, errorString)) {
+ if (unit->verifyHeader(sourceTimeStamp, errorString)) {
*this = mapper;
return unit;
}
@@ -63,10 +63,19 @@ CompiledData::Unit *CompilationUnitMapper::get(
}
CompiledData::Unit *data = open(cacheFilePath, sourceTimeStamp, errorString);
- if (data && (data->flags & CompiledData::Unit::StaticData))
+ if (data && (data->flags & CompiledData::Unit::StaticData)) {
cache.set(cacheFilePath, *this);
+ return data;
+ } else {
+ close();
+ return nullptr;
+ }
+}
- return data;
+void CompilationUnitMapper::invalidate(const QString &cacheFilePath)
+{
+ StaticUnitCache cache;
+ cache.remove(cacheFilePath);
}
QT_END_NAMESPACE
diff --git a/src/qml/jsruntime/qv4compilationunitmapper_p.h b/src/qml/jsruntime/qv4compilationunitmapper_p.h
index 0d3ec4eeee..c214141804 100644
--- a/src/qml/jsruntime/qv4compilationunitmapper_p.h
+++ b/src/qml/jsruntime/qv4compilationunitmapper_p.h
@@ -29,10 +29,9 @@ struct Unit;
class CompilationUnitMapper
{
public:
- ~CompilationUnitMapper();
-
CompiledData::Unit *get(
const QString &cacheFilePath, const QDateTime &sourceTimeStamp, QString *errorString);
+ static void invalidate(const QString &cacheFilePath);
private:
CompiledData::Unit *open(
diff --git a/src/qml/jsruntime/qv4compilationunitmapper_unix.cpp b/src/qml/jsruntime/qv4compilationunitmapper_unix.cpp
index d7364f8706..204e222121 100644
--- a/src/qml/jsruntime/qv4compilationunitmapper_unix.cpp
+++ b/src/qml/jsruntime/qv4compilationunitmapper_unix.cpp
@@ -3,13 +3,14 @@
#include "qv4compilationunitmapper_p.h"
-#include <sys/mman.h>
-#include <functional>
#include <private/qcore_unix_p.h>
-#include <QScopeGuard>
-#include <QDateTime>
+#include <private/qv4compileddata_p.h>
-#include "qv4executablecompilationunit_p.h"
+#include <QtCore/qscopeguard.h>
+#include <QtCore/qdatetime.h>
+
+#include <functional>
+#include <sys/mman.h>
QT_BEGIN_NAMESPACE
@@ -37,12 +38,22 @@ CompiledData::Unit *CompilationUnitMapper::open(const QString &cacheFileName, co
return nullptr;
}
- if (!ExecutableCompilationUnit::verifyHeader(&header, sourceTimeStamp, errorString))
+ if (!header.verifyHeader(sourceTimeStamp, errorString))
return nullptr;
// Data structure and qt version matched, so now we can access the rest of the file safely.
length = static_cast<size_t>(lseek(fd, 0, SEEK_END));
+ /* Error out early on file corruption. We assume we can read header.unitSize bytes
+ later (even before verifying the checksum), potentially causing out-of-bound
+ reads
+ Also, no need to wait until checksum verification if we know beforehand
+ that the cached unit is bogus
+ */
+ if (length != header.unitSize) {
+ *errorString = QStringLiteral("Potential file corruption, file too small");
+ return nullptr;
+ }
void *ptr = mmap(nullptr, length, PROT_READ, MAP_SHARED, fd, /*offset*/0);
if (ptr == MAP_FAILED) {
diff --git a/src/qml/jsruntime/qv4compilationunitmapper_win.cpp b/src/qml/jsruntime/qv4compilationunitmapper_win.cpp
index 2ea54ce286..73096207b4 100644
--- a/src/qml/jsruntime/qv4compilationunitmapper_win.cpp
+++ b/src/qml/jsruntime/qv4compilationunitmapper_win.cpp
@@ -3,10 +3,12 @@
#include "qv4compilationunitmapper_p.h"
-#include "qv4executablecompilationunit_p.h"
-#include <QScopeGuard>
-#include <QFileInfo>
-#include <QDateTime>
+#include <private/qv4compileddata_p.h>
+
+#include <QtCore/qdatetime.h>
+#include <QtCore/qfileinfo.h>
+#include <QtCore/qscopeguard.h>
+
#include <qt_windows.h>
QT_BEGIN_NAMESPACE
@@ -45,11 +47,28 @@ CompiledData::Unit *CompilationUnitMapper::open(const QString &cacheFileName, co
return nullptr;
}
- if (!ExecutableCompilationUnit::verifyHeader(&header, sourceTimeStamp, errorString))
+ if (!header.verifyHeader(sourceTimeStamp, errorString))
return nullptr;
// Data structure and qt version matched, so now we can access the rest of the file safely.
+ /* Error out early on file corruption. We assume we can read header.unitSize bytes
+ later (even before verifying the checksum), potentially causing out-of-bound
+ reads
+ Also, no need to wait until checksum verification if we know beforehand
+ that the cached unit is bogus
+ */
+ LARGE_INTEGER fileSize;
+ if (!GetFileSizeEx(handle, &fileSize)) {
+ *errorString = QStringLiteral("Could not determine file size");
+ return nullptr;
+ }
+ if (header.unitSize != fileSize.QuadPart) {
+ *errorString = QStringLiteral("Potential file corruption, file too small");
+ return nullptr;
+ }
+
+
HANDLE fileMappingHandle = CreateFileMapping(handle, 0, PAGE_READONLY, 0, 0, 0);
if (!fileMappingHandle) {
*errorString = qt_error_string(GetLastError());
diff --git a/src/qml/jsruntime/qv4context.cpp b/src/qml/jsruntime/qv4context.cpp
index b702dbe2db..01f9b4adf3 100644
--- a/src/qml/jsruntime/qv4context.cpp
+++ b/src/qml/jsruntime/qv4context.cpp
@@ -32,7 +32,7 @@ Heap::CallContext *ExecutionContext::newBlockContext(CppStackFrame *frame, int b
Heap::ExecutionContext *outer = static_cast<Heap::ExecutionContext *>(frame->context()->m());
c->outer.set(v4, outer);
if (frame->isJSTypesFrame()) {
- c->function.set(v4, static_cast<Heap::FunctionObject *>(
+ c->function.set(v4, static_cast<Heap::JavaScriptFunctionObject *>(
Value::fromStaticValue(
static_cast<JSTypesStackFrame *>(frame)->jsFrame->function).m()));
} else {
@@ -74,7 +74,7 @@ Heap::CallContext *ExecutionContext::newCallContext(JSTypesStackFrame *frame)
c->init();
c->outer.set(v4, outer);
- c->function.set(v4, static_cast<Heap::FunctionObject *>(
+ c->function.set(v4, static_cast<Heap::JavaScriptFunctionObject *>(
Value::fromStaticValue(frame->jsFrame->function).m()));
const CompiledData::Function *compiledFunction = function->compiledFunction;
@@ -299,9 +299,14 @@ ReturnedValue ExecutionContext::getProperty(String *name)
case Heap::ExecutionContext::Type_CallContext: {
Heap::CallContext *c = static_cast<Heap::CallContext *>(ctx);
- uint index = c->internalClass->indexOfValueOrGetter(id);
- if (index < UINT_MAX)
+ const uint index = c->internalClass->indexOfValueOrGetter(id);
+ if (index < c->locals.alloc)
return c->locals[index].asReturnedValue();
+
+ // TODO: We should look up the module imports here, but those are part of the CU:
+ // imports[index - c->locals.size];
+ // See QTBUG-118478
+
Q_FALLTHROUGH();
}
case Heap::ExecutionContext::Type_WithContext:
@@ -349,9 +354,14 @@ ReturnedValue ExecutionContext::getPropertyAndBase(String *name, Value *base)
case Heap::ExecutionContext::Type_CallContext: {
Heap::CallContext *c = static_cast<Heap::CallContext *>(ctx);
- uint index = c->internalClass->indexOfValueOrGetter(id);
- if (index < UINT_MAX)
+ const uint index = c->internalClass->indexOfValueOrGetter(id);
+ if (index < c->locals.alloc)
return c->locals[index].asReturnedValue();
+
+ // TODO: We should look up the module imports here, but those are part of the CU:
+ // imports[index - c->locals.size];
+ // See QTBUG-118478
+
Q_FALLTHROUGH();
}
case Heap::ExecutionContext::Type_GlobalContext: {
diff --git a/src/qml/jsruntime/qv4context_p.h b/src/qml/jsruntime/qv4context_p.h
index 82a9472223..48b6e04025 100644
--- a/src/qml/jsruntime/qv4context_p.h
+++ b/src/qml/jsruntime/qv4context_p.h
@@ -64,7 +64,7 @@ Q_STATIC_ASSERT(offsetof(ExecutionContextData, outer) == 0);
Q_STATIC_ASSERT(offsetof(ExecutionContextData, activation) == offsetof(ExecutionContextData, outer) + QT_POINTER_SIZE);
#define CallContextMembers(class, Member) \
- Member(class, Pointer, FunctionObject *, function) \
+ Member(class, Pointer, JavaScriptFunctionObject *, function) \
Member(class, ValueArray, ValueArray, locals)
DECLARE_HEAP_OBJECT(CallContext, ExecutionContext) {
diff --git a/src/qml/jsruntime/qv4dataview.cpp b/src/qml/jsruntime/qv4dataview.cpp
index f4ca09a127..689eb9232b 100644
--- a/src/qml/jsruntime/qv4dataview.cpp
+++ b/src/qml/jsruntime/qv4dataview.cpp
@@ -13,9 +13,9 @@ using namespace QV4;
DEFINE_OBJECT_VTABLE(DataViewCtor);
DEFINE_OBJECT_VTABLE(DataView);
-void Heap::DataViewCtor::init(QV4::ExecutionContext *scope)
+void Heap::DataViewCtor::init(QV4::ExecutionEngine *engine)
{
- Heap::FunctionObject::init(scope, QStringLiteral("DataView"));
+ Heap::FunctionObject::init(engine, QStringLiteral("DataView"));
}
static uint toIndex(ExecutionEngine *e, const Value &v)
diff --git a/src/qml/jsruntime/qv4dataview_p.h b/src/qml/jsruntime/qv4dataview_p.h
index ab2e1e589a..b5fa41d964 100644
--- a/src/qml/jsruntime/qv4dataview_p.h
+++ b/src/qml/jsruntime/qv4dataview_p.h
@@ -24,7 +24,7 @@ namespace QV4 {
namespace Heap {
struct DataViewCtor : FunctionObject {
- void init(QV4::ExecutionContext *scope);
+ void init(ExecutionEngine *engine);
};
#define DataViewMembers(class, Member) \
diff --git a/src/qml/jsruntime/qv4dateobject.cpp b/src/qml/jsruntime/qv4dateobject.cpp
index ed64493d9a..2cb020e495 100644
--- a/src/qml/jsruntime/qv4dateobject.cpp
+++ b/src/qml/jsruntime/qv4dateobject.cpp
@@ -315,14 +315,14 @@ static inline double ParseString(const QString &s, double localTZA)
First, try the format defined in ECMA 262's "Date Time String Format";
only if that fails, fall back to QDateTime for parsing
- The defined string format is YYYY-MM-DDTHH:mm:ss.sssZ; the time (T and all
- after it) may be omitted; in each part, the second and later components
- are optional; and there's an extended syntax for negative and large
- positive years: +/-YYYYYY; the leading sign, even when +, isn't optional.
- If month or day is omitted, it is 01; if minute or second is omitted, it's
- 00; if milliseconds are omitted, they're 000.
-
- When the time zone offset is absent, date-only forms are interpreted as
+ The defined string format is yyyy-MM-ddTHH:mm:ss.zzzt; the time (T and all
+ after it) may be omitted. In each part, the second and later components
+ are optional. There's an extended syntax for negative and large positive
+ years: ±yyyyyy; the leading sign, even when +, isn't optional. If month
+ (MM) or day (dd) is omitted, it is 01; if minute (mm) or second (ss) is
+ omitted, it's 00; if milliseconds (zzz) are omitted, they're 000.
+
+ When the time zone offset (t) is absent, date-only forms are interpreted as
indicating a UTC time and date-time forms are interpreted in local time.
*/
@@ -719,11 +719,33 @@ QString DateObject::dateTimeToString(const QDateTime &dateTime, ExecutionEngine
return ToString(TimeClip(dateTime.toMSecsSinceEpoch()), engine->localTZA);
}
+double DateObject::dateTimeToNumber(const QDateTime &dateTime)
+{
+ if (!dateTime.isValid())
+ return qQNaN();
+ return TimeClip(dateTime.toMSecsSinceEpoch());
+}
+
QDateTime DateObject::stringToDateTime(const QString &string, ExecutionEngine *engine)
{
return ToDateTime(ParseString(string, engine->localTZA), QTimeZone::LocalTime);
}
+QDateTime DateObject::timestampToDateTime(double timestamp, QTimeZone zone)
+{
+ return ToDateTime(timestamp, zone);
+}
+
+double DateObject::componentsToTimestamp(
+ double year, double month, double day, double hours,
+ double mins, double secs, double ms, ExecutionEngine *v4)
+{
+ if (year >= 0 && year <= 99)
+ year += 1900;
+ const double t = MakeDate(MakeDay(year, month, day), MakeTime(hours, mins, secs, ms));
+ return UTC(t, v4->localTZA);
+}
+
QDate DateObject::dateTimeToDate(const QDateTime &dateTime)
{
// If the Date object was parse()d from a string with no time part
@@ -744,9 +766,9 @@ QDate DateObject::dateTimeToDate(const QDateTime &dateTime)
DEFINE_OBJECT_VTABLE(DateCtor);
-void Heap::DateCtor::init(QV4::ExecutionContext *scope)
+void Heap::DateCtor::init(QV4::ExecutionEngine *engine)
{
- Heap::FunctionObject::init(scope, QStringLiteral("Date"));
+ Heap::FunctionObject::init(engine, QStringLiteral("Date"));
}
ReturnedValue DateCtor::virtualCallAsConstructor(const FunctionObject *that, const Value *argv, int argc, const Value *newTarget)
@@ -773,17 +795,14 @@ ReturnedValue DateCtor::virtualCallAsConstructor(const FunctionObject *that, con
}
else { // d.argc > 1
- double year = argv[0].toNumber();
- double month = argv[1].toNumber();
- double day = argc >= 3 ? argv[2].toNumber() : 1;
- double hours = argc >= 4 ? argv[3].toNumber() : 0;
- double mins = argc >= 5 ? argv[4].toNumber() : 0;
- double secs = argc >= 6 ? argv[5].toNumber() : 0;
- double ms = argc >= 7 ? argv[6].toNumber() : 0;
- if (year >= 0 && year <= 99)
- year += 1900;
- t = MakeDate(MakeDay(year, month, day), MakeTime(hours, mins, secs, ms));
- t = UTC(t, v4->localTZA);
+ const double year = argv[0].toNumber();
+ const double month = argv[1].toNumber();
+ const double day = argc >= 3 ? argv[2].toNumber() : 1;
+ const double hours = argc >= 4 ? argv[3].toNumber() : 0;
+ const double mins = argc >= 5 ? argv[4].toNumber() : 0;
+ const double secs = argc >= 6 ? argv[5].toNumber() : 0;
+ const double ms = argc >= 7 ? argv[6].toNumber() : 0;
+ t = DateObject::componentsToTimestamp(year, month, day, hours, mins, secs, ms, v4);
}
ReturnedValue o = Encode(v4->newDateObject(t));
diff --git a/src/qml/jsruntime/qv4dateobject_p.h b/src/qml/jsruntime/qv4dateobject_p.h
index ad5b51f063..4c184de897 100644
--- a/src/qml/jsruntime/qv4dateobject_p.h
+++ b/src/qml/jsruntime/qv4dateobject_p.h
@@ -189,7 +189,7 @@ private:
struct DateCtor : FunctionObject {
- void init(QV4::ExecutionContext *scope);
+ void init(QV4::ExecutionEngine *engine);
};
}
@@ -202,12 +202,18 @@ struct DateObject: ReferenceObject {
void setDate(double date) { d()->setDate(date); }
double date() const { return d()->date(); }
- Q_QML_PRIVATE_EXPORT QDateTime toQDateTime() const;
+ Q_QML_EXPORT QDateTime toQDateTime() const;
QString toString() const;
static QString dateTimeToString(const QDateTime &dateTime, ExecutionEngine *engine);
+ static double dateTimeToNumber(const QDateTime &dateTime);
static QDate dateTimeToDate(const QDateTime &dateTime);
static QDateTime stringToDateTime(const QString &string, ExecutionEngine *engine);
+ static QDateTime timestampToDateTime(double timestamp, QTimeZone zone = QTimeZone::LocalTime);
+ static double componentsToTimestamp(
+ double year, double month, double day,
+ double hours, double mins, double secs, double ms,
+ ExecutionEngine *v4);
};
template<>
diff --git a/src/qml/jsruntime/qv4engine.cpp b/src/qml/jsruntime/qv4engine.cpp
index 3d14d504e2..6754c3c887 100644
--- a/src/qml/jsruntime/qv4engine.cpp
+++ b/src/qml/jsruntime/qv4engine.cpp
@@ -61,6 +61,7 @@
#include "qv4variantobject_p.h"
#include "qv4sequenceobject_p.h"
#include "qv4qobjectwrapper_p.h"
+#include "qv4qmetaobjectwrapper_p.h"
#include "qv4memberdata_p.h"
#include "qv4arraybuffer_p.h"
#include "qv4dataview_p.h"
@@ -92,8 +93,6 @@
#include <valgrind/memcheck.h>
#endif
-Q_DECLARE_METATYPE(QList<int>)
-
QT_BEGIN_NAMESPACE
DEFINE_BOOL_CONFIG_OPTION(disableDiskCache, QML_DISABLE_DISK_CACHE);
@@ -372,6 +371,8 @@ ExecutionEngine::ExecutionEngine(QJSEngine *jsEngine)
const size_t guardPages = 2 * WTF::pageSize();
memoryManager = new QV4::MemoryManager(this);
+ // we don't want to run the gc while the initial setup is not done; not even in aggressive mode
+ GCCriticalSection gcCriticalSection(this);
// reserve space for the JS stack
// we allow it to grow to a bit more than m_maxJSStackSize, as we can overshoot due to ScopedValues
// allocated outside of JIT'ed methods.
@@ -626,25 +627,23 @@ ExecutionEngine::ExecutionEngine(QJSEngine *jsEngine)
ic = newInternalClass(SequencePrototype::staticVTable(), SequencePrototype::defaultPrototype(this));
jsObjects[SequenceProto] = ScopedValue(scope, memoryManager->allocObject<SequencePrototype>(ic->d()));
- ExecutionContext *global = rootContext();
-
- jsObjects[Object_Ctor] = memoryManager->allocate<ObjectCtor>(global);
- jsObjects[String_Ctor] = memoryManager->allocate<StringCtor>(global);
- jsObjects[Symbol_Ctor] = memoryManager->allocate<SymbolCtor>(global);
- jsObjects[Number_Ctor] = memoryManager->allocate<NumberCtor>(global);
- jsObjects[Boolean_Ctor] = memoryManager->allocate<BooleanCtor>(global);
- jsObjects[Array_Ctor] = memoryManager->allocate<ArrayCtor>(global);
- jsObjects[Function_Ctor] = memoryManager->allocate<FunctionCtor>(global);
- jsObjects[GeneratorFunction_Ctor] = memoryManager->allocate<GeneratorFunctionCtor>(global);
- jsObjects[Date_Ctor] = memoryManager->allocate<DateCtor>(global);
- jsObjects[RegExp_Ctor] = memoryManager->allocate<RegExpCtor>(global);
- jsObjects[Error_Ctor] = memoryManager->allocate<ErrorCtor>(global);
- jsObjects[EvalError_Ctor] = memoryManager->allocate<EvalErrorCtor>(global);
- jsObjects[RangeError_Ctor] = memoryManager->allocate<RangeErrorCtor>(global);
- jsObjects[ReferenceError_Ctor] = memoryManager->allocate<ReferenceErrorCtor>(global);
- jsObjects[SyntaxError_Ctor] = memoryManager->allocate<SyntaxErrorCtor>(global);
- jsObjects[TypeError_Ctor] = memoryManager->allocate<TypeErrorCtor>(global);
- jsObjects[URIError_Ctor] = memoryManager->allocate<URIErrorCtor>(global);
+ jsObjects[Object_Ctor] = memoryManager->allocate<ObjectCtor>(this);
+ jsObjects[String_Ctor] = memoryManager->allocate<StringCtor>(this);
+ jsObjects[Symbol_Ctor] = memoryManager->allocate<SymbolCtor>(this);
+ jsObjects[Number_Ctor] = memoryManager->allocate<NumberCtor>(this);
+ jsObjects[Boolean_Ctor] = memoryManager->allocate<BooleanCtor>(this);
+ jsObjects[Array_Ctor] = memoryManager->allocate<ArrayCtor>(this);
+ jsObjects[Function_Ctor] = memoryManager->allocate<FunctionCtor>(this);
+ jsObjects[GeneratorFunction_Ctor] = memoryManager->allocate<GeneratorFunctionCtor>(this);
+ jsObjects[Date_Ctor] = memoryManager->allocate<DateCtor>(this);
+ jsObjects[RegExp_Ctor] = memoryManager->allocate<RegExpCtor>(this);
+ jsObjects[Error_Ctor] = memoryManager->allocate<ErrorCtor>(this);
+ jsObjects[EvalError_Ctor] = memoryManager->allocate<EvalErrorCtor>(this);
+ jsObjects[RangeError_Ctor] = memoryManager->allocate<RangeErrorCtor>(this);
+ jsObjects[ReferenceError_Ctor] = memoryManager->allocate<ReferenceErrorCtor>(this);
+ jsObjects[SyntaxError_Ctor] = memoryManager->allocate<SyntaxErrorCtor>(this);
+ jsObjects[TypeError_Ctor] = memoryManager->allocate<TypeErrorCtor>(this);
+ jsObjects[URIError_Ctor] = memoryManager->allocate<URIErrorCtor>(this);
jsObjects[IteratorProto] = memoryManager->allocate<IteratorPrototype>();
ic = newInternalClass(ForInIteratorPrototype::staticVTable(), iteratorPrototype());
@@ -662,9 +661,9 @@ ExecutionEngine::ExecutionEngine(QJSEngine *jsEngine)
// url
//
- jsObjects[Url_Ctor] = memoryManager->allocate<UrlCtor>(global);
+ jsObjects[Url_Ctor] = memoryManager->allocate<UrlCtor>(this);
jsObjects[UrlProto] = memoryManager->allocate<UrlPrototype>();
- jsObjects[UrlSearchParams_Ctor] = memoryManager->allocate<UrlSearchParamsCtor>(global);
+ jsObjects[UrlSearchParams_Ctor] = memoryManager->allocate<UrlSearchParamsCtor>(this);
jsObjects[UrlSearchParamsProto] = memoryManager->allocate<UrlSearchParamsPrototype>();
str = newString(QStringLiteral("get [Symbol.species]"));
@@ -702,19 +701,19 @@ ExecutionEngine::ExecutionEngine(QJSEngine *jsEngine)
sequencePrototype()->cast<SequencePrototype>()->init();
- jsObjects[WeakMap_Ctor] = memoryManager->allocate<WeakMapCtor>(global);
+ jsObjects[WeakMap_Ctor] = memoryManager->allocate<WeakMapCtor>(this);
jsObjects[WeakMapProto] = memoryManager->allocate<WeakMapPrototype>();
static_cast<WeakMapPrototype *>(weakMapPrototype())->init(this, weakMapCtor());
- jsObjects[Map_Ctor] = memoryManager->allocate<MapCtor>(global);
+ jsObjects[Map_Ctor] = memoryManager->allocate<MapCtor>(this);
jsObjects[MapProto] = memoryManager->allocate<MapPrototype>();
static_cast<MapPrototype *>(mapPrototype())->init(this, mapCtor());
- jsObjects[WeakSet_Ctor] = memoryManager->allocate<WeakSetCtor>(global);
+ jsObjects[WeakSet_Ctor] = memoryManager->allocate<WeakSetCtor>(this);
jsObjects[WeakSetProto] = memoryManager->allocate<WeakSetPrototype>();
static_cast<WeakSetPrototype *>(weakSetPrototype())->init(this, weakSetCtor());
- jsObjects[Set_Ctor] = memoryManager->allocate<SetCtor>(global);
+ jsObjects[Set_Ctor] = memoryManager->allocate<SetCtor>(this);
jsObjects[SetProto] = memoryManager->allocate<SetPrototype>();
static_cast<SetPrototype *>(setPrototype())->init(this, setCtor());
@@ -722,33 +721,34 @@ ExecutionEngine::ExecutionEngine(QJSEngine *jsEngine)
// promises
//
- jsObjects[Promise_Ctor] = memoryManager->allocate<PromiseCtor>(global);
+ jsObjects[Promise_Ctor] = memoryManager->allocate<PromiseCtor>(this);
jsObjects[PromiseProto] = memoryManager->allocate<PromisePrototype>();
static_cast<PromisePrototype *>(promisePrototype())->init(this, promiseCtor());
// typed arrays
- jsObjects[SharedArrayBuffer_Ctor] = memoryManager->allocate<SharedArrayBufferCtor>(global);
+ jsObjects[SharedArrayBuffer_Ctor] = memoryManager->allocate<SharedArrayBufferCtor>(this);
jsObjects[SharedArrayBufferProto] = memoryManager->allocate<SharedArrayBufferPrototype>();
static_cast<SharedArrayBufferPrototype *>(sharedArrayBufferPrototype())->init(this, sharedArrayBufferCtor());
- jsObjects[ArrayBuffer_Ctor] = memoryManager->allocate<ArrayBufferCtor>(global);
+ jsObjects[ArrayBuffer_Ctor] = memoryManager->allocate<ArrayBufferCtor>(this);
jsObjects[ArrayBufferProto] = memoryManager->allocate<ArrayBufferPrototype>();
static_cast<ArrayBufferPrototype *>(arrayBufferPrototype())->init(this, arrayBufferCtor());
- jsObjects[DataView_Ctor] = memoryManager->allocate<DataViewCtor>(global);
+ jsObjects[DataView_Ctor] = memoryManager->allocate<DataViewCtor>(this);
jsObjects[DataViewProto] = memoryManager->allocate<DataViewPrototype>();
static_cast<DataViewPrototype *>(dataViewPrototype())->init(this, dataViewCtor());
jsObjects[ValueTypeProto] = (Heap::Base *) nullptr;
jsObjects[SignalHandlerProto] = (Heap::Base *) nullptr;
+ jsObjects[TypeWrapperProto] = (Heap::Base *) nullptr;
- jsObjects[IntrinsicTypedArray_Ctor] = memoryManager->allocate<IntrinsicTypedArrayCtor>(global);
+ jsObjects[IntrinsicTypedArray_Ctor] = memoryManager->allocate<IntrinsicTypedArrayCtor>(this);
jsObjects[IntrinsicTypedArrayProto] = memoryManager->allocate<IntrinsicTypedArrayPrototype>();
static_cast<IntrinsicTypedArrayPrototype *>(intrinsicTypedArrayPrototype())
->init(this, static_cast<IntrinsicTypedArrayCtor *>(intrinsicTypedArrayCtor()));
for (int i = 0; i < NTypedArrayTypes; ++i) {
- static_cast<Value &>(typedArrayCtors[i]) = memoryManager->allocate<TypedArrayCtor>(global, Heap::TypedArray::Type(i));
+ static_cast<Value &>(typedArrayCtors[i]) = memoryManager->allocate<TypedArrayCtor>(this, Heap::TypedArray::Type(i));
static_cast<Value &>(typedArrayPrototype[i]) = memoryManager->allocate<TypedArrayPrototype>(Heap::TypedArray::Type(i));
typedArrayPrototype[i].as<TypedArrayPrototype>()->init(this, static_cast<TypedArrayCtor *>(typedArrayCtors[i].as<Object>()));
}
@@ -795,14 +795,14 @@ ExecutionEngine::ExecutionEngine(QJSEngine *jsEngine)
globalObject->defineDefaultProperty(QStringLiteral("Math"), (o = memoryManager->allocate<MathObject>()));
globalObject->defineDefaultProperty(QStringLiteral("JSON"), (o = memoryManager->allocate<JsonObject>()));
globalObject->defineDefaultProperty(QStringLiteral("Reflect"), (o = memoryManager->allocate<Reflect>()));
- globalObject->defineDefaultProperty(QStringLiteral("Proxy"), (o = memoryManager->allocate<Proxy>(rootContext())));
+ globalObject->defineDefaultProperty(QStringLiteral("Proxy"), (o = memoryManager->allocate<Proxy>(this)));
globalObject->defineReadonlyProperty(QStringLiteral("undefined"), Value::undefinedValue());
globalObject->defineReadonlyProperty(QStringLiteral("NaN"), Value::fromDouble(std::numeric_limits<double>::quiet_NaN()));
globalObject->defineReadonlyProperty(QStringLiteral("Infinity"), Value::fromDouble(Q_INFINITY));
- jsObjects[Eval_Function] = memoryManager->allocate<EvalFunction>(global);
+ jsObjects[Eval_Function] = memoryManager->allocate<EvalFunction>(this);
globalObject->defineDefaultProperty(QStringLiteral("eval"), *evalFunction());
// ES6: 20.1.2.12 & 20.1.2.13:
@@ -831,7 +831,9 @@ ExecutionEngine::ExecutionEngine(QJSEngine *jsEngine)
globalObject->defineDefaultProperty(QStringLiteral("escape"), GlobalFunctions::method_escape, 1);
globalObject->defineDefaultProperty(QStringLiteral("unescape"), GlobalFunctions::method_unescape, 1);
- ScopedFunctionObject t(scope, memoryManager->allocate<FunctionObject>(rootContext(), nullptr, ::throwTypeError));
+ ScopedFunctionObject t(
+ scope,
+ memoryManager->allocate<DynamicFunctionObject>(this, nullptr, ::throwTypeError));
t->defineReadonlyProperty(id_length(), Value::fromInt32(0));
t->setInternalClass(t->internalClass()->cryopreserved());
jsObjects[ThrowerObject] = t;
@@ -850,7 +852,6 @@ ExecutionEngine::ExecutionEngine(QJSEngine *jsEngine)
ExecutionEngine::~ExecutionEngine()
{
- modules.clear();
for (auto val : nativeModules) {
PersistentValueStorage::free(val);
}
@@ -861,8 +862,12 @@ ExecutionEngine::~ExecutionEngine()
delete identifierTable;
delete memoryManager;
- while (!compilationUnits.isEmpty())
- (*compilationUnits.begin())->unlink();
+ for (const auto &cu : std::as_const(m_compilationUnits)) {
+ Q_ASSERT(cu->engine == this);
+ cu->clear();
+ cu->engine = nullptr;
+ }
+ m_compilationUnits.clear();
delete bumperPointerAllocator;
delete regExpCache;
@@ -896,7 +901,7 @@ void ExecutionEngine::setProfiler(Profiling::Profiler *profiler)
void ExecutionEngine::initRootContext()
{
Scope scope(this);
- Scoped<ExecutionContext> r(scope, memoryManager->allocManaged<ExecutionContext>(sizeof(ExecutionContext::Data)));
+ Scoped<ExecutionContext> r(scope, memoryManager->allocManaged<ExecutionContext>());
r->d_unchecked()->init(Heap::ExecutionContext::Type_GlobalContext);
r->d()->activation.set(this, globalObject->d());
jsObjects[RootContext] = r;
@@ -1227,8 +1232,6 @@ QQmlRefPointer<QQmlContextData> ExecutionEngine::callingQmlContext() const
StackTrace ExecutionEngine::stackTrace(int frameLimit) const
{
- Scope scope(const_cast<ExecutionEngine *>(this));
- ScopedString name(scope);
StackTrace stack;
CppStackFrame *f = currentStackFrame;
@@ -1236,8 +1239,8 @@ StackTrace ExecutionEngine::stackTrace(int frameLimit) const
QV4::StackFrame frame;
frame.source = f->source();
frame.function = f->function();
- frame.line = qAbs(f->lineNumber());
- frame.column = -1;
+ frame.line = f->lineNumber();
+
stack.append(frame);
if (f->isJSTypesFrame()) {
if (static_cast<JSTypesStackFrame *>(f)->isTailCalling()) {
@@ -1274,7 +1277,7 @@ static inline char *v4StackTrace(const ExecutionContext *context)
const QString fileName = url.isLocalFile() ? url.toLocalFile() : url.toString();
str << "frame={level=\"" << i << "\",func=\"" << stackTrace.at(i).function
<< "\",file=\"" << fileName << "\",fullname=\"" << fileName
- << "\",line=\"" << stackTrace.at(i).line << "\",language=\"js\"}";
+ << "\",line=\"" << qAbs(stackTrace.at(i).line) << "\",language=\"js\"}";
}
}
str << ']';
@@ -1326,7 +1329,7 @@ void ExecutionEngine::markObjects(MarkStack *markStack)
identifierTable->markObjects(markStack);
- for (auto compilationUnit: compilationUnits)
+ for (const auto &compilationUnit : std::as_const(m_compilationUnits))
compilationUnit->markObjects(markStack);
}
@@ -1469,7 +1472,7 @@ QQmlError ExecutionEngine::catchExceptionAsQmlError()
if (!trace.isEmpty()) {
QV4::StackFrame frame = trace.constFirst();
error.setUrl(QUrl(frame.source));
- error.setLine(frame.line);
+ error.setLine(qAbs(frame.line));
error.setColumn(frame.column);
}
QV4::Scoped<QV4::ErrorObject> errorObj(scope, exception);
@@ -1480,20 +1483,21 @@ QQmlError ExecutionEngine::catchExceptionAsQmlError()
// Variant conversion code
typedef QSet<QV4::Heap::Object *> V4ObjectSet;
-static QVariant toVariant(const QV4::Value &value, QMetaType typeHint, bool createJSValueForObjects, V4ObjectSet *visitedObjects);
+enum class JSToQVariantConversionBehavior {Never, Safish, Aggressive };
+static QVariant toVariant(
+ const QV4::Value &value, QMetaType typeHint, JSToQVariantConversionBehavior conversionBehavior,
+ V4ObjectSet *visitedObjects);
static QObject *qtObjectFromJS(const QV4::Value &value);
-static QVariant objectToVariant(const QV4::Object *o, V4ObjectSet *visitedObjects = nullptr);
+static QVariant objectToVariant(const QV4::Object *o, V4ObjectSet *visitedObjects = nullptr,
+ JSToQVariantConversionBehavior behavior = JSToQVariantConversionBehavior::Safish);
static bool convertToNativeQObject(const QV4::Value &value, QMetaType targetType, void **result);
-static QV4::ReturnedValue variantListToJS(QV4::ExecutionEngine *v4, const QVariantList &lst);
-static QV4::ReturnedValue sequentialIterableToJS(QV4::ExecutionEngine *v4, const QSequentialIterable &lst);
static QV4::ReturnedValue variantMapToJS(QV4::ExecutionEngine *v4, const QVariantMap &vmap);
static QV4::ReturnedValue variantToJS(QV4::ExecutionEngine *v4, const QVariant &value)
{
return v4->metaTypeToJS(value.metaType(), value.constData());
}
-static QVariant toVariant(
- const QV4::Value &value, QMetaType metaType, bool createJSValueForObjects,
+static QVariant toVariant(const QV4::Value &value, QMetaType metaType, JSToQVariantConversionBehavior conversionBehavior,
V4ObjectSet *visitedObjects)
{
Q_ASSERT (!value.isEmpty());
@@ -1504,6 +1508,12 @@ static QVariant toVariant(
if (metaType == QMetaType::fromType<bool>())
return QVariant(value.toBoolean());
+ if (metaType == QMetaType::fromType<double>())
+ return QVariant(value.toNumber());
+
+ if (metaType == QMetaType::fromType<float>())
+ return QVariant(float(value.toNumber()));
+
if (metaType == QMetaType::fromType<QJsonValue>())
return QVariant::fromValue(QV4::JsonObject::toJsonValue(value));
@@ -1565,9 +1575,9 @@ static QVariant toVariant(
QV4::ScopedValue arrayValue(scope);
for (qint64 i = 0; i < length; ++i) {
arrayValue = a->get(i);
- QVariant asVariant(valueMetaType);
- if (QQmlValueTypeProvider::createValueType(
- arrayValue, valueMetaType, asVariant.data())) {
+ QVariant asVariant = QQmlValueTypeProvider::createValueType(
+ arrayValue, valueMetaType);
+ if (asVariant.isValid()) {
retnAsIterable.metaContainer().addValue(retn.data(), asVariant.constData());
continue;
}
@@ -1584,21 +1594,21 @@ static QVariant toVariant(
}
}
- asVariant = toVariant(arrayValue, valueMetaType, false, visitedObjects);
+ asVariant = toVariant(arrayValue, valueMetaType, JSToQVariantConversionBehavior::Never, visitedObjects);
if (valueMetaType == QMetaType::fromType<QVariant>()) {
retnAsIterable.metaContainer().addValue(retn.data(), &asVariant);
} else {
auto originalType = asVariant.metaType();
bool couldConvert = asVariant.convert(valueMetaType);
- if (!couldConvert) {
+ if (!couldConvert && originalType.isValid()) {
+ // If the original type was void, we're converting a "hole" in a sparse
+ // array. There is no point in warning about that.
qWarning().noquote()
<< QLatin1String("Could not convert array value "
"at position %1 from %2 to %3")
.arg(QString::number(i),
QString::fromUtf8(originalType.name()),
QString::fromUtf8(valueMetaType.name()));
- // create default constructed value
- asVariant = QVariant(valueMetaType, nullptr);
}
retnAsIterable.metaContainer().addValue(retn.data(), asVariant.constData());
}
@@ -1625,11 +1635,10 @@ static QVariant toVariant(
return str.at(0);
return str;
}
-#if QT_CONFIG(qml_locale)
- if (const QV4::QQmlLocaleData *ld = value.as<QV4::QQmlLocaleData>())
- return *ld->d()->locale;
-#endif
if (const QV4::DateObject *d = value.as<DateObject>()) {
+ // NOTE: since we convert QTime to JS Date,
+ // round trip will change the variant type (to QDateTime)!
+
if (metaType == QMetaType::fromType<QDate>())
return DateObject::dateTimeToDate(d->toQDateTime());
@@ -1645,7 +1654,11 @@ static QVariant toVariant(
return d->toQUrl();
if (const ArrayBuffer *d = value.as<ArrayBuffer>())
return d->asByteArray();
- // NOTE: since we convert QTime to JS Date, round trip will change the variant type (to QDateTime)!
+ if (const Symbol *symbol = value.as<Symbol>()) {
+ return conversionBehavior == JSToQVariantConversionBehavior::Never
+ ? QVariant::fromValue(QJSValuePrivate::fromReturnedValue(symbol->asReturnedValue()))
+ : symbol->descriptiveString();
+ }
const QV4::Object *object = value.as<QV4::Object>();
Q_ASSERT(object);
@@ -1658,24 +1671,53 @@ static QVariant toVariant(
#endif
if (metaType.isValid() && !(metaType.flags() & QMetaType::PointerToQObject)) {
- QVariant result(metaType);
- if (QQmlValueTypeProvider::createValueType(value, metaType, result.data()))
+ const QVariant result = QQmlValueTypeProvider::createValueType(value, metaType);
+ if (result.isValid())
return result;
}
- if (createJSValueForObjects)
+ if (conversionBehavior == JSToQVariantConversionBehavior::Never)
return QVariant::fromValue(QJSValuePrivate::fromReturnedValue(o->asReturnedValue()));
- return objectToVariant(o, visitedObjects);
+ return objectToVariant(o, visitedObjects, conversionBehavior);
}
+QVariant ExecutionEngine::toVariantLossy(const Value &value)
+{
+ return ::toVariant(value, QMetaType(), JSToQVariantConversionBehavior::Aggressive, nullptr);
+}
-QVariant ExecutionEngine::toVariant(const Value &value, QMetaType typeHint, bool createJSValueForObjects)
+QVariant ExecutionEngine::toVariant(
+ const Value &value, QMetaType typeHint, bool createJSValueForObjectsAndSymbols)
{
- return ::toVariant(value, typeHint, createJSValueForObjects, nullptr);
+ auto behavior = createJSValueForObjectsAndSymbols ? JSToQVariantConversionBehavior::Never
+ : JSToQVariantConversionBehavior::Safish;
+ return ::toVariant(value, typeHint, behavior, nullptr);
+}
+
+static QVariantMap objectToVariantMap(const QV4::Object *o, V4ObjectSet *visitedObjects,
+ JSToQVariantConversionBehavior conversionBehvior)
+{
+ QVariantMap map;
+ QV4::Scope scope(o->engine());
+ QV4::ObjectIterator it(scope, o, QV4::ObjectIterator::EnumerableOnly);
+ QV4::ScopedValue name(scope);
+ QV4::ScopedValue val(scope);
+ while (1) {
+ name = it.nextPropertyNameAsString(val);
+ if (name->isNull())
+ break;
+
+ QString key = name->toQStringNoThrow();
+ map.insert(key, ::toVariant(
+ val, /*type hint*/ QMetaType {},
+ conversionBehvior, visitedObjects));
+ }
+ return map;
}
-static QVariant objectToVariant(const QV4::Object *o, V4ObjectSet *visitedObjects)
+static QVariant objectToVariant(const QV4::Object *o, V4ObjectSet *visitedObjects,
+ JSToQVariantConversionBehavior conversionBehvior)
{
Q_ASSERT(o);
@@ -1703,31 +1745,23 @@ static QVariant objectToVariant(const QV4::Object *o, V4ObjectSet *visitedObject
int length = a->getLength();
for (int ii = 0; ii < length; ++ii) {
v = a->get(ii);
- list << ::toVariant(v, QMetaType {}, /*createJSValueForObjects*/false, visitedObjects);
+ list << ::toVariant(v, QMetaType {}, conversionBehvior,
+ visitedObjects);
}
result = list;
- } else if (const FunctionObject *f = o->as<FunctionObject>()) {
- // If it's a FunctionObject, we can only save it as QJSValue.
- result = QVariant::fromValue(QJSValuePrivate::fromReturnedValue(f->asReturnedValue()));
+ } else if (o->getPrototypeOf() == o->engine()->objectPrototype()->d()
+ || (conversionBehvior == JSToQVariantConversionBehavior::Aggressive &&
+ !o->as<QV4::FunctionObject>())) {
+ /* FunctionObject is excluded for historical reasons, even though
+ objects with a custom prototype risk losing information
+ But the Aggressive path is used only in QJSValue::toVariant
+ which is documented to be lossy
+ */
+ result = objectToVariantMap(o, visitedObjects, conversionBehvior);
} else {
- QVariantMap map;
- QV4::Scope scope(o->engine());
- QV4::ObjectIterator it(scope, o, QV4::ObjectIterator::EnumerableOnly);
- QV4::ScopedValue name(scope);
- QV4::ScopedValue val(scope);
- while (1) {
- name = it.nextPropertyNameAsString(val);
- if (name->isNull())
- break;
-
- QString key = name->toQStringNoThrow();
- map.insert(key, ::toVariant(
- val, /*type hint*/ QMetaType {},
- /*createJSValueForObjects*/false, visitedObjects));
- }
-
- result = map;
+ // If it's not a plain object, we can only save it as QJSValue.
+ result = QVariant::fromValue(QJSValuePrivate::fromReturnedValue(o->asReturnedValue()));
}
visitedObjects->remove(o->d());
@@ -1743,6 +1777,18 @@ QV4::ReturnedValue ExecutionEngine::fromData(
QMetaType metaType, const void *ptr,
QV4::Heap::Object *container, int property, uint flags)
{
+ const auto createSequence = [&](const QMetaSequence metaSequence) {
+ QV4::Scope scope(this);
+ QV4::Scoped<Sequence> sequence(scope);
+ if (container) {
+ return QV4::SequencePrototype::newSequence(
+ this, metaType, metaSequence, ptr,
+ container, property, Heap::ReferenceObject::Flags(flags));
+ } else {
+ return QV4::SequencePrototype::fromData(this, metaType, metaSequence, ptr);
+ }
+ };
+
const int type = metaType.id();
if (type < QMetaType::User) {
switch (QMetaType::Type(type)) {
@@ -1758,6 +1804,10 @@ QV4::ReturnedValue ExecutionEngine::fromData(
return QV4::Encode(*reinterpret_cast<const int*>(ptr));
case QMetaType::UInt:
return QV4::Encode(*reinterpret_cast<const uint*>(ptr));
+ case QMetaType::Long:
+ return QV4::Encode((double)*reinterpret_cast<const long *>(ptr));
+ case QMetaType::ULong:
+ return QV4::Encode((double)*reinterpret_cast<const ulong *>(ptr));
case QMetaType::LongLong:
return QV4::Encode((double)*reinterpret_cast<const qlonglong*>(ptr));
case QMetaType::ULongLong:
@@ -1803,15 +1853,9 @@ QV4::ReturnedValue ExecutionEngine::fromData(
case QMetaType::QObjectStar:
return QV4::QObjectWrapper::wrap(this, *reinterpret_cast<QObject* const *>(ptr));
case QMetaType::QStringList:
- {
- QV4::Scope scope(this);
- QV4::ScopedValue retn(scope, QV4::SequencePrototype::fromData(this, metaType, ptr));
- if (!retn->isUndefined())
- return retn->asReturnedValue();
- return QV4::Encode(newArrayObject(*reinterpret_cast<const QStringList *>(ptr)));
- }
+ return createSequence(QMetaSequence::fromContainer<QStringList>());
case QMetaType::QVariantList:
- return variantListToJS(this, *reinterpret_cast<const QVariantList *>(ptr));
+ return createSequence(QMetaSequence::fromContainer<QVariantList>());
case QMetaType::QVariantMap:
return variantMapToJS(this, *reinterpret_cast<const QVariantMap *>(ptr));
case QMetaType::QJsonValue:
@@ -1820,10 +1864,6 @@ QV4::ReturnedValue ExecutionEngine::fromData(
return QV4::JsonObject::fromJsonObject(this, *reinterpret_cast<const QJsonObject *>(ptr));
case QMetaType::QJsonArray:
return QV4::JsonObject::fromJsonArray(this, *reinterpret_cast<const QJsonArray *>(ptr));
-#if QT_CONFIG(qml_locale)
- case QMetaType::QLocale:
- return QQmlLocale::wrap(this, *reinterpret_cast<const QLocale*>(ptr));
-#endif
case QMetaType::QPixmap:
case QMetaType::QImage:
// Scarce value types
@@ -1831,106 +1871,95 @@ QV4::ReturnedValue ExecutionEngine::fromData(
default:
break;
}
+ }
- if (const QMetaObject *vtmo = QQmlMetaType::metaObjectForValueType(metaType)) {
- if (container) {
- return QV4::QQmlValueTypeWrapper::create(
- this, ptr, vtmo, metaType,
- container, property, Heap::ReferenceObject::Flags(flags));
- } else {
- return QV4::QQmlValueTypeWrapper::create(this, ptr, vtmo, metaType);
- }
- }
+ if (metaType.flags() & QMetaType::IsEnumeration)
+ return fromData(metaType.underlyingType(), ptr, container, property, flags);
- } else {
- QV4::Scope scope(this);
- if (metaType == QMetaType::fromType<QQmlListReference>()) {
- typedef QQmlListReferencePrivate QDLRP;
- QDLRP *p = QDLRP::get((QQmlListReference*)const_cast<void *>(ptr));
- if (p->object)
- return QV4::QmlListWrapper::create(scope.engine, p->property, p->propertyType);
- else
- return QV4::Encode::null();
- } else if (auto flags = metaType.flags(); flags & QMetaType::IsQmlList) {
- // casting to QQmlListProperty<QObject> is slightly nasty, but it's the
- // same QQmlListReference does.
- const auto *p = static_cast<const QQmlListProperty<QObject> *>(ptr);
- if (p->object)
- return QV4::QmlListWrapper::create(scope.engine, *p, metaType);
- else
- return QV4::Encode::null();
- } else if (metaType == QMetaType::fromType<QJSValue>()) {
- return QJSValuePrivate::convertToReturnedValue(
- this, *reinterpret_cast<const QJSValue *>(ptr));
- } else if (metaType == QMetaType::fromType<QList<QObject *> >()) {
- // XXX Can this be made more by using Array as a prototype and implementing
- // directly against QList<QObject*>?
- const QList<QObject *> &list = *(const QList<QObject *>*)ptr;
- QV4::ScopedArrayObject a(scope, newArrayObject());
- a->arrayReserve(list.size());
- QV4::ScopedValue v(scope);
- for (int ii = 0; ii < list.size(); ++ii)
- a->arrayPut(ii, (v = QV4::QObjectWrapper::wrap(this, list.at(ii))));
- a->setArrayLengthUnchecked(list.size());
- return a.asReturnedValue();
- } else if (auto flags = metaType.flags(); flags & QMetaType::PointerToQObject) {
- if (flags.testFlag(QMetaType::IsConst))
- return QV4::QObjectWrapper::wrapConst(this, *reinterpret_cast<QObject* const *>(ptr));
- else
- return QV4::QObjectWrapper::wrap(this, *reinterpret_cast<QObject* const *>(ptr));
- } else if (metaType == QMetaType::fromType<QJSPrimitiveValue>()) {
- const QJSPrimitiveValue *primitive = static_cast<const QJSPrimitiveValue *>(ptr);
- switch (primitive->type()) {
- case QJSPrimitiveValue::Boolean:
- return Encode(primitive->asBoolean());
- case QJSPrimitiveValue::Integer:
- return Encode(primitive->asInteger());
- case QJSPrimitiveValue::String:
- return newString(primitive->asString())->asReturnedValue();
- case QJSPrimitiveValue::Undefined:
- return Encode::undefined();
- case QJSPrimitiveValue::Null:
- return Encode::null();
- case QJSPrimitiveValue::Double:
- return Encode(primitive->asDouble());
- }
+ QV4::Scope scope(this);
+ if (metaType == QMetaType::fromType<QQmlListReference>()) {
+ typedef QQmlListReferencePrivate QDLRP;
+ QDLRP *p = QDLRP::get((QQmlListReference*)const_cast<void *>(ptr));
+ if (p->object)
+ return QV4::QmlListWrapper::create(scope.engine, p->property, p->propertyType);
+ else
+ return QV4::Encode::null();
+ } else if (auto flags = metaType.flags(); flags & QMetaType::IsQmlList) {
+ // casting to QQmlListProperty<QObject> is slightly nasty, but it's the
+ // same QQmlListReference does.
+ const auto *p = static_cast<const QQmlListProperty<QObject> *>(ptr);
+ if (p->object)
+ return QV4::QmlListWrapper::create(scope.engine, *p, metaType);
+ else
+ return QV4::Encode::null();
+ } else if (metaType == QMetaType::fromType<QJSValue>()) {
+ return QJSValuePrivate::convertToReturnedValue(
+ this, *reinterpret_cast<const QJSValue *>(ptr));
+ } else if (metaType == QMetaType::fromType<QList<QObject *> >()) {
+ // XXX Can this be made more by using Array as a prototype and implementing
+ // directly against QList<QObject*>?
+ const QList<QObject *> &list = *(const QList<QObject *>*)ptr;
+ QV4::ScopedArrayObject a(scope, newArrayObject());
+ a->arrayReserve(list.size());
+ QV4::ScopedValue v(scope);
+ for (int ii = 0; ii < list.size(); ++ii)
+ a->arrayPut(ii, (v = QV4::QObjectWrapper::wrap(this, list.at(ii))));
+ a->setArrayLengthUnchecked(list.size());
+ return a.asReturnedValue();
+ } else if (auto flags = metaType.flags(); flags & QMetaType::PointerToQObject) {
+ if (flags.testFlag(QMetaType::IsConst))
+ return QV4::QObjectWrapper::wrapConst(this, *reinterpret_cast<QObject* const *>(ptr));
+ else
+ return QV4::QObjectWrapper::wrap(this, *reinterpret_cast<QObject* const *>(ptr));
+ } else if (metaType == QMetaType::fromType<QJSPrimitiveValue>()) {
+ const QJSPrimitiveValue *primitive = static_cast<const QJSPrimitiveValue *>(ptr);
+ switch (primitive->type()) {
+ case QJSPrimitiveValue::Boolean:
+ return Encode(primitive->asBoolean());
+ case QJSPrimitiveValue::Integer:
+ return Encode(primitive->asInteger());
+ case QJSPrimitiveValue::String:
+ return newString(primitive->asString())->asReturnedValue();
+ case QJSPrimitiveValue::Undefined:
+ return Encode::undefined();
+ case QJSPrimitiveValue::Null:
+ return Encode::null();
+ case QJSPrimitiveValue::Double:
+ return Encode(primitive->asDouble());
}
+ }
- QV4::Scoped<Sequence> sequence(scope);
+ if (const QMetaObject *vtmo = QQmlMetaType::metaObjectForValueType(metaType)) {
if (container) {
- sequence = QV4::SequencePrototype::newSequence(
- this, metaType, ptr,
- container, property, Heap::ReferenceObject::Flags(flags));
+ return QV4::QQmlValueTypeWrapper::create(
+ this, ptr, vtmo, metaType,
+ container, property, Heap::ReferenceObject::Flags(flags));
} else {
- sequence = QV4::SequencePrototype::fromData(this, metaType, ptr);
+ return QV4::QQmlValueTypeWrapper::create(this, ptr, vtmo, metaType);
}
- if (!sequence->isUndefined())
- return sequence->asReturnedValue();
+ }
- if (QMetaType::canConvert(metaType, QMetaType::fromType<QSequentialIterable>())) {
- QSequentialIterable lst;
- QMetaType::convert(metaType, ptr, QMetaType::fromType<QSequentialIterable>(), &lst);
- return sequentialIterableToJS(this, lst);
- }
+ const QQmlType listType = QQmlMetaType::qmlListType(metaType);
+ if (listType.isSequentialContainer())
+ return createSequence(listType.listMetaSequence());
- if (const QMetaObject *vtmo = QQmlMetaType::metaObjectForValueType(metaType)) {
- if (container) {
- return QV4::QQmlValueTypeWrapper::create(
- this, ptr, vtmo, metaType,
- container, property, Heap::ReferenceObject::Flags(flags));
- } else {
- return QV4::QQmlValueTypeWrapper::create(this, ptr, vtmo, metaType);
- }
- }
- }
+ QSequentialIterable iterable;
+ if (QMetaType::convert(metaType, ptr, QMetaType::fromType<QSequentialIterable>(), &iterable)) {
- // XXX TODO: To be compatible, we still need to handle:
- // + QObjectList
- // + QList<int>
+ // If the resulting iterable is useful for anything, turn it into a QV4::Sequence.
+ const QMetaSequence sequence = iterable.metaContainer();
+ if (sequence.hasSize() && sequence.canGetValueAtIndex())
+ return createSequence(sequence);
- // Enumeration types can just be treated as integers for now
- if (metaType.flags() & QMetaType::IsEnumeration)
- return QV4::Encode(*reinterpret_cast<const int *>(ptr));
+ // As a last resort, try to read the contents of the container via an iterator
+ // and build a JS array from them.
+ if (sequence.hasConstIterator() && sequence.canGetValueAtConstIterator()) {
+ QV4::ScopedArrayObject a(scope, newArrayObject());
+ for (auto it = iterable.constBegin(), end = iterable.constEnd(); it != end; ++it)
+ a->push_back(fromVariant(*it));
+ return a.asReturnedValue();
+ }
+ }
return QV4::Encode(newVariantObject(metaType, ptr));
}
@@ -1948,40 +1977,10 @@ ReturnedValue ExecutionEngine::fromVariant(
QVariantMap ExecutionEngine::variantMapFromJS(const Object *o)
{
- return objectToVariant(o).toMap();
-}
-
-
-// Converts a QVariantList to JS.
-// The result is a new Array object with length equal to the length
-// of the QVariantList, and the elements being the QVariantList's
-// elements converted to JS, recursively.
-static QV4::ReturnedValue variantListToJS(QV4::ExecutionEngine *v4, const QVariantList &lst)
-{
- QV4::Scope scope(v4);
- QV4::ScopedArrayObject a(scope, v4->newArrayObject());
- a->arrayReserve(lst.size());
- QV4::ScopedValue v(scope);
- for (int i = 0; i < lst.size(); i++)
- a->arrayPut(i, (v = variantToJS(v4, lst.at(i))));
- a->setArrayLengthUnchecked(lst.size());
- return a.asReturnedValue();
-}
-
-// Converts a QSequentialIterable to JS.
-// The result is a new Array object with length equal to the length
-// of the QSequentialIterable, and the elements being the QSequentialIterable's
-// elements converted to JS, recursively.
-static QV4::ReturnedValue sequentialIterableToJS(QV4::ExecutionEngine *v4, const QSequentialIterable &lst)
-{
- QV4::Scope scope(v4);
- QV4::ScopedArrayObject a(scope, v4->newArrayObject());
- a->arrayReserve(lst.size());
- QV4::ScopedValue v(scope);
- for (int i = 0; i < lst.size(); i++)
- a->arrayPut(i, (v = variantToJS(v4, lst.at(i))));
- a->setArrayLengthUnchecked(lst.size());
- return a.asReturnedValue();
+ Q_ASSERT(o);
+ V4ObjectSet visitedObjects;
+ visitedObjects.insert(o->d());
+ return objectToVariantMap(o, &visitedObjects, JSToQVariantConversionBehavior::Safish);
}
// Converts a QVariantMap to JS.
@@ -2071,10 +2070,10 @@ QQmlRefPointer<ExecutableCompilationUnit> ExecutionEngine::compileModule(const Q
: QQmlMetaType::RequireFullyTyped,
&cacheError)
: nullptr) {
- return ExecutableCompilationUnit::create(
- QV4::CompiledData::CompilationUnit(
- cachedUnit->qmlData, cachedUnit->aotCompiledFunctions,
- url.fileName(), url.toString()));
+ return executableCompilationUnit(
+ QQml::makeRefPointer<QV4::CompiledData::CompilationUnit>(
+ cachedUnit->qmlData, cachedUnit->aotCompiledFunctions, url.fileName(),
+ url.toString()));
}
QFile f(QQmlFile::urlToLocalFileOrQrc(url));
@@ -2108,21 +2107,58 @@ QQmlRefPointer<ExecutableCompilationUnit> ExecutionEngine::compileModule(
}
}
- return ExecutableCompilationUnit::create(std::move(unit));
+ return insertCompilationUnit(std::move(unit));
}
-void ExecutionEngine::injectCompiledModule(const QQmlRefPointer<ExecutableCompilationUnit> &moduleUnit)
+QQmlRefPointer<ExecutableCompilationUnit> ExecutionEngine::compilationUnitForUrl(const QUrl &url) const
{
- // Injection can happen from the QML type loader thread for example, but instantiation and
- // evaluation must be limited to the ExecutionEngine's thread.
- QMutexLocker moduleGuard(&moduleMutex);
- modules.insert(moduleUnit->finalUrl(), moduleUnit);
+ // Gives the _most recently inserted_ CU of that URL. That's what we want.
+ return m_compilationUnits.value(url);
+}
+
+QQmlRefPointer<ExecutableCompilationUnit> ExecutionEngine::executableCompilationUnit(
+ QQmlRefPointer<CompiledData::CompilationUnit> &&unit)
+{
+ const QUrl url = unit->finalUrl();
+ auto [begin, end] = std::as_const(m_compilationUnits).equal_range(url);
+
+ for (auto it = begin; it != end; ++it) {
+ if ((*it)->baseCompilationUnit() == unit)
+ return *it;
+ }
+
+ auto executableUnit = m_compilationUnits.insert(
+ url, ExecutableCompilationUnit::create(std::move(unit), this));
+ // runtime data should not be initialized yet, so we don't need to mark the CU
+ Q_ASSERT(!(*executableUnit)->runtimeStrings);
+ return *executableUnit;
+}
+
+QQmlRefPointer<ExecutableCompilationUnit> ExecutionEngine::insertCompilationUnit(QQmlRefPointer<CompiledData::CompilationUnit> &&unit) {
+ QUrl url = unit->finalUrl();
+ auto executableUnit = ExecutableCompilationUnit::create(std::move(unit), this);
+ /* Compilation Units stored in the engine are part of the gc roots,
+ so we don't trigger any write-barrier when they are added. Use
+ markCustom to make sure they are still marked when we insert them */
+ QV4::WriteBarrier::markCustom(this, [&executableUnit](QV4::MarkStack *ms) {
+ executableUnit->markObjects(ms);
+ });
+ return *m_compilationUnits.insert(std::move(url), std::move(executableUnit));
+}
+
+void ExecutionEngine::trimCompilationUnits()
+{
+ for (auto it = m_compilationUnits.begin(); it != m_compilationUnits.end();) {
+ if ((*it)->count() == 1)
+ it = m_compilationUnits.erase(it);
+ else
+ ++it;
+ }
}
ExecutionEngine::Module ExecutionEngine::moduleForUrl(
const QUrl &url, const ExecutableCompilationUnit *referrer) const
{
- QMutexLocker moduleGuard(&moduleMutex);
const auto nativeModule = nativeModules.find(url);
if (nativeModule != nativeModules.end())
return Module { nullptr, *nativeModule };
@@ -2130,47 +2166,45 @@ ExecutionEngine::Module ExecutionEngine::moduleForUrl(
const QUrl resolved = referrer
? referrer->finalUrl().resolved(QQmlTypeLoader::normalize(url))
: QQmlTypeLoader::normalize(url);
- auto existingModule = modules.find(resolved);
- if (existingModule == modules.end())
+ auto existingModule = m_compilationUnits.find(resolved);
+ if (existingModule == m_compilationUnits.end())
return Module { nullptr, nullptr };
return Module { *existingModule, nullptr };
}
ExecutionEngine::Module ExecutionEngine::loadModule(const QUrl &url, const ExecutableCompilationUnit *referrer)
{
- QMutexLocker moduleGuard(&moduleMutex);
- const auto nativeModule = nativeModules.find(url);
- if (nativeModule != nativeModules.end())
+ const auto nativeModule = nativeModules.constFind(url);
+ if (nativeModule != nativeModules.cend())
return Module { nullptr, *nativeModule };
const QUrl resolved = referrer
? referrer->finalUrl().resolved(QQmlTypeLoader::normalize(url))
: QQmlTypeLoader::normalize(url);
- auto existingModule = modules.find(resolved);
- if (existingModule != modules.end())
+ auto existingModule = m_compilationUnits.constFind(resolved);
+ if (existingModule != m_compilationUnits.cend())
return Module { *existingModule, nullptr };
- moduleGuard.unlock();
-
auto newModule = compileModule(resolved);
- if (newModule) {
- moduleGuard.relock();
- modules.insert(resolved, newModule);
- }
+ Q_ASSERT(!newModule || m_compilationUnits.contains(resolved, newModule));
return Module { newModule, nullptr };
}
QV4::Value *ExecutionEngine::registerNativeModule(const QUrl &url, const QV4::Value &module)
{
- QMutexLocker moduleGuard(&moduleMutex);
- const auto existingModule = nativeModules.find(url);
- if (existingModule != nativeModules.end())
+ const auto existingModule = nativeModules.constFind(url);
+ if (existingModule != nativeModules.cend())
return nullptr;
QV4::Value *val = this->memoryManager->m_persistentValues->allocate();
*val = module.asReturnedValue();
nativeModules.insert(url, val);
+
+ // Make sure the type loader doesn't try to resolve the script anymore.
+ if (m_qmlEngine)
+ QQmlEnginePrivate::get(m_qmlEngine)->typeLoader.injectScript(url, *val);
+
return val;
}
@@ -2437,9 +2471,30 @@ void ExecutionEngine::setExtensionData(int index, Deletable *data)
m_extensionData[index] = data;
}
-// Converts a JS value to a meta-type.
-// data must point to a place that can store a value of the given type.
-// Returns true if conversion succeeded, false otherwise.
+template<typename Source>
+bool convertToIterable(QMetaType metaType, void *data, Source *sequence)
+{
+ QSequentialIterable iterable;
+ if (!QMetaType::view(metaType, data, QMetaType::fromType<QSequentialIterable>(), &iterable))
+ return false;
+
+ const QMetaType elementMetaType = iterable.valueMetaType();
+ for (qsizetype i = 0, end = sequence->getLength(); i < end; ++i) {
+ QVariant element(elementMetaType);
+ ExecutionEngine::metaTypeFromJS(sequence->get(i), elementMetaType, element.data());
+ iterable.addValue(element, QSequentialIterable::AtEnd);
+ }
+ return true;
+}
+
+/*!
+ * \internal
+ *
+ * Converts a JS value to a meta-type.
+ * \a data must point to a default-constructed instance of \a metaType.
+ * Returns \c true if conversion succeeded, \c false otherwise. In the latter case,
+ * \a data is not modified.
+ */
bool ExecutionEngine::metaTypeFromJS(const Value &value, QMetaType metaType, void *data)
{
// check if it's one of the types we know
@@ -2453,6 +2508,12 @@ bool ExecutionEngine::metaTypeFromJS(const Value &value, QMetaType metaType, voi
case QMetaType::UInt:
*reinterpret_cast<uint*>(data) = value.toUInt32();
return true;
+ case QMetaType::Long:
+ *reinterpret_cast<long*>(data) = long(value.toInteger());
+ return true;
+ case QMetaType::ULong:
+ *reinterpret_cast<ulong*>(data) = ulong(value.toInteger());
+ return true;
case QMetaType::LongLong:
*reinterpret_cast<qlonglong*>(data) = qlonglong(value.toInteger());
return true;
@@ -2471,12 +2532,24 @@ bool ExecutionEngine::metaTypeFromJS(const Value &value, QMetaType metaType, voi
*reinterpret_cast<QString*>(data) = value.toQString();
return true;
case QMetaType::QByteArray:
- if (const ArrayBuffer *ab = value.as<ArrayBuffer>())
+ if (const ArrayBuffer *ab = value.as<ArrayBuffer>()) {
*reinterpret_cast<QByteArray*>(data) = ab->asByteArray();
- else if (const String *string = value.as<String>())
+ } else if (const String *string = value.as<String>()) {
*reinterpret_cast<QByteArray*>(data) = string->toQString().toUtf8();
- else
+ } else if (const ArrayObject *ao = value.as<ArrayObject>()) {
+ // Since QByteArray is sequentially iterable, we have to construct it from a JS Array.
+ QByteArray result;
+ const qint64 length = ao->getLength();
+ result.reserve(length);
+ for (qint64 i = 0; i < length; ++i) {
+ char value = 0;
+ ExecutionEngine::metaTypeFromJS(ao->get(i), QMetaType::fromType<char>(), &value);
+ result.push_back(value);
+ }
+ *reinterpret_cast<QByteArray*>(data) = std::move(result);
+ } else {
*reinterpret_cast<QByteArray*>(data) = QByteArray();
+ }
return true;
case QMetaType::Float:
*reinterpret_cast<float*>(data) = value.toNumber();
@@ -2493,6 +2566,9 @@ bool ExecutionEngine::metaTypeFromJS(const Value &value, QMetaType metaType, voi
case QMetaType::UChar:
*reinterpret_cast<unsigned char*>(data) = (unsigned char)(value.toInt32());
return true;
+ case QMetaType::SChar:
+ *reinterpret_cast<signed char*>(data) = (signed char)(value.toInt32());
+ return true;
case QMetaType::QChar:
if (String *s = value.stringValue()) {
QString str = s->toQString();
@@ -2562,7 +2638,8 @@ bool ExecutionEngine::metaTypeFromJS(const Value &value, QMetaType metaType, voi
const QV4::ArrayObject *a = value.as<QV4::ArrayObject>();
if (a) {
*reinterpret_cast<QVariantList *>(data) = ExecutionEngine::toVariant(
- *a, /*typeHint*/QMetaType{}, /*createJSValueForObjects*/false).toList();
+ *a, /*typeHint*/QMetaType{}, /*createJSValueForObjectsAndSymbols*/false)
+ .toList();
return true;
}
break;
@@ -2578,7 +2655,7 @@ bool ExecutionEngine::metaTypeFromJS(const Value &value, QMetaType metaType, voi
case QMetaType::QVariant:
if (value.as<QV4::Managed>()) {
*reinterpret_cast<QVariant*>(data) = ExecutionEngine::toVariant(
- value, /*typeHint*/QMetaType{}, /*createJSValueForObjects*/false);
+ value, /*typeHint*/QMetaType{}, /*createJSValueForObjectsAndSymbols*/false);
} else if (value.isNull()) {
*reinterpret_cast<QVariant*>(data) = QVariant::fromValue(nullptr);
} else if (value.isUndefined()) {
@@ -2606,15 +2683,6 @@ bool ExecutionEngine::metaTypeFromJS(const Value &value, QMetaType metaType, voi
}
break;
}
-#if QT_CONFIG(qml_locale)
- case QMetaType::QLocale: {
- if (const QV4::QQmlLocaleData *l = value.as<QQmlLocaleData>()) {
- *reinterpret_cast<QLocale *>(data) = *l->d()->locale;
- return true;
- }
- break;
- }
-#endif
default:
break;
}
@@ -2624,19 +2692,40 @@ bool ExecutionEngine::metaTypeFromJS(const Value &value, QMetaType metaType, voi
return true;
}
- if (metaType == QMetaType::fromType<QQmlListReference>()) {
- if (const QV4::QmlListWrapper *wrapper = value.as<QV4::QmlListWrapper>()) {
+ if (const QV4::QmlListWrapper *wrapper = value.as<QV4::QmlListWrapper>()) {
+ if (metaType == QMetaType::fromType<QQmlListReference>()) {
*reinterpret_cast<QQmlListReference *>(data) = wrapper->toListReference();
return true;
}
+
+ const auto wrapperPrivate = wrapper->d();
+ if (wrapperPrivate->propertyType() == metaType) {
+ *reinterpret_cast<QQmlListProperty<QObject> *>(data) = *wrapperPrivate->property();
+ return true;
+ }
}
if (const QQmlValueTypeWrapper *vtw = value.as<QQmlValueTypeWrapper>()) {
const QMetaType valueType = vtw->type();
if (valueType == metaType)
return vtw->toGadget(data);
- if (QMetaType::canConvert(valueType, metaType))
- return QMetaType::convert(valueType, vtw->d()->gadgetPtr(), metaType, data);
+
+ Heap::QQmlValueTypeWrapper *d = vtw->d();
+ if (d->isReference())
+ d->readReference();
+
+ if (void *gadgetPtr = d->gadgetPtr()) {
+ if (QQmlValueTypeProvider::populateValueType(metaType, data, valueType, gadgetPtr))
+ return true;
+ if (QMetaType::canConvert(valueType, metaType))
+ return QMetaType::convert(valueType, gadgetPtr, metaType, data);
+ } else {
+ QVariant empty(valueType);
+ if (QQmlValueTypeProvider::populateValueType(metaType, data, valueType, empty.data()))
+ return true;
+ if (QMetaType::canConvert(valueType, metaType))
+ return QMetaType::convert(valueType, empty.data(), metaType, data);
+ }
}
// Try to use magic; for compatibility with qjsvalue_cast.
@@ -2645,43 +2734,62 @@ bool ExecutionEngine::metaTypeFromJS(const Value &value, QMetaType metaType, voi
return true;
const bool isPointer = (metaType.flags() & QMetaType::IsPointer);
- if (value.as<QV4::VariantObject>() && isPointer) {
- const QByteArray pointedToTypeName = QByteArray(metaType.name()).chopped(1);
- const QMetaType valueType = QMetaType::fromName(pointedToTypeName);
- QVariant &var = value.as<QV4::VariantObject>()->d()->data();
- if (valueType == var.metaType()) {
- // We have T t, T* is requested, so return &t.
- *reinterpret_cast<void* *>(data) = var.data();
+ const QV4::VariantObject *variantObject = value.as<QV4::VariantObject>();
+ if (variantObject) {
+ // Actually a reference, because we're poking it for its data() below and we want
+ // the _original_ data, not some copy.
+ QVariant &var = variantObject->d()->data();
+
+ if (var.metaType() == metaType) {
+ metaType.destruct(data);
+ metaType.construct(data, var.data());
return true;
- } else if (Object *o = value.objectValue()) {
- // Look in the prototype chain.
- QV4::Scope scope(o->engine());
- QV4::ScopedObject proto(scope, o->getPrototypeOf());
- while (proto) {
- bool canCast = false;
- if (QV4::VariantObject *vo = proto->as<QV4::VariantObject>()) {
- const QVariant &v = vo->d()->data();
- canCast = (metaType == v.metaType());
- }
- else if (proto->as<QV4::QObjectWrapper>()) {
- QV4::ScopedObject p(scope, proto.getPointer());
- if (QObject *qobject = qtObjectFromJS(p)) {
- if (const QMetaObject *metaObject = metaType.metaObject())
- canCast = metaObject->cast(qobject) != nullptr;
- else
- canCast = qobject->qt_metacast(pointedToTypeName);
+ }
+
+ if (isPointer) {
+ const QByteArray pointedToTypeName = QByteArray(metaType.name()).chopped(1);
+ const QMetaType valueType = QMetaType::fromName(pointedToTypeName);
+
+ if (valueType == var.metaType()) {
+ // ### Qt7: Remove this. Returning pointers to potentially gc'd data is crazy.
+ // We have T t, T* is requested, so return &t.
+ *reinterpret_cast<const void **>(data) = var.data();
+ return true;
+ } else if (Object *o = value.objectValue()) {
+ // Look in the prototype chain.
+ QV4::Scope scope(o->engine());
+ QV4::ScopedObject proto(scope, o->getPrototypeOf());
+ while (proto) {
+ bool canCast = false;
+ if (QV4::VariantObject *vo = proto->as<QV4::VariantObject>()) {
+ const QVariant &v = vo->d()->data();
+ canCast = (metaType == v.metaType());
}
+ else if (proto->as<QV4::QObjectWrapper>()) {
+ QV4::ScopedObject p(scope, proto.getPointer());
+ if (QObject *qobject = qtObjectFromJS(p)) {
+ if (const QMetaObject *metaObject = metaType.metaObject())
+ canCast = metaObject->cast(qobject) != nullptr;
+ else
+ canCast = qobject->qt_metacast(pointedToTypeName);
+ }
+ }
+ if (canCast) {
+ const QMetaType varType = var.metaType();
+ if (varType.flags() & QMetaType::IsPointer) {
+ *reinterpret_cast<const void **>(data)
+ = *reinterpret_cast<void *const *>(var.data());
+ } else {
+ *reinterpret_cast<const void **>(data) = var.data();
+ }
+ return true;
+ }
+ proto = proto->getPrototypeOf();
}
- if (canCast) {
- const QMetaType varType = var.metaType();
- if (varType.flags() & QMetaType::IsPointer)
- *reinterpret_cast<void* *>(data) = *reinterpret_cast<void* *>(var.data());
- else
- *reinterpret_cast<void* *>(data) = var.data();
- return true;
- }
- proto = proto->getPrototypeOf();
}
+ } else if (QQmlValueTypeProvider::populateValueType(
+ metaType, data, var.metaType(), var.data())) {
+ return true;
}
} else if (value.isNull() && isPointer) {
*reinterpret_cast<void* *>(data) = nullptr;
@@ -2693,7 +2801,7 @@ bool ExecutionEngine::metaTypeFromJS(const Value &value, QMetaType metaType, voi
*reinterpret_cast<QJSPrimitiveValue *>(data) = createPrimitive(&value);
return true;
} else if (!isPointer) {
- if (QQmlValueTypeProvider::createValueType(value, metaType, data))
+ if (QQmlValueTypeProvider::populateValueType(metaType, data, value))
return true;
}
@@ -2704,21 +2812,14 @@ bool ExecutionEngine::metaTypeFromJS(const Value &value, QMetaType metaType, voi
metaType.construct(data, result.constData());
return true;
}
+
+ if (convertToIterable(metaType, data, sequence))
+ return true;
}
if (const QV4::ArrayObject *array = value.as<ArrayObject>()) {
- QSequentialIterable iterable;
- if (QMetaType::view(
- metaType, data, QMetaType::fromType<QSequentialIterable>(), &iterable)) {
- const QMetaType elementMetaType = iterable.valueMetaType();
- QVariant element(elementMetaType);
- for (qsizetype i = 0, end = array->getLength(); i < end; ++i) {
- if (!metaTypeFromJS(array->get(i), elementMetaType, element.data()))
- element = QVariant(elementMetaType);
- iterable.addValue(element, QSequentialIterable::AtEnd);
- }
+ if (convertToIterable(metaType, data, array))
return true;
- }
}
return false;
diff --git a/src/qml/jsruntime/qv4engine_p.h b/src/qml/jsruntime/qv4engine_p.h
index ec1150120d..0958ab3ab5 100644
--- a/src/qml/jsruntime/qv4engine_p.h
+++ b/src/qml/jsruntime/qv4engine_p.h
@@ -14,23 +14,22 @@
// We mean it.
//
-#include "qv4global_p.h"
-#include "qv4context_p.h"
-#include "qv4stackframe_p.h"
#include <private/qintrusivelist_p.h>
-#include "qv4enginebase_p.h"
-#include <private/qqmlrefcount_p.h>
#include <private/qqmldelayedcallqueue_p.h>
-#include <QtCore/qelapsedtimer.h>
-#include <QtCore/qmutex.h>
-#include <QtCore/qset.h>
-#include <QtCore/qprocessordetection.h>
-
-#include "qv4function_p.h"
+#include <private/qqmlrefcount_p.h>
#include <private/qv4compileddata_p.h>
+#include <private/qv4context_p.h>
+#include <private/qv4enginebase_p.h>
#include <private/qv4executablecompilationunit_p.h>
+#include <private/qv4function_p.h>
+#include <private/qv4global_p.h>
#include <private/qv4stacklimits_p.h>
+#include <QtCore/qelapsedtimer.h>
+#include <QtCore/qmutex.h>
+#include <QtCore/qprocessordetection.h>
+#include <QtCore/qset.h>
+
namespace WTF {
class BumpPointerAllocator;
class PageAllocation;
@@ -76,7 +75,7 @@ namespace QV4 { struct QObjectMethod; }
// class MyClass : public QObject {
// Q_OBJECT
// ...
-// Q_INVOKABLE void myMethod(QQmlV4Function*);
+// Q_INVOKABLE void myMethod(QQmlV4FunctionPtr);
// };
// The QQmlV8Function - and consequently the arguments and return value - only remains
// valid during the call. If the return value isn't set within myMethod(), the will return
@@ -110,6 +109,7 @@ class QQmlError;
class QJSEngine;
class QQmlEngine;
class QQmlContextData;
+class QQmlTypeLoader;
namespace QV4 {
namespace Debugging {
@@ -174,6 +174,14 @@ public:
QQmlEngine *qmlEngine() const { return m_qmlEngine; }
QJSEngine *publicEngine;
+ template<typename TypeLoader = QQmlTypeLoader>
+ TypeLoader *typeLoader()
+ {
+ if (m_qmlEngine)
+ return TypeLoader::get(m_qmlEngine);
+ return nullptr;
+ }
+
enum JSObjects {
RootContext,
ScriptContext,
@@ -209,6 +217,7 @@ public:
MapProto,
IntrinsicTypedArrayProto,
ValueTypeProto,
+ TypeWrapperProto,
SignalHandlerProto,
IteratorProto,
ForInIteratorProto,
@@ -334,6 +343,7 @@ public:
Object *valueTypeWrapperPrototype() const { return reinterpret_cast<Object *>(jsObjects + ValueTypeProto); }
Object *signalHandlerPrototype() const { return reinterpret_cast<Object *>(jsObjects + SignalHandlerProto); }
+ Object *typeWrapperPrototype() const { return reinterpret_cast<Object *>(jsObjects + TypeWrapperProto); }
Object *iteratorPrototype() const { return reinterpret_cast<Object *>(jsObjects + IteratorProto); }
Object *forInIteratorPrototype() const { return reinterpret_cast<Object *>(jsObjects + ForInIteratorProto); }
Object *setIteratorPrototype() const { return reinterpret_cast<Object *>(jsObjects + SetIteratorProto); }
@@ -488,8 +498,6 @@ public:
Symbol *symbol_unscopables() const { return reinterpret_cast<Symbol *>(jsSymbols + Symbol_unscopables); }
Symbol *symbol_revokableProxy() const { return reinterpret_cast<Symbol *>(jsSymbols + Symbol_revokableProxy); }
- QIntrusiveList<ExecutableCompilationUnit, &ExecutableCompilationUnit::nextCompilationUnit> compilationUnits;
-
quint32 m_engineId;
RegExpCache *regExpCache;
@@ -536,7 +544,14 @@ public:
void setProfiler(Profiling::Profiler *profiler);
#endif // QT_CONFIG(qml_debug)
- ExecutionContext *currentContext() const { return currentStackFrame->context(); }
+ // We don't want to #include <private/qv4stackframe_p.h> here, but we still want
+ // currentContext() to be inline. Therefore we shift the requirement to provide the
+ // complete type of CppStackFrame to the caller by making this a template.
+ template<typename StackFrame = CppStackFrame>
+ ExecutionContext *currentContext() const
+ {
+ return static_cast<const StackFrame *>(currentStackFrame)->context();
+ }
// ensure we always get odd prototype IDs. This helps make marking in QV4::Lookup fast
quintptr newProtoId() { return (protoIdCount += 2); }
@@ -546,6 +561,7 @@ public:
Heap::Object *newObject();
Heap::Object *newObject(Heap::InternalClass *internalClass);
+ Heap::String *newString(char16_t c) { return newString(QChar(c)); }
Heap::String *newString(const QString &s = QString());
Heap::String *newIdentifier(const QString &text);
@@ -657,7 +673,8 @@ public:
// variant conversions
static QVariant toVariant(
- const QV4::Value &value, QMetaType typeHint, bool createJSValueForObjects = true);
+ const QV4::Value &value, QMetaType typeHint, bool createJSValueForObjectsAndSymbols = true);
+ static QVariant toVariantLossy(const QV4::Value &value);
QV4::ReturnedValue fromVariant(const QVariant &);
QV4::ReturnedValue fromVariant(
const QVariant &variant, Heap::Object *parent, int property, uint flags);
@@ -738,7 +755,21 @@ public:
QQmlRefPointer<ExecutableCompilationUnit> compileModule(
const QUrl &url, const QString &sourceCode, const QDateTime &sourceTimeStamp);
- void injectCompiledModule(const QQmlRefPointer<ExecutableCompilationUnit> &moduleUnit);
+ QQmlRefPointer<ExecutableCompilationUnit> compilationUnitForUrl(const QUrl &url) const;
+
+ QQmlRefPointer<ExecutableCompilationUnit> executableCompilationUnit(
+ QQmlRefPointer<QV4::CompiledData::CompilationUnit> &&unit);
+
+ QQmlRefPointer<ExecutableCompilationUnit> insertCompilationUnit(
+ QQmlRefPointer<QV4::CompiledData::CompilationUnit> &&unit);
+
+ QMultiHash<QUrl, QQmlRefPointer<ExecutableCompilationUnit>> compilationUnits() const
+ {
+ return m_compilationUnits;
+ }
+ void clearCompilationUnits() { m_compilationUnits.clear(); }
+ void trimCompilationUnits();
+
QV4::Value *registerNativeModule(const QUrl &url, const QV4::Value &module);
struct Module {
@@ -854,8 +885,7 @@ private:
QVector<Deletable *> m_extensionData;
- mutable QMutex moduleMutex;
- QHash<QUrl, QQmlRefPointer<ExecutableCompilationUnit>> modules;
+ QMultiHash<QUrl, QQmlRefPointer<ExecutableCompilationUnit>> m_compilationUnits;
// QV4::PersistentValue would be preferred, but using QHash will create copies,
// and QV4::PersistentValue doesn't like creating copies.
diff --git a/src/qml/jsruntime/qv4enginebase_p.h b/src/qml/jsruntime/qv4enginebase_p.h
index 721a024efb..68e906baa1 100644
--- a/src/qml/jsruntime/qv4enginebase_p.h
+++ b/src/qml/jsruntime/qv4enginebase_p.h
@@ -45,7 +45,8 @@ struct Q_QML_EXPORT EngineBase {
quint8 isExecutingInRegExpJIT = false;
quint8 isInitialized = false;
- quint8 padding[2];
+ quint8 inShutdown = false;
+ quint8 isGCOngoing = false; // incremental gc is ongoing (but mutator might be running)
MemoryManager *memoryManager = nullptr;
union {
diff --git a/src/qml/jsruntime/qv4errorobject.cpp b/src/qml/jsruntime/qv4errorobject.cpp
index 516c81864f..02145a0243 100644
--- a/src/qml/jsruntime/qv4errorobject.cpp
+++ b/src/qml/jsruntime/qv4errorobject.cpp
@@ -56,7 +56,7 @@ void Heap::ErrorObject::init(const Value &message, ErrorType t)
e->d()->stackTrace = new StackTrace(scope.engine->stackTrace());
if (!e->d()->stackTrace->isEmpty()) {
setProperty(scope.engine, QV4::ErrorObject::Index_FileName, scope.engine->newString(e->d()->stackTrace->at(0).source));
- setProperty(scope.engine, QV4::ErrorObject::Index_LineNumber, Value::fromInt32(e->d()->stackTrace->at(0).line));
+ setProperty(scope.engine, QV4::ErrorObject::Index_LineNumber, Value::fromInt32(qAbs(e->d()->stackTrace->at(0).line)));
}
if (!message.isUndefined())
@@ -84,7 +84,7 @@ void Heap::ErrorObject::init(const Value &message, const QString &fileName, int
Q_ASSERT(!e->d()->stackTrace->isEmpty());
setProperty(scope.engine, QV4::ErrorObject::Index_FileName, scope.engine->newString(e->d()->stackTrace->at(0).source));
- setProperty(scope.engine, QV4::ErrorObject::Index_LineNumber, Value::fromInt32(e->d()->stackTrace->at(0).line));
+ setProperty(scope.engine, QV4::ErrorObject::Index_LineNumber, Value::fromInt32(qAbs(e->d()->stackTrace->at(0).line)));
if (!message.isUndefined())
setProperty(scope.engine, QV4::ErrorObject::Index_Message, message);
@@ -182,14 +182,14 @@ DEFINE_OBJECT_VTABLE(SyntaxErrorCtor);
DEFINE_OBJECT_VTABLE(TypeErrorCtor);
DEFINE_OBJECT_VTABLE(URIErrorCtor);
-void Heap::ErrorCtor::init(QV4::ExecutionContext *scope)
+void Heap::ErrorCtor::init(QV4::ExecutionEngine *engine)
{
- Heap::FunctionObject::init(scope, QStringLiteral("Error"));
+ Heap::FunctionObject::init(engine, QStringLiteral("Error"));
}
-void Heap::ErrorCtor::init(QV4::ExecutionContext *scope, const QString &name)
+void Heap::ErrorCtor::init(QV4::ExecutionEngine *engine, const QString &name)
{
- Heap::FunctionObject::init(scope, name);
+ Heap::FunctionObject::init(engine, name);
}
ReturnedValue ErrorCtor::virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *newTarget)
@@ -203,9 +203,9 @@ ReturnedValue ErrorCtor::virtualCall(const FunctionObject *f, const Value *, con
return f->callAsConstructor(argv, argc);
}
-void Heap::EvalErrorCtor::init(QV4::ExecutionContext *scope)
+void Heap::EvalErrorCtor::init(QV4::ExecutionEngine *engine)
{
- Heap::FunctionObject::init(scope, QStringLiteral("EvalError"));
+ Heap::FunctionObject::init(engine, QStringLiteral("EvalError"));
}
ReturnedValue EvalErrorCtor::virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *newTarget)
@@ -214,9 +214,9 @@ ReturnedValue EvalErrorCtor::virtualCallAsConstructor(const FunctionObject *f, c
return ErrorObject::create<EvalErrorObject>(f->engine(), v, newTarget)->asReturnedValue();
}
-void Heap::RangeErrorCtor::init(QV4::ExecutionContext *scope)
+void Heap::RangeErrorCtor::init(QV4::ExecutionEngine *engine)
{
- Heap::FunctionObject::init(scope, QStringLiteral("RangeError"));
+ Heap::FunctionObject::init(engine, QStringLiteral("RangeError"));
}
ReturnedValue RangeErrorCtor::virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *newTarget)
@@ -225,9 +225,9 @@ ReturnedValue RangeErrorCtor::virtualCallAsConstructor(const FunctionObject *f,
return ErrorObject::create<RangeErrorObject>(f->engine(), v, newTarget)->asReturnedValue();
}
-void Heap::ReferenceErrorCtor::init(QV4::ExecutionContext *scope)
+void Heap::ReferenceErrorCtor::init(QV4::ExecutionEngine *engine)
{
- Heap::FunctionObject::init(scope, QStringLiteral("ReferenceError"));
+ Heap::FunctionObject::init(engine, QStringLiteral("ReferenceError"));
}
ReturnedValue ReferenceErrorCtor::virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *newTarget)
@@ -236,9 +236,9 @@ ReturnedValue ReferenceErrorCtor::virtualCallAsConstructor(const FunctionObject
return ErrorObject::create<ReferenceErrorObject>(f->engine(), v, newTarget)->asReturnedValue();
}
-void Heap::SyntaxErrorCtor::init(QV4::ExecutionContext *scope)
+void Heap::SyntaxErrorCtor::init(QV4::ExecutionEngine *engine)
{
- Heap::FunctionObject::init(scope, QStringLiteral("SyntaxError"));
+ Heap::FunctionObject::init(engine, QStringLiteral("SyntaxError"));
}
ReturnedValue SyntaxErrorCtor::virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *newTarget)
@@ -247,9 +247,9 @@ ReturnedValue SyntaxErrorCtor::virtualCallAsConstructor(const FunctionObject *f,
return ErrorObject::create<SyntaxErrorObject>(f->engine(), v, newTarget)->asReturnedValue();
}
-void Heap::TypeErrorCtor::init(QV4::ExecutionContext *scope)
+void Heap::TypeErrorCtor::init(QV4::ExecutionEngine *engine)
{
- Heap::FunctionObject::init(scope, QStringLiteral("TypeError"));
+ Heap::FunctionObject::init(engine, QStringLiteral("TypeError"));
}
ReturnedValue TypeErrorCtor::virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *newTarget)
@@ -258,9 +258,9 @@ ReturnedValue TypeErrorCtor::virtualCallAsConstructor(const FunctionObject *f, c
return ErrorObject::create<TypeErrorObject>(f->engine(), v, newTarget)->asReturnedValue();
}
-void Heap::URIErrorCtor::init(QV4::ExecutionContext *scope)
+void Heap::URIErrorCtor::init(QV4::ExecutionEngine *engine)
{
- Heap::FunctionObject::init(scope, QStringLiteral("URIError"));
+ Heap::FunctionObject::init(engine, QStringLiteral("URIError"));
}
ReturnedValue URIErrorCtor::virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *newTarget)
diff --git a/src/qml/jsruntime/qv4errorobject_p.h b/src/qml/jsruntime/qv4errorobject_p.h
index 541f5cae36..f9adbb443b 100644
--- a/src/qml/jsruntime/qv4errorobject_p.h
+++ b/src/qml/jsruntime/qv4errorobject_p.h
@@ -79,32 +79,32 @@ struct URIErrorObject : ErrorObject {
};
struct ErrorCtor : FunctionObject {
- void init(QV4::ExecutionContext *scope);
- void init(QV4::ExecutionContext *scope, const QString &name);
+ void init(ExecutionEngine *engine);
+ void init(ExecutionEngine *engine, const QString &name);
};
struct EvalErrorCtor : ErrorCtor {
- void init(QV4::ExecutionContext *scope);
+ void init(ExecutionEngine *engine);
};
struct RangeErrorCtor : ErrorCtor {
- void init(QV4::ExecutionContext *scope);
+ void init(ExecutionEngine *engine);
};
struct ReferenceErrorCtor : ErrorCtor {
- void init(QV4::ExecutionContext *scope);
+ void init(ExecutionEngine *engine);
};
struct SyntaxErrorCtor : ErrorCtor {
- void init(QV4::ExecutionContext *scope);
+ void init(ExecutionEngine *engine);
};
struct TypeErrorCtor : ErrorCtor {
- void init(QV4::ExecutionContext *scope);
+ void init(ExecutionEngine *engine);
};
struct URIErrorCtor : ErrorCtor {
- void init(QV4::ExecutionContext *scope);
+ void init(ExecutionEngine *engine);
};
}
diff --git a/src/qml/jsruntime/qv4estable.cpp b/src/qml/jsruntime/qv4estable.cpp
index ebd62db1b5..fb36b10728 100644
--- a/src/qml/jsruntime/qv4estable.cpp
+++ b/src/qml/jsruntime/qv4estable.cpp
@@ -111,21 +111,18 @@ ReturnedValue ESTable::get(const Value &key, bool *hasValue) const
// Removes the given \a key from the table
bool ESTable::remove(const Value &key)
{
- bool found = false;
- uint idx = 0;
- for (; idx < m_size; ++idx) {
- if (m_keys[idx].sameValueZero(key)) {
- found = true;
- break;
+ for (uint index = 0; index < m_size; ++index) {
+ if (m_keys[index].sameValueZero(key)) {
+ // Remove the element at |index| by moving all elements to the right
+ // of |index| one place to the left.
+ size_t count = (m_size - (index + 1)) * sizeof(Value);
+ memmove(m_keys + index, m_keys + index + 1, count);
+ memmove(m_values + index, m_values + index + 1, count);
+ m_size--;
+ return true;
}
}
-
- if (found == true) {
- memmove(m_keys + idx, m_keys + idx + 1, (m_size - idx)*sizeof(Value));
- memmove(m_values + idx, m_values + idx + 1, (m_size - idx)*sizeof(Value));
- m_size--;
- }
- return found;
+ return false;
}
// Returns the size of the table. Note that the size may not match the underlying allocation.
diff --git a/src/qml/jsruntime/qv4estable_p.h b/src/qml/jsruntime/qv4estable_p.h
index 25fbf0c372..f0c5c7cb81 100644
--- a/src/qml/jsruntime/qv4estable_p.h
+++ b/src/qml/jsruntime/qv4estable_p.h
@@ -17,12 +17,13 @@
#include "qv4value_p.h"
+class tst_qv4estable;
+
QT_BEGIN_NAMESPACE
-namespace QV4
-{
+namespace QV4 {
-class ESTable
+class Q_AUTOTEST_EXPORT ESTable
{
public:
ESTable();
@@ -40,13 +41,15 @@ public:
void removeUnmarkedKeys();
private:
+ friend class ::tst_qv4estable;
+
Value *m_keys = nullptr;
Value *m_values = nullptr;
uint m_size = 0;
uint m_capacity = 0;
};
-}
+} // namespace QV4
QT_END_NAMESPACE
diff --git a/src/qml/jsruntime/qv4executableallocator_p.h b/src/qml/jsruntime/qv4executableallocator_p.h
index 2f97c9ae6e..8181bf17ae 100644
--- a/src/qml/jsruntime/qv4executableallocator_p.h
+++ b/src/qml/jsruntime/qv4executableallocator_p.h
@@ -100,7 +100,7 @@ public:
private:
QMultiMap<size_t, Allocation*> freeAllocations;
QMap<quintptr, ChunkOfPages*> chunks;
- mutable QRecursiveMutex mutex;
+ mutable QMutex mutex;
};
}
diff --git a/src/qml/jsruntime/qv4executablecompilationunit.cpp b/src/qml/jsruntime/qv4executablecompilationunit.cpp
index 898f911334..34d737cdae 100644
--- a/src/qml/jsruntime/qv4executablecompilationunit.cpp
+++ b/src/qml/jsruntime/qv4executablecompilationunit.cpp
@@ -17,36 +17,14 @@
#include <private/qqmlscriptdata_p.h>
#include <private/qv4module_p.h>
#include <private/qv4compilationunitmapper_p.h>
-#include <private/qml_compile_hash_p.h>
#include <private/qqmltypewrapper_p.h>
-#include <private/inlinecomponentutils_p.h>
#include <private/qv4resolvedtypereference_p.h>
#include <private/qv4objectiterator_p.h>
-#include <QtQml/qqmlfile.h>
#include <QtQml/qqmlpropertymap.h>
-#include <QtCore/qdir.h>
-#include <QtCore/qstandardpaths.h>
#include <QtCore/qfileinfo.h>
-#include <QtCore/qscopeguard.h>
#include <QtCore/qcryptographichash.h>
-#include <QtCore/QScopedValueRollback>
-
-static_assert(QV4::CompiledData::QmlCompileHashSpace > QML_COMPILE_HASH_LENGTH);
-
-#if defined(QML_COMPILE_HASH) && defined(QML_COMPILE_HASH_LENGTH) && QML_COMPILE_HASH_LENGTH > 0
-# ifdef Q_OS_LINUX
-// Place on a separate section on Linux so it's easier to check from outside
-// what the hash version is.
-__attribute__((section(".qml_compile_hash")))
-# endif
-const char qml_compile_hash[QV4::CompiledData::QmlCompileHashSpace] = QML_COMPILE_HASH;
-static_assert(sizeof(QV4::CompiledData::Unit::libraryVersionHash) > QML_COMPILE_HASH_LENGTH,
- "Compile hash length exceeds reserved size in data structure. Please adjust and bump the format version");
-#else
-# error "QML_COMPILE_HASH must be defined for the build of QtDeclarative to ensure version checking for cache files"
-#endif
QT_BEGIN_NAMESPACE
@@ -55,28 +33,16 @@ namespace QV4 {
ExecutableCompilationUnit::ExecutableCompilationUnit() = default;
ExecutableCompilationUnit::ExecutableCompilationUnit(
- CompiledData::CompilationUnit &&compilationUnit)
- : CompiledData::CompilationUnit(std::move(compilationUnit))
-{}
-
-ExecutableCompilationUnit::~ExecutableCompilationUnit()
+ QQmlRefPointer<CompiledData::CompilationUnit> &&compilationUnit)
+ : m_compilationUnit(std::move(compilationUnit))
{
- unlink();
+ constants = m_compilationUnit->constants;
}
-QString ExecutableCompilationUnit::localCacheFilePath(const QUrl &url)
+ExecutableCompilationUnit::~ExecutableCompilationUnit()
{
- static const QByteArray envCachePath = qgetenv("QML_DISK_CACHE_PATH");
-
- const QString localSourcePath = QQmlFile::urlToLocalFileOrQrc(url);
- const QString cacheFileSuffix = QFileInfo(localSourcePath + QLatin1Char('c')).completeSuffix();
- QCryptographicHash fileNameHash(QCryptographicHash::Sha1);
- fileNameHash.addData(localSourcePath.toUtf8());
- QString directory = envCachePath.isEmpty()
- ? QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QLatin1String("/qmlcache/")
- : QString::fromLocal8Bit(envCachePath) + QLatin1String("/");
- QDir::root().mkpath(directory);
- return directory + QString::fromUtf8(fileNameHash.result().toHex()) + QLatin1Char('.') + cacheFileSuffix;
+ if (engine)
+ clear();
}
static QString toString(QV4::ReturnedValue v)
@@ -104,25 +70,30 @@ static void dumpConstantTable(const StaticValue *constants, uint count)
}
}
-QV4::Function *ExecutableCompilationUnit::linkToEngine(ExecutionEngine *engine)
+void ExecutableCompilationUnit::populate()
{
- this->engine = engine;
- engine->compilationUnits.insert(this);
+ /* In general, we should use QV4::Scope whenever we allocate heap objects, and employ write barriers
+ for member variables pointing to heap objects. However, ExecutableCompilationUnit is special, as it
+ is always part of the root set. So instead of using scopde allocations and write barriers, we use a
+ slightly different approach: We temporarily block the gc from running. Afterwards, at the end of the
+ function we check whether the gc was already running, and mark the ExecutableCompilationUnit. This
+ ensures that all the newly allocated objects of the compilation unit will be marked in turn.
+ If the gc was not running, we don't have to do anything, because everything will be marked when the
+ gc starts marking the root set at the start of a run.
+ */
+ const CompiledData::Unit *data = m_compilationUnit->data;
+ GCCriticalSection<ExecutableCompilationUnit> criticalSection(engine, this);
Q_ASSERT(!runtimeStrings);
+ Q_ASSERT(engine);
Q_ASSERT(data);
const quint32 stringCount = totalStringCount();
- runtimeStrings = (QV4::Heap::String **)malloc(stringCount * sizeof(QV4::Heap::String*));
- // memset the strings to 0 in case a GC run happens while we're within the loop below
- memset(runtimeStrings, 0, stringCount * sizeof(QV4::Heap::String*));
+ runtimeStrings = (QV4::Heap::String **)calloc(stringCount, sizeof(QV4::Heap::String*));
for (uint i = 0; i < stringCount; ++i)
runtimeStrings[i] = engine->newString(stringAt(i));
runtimeRegularExpressions
= new QV4::Value[data->regexpTableSize];
- // memset the regexps to 0 in case a GC run happens while we're within the loop below
- memset(runtimeRegularExpressions, 0,
- data->regexpTableSize * sizeof(QV4::Value));
for (uint i = 0; i < data->regexpTableSize; ++i) {
const CompiledData::RegExp *re = data->regexpAt(i);
uint f = re->flags();
@@ -155,11 +126,9 @@ QV4::Function *ExecutableCompilationUnit::linkToEngine(ExecutionEngine *engine)
if (data->jsClassTableSize) {
runtimeClasses
- = (QV4::Heap::InternalClass **)malloc(data->jsClassTableSize
- * sizeof(QV4::Heap::InternalClass *));
- // memset the regexps to 0 in case a GC run happens while we're within the loop below
- memset(runtimeClasses, 0,
- data->jsClassTableSize * sizeof(QV4::Heap::InternalClass *));
+ = (QV4::Heap::InternalClass **)calloc(data->jsClassTableSize,
+ sizeof(QV4::Heap::InternalClass *));
+
for (uint i = 0; i < data->jsClassTableSize; ++i) {
int memberCount = 0;
const CompiledData::JSClassMember *member
@@ -182,13 +151,13 @@ QV4::Function *ExecutableCompilationUnit::linkToEngine(ExecutionEngine *engine)
= qEnvironmentVariableIsSet("QV4_FORCE_INTERPRETER")
|| !(engine->diskCacheOptions() & ExecutionEngine::DiskCache::AotNative);
- const QQmlPrivate::TypedFunction *aotFunction
- = ignoreAotCompiledFunctions ? nullptr : aotCompiledFunctions;
+ const QQmlPrivate::AOTCompiledFunction *aotFunction
+ = ignoreAotCompiledFunctions ? nullptr : m_compilationUnit->aotCompiledFunctions;
- auto advanceAotFunction = [&](int i) -> const QQmlPrivate::TypedFunction * {
+ auto advanceAotFunction = [&](int i) -> const QQmlPrivate::AOTCompiledFunction * {
if (aotFunction) {
if (aotFunction->functionPtr) {
- if (aotFunction->extraData == i)
+ if (aotFunction->functionIndex == i)
return aotFunction++;
} else {
aotFunction = nullptr;
@@ -234,15 +203,14 @@ QV4::Function *ExecutableCompilationUnit::linkToEngine(ExecutionEngine *engine)
<< (data->indexOfRootFunction != -1
? data->indexOfRootFunction : 0);
}
-
- if (data->indexOfRootFunction != -1)
- return runtimeFunctions[data->indexOfRootFunction];
- else
- return nullptr;
}
Heap::Object *ExecutableCompilationUnit::templateObjectAt(int index) const
{
+ const CompiledData::Unit *data = m_compilationUnit->data;
+ Q_ASSERT(data);
+ Q_ASSERT(engine);
+
Q_ASSERT(index < int(data->templateObjectTableSize));
if (!templateObjects.size())
templateObjects.resize(data->templateObjectTableSize);
@@ -271,33 +239,17 @@ Heap::Object *ExecutableCompilationUnit::templateObjectAt(int index) const
return templateObjects.at(index);
}
-void ExecutableCompilationUnit::unlink()
+void ExecutableCompilationUnit::clear()
{
- if (engine)
- nextCompilationUnit.remove();
-
- if (isRegistered) {
- Q_ASSERT(data && propertyCaches.count() > 0 && propertyCaches.at(/*root object*/0));
- QQmlMetaType::unregisterInternalCompositeType(this);
- }
-
- propertyCaches.clear();
+ delete [] imports;
+ imports = nullptr;
if (runtimeLookups) {
- for (uint i = 0; i < data->lookupTableSize; ++i)
+ const uint lookupTableSize = unitData()->lookupTableSize;
+ for (uint i = 0; i < lookupTableSize; ++i)
runtimeLookups[i].releasePropertyCache();
}
- dependentScripts.clear();
-
- typeNameCache.reset();
-
- qDeleteAll(resolvedTypes);
- resolvedTypes.clear();
-
- engine = nullptr;
- qmlEngine = nullptr;
-
delete [] runtimeLookups;
runtimeLookups = nullptr;
@@ -313,8 +265,10 @@ void ExecutableCompilationUnit::unlink()
runtimeClasses = nullptr;
}
-void ExecutableCompilationUnit::markObjects(QV4::MarkStack *markStack)
+void ExecutableCompilationUnit::markObjects(QV4::MarkStack *markStack) const
{
+ const CompiledData::Unit *data = m_compilationUnit->data;
+
if (runtimeStrings) {
for (uint i = 0, end = totalStringCount(); i < end; ++i)
if (runtimeStrings[i])
@@ -362,174 +316,29 @@ IdentifierHash ExecutableCompilationUnit::createNamedObjectsPerComponent(int com
return *namedObjectsPerComponentCache.insert(componentObjectIndex, namedObjectCache);
}
-void ExecutableCompilationUnit::finalizeCompositeType(QQmlEnginePrivate *qmlEngine, CompositeMetaTypeIds types)
-{
- this->qmlEngine = qmlEngine;
-
- // Add to type registry of composites
- if (propertyCaches.needsVMEMetaObject(/*root object*/0)) {
- // typeIds is only valid for types that have references to themselves.
- if (!types.isValid())
- types = CompositeMetaTypeIds::fromCompositeName(rootPropertyCache()->className());
- typeIds = types;
- QQmlMetaType::registerInternalCompositeType(this);
-
- } else {
- const QV4::CompiledData::Object *obj = objectAt(/*root object*/0);
- auto *typeRef = resolvedTypes.value(obj->inheritedTypeNameIndex);
- Q_ASSERT(typeRef);
- if (const auto compilationUnit = typeRef->compilationUnit()) {
- typeIds = compilationUnit->typeIds;
- } else {
- const auto type = typeRef->type();
- typeIds = CompositeMetaTypeIds{ type.typeId(), type.qListTypeId() };
- }
- }
-
- // Collect some data for instantiation later.
- using namespace icutils;
- std::vector<QV4::CompiledData::InlineComponent> allICs {};
- for (int i=0; i != objectCount(); ++i) {
- const CompiledObject *obj = objectAt(i);
- for (auto it = obj->inlineComponentsBegin(); it != obj->inlineComponentsEnd(); ++it) {
- allICs.push_back(*it);
- }
- }
- std::vector<Node> nodes;
- nodes.resize(allICs.size());
- std::iota(nodes.begin(), nodes.end(), 0);
- AdjacencyList adjacencyList;
- adjacencyList.resize(nodes.size());
- fillAdjacencyListForInlineComponents(this, adjacencyList, nodes, allICs);
- bool hasCycle = false;
- auto nodesSorted = topoSort(nodes, adjacencyList, hasCycle);
- Q_ASSERT(!hasCycle); // would have already been discovered by qqmlpropertycachcecreator
-
- // We need to first iterate over all inline components, as the containing component might create instances of them
- // and in that case we need to add its object count
- for (auto nodeIt = nodesSorted.rbegin(); nodeIt != nodesSorted.rend(); ++nodeIt) {
- const auto &ic = allICs.at(nodeIt->index());
- int lastICRoot = ic.objectIndex;
- for (int i = ic.objectIndex; i<objectCount(); ++i) {
- const QV4::CompiledData::Object *obj = objectAt(i);
- bool leftCurrentInlineComponent
- = (i != lastICRoot
- && obj->hasFlag(QV4::CompiledData::Object::IsInlineComponentRoot))
- || !obj->hasFlag(QV4::CompiledData::Object::IsPartOfInlineComponent);
- if (leftCurrentInlineComponent)
- break;
- inlineComponentData[lastICRoot].totalBindingCount += obj->nBindings;
-
- if (auto *typeRef = resolvedTypes.value(obj->inheritedTypeNameIndex)) {
- const auto type = typeRef->type();
- if (type.isValid() && type.parserStatusCast() != -1)
- ++inlineComponentData[lastICRoot].totalParserStatusCount;
-
- ++inlineComponentData[lastICRoot].totalObjectCount;
- if (const auto compilationUnit = typeRef->compilationUnit()) {
- // if the type is an inline component type, we have to extract the information from it
- // This requires that inline components are visited in the correct order
- auto icRoot = compilationUnit->icRoot;
- if (type.isInlineComponentType())
- icRoot = type.inlineComponentId();
- QScopedValueRollback<int> rollback {compilationUnit->icRoot, icRoot};
- inlineComponentData[lastICRoot].totalBindingCount += compilationUnit->totalBindingsCount();
- inlineComponentData[lastICRoot].totalParserStatusCount += compilationUnit->totalParserStatusCount();
- inlineComponentData[lastICRoot].totalObjectCount += compilationUnit->totalObjectCount();
- }
- }
- }
- }
- int bindingCount = 0;
- int parserStatusCount = 0;
- int objectCount = 0;
- for (quint32 i = 0, count = this->objectCount(); i < count; ++i) {
- const QV4::CompiledData::Object *obj = objectAt(i);
- if (obj->hasFlag(QV4::CompiledData::Object::IsPartOfInlineComponent))
- continue;
-
- bindingCount += obj->nBindings;
- if (auto *typeRef = resolvedTypes.value(obj->inheritedTypeNameIndex)) {
- const auto type = typeRef->type();
- if (type.isValid() && type.parserStatusCast() != -1)
- ++parserStatusCount;
- ++objectCount;
- if (const auto compilationUnit = typeRef->compilationUnit()) {
- auto icRoot = compilationUnit->icRoot;
- if (type.isInlineComponentType())
- icRoot = type.inlineComponentId();
- QScopedValueRollback<int> rollback {compilationUnit->icRoot, icRoot};
- bindingCount += compilationUnit->totalBindingsCount();
- parserStatusCount += compilationUnit->totalParserStatusCount();
- objectCount += compilationUnit->totalObjectCount();
- }
- }
- }
-
- m_totalBindingsCount = bindingCount;
- m_totalParserStatusCount = parserStatusCount;
- m_totalObjectCount = objectCount;
-}
-
-int ExecutableCompilationUnit::totalBindingsCount() const {
- if (icRoot == -1)
- return m_totalBindingsCount;
- return inlineComponentData[icRoot].totalBindingCount;
-}
-
-int ExecutableCompilationUnit::totalObjectCount() const {
- if (icRoot == -1)
- return m_totalObjectCount;
- return inlineComponentData[icRoot].totalObjectCount;
-}
-
-int ExecutableCompilationUnit::totalParserStatusCount() const {
- if (icRoot == -1)
- return m_totalParserStatusCount;
- return inlineComponentData[icRoot].totalParserStatusCount;
-}
-
-bool ExecutableCompilationUnit::verifyChecksum(const CompiledData::DependentTypesHasher &dependencyHasher) const
+QQmlRefPointer<ExecutableCompilationUnit> ExecutableCompilationUnit::create(
+ QQmlRefPointer<CompiledData::CompilationUnit> &&compilationUnit, ExecutionEngine *engine)
{
- if (!dependencyHasher) {
- for (size_t i = 0; i < sizeof(data->dependencyMD5Checksum); ++i) {
- if (data->dependencyMD5Checksum[i] != 0)
- return false;
- }
- return true;
- }
- const QByteArray checksum = dependencyHasher();
- return checksum.size() == sizeof(data->dependencyMD5Checksum)
- && memcmp(data->dependencyMD5Checksum, checksum.constData(),
- sizeof(data->dependencyMD5Checksum)) == 0;
-}
-
-CompositeMetaTypeIds ExecutableCompilationUnit::typeIdsForComponent(int objectid) const
-{
- if (objectid == 0)
- return typeIds;
- return inlineComponentData[objectid].typeIds;
+ auto result = QQmlRefPointer<ExecutableCompilationUnit>(
+ new ExecutableCompilationUnit(std::move(compilationUnit)),
+ QQmlRefPointer<ExecutableCompilationUnit>::Adopt);
+ result->engine = engine;
+ return result;
}
-QStringList ExecutableCompilationUnit::moduleRequests() const
+Heap::Module *ExecutableCompilationUnit::instantiate()
{
- QStringList requests;
- requests.reserve(data->moduleRequestTableSize);
- for (uint i = 0; i < data->moduleRequestTableSize; ++i)
- requests << stringAt(data->moduleRequestTable()[i]);
- return requests;
-}
+ const CompiledData::Unit *data = m_compilationUnit->data;
-Heap::Module *ExecutableCompilationUnit::instantiate(ExecutionEngine *engine)
-{
if (isESModule() && module())
return module();
if (data->indexOfRootFunction < 0)
return nullptr;
- if (!this->engine)
- linkToEngine(engine);
+ Q_ASSERT(engine);
+ if (!runtimeStrings)
+ populate();
Scope scope(engine);
Scoped<Module> module(scope, engine->memoryManager->allocate<Module>(engine, this));
@@ -537,13 +346,14 @@ Heap::Module *ExecutableCompilationUnit::instantiate(ExecutionEngine *engine)
if (isESModule())
setModule(module->d());
- for (const QString &request: moduleRequests()) {
+ const QStringList moduleRequests = m_compilationUnit->moduleRequests();
+ for (const QString &request: moduleRequests) {
const QUrl url(request);
const auto dependentModuleUnit = engine->loadModule(url, this);
if (engine->hasException)
return nullptr;
if (dependentModuleUnit.compiled)
- dependentModuleUnit.compiled->instantiate(engine);
+ dependentModuleUnit.compiled->instantiate();
}
ScopedString importName(scope);
@@ -657,6 +467,11 @@ const Value *ExecutableCompilationUnit::resolveExportRecursively(
if (exportName->toQString() == QLatin1String("*"))
return &module()->self;
+ const CompiledData::Unit *data = m_compilationUnit->data;
+
+ Q_ASSERT(data);
+ Q_ASSERT(engine);
+
Scope scope(engine);
if (auto localExport = lookupNameInExportTable(
@@ -767,6 +582,11 @@ void ExecutableCompilationUnit::getExportedNamesRecursively(
names->append(name);
};
+ const CompiledData::Unit *data = m_compilationUnit->data;
+
+ Q_ASSERT(data);
+ Q_ASSERT(engine);
+
for (uint i = 0; i < data->localExportEntryTableSize; ++i) {
const CompiledData::ExportEntry &entry = data->localExportEntryTable()[i];
append(stringAt(entry.exportName));
@@ -799,6 +619,8 @@ void ExecutableCompilationUnit::getExportedNamesRecursively(
void ExecutableCompilationUnit::evaluate()
{
+ Q_ASSERT(engine);
+
QV4::Scope scope(engine);
QV4::Scoped<Module> mod(scope, module());
mod->evaluate();
@@ -806,7 +628,10 @@ void ExecutableCompilationUnit::evaluate()
void ExecutableCompilationUnit::evaluateModuleRequests()
{
- for (const QString &request: moduleRequests()) {
+ Q_ASSERT(engine);
+
+ const QStringList moduleRequests = m_compilationUnit->moduleRequests();
+ for (const QString &request: moduleRequests) {
auto dependentModule = engine->loadModule(QUrl(request), this);
if (dependentModule.native)
continue;
@@ -821,88 +646,6 @@ void ExecutableCompilationUnit::evaluateModuleRequests()
}
}
-bool ExecutableCompilationUnit::loadFromDisk(const QUrl &url, const QDateTime &sourceTimeStamp, QString *errorString)
-{
- if (!QQmlFile::isLocalFile(url)) {
- *errorString = QStringLiteral("File has to be a local file.");
- return false;
- }
-
- const QString sourcePath = QQmlFile::urlToLocalFileOrQrc(url);
- auto cacheFile = std::make_unique<CompilationUnitMapper>();
-
- const QStringList cachePaths = { sourcePath + QLatin1Char('c'), localCacheFilePath(url) };
- for (const QString &cachePath : cachePaths) {
- CompiledData::Unit *mappedUnit = cacheFile->get(cachePath, sourceTimeStamp, errorString);
- if (!mappedUnit)
- continue;
-
- const CompiledData::Unit * const oldDataPtr
- = (data && !(data->flags & QV4::CompiledData::Unit::StaticData)) ? data
- : nullptr;
- const CompiledData::Unit *oldData = data;
- auto dataPtrRevert = qScopeGuard([this, oldData](){
- setUnitData(oldData);
- });
- setUnitData(mappedUnit);
-
- if (data->sourceFileIndex != 0
- && sourcePath != QQmlFile::urlToLocalFileOrQrc(stringAt(data->sourceFileIndex))) {
- *errorString = QStringLiteral("QML source file has moved to a different location.");
- continue;
- }
-
- dataPtrRevert.dismiss();
- free(const_cast<CompiledData::Unit*>(oldDataPtr));
- backingFile = std::move(cacheFile);
- return true;
- }
-
- return false;
-}
-
-bool ExecutableCompilationUnit::saveToDisk(const QUrl &unitUrl, QString *errorString)
-{
- if (data->sourceTimeStamp == 0) {
- *errorString = QStringLiteral("Missing time stamp for source file");
- return false;
- }
-
- if (!QQmlFile::isLocalFile(unitUrl)) {
- *errorString = QStringLiteral("File has to be a local file.");
- return false;
- }
-
- return CompiledData::SaveableUnitPointer(unitData()).saveToDisk<char>(
- [&unitUrl, errorString](const char *data, quint32 size) {
- return CompiledData::SaveableUnitPointer::writeDataToFile(localCacheFilePath(unitUrl), data,
- size, errorString);
- });
-}
-
-/*!
- \internal
- This function creates a temporary key vector and sorts it to guarantuee a stable
- hash. This is used to calculate a check-sum on dependent meta-objects.
- */
-bool ResolvedTypeReferenceMap::addToHash(
- QCryptographicHash *hash, QHash<quintptr, QByteArray> *checksums) const
-{
- std::vector<int> keys (size());
- int i = 0;
- for (auto it = constBegin(), end = constEnd(); it != end; ++it) {
- keys[i] = it.key();
- ++i;
- }
- std::sort(keys.begin(), keys.end());
- for (int key: keys) {
- if (!this->operator[](key)->addToHash(hash, checksums))
- return false;
- }
-
- return true;
-}
-
QString ExecutableCompilationUnit::bindingValueAsString(const CompiledData::Binding *binding) const
{
#if QT_CONFIG(translation)
@@ -919,7 +662,7 @@ QString ExecutableCompilationUnit::bindingValueAsString(const CompiledData::Bind
break;
}
#endif
- return CompilationUnit::bindingValueAsString(binding);
+ return m_compilationUnit->bindingValueAsString(binding);
}
QString ExecutableCompilationUnit::translateFrom(TranslationDataIndex index) const
@@ -927,7 +670,7 @@ QString ExecutableCompilationUnit::translateFrom(TranslationDataIndex index) con
#if !QT_CONFIG(translation)
return QString();
#else
- const CompiledData::TranslationData &translation = data->translations()[index.index];
+ const CompiledData::TranslationData &translation = unitData()->translations()[index.index];
if (index.byId) {
QByteArray id = stringAt(translation.stringIndex).toUtf8();
@@ -945,62 +688,21 @@ QString ExecutableCompilationUnit::translateFrom(TranslationDataIndex index) con
return context.toUtf8();
};
- QByteArray context = stringAt(translation.contextIndex).toUtf8();
- QByteArray comment = stringAt(translation.commentIndex).toUtf8();
- QByteArray text = stringAt(translation.stringIndex).toUtf8();
- return QCoreApplication::translate(
- context.isEmpty() ? fileContext() : context, text, comment, translation.number);
-#endif
-}
-
-bool ExecutableCompilationUnit::verifyHeader(
- const CompiledData::Unit *unit, QDateTime expectedSourceTimeStamp, QString *errorString)
-{
- if (strncmp(unit->magic, CompiledData::magic_str, sizeof(unit->magic))) {
- *errorString = QStringLiteral("Magic bytes in the header do not match");
- return false;
- }
-
- if (unit->version != quint32(QV4_DATA_STRUCTURE_VERSION)) {
- *errorString = QString::fromUtf8("V4 data structure version mismatch. Found %1 expected %2")
- .arg(unit->version, 0, 16).arg(QV4_DATA_STRUCTURE_VERSION, 0, 16);
- return false;
- }
-
- if (unit->qtVersion != quint32(QT_VERSION)) {
- *errorString = QString::fromUtf8("Qt version mismatch. Found %1 expected %2")
- .arg(unit->qtVersion, 0, 16).arg(QT_VERSION, 0, 16);
- return false;
- }
-
- if (unit->sourceTimeStamp) {
- // Files from the resource system do not have any time stamps, so fall back to the application
- // executable.
- if (!expectedSourceTimeStamp.isValid())
- expectedSourceTimeStamp = QFileInfo(QCoreApplication::applicationFilePath()).lastModified();
-
- if (expectedSourceTimeStamp.isValid()
- && expectedSourceTimeStamp.toMSecsSinceEpoch() != unit->sourceTimeStamp) {
- *errorString = QStringLiteral("QML source file has a different time stamp than cached file.");
- return false;
- }
+ const bool hasContext
+ = translation.contextIndex != QV4::CompiledData::TranslationData::NoContextIndex;
+ QByteArray context;
+ if (hasContext) {
+ context = stringAt(translation.contextIndex).toUtf8();
+ } else {
+ auto pragmaTranslationContext = unitData()->translationContextIndex();
+ context = stringAt(*pragmaTranslationContext).toUtf8();
+ context = context.isEmpty() ? fileContext() : context;
}
-#if defined(QML_COMPILE_HASH) && defined(QML_COMPILE_HASH_LENGTH) && QML_COMPILE_HASH_LENGTH > 0
- if (qstrncmp(qml_compile_hash, unit->libraryVersionHash, QML_COMPILE_HASH_LENGTH) != 0) {
- *errorString = QStringLiteral("QML compile hashes don't match. Found %1 expected %2")
- .arg(QString::fromLatin1(
- QByteArray(unit->libraryVersionHash, QML_COMPILE_HASH_LENGTH)
- .toPercentEncoding()),
- QString::fromLatin1(
- QByteArray(qml_compile_hash, QML_COMPILE_HASH_LENGTH)
- .toPercentEncoding()));
- return false;
- }
-#else
-#error "QML_COMPILE_HASH must be defined for the build of QtDeclarative to ensure version checking for cache files"
+ QByteArray comment = stringAt(translation.commentIndex).toUtf8();
+ QByteArray text = stringAt(translation.stringIndex).toUtf8();
+ return QCoreApplication::translate(context, text, comment, translation.number);
#endif
- return true;
}
} // namespace QV4
diff --git a/src/qml/jsruntime/qv4executablecompilationunit_p.h b/src/qml/jsruntime/qv4executablecompilationunit_p.h
index e6472f3f83..930e138732 100644
--- a/src/qml/jsruntime/qv4executablecompilationunit_p.h
+++ b/src/qml/jsruntime/qv4executablecompilationunit_p.h
@@ -15,14 +15,15 @@
// We mean it.
//
-#include <private/qv4compileddata_p.h>
-#include <private/qv4identifierhash_p.h>
-#include <private/qqmlrefcount_p.h>
#include <private/qintrusivelist_p.h>
+#include <private/qqmlmetatype_p.h>
+#include <private/qqmlnullablevalue_p.h>
#include <private/qqmlpropertycachevector_p.h>
+#include <private/qqmlrefcount_p.h>
#include <private/qqmltype_p.h>
-#include <private/qqmlnullablevalue_p.h>
-#include <private/qqmlmetatype_p.h>
+#include <private/qqmltypenamecache_p.h>
+#include <private/qv4compileddata_p.h>
+#include <private/qv4identifierhash_p.h>
#include <memory>
@@ -31,210 +32,121 @@ QT_BEGIN_NAMESPACE
class QQmlScriptData;
class QQmlEnginePrivate;
-struct InlineComponentData {
-
- InlineComponentData() = default;
- InlineComponentData(const CompositeMetaTypeIds &typeIds, int objectIndex, int nameIndex, int totalObjectCount, int totalBindingCount, int totalParserStatusCount)
- : typeIds(typeIds)
- , objectIndex(objectIndex)
- , nameIndex(nameIndex)
- , totalObjectCount(totalObjectCount)
- , totalBindingCount(totalBindingCount)
- , totalParserStatusCount(totalParserStatusCount) {}
-
- CompositeMetaTypeIds typeIds;
- int objectIndex = -1;
- int nameIndex = -1;
- int totalObjectCount = 0;
- int totalBindingCount = 0;
- int totalParserStatusCount = 0;
-};
-
namespace QV4 {
-// index is per-object binding index
-typedef QVector<const QQmlPropertyData *> BindingPropertyData;
-
class CompilationUnitMapper;
-class ResolvedTypeReference;
-// map from name index
-struct ResolvedTypeReferenceMap: public QHash<int, ResolvedTypeReference*>
-{
- bool addToHash(QCryptographicHash *hash, QHash<quintptr, QByteArray> *checksums) const;
-};
-class Q_QML_PRIVATE_EXPORT ExecutableCompilationUnit final: public CompiledData::CompilationUnit,
- public QQmlRefCount
+struct CompilationUnitRuntimeData
{
- Q_DISABLE_COPY_MOVE(ExecutableCompilationUnit)
-public:
- friend class QQmlRefPointer<ExecutableCompilationUnit>;
-
- static QQmlRefPointer<ExecutableCompilationUnit> create(
- CompiledData::CompilationUnit &&compilationUnit)
- {
- return QQmlRefPointer<ExecutableCompilationUnit>(
- new ExecutableCompilationUnit(std::move(compilationUnit)),
- QQmlRefPointer<ExecutableCompilationUnit>::Adopt);
- }
+ Heap::String **runtimeStrings = nullptr; // Array
- static QQmlRefPointer<ExecutableCompilationUnit> create()
- {
- return QQmlRefPointer<ExecutableCompilationUnit>(
- new ExecutableCompilationUnit,
- QQmlRefPointer<ExecutableCompilationUnit>::Adopt);
- }
+ // pointers either to data->constants() or little-endian memory copy.
+ // We keep this member twice so that the JIT can access it via standard layout.
+ const StaticValue *constants = nullptr;
- QIntrusiveListNode nextCompilationUnit;
- ExecutionEngine *engine = nullptr;
- QQmlEnginePrivate *qmlEngine = nullptr; // only used in QML environment for composite types, not in plain QJSEngine case.
-
- // url() and fileName() shall be used to load the actual QML/JS code or to show errors or
- // warnings about that code. They include any potential URL interceptions and thus represent the
- // "physical" location of the code.
- //
- // finalUrl() and finalUrlString() shall be used to resolve further URLs referred to in the code
- // They are _not_ intercepted and thus represent the "logical" name for the code.
-
- QUrl url() const { if (m_url.isNull) m_url = QUrl(fileName()); return m_url; }
- QUrl finalUrl() const
- {
- if (m_finalUrl.isNull)
- m_finalUrl = QUrl(finalUrlString());
- return m_finalUrl;
- }
+ QV4::StaticValue *runtimeRegularExpressions = nullptr;
+ Heap::InternalClass **runtimeClasses = nullptr;
+ const StaticValue **imports = nullptr;
QV4::Lookup *runtimeLookups = nullptr;
QVector<QV4::Function *> runtimeFunctions;
QVector<QV4::Heap::InternalClass *> runtimeBlocks;
mutable QVector<QV4::Heap::Object *> templateObjects;
- mutable QQmlNullableValue<QUrl> m_url;
- mutable QQmlNullableValue<QUrl> m_finalUrl;
-
- // QML specific fields
- QQmlPropertyCacheVector propertyCaches;
- QQmlPropertyCache::ConstPtr rootPropertyCache() const { return propertyCaches.at(/*root object*/0); }
-
- QQmlRefPointer<QQmlTypeNameCache> typeNameCache;
-
- // index is object index. This allows fast access to the
- // property data when initializing bindings, avoiding expensive
- // lookups by string (property name).
- QVector<BindingPropertyData> bindingPropertyDataPerObject;
-
- // mapping from component object index (CompiledData::Unit object index that points to component) to identifier hash of named objects
- // this is initialized on-demand by QQmlContextData
- QHash<int, IdentifierHash> namedObjectsPerComponentCache;
- inline IdentifierHash namedObjectsPerComponent(int componentObjectIndex);
-
- void finalizeCompositeType(QQmlEnginePrivate *qmlEngine, CompositeMetaTypeIds typeIdsForComponent);
-
- int m_totalBindingsCount = 0; // Number of bindings used in this type
- int m_totalParserStatusCount = 0; // Number of instantiated types that are QQmlParserStatus subclasses
- int m_totalObjectCount = 0; // Number of objects explicitly instantiated
- int icRoot = -1;
-
- int totalBindingsCount() const;
- int totalParserStatusCount() const;
- int totalObjectCount() const;
-
- QVector<QQmlRefPointer<QQmlScriptData>> dependentScripts;
- ResolvedTypeReferenceMap resolvedTypes;
- ResolvedTypeReference *resolvedType(int id) const { return resolvedTypes.value(id); }
+};
- bool verifyChecksum(const CompiledData::DependentTypesHasher &dependencyHasher) const;
+static_assert(std::is_standard_layout_v<CompilationUnitRuntimeData>);
+static_assert(offsetof(CompilationUnitRuntimeData, runtimeStrings) == 0);
+static_assert(offsetof(CompilationUnitRuntimeData, constants) == sizeof(QV4::Heap::String **));
+static_assert(offsetof(CompilationUnitRuntimeData, runtimeRegularExpressions) == offsetof(CompilationUnitRuntimeData, constants) + sizeof(const StaticValue *));
+static_assert(offsetof(CompilationUnitRuntimeData, runtimeClasses) == offsetof(CompilationUnitRuntimeData, runtimeRegularExpressions) + sizeof(const StaticValue *));
+static_assert(offsetof(CompilationUnitRuntimeData, imports) == offsetof(CompilationUnitRuntimeData, runtimeClasses) + sizeof(const StaticValue *));
- CompositeMetaTypeIds typeIdsForComponent(int objectid = 0) const;
+class Q_QML_EXPORT ExecutableCompilationUnit final
+ : public CompilationUnitRuntimeData,
+ public QQmlRefCounted<ExecutableCompilationUnit>
+{
+ Q_DISABLE_COPY_MOVE(ExecutableCompilationUnit)
+public:
+ friend class QQmlRefCounted<ExecutableCompilationUnit>;
+ friend class QQmlRefPointer<ExecutableCompilationUnit>;
+ friend struct ExecutionEngine;
- CompositeMetaTypeIds typeIds;
- bool isRegistered = false;
+ ExecutionEngine *engine = nullptr;
- QHash<int, InlineComponentData> inlineComponentData;
+ QString finalUrlString() const { return m_compilationUnit->finalUrlString(); }
+ QString fileName() const { return m_compilationUnit->fileName(); }
- std::unique_ptr<CompilationUnitMapper> backingFile;
+ QUrl url() const { return m_compilationUnit->url(); }
+ QUrl finalUrl() const { return m_compilationUnit->finalUrl(); }
- // --- interface for QQmlPropertyCacheCreator
- using CompiledObject = CompiledData::Object;
- using CompiledFunction = CompiledData::Function;
- enum class ListPropertyAssignBehavior { Append, Replace, ReplaceIfNotDefault };
- ListPropertyAssignBehavior listPropertyAssignBehavior() const
+ QQmlRefPointer<QQmlTypeNameCache> typeNameCache() const
{
- if (data->flags & CompiledData::Unit::ListPropertyAssignReplace)
- return ListPropertyAssignBehavior::Replace;
- if (data->flags & CompiledData::Unit::ListPropertyAssignReplaceIfNotDefault)
- return ListPropertyAssignBehavior::ReplaceIfNotDefault;
- return ListPropertyAssignBehavior::Append;
+ return m_compilationUnit->typeNameCache;
}
- bool enforcesFunctionSignature() const
+ QQmlPropertyCacheVector *propertyCachesPtr()
{
- return data->flags & CompiledData::Unit::FunctionSignaturesEnforced;
+ return &m_compilationUnit->propertyCaches;
}
- bool nativeMethodsAcceptThisObjects() const
+ QQmlPropertyCache::ConstPtr rootPropertyCache() const
{
- return data->flags & CompiledData::Unit::NativeMethodsAcceptThisObject;
+ return m_compilationUnit->rootPropertyCache();
}
- bool valueTypesAreCopied() const
- {
- return data->flags & CompiledData::Unit::ValueTypesCopied;
- }
+ // mapping from component object index (CompiledData::Unit object index that points to component) to identifier hash of named objects
+ // this is initialized on-demand by QQmlContextData
+ QHash<int, IdentifierHash> namedObjectsPerComponentCache;
+ inline IdentifierHash namedObjectsPerComponent(int componentObjectIndex);
- int objectCount() const { return qmlData->nObjects; }
- const CompiledObject *objectAt(int index) const
+ int totalBindingsCount() const { return m_compilationUnit->totalBindingsCount(); }
+ int totalParserStatusCount() const { return m_compilationUnit->totalParserStatusCount(); }
+ int totalObjectCount() const { return m_compilationUnit->totalObjectCount(); }
+
+ ResolvedTypeReference *resolvedType(int id) const
{
- return qmlData->objectAt(index);
+ return m_compilationUnit->resolvedType(id);
}
- int importCount() const { return qmlData->nImports; }
- const CompiledData::Import *importAt(int index) const
+ QQmlType qmlTypeForComponent(const QString &inlineComponentName = QString()) const
{
- return qmlData->importAt(index);
+ return m_compilationUnit->qmlTypeForComponent(inlineComponentName);
}
- Heap::Object *templateObjectAt(int index) const;
-
- struct FunctionIterator
- {
- FunctionIterator(const CompiledData::Unit *unit, const CompiledObject *object, int index)
- : unit(unit), object(object), index(index) {}
- const CompiledData::Unit *unit;
- const CompiledObject *object;
- int index;
-
- const CompiledFunction *operator->() const
- {
- return unit->functionAt(object->functionOffsetTable()[index]);
- }
-
- void operator++() { ++index; }
- bool operator==(const FunctionIterator &rhs) const { return index == rhs.index; }
- bool operator!=(const FunctionIterator &rhs) const { return index != rhs.index; }
- };
+ QMetaType metaType() const { return m_compilationUnit->qmlType.typeId(); }
- FunctionIterator objectFunctionsBegin(const CompiledObject *object) const
+ int inlineComponentId(const QString &inlineComponentName) const
{
- return FunctionIterator(data, object, 0);
+ return m_compilationUnit->inlineComponentId(inlineComponentName);
}
- FunctionIterator objectFunctionsEnd(const CompiledObject *object) const
- {
- return FunctionIterator(data, object, object->nFunctions);
- }
+ // --- interface for QQmlPropertyCacheCreator
+ using CompiledObject = CompiledData::CompilationUnit::CompiledObject;
+ using CompiledFunction = CompiledData::CompilationUnit::CompiledFunction;
+ using CompiledBinding = CompiledData::CompilationUnit::CompiledBinding;
+ using IdToObjectMap = CompiledData::CompilationUnit::IdToObjectMap;
- bool isESModule() const
+ bool nativeMethodsAcceptThisObjects() const
{
- return data->flags & CompiledData::Unit::IsESModule;
+ return m_compilationUnit->nativeMethodsAcceptThisObjects();
}
- bool isSharedLibrary() const
+ bool ignoresFunctionSignature() const { return m_compilationUnit->ignoresFunctionSignature(); }
+ bool valueTypesAreCopied() const { return m_compilationUnit->valueTypesAreCopied(); }
+ bool valueTypesAreAddressable() const { return m_compilationUnit->valueTypesAreAddressable(); }
+ bool valueTypesAreAssertable() const { return m_compilationUnit->valueTypesAreAssertable(); }
+ bool componentsAreBound() const { return m_compilationUnit->componentsAreBound(); }
+ bool isESModule() const { return m_compilationUnit->isESModule(); }
+
+ int objectCount() const { return m_compilationUnit->objectCount(); }
+ const CompiledObject *objectAt(int index) const
{
- return data->flags & CompiledData::Unit::IsSharedLibrary;
+ return m_compilationUnit->objectAt(index);
}
- QStringList moduleRequests() const;
- Heap::Module *instantiate(ExecutionEngine *engine);
+ Heap::Object *templateObjectAt(int index) const;
+
+ Heap::Module *instantiate();
const Value *resolveExport(QV4::String *exportName)
{
QVector<ResolveSetEntry> resolveSet;
@@ -255,17 +167,18 @@ public:
void evaluate();
void evaluateModuleRequests();
- QV4::Function *linkToEngine(QV4::ExecutionEngine *engine);
- void unlink();
-
- void markObjects(MarkStack *markStack);
-
- bool loadFromDisk(const QUrl &url, const QDateTime &sourceTimeStamp, QString *errorString);
-
- static QString localCacheFilePath(const QUrl &url);
- bool saveToDisk(const QUrl &unitUrl, QString *errorString);
+ void mark(MarkStack *markStack) const { markObjects(markStack); }
+ void markObjects(MarkStack *markStack) const;
QString bindingValueAsString(const CompiledData::Binding *binding) const;
+ double bindingValueAsNumber(const CompiledData::Binding *binding) const
+ {
+ return m_compilationUnit->bindingValueAsNumber(binding);
+ }
+ QString bindingValueAsScriptString(const CompiledData::Binding *binding) const
+ {
+ return m_compilationUnit->bindingValueAsScriptString(binding);
+ }
struct TranslationDataIndex
{
@@ -275,13 +188,53 @@ public:
QString translateFrom(TranslationDataIndex index) const;
- static bool verifyHeader(const CompiledData::Unit *unit, QDateTime expectedSourceTimeStamp,
- QString *errorString);
+ Heap::Module *module() const { return m_module; }
+ void setModule(Heap::Module *module) { m_module = module; }
+
+ const CompiledData::Unit *unitData() const { return m_compilationUnit->data; }
+
+ QString stringAt(uint index) const { return m_compilationUnit->stringAt(index); }
+
+ const QVector<QQmlRefPointer<QQmlScriptData>> *dependentScriptsPtr() const
+ {
+ return &m_compilationUnit->dependentScripts;
+ }
+
+ const CompiledData::BindingPropertyData *bindingPropertyDataPerObjectAt(
+ qsizetype objectIndex) const
+ {
+ return &m_compilationUnit->bindingPropertyDataPerObject.at(objectIndex);
+ }
+
+ const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &baseCompilationUnit() const
+ {
+ return m_compilationUnit;
+ }
+
+ QV4::Function *rootFunction()
+ {
+ if (!runtimeStrings)
+ populate();
+
+ const auto *data = unitData();
+ return data->indexOfRootFunction != -1
+ ? runtimeFunctions[data->indexOfRootFunction]
+ : nullptr;
+ }
+
+ void populate();
+ void clear();
+
protected:
quint32 totalStringCount() const
- { return data->stringTableSize; }
+ { return unitData()->stringTableSize; }
private:
+ friend struct ExecutionEngine;
+
+ QQmlRefPointer<CompiledData::CompilationUnit> m_compilationUnit;
+ Heap::Module *m_module = nullptr;
+
struct ResolveSetEntry
{
ResolveSetEntry() {}
@@ -292,9 +245,13 @@ private:
};
ExecutableCompilationUnit();
- ExecutableCompilationUnit(CompiledData::CompilationUnit &&compilationUnit);
+ ExecutableCompilationUnit(QQmlRefPointer<CompiledData::CompilationUnit> &&compilationUnit);
~ExecutableCompilationUnit();
+ static QQmlRefPointer<ExecutableCompilationUnit> create(
+ QQmlRefPointer<CompiledData::CompilationUnit> &&compilationUnit,
+ ExecutionEngine *engine);
+
const Value *resolveExportRecursively(QV4::String *exportName,
QVector<ResolveSetEntry> *resolveSet);
@@ -312,8 +269,8 @@ private:
IdentifierHash ExecutableCompilationUnit::namedObjectsPerComponent(int componentObjectIndex)
{
- auto it = namedObjectsPerComponentCache.find(componentObjectIndex);
- if (Q_UNLIKELY(it == namedObjectsPerComponentCache.end()))
+ auto it = namedObjectsPerComponentCache.constFind(componentObjectIndex);
+ if (Q_UNLIKELY(it == namedObjectsPerComponentCache.cend()))
return createNamedObjectsPerComponent(componentObjectIndex);
Q_ASSERT(!it->isEmpty());
return *it;
diff --git a/src/qml/jsruntime/qv4function.cpp b/src/qml/jsruntime/qv4function.cpp
index 29e3d8d7ef..82646e2822 100644
--- a/src/qml/jsruntime/qv4function.cpp
+++ b/src/qml/jsruntime/qv4function.cpp
@@ -1,20 +1,18 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
-#include "qml/qqmlprivate.h"
#include "qv4function_p.h"
-#include "qv4managed_p.h"
-#include "qv4string_p.h"
-#include "qv4value_p.h"
-#include "qv4engine_p.h"
-#include <private/qv4mm_p.h>
-#include <private/qv4identifiertable_p.h>
+
+#include <private/qqmlpropertycachecreator_p.h>
+#include <private/qqmltype_p_p.h>
+
+#include <private/qv4engine_p.h>
#include <private/qv4functiontable_p.h>
-#include <assembler/MacroAssemblerCodeRef.h>
-#include <private/qv4vme_moth_p.h>
-#include <private/qqmlglobal_p.h>
+#include <private/qv4identifiertable_p.h>
#include <private/qv4jscall_p.h>
-#include <private/qqmlpropertycachecreator_p.h>
+#include <private/qv4vme_moth_p.h>
+
+#include <assembler/MacroAssemblerCodeRef.h>
QT_BEGIN_NAMESPACE
@@ -41,8 +39,8 @@ bool Function::call(QObject *thisObject, void **a, const QMetaType *types, int a
}
static ReturnedValue doCall(
- Function *self, const Value *thisObject, const Value *argv, int argc,
- ExecutionContext *context)
+ QV4::Function *self, const QV4::Value *thisObject, const QV4::Value *argv, int argc,
+ QV4::ExecutionContext *context)
{
ExecutionEngine *engine = context->engine();
JSTypesStackFrame frame;
@@ -61,15 +59,15 @@ ReturnedValue Function::call(
switch (kind) {
case AotCompiled:
return QV4::convertAndCall(
- context->engine(), typedFunction, thisObject, argv, argc,
+ context->engine(), &aotCompiledFunction, thisObject, argv, argc,
[this, context](
QObject *thisObject, void **a, const QMetaType *types, int argc) {
call(thisObject, a, types, argc, context);
});
case JsTyped:
return QV4::coerceAndCall(
- context->engine(), typedFunction, thisObject, argv, argc,
- [this, context](const Value *thisObject, const Value *argv, int argc) {
+ context->engine(), &jsTypedFunction, compiledFunction, argv, argc,
+ [this, context, thisObject](const Value *argv, int argc) {
return doCall(this, thisObject, argv, argc, context);
});
default:
@@ -81,7 +79,7 @@ ReturnedValue Function::call(
Function *Function::create(ExecutionEngine *engine, ExecutableCompilationUnit *unit,
const CompiledData::Function *function,
- const QQmlPrivate::TypedFunction *aotFunction)
+ const QQmlPrivate::AOTCompiledFunction *aotFunction)
{
return new Function(engine, unit, function, aotFunction);
}
@@ -91,16 +89,24 @@ void Function::destroy()
delete this;
}
+void Function::mark(MarkStack *ms)
+{
+ if (internalClass)
+ internalClass->mark(ms);
+}
+
+static bool isSpecificType(const CompiledData::ParameterType &type)
+{
+ return type.typeNameIndexOrCommonType()
+ != (type.indexIsCommonType() ? quint32(CompiledData::CommonType::Invalid) : 0);
+}
+
Function::Function(ExecutionEngine *engine, ExecutableCompilationUnit *unit,
const CompiledData::Function *function,
- const QQmlPrivate::TypedFunction *aotFunction)
- : FunctionData(unit)
+ const QQmlPrivate::AOTCompiledFunction *aotFunction)
+ : FunctionData(engine, unit)
, compiledFunction(function)
, codeData(function->code())
- , jittedCode(nullptr)
- , codeRef(nullptr)
- , typedFunction(aotFunction)
- , kind(aotFunction ? AotCompiled : JsUntyped)
{
Scope scope(engine);
Scoped<InternalClass> ic(scope, engine->internalClasses(EngineBase::Class_CallContext));
@@ -111,74 +117,59 @@ Function::Function(ExecutionEngine *engine, ExecutableCompilationUnit *unit,
ic = ic->addMember(engine->identifierTable->asPropertyKey(compilationUnit->runtimeStrings[localsIndices[i]]), Attr_NotConfigurable);
const CompiledData::Parameter *formalsIndices = compiledFunction->formalsTable();
- const bool enforcesSignature = !aotFunction && unit->enforcesFunctionSignature();
- bool hasTypes = false;
+ bool enforceJsTypes = !unit->ignoresFunctionSignature();
+
for (quint32 i = 0; i < compiledFunction->nFormals; ++i) {
ic = ic->addMember(engine->identifierTable->asPropertyKey(compilationUnit->runtimeStrings[formalsIndices[i].nameIndex]), Attr_NotConfigurable);
- if (enforcesSignature
- && !hasTypes
- && formalsIndices[i].type.typeNameIndexOrBuiltinType()
- != quint32(QV4::CompiledData::BuiltinType::InvalidBuiltin)) {
- hasTypes = true;
- }
+ if (enforceJsTypes && !isSpecificType(formalsIndices[i].type))
+ enforceJsTypes = false;
}
- internalClass = ic->d();
+ internalClass.set(engine, ic->d());
nFormals = compiledFunction->nFormals;
- if (!enforcesSignature)
+ if (!enforceJsTypes)
return;
- if (!hasTypes
- && compiledFunction->returnType.typeNameIndexOrBuiltinType()
- == quint32(QV4::CompiledData::BuiltinType::InvalidBuiltin)) {
+ if (aotFunction) {
+ aotCompiledCode = aotFunction->functionPtr;
+ new (&aotCompiledFunction) AOTCompiledFunction;
+ kind = AotCompiled;
+ aotCompiledFunction.types.resize(aotFunction->numArguments + 1);
+ aotFunction->signature(unit, aotCompiledFunction.types.data());
return;
}
- QQmlPrivate::TypedFunction *synthesized = new QQmlPrivate::TypedFunction;
- QQmlEnginePrivate *enginePrivate = QQmlEnginePrivate::get(engine->qmlEngine());
-
- auto findMetaType = [&](const CompiledData::ParameterType &param) {
- const quint32 type = param.typeNameIndexOrBuiltinType();
- if (param.indexIsBuiltinType()) {
- if (param.isList()) {
- return QQmlPropertyCacheCreatorBase::listTypeForPropertyType(
- QV4::CompiledData::BuiltinType(type));
- }
- return QQmlPropertyCacheCreatorBase::metaTypeForPropertyType(
- QV4::CompiledData::BuiltinType(type));
- }
-
- if (type == 0)
- return QMetaType();
-
- const QQmlType qmltype = unit->typeNameCache->query(unit->stringAt(type)).type;
- if (!qmltype.isValid())
- return QMetaType();
+ // If a function has any typed arguments, but an untyped return value, the return value is void.
+ // If it doesn't have any arguments at all and the return value is untyped, the function is
+ // untyped. Users can specifically set the return type to "void" to have it enforced.
+ if (nFormals == 0 && !isSpecificType(compiledFunction->returnType))
+ return;
- const QMetaType metaType = param.isList() ? qmltype.qListTypeId() : qmltype.typeId();
- if (metaType.isValid())
- return metaType;
+ QQmlTypeLoader *typeLoader = engine->typeLoader();
- if (!qmltype.isComposite()) {
- if (!qmltype.isInlineComponentType())
- return QMetaType();
- const CompositeMetaTypeIds typeIds
- = unit->typeIdsForComponent(qmltype.inlineComponentId());
- return param.isList() ? typeIds.listId : typeIds.id;
+ auto findQmlType = [&](const CompiledData::ParameterType &param) {
+ const quint32 type = param.typeNameIndexOrCommonType();
+ if (param.indexIsCommonType()) {
+ return QQmlMetaType::qmlType(QQmlPropertyCacheCreatorBase::metaTypeForPropertyType(
+ QV4::CompiledData::CommonType(type)));
}
- const CompositeMetaTypeIds typeIds = enginePrivate->typeLoader.getType(
- qmltype.sourceUrl())->compilationUnit()->typeIds;
- return param.isList() ? typeIds.listId : typeIds.id;
- };
+ if (type == 0 || !typeLoader)
+ return QQmlType();
- for (quint16 i = 0; i < nFormals; ++i)
- synthesized->argumentTypes.append(findMetaType(formalsIndices[i].type));
+ const auto &base = unit->baseCompilationUnit();
+ const QQmlType qmltype = QQmlTypePrivate::compositeQmlType(
+ base, typeLoader, base->stringAt(type));
+ return qmltype.typeId().isValid() ? qmltype : QQmlType();
+ };
- synthesized->returnType = findMetaType(compiledFunction->returnType);
- typedFunction = synthesized;
+ new (&jsTypedFunction) JSTypedFunction;
kind = JsTyped;
+ jsTypedFunction.types.reserve(nFormals + 1);
+ jsTypedFunction.types.append(findQmlType(compiledFunction->returnType));
+ for (quint16 i = 0; i < nFormals; ++i)
+ jsTypedFunction.types.append(findQmlType(formalsIndices[i].type));
}
Function::~Function()
@@ -187,8 +178,18 @@ Function::~Function()
destroyFunctionTable(this, codeRef);
delete codeRef;
}
- if (kind == JsTyped)
- delete typedFunction;
+
+ switch (kind) {
+ case JsTyped:
+ jsTypedFunction.~JSTypedFunction();
+ break;
+ case AotCompiled:
+ aotCompiledFunction.~AOTCompiledFunction();
+ break;
+ case JsUntyped:
+ case Eval:
+ break;
+ }
}
void Function::updateInternalClass(ExecutionEngine *engine, const QList<QByteArray> &parameters)
@@ -219,22 +220,23 @@ void Function::updateInternalClass(ExecutionEngine *engine, const QList<QByteArr
}
- internalClass = engine->internalClasses(EngineBase::Class_CallContext);
+ Scope scope(engine);
+ Scoped<InternalClass> ic(scope, engine->internalClasses(EngineBase::Class_CallContext));
// first locals
const quint32_le *localsIndices = compiledFunction->localsTable();
for (quint32 i = 0; i < compiledFunction->nLocals; ++i) {
- internalClass = internalClass->addMember(
+ ic = ic->addMember(
engine->identifierTable->asPropertyKey(compilationUnit->runtimeStrings[localsIndices[i]]),
Attr_NotConfigurable);
}
- Scope scope(engine);
ScopedString arg(scope);
for (const QString &parameterName : parameterNames) {
arg = engine->newIdentifier(parameterName);
- internalClass = internalClass->addMember(arg->propertyKey(), Attr_NotConfigurable);
+ ic = ic->addMember(arg->propertyKey(), Attr_NotConfigurable);
}
+ internalClass.set(engine, ic->d());
nFormals = parameters.size();
}
@@ -255,6 +257,11 @@ QQmlSourceLocation Function::sourceLocation() const
sourceFile(), compiledFunction->location.line(), compiledFunction->location.column());
}
+FunctionData::FunctionData(EngineBase *engine, ExecutableCompilationUnit *compilationUnit_)
+{
+ compilationUnit.set(engine, compilationUnit_);
+}
+
} // namespace QV4
QT_END_NAMESPACE
diff --git a/src/qml/jsruntime/qv4function_p.h b/src/qml/jsruntime/qv4function_p.h
index 181c88a1e2..7543dd3c4b 100644
--- a/src/qml/jsruntime/qv4function_p.h
+++ b/src/qml/jsruntime/qv4function_p.h
@@ -30,16 +30,15 @@ struct QQmlSourceLocation;
namespace QV4 {
-struct Q_QML_EXPORT FunctionData {
- CompiledData::CompilationUnitBase *compilationUnit;
+struct Q_QML_EXPORT FunctionData
+{
+ WriteBarrier::HeapObjectWrapper<CompilationUnitRuntimeData, 1> compilationUnit;
// Intentionally require an ExecutableCompilationUnit but save only a pointer to
// CompilationUnitBase. This is so that we can take advantage of the standard layout
// of CompilationUnitBase in the JIT. Furthermore we can safely static_cast to
// ExecutableCompilationUnit where we need it.
- FunctionData(ExecutableCompilationUnit *compilationUnit)
- : compilationUnit(compilationUnit)
- {}
+ FunctionData(EngineBase *engine, ExecutableCompilationUnit *compilationUnit_);
};
// Make sure this class can be accessed through offsetof (done by the assemblers):
Q_STATIC_ASSERT(std::is_standard_layout< FunctionData >::value);
@@ -47,16 +46,22 @@ Q_STATIC_ASSERT(std::is_standard_layout< FunctionData >::value);
struct Q_QML_EXPORT Function : public FunctionData {
protected:
Function(ExecutionEngine *engine, ExecutableCompilationUnit *unit,
- const CompiledData::Function *function, const QQmlPrivate::TypedFunction *aotFunction);
+ const CompiledData::Function *function, const QQmlPrivate::AOTCompiledFunction *aotFunction);
~Function();
public:
- const CompiledData::Function *compiledFunction;
+ struct JSTypedFunction {
+ QVarLengthArray<QQmlType, 4> types;
+ };
+
+ struct AOTCompiledFunction {
+ QVarLengthArray<QMetaType, 4> types;
+ };
QV4::ExecutableCompilationUnit *executableCompilationUnit() const
{
// This is safe: We require an ExecutableCompilationUnit in the ctor.
- return static_cast<QV4::ExecutableCompilationUnit *>(compilationUnit);
+ return static_cast<QV4::ExecutableCompilationUnit *>(compilationUnit.get());
}
QV4::Heap::String *runtimeString(uint i) const
@@ -69,26 +74,39 @@ public:
ReturnedValue call(const Value *thisObject, const Value *argv, int argc,
ExecutionContext *context);
- const char *codeData;
+ const CompiledData::Function *compiledFunction = nullptr;
+ const char *codeData = nullptr;
+ JSC::MacroAssemblerCodeRef *codeRef = nullptr;
typedef ReturnedValue (*JittedCode)(CppStackFrame *, ExecutionEngine *);
- JittedCode jittedCode;
- JSC::MacroAssemblerCodeRef *codeRef;
- const QQmlPrivate::TypedFunction *typedFunction = nullptr;
+ typedef void (*AotCompiledCode)(const QQmlPrivate::AOTCompiledContext *context, void **argv);
+
+ union {
+ void *noFunction = nullptr;
+ JSTypedFunction jsTypedFunction;
+ AOTCompiledFunction aotCompiledFunction;
+ };
+
+ union {
+ JittedCode jittedCode = nullptr;
+ AotCompiledCode aotCompiledCode;
+ };
// first nArguments names in internalClass are the actual arguments
- Heap::InternalClass *internalClass;
+ QV4::WriteBarrier::Pointer<Heap::InternalClass> internalClass;
int interpreterCallCount = 0;
- quint16 nFormals;
+ quint16 nFormals = 0;
enum Kind : quint8 { JsUntyped, JsTyped, AotCompiled, Eval };
Kind kind = JsUntyped;
bool detectedInjectedParameters = false;
static Function *create(ExecutionEngine *engine, ExecutableCompilationUnit *unit,
const CompiledData::Function *function,
- const QQmlPrivate::TypedFunction *aotFunction);
+ const QQmlPrivate::AOTCompiledFunction *aotFunction);
void destroy();
+ void mark(QV4::MarkStack *ms);
+
// used when dynamically assigning signal handlers (QQmlConnection)
void updateInternalClass(ExecutionEngine *engine, const QList<QByteArray> &parameters);
diff --git a/src/qml/jsruntime/qv4functionobject.cpp b/src/qml/jsruntime/qv4functionobject.cpp
index 84ba0cfcfb..e9f91fbc06 100644
--- a/src/qml/jsruntime/qv4functionobject.cpp
+++ b/src/qml/jsruntime/qv4functionobject.cpp
@@ -30,84 +30,57 @@ using namespace QV4;
DEFINE_OBJECT_VTABLE(FunctionObject);
-void Heap::FunctionObject::init(QV4::ExecutionContext *scope, QV4::String *name,
- VTable::Call call, VTable::CallWithMetaTypes callWithMetaTypes)
+void Heap::FunctionObject::init(QV4::ExecutionEngine *engine, QV4::String *name)
{
- jsCall = call;
- jsCallWithMetaTypes = callWithMetaTypes;
- jsConstruct = nullptr;
-
Object::init();
- this->scope.set(scope->engine(), scope->d());
- Scope s(scope->engine());
+ Scope s(engine);
ScopedFunctionObject f(s, this);
if (name)
f->setName(name);
}
-void Heap::FunctionObject::init(QV4::ExecutionContext *scope, QV4::String *name)
+void Heap::FunctionObject::init(QV4::ExecutionEngine *engine, const QString &name)
{
- ExecutionEngine *e = scope->engine();
-
- jsCall = vtable()->call;
- jsCallWithMetaTypes = vtable()->callWithMetaTypes;
- jsConstruct = vtable()->callAsConstructor;
-
- Object::init();
- this->scope.set(scope->engine(), scope->d());
- Scope s(e);
- ScopedFunctionObject f(s, this);
- if (name)
- f->setName(name);
+ Scope valueScope(engine);
+ ScopedString s(valueScope, engine->newString(name));
+ init(engine, s);
}
-
-
-void Heap::FunctionObject::init(QV4::ExecutionContext *scope, Function *function, QV4::String *n)
-{
- jsCall = vtable()->call;
- jsCallWithMetaTypes = vtable()->callWithMetaTypes;
- jsConstruct = vtable()->callAsConstructor;
-
- Object::init();
- setFunction(function);
- this->scope.set(scope->engine(), scope->d());
- Scope s(scope->engine());
- ScopedString name(s, n ? n->d() : function->name());
- ScopedFunctionObject f(s, this);
- if (name)
- f->setName(name);
-}
-
-void Heap::FunctionObject::init(QV4::ExecutionContext *scope, const QString &name)
+void Heap::FunctionObject::init()
{
- Scope valueScope(scope);
- ScopedString s(valueScope, valueScope.engine->newString(name));
- init(scope, s);
+ init(internalClass->engine, static_cast<QV4::String *>(nullptr));
}
-void Heap::FunctionObject::init()
+void Heap::JavaScriptFunctionObject::init(
+ QV4::ExecutionContext *scope, Function *function, QV4::String *n)
{
- jsCall = vtable()->call;
- jsCallWithMetaTypes = vtable()->callWithMetaTypes;
- jsConstruct = vtable()->callAsConstructor;
-
- Object::init();
- this->scope.set(internalClass->engine, internalClass->engine->rootContext()->d());
+ Q_ASSERT(n || function);
+ Scope s(scope->engine());
+ ScopedString name(s, n ? n->d() : function->name());
+ FunctionObject::init(s.engine, name);
+ this->scope.set(s.engine, scope->d());
+ setFunction(function);
}
-void Heap::FunctionObject::setFunction(Function *f)
+void Heap::JavaScriptFunctionObject::setFunction(Function *f)
{
if (f) {
function = f;
function->executableCompilationUnit()->addref();
}
}
-void Heap::FunctionObject::destroy()
+void Heap::JavaScriptFunctionObject::destroy()
{
if (function)
function->executableCompilationUnit()->release();
- Object::destroy();
+ FunctionObject::destroy();
+}
+
+void Heap::DynamicFunctionObject::init(
+ QV4::ExecutionEngine *engine, QV4::String *name, VTable::Call call)
+{
+ FunctionObject::init(engine, name);
+ jsCall = call;
}
void FunctionObject::createDefaultPrototypeProperty(uint protoConstructorSlot)
@@ -121,32 +94,29 @@ void FunctionObject::createDefaultPrototypeProperty(uint protoConstructorSlot)
defineDefaultProperty(s.engine->id_prototype(), proto, Attr_NotEnumerable|Attr_NotConfigurable);
}
-void FunctionObject::call(QObject *thisObject, void **a, const QMetaType *types, int argc)
+ReturnedValue FunctionObject::name() const
{
- if (const auto callWithMetaTypes = d()->jsCallWithMetaTypes) {
- callWithMetaTypes(this, thisObject, a, types, argc);
- return;
- }
-
- QV4::convertAndCall(engine(), thisObject, a, types, argc,
- [this](const Value *thisObject, const Value *argv, int argc) {
- return call(thisObject, argv, argc);
- });
+ return get(engine()->id_name());
}
-ReturnedValue FunctionObject::name() const
+ReturnedValue FunctionObject::failCall() const
{
- return get(scope()->internalClass->engine->id_name());
+ return engine()->throwTypeError(QStringLiteral("Function can only be called with |new|."));
}
-ReturnedValue FunctionObject::virtualCall(const FunctionObject *, const Value *, const Value *, int)
+ReturnedValue FunctionObject::failCallAsConstructor() const
{
- return Encode::undefined();
+ return engine()->throwTypeError(QStringLiteral("Function is not a constructor."));
}
-void FunctionObject::virtualCallWithMetaTypes(
- const FunctionObject *, QObject *, void **, const QMetaType *, int)
+void FunctionObject::virtualConvertAndCall(
+ const FunctionObject *f, QObject *thisObject,
+ void **argv, const QMetaType *types, int argc)
{
+ QV4::convertAndCall(f->engine(), thisObject, argv, types, argc,
+ [f](const Value *thisObject, const Value *argv, int argc) {
+ return f->call(thisObject, argv, argc);
+ });
}
Heap::FunctionObject *FunctionObject::createScriptFunction(ExecutionContext *scope, Function *function)
@@ -156,15 +126,20 @@ Heap::FunctionObject *FunctionObject::createScriptFunction(ExecutionContext *sco
return scope->engine()->memoryManager->allocate<ScriptFunction>(scope, function);
}
-Heap::FunctionObject *FunctionObject::createConstructorFunction(ExecutionContext *scope, Function *function, Object *homeObject, bool isDerivedConstructor)
+Heap::FunctionObject *FunctionObject::createConstructorFunction(
+ ExecutionContext *scope, Function *function, Object *homeObject, bool isDerivedConstructor)
{
+ QV4::ExecutionEngine *engine = scope->engine();
if (!function) {
- Heap::DefaultClassConstructorFunction *c = scope->engine()->memoryManager->allocate<DefaultClassConstructorFunction>(scope);
+ Heap::DefaultClassConstructorFunction *c
+ = engine->memoryManager->allocate<DefaultClassConstructorFunction>(scope);
c->isDerivedConstructor = isDerivedConstructor;
return c;
}
- Heap::ConstructorFunction *c = scope->engine()->memoryManager->allocate<ConstructorFunction>(scope, function);
- c->homeObject.set(scope->engine(), homeObject->d());
+
+ Heap::ConstructorFunction *c
+ = engine->memoryManager->allocate<ConstructorFunction>(scope, function);
+ c->homeObject.set(engine, homeObject->d());
c->isDerivedConstructor = isDerivedConstructor;
return c;
}
@@ -183,7 +158,8 @@ Heap::FunctionObject *FunctionObject::createBuiltinFunction(ExecutionEngine *eng
if (!name)
name = engine->newString(QChar::fromLatin1('[') + QStringView{nameOrSymbol->toQString()}.mid(1) + QChar::fromLatin1(']'));
- ScopedFunctionObject function(scope, engine->memoryManager->allocate<FunctionObject>(engine->rootContext(), name, code));
+ ScopedFunctionObject function(
+ scope, engine->memoryManager->allocate<DynamicFunctionObject>(engine, name, code));
function->defineReadonlyConfigurableProperty(engine->id_length(), Value::fromInt32(argumentCount));
return function->d();
}
@@ -199,16 +175,29 @@ ReturnedValue FunctionObject::getHomeObject() const
return Encode::undefined();
}
-QQmlSourceLocation FunctionObject::sourceLocation() const
+DEFINE_OBJECT_VTABLE(JavaScriptFunctionObject);
+
+QQmlSourceLocation JavaScriptFunctionObject::sourceLocation() const
{
return d()->function->sourceLocation();
}
+DEFINE_OBJECT_VTABLE(DynamicFunctionObject);
+
+ReturnedValue DynamicFunctionObject::virtualCall(
+ const FunctionObject *f, const Value *thisObject, const Value *argv, int argc) {
+ Heap::DynamicFunctionObject *d = static_cast<const DynamicFunctionObject *>(f)->d();
+ if (d->jsCall)
+ return d->jsCall(f, thisObject, argv, argc);
+ return d->internalClass->engine->throwTypeError(
+ QStringLiteral("Function can only be called with |new|."));
+}
+
DEFINE_OBJECT_VTABLE(FunctionCtor);
-void Heap::FunctionCtor::init(QV4::ExecutionContext *scope)
+void Heap::FunctionCtor::init(QV4::ExecutionEngine *engine)
{
- Heap::FunctionObject::init(scope, QStringLiteral("Function"));
+ Heap::FunctionObject::init(engine, QStringLiteral("Function"));
}
// 15.3.2
@@ -256,7 +245,7 @@ QQmlRefPointer<ExecutableCompilationUnit> FunctionCtor::parse(ExecutionEngine *e
if (engine->hasException)
return nullptr;
- return ExecutableCompilationUnit::create(cg.generateCompilationUnit());
+ return engine->insertCompilationUnit(cg.generateCompilationUnit());
}
ReturnedValue FunctionCtor::virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *newTarget)
@@ -268,7 +257,7 @@ ReturnedValue FunctionCtor::virtualCallAsConstructor(const FunctionObject *f, co
if (engine->hasException)
return Encode::undefined();
- Function *vmf = compilationUnit->linkToEngine(engine);
+ Function *vmf = compilationUnit->rootFunction();
ExecutionContext *global = engine->scriptContext();
ReturnedValue o = Encode(FunctionObject::createScriptFunction(global, vmf));
@@ -311,6 +300,12 @@ void FunctionPrototype::init(ExecutionEngine *engine, Object *ctor)
defineDefaultProperty(engine->symbol_hasInstance(), method_hasInstance, 1, Attr_ReadOnly);
}
+ReturnedValue FunctionPrototype::virtualCall(
+ const FunctionObject *, const Value *, const Value *, int)
+{
+ return Encode::undefined();
+}
+
ReturnedValue FunctionPrototype::method_toString(const FunctionObject *b, const Value *thisObject, const Value *, int)
{
ExecutionEngine *v4 = b->engine();
@@ -427,10 +422,13 @@ ReturnedValue FunctionPrototype::method_bind(const FunctionObject *b, const Valu
boundArgs->set(scope.engine, i, argv[i + 1]);
}
- ScopedContext ctx(scope, target->scope());
- Heap::BoundFunction *bound = BoundFunction::create(ctx, target, boundThis, boundArgs);
- bound->setFunction(target->function());
- return bound->asReturnedValue();
+ if (target->isConstructor()) {
+ return scope.engine->memoryManager->allocate<BoundConstructor>(target, boundThis, boundArgs)
+ ->asReturnedValue();
+ }
+
+ return scope.engine->memoryManager->allocate<BoundFunction>(target, boundThis, boundArgs)
+ ->asReturnedValue();
}
ReturnedValue FunctionPrototype::method_hasInstance(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
@@ -490,7 +488,9 @@ DEFINE_OBJECT_VTABLE(ArrowFunction);
void ArrowFunction::virtualCallWithMetaTypes(const FunctionObject *fo, QObject *thisObject,
void **a, const QMetaType *types, int argc)
{
- if (fo->function()->kind != Function::AotCompiled) {
+ const ArrowFunction *self = static_cast<const ArrowFunction *>(fo);
+ Function *function = self->function();
+ if (function->kind != Function::AotCompiled) {
QV4::convertAndCall(fo->engine(), thisObject, a, types, argc,
[fo](const Value *thisObject, const Value *argv, int argc) {
return ArrowFunction::virtualCall(fo, thisObject, argv, argc);
@@ -499,15 +499,17 @@ void ArrowFunction::virtualCallWithMetaTypes(const FunctionObject *fo, QObject *
}
QV4::Scope scope(fo->engine());
- QV4::Scoped<ExecutionContext> context(scope, fo->scope());
+ QV4::Scoped<ExecutionContext> context(scope, self->scope());
MetaTypesStackFrame frame;
- frame.init(fo->function(), thisObject, context, a, types, argc);
+ frame.init(function, thisObject, context, a, types, argc);
frame.push(scope.engine);
Moth::VME::exec(&frame, scope.engine);
frame.pop(scope.engine);
}
-static ReturnedValue doCall(const FunctionObject *fo, const Value *thisObject, const Value *argv, int argc)
+static ReturnedValue qfoDoCall(
+ const QV4::JavaScriptFunctionObject *fo, const QV4::Value *thisObject,
+ const QV4::Value *argv, int argc)
{
ExecutionEngine *engine = fo->engine();
JSTypesStackFrame frame;
@@ -531,47 +533,39 @@ static ReturnedValue doCall(const FunctionObject *fo, const Value *thisObject, c
return result;
}
-ReturnedValue ArrowFunction::virtualCall(const FunctionObject *fo, const Value *thisObject, const Value *argv, int argc)
+ReturnedValue ArrowFunction::virtualCall(const QV4::FunctionObject *fo, const Value *thisObject,
+ const QV4::Value *argv, int argc)
{
- Function *function = fo->function();
+ const ArrowFunction *self = static_cast<const ArrowFunction *>(fo);
+ Function *function = self->function();
switch (function->kind) {
case Function::AotCompiled:
return QV4::convertAndCall(
- fo->engine(), function->typedFunction, thisObject, argv, argc,
+ fo->engine(), &function->aotCompiledFunction, thisObject, argv, argc,
[fo](QObject *thisObject, void **a, const QMetaType *types, int argc) {
ArrowFunction::virtualCallWithMetaTypes(fo, thisObject, a, types, argc);
});
case Function::JsTyped:
return QV4::coerceAndCall(
- fo->engine(), function->typedFunction, thisObject, argv, argc,
- [fo](const Value *thisObject, const Value *argv, int argc) {
- return doCall(fo, thisObject, argv, argc);
+ fo->engine(), &function->jsTypedFunction, function->compiledFunction, argv, argc,
+ [self, thisObject](const Value *argv, int argc) {
+ return qfoDoCall(self, thisObject, argv, argc);
});
default:
break;
}
- return doCall(fo, thisObject, argv, argc);
+ return qfoDoCall(self, thisObject, argv, argc);
}
void Heap::ArrowFunction::init(QV4::ExecutionContext *scope, Function *function, QV4::String *n)
{
- FunctionObject::init();
- this->scope.set(scope->engine(), scope->d());
-
- setFunction(function);
Q_ASSERT(function);
+ JavaScriptFunctionObject::init(scope, function, n);
Scope s(scope);
- ScopedFunctionObject f(s, this);
-
- ScopedString name(s, n ? n->d() : function->name());
- if (name)
- f->setName(name);
-
Q_ASSERT(internalClass && internalClass->verifyIndex(s.engine->id_length()->propertyKey(), Index_Length));
setProperty(s.engine, Index_Length, Value::fromInt32(int(function->compiledFunction->length)));
- canBeTailCalled = true;
}
void Heap::ScriptFunction::init(QV4::ExecutionContext *scope, Function *function)
@@ -584,6 +578,13 @@ void Heap::ScriptFunction::init(QV4::ExecutionContext *scope, Function *function
f->createDefaultPrototypeProperty(Heap::FunctionObject::Index_ProtoConstructor);
}
+void Heap::DefaultClassConstructorFunction::init(QV4::ExecutionContext *scope)
+{
+ Scope s(scope->engine());
+ FunctionObject::init(s.engine, nullptr);
+ this->scope.set(s.engine, scope->d());
+}
+
Heap::InternalClass *ScriptFunction::classForConstructor() const
{
Scope scope(engine());
@@ -611,8 +612,8 @@ ReturnedValue ConstructorFunction::virtualCallAsConstructor(const FunctionObject
ExecutionEngine *v4 = f->engine();
JSTypesStackFrame frame;
- frame.init(f->function(), argv, argc);
- frame.setupJSFrame(v4->jsStackTop, *f, f->scope(),
+ frame.init(c->function(), argv, argc);
+ frame.setupJSFrame(v4->jsStackTop, *f, c->scope(),
Value::emptyValue(),
newTarget ? *newTarget : Value::undefinedValue());
@@ -666,7 +667,7 @@ ReturnedValue DefaultClassConstructorFunction::virtualCallAsConstructor(const Fu
JSTypesStackFrame frame;
frame.init(nullptr, argv, argc);
- frame.setupJSFrame(v4->jsStackTop, *f, f->scope(),
+ frame.setupJSFrame(v4->jsStackTop, *f, c->scope(),
Value::undefinedValue(),
newTarget ? *newTarget : Value::undefinedValue(), argc, argc);
@@ -703,33 +704,39 @@ DEFINE_OBJECT_VTABLE(IndexedBuiltinFunction);
DEFINE_OBJECT_VTABLE(BoundFunction);
-void Heap::BoundFunction::init(QV4::ExecutionContext *scope, QV4::FunctionObject *target,
- const Value &boundThis, QV4::MemberData *boundArgs)
+void Heap::BoundFunction::init(
+ QV4::FunctionObject *target, const Value &boundThis, QV4::MemberData *boundArgs)
{
- Scope s(scope);
- Heap::FunctionObject::init(scope, QStringLiteral("__bound function__"));
+ ExecutionEngine *engine = target->engine();
+ Scope s(engine);
+ ScopedString name(s, engine->newString(QStringLiteral("__bound function__")));
+ if (auto *js = target->as<QV4::JavaScriptFunctionObject>()) {
+ ScopedContext ctx(s, js->scope());
+ JavaScriptFunctionObject::init(ctx, js->function(), name);
+ } else {
+ Q_ASSERT(name);
+ JavaScriptFunctionObject::init(engine->rootContext(), nullptr, name);
+ }
+
this->target.set(s.engine, target->d());
this->boundArgs.set(s.engine, boundArgs ? boundArgs->d() : nullptr);
- this->boundThis.set(scope->engine(), boundThis);
-
- if (!target->isConstructor())
- jsConstruct = nullptr;
+ this->boundThis.set(s.engine, boundThis);
ScopedObject f(s, this);
- ScopedValue l(s, target->get(s.engine->id_length()));
+ ScopedValue l(s, target->get(engine->id_length()));
int len = l->toUInt32();
if (boundArgs)
len -= boundArgs->size();
if (len < 0)
len = 0;
- f->defineReadonlyConfigurableProperty(s.engine->id_length(), Value::fromInt32(len));
+ f->defineReadonlyConfigurableProperty(engine->id_length(), Value::fromInt32(len));
ScopedProperty pd(s);
- pd->value = s.engine->thrower();
- pd->set = s.engine->thrower();
- f->insertMember(s.engine->id_arguments(), pd, Attr_Accessor|Attr_NotConfigurable|Attr_NotEnumerable);
- f->insertMember(s.engine->id_caller(), pd, Attr_Accessor|Attr_NotConfigurable|Attr_NotEnumerable);
+ pd->value = engine->thrower();
+ pd->set = engine->thrower();
+ f->insertMember(engine->id_arguments(), pd, Attr_Accessor|Attr_NotConfigurable|Attr_NotEnumerable);
+ f->insertMember(engine->id_caller(), pd, Attr_Accessor|Attr_NotConfigurable|Attr_NotEnumerable);
}
ReturnedValue BoundFunction::virtualCall(const FunctionObject *fo, const Value *, const Value *argv, int argc)
@@ -753,7 +760,10 @@ ReturnedValue BoundFunction::virtualCall(const FunctionObject *fo, const Value *
return checkedResult(v4, target->call(jsCallData));
}
-ReturnedValue BoundFunction::virtualCallAsConstructor(const FunctionObject *fo, const Value *argv, int argc, const Value *)
+DEFINE_OBJECT_VTABLE(BoundConstructor);
+
+ReturnedValue BoundConstructor::virtualCallAsConstructor(
+ const FunctionObject *fo, const Value *argv, int argc, const Value *)
{
const BoundFunction *f = static_cast<const BoundFunction *>(fo);
Scope scope(f->engine());
diff --git a/src/qml/jsruntime/qv4functionobject_p.h b/src/qml/jsruntime/qv4functionobject_p.h
index e32fce28ba..f4a2935b5a 100644
--- a/src/qml/jsruntime/qv4functionobject_p.h
+++ b/src/qml/jsruntime/qv4functionobject_p.h
@@ -28,37 +28,42 @@ namespace QV4 {
struct IndexedBuiltinFunction;
struct JSCallData;
-namespace Heap {
-
+// A FunctionObject is generally something that can be called, either with a JavaScript
+// signature (QV4::Value etc) or with a C++ signature (QMetaType etc). For this, it has
+// the Call and CallWithMetaTypes VTable entries.
+// Some FunctionObjects need to select the actual implementation of the call at run time.
+// This comese in two flavors:
+// 1. The implementation is a JavaScript function. For these we have
+// JavaScriptFunctionObject that holds a QV4::Function member to defer the call to.
+// 2. The implementation is a C++ function. For these we have DynamicFunctionObject that
+// holds another Call member in the heap object to defer the call to.
+// In addition, a FunctionObject may want to be called as constructor. For this we have
+// another VTable entry and a flag in the heap object.
-#define FunctionObjectMembers(class, Member) \
- Member(class, Pointer, ExecutionContext *, scope) \
- Member(class, NoMark, Function *, function) \
- Member(class, NoMark, VTable::Call, jsCall) \
- Member(class, NoMark, VTable::CallAsConstructor, jsConstruct) \
- Member(class, NoMark, VTable::CallWithMetaTypes, jsCallWithMetaTypes) \
- Member(class, NoMark, bool, canBeTailCalled)
+namespace Heap {
+#define FunctionObjectMembers(class, Member)
DECLARE_HEAP_OBJECT(FunctionObject, Object) {
- DECLARE_MARKOBJECTS(FunctionObject)
enum {
Index_ProtoConstructor = 0,
Index_Prototype = 0,
Index_HasInstance = 1,
};
- bool isConstructor() const {
- return jsConstruct != nullptr;
- }
+ Q_QML_EXPORT void init(QV4::ExecutionEngine *engine, QV4::String *name = nullptr);
+ Q_QML_EXPORT void init(QV4::ExecutionEngine *engine, const QString &name);
+ Q_QML_EXPORT void init();
+};
+
+#define JavaScriptFunctionObjectMembers(class, Member) \
+ Member(class, Pointer, ExecutionContext *, scope) \
+ Member(class, NoMark, Function *, function)
- Q_QML_PRIVATE_EXPORT void init(
- QV4::ExecutionContext *scope, QV4::String *name,
- VTable::Call call, VTable::CallWithMetaTypes callWithMetaTypes = nullptr);
- Q_QML_PRIVATE_EXPORT void init(QV4::ExecutionContext *scope, QV4::String *name = nullptr);
- Q_QML_PRIVATE_EXPORT void init(QV4::ExecutionContext *scope, QV4::Function *function, QV4::String *n = nullptr);
- Q_QML_PRIVATE_EXPORT void init(QV4::ExecutionContext *scope, const QString &name);
- Q_QML_PRIVATE_EXPORT void init();
- Q_QML_PRIVATE_EXPORT void destroy();
+DECLARE_HEAP_OBJECT(JavaScriptFunctionObject, FunctionObject) {
+ DECLARE_MARKOBJECTS(JavaScriptFunctionObject)
+
+ void init(QV4::ExecutionContext *scope, QV4::Function *function, QV4::String *n = nullptr);
+ Q_QML_EXPORT void destroy();
void setFunction(Function *f);
@@ -66,20 +71,32 @@ DECLARE_HEAP_OBJECT(FunctionObject, Object) {
unsigned int varCount() { return function ? function->compiledFunction->nLocals : 0; }
};
+#define DynamicFunctionObjectMembers(class, Member) \
+ Member(class, NoMark, VTable::Call, jsCall)
+
+DECLARE_HEAP_OBJECT(DynamicFunctionObject, FunctionObject) {
+ // NB: We might add a CallWithMetaTypes member to this struct and implement our
+ // builtins with metatypes, to be called from C++ code. This would make them
+ // available to qmlcachegen's C++ code generation.
+ void init(ExecutionEngine *engine, QV4::String *name, VTable::Call call);
+};
+
struct FunctionCtor : FunctionObject {
- void init(QV4::ExecutionContext *scope);
+ void init(QV4::ExecutionEngine *engine);
};
struct FunctionPrototype : FunctionObject {
void init();
};
-struct IndexedBuiltinFunction : FunctionObject {
- inline void init(QV4::ExecutionContext *scope, uint index, VTable::Call call);
- uint index;
+// A function object with an additional index into a list.
+// Used by Models to refer to property roles.
+struct IndexedBuiltinFunction : DynamicFunctionObject {
+ inline void init(QV4::ExecutionEngine *engine, qsizetype index, VTable::Call call);
+ qsizetype index;
};
-struct ArrowFunction : FunctionObject {
+struct ArrowFunction : JavaScriptFunctionObject {
enum {
Index_Name = Index_HasInstance + 1,
Index_Length
@@ -114,9 +131,15 @@ DECLARE_HEAP_OBJECT(ConstructorFunction, ScriptFunction) {
bool isDerivedConstructor;
};
-struct DefaultClassConstructorFunction : FunctionObject
-{
+#define DefaultClassConstructorFunctionMembers(class, Member) \
+ Member(class, Pointer, ExecutionContext *, scope)
+
+DECLARE_HEAP_OBJECT(DefaultClassConstructorFunction, FunctionObject) {
+ DECLARE_MARKOBJECTS(DefaultClassConstructorFunction)
+
bool isDerivedConstructor;
+
+ void init(QV4::ExecutionContext *scope);
};
#define BoundFunctionMembers(class, Member) \
@@ -124,66 +147,72 @@ struct DefaultClassConstructorFunction : FunctionObject
Member(class, HeapValue, HeapValue, boundThis) \
Member(class, Pointer, MemberData *, boundArgs)
-DECLARE_HEAP_OBJECT(BoundFunction, FunctionObject) {
+DECLARE_HEAP_OBJECT(BoundFunction, JavaScriptFunctionObject) {
DECLARE_MARKOBJECTS(BoundFunction)
- void init(QV4::ExecutionContext *scope, QV4::FunctionObject *target, const Value &boundThis, QV4::MemberData *boundArgs);
+ void init(QV4::FunctionObject *target, const Value &boundThis, QV4::MemberData *boundArgs);
};
+struct BoundConstructor : BoundFunction {};
+
}
struct Q_QML_EXPORT FunctionObject: Object {
- enum {
- IsFunctionObject = true
- };
V4_OBJECT2(FunctionObject, Object)
Q_MANAGED_TYPE(FunctionObject)
V4_INTERNALCLASS(FunctionObject)
V4_PROTOTYPE(functionPrototype)
- V4_NEEDS_DESTROY
enum { NInlineProperties = 1 };
- bool canBeTailCalled() const { return d()->canBeTailCalled; }
- Heap::ExecutionContext *scope() const { return d()->scope; }
- Function *function() const { return d()->function; }
+ bool canBeTailCalled() const { return vtable()->isTailCallable; }
ReturnedValue name() const;
- unsigned int formalParameterCount() const { return d()->formalParameterCount(); }
- unsigned int varCount() const { return d()->varCount(); }
void setName(String *name) {
defineReadonlyConfigurableProperty(engine()->id_name(), *name);
}
void createDefaultPrototypeProperty(uint protoConstructorSlot);
- inline ReturnedValue callAsConstructor(const JSCallData &data) const;
- ReturnedValue callAsConstructor(const Value *argv, int argc, const Value *newTarget = nullptr) const {
- if (!d()->jsConstruct)
- return engine()->throwTypeError(QStringLiteral("Function is not a constructor."));
- return d()->jsConstruct(this, argv, argc, newTarget ? newTarget : this);
+ ReturnedValue callAsConstructor(
+ const Value *argv, int argc, const Value *newTarget = nullptr) const
+ {
+ if (const auto callAsConstructor = vtable()->callAsConstructor)
+ return callAsConstructor(this, argv, argc, newTarget ? newTarget : this);
+ return failCallAsConstructor();
}
- inline ReturnedValue call(const JSCallData &data) const;
- ReturnedValue call(const Value *thisObject, const Value *argv, int argc) const {
- if (!d()->jsCall)
- return engine()->throwTypeError(QStringLiteral("Function can only be called with |new|."));
- return d()->jsCall(this, thisObject, argv, argc);
+
+ ReturnedValue call(const Value *thisObject, const Value *argv, int argc) const
+ {
+ if (const auto call = vtable()->call)
+ return call(this, thisObject, argv, argc);
+ return failCall();
+ }
+
+ void call(QObject *thisObject, void **argv, const QMetaType *types, int argc) const
+ {
+ if (const auto callWithMetaTypes = vtable()->callWithMetaTypes)
+ callWithMetaTypes(this, thisObject, argv, types, argc);
+ else
+ failCall();
}
- static ReturnedValue virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc);
- void call(QObject *thisObject, void **a, const QMetaType *types, int argc);
- static void virtualCallWithMetaTypes(const FunctionObject *f, QObject *thisObject,
- void **a, const QMetaType *types, int argc);
+
+ inline ReturnedValue callAsConstructor(const JSCallData &data) const;
+ inline ReturnedValue call(const JSCallData &data) const;
+
+ ReturnedValue failCall() const;
+ ReturnedValue failCallAsConstructor() const;
+ static void virtualConvertAndCall(
+ const FunctionObject *f, QObject *thisObject,
+ void **argv, const QMetaType *types, int argc);
static Heap::FunctionObject *createScriptFunction(ExecutionContext *scope, Function *function);
static Heap::FunctionObject *createConstructorFunction(ExecutionContext *scope, Function *function, Object *homeObject, bool isDerivedConstructor);
static Heap::FunctionObject *createMemberFunction(ExecutionContext *scope, Function *function, Object *homeObject, String *name);
static Heap::FunctionObject *createBuiltinFunction(ExecutionEngine *engine, StringOrSymbol *nameOrSymbol, VTable::Call code, int argumentCount);
- bool strictMode() const { return d()->function ? d()->function->isStrict() : false; }
bool isBinding() const;
bool isBoundFunction() const;
- bool isConstructor() const {
- return d()->isConstructor();
- }
+ bool isConstructor() const { return vtable()->callAsConstructor; }
ReturnedValue getHomeObject() const;
@@ -193,15 +222,40 @@ struct Q_QML_EXPORT FunctionObject: Object {
bool hasHasInstanceProperty() const {
return !internalClass()->propertyData.at(Heap::FunctionObject::Index_HasInstance).isEmpty();
}
-
- QQmlSourceLocation sourceLocation() const;
};
template<>
inline const FunctionObject *Value::as() const {
- return isManaged() && m()->internalClass->vtable->isFunctionObject ? reinterpret_cast<const FunctionObject *>(this) : nullptr;
+ if (!isManaged())
+ return nullptr;
+
+ const VTable *vtable = m()->internalClass->vtable;
+ return (vtable->call || vtable->callAsConstructor)
+ ? reinterpret_cast<const FunctionObject *>(this)
+ : nullptr;
}
+struct Q_QML_EXPORT JavaScriptFunctionObject: FunctionObject
+{
+ V4_OBJECT2(JavaScriptFunctionObject, FunctionObject)
+ V4_NEEDS_DESTROY
+
+ Heap::ExecutionContext *scope() const { return d()->scope; }
+
+ Function *function() const { return d()->function; }
+ unsigned int formalParameterCount() const { return d()->formalParameterCount(); }
+ unsigned int varCount() const { return d()->varCount(); }
+ bool strictMode() const { return d()->function ? d()->function->isStrict() : false; }
+ QQmlSourceLocation sourceLocation() const;
+};
+
+struct Q_QML_EXPORT DynamicFunctionObject: FunctionObject
+{
+ V4_OBJECT2(DynamicFunctionObject, FunctionObject)
+
+ static ReturnedValue virtualCall(
+ const FunctionObject *f, const Value *thisObject, const Value *argv, int argc);
+};
struct FunctionCtor: FunctionObject
{
@@ -223,6 +277,9 @@ struct FunctionPrototype: FunctionObject
void init(ExecutionEngine *engine, Object *ctor);
+ static ReturnedValue virtualCall(
+ const FunctionObject *f, const Value *thisObject, const Value *argv, int argc);
+
static ReturnedValue method_toString(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
static ReturnedValue method_apply(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
static ReturnedValue method_call(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
@@ -230,26 +287,31 @@ struct FunctionPrototype: FunctionObject
static ReturnedValue method_hasInstance(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
};
-struct Q_QML_PRIVATE_EXPORT IndexedBuiltinFunction : FunctionObject
+struct Q_QML_EXPORT IndexedBuiltinFunction : DynamicFunctionObject
{
- V4_OBJECT2(IndexedBuiltinFunction, FunctionObject)
+ V4_OBJECT2(IndexedBuiltinFunction, DynamicFunctionObject)
};
-void Heap::IndexedBuiltinFunction::init(QV4::ExecutionContext *scope, uint index, VTable::Call call)
+void Heap::IndexedBuiltinFunction::init(
+ QV4::ExecutionEngine *engine, qsizetype index, VTable::Call call)
{
- Heap::FunctionObject::init(scope);
+ Heap::FunctionObject::init(engine);
this->jsCall = call;
this->index = index;
}
-struct ArrowFunction : FunctionObject {
- V4_OBJECT2(ArrowFunction, FunctionObject)
+struct ArrowFunction : JavaScriptFunctionObject {
+ V4_OBJECT2(ArrowFunction, JavaScriptFunctionObject)
V4_INTERNALCLASS(ArrowFunction)
- enum { NInlineProperties = 3 };
+ enum {
+ NInlineProperties = 3,
+ IsTailCallable = true,
+ };
static void virtualCallWithMetaTypes(const FunctionObject *f, QObject *thisObject,
void **a, const QMetaType *types, int argc);
- static ReturnedValue virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc);
+ static ReturnedValue virtualCall(const QV4::FunctionObject *f, const QV4::Value *thisObject,
+ const QV4::Value *argv, int argc);
};
struct ScriptFunction : ArrowFunction {
@@ -275,29 +337,33 @@ struct ConstructorFunction : ScriptFunction {
struct DefaultClassConstructorFunction : FunctionObject {
V4_OBJECT2(DefaultClassConstructorFunction, FunctionObject)
+
+ Heap::ExecutionContext *scope() const { return d()->scope; }
static ReturnedValue virtualCallAsConstructor(const FunctionObject *, const Value *argv, int argc, const Value *);
static ReturnedValue virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc);
};
-struct BoundFunction: FunctionObject {
- V4_OBJECT2(BoundFunction, FunctionObject)
-
- static Heap::BoundFunction *create(ExecutionContext *scope, FunctionObject *target, const Value &boundThis, QV4::MemberData *boundArgs)
- {
- return scope->engine()->memoryManager->allocate<BoundFunction>(scope, target, boundThis, boundArgs);
- }
+struct BoundFunction: JavaScriptFunctionObject {
+ V4_OBJECT2(BoundFunction, JavaScriptFunctionObject)
Heap::FunctionObject *target() const { return d()->target; }
Value boundThis() const { return d()->boundThis; }
Heap::MemberData *boundArgs() const { return d()->boundArgs; }
- static ReturnedValue virtualCallAsConstructor(const FunctionObject *, const Value *argv, int argc, const Value *);
static ReturnedValue virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc);
};
+struct BoundConstructor: BoundFunction {
+ V4_OBJECT2(BoundConstructor, BoundFunction)
+
+ static ReturnedValue virtualCallAsConstructor(
+ const FunctionObject *f, const Value *argv, int argc, const Value *);
+};
+
inline bool FunctionObject::isBoundFunction() const
{
- return d()->vtable() == BoundFunction::staticVTable();
+ const VTable *vtable = d()->vtable();
+ return vtable == BoundFunction::staticVTable() || vtable == BoundConstructor::staticVTable();
}
inline ReturnedValue checkedResult(QV4::ExecutionEngine *v4, ReturnedValue result)
diff --git a/src/qml/jsruntime/qv4generatorobject.cpp b/src/qml/jsruntime/qv4generatorobject.cpp
index 9f77d83ac3..f2d7dffde5 100644
--- a/src/qml/jsruntime/qv4generatorobject.cpp
+++ b/src/qml/jsruntime/qv4generatorobject.cpp
@@ -13,9 +13,9 @@ DEFINE_OBJECT_VTABLE(GeneratorFunctionCtor);
DEFINE_OBJECT_VTABLE(GeneratorFunction);
DEFINE_OBJECT_VTABLE(GeneratorObject);
-void Heap::GeneratorFunctionCtor::init(QV4::ExecutionContext *scope)
+void Heap::GeneratorFunctionCtor::init(QV4::ExecutionEngine *engine)
{
- Heap::FunctionObject::init(scope, QStringLiteral("GeneratorFunction"));
+ Heap::FunctionObject::init(engine, QStringLiteral("GeneratorFunction"));
}
ReturnedValue GeneratorFunctionCtor::virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *newTarget)
@@ -26,7 +26,7 @@ ReturnedValue GeneratorFunctionCtor::virtualCallAsConstructor(const FunctionObje
if (engine->hasException)
return Encode::undefined();
- Function *vmf = compilationUnit->linkToEngine(engine);
+ Function *vmf = compilationUnit->rootFunction();
ExecutionContext *global = engine->scriptContext();
ReturnedValue o = Encode(GeneratorFunction::create(global, vmf));
@@ -62,7 +62,7 @@ ReturnedValue GeneratorFunction::virtualCall(const FunctionObject *f, const Valu
ExecutionEngine *engine = gf->engine();
Scope scope(gf);
- Scoped<GeneratorObject> g(scope, engine->memoryManager->allocManaged<GeneratorObject>(sizeof(GeneratorObject::Data), engine->classes[EngineBase::Class_GeneratorObject]));
+ Scoped<GeneratorObject> g(scope, engine->memoryManager->allocManaged<GeneratorObject>(engine->classes[EngineBase::Class_GeneratorObject]));
g->setPrototypeOf(ScopedObject(scope, gf->get(scope.engine->id_prototype())));
// We need to set up a separate JSFrame for the generator, as it's being re-entered
diff --git a/src/qml/jsruntime/qv4generatorobject_p.h b/src/qml/jsruntime/qv4generatorobject_p.h
index 55ccc133aa..cb2c1962c5 100644
--- a/src/qml/jsruntime/qv4generatorobject_p.h
+++ b/src/qml/jsruntime/qv4generatorobject_p.h
@@ -33,7 +33,7 @@ enum class GeneratorState {
namespace Heap {
struct GeneratorFunctionCtor : FunctionObject {
- void init(QV4::ExecutionContext *scope);
+ void init(ExecutionEngine *engine);
};
struct GeneratorFunction : ArrowFunction {
diff --git a/src/qml/jsruntime/qv4global_p.h b/src/qml/jsruntime/qv4global_p.h
index 8279fa00f5..e3fc0ac1b3 100644
--- a/src/qml/jsruntime/qv4global_p.h
+++ b/src/qml/jsruntime/qv4global_p.h
@@ -80,6 +80,7 @@ namespace Heap {
struct ArrayObject;
struct DateObject;
struct FunctionObject;
+ struct JavaScriptFunctionObject;
struct ErrorObject;
struct ArgumentsObject;
struct QObjectWrapper;
@@ -191,6 +192,8 @@ Q_DECLARE_OPERATORS_FOR_FLAGS(PropertyFlags)
struct PropertyAttributes
{
+ QT_WARNING_PUSH
+ QT_WARNING_DISABLE_MSVC(4201) // nonstandard extension used: nameless struct/union
union {
uchar m_all;
struct {
@@ -208,6 +211,7 @@ struct PropertyAttributes
uchar configurable_set : 1;
};
};
+ QT_WARNING_POP
enum Type {
Data = 0,
diff --git a/src/qml/jsruntime/qv4globalobject.cpp b/src/qml/jsruntime/qv4globalobject.cpp
index 32e93cedb4..989de0de23 100644
--- a/src/qml/jsruntime/qv4globalobject.cpp
+++ b/src/qml/jsruntime/qv4globalobject.cpp
@@ -2,25 +2,28 @@
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qv4globalobject_p.h"
-#include <private/qv4mm_p.h>
-#include "qv4value_p.h"
-#include "qv4context_p.h"
-#include "qv4function_p.h"
-#include "qv4script_p.h"
-#include "qv4scopedvalue_p.h"
-#include "qv4string_p.h"
-#include <private/qv4codegen_p.h>
#include <private/qv4alloca_p.h>
-#include "private/qlocale_tools_p.h"
-#include "private/qtools_p.h"
-
-#include <QtCore/QDebug>
-#include <QtCore/QString>
-#include <iostream>
+#include <private/qv4codegen_p.h>
+#include <private/qv4context_p.h>
+#include <private/qv4function_p.h>
+#include <private/qv4mm_p.h>
+#include <private/qv4scopedvalue_p.h>
+#include <private/qv4script_p.h>
+#include <private/qv4stackframe_p.h>
+#include <private/qv4string_p.h>
+#include <private/qv4value_p.h>
#include <wtf/MathExtras.h>
+#include <QtCore/private/qlocale_tools_p.h>
+#include <QtCore/private/qtools_p.h>
+
+#include <QtCore/qdebug.h>
+#include <QtCore/qstring.h>
+
+#include <iostream>
+
using namespace QV4;
using QtMiscUtils::toHexUpper;
using QtMiscUtils::fromHex;
@@ -287,10 +290,10 @@ static QString decode(const QString &input, DecodeMode decodeMode, bool *ok)
DEFINE_OBJECT_VTABLE(EvalFunction);
-void Heap::EvalFunction::init(QV4::ExecutionContext *scope)
+void Heap::EvalFunction::init(QV4::ExecutionEngine *engine)
{
- Scope s(scope);
- Heap::FunctionObject::init(scope, s.engine->id_eval());
+ Scope s(engine);
+ Heap::FunctionObject::init(engine, s.engine->id_eval());
ScopedFunctionObject f(s, this);
f->defineReadonlyConfigurableProperty(s.engine->id_length(), Value::fromInt32(1));
}
diff --git a/src/qml/jsruntime/qv4globalobject_p.h b/src/qml/jsruntime/qv4globalobject_p.h
index 71e06ef417..fd23d71332 100644
--- a/src/qml/jsruntime/qv4globalobject_p.h
+++ b/src/qml/jsruntime/qv4globalobject_p.h
@@ -24,7 +24,7 @@ namespace QV4 {
namespace Heap {
struct EvalFunction : FunctionObject {
- void init(QV4::ExecutionContext *scope);
+ void init(ExecutionEngine *engine);
};
}
diff --git a/src/qml/jsruntime/qv4identifierhash.cpp b/src/qml/jsruntime/qv4identifierhash.cpp
index 3cc8aa2194..48df2283f0 100644
--- a/src/qml/jsruntime/qv4identifierhash.cpp
+++ b/src/qml/jsruntime/qv4identifierhash.cpp
@@ -91,7 +91,7 @@ const IdentifierHashEntry *IdentifierHash::lookup(const QString &str) const
if (!d)
return nullptr;
- PropertyKey id = d->identifierTable->asPropertyKey(str);
+ PropertyKey id = d->identifierTable->asPropertyKey(str, IdentifierTable::ForceConversionToId);
return lookup(id);
}
@@ -110,7 +110,7 @@ inline
const PropertyKey IdentifierHash::toIdentifier(const QString &str) const
{
Q_ASSERT(d);
- return d->identifierTable->asPropertyKey(str);
+ return d->identifierTable->asPropertyKey(str, IdentifierTable::ForceConversionToId);
}
inline
diff --git a/src/qml/jsruntime/qv4identifierhash_p.h b/src/qml/jsruntime/qv4identifierhash_p.h
index ad3c179502..6c77a78f85 100644
--- a/src/qml/jsruntime/qv4identifierhash_p.h
+++ b/src/qml/jsruntime/qv4identifierhash_p.h
@@ -24,7 +24,7 @@ namespace QV4 {
struct IdentifierHashEntry;
struct IdentifierHashData;
-struct Q_QML_PRIVATE_EXPORT IdentifierHash
+struct Q_QML_EXPORT IdentifierHash
{
IdentifierHash() = default;
IdentifierHash(ExecutionEngine *engine);
diff --git a/src/qml/jsruntime/qv4identifiertable.cpp b/src/qml/jsruntime/qv4identifiertable.cpp
index 96aa54018b..4c915442f4 100644
--- a/src/qml/jsruntime/qv4identifiertable.cpp
+++ b/src/qml/jsruntime/qv4identifiertable.cpp
@@ -36,7 +36,7 @@ void IdentifierTable::addEntry(Heap::StringOrSymbol *str)
if (str->subtype == Heap::String::StringType_ArrayIndex)
return;
- str->identifier = PropertyKey::fromStringOrSymbol(str);
+ str->identifier = PropertyKey::fromStringOrSymbol(engine, str);
bool grow = (alloc <= size*2);
@@ -165,6 +165,9 @@ PropertyKey IdentifierTable::asPropertyKeyImpl(const Heap::String *str)
while (Heap::StringOrSymbol *e = entriesByHash[idx]) {
if (e->stringHash == hash && e->toQString() == str->toQString()) {
str->identifier = e->identifier;
+ QV4::WriteBarrier::markCustom(engine, [&](QV4::MarkStack *stack) {
+ e->identifier.asStringOrSymbol()->mark(stack);
+ });
return e->identifier;
}
++idx;
@@ -249,24 +252,20 @@ void IdentifierTable::sweep()
size -= freed;
}
-PropertyKey IdentifierTable::asPropertyKey(const QString &s)
+PropertyKey IdentifierTable::asPropertyKey(const QString &s,
+ IdentifierTable::KeyConversionBehavior conversionBehvior)
{
uint subtype;
- const uint hash = String::createHashValue(s.constData(), s.size(), &subtype);
- if (subtype == Heap::String::StringType_ArrayIndex)
- return PropertyKey::fromArrayIndex(hash);
+ uint hash = String::createHashValue(s.constData(), s.size(), &subtype);
+ if (subtype == Heap::String::StringType_ArrayIndex) {
+ if (Q_UNLIKELY(conversionBehvior == ForceConversionToId))
+ hash = String::createHashValueDisallowingArrayIndex(s.constData(), s.size(), &subtype);
+ else
+ return PropertyKey::fromArrayIndex(hash);
+ }
return resolveStringEntry(s, hash, subtype)->identifier;
}
-PropertyKey IdentifierTable::asPropertyKey(const char *s, int len)
-{
- uint subtype;
- uint hash = String::createHashValue(s, len, &subtype);
- if (subtype == Heap::String::StringType_ArrayIndex)
- return PropertyKey::fromArrayIndex(hash);
- return resolveStringEntry(QString::fromLatin1(s, len), hash, subtype)->identifier;
-}
-
}
QT_END_NAMESPACE
diff --git a/src/qml/jsruntime/qv4identifiertable_p.h b/src/qml/jsruntime/qv4identifiertable_p.h
index 09e07fe5b8..2ecd4a7294 100644
--- a/src/qml/jsruntime/qv4identifiertable_p.h
+++ b/src/qml/jsruntime/qv4identifiertable_p.h
@@ -24,7 +24,7 @@ QT_BEGIN_NAMESPACE
namespace QV4 {
-struct Q_QML_PRIVATE_EXPORT IdentifierTable
+struct Q_QML_EXPORT IdentifierTable
{
ExecutionEngine *engine;
@@ -55,8 +55,8 @@ public:
return asPropertyKey(str->d());
}
- PropertyKey asPropertyKey(const QString &s);
- PropertyKey asPropertyKey(const char *s, int len);
+ enum KeyConversionBehavior { Default, ForceConversionToId };
+ PropertyKey asPropertyKey(const QString &s, KeyConversionBehavior conversionBehavior = Default);
PropertyKey asPropertyKeyImpl(const Heap::String *str);
diff --git a/src/qml/jsruntime/qv4include.cpp b/src/qml/jsruntime/qv4include.cpp
index 67572b50a9..76545ba692 100644
--- a/src/qml/jsruntime/qv4include.cpp
+++ b/src/qml/jsruntime/qv4include.cpp
@@ -26,7 +26,7 @@ QV4Include::QV4Include(const QUrl &url, QV4::ExecutionEngine *engine,
: QObject(engine->jsEngine())
, v4(engine), m_url(url)
#if QT_CONFIG(qml_network)
- , m_redirectCount(0), m_network(nullptr) , m_reply(nullptr)
+ , m_network(nullptr) , m_reply(nullptr)
#endif
{
if (qmlContext)
@@ -104,27 +104,9 @@ QV4::ReturnedValue QV4Include::result()
return m_resultObject.value();
}
-#define INCLUDE_MAXIMUM_REDIRECT_RECURSION 15
void QV4Include::finished()
{
#if QT_CONFIG(qml_network)
- m_redirectCount++;
-
- if (m_redirectCount < INCLUDE_MAXIMUM_REDIRECT_RECURSION) {
- QVariant redirect = m_reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
- if (redirect.isValid()) {
- m_url = m_url.resolved(redirect.toUrl());
- delete m_reply;
-
- QNetworkRequest request;
- request.setUrl(m_url);
-
- m_reply = m_network->get(request);
- QObject::connect(m_reply, SIGNAL(finished()), this, SLOT(finished()));
- return;
- }
- }
-
QV4::Scope scope(v4);
QV4::ScopedObject resultObj(scope, m_resultObject.value());
QV4::ScopedString status(scope, v4->newString(QStringLiteral("status")));
diff --git a/src/qml/jsruntime/qv4include_p.h b/src/qml/jsruntime/qv4include_p.h
index c5f6560b08..c6ac98c761 100644
--- a/src/qml/jsruntime/qv4include_p.h
+++ b/src/qml/jsruntime/qv4include_p.h
@@ -63,7 +63,6 @@ private:
QUrl m_url;
#if QT_CONFIG(qml_network)
- int m_redirectCount;
QNetworkAccessManager *m_network;
QPointer<QNetworkReply> m_reply;
#endif
diff --git a/src/qml/jsruntime/qv4internalclass.cpp b/src/qml/jsruntime/qv4internalclass.cpp
index c72effd406..228a6bcd36 100644
--- a/src/qml/jsruntime/qv4internalclass.cpp
+++ b/src/qml/jsruntime/qv4internalclass.cpp
@@ -67,12 +67,11 @@ void PropertyHash::detach(bool grow, int classSize)
SharedInternalClassDataPrivate<PropertyKey>::SharedInternalClassDataPrivate(const SharedInternalClassDataPrivate<PropertyKey> &other)
: refcount(1),
- engine(other.engine),
- data(nullptr)
+ engine(other.engine)
{
if (other.alloc()) {
const uint s = other.size();
- data = MemberData::allocate(engine, other.alloc(), other.data);
+ data.set(engine, MemberData::allocate(engine, other.alloc(), other.data));
setSize(s);
}
}
@@ -82,7 +81,7 @@ SharedInternalClassDataPrivate<PropertyKey>::SharedInternalClassDataPrivate(cons
: refcount(1),
engine(other.engine)
{
- data = MemberData::allocate(engine, other.alloc(), nullptr);
+ data.set(engine, MemberData::allocate(engine, other.alloc(), nullptr));
memcpy(data, other.data, sizeof(Heap::MemberData) - sizeof(Value) + pos*sizeof(Value));
data->values.size = pos + 1;
data->values.set(engine, pos, Value::fromReturnedValue(value.id()));
@@ -92,7 +91,7 @@ void SharedInternalClassDataPrivate<PropertyKey>::grow()
{
const uint a = alloc() * 2;
const uint s = size();
- data = MemberData::allocate(engine, a, data);
+ data.set(engine, MemberData::allocate(engine, a, data));
setSize(s);
Q_ASSERT(alloc() >= a);
}
@@ -122,6 +121,11 @@ PropertyKey SharedInternalClassDataPrivate<PropertyKey>::at(uint i) const
void SharedInternalClassDataPrivate<PropertyKey>::set(uint i, PropertyKey t)
{
Q_ASSERT(data && i < size());
+ QV4::WriteBarrier::markCustom(engine, [&](QV4::MarkStack *stack) {
+ if constexpr (QV4::WriteBarrier::isInsertionBarrier)
+ if (auto string = t.asStringOrSymbol())
+ string->mark(stack);
+ });
data->values.values[i].rawValueRef() = t.id();
}
@@ -227,7 +231,7 @@ void InternalClass::init(ExecutionEngine *engine)
new (&propertyTable) PropertyHash();
new (&nameMap) SharedInternalClassData<PropertyKey>(engine);
new (&propertyData) SharedInternalClassData<PropertyAttributes>(engine);
- new (&transitions) std::vector<Transition>();
+ new (&transitions) QVarLengthArray<Transition, 1>();
this->engine = engine;
vtable = QV4::InternalClass::staticVTable();
@@ -244,7 +248,7 @@ void InternalClass::init(Heap::InternalClass *other)
new (&propertyTable) PropertyHash(other->propertyTable);
new (&nameMap) SharedInternalClassData<PropertyKey>(other->nameMap);
new (&propertyData) SharedInternalClassData<PropertyAttributes>(other->propertyData);
- new (&transitions) std::vector<Transition>();
+ new (&transitions) QVarLengthArray<Transition, 1>();
engine = other->engine;
vtable = other->vtable;
@@ -256,6 +260,11 @@ void InternalClass::init(Heap::InternalClass *other)
protoId = engine->newProtoId();
internalClass.set(engine, other->internalClass);
+ QV4::WriteBarrier::markCustom(engine, [&](QV4::MarkStack *stack) {
+ if constexpr (QV4::WriteBarrier::isInsertionBarrier) {
+ other->mark(stack);
+ }
+ });
}
void InternalClass::destroy()
@@ -275,14 +284,20 @@ void InternalClass::destroy()
propertyTable.~PropertyHash();
nameMap.~SharedInternalClassData<PropertyKey>();
propertyData.~SharedInternalClassData<PropertyAttributes>();
- transitions.~vector<Transition>();
+ transitions.~QVarLengthArray<Transition, 1>();
engine = nullptr;
Base::destroy();
}
-QString InternalClass::keyAt(uint index) const
+ReturnedValue InternalClass::keyAt(uint index) const
{
- return nameMap.at(index).toQString();
+ PropertyKey key = nameMap.at(index);
+ if (!key.isValid())
+ return Encode::undefined();
+ if (key.isArrayIndex())
+ return Encode(key.asArrayIndex());
+ Q_ASSERT(key.isStringOrSymbol());
+ return key.asStringOrSymbol()->asReturnedValue();
}
void InternalClass::changeMember(QV4::Object *object, PropertyKey id, PropertyAttributes data, InternalClassEntry *entry)
@@ -296,7 +311,7 @@ void InternalClass::changeMember(QV4::Object *object, PropertyKey id, PropertyAt
InternalClassTransition &InternalClass::lookupOrInsertTransition(const InternalClassTransition &t)
{
- std::vector<Transition>::iterator it = std::lower_bound(transitions.begin(), transitions.end(), t);
+ QVarLengthArray<Transition, 1>::iterator it = std::lower_bound(transitions.begin(), transitions.end(), t);
if (it != transitions.end() && *it == t) {
return *it;
} else {
@@ -328,7 +343,7 @@ static Heap::InternalClass *cleanInternalClass(Heap::InternalClass *orig)
// We will generally add quite a few transitions here. We have 255 redundant ones.
// We can expect at least as many significant ones in addition.
- std::vector<InternalClassTransition> transitions;
+ QVarLengthArray<InternalClassTransition, 1> transitions;
Scope scope(orig->engine);
Scoped<QV4::InternalClass> child(scope, orig);
@@ -470,6 +485,10 @@ Heap::InternalClass *InternalClass::changePrototypeImpl(Heap::Object *proto)
// create a new class and add it to the tree
Heap::InternalClass *newClass = engine->newClass(this);
+ QV4::WriteBarrier::markCustom(engine, [&](QV4::MarkStack *stack) {
+ if (proto && QV4::WriteBarrier::isInsertionBarrier)
+ proto->mark(stack);
+ });
newClass->prototype = proto;
t.lookup = newClass;
diff --git a/src/qml/jsruntime/qv4internalclass_p.h b/src/qml/jsruntime/qv4internalclass_p.h
index df73ee6ccc..56ce787859 100644
--- a/src/qml/jsruntime/qv4internalclass_p.h
+++ b/src/qml/jsruntime/qv4internalclass_p.h
@@ -17,6 +17,7 @@
#include "qv4global_p.h"
#include <QHash>
+#include <QVarLengthArray>
#include <climits> // for UINT_MAX
#include <private/qv4propertykey_p.h>
#include <private/qv4heap_p.h>
@@ -132,6 +133,8 @@ struct SharedInternalClassDataPrivate<PropertyAttributes> {
void grow();
+ void markIfNecessary(const PropertyAttributes &) {}
+
uint alloc() const { return m_alloc; }
uint size() const { return m_size; }
void setSize(uint s) { m_size = s; }
@@ -174,11 +177,14 @@ private:
template<>
struct SharedInternalClassDataPrivate<PropertyKey> {
- SharedInternalClassDataPrivate(ExecutionEngine *e) : refcount(1), engine(e), data(nullptr) {}
+ SharedInternalClassDataPrivate(ExecutionEngine *e) : refcount(1), engine(e) {}
SharedInternalClassDataPrivate(const SharedInternalClassDataPrivate &other);
SharedInternalClassDataPrivate(const SharedInternalClassDataPrivate &other, uint pos, PropertyKey value);
~SharedInternalClassDataPrivate() {}
+ template<typename StringOrSymbol = Heap::StringOrSymbol>
+ void markIfNecessary(const PropertyKey &value);
+
void grow();
uint alloc() const;
uint size() const;
@@ -192,9 +198,20 @@ struct SharedInternalClassDataPrivate<PropertyKey> {
int refcount = 1;
private:
ExecutionEngine *engine;
- Heap::MemberData *data;
+ WriteBarrier::Pointer<Heap::MemberData> data;
};
+template<typename StringOrSymbol>
+void QV4::SharedInternalClassDataPrivate<PropertyKey>::markIfNecessary(const PropertyKey &value)
+{
+ QV4::WriteBarrier::markCustom(engine, [&](QV4::MarkStack *stack) {
+ if constexpr (QV4::WriteBarrier::isInsertionBarrier) {
+ if (auto s = value.asStringOrSymbol<StringOrSymbol>())
+ s->mark(stack);
+ }
+ });
+}
+
template <typename T>
struct SharedInternalClassData {
using Private = SharedInternalClassDataPrivate<T>;
@@ -222,6 +239,7 @@ struct SharedInternalClassData {
}
void add(uint pos, T value) {
+ d->markIfNecessary(value);
if (pos < d->size()) {
Q_ASSERT(d->refcount > 1);
// need to detach
@@ -243,6 +261,7 @@ struct SharedInternalClassData {
void set(uint pos, T value) {
Q_ASSERT(pos < d->size());
+ d->markIfNecessary(value);
if (d->refcount > 1) {
// need to detach
Private *dd = new Private(*d);
@@ -315,7 +334,7 @@ struct InternalClass : Base {
SharedInternalClassData<PropertyAttributes> propertyData;
typedef InternalClassTransition Transition;
- std::vector<Transition> transitions;
+ QVarLengthArray<Transition, 1> transitions;
InternalClassTransition &lookupOrInsertTransition(const InternalClassTransition &t);
uint size;
@@ -332,7 +351,7 @@ struct InternalClass : Base {
void init(InternalClass *other);
void destroy();
- Q_QML_PRIVATE_EXPORT QString keyAt(uint index) const;
+ Q_QML_EXPORT ReturnedValue keyAt(uint index) const;
Q_REQUIRED_RESULT InternalClass *nonExtensible();
Q_REQUIRED_RESULT InternalClass *locked();
diff --git a/src/qml/jsruntime/qv4jscall.cpp b/src/qml/jsruntime/qv4jscall.cpp
index 350e0fe542..513ae59145 100644
--- a/src/qml/jsruntime/qv4jscall.cpp
+++ b/src/qml/jsruntime/qv4jscall.cpp
@@ -3,6 +3,8 @@
#include "qv4jscall_p.h"
+#include <QtQml/qqmlinfo.h>
+
#include <private/qqmlengine_p.h>
#include <private/qv4qobjectwrapper_p.h>
@@ -20,4 +22,25 @@ void QV4::populateJSCallArguments(ExecutionEngine *v4, JSCallArguments &jsCall,
jsCall.args[ii] = v4->metaTypeToJS(types[ii], args[ii + 1]);
}
+void QV4::warnAboutCoercionToVoid(
+ ExecutionEngine *engine, const Value &value, CoercionProblem problem)
+{
+ auto log = qCritical().nospace().noquote();
+ if (const CppStackFrame *frame = engine->currentStackFrame)
+ log << frame->source() << ':' << frame->lineNumber() << ": ";
+ log << value.toQStringNoThrow()
+ << " should be coerced to void because";
+ switch (problem) {
+ case InsufficientAnnotation:
+ log << " the function called is insufficiently annotated.";
+ break;
+ case InvalidListType:
+ log << " the target type, a list of unknown elements, cannot be resolved.";
+ break;
+ }
+
+ log << " The original value is retained. This will change in a future version of Qt.";
+}
+
+
QT_END_NAMESPACE
diff --git a/src/qml/jsruntime/qv4jscall_p.h b/src/qml/jsruntime/qv4jscall_p.h
index 4126a25758..43776e4b62 100644
--- a/src/qml/jsruntime/qv4jscall_p.h
+++ b/src/qml/jsruntime/qv4jscall_p.h
@@ -14,14 +14,23 @@
// We mean it.
//
-#include "qv4object_p.h"
-#include "qv4function_p.h"
-#include "qv4functionobject_p.h"
-#include "qv4context_p.h"
-#include "qv4scopedvalue_p.h"
-#include "qv4stackframe_p.h"
-#include "qv4qobjectwrapper_p.h"
+#include <private/qqmllistwrapper_p.h>
+#include <private/qqmlvaluetypewrapper_p.h>
+
#include <private/qv4alloca_p.h>
+#include <private/qv4dateobject_p.h>
+#include <private/qv4function_p.h>
+#include <private/qv4functionobject_p.h>
+#include <private/qv4qobjectwrapper_p.h>
+#include <private/qv4regexpobject_p.h>
+#include <private/qv4scopedvalue_p.h>
+#include <private/qv4sequenceobject_p.h>
+#include <private/qv4urlobject_p.h>
+#include <private/qv4variantobject_p.h>
+
+#if QT_CONFIG(regularexpression)
+#include <QtCore/qregularexpression.h>
+#endif
QT_BEGIN_NAMESPACE
@@ -99,47 +108,32 @@ ReturnedValue FunctionObject::call(const JSCallData &data) const
void populateJSCallArguments(ExecutionEngine *v4, JSCallArguments &jsCall, int argc,
void **args, const QMetaType *types);
-struct ScopedStackFrame
-{
- ScopedStackFrame(const Scope &scope, ExecutionContext *context)
- : engine(scope.engine)
- {
- if (auto currentFrame = engine->currentStackFrame) {
- frame.init(currentFrame->v4Function, nullptr, context, nullptr, nullptr, 0);
- frame.instructionPointer = currentFrame->instructionPointer;
- } else {
- frame.init(nullptr, nullptr, context, nullptr, nullptr, 0);
- }
- frame.push(engine);
- }
-
- ~ScopedStackFrame()
- {
- frame.pop(engine);
- }
-
-private:
- ExecutionEngine *engine = nullptr;
- MetaTypesStackFrame frame;
-};
-
template<typename Callable>
ReturnedValue convertAndCall(
- ExecutionEngine *engine, const QQmlPrivate::TypedFunction *aotFunction,
+ ExecutionEngine *engine, const Function::AOTCompiledFunction *aotFunction,
const Value *thisObject, const Value *argv, int argc, Callable call)
{
- const qsizetype numFunctionArguments = aotFunction->argumentTypes.size();
+ const qsizetype numFunctionArguments = aotFunction->types.length() - 1;
Q_ALLOCA_VAR(void *, values, (numFunctionArguments + 1) * sizeof(void *));
Q_ALLOCA_VAR(QMetaType, types, (numFunctionArguments + 1) * sizeof(QMetaType));
for (qsizetype i = 0; i < numFunctionArguments; ++i) {
- const QMetaType argumentType = aotFunction->argumentTypes[i];
+ const QMetaType argumentType = aotFunction->types[i + 1];
types[i + 1] = argumentType;
if (const qsizetype argumentSize = argumentType.sizeOf()) {
Q_ALLOCA_VAR(void, argument, argumentSize);
- argumentType.construct(argument);
- if (i < argc)
- ExecutionEngine::metaTypeFromJS(argv[i], argumentType, argument);
+ if (argumentType.flags() & QMetaType::NeedsConstruction) {
+ argumentType.construct(argument);
+ if (i < argc)
+ ExecutionEngine::metaTypeFromJS(argv[i], argumentType, argument);
+ } else if (i >= argc
+ || !ExecutionEngine::metaTypeFromJS(argv[i], argumentType, argument)) {
+ // If we can't convert the argument, we need to default-construct it even if it
+ // doesn't formally need construction.
+ // E.g. an int doesn't need construction, but we still want it to be 0.
+ argumentType.construct(argument);
+ }
+
values[i + 1] = argument;
} else {
values[i + 1] = nullptr;
@@ -147,29 +141,37 @@ ReturnedValue convertAndCall(
}
Q_ALLOCA_DECLARE(void, returnValue);
- types[0] = aotFunction->returnType;
+ types[0] = aotFunction->types[0];
if (const qsizetype returnSize = types[0].sizeOf()) {
Q_ALLOCA_ASSIGN(void, returnValue, returnSize);
values[0] = returnValue;
+ if (types[0].flags() & QMetaType::NeedsConstruction)
+ types[0].construct(returnValue);
} else {
values[0] = nullptr;
}
- if (const QV4::QObjectWrapper *cppThisObject = thisObject->as<QV4::QObjectWrapper>())
+ if (const QV4::QObjectWrapper *cppThisObject = thisObject
+ ? thisObject->as<QV4::QObjectWrapper>()
+ : nullptr) {
call(cppThisObject->object(), values, types, argc);
- else
+ } else {
call(nullptr, values, types, argc);
+ }
ReturnedValue result;
if (values[0]) {
result = engine->metaTypeToJS(types[0], values[0]);
- types[0].destruct(values[0]);
+ if (types[0].flags() & QMetaType::NeedsDestruction)
+ types[0].destruct(values[0]);
} else {
result = Encode::undefined();
}
- for (qsizetype i = 1, end = numFunctionArguments + 1; i < end; ++i)
- types[i].destruct(values[i]);
+ for (qsizetype i = 1, end = numFunctionArguments + 1; i < end; ++i) {
+ if (types[i].flags() & QMetaType::NeedsDestruction)
+ types[i].destruct(values[i]);
+ }
return result;
}
@@ -202,57 +204,380 @@ bool convertAndCall(ExecutionEngine *engine, QObject *thisObject,
const QMetaType resultType = types[0];
if (scope.hasException()) {
// Clear the return value
+ resultType.destruct(result);
resultType.construct(result);
- } else {
+ } else if (resultType == QMetaType::fromType<QVariant>()) {
// When the return type is QVariant, JS objects are to be returned as
// QJSValue wrapped in QVariant. metaTypeFromJS unwraps them, unfortunately.
- if (resultType == QMetaType::fromType<QVariant>()) {
- new (result) QVariant(ExecutionEngine::toVariant(jsResult, QMetaType {}));
- } else {
- resultType.construct(result);
- ExecutionEngine::metaTypeFromJS(jsResult, resultType, result);
- }
+ *static_cast<QVariant *>(result) = ExecutionEngine::toVariant(jsResult, QMetaType {});
+ } else if (!ExecutionEngine::metaTypeFromJS(jsResult, resultType, result)) {
+ // If we cannot convert, also clear the return value.
+ // The caller may have given us an uninitialized QObject*, expecting it to be overwritten.
+ resultType.destruct(result);
+ resultType.construct(result);
}
return !jsResult->isUndefined();
}
+inline ReturnedValue coerce(
+ ExecutionEngine *engine, const Value &value, const QQmlType &qmlType, bool isList);
+
+inline QObject *coerceQObject(const Value &value, const QQmlType &qmlType)
+{
+ QObject *o;
+ if (const QV4::QObjectWrapper *wrapper = value.as<QV4::QObjectWrapper>())
+ o = wrapper->object();
+ else if (const QV4::QQmlTypeWrapper *wrapper = value.as<QQmlTypeWrapper>())
+ o = wrapper->object();
+ else
+ return nullptr;
+
+ return (o && qmlobject_can_qml_cast(o, qmlType)) ? o : nullptr;
+}
+
+enum CoercionProblem
+{
+ InsufficientAnnotation,
+ InvalidListType
+};
+
+Q_QML_EXPORT void warnAboutCoercionToVoid(
+ ExecutionEngine *engine, const Value &value, CoercionProblem problem);
+
+inline ReturnedValue coerceListType(
+ ExecutionEngine *engine, const Value &value, const QQmlType &qmlType)
+{
+ QMetaType type = qmlType.qListTypeId();
+ const auto metaSequence = [&]() {
+ // TODO: We should really add the metasequence to the same QQmlType that holds
+ // all the other type information. Then we can get rid of the extra
+ // QQmlMetaType::qmlListType() here.
+ return qmlType.isSequentialContainer()
+ ? qmlType.listMetaSequence()
+ : QQmlMetaType::qmlListType(type).listMetaSequence();
+ };
+
+ if (const QV4::Sequence *sequence = value.as<QV4::Sequence>()) {
+ if (sequence->d()->listType() == type)
+ return value.asReturnedValue();
+ }
+
+ if (const QmlListWrapper *list = value.as<QmlListWrapper>()) {
+ if (list->d()->propertyType() == type)
+ return value.asReturnedValue();
+ }
+
+ QMetaType listValueType = qmlType.typeId();
+ if (!listValueType.isValid()) {
+ warnAboutCoercionToVoid(engine, value, InvalidListType);
+ return value.asReturnedValue();
+ }
+
+ QV4::Scope scope(engine);
+
+ const ArrayObject *array = value.as<ArrayObject>();
+ if (!array) {
+ return (listValueType.flags() & QMetaType::PointerToQObject)
+ ? QmlListWrapper::create(engine, listValueType)
+ : SequencePrototype::fromData(engine, type, metaSequence(), nullptr);
+ }
+
+ if (listValueType.flags() & QMetaType::PointerToQObject) {
+ QV4::Scoped<QmlListWrapper> newList(scope, QmlListWrapper::create(engine, type));
+ QQmlListProperty<QObject> *listProperty = newList->d()->property();
+
+ const qsizetype length = array->getLength();
+ qsizetype i = 0;
+ for (; i < length; ++i) {
+ ScopedValue v(scope, array->get(i));
+ listProperty->append(listProperty, coerceQObject(v, qmlType));
+ }
+
+ return newList->asReturnedValue();
+ }
+
+ QV4::Scoped<Sequence> sequence(
+ scope, SequencePrototype::fromData(engine, type, metaSequence(), nullptr));
+ const qsizetype length = array->getLength();
+ for (qsizetype i = 0; i < length; ++i)
+ sequence->containerPutIndexed(i, array->get(i));
+ return sequence->asReturnedValue();
+}
+
+inline ReturnedValue coerce(
+ ExecutionEngine *engine, const Value &value, const QQmlType &qmlType, bool isList)
+{
+ // These are all the named non-list, non-QObject builtins. Only those need special handling.
+ // Some of them may be wrapped in VariantObject because that is how they are stored in VME
+ // properties.
+ if (isList)
+ return coerceListType(engine, value, qmlType);
+
+ const QMetaType metaType = qmlType.typeId();
+ if (!metaType.isValid()) {
+ if (!value.isUndefined())
+ warnAboutCoercionToVoid(engine, value, InsufficientAnnotation);
+ return value.asReturnedValue();
+ }
+
+ switch (metaType.id()) {
+ case QMetaType::Void:
+ return Encode::undefined();
+ case QMetaType::QVariant:
+ return value.asReturnedValue();
+ case QMetaType::Int:
+ return Encode(value.toInt32());
+ case QMetaType::Double:
+ return value.convertedToNumber();
+ case QMetaType::QString:
+ return value.toString(engine)->asReturnedValue();
+ case QMetaType::Bool:
+ return Encode(value.toBoolean());
+ case QMetaType::QDateTime:
+ if (value.as<DateObject>())
+ return value.asReturnedValue();
+ if (const VariantObject *varObject = value.as<VariantObject>()) {
+ const QVariant &var = varObject->d()->data();
+ switch (var.metaType().id()) {
+ case QMetaType::QDateTime:
+ return engine->newDateObject(var.value<QDateTime>())->asReturnedValue();
+ case QMetaType::QTime:
+ return engine->newDateObject(var.value<QTime>(), nullptr, -1, 0)->asReturnedValue();
+ case QMetaType::QDate:
+ return engine->newDateObject(var.value<QDate>(), nullptr, -1, 0)->asReturnedValue();
+ default:
+ break;
+ }
+ }
+ return engine->newDateObject(QDateTime())->asReturnedValue();
+ case QMetaType::QUrl:
+ if (value.as<UrlObject>())
+ return value.asReturnedValue();
+ if (const VariantObject *varObject = value.as<VariantObject>()) {
+ const QVariant &var = varObject->d()->data();
+ return var.metaType() == QMetaType::fromType<QUrl>()
+ ? engine->newUrlObject(var.value<QUrl>())->asReturnedValue()
+ : engine->newUrlObject()->asReturnedValue();
+ }
+ // Since URL properties are stored as string, we need to support the string conversion here.
+ if (const String *string = value.stringValue())
+ return engine->newUrlObject(QUrl(string->toQString()))->asReturnedValue();
+ return engine->newUrlObject()->asReturnedValue();
+#if QT_CONFIG(regularexpression)
+ case QMetaType::QRegularExpression:
+ if (value.as<RegExpObject>())
+ return value.asReturnedValue();
+ if (const VariantObject *varObject = value.as<VariantObject>()) {
+ const QVariant &var = varObject->d()->data();
+ if (var.metaType() == QMetaType::fromType<QRegularExpression>())
+ return engine->newRegExpObject(var.value<QRegularExpression>())->asReturnedValue();
+ }
+ return engine->newRegExpObject(QString(), 0)->asReturnedValue();
+#endif
+ default:
+ break;
+ }
+
+ if (metaType.flags() & QMetaType::PointerToQObject) {
+ return coerceQObject(value, qmlType)
+ ? value.asReturnedValue()
+ : Encode::null();
+ }
+
+ if (const QQmlValueTypeWrapper *wrapper = value.as<QQmlValueTypeWrapper>()) {
+ if (wrapper->type() == metaType)
+ return value.asReturnedValue();
+ }
+
+ if (void *target = QQmlValueTypeProvider::heapCreateValueType(qmlType, value)) {
+ Heap::QQmlValueTypeWrapper *wrapper = engine->memoryManager->allocate<QQmlValueTypeWrapper>(
+ nullptr, metaType, qmlType.metaObjectForValueType(),
+ nullptr, -1, Heap::ReferenceObject::NoFlag);
+ Q_ASSERT(!wrapper->gadgetPtr());
+ wrapper->setGadgetPtr(target);
+ return wrapper->asReturnedValue();
+ }
+
+ return Encode::undefined();
+}
+
template<typename Callable>
ReturnedValue coerceAndCall(
- ExecutionEngine *engine, const QQmlPrivate::TypedFunction *typedFunction,
- const Value *thisObject, const Value *argv, int argc, Callable call)
+ ExecutionEngine *engine,
+ const Function::JSTypedFunction *typedFunction, const CompiledData::Function *compiledFunction,
+ const Value *argv, int argc, Callable call)
{
Scope scope(engine);
- QV4::JSCallArguments jsCallData(scope, argc);
- const qsizetype numFunctionArguments = typedFunction->argumentTypes.size();
- for (qsizetype i = 0; i < numFunctionArguments; ++i) {
- const QMetaType argumentType = typedFunction->argumentTypes[i];
- if (const qsizetype argumentSize = argumentType.sizeOf()) {
- Q_ALLOCA_VAR(void, argument, argumentSize);
- argumentType.construct(argument);
- if (i < argc)
- ExecutionEngine::metaTypeFromJS(argv[i], argumentType, argument);
- jsCallData.args[i] = engine->metaTypeToJS(argumentType, argument);
+ QV4::JSCallArguments jsCallData(scope, typedFunction->types.size() - 1);
+ const CompiledData::Parameter *formals = compiledFunction->formalsTable();
+ for (qsizetype i = 0; i < jsCallData.argc; ++i) {
+ jsCallData.args[i] = coerce(
+ engine, i < argc ? argv[i] : Encode::undefined(),
+ typedFunction->types[i + 1], formals[i].type.isList());
+ }
+
+ ScopedValue result(scope, call(jsCallData.args, jsCallData.argc));
+ return coerce(engine, result, typedFunction->types[0], compiledFunction->returnType.isList());
+}
+
+// Note: \a to is unininitialized here! This is in contrast to most other related functions.
+inline void coerce(
+ ExecutionEngine *engine, QMetaType fromType, const void *from, QMetaType toType, void *to)
+{
+ if ((fromType.flags() & QMetaType::PointerToQObject)
+ && (toType.flags() & QMetaType::PointerToQObject)) {
+ QObject *fromObj = *static_cast<QObject * const*>(from);
+ *static_cast<QObject **>(to)
+ = (fromObj && fromObj->metaObject()->inherits(toType.metaObject()))
+ ? fromObj
+ : nullptr;
+ return;
+ }
+
+ if (toType == QMetaType::fromType<QVariant>()) {
+ new (to) QVariant(fromType, from);
+ return;
+ }
+
+ if (toType == QMetaType::fromType<QJSPrimitiveValue>()) {
+ new (to) QJSPrimitiveValue(fromType, from);
+ return;
+ }
+
+ if (fromType == QMetaType::fromType<QVariant>()) {
+ const QVariant *fromVariant = static_cast<const QVariant *>(from);
+ if (fromVariant->metaType() == toType)
+ toType.construct(to, fromVariant->data());
+ else
+ coerce(engine, fromVariant->metaType(), fromVariant->data(), toType, to);
+ return;
+ }
+
+ if (fromType == QMetaType::fromType<QJSPrimitiveValue>()) {
+ const QJSPrimitiveValue *fromPrimitive = static_cast<const QJSPrimitiveValue *>(from);
+ if (fromPrimitive->metaType() == toType)
+ toType.construct(to, fromPrimitive->data());
+ else
+ coerce(engine, fromPrimitive->metaType(), fromPrimitive->data(), toType, to);
+ return;
+ }
+
+ // TODO: This is expensive. We might establish a direct C++-to-C++ type coercion, like we have
+ // for JS-to-JS. However, we shouldn't need this very often. Most of the time the compiler
+ // will generate code that passes the right arguments.
+ if (toType.flags() & QMetaType::NeedsConstruction)
+ toType.construct(to);
+ QV4::Scope scope(engine);
+ QV4::ScopedValue value(scope, engine->fromData(fromType, from));
+ if (!ExecutionEngine::metaTypeFromJS(value, toType, to))
+ QMetaType::convert(fromType, from, toType, to);
+}
+
+template<typename TypedFunction, typename Callable>
+void coerceAndCall(
+ ExecutionEngine *engine, const TypedFunction *typedFunction,
+ void **argv, const QMetaType *types, int argc, Callable call)
+{
+ const qsizetype numFunctionArguments = typedFunction->parameterCount();
+
+ Q_ALLOCA_DECLARE(void *, transformedArguments);
+ Q_ALLOCA_DECLARE(void, transformedResult);
+
+ const QMetaType returnType = typedFunction->returnMetaType();
+ const QMetaType frameReturn = types[0];
+ bool returnsQVariantWrapper = false;
+ if (argv[0] && returnType != frameReturn) {
+ Q_ALLOCA_ASSIGN(void *, transformedArguments, (numFunctionArguments + 1) * sizeof(void *));
+ memcpy(transformedArguments, argv, (argc + 1) * sizeof(void *));
+
+ if (frameReturn == QMetaType::fromType<QVariant>()) {
+ QVariant *returnValue = static_cast<QVariant *>(argv[0]);
+ *returnValue = QVariant(returnType);
+ transformedResult = transformedArguments[0] = returnValue->data();
+ returnsQVariantWrapper = true;
+ } else if (returnType.sizeOf() > 0) {
+ Q_ALLOCA_ASSIGN(void, transformedResult, returnType.sizeOf());
+ transformedArguments[0] = transformedResult;
+ if (returnType.flags() & QMetaType::NeedsConstruction)
+ returnType.construct(transformedResult);
} else {
- jsCallData.args[i] = argv[i];
+ transformedResult = transformedArguments[0] = &argc; // Some non-null marker value
}
}
- ScopedValue result(scope, call(thisObject, jsCallData.args, argc));
- const QMetaType returnType = typedFunction->returnType;
- if (const qsizetype returnSize = returnType.sizeOf()) {
- Q_ALLOCA_VAR(void, returnValue, returnSize);
- if (scope.hasException()) {
- returnType.construct(returnValue);
- } else if (returnType == QMetaType::fromType<QVariant>()) {
- new (returnValue) QVariant(ExecutionEngine::toVariant(result, QMetaType {}));
- } else {
- returnType.construct(returnValue);
- ExecutionEngine::metaTypeFromJS(result, returnType, returnValue);
+ for (qsizetype i = 0; i < numFunctionArguments; ++i) {
+ const bool isValid = argc > i;
+ const QMetaType frameType = isValid ? types[i + 1] : QMetaType();
+
+ const QMetaType argumentType = typedFunction->parameterMetaType(i);
+ if (isValid && argumentType == frameType)
+ continue;
+
+ if (transformedArguments == nullptr) {
+ Q_ALLOCA_ASSIGN(void *, transformedArguments, (numFunctionArguments + 1) * sizeof(void *));
+ memcpy(transformedArguments, argv, (argc + 1) * sizeof(void *));
+ }
+
+ if (argumentType.sizeOf() == 0) {
+ transformedArguments[i + 1] = nullptr;
+ continue;
+ }
+
+ void *frameVal = isValid ? argv[i + 1] : nullptr;
+ if (isValid && frameType == QMetaType::fromType<QVariant>()) {
+ QVariant *variant = static_cast<QVariant *>(frameVal);
+
+ const QMetaType variantType = variant->metaType();
+ if (variantType == argumentType) {
+ // Slightly nasty, but we're allowed to do this.
+ // We don't want to destruct() the QVariant's data() below.
+ transformedArguments[i + 1] = argv[i + 1] = variant->data();
+ } else {
+ Q_ALLOCA_VAR(void, arg, argumentType.sizeOf());
+ coerce(engine, variantType, variant->constData(), argumentType, arg);
+ transformedArguments[i + 1] = arg;
+ }
+ continue;
+ }
+
+ Q_ALLOCA_VAR(void, arg, argumentType.sizeOf());
+
+ if (isValid)
+ coerce(engine, frameType, frameVal, argumentType, arg);
+ else
+ argumentType.construct(arg);
+
+ transformedArguments[i + 1] = arg;
+ }
+
+ if (!transformedArguments) {
+ call(argv, numFunctionArguments);
+ return;
+ }
+
+ call(transformedArguments, numFunctionArguments);
+
+ if (transformedResult && !returnsQVariantWrapper) {
+ if (frameReturn.sizeOf() > 0) {
+ if (frameReturn.flags() & QMetaType::NeedsDestruction)
+ frameReturn.destruct(argv[0]);
+ coerce(engine, returnType, transformedResult, frameReturn, argv[0]);
+ }
+ if (returnType.flags() & QMetaType::NeedsDestruction)
+ returnType.destruct(transformedResult);
+ }
+
+ for (qsizetype i = 0; i < numFunctionArguments; ++i) {
+ void *arg = transformedArguments[i + 1];
+ if (arg == nullptr)
+ continue;
+ if (i >= argc || arg != argv[i + 1]) {
+ const QMetaType argumentType = typedFunction->parameterMetaType(i);
+ if (argumentType.flags() & QMetaType::NeedsDestruction)
+ argumentType.destruct(arg);
}
- return engine->metaTypeToJS(returnType, returnValue);
}
- return result->asReturnedValue();
}
} // namespace QV4
diff --git a/src/qml/jsruntime/qv4jsonobject.cpp b/src/qml/jsruntime/qv4jsonobject.cpp
index 4f46e605af..d78d09113a 100644
--- a/src/qml/jsruntime/qv4jsonobject.cpp
+++ b/src/qml/jsruntime/qv4jsonobject.cpp
@@ -114,6 +114,7 @@ QChar JsonParser::nextToken()
case EndArray:
case EndObject:
eatSpace();
+ break;
case Quote:
break;
default:
@@ -695,12 +696,18 @@ QString Stringify::Str(const QString &key, const Value &v)
}
if (replacerFunction) {
- ScopedObject holder(scope, v4->newObject());
- holder->put(scope.engine->id_empty(), value);
JSCallArguments jsCallData(scope, 2);
jsCallData.args[0] = v4->newString(key);
jsCallData.args[1] = value;
- *jsCallData.thisObject = holder;
+
+ if (stack.isEmpty()) {
+ ScopedObject holder(scope, v4->newObject());
+ holder->put(scope.engine->id_empty(), v);
+ *jsCallData.thisObject = holder;
+ } else {
+ *jsCallData.thisObject = stack.top();
+ }
+
value = replacerFunction->call(jsCallData);
if (v4->hasException)
return QString();
@@ -981,12 +988,16 @@ QJsonValue JsonObject::toJsonValue(const Value &value, V4ObjectSet &visitedObjec
Q_ASSERT(value.isObject());
Scope scope(value.as<Object>()->engine());
- ScopedArrayObject a(scope, value);
- if (a)
+ if (ScopedArrayObject a{ scope, value }) {
return toJsonArray(a, visitedObjects);
- ScopedObject o(scope, value);
- if (o)
+ } else if (Scoped<QV4::Sequence> a{ scope, value }) {
+ return toJsonArray(a, visitedObjects);
+ } else if (Scoped<QmlListWrapper> lw{ scope, value }) {
+ return toJsonArray(lw, visitedObjects);
+ } else if (ScopedObject o{ scope, value }) {
return toJsonObject(o, visitedObjects);
+ }
+
return QJsonValue(value.toQString());
}
@@ -1051,7 +1062,7 @@ QV4::ReturnedValue JsonObject::fromJsonArray(ExecutionEngine *engine, const QJso
return a.asReturnedValue();
}
-QJsonArray JsonObject::toJsonArray(const ArrayObject *a, V4ObjectSet &visitedObjects)
+QJsonArray JsonObject::toJsonArray(const Object *a, V4ObjectSet &visitedObjects)
{
QJsonArray result;
if (!a)
diff --git a/src/qml/jsruntime/qv4jsonobject_p.h b/src/qml/jsruntime/qv4jsonobject_p.h
index abdfcf5d37..f6f63d7eb3 100644
--- a/src/qml/jsruntime/qv4jsonobject_p.h
+++ b/src/qml/jsruntime/qv4jsonobject_p.h
@@ -63,14 +63,13 @@ public:
{ V4ObjectSet visitedObjects; return toJsonValue(value, visitedObjects); }
static inline QJsonObject toJsonObject(const QV4::Object *o)
{ V4ObjectSet visitedObjects; return toJsonObject(o, visitedObjects); }
- static inline QJsonArray toJsonArray(const QV4::ArrayObject *a)
- { V4ObjectSet visitedObjects; return toJsonArray(a, visitedObjects); }
+ static inline QJsonArray toJsonArray(const QV4::Object *o)
+ { V4ObjectSet visitedObjects; return toJsonArray(o, visitedObjects); }
private:
static QJsonValue toJsonValue(const QV4::Value &value, V4ObjectSet &visitedObjects);
static QJsonObject toJsonObject(const Object *o, V4ObjectSet &visitedObjects);
- static QJsonArray toJsonArray(const ArrayObject *a, V4ObjectSet &visitedObjects);
-
+ static QJsonArray toJsonArray(const Object *o, V4ObjectSet &visitedObjects);
};
class JsonParser
diff --git a/src/qml/jsruntime/qv4lookup.cpp b/src/qml/jsruntime/qv4lookup.cpp
index abf88f4283..654275a709 100644
--- a/src/qml/jsruntime/qv4lookup.cpp
+++ b/src/qml/jsruntime/qv4lookup.cpp
@@ -1,10 +1,12 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
-#include "qv4lookup_p.h"
-#include "qv4functionobject_p.h"
+
+#include <private/qv4functionobject_p.h>
#include <private/qv4identifiertable_p.h>
-#include <QtQml/private/qv4runtime_p.h>
-#include <QtQml/private/qv4qobjectwrapper_p.h>
+#include <private/qv4lookup_p.h>
+#include <private/qv4qobjectwrapper_p.h>
+#include <private/qv4runtime_p.h>
+#include <private/qv4stackframe_p.h>
QT_BEGIN_NAMESPACE
@@ -52,12 +54,12 @@ ReturnedValue Lookup::resolvePrimitiveGetter(ExecutionEngine *engine, const Valu
return engine->throwTypeError(message);
}
case Value::Boolean_Type:
- primitiveLookup.proto = engine->booleanPrototype()->d();
+ primitiveLookup.proto.set(engine, engine->booleanPrototype()->d());
break;
case Value::Managed_Type: {
// ### Should move this over to the Object path, as strings also have an internalClass
Q_ASSERT(object.isStringOrSymbol());
- primitiveLookup.proto = static_cast<const Managed &>(object).internalClass()->prototype;
+ primitiveLookup.proto.set(engine, static_cast<const Managed &>(object).internalClass()->prototype);
Q_ASSERT(primitiveLookup.proto);
Scope scope(engine);
ScopedString name(scope, engine->currentStackFrame->v4Function->compilationUnit->runtimeStrings[nameIndex]);
@@ -70,7 +72,7 @@ ReturnedValue Lookup::resolvePrimitiveGetter(ExecutionEngine *engine, const Valu
}
case Value::Integer_Type:
default: // Number
- primitiveLookup.proto = engine->numberPrototype()->d();
+ primitiveLookup.proto.set(engine, engine->numberPrototype()->d());
}
PropertyKey name = engine->identifierTable->asPropertyKey(engine->currentStackFrame->v4Function->compilationUnit->runtimeStrings[nameIndex]);
@@ -120,9 +122,10 @@ static inline void setupObjectLookupTwoClasses(Lookup *l, const Lookup &first, c
const uint offset1 = first.objectLookup.offset;
Heap::InternalClass *ic2 = second.objectLookup.ic;
const uint offset2 = second.objectLookup.offset;
+ auto engine = ic1->engine;
- l->objectLookupTwoClasses.ic = ic1;
- l->objectLookupTwoClasses.ic2 = ic2;
+ l->objectLookupTwoClasses.ic.set(engine, ic1);
+ l->objectLookupTwoClasses.ic2.set(engine, ic2);
l->objectLookupTwoClasses.offset = offset1;
l->objectLookupTwoClasses.offset2 = offset2;
}
@@ -201,6 +204,21 @@ ReturnedValue Lookup::getterFallback(Lookup *l, ExecutionEngine *engine, const V
return o->get(name);
}
+ReturnedValue Lookup::getterFallbackAsVariant(
+ Lookup *l, ExecutionEngine *engine, const Value &object)
+{
+ if (&Lookup::getterFallback == &Lookup::getterFallbackAsVariant) {
+ // Certain compilers, e.g. MSVC, will "helpfully" deduplicate methods that are completely
+ // equal. As a result, the pointers are the same, which wreaks havoc on the logic that
+ // decides how to retrieve the property.
+ qFatal("Your C++ compiler is broken.");
+ }
+
+ // This getter just marks the presence of a fallback lookup with variant conversion.
+ // It only does anything with it when running AOT-compiled code.
+ return getterFallback(l, engine, object);
+}
+
ReturnedValue Lookup::getter0MemberData(Lookup *l, ExecutionEngine *engine, const Value &object)
{
// we can safely cast to a QV4::Object here. If object is actually a string,
@@ -401,6 +419,21 @@ ReturnedValue Lookup::getterQObject(Lookup *lookup, ExecutionEngine *engine, con
return QObjectWrapper::lookupPropertyGetterImpl(lookup, engine, object, flags, revertLookup);
}
+ReturnedValue Lookup::getterQObjectAsVariant(
+ Lookup *lookup, ExecutionEngine *engine, const Value &object)
+{
+ if (&Lookup::getterQObject == &Lookup::getterQObjectAsVariant) {
+ // Certain compilers, e.g. MSVC, will "helpfully" deduplicate methods that are completely
+ // equal. As a result, the pointers are the same, which wreaks havoc on the logic that
+ // decides how to retrieve the property.
+ qFatal("Your C++ compiler is broken.");
+ }
+
+ // This getter marks the presence of a qobjectlookup with variant conversion.
+ // It only does anything with it when running AOT-compiled code.
+ return getterQObject(lookup, engine, object);
+}
+
ReturnedValue Lookup::getterQObjectMethod(Lookup *lookup, ExecutionEngine *engine, const Value &object)
{
const auto revertLookup = [lookup, engine, &object]() {
@@ -533,8 +566,9 @@ bool Lookup::setterTwoClasses(Lookup *l, ExecutionEngine *engine, Value &object,
}
if (l->setter == Lookup::setter0MemberData || l->setter == Lookup::setter0Inline) {
- l->objectLookupTwoClasses.ic = ic;
- l->objectLookupTwoClasses.ic2 = ic;
+ auto engine = ic->engine;
+ l->objectLookupTwoClasses.ic.set(engine, ic);
+ l->objectLookupTwoClasses.ic2.set(engine, ic);
l->objectLookupTwoClasses.offset = index;
l->objectLookupTwoClasses.offset2 = index;
l->setter = setter0setter0;
@@ -559,6 +593,21 @@ bool Lookup::setterFallback(Lookup *l, ExecutionEngine *engine, Value &object, c
return o->put(name, value);
}
+bool Lookup::setterFallbackAsVariant(
+ Lookup *l, ExecutionEngine *engine, Value &object, const Value &value)
+{
+ if (&Lookup::setterFallback == &Lookup::setterFallbackAsVariant) {
+ // Certain compilers, e.g. MSVC, will "helpfully" deduplicate methods that are completely
+ // equal. As a result, the pointers are the same, which wreaks havoc on the logic that
+ // decides how to retrieve the property.
+ qFatal("Your C++ compiler is broken.");
+ }
+
+ // This setter just marks the presence of a fallback lookup with QVariant conversion.
+ // It only does anything with it when running AOT-compiled code.
+ return setterFallback(l, engine, object, value);
+}
+
bool Lookup::setter0MemberData(Lookup *l, ExecutionEngine *engine, Value &object, const Value &value)
{
Heap::Object *o = static_cast<Heap::Object *>(object.heapObject());
@@ -619,9 +668,25 @@ bool Lookup::setterQObject(Lookup *l, ExecutionEngine *engine, Value &object, co
{
// This setter just marks the presence of a qobjectlookup. It only does anything with it when
// running AOT-compiled code, though.
- return QV4::Lookup::setterFallback(l, engine, object, v);
+ return setterFallback(l, engine, object, v);
}
+bool Lookup::setterQObjectAsVariant(
+ Lookup *l, ExecutionEngine *engine, Value &object, const Value &v)
+{
+ if (&Lookup::setterQObject == &Lookup::setterQObjectAsVariant) {
+ // Certain compilers, e.g. MSVC, will "helpfully" deduplicate methods that are completely
+ // equal. As a result, the pointers are the same, which wreaks havoc on the logic that
+ // decides how to retrieve the property.
+ qFatal("Your C++ compiler is broken.");
+ }
+
+ // This setter marks the presence of a qobjectlookup with QVariant conversion.
+ // It only does anything with it when running AOT-compiled code.
+ return setterQObject(l, engine, object, v);
+}
+
+
bool Lookup::arrayLengthSetter(Lookup *, ExecutionEngine *engine, Value &object, const Value &value)
{
Q_ASSERT(object.isObject() && static_cast<Object &>(object).isArrayObject());
diff --git a/src/qml/jsruntime/qv4lookup_p.h b/src/qml/jsruntime/qv4lookup_p.h
index bd481d366e..258184cd37 100644
--- a/src/qml/jsruntime/qv4lookup_p.h
+++ b/src/qml/jsruntime/qv4lookup_p.h
@@ -19,6 +19,7 @@
#include "qv4internalclass_p.h"
#include "qv4qmlcontext_p.h"
#include <private/qqmltypewrapper_p.h>
+#include <private/qv4mm_p.h>
QT_BEGIN_NAMESPACE
@@ -28,10 +29,13 @@ namespace Heap {
struct QObjectMethod;
}
+template <typename T, int PhantomTag>
+using HeapObjectWrapper = WriteBarrier::HeapObjectWrapper<T, PhantomTag>;
+
// Note: We cannot hide the copy ctor and assignment operator of this class because it needs to
// be trivially copyable. But you should never ever copy it. There are refcounted members
// in there.
-struct Q_QML_PRIVATE_EXPORT Lookup {
+struct Q_QML_EXPORT Lookup {
union {
ReturnedValue (*getter)(Lookup *l, ExecutionEngine *engine, const Value &object);
ReturnedValue (*globalGetter)(Lookup *l, ExecutionEngine *engine);
@@ -48,7 +52,7 @@ struct Q_QML_PRIVATE_EXPORT Lookup {
quintptr unused2;
} markDef;
struct {
- Heap::InternalClass *ic;
+ HeapObjectWrapper<Heap::InternalClass, 0> ic;
quintptr unused;
uint index;
uint offset;
@@ -59,8 +63,8 @@ struct Q_QML_PRIVATE_EXPORT Lookup {
const Value *data;
} protoLookup;
struct {
- Heap::InternalClass *ic;
- Heap::InternalClass *ic2;
+ HeapObjectWrapper<Heap::InternalClass, 1> ic;
+ HeapObjectWrapper<Heap::InternalClass, 2> ic2;
uint offset;
uint offset2;
} objectLookupTwoClasses;
@@ -73,12 +77,12 @@ struct Q_QML_PRIVATE_EXPORT Lookup {
struct {
// Make sure the next two values are in sync with protoLookup
quintptr protoId;
- Heap::Object *proto;
+ HeapObjectWrapper<Heap::Object, 3> proto;
const Value *data;
quintptr type;
} primitiveLookup;
struct {
- Heap::InternalClass *newClass;
+ HeapObjectWrapper<Heap::InternalClass, 4> newClass;
quintptr protoId;
uint offset;
uint unused;
@@ -90,14 +94,14 @@ struct Q_QML_PRIVATE_EXPORT Lookup {
uint unused;
} indexedLookup;
struct {
- Heap::InternalClass *ic;
- Heap::InternalClass *qmlTypeIc; // only used when lookup goes through QQmlTypeWrapper
+ HeapObjectWrapper<Heap::InternalClass, 5> ic;
+ HeapObjectWrapper<Heap::InternalClass, 6> qmlTypeIc; // only used when lookup goes through QQmlTypeWrapper
const QQmlPropertyCache *propertyCache;
const QQmlPropertyData *propertyData;
} qobjectLookup;
struct {
- Heap::InternalClass *ic;
- Heap::QObjectMethod *method;
+ HeapObjectWrapper<Heap::InternalClass, 7> ic;
+ HeapObjectWrapper<Heap::QObjectMethod, 8> method;
const QQmlPropertyCache *propertyCache;
const QQmlPropertyData *propertyData;
} qobjectMethodLookup;
@@ -108,7 +112,7 @@ struct Q_QML_PRIVATE_EXPORT Lookup {
int notifyIndex;
} qobjectFallbackLookup;
struct {
- Heap::InternalClass *ic;
+ HeapObjectWrapper<Heap::InternalClass, 9> ic;
quintptr metaObject; // a (const QMetaObject* & 1) or nullptr
const QtPrivate::QMetaTypeInterface *metaType; // cannot use QMetaType; class must be trivial
quint16 coreIndex;
@@ -121,7 +125,7 @@ struct Q_QML_PRIVATE_EXPORT Lookup {
int scriptIndex;
} qmlContextScriptLookup;
struct {
- Heap::Base *singletonObject;
+ HeapObjectWrapper<Heap::Base, 10> singletonObject;
quintptr unused2;
QV4::ReturnedValue singletonValue;
} qmlContextSingletonLookup;
@@ -138,17 +142,18 @@ struct Q_QML_PRIVATE_EXPORT Lookup {
ReturnedValue (*getterTrampoline)(Lookup *l, ExecutionEngine *engine);
} qmlContextGlobalLookup;
struct {
- Heap::Base *qmlTypeWrapper;
+ HeapObjectWrapper<Heap::Base, 11> qmlTypeWrapper;
quintptr unused2;
} qmlTypeLookup;
struct {
- Heap::InternalClass *ic;
+ HeapObjectWrapper<Heap::InternalClass, 12> ic;
quintptr unused;
ReturnedValue encodedEnumValue;
+ const QtPrivate::QMetaTypeInterface *metaType;
} qmlEnumValueLookup;
struct {
- Heap::InternalClass *ic;
- Heap::Object *qmlScopedEnumWrapper;
+ HeapObjectWrapper<Heap::InternalClass, 13> ic;
+ HeapObjectWrapper<Heap::Object, 14> qmlScopedEnumWrapper;
} qmlScopedEnumWrapperLookup;
};
@@ -164,6 +169,7 @@ struct Q_QML_PRIVATE_EXPORT Lookup {
static ReturnedValue getterGeneric(Lookup *l, ExecutionEngine *engine, const Value &object);
static ReturnedValue getterTwoClasses(Lookup *l, ExecutionEngine *engine, const Value &object);
static ReturnedValue getterFallback(Lookup *l, ExecutionEngine *engine, const Value &object);
+ static ReturnedValue getterFallbackAsVariant(Lookup *l, ExecutionEngine *engine, const Value &object);
static ReturnedValue getter0MemberData(Lookup *l, ExecutionEngine *engine, const Value &object);
static ReturnedValue getter0Inline(Lookup *l, ExecutionEngine *engine, const Value &object);
@@ -177,6 +183,7 @@ struct Q_QML_PRIVATE_EXPORT Lookup {
static ReturnedValue getterProtoAccessorTwoClasses(Lookup *l, ExecutionEngine *engine, const Value &object);
static ReturnedValue getterIndexed(Lookup *l, ExecutionEngine *engine, const Value &object);
static ReturnedValue getterQObject(Lookup *l, ExecutionEngine *engine, const Value &object);
+ static ReturnedValue getterQObjectAsVariant(Lookup *l, ExecutionEngine *engine, const Value &object);
static ReturnedValue getterQObjectMethod(Lookup *l, ExecutionEngine *engine, const Value &object);
static ReturnedValue primitiveGetterProto(Lookup *l, ExecutionEngine *engine, const Value &object);
@@ -191,11 +198,13 @@ struct Q_QML_PRIVATE_EXPORT Lookup {
static bool setterGeneric(Lookup *l, ExecutionEngine *engine, Value &object, const Value &value);
Q_NEVER_INLINE static bool setterTwoClasses(Lookup *l, ExecutionEngine *engine, Value &object, const Value &value);
static bool setterFallback(Lookup *l, ExecutionEngine *engine, Value &object, const Value &value);
+ static bool setterFallbackAsVariant(Lookup *l, ExecutionEngine *engine, Value &object, const Value &value);
static bool setter0MemberData(Lookup *l, ExecutionEngine *engine, Value &object, const Value &value);
static bool setter0Inline(Lookup *l, ExecutionEngine *engine, Value &object, const Value &value);
static bool setter0setter0(Lookup *l, ExecutionEngine *engine, Value &object, const Value &value);
static bool setterInsert(Lookup *l, ExecutionEngine *engine, Value &object, const Value &value);
static bool setterQObject(Lookup *l, ExecutionEngine *engine, Value &object, const Value &value);
+ static bool setterQObjectAsVariant(Lookup *l, ExecutionEngine *engine, Value &object, const Value &value);
static bool arrayLengthSetter(Lookup *l, ExecutionEngine *engine, Value &object, const Value &value);
void markObjects(MarkStack *stack) {
@@ -205,17 +214,15 @@ struct Q_QML_PRIVATE_EXPORT Lookup {
markDef.h2->mark(stack);
}
- void clear() {
- memset(&markDef, 0, sizeof(markDef));
- }
-
void releasePropertyCache()
{
if (getter == getterQObject
|| getter == QQmlTypeWrapper::lookupSingletonProperty
|| setter == setterQObject
|| qmlContextPropertyGetter == QQmlContextWrapper::lookupScopeObjectProperty
- || qmlContextPropertyGetter == QQmlContextWrapper::lookupContextObjectProperty) {
+ || qmlContextPropertyGetter == QQmlContextWrapper::lookupContextObjectProperty
+ || getter == getterQObjectAsVariant
+ || setter == setterQObjectAsVariant) {
if (const QQmlPropertyCache *pc = qobjectLookup.propertyCache)
pc->release();
} else if (getter == getterQObjectMethod
@@ -248,7 +255,7 @@ inline void setupQObjectLookup(
const Object *self)
{
setupQObjectLookup(lookup, ddata, propertyData);
- lookup->qobjectLookup.ic = self->internalClass();
+ lookup->qobjectLookup.ic.set(self->engine(), self->internalClass());
}
@@ -257,17 +264,20 @@ inline void setupQObjectLookup(
const Object *self, const Object *qmlType)
{
setupQObjectLookup(lookup, ddata, propertyData, self);
- lookup->qobjectLookup.qmlTypeIc = qmlType->internalClass();
+ lookup->qobjectLookup.qmlTypeIc.set(self->engine(), qmlType->internalClass());
}
+// template parameter is an ugly trick to avoid pulling in the QObjectMethod header here
+template<typename QObjectMethod = Heap::QObjectMethod>
inline void setupQObjectMethodLookup(
Lookup *lookup, const QQmlData *ddata, const QQmlPropertyData *propertyData,
- const Object *self, Heap::QObjectMethod *method)
+ const Object *self, QObjectMethod *method)
{
lookup->releasePropertyCache();
Q_ASSERT(!ddata->propertyCache.isNull());
- lookup->qobjectMethodLookup.method = method;
- lookup->qobjectMethodLookup.ic = self->internalClass();
+ auto engine = self->engine();
+ lookup->qobjectMethodLookup.method.set(engine, method);
+ lookup->qobjectMethodLookup.ic.set(engine, self->internalClass());
lookup->qobjectMethodLookup.propertyCache = ddata->propertyCache.data();
lookup->qobjectMethodLookup.propertyCache->addref();
lookup->qobjectMethodLookup.propertyData = propertyData;
diff --git a/src/qml/jsruntime/qv4managed_p.h b/src/qml/jsruntime/qv4managed_p.h
index 299d4b4196..aeac8c4914 100644
--- a/src/qml/jsruntime/qv4managed_p.h
+++ b/src/qml/jsruntime/qv4managed_p.h
@@ -69,7 +69,7 @@ inline void qYouForgotTheQ_MANAGED_Macro(T1, T2) {}
static Heap::InternalClass *defaultInternalClass(QV4::EngineBase *e) \
{ return e->internalClasses(QV4::EngineBase::Class_##c); }
-struct Q_QML_PRIVATE_EXPORT Managed : Value, VTableBase
+struct Q_QML_EXPORT Managed : Value, VTableBase
{
V4_MANAGED_ITSELF(Base, Managed)
enum {
@@ -77,7 +77,7 @@ struct Q_QML_PRIVATE_EXPORT Managed : Value, VTableBase
IsString = false,
IsStringOrSymbol = false,
IsObject = false,
- IsFunctionObject = false,
+ IsTailCallable = false,
IsErrorObject = false,
IsArrayData = false
};
diff --git a/src/qml/jsruntime/qv4mapobject.cpp b/src/qml/jsruntime/qv4mapobject.cpp
index 72e66e0e76..94cac02556 100644
--- a/src/qml/jsruntime/qv4mapobject.cpp
+++ b/src/qml/jsruntime/qv4mapobject.cpp
@@ -12,14 +12,14 @@ DEFINE_OBJECT_VTABLE(WeakMapCtor);
DEFINE_OBJECT_VTABLE(MapCtor);
DEFINE_OBJECT_VTABLE(MapObject);
-void Heap::WeakMapCtor::init(QV4::ExecutionContext *scope)
+void Heap::WeakMapCtor::init(QV4::ExecutionEngine *engine)
{
- Heap::FunctionObject::init(scope, QStringLiteral("WeakMap"));
+ Heap::FunctionObject::init(engine, QStringLiteral("WeakMap"));
}
-void Heap::MapCtor::init(QV4::ExecutionContext *scope)
+void Heap::MapCtor::init(QV4::ExecutionEngine *engine)
{
- Heap::FunctionObject::init(scope, QStringLiteral("Map"));
+ Heap::FunctionObject::init(engine, QStringLiteral("Map"));
}
ReturnedValue WeakMapCtor::construct(const FunctionObject *f, const Value *argv, int argc, const Value *newTarget, bool weakMap)
@@ -74,8 +74,7 @@ ReturnedValue WeakMapCtor::construct(const FunctionObject *f, const Value *argv,
if (scope.hasException())
break;
}
- ScopedValue falsey(scope, Encode(false));
- return Runtime::IteratorClose::call(scope.engine, iter, falsey);
+ return Runtime::IteratorClose::call(scope.engine, iter);
}
}
return a->asReturnedValue();
@@ -215,6 +214,16 @@ ReturnedValue WeakMapPrototype::method_set(const FunctionObject *b, const Value
(!argc || !argv[0].isObject()))
return scope.engine->throwTypeError();
+ QV4::WriteBarrier::markCustom(scope.engine, [&](QV4::MarkStack *ms) {
+ if (scope.engine->memoryManager->gcStateMachine->state <= GCState::FreeWeakMaps)
+ return;
+ argv[0].heapObject()->mark(ms);
+ if (argc > 1) {
+ if (auto *h = argv[1].heapObject())
+ h->mark(ms);
+ }
+ });
+
that->d()->esTable->set(argv[0], argc > 1 ? argv[1] : Value::undefinedValue());
return that.asReturnedValue();
}
@@ -318,6 +327,14 @@ ReturnedValue MapPrototype::method_set(const FunctionObject *b, const Value *thi
if (!that || that->d()->isWeakMap)
return scope.engine->throwTypeError();
+ QV4::WriteBarrier::markCustom(scope.engine, [&](QV4::MarkStack *ms) {
+ argv[0].heapObject()->mark(ms);
+ if (argc > 1) {
+ if (auto *h = argv[1].heapObject())
+ h->mark(ms);
+ }
+ });
+
that->d()->esTable->set(argc ? argv[0] : Value::undefinedValue(), argc > 1 ? argv[1] : Value::undefinedValue());
return that.asReturnedValue();
}
diff --git a/src/qml/jsruntime/qv4mapobject_p.h b/src/qml/jsruntime/qv4mapobject_p.h
index 68bc7ceed8..e7ff02c13a 100644
--- a/src/qml/jsruntime/qv4mapobject_p.h
+++ b/src/qml/jsruntime/qv4mapobject_p.h
@@ -27,11 +27,11 @@ class ESTable;
namespace Heap {
struct WeakMapCtor : FunctionObject {
- void init(QV4::ExecutionContext *scope);
+ void init(ExecutionEngine *engine);
};
struct MapCtor : WeakMapCtor {
- void init(QV4::ExecutionContext *scope);
+ void init(ExecutionEngine *engine);
};
struct MapObject : Object {
@@ -78,7 +78,7 @@ struct WeakMapPrototype : Object
static ReturnedValue method_delete(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
static ReturnedValue method_get(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
static ReturnedValue method_has(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
- static ReturnedValue method_set(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
+ Q_AUTOTEST_EXPORT static ReturnedValue method_set(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
};
struct MapPrototype : WeakMapPrototype
@@ -92,7 +92,7 @@ struct MapPrototype : WeakMapPrototype
static ReturnedValue method_get(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
static ReturnedValue method_has(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
static ReturnedValue method_keys(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
- static ReturnedValue method_set(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
+ Q_AUTOTEST_EXPORT static ReturnedValue method_set(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
static ReturnedValue method_get_size(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
static ReturnedValue method_values(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
};
diff --git a/src/qml/jsruntime/qv4math_p.h b/src/qml/jsruntime/qv4math_p.h
index 2692c2617b..b12990700d 100644
--- a/src/qml/jsruntime/qv4math_p.h
+++ b/src/qml/jsruntime/qv4math_p.h
@@ -34,7 +34,7 @@ namespace QV4 {
static inline QMLJS_READONLY ReturnedValue add_int32(int a, int b)
{
int result;
- if (Q_UNLIKELY(add_overflow(a, b, &result)))
+ if (Q_UNLIKELY(qAddOverflow(a, b, &result)))
return StaticValue::fromDouble(static_cast<double>(a) + b).asReturnedValue();
return StaticValue::fromInt32(result).asReturnedValue();
}
@@ -42,7 +42,7 @@ static inline QMLJS_READONLY ReturnedValue add_int32(int a, int b)
static inline QMLJS_READONLY ReturnedValue sub_int32(int a, int b)
{
int result;
- if (Q_UNLIKELY(sub_overflow(a, b, &result)))
+ if (Q_UNLIKELY(qSubOverflow(a, b, &result)))
return StaticValue::fromDouble(static_cast<double>(a) - b).asReturnedValue();
return StaticValue::fromInt32(result).asReturnedValue();
}
@@ -50,7 +50,7 @@ static inline QMLJS_READONLY ReturnedValue sub_int32(int a, int b)
static inline QMLJS_READONLY ReturnedValue mul_int32(int a, int b)
{
int result;
- if (Q_UNLIKELY(mul_overflow(a, b, &result)))
+ if (Q_UNLIKELY(qMulOverflow(a, b, &result)))
return StaticValue::fromDouble(static_cast<double>(a) * b).asReturnedValue();
// need to handle the case where one number is negative and the other 0 ==> -0
if (((a < 0) xor (b < 0)) && (result == 0))
diff --git a/src/qml/jsruntime/qv4memberdata.cpp b/src/qml/jsruntime/qv4memberdata.cpp
index 6c0fc3a183..0231641609 100644
--- a/src/qml/jsruntime/qv4memberdata.cpp
+++ b/src/qml/jsruntime/qv4memberdata.cpp
@@ -46,7 +46,7 @@ Heap::MemberData *MemberData::allocate(ExecutionEngine *e, uint n, Heap::MemberD
if (oldSize > alloc)
alloc = oldSize;
m = e->memoryManager->allocManaged<MemberData>(alloc);
- // no write barrier required here
+ // no write barrier required here, as m gets marked later when member data is set
memcpy(m, old, oldSize);
} else {
m = e->memoryManager->allocManaged<MemberData>(alloc);
diff --git a/src/qml/jsruntime/qv4module.cpp b/src/qml/jsruntime/qv4module.cpp
index 1e1a059dfa..f701529096 100644
--- a/src/qml/jsruntime/qv4module.cpp
+++ b/src/qml/jsruntime/qv4module.cpp
@@ -4,13 +4,14 @@
#include "qv4module_p.h"
-#include <private/qv4mm_p.h>
-#include <private/qv4vme_moth_p.h>
#include <private/qv4context_p.h>
-#include <private/qv4symbol_p.h>
#include <private/qv4identifiertable_p.h>
+#include <private/qv4mm_p.h>
+#include <private/qv4stackframe_p.h>
+#include <private/qv4symbol_p.h>
+#include <private/qv4vme_moth_p.h>
-#include <QScopeGuard>
+#include <QtCore/qscopeguard.h>
using namespace QV4;
@@ -50,8 +51,8 @@ void Heap::Module::init(ExecutionEngine *engine, ExecutableCompilationUnit *modu
{
Scoped<QV4::InternalClass> ic(valueScope, scope->internalClass);
- for (uint i = 0; i < unit->data->importEntryTableSize; ++i) {
- const CompiledData::ImportEntry &import = unit->data->importEntryTable()[i];
+ for (uint i = 0; i < unit->unitData()->importEntryTableSize; ++i) {
+ const CompiledData::ImportEntry &import = unit->unitData()->importEntryTable()[i];
ic = ic->addMember(engine->identifierTable->asPropertyKey(unit->runtimeStrings[import.localName]), Attr_NotConfigurable);
}
scope->internalClass.set(engine, ic->d());
@@ -75,7 +76,7 @@ void Module::evaluate()
unit->evaluateModuleRequests();
ExecutionEngine *v4 = engine();
- Function *moduleFunction = unit->runtimeFunctions[unit->data->indexOfRootFunction];
+ Function *moduleFunction = unit->runtimeFunctions[unit->unitData()->indexOfRootFunction];
JSTypesStackFrame frame;
frame.init(moduleFunction, nullptr, 0);
frame.setupJSFrame(v4->jsStackTop, Value::undefinedValue(), d()->scope,
@@ -222,9 +223,12 @@ OwnPropertyKeyIterator *Module::virtualOwnPropertyKeys(const Object *o, Value *t
if (module->d()->unit->isESModule()) {
names = module->d()->unit->exportedNames();
} else {
- Heap::InternalClass *scopeClass = module->d()->scope->internalClass;
- for (uint i = 0; i < scopeClass->size; ++i)
- names << scopeClass->keyAt(i);
+ QV4::Scope scope(module->engine());
+ QV4::Scoped<InternalClass> scopeClass(scope, module->d()->scope->internalClass);
+ for (uint i = 0, end = scopeClass->d()->size; i < end; ++i) {
+ QV4::ScopedValue key(scope, scopeClass->d()->keyAt(i));
+ names << key->toQString();
+ }
}
return new ModuleNamespaceIterator(names);
diff --git a/src/qml/jsruntime/qv4numberobject.cpp b/src/qml/jsruntime/qv4numberobject.cpp
index d744e569f5..5299a2568a 100644
--- a/src/qml/jsruntime/qv4numberobject.cpp
+++ b/src/qml/jsruntime/qv4numberobject.cpp
@@ -36,9 +36,9 @@ const NumberLocale *NumberLocale::instance()
return numberLocaleHolder();
}
-void Heap::NumberCtor::init(QV4::ExecutionContext *scope)
+void Heap::NumberCtor::init(QV4::ExecutionEngine *engine)
{
- Heap::FunctionObject::init(scope, QStringLiteral("Number"));
+ Heap::FunctionObject::init(engine, QStringLiteral("Number"));
}
ReturnedValue NumberCtor::virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *newTarget)
diff --git a/src/qml/jsruntime/qv4numberobject_p.h b/src/qml/jsruntime/qv4numberobject_p.h
index 8ac2bd1f79..3f0d6d9425 100644
--- a/src/qml/jsruntime/qv4numberobject_p.h
+++ b/src/qml/jsruntime/qv4numberobject_p.h
@@ -25,7 +25,7 @@ namespace QV4 {
namespace Heap {
struct NumberCtor : FunctionObject {
- void init(QV4::ExecutionContext *scope);
+ void init(ExecutionEngine *engine);
};
}
diff --git a/src/qml/jsruntime/qv4object.cpp b/src/qml/jsruntime/qv4object.cpp
index d58a712147..1ad5e063e8 100644
--- a/src/qml/jsruntime/qv4object.cpp
+++ b/src/qml/jsruntime/qv4object.cpp
@@ -2,22 +2,25 @@
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qv4object_p.h"
-#include "qv4stringobject_p.h"
-#include "qv4argumentsobject_p.h"
+
+#include <private/qv4argumentsobject_p.h>
+#include <private/qv4identifiertable_p.h>
+#include <private/qv4jscall_p.h>
+#include <private/qv4lookup_p.h>
+#include <private/qv4memberdata_p.h>
#include <private/qv4mm_p.h>
-#include "qv4lookup_p.h"
-#include "qv4scopedvalue_p.h"
-#include "qv4memberdata_p.h"
-#include "qv4identifiertable_p.h"
-#include "qv4jscall_p.h"
-#include "qv4symbol_p.h"
-#include "qv4proxy_p.h"
+#include <private/qv4proxy_p.h>
+#include <private/qv4scopedvalue_p.h>
+#include <private/qv4stackframe_p.h>
+#include <private/qv4stringobject_p.h>
+#include <private/qv4symbol_p.h>
#include <QtCore/qloggingcategory.h>
#include <stdint.h>
using namespace QV4;
+using namespace Qt::Literals::StringLiterals;
Q_LOGGING_CATEGORY(lcJavaScriptGlobals, "qt.qml.js.globals")
@@ -39,8 +42,11 @@ void Object::setInternalClass(Heap::InternalClass *ic)
// Pick the members of the old IC that are still valid in the new IC.
// Order them by index in memberData (or inline data).
Scoped<MemberData> newMembers(scope, MemberData::allocate(scope.engine, ic->size));
- for (uint i = 0; i < ic->size; ++i)
- newMembers->set(scope.engine, i, get(ic->nameMap.at(i)));
+ for (uint i = 0; i < ic->size; ++i) {
+ // Note that some members might have been deleted. The key may be invalid.
+ const PropertyKey key = ic->nameMap.at(i);
+ newMembers->set(scope.engine, i, key.isValid() ? get(key) : Encode::undefined());
+ }
p->internalClass.set(scope.engine, ic);
const uint nInline = p->vtable()->nInlineProperties;
@@ -177,16 +183,16 @@ void Object::defineAccessorProperty(StringOrSymbol *name, VTable::Call getter, V
QV4::Scope scope(v4);
ScopedProperty p(scope);
QString n = name->toQString();
- if (n.at(0) == QLatin1Char('@'))
- n = QChar::fromLatin1('[') + QStringView{n}.mid(1) + QChar::fromLatin1(']');
+ if (!n.isEmpty() && n.at(0) == '@'_L1)
+ n = '['_L1 + QStringView{n}.mid(1) + ']'_L1;
if (getter) {
- ScopedString getName(scope, v4->newString(QString::fromLatin1("get ") + n));
+ ScopedString getName(scope, v4->newString("get "_L1 + n));
p->setGetter(ScopedFunctionObject(scope, FunctionObject::createBuiltinFunction(v4, getName, getter, 0)));
} else {
p->setGetter(nullptr);
}
if (setter) {
- ScopedString setName(scope, v4->newString(QString::fromLatin1("set ") + n));
+ ScopedString setName(scope, v4->newString("set "_L1 + n));
p->setSetter(ScopedFunctionObject(scope, FunctionObject::createBuiltinFunction(v4, setName, setter, 0)));
} else {
p->setSetter(nullptr);
@@ -762,7 +768,7 @@ ReturnedValue Object::virtualResolveLookupGetter(const Object *object, Execution
} else {
lookup->getter = Lookup::getterAccessor;
}
- lookup->objectLookup.ic = obj->internalClass;
+ lookup->objectLookup.ic.set(engine, obj->internalClass.get());
lookup->objectLookup.offset = index.index;
return lookup->getter(lookup, engine, *object);
}
@@ -789,7 +795,7 @@ bool Object::virtualResolveLookupSetter(Object *object, ExecutionEngine *engine,
lookup->setter = Lookup::arrayLengthSetter;
return lookup->setter(lookup, engine, *object, value);
} else if (idx.attrs.isData() && idx.attrs.isWritable()) {
- lookup->objectLookup.ic = object->internalClass();
+ lookup->objectLookup.ic.set(engine, object->internalClass());
lookup->objectLookup.index = idx.index;
const auto nInline = object->d()->vtable()->nInlineProperties;
if (idx.index < nInline) {
@@ -823,7 +829,7 @@ bool Object::virtualResolveLookupSetter(Object *object, ExecutionEngine *engine,
lookup->setter = Lookup::setterFallback;
return false;
}
- lookup->insertionLookup.newClass = object->internalClass();
+ lookup->insertionLookup.newClass.set(engine, object->internalClass());
lookup->insertionLookup.offset = idx.index;
lookup->setter = Lookup::setterInsert;
return true;
@@ -1178,6 +1184,7 @@ QStringList ArrayObject::toQStringList() const
ScopedValue v(scope);
uint length = getLength();
+ result.reserve(length);
for (uint i = 0; i < length; ++i) {
v = const_cast<ArrayObject *>(this)->get(i);
result.append(v->toQStringNoThrow());
diff --git a/src/qml/jsruntime/qv4object_p.h b/src/qml/jsruntime/qv4object_p.h
index acfc7d3a47..55d18fad52 100644
--- a/src/qml/jsruntime/qv4object_p.h
+++ b/src/qml/jsruntime/qv4object_p.h
@@ -377,7 +377,7 @@ private:
friend struct ObjectPrototype;
};
-struct Q_QML_PRIVATE_EXPORT ObjectOwnPropertyKeyIterator : OwnPropertyKeyIterator
+struct Q_QML_EXPORT ObjectOwnPropertyKeyIterator : OwnPropertyKeyIterator
{
uint arrayIndex = 0;
uint memberIndex = 0;
diff --git a/src/qml/jsruntime/qv4objectproto.cpp b/src/qml/jsruntime/qv4objectproto.cpp
index e7f96a12ba..2a78bb4540 100644
--- a/src/qml/jsruntime/qv4objectproto.cpp
+++ b/src/qml/jsruntime/qv4objectproto.cpp
@@ -19,9 +19,9 @@ using namespace QV4;
DEFINE_OBJECT_VTABLE(ObjectCtor);
-void Heap::ObjectCtor::init(QV4::ExecutionContext *scope)
+void Heap::ObjectCtor::init(QV4::ExecutionEngine *engine)
{
- Heap::FunctionObject::init(scope, QStringLiteral("Object"));
+ Heap::FunctionObject::init(engine, QStringLiteral("Object"));
}
ReturnedValue ObjectCtor::virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *newTarget)
diff --git a/src/qml/jsruntime/qv4objectproto_p.h b/src/qml/jsruntime/qv4objectproto_p.h
index fa12f77a6a..d74cd64926 100644
--- a/src/qml/jsruntime/qv4objectproto_p.h
+++ b/src/qml/jsruntime/qv4objectproto_p.h
@@ -25,7 +25,7 @@ namespace QV4 {
namespace Heap {
struct ObjectCtor : FunctionObject {
- void init(QV4::ExecutionContext *scope);
+ void init(ExecutionEngine *engine);
};
}
@@ -38,7 +38,7 @@ struct ObjectCtor: FunctionObject
static ReturnedValue virtualCall(const FunctionObject *m, const Value *thisObject, const Value *argv, int argc);
};
-struct Q_QML_PRIVATE_EXPORT ObjectPrototype: Object
+struct Q_QML_EXPORT ObjectPrototype: Object
{
void init(ExecutionEngine *engine, Object *ctor);
diff --git a/src/qml/jsruntime/qv4persistent.cpp b/src/qml/jsruntime/qv4persistent.cpp
index 75353935a1..4f11d0a2ad 100644
--- a/src/qml/jsruntime/qv4persistent.cpp
+++ b/src/qml/jsruntime/qv4persistent.cpp
@@ -142,6 +142,7 @@ PersistentValueStorage::PersistentValueStorage(ExecutionEngine *engine)
PersistentValueStorage::~PersistentValueStorage()
{
+ clearFreePageHint();
Page *p = static_cast<Page *>(firstPage);
while (p) {
for (int i = 0; i < kEntriesPerPage; ++i) {
@@ -159,7 +160,9 @@ PersistentValueStorage::~PersistentValueStorage()
Value *PersistentValueStorage::allocate()
{
- Page *p = static_cast<Page *>(firstPage);
+ Page *p = static_cast<Page *>(freePageHint);
+ if (p && p->header.freeList == -1)
+ p = static_cast<Page *>(firstPage);
while (p) {
if (p->header.freeList != -1)
break;
@@ -171,9 +174,15 @@ Value *PersistentValueStorage::allocate()
Value *v = p->values + p->header.freeList;
p->header.freeList = v->int_32();
- if (p->header.freeList != -1 && p != firstPage) {
- unlink(p);
- insertInFront(this, p);
+ if (p->header.freeList != -1 && p != freePageHint) {
+ if (auto oldHint = static_cast<Page *>(freePageHint)) {
+ oldHint->header.refCount--;
+ // no need to free - if the old page were unused,
+ // we would have used it to serve the allocation
+ Q_ASSERT(oldHint->header.refCount);
+ }
+ freePageHint = p;
+ p->header.refCount++;
}
++p->header.refCount;
@@ -207,6 +216,17 @@ void PersistentValueStorage::mark(MarkStack *markStack)
}
}
+void PersistentValueStorage::clearFreePageHint()
+{
+ if (!freePageHint)
+ return;
+ auto page = static_cast<Page *>(freePageHint);
+ if (!--page->header.refCount)
+ freePage(page);
+ freePageHint = nullptr;
+
+}
+
ExecutionEngine *PersistentValueStorage::getEngine(const Value *v)
{
return getPage(v)->header.engine;
@@ -223,22 +243,18 @@ void PersistentValueStorage::freePage(void *page)
PersistentValue::PersistentValue(const PersistentValue &other)
: val(nullptr)
{
- if (other.val) {
- val = other.engine()->memoryManager->m_persistentValues->allocate();
- *val = *other.val;
- }
+ if (other.val)
+ set(other.engine(), *other.val);
}
PersistentValue::PersistentValue(ExecutionEngine *engine, const Value &value)
{
- val = engine->memoryManager->m_persistentValues->allocate();
- *val = value;
+ set(engine, value);
}
PersistentValue::PersistentValue(ExecutionEngine *engine, ReturnedValue value)
{
- val = engine->memoryManager->m_persistentValues->allocate();
- *val = value;
+ set(engine, value);
}
PersistentValue::PersistentValue(ExecutionEngine *engine, Object *object)
@@ -246,9 +262,7 @@ PersistentValue::PersistentValue(ExecutionEngine *engine, Object *object)
{
if (!object)
return;
-
- val = engine->memoryManager->m_persistentValues->allocate();
- *val = object;
+ set(engine, *object);
}
PersistentValue &PersistentValue::operator=(const PersistentValue &other)
@@ -271,19 +285,16 @@ PersistentValue &PersistentValue::operator=(const PersistentValue &other)
PersistentValue &PersistentValue::operator=(const WeakValue &other)
{
- if (!val) {
- if (!other.valueRef())
- return *this;
- val = other.engine()->memoryManager->m_persistentValues->allocate();
- }
+ if (!val && !other.valueRef())
+ return *this;
if (!other.valueRef()) {
*val = Encode::undefined();
return *this;
}
- Q_ASSERT(engine() == other.engine());
+ Q_ASSERT(!engine() || engine() == other.engine());
- *val = *other.valueRef();
+ set(other.engine(), *other.valueRef());
return *this;
}
@@ -293,10 +304,7 @@ PersistentValue &PersistentValue::operator=(Object *object)
PersistentValueStorage::free(val);
return *this;
}
- if (!val)
- val = object->engine()->memoryManager->m_persistentValues->allocate();
-
- *val = object;
+ set(object->engine(), *object);
return *this;
}
@@ -304,6 +312,10 @@ void PersistentValue::set(ExecutionEngine *engine, const Value &value)
{
if (!val)
val = engine->memoryManager->m_persistentValues->allocate();
+ QV4::WriteBarrier::markCustom(engine, [&](QV4::MarkStack *stack){
+ if (QV4::WriteBarrier::isInsertionBarrier && value.isManaged())
+ value.heapObject()->mark(stack);
+ });
*val = value;
}
@@ -311,6 +323,13 @@ void PersistentValue::set(ExecutionEngine *engine, ReturnedValue value)
{
if (!val)
val = engine->memoryManager->m_persistentValues->allocate();
+ QV4::WriteBarrier::markCustom(engine, [&](QV4::MarkStack *stack){
+ if constexpr (!QV4::WriteBarrier::isInsertionBarrier)
+ return;
+ auto val = Value::fromReturnedValue(value);
+ if (val.isManaged())
+ val.heapObject()->mark(stack);
+ });
*val = value;
}
@@ -318,6 +337,11 @@ void PersistentValue::set(ExecutionEngine *engine, Heap::Base *obj)
{
if (!val)
val = engine->memoryManager->m_persistentValues->allocate();
+ QV4::WriteBarrier::markCustom(engine, [&](QV4::MarkStack *stack){
+ if constexpr (QV4::WriteBarrier::isInsertionBarrier)
+ obj->mark(stack);
+ });
+
*val = obj;
}
@@ -359,6 +383,56 @@ WeakValue::~WeakValue()
free();
}
+/*
+ WeakValue::set shold normally not mark objects, after all a weak value
+ is not supposed to keep an object alive.
+ However, if we are past GCState::HandleQObjectWrappers, nothing will
+ reset weak values referencing unmarked values, but those values will
+ still be swept.
+ That lead to stale pointers, and potentially to crashes. To avoid this,
+ we mark the objects here (they might still get collected in the next gc
+ run).
+ This is especially important due to the way we handle QObjectWrappers.
+ */
+void WeakValue::set(ExecutionEngine *engine, const Value &value)
+{
+ if (!val)
+ allocVal(engine);
+ QV4::WriteBarrier::markCustom(engine, [&](QV4::MarkStack *ms) {
+ if (engine->memoryManager->gcStateMachine->state <= GCState::HandleQObjectWrappers)
+ return;
+ if (auto *h = value.heapObject())
+ h->mark(ms);
+ });
+ *val = value;
+}
+
+void WeakValue::set(ExecutionEngine *engine, ReturnedValue value)
+{
+ if (!val)
+ allocVal(engine);
+ QV4::WriteBarrier::markCustom(engine, [&](QV4::MarkStack *ms) {
+ if (engine->memoryManager->gcStateMachine->state <= GCState::HandleQObjectWrappers)
+ return;
+ if (auto *h = QV4::Value::fromReturnedValue(value).heapObject())
+ h->mark(ms);
+ });
+
+ *val = value;
+}
+
+void WeakValue::set(ExecutionEngine *engine, Heap::Base *obj)
+{
+ if (!val)
+ allocVal(engine);
+ QV4::WriteBarrier::markCustom(engine, [&](QV4::MarkStack *ms) {
+ if (engine->memoryManager->gcStateMachine->state <= GCState::HandleQObjectWrappers)
+ return;
+ obj->mark(ms);
+ });
+ *val = obj;
+}
+
void WeakValue::allocVal(ExecutionEngine *engine)
{
val = engine->memoryManager->m_weakValues->allocate();
diff --git a/src/qml/jsruntime/qv4persistent_p.h b/src/qml/jsruntime/qv4persistent_p.h
index 7e208bd4fd..d0e29a166e 100644
--- a/src/qml/jsruntime/qv4persistent_p.h
+++ b/src/qml/jsruntime/qv4persistent_p.h
@@ -51,10 +51,13 @@ struct Q_QML_EXPORT PersistentValueStorage
Iterator begin() { return Iterator(firstPage, 0); }
Iterator end() { return Iterator(nullptr, 0); }
+ void clearFreePageHint();
+
static ExecutionEngine *getEngine(const Value *v);
ExecutionEngine *engine;
void *firstPage;
+ void *freePageHint = nullptr;
private:
static void freeUnchecked(Value *v);
static void freePage(void *page);
@@ -128,26 +131,11 @@ public:
WeakValue &operator=(const WeakValue &other);
~WeakValue();
- void set(ExecutionEngine *engine, const Value &value)
- {
- if (!val)
- allocVal(engine);
- *val = value;
- }
+ void set(ExecutionEngine *engine, const Value &value);
- void set(ExecutionEngine *engine, ReturnedValue value)
- {
- if (!val)
- allocVal(engine);
- *val = value;
- }
+ void set(ExecutionEngine *engine, ReturnedValue value);
- void set(ExecutionEngine *engine, Heap::Base *obj)
- {
- if (!val)
- allocVal(engine);
- *val = obj;
- }
+ void set(ExecutionEngine *engine, Heap::Base *obj);
ReturnedValue value() const {
return (val ? val->asReturnedValue() : Encode::undefined());
diff --git a/src/qml/jsruntime/qv4profiling.cpp b/src/qml/jsruntime/qv4profiling.cpp
index ed0dba1809..df63d4bdf7 100644
--- a/src/qml/jsruntime/qv4profiling.cpp
+++ b/src/qml/jsruntime/qv4profiling.cpp
@@ -88,10 +88,10 @@ void Profiler::startProfiling(quint64 features)
(qint64)m_engine->memoryManager->getLargeItemsMem(),
HeapPage};
m_memory_data.append(heap);
- MemoryAllocationProperties small = {timestamp,
+ MemoryAllocationProperties smallP = {timestamp,
(qint64)m_engine->memoryManager->getUsedMem(),
SmallItem};
- m_memory_data.append(small);
+ m_memory_data.append(smallP);
MemoryAllocationProperties large = {timestamp,
(qint64)m_engine->memoryManager->getLargeItemsMem(),
LargeItem};
diff --git a/src/qml/jsruntime/qv4profiling_p.h b/src/qml/jsruntime/qv4profiling_p.h
index 6fdd457f9d..d1e8e34ba3 100644
--- a/src/qml/jsruntime/qv4profiling_p.h
+++ b/src/qml/jsruntime/qv4profiling_p.h
@@ -23,8 +23,8 @@
#if !QT_CONFIG(qml_debug)
-#define Q_V4_PROFILE_ALLOC(engine, size, type) (!engine)
-#define Q_V4_PROFILE_DEALLOC(engine, size, type) (!engine)
+#define Q_V4_PROFILE_ALLOC(engine, size, type) Q_UNUSED(engine)
+#define Q_V4_PROFILE_DEALLOC(engine, size, type) Q_UNUSED(engine)
QT_BEGIN_NAMESPACE
diff --git a/src/qml/jsruntime/qv4promiseobject.cpp b/src/qml/jsruntime/qv4promiseobject.cpp
index 5424545e79..b8ede3e578 100644
--- a/src/qml/jsruntime/qv4promiseobject.cpp
+++ b/src/qml/jsruntime/qv4promiseobject.cpp
@@ -314,9 +314,9 @@ void Heap::PromiseReaction::triggerWithValue(ExecutionEngine *e, const Value *va
handler->addReaction(e, reaction, value);
}
-void Heap::PromiseCtor::init(QV4::ExecutionContext *scope)
+void Heap::PromiseCtor::init(QV4::ExecutionEngine *engine)
{
- Heap::FunctionObject::init(scope, QStringLiteral("Promise"));
+ Heap::FunctionObject::init(engine, QStringLiteral("Promise"));
}
void Heap::PromiseObject::init(ExecutionEngine *e)
@@ -575,7 +575,7 @@ ReturnedValue PromiseCtor::method_all(const FunctionObject *f, const Value *this
}
if (!doneValue->toBoolean())
- completion = Runtime::IteratorClose::call(e, iteratorObject, doneValue);
+ completion = Runtime::IteratorClose::call(e, iteratorObject);
reject->call(newPromise, completion, 1);
return newPromise.asReturnedValue();
@@ -583,7 +583,9 @@ ReturnedValue PromiseCtor::method_all(const FunctionObject *f, const Value *this
ScopedObject nextPromise(scope, Value::fromReturnedValue(resolve->call(thisObject, nextValue, 1)));
if (scope.hasException() || !nextPromise) {
- ScopedValue completion(scope, Runtime::IteratorClose::call(e, iteratorObject, doneValue));
+ ScopedValue completion(scope, doneValue->toBoolean()
+ ? Encode::undefined()
+ : Runtime::IteratorClose::call(e, iteratorObject));
if (scope.hasException()) {
completion = e->exceptionValue->asReturnedValue();
dropException(e);
@@ -605,7 +607,7 @@ ReturnedValue PromiseCtor::method_all(const FunctionObject *f, const Value *this
}
if (!doneValue->toBoolean())
- completion = Runtime::IteratorClose::call(scope.engine, iteratorObject, doneValue);
+ completion = Runtime::IteratorClose::call(scope.engine, iteratorObject);
reject->call(newPromise, completion, 1);
return newPromise.asReturnedValue();
@@ -624,7 +626,7 @@ ReturnedValue PromiseCtor::method_all(const FunctionObject *f, const Value *this
dropException(e);
if (!doneValue->toBoolean())
- completion = Runtime::IteratorClose::call(scope.engine, iteratorObject, doneValue);
+ completion = Runtime::IteratorClose::call(scope.engine, iteratorObject);
reject->call(newPromise, completion, 1);
return newPromise.asReturnedValue();
@@ -686,7 +688,9 @@ ReturnedValue PromiseCtor::method_race(const FunctionObject *f, const Value *thi
doneValue = Value::fromReturnedValue(Runtime::IteratorNext::call(e, iteratorObject, nextValue));
if (scope.hasException()) {
- ScopedValue completion(scope, Runtime::IteratorClose::call(e, iteratorObject, doneValue));
+ ScopedValue completion(scope, doneValue->toBoolean()
+ ? Encode::undefined()
+ : Runtime::IteratorClose::call(e, iteratorObject));
if (scope.hasException()) {
completion = e->exceptionValue->asReturnedValue();
dropException(e);
@@ -721,7 +725,7 @@ ReturnedValue PromiseCtor::method_race(const FunctionObject *f, const Value *thi
}
if (!doneValue->toBoolean())
- completion = Runtime::IteratorClose::call(e, iteratorObject, doneValue);
+ completion = Runtime::IteratorClose::call(e, iteratorObject);
reject->call(newPromise, completion, 1);
return newPromise.asReturnedValue();
@@ -729,7 +733,9 @@ ReturnedValue PromiseCtor::method_race(const FunctionObject *f, const Value *thi
ScopedObject nextPromise(scope, Value::fromReturnedValue(resolve->call(thisObject, nextValue, 1)));
if (scope.hasException() || !nextPromise) {
- ScopedValue completion(scope, Runtime::IteratorClose::call(e, iteratorObject, doneValue));
+ ScopedValue completion(scope, doneValue->toBoolean()
+ ? Encode::undefined()
+ : Runtime::IteratorClose::call(e, iteratorObject));
if (scope.hasException()) {
completion = e->exceptionValue->asReturnedValue();
dropException(e);
@@ -749,7 +755,7 @@ ReturnedValue PromiseCtor::method_race(const FunctionObject *f, const Value *thi
}
if (!doneValue->toBoolean())
- completion = Runtime::IteratorClose::call(e, iteratorObject, doneValue);
+ completion = Runtime::IteratorClose::call(e, iteratorObject);
reject->call(newPromise, completion, 1);
return newPromise.asReturnedValue();
@@ -768,7 +774,7 @@ ReturnedValue PromiseCtor::method_race(const FunctionObject *f, const Value *thi
dropException(e);
if (!doneValue->toBoolean())
- completion = Runtime::IteratorClose::call(e, iteratorObject, doneValue);
+ completion = Runtime::IteratorClose::call(e, iteratorObject);
reject->call(newPromise, completion, 1);
return newPromise.asReturnedValue();
diff --git a/src/qml/jsruntime/qv4promiseobject_p.h b/src/qml/jsruntime/qv4promiseobject_p.h
index 1c11ebbfd6..fbde9f95e0 100644
--- a/src/qml/jsruntime/qv4promiseobject_p.h
+++ b/src/qml/jsruntime/qv4promiseobject_p.h
@@ -50,7 +50,7 @@ protected:
namespace Heap {
struct PromiseCtor : FunctionObject {
- void init(QV4::ExecutionContext *scope);
+ void init(ExecutionEngine *engine);
};
#define PromiseObjectMembers(class, Member) \
@@ -181,7 +181,7 @@ struct PromiseExecutionState : Object
V4_OBJECT2(PromiseExecutionState, Object)
};
-struct Q_QML_PRIVATE_EXPORT PromiseObject : Object
+struct Q_QML_EXPORT PromiseObject : Object
{
V4_OBJECT2(PromiseObject, Object)
V4_NEEDS_DESTROY
diff --git a/src/qml/jsruntime/qv4property_p.h b/src/qml/jsruntime/qv4property_p.h
index 055c132fcd..e7ad6b58d6 100644
--- a/src/qml/jsruntime/qv4property_p.h
+++ b/src/qml/jsruntime/qv4property_p.h
@@ -173,8 +173,6 @@ struct PropertyIndex {
}
-Q_DECLARE_TYPEINFO(QV4::Property, Q_RELOCATABLE_TYPE);
-
QT_END_NAMESPACE
#endif
diff --git a/src/qml/jsruntime/qv4propertykey.cpp b/src/qml/jsruntime/qv4propertykey.cpp
index ac03e01a73..65dd7e7fc1 100644
--- a/src/qml/jsruntime/qv4propertykey.cpp
+++ b/src/qml/jsruntime/qv4propertykey.cpp
@@ -7,6 +7,9 @@
#include <qv4string_p.h>
#include <qv4engine_p.h>
#include <qv4scopedvalue_p.h>
+#include <private/qv4mm_p.h>
+
+using namespace Qt::Literals::StringLiterals;
QV4::Heap::StringOrSymbol *QV4::PropertyKey::toStringOrSymbol(QV4::ExecutionEngine *e)
{
@@ -56,9 +59,9 @@ QV4::Heap::String *QV4::PropertyKey::asFunctionName(ExecutionEngine *engine, Fun
{
QString n;
if (prefix == Getter)
- n = QStringLiteral("get ");
+ n += "get "_L1;
else if (prefix == Setter)
- n = QStringLiteral("set ");
+ n += "set "_L1;
if (isArrayIndex())
n += QString::number(asArrayIndex());
else {
diff --git a/src/qml/jsruntime/qv4propertykey_p.h b/src/qml/jsruntime/qv4propertykey_p.h
index 0b8ad084d2..f3b05ee0d8 100644
--- a/src/qml/jsruntime/qv4propertykey_p.h
+++ b/src/qml/jsruntime/qv4propertykey_p.h
@@ -14,6 +14,7 @@
// We mean it.
//
+#include <private/qv4writebarrier_p.h>
#include <private/qv4global_p.h>
#include <private/qv4staticvalue_p.h>
#include <QtCore/qhashfunctions.h>
@@ -70,11 +71,18 @@ public:
// We cannot #include the declaration of Heap::StringOrSymbol here.
// Therefore we do some gymnastics to enforce the type safety.
- template<typename StringOrSymbol = Heap::StringOrSymbol>
- static PropertyKey fromStringOrSymbol(StringOrSymbol *b)
+ template<typename StringOrSymbol = Heap::StringOrSymbol, typename Engine = QV4::EngineBase>
+ static PropertyKey fromStringOrSymbol(Engine *engine, StringOrSymbol *b)
{
static_assert(std::is_base_of_v<Heap::StringOrSymbol, StringOrSymbol>);
PropertyKey key;
+ QV4::WriteBarrier::markCustom(engine, [&](QV4::MarkStack *stack) {
+ if constexpr (QV4::WriteBarrier::isInsertionBarrier) {
+ // treat this as an insertion - the StringOrSymbol becomes reachable
+ // via the propertykey, so we consequently need to mark it durnig gc
+ b->mark(stack);
+ }
+ });
key.val.setM(b);
Q_ASSERT(key.isManaged());
return key;
@@ -89,11 +97,11 @@ public:
return static_cast<StringOrSymbol *>(val.m());
}
- Q_QML_PRIVATE_EXPORT bool isString() const;
- Q_QML_PRIVATE_EXPORT bool isSymbol() const;
+ Q_QML_EXPORT bool isString() const;
+ Q_QML_EXPORT bool isSymbol() const;
bool isCanonicalNumericIndexString() const;
- Q_QML_PRIVATE_EXPORT QString toQString() const;
+ Q_QML_EXPORT QString toQString() const;
Heap::StringOrSymbol *toStringOrSymbol(ExecutionEngine *e);
quint64 id() const { return val._val; }
static PropertyKey fromId(quint64 id) {
diff --git a/src/qml/jsruntime/qv4proxy.cpp b/src/qml/jsruntime/qv4proxy.cpp
index 109ab61059..974788d420 100644
--- a/src/qml/jsruntime/qv4proxy.cpp
+++ b/src/qml/jsruntime/qv4proxy.cpp
@@ -13,6 +13,7 @@ using namespace QV4;
DEFINE_OBJECT_VTABLE(ProxyObject);
DEFINE_OBJECT_VTABLE(ProxyFunctionObject);
+DEFINE_OBJECT_VTABLE(ProxyConstructorObject);
void Heap::ProxyObject::init(const QV4::Object *target, const QV4::Object *handler)
{
@@ -25,12 +26,9 @@ void Heap::ProxyObject::init(const QV4::Object *target, const QV4::Object *handl
void Heap::ProxyFunctionObject::init(const QV4::FunctionObject *target, const QV4::Object *handler)
{
ExecutionEngine *e = internalClass->engine;
- FunctionObject::init(e->rootContext());
+ FunctionObject::init(e);
this->target.set(e, target->d());
this->handler.set(e, handler->d());
-
- if (!target->isConstructor())
- jsConstruct = nullptr;
}
@@ -643,7 +641,8 @@ OwnPropertyKeyIterator *ProxyObject::virtualOwnPropertyKeys(const Object *m, Val
}
-ReturnedValue ProxyFunctionObject::virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *newTarget)
+ReturnedValue ProxyConstructorObject::virtualCallAsConstructor(
+ const FunctionObject *f, const Value *argv, int argc, const Value *newTarget)
{
Scope scope(f);
const ProxyObject *o = static_cast<const ProxyObject *>(f);
@@ -658,10 +657,8 @@ ReturnedValue ProxyFunctionObject::virtualCallAsConstructor(const FunctionObject
if (scope.hasException())
return Encode::undefined();
- if (trap->isNullOrUndefined()) {
- Q_ASSERT(target->isConstructor());
+ if (trap->isNullOrUndefined())
return target->callAsConstructor(argv, argc, newTarget);
- }
if (!trap->isFunctionObject())
return scope.engine->throwTypeError();
@@ -708,11 +705,11 @@ ReturnedValue ProxyFunctionObject::virtualCall(const FunctionObject *f, const Va
DEFINE_OBJECT_VTABLE(Proxy);
-void Heap::Proxy::init(QV4::ExecutionContext *ctx)
+void Heap::Proxy::init(QV4::ExecutionEngine *engine)
{
- Heap::FunctionObject::init(ctx, QStringLiteral("Proxy"));
+ Heap::FunctionObject::init(engine, QStringLiteral("Proxy"));
- Scope scope(ctx);
+ Scope scope(engine);
Scoped<QV4::Proxy> ctor(scope, this);
ctor->defineDefaultProperty(QStringLiteral("revocable"), QV4::Proxy::method_revocable, 2);
ctor->defineReadonlyConfigurableProperty(scope.engine->id_length(), Value::fromInt32(2));
@@ -734,9 +731,18 @@ ReturnedValue Proxy::virtualCallAsConstructor(const FunctionObject *f, const Val
return scope.engine->throwTypeError();
const FunctionObject *targetFunction = target->as<FunctionObject>();
- if (targetFunction)
- return scope.engine->memoryManager->allocate<ProxyFunctionObject>(targetFunction, handler)->asReturnedValue();
- return scope.engine->memoryManager->allocate<ProxyObject>(target, handler)->asReturnedValue();
+ if (!targetFunction) {
+ return scope.engine->memoryManager->allocate<ProxyObject>(target, handler)
+ ->asReturnedValue();
+ }
+
+ if (targetFunction->isConstructor()) {
+ return scope.engine->memoryManager->allocate<ProxyConstructorObject>(
+ targetFunction, handler)->asReturnedValue();
+ }
+
+ return scope.engine->memoryManager->allocate<ProxyFunctionObject>(targetFunction, handler)
+ ->asReturnedValue();
}
ReturnedValue Proxy::virtualCall(const FunctionObject *f, const Value *, const Value *, int)
@@ -753,7 +759,10 @@ ReturnedValue Proxy::method_revocable(const FunctionObject *f, const Value *, co
Q_ASSERT(proxy);
ScopedString revoke(scope, scope.engine->newString(QStringLiteral("revoke")));
- ScopedFunctionObject revoker(scope, scope.engine->memoryManager->allocate<FunctionObject>(scope.engine->rootContext(), nullptr, method_revoke));
+ ScopedFunctionObject revoker(
+ scope,
+ scope.engine->memoryManager->allocate<DynamicFunctionObject>(
+ scope.engine, nullptr, method_revoke));
revoker->defineReadonlyConfigurableProperty(scope.engine->id_length(), Value::fromInt32(0));
revoker->defineDefaultProperty(scope.engine->symbol_revokableProxy(), proxy);
diff --git a/src/qml/jsruntime/qv4proxy_p.h b/src/qml/jsruntime/qv4proxy_p.h
index b3be05a11b..b3ce9c7a96 100644
--- a/src/qml/jsruntime/qv4proxy_p.h
+++ b/src/qml/jsruntime/qv4proxy_p.h
@@ -37,13 +37,15 @@ struct ProxyFunctionObject : ProxyObject {
void init(const QV4::FunctionObject *target, const QV4::Object *handler);
};
+struct ProxyConstructorObject : ProxyFunctionObject {};
+
#define ProxyMembers(class, Member) \
Member(class, Pointer, Symbol *, revokableProxySymbol) \
DECLARE_HEAP_OBJECT(Proxy, FunctionObject) {
DECLARE_MARKOBJECTS(Proxy)
- void init(QV4::ExecutionContext *ctx);
+ void init(ExecutionEngine *engine);
};
}
@@ -60,9 +62,6 @@ struct ProxyObject : FunctionObject {
V4_OBJECT2(ProxyObject, Object)
Q_MANAGED_TYPE(ProxyObject)
V4_INTERNALCLASS(ProxyObject)
- enum {
- IsFunctionObject = false
- };
static ReturnedValue virtualGet(const Managed *m, PropertyKey id, const Value *receiver, bool *hasProperty);
static bool virtualPut(Managed *m, PropertyKey id, const Value &value, Value *receiver);
@@ -81,14 +80,16 @@ struct ProxyFunctionObject : ProxyObject {
V4_OBJECT2(ProxyFunctionObject, FunctionObject)
Q_MANAGED_TYPE(ProxyObject)
V4_INTERNALCLASS(ProxyFunctionObject)
- enum {
- IsFunctionObject = true
- };
- static ReturnedValue virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *);
static ReturnedValue virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc);
};
+struct ProxyConstructorObject : ProxyFunctionObject {
+ V4_OBJECT2(ProxyConstructorObject, ProxyFunctionObject)
+
+ static ReturnedValue virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *);
+};
+
struct Proxy : FunctionObject
{
V4_OBJECT2(Proxy, FunctionObject)
diff --git a/src/qml/jsruntime/qv4qmetaobjectwrapper.cpp b/src/qml/jsruntime/qv4qmetaobjectwrapper.cpp
new file mode 100644
index 0000000000..6521c98dbf
--- /dev/null
+++ b/src/qml/jsruntime/qv4qmetaobjectwrapper.cpp
@@ -0,0 +1,111 @@
+// Copyright (C) 2024 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
+
+#include "qv4qmetaobjectwrapper_p.h"
+
+#include <private/qqmlobjectorgadget_p.h>
+#include <private/qv4jscall_p.h>
+#include <private/qv4qobjectwrapper_p.h>
+
+QT_BEGIN_NAMESPACE
+
+using namespace Qt::StringLiterals;
+
+namespace QV4 {
+
+void Heap::QMetaObjectWrapper::init(const QMetaObject *metaObject)
+{
+ FunctionObject::init();
+ m_metaObject = metaObject;
+}
+
+void Heap::QMetaObjectWrapper::destroy()
+{
+ delete[] m_constructors;
+ FunctionObject::destroy();
+}
+
+ReturnedValue QMetaObjectWrapper::create(ExecutionEngine *engine, const QMetaObject* metaObject) {
+
+ Scope scope(engine);
+ Scoped<QMetaObjectWrapper> mo(scope, engine->memoryManager->allocate<QMetaObjectWrapper>(metaObject)->asReturnedValue());
+ mo->init(engine);
+ return mo->asReturnedValue();
+}
+
+void QMetaObjectWrapper::init(ExecutionEngine *) {
+ const QMetaObject &mo = *d()->metaObject();
+
+ for (int i = 0; i < mo.enumeratorCount(); i++) {
+ QMetaEnum Enum = mo.enumerator(i);
+ for (int k = 0; k < Enum.keyCount(); k++) {
+ const char* key = Enum.key(k);
+ const int value = Enum.value(k);
+ defineReadonlyProperty(QLatin1String(key), Value::fromInt32(value));
+ }
+ }
+}
+
+ReturnedValue QMetaObjectWrapper::virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *)
+{
+ Q_ASSERT(f->as<QMetaObjectWrapper>());
+ return construct(static_cast<const QMetaObjectWrapper*>(f)->d(), argv, argc);
+}
+
+ReturnedValue QMetaObjectWrapper::constructInternal(
+ const QMetaObject *mo, const QQmlPropertyData *constructors, Heap::FunctionObject *d,
+ const Value *argv, int argc)
+{
+ ExecutionEngine *v4 = d->internalClass->engine;
+
+ if (!constructors) {
+ return v4->throwTypeError(QLatin1String(mo->className())
+ + QLatin1String(" has no invokable constructor"));
+ }
+
+ Scope scope(v4);
+ ScopedObject object(scope);
+ JSCallData cData(nullptr, argv, argc);
+ CallData *callData = cData.callData(scope);
+
+ const QQmlObjectOrGadget objectOrGadget(mo);
+
+ const auto callType = [](QMetaType metaType) {
+ return metaType.flags() & QMetaType::PointerToQObject
+ ? QMetaObject::CreateInstance
+ : QMetaObject::ConstructInPlace;
+ };
+
+ const int constructorCount = mo->constructorCount();
+ if (constructorCount == 1) {
+ object = QObjectMethod::callPrecise(
+ objectOrGadget, constructors[0], v4, callData,
+ callType(constructors[0].propType()));
+ } else if (const QQmlPropertyData *ctor = QObjectMethod::resolveOverloaded(
+ objectOrGadget, constructors, constructorCount, v4, callData)) {
+ object = QObjectMethod::callPrecise(
+ objectOrGadget, *ctor, v4, callData, callType(ctor->propType()));
+ }
+
+ if (object) {
+ Scoped<FunctionObject> functionObject(scope, d);
+ object->defineDefaultProperty(v4->id_constructor(), functionObject);
+ object->setPrototypeOf(functionObject);
+ }
+
+ return object.asReturnedValue();
+}
+
+bool QMetaObjectWrapper::virtualIsEqualTo(Managed *a, Managed *b)
+{
+ const QMetaObjectWrapper *aMetaObject = a->as<QMetaObjectWrapper>();
+ Q_ASSERT(aMetaObject);
+ const QMetaObjectWrapper *bMetaObject = b->as<QMetaObjectWrapper>();
+ return bMetaObject && aMetaObject->metaObject() == bMetaObject->metaObject();
+}
+
+DEFINE_OBJECT_VTABLE(QMetaObjectWrapper);
+
+} // namespace QV4
+
+QT_END_NAMESPACE
diff --git a/src/qml/jsruntime/qv4qmetaobjectwrapper_p.h b/src/qml/jsruntime/qv4qmetaobjectwrapper_p.h
new file mode 100644
index 0000000000..c44b18f291
--- /dev/null
+++ b/src/qml/jsruntime/qv4qmetaobjectwrapper_p.h
@@ -0,0 +1,120 @@
+// Copyright (C) 2024 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
+
+#ifndef QV4QMETAOBJECTWRAPPER_P_H
+#define QV4QMETAOBJECTWRAPPER_P_H
+
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the Qt API. It exists purely as an
+// implementation detail. This header file may change from version to
+// version without notice, or even be removed.
+//
+// We mean it.
+//
+
+#include <private/qv4functionobject_p.h>
+#include <private/qv4value_p.h>
+
+#include <QtCore/qmetaobject.h>
+
+QT_BEGIN_NAMESPACE
+
+class QQmlPropertyData;
+
+namespace QV4 {
+namespace Heap {
+
+struct QMetaObjectWrapper : FunctionObject
+{
+ void init(const QMetaObject *metaObject);
+ void destroy();
+
+ const QMetaObject *metaObject() const { return m_metaObject; }
+ QMetaType metaType() const
+ {
+ const QMetaType type = m_metaObject->metaType();
+ if (type.flags() & QMetaType::IsGadget)
+ return type;
+
+ // QObject* is our best guess because we can't get from a metatype to
+ // the metatype of its pointer.
+ return QMetaType::fromType<QObject *>();
+ }
+
+ const QQmlPropertyData *ensureConstructorsCache(
+ const QMetaObject *metaObject, QMetaType metaType)
+ {
+ Q_ASSERT(metaObject);
+ if (!m_constructors)
+ m_constructors = createConstructors(metaObject, metaType);
+ return m_constructors;
+ }
+
+
+ static const QQmlPropertyData *createConstructors(
+ const QMetaObject *metaObject, QMetaType metaType)
+ {
+ Q_ASSERT(metaObject);
+ const int count = metaObject->constructorCount();
+ if (count == 0)
+ return nullptr;
+
+ QQmlPropertyData *constructors = new QQmlPropertyData[count];
+
+ for (int i = 0; i < count; ++i) {
+ QMetaMethod method = metaObject->constructor(i);
+ QQmlPropertyData &d = constructors[i];
+ d.load(method);
+ d.setPropType(metaType);
+ d.setCoreIndex(i);
+ }
+
+ return constructors;
+ }
+
+private:
+ const QMetaObject *m_metaObject;
+ const QQmlPropertyData *m_constructors;
+};
+
+} // namespace Heap
+
+struct Q_QML_EXPORT QMetaObjectWrapper : public FunctionObject
+{
+ V4_OBJECT2(QMetaObjectWrapper, FunctionObject)
+ V4_NEEDS_DESTROY
+
+ static ReturnedValue create(ExecutionEngine *engine, const QMetaObject* metaObject);
+ const QMetaObject *metaObject() const { return d()->metaObject(); }
+
+ template<typename HeapObject>
+ ReturnedValue static construct(HeapObject *d, const Value *argv, int argc)
+ {
+ const QMetaObject *mo = d->metaObject();
+ return constructInternal(
+ mo, d->ensureConstructorsCache(mo, d->metaType()), d, argv, argc);
+ }
+
+protected:
+ static ReturnedValue virtualCallAsConstructor(
+ const FunctionObject *, const Value *argv, int argc, const Value *);
+ static bool virtualIsEqualTo(Managed *a, Managed *b);
+
+private:
+ void init(ExecutionEngine *engine);
+
+ static ReturnedValue constructInternal(
+ const QMetaObject *mo, const QQmlPropertyData *constructors, Heap::FunctionObject *d,
+ const Value *argv, int argc);
+};
+
+} // namespace QV4
+
+QT_END_NAMESPACE
+
+#endif // QV4QMETAOBJECTWRAPPER_P_H
+
+
diff --git a/src/qml/jsruntime/qv4qmlcontext.cpp b/src/qml/jsruntime/qv4qmlcontext.cpp
index 1cabf7c6c2..53444cddb7 100644
--- a/src/qml/jsruntime/qv4qmlcontext.cpp
+++ b/src/qml/jsruntime/qv4qmlcontext.cpp
@@ -19,6 +19,7 @@
#include <private/qv4module_p.h>
#include <private/qv4objectproto_p.h>
#include <private/qv4qobjectwrapper_p.h>
+#include <private/qv4stackframe_p.h>
#include <private/qv4value_p.h>
#include <QtCore/qloggingcategory.h>
@@ -196,9 +197,11 @@ ReturnedValue QQmlContextWrapper::getPropertyAndBase(const QQmlContextWrapper *r
return result->asReturnedValue();
}
- if (context->imports() && name->startsWithUpper()) {
+ QQmlEnginePrivate *ep = QQmlEnginePrivate::get(v4->qmlEngine());
+ if (context->imports() && (name->startsWithUpper() || context->valueTypesAreAddressable())) {
// Search for attached properties, enums and imported scripts
- QQmlTypeNameCache::Result r = context->imports()->query<QQmlImport::AllowRecursion>(name);
+ QQmlTypeNameCache::Result r = context->imports()->query<QQmlImport::AllowRecursion>(
+ name, QQmlTypeLoader::get(ep));
if (r.isValid()) {
if (hasProperty)
@@ -220,10 +223,10 @@ ReturnedValue QQmlContextWrapper::getPropertyAndBase(const QQmlContextWrapper *r
QQmlEnginePrivate *e = QQmlEnginePrivate::get(v4->qmlEngine());
if (r.type.isQObjectSingleton() || r.type.isCompositeSingleton()) {
e->singletonInstance<QObject*>(r.type);
- lookup->qmlContextSingletonLookup.singletonObject =
+ lookup->qmlContextSingletonLookup.singletonObject.set(v4,
Value::fromReturnedValue(
QQmlTypeWrapper::create(v4, nullptr, r.type)
- ).heapObject();
+ ).heapObject());
} else {
QJSValue singleton = e->singletonInstance<QJSValue>(r.type);
@@ -232,7 +235,7 @@ ReturnedValue QQmlContextWrapper::getPropertyAndBase(const QQmlContextWrapper *r
Q_ASSERT(!QJSValuePrivate::asQString(&singleton));
if (QV4::Value *val = QJSValuePrivate::takeManagedValue(&singleton)) {
- lookup->qmlContextSingletonLookup.singletonObject = val->heapObject();
+ lookup->qmlContextSingletonLookup.singletonObject.set(v4, val->heapObject());
} else {
lookup->qmlContextSingletonLookup.singletonValue = QJSValuePrivate::asReturnedValue(&singleton);
isValueSingleton = true;
@@ -248,7 +251,7 @@ ReturnedValue QQmlContextWrapper::getPropertyAndBase(const QQmlContextWrapper *r
result = QQmlTypeWrapper::create(v4, scopeObject, context->imports(), r.importNamespace);
}
if (lookup) {
- lookup->qmlTypeLookup.qmlTypeWrapper = result->heapObject();
+ lookup->qmlTypeLookup.qmlTypeWrapper.set(v4, result->heapObject());
lookup->qmlContextPropertyGetter = QQmlContextWrapper::lookupType;
}
return result->asReturnedValue();
@@ -257,7 +260,6 @@ ReturnedValue QQmlContextWrapper::getPropertyAndBase(const QQmlContextWrapper *r
// Fall through
}
- QQmlEnginePrivate *ep = QQmlEnginePrivate::get(v4->qmlEngine());
Lookup * const originalLookup = lookup;
decltype(lookup->qmlContextPropertyGetter) contextGetterFunction = QQmlContextWrapper::lookupContextObjectProperty;
@@ -268,9 +270,33 @@ ReturnedValue QQmlContextWrapper::getPropertyAndBase(const QQmlContextWrapper *r
contextGetterFunction = QQmlContextWrapper::lookupScopeObjectProperty;
}
+ QQmlRefPointer<QQmlContextData> outer = context;
while (context) {
- if (auto property = searchContextProperties(v4, context, name, hasProperty, base, lookup, originalLookup, ep))
- return *property;
+ if (outer == context) {
+ if (auto property = searchContextProperties(
+ v4, context, name, hasProperty, base, lookup, originalLookup, ep)) {
+ return *property;
+ }
+
+ outer = outer->parent();
+
+ if (const auto cu = context->typeCompilationUnit(); cu && cu->componentsAreBound()) {
+ // If components are bound in this CU, we can search the whole context hierarchy
+ // of the file. Bound components' contexts override their local properties.
+ // You also can't instantiate bound components outside of their creation
+ // context. Therefore this is safe.
+
+ for (;
+ outer && outer->typeCompilationUnit() == cu;
+ outer = outer->parent()) {
+ if (auto property = searchContextProperties(
+ v4, outer, name, hasProperty, base,
+ nullptr, originalLookup, ep)) {
+ return *property;
+ }
+ }
+ }
+ }
// Search scope object
if (scopeObject) {
@@ -475,8 +501,8 @@ ReturnedValue QQmlContextWrapper::resolveQmlContextPropertyLookupGetter(Lookup *
{
Scope scope(engine);
auto *func = engine->currentStackFrame->v4Function;
- PropertyKey name =engine->identifierTable->asPropertyKey(
- func->compilationUnit->runtimeStrings[l->nameIndex]);
+ ScopedPropertyKey name(scope, engine->identifierTable->asPropertyKey(
+ func->compilationUnit->runtimeStrings[l->nameIndex]));
// Special hack for bounded signal expressions, where the parameters of signals are injected
// into the handler expression through the locals of the call context. So for onClicked: { ... }
@@ -490,7 +516,7 @@ ReturnedValue QQmlContextWrapper::resolveQmlContextPropertyLookupGetter(Lookup *
const auto location = func->sourceLocation();
qCWarning(lcQmlContext).nospace().noquote()
<< location.sourceFile << ":" << location.line << ":" << location.column
- << " Parameter \"" << name.toQString() << "\" is not declared."
+ << " Parameter \"" << name->toQString() << "\" is not declared."
<< " Injection of parameters into signal handlers is deprecated."
<< " Use JavaScript functions with formal parameters instead.";
@@ -526,7 +552,7 @@ ReturnedValue QQmlContextWrapper::resolveQmlContextPropertyLookupGetter(Lookup *
}
}
if (!hasProperty)
- return engine->throwReferenceError(name.toQString());
+ return engine->throwReferenceError(name->toQString());
return result->asReturnedValue();
}
@@ -789,7 +815,7 @@ ReturnedValue QQmlContextWrapper::lookupType(Lookup *l, ExecutionEngine *engine,
Heap::Base *heapObject = l->qmlTypeLookup.qmlTypeWrapper;
if (static_cast<Heap::QQmlTypeWrapper *>(heapObject)->object != scopeObject) {
- l->qmlTypeLookup.qmlTypeWrapper = nullptr;
+ l->qmlTypeLookup.qmlTypeWrapper.clear();
l->qmlContextPropertyGetter = QQmlContextWrapper::resolveQmlContextPropertyLookupGetter;
return QQmlContextWrapper::resolveQmlContextPropertyLookupGetter(l, engine, base);
}
diff --git a/src/qml/jsruntime/qv4qobjectwrapper.cpp b/src/qml/jsruntime/qv4qobjectwrapper.cpp
index 694b7828b6..eaee078a5f 100644
--- a/src/qml/jsruntime/qv4qobjectwrapper.cpp
+++ b/src/qml/jsruntime/qv4qobjectwrapper.cpp
@@ -3,58 +3,57 @@
#include "qv4qobjectwrapper_p.h"
-#include <private/qqmlobjectorgadget_p.h>
-#include <private/qqmlengine_p.h>
-#include <private/qqmlvmemetaobject_p.h>
-#include <private/qqmlbinding_p.h>
#include <private/qjsvalue_p.h>
-#include <private/qqmlexpression_p.h>
-#include <private/qqmlglobal_p.h>
+
+#include <private/qqmlbinding_p.h>
+#include <private/qqmlbuiltinfunctions_p.h>
+#include <private/qqmlengine_p.h>
+#include <private/qqmlobjectorgadget_p.h>
+#include <private/qqmlpropertybinding_p.h>
+#include <private/qqmlscriptstring_p.h>
+#include <private/qqmlsignalnames_p.h>
#include <private/qqmltypewrapper_p.h>
#include <private/qqmlvaluetypewrapper_p.h>
-#include <private/qqmllistwrapper_p.h>
-#include <private/qqmlbuiltinfunctions_p.h>
+#include <private/qqmlvmemetaobject_p.h>
#include <private/qv4arraybuffer_p.h>
+#include <private/qv4compileddata_p.h>
+#include <private/qv4dateobject_p.h>
#include <private/qv4functionobject_p.h>
-#include <private/qv4runtime_p.h>
-#include <private/qv4variantobject_p.h>
#include <private/qv4identifiertable_p.h>
-#include <private/qv4lookup_p.h>
-#include <private/qv4qmlcontext_p.h>
-#include <private/qv4sequenceobject_p.h>
-#include <private/qv4objectproto_p.h>
+#include <private/qv4jscall_p.h>
#include <private/qv4jsonobject_p.h>
+#include <private/qv4lookup_p.h>
+#include <private/qv4mm_p.h>
#include <private/qv4regexpobject_p.h>
-#include <private/qv4dateobject_p.h>
+#include <private/qv4runtime_p.h>
#include <private/qv4scopedvalue_p.h>
-#include <private/qv4jscall_p.h>
-#include <private/qv4mm_p.h>
-#include <private/qqmlscriptstring_p.h>
-#include <private/qv4compileddata_p.h>
-#include <private/qqmlpropertybinding_p.h>
+#include <private/qv4sequenceobject_p.h>
+#include <private/qv4variantobject_p.h>
-#include <QtQml/qjsvalue.h>
#include <QtCore/qjsonarray.h>
#include <QtCore/qjsonobject.h>
#include <QtCore/qjsonvalue.h>
-#include <QtCore/qvarlengtharray.h>
-#include <QtCore/qtimer.h>
-#include <QtCore/qatomic.h>
+#include <QtCore/qloggingcategory.h>
#include <QtCore/qmetaobject.h>
+#include <QtCore/qqueue.h>
+#include <QtCore/qtimer.h>
+#include <QtCore/qtypes.h>
+#include <QtCore/qvarlengtharray.h>
+
+#include <vector>
+
#if QT_CONFIG(qml_itemmodel)
#include <QtCore/qabstractitemmodel.h>
#endif
-#include <QtCore/qloggingcategory.h>
-#include <QtCore/qqueue.h>
-#include <vector>
QT_BEGIN_NAMESPACE
-Q_LOGGING_CATEGORY(lcBindingRemoval, "qt.qml.binding.removal", QtWarningMsg)
+Q_LOGGING_CATEGORY(lcBuiltinsBindingRemoval, "qt.qml.binding.removal", QtWarningMsg)
Q_LOGGING_CATEGORY(lcObjectConnect, "qt.qml.object.connect", QtWarningMsg)
Q_LOGGING_CATEGORY(lcOverloadResolution, "qt.qml.overloadresolution", QtWarningMsg)
Q_LOGGING_CATEGORY(lcMethodBehavior, "qt.qml.method.behavior")
+Q_LOGGING_CATEGORY(lcSignalHandler, "qt.qml.signalhandler")
// The code in this file does not violate strict aliasing, but GCC thinks it does
// so turn off the warnings for us to have a clean build
@@ -130,37 +129,133 @@ static ReturnedValue loadProperty(
if (property.isQList() && propMetaType.flags().testFlag(QMetaType::IsQmlList))
return QmlListWrapper::create(v4, object, property.coreIndex(), propMetaType);
- // TODO: Check all the builtin types here. See getGadgetProperty() in qqmlvaluetypewrapper.cpp
- switch (property.isEnum() ? QMetaType::Int : propMetaType.id()) {
- case QMetaType::Int: {
- int v = 0;
+ const auto encodeSimple = [&](auto v) {
property.readProperty(object, &v);
return Encode(v);
+ };
+
+ const auto encodeInt = [&](auto v) {
+ property.readProperty(object, &v);
+ return Encode(int(v));
+ };
+
+ const auto encodeDouble = [&](auto v) {
+ property.readProperty(object, &v);
+ return Encode(double(v));
+ };
+
+ const auto encodeDate = [&](auto v) {
+ property.readProperty(object, &v);
+ return Encode(v4->newDateObject(
+ v, wrapper, property.coreIndex(), referenceFlags(scope.engine, property)));
+ };
+
+ const auto encodeString = [&](auto v) {
+ property.readProperty(object, &v);
+ return v4->newString(v)->asReturnedValue();
+ };
+
+ const auto encodeSequence = [&](QMetaSequence metaSequence) {
+ // Pass nullptr as data. It's lazy-loaded.
+ return QV4::SequencePrototype::newSequence(
+ v4, propMetaType, metaSequence, nullptr,
+ wrapper, property.coreIndex(), referenceFlags(scope.engine, property));
+ };
+
+
+ switch (property.isEnum() ? propMetaType.underlyingType().id() : propMetaType.id()) {
+ case QMetaType::UnknownType:
+ case QMetaType::Void:
+ return Encode::undefined();
+ case QMetaType::Nullptr:
+ case QMetaType::VoidStar:
+ return Encode::null();
+ case QMetaType::Int:
+ return encodeSimple(int());
+ case QMetaType::Bool:
+ return encodeSimple(bool());
+ case QMetaType::QString:
+ return encodeString(QString());
+ case QMetaType::QByteArray: {
+ QByteArray v;
+ property.readProperty(object, &v);
+ return v4->newArrayBuffer(v)->asReturnedValue();
}
- case QMetaType::Bool: {
- bool v = false;
+ case QMetaType::QChar:
+ return encodeString(QChar());
+ case QMetaType::Char16:
+ return encodeString(char16_t());
+ case QMetaType::UInt:
+ return encodeSimple(uint());
+ case QMetaType::Float:
+ return encodeSimple(float());
+ case QMetaType::Double:
+ return encodeSimple(double());
+ case QMetaType::Short:
+ return encodeInt(short());
+ case QMetaType::UShort:
+ return encodeInt(ushort());
+ case QMetaType::Char:
+ return encodeInt(char());
+ case QMetaType::UChar:
+ return encodeInt(uchar());
+ case QMetaType::SChar:
+ return encodeInt(qint8());
+ case QMetaType::Long:
+ return encodeDouble(long());
+ case QMetaType::ULong:
+ return encodeDouble(ulong());
+ case QMetaType::LongLong:
+ return encodeDouble(qlonglong());
+ case QMetaType::ULongLong:
+ return encodeDouble(qulonglong());
+ case QMetaType::QDateTime:
+ return encodeDate(QDateTime());
+ case QMetaType::QDate:
+ return encodeDate(QDate());
+ case QMetaType::QTime:
+ return encodeDate(QTime());
+#if QT_CONFIG(regularexpression)
+ case QMetaType::QRegularExpression: {
+ QRegularExpression v;
property.readProperty(object, &v);
- return Encode(v);
+ return Encode(v4->newRegExpObject(v));
}
- case QMetaType::QString: {
- QString v;
+#endif
+ case QMetaType::QVariantMap: {
+ QVariantMap v;
property.readProperty(object, &v);
- return v4->newString(v)->asReturnedValue();
+ return scope.engine->fromData(
+ propMetaType, &v, wrapper, property.coreIndex(), referenceFlags(v4, property));
}
- case QMetaType::UInt: {
- uint v = 0;
+ case QMetaType::QJsonValue: {
+ QJsonValue v;
property.readProperty(object, &v);
- return Encode(v);
+ return QV4::JsonObject::fromJsonValue(v4, v);
}
- case QMetaType::Float: {
- float v = 0;
+ case QMetaType::QJsonObject: {
+ QJsonObject v;
property.readProperty(object, &v);
- return Encode(v);
+ return QV4::JsonObject::fromJsonObject(v4, v);
}
- case QMetaType::Double: {
- double v = 0;
+ case QMetaType::QJsonArray:
+ return encodeSequence(QMetaSequence::fromContainer<QJsonArray>());
+ case QMetaType::QStringList:
+ return encodeSequence(QMetaSequence::fromContainer<QStringList>());
+ case QMetaType::QVariantList:
+ return encodeSequence(QMetaSequence::fromContainer<QVariantList>());
+ case QMetaType::QUrl: {
+ // ### Qt7: We really want this to be a JS URL object, but that would break things.
+ QUrl v;
property.readProperty(object, &v);
- return Encode(v);
+ return Encode(v4->newVariantObject(propMetaType, &v));
+ }
+ case QMetaType::QPixmap:
+ case QMetaType::QImage: {
+ // Scarce value types
+ QVariant v(propMetaType);
+ property.readProperty(object, v.data());
+ return Encode(v4->newVariantObject(propMetaType, v.constData()));
}
default:
break;
@@ -204,13 +299,9 @@ static ReturnedValue loadProperty(
}
// See if it's a sequence type.
- // Pass nullptr as data. It's lazy-loaded.
- QV4::ScopedValue retn(scope, QV4::SequencePrototype::newSequence(
- v4, propMetaType, nullptr,
- wrapper, property.coreIndex(),
- referenceFlags(scope.engine, property)));
- if (!retn->isUndefined())
- return retn->asReturnedValue();
+ const QQmlType qmlType = QQmlMetaType::qmlListType(propMetaType);
+ if (qmlType.isSequentialContainer())
+ return encodeSequence(qmlType.listMetaSequence());
QVariant v(propMetaType);
property.readProperty(object, v.data());
@@ -258,20 +349,15 @@ ReturnedValue QObjectWrapper::getProperty(
Q_ASSERT(vmemo);
return vmemo->vmeMethod(property->coreIndex());
} else if (property->isV4Function()) {
- Scope scope(engine);
- ScopedContext global(scope, engine->qmlContext());
- if (!global)
- global = engine->rootContext();
return QObjectMethod::create(
- global, (flags & AttachMethods) ? object : nullptr, property->coreIndex());
+ engine, (flags & AttachMethods) ? wrapper : nullptr, property->coreIndex());
} else if (property->isSignalHandler()) {
QmlSignalHandler::initProto(engine);
return engine->memoryManager->allocate<QmlSignalHandler>(
object, property->coreIndex())->asReturnedValue();
} else {
- ExecutionContext *global = engine->rootContext();
return QObjectMethod::create(
- global, (flags & AttachMethods) ? object : nullptr, property->coreIndex());
+ engine, (flags & AttachMethods) ? wrapper : nullptr, property->coreIndex());
}
}
@@ -290,7 +376,8 @@ ReturnedValue QObjectWrapper::getProperty(
}
}
-static OptionalReturnedValue getDestroyOrToStringMethod(ExecutionEngine *v4, String *name, QObject *qobj, bool *hasProperty = nullptr)
+static OptionalReturnedValue getDestroyOrToStringMethod(
+ ExecutionEngine *v4, String *name, Heap::Object *qobj, bool *hasProperty = nullptr)
{
int index = 0;
if (name->equals(v4->id_destroy()))
@@ -302,8 +389,7 @@ static OptionalReturnedValue getDestroyOrToStringMethod(ExecutionEngine *v4, Str
if (hasProperty)
*hasProperty = true;
- ExecutionContext *global = v4->rootContext();
- return OptionalReturnedValue(QObjectMethod::create(global, qobj, index));
+ return OptionalReturnedValue(QObjectMethod::create(v4, qobj, index));
}
static OptionalReturnedValue getPropertyFromImports(
@@ -313,24 +399,29 @@ static OptionalReturnedValue getPropertyFromImports(
if (!qmlContext || !qmlContext->imports())
return OptionalReturnedValue();
- QQmlTypeNameCache::Result r = qmlContext->imports()->query(name);
-
if (hasProperty)
*hasProperty = true;
- if (!r.isValid())
- return OptionalReturnedValue();
+ if (QQmlTypeLoader *typeLoader = v4->typeLoader()) {
+ QQmlTypeNameCache::Result r = qmlContext->imports()->query(name, typeLoader);
+
+ if (!r.isValid())
+ return OptionalReturnedValue();
- if (r.scriptIndex != -1) {
- return OptionalReturnedValue(Encode::undefined());
- } else if (r.type.isValid()) {
- return OptionalReturnedValue(QQmlTypeWrapper::create(v4, qobj,r.type, Heap::QQmlTypeWrapper::ExcludeEnums));
- } else if (r.importNamespace) {
- return OptionalReturnedValue(QQmlTypeWrapper::create(
- v4, qobj, qmlContext->imports(), r.importNamespace,
- Heap::QQmlTypeWrapper::ExcludeEnums));
+ if (r.scriptIndex != -1) {
+ return OptionalReturnedValue(Encode::undefined());
+ } else if (r.type.isValid()) {
+ return OptionalReturnedValue(
+ QQmlTypeWrapper::create(v4, qobj,r.type, Heap::QQmlTypeWrapper::ExcludeEnums));
+ } else if (r.importNamespace) {
+ return OptionalReturnedValue(QQmlTypeWrapper::create(
+ v4, qobj, qmlContext->imports(), r.importNamespace,
+ Heap::QQmlTypeWrapper::ExcludeEnums));
+ }
+ Q_UNREACHABLE_RETURN(OptionalReturnedValue());
+ } else {
+ return OptionalReturnedValue();
}
- Q_UNREACHABLE_RETURN(OptionalReturnedValue());
}
ReturnedValue QObjectWrapper::getQmlProperty(
@@ -347,7 +438,7 @@ ReturnedValue QObjectWrapper::getQmlProperty(
ExecutionEngine *v4 = engine();
- if (auto methodValue = getDestroyOrToStringMethod(v4, name, d()->object(), hasProperty))
+ if (auto methodValue = getDestroyOrToStringMethod(v4, name, d(), hasProperty))
return *methodValue;
QQmlPropertyData local;
@@ -390,7 +481,7 @@ ReturnedValue QObjectWrapper::getQmlProperty(
return Encode::null();
}
- if (auto methodValue = getDestroyOrToStringMethod(engine, name, object, hasProperty))
+ if (auto methodValue = getDestroyOrToStringMethod(engine, name, wrapper, hasProperty))
return *methodValue;
QQmlData *ddata = QQmlData::get(object, false);
@@ -465,6 +556,28 @@ bool QObjectWrapper::setQmlProperty(
return true;
}
+/*!
+ \internal
+ If an QObjectWrapper is created via wrap, then it needs to be stored somewhere.
+ Otherwise, the garbage collector will immediately collect it if it is already
+ past the "mark QObjectWrapper's" phase (note that QObjectWrapper are marked
+ by iterating over a list of all QObjectWrapper, and then checking if the
+ wrapper fulfills some conditions).
+ However, sometimes we don't really want to keep a reference to the wrapper,
+ but just want to make sure that it exists (and we know that the wrapper
+ already fulfills the conditions to be kept alive). Then ensureWrapper
+ can be used, which creates the wrapper and ensures that it is also
+ marked.
+ */
+void QObjectWrapper::ensureWrapper(ExecutionEngine *engine, QObject *object)
+{
+ QV4::Scope scope(engine);
+ QV4::Scoped<QV4::QObjectWrapper> wrapper {scope, QV4::QObjectWrapper::wrap(engine, object)};
+ QV4::WriteBarrier::markCustom(engine, [&wrapper](QV4::MarkStack *ms) {
+ wrapper->mark(ms);
+ });
+}
+
void QObjectWrapper::setProperty(
ExecutionEngine *engine, QObject *object,
const QQmlPropertyData *property, const Value &value)
@@ -478,7 +591,9 @@ void QObjectWrapper::setProperty(
Scope scope(engine);
if (ScopedFunctionObject f(scope, value); f) {
- if (!f->isBinding()) {
+ if (f->as<QQmlTypeWrapper>()) {
+ // Ignore. It's probably a singleton or an attached type.
+ } else if (!f->isBinding()) {
const bool isAliasToAllowed = [&]() {
if (property->isAlias()) {
const QQmlPropertyIndex originalIndex(property->coreIndex(), -1);
@@ -511,7 +626,7 @@ void QObjectWrapper::setProperty(
QQmlRefPointer<QQmlContextData> callingQmlContext = scope.engine->callingQmlContext();
Scoped<QQmlBindingFunction> bindingFunction(scope, (const Value &)f);
- ScopedFunctionObject f(scope, bindingFunction->bindingFunction());
+ Scoped<JavaScriptFunctionObject> f(scope, bindingFunction->bindingFunction());
ScopedContext ctx(scope, f->scope());
// binding assignment.
@@ -549,16 +664,28 @@ void QObjectWrapper::setProperty(
}
}
- if (Q_UNLIKELY(lcBindingRemoval().isInfoEnabled())) {
+ if (Q_UNLIKELY(lcBuiltinsBindingRemoval().isInfoEnabled())) {
if (auto binding = QQmlPropertyPrivate::binding(object, QQmlPropertyIndex(property->coreIndex()))) {
- Q_ASSERT(binding->kind() == QQmlAbstractBinding::QmlBinding);
- const auto qmlBinding = static_cast<const QQmlBinding*>(binding);
const auto stackFrame = engine->currentStackFrame;
- qCInfo(lcBindingRemoval,
- "Overwriting binding on %s::%s at %s:%d that was initially bound at %s",
- object->metaObject()->className(), qPrintable(property->name(object)),
- qPrintable(stackFrame->source()), stackFrame->lineNumber(),
- qPrintable(qmlBinding->expressionIdentifier()));
+ switch (binding->kind()) {
+ case QQmlAbstractBinding::QmlBinding: {
+ const auto qmlBinding = static_cast<const QQmlBinding*>(binding);
+ qCInfo(lcBuiltinsBindingRemoval,
+ "Overwriting binding on %s::%s at %s:%d that was initially bound at %s",
+ object->metaObject()->className(), qPrintable(property->name(object)),
+ qPrintable(stackFrame->source()), stackFrame->lineNumber(),
+ qPrintable(qmlBinding->expressionIdentifier()));
+ break;
+ }
+ case QQmlAbstractBinding::ValueTypeProxy:
+ case QQmlAbstractBinding::PropertyToPropertyBinding: {
+ qCInfo(lcBuiltinsBindingRemoval,
+ "Overwriting binding on %s::%s at %s:%d",
+ object->metaObject()->className(), qPrintable(property->name(object)),
+ qPrintable(stackFrame->source()), stackFrame->lineNumber());
+ break;
+ }
+ }
}
}
QQmlPropertyPrivate::removeBinding(object, QQmlPropertyIndex(property->coreIndex()));
@@ -580,7 +707,9 @@ void QObjectWrapper::setProperty(
const QMetaType propType = property->propType();
// functions are already handled, except for the QJSValue case
- Q_ASSERT(!value.as<FunctionObject>() || propType == QMetaType::fromType<QJSValue>());
+ Q_ASSERT(!value.as<FunctionObject>()
+ || value.as<QV4::QQmlTypeWrapper>()
+ || propType == QMetaType::fromType<QJSValue>());
if (value.isNull() && property->isQObject()) {
PROPERTY_STORE(QObject*, nullptr);
@@ -602,7 +731,7 @@ void QObjectWrapper::setProperty(
scope.engine->throwError(error);
return;
} else if (propType == QMetaType::fromType<int>() && value.isNumber()) {
- PROPERTY_STORE(int, value.asDouble());
+ PROPERTY_STORE(int, value.toInt32());
} else if (propType == QMetaType::fromType<qreal>() && value.isNumber()) {
PROPERTY_STORE(qreal, qreal(value.asDouble()));
} else if (propType == QMetaType::fromType<float>() && value.isNumber()) {
@@ -724,15 +853,6 @@ ReturnedValue QObjectWrapper::wrapConst_slowPath(ExecutionEngine *engine, QObjec
return constWrapper.asReturnedValue();
}
-Heap::QObjectMethod *QObjectWrapper::cloneMethod(
- ExecutionEngine *engine, Heap::QObjectMethod *cloneFrom,
- Heap::Object *wrapper, QObject *object)
-{
- Scope scope(engine);
- Scoped<QObjectMethod> method(scope, QObjectMethod::create(engine, cloneFrom, wrapper, object));
- return method ? method->d() : nullptr;
-}
-
void QObjectWrapper::markWrapper(QObject *object, MarkStack *markStack)
{
if (QQmlData::wasDeleted(object))
@@ -972,7 +1092,7 @@ ReturnedValue QObjectWrapper::virtualResolveLookupGetter(const Object *object, E
return Encode::undefined();
QQmlData *ddata = QQmlData::get(qobj, false);
- if (auto methodValue = getDestroyOrToStringMethod(engine, name, qobj)) {
+ if (auto methodValue = getDestroyOrToStringMethod(engine, name, This->d())) {
Scoped<QObjectMethod> method(scope, *methodValue);
setupQObjectMethodLookup(
lookup, ddata ? ddata : QQmlData::get(qobj, true), nullptr, This, method->d());
@@ -1004,7 +1124,8 @@ ReturnedValue QObjectWrapper::virtualResolveLookupGetter(const Object *object, E
&& !property->isVarProperty()
&& !property->isVMEFunction() // Handled by QObjectLookup
&& !property->isSignalHandler()) { // TODO: Optimize SignalHandler, too
- setupQObjectMethodLookup(lookup, ddata, property, This, nullptr);
+ QV4::Heap::QObjectMethod *method = nullptr;
+ setupQObjectMethodLookup(lookup, ddata, property, This, method);
lookup->getter = Lookup::getterQObjectMethod;
return lookup->getter(lookup, engine, *object);
}
@@ -1081,13 +1202,15 @@ struct QObjectSlotDispatcher : public QtPrivate::QSlotObjectBase
static void impl(int which, QSlotObjectBase *this_, QObject *receiver, void **metaArgs, bool *ret)
{
- Q_UNUSED(receiver);
switch (which) {
case Destroy: {
delete static_cast<QObjectSlotDispatcher*>(this_);
}
break;
case Call: {
+ if (QQmlData::wasDeleted(receiver))
+ break;
+
QObjectSlotDispatcher *This = static_cast<QObjectSlotDispatcher*>(this_);
ExecutionEngine *v4 = This->function.engine();
// Might be that we're still connected to a signal that's emitted long
@@ -1096,7 +1219,7 @@ struct QObjectSlotDispatcher : public QtPrivate::QSlotObjectBase
if (!v4)
break;
- QQmlMetaObject::ArgTypeStorage storage;
+ QQmlMetaObject::ArgTypeStorage<9> storage;
QQmlMetaObject::methodParameterTypes(This->signal, &storage, nullptr);
int argCount = storage.size();
@@ -1236,7 +1359,16 @@ ReturnedValue QObjectWrapper::method_connect(const FunctionObject *b, const Valu
}
QPair<QObject *, int> functionData = QObjectMethod::extractQtMethod(f); // align with disconnect
- if (QObject *receiver = functionData.first) {
+ QObject *receiver = nullptr;
+
+ if (functionData.first)
+ receiver = functionData.first;
+ else if (auto qobjectWrapper = object->as<QV4::QObjectWrapper>())
+ receiver = qobjectWrapper->object();
+ else if (auto typeWrapper = object->as<QV4::QQmlTypeWrapper>())
+ receiver = typeWrapper->object();
+
+ if (receiver) {
QObjectPrivate::connect(signalObject, signalIndex, receiver, slot, Qt::AutoConnection);
} else {
qCInfo(lcObjectConnect,
@@ -1294,7 +1426,16 @@ ReturnedValue QObjectWrapper::method_disconnect(const FunctionObject *b, const V
&functionData.second
};
- if (QObject *receiver = functionData.first) {
+ QObject *receiver = nullptr;
+
+ if (functionData.first)
+ receiver = functionData.first;
+ else if (auto qobjectWrapper = functionThisValue->as<QV4::QObjectWrapper>())
+ receiver = qobjectWrapper->object();
+ else if (auto typeWrapper = functionThisValue->as<QV4::QQmlTypeWrapper>())
+ receiver = typeWrapper->object();
+
+ if (receiver) {
QObjectPrivate::disconnect(signalObject, signalIndex, receiver,
reinterpret_cast<void **>(&a));
} else {
@@ -1324,9 +1465,37 @@ void Heap::QObjectWrapper::markObjects(Heap::Base *that, MarkStack *markStack)
QObjectWrapper *This = static_cast<QObjectWrapper *>(that);
if (QObject *o = This->object()) {
- QQmlVMEMetaObject *vme = QQmlVMEMetaObject::get(o);
- if (vme)
- vme->mark(markStack);
+ if (QQmlData *ddata = QQmlData::get(o)) {
+ if (ddata->hasVMEMetaObject) {
+ if (QQmlVMEMetaObject *vme
+ = static_cast<QQmlVMEMetaObject *>(QObjectPrivate::get(o)->metaObject)) {
+ vme->mark(markStack);
+ }
+ }
+
+ if (ddata->hasConstWrapper) {
+ Scope scope(that->internalClass->engine);
+ Q_ASSERT(scope.engine->m_multiplyWrappedQObjects);
+
+ Scoped<QV4::QObjectWrapper> constWrapper(
+ scope,
+ scope.engine->m_multiplyWrappedQObjects->value(
+ static_cast<const QObject *>(o)));
+
+ Q_ASSERT(constWrapper);
+
+ if (This == constWrapper->d()) {
+ // We've got the const wrapper. Also mark the non-const one
+ if (ddata->jsEngineId == scope.engine->m_engineId)
+ ddata->jsWrapper.markOnce(markStack);
+ else
+ scope.engine->m_multiplyWrappedQObjects->mark(o, markStack);
+ } else {
+ // We've got the non-const wrapper. Also mark the const one.
+ constWrapper->mark(markStack);
+ }
+ }
+ }
// Children usually don't need to be marked, the gc keeps them alive.
// But in the rare case of a "floating" QObject without a parent that
@@ -1350,13 +1519,22 @@ void QObjectWrapper::destroyObject(bool lastCall)
if (!o->parent() && !ddata->indestructible) {
if (ddata && ddata->ownContext) {
Q_ASSERT(ddata->ownContext.data() == ddata->context);
- ddata->ownContext->emitDestruction();
+ ddata->ownContext->deepClearContextObject(o);
ddata->ownContext.reset();
ddata->context = nullptr;
}
- // This object is notionally destroyed now
+
+ // This object is notionally destroyed now. It might still live until the next
+ // event loop iteration, but it won't need its connections, CU, or deferredData
+ // anymore.
+
ddata->isQueuedForDeletion = true;
- ddata->disconnectNotifiers();
+ ddata->disconnectNotifiers(QQmlData::DeleteNotifyList::No);
+ ddata->compilationUnit.reset();
+
+ qDeleteAll(ddata->deferredData);
+ ddata->deferredData.clear();
+
if (lastCall)
delete o;
else
@@ -1476,7 +1654,7 @@ static ReturnedValue CallMethod(const QQmlObjectOrGadget &object, int index, QMe
const bool is_signal =
object.metaObject()->method(index).methodType() == QMetaMethod::Signal;
if (is_signal) {
- qWarning() << "Passing incomatible arguments to signals is not supported.";
+ qWarning() << "Passing incompatible arguments to signals is not supported.";
} else {
return engine->throwTypeError(
QLatin1String("Passing incompatible arguments to C++ functions from "
@@ -1512,6 +1690,26 @@ static ReturnedValue CallMethod(const QQmlObjectOrGadget &object, int index, QMe
}
}
+template<typename Retrieve>
+int MatchVariant(QMetaType conversionMetaType, Retrieve &&retrieve) {
+ if (conversionMetaType == QMetaType::fromType<QVariant>())
+ return 0;
+
+ const QMetaType type = retrieve();
+ if (type == conversionMetaType)
+ return 0;
+
+ if (const QMetaObject *conversionMetaObject = conversionMetaType.metaObject()) {
+ if (const QMetaObject *mo = type.metaObject(); mo && mo->inherits(conversionMetaObject))
+ return 1;
+ }
+
+ if (QMetaType::canConvert(type, conversionMetaType))
+ return 5;
+
+ return 10;
+};
+
/*
Returns the match score for converting \a actual to be of type \a conversionType. A
zero score means "perfect match" whereas a higher score is worse.
@@ -1625,22 +1823,48 @@ static int MatchScore(const Value &actual, QMetaType conversionMetaType)
}
}
} else if (const Object *obj = actual.as<Object>()) {
- if (obj->as<VariantObject>()) {
- if (conversionType == qMetaTypeId<QVariant>())
- return 0;
- if (ExecutionEngine::toVariant(actual, QMetaType {}).metaType() == conversionMetaType)
- return 0;
- else
- return 10;
+ if (const VariantObject *variantObject = obj->as<VariantObject>()) {
+ return MatchVariant(conversionMetaType, [variantObject]() {
+ return variantObject->d()->data().metaType();
+ });
}
- if (obj->as<QObjectWrapper>()) {
+ if (const QObjectWrapper *wrapper = obj->as<QObjectWrapper>()) {
switch (conversionType) {
case QMetaType::QObjectStar:
return 0;
default:
- return 10;
+ if (conversionMetaType.flags() & QMetaType::PointerToQObject) {
+ QObject *wrapped = wrapper->object();
+ if (!wrapped)
+ return 0;
+ if (qmlobject_can_cpp_cast(wrapped, conversionMetaType.metaObject()))
+ return 0;
+ }
}
+ return 10;
+ }
+
+ if (const QQmlTypeWrapper *wrapper = obj->as<QQmlTypeWrapper>()) {
+ const QQmlType type = wrapper->d()->type();
+ if (type.isSingleton()) {
+ const QMetaType metaType = type.typeId();
+ if (metaType == conversionMetaType)
+ return 0;
+
+ if (conversionMetaType.flags() & QMetaType::PointerToQObject
+ && metaType.flags() & QMetaType::PointerToQObject
+ && type.metaObject()->inherits(conversionMetaType.metaObject())) {
+ return 0;
+ }
+ } else if (QObject *object = wrapper->object()) {
+ if (conversionMetaType.flags() & QMetaType::PointerToQObject
+ && qmlobject_can_cpp_cast(object, conversionMetaType.metaObject())) {
+ return 0;
+ }
+ }
+
+ return 10;
}
if (const Sequence *sequence = obj->as<Sequence>()) {
@@ -1650,13 +1874,12 @@ static int MatchScore(const Value &actual, QMetaType conversionMetaType)
return 10;
}
- if (obj->as<QQmlValueTypeWrapper>()) {
- const QVariant v = ExecutionEngine::toVariant(actual, QMetaType {});
- if (v.userType() == conversionType)
- return 0;
- else if (v.canConvert(conversionMetaType))
- return 5;
- return 10;
+ if (const QQmlValueTypeWrapper *wrapper = obj->as<QQmlValueTypeWrapper>()) {
+ return MatchVariant(conversionMetaType, [wrapper]() {
+ return wrapper->d()->isVariant()
+ ? wrapper->toVariant().metaType()
+ : wrapper->type();
+ });
}
if (conversionType == QMetaType::QJsonObject)
@@ -1680,9 +1903,17 @@ static int numDefinedArguments(CallData *callArgs)
return numDefinedArguments;
}
-static ReturnedValue CallPrecise(const QQmlObjectOrGadget &object, const QQmlPropertyData &data,
- ExecutionEngine *engine, CallData *callArgs,
- QMetaObject::Call callType = QMetaObject::InvokeMetaMethod)
+static bool requiresStrictArguments(const QQmlObjectOrGadget &object)
+{
+ const QMetaObject *metaObject = object.metaObject();
+ const int indexOfClassInfo = metaObject->indexOfClassInfo("QML.StrictArguments");
+ return indexOfClassInfo != -1
+ && metaObject->classInfo(indexOfClassInfo).value() == QByteArrayView("true");
+}
+
+ReturnedValue QObjectMethod::callPrecise(
+ const QQmlObjectOrGadget &object, const QQmlPropertyData &data, ExecutionEngine *engine,
+ CallData *callArgs, QMetaObject::Call callType)
{
QByteArray unknownTypeError;
@@ -1694,11 +1925,7 @@ static ReturnedValue CallPrecise(const QQmlObjectOrGadget &object, const QQmlPro
}
auto handleTooManyArguments = [&](int expectedArguments) {
- const QMetaObject *metaObject = object.metaObject();
- const int indexOfClassInfo = metaObject->indexOfClassInfo("QML.StrictArguments");
- if (indexOfClassInfo != -1
- && QString::fromUtf8(metaObject->classInfo(indexOfClassInfo).value())
- == QStringLiteral("true")) {
+ if (requiresStrictArguments(object)) {
engine->throwError(QStringLiteral("Too many arguments"));
return false;
}
@@ -1709,7 +1936,7 @@ static ReturnedValue CallPrecise(const QQmlObjectOrGadget &object, const QQmlPro
<< "When matching arguments for "
<< object.className() << "::" << data.name(object.metaObject()) << "():";
} else {
- const StackFrame frame = engine->stackTrace().first();
+ const StackFrame frame = stackTrace.first();
qWarning().noquote() << frame.function + QLatin1Char('@') + frame.source
+ (frame.line > 0 ? (QLatin1Char(':') + QString::number(frame.line))
: QString());
@@ -1724,7 +1951,7 @@ static ReturnedValue CallPrecise(const QQmlObjectOrGadget &object, const QQmlPro
if (data.hasArguments()) {
- QQmlMetaObject::ArgTypeStorage storage;
+ QQmlMetaObject::ArgTypeStorage<9> storage;
bool ok = false;
if (data.isConstructor())
@@ -1771,7 +1998,7 @@ Resolve the overloaded method to call. The algorithm works conceptually like th
If two or more overloads have the same match score, return the last one. The match
score is constructed by adding the matchScore() result for each of the parameters.
*/
-static const QQmlPropertyData *ResolveOverloaded(
+const QQmlPropertyData *QObjectMethod::resolveOverloaded(
const QQmlObjectOrGadget &object, const QQmlPropertyData *methods, int methodCount,
ExecutionEngine *engine, CallData *callArgs)
{
@@ -1789,12 +2016,12 @@ static const QQmlPropertyData *ResolveOverloaded(
for (int i = 0; i < methodCount; ++i) {
const QQmlPropertyData *attempt = methods + i;
- if (lcOverloadResolution().isInfoEnabled()) {
+ if (lcOverloadResolution().isDebugEnabled()) {
const QQmlPropertyData &candidate = methods[i];
const QMetaMethod m = candidate.isConstructor()
? object.metaObject()->constructor(candidate.coreIndex())
: object.metaObject()->method(candidate.coreIndex());
- qCInfo(lcOverloadResolution) << "::: considering signature" << m.methodSignature();
+ qCDebug(lcOverloadResolution) << "::: considering signature" << m.methodSignature();
}
// QQmlV4Function overrides anything that doesn't provide the exact number of arguments
@@ -1805,17 +2032,17 @@ static const QQmlPropertyData *ResolveOverloaded(
int sumMethodMatchScore = bestSumMatchScore;
if (!attempt->isV4Function()) {
- QQmlMetaObject::ArgTypeStorage storage;
+ QQmlMetaObject::ArgTypeStorage<9> storage;
int methodArgumentCount = 0;
if (attempt->hasArguments()) {
if (attempt->isConstructor()) {
if (!object.constructorParameterTypes(attempt->coreIndex(), &storage, nullptr)) {
- qCInfo(lcOverloadResolution, "rejected, could not get ctor argument types");
+ qCDebug(lcOverloadResolution, "rejected, could not get ctor argument types");
continue;
}
} else {
if (!object.methodParameterTypes(attempt->coreIndex(), &storage, nullptr)) {
- qCInfo(lcOverloadResolution, "rejected, could not get ctor argument types");
+ qCDebug(lcOverloadResolution, "rejected, could not get ctor argument types");
continue;
}
}
@@ -1823,7 +2050,7 @@ static const QQmlPropertyData *ResolveOverloaded(
}
if (methodArgumentCount > argumentCount) {
- qCInfo(lcOverloadResolution, "rejected, insufficient arguments");
+ qCDebug(lcOverloadResolution, "rejected, insufficient arguments");
continue; // We don't have sufficient arguments to call this method
}
@@ -1831,7 +2058,7 @@ static const QQmlPropertyData *ResolveOverloaded(
? 0
: (definedArgumentCount - methodArgumentCount + 1);
if (methodParameterScore > bestParameterScore) {
- qCInfo(lcOverloadResolution) << "rejected, score too bad. own" << methodParameterScore << "vs best:" << bestParameterScore;
+ qCDebug(lcOverloadResolution) << "rejected, score too bad. own" << methodParameterScore << "vs best:" << bestParameterScore;
continue; // We already have a better option
}
@@ -1853,21 +2080,21 @@ static const QQmlPropertyData *ResolveOverloaded(
bestParameterScore = methodParameterScore;
bestMaxMatchScore = maxMethodMatchScore;
bestSumMatchScore = sumMethodMatchScore;
- qCInfo(lcOverloadResolution) << "updated best" << "bestParameterScore" << bestParameterScore << "\n"
- << "bestMaxMatchScore" << bestMaxMatchScore << "\n"
- << "bestSumMatchScore" << bestSumMatchScore << "\n";
+ qCDebug(lcOverloadResolution) << "updated best" << "bestParameterScore" << bestParameterScore << "\n"
+ << "bestMaxMatchScore" << bestMaxMatchScore << "\n"
+ << "bestSumMatchScore" << bestSumMatchScore << "\n";
} else {
- qCInfo(lcOverloadResolution) << "did not update best\n"
- << "bestParameterScore" << bestParameterScore << "\t"
- << "methodParameterScore" << methodParameterScore << "\n"
- << "bestMaxMatchScore" << bestMaxMatchScore << "\t"
- << "maxMethodMatchScore" << maxMethodMatchScore << "\n"
- << "bestSumMatchScore" << bestSumMatchScore << "\t"
- << "sumMethodMatchScore" << sumMethodMatchScore << "\n";
+ qCDebug(lcOverloadResolution) << "did not update best\n"
+ << "bestParameterScore" << bestParameterScore << "\t"
+ << "methodParameterScore" << methodParameterScore << "\n"
+ << "bestMaxMatchScore" << bestMaxMatchScore << "\t"
+ << "maxMethodMatchScore" << maxMethodMatchScore << "\n"
+ << "bestSumMatchScore" << bestSumMatchScore << "\t"
+ << "sumMethodMatchScore" << sumMethodMatchScore << "\n";
}
if (bestParameterScore == 0 && bestMaxMatchScore == 0) {
- qCInfo(lcOverloadResolution, "perfect match");
+ qCDebug(lcOverloadResolution, "perfect match");
break; // We can't get better than that
}
@@ -1878,13 +2105,11 @@ static const QQmlPropertyData *ResolveOverloaded(
} else {
QString error = QLatin1String("Unable to determine callable overload. Candidates are:");
for (int i = 0; i < methodCount; ++i) {
- for (int i = 0; i < methodCount; ++i) {
- const QQmlPropertyData &candidate = methods[i];
- const QMetaMethod m = candidate.isConstructor()
- ? object.metaObject()->constructor(candidate.coreIndex())
- : object.metaObject()->method(candidate.coreIndex());
- error += u"\n " + QString::fromUtf8(m.methodSignature());
- }
+ const QQmlPropertyData &candidate = methods[i];
+ const QMetaMethod m = candidate.isConstructor()
+ ? object.metaObject()->constructor(candidate.coreIndex())
+ : object.metaObject()->method(candidate.coreIndex());
+ error += u"\n " + QString::fromUtf8(m.methodSignature());
}
engine->throwError(error);
@@ -1892,7 +2117,69 @@ static const QQmlPropertyData *ResolveOverloaded(
}
}
+static bool ExactMatch(QMetaType passed, QMetaType required, const void *data)
+{
+ if (required == QMetaType::fromType<QVariant>()
+ || required == QMetaType::fromType<QJSValue>()
+ || required == QMetaType::fromType<QJSManagedValue>()) {
+ return true;
+ }
+
+ if (data) {
+ if (passed == QMetaType::fromType<QVariant>())
+ passed = static_cast<const QVariant *>(data)->metaType();
+ else if (passed == QMetaType::fromType<QJSPrimitiveValue>())
+ passed = static_cast<const QJSPrimitiveValue *>(data)->metaType();
+ }
+
+ if (passed == required)
+ return true;
+
+ if (required == QMetaType::fromType<QJSPrimitiveValue>()) {
+ switch (passed.id()) {
+ case QMetaType::UnknownType:
+ case QMetaType::Nullptr:
+ case QMetaType::Bool:
+ case QMetaType::Int:
+ case QMetaType::Double:
+ case QMetaType::QString:
+ return true;
+ default:
+ break;
+ }
+ }
+ return false;
+}
+
+const QQmlPropertyData *QObjectMethod::resolveOverloaded(
+ const QQmlPropertyData *methods, int methodCount,
+ void **argv, int argc, const QMetaType *types)
+{
+ // We only accept exact matches here. Everything else goes through the JavaScript conversion.
+ for (int i = 0; i < methodCount; ++i) {
+ const QQmlPropertyData *attempt = methods + i;
+ if (types[0].isValid() && !ExactMatch(attempt->propType(), types[0], nullptr))
+ continue;
+
+ const QMetaMethod method = attempt->metaMethod();
+ if (method.parameterCount() != argc)
+ continue;
+
+ bool valid = true;
+ for (int i = 0; i < argc; ++i) {
+ if (!ExactMatch(types[i + 1], method.parameterMetaType(i), argv[i + 1])) {
+ valid = false;
+ break;
+ }
+ }
+
+ if (valid)
+ return attempt;
+ }
+
+ return nullptr;
+}
void CallArgument::cleanup()
{
@@ -2057,6 +2344,10 @@ bool CallArgument::fromValue(QMetaType metaType, ExecutionEngine *engine, const
else
qstringPtr = new (&allocData) QString(value.toQStringNoThrow());
return true;
+ case QMetaType::QByteArray:
+ qbyteArrayPtr = new (&allocData) QByteArray();
+ ExecutionEngine::metaTypeFromJS(value, metaType, qbyteArrayPtr);
+ return true;
case QMetaType::QObjectStar:
if (const QObjectWrapper *qobjectWrapper = value.as<QObjectWrapper>()) {
qobjectPtr = qobjectWrapper->object();
@@ -2087,8 +2378,8 @@ bool CallArgument::fromValue(QMetaType metaType, ExecutionEngine *engine, const
return true;
case QMetaType::QJsonArray: {
Scope scope(engine);
- ScopedArrayObject a(scope, value);
- jsonArrayPtr = new (&allocData) QJsonArray(JsonObject::toJsonArray(a));
+ ScopedObject o(scope, value);
+ jsonArrayPtr = new (&allocData) QJsonArray(JsonObject::toJsonArray(o));
return true;
}
case QMetaType::QJsonObject: {
@@ -2135,6 +2426,11 @@ bool CallArgument::fromValue(QMetaType metaType, ExecutionEngine *engine, const
return true;
}
+ if (const QmlListWrapper *listWrapper = value.as<QmlListWrapper>()) {
+ *qlistPtr = listWrapper->d()->property()->toList<QList<QObject *>>();
+ return true;
+ }
+
qlistPtr->append(nullptr);
return value.isNullOrUndefined();
}
@@ -2175,37 +2471,14 @@ bool CallArgument::fromValue(QMetaType metaType, ExecutionEngine *engine, const
}
// Convert via QVariant through the QML engine.
- qvariantPtr = new (&allocData) QVariant();
+ qvariantPtr = new (&allocData) QVariant(metaType);
type = QVariantWrappedType;
- QVariant v = ExecutionEngine::toVariant(value, metaType);
-
- if (v.metaType() == metaType) {
- *qvariantPtr = std::move(v);
- return true;
- }
-
- if (v.canConvert(metaType)) {
- *qvariantPtr = std::move(v);
- qvariantPtr->convert(metaType);
+ if (ExecutionEngine::metaTypeFromJS(value, metaType, qvariantPtr->data()))
return true;
- }
- const QQmlMetaObject mo = QQmlMetaType::rawMetaObjectForType(metaType);
- if (!mo.isNull() && v.metaType().flags().testFlag(QMetaType::PointerToQObject)) {
- QObject *obj = QQmlMetaType::toQObject(v);
-
- if (obj != nullptr && !QQmlMetaObject::canConvert(obj, mo)) {
- *qvariantPtr = QVariant(metaType, nullptr);
- return false;
- }
-
- *qvariantPtr = QVariant(metaType, &obj);
- return true;
- }
-
- *qvariantPtr = QVariant(metaType, (void *)nullptr);
- return false;
+ const QVariant v = ExecutionEngine::toVariant(value, metaType);
+ return QMetaType::convert(v.metaType(), v.constData(), metaType, qvariantPtr->data());
}
ReturnedValue CallArgument::toValue(ExecutionEngine *engine)
@@ -2273,34 +2546,34 @@ ReturnedValue CallArgument::toValue(ExecutionEngine *engine)
return Encode::undefined();
}
-ReturnedValue QObjectMethod::create(ExecutionContext *scope, QObject *object, int index)
+ReturnedValue QObjectMethod::create(ExecutionEngine *engine, Heap::Object *wrapper, int index)
{
- Scope valueScope(scope);
- Scoped<QObjectMethod> method(valueScope, valueScope.engine->memoryManager->allocate<QObjectMethod>(scope));
- method->d()->setObject(object);
-
- method->d()->index = index;
+ Scope valueScope(engine);
+ Scoped<QObjectMethod> method(
+ valueScope,
+ engine->memoryManager->allocate<QObjectMethod>(engine, wrapper, index));
return method.asReturnedValue();
}
-ReturnedValue QObjectMethod::create(ExecutionContext *scope, Heap::QQmlValueTypeWrapper *valueType, int index)
+ReturnedValue QObjectMethod::create(
+ ExecutionEngine *engine, Heap::QQmlValueTypeWrapper *valueType, int index)
{
- Scope valueScope(scope);
- Scoped<QObjectMethod> method(valueScope, valueScope.engine->memoryManager->allocate<QObjectMethod>(scope));
- method->d()->index = index;
- method->d()->valueTypeWrapper.set(valueScope.engine, valueType);
+ Scope valueScope(engine);
+ Scoped<QObjectMethod> method(
+ valueScope,
+ engine->memoryManager->allocate<QObjectMethod>(engine, valueType, index));
return method.asReturnedValue();
}
ReturnedValue QObjectMethod::create(
ExecutionEngine *engine, Heap::QObjectMethod *cloneFrom,
- Heap::Object *wrapper, QObject *object)
+ Heap::Object *wrapper, Heap::Object *object)
{
Scope valueScope(engine);
Scoped<QQmlValueTypeWrapper> valueTypeWrapper(valueScope);
- if (cloneFrom->valueTypeWrapper) {
- Scoped<QQmlValueTypeWrapper> ref(valueScope, cloneFrom->valueTypeWrapper);
+ if (cloneFrom->wrapper) {
+ Scoped<QQmlValueTypeWrapper> ref(valueScope, cloneFrom->wrapper);
if (ref) {
valueTypeWrapper = QQmlValueTypeWrapper::create(engine, ref->d(), wrapper);
} else {
@@ -2311,73 +2584,97 @@ ReturnedValue QObjectMethod::create(
}
}
- Scoped<ExecutionContext> context(valueScope, cloneFrom->scope.get());
Scoped<QObjectMethod> method(
- valueScope, engine->memoryManager->allocate<QV4::QObjectMethod>(context));
+ valueScope,
+ engine->memoryManager->allocate<QV4::QObjectMethod>(
+ engine, valueTypeWrapper ? valueTypeWrapper->d() : object, cloneFrom->index));
- method->d()->index = cloneFrom->index;
method->d()->methodCount = cloneFrom->methodCount;
- if (valueTypeWrapper)
- method->d()->valueTypeWrapper.set(engine, valueTypeWrapper->d());
- else
- method->d()->setObject(object);
-
- if (cloneFrom->methodCount == 1) {
+ Q_ASSERT(method->d()->methods == nullptr);
+ switch (cloneFrom->methodCount) {
+ case 0:
+ Q_ASSERT(cloneFrom->methods == nullptr);
+ break;
+ case 1:
+ Q_ASSERT(cloneFrom->methods
+ == reinterpret_cast<QQmlPropertyData *>(&cloneFrom->_singleMethod));
method->d()->methods = reinterpret_cast<QQmlPropertyData *>(&method->d()->_singleMethod);
*method->d()->methods = *cloneFrom->methods;
- } else {
+ break;
+ default:
+ Q_ASSERT(cloneFrom->methods != nullptr);
method->d()->methods = new QQmlPropertyData[cloneFrom->methodCount];
memcpy(method->d()->methods, cloneFrom->methods,
cloneFrom->methodCount * sizeof(QQmlPropertyData));
+ break;
}
+
return method.asReturnedValue();
}
-void Heap::QObjectMethod::init(QV4::ExecutionContext *scope)
+void Heap::QObjectMethod::init(QV4::ExecutionEngine *engine, Object *object, int methodIndex)
{
- Heap::FunctionObject::init(scope);
+ Heap::FunctionObject::init(engine);
+ wrapper.set(engine, object);
+ index = methodIndex;
}
const QMetaObject *Heap::QObjectMethod::metaObject() const
{
- if (valueTypeWrapper)
- return valueTypeWrapper->metaObject();
+ Scope scope(internalClass->engine);
+
+ if (Scoped<QV4::QQmlValueTypeWrapper> valueWrapper(scope, wrapper); valueWrapper)
+ return valueWrapper->metaObject();
if (QObject *self = object())
return self->metaObject();
+
+ return nullptr;
+}
+
+QObject *Heap::QObjectMethod::object() const
+{
+ Scope scope(internalClass->engine);
+
+ if (Scoped<QV4::QObjectWrapper> objectWrapper(scope, wrapper); objectWrapper)
+ return objectWrapper->object();
+ if (Scoped<QV4::QQmlTypeWrapper> typeWrapper(scope, wrapper); typeWrapper)
+ return typeWrapper->object();
return nullptr;
}
bool Heap::QObjectMethod::isDetached() const
{
- if (qObj.isValid())
- return false;
+ if (!wrapper)
+ return true;
- if (Heap::QQmlValueTypeWrapper *wrapper = valueTypeWrapper.get())
- return wrapper->object() == nullptr;
+ QV4::Scope scope(internalClass->engine);
+ if (Scoped<QV4::QQmlValueTypeWrapper> valueWrapper(scope, wrapper); valueWrapper)
+ return valueWrapper->d()->object() == nullptr;
- return true;
+ return false;
}
bool Heap::QObjectMethod::isAttachedTo(QObject *o) const
{
- if (qObj.isValid() && qObj != o)
- return false;
+ QV4::Scope scope(internalClass->engine);
+ if (Scoped<QV4::QObjectWrapper> objectWrapper(scope, wrapper); objectWrapper)
+ return objectWrapper->object() == o;
+ if (Scoped<QV4::QQmlTypeWrapper> typeWrapper(scope, wrapper); typeWrapper)
+ return typeWrapper->object() == o;
- if (Heap::QQmlValueTypeWrapper *wrapper = valueTypeWrapper.get()) {
+ if (Scoped<QV4::QQmlValueTypeWrapper> valueWrapper(scope, wrapper); valueWrapper) {
QV4::Scope scope(wrapper->internalClass->engine);
- QV4::Scoped<QV4::QObjectWrapper> qobject(scope, wrapper->object());
- if (qobject && qobject->object() == o)
- return true;
- QV4::Scoped<QV4::QQmlTypeWrapper> type(scope, wrapper->object());
- if (type && type->object() == o)
- return true;
+ if (QV4::Scoped<QV4::QObjectWrapper> qobject(scope, valueWrapper->d()->object()); qobject)
+ return qobject->object() == o;
+ if (QV4::Scoped<QV4::QQmlTypeWrapper> type(scope, valueWrapper->d()->object()); type)
+ return type->object() == o;
// Attached to some nested value type or sequence object
return false;
}
- return true;
+ return false;
}
Heap::QObjectMethod::ThisObjectMode Heap::QObjectMethod::checkThisObject(
@@ -2387,7 +2684,7 @@ Heap::QObjectMethod::ThisObjectMode Heap::QObjectMethod::checkThisObject(
if (!thisMeta) {
// You can only get a detached method via a lookup, and then you have a thisObject.
- Q_ASSERT(valueTypeWrapper || qObj);
+ Q_ASSERT(wrapper);
return Included;
}
@@ -2405,6 +2702,10 @@ Heap::QObjectMethod::ThisObjectMode Heap::QObjectMethod::checkThisObject(
return Included;
}
+ // destroy() and toString() can be called on all QObjects, but not on gadgets.
+ if (index < 0)
+ return thisMeta->inherits(&QObject::staticMetaObject) ? Explicit : Invalid;
+
// Find the base type the method belongs to.
int methodOffset = included->methodOffset();
while (true) {
@@ -2414,19 +2715,16 @@ Heap::QObjectMethod::ThisObjectMode Heap::QObjectMethod::checkThisObject(
if (methodOffset <= index)
return thisMeta->inherits(included) ? Explicit : Invalid;
- methodOffset -= QMetaObjectPrivate::get(included)->methodCount;
included = included->superClass();
Q_ASSERT(included);
+ methodOffset -= QMetaObjectPrivate::get(included)->methodCount;
};
Q_UNREACHABLE_RETURN(Invalid);
};
- if (QObject *o = object())
- return check(o->metaObject());
-
- if (valueTypeWrapper)
- return check(valueTypeWrapper->metaObject());
+ if (const QMetaObject *meta = metaObject())
+ return check(meta);
// If the QObjectMethod is detached, we can only have gotten here via a lookup.
// The lookup checks that the QQmlPropertyCache matches.
@@ -2456,8 +2754,10 @@ QString Heap::QObjectMethod::name() const
void Heap::QObjectMethod::ensureMethodsCache(const QMetaObject *thisMeta)
{
- if (methods)
+ if (methods) {
+ Q_ASSERT(methodCount > 0);
return;
+ }
const QMetaObject *mo = metaObject();
@@ -2475,6 +2775,7 @@ void Heap::QObjectMethod::ensureMethodsCache(const QMetaObject *thisMeta)
QQmlPropertyData dummy;
QMetaMethod method = mo->method(index);
dummy.load(method);
+ dummy.setMetaObject(mo);
resolvedMethods.append(dummy);
// Look for overloaded methods
QByteArray methodName = method.name();
@@ -2494,15 +2795,8 @@ void Heap::QObjectMethod::ensureMethodsCache(const QMetaObject *thisMeta)
*methods = resolvedMethods.at(0);
methodCount = 1;
}
-}
-static QObject *qObject(const Value *v)
-{
- if (const QObjectWrapper *o = v->as<QObjectWrapper>())
- return o->object();
- if (const QQmlTypeWrapper *t = v->as<QQmlTypeWrapper>())
- return t->object();
- return nullptr;
+ Q_ASSERT(methodCount > 0);
}
ReturnedValue QObjectMethod::method_toString(ExecutionEngine *engine, QObject *o) const
@@ -2533,32 +2827,44 @@ ReturnedValue QObjectMethod::method_destroy(
return Encode::undefined();
}
-ReturnedValue QObjectMethod::virtualCall(const FunctionObject *m, const Value *thisObject, const Value *argv, int argc)
+ReturnedValue QObjectMethod::virtualCall(
+ const FunctionObject *m, const Value *thisObject, const Value *argv, int argc)
{
const QObjectMethod *This = static_cast<const QObjectMethod*>(m);
return This->callInternal(thisObject, argv, argc);
}
+void QObjectMethod::virtualCallWithMetaTypes(
+ const FunctionObject *m, QObject *thisObject, void **argv, const QMetaType *types, int argc)
+{
+ const QObjectMethod *This = static_cast<const QObjectMethod*>(m);
+ This->callInternalWithMetaTypes(thisObject, argv, types, argc);
+}
+
ReturnedValue QObjectMethod::callInternal(const Value *thisObject, const Value *argv, int argc) const
{
ExecutionEngine *v4 = engine();
const QMetaObject *thisMeta = nullptr;
- QObject *o = qObject(thisObject);
- Heap::QQmlValueTypeWrapper *wrapper = nullptr;
- if (o) {
- thisMeta = o->metaObject();
- } else if (const QQmlValueTypeWrapper *value = thisObject->as<QQmlValueTypeWrapper>()) {
- wrapper = value->d();
- thisMeta = wrapper->metaObject();
+ QObject *o = nullptr;
+ Heap::QQmlValueTypeWrapper *valueWrapper = nullptr;
+ if (const QObjectWrapper *w = thisObject->as<QObjectWrapper>()) {
+ thisMeta = w->metaObject();
+ o = w->object();
+ } else if (const QQmlTypeWrapper *w = thisObject->as<QQmlTypeWrapper>()) {
+ thisMeta = w->metaObject();
+ o = w->object();
+ } else if (const QQmlValueTypeWrapper *w = thisObject->as<QQmlValueTypeWrapper>()) {
+ thisMeta = w->metaObject();
+ valueWrapper = w->d();
}
Heap::QObjectMethod::ThisObjectMode mode = Heap::QObjectMethod::Invalid;
if (o && o == d()->object()) {
mode = Heap::QObjectMethod::Explicit;
// Nothing to do; objects are the same. This should be common
- } else if (wrapper && wrapper == d()->valueTypeWrapper) {
+ } else if (valueWrapper && valueWrapper == d()->wrapper) {
mode = Heap::QObjectMethod::Explicit;
// Nothing to do; gadgets are the same. This should be somewhat common
} else {
@@ -2572,20 +2878,24 @@ ReturnedValue QObjectMethod::callInternal(const Value *thisObject, const Value *
QQmlObjectOrGadget object = [&](){
if (mode == Heap::QObjectMethod::Included) {
- if (QObject *qObject = d()->qObj)
- return QQmlObjectOrGadget(qObject);
-
- wrapper = d()->valueTypeWrapper;
- Q_ASSERT(wrapper);
- return QQmlObjectOrGadget(wrapper->metaObject(), wrapper->gadgetPtr());
+ QV4::Scope scope(v4);
+ if (QV4::Scoped<QV4::QObjectWrapper> qobject(scope, d()->wrapper); qobject)
+ return QQmlObjectOrGadget(qobject->object());
+ if (QV4::Scoped<QV4::QQmlTypeWrapper> type(scope, d()->wrapper); type)
+ return QQmlObjectOrGadget(type->object());
+ if (QV4::Scoped<QV4::QQmlValueTypeWrapper> value(scope, d()->wrapper); value) {
+ valueWrapper = value->d();
+ return QQmlObjectOrGadget(valueWrapper->metaObject(), valueWrapper->gadgetPtr());
+ }
+ Q_UNREACHABLE();
} else {
if (o)
return QQmlObjectOrGadget(o);
- Q_ASSERT(wrapper);
- if (!wrapper->enforcesLocation())
- QV4::ReferenceObject::readReference(wrapper);
- return QQmlObjectOrGadget(thisMeta, wrapper->gadgetPtr());
+ Q_ASSERT(valueWrapper);
+ if (!valueWrapper->enforcesLocation())
+ QV4::ReferenceObject::readReference(valueWrapper);
+ return QQmlObjectOrGadget(thisMeta, valueWrapper->gadgetPtr());
}
}();
@@ -2609,9 +2919,9 @@ ReturnedValue QObjectMethod::callInternal(const Value *thisObject, const Value *
// The method might change the value.
const auto doCall = [&](const auto &call) {
if (!method->isConstant()) {
- if (wrapper && wrapper->isReference()) {
+ if (valueWrapper && valueWrapper->isReference()) {
ScopedValue rv(scope, call());
- d()->valueTypeWrapper->writeBack();
+ valueWrapper->writeBack();
return rv->asReturnedValue();
}
}
@@ -2620,7 +2930,8 @@ ReturnedValue QObjectMethod::callInternal(const Value *thisObject, const Value *
};
if (d()->methodCount != 1) {
- method = ResolveOverloaded(object, d()->methods, d()->methodCount, v4, callData);
+ Q_ASSERT(d()->methodCount > 0);
+ method = resolveOverloaded(object, d()->methods, d()->methodCount, v4, callData);
if (method == nullptr)
return Encode::undefined();
}
@@ -2629,7 +2940,7 @@ ReturnedValue QObjectMethod::callInternal(const Value *thisObject, const Value *
return doCall([&]() {
ScopedValue rv(scope, Value::undefinedValue());
QQmlV4Function func(callData, rv, v4);
- QQmlV4Function *funcptr = &func;
+ QQmlV4FunctionPtr funcptr = &func;
void *args[] = { nullptr, &funcptr };
object.metacall(QMetaObject::InvokeMetaMethod, method->coreIndex(), args);
@@ -2638,116 +2949,127 @@ ReturnedValue QObjectMethod::callInternal(const Value *thisObject, const Value *
});
}
- return doCall([&]() { return CallPrecise(object, *method, v4, callData); });
+ return doCall([&]() { return callPrecise(object, *method, v4, callData); });
}
-DEFINE_OBJECT_VTABLE(QObjectMethod);
-
-
-void Heap::QMetaObjectWrapper::init(const QMetaObject *metaObject)
+struct ToStringMetaMethod
{
- FunctionObject::init();
- this->metaObject = metaObject;
- constructors = nullptr;
- constructorCount = 0;
-}
+ constexpr int parameterCount() const { return 0; }
+ constexpr QMetaType returnMetaType() const { return QMetaType::fromType<QString>(); }
+ constexpr QMetaType parameterMetaType(int) const { return QMetaType(); }
+};
-void Heap::QMetaObjectWrapper::destroy()
+void QObjectMethod::callInternalWithMetaTypes(
+ QObject *thisObject, void **argv, const QMetaType *types, int argc) const
{
- delete[] constructors;
-}
-
-void Heap::QMetaObjectWrapper::ensureConstructorsCache() {
+ ExecutionEngine *v4 = engine();
- const int count = metaObject->constructorCount();
- if (constructorCount != count) {
- delete[] constructors;
- constructorCount = count;
- if (count == 0) {
- constructors = nullptr;
- return;
- }
- constructors = new QQmlPropertyData[count];
+ const QMetaObject *thisMeta = nullptr;
+ Heap::QQmlValueTypeWrapper *valueWrapper = nullptr;
- for (int i = 0; i < count; ++i) {
- QMetaMethod method = metaObject->constructor(i);
- QQmlPropertyData &d = constructors[i];
- d.load(method);
- d.setCoreIndex(i);
- }
+ if (thisObject) {
+ thisMeta = thisObject->metaObject();
+ } else {
+ Q_ASSERT(Value::fromHeapObject(d()->wrapper).as<QQmlValueTypeWrapper>());
+ valueWrapper = d()->wrapper.cast<Heap::QQmlValueTypeWrapper>();
+ thisMeta = valueWrapper->metaObject();
}
-}
+ QQmlObjectOrGadget object = [&](){
+ if (thisObject)
+ return QQmlObjectOrGadget(thisObject);
-ReturnedValue QMetaObjectWrapper::create(ExecutionEngine *engine, const QMetaObject* metaObject) {
+ Scope scope(v4);
+ Scoped<QQmlValueTypeWrapper> wrapper(scope, d()->wrapper);
+ Q_ASSERT(wrapper);
- Scope scope(engine);
- Scoped<QMetaObjectWrapper> mo(scope, engine->memoryManager->allocate<QMetaObjectWrapper>(metaObject)->asReturnedValue());
- mo->init(engine);
- return mo->asReturnedValue();
-}
+ Heap::QQmlValueTypeWrapper *valueWrapper = wrapper->d();
+ if (!valueWrapper->enforcesLocation())
+ QV4::ReferenceObject::readReference(valueWrapper);
+ return QQmlObjectOrGadget(thisMeta, valueWrapper->gadgetPtr());
+ }();
-void QMetaObjectWrapper::init(ExecutionEngine *) {
- const QMetaObject & mo = *d()->metaObject;
+ if (object.isNull())
+ return;
- for (int i = 0; i < mo.enumeratorCount(); i++) {
- QMetaEnum Enum = mo.enumerator(i);
- for (int k = 0; k < Enum.keyCount(); k++) {
- const char* key = Enum.key(k);
- const int value = Enum.value(k);
- defineReadonlyProperty(QLatin1String(key), Value::fromInt32(value));
- }
+ if (d()->index == DestroyMethod) {
+ // method_destroy will use at most one argument
+ QV4::convertAndCall(
+ v4, thisObject, argv, types, std::min(argc, 1),
+ [this, v4, object](const Value *thisObject, const Value *argv, int argc) {
+ Q_UNUSED(thisObject);
+ return method_destroy(v4, object.qObject(), argv, argc);
+ });
+ return;
}
-}
-ReturnedValue QMetaObjectWrapper::virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *)
-{
- const QMetaObjectWrapper *This = static_cast<const QMetaObjectWrapper*>(f);
- return This->constructInternal(argv, argc);
-}
-
-ReturnedValue QMetaObjectWrapper::constructInternal(const Value *argv, int argc) const
-{
+ if (d()->index == ToStringMethod) {
+ const ToStringMetaMethod metaMethod;
+ QV4::coerceAndCall(
+ v4, &metaMethod, argv, types, argc,
+ [v4, thisMeta, object](void **argv, int) {
+ *static_cast<QString *>(argv[0])
+ = QObjectWrapper::objectToString(v4, thisMeta, object.qObject());
+ });
+ return;
+ }
- d()->ensureConstructorsCache();
+ d()->ensureMethodsCache(thisMeta);
- ExecutionEngine *v4 = engine();
- const QMetaObject* mo = d()->metaObject;
- if (d()->constructorCount == 0) {
- return v4->throwTypeError(QLatin1String(mo->className())
- + QLatin1String(" has no invokable constructor"));
+ const QQmlPropertyData *method = d()->methods;
+ if (d()->methodCount != 1) {
+ Q_ASSERT(d()->methodCount > 0);
+ method = resolveOverloaded(d()->methods, d()->methodCount, argv, argc, types);
}
- Scope scope(v4);
- Scoped<QObjectWrapper> object(scope);
- JSCallData cData(nullptr, argv, argc);
- CallData *callData = cData.callData(scope);
+ if (!method || method->isV4Function()) {
+ QV4::convertAndCall(
+ v4, thisObject, argv, types, argc,
+ [this](const Value *thisObject, const Value *argv, int argc) {
+ return callInternal(thisObject, argv, argc);
+ });
+ } else {
+ const QMetaMethod metaMethod = method->metaMethod();
+ QV4::coerceAndCall(
+ v4, &metaMethod, argv, types, argc,
+ [v4, object, valueWrapper, method](void **argv, int argc) {
+ Q_UNUSED(argc);
+
+ // If we call the method, we have to write back any value type references afterwards.
+ // The method might change the value.
+ object.metacall(QMetaObject::InvokeMetaMethod, method->coreIndex(), argv);
+ if (!method->isConstant()) {
+ if (valueWrapper && valueWrapper->isReference())
+ valueWrapper->writeBack();
+ }
- const QQmlObjectOrGadget objectOrGadget(mo);
+ // If the method returns a QObject* we need to track it on the JS heap
+ // (if it's destructible).
+ QObject *qobjectPtr = nullptr;
+ const QMetaType resultType = method->propType();
+ if (argv[0]) {
+ if (resultType.flags() & QMetaType::PointerToQObject) {
+ qobjectPtr = *static_cast<QObject **>(argv[0]);
+ } else if (resultType == QMetaType::fromType<QVariant>()) {
+ const QVariant *result = static_cast<const QVariant *>(argv[0]);
+ const QMetaType variantType = result->metaType();
+ if (variantType.flags() & QMetaType::PointerToQObject)
+ qobjectPtr = *static_cast<QObject *const *>(result->data());
+ }
+ }
- if (d()->constructorCount == 1) {
- object = CallPrecise(objectOrGadget, d()->constructors[0], v4, callData, QMetaObject::CreateInstance);
- } else if (const QQmlPropertyData *ctor = ResolveOverloaded(
- objectOrGadget, d()->constructors, d()->constructorCount, v4, callData)) {
- object = CallPrecise(objectOrGadget, *ctor, v4, callData, QMetaObject::CreateInstance);
+ if (qobjectPtr) {
+ QQmlData *ddata = QQmlData::get(qobjectPtr, true);
+ if (!ddata->explicitIndestructibleSet) {
+ ddata->indestructible = false;
+ QObjectWrapper::ensureWrapper(v4, qobjectPtr);
+ }
+ }
+ });
}
- Scoped<QMetaObjectWrapper> metaObject(scope, this);
- object->defineDefaultProperty(v4->id_constructor(), metaObject);
- object->setPrototypeOf(const_cast<QMetaObjectWrapper*>(this));
- return object.asReturnedValue();
-
}
-bool QMetaObjectWrapper::virtualIsEqualTo(Managed *a, Managed *b)
-{
- const QMetaObjectWrapper *aMetaObject = a->as<QMetaObjectWrapper>();
- Q_ASSERT(aMetaObject);
- const QMetaObjectWrapper *bMetaObject = b->as<QMetaObjectWrapper>();
- return bMetaObject && aMetaObject->metaObject() == bMetaObject->metaObject();
-}
-
-DEFINE_OBJECT_VTABLE(QMetaObjectWrapper);
-
+DEFINE_OBJECT_VTABLE(QObjectMethod);
void Heap::QmlSignalHandler::init(QObject *object, int signalIndex)
{
@@ -2758,6 +3080,26 @@ void Heap::QmlSignalHandler::init(QObject *object, int signalIndex)
DEFINE_OBJECT_VTABLE(QmlSignalHandler);
+ReturnedValue QmlSignalHandler::call(const Value *thisObject, const Value *argv, int argc) const
+{
+ const QString handlerName = QQmlSignalNames::signalNameToHandlerName(
+ object()->metaObject()->method(signalIndex()).name());
+ qCWarning(lcSignalHandler).noquote()
+ << QStringLiteral("Property '%1' of object %2 is a signal handler. You should "
+ "not call it directly. Make it a proper function and call "
+ "that or emit the signal.")
+ .arg(handlerName, thisObject->toQStringNoThrow());
+
+ Scope scope(engine());
+ Scoped<QObjectMethod> method(
+ scope, QObjectMethod::create(
+ scope.engine,
+ static_cast<Heap::QObjectWrapper *>(nullptr),
+ signalIndex()));
+
+ return method->call(thisObject, argv, argc);
+}
+
void QmlSignalHandler::initProto(ExecutionEngine *engine)
{
if (engine->signalHandlerPrototype()->d_unchecked())
diff --git a/src/qml/jsruntime/qv4qobjectwrapper_p.h b/src/qml/jsruntime/qv4qobjectwrapper_p.h
index 3f94cc7798..288585f844 100644
--- a/src/qml/jsruntime/qv4qobjectwrapper_p.h
+++ b/src/qml/jsruntime/qv4qobjectwrapper_p.h
@@ -15,16 +15,17 @@
// We mean it.
//
+#include <private/qbipointer_p.h>
+#include <private/qintrusivelist_p.h>
+#include <private/qqmldata_p.h>
+#include <private/qv4functionobject_p.h>
+#include <private/qv4lookup_p.h>
+#include <private/qv4value_p.h>
+
#include <QtCore/qglobal.h>
#include <QtCore/qmetatype.h>
#include <QtCore/qpair.h>
#include <QtCore/qhash.h>
-#include <private/qqmldata_p.h>
-#include <private/qintrusivelist_p.h>
-
-#include <private/qv4value_p.h>
-#include <private/qv4functionobject_p.h>
-#include <private/qv4lookup_p.h>
QT_BEGIN_NAMESPACE
@@ -32,6 +33,7 @@ class QObject;
class QQmlData;
class QQmlPropertyCache;
class QQmlPropertyData;
+class QQmlObjectOrGadget;
namespace QV4 {
struct QObjectSlotDispatcher;
@@ -60,23 +62,21 @@ private:
};
#define QObjectMethodMembers(class, Member) \
- Member(class, Pointer, QQmlValueTypeWrapper *, valueTypeWrapper)
+ Member(class, Pointer, Object *, wrapper) \
-DECLARE_HEAP_OBJECT(QObjectMethod, FunctionObject) {
+DECLARE_EXPORTED_HEAP_OBJECT(QObjectMethod, FunctionObject) {
DECLARE_MARKOBJECTS(QObjectMethod)
- QV4QPointer<QObject> qObj;
QQmlPropertyData *methods;
alignas(alignof(QQmlPropertyData)) std::byte _singleMethod[sizeof(QQmlPropertyData)];
int methodCount;
int index;
- void init(QV4::ExecutionContext *scope);
+ void init(QV4::ExecutionEngine *engine, Object *wrapper, int index);
void destroy()
{
if (methods != reinterpret_cast<const QQmlPropertyData *>(&_singleMethod))
delete[] methods;
- qObj.destroy();
FunctionObject::destroy();
}
@@ -84,8 +84,7 @@ DECLARE_HEAP_OBJECT(QObjectMethod, FunctionObject) {
QString name() const;
const QMetaObject *metaObject() const;
- QObject *object() const { return qObj.data(); }
- void setObject(QObject *o) { qObj = o; }
+ QObject *object() const;
bool isDetached() const;
bool isAttachedTo(QObject *o) const;
@@ -99,16 +98,6 @@ DECLARE_HEAP_OBJECT(QObjectMethod, FunctionObject) {
QV4::Heap::QObjectMethod::ThisObjectMode checkThisObject(const QMetaObject *thisMeta) const;
};
-struct QMetaObjectWrapper : FunctionObject {
- const QMetaObject* metaObject;
- QQmlPropertyData *constructors;
- int constructorCount;
-
- void init(const QMetaObject* metaObject);
- void destroy();
- void ensureConstructorsCache();
-};
-
struct QmlSignalHandler : Object {
void init(QObject *object, int signalIndex);
void destroy() {
@@ -143,6 +132,13 @@ struct Q_QML_EXPORT QObjectWrapper : public Object
static void initializeBindings(ExecutionEngine *engine);
+ const QMetaObject *metaObject() const
+ {
+ if (QObject *o = object())
+ return o->metaObject();
+ return nullptr;
+ }
+
QObject *object() const { return d()->object(); }
ReturnedValue getQmlProperty(
@@ -158,8 +154,11 @@ struct Q_QML_EXPORT QObjectWrapper : public Object
ExecutionEngine *engine, const QQmlRefPointer<QQmlContextData> &qmlContext,
QObject *object, String *name, Flags flags, const Value &value);
+ Q_NODISCARD_X("Use ensureWrapper if you don't need the return value")
static ReturnedValue wrap(ExecutionEngine *engine, QObject *object);
+ Q_NODISCARD_X("Throwing the const wrapper away can cause it to be garbage collected")
static ReturnedValue wrapConst(ExecutionEngine *engine, QObject *object);
+ static void ensureWrapper(ExecutionEngine *engine, QObject *object);
static void markWrapper(QObject *object, MarkStack *markStack);
using Object::get;
@@ -219,10 +218,6 @@ protected:
private:
Q_NEVER_INLINE static ReturnedValue wrap_slowPath(ExecutionEngine *engine, QObject *object);
Q_NEVER_INLINE static ReturnedValue wrapConst_slowPath(ExecutionEngine *engine, QObject *object);
-
- static Heap::QObjectMethod *cloneMethod(
- ExecutionEngine *engine, Heap::QObjectMethod *cloneFrom,
- Heap::Object *wrapper, QObject *object);
};
Q_DECLARE_OPERATORS_FOR_FLAGS(QObjectWrapper::Flags)
@@ -328,14 +323,8 @@ inline ReturnedValue QObjectWrapper::lookupMethodGetterImpl(
}
if (Heap::QObjectMethod *method = lookup->qobjectMethodLookup.method) {
- if (lookup->forCall && !method->isDetached()) {
- method = lookup->qobjectMethodLookup.method
- = cloneMethod(engine, method, nullptr, nullptr);
- } else if (!lookup->forCall && !method->isAttachedTo(qobj)) {
- method = lookup->qobjectMethodLookup.method
- = cloneMethod(engine, method, This, qobj);
- }
- return method ? method->asReturnedValue() : revertLookup();
+ if (method->isDetached())
+ return method->asReturnedValue();
}
if (!property) // was toString() or destroy()
@@ -346,7 +335,7 @@ inline ReturnedValue QObjectWrapper::lookupMethodGetterImpl(
if (!v->as<QObjectMethod>())
return revertLookup();
- lookup->qobjectMethodLookup.method = static_cast<Heap::QObjectMethod *>(v->heapObject());
+ lookup->qobjectMethodLookup.method.set(engine, static_cast<Heap::QObjectMethod *>(v->heapObject()));
return v->asReturnedValue();
}
@@ -359,13 +348,12 @@ struct Q_QML_EXPORT QObjectMethod : public QV4::FunctionObject
enum { DestroyMethod = -1, ToStringMethod = -2 };
+ static ReturnedValue create(ExecutionEngine *engine, Heap::Object *wrapper, int index);
static ReturnedValue create(
- QV4::ExecutionContext *scope, QObject *object, int index);
+ ExecutionEngine *engine, Heap::QQmlValueTypeWrapper *valueType, int index);
static ReturnedValue create(
- QV4::ExecutionContext *scope, Heap::QQmlValueTypeWrapper *valueType, int index);
- static ReturnedValue create(
- QV4::ExecutionEngine *engine, Heap::QObjectMethod *cloneFrom,
- Heap::Object *wrapper, QObject *object);
+ ExecutionEngine *engine, Heap::QObjectMethod *cloneFrom,
+ Heap::Object *wrapper, Heap::Object *object);
int methodIndex() const { return d()->index; }
QObject *object() const { return d()->object(); }
@@ -373,30 +361,38 @@ struct Q_QML_EXPORT QObjectMethod : public QV4::FunctionObject
QV4::ReturnedValue method_toString(QV4::ExecutionEngine *engine, QObject *o) const;
QV4::ReturnedValue method_destroy(
QV4::ExecutionEngine *ctx, QObject *o, const Value *args, int argc) const;
+ void method_destroy(
+ QV4::ExecutionEngine *engine, QObject *o,
+ void **argv, const QMetaType *types, int argc) const;
- static ReturnedValue virtualCall(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
+ static ReturnedValue virtualCall(
+ const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
+ static void virtualCallWithMetaTypes(
+ const FunctionObject *m, QObject *thisObject,
+ void **argv, const QMetaType *types, int argc);
- ReturnedValue callInternal(const Value *thisObject, const Value *argv, int argc) const;
+ ReturnedValue callInternal(
+ const Value *thisObject, const Value *argv, int argc) const;
+ void callInternalWithMetaTypes(
+ QObject *thisObject, void **argv, const QMetaType *types, int argc) const;
static QPair<QObject *, int> extractQtMethod(const QV4::FunctionObject *function);
-};
-
-struct Q_QML_EXPORT QMetaObjectWrapper : public QV4::FunctionObject
-{
- V4_OBJECT2(QMetaObjectWrapper, QV4::FunctionObject)
- V4_NEEDS_DESTROY
+private:
+ friend struct QMetaObjectWrapper;
- static ReturnedValue create(ExecutionEngine *engine, const QMetaObject* metaObject);
- const QMetaObject *metaObject() const { return d()->metaObject; }
+ static const QQmlPropertyData *resolveOverloaded(
+ const QQmlObjectOrGadget &object, const QQmlPropertyData *methods, int methodCount,
+ ExecutionEngine *engine, CallData *callArgs);
-protected:
- static ReturnedValue virtualCallAsConstructor(const FunctionObject *, const Value *argv, int argc, const Value *);
- static bool virtualIsEqualTo(Managed *a, Managed *b);
+ static const QQmlPropertyData *resolveOverloaded(
+ const QQmlPropertyData *methods, int methodCount,
+ void **argv, int argc, const QMetaType *types);
-private:
- void init(ExecutionEngine *engine);
- ReturnedValue constructInternal(const Value *argv, int argc) const;
+ static ReturnedValue callPrecise(
+ const QQmlObjectOrGadget &object, const QQmlPropertyData &data,
+ ExecutionEngine *engine, CallData *callArgs,
+ QMetaObject::Call callType = QMetaObject::InvokeMetaMethod);
};
struct Q_QML_EXPORT QmlSignalHandler : public QV4::Object
@@ -408,6 +404,8 @@ struct Q_QML_EXPORT QmlSignalHandler : public QV4::Object
int signalIndex() const { return d()->signalIndex; }
QObject *object() const { return d()->object(); }
+ ReturnedValue call(const Value *thisObject, const Value *argv, int argc) const;
+
static void initProto(ExecutionEngine *v4);
};
diff --git a/src/qml/jsruntime/qv4referenceobject_p.h b/src/qml/jsruntime/qv4referenceobject_p.h
index 5378a0c362..094749ce6e 100644
--- a/src/qml/jsruntime/qv4referenceobject_p.h
+++ b/src/qml/jsruntime/qv4referenceobject_p.h
@@ -16,6 +16,7 @@
//
#include <private/qv4object_p.h>
+#include <private/qv4stackframe_p.h>
QT_BEGIN_NAMESPACE
diff --git a/src/qml/jsruntime/qv4regexp.cpp b/src/qml/jsruntime/qv4regexp.cpp
index be7ff77603..9c48199157 100644
--- a/src/qml/jsruntime/qv4regexp.cpp
+++ b/src/qml/jsruntime/qv4regexp.cpp
@@ -9,19 +9,23 @@
using namespace QV4;
+#if ENABLE(YARR_JIT)
+static constexpr quint8 RegexpJitThreshold = 5;
+#endif
+
static JSC::RegExpFlags jscFlags(uint flags)
{
JSC::RegExpFlags jscFlags = JSC::NoFlags;
if (flags & CompiledData::RegExp::RegExp_Global)
- jscFlags = static_cast<JSC::RegExpFlags>(flags | JSC::FlagGlobal);
+ jscFlags = static_cast<JSC::RegExpFlags>(jscFlags | JSC::FlagGlobal);
if (flags & CompiledData::RegExp::RegExp_IgnoreCase)
- jscFlags = static_cast<JSC::RegExpFlags>(flags | JSC::FlagIgnoreCase);
+ jscFlags = static_cast<JSC::RegExpFlags>(jscFlags | JSC::FlagIgnoreCase);
if (flags & CompiledData::RegExp::RegExp_Multiline)
- jscFlags = static_cast<JSC::RegExpFlags>(flags | JSC::FlagMultiline);
+ jscFlags = static_cast<JSC::RegExpFlags>(jscFlags | JSC::FlagMultiline);
if (flags & CompiledData::RegExp::RegExp_Unicode)
- jscFlags = static_cast<JSC::RegExpFlags>(flags | JSC::FlagUnicode);
+ jscFlags = static_cast<JSC::RegExpFlags>(jscFlags | JSC::FlagUnicode);
if (flags & CompiledData::RegExp::RegExp_Sticky)
- jscFlags = static_cast<JSC::RegExpFlags>(flags | JSC::FlagSticky);
+ jscFlags = static_cast<JSC::RegExpFlags>(jscFlags | JSC::FlagSticky);
return jscFlags;
}
@@ -40,12 +44,58 @@ uint RegExp::match(const QString &string, int start, uint *matchOffsets)
if (!isValid())
return JSC::Yarr::offsetNoMatch;
+#if ENABLE(YARR_JIT)
+ auto *priv = d();
+
+ auto regenerateByteCode = [](Heap::RegExp *regexp) {
+ JSC::Yarr::ErrorCode error = JSC::Yarr::ErrorCode::NoError;
+ JSC::Yarr::YarrPattern yarrPattern(WTF::String(*regexp->pattern), jscFlags(regexp->flags),
+ error);
+
+ // As we successfully parsed the pattern before, we should still be able to.
+ Q_ASSERT(error == JSC::Yarr::ErrorCode::NoError);
+
+ regexp->byteCode = JSC::Yarr::byteCompile(
+ yarrPattern,
+ regexp->internalClass->engine->bumperPointerAllocator).release();
+ };
+
+ auto removeJitCode = [](Heap::RegExp *regexp) {
+ delete regexp->jitCode;
+ regexp->jitCode = nullptr;
+ regexp->jitFailed = true;
+ };
+
+ auto removeByteCode = [](Heap::RegExp *regexp) {
+ delete regexp->byteCode;
+ regexp->byteCode = nullptr;
+ };
+
+ if (!priv->jitCode && !priv->jitFailed && priv->internalClass->engine->canJIT()
+ && (string.length() > 1024 || priv->matchCount++ == RegexpJitThreshold)) {
+ removeByteCode(priv);
+
+ JSC::Yarr::ErrorCode error = JSC::Yarr::ErrorCode::NoError;
+ JSC::Yarr::YarrPattern yarrPattern(
+ WTF::String(*priv->pattern), jscFlags(priv->flags), error);
+ if (!yarrPattern.m_containsBackreferences) {
+ priv->jitCode = new JSC::Yarr::YarrCodeBlock;
+ JSC::VM *vm = static_cast<JSC::VM *>(priv->internalClass->engine);
+ JSC::Yarr::jitCompile(yarrPattern, JSC::Yarr::Char16, vm, *priv->jitCode);
+ }
+
+ if (!priv->hasValidJITCode()) {
+ removeJitCode(priv);
+ regenerateByteCode(priv);
+ }
+ }
+#endif
+
WTF::String s(string);
#if ENABLE(YARR_JIT)
- static const uint offsetJITFail = std::numeric_limits<unsigned>::max() - 1;
- auto *priv = d();
if (priv->hasValidJITCode()) {
+ static const uint offsetJITFail = std::numeric_limits<unsigned>::max() - 1;
uint ret = JSC::Yarr::offsetNoMatch;
#if ENABLE(YARR_JIT_ALL_PARENS_EXPRESSIONS)
char buffer[8192];
@@ -58,19 +108,10 @@ uint RegExp::match(const QString &string, int start, uint *matchOffsets)
if (ret != offsetJITFail)
return ret;
+ removeJitCode(priv);
// JIT failed. We need byteCode to run the interpreter.
- if (!priv->byteCode) {
- JSC::Yarr::ErrorCode error = JSC::Yarr::ErrorCode::NoError;
- JSC::Yarr::YarrPattern yarrPattern(WTF::String(*priv->pattern), jscFlags(priv->flags),
- error);
-
- // As we successfully parsed the pattern before, we should still be able to.
- Q_ASSERT(error == JSC::Yarr::ErrorCode::NoError);
-
- priv->byteCode = JSC::Yarr::byteCompile(
- yarrPattern,
- priv->internalClass->engine->bumperPointerAllocator).release();
- }
+ Q_ASSERT(!priv->byteCode);
+ regenerateByteCode(priv);
}
#endif // ENABLE(YARR_JIT)
@@ -175,25 +216,15 @@ void Heap::RegExp::init(ExecutionEngine *engine, const QString &pattern, uint fl
this->flags = flags;
valid = false;
+ jitFailed = false;
+ matchCount = 0;
JSC::Yarr::ErrorCode error = JSC::Yarr::ErrorCode::NoError;
JSC::Yarr::YarrPattern yarrPattern(WTF::String(pattern), jscFlags(flags), error);
if (error != JSC::Yarr::ErrorCode::NoError)
return;
subPatternCount = yarrPattern.m_numSubpatterns;
-#if ENABLE(YARR_JIT)
- if (!yarrPattern.m_containsBackreferences && engine->canJIT()) {
- jitCode = new JSC::Yarr::YarrCodeBlock;
- JSC::VM *vm = static_cast<JSC::VM *>(engine);
- JSC::Yarr::jitCompile(yarrPattern, JSC::Yarr::Char16, vm, *jitCode);
- }
-#else
Q_UNUSED(engine);
-#endif
- if (hasValidJITCode()) {
- valid = true;
- return;
- }
byteCode = JSC::Yarr::byteCompile(yarrPattern, internalClass->engine->bumperPointerAllocator).release();
if (byteCode)
valid = true;
diff --git a/src/qml/jsruntime/qv4regexp_p.h b/src/qml/jsruntime/qv4regexp_p.h
index 1b70354caa..8a178dd2f6 100644
--- a/src/qml/jsruntime/qv4regexp_p.h
+++ b/src/qml/jsruntime/qv4regexp_p.h
@@ -66,6 +66,8 @@ struct RegExp : Base {
int subPatternCount;
uint flags;
bool valid;
+ bool jitFailed;
+ quint8 matchCount;
QString flagsAsString() const;
int captureCount() const { return subPatternCount + 1; }
diff --git a/src/qml/jsruntime/qv4regexpobject.cpp b/src/qml/jsruntime/qv4regexpobject.cpp
index acb9f0acfc..144cd39bcd 100644
--- a/src/qml/jsruntime/qv4regexpobject.cpp
+++ b/src/qml/jsruntime/qv4regexpobject.cpp
@@ -195,9 +195,9 @@ ReturnedValue RegExpObject::builtinExec(ExecutionEngine *engine, const String *s
DEFINE_OBJECT_VTABLE(RegExpCtor);
-void Heap::RegExpCtor::init(QV4::ExecutionContext *scope)
+void Heap::RegExpCtor::init(QV4::ExecutionEngine *engine)
{
- Heap::FunctionObject::init(scope, QStringLiteral("RegExp"));
+ Heap::FunctionObject::init(engine, QStringLiteral("RegExp"));
clearLastMatch();
}
diff --git a/src/qml/jsruntime/qv4regexpobject_p.h b/src/qml/jsruntime/qv4regexpobject_p.h
index 10702c48bd..179a01fb45 100644
--- a/src/qml/jsruntime/qv4regexpobject_p.h
+++ b/src/qml/jsruntime/qv4regexpobject_p.h
@@ -14,17 +14,13 @@
// We mean it.
//
-#include "qv4engine_p.h"
-#include "qv4context_p.h"
-#include "qv4functionobject_p.h"
-#include "qv4managed_p.h"
-#include "qv4regexp_p.h"
-
-#include <QtCore/QString>
-#include <QtCore/QHash>
-#include <QtCore/QScopedPointer>
-#include <cstdio>
-#include <cassert>
+#include <private/qv4context_p.h>
+#include <private/qv4engine_p.h>
+#include <private/qv4functionobject_p.h>
+#include <private/qv4managed_p.h>
+
+#include <QtCore/qhash.h>
+#include <QtCore/qstring.h>
QT_BEGIN_NAMESPACE
@@ -54,13 +50,13 @@ DECLARE_HEAP_OBJECT(RegExpObject, Object) {
DECLARE_HEAP_OBJECT(RegExpCtor, FunctionObject) {
DECLARE_MARKOBJECTS(RegExpCtor)
- void init(QV4::ExecutionContext *scope);
+ void init(ExecutionEngine *engine);
void clearLastMatch();
};
}
-struct Q_QML_PRIVATE_EXPORT RegExpObject: Object {
+struct Q_QML_EXPORT RegExpObject: Object {
V4_OBJECT2(RegExpObject, Object)
Q_MANAGED_TYPE(RegExpObject)
V4_INTERNALCLASS(RegExpObject)
@@ -110,11 +106,19 @@ struct Q_QML_PRIVATE_EXPORT RegExpObject: Object {
return s->toQString();
}
- Heap::RegExp *value() const { return d()->value; }
- uint flags() const { return d()->value->flags; }
- bool global() const { return d()->value->global(); }
- bool sticky() const { return d()->value->sticky(); }
- bool unicode() const { return d()->value->unicode(); }
+ // We cannot name Heap::RegExp here since we don't want to include qv4regexp_p.h but we still
+ // want to keep the methods inline. We shift the requirement to name the type to the caller by
+ // making it a template.
+ template<typename RegExp = Heap::RegExp>
+ RegExp *value() const { return d()->value; }
+ template<typename RegExp = Heap::RegExp>
+ uint flags() const { return value<RegExp>()->flags; }
+ template<typename RegExp = Heap::RegExp>
+ bool global() const { return value<RegExp>()->global(); }
+ template<typename RegExp = Heap::RegExp>
+ bool sticky() const { return value<RegExp>()->sticky(); }
+ template<typename RegExp = Heap::RegExp>
+ bool unicode() const { return value<RegExp>()->unicode(); }
ReturnedValue builtinExec(ExecutionEngine *engine, const String *s);
};
diff --git a/src/qml/jsruntime/qv4resolvedtypereference.cpp b/src/qml/jsruntime/qv4resolvedtypereference.cpp
index 0bf04c941a..7dcf2cd0b8 100644
--- a/src/qml/jsruntime/qv4resolvedtypereference.cpp
+++ b/src/qml/jsruntime/qv4resolvedtypereference.cpp
@@ -55,8 +55,8 @@ QQmlPropertyCache::ConstPtr ResolvedTypeReference::createPropertyCache()
const QMetaObject *metaObject = m_type.metaObject();
if (!metaObject) // value type of non-Q_GADGET base with extension
metaObject = m_type.extensionMetaObject();
- Q_ASSERT(metaObject);
- m_typePropertyCache = QQmlMetaType::propertyCache(metaObject, m_version);
+ if (metaObject)
+ m_typePropertyCache = QQmlMetaType::propertyCache(metaObject, m_version);
return m_typePropertyCache;
} else {
Q_ASSERT(m_compilationUnit);
@@ -67,15 +67,29 @@ QQmlPropertyCache::ConstPtr ResolvedTypeReference::createPropertyCache()
bool ResolvedTypeReference::addToHash(
QCryptographicHash *hash, QHash<quintptr, QByteArray> *checksums)
{
- if (m_type.isValid() && !m_type.isInlineComponentType()) {
+ if (m_type.isInlineComponentType()) {
+
+ // A reference to an inline component in the same file will have
+ // - no compilation unit since we cannot resolve the compilation unit before it's built.
+ // - a property cache since we've assigned one in buildMetaObjectsIncrementally().
+ // - a QQmlType that says it's an inline component.
+ // We don't have to add such a thing to the hash since if it changes, the QML document
+ // itself changes, leading to a new timestamp, which is checked before the checksum.
+ if (!m_compilationUnit)
+ return !m_typePropertyCache.isNull();
+
+ } else if (m_type.isValid()) {
bool ok = false;
- hash->addData(createPropertyCache()->checksum(checksums, &ok));
+ if (QQmlPropertyCache::ConstPtr propertyCache = createPropertyCache())
+ hash->addData(propertyCache->checksum(checksums, &ok));
+ else
+ Q_ASSERT(m_type.module() == QLatin1String("QML")); // a builtin without metaobject
return ok;
}
if (!m_compilationUnit)
return false;
- hash->addData({m_compilationUnit->data->md5Checksum,
- sizeof(m_compilationUnit->data->md5Checksum)});
+ hash->addData({m_compilationUnit->unitData()->md5Checksum,
+ sizeof(m_compilationUnit->unitData()->md5Checksum)});
return true;
}
diff --git a/src/qml/jsruntime/qv4resolvedtypereference_p.h b/src/qml/jsruntime/qv4resolvedtypereference_p.h
index 9720c90a12..4aaafdd71c 100644
--- a/src/qml/jsruntime/qv4resolvedtypereference_p.h
+++ b/src/qml/jsruntime/qv4resolvedtypereference_p.h
@@ -19,7 +19,7 @@
#include <QtQml/private/qqmlrefcount_p.h>
#include <QtQml/private/qqmlpropertycache_p.h>
#include <QtQml/private/qqmltype_p.h>
-#include <QtQml/private/qv4executablecompilationunit_p.h>
+#include <QtQml/private/qv4compileddata_p.h>
QT_BEGIN_NAMESPACE
@@ -46,8 +46,12 @@ public:
QQmlType type() const { return m_type; }
void setType(QQmlType type) { m_type = std::move(type); }
- QQmlRefPointer<QV4::ExecutableCompilationUnit> compilationUnit() { return m_compilationUnit; }
- void setCompilationUnit(QQmlRefPointer<QV4::ExecutableCompilationUnit> unit)
+ QQmlRefPointer<QV4::CompiledData::CompilationUnit> compilationUnit()
+ {
+ return m_compilationUnit;
+ }
+
+ void setCompilationUnit(QQmlRefPointer<QV4::CompiledData::CompilationUnit> unit)
{
if (m_compilationUnit == unit.data())
return;
@@ -93,7 +97,7 @@ public:
private:
QQmlType m_type;
QQmlPropertyCache::ConstPtr m_typePropertyCache;
- QV4::ExecutableCompilationUnit *m_compilationUnit = nullptr;
+ QV4::CompiledData::CompilationUnit *m_compilationUnit = nullptr;
QTypeRevision m_version = QTypeRevision::zero();
// Types such as QQmlPropertyMap can add properties dynamically at run-time and
diff --git a/src/qml/jsruntime/qv4runtime.cpp b/src/qml/jsruntime/qv4runtime.cpp
index aa4d5c875a..5977360080 100644
--- a/src/qml/jsruntime/qv4runtime.cpp
+++ b/src/qml/jsruntime/qv4runtime.cpp
@@ -1,44 +1,48 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
-#include "qv4global_p.h"
#include "qv4runtime_p.h"
-#include "qv4engine_p.h"
-#include "qv4object_p.h"
-#include "qv4globalobject_p.h"
-#include "qv4argumentsobject_p.h"
-#include "qv4lookup_p.h"
-#include "qv4function_p.h"
-#include "qv4regexp_p.h"
-#include "qv4regexpobject_p.h"
-#include "private/qlocale_tools_p.h"
-#include "qv4scopedvalue_p.h"
-#include "qv4jscall_p.h"
-#include <private/qv4qmlcontext_p.h>
-#include <private/qqmltypewrapper_p.h>
+
#include <private/qqmlengine_p.h>
#include <private/qqmljavascriptexpression_p.h>
#include <private/qqmljsast_p.h>
+#include <private/qqmltypewrapper_p.h>
#include <private/qqmlvaluetypewrapper_p.h>
-#include "qv4qobjectwrapper_p.h"
-#include "qv4symbol_p.h"
-#include "qv4generatorobject_p.h"
-#include <QtQml/private/qv4math_p.h>
-
-#include <QtCore/QDebug>
-#include <cassert>
-#include <cstdio>
-#include <stdlib.h>
+#include <private/qv4argumentsobject_p.h>
+#include <private/qv4engine_p.h>
+#include <private/qv4function_p.h>
+#include <private/qv4generatorobject_p.h>
+#include <private/qv4global_p.h>
+#include <private/qv4globalobject_p.h>
+#include <private/qv4jscall_p.h>
+#include <private/qv4lookup_p.h>
+#include <private/qv4math_p.h>
+#include <private/qv4object_p.h>
+#include <private/qv4qmlcontext_p.h>
+#include <private/qv4qobjectwrapper_p.h>
+#include <private/qv4regexp_p.h>
+#include <private/qv4regexpobject_p.h>
+#include <private/qv4scopedvalue_p.h>
+#include <private/qv4stackframe_p.h>
+#include <private/qv4symbol_p.h>
#include <wtf/MathExtras.h>
+#include <QtCore/private/qlocale_tools_p.h>
+#include <QtCore/qdebug.h>
+
#ifdef QV4_COUNT_RUNTIME_FUNCTIONS
-# include <QtCore/QBuffer>
-# include <QtCore/QDebug>
+# include <QtCore/qbuffer.h>
#endif // QV4_COUNT_RUNTIME_FUNCTIONS
+#include <cassert>
+#include <cstdio>
+#include <stdlib.h>
+
QT_BEGIN_NAMESPACE
+Q_LOGGING_CATEGORY(lcCoercingTypeAssertion, "qt.qml.coercingTypeAssertion");
+
namespace QV4 {
#ifdef QV4_COUNT_RUNTIME_FUNCTIONS
@@ -329,7 +333,7 @@ ReturnedValue Runtime::DeleteName::call(ExecutionEngine *engine, Function *funct
}
}
-QV4::ReturnedValue Runtime::Instanceof::call(ExecutionEngine *engine, const Value &lval, const Value &rval)
+static QV4::ReturnedValue doInstanceof(ExecutionEngine *engine, const Value &lval, const Value &rval)
{
// 11.8.6, 5: rval must be an Object
const Object *rhs = rval.as<Object>();
@@ -345,26 +349,75 @@ QV4::ReturnedValue Runtime::Instanceof::call(ExecutionEngine *engine, const Valu
Scope scope(engine);
ScopedValue hasInstance(scope, rhs->get(engine->symbol_hasInstance()));
if (hasInstance->isUndefined())
- return rhs->instanceOf(lval);
+ return Encode(rhs->instanceOf(lval));
+
FunctionObject *fHasInstance = hasInstance->as<FunctionObject>();
if (!fHasInstance)
return engine->throwTypeError();
- ScopedValue result(scope, fHasInstance->call(&rval, &lval, 1));
+ return Encode(fHasInstance->call(&rval, &lval, 1));
+}
+
+QV4::ReturnedValue Runtime::Instanceof::call(ExecutionEngine *engine, const Value &lval, const Value &rval)
+{
+ Scope scope(engine);
+ ScopedValue result(scope, doInstanceof(engine, lval, rval));
return scope.hasException() ? Encode::undefined() : Encode(result->toBoolean());
}
QV4::ReturnedValue Runtime::As::call(ExecutionEngine *engine, const Value &lval, const Value &rval)
{
Scope scope(engine);
- ScopedValue result(scope, Runtime::Instanceof::call(engine, lval, rval));
+ ScopedValue result(scope, doInstanceof(engine, lval, rval));
- if (scope.hasException())
+ if (scope.hasException()) {
+ // "foo instanceof valueType" must not throw an exception.
+ // So this can only be an object type.
engine->catchException();
- else if (result->toBoolean())
+ return Encode::null();
+ }
+
+ if (result->toBoolean())
return lval.asReturnedValue();
+ else if (result->isBoolean())
+ return Encode::null();
+
+ if (engine->callingQmlContext()->valueTypesAreAssertable())
+ return Encode::undefined();
- return Encode::null();
+ // Try to convert the value type
+ Scoped<QQmlTypeWrapper> typeWrapper(scope, rval);
+ if (!typeWrapper)
+ return Encode::undefined();
+
+ const auto *stackFrame = engine->currentStackFrame;
+ if (lval.as<QQmlValueTypeWrapper>()) {
+ qCWarning(lcCoercingTypeAssertion).nospace().noquote()
+ << stackFrame->source() << ':' << stackFrame->lineNumber() << ':'
+ << " Coercing between incompatible value types mistakenly yields null rather than"
+ << " undefined. Add 'pragma ValueTypeBehavior: Assertable' to prevent this.";
+ return Encode::null();
+ }
+
+ if (lval.as<QV4::QObjectWrapper>()) {
+ qCWarning(lcCoercingTypeAssertion).nospace().noquote()
+ << stackFrame->source() << ':' << stackFrame->lineNumber() << ':'
+ << " Coercing from instances of object types to value types mistakenly yields null"
+ << " rather than undefined. Add 'pragma ValueTypeBehavior: Assertable' to prevent"
+ << " this.";
+ return Encode::null();
+ }
+
+ result = coerce(engine, lval, typeWrapper->d()->type(), false);
+ if (result->isUndefined())
+ return Encode::undefined();
+
+ qCWarning(lcCoercingTypeAssertion).nospace().noquote()
+ << stackFrame->source() << ':' << stackFrame->lineNumber() << ':'
+ << " Coercing a value to " << typeWrapper->toQStringNoThrow()
+ << " using a type assertion. This behavior is deprecated."
+ << " Add 'pragma ValueTypeBehavior: Assertable' to prevent it.";
+ return result->asReturnedValue();
}
QV4::ReturnedValue Runtime::In::call(ExecutionEngine *engine, const Value &left, const Value &right)
@@ -564,7 +617,7 @@ Heap::String *RuntimeHelpers::convertToString(ExecutionEngine *engine, Value val
goto redo;
}
case Value::Integer_Type:
- return RuntimeHelpers::stringFromNumber(engine, value.int_32());
+ return engine->newString(QString::number(value.int_32()));
default: // double
return RuntimeHelpers::stringFromNumber(engine, value.doubleValue());
} // switch
@@ -842,6 +895,8 @@ ReturnedValue Runtime::IteratorNextForYieldStar::call(ExecutionEngine *engine, c
engine->hasException = false;
ScopedValue ret(scope, static_cast<const Object &>(iterator).get(engine->id_return()));
+ if (engine->hasException)
+ return Encode(true);
if (ret->isUndefined()) {
// propagate return()
return Encode::undefined();
@@ -856,14 +911,13 @@ ReturnedValue Runtime::IteratorNextForYieldStar::call(ExecutionEngine *engine, c
ScopedValue t(scope, static_cast<const Object &>(iterator).get(engine->id_throw()));
if (engine->hasException)
- return Encode::undefined();
+ return Encode(true);
if (t->isUndefined()) {
// no throw method on the iterator
- ScopedValue done(scope, Encode(false));
- IteratorClose::call(engine, iterator, done);
- if (engine->hasException)
- return Encode::undefined();
- return engine->throwTypeError();
+ IteratorClose::call(engine, iterator);
+ if (!engine->hasException)
+ engine->throwTypeError();
+ return Encode(true);
}
f = t->as<FunctionObject>();
arg = exceptionValue;
@@ -874,14 +928,18 @@ ReturnedValue Runtime::IteratorNextForYieldStar::call(ExecutionEngine *engine, c
f = next->as<FunctionObject>();
}
- if (!f)
- return engine->throwTypeError();
+ if (!f) {
+ engine->throwTypeError();
+ return Encode(true);
+ }
ScopedObject o(scope, f->call(&iterator, arg, 1));
if (scope.hasException())
return Encode(true);
- if (!o)
- return engine->throwTypeError();
+ if (!o) {
+ engine->throwTypeError();
+ return Encode(true);
+ }
ScopedValue d(scope, o->get(engine->id_done()));
if (scope.hasException())
@@ -889,18 +947,15 @@ ReturnedValue Runtime::IteratorNextForYieldStar::call(ExecutionEngine *engine, c
bool done = d->toBoolean();
if (done) {
*object = o->get(engine->id_value());
- return returnCalled ? Encode::undefined() : Encode(true);
+ return (returnCalled && !engine->hasException) ? Encode::undefined() : Encode(true);
}
*object = o;
return Encode(false);
}
-ReturnedValue Runtime::IteratorClose::call(ExecutionEngine *engine, const Value &iterator, const Value &done)
+ReturnedValue Runtime::IteratorClose::call(ExecutionEngine *engine, const Value &iterator)
{
Q_ASSERT(iterator.isObject());
- Q_ASSERT(done.isBoolean());
- if (done.booleanValue())
- return Encode::undefined();
Scope scope(engine);
ScopedValue e(scope);
@@ -1007,7 +1062,7 @@ ReturnedValue Runtime::LoadName::call(ExecutionEngine *engine, int nameIndex)
static Object *getSuperBase(Scope &scope)
{
- ScopedFunctionObject f(scope);
+ Scoped<JavaScriptFunctionObject> f(scope);
ScopedObject homeObject(scope);
if (scope.engine->currentStackFrame->isJSTypesFrame()) {
JSTypesStackFrame *frame = static_cast<JSTypesStackFrame *>(
@@ -1166,7 +1221,7 @@ ReturnedValue Runtime::LoadSuperConstructor::call(ExecutionEngine *engine, const
if (!f)
return engine->throwTypeError();
Heap::Object *c = static_cast<const Object &>(t).getPrototypeOf();
- if (!c->vtable()->isFunctionObject || !static_cast<Heap::FunctionObject *>(c)->isConstructor())
+ if (!c->vtable()->callAsConstructor)
return engine->throwTypeError();
return c->asReturnedValue();
}
@@ -1481,10 +1536,17 @@ ReturnedValue Runtime::CallPropertyLookup::call(ExecutionEngine *engine, const V
// ok to have the value on the stack here
Value f = Value::fromReturnedValue(l->getter(l, engine, base));
- if (!f.isFunctionObject())
- return engine->throwTypeError();
+ if (Q_LIKELY(f.isFunctionObject()))
+ return checkedResult(engine, static_cast<FunctionObject &>(f).call(&base, argv, argc));
- return checkedResult(engine, static_cast<FunctionObject &>(f).call(&base, argv, argc));
+ if (QmlSignalHandler *handler = f.as<QmlSignalHandler>())
+ return checkedResult(engine, handler->call(&base, argv, argc));
+
+ const QString message = QStringLiteral("Property '%1' of object %2 is not a function")
+ .arg(engine->currentStackFrame->v4Function->compilationUnit
+ ->runtimeStrings[l->nameIndex]->toQString())
+ .arg(base.toQStringNoThrow());
+ return engine->throwTypeError(message);
}
ReturnedValue Runtime::CallValue::call(ExecutionEngine *engine, const Value &func, Value *argv, int argc)
@@ -1598,20 +1660,23 @@ ReturnedValue Runtime::TailCall::call(JSTypesStackFrame *frame, ExecutionEngine
int argc = tos[StackOffsets::tailCall_argc].int_32();
Q_ASSERT(argc >= 0);
- if (!function.isFunctionObject())
+ const JavaScriptFunctionObject *jsfo = function.as<JavaScriptFunctionObject>();
+ if (!jsfo) {
+ if (const FunctionObject *fo = function.as<FunctionObject>())
+ return checkedResult(engine, fo->call(&thisObject, argv, argc));
return engine->throwTypeError();
+ }
- const FunctionObject &fo = static_cast<const FunctionObject &>(function);
- if (!frame->callerCanHandleTailCall() || !fo.canBeTailCalled() || engine->debugger()
- || unsigned(argc) > fo.formalParameterCount()) {
+ if (!frame->callerCanHandleTailCall() || !jsfo->canBeTailCalled() || engine->debugger()
+ || unsigned(argc) > jsfo->formalParameterCount()) {
// Cannot tailcall, do a normal call:
- return checkedResult(engine, fo.call(&thisObject, argv, argc));
+ return checkedResult(engine, jsfo->call(&thisObject, argv, argc));
}
- memcpy(frame->jsFrame->args, argv, argc * sizeof(Value));
- frame->init(fo.function(), frame->jsFrame->argValues<Value>(), argc,
+ memmove(frame->jsFrame->args, argv, argc * sizeof(Value));
+ frame->init(jsfo->function(), frame->jsFrame->argValues<Value>(), argc,
frame->callerCanHandleTailCall());
- frame->setupJSFrame(frame->framePointer(), fo, fo.scope(), thisObject,
+ frame->setupJSFrame(frame->framePointer(), *jsfo, jsfo->scope(), thisObject,
Primitive::undefinedValue());
engine->jsStackTop = frame->framePointer() + frame->requiredJSStackFrameSize();
frame->setPendingTailCall(true);
diff --git a/src/qml/jsruntime/qv4runtime_p.h b/src/qml/jsruntime/qv4runtime_p.h
index 40994633f0..a012728cd9 100644
--- a/src/qml/jsruntime/qv4runtime_p.h
+++ b/src/qml/jsruntime/qv4runtime_p.h
@@ -58,7 +58,7 @@ enum TypeHint {
STRING_HINT
};
-struct Q_QML_PRIVATE_EXPORT RuntimeHelpers {
+struct Q_QML_EXPORT RuntimeHelpers {
static ReturnedValue objectDefaultValue(const Object *object, int typeHint);
static ReturnedValue toPrimitive(const Value &value, TypeHint typeHint);
static ReturnedValue ordinaryToPrimitive(ExecutionEngine *engine, const Object *object, String *typeHint);
diff --git a/src/qml/jsruntime/qv4runtimeapi_p.h b/src/qml/jsruntime/qv4runtimeapi_p.h
index 5e11d15495..fdee6ac580 100644
--- a/src/qml/jsruntime/qv4runtimeapi_p.h
+++ b/src/qml/jsruntime/qv4runtimeapi_p.h
@@ -23,7 +23,7 @@ namespace QV4 {
typedef uint Bool;
-struct Q_QML_PRIVATE_EXPORT Runtime {
+struct Q_QML_EXPORT Runtime {
typedef ReturnedValue (*UnaryOperation)(const Value &value);
typedef ReturnedValue (*BinaryOperation)(const Value &left, const Value &right);
typedef ReturnedValue (*BinaryOperationContext)(ExecutionEngine *, const Value &left, const Value &right);
@@ -43,418 +43,418 @@ struct Q_QML_PRIVATE_EXPORT Runtime {
static constexpr bool lastArgumentIsOutputValue = out == LastArgumentIsOutputValue::Yes;
};
using PureMethod = Method<Throws::No, ChangesContext::No, Pure::Yes>;
- using IteratorMethod = Method<Throws::Yes, ChangesContext::No, Pure::No,
+ using IteratorMethod = Method<Throws::No, ChangesContext::No, Pure::No,
LastArgumentIsOutputValue::Yes>;
/* call */
- struct Q_QML_PRIVATE_EXPORT CallGlobalLookup : Method<Throws::Yes>
+ struct Q_QML_EXPORT CallGlobalLookup : Method<Throws::Yes>
{
static ReturnedValue call(ExecutionEngine *, uint, Value[], int);
};
- struct Q_QML_PRIVATE_EXPORT CallQmlContextPropertyLookup : Method<Throws::Yes>
+ struct Q_QML_EXPORT CallQmlContextPropertyLookup : Method<Throws::Yes>
{
static ReturnedValue call(ExecutionEngine *, uint, Value[], int);
};
- struct Q_QML_PRIVATE_EXPORT CallName : Method<Throws::Yes>
+ struct Q_QML_EXPORT CallName : Method<Throws::Yes>
{
static ReturnedValue call(ExecutionEngine *, int, Value[], int);
};
- struct Q_QML_PRIVATE_EXPORT CallProperty : Method<Throws::Yes>
+ struct Q_QML_EXPORT CallProperty : Method<Throws::Yes>
{
static ReturnedValue call(ExecutionEngine *, const Value &, int, Value[], int);
};
- struct Q_QML_PRIVATE_EXPORT CallPropertyLookup : Method<Throws::Yes>
+ struct Q_QML_EXPORT CallPropertyLookup : Method<Throws::Yes>
{
static ReturnedValue call(ExecutionEngine *, const Value &, uint, Value[], int);
};
- struct Q_QML_PRIVATE_EXPORT CallValue : Method<Throws::Yes>
+ struct Q_QML_EXPORT CallValue : Method<Throws::Yes>
{
static ReturnedValue call(ExecutionEngine *, const Value &, Value[], int);
};
- struct Q_QML_PRIVATE_EXPORT CallWithReceiver : Method<Throws::Yes>
+ struct Q_QML_EXPORT CallWithReceiver : Method<Throws::Yes>
{
static ReturnedValue call(ExecutionEngine *, const Value &, const Value &, Value[], int);
};
- struct Q_QML_PRIVATE_EXPORT CallPossiblyDirectEval : Method<Throws::Yes>
+ struct Q_QML_EXPORT CallPossiblyDirectEval : Method<Throws::Yes>
{
static ReturnedValue call(ExecutionEngine *, Value[], int);
};
- struct Q_QML_PRIVATE_EXPORT CallWithSpread : Method<Throws::Yes>
+ struct Q_QML_EXPORT CallWithSpread : Method<Throws::Yes>
{
static ReturnedValue call(ExecutionEngine *, const Value &, const Value &, Value[], int);
};
- struct Q_QML_PRIVATE_EXPORT TailCall : Method<Throws::Yes>
+ struct Q_QML_EXPORT TailCall : Method<Throws::Yes>
{
static ReturnedValue call(JSTypesStackFrame *, ExecutionEngine *engine);
};
/* construct */
- struct Q_QML_PRIVATE_EXPORT Construct : Method<Throws::Yes>
+ struct Q_QML_EXPORT Construct : Method<Throws::Yes>
{
static ReturnedValue call(ExecutionEngine *, const Value &, const Value &, Value[], int);
};
- struct Q_QML_PRIVATE_EXPORT ConstructWithSpread : Method<Throws::Yes>
+ struct Q_QML_EXPORT ConstructWithSpread : Method<Throws::Yes>
{
static ReturnedValue call(ExecutionEngine *, const Value &, const Value &, Value[], int);
};
/* load & store */
- struct Q_QML_PRIVATE_EXPORT StoreNameStrict : Method<Throws::Yes>
+ struct Q_QML_EXPORT StoreNameStrict : Method<Throws::Yes>
{
static void call(ExecutionEngine *, int, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT StoreNameSloppy : Method<Throws::Yes>
+ struct Q_QML_EXPORT StoreNameSloppy : Method<Throws::Yes>
{
static void call(ExecutionEngine *, int, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT StoreProperty : Method<Throws::Yes>
+ struct Q_QML_EXPORT StoreProperty : Method<Throws::Yes>
{
static void call(ExecutionEngine *, const Value &, int, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT StoreElement : Method<Throws::Yes>
+ struct Q_QML_EXPORT StoreElement : Method<Throws::Yes>
{
static void call(ExecutionEngine *, const Value &, const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT LoadProperty : Method<Throws::Yes>
+ struct Q_QML_EXPORT LoadProperty : Method<Throws::Yes>
{
static ReturnedValue call(ExecutionEngine *, const Value &, int);
};
- struct Q_QML_PRIVATE_EXPORT LoadName : Method<Throws::Yes>
+ struct Q_QML_EXPORT LoadName : Method<Throws::Yes>
{
static ReturnedValue call(ExecutionEngine *, int);
};
- struct Q_QML_PRIVATE_EXPORT LoadElement : Method<Throws::Yes>
+ struct Q_QML_EXPORT LoadElement : Method<Throws::Yes>
{
static ReturnedValue call(ExecutionEngine *, const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT LoadSuperProperty : Method<Throws::Yes>
+ struct Q_QML_EXPORT LoadSuperProperty : Method<Throws::Yes>
{
static ReturnedValue call(ExecutionEngine *, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT StoreSuperProperty : Method<Throws::Yes>
+ struct Q_QML_EXPORT StoreSuperProperty : Method<Throws::Yes>
{
static void call(ExecutionEngine *, const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT LoadSuperConstructor : Method<Throws::Yes>
+ struct Q_QML_EXPORT LoadSuperConstructor : Method<Throws::Yes>
{
static ReturnedValue call(ExecutionEngine *, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT LoadGlobalLookup : Method<Throws::Yes>
+ struct Q_QML_EXPORT LoadGlobalLookup : Method<Throws::Yes>
{
static ReturnedValue call(ExecutionEngine *, Function *, int);
};
- struct Q_QML_PRIVATE_EXPORT LoadQmlContextPropertyLookup : Method<Throws::Yes>
+ struct Q_QML_EXPORT LoadQmlContextPropertyLookup : Method<Throws::Yes>
{
static ReturnedValue call(ExecutionEngine *, uint);
};
- struct Q_QML_PRIVATE_EXPORT GetLookup : Method<Throws::Yes>
+ struct Q_QML_EXPORT GetLookup : Method<Throws::Yes>
{
static ReturnedValue call(ExecutionEngine *, Function *, const Value &, int);
};
- struct Q_QML_PRIVATE_EXPORT SetLookupStrict : Method<Throws::Yes>
+ struct Q_QML_EXPORT SetLookupStrict : Method<Throws::Yes>
{
static void call(Function *, const Value &, int, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT SetLookupSloppy : Method<Throws::Yes>
+ struct Q_QML_EXPORT SetLookupSloppy : Method<Throws::Yes>
{
static void call(Function *, const Value &, int, const Value &);
};
/* typeof */
- struct Q_QML_PRIVATE_EXPORT TypeofValue : PureMethod
+ struct Q_QML_EXPORT TypeofValue : PureMethod
{
static ReturnedValue call(ExecutionEngine *, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT TypeofName : Method<Throws::No>
+ struct Q_QML_EXPORT TypeofName : Method<Throws::No>
{
static ReturnedValue call(ExecutionEngine *, int);
};
/* delete */
- struct Q_QML_PRIVATE_EXPORT DeleteProperty_NoThrow : Method<Throws::No>
+ struct Q_QML_EXPORT DeleteProperty_NoThrow : Method<Throws::No>
{
static Bool call(ExecutionEngine *, const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT DeleteProperty : Method<Throws::Yes>
+ struct Q_QML_EXPORT DeleteProperty : Method<Throws::Yes>
{
static ReturnedValue call(ExecutionEngine *, Function *, const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT DeleteName_NoThrow : Method<Throws::No>
+ struct Q_QML_EXPORT DeleteName_NoThrow : Method<Throws::No>
{
static Bool call(ExecutionEngine *, int);
};
- struct Q_QML_PRIVATE_EXPORT DeleteName : Method<Throws::Yes>
+ struct Q_QML_EXPORT DeleteName : Method<Throws::Yes>
{
static ReturnedValue call(ExecutionEngine *, Function *, int);
};
/* exceptions & scopes */
- struct Q_QML_PRIVATE_EXPORT ThrowException : Method<Throws::Yes>
+ struct Q_QML_EXPORT ThrowException : Method<Throws::Yes>
{
static void call(ExecutionEngine *, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT PushCallContext : Method<Throws::No, ChangesContext::Yes>
+ struct Q_QML_EXPORT PushCallContext : Method<Throws::No, ChangesContext::Yes>
{
static void call(JSTypesStackFrame *);
};
- struct Q_QML_PRIVATE_EXPORT PushWithContext : Method<Throws::Yes, ChangesContext::Yes>
+ struct Q_QML_EXPORT PushWithContext : Method<Throws::Yes, ChangesContext::Yes>
{
static ReturnedValue call(ExecutionEngine *, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT PushCatchContext : Method<Throws::No, ChangesContext::Yes>
+ struct Q_QML_EXPORT PushCatchContext : Method<Throws::No, ChangesContext::Yes>
{
static void call(ExecutionEngine *, int, int);
};
- struct Q_QML_PRIVATE_EXPORT PushBlockContext : Method<Throws::No, ChangesContext::Yes>
+ struct Q_QML_EXPORT PushBlockContext : Method<Throws::No, ChangesContext::Yes>
{
static void call(ExecutionEngine *, int);
};
- struct Q_QML_PRIVATE_EXPORT CloneBlockContext : Method<Throws::No, ChangesContext::Yes>
+ struct Q_QML_EXPORT CloneBlockContext : Method<Throws::No, ChangesContext::Yes>
{
static void call(ExecutionEngine *);
};
- struct Q_QML_PRIVATE_EXPORT PushScriptContext : Method<Throws::No, ChangesContext::Yes>
+ struct Q_QML_EXPORT PushScriptContext : Method<Throws::No, ChangesContext::Yes>
{
static void call(ExecutionEngine *, int);
};
- struct Q_QML_PRIVATE_EXPORT PopScriptContext : Method<Throws::No, ChangesContext::Yes>
+ struct Q_QML_EXPORT PopScriptContext : Method<Throws::No, ChangesContext::Yes>
{
static void call(ExecutionEngine *);
};
- struct Q_QML_PRIVATE_EXPORT ThrowReferenceError : Method<Throws::Yes>
+ struct Q_QML_EXPORT ThrowReferenceError : Method<Throws::Yes>
{
static void call(ExecutionEngine *, int);
};
- struct Q_QML_PRIVATE_EXPORT ThrowOnNullOrUndefined : Method<Throws::Yes>
+ struct Q_QML_EXPORT ThrowOnNullOrUndefined : Method<Throws::Yes>
{
static void call(ExecutionEngine *, const Value &);
};
/* closures */
- struct Q_QML_PRIVATE_EXPORT Closure : Method<Throws::No>
+ struct Q_QML_EXPORT Closure : Method<Throws::No>
{
static ReturnedValue call(ExecutionEngine *, int);
};
/* Function header */
- struct Q_QML_PRIVATE_EXPORT ConvertThisToObject : Method<Throws::Yes>
+ struct Q_QML_EXPORT ConvertThisToObject : Method<Throws::Yes>
{
static ReturnedValue call(ExecutionEngine *, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT DeclareVar : Method<Throws::Yes>
+ struct Q_QML_EXPORT DeclareVar : Method<Throws::Yes>
{
static void call(ExecutionEngine *, Bool, int);
};
- struct Q_QML_PRIVATE_EXPORT CreateMappedArgumentsObject : Method<Throws::No>
+ struct Q_QML_EXPORT CreateMappedArgumentsObject : Method<Throws::No>
{
static ReturnedValue call(ExecutionEngine *);
};
- struct Q_QML_PRIVATE_EXPORT CreateUnmappedArgumentsObject : Method<Throws::No>
+ struct Q_QML_EXPORT CreateUnmappedArgumentsObject : Method<Throws::No>
{
static ReturnedValue call(ExecutionEngine *);
};
- struct Q_QML_PRIVATE_EXPORT CreateRestParameter : PureMethod
+ struct Q_QML_EXPORT CreateRestParameter : PureMethod
{
static ReturnedValue call(ExecutionEngine *, int);
};
/* literals */
- struct Q_QML_PRIVATE_EXPORT ArrayLiteral : Method<Throws::Yes>
+ struct Q_QML_EXPORT ArrayLiteral : Method<Throws::Yes>
{
static ReturnedValue call(ExecutionEngine *, Value[], uint);
};
- struct Q_QML_PRIVATE_EXPORT ObjectLiteral : Method<Throws::Yes>
+ struct Q_QML_EXPORT ObjectLiteral : Method<Throws::Yes>
{
static ReturnedValue call(ExecutionEngine *, int, Value[], int);
};
- struct Q_QML_PRIVATE_EXPORT CreateClass : Method<Throws::Yes>
+ struct Q_QML_EXPORT CreateClass : Method<Throws::Yes>
{
static ReturnedValue call(ExecutionEngine *, int, const Value &, Value[]);
};
/* for-in, for-of and array destructuring */
- struct Q_QML_PRIVATE_EXPORT GetIterator : Method<Throws::Yes>
+ struct Q_QML_EXPORT GetIterator : Method<Throws::Yes>
{
static ReturnedValue call(ExecutionEngine *, const Value &, int);
};
- struct Q_QML_PRIVATE_EXPORT IteratorNext : IteratorMethod
+ struct Q_QML_EXPORT IteratorNext : IteratorMethod
{
static ReturnedValue call(ExecutionEngine *, const Value &, Value *);
};
- struct Q_QML_PRIVATE_EXPORT IteratorNextForYieldStar : IteratorMethod
+ struct Q_QML_EXPORT IteratorNextForYieldStar : IteratorMethod
{
static ReturnedValue call(ExecutionEngine *, const Value &, const Value &, Value *);
};
- struct Q_QML_PRIVATE_EXPORT IteratorClose : Method<Throws::Yes>
+ struct Q_QML_EXPORT IteratorClose : Method<Throws::No>
{
- static ReturnedValue call(ExecutionEngine *, const Value &, const Value &);
+ static ReturnedValue call(ExecutionEngine *, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT DestructureRestElement : Method<Throws::Yes>
+ struct Q_QML_EXPORT DestructureRestElement : Method<Throws::Yes>
{
static ReturnedValue call(ExecutionEngine *, const Value &);
};
/* conversions */
- struct Q_QML_PRIVATE_EXPORT ToObject : Method<Throws::Yes>
+ struct Q_QML_EXPORT ToObject : Method<Throws::Yes>
{
static ReturnedValue call(ExecutionEngine *, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT ToBoolean : PureMethod
+ struct Q_QML_EXPORT ToBoolean : PureMethod
{
static Bool call(const Value &);
};
- struct Q_QML_PRIVATE_EXPORT ToNumber : Method<Throws::Yes>
+ struct Q_QML_EXPORT ToNumber : Method<Throws::Yes>
{
static ReturnedValue call(ExecutionEngine *, const Value &);
};
/* unary operators */
- struct Q_QML_PRIVATE_EXPORT UMinus : Method<Throws::Yes>
+ struct Q_QML_EXPORT UMinus : Method<Throws::Yes>
{
static ReturnedValue call(const Value &);
};
/* binary operators */
- struct Q_QML_PRIVATE_EXPORT Instanceof : Method<Throws::Yes>
+ struct Q_QML_EXPORT Instanceof : Method<Throws::Yes>
{
static ReturnedValue call(ExecutionEngine *, const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT As : Method<Throws::No>
+ struct Q_QML_EXPORT As : Method<Throws::No>
{
static ReturnedValue call(ExecutionEngine *, const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT In : Method<Throws::Yes>
+ struct Q_QML_EXPORT In : Method<Throws::Yes>
{
static ReturnedValue call(ExecutionEngine *, const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT Add : Method<Throws::Yes>
+ struct Q_QML_EXPORT Add : Method<Throws::Yes>
{
static ReturnedValue call(ExecutionEngine *, const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT Sub : Method<Throws::Yes>
+ struct Q_QML_EXPORT Sub : Method<Throws::Yes>
{
static ReturnedValue call(const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT Mul : Method<Throws::Yes>
+ struct Q_QML_EXPORT Mul : Method<Throws::Yes>
{
static ReturnedValue call(const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT Div : Method<Throws::Yes>
+ struct Q_QML_EXPORT Div : Method<Throws::Yes>
{
static ReturnedValue call(const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT Mod : Method<Throws::Yes>
+ struct Q_QML_EXPORT Mod : Method<Throws::Yes>
{
static ReturnedValue call(const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT Exp : Method<Throws::Yes>
+ struct Q_QML_EXPORT Exp : Method<Throws::Yes>
{
static ReturnedValue call(const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT BitAnd : Method<Throws::Yes>
+ struct Q_QML_EXPORT BitAnd : Method<Throws::Yes>
{
static ReturnedValue call(const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT BitOr : Method<Throws::Yes>
+ struct Q_QML_EXPORT BitOr : Method<Throws::Yes>
{
static ReturnedValue call(const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT BitXor : Method<Throws::Yes>
+ struct Q_QML_EXPORT BitXor : Method<Throws::Yes>
{
static ReturnedValue call(const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT Shl : Method<Throws::Yes>
+ struct Q_QML_EXPORT Shl : Method<Throws::Yes>
{
static ReturnedValue call(const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT Shr : Method<Throws::Yes>
+ struct Q_QML_EXPORT Shr : Method<Throws::Yes>
{
static ReturnedValue call(const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT UShr : Method<Throws::Yes>
+ struct Q_QML_EXPORT UShr : Method<Throws::Yes>
{
static ReturnedValue call(const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT GreaterThan : Method<Throws::Yes>
+ struct Q_QML_EXPORT GreaterThan : Method<Throws::Yes>
{
static ReturnedValue call(const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT LessThan : Method<Throws::Yes>
+ struct Q_QML_EXPORT LessThan : Method<Throws::Yes>
{
static ReturnedValue call(const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT GreaterEqual : Method<Throws::Yes>
+ struct Q_QML_EXPORT GreaterEqual : Method<Throws::Yes>
{
static ReturnedValue call(const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT LessEqual : Method<Throws::Yes>
+ struct Q_QML_EXPORT LessEqual : Method<Throws::Yes>
{
static ReturnedValue call(const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT Equal : Method<Throws::Yes>
+ struct Q_QML_EXPORT Equal : Method<Throws::Yes>
{
static ReturnedValue call(const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT NotEqual : Method<Throws::Yes>
+ struct Q_QML_EXPORT NotEqual : Method<Throws::Yes>
{
static ReturnedValue call(const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT StrictEqual : Method<Throws::Yes>
+ struct Q_QML_EXPORT StrictEqual : Method<Throws::Yes>
{
static ReturnedValue call(const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT StrictNotEqual : Method<Throws::Yes>
+ struct Q_QML_EXPORT StrictNotEqual : Method<Throws::Yes>
{
static ReturnedValue call(const Value &, const Value &);
};
/* comparisons */
- struct Q_QML_PRIVATE_EXPORT CompareGreaterThan : Method<Throws::Yes>
+ struct Q_QML_EXPORT CompareGreaterThan : Method<Throws::Yes>
{
static Bool call(const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT CompareLessThan : Method<Throws::Yes>
+ struct Q_QML_EXPORT CompareLessThan : Method<Throws::Yes>
{
static Bool call(const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT CompareGreaterEqual : Method<Throws::Yes>
+ struct Q_QML_EXPORT CompareGreaterEqual : Method<Throws::Yes>
{
static Bool call(const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT CompareLessEqual : Method<Throws::Yes>
+ struct Q_QML_EXPORT CompareLessEqual : Method<Throws::Yes>
{
static Bool call(const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT CompareEqual : Method<Throws::Yes>
+ struct Q_QML_EXPORT CompareEqual : Method<Throws::Yes>
{
static Bool call(const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT CompareNotEqual : Method<Throws::Yes>
+ struct Q_QML_EXPORT CompareNotEqual : Method<Throws::Yes>
{
static Bool call(const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT CompareStrictEqual : Method<Throws::Yes>
+ struct Q_QML_EXPORT CompareStrictEqual : Method<Throws::Yes>
{
static Bool call(const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT CompareStrictNotEqual : Method<Throws::Yes>
+ struct Q_QML_EXPORT CompareStrictNotEqual : Method<Throws::Yes>
{
static Bool call(const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT CompareInstanceof : Method<Throws::Yes>
+ struct Q_QML_EXPORT CompareInstanceof : Method<Throws::Yes>
{
static Bool call(ExecutionEngine *, const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT CompareIn : Method<Throws::Yes>
+ struct Q_QML_EXPORT CompareIn : Method<Throws::Yes>
{
static Bool call(ExecutionEngine *, const Value &, const Value &);
};
- struct Q_QML_PRIVATE_EXPORT RegexpLiteral : PureMethod
+ struct Q_QML_EXPORT RegexpLiteral : PureMethod
{
static ReturnedValue call(ExecutionEngine *, int);
};
- struct Q_QML_PRIVATE_EXPORT GetTemplateObject : PureMethod
+ struct Q_QML_EXPORT GetTemplateObject : PureMethod
{
static ReturnedValue call(Function *, int);
};
diff --git a/src/qml/jsruntime/qv4script.cpp b/src/qml/jsruntime/qv4script.cpp
index 395409ad4b..894426ce3b 100644
--- a/src/qml/jsruntime/qv4script.cpp
+++ b/src/qml/jsruntime/qv4script.cpp
@@ -26,14 +26,15 @@ using namespace QQmlJS;
Script::Script(ExecutionEngine *v4, QmlContext *qml, const QQmlRefPointer<ExecutableCompilationUnit> &compilationUnit)
: line(1), column(0), context(v4->rootContext()), strictMode(false), inheritContext(true), parsed(false)
- , compilationUnit(compilationUnit), vmFunction(nullptr), parseAsBinding(true)
+ , compilationUnit(compilationUnit), parseAsBinding(true)
{
if (qml)
qmlContext.set(v4, *qml);
parsed = true;
- vmFunction = compilationUnit ? compilationUnit->linkToEngine(v4) : nullptr;
+ vmFunction.set(v4,
+ compilationUnit ? compilationUnit->rootFunction() : nullptr);
}
Script::~Script()
@@ -45,14 +46,12 @@ void Script::parse()
if (parsed)
return;
- using namespace QV4::Compiler;
-
parsed = true;
ExecutionEngine *v4 = context->engine();
Scope valueScope(v4);
- Module module(v4->debugger() != nullptr);
+ QV4::Compiler::Module module(v4->debugger() != nullptr);
if (sourceCode.startsWith(QLatin1String("function("))) {
static const int snippetLength = 70;
@@ -96,8 +95,8 @@ void Script::parse()
if (v4->hasException)
return;
- compilationUnit = QV4::ExecutableCompilationUnit::create(cg.generateCompilationUnit());
- vmFunction = compilationUnit->linkToEngine(v4);
+ compilationUnit = v4->insertCompilationUnit(cg.generateCompilationUnit());
+ vmFunction.set(v4, compilationUnit->rootFunction());
}
if (!vmFunction) {
@@ -135,7 +134,7 @@ Function *Script::function()
return vmFunction;
}
-QV4::CompiledData::CompilationUnit Script::precompile(
+QQmlRefPointer<QV4::CompiledData::CompilationUnit> Script::precompile(
QV4::Compiler::Module *module, QQmlJS::Engine *jsEngine,
Compiler::JSUnitGenerator *unitGenerator, const QString &fileName, const QString &finalUrl,
const QString &source, QList<QQmlError> *reportedErrors,
@@ -199,8 +198,9 @@ Script *Script::createFromFileOrCache(ExecutionEngine *engine, QmlContext *qmlCo
&cacheError)
: nullptr) {
QQmlRefPointer<QV4::ExecutableCompilationUnit> jsUnit
- = QV4::ExecutableCompilationUnit::create(
- QV4::CompiledData::CompilationUnit(cachedUnit->qmlData, cachedUnit->aotCompiledFunctions));
+ = engine->insertCompilationUnit(
+ QQml::makeRefPointer<QV4::CompiledData::CompilationUnit>(
+ cachedUnit->qmlData, cachedUnit->aotCompiledFunctions));
return new QV4::Script(engine, qmlContext, jsUnit);
}
diff --git a/src/qml/jsruntime/qv4script_p.h b/src/qml/jsruntime/qv4script_p.h
index 26996ca2bc..7b3dcae486 100644
--- a/src/qml/jsruntime/qv4script_p.h
+++ b/src/qml/jsruntime/qv4script_p.h
@@ -36,11 +36,11 @@ struct Q_QML_EXPORT Script {
Script(ExecutionContext *scope, QV4::Compiler::ContextType mode, const QString &sourceCode, const QString &source = QString(), int line = 1, int column = 0)
: sourceFile(source), line(line), column(column), sourceCode(sourceCode)
, context(scope), strictMode(false), inheritContext(false), parsed(false), contextType(mode)
- , vmFunction(nullptr), parseAsBinding(false) {}
+ , parseAsBinding(false) {}
Script(ExecutionEngine *engine, QmlContext *qml, bool parseAsBinding, const QString &sourceCode, const QString &source = QString(), int line = 1, int column = 0)
: sourceFile(source), line(line), column(column), sourceCode(sourceCode)
, context(engine->rootContext()), strictMode(false), inheritContext(true), parsed(false)
- , vmFunction(nullptr), parseAsBinding(parseAsBinding) {
+ , parseAsBinding(parseAsBinding) {
if (qml)
qmlContext.set(engine, *qml);
}
@@ -57,7 +57,7 @@ struct Q_QML_EXPORT Script {
QV4::Compiler::ContextType contextType = QV4::Compiler::ContextType::Eval;
QV4::PersistentValue qmlContext;
QQmlRefPointer<ExecutableCompilationUnit> compilationUnit;
- Function *vmFunction;
+ QV4::WriteBarrier::Pointer<Function> vmFunction;
bool parseAsBinding;
void parse();
@@ -65,7 +65,7 @@ struct Q_QML_EXPORT Script {
Function *function();
- static QV4::CompiledData::CompilationUnit precompile(
+ static QQmlRefPointer<QV4::CompiledData::CompilationUnit> precompile(
QV4::Compiler::Module *module, QQmlJS::Engine *jsEngine,
Compiler::JSUnitGenerator *unitGenerator, const QString &fileName,
const QString &finalUrl, const QString &source,
diff --git a/src/qml/jsruntime/qv4sequenceobject.cpp b/src/qml/jsruntime/qv4sequenceobject.cpp
index 132084705c..8de85a86bf 100644
--- a/src/qml/jsruntime/qv4sequenceobject.cpp
+++ b/src/qml/jsruntime/qv4sequenceobject.cpp
@@ -27,9 +27,9 @@ static ReturnedValue doGetIndexed(const Sequence *s, qsizetype index) {
Heap::ReferenceObject::Flags flags =
Heap::ReferenceObject::EnforcesLocation;
- if (s->d()->typePrivate()->extraData.ld->canSetValueAtIndex())
+ if (s->d()->metaSequence().canSetValueAtIndex())
flags |= Heap::ReferenceObject::CanWriteBack;
- if (Sequence::valueMetaType(s->d()) == QMetaType::fromType<QVariant>())
+ if (s->d()->valueMetaType() == QMetaType::fromType<QVariant>())
flags |= Heap::ReferenceObject::IsVariant;
QV4::ScopedValue v(scope, scope.engine->fromVariant(
@@ -42,31 +42,6 @@ static ReturnedValue doGetIndexed(const Sequence *s, qsizetype index) {
return v->asReturnedValue();
}
-static const QMetaSequence *metaSequence(const Heap::Sequence *p)
-{
- return p->typePrivate()->extraData.ld;
-}
-
-template<typename Compare>
-void sortSequence(Sequence *sequence, const Compare &compare)
-{
- auto *p = sequence->d();
- const auto *m = metaSequence(p);
-
- QSequentialIterable iterable(*m, p->typePrivate()->listId, p->storagePointer());
- if (iterable.canRandomAccessIterate()) {
- std::sort(QSequentialIterable::RandomAccessIterator(iterable.mutableBegin()),
- QSequentialIterable::RandomAccessIterator(iterable.mutableEnd()),
- compare);
- } else if (iterable.canReverseIterate()) {
- std::sort(QSequentialIterable::BidirectionalIterator(iterable.mutableBegin()),
- QSequentialIterable::BidirectionalIterator(iterable.mutableEnd()),
- compare);
- } else {
- qWarning() << "Container has no suitable iterator for sorting";
- }
-}
-
// helper function to generate valid warnings if errors occur during sequence operations.
static void generateWarning(QV4::ExecutionEngine *v4, const QString& description)
{
@@ -114,71 +89,35 @@ struct SequenceOwnPropertyKeyIterator : ObjectOwnPropertyKeyIterator
}
};
-struct SequenceCompareFunctor
-{
- SequenceCompareFunctor(QV4::ExecutionEngine *v4, const QV4::Value &compareFn)
- : m_v4(v4), m_compareFn(&compareFn)
- {}
-
- bool operator()(const QVariant &lhs, const QVariant &rhs)
- {
- QV4::Scope scope(m_v4);
- ScopedFunctionObject compare(scope, m_compareFn);
- if (!compare)
- return m_v4->throwTypeError();
- Value *argv = scope.alloc(2);
- argv[0] = m_v4->fromVariant(lhs);
- argv[1] = m_v4->fromVariant(rhs);
- QV4::ScopedValue result(scope, compare->call(m_v4->globalObject, argv, 2));
- if (scope.hasException())
- return false;
- return result->toNumber() < 0;
- }
-
-private:
- QV4::ExecutionEngine *m_v4;
- const QV4::Value *m_compareFn;
-};
-
-struct SequenceDefaultCompareFunctor
-{
- bool operator()(const QVariant &lhs, const QVariant &rhs)
- {
- return lhs.toString() < rhs.toString();
- }
-};
-
-void Heap::Sequence::init(const QQmlType &qmlType, const void *container)
+void Heap::Sequence::initTypes(QMetaType listType, QMetaSequence metaSequence)
{
- ReferenceObject::init(nullptr, -1, NoFlag);
-
- Q_ASSERT(qmlType.isSequentialContainer());
- m_typePrivate = qmlType.priv();
- QQmlType::refHandle(m_typePrivate);
-
- m_container = m_typePrivate->listId.create(container);
-
+ m_listType = listType.iface();
+ Q_ASSERT(m_listType);
+ m_metaSequence = metaSequence.iface();
+ Q_ASSERT(m_metaSequence);
QV4::Scope scope(internalClass->engine);
QV4::Scoped<QV4::Sequence> o(scope, this);
o->setArrayType(Heap::ArrayData::Custom);
}
+void Heap::Sequence::init(QMetaType listType, QMetaSequence metaSequence, const void *container)
+{
+ ReferenceObject::init(nullptr, -1, NoFlag);
+ initTypes(listType, metaSequence);
+ m_container = listType.create(container);
+}
+
void Heap::Sequence::init(
- const QQmlType &qmlType, const void *container,
- Heap::Object *object, int propertyIndex, Heap::ReferenceObject::Flags flags)
+ QMetaType listType, QMetaSequence metaSequence, const void *container,
+ Heap::Object *object, int propertyIndex, Heap::ReferenceObject::Flags flags)
{
ReferenceObject::init(object, propertyIndex, flags);
+ initTypes(listType, metaSequence);
- Q_ASSERT(qmlType.isSequentialContainer());
- m_typePrivate = qmlType.priv();
- QQmlType::refHandle(m_typePrivate);
- QV4::Scope scope(internalClass->engine);
- QV4::Scoped<QV4::Sequence> o(scope, this);
- o->setArrayType(Heap::ArrayData::Custom);
- if (CppStackFrame *frame = scope.engine->currentStackFrame)
+ if (CppStackFrame *frame = internalClass->engine->currentStackFrame)
setLocation(frame->v4Function, frame->statementNumber());
if (container)
- m_container = QMetaType(m_typePrivate->listId).create(container);
+ m_container = listType.create(container);
else if (flags & EnforcesLocation)
QV4::ReferenceObject::readReference(this);
}
@@ -186,28 +125,27 @@ void Heap::Sequence::init(
Heap::Sequence *Heap::Sequence::detached() const
{
return internalClass->engine->memoryManager->allocate<QV4::Sequence>(
- QQmlType(m_typePrivate), m_container);
+ QMetaType(m_listType), QMetaSequence(m_metaSequence), m_container);
}
void Heap::Sequence::destroy()
{
if (m_container)
- m_typePrivate->listId.destroy(m_container);
- QQmlType::derefHandle(m_typePrivate);
+ listType().destroy(m_container);
ReferenceObject::destroy();
}
void *Heap::Sequence::storagePointer()
{
if (!m_container)
- m_container = m_typePrivate->listId.create();
+ m_container = listType().create();
return m_container;
}
bool Heap::Sequence::setVariant(const QVariant &variant)
{
const QMetaType variantReferenceType = variant.metaType();
- if (variantReferenceType != m_typePrivate->listId) {
+ if (variantReferenceType != listType()) {
// This is a stale reference. That is, the property has been
// overwritten with a different type in the meantime.
// We need to modify this reference to the updated type, if
@@ -215,11 +153,10 @@ bool Heap::Sequence::setVariant(const QVariant &variant)
const QQmlType newType = QQmlMetaType::qmlListType(variantReferenceType);
if (newType.isSequentialContainer()) {
if (m_container)
- m_typePrivate->listId.destroy(m_container);
- QQmlType::derefHandle(m_typePrivate);
- m_typePrivate = newType.priv();
- QQmlType::refHandle(m_typePrivate);
- m_container = m_typePrivate->listId.create(variant.constData());
+ listType().destroy(m_container);
+ m_listType = newType.qListTypeId().iface();
+ m_metaSequence = newType.listMetaSequence().iface();
+ m_container = listType().create(variant.constData());
return true;
} else {
return false;
@@ -235,36 +172,67 @@ bool Heap::Sequence::setVariant(const QVariant &variant)
}
QVariant Heap::Sequence::toVariant() const
{
- return QVariant(m_typePrivate->listId, m_container);
-}
-
-const QMetaType Sequence::valueMetaType(const Heap::Sequence *p)
-{
- return p->typePrivate()->typeId;
+ return QVariant(listType(), m_container);
}
qsizetype Sequence::size() const
{
const auto *p = d();
Q_ASSERT(p->storagePointer()); // Must readReference() before
- return metaSequence(p)->size(p->storagePointer());
+ return p->metaSequence().size(p->storagePointer());
}
QVariant Sequence::at(qsizetype index) const
{
const auto *p = d();
Q_ASSERT(p->storagePointer()); // Must readReference() before
- const QMetaType v = valueMetaType(p);
+ const QMetaType v = p->valueMetaType();
QVariant result;
if (v == QMetaType::fromType<QVariant>()) {
- metaSequence(p)->valueAtIndex(p->storagePointer(), index, &result);
+ p->metaSequence().valueAtIndex(p->storagePointer(), index, &result);
} else {
result = QVariant(v);
- metaSequence(p)->valueAtIndex(p->storagePointer(), index, result.data());
+ p->metaSequence().valueAtIndex(p->storagePointer(), index, result.data());
}
return result;
}
+QVariant Sequence::shift()
+{
+ auto *p = d();
+ void *storage = p->storagePointer();
+ Q_ASSERT(storage); // Must readReference() before
+ const QMetaType v = p->valueMetaType();
+ const QMetaSequence m = p->metaSequence();
+
+ const auto variantData = [&](QVariant *variant) -> void *{
+ if (v == QMetaType::fromType<QVariant>())
+ return variant;
+
+ *variant = QVariant(v);
+ return variant->data();
+ };
+
+ QVariant result;
+ void *resultData = variantData(&result);
+ m.valueAtIndex(storage, 0, resultData);
+
+ if (m.canRemoveValueAtBegin()) {
+ m.removeValueAtBegin(storage);
+ return result;
+ }
+
+ QVariant t;
+ void *tData = variantData(&t);
+ for (qsizetype i = 1, end = m.size(storage); i < end; ++i) {
+ m.valueAtIndex(storage, i, tData);
+ m.setValueAtIndex(storage, i - 1, tData);
+ }
+ m.removeValueAtEnd(storage);
+
+ return result;
+}
+
template<typename Action>
void convertAndDo(const QVariant &item, const QMetaType v, Action action)
@@ -284,45 +252,45 @@ void convertAndDo(const QVariant &item, const QMetaType v, Action action)
void Sequence::append(const QVariant &item)
{
Heap::Sequence *p = d();
- convertAndDo(item, valueMetaType(p), [p](const void *data) {
- metaSequence(p)->addValueAtEnd(p->storagePointer(), data);
+ convertAndDo(item, p->valueMetaType(), [p](const void *data) {
+ p->metaSequence().addValueAtEnd(p->storagePointer(), data);
});
}
void Sequence::append(qsizetype num, const QVariant &item)
{
Heap::Sequence *p = d();
- convertAndDo(item, valueMetaType(p), [p, num](const void *data) {
- const QMetaSequence *m = metaSequence(p);
+ convertAndDo(item, p->valueMetaType(), [p, num](const void *data) {
+ const QMetaSequence m = p->metaSequence();
void *container = p->storagePointer();
for (qsizetype i = 0; i < num; ++i)
- m->addValueAtEnd(container, data);
+ m.addValueAtEnd(container, data);
});
}
void Sequence::replace(qsizetype index, const QVariant &item)
{
Heap::Sequence *p = d();
- convertAndDo(item, valueMetaType(p), [p, index](const void *data) {
- metaSequence(p)->setValueAtIndex(p->storagePointer(), index, data);
+ convertAndDo(item, p->valueMetaType(), [p, index](const void *data) {
+ p->metaSequence().setValueAtIndex(p->storagePointer(), index, data);
});
}
void Sequence::removeLast(qsizetype num)
{
auto *p = d();
- const auto *m = metaSequence(p);
-
- if (m->canEraseRangeAtIterator() && m->hasRandomAccessIterator() && num > 1) {
- void *i = m->end(p->storagePointer());
- m->advanceIterator(i, -num);
- void *j = m->end(p->storagePointer());
- m->eraseRangeAtIterator(p->storagePointer(), i, j);
- m->destroyIterator(i);
- m->destroyIterator(j);
+ const QMetaSequence m = p->metaSequence();
+
+ if (m.canEraseRangeAtIterator() && m.hasRandomAccessIterator() && num > 1) {
+ void *i = m.end(p->storagePointer());
+ m.advanceIterator(i, -num);
+ void *j = m.end(p->storagePointer());
+ m.eraseRangeAtIterator(p->storagePointer(), i, j);
+ m.destroyIterator(i);
+ m.destroyIterator(j);
} else {
for (int i = 0; i < num; ++i)
- m->removeValueAtEnd(p->storagePointer());
+ m.removeValueAtEnd(p->storagePointer());
}
}
@@ -355,7 +323,7 @@ bool Sequence::containerPutIndexed(qsizetype index, const Value &value)
return false;
const qsizetype count = size();
- const QMetaType valueType = valueMetaType(d());
+ const QMetaType valueType = d()->valueMetaType();
const QVariant element = ExecutionEngine::toVariant(value, valueType, false);
if (index < 0)
@@ -419,24 +387,6 @@ bool Sequence::containerIsEqualTo(Managed *other)
return false;
}
-bool Sequence::sort(const FunctionObject *f, const Value *, const Value *argv, int argc)
-{
- if (d()->isReadOnly())
- return false;
- if (d()->isReference() && !loadReference())
- return false;
-
- if (argc == 1 && argv[0].as<FunctionObject>())
- sortSequence(this, SequenceCompareFunctor(f->engine(), argv[0]));
- else
- sortSequence(this, SequenceDefaultCompareFunctor());
-
- if (d()->object())
- storeReference();
-
- return true;
-}
-
void *Sequence::getRawContainerPtr() const
{ return d()->storagePointer(); }
@@ -467,6 +417,14 @@ ReturnedValue Sequence::virtualGet(const Managed *that, PropertyKey id, const Va
return Object::virtualGet(that, id, receiver, hasProperty);
}
+qint64 Sequence::virtualGetLength(const Managed *m)
+{
+ const Sequence *s = static_cast<const Sequence *>(m);
+ if (s->d()->isReference() && !s->loadReference())
+ return 0;
+ return s->size();
+}
+
bool Sequence::virtualPut(Managed *that, PropertyKey id, const Value &value, Value *receiver)
{
if (id.isArrayIndex()) {
@@ -510,26 +468,27 @@ int Sequence::virtualMetacall(Object *object, QMetaObject::Call call, int index,
switch (call) {
case QMetaObject::ReadProperty: {
- const QMetaType valueType = valueMetaType(sequence->d());
- if (!sequence->loadReference())
+ const QMetaType valueType = sequence->d()->valueMetaType();
+ if (sequence->d()->isReference() && !sequence->loadReference())
return 0;
- const QMetaSequence *metaSequence = sequence->d()->typePrivate()->extraData.ld;
- if (metaSequence->valueMetaType() != valueType)
+ const QMetaSequence metaSequence = sequence->d()->metaSequence();
+ if (metaSequence.valueMetaType() != valueType)
return 0; // value metatype is not what the caller expects anymore.
const void *storagePointer = sequence->d()->storagePointer();
- if (index < 0 || index >= metaSequence->size(storagePointer))
+ if (index < 0 || index >= metaSequence.size(storagePointer))
return 0;
- metaSequence->valueAtIndex(storagePointer, index, a[0]);
+ metaSequence.valueAtIndex(storagePointer, index, a[0]);
break;
}
case QMetaObject::WriteProperty: {
void *storagePointer = sequence->d()->storagePointer();
- const QMetaSequence *metaSequence = sequence->d()->typePrivate()->extraData.ld;
- if (index < 0 || index >= metaSequence->size(storagePointer))
+ const QMetaSequence metaSequence = sequence->d()->metaSequence();
+ if (index < 0 || index >= metaSequence.size(storagePointer))
return 0;
- metaSequence->setValueAtIndex(storagePointer, index, a[0]);
- sequence->storeReference();
+ metaSequence.setValueAtIndex(storagePointer, index, a[0]);
+ if (sequence->d()->isReference())
+ sequence->storeReference();
break;
}
default:
@@ -584,7 +543,7 @@ static QV4::ReturnedValue method_set_length(const FunctionObject *f, const Value
if (newCount == count) {
RETURN_UNDEFINED();
} else if (newCount > count) {
- const QMetaType valueMetaType = metaSequence(This->d())->valueMetaType();
+ const QMetaType valueMetaType = This->d()->valueMetaType();
/* according to ECMA262r3 we need to insert */
/* undefined values increasing length to newLength. */
/* We cannot, so we insert default-values instead. */
@@ -605,9 +564,9 @@ static QV4::ReturnedValue method_set_length(const FunctionObject *f, const Value
void SequencePrototype::init()
{
- defineDefaultProperty(QStringLiteral("sort"), method_sort, 1);
defineDefaultProperty(engine()->id_valueOf(), method_valueOf, 0);
defineAccessorProperty(QStringLiteral("length"), method_get_length, method_set_length);
+ defineDefaultProperty(QStringLiteral("shift"), method_shift, 0);
}
ReturnedValue SequencePrototype::method_valueOf(const FunctionObject *f, const Value *thisObject, const Value *, int)
@@ -615,59 +574,67 @@ ReturnedValue SequencePrototype::method_valueOf(const FunctionObject *f, const V
return Encode(thisObject->toString(f->engine()));
}
-ReturnedValue SequencePrototype::method_sort(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc)
+ReturnedValue SequencePrototype::method_shift(
+ const FunctionObject *b, const Value *thisObject, const Value *argv, int argc)
{
Scope scope(b);
- QV4::ScopedObject o(scope, thisObject);
- if (!o || !o->isV4SequenceType())
- THROW_TYPE_ERROR();
+ Scoped<Sequence> s(scope, thisObject);
+ if (!s)
+ return ArrayPrototype::method_shift(b, thisObject, argv, argc);
+
+ if (s->d()->isReference() && !s->loadReference())
+ RETURN_UNDEFINED();
- if (argc >= 2)
- return o.asReturnedValue();
+ const qsizetype len = s->size();
+ if (!len)
+ RETURN_UNDEFINED();
- if (auto *s = o->as<Sequence>()) {
- if (!s->sort(b, thisObject, argv, argc))
- THROW_TYPE_ERROR();
- }
+ ScopedValue result(scope, scope.engine->fromVariant(s->shift()));
- return o.asReturnedValue();
+ if (s->d()->object())
+ s->storeReference();
+
+ return result->asReturnedValue();
}
ReturnedValue SequencePrototype::newSequence(
- QV4::ExecutionEngine *engine, QMetaType sequenceType, const void *data,
- Heap::Object *object, int propertyIndex, Heap::ReferenceObject::Flags flags)
+ QV4::ExecutionEngine *engine, QMetaType type, QMetaSequence metaSequence, const void *data,
+ Heap::Object *object, int propertyIndex, Heap::ReferenceObject::Flags flags)
{
// This function is called when the property is a QObject Q_PROPERTY of
// the given sequence type. Internally we store a sequence
// (as well as object ptr + property index for updated-read and write-back)
// and so access/mutate avoids variant conversion.
- const QQmlType qmlType = QQmlMetaType::qmlListType(sequenceType);
- if (qmlType.isSequentialContainer()) {
- return engine->memoryManager->allocate<Sequence>(
- qmlType, data, object, propertyIndex, flags)->asReturnedValue();
- }
-
- return Encode::undefined();
+ return engine->memoryManager->allocate<Sequence>(
+ type, metaSequence, data, object, propertyIndex, flags)->asReturnedValue();
}
ReturnedValue SequencePrototype::fromVariant(QV4::ExecutionEngine *engine, const QVariant &v)
{
- return fromData(engine, v.metaType(), v.constData());
+ const QMetaType type = v.metaType();
+ const QQmlType qmlType = QQmlMetaType::qmlListType(type);
+ if (qmlType.isSequentialContainer())
+ return fromData(engine, type, qmlType.listMetaSequence(), v.constData());
+
+ QSequentialIterable iterable;
+ if (QMetaType::convert(
+ type, v.constData(), QMetaType::fromType<QSequentialIterable>(), &iterable)) {
+ return fromData(engine, type, iterable.metaContainer(), v.constData());
+ }
+
+ return Encode::undefined();
}
-ReturnedValue SequencePrototype::fromData(ExecutionEngine *engine, QMetaType type, const void *data)
+ReturnedValue SequencePrototype::fromData(
+ ExecutionEngine *engine, QMetaType type, QMetaSequence metaSequence, const void *data)
{
// This function is called when assigning a sequence value to a normal JS var
// in a JS block. Internally, we store a sequence of the specified type.
// Access and mutation is extremely fast since it will not need to modify any
// QObject property.
- const QQmlType qmlType = QQmlMetaType::qmlListType(type);
- if (qmlType.isSequentialContainer())
- return engine->memoryManager->allocate<Sequence>(qmlType, data)->asReturnedValue();
-
- return Encode::undefined();
+ return engine->memoryManager->allocate<Sequence>(type, metaSequence, data)->asReturnedValue();
}
QVariant SequencePrototype::toVariant(const Sequence *object)
@@ -684,7 +651,7 @@ QVariant SequencePrototype::toVariant(const Sequence *object)
if (!p->hasData())
return QVariant();
- return QVariant(p->typePrivate()->listId, p->storagePointer());
+ return QVariant(p->listType(), p->storagePointer());
}
QVariant SequencePrototype::toVariant(const QV4::Value &array, QMetaType typeHint)
@@ -698,7 +665,7 @@ QVariant SequencePrototype::toVariant(const QV4::Value &array, QMetaType typeHin
const QQmlType type = QQmlMetaType::qmlListType(typeHint);
if (type.isSequentialContainer()) {
const QQmlTypePrivate *priv = type.priv();
- const QMetaSequence *meta = priv->extraData.ld;
+ const QMetaSequence *meta = &priv->extraData.sequentialContainerTypeData;
const QMetaType containerMetaType(priv->listId);
QVariant result(containerMetaType);
qint64 length = a->getLength();
@@ -714,18 +681,19 @@ QVariant SequencePrototype::toVariant(const QV4::Value &array, QMetaType typeHin
} else {
const QMetaType originalType = variant.metaType();
if (originalType != valueMetaType) {
- QVariant converted(valueMetaType);
- if (QQmlValueTypeProvider::createValueType(
- variant, valueMetaType, converted.data())) {
+ const QVariant converted = QQmlValueTypeProvider::createValueType(
+ variant, valueMetaType);
+ if (converted.isValid()) {
variant = converted;
- } else if (!variant.convert(valueMetaType)) {
+ } else if (!variant.convert(valueMetaType) && originalType.isValid()) {
+ // If the original type was void, we're converting a "hole" in a sparse
+ // array. There is no point in warning about that.
qWarning().noquote()
<< QLatin1String("Could not convert array value "
"at position %1 from %2 to %3")
.arg(QString::number(i),
QString::fromUtf8(originalType.name()),
QString::fromUtf8(valueMetaType.name()));
- variant = converted;
}
}
meta->addValueAtEnd(result.data(), variant.constData());
@@ -739,14 +707,14 @@ QVariant SequencePrototype::toVariant(const QV4::Value &array, QMetaType typeHin
void *SequencePrototype::getRawContainerPtr(const Sequence *object, QMetaType typeHint)
{
- if (object->d()->typePrivate()->listId == typeHint)
+ if (object->d()->listType() == typeHint)
return object->getRawContainerPtr();
return nullptr;
}
QMetaType SequencePrototype::metaTypeForSequence(const Sequence *object)
{
- return object->d()->typePrivate()->listId;
+ return object->d()->listType();
}
} // namespace QV4
diff --git a/src/qml/jsruntime/qv4sequenceobject_p.h b/src/qml/jsruntime/qv4sequenceobject_p.h
index 282928e661..0908e52574 100644
--- a/src/qml/jsruntime/qv4sequenceobject_p.h
+++ b/src/qml/jsruntime/qv4sequenceobject_p.h
@@ -28,19 +28,20 @@ QT_BEGIN_NAMESPACE
namespace QV4 {
struct Sequence;
-struct Q_QML_PRIVATE_EXPORT SequencePrototype : public QV4::Object
+struct Q_QML_EXPORT SequencePrototype : public QV4::Object
{
V4_PROTOTYPE(arrayPrototype)
void init();
static ReturnedValue method_valueOf(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
- static ReturnedValue method_sort(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
+ static ReturnedValue method_shift(const FunctionObject *b, const Value *thisObject, const Value *, int);
static ReturnedValue newSequence(
- QV4::ExecutionEngine *engine, QMetaType sequenceType, const void *data,
- Heap::Object *object, int propertyIndex, Heap::ReferenceObject::Flags flags);
+ QV4::ExecutionEngine *engine, QMetaType type, QMetaSequence metaSequence, const void *data,
+ Heap::Object *object, int propertyIndex, Heap::ReferenceObject::Flags flags);
static ReturnedValue fromVariant(QV4::ExecutionEngine *engine, const QVariant &vd);
- static ReturnedValue fromData(QV4::ExecutionEngine *engine, QMetaType type, const void *data);
+ static ReturnedValue fromData(
+ QV4::ExecutionEngine *engine, QMetaType type, QMetaSequence metaSequence, const void *data);
static QMetaType metaTypeForSequence(const Sequence *object);
static QVariant toVariant(const Sequence *object);
@@ -52,8 +53,8 @@ namespace Heap {
struct Sequence : ReferenceObject
{
- void init(const QQmlType &qmlType, const void *container);
- void init(const QQmlType &qmlType, const void *container,
+ void init(QMetaType listType, QMetaSequence metaSequence, const void *container);
+ void init(QMetaType listType, QMetaSequence metaSequence, const void *container,
Object *object, int propertyIndex, Heap::ReferenceObject::Flags flags);
Sequence *detached() const;
@@ -68,25 +69,30 @@ struct Sequence : ReferenceObject
bool setVariant(const QVariant &variant);
QVariant toVariant() const;
- const QQmlTypePrivate *typePrivate() const { return m_typePrivate; }
+ QMetaType listType() const { return QMetaType(m_listType); }
+ QMetaType valueMetaType() const { return QMetaType(m_metaSequence->valueMetaType); }
+ QMetaSequence metaSequence() const { return QMetaSequence(m_metaSequence); }
private:
+ void initTypes(QMetaType listType, QMetaSequence metaSequence);
+
void *m_container;
- const QQmlTypePrivate *m_typePrivate;
+ const QtPrivate::QMetaTypeInterface *m_listType;
+ const QtMetaContainerPrivate::QMetaSequenceInterface *m_metaSequence;
};
}
-struct Q_QML_PRIVATE_EXPORT Sequence : public QV4::ReferenceObject
+struct Q_QML_EXPORT Sequence : public QV4::ReferenceObject
{
V4_OBJECT2(Sequence, QV4::ReferenceObject)
Q_MANAGED_TYPE(V4Sequence)
V4_PROTOTYPE(sequencePrototype)
V4_NEEDS_DESTROY
public:
- static const QMetaType valueMetaType(const Heap::Sequence *p);
static QV4::ReturnedValue virtualGet(
const QV4::Managed *that, PropertyKey id, const Value *receiver, bool *hasProperty);
+ static qint64 virtualGetLength(const Managed *m);
static bool virtualPut(Managed *that, PropertyKey id, const QV4::Value &value, Value *receiver);
static bool virtualDeleteProperty(QV4::Managed *that, PropertyKey id);
static bool virtualIsEqualTo(Managed *that, Managed *other);
@@ -95,6 +101,7 @@ public:
qsizetype size() const;
QVariant at(qsizetype index) const;
+ QVariant shift();
void append(const QVariant &item);
void append(qsizetype num, const QVariant &item);
void replace(qsizetype index, const QVariant &item);
@@ -104,7 +111,6 @@ public:
bool containerPutIndexed(qsizetype index, const QV4::Value &value);
bool containerDeleteIndexedProperty(qsizetype index);
bool containerIsEqualTo(Managed *other);
- bool sort(const FunctionObject *f, const Value *, const Value *argv, int argc);
void *getRawContainerPtr() const;
bool loadReference() const;
bool storeReference();
diff --git a/src/qml/jsruntime/qv4setobject.cpp b/src/qml/jsruntime/qv4setobject.cpp
index 2a4cdc1944..de2c378558 100644
--- a/src/qml/jsruntime/qv4setobject.cpp
+++ b/src/qml/jsruntime/qv4setobject.cpp
@@ -8,19 +8,20 @@
#include "qv4symbol_p.h"
using namespace QV4;
+using namespace Qt::Literals::StringLiterals;
DEFINE_OBJECT_VTABLE(SetCtor);
DEFINE_OBJECT_VTABLE(WeakSetCtor);
DEFINE_OBJECT_VTABLE(SetObject);
-void Heap::WeakSetCtor::init(QV4::ExecutionContext *scope)
+void Heap::WeakSetCtor::init(QV4::ExecutionEngine *engine)
{
- Heap::FunctionObject::init(scope, QStringLiteral("WeakSet"));
+ Heap::FunctionObject::init(engine, QStringLiteral("WeakSet"));
}
-void Heap::SetCtor::init(QV4::ExecutionContext *scope)
+void Heap::SetCtor::init(QV4::ExecutionEngine *engine)
{
- Heap::FunctionObject::init(scope, QStringLiteral("Set"));
+ Heap::FunctionObject::init(engine, QStringLiteral("Set"));
}
ReturnedValue WeakSetCtor::construct(const FunctionObject *f, const Value *argv, int argc, const Value *newTarget, bool isWeak)
@@ -37,7 +38,7 @@ ReturnedValue WeakSetCtor::construct(const FunctionObject *f, const Value *argv,
if (argc > 0) {
ScopedValue iterable(scope, argv[0]);
if (!iterable->isUndefined() && !iterable->isNull()) {
- ScopedFunctionObject adder(scope, a->get(ScopedString(scope, scope.engine->newString(QString::fromLatin1("add")))));
+ ScopedFunctionObject adder(scope, a->get(ScopedString(scope, scope.engine->newString(u"add"_s))));
if (!adder)
return scope.engine->throwTypeError();
ScopedObject iter(scope, Runtime::GetIterator::call(scope.engine, iterable, true));
@@ -54,10 +55,8 @@ ReturnedValue WeakSetCtor::construct(const FunctionObject *f, const Value *argv,
return a.asReturnedValue();
adder->call(a, nextValue, 1);
- if (scope.hasException()) {
- ScopedValue falsey(scope, Encode(false));
- return Runtime::IteratorClose::call(scope.engine, iter, falsey);
- }
+ if (scope.hasException())
+ return Runtime::IteratorClose::call(scope.engine, iter);
}
}
}
@@ -105,6 +104,12 @@ ReturnedValue WeakSetPrototype::method_add(const FunctionObject *b, const Value
(!argc || !argv[0].isObject()))
return scope.engine->throwTypeError();
+ QV4::WriteBarrier::markCustom(scope.engine, [&](QV4::MarkStack *ms) {
+ if (scope.engine->memoryManager->gcStateMachine->state <= GCState::FreeWeakSets)
+ return;
+ argv[0].heapObject()->mark(ms);
+ });
+
that->d()->esTable->set(argv[0], Value::undefinedValue());
return that.asReturnedValue();
}
@@ -193,6 +198,10 @@ ReturnedValue SetPrototype::method_add(const FunctionObject *b, const Value *thi
if (!that || that->d()->isWeakSet)
return scope.engine->throwTypeError();
+ QV4::WriteBarrier::markCustom(scope.engine, [&](QV4::MarkStack *ms) {
+ argv[0].heapObject()->mark(ms);
+ });
+
that->d()->esTable->set(argv[0], Value::undefinedValue());
return that.asReturnedValue();
}
diff --git a/src/qml/jsruntime/qv4setobject_p.h b/src/qml/jsruntime/qv4setobject_p.h
index 6ed44b4ed9..118cdebd5a 100644
--- a/src/qml/jsruntime/qv4setobject_p.h
+++ b/src/qml/jsruntime/qv4setobject_p.h
@@ -29,12 +29,12 @@ class ESTable;
namespace Heap {
struct WeakSetCtor : FunctionObject {
- void init(QV4::ExecutionContext *scope);
+ void init(ExecutionEngine *engine);
};
struct SetCtor : WeakSetCtor {
- void init(QV4::ExecutionContext *scope);
+ void init(ExecutionEngine *engine);
};
struct SetObject : Object {
@@ -79,7 +79,7 @@ struct WeakSetPrototype : Object
{
void init(ExecutionEngine *engine, Object *ctor);
- static ReturnedValue method_add(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
+ Q_AUTOTEST_EXPORT static ReturnedValue method_add(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
static ReturnedValue method_delete(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
static ReturnedValue method_has(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
};
@@ -89,7 +89,7 @@ struct SetPrototype : WeakSetPrototype
{
void init(ExecutionEngine *engine, Object *ctor);
- static ReturnedValue method_add(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
+ Q_AUTOTEST_EXPORT static ReturnedValue method_add(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
static ReturnedValue method_clear(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
static ReturnedValue method_delete(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
static ReturnedValue method_entries(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
diff --git a/src/qml/jsruntime/qv4sparsearray_p.h b/src/qml/jsruntime/qv4sparsearray_p.h
index db23c0af75..7da42a4985 100644
--- a/src/qml/jsruntime/qv4sparsearray_p.h
+++ b/src/qml/jsruntime/qv4sparsearray_p.h
@@ -110,8 +110,8 @@ struct Q_QML_EXPORT SparseArray
{
SparseArray();
~SparseArray() {
- if (root())
- freeTree(header.left, alignof(SparseArrayNode));
+ if (SparseArrayNode *n = root())
+ freeTree(n, alignof(SparseArrayNode));
}
SparseArray(const SparseArray &other);
@@ -287,37 +287,45 @@ inline QList<int> SparseArray::keys() const
inline const SparseArrayNode *SparseArray::lowerBound(uint akey) const
{
- const SparseArrayNode *lb = root()->lowerBound(akey);
- if (!lb)
- lb = end();
- return lb;
+ if (SparseArrayNode *n = root()) {
+ if (const SparseArrayNode *lb = n->lowerBound(akey))
+ return lb;
+ }
+
+ return end();
}
inline SparseArrayNode *SparseArray::lowerBound(uint akey)
{
- SparseArrayNode *lb = root()->lowerBound(akey);
- if (!lb)
- lb = end();
- return lb;
+ if (SparseArrayNode *n = root()) {
+ if (SparseArrayNode *lb = n->lowerBound(akey))
+ return lb;
+ }
+
+ return end();
}
inline const SparseArrayNode *SparseArray::upperBound(uint akey) const
{
- const SparseArrayNode *ub = root()->upperBound(akey);
- if (!ub)
- ub = end();
- return ub;
+ if (SparseArrayNode *n = root()) {
+ if (const SparseArrayNode *ub = n->upperBound(akey))
+ return ub;
+ }
+
+ return end();
}
inline SparseArrayNode *SparseArray::upperBound(uint akey)
{
- SparseArrayNode *ub = root()->upperBound(akey);
- if (!ub)
- ub = end();
- return ub;
+ if (SparseArrayNode *n = root()) {
+ if (SparseArrayNode *ub = n->upperBound(akey))
+ return ub;
+ }
+
+ return end();
}
}
diff --git a/src/qml/jsruntime/qv4stackframe.cpp b/src/qml/jsruntime/qv4stackframe.cpp
index e8ff9a89bc..5117e745a0 100644
--- a/src/qml/jsruntime/qv4stackframe.cpp
+++ b/src/qml/jsruntime/qv4stackframe.cpp
@@ -40,7 +40,7 @@ int CppStackFrame::lineNumber() const
{
if (auto *line = lineAndStatement(this))
return line->line;
- return -1;
+ return missingLineNumber();
}
int CppStackFrame::statementNumber() const
@@ -50,6 +50,15 @@ int CppStackFrame::statementNumber() const
return -1;
}
+int CppStackFrame::missingLineNumber() const
+{
+ // Remove the first bit so that we can cast to positive int and negate.
+ // Remove the last bit so that it can't be -1.
+ const int result = -int(quintptr(this) & 0x7ffffffe);
+ Q_ASSERT(result < -1);
+ return result;
+}
+
ReturnedValue QV4::CppStackFrame::thisObject() const
{
if (isJSTypesFrame())
diff --git a/src/qml/jsruntime/qv4stackframe_p.h b/src/qml/jsruntime/qv4stackframe_p.h
index 2777d79c31..f24b0b2433 100644
--- a/src/qml/jsruntime/qv4stackframe_p.h
+++ b/src/qml/jsruntime/qv4stackframe_p.h
@@ -14,6 +14,7 @@
// We mean it.
//
+#include <private/qv4scopedvalue_p.h>
#include <private/qv4context_p.h>
#include <private/qv4enginebase_p.h>
#include <private/qv4calldata_p.h>
@@ -26,7 +27,7 @@ QT_BEGIN_NAMESPACE
namespace QV4 {
struct CppStackFrame;
-struct Q_QML_PRIVATE_EXPORT CppStackFrameBase
+struct Q_QML_EXPORT CppStackFrameBase
{
enum class Kind : quint8 { JS, Meta };
@@ -35,6 +36,8 @@ struct Q_QML_PRIVATE_EXPORT CppStackFrameBase
int originalArgumentsCount;
int instructionPointer;
+ QT_WARNING_PUSH
+ QT_WARNING_DISABLE_MSVC(4201) // nonstandard extension used: nameless struct/union
union {
struct {
Value *savedStackTop;
@@ -57,11 +60,12 @@ struct Q_QML_PRIVATE_EXPORT CppStackFrameBase
bool returnValueIsUndefined;
};
};
+ QT_WARNING_POP
Kind kind;
};
-struct Q_QML_PRIVATE_EXPORT CppStackFrame : protected CppStackFrameBase
+struct Q_QML_EXPORT CppStackFrame : protected CppStackFrameBase
{
// We want to have those public but we can't declare them as public without making the struct
// non-standard layout. So we have this other struct with "using" in between.
@@ -83,6 +87,8 @@ struct Q_QML_PRIVATE_EXPORT CppStackFrame : protected CppStackFrameBase
int lineNumber() const;
int statementNumber() const;
+ int missingLineNumber() const;
+
CppStackFrame *parentFrame() const { return parent; }
void setParentFrame(CppStackFrame *parentFrame) { parent = parentFrame; }
@@ -116,7 +122,7 @@ protected:
}
};
-struct Q_QML_PRIVATE_EXPORT MetaTypesStackFrame : public CppStackFrame
+struct Q_QML_EXPORT MetaTypesStackFrame : public CppStackFrame
{
using CppStackFrame::push;
using CppStackFrame::pop;
@@ -141,6 +147,9 @@ struct Q_QML_PRIVATE_EXPORT MetaTypesStackFrame : public CppStackFrame
const QMetaType *argTypes() const { return metaTypes + 1; }
void **argv() const { return returnAndArgs + 1; }
+ const QMetaType *returnAndArgTypes() const { return metaTypes; }
+ void **returnAndArgValues() const { return returnAndArgs; }
+
QObject *thisObject() const { return CppStackFrameBase::thisObject; }
ExecutionContext *context() const { return CppStackFrameBase::context; }
@@ -152,7 +161,7 @@ struct Q_QML_PRIVATE_EXPORT MetaTypesStackFrame : public CppStackFrame
}
};
-struct Q_QML_PRIVATE_EXPORT JSTypesStackFrame : public CppStackFrame
+struct Q_QML_EXPORT JSTypesStackFrame : public CppStackFrame
{
using CppStackFrame::jsFrame;
@@ -285,6 +294,30 @@ inline ExecutionContext *CppStackFrame::context() const
return static_cast<const MetaTypesStackFrame *>(this)->context();
}
+struct ScopedStackFrame
+{
+ ScopedStackFrame(const Scope &scope, ExecutionContext *context)
+ : engine(scope.engine)
+ {
+ if (auto currentFrame = engine->currentStackFrame) {
+ frame.init(currentFrame->v4Function, nullptr, context, nullptr, nullptr, 0);
+ frame.instructionPointer = currentFrame->instructionPointer;
+ } else {
+ frame.init(nullptr, nullptr, context, nullptr, nullptr, 0);
+ }
+ frame.push(engine);
+ }
+
+ ~ScopedStackFrame()
+ {
+ frame.pop(engine);
+ }
+
+private:
+ ExecutionEngine *engine = nullptr;
+ MetaTypesStackFrame frame;
+};
+
Q_STATIC_ASSERT(sizeof(CppStackFrame) == sizeof(JSTypesStackFrame));
Q_STATIC_ASSERT(sizeof(CppStackFrame) == sizeof(MetaTypesStackFrame));
Q_STATIC_ASSERT(std::is_standard_layout_v<CppStackFrame>);
diff --git a/src/qml/jsruntime/qv4string.cpp b/src/qml/jsruntime/qv4string.cpp
index 118382d5dc..8b5594b43b 100644
--- a/src/qml/jsruntime/qv4string.cpp
+++ b/src/qml/jsruntime/qv4string.cpp
@@ -170,23 +170,38 @@ bool Heap::String::startsWithUpper() const
void Heap::String::append(const String *data, QChar *ch)
{
- std::vector<const String *> worklist;
+ // in-order visitation with explicit stack
+ // where leaf nodes are "real" strings that get appended to ch
+
+ enum StatusTag : bool { NotVisited, Visited };
+ using Pointer = QTaggedPointer<const String, StatusTag>;
+
+ std::vector<Pointer> worklist;
worklist.reserve(32);
- worklist.push_back(data);
+ worklist.push_back(Pointer(data));
while (!worklist.empty()) {
- const String *item = worklist.back();
- worklist.pop_back();
+ Pointer item = worklist.back();
+ if (item.tag() == Visited) {
+ Q_ASSERT(item->subtype == StringType_AddedString);
+ const ComplexString *cs = static_cast<const ComplexString *>(item.data());
+ worklist.pop_back();
+ worklist.push_back(Pointer(cs->right));
+ continue;
+ }
if (item->subtype == StringType_AddedString) {
- const ComplexString *cs = static_cast<const ComplexString *>(item);
- worklist.push_back(cs->right);
- worklist.push_back(cs->left);
+ // we need to keep the node in the worklist, as we still need to handle "right"
+ worklist.back().setTag(Visited);
+ const ComplexString *cs = static_cast<const ComplexString *>(item.data());
+ worklist.push_back(Pointer(cs->left));
} else if (item->subtype == StringType_SubString) {
- const ComplexString *cs = static_cast<const ComplexString *>(item);
+ worklist.pop_back();
+ const ComplexString *cs = static_cast<const ComplexString *>(item.data());
memcpy(ch, cs->left->toQString().constData() + cs->from, cs->len*sizeof(QChar));
ch += cs->len;
} else {
+ worklist.pop_back();
memcpy(static_cast<void *>(ch), item->text().data(), item->text().size * sizeof(QChar));
ch += item->text().size;
}
diff --git a/src/qml/jsruntime/qv4string_p.h b/src/qml/jsruntime/qv4string_p.h
index 78727bdcc3..370baadc98 100644
--- a/src/qml/jsruntime/qv4string_p.h
+++ b/src/qml/jsruntime/qv4string_p.h
@@ -29,7 +29,7 @@ struct PropertyKey;
namespace Heap {
-struct Q_QML_PRIVATE_EXPORT StringOrSymbol : Base
+struct Q_QML_EXPORT StringOrSymbol : Base
{
enum StringType {
StringType_Symbol,
@@ -76,7 +76,7 @@ struct Q_QML_PRIVATE_EXPORT StringOrSymbol : Base
}
};
-struct Q_QML_PRIVATE_EXPORT String : StringOrSymbol {
+struct Q_QML_EXPORT String : StringOrSymbol {
static void markObjects(Heap::Base *that, MarkStack *markStack);
const VTable *vtable() const {
@@ -136,7 +136,7 @@ int String::length() const {
}
-struct Q_QML_PRIVATE_EXPORT StringOrSymbol : public Managed {
+struct Q_QML_EXPORT StringOrSymbol : public Managed {
V4_MANAGED(StringOrSymbol, Managed)
V4_NEEDS_DESTROY
enum {
@@ -155,7 +155,7 @@ public:
}
};
-struct Q_QML_PRIVATE_EXPORT String : public StringOrSymbol {
+struct Q_QML_EXPORT String : public StringOrSymbol {
V4_MANAGED(String, StringOrSymbol)
Q_MANAGED_TYPE(String)
V4_INTERNALCLASS(String)
@@ -195,6 +195,12 @@ struct Q_QML_PRIVATE_EXPORT String : public StringOrSymbol {
return calculateHashValue(ch, end, subtype);
}
+ static uint createHashValueDisallowingArrayIndex(const QChar *ch, int length, uint *subtype)
+ {
+ const QChar *end = ch + length;
+ return calculateHashValue<String::DisallowArrayIndex>(ch, end, subtype);
+ }
+
static uint createHashValue(const char *ch, int length, uint *subtype)
{
const char *end = ch + length;
@@ -208,15 +214,19 @@ protected:
static qint64 virtualGetLength(const Managed *m);
public:
- template <typename T>
+ enum IndicesBehavior {Default, DisallowArrayIndex};
+ template <IndicesBehavior Behavior = Default, typename T>
static inline uint calculateHashValue(const T *ch, const T* end, uint *subtype)
{
// array indices get their number as hash value
- uint h = stringToArrayIndex(ch, end);
- if (h != UINT_MAX) {
- if (subtype)
- *subtype = Heap::StringOrSymbol::StringType_ArrayIndex;
- return h;
+ uint h = UINT_MAX;
+ if constexpr (Behavior != DisallowArrayIndex) {
+ h = stringToArrayIndex(ch, end);
+ if (h != UINT_MAX) {
+ if (subtype)
+ *subtype = Heap::StringOrSymbol::StringType_ArrayIndex;
+ return h;
+ }
}
while (ch < end) {
diff --git a/src/qml/jsruntime/qv4stringobject.cpp b/src/qml/jsruntime/qv4stringobject.cpp
index 98bb6347d3..5f3d833f33 100644
--- a/src/qml/jsruntime/qv4stringobject.cpp
+++ b/src/qml/jsruntime/qv4stringobject.cpp
@@ -30,6 +30,7 @@
#endif
using namespace QV4;
+using namespace Qt::Literals::StringLiterals;
DEFINE_OBJECT_VTABLE(StringObject);
@@ -130,9 +131,9 @@ PropertyAttributes StringObject::virtualGetOwnProperty(const Managed *m, Propert
DEFINE_OBJECT_VTABLE(StringCtor);
-void Heap::StringCtor::init(QV4::ExecutionContext *scope)
+void Heap::StringCtor::init(QV4::ExecutionEngine *engine)
{
- Heap::FunctionObject::init(scope, QStringLiteral("String"));
+ Heap::FunctionObject::init(engine, QStringLiteral("String"));
}
ReturnedValue StringCtor::virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *newTarget)
@@ -599,7 +600,7 @@ ReturnedValue StringPrototype::method_padEnd(const FunctionObject *f, const Valu
double maxLen = argv[0].toInteger();
if (maxLen <= s->d()->length())
return s->asReturnedValue();
- QString fillString = (argc > 1 && !argv[1].isUndefined()) ? argv[1].toQString() : QString::fromLatin1(" ");
+ QString fillString = (argc > 1 && !argv[1].isUndefined()) ? argv[1].toQString() : u" "_s;
if (v4->hasException)
return Encode::undefined();
@@ -638,7 +639,7 @@ ReturnedValue StringPrototype::method_padStart(const FunctionObject *f, const Va
double maxLen = argv[0].toInteger();
if (maxLen <= s->d()->length())
return s->asReturnedValue();
- QString fillString = (argc > 1 && !argv[1].isUndefined()) ? argv[1].toQString() : QString::fromLatin1(" ");
+ QString fillString = (argc > 1 && !argv[1].isUndefined()) ? argv[1].toQString() : u" "_s;
if (v4->hasException)
return Encode::undefined();
diff --git a/src/qml/jsruntime/qv4stringobject_p.h b/src/qml/jsruntime/qv4stringobject_p.h
index 451a989ef4..73c2bd7b34 100644
--- a/src/qml/jsruntime/qv4stringobject_p.h
+++ b/src/qml/jsruntime/qv4stringobject_p.h
@@ -44,7 +44,7 @@ DECLARE_HEAP_OBJECT(StringObject, Object) {
};
struct StringCtor : FunctionObject {
- void init(QV4::ExecutionContext *scope);
+ void init(QV4::ExecutionEngine *engine);
};
}
diff --git a/src/qml/jsruntime/qv4symbol.cpp b/src/qml/jsruntime/qv4symbol.cpp
index 585038937e..85ef57f680 100644
--- a/src/qml/jsruntime/qv4symbol.cpp
+++ b/src/qml/jsruntime/qv4symbol.cpp
@@ -16,12 +16,12 @@ void Heap::Symbol::init(const QString &s)
Q_ASSERT(s.at(0) == QLatin1Char('@'));
QString desc(s);
StringOrSymbol::init(desc.data_ptr());
- identifier = PropertyKey::fromStringOrSymbol(this);
+ identifier = PropertyKey::fromStringOrSymbol(internalClass->engine, this);
}
-void Heap::SymbolCtor::init(QV4::ExecutionContext *scope)
+void Heap::SymbolCtor::init(QV4::ExecutionEngine *engine)
{
- Heap::FunctionObject::init(scope, QStringLiteral("Symbol"));
+ Heap::FunctionObject::init(engine, QStringLiteral("Symbol"));
}
void Heap::SymbolObject::init(const QV4::Symbol *s)
diff --git a/src/qml/jsruntime/qv4symbol_p.h b/src/qml/jsruntime/qv4symbol_p.h
index e56510bd69..29a0189b69 100644
--- a/src/qml/jsruntime/qv4symbol_p.h
+++ b/src/qml/jsruntime/qv4symbol_p.h
@@ -25,7 +25,7 @@ namespace QV4 {
namespace Heap {
struct SymbolCtor : FunctionObject {
- void init(QV4::ExecutionContext *scope);
+ void init(ExecutionEngine *engine);
};
struct Symbol : StringOrSymbol {
diff --git a/src/qml/jsruntime/qv4typedarray.cpp b/src/qml/jsruntime/qv4typedarray.cpp
index 3027cdf94f..9c752f43bb 100644
--- a/src/qml/jsruntime/qv4typedarray.cpp
+++ b/src/qml/jsruntime/qv4typedarray.cpp
@@ -236,9 +236,9 @@ const TypedArrayOperations operations[NTypedArrayTypes] = {
};
-void Heap::TypedArrayCtor::init(QV4::ExecutionContext *scope, TypedArray::Type t)
+void Heap::TypedArrayCtor::init(QV4::ExecutionEngine *engine, TypedArray::Type t)
{
- Heap::FunctionObject::init(scope, QLatin1String(operations[t].name));
+ Heap::FunctionObject::init(engine, QLatin1String(operations[t].name));
type = t;
}
@@ -373,7 +373,7 @@ ReturnedValue TypedArrayCtor::virtualCallAsConstructor(const FunctionObject *f,
uint elementSize = operations[that->d()->type].bytesPerElement;
size_t bufferSize;
- if (mul_overflow(size_t(l), size_t(elementSize), &bufferSize))
+ if (qMulOverflow(size_t(l), size_t(elementSize), &bufferSize))
return scope.engine->throwRangeError(QLatin1String("new TypedArray: invalid length"));
Scoped<ArrayBuffer> newBuffer(scope, scope.engine->newArrayBuffer(bufferSize));
if (scope.hasException())
@@ -762,8 +762,6 @@ ReturnedValue IntrinsicTypedArrayPrototype::method_fill(const FunctionObject *b,
fin = static_cast<uint>(std::min(relativeEnd, dlen));
}
- double val = argc ? argv[0].toNumber() : std::numeric_limits<double>::quiet_NaN();
- Value value = Value::fromDouble(val);
if (scope.hasException() || v->hasDetachedArrayData())
return scope.engine->throwTypeError();
@@ -771,6 +769,14 @@ ReturnedValue IntrinsicTypedArrayPrototype::method_fill(const FunctionObject *b,
uint bytesPerElement = v->bytesPerElement();
uint byteOffset = v->byteOffset();
+ Value value;
+ if (!argc)
+ value.setDouble(std::numeric_limits<double>::quiet_NaN());
+ else if (argv[0].isNumber())
+ value = argv[0];
+ else
+ value.setDouble(argv[0].toNumber());
+
while (k < fin) {
v->d()->type->write(data + byteOffset + k * bytesPerElement, value);
k++;
@@ -1680,14 +1686,13 @@ ReturnedValue IntrinsicTypedArrayCtor::method_from(const FunctionObject *f, cons
forever {
// Here we calculate the length of the iterable range.
if (iterableLength > (static_cast<qint64>(1) << 53) - 1) {
- ScopedValue falsey(scope, Encode(false));
ScopedValue error(scope, scope.engine->throwTypeError());
- return Runtime::IteratorClose::call(scope.engine, lengthIterator, falsey);
+ return Runtime::IteratorClose::call(scope.engine, lengthIterator);
}
// Retrieve the next value. If the iteration ends, we're done here.
done = Value::fromReturnedValue(Runtime::IteratorNext::call(scope.engine, lengthIterator, nextValue));
if (scope.hasException())
- return Runtime::IteratorClose::call(scope.engine, lengthIterator, Value::fromBoolean(false));
+ return Runtime::IteratorClose::call(scope.engine, lengthIterator);
if (done->toBoolean()) {
break;
}
@@ -1720,21 +1725,21 @@ ReturnedValue IntrinsicTypedArrayCtor::method_from(const FunctionObject *f, cons
for (qint64 k = 0; k < iterableLength; ++k) {
done = Value::fromReturnedValue(Runtime::IteratorNext::call(scope.engine, iterator, nextValue));
if (scope.hasException())
- return Runtime::IteratorClose::call(scope.engine, iterator, Value::fromBoolean(false));
+ return Runtime::IteratorClose::call(scope.engine, iterator);
if (mapfn) {
mapArguments[0] = *nextValue;
mapArguments[1] = Value::fromDouble(k);
mappedValue = mapfn->call(thisArg, mapArguments, 2);
if (scope.hasException())
- return Runtime::IteratorClose::call(scope.engine, iterator, Value::fromBoolean(false));
+ return Runtime::IteratorClose::call(scope.engine, iterator);
} else {
mappedValue = *nextValue;
}
a->put(k, mappedValue);
if (scope.hasException())
- return Runtime::IteratorClose::call(scope.engine, iterator, Value::fromBoolean(false));
+ return Runtime::IteratorClose::call(scope.engine, iterator);
}
return a.asReturnedValue();
} else {
diff --git a/src/qml/jsruntime/qv4typedarray_p.h b/src/qml/jsruntime/qv4typedarray_p.h
index 0284dceb7b..50db9610c7 100644
--- a/src/qml/jsruntime/qv4typedarray_p.h
+++ b/src/qml/jsruntime/qv4typedarray_p.h
@@ -90,7 +90,7 @@ struct IntrinsicTypedArrayCtor : FunctionObject {
};
struct TypedArrayCtor : FunctionObject {
- void init(QV4::ExecutionContext *scope, TypedArray::Type t);
+ void init(ExecutionEngine *engine, TypedArray::Type t);
TypedArray::Type type;
};
@@ -106,7 +106,7 @@ struct TypedArrayPrototype : Object {
}
-struct Q_QML_PRIVATE_EXPORT TypedArray : Object
+struct Q_QML_EXPORT TypedArray : Object
{
V4_OBJECT2(TypedArray, Object)
@@ -141,8 +141,6 @@ struct IntrinsicTypedArrayCtor: FunctionObject
{
V4_OBJECT2(IntrinsicTypedArrayCtor, FunctionObject)
- static constexpr VTable::Call virtualCall = nullptr;
-
static ReturnedValue method_of(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
static ReturnedValue method_from(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
};
diff --git a/src/qml/jsruntime/qv4urlobject.cpp b/src/qml/jsruntime/qv4urlobject.cpp
index 8b6db439e2..89c8b9cda2 100644
--- a/src/qml/jsruntime/qv4urlobject.cpp
+++ b/src/qml/jsruntime/qv4urlobject.cpp
@@ -18,9 +18,9 @@ DEFINE_OBJECT_VTABLE(UrlSearchParamsObject);
DEFINE_OBJECT_VTABLE(UrlSearchParamsCtor);
-void Heap::UrlCtor::init(QV4::ExecutionContext *scope)
+void Heap::UrlCtor::init(QV4::ExecutionEngine *engine)
{
- Heap::FunctionObject::init(scope, QLatin1String("URL"));
+ Heap::FunctionObject::init(engine, QLatin1String("URL"));
}
void UrlPrototype::init(ExecutionEngine *engine, Object *ctor)
@@ -124,14 +124,14 @@ void UrlObject::setUrl(const QUrl &url)
{
d()->hash.set(engine(), engine()->newString(url.fragment()));
d()->hostname.set(engine(), engine()->newString(url.host()));
- d()->href.set(engine(), engine()->newString(url.toString()));
+ d()->href.set(engine(), engine()->newString(url.toString(QUrl::ComponentFormattingOptions(QUrl::ComponentFormattingOption::FullyEncoded))));
d()->password.set(engine(), engine()->newString(url.password()));
d()->pathname.set(engine(), engine()->newString(url.path()));
d()->port.set(engine(),
engine()->newString(url.port() == -1 ? QLatin1String("")
: QString::number(url.port())));
d()->protocol.set(engine(), engine()->newString(url.scheme() + QLatin1Char(':')));
- d()->search.set(engine(), engine()->newString(url.query()));
+ d()->search.set(engine(), engine()->newString(url.query(QUrl::ComponentFormattingOptions(QUrl::ComponentFormattingOption::FullyEncoded))));
d()->username.set(engine(), engine()->newString(url.userName()));
updateOrigin();
@@ -242,6 +242,18 @@ bool UrlObject::setUsername(QString username)
return true;
}
+QString UrlObject::search() const
+{
+ auto url = QUrl(href());
+ if (auto url = QUrl(href()); !url.hasQuery() || url.query().isEmpty())
+ return QLatin1String("");
+
+ constexpr auto options = QUrl::ComponentFormattingOption::EncodeSpaces
+ | QUrl::ComponentFormattingOption::EncodeUnicode
+ | QUrl::ComponentFormattingOption::EncodeReserved;
+ return u'?' + url.query(options);
+}
+
QUrl UrlObject::toQUrl() const
{
return QUrl(href());
@@ -670,6 +682,7 @@ ReturnedValue UrlPrototype::method_getSearchParams(const FunctionObject *b, cons
Scoped<UrlSearchParamsObject> usp(scope, v4->newUrlSearchParamsObject());
+ usp->setUrlObject(thisObject->as<UrlObject>());
usp->initializeParams(r->search());
return usp->asReturnedValue();
@@ -737,9 +750,9 @@ ReturnedValue UrlCtor::virtualCallAsConstructor(const FunctionObject *that, cons
}
-void Heap::UrlSearchParamsCtor::init(QV4::ExecutionContext *scope)
+void Heap::UrlSearchParamsCtor::init(QV4::ExecutionEngine *engine)
{
- Heap::FunctionObject::init(scope, QLatin1String("URLSearchParams"));
+ Heap::FunctionObject::init(engine, QLatin1String("URLSearchParams"));
}
void UrlSearchParamsPrototype::init(ExecutionEngine *engine, Object *ctor)
@@ -960,6 +973,11 @@ void UrlSearchParamsObject::setParams(QList<QStringList> params)
d()->values.set(engine(), values);
}
+void UrlSearchParamsObject::setUrlObject(const UrlObject *url)
+{
+ d()->url.set(engine(), url->d());
+}
+
void UrlSearchParamsObject::append(Heap::String *name, Heap::String *value)
{
Scope scope(engine());
@@ -1008,6 +1026,25 @@ QList<QStringList> UrlSearchParamsObject::params() const
return result;
}
+Heap::UrlObject *UrlSearchParamsObject::urlObject() const
+{
+ return d()->url.get();
+}
+
+QString UrlSearchParamsObject::searchString() const
+{
+ QString search = QLatin1String("");
+ auto params = this->params();
+ auto len = params.size();
+ for (int i = 0; i < len; ++i) {
+ const QStringList &param = params[i];
+ search += param[0] + QLatin1Char('=') + param[1];
+ if (i != len - 1)
+ search += QLatin1Char('&');
+ }
+ return search;
+}
+
int UrlSearchParamsObject::length() const
{
auto *arrayObject = d()->params.get();
@@ -1251,12 +1288,7 @@ ReturnedValue UrlSearchParamsPrototype::method_delete(const FunctionObject *b, c
return Encode::undefined();
QList<QStringList> params = o->params();
-
- auto to_remove = std::remove_if(params.begin(), params.end(), [&name](QStringList pair) {
- return pair[0] == name;
- });
-
- params.erase(to_remove, params.end());
+ params.removeIf([&name](const auto &pair) { return pair.at(0) == name; });
o->setParams(params);
@@ -1335,6 +1367,10 @@ ReturnedValue UrlSearchParamsPrototype::method_set(const FunctionObject *b, cons
o->setParams(params);
+ Scoped<UrlObject> scopedUrlObject(scope, o->d()->url.get());
+ if (scopedUrlObject)
+ scopedUrlObject->setSearch(o->searchString());
+
return Encode::undefined();
}
diff --git a/src/qml/jsruntime/qv4urlobject_p.h b/src/qml/jsruntime/qv4urlobject_p.h
index e4a3ba073b..b3b76e1158 100644
--- a/src/qml/jsruntime/qv4urlobject_p.h
+++ b/src/qml/jsruntime/qv4urlobject_p.h
@@ -47,15 +47,16 @@ DECLARE_HEAP_OBJECT(UrlObject, Object)
struct UrlCtor : FunctionObject
{
- void init(QV4::ExecutionContext *scope);
+ void init(ExecutionEngine *engine);
};
-// clang-format on
+// clang-format off
#define UrlSearchParamsObjectMembers(class, Member) \
Member(class, Pointer, ArrayObject *, params) \
Member(class, Pointer, ArrayObject *, keys) \
- Member(class, Pointer, ArrayObject *, values)
-// clang-format off
+ Member(class, Pointer, ArrayObject *, values) \
+ Member(class, Pointer, UrlObject *, url)
+// clang-format on
DECLARE_HEAP_OBJECT(UrlSearchParamsObject, Object)
{
@@ -65,7 +66,7 @@ DECLARE_HEAP_OBJECT(UrlSearchParamsObject, Object)
struct UrlSearchParamsCtor : FunctionObject
{
- void init(QV4::ExecutionContext *scope);
+ void init(ExecutionEngine *engine);
};
}
@@ -101,7 +102,7 @@ struct UrlObject : Object
QString protocol() const { return toQString(d()->protocol); }
bool setProtocol(QString protocol);
- QString search() const { return QLatin1String("?") + toQString(d()->search); }
+ Q_QML_AUTOTEST_EXPORT QString search() const;
bool setSearch(QString search);
QString username() const { return toQString(d()->username); }
@@ -212,6 +213,10 @@ struct UrlSearchParamsObject : Object
QList<QStringList> params() const;
void setParams(QList<QStringList> params);
+ Heap::UrlObject *urlObject() const;
+ void setUrlObject(const UrlObject *url);
+
+ QString searchString() const;
QString nameAt(int index) const;
Heap::String * nameAtRaw(int index) const;
diff --git a/src/qml/jsruntime/qv4value_p.h b/src/qml/jsruntime/qv4value_p.h
index 259334e12c..eaa57a36aa 100644
--- a/src/qml/jsruntime/qv4value_p.h
+++ b/src/qml/jsruntime/qv4value_p.h
@@ -34,7 +34,7 @@ namespace Heap {
struct Base;
}
-struct Q_QML_PRIVATE_EXPORT Value : public StaticValue
+struct Q_QML_EXPORT Value : public StaticValue
{
using ManagedPtr = Managed *;
@@ -309,7 +309,10 @@ inline bool Value::isObject() const
inline bool Value::isFunctionObject() const
{
HeapBasePtr b = heapObject();
- return b && b->internalClass->vtable->isFunctionObject;
+ if (!b)
+ return false;
+ const VTable *vtable = b->internalClass->vtable;
+ return vtable->call || vtable->callAsConstructor;
}
inline bool Value::isPrimitive() const
@@ -442,19 +445,6 @@ struct ValueArray {
return values;
}
- void insertData(EngineBase *e, uint index, Value v) {
- for (uint i = size - 1; i > index; --i) {
- values[i] = values[i - 1];
- }
- set(e, index, v);
- }
- void removeData(EngineBase *e, uint index, int n = 1) {
- Q_UNUSED(e);
- for (uint i = index; i < size - n; ++i) {
- values[i] = values[i + n];
- }
- }
-
void mark(MarkStack *markStack) {
for (Value *v = values, *end = values + alloc; v < end; ++v)
v->mark(markStack);
diff --git a/src/qml/jsruntime/qv4variantobject.cpp b/src/qml/jsruntime/qv4variantobject.cpp
index ccea9cac2a..62e21a120c 100644
--- a/src/qml/jsruntime/qv4variantobject.cpp
+++ b/src/qml/jsruntime/qv4variantobject.cpp
@@ -131,7 +131,8 @@ ReturnedValue VariantPrototype::method_valueOf(const FunctionObject *b, const Va
return Encode(v.toBool());
default:
if (QMetaType(v.metaType()).flags() & QMetaType::IsEnumeration)
- return Encode(v.toInt());
+ if (v.metaType().sizeOf() <= qsizetype(sizeof(int)))
+ return Encode(v.toInt());
if (v.canConvert<double>())
return Encode(v.toDouble());
if (v.canConvert<int>())
diff --git a/src/qml/jsruntime/qv4variantobject_p.h b/src/qml/jsruntime/qv4variantobject_p.h
index 1a4e0eeaa7..f2394ce9a2 100644
--- a/src/qml/jsruntime/qv4variantobject_p.h
+++ b/src/qml/jsruntime/qv4variantobject_p.h
@@ -54,7 +54,7 @@ private:
}
-struct VariantObject : Object
+struct Q_QML_EXPORT VariantObject : Object
{
V4_OBJECT2(VariantObject, Object)
V4_PROTOTYPE(variantPrototype)
diff --git a/src/qml/jsruntime/qv4vme_moth.cpp b/src/qml/jsruntime/qv4vme_moth.cpp
index 41fbc9c182..096b9a6299 100644
--- a/src/qml/jsruntime/qv4vme_moth.cpp
+++ b/src/qml/jsruntime/qv4vme_moth.cpp
@@ -35,6 +35,9 @@
#undef COUNT_INSTRUCTIONS
+Q_TRACE_POINT(qtqml, QQmlV4_function_call_entry, const QV4::ExecutionEngine *engine, const QString &function, const QString &fileName, int line, int column)
+Q_TRACE_POINT(qtqml, QQmlV4_function_call_exit)
+
enum { ShowWhenDeoptimiationHappens = 0 };
extern "C" {
@@ -392,6 +395,21 @@ static bool compareEqualInt(QV4::Value &accumulator, QV4::Value lhs, int rhs)
} \
} while (false)
+struct AOTCompiledMetaMethod
+{
+public:
+ AOTCompiledMetaMethod(const Function::AOTCompiledFunction *aotCompiledFunction)
+ : aotCompiledFunction(aotCompiledFunction)
+ {}
+
+ int parameterCount() const { return aotCompiledFunction->types.size() - 1; }
+ QMetaType returnMetaType() const { return aotCompiledFunction->types[0]; }
+ QMetaType parameterMetaType(int i) const { return aotCompiledFunction->types[i + 1]; }
+
+private:
+ const Function::AOTCompiledFunction *aotCompiledFunction = nullptr;
+};
+
void VME::exec(MetaTypesStackFrame *frame, ExecutionEngine *engine)
{
qt_v4ResolvePendingBreakpointsHook();
@@ -402,103 +420,31 @@ void VME::exec(MetaTypesStackFrame *frame, ExecutionEngine *engine)
ExecutionEngineCallDepthRecorder executionEngineCallDepthRecorder(engine);
Function *function = frame->v4Function;
- Q_ASSERT(function->typedFunction);
+ Q_ASSERT(function->aotCompiledCode);
Q_TRACE_SCOPE(QQmlV4_function_call, engine, function->name()->toQString(),
function->executableCompilationUnit()->fileName(),
function->compiledFunction->location.line(),
function->compiledFunction->location.column());
Profiling::FunctionCallProfiler profiler(engine, function); // start execution profiling
- const qsizetype numFunctionArguments = function->typedFunction->argumentTypes.size();
-
- Q_ALLOCA_DECLARE(void *, transformedArguments);
- for (qsizetype i = 0; i < numFunctionArguments; ++i) {
- const QMetaType argumentType = function->typedFunction->argumentTypes[i];
- if (frame->argc() > i && argumentType == frame->argTypes()[i])
- continue;
-
- if (transformedArguments == nullptr) {
- Q_ALLOCA_ASSIGN(void *, transformedArguments, numFunctionArguments * sizeof(void *));
- memcpy(transformedArguments, frame->argv(), frame->argc() * sizeof(void *));
- }
-
- if (argumentType.sizeOf() == 0) {
- transformedArguments[i] = nullptr;
- continue;
- }
-
- Q_ALLOCA_VAR(void, arg, argumentType.sizeOf());
-
- if (argumentType == QMetaType::fromType<QVariant>()) {
- if (frame->argc() > i)
- new (arg) QVariant(frame->argTypes()[i], frame->argv()[i]);
- else
- new (arg) QVariant();
- } else if (argumentType == QMetaType::fromType<QJSPrimitiveValue>()) {
- if (frame->argc() > i)
- new (arg) QJSPrimitiveValue(frame->argTypes()[i], frame->argv()[i]);
- else
- new (arg) QJSPrimitiveValue();
- } else {
- argumentType.construct(arg);
- if (frame->argc() > i)
- QMetaType::convert(frame->argTypes()[i], frame->argv()[i], argumentType, arg);
- }
-
- transformedArguments[i] = arg;
- }
-
- const QMetaType returnType = function->typedFunction->returnType;
- const QMetaType frameReturn = frame->returnType();
- Q_ALLOCA_DECLARE(void, transformedResult);
- if (frame->returnValue() && returnType != frameReturn) {
- if (returnType.sizeOf() > 0)
- Q_ALLOCA_ASSIGN(void, transformedResult, returnType.sizeOf());
- else
- transformedResult = frame; // Some non-null marker value
- }
-
- QQmlPrivate::AOTCompiledContext aotContext;
- if (auto context = QV4::ExecutionEngine::qmlContext(frame->context()->d())) {
- QV4::Heap::QQmlContextWrapper *wrapper = static_cast<Heap::QmlContext *>(context)->qml();
- aotContext.qmlScopeObject = wrapper->scopeObject;
- aotContext.qmlContext = wrapper->context;
- }
-
- aotContext.engine = engine->jsEngine();
- aotContext.compilationUnit = function->executableCompilationUnit();
-
- function->typedFunction->functionPtr(
- &aotContext, transformedResult ? transformedResult : frame->returnValue(),
- transformedArguments ? transformedArguments : frame->argv());
-
- if (transformedResult) {
- // Shortcut the common case of the AOT function returning a more generic QObject pointer
- // that we need to QObject-cast. No need to construct or destruct anything in that case.
- if ((frameReturn.flags() & QMetaType::PointerToQObject)
- && (returnType.flags() & QMetaType::PointerToQObject)) {
- QObject *resultObj = *static_cast<QObject **>(transformedResult);
- *static_cast<QObject **>(frame->returnValue())
- = (resultObj && resultObj->metaObject()->inherits(frameReturn.metaObject()))
- ? resultObj
- : nullptr;
- } else {
- // Convert needs a pre-constructed target.
- frameReturn.construct(frame->returnValue());
- QMetaType::convert(returnType, transformedResult, frameReturn, frame->returnValue());
- returnType.destruct(transformedResult);
+ const AOTCompiledMetaMethod method(&function->aotCompiledFunction);
+ QV4::coerceAndCall(
+ engine, &method, frame->returnAndArgValues(),
+ frame->returnAndArgTypes(), frame->argc(),
+ [frame, engine, function](void **argv, int argc) {
+ Q_UNUSED(argc);
+
+ QQmlPrivate::AOTCompiledContext aotContext;
+ if (auto context = QV4::ExecutionEngine::qmlContext(frame->context()->d())) {
+ QV4::Heap::QQmlContextWrapper *wrapper = static_cast<Heap::QmlContext *>(context)->qml();
+ aotContext.qmlScopeObject = wrapper->scopeObject;
+ aotContext.qmlContext = wrapper->context;
}
- }
- if (transformedArguments) {
- for (int i = 0; i < numFunctionArguments; ++i) {
- void *arg = transformedArguments[i];
- if (arg == nullptr)
- continue;
- if (i >= frame->argc() || arg != frame->argv()[i])
- function->typedFunction->argumentTypes[i].destruct(arg);
- }
- }
+ aotContext.engine = engine->jsEngine();
+ aotContext.compilationUnit = function->executableCompilationUnit();
+ function->aotCompiledCode(&aotContext, argv);
+ });
}
ReturnedValue VME::exec(JSTypesStackFrame *frame, ExecutionEngine *engine)
@@ -736,7 +682,6 @@ QV4::ReturnedValue VME::interpret(JSTypesStackFrame *frame, ExecutionEngine *eng
QV4::Lookup *l = function->executableCompilationUnit()->runtimeLookups + index;
if (accumulator.isNullOrUndefined()) {
- acc = Encode::undefined();
code += offset;
} else {
acc = l->getter(l, engine, accumulator);
@@ -801,7 +746,8 @@ QV4::ReturnedValue VME::interpret(JSTypesStackFrame *frame, ExecutionEngine *eng
MOTH_BEGIN_INSTR(IteratorNextForYieldStar)
STORE_ACC();
acc = Runtime::IteratorNextForYieldStar::call(engine, accumulator, STACK_VALUE(iterator), &STACK_VALUE(object));
- CHECK_EXCEPTION;
+ if (ACC.toBoolean())
+ code += offset;
MOTH_END_INSTR(IteratorNextForYieldStar)
MOTH_BEGIN_INSTR(CallValue)
@@ -848,15 +794,19 @@ QV4::ReturnedValue VME::interpret(JSTypesStackFrame *frame, ExecutionEngine *eng
// ok to have the value on the stack here
Value f = Value::fromReturnedValue(l->getter(l, engine, STACK_VALUE(base)));
- if (Q_UNLIKELY(!f.isFunctionObject())) {
- QString message = QStringLiteral("Property '%1' of object %2 is not a function")
- .arg(engine->currentStackFrame->v4Function->compilationUnit->runtimeStrings[l->nameIndex]->toQString())
+ if (Q_LIKELY(f.isFunctionObject())) {
+ acc = static_cast<FunctionObject &>(f).call(stack + base, stack + argv, argc);
+ } else if (QmlSignalHandler *handler = f.as<QmlSignalHandler>()) {
+ acc = handler->call(stack + base, stack + argv, argc);
+ } else {
+ const QString message = QStringLiteral("Property '%1' of object %2 is not a function")
+ .arg(engine->currentStackFrame->v4Function->compilationUnit
+ ->runtimeStrings[l->nameIndex]->toQString())
.arg(STACK_VALUE(base).toQStringNoThrow());
acc = engine->throwTypeError(message);
goto handleUnwind;
}
- acc = static_cast<FunctionObject &>(f).call(stack + base, stack + argv, argc);
CHECK_EXCEPTION;
MOTH_END_INSTR(CallPropertyLookup)
@@ -1011,15 +961,14 @@ QV4::ReturnedValue VME::interpret(JSTypesStackFrame *frame, ExecutionEngine *eng
STORE_IP();
STORE_ACC();
acc = Runtime::IteratorNext::call(engine, accumulator, &STACK_VALUE(value));
- STACK_VALUE(done) = acc;
- CHECK_EXCEPTION;
+ if (ACC.toBoolean())
+ code += offset;
MOTH_END_INSTR(IteratorNext)
MOTH_BEGIN_INSTR(IteratorClose)
STORE_IP();
STORE_ACC();
- acc = Runtime::IteratorClose::call(engine, accumulator, STACK_VALUE(done));
- CHECK_EXCEPTION;
+ acc = Runtime::IteratorClose::call(engine, accumulator);
MOTH_END_INSTR(IteratorClose)
MOTH_BEGIN_INSTR(DestructureRestElement)
diff --git a/src/qml/jsruntime/qv4vtable_p.h b/src/qml/jsruntime/qv4vtable_p.h
index 7db1e71704..0532fdc32d 100644
--- a/src/qml/jsruntime/qv4vtable_p.h
+++ b/src/qml/jsruntime/qv4vtable_p.h
@@ -24,7 +24,7 @@ namespace QV4 {
struct Lookup;
-struct Q_QML_PRIVATE_EXPORT OwnPropertyKeyIterator {
+struct Q_QML_EXPORT OwnPropertyKeyIterator {
virtual ~OwnPropertyKeyIterator() = 0;
virtual PropertyKey next(const Object *o, Property *p = nullptr, PropertyAttributes *attrs = nullptr) = 0;
};
@@ -64,7 +64,7 @@ struct VTable
quint8 isExecutionContext;
quint8 isString;
quint8 isObject;
- quint8 isFunctionObject;
+ quint8 isTailCallable;
quint8 isErrorObject;
quint8 isArrayData;
quint8 isStringOrSymbol;
@@ -110,20 +110,38 @@ template<class Class>
constexpr VTable::CallWithMetaTypes vtableMetaTypesCallEntry()
{
// If Class overrides virtualCallWithMetaTypes, return that.
- // Otherwise, if it overrides virtualCall, return nullptr so that we convert calls.
+ // Otherwise, if it overrides virtualCall, return convertAndCall.
// Otherwise, just return whatever the base class had.
- // A simple != is not considered constexpr, so we have to jump through some hoops.
- if constexpr (!std::is_same_v<
- VTableCallWithMetaTypesWrapper<Class::virtualCallWithMetaTypes>,
- VTableCallWithMetaTypesWrapper<Class::SuperClass::virtualCallWithMetaTypes>>) {
- return Class::virtualCallWithMetaTypes;
- }
-
- if constexpr (!std::is_same_v<
- VTableCallWrapper<Class::virtualCall>,
- VTableCallWrapper<Class::SuperClass::virtualCall>>) {
- return nullptr;
+ // A simple == on methods is not considered constexpr, so we have to jump through some hoops.
+
+ static_assert(
+ std::is_same_v<
+ VTableCallWithMetaTypesWrapper<Class::virtualCallWithMetaTypes>,
+ VTableCallWithMetaTypesWrapper<Class::SuperClass::virtualCallWithMetaTypes>>
+ || !std::is_same_v<
+ VTableCallWithMetaTypesWrapper<Class::virtualCallWithMetaTypes>,
+ VTableCallWithMetaTypesWrapper<nullptr>>,
+ "You mustn't override virtualCallWithMetaTypes with nullptr");
+
+ static_assert(
+ std::is_same_v<
+ VTableCallWithMetaTypesWrapper<Class::virtualConvertAndCall>,
+ VTableCallWithMetaTypesWrapper<Class::SuperClass::virtualConvertAndCall>>
+ || !std::is_same_v<
+ VTableCallWithMetaTypesWrapper<Class::virtualConvertAndCall>,
+ VTableCallWithMetaTypesWrapper<nullptr>>,
+ "You mustn't override virtualConvertAndCall with nullptr");
+
+ if constexpr (
+ std::is_same_v<
+ VTableCallWithMetaTypesWrapper<Class::virtualCallWithMetaTypes>,
+ VTableCallWithMetaTypesWrapper<Class::SuperClass::virtualCallWithMetaTypes>>
+ && !std::is_same_v<
+ VTableCallWrapper<Class::virtualCall>,
+ VTableCallWrapper<Class::SuperClass::virtualCall>>) {
+ // Converting from metatypes to JS signature is easy.
+ return Class::virtualConvertAndCall;
}
return Class::virtualCallWithMetaTypes;
@@ -133,21 +151,27 @@ template<class Class>
constexpr VTable::Call vtableJsTypesCallEntry()
{
// If Class overrides virtualCall, return that.
- // Otherwise, if it overrides virtualCallWithMetaTypes, return nullptr so that we convert calls.
+ // Otherwise, if it overrides virtualCallWithMetaTypes, fail.
+ // (We cannot determine the target types to call virtualCallWithMetaTypes in that case)
// Otherwise, just return whatever the base class had.
- // A simple != is not considered constexpr, so we have to jump through some hoops.
- if constexpr (!std::is_same_v<
- VTableCallWrapper<Class::virtualCall>,
- VTableCallWrapper<Class::SuperClass::virtualCall>>) {
- return Class::virtualCall;
- }
+ // A simple == on methods is not considered constexpr, so we have to jump through some hoops.
- if constexpr (!std::is_same_v<
- VTableCallWithMetaTypesWrapper<Class::virtualCallWithMetaTypes>,
- VTableCallWithMetaTypesWrapper<Class::SuperClass::virtualCallWithMetaTypes>>) {
- return nullptr;
- }
+ static_assert(
+ !std::is_same_v<
+ VTableCallWrapper<Class::virtualCall>,
+ VTableCallWrapper<Class::SuperClass::virtualCall>>
+ || std::is_same_v<
+ VTableCallWithMetaTypesWrapper<Class::virtualCallWithMetaTypes>,
+ VTableCallWithMetaTypesWrapper<Class::SuperClass::virtualCallWithMetaTypes>>,
+ "If you override virtualCallWithMetaTypes, override virtualCall, too");
+
+ static_assert(
+ std::is_same_v<
+ VTableCallWrapper<Class::virtualCall>,
+ VTableCallWrapper<Class::SuperClass::virtualCall>>
+ || VTableCallWrapper<Class::virtualCall>::c != nullptr,
+ "You mustn't override virtualCall with nullptr");
return Class::virtualCall;
}
@@ -174,6 +198,7 @@ protected:
static constexpr VTable::Call virtualCall = nullptr;
static constexpr VTable::CallAsConstructor virtualCallAsConstructor = nullptr;
static constexpr VTable::CallWithMetaTypes virtualCallWithMetaTypes = nullptr;
+ static constexpr VTable::CallWithMetaTypes virtualConvertAndCall = nullptr;
static constexpr VTable::ResolveLookupGetter virtualResolveLookupGetter = nullptr;
static constexpr VTable::ResolveLookupSetter virtualResolveLookupSetter = nullptr;
@@ -196,7 +221,7 @@ protected:
classname::IsExecutionContext, \
classname::IsString, \
classname::IsObject, \
- classname::IsFunctionObject, \
+ classname::IsTailCallable, \
classname::IsErrorObject, \
classname::IsArrayData, \
classname::IsStringOrSymbol, \