diff options
Diffstat (limited to 'src/qml/jsruntime')
80 files changed, 3076 insertions, 2152 deletions
diff --git a/src/qml/jsruntime/jsruntime.pri b/src/qml/jsruntime/jsruntime.pri index 038b23e8d3..dcc04cbd54 100644 --- a/src/qml/jsruntime/jsruntime.pri +++ b/src/qml/jsruntime/jsruntime.pri @@ -6,7 +6,6 @@ SOURCES += \ $$PWD/qv4engine.cpp \ $$PWD/qv4context.cpp \ $$PWD/qv4persistent.cpp \ - $$PWD/qv4debugging.cpp \ $$PWD/qv4lookup.cpp \ $$PWD/qv4identifier.cpp \ $$PWD/qv4identifiertable.cpp \ @@ -39,12 +38,12 @@ SOURCES += \ $$PWD/qv4sequenceobject.cpp \ $$PWD/qv4include.cpp \ $$PWD/qv4qobjectwrapper.cpp \ - $$PWD/qv4vme_moth.cpp \ - $$PWD/qv4profiling.cpp \ $$PWD/qv4arraybuffer.cpp \ $$PWD/qv4typedarray.cpp \ $$PWD/qv4dataview.cpp +!contains(QT_CONFIG, no-qml-debug): SOURCES += $$PWD/qv4profiling.cpp + HEADERS += \ $$PWD/qv4global_p.h \ $$PWD/qv4engine_p.h \ @@ -88,17 +87,24 @@ HEADERS += \ $$PWD/qv4sequenceobject_p.h \ $$PWD/qv4include_p.h \ $$PWD/qv4qobjectwrapper_p.h \ - $$PWD/qv4vme_moth_p.h \ $$PWD/qv4profiling_p.h \ $$PWD/qv4arraybuffer_p.h \ $$PWD/qv4typedarray_p.h \ $$PWD/qv4dataview_p.h +qtConfig(qml-interpreter) { + HEADERS += \ + $$PWD/qv4vme_moth_p.h + SOURCES += \ + $$PWD/qv4vme_moth.cpp +} + } HEADERS += \ $$PWD/qv4runtime_p.h \ + $$PWD/qv4runtimeapi_p.h \ $$PWD/qv4value_p.h \ $$PWD/qv4string_p.h \ $$PWD/qv4value_p.h @@ -111,3 +117,7 @@ SOURCES += \ valgrind { DEFINES += V4_USE_VALGRIND } + +heaptrack { + DEFINES += V4_USE_HEAPTRACK +} diff --git a/src/qml/jsruntime/qv4argumentsobject.cpp b/src/qml/jsruntime/qv4argumentsobject.cpp index 94f418cae1..0dfdf25158 100644 --- a/src/qml/jsruntime/qv4argumentsobject.cpp +++ b/src/qml/jsruntime/qv4argumentsobject.cpp @@ -45,10 +45,11 @@ using namespace QV4; DEFINE_OBJECT_VTABLE(ArgumentsObject); -Heap::ArgumentsObject::ArgumentsObject(QV4::CallContext *context) - : context(context->d()) - , fullyCreated(false) +void Heap::ArgumentsObject::init(QV4::CallContext *context) { + Object::init(); + fullyCreated = false; + this->context = context->d(); Q_ASSERT(vtable() == QV4::ArgumentsObject::staticVTable()); ExecutionEngine *v4 = context->d()->engine; @@ -134,7 +135,7 @@ bool ArgumentsObject::defineOwnProperty(ExecutionEngine *engine, uint index, con ScopedCallData callData(scope, 1); callData->thisObject = this->asReturnedValue(); callData->args[0] = desc->value; - setter->call(callData); + setter->call(scope, callData); if (attrs.isWritable()) { setArrayAttributes(index, mapAttrs); @@ -203,33 +204,35 @@ PropertyAttributes ArgumentsObject::queryIndexed(const Managed *m, uint index) DEFINE_OBJECT_VTABLE(ArgumentsGetterFunction); -ReturnedValue ArgumentsGetterFunction::call(const Managed *getter, CallData *callData) +void ArgumentsGetterFunction::call(const Managed *getter, Scope &scope, CallData *callData) { ExecutionEngine *v4 = static_cast<const ArgumentsGetterFunction *>(getter)->engine(); - Scope scope(v4); Scoped<ArgumentsGetterFunction> g(scope, static_cast<const ArgumentsGetterFunction *>(getter)); Scoped<ArgumentsObject> o(scope, callData->thisObject.as<ArgumentsObject>()); - if (!o) - return v4->throwTypeError(); + if (!o) { + scope.result = v4->throwTypeError(); + return; + } Q_ASSERT(g->index() < static_cast<unsigned>(o->context()->callData->argc)); - return o->context()->callData->args[g->index()].asReturnedValue(); + scope.result = o->context()->callData->args[g->index()]; } DEFINE_OBJECT_VTABLE(ArgumentsSetterFunction); -ReturnedValue ArgumentsSetterFunction::call(const Managed *setter, CallData *callData) +void ArgumentsSetterFunction::call(const Managed *setter, Scope &scope, CallData *callData) { ExecutionEngine *v4 = static_cast<const ArgumentsSetterFunction *>(setter)->engine(); - Scope scope(v4); Scoped<ArgumentsSetterFunction> s(scope, static_cast<const ArgumentsSetterFunction *>(setter)); Scoped<ArgumentsObject> o(scope, callData->thisObject.as<ArgumentsObject>()); - if (!o) - return v4->throwTypeError(); + if (!o) { + scope.result = v4->throwTypeError(); + return; + } Q_ASSERT(s->index() < static_cast<unsigned>(o->context()->callData->argc)); o->context()->callData->args[s->index()] = callData->argc ? callData->args[0].asReturnedValue() : Encode::undefined(); - return Encode::undefined(); + scope.result = Encode::undefined(); } void ArgumentsObject::markObjects(Heap::Base *that, ExecutionEngine *e) diff --git a/src/qml/jsruntime/qv4argumentsobject_p.h b/src/qml/jsruntime/qv4argumentsobject_p.h index eeedcaf995..37a8d0a94a 100644 --- a/src/qml/jsruntime/qv4argumentsobject_p.h +++ b/src/qml/jsruntime/qv4argumentsobject_p.h @@ -60,12 +60,12 @@ namespace QV4 { namespace Heap { struct ArgumentsGetterFunction : FunctionObject { - inline ArgumentsGetterFunction(QV4::ExecutionContext *scope, uint index); + inline void init(QV4::ExecutionContext *scope, uint index); uint index; }; struct ArgumentsSetterFunction : FunctionObject { - inline ArgumentsSetterFunction(QV4::ExecutionContext *scope, uint index); + inline void init(QV4::ExecutionContext *scope, uint index); uint index; }; @@ -75,7 +75,7 @@ struct ArgumentsObject : Object { CalleePropertyIndex = 1, CallerPropertyIndex = 3 }; - ArgumentsObject(QV4::CallContext *context); + void init(QV4::CallContext *context); Pointer<CallContext> context; bool fullyCreated; Pointer<MemberData> mappedArguments; @@ -88,14 +88,14 @@ struct ArgumentsGetterFunction: FunctionObject V4_OBJECT2(ArgumentsGetterFunction, FunctionObject) uint index() const { return d()->index; } - static ReturnedValue call(const Managed *that, CallData *d); + static void call(const Managed *that, Scope &scope, CallData *d); }; -inline -Heap::ArgumentsGetterFunction::ArgumentsGetterFunction(QV4::ExecutionContext *scope, uint index) - : Heap::FunctionObject(scope) - , index(index) +inline void +Heap::ArgumentsGetterFunction::init(QV4::ExecutionContext *scope, uint index) { + Heap::FunctionObject::init(scope); + this->index = index; } struct ArgumentsSetterFunction: FunctionObject @@ -103,14 +103,14 @@ struct ArgumentsSetterFunction: FunctionObject V4_OBJECT2(ArgumentsSetterFunction, FunctionObject) uint index() const { return d()->index; } - static ReturnedValue call(const Managed *that, CallData *callData); + static void call(const Managed *that, Scope &scope, CallData *callData); }; -inline -Heap::ArgumentsSetterFunction::ArgumentsSetterFunction(QV4::ExecutionContext *scope, uint index) - : Heap::FunctionObject(scope) - , index(index) +inline void +Heap::ArgumentsSetterFunction::init(QV4::ExecutionContext *scope, uint index) { + Heap::FunctionObject::init(scope); + this->index = index; } diff --git a/src/qml/jsruntime/qv4arraybuffer.cpp b/src/qml/jsruntime/qv4arraybuffer.cpp index d170bde0e8..23075aa78c 100644 --- a/src/qml/jsruntime/qv4arraybuffer.cpp +++ b/src/qml/jsruntime/qv4arraybuffer.cpp @@ -46,34 +46,39 @@ using namespace QV4; DEFINE_OBJECT_VTABLE(ArrayBufferCtor); DEFINE_OBJECT_VTABLE(ArrayBuffer); -Heap::ArrayBufferCtor::ArrayBufferCtor(QV4::ExecutionContext *scope) - : Heap::FunctionObject(scope, QStringLiteral("ArrayBuffer")) +void Heap::ArrayBufferCtor::init(QV4::ExecutionContext *scope) { + Heap::FunctionObject::init(scope, QStringLiteral("ArrayBuffer")); } -ReturnedValue ArrayBufferCtor::construct(const Managed *m, CallData *callData) +void ArrayBufferCtor::construct(const Managed *m, Scope &scope, CallData *callData) { ExecutionEngine *v4 = static_cast<const Object *>(m)->engine(); - Scope scope(v4); ScopedValue l(scope, callData->argument(0)); double dl = l->toInteger(); - if (v4->hasException) - return Encode::undefined(); + if (v4->hasException) { + scope.result = Encode::undefined(); + return; + } uint len = (uint)qBound(0., dl, (double)UINT_MAX); - if (len != dl) - return v4->throwRangeError(QLatin1String("ArrayBuffer constructor: invalid length")); + if (len != dl) { + scope.result = v4->throwRangeError(QLatin1String("ArrayBuffer constructor: invalid length")); + return; + } Scoped<ArrayBuffer> a(scope, v4->newArrayBuffer(len)); - if (scope.engine->hasException) - return Encode::undefined(); - return a.asReturnedValue(); + if (scope.engine->hasException) { + scope.result = Encode::undefined(); + } else { + scope.result = a->asReturnedValue(); + } } -ReturnedValue ArrayBufferCtor::call(const Managed *that, CallData *callData) +void ArrayBufferCtor::call(const Managed *that, Scope &scope, CallData *callData) { - return construct(that, callData); + construct(that, scope, callData); } ReturnedValue ArrayBufferCtor::method_isView(CallContext *ctx) @@ -89,8 +94,9 @@ ReturnedValue ArrayBufferCtor::method_isView(CallContext *ctx) } -Heap::ArrayBuffer::ArrayBuffer(size_t length) +void Heap::ArrayBuffer::init(size_t length) { + Object::init(); data = QTypedArrayData<char>::allocate(length + 1); if (!data) { data = 0; @@ -101,16 +107,18 @@ Heap::ArrayBuffer::ArrayBuffer(size_t length) memset(data->data(), 0, length + 1); } -Heap::ArrayBuffer::ArrayBuffer(const QByteArray& array) - : data(const_cast<QByteArray&>(array).data_ptr()) +void Heap::ArrayBuffer::init(const QByteArray& array) { + Object::init(); + data = const_cast<QByteArray&>(array).data_ptr(); data->ref.ref(); } -Heap::ArrayBuffer::~ArrayBuffer() +void Heap::ArrayBuffer::destroy() { if (!data->ref.deref()) QTypedArrayData<char>::deallocate(data); + Object::destroy(); } QByteArray ArrayBuffer::asByteArray() const @@ -149,6 +157,7 @@ void ArrayBufferPrototype::init(ExecutionEngine *engine, Object *ctor) defineDefaultProperty(engine->id_constructor(), (o = ctor)); defineAccessorProperty(QStringLiteral("byteLength"), method_get_byteLength, 0); defineDefaultProperty(QStringLiteral("slice"), method_slice, 2); + defineDefaultProperty(QStringLiteral("toString"), method_toString, 0); } ReturnedValue ArrayBufferPrototype::method_get_byteLength(CallContext *ctx) @@ -184,7 +193,8 @@ ReturnedValue ArrayBufferPrototype::method_slice(CallContext *ctx) ScopedCallData callData(scope, 1); double newLen = qMax(final - first, 0.); callData->args[0] = QV4::Encode(newLen); - QV4::Scoped<ArrayBuffer> newBuffer(scope, constructor->construct(callData)); + constructor->construct(scope, callData); + QV4::Scoped<ArrayBuffer> newBuffer(scope, scope.result.asReturnedValue()); if (!newBuffer || newBuffer->d()->data->size < (int)newLen) return scope.engine->throwTypeError(); @@ -192,3 +202,12 @@ ReturnedValue ArrayBufferPrototype::method_slice(CallContext *ctx) return newBuffer.asReturnedValue(); } + +ReturnedValue ArrayBufferPrototype::method_toString(CallContext *ctx) +{ + Scope scope(ctx); + Scoped<ArrayBuffer> a(scope, ctx->thisObject()); + if (!a) + return Encode::undefined(); + return Encode(ctx->engine()->newString(QString::fromUtf8(a->asByteArray()))); +} diff --git a/src/qml/jsruntime/qv4arraybuffer_p.h b/src/qml/jsruntime/qv4arraybuffer_p.h index 0413d2f28d..bc56d1ea31 100644 --- a/src/qml/jsruntime/qv4arraybuffer_p.h +++ b/src/qml/jsruntime/qv4arraybuffer_p.h @@ -60,13 +60,13 @@ namespace QV4 { namespace Heap { struct ArrayBufferCtor : FunctionObject { - ArrayBufferCtor(QV4::ExecutionContext *scope); + void init(QV4::ExecutionContext *scope); }; struct Q_QML_PRIVATE_EXPORT ArrayBuffer : Object { - ArrayBuffer(size_t length); - ArrayBuffer(const QByteArray& array); - ~ArrayBuffer(); + void init(size_t length); + void init(const QByteArray& array); + void destroy(); QTypedArrayData<char> *data; uint byteLength() const { return data->size; } @@ -78,8 +78,8 @@ struct ArrayBufferCtor: FunctionObject { V4_OBJECT2(ArrayBufferCtor, FunctionObject) - static ReturnedValue construct(const Managed *m, CallData *callData); - static ReturnedValue call(const Managed *that, CallData *callData); + static void construct(const Managed *m, Scope &scope, CallData *callData); + static void call(const Managed *that, Scope &scope, CallData *callData); static ReturnedValue method_isView(CallContext *ctx); @@ -106,6 +106,7 @@ struct ArrayBufferPrototype: Object static ReturnedValue method_get_byteLength(CallContext *ctx); static ReturnedValue method_slice(CallContext *ctx); + static ReturnedValue method_toString(CallContext *ctx); }; diff --git a/src/qml/jsruntime/qv4arraydata.cpp b/src/qml/jsruntime/qv4arraydata.cpp index aa64ae1411..bfeb3d4699 100644 --- a/src/qml/jsruntime/qv4arraydata.cpp +++ b/src/qml/jsruntime/qv4arraydata.cpp @@ -147,13 +147,13 @@ void ArrayData::realloc(Object *o, Type newType, uint requested, bool enforceAtt Scoped<ArrayData> newData(scope); if (newType < Heap::ArrayData::Sparse) { Heap::SimpleArrayData *n = scope.engine->memoryManager->allocManaged<SimpleArrayData>(size); - new (n) Heap::SimpleArrayData; + n->init(); n->offset = 0; n->len = d ? d->d()->len : 0; newData = n; } else { Heap::SparseArrayData *n = scope.engine->memoryManager->allocManaged<SparseArrayData>(size); - new (n) Heap::SparseArrayData; + n->init(); newData = n; } newData->setAlloc(alloc); @@ -691,7 +691,7 @@ bool ArrayElementLessThan::operator()(Value v1, Value v2) const callData->thisObject = Primitive::undefinedValue(); callData->args[0] = v1; callData->args[1] = v2; - result = Runtime::callValue(scope.engine, m_comparefn, callData); + result = scope.engine->runtime.callValue(scope.engine, m_comparefn, callData); return result->toNumber() < 0; } diff --git a/src/qml/jsruntime/qv4arraydata_p.h b/src/qml/jsruntime/qv4arraydata_p.h index 8ad4704227..24b948f01e 100644 --- a/src/qml/jsruntime/qv4arraydata_p.h +++ b/src/qml/jsruntime/qv4arraydata_p.h @@ -133,6 +133,7 @@ struct ArrayData : public Base { } }; +V4_ASSERT_IS_TRIVIAL(ArrayData) struct SimpleArrayData : public ArrayData { uint mappedIndex(uint index) const { return (index + offset) % alloc; } @@ -152,9 +153,13 @@ struct SimpleArrayData : public ArrayData { return attrs ? attrs[i] : Attr_Data; } }; +V4_ASSERT_IS_TRIVIAL(SimpleArrayData) struct SparseArrayData : public ArrayData { - inline ~SparseArrayData(); + void destroy() { + delete sparse; + ArrayData::destroy(); + } uint mappedIndex(uint index) const { SparseArrayNode *n = sparse->findNode(index); @@ -285,11 +290,6 @@ struct Q_QML_EXPORT SparseArrayData : public ArrayData namespace Heap { -inline SparseArrayData::~SparseArrayData() -{ - delete sparse; -} - void ArrayData::getProperty(uint index, Property *p, PropertyAttributes *attrs) { Property *pd = getProperty(index); diff --git a/src/qml/jsruntime/qv4arrayobject.cpp b/src/qml/jsruntime/qv4arrayobject.cpp index 555eb964c1..659ede7552 100644 --- a/src/qml/jsruntime/qv4arrayobject.cpp +++ b/src/qml/jsruntime/qv4arrayobject.cpp @@ -49,23 +49,24 @@ using namespace QV4; DEFINE_OBJECT_VTABLE(ArrayCtor); -Heap::ArrayCtor::ArrayCtor(QV4::ExecutionContext *scope) - : Heap::FunctionObject(scope, QStringLiteral("Array")) +void Heap::ArrayCtor::init(QV4::ExecutionContext *scope) { + Heap::FunctionObject::init(scope, QStringLiteral("Array")); } -ReturnedValue ArrayCtor::construct(const Managed *m, CallData *callData) +void ArrayCtor::construct(const Managed *m, Scope &scope, CallData *callData) { ExecutionEngine *v4 = static_cast<const ArrayCtor *>(m)->engine(); - Scope scope(v4); ScopedArrayObject a(scope, v4->newArrayObject()); uint len; if (callData->argc == 1 && callData->args[0].isNumber()) { bool ok; len = callData->args[0].asArrayLength(&ok); - if (!ok) - return v4->throwRangeError(callData->args[0]); + if (!ok) { + scope.result = v4->throwRangeError(callData->args[0]); + return; + } if (len < 0x1000) a->arrayReserve(len); @@ -76,12 +77,12 @@ ReturnedValue ArrayCtor::construct(const Managed *m, CallData *callData) } a->setArrayLengthUnchecked(len); - return a.asReturnedValue(); + scope.result = a.asReturnedValue(); } -ReturnedValue ArrayCtor::call(const Managed *that, CallData *callData) +void ArrayCtor::call(const Managed *that, Scope &scope, CallData *callData) { - return construct(that, callData); + construct(that, scope, callData); } void ArrayPrototype::init(ExecutionEngine *engine, Object *ctor) @@ -132,7 +133,8 @@ ReturnedValue ArrayPrototype::method_toString(CallContext *ctx) if (!!f) { ScopedCallData d(scope, 0); d->thisObject = ctx->thisObject(); - return f->call(d); + f->call(scope, d); + return scope.result.asReturnedValue(); } return ObjectPrototype::method_toString(ctx); } @@ -707,7 +709,6 @@ ReturnedValue ArrayPrototype::method_every(CallContext *ctx) ScopedCallData callData(scope, 3); callData->args[2] = instance; callData->thisObject = ctx->argument(1); - ScopedValue r(scope); ScopedValue v(scope); bool ok = true; @@ -719,8 +720,8 @@ ReturnedValue ArrayPrototype::method_every(CallContext *ctx) callData->args[0] = v; callData->args[1] = Primitive::fromDouble(k); - r = callback->call(callData); - ok = r->toBoolean(); + callback->call(scope, callData); + ok = scope.result.toBoolean(); } return Encode(ok); } @@ -743,7 +744,6 @@ ReturnedValue ArrayPrototype::method_some(CallContext *ctx) callData->args[2] = instance; ScopedValue v(scope); - ScopedValue r(scope); for (uint k = 0; k < len; ++k) { bool exists; v = instance->getIndexed(k, &exists); @@ -752,8 +752,8 @@ ReturnedValue ArrayPrototype::method_some(CallContext *ctx) callData->args[0] = v; callData->args[1] = Primitive::fromDouble(k); - r = callback->call(callData); - if (r->toBoolean()) + callback->call(scope, callData); + if (scope.result.toBoolean()) return Encode(true); } return Encode(false); @@ -785,7 +785,7 @@ ReturnedValue ArrayPrototype::method_forEach(CallContext *ctx) callData->args[0] = v; callData->args[1] = Primitive::fromDouble(k); - callback->call(callData); + callback->call(scope, callData); } return Encode::undefined(); } @@ -807,7 +807,6 @@ ReturnedValue ArrayPrototype::method_map(CallContext *ctx) a->arrayReserve(len); a->setArrayLengthUnchecked(len); - ScopedValue mapped(scope); ScopedCallData callData(scope, 3); callData->thisObject = ctx->argument(1); callData->args[2] = instance; @@ -821,8 +820,8 @@ ReturnedValue ArrayPrototype::method_map(CallContext *ctx) callData->args[0] = v; callData->args[1] = Primitive::fromDouble(k); - mapped = callback->call(callData); - a->arraySet(k, mapped); + callback->call(scope, callData); + a->arraySet(k, scope.result); } return a.asReturnedValue(); } @@ -843,7 +842,6 @@ ReturnedValue ArrayPrototype::method_filter(CallContext *ctx) ScopedArrayObject a(scope, ctx->d()->engine->newArrayObject()); a->arrayReserve(len); - ScopedValue selected(scope); ScopedCallData callData(scope, 3); callData->thisObject = ctx->argument(1); callData->args[2] = instance; @@ -859,8 +857,8 @@ ReturnedValue ArrayPrototype::method_filter(CallContext *ctx) callData->args[0] = v; callData->args[1] = Primitive::fromDouble(k); - selected = callback->call(callData); - if (selected->toBoolean()) { + callback->call(scope, callData); + if (scope.result.toBoolean()) { a->arraySet(to, v); ++to; } @@ -882,17 +880,16 @@ ReturnedValue ArrayPrototype::method_reduce(CallContext *ctx) return ctx->engine()->throwTypeError(); uint k = 0; - ScopedValue acc(scope); ScopedValue v(scope); if (ctx->argc() > 1) { - acc = ctx->argument(1); + scope.result = ctx->argument(1); } else { bool kPresent = false; while (k < len && !kPresent) { v = instance->getIndexed(k, &kPresent); if (kPresent) - acc = v; + scope.result = v; ++k; } if (!kPresent) @@ -901,21 +898,21 @@ ReturnedValue ArrayPrototype::method_reduce(CallContext *ctx) ScopedCallData callData(scope, 4); callData->thisObject = Primitive::undefinedValue(); - callData->args[0] = acc; + callData->args[0] = scope.result; callData->args[3] = instance; while (k < len) { bool kPresent; v = instance->getIndexed(k, &kPresent); if (kPresent) { - callData->args[0] = acc; + callData->args[0] = scope.result; callData->args[1] = v; callData->args[2] = Primitive::fromDouble(k); - acc = callback->call(callData); + callback->call(scope, callData); } ++k; } - return acc->asReturnedValue(); + return scope.result.asReturnedValue(); } ReturnedValue ArrayPrototype::method_reduceRight(CallContext *ctx) @@ -938,16 +935,15 @@ ReturnedValue ArrayPrototype::method_reduceRight(CallContext *ctx) } uint k = len; - ScopedValue acc(scope); ScopedValue v(scope); if (ctx->argc() > 1) { - acc = ctx->argument(1); + scope.result = ctx->argument(1); } else { bool kPresent = false; while (k > 0 && !kPresent) { v = instance->getIndexed(k - 1, &kPresent); if (kPresent) - acc = v; + scope.result = v; --k; } if (!kPresent) @@ -962,13 +958,13 @@ ReturnedValue ArrayPrototype::method_reduceRight(CallContext *ctx) bool kPresent; v = instance->getIndexed(k - 1, &kPresent); if (kPresent) { - callData->args[0] = acc; + callData->args[0] = scope.result; callData->args[1] = v; callData->args[2] = Primitive::fromDouble(k - 1); - acc = callback->call(callData); + callback->call(scope, callData); } --k; } - return acc->asReturnedValue(); + return scope.result.asReturnedValue(); } diff --git a/src/qml/jsruntime/qv4arrayobject_p.h b/src/qml/jsruntime/qv4arrayobject_p.h index bae5f9e0da..9a05bb8681 100644 --- a/src/qml/jsruntime/qv4arrayobject_p.h +++ b/src/qml/jsruntime/qv4arrayobject_p.h @@ -61,7 +61,7 @@ namespace QV4 { namespace Heap { struct ArrayCtor : FunctionObject { - ArrayCtor(QV4::ExecutionContext *scope); + void init(QV4::ExecutionContext *scope); }; } @@ -70,8 +70,8 @@ struct ArrayCtor: FunctionObject { V4_OBJECT2(ArrayCtor, FunctionObject) - static ReturnedValue construct(const Managed *m, CallData *callData); - static ReturnedValue call(const Managed *that, CallData *callData); + static void construct(const Managed *m, Scope &scope, CallData *callData); + static void call(const Managed *that, Scope &scope, CallData *callData); }; struct ArrayPrototype: ArrayObject diff --git a/src/qml/jsruntime/qv4booleanobject.cpp b/src/qml/jsruntime/qv4booleanobject.cpp index d9da7d7754..8047993266 100644 --- a/src/qml/jsruntime/qv4booleanobject.cpp +++ b/src/qml/jsruntime/qv4booleanobject.cpp @@ -45,22 +45,21 @@ using namespace QV4; DEFINE_OBJECT_VTABLE(BooleanCtor); DEFINE_OBJECT_VTABLE(BooleanObject); -Heap::BooleanCtor::BooleanCtor(QV4::ExecutionContext *scope) - : Heap::FunctionObject(scope, QStringLiteral("Boolean")) +void Heap::BooleanCtor::init(QV4::ExecutionContext *scope) { + Heap::FunctionObject::init(scope, QStringLiteral("Boolean")); } -ReturnedValue BooleanCtor::construct(const Managed *m, CallData *callData) +void BooleanCtor::construct(const Managed *, Scope &scope, CallData *callData) { - Scope scope(static_cast<const BooleanCtor *>(m)->engine()); bool n = callData->argc ? callData->args[0].toBoolean() : false; - return Encode(scope.engine->newBooleanObject(n)); + scope.result = Encode(scope.engine->newBooleanObject(n)); } -ReturnedValue BooleanCtor::call(const Managed *, CallData *callData) +void BooleanCtor::call(const Managed *, Scope &scope, CallData *callData) { bool value = callData->argc ? callData->args[0].toBoolean() : 0; - return Encode(value); + scope.result = Encode(value); } void BooleanPrototype::init(ExecutionEngine *engine, Object *ctor) diff --git a/src/qml/jsruntime/qv4booleanobject_p.h b/src/qml/jsruntime/qv4booleanobject_p.h index eedafa6126..4c2f3c09e7 100644 --- a/src/qml/jsruntime/qv4booleanobject_p.h +++ b/src/qml/jsruntime/qv4booleanobject_p.h @@ -61,7 +61,7 @@ namespace QV4 { namespace Heap { struct BooleanCtor : FunctionObject { - BooleanCtor(QV4::ExecutionContext *scope); + void init(QV4::ExecutionContext *scope); }; } @@ -70,8 +70,8 @@ struct BooleanCtor: FunctionObject { V4_OBJECT2(BooleanCtor, FunctionObject) - static ReturnedValue construct(const Managed *, CallData *callData); - static ReturnedValue call(const Managed *that, CallData *callData); + static void construct(const Managed *, Scope &scope, CallData *callData); + static void call(const Managed *that, Scope &scope, CallData *callData); }; struct BooleanPrototype: BooleanObject diff --git a/src/qml/jsruntime/qv4context.cpp b/src/qml/jsruntime/qv4context.cpp index 97b3e26a26..390a5e7d7a 100644 --- a/src/qml/jsruntime/qv4context.cpp +++ b/src/qml/jsruntime/qv4context.cpp @@ -61,8 +61,9 @@ Heap::CallContext *ExecutionContext::newCallContext(const FunctionObject *functi { Q_ASSERT(function->function()); - Heap::CallContext *c = d()->engine->memoryManager->allocManaged<CallContext>(requiredMemoryForExecutionContect(function, callData->argc)); - new (c) Heap::CallContext(d()->engine, Heap::ExecutionContext::Type_CallContext); + Heap::CallContext *c = d()->engine->memoryManager->allocManaged<CallContext>( + requiredMemoryForExecutionContect(function, callData->argc)); + c->init(d()->engine, Heap::ExecutionContext::Type_CallContext); c->function = function->d(); @@ -73,6 +74,7 @@ Heap::CallContext *ExecutionContext::newCallContext(const FunctionObject *functi c->compilationUnit = function->function()->compilationUnit; c->lookups = c->compilationUnit->runtimeLookups; + c->constantTable = c->compilationUnit->constants; c->locals = (Value *)((quintptr(c + 1) + 7) & ~7); const CompiledData::Function *compiledFunction = function->function()->compiledFunction; @@ -159,44 +161,35 @@ void ExecutionContext::createMutableBinding(String *name, bool deletable) activation->__defineOwnProperty__(scope.engine, name, desc, attrs); } - -Heap::GlobalContext::GlobalContext(ExecutionEngine *eng) - : Heap::ExecutionContext(eng, Heap::ExecutionContext::Type_GlobalContext) +void Heap::GlobalContext::init(ExecutionEngine *eng) { + Heap::ExecutionContext::init(eng, Heap::ExecutionContext::Type_GlobalContext); global = eng->globalObject->d(); } -Heap::WithContext::WithContext(ExecutionContext *outerContext, Object *with) - : Heap::ExecutionContext(outerContext->engine, Heap::ExecutionContext::Type_WithContext) -{ - outer = outerContext; - callData = outer->callData; - lookups = outer->lookups; - compilationUnit = outer->compilationUnit; - - withObject = with; -} - -Heap::CatchContext::CatchContext(ExecutionContext *outerContext, String *exceptionVarName, const Value &exceptionValue) - : Heap::ExecutionContext(outerContext->engine, Heap::ExecutionContext::Type_CatchContext) +void Heap::CatchContext::init(ExecutionContext *outerContext, String *exceptionVarName, + const Value &exceptionValue) { + Heap::ExecutionContext::init(outerContext->engine, Heap::ExecutionContext::Type_CatchContext); outer = outerContext; strictMode = outer->strictMode; callData = outer->callData; lookups = outer->lookups; + constantTable = outer->constantTable; compilationUnit = outer->compilationUnit; this->exceptionVarName = exceptionVarName; this->exceptionValue = exceptionValue; } -Heap::QmlContext::QmlContext(QV4::ExecutionContext *outerContext, QV4::QmlContextWrapper *qml) - : Heap::ExecutionContext(outerContext->engine(), Heap::ExecutionContext::Type_QmlContext) +void Heap::QmlContext::init(QV4::ExecutionContext *outerContext, QV4::QmlContextWrapper *qml) { + Heap::ExecutionContext::init(outerContext->engine(), Heap::ExecutionContext::Type_QmlContext); outer = outerContext->d(); strictMode = false; callData = outer->callData; lookups = outer->lookups; + constantTable = outer->constantTable; compilationUnit = outer->compilationUnit; this->qml = qml->d(); diff --git a/src/qml/jsruntime/qv4context_p.h b/src/qml/jsruntime/qv4context_p.h index 2e6773a927..0b42288ccc 100644 --- a/src/qml/jsruntime/qv4context_p.h +++ b/src/qml/jsruntime/qv4context_p.h @@ -101,36 +101,41 @@ struct ExecutionContext : Base { Type_CallContext = 0x6 }; - inline ExecutionContext(ExecutionEngine *engine, ContextType t); + void init(ExecutionEngine *engine, ContextType t) + { + Base::init(); + + callData = nullptr; + this->engine = engine; + outer = nullptr; + lookups = nullptr; + constantTable = nullptr; + compilationUnit = nullptr; + type = t; + strictMode = false; + lineNumber = -1; + } CallData *callData; ExecutionEngine *engine; Pointer<ExecutionContext> outer; Lookup *lookups; + const QV4::Value *constantTable; CompiledData::CompilationUnit *compilationUnit; ContextType type : 8; bool strictMode : 8; int lineNumber; }; - -inline -ExecutionContext::ExecutionContext(ExecutionEngine *engine, ContextType t) - : engine(engine) - , outer(0) - , lookups(0) - , compilationUnit(0) - , type(t) - , strictMode(false) - , lineNumber(-1) -{} - +V4_ASSERT_IS_TRIVIAL(ExecutionContext) struct CallContext : ExecutionContext { - CallContext(ExecutionEngine *engine, ContextType t = Type_SimpleCallContext) - : ExecutionContext(engine, t) + static CallContext createOnStack(ExecutionEngine *v4); + + void init(ExecutionEngine *engine, ContextType t = Type_SimpleCallContext) { + ExecutionContext::init(engine, t); function = 0; locals = 0; activation = 0; @@ -140,27 +145,43 @@ struct CallContext : ExecutionContext { Value *locals; Pointer<Object> activation; }; +V4_ASSERT_IS_TRIVIAL(CallContext) struct GlobalContext : ExecutionContext { - GlobalContext(ExecutionEngine *engine); + void init(ExecutionEngine *engine); Pointer<Object> global; }; +V4_ASSERT_IS_TRIVIAL(GlobalContext) struct CatchContext : ExecutionContext { - CatchContext(ExecutionContext *outerContext, String *exceptionVarName, const Value &exceptionValue); + void init(ExecutionContext *outerContext, String *exceptionVarName, const Value &exceptionValue); Pointer<String> exceptionVarName; Value exceptionValue; }; +V4_ASSERT_IS_TRIVIAL(CatchContext) struct WithContext : ExecutionContext { - WithContext(ExecutionContext *outerContext, Object *with); + void init(ExecutionContext *outerContext, Object *with) + { + Heap::ExecutionContext::init(outerContext->engine, Heap::ExecutionContext::Type_WithContext); + outer = outerContext; + callData = outer->callData; + lookups = outer->lookups; + constantTable = outer->constantTable; + compilationUnit = outer->compilationUnit; + + withObject = with; + } + Pointer<Object> withObject; }; +V4_ASSERT_IS_TRIVIAL(WithContext) struct QmlContextWrapper; struct QmlContext : ExecutionContext { - QmlContext(QV4::ExecutionContext *outerContext, QV4::QmlContextWrapper *qml); + void init(QV4::ExecutionContext *outerContext, QV4::QmlContextWrapper *qml); + Pointer<QmlContextWrapper> qml; }; @@ -277,6 +298,16 @@ inline const WithContext *ExecutionContext::asWithContext() const return d()->type == Heap::ExecutionContext::Type_WithContext ? static_cast<const WithContext *>(this) : 0; } +inline Heap::CallContext Heap::CallContext::createOnStack(ExecutionEngine *v4) +{ + Heap::CallContext ctxt; + memset(&ctxt, 0, sizeof(Heap::CallContext)); + ctxt.mm_data = 0; + ctxt.setVtable(QV4::CallContext::staticVTable()); + ctxt.init(v4); + return ctxt; +} + /* Function *f, int argc */ #define requiredMemoryForExecutionContect(f, argc) \ ((sizeof(CallContext::Data) + 7) & ~7) + sizeof(Value) * (f->varCount() + qMax((uint)argc, f->formalParameterCount())) + sizeof(CallData) diff --git a/src/qml/jsruntime/qv4context_p_p.h b/src/qml/jsruntime/qv4context_p_p.h index 0da9f678ed..ca8dc0b518 100644 --- a/src/qml/jsruntime/qv4context_p_p.h +++ b/src/qml/jsruntime/qv4context_p_p.h @@ -69,7 +69,7 @@ QObject *QmlContext::qmlScope() const QQmlContextData *QmlContext::qmlContext() const { - return d()->qml->context; + return *d()->qml->context; } void QmlContext::takeContextOwnership() { diff --git a/src/qml/jsruntime/qv4dataview.cpp b/src/qml/jsruntime/qv4dataview.cpp index f296ffd71e..db8376272d 100644 --- a/src/qml/jsruntime/qv4dataview.cpp +++ b/src/qml/jsruntime/qv4dataview.cpp @@ -49,37 +49,39 @@ using namespace QV4; DEFINE_OBJECT_VTABLE(DataViewCtor); DEFINE_OBJECT_VTABLE(DataView); -Heap::DataViewCtor::DataViewCtor(QV4::ExecutionContext *scope) - : Heap::FunctionObject(scope, QStringLiteral("DataView")) +void Heap::DataViewCtor::init(QV4::ExecutionContext *scope) { + Heap::FunctionObject::init(scope, QStringLiteral("DataView")); } -ReturnedValue DataViewCtor::construct(const Managed *m, CallData *callData) +void DataViewCtor::construct(const Managed *, Scope &scope, CallData *callData) { - Scope scope(static_cast<const Object *>(m)->engine()); Scoped<ArrayBuffer> buffer(scope, callData->argument(0)); - if (!buffer) - return scope.engine->throwTypeError(); + if (!buffer) { + scope.result = scope.engine->throwTypeError(); + return; + } double bo = callData->argc > 1 ? callData->args[1].toNumber() : 0; uint byteOffset = (uint)bo; uint bufferLength = buffer->d()->data->size; double bl = callData->argc < 3 || callData->args[2].isUndefined() ? (bufferLength - bo) : callData->args[2].toNumber(); uint byteLength = (uint)bl; - if (bo != byteOffset || bl != byteLength || byteOffset + byteLength > bufferLength) - return scope.engine->throwRangeError(QStringLiteral("DataView: constructor arguments out of range")); + if (bo != byteOffset || bl != byteLength || byteOffset + byteLength > bufferLength) { + scope.result = scope.engine->throwRangeError(QStringLiteral("DataView: constructor arguments out of range")); + return; + } Scoped<DataView> a(scope, scope.engine->memoryManager->allocObject<DataView>()); a->d()->buffer = buffer->d(); a->d()->byteLength = byteLength; a->d()->byteOffset = byteOffset; - return a.asReturnedValue(); - + scope.result = a.asReturnedValue(); } -ReturnedValue DataViewCtor::call(const Managed *that, CallData *callData) +void DataViewCtor::call(const Managed *that, Scope &scope, CallData *callData) { - return construct(that, callData); + construct(that, scope, callData); } diff --git a/src/qml/jsruntime/qv4dataview_p.h b/src/qml/jsruntime/qv4dataview_p.h index 2e8e94cecd..246124394a 100644 --- a/src/qml/jsruntime/qv4dataview_p.h +++ b/src/qml/jsruntime/qv4dataview_p.h @@ -60,11 +60,11 @@ namespace QV4 { namespace Heap { struct DataViewCtor : FunctionObject { - DataViewCtor(QV4::ExecutionContext *scope); + void init(QV4::ExecutionContext *scope); }; struct DataView : Object { - DataView() {} + void init() { Object::init(); } Pointer<ArrayBuffer> buffer; uint byteLength; uint byteOffset; @@ -76,8 +76,8 @@ struct DataViewCtor: FunctionObject { V4_OBJECT2(DataViewCtor, FunctionObject) - static ReturnedValue construct(const Managed *m, CallData *callData); - static ReturnedValue call(const Managed *that, CallData *callData); + static void construct(const Managed *m, Scope &scope, CallData *callData); + static void call(const Managed *that, Scope &scope, CallData *callData); }; struct DataView : Object diff --git a/src/qml/jsruntime/qv4dateobject.cpp b/src/qml/jsruntime/qv4dateobject.cpp index 5397ad43c5..4f3138a452 100644 --- a/src/qml/jsruntime/qv4dateobject.cpp +++ b/src/qml/jsruntime/qv4dateobject.cpp @@ -466,7 +466,7 @@ static inline double ParseString(const QString &s) if (format < Minute || format >= TimezoneHour) error = true; format = TimezoneHour; - } else if (*ch == 'Z' || *ch == 0) { + } else if (*ch == 'Z' || ch->unicode() == 0) { format = Done; } current = 0; @@ -565,7 +565,7 @@ static inline QString ToString(double t) { if (std::isnan(t)) return QStringLiteral("Invalid Date"); - QString str = ToDateTime(t, Qt::LocalTime).toString() + QStringLiteral(" GMT"); + QString str = ToDateTime(t, Qt::LocalTime).toString() + QLatin1String(" GMT"); double tzoffset = LocalTZA + DaylightSavingTA(t); if (tzoffset) { int hours = static_cast<int>(::fabs(tzoffset) / 1000 / 60 / 60); @@ -634,11 +634,35 @@ static double getLocalTZA() DEFINE_OBJECT_VTABLE(DateObject); -Heap::DateObject::DateObject(const QDateTime &date) +void Heap::DateObject::init(const QDateTime &date) { + Object::init(); this->date = date.isValid() ? date.toMSecsSinceEpoch() : qt_qnan(); } +void Heap::DateObject::init(const QTime &time) +{ + Object::init(); + if (!time.isValid()) { + date = qt_qnan(); + return; + } + + /* All programmers know that stuff starts at 0. Whatever that may mean in this context (and + * local timezone), it's before the epoch, so there is defenitely no DST problem. Specifically: + * you can't start with a date before the epoch, add some[*] hours, and end up with a date + * after. That's a problem for timezones where new year happens during DST, like + * Australia/Hobart, because we have to ignore DST before the epoch (but honor it after the + * epoch). + * + * [*] Well, when "some" is in the range 0-24. If you add something like 1M then this might + * still happen. + */ + static const double d = MakeDay(0, 0, 0); + double t = MakeTime(time.hour(), time.minute(), time.second(), time.msec()); + date = TimeClip(UTC(MakeDate(d, t))); +} + QDateTime DateObject::toQDateTime() const { return ToDateTime(date(), Qt::LocalTime); @@ -646,14 +670,13 @@ QDateTime DateObject::toQDateTime() const DEFINE_OBJECT_VTABLE(DateCtor); -Heap::DateCtor::DateCtor(QV4::ExecutionContext *scope) - : Heap::FunctionObject(scope, QStringLiteral("Date")) +void Heap::DateCtor::init(QV4::ExecutionContext *scope) { + Heap::FunctionObject::init(scope, QStringLiteral("Date")); } -ReturnedValue DateCtor::construct(const Managed *m, CallData *callData) +void DateCtor::construct(const Managed *, Scope &scope, CallData *callData) { - Scope scope(static_cast<const DateCtor *>(m)->engine()); double t = 0; if (callData->argc == 0) @@ -687,13 +710,13 @@ ReturnedValue DateCtor::construct(const Managed *m, CallData *callData) t = TimeClip(UTC(t)); } - return Encode(scope.engine->newDateObject(Primitive::fromDouble(t))); + scope.result = Encode(scope.engine->newDateObject(Primitive::fromDouble(t))); } -ReturnedValue DateCtor::call(const Managed *m, CallData *) +void DateCtor::call(const Managed *m, Scope &scope, CallData *) { double t = currentTime(); - return static_cast<const DateCtor *>(m)->engine()->newString(ToString(t))->asReturnedValue(); + scope.result = static_cast<const DateCtor *>(m)->engine()->newString(ToString(t)); } void DatePrototype::init(ExecutionEngine *engine, Object *ctor) @@ -1311,7 +1334,8 @@ ReturnedValue DatePrototype::method_toJSON(CallContext *ctx) ScopedCallData callData(scope); callData->thisObject = ctx->thisObject(); - return toIso->call(callData); + toIso->call(scope, callData); + return scope.result.asReturnedValue(); } void DatePrototype::timezoneUpdated() diff --git a/src/qml/jsruntime/qv4dateobject_p.h b/src/qml/jsruntime/qv4dateobject_p.h index e3615d76a7..2d0648396e 100644 --- a/src/qml/jsruntime/qv4dateobject_p.h +++ b/src/qml/jsruntime/qv4dateobject_p.h @@ -63,22 +63,26 @@ namespace QV4 { namespace Heap { struct DateObject : Object { - DateObject() + void init() { + Object::init(); date = qt_qnan(); } - DateObject(const Value &date) + void init(const Value &date) { + Object::init(); this->date = date.toNumber(); } - DateObject(const QDateTime &date); + void init(const QDateTime &date); + void init(const QTime &time); + double date; }; struct DateCtor : FunctionObject { - DateCtor(QV4::ExecutionContext *scope); + void init(QV4::ExecutionContext *scope); }; } @@ -104,8 +108,8 @@ struct DateCtor: FunctionObject { V4_OBJECT2(DateCtor, FunctionObject) - static ReturnedValue construct(const Managed *, CallData *callData); - static ReturnedValue call(const Managed *that, CallData *); + static void construct(const Managed *, Scope &scope, CallData *callData); + static void call(const Managed *that, Scope &scope, CallData *); }; struct DatePrototype: DateObject diff --git a/src/qml/jsruntime/qv4debugging.cpp b/src/qml/jsruntime/qv4debugging.cpp deleted file mode 100644 index 9fcba64038..0000000000 --- a/src/qml/jsruntime/qv4debugging.cpp +++ /dev/null @@ -1,60 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtQml module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qv4debugging_p.h" -#include "qv4object_p.h" -#include "qv4functionobject_p.h" -#include "qv4function_p.h" -#include "qv4instr_moth_p.h" -#include "qv4runtime_p.h" -#include "qv4script_p.h" -#include "qv4identifier_p.h" -#include "qv4string_p.h" -#include "qv4objectiterator_p.h" - -#include <iostream> -#include <algorithm> - -#include <QtCore/QJsonArray> -#include <QtCore/QJsonDocument> -#include <QtCore/QJsonValue> - -QT_BEGIN_NAMESPACE - -QT_END_NAMESPACE diff --git a/src/qml/jsruntime/qv4debugging_p.h b/src/qml/jsruntime/qv4debugging_p.h index 9dca7e9979..3b589a41f1 100644 --- a/src/qml/jsruntime/qv4debugging_p.h +++ b/src/qml/jsruntime/qv4debugging_p.h @@ -59,6 +59,19 @@ QT_BEGIN_NAMESPACE namespace QV4 { namespace Debugging { +#ifdef QT_NO_QML_DEBUGGER + +struct Debugger +{ + bool pauseAtNextOpportunity() const { return false; } + void maybeBreakAtInstruction() {} + void enteringFunction() {} + void leavingFunction(const ReturnedValue &) {} + void aboutToThrow() {} +}; + +#else + class Q_QML_EXPORT Debugger : public QObject { Q_OBJECT @@ -72,6 +85,8 @@ public: virtual void aboutToThrow() = 0; }; +#endif // QT_NO_QML_DEBUGGING + } // namespace Debugging } // namespace QV4 diff --git a/src/qml/jsruntime/qv4engine.cpp b/src/qml/jsruntime/qv4engine.cpp index 26f473a7aa..a9284f2e69 100644 --- a/src/qml/jsruntime/qv4engine.cpp +++ b/src/qml/jsruntime/qv4engine.cpp @@ -86,7 +86,9 @@ #include "qv4isel_masm_p.h" #endif // V4_ENABLE_JIT +#if QT_CONFIG(qml_interpreter) #include "qv4isel_moth_p.h" +#endif #if USE(PTHREADS) # include <pthread.h> @@ -136,8 +138,6 @@ ExecutionEngine::ExecutionEngine(EvalISelFactory *factory) , currentContext(0) , bumperPointerAllocator(new WTF::BumpPointerAllocator) , jsStack(new WTF::PageAllocation) - , debugger(0) - , profiler(0) , globalCode(0) , v8Engine(0) , argumentsAccessors(0) @@ -145,6 +145,10 @@ ExecutionEngine::ExecutionEngine(EvalISelFactory *factory) , m_engineId(engineSerial.fetchAndAddOrdered(1)) , regExpCache(0) , m_multiplyWrappedQObjects(0) +#ifndef QT_NO_QML_DEBUGGER + , m_debugger(0) + , m_profiler(0) +#endif { if (maxCallDepth == -1) { bool ok = false; @@ -158,6 +162,7 @@ ExecutionEngine::ExecutionEngine(EvalISelFactory *factory) MemoryManager::GCBlocker gcBlocker(memoryManager); if (!factory) { +#if QT_CONFIG(qml_interpreter) bool jitDisabled = true; #ifdef V4_ENABLE_JIT @@ -178,6 +183,9 @@ ExecutionEngine::ExecutionEngine(EvalISelFactory *factory) "very slow. Visit https://wiki.qt.io/V4 to learn about possible " "solutions for your platform."); } +#else + factory = new JIT::ISelFactory; +#endif } iselFactory.reset(factory); @@ -442,10 +450,12 @@ ExecutionEngine::ExecutionEngine(EvalISelFactory *factory) ExecutionEngine::~ExecutionEngine() { - delete debugger; - debugger = 0; - delete profiler; - profiler = 0; +#ifndef QT_NO_QML_DEBUGGER + delete m_debugger; + m_debugger = 0; + delete m_profiler; + m_profiler = 0; +#endif delete m_multiplyWrappedQObjects; m_multiplyWrappedQObjects = 0; delete identifierTable; @@ -467,23 +477,26 @@ ExecutionEngine::~ExecutionEngine() delete [] argumentsAccessors; } -void ExecutionEngine::setDebugger(Debugging::Debugger *debugger_) +#ifndef QT_NO_QML_DEBUGGER +void ExecutionEngine::setDebugger(Debugging::Debugger *debugger) { - Q_ASSERT(!debugger); - debugger = debugger_; + Q_ASSERT(!m_debugger); + m_debugger = debugger; } -void ExecutionEngine::enableProfiler() +void ExecutionEngine::setProfiler(Profiling::Profiler *profiler) { - Q_ASSERT(!profiler); - profiler = new QV4::Profiling::Profiler(this); + Q_ASSERT(!m_profiler); + m_profiler = profiler; } +#endif // QT_NO_QML_DEBUGGER void ExecutionEngine::initRootContext() { Scope scope(this); - Scoped<GlobalContext> r(scope, memoryManager->allocManaged<GlobalContext>(sizeof(GlobalContext::Data) + sizeof(CallData))); - new (r->d()) GlobalContext::Data(this); + Scoped<GlobalContext> r(scope, memoryManager->allocManaged<GlobalContext>( + sizeof(GlobalContext::Data) + sizeof(CallData))); + r->d_unchecked()->init(this); r->d()->callData = reinterpret_cast<CallData *>(r->d() + 1); r->d()->callData->tag = QV4::Value::Integer_Type_Internal; r->d()->callData->argc = 0; @@ -566,7 +579,7 @@ Heap::ArrayObject *ExecutionEngine::newArrayObject(const Value *values, int leng if (length) { size_t size = sizeof(Heap::ArrayData) + (length-1)*sizeof(Value); Heap::SimpleArrayData *d = scope.engine->memoryManager->allocManaged<SimpleArrayData>(size); - new (d) Heap::SimpleArrayData; + d->init(); d->alloc = length; d->type = Heap::ArrayData::Simple; d->offset = 0; @@ -615,6 +628,13 @@ Heap::DateObject *ExecutionEngine::newDateObject(const QDateTime &dt) return object->d(); } +Heap::DateObject *ExecutionEngine::newDateObjectFromTime(const QTime &t) +{ + Scope scope(this); + Scoped<DateObject> object(scope, memoryManager->allocObject<DateObject>(t)); + return object->d(); +} + Heap::RegExpObject *ExecutionEngine::newRegExpObject(const QString &pattern, int flags) { bool global = (flags & IR::RegExp::RegExp_Global); @@ -730,7 +750,7 @@ QQmlContextData *ExecutionEngine::callingQmlContext() const if (!ctx) return 0; - return ctx->qml->context.contextData(); + return ctx->qml->context->contextData(); } QVector<StackFrame> ExecutionEngine::stackTrace(int frameLimit) const @@ -906,12 +926,12 @@ ReturnedValue ExecutionEngine::throwError(const Value &value) QV4::Scope scope(this); QV4::Scoped<ErrorObject> error(scope, value); if (!!error) - exceptionStackTrace = error->d()->stackTrace; + exceptionStackTrace = *error->d()->stackTrace; else exceptionStackTrace = stackTrace(); - if (debugger) - debugger->aboutToThrow(); + if (QV4::Debugging::Debugger *debug = debugger()) + debug->aboutToThrow(); return Encode::undefined(); } @@ -969,7 +989,7 @@ ReturnedValue ExecutionEngine::throwReferenceError(const Value &value) { Scope scope(this); ScopedString s(scope, value.toString(this)); - QString msg = s->toQString() + QStringLiteral(" is not defined"); + QString msg = s->toQString() + QLatin1String(" is not defined"); ScopedObject error(scope, newReferenceErrorObject(msg)); return throwError(error); } @@ -993,7 +1013,7 @@ ReturnedValue ExecutionEngine::throwRangeError(const Value &value) { Scope scope(this); ScopedString s(scope, value.toString(this)); - QString msg = s->toQString() + QStringLiteral(" out of range"); + QString msg = s->toQString() + QLatin1String(" out of range"); ScopedObject error(scope, newRangeErrorObject(msg)); return throwError(error); } @@ -1065,7 +1085,7 @@ static QVariant toVariant(QV4::ExecutionEngine *e, const QV4::Value &value, int QV4::Scope scope(e); if (const QV4::VariantObject *v = value.as<QV4::VariantObject>()) - return v->d()->data; + return v->d()->data(); if (typeHint == QVariant::Bool) return QVariant(value.toBoolean()); @@ -1124,7 +1144,7 @@ static QVariant toVariant(QV4::ExecutionEngine *e, const QV4::Value &value, int if (value.isUndefined()) return QVariant(); if (value.isNull()) - return QVariant(QMetaType::VoidStar, (void *)0); + return QVariant::fromValue(nullptr); if (value.isBoolean()) return value.booleanValue(); if (value.isInteger()) @@ -1139,10 +1159,10 @@ static QVariant toVariant(QV4::ExecutionEngine *e, const QV4::Value &value, int return str; } if (const QV4::QQmlLocaleData *ld = value.as<QV4::QQmlLocaleData>()) - return ld->d()->locale; + return *ld->d()->locale; if (const QV4::DateObject *d = value.as<DateObject>()) return d->toQDateTime(); - if (const QV4::ArrayBuffer *d = value.as<ArrayBuffer>()) + 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)! @@ -1254,6 +1274,7 @@ QV4::ReturnedValue QV4::ExecutionEngine::fromVariant(const QVariant &variant) case QMetaType::UnknownType: case QMetaType::Void: return QV4::Encode::undefined(); + case QMetaType::Nullptr: case QMetaType::VoidStar: return QV4::Encode::null(); case QMetaType::Bool: @@ -1270,6 +1291,8 @@ QV4::ReturnedValue QV4::ExecutionEngine::fromVariant(const QVariant &variant) return QV4::Encode(*reinterpret_cast<const double*>(ptr)); case QMetaType::QString: return newString(*reinterpret_cast<const QString*>(ptr))->asReturnedValue(); + case QMetaType::QByteArray: + return newArrayBuffer(*reinterpret_cast<const QByteArray*>(ptr))->asReturnedValue(); case QMetaType::Float: return QV4::Encode(*reinterpret_cast<const float*>(ptr)); case QMetaType::Short: @@ -1287,7 +1310,7 @@ QV4::ReturnedValue QV4::ExecutionEngine::fromVariant(const QVariant &variant) case QMetaType::QDate: return QV4::Encode(newDateObject(QDateTime(*reinterpret_cast<const QDate *>(ptr)))); case QMetaType::QTime: - return QV4::Encode(newDateObject(QDateTime(QDate(1970,1,1), *reinterpret_cast<const QTime *>(ptr)))); + return QV4::Encode(newDateObjectFromTime(*reinterpret_cast<const QTime *>(ptr))); case QMetaType::QRegExp: return QV4::Encode(newRegExpObject(*reinterpret_cast<const QRegExp *>(ptr))); case QMetaType::QObjectStar: @@ -1423,6 +1446,7 @@ QV4::ReturnedValue ExecutionEngine::metaTypeToJS(int type, const void *data) case QMetaType::UnknownType: case QMetaType::Void: return QV4::Encode::undefined(); + case QMetaType::Nullptr: case QMetaType::VoidStar: return QV4::Encode::null(); case QMetaType::Bool: @@ -1446,6 +1470,8 @@ QV4::ReturnedValue ExecutionEngine::metaTypeToJS(int type, const void *data) return QV4::Encode(*reinterpret_cast<const double*>(data)); case QMetaType::QString: return newString(*reinterpret_cast<const QString*>(data))->asReturnedValue(); + case QMetaType::QByteArray: + return newArrayBuffer(*reinterpret_cast<const QByteArray*>(data))->asReturnedValue(); case QMetaType::Float: return QV4::Encode(*reinterpret_cast<const float*>(data)); case QMetaType::Short: @@ -1507,6 +1533,11 @@ void ExecutionEngine::assertObjectBelongsToEngine(const Heap::Base &baseObject) Q_UNUSED(baseObject); } +void ExecutionEngine::failStackLimitCheck(Scope &scope) +{ + scope.result = throwRangeError(QStringLiteral("Maximum call stack size exceeded.")); +} + // 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. @@ -1538,6 +1569,12 @@ bool ExecutionEngine::metaTypeFromJS(const Value *value, int type, void *data) else *reinterpret_cast<QString*>(data) = value->toQString(); return true; + case QMetaType::QByteArray: + if (const ArrayBuffer *ab = value->as<ArrayBuffer>()) + *reinterpret_cast<QByteArray*>(data) = ab->asByteArray(); + else + *reinterpret_cast<QByteArray*>(data) = QByteArray(); + return true; case QMetaType::Float: *reinterpret_cast<float*>(data) = value->toNumber(); return true; @@ -1662,7 +1699,7 @@ bool ExecutionEngine::metaTypeFromJS(const Value *value, int type, void *data) return true; if (value->as<QV4::VariantObject>() && name.endsWith('*')) { int valueType = QMetaType::type(name.left(name.size()-1)); - QVariant &var = value->as<QV4::VariantObject>()->d()->data; + QVariant &var = value->as<QV4::VariantObject>()->d()->data(); if (valueType == var.userType()) { // We have T t, T* is requested, so return &t. *reinterpret_cast<void* *>(data) = var.data(); @@ -1674,7 +1711,7 @@ bool ExecutionEngine::metaTypeFromJS(const Value *value, int type, void *data) while (proto) { bool canCast = false; if (QV4::VariantObject *vo = proto->as<QV4::VariantObject>()) { - const QVariant &v = vo->d()->data; + const QVariant &v = vo->d()->data(); canCast = (type == v.userType()) || (valueType && (valueType == v.userType())); } else if (proto->as<QV4::QObjectWrapper>()) { @@ -1729,7 +1766,7 @@ static QObject *qtObjectFromJS(QV4::ExecutionEngine *engine, const Value &value) QV4::Scoped<QV4::VariantObject> v(scope, value); if (v) { - QVariant variant = v->d()->data; + QVariant variant = v->d()->data(); int type = variant.userType(); if (type == QMetaType::QObjectStar) return *reinterpret_cast<QObject* const *>(variant.constData()); diff --git a/src/qml/jsruntime/qv4engine_p.h b/src/qml/jsruntime/qv4engine_p.h index aeb2533d35..843a6f4d94 100644 --- a/src/qml/jsruntime/qv4engine_p.h +++ b/src/qml/jsruntime/qv4engine_p.h @@ -54,7 +54,7 @@ #include "private/qv4isel_p.h" #include "qv4managed_p.h" #include "qv4context_p.h" -#include "qv4internalclass_p.h" +#include "qv4runtimeapi_p.h" #include <private/qintrusivelist_p.h> #ifndef V4_BOOTSTRAP @@ -85,6 +85,9 @@ namespace CompiledData { struct CompilationUnit; } +struct InternalClass; +struct InternalClassPool; + struct Q_QML_EXPORT ExecutionEngine { private: @@ -109,6 +112,8 @@ public: Value *jsStackLimit; + Runtime runtime; + WTF::BumpPointerAllocator *bumperPointerAllocator; // Used by Yarr Regex engine. enum { JSStackLimit = 4*1024*1024 }; @@ -132,9 +137,6 @@ public: IdentifierTable *identifierTable; - QV4::Debugging::Debugger *debugger; - QV4::Profiling::Profiler *profiler; - Object *globalObject; Function *globalCode; @@ -377,8 +379,19 @@ public: ExecutionEngine(EvalISelFactory *iselFactory = 0); ~ExecutionEngine(); +#ifdef QT_NO_QML_DEBUGGER + QV4::Debugging::Debugger *debugger() const { return nullptr; } + QV4::Profiling::Profiler *profiler() const { return nullptr; } + + void setDebugger(Debugging::Debugger *) {} + void setProfiler(Profiling::Profiler *) {} +#else + QV4::Debugging::Debugger *debugger() const { return m_debugger; } + QV4::Profiling::Profiler *profiler() const { return m_profiler; } + void setDebugger(Debugging::Debugger *debugger); - void enableProfiler(); + void setProfiler(Profiling::Profiler *profiler); +#endif // QT_NO_QML_DEBUGGER ExecutionContext *pushGlobalContext(); void pushContext(Heap::ExecutionContext *context); @@ -406,6 +419,7 @@ public: Heap::DateObject *newDateObject(const Value &value); Heap::DateObject *newDateObject(const QDateTime &dt); + Heap::DateObject *newDateObjectFromTime(const QTime &t); Heap::RegExpObject *newRegExpObject(const QString &pattern, int flags); Heap::RegExpObject *newRegExpObject(RegExp *re, bool global); @@ -475,9 +489,28 @@ public: void assertObjectBelongsToEngine(const Heap::Base &baseObject); - bool checkStackLimits(ReturnedValue &exception); + bool checkStackLimits(Scope &scope); + +private: + void failStackLimitCheck(Scope &scope); + +#ifndef QT_NO_QML_DEBUGGER + QV4::Debugging::Debugger *m_debugger; + QV4::Profiling::Profiler *m_profiler; +#endif }; +// This is a trick to tell the code generators that functions taking a NoThrowContext won't +// throw exceptions and therefore don't need a check after the call. +#ifndef V4_BOOTSTRAP +struct NoThrowEngine : public ExecutionEngine +{ +}; +#else +struct NoThrowEngine; +#endif + + inline void ExecutionEngine::pushContext(Heap::ExecutionContext *context) { Q_ASSERT(currentContext && context); @@ -557,7 +590,7 @@ inline void Value::mark(ExecutionEngine *e) o->mark(e); } -#define CHECK_STACK_LIMITS(v4) { ReturnedValue e; if ((v4)->checkStackLimits(e)) return e; } \ +#define CHECK_STACK_LIMITS(v4, scope) if ((v4)->checkStackLimits(scope)) return; \ ExecutionEngineCallDepthRecorder _executionEngineCallDepthRecorder(v4); struct ExecutionEngineCallDepthRecorder @@ -568,10 +601,10 @@ struct ExecutionEngineCallDepthRecorder ~ExecutionEngineCallDepthRecorder() { --ee->callDepth; } }; -inline bool ExecutionEngine::checkStackLimits(ReturnedValue &exception) +inline bool ExecutionEngine::checkStackLimits(Scope &scope) { if (Q_UNLIKELY((jsStackTop > jsStackLimit) || (callDepth >= maxCallDepth))) { - exception = throwRangeError(QStringLiteral("Maximum call stack size exceeded.")); + failStackLimitCheck(scope); return true; } diff --git a/src/qml/jsruntime/qv4errorobject.cpp b/src/qml/jsruntime/qv4errorobject.cpp index 9f1e6b613b..597ded6ae1 100644 --- a/src/qml/jsruntime/qv4errorobject.cpp +++ b/src/qml/jsruntime/qv4errorobject.cpp @@ -67,8 +67,11 @@ using namespace QV4; -Heap::ErrorObject::ErrorObject() +void Heap::ErrorObject::init() { + Object::init(); + stackTrace = nullptr; + Scope scope(internalClass->engine); Scoped<QV4::ErrorObject> e(scope, this); @@ -81,8 +84,9 @@ Heap::ErrorObject::ErrorObject() *propertyData(QV4::ErrorObject::Index_LineNumber) = Encode::undefined(); } -Heap::ErrorObject::ErrorObject(const Value &message, ErrorType t) +void Heap::ErrorObject::init(const Value &message, ErrorType t) { + Object::init(); errorType = t; Scope scope(internalClass->engine); @@ -91,18 +95,19 @@ Heap::ErrorObject::ErrorObject(const Value &message, ErrorType t) *propertyData(QV4::ErrorObject::Index_Stack) = scope.engine->getStackFunction(); *propertyData(QV4::ErrorObject::Index_Stack + QV4::Object::SetterOffset) = Encode::undefined(); - e->d()->stackTrace = scope.engine->stackTrace(); - if (!e->d()->stackTrace.isEmpty()) { - *propertyData(QV4::ErrorObject::Index_FileName) = scope.engine->newString(e->d()->stackTrace.at(0).source); - *propertyData(QV4::ErrorObject::Index_LineNumber) = Primitive::fromInt32(e->d()->stackTrace.at(0).line); + e->d()->stackTrace = new StackTrace(scope.engine->stackTrace()); + if (!e->d()->stackTrace->isEmpty()) { + *propertyData(QV4::ErrorObject::Index_FileName) = scope.engine->newString(e->d()->stackTrace->at(0).source); + *propertyData(QV4::ErrorObject::Index_LineNumber) = Primitive::fromInt32(e->d()->stackTrace->at(0).line); } if (!message.isUndefined()) *propertyData(QV4::ErrorObject::Index_Message) = message; } -Heap::ErrorObject::ErrorObject(const Value &message, const QString &fileName, int line, int column, ErrorObject::ErrorType t) +void Heap::ErrorObject::init(const Value &message, const QString &fileName, int line, int column, ErrorObject::ErrorType t) { + Object::init(); errorType = t; Scope scope(internalClass->engine); @@ -111,16 +116,16 @@ Heap::ErrorObject::ErrorObject(const Value &message, const QString &fileName, in *propertyData(QV4::ErrorObject::Index_Stack) = scope.engine->getStackFunction(); *propertyData(QV4::ErrorObject::Index_Stack + QV4::Object::SetterOffset) = Encode::undefined(); - e->d()->stackTrace = scope.engine->stackTrace(); + e->d()->stackTrace = new StackTrace(scope.engine->stackTrace()); StackFrame frame; frame.source = fileName; frame.line = line; frame.column = column; - e->d()->stackTrace.prepend(frame); + e->d()->stackTrace->prepend(frame); - if (!e->d()->stackTrace.isEmpty()) { - *propertyData(QV4::ErrorObject::Index_FileName) = scope.engine->newString(e->d()->stackTrace.at(0).source); - *propertyData(QV4::ErrorObject::Index_LineNumber) = Primitive::fromInt32(e->d()->stackTrace.at(0).line); + if (!e->d()->stackTrace->isEmpty()) { + *propertyData(QV4::ErrorObject::Index_FileName) = scope.engine->newString(e->d()->stackTrace->at(0).source); + *propertyData(QV4::ErrorObject::Index_LineNumber) = Primitive::fromInt32(e->d()->stackTrace->at(0).line); } if (!message.isUndefined()) @@ -156,17 +161,13 @@ ReturnedValue ErrorObject::method_get_stack(CallContext *ctx) return ctx->engine()->throwTypeError(); if (!This->d()->stack) { QString trace; - for (int i = 0; i < This->d()->stackTrace.count(); ++i) { + for (int i = 0; i < This->d()->stackTrace->count(); ++i) { if (i > 0) trace += QLatin1Char('\n'); - const StackFrame &frame = This->d()->stackTrace[i]; - trace += frame.function; - trace += QLatin1Char('@'); - trace += frame.source; - if (frame.line >= 0) { - trace += QLatin1Char(':'); - trace += QString::number(frame.line); - } + const StackFrame &frame = This->d()->stackTrace->at(i); + trace += frame.function + QLatin1Char('@') + frame.source; + if (frame.line >= 0) + trace += QLatin1Char(':') + QString::number(frame.line); } This->d()->stack = ctx->d()->engine->newString(trace); } @@ -185,44 +186,44 @@ DEFINE_OBJECT_VTABLE(ErrorObject); DEFINE_OBJECT_VTABLE(SyntaxErrorObject); -Heap::SyntaxErrorObject::SyntaxErrorObject(const Value &msg) - : Heap::ErrorObject(msg, SyntaxError) +void Heap::SyntaxErrorObject::init(const Value &msg) { + Heap::ErrorObject::init(msg, SyntaxError); } -Heap::SyntaxErrorObject::SyntaxErrorObject(const Value &msg, const QString &fileName, int lineNumber, int columnNumber) - : Heap::ErrorObject(msg, fileName, lineNumber, columnNumber, SyntaxError) +void Heap::SyntaxErrorObject::init(const Value &msg, const QString &fileName, int lineNumber, int columnNumber) { + Heap::ErrorObject::init(msg, fileName, lineNumber, columnNumber, SyntaxError); } -Heap::EvalErrorObject::EvalErrorObject(const Value &message) - : Heap::ErrorObject(message, EvalError) +void Heap::EvalErrorObject::init(const Value &message) { + Heap::ErrorObject::init(message, EvalError); } -Heap::RangeErrorObject::RangeErrorObject(const Value &message) - : Heap::ErrorObject(message, RangeError) +void Heap::RangeErrorObject::init(const Value &message) { + Heap::ErrorObject::init(message, RangeError); } -Heap::ReferenceErrorObject::ReferenceErrorObject(const Value &message) - : Heap::ErrorObject(message, ReferenceError) +void Heap::ReferenceErrorObject::init(const Value &message) { + Heap::ErrorObject::init(message, ReferenceError); } -Heap::ReferenceErrorObject::ReferenceErrorObject(const Value &msg, const QString &fileName, int lineNumber, int columnNumber) - : Heap::ErrorObject(msg, fileName, lineNumber, columnNumber, ReferenceError) +void Heap::ReferenceErrorObject::init(const Value &msg, const QString &fileName, int lineNumber, int columnNumber) { + Heap::ErrorObject::init(msg, fileName, lineNumber, columnNumber, ReferenceError); } -Heap::TypeErrorObject::TypeErrorObject(const Value &message) - : Heap::ErrorObject(message, TypeError) +void Heap::TypeErrorObject::init(const Value &message) { + Heap::ErrorObject::init(message, TypeError); } -Heap::URIErrorObject::URIErrorObject(const Value &message) - : Heap::ErrorObject(message, URIError) +void Heap::URIErrorObject::init(const Value &message) { + Heap::ErrorObject::init(message, URIError); } DEFINE_OBJECT_VTABLE(ErrorCtor); @@ -233,98 +234,91 @@ DEFINE_OBJECT_VTABLE(SyntaxErrorCtor); DEFINE_OBJECT_VTABLE(TypeErrorCtor); DEFINE_OBJECT_VTABLE(URIErrorCtor); -Heap::ErrorCtor::ErrorCtor(QV4::ExecutionContext *scope) - : Heap::FunctionObject(scope, QStringLiteral("Error")) +void Heap::ErrorCtor::init(QV4::ExecutionContext *scope) { + Heap::FunctionObject::init(scope, QStringLiteral("Error")); } -Heap::ErrorCtor::ErrorCtor(QV4::ExecutionContext *scope, const QString &name) - : Heap::FunctionObject(scope, name) +void Heap::ErrorCtor::init(QV4::ExecutionContext *scope, const QString &name) { + Heap::FunctionObject::init(scope, name); } -ReturnedValue ErrorCtor::construct(const Managed *m, CallData *callData) +void ErrorCtor::construct(const Managed *, Scope &scope, CallData *callData) { - Scope scope(static_cast<const ErrorCtor *>(m)->engine()); ScopedValue v(scope, callData->argument(0)); - return ErrorObject::create<ErrorObject>(scope.engine, v)->asReturnedValue(); + scope.result = ErrorObject::create<ErrorObject>(scope.engine, v); } -ReturnedValue ErrorCtor::call(const Managed *that, CallData *callData) +void ErrorCtor::call(const Managed *that, Scope &scope, CallData *callData) { - return static_cast<const Object *>(that)->construct(callData); + static_cast<const Object *>(that)->construct(scope, callData); } -Heap::EvalErrorCtor::EvalErrorCtor(QV4::ExecutionContext *scope) - : Heap::ErrorCtor(scope, QStringLiteral("EvalError")) +void Heap::EvalErrorCtor::init(QV4::ExecutionContext *scope) { + Heap::ErrorCtor::init(scope, QStringLiteral("EvalError")); } -ReturnedValue EvalErrorCtor::construct(const Managed *m, CallData *callData) +void EvalErrorCtor::construct(const Managed *, Scope &scope, CallData *callData) { - Scope scope(static_cast<const EvalErrorCtor *>(m)->engine()); ScopedValue v(scope, callData->argument(0)); - return ErrorObject::create<EvalErrorObject>(scope.engine, v)->asReturnedValue(); + scope.result = ErrorObject::create<EvalErrorObject>(scope.engine, v); } -Heap::RangeErrorCtor::RangeErrorCtor(QV4::ExecutionContext *scope) - : Heap::ErrorCtor(scope, QStringLiteral("RangeError")) +void Heap::RangeErrorCtor::init(QV4::ExecutionContext *scope) { + Heap::ErrorCtor::init(scope, QStringLiteral("RangeError")); } -ReturnedValue RangeErrorCtor::construct(const Managed *m, CallData *callData) +void RangeErrorCtor::construct(const Managed *, Scope &scope, CallData *callData) { - Scope scope(static_cast<const RangeErrorCtor *>(m)->engine()); ScopedValue v(scope, callData->argument(0)); - return ErrorObject::create<RangeErrorObject>(scope.engine, v)->asReturnedValue(); + scope.result = ErrorObject::create<RangeErrorObject>(scope.engine, v); } -Heap::ReferenceErrorCtor::ReferenceErrorCtor(QV4::ExecutionContext *scope) - : Heap::ErrorCtor(scope, QStringLiteral("ReferenceError")) +void Heap::ReferenceErrorCtor::init(QV4::ExecutionContext *scope) { + Heap::ErrorCtor::init(scope, QStringLiteral("ReferenceError")); } -ReturnedValue ReferenceErrorCtor::construct(const Managed *m, CallData *callData) +void ReferenceErrorCtor::construct(const Managed *, Scope &scope, CallData *callData) { - Scope scope(static_cast<const ReferenceErrorCtor *>(m)->engine()); ScopedValue v(scope, callData->argument(0)); - return ErrorObject::create<ReferenceErrorObject>(scope.engine, v)->asReturnedValue(); + scope.result = ErrorObject::create<ReferenceErrorObject>(scope.engine, v); } -Heap::SyntaxErrorCtor::SyntaxErrorCtor(QV4::ExecutionContext *scope) - : Heap::ErrorCtor(scope, QStringLiteral("SyntaxError")) +void Heap::SyntaxErrorCtor::init(QV4::ExecutionContext *scope) { + Heap::ErrorCtor::init(scope, QStringLiteral("SyntaxError")); } -ReturnedValue SyntaxErrorCtor::construct(const Managed *m, CallData *callData) +void SyntaxErrorCtor::construct(const Managed *, Scope &scope, CallData *callData) { - Scope scope(static_cast<const SyntaxErrorCtor *>(m)->engine()); ScopedValue v(scope, callData->argument(0)); - return ErrorObject::create<SyntaxErrorObject>(scope.engine, v)->asReturnedValue(); + scope.result = ErrorObject::create<SyntaxErrorObject>(scope.engine, v); } -Heap::TypeErrorCtor::TypeErrorCtor(QV4::ExecutionContext *scope) - : Heap::ErrorCtor(scope, QStringLiteral("TypeError")) +void Heap::TypeErrorCtor::init(QV4::ExecutionContext *scope) { + Heap::ErrorCtor::init(scope, QStringLiteral("TypeError")); } -ReturnedValue TypeErrorCtor::construct(const Managed *m, CallData *callData) +void TypeErrorCtor::construct(const Managed *, Scope &scope, CallData *callData) { - Scope scope(static_cast<const TypeErrorCtor *>(m)->engine()); ScopedValue v(scope, callData->argument(0)); - return ErrorObject::create<TypeErrorObject>(scope.engine, v)->asReturnedValue(); + scope.result = ErrorObject::create<TypeErrorObject>(scope.engine, v); } -Heap::URIErrorCtor::URIErrorCtor(QV4::ExecutionContext *scope) - : Heap::ErrorCtor(scope, QStringLiteral("URIError")) +void Heap::URIErrorCtor::init(QV4::ExecutionContext *scope) { + Heap::ErrorCtor::init(scope, QStringLiteral("URIError")); } -ReturnedValue URIErrorCtor::construct(const Managed *m, CallData *callData) +void URIErrorCtor::construct(const Managed *, Scope &scope, CallData *callData) { - Scope scope(static_cast<const URIErrorCtor *>(m)->engine()); ScopedValue v(scope, callData->argument(0)); - return ErrorObject::create<URIErrorObject>(scope.engine, v)->asReturnedValue(); + scope.result = ErrorObject::create<URIErrorObject>(scope.engine, v); } void ErrorPrototype::init(ExecutionEngine *engine, Object *ctor, Object *obj, Heap::ErrorObject::ErrorType t) diff --git a/src/qml/jsruntime/qv4errorobject_p.h b/src/qml/jsruntime/qv4errorobject_p.h index 1ca2fedd7b..42a6e0b4b1 100644 --- a/src/qml/jsruntime/qv4errorobject_p.h +++ b/src/qml/jsruntime/qv4errorobject_p.h @@ -73,68 +73,72 @@ struct ErrorObject : Object { URIError }; - ErrorObject(); - ErrorObject(const Value &message, ErrorType t = Error); - ErrorObject(const Value &message, const QString &fileName, int line, int column, ErrorType t = Error); + void init(); + void init(const Value &message, ErrorType t = Error); + void init(const Value &message, const QString &fileName, int line, int column, ErrorType t = Error); + void destroy() { + delete stackTrace; + Object::destroy(); + } ErrorType errorType; - StackTrace stackTrace; + StackTrace *stackTrace; Pointer<String> stack; }; struct EvalErrorObject : ErrorObject { - EvalErrorObject(const Value &message); + void init(const Value &message); }; struct RangeErrorObject : ErrorObject { - RangeErrorObject(const Value &message); + void init(const Value &message); }; struct ReferenceErrorObject : ErrorObject { - ReferenceErrorObject(const Value &message); - ReferenceErrorObject(const Value &msg, const QString &fileName, int lineNumber, int columnNumber); + void init(const Value &message); + void init(const Value &msg, const QString &fileName, int lineNumber, int columnNumber); }; struct SyntaxErrorObject : ErrorObject { - SyntaxErrorObject(const Value &message); - SyntaxErrorObject(const Value &msg, const QString &fileName, int lineNumber, int columnNumber); + void init(const Value &message); + void init(const Value &msg, const QString &fileName, int lineNumber, int columnNumber); }; struct TypeErrorObject : ErrorObject { - TypeErrorObject(const Value &message); + void init(const Value &message); }; struct URIErrorObject : ErrorObject { - URIErrorObject(const Value &message); + void init(const Value &message); }; struct ErrorCtor : Heap::FunctionObject { - ErrorCtor(QV4::ExecutionContext *scope); - ErrorCtor(QV4::ExecutionContext *scope, const QString &name); + void init(QV4::ExecutionContext *scope); + void init(QV4::ExecutionContext *scope, const QString &name); }; struct EvalErrorCtor : ErrorCtor { - EvalErrorCtor(QV4::ExecutionContext *scope); + void init(QV4::ExecutionContext *scope); }; struct RangeErrorCtor : ErrorCtor { - RangeErrorCtor(QV4::ExecutionContext *scope); + void init(QV4::ExecutionContext *scope); }; struct ReferenceErrorCtor : ErrorCtor { - ReferenceErrorCtor(QV4::ExecutionContext *scope); + void init(QV4::ExecutionContext *scope); }; struct SyntaxErrorCtor : ErrorCtor { - SyntaxErrorCtor(QV4::ExecutionContext *scope); + void init(QV4::ExecutionContext *scope); }; struct TypeErrorCtor : ErrorCtor { - TypeErrorCtor(QV4::ExecutionContext *scope); + void init(QV4::ExecutionContext *scope); }; struct URIErrorCtor : ErrorCtor { - URIErrorCtor(QV4::ExecutionContext *scope); + void init(QV4::ExecutionContext *scope); }; } @@ -221,50 +225,50 @@ struct ErrorCtor: FunctionObject { V4_OBJECT2(ErrorCtor, FunctionObject) - static ReturnedValue construct(const Managed *, CallData *callData); - static ReturnedValue call(const Managed *that, CallData *callData); + static void construct(const Managed *, Scope &scope, CallData *callData); + static void call(const Managed *that, Scope &scope, CallData *callData); }; struct EvalErrorCtor: ErrorCtor { V4_OBJECT2(EvalErrorCtor, ErrorCtor) - static ReturnedValue construct(const Managed *m, CallData *callData); + static void construct(const Managed *m, Scope &scope, CallData *callData); }; struct RangeErrorCtor: ErrorCtor { V4_OBJECT2(RangeErrorCtor, ErrorCtor) - static ReturnedValue construct(const Managed *m, CallData *callData); + static void construct(const Managed *, Scope &scope, CallData *callData); }; struct ReferenceErrorCtor: ErrorCtor { V4_OBJECT2(ReferenceErrorCtor, ErrorCtor) - static ReturnedValue construct(const Managed *m, CallData *callData); + static void construct(const Managed *m, Scope &scope, CallData *callData); }; struct SyntaxErrorCtor: ErrorCtor { V4_OBJECT2(SyntaxErrorCtor, ErrorCtor) - static ReturnedValue construct(const Managed *m, CallData *callData); + static void construct(const Managed *m, Scope &scope, CallData *callData); }; struct TypeErrorCtor: ErrorCtor { V4_OBJECT2(TypeErrorCtor, ErrorCtor) - static ReturnedValue construct(const Managed *m, CallData *callData); + static void construct(const Managed *m, Scope &scope, CallData *callData); }; struct URIErrorCtor: ErrorCtor { V4_OBJECT2(URIErrorCtor, ErrorCtor) - static ReturnedValue construct(const Managed *m, CallData *callData); + static void construct(const Managed *m, Scope &scope, CallData *callData); }; diff --git a/src/qml/jsruntime/qv4function.cpp b/src/qml/jsruntime/qv4function.cpp index f314e20863..caabee322a 100644 --- a/src/qml/jsruntime/qv4function.cpp +++ b/src/qml/jsruntime/qv4function.cpp @@ -59,7 +59,7 @@ Function::Function(ExecutionEngine *engine, CompiledData::CompilationUnit *unit, Q_UNUSED(engine); internalClass = engine->emptyClass; - const quint32 *formalsIndices = compiledFunction->formalsTable(); + const CompiledData::LEUInt32 *formalsIndices = compiledFunction->formalsTable(); // iterate backwards, so we get the right ordering for duplicate names Scope scope(engine); ScopedString arg(scope); @@ -78,7 +78,7 @@ Function::Function(ExecutionEngine *engine, CompiledData::CompilationUnit *unit, } nFormals = compiledFunction->nFormals; - const quint32 *localsIndices = compiledFunction->localsTable(); + const CompiledData::LEUInt32 *localsIndices = compiledFunction->localsTable(); for (quint32 i = 0; i < compiledFunction->nLocals; ++i) internalClass = internalClass->addMember(compilationUnit->runtimeStrings[localsIndices[i]]->identifier, Attr_NotConfigurable); @@ -110,7 +110,7 @@ void Function::updateInternalClass(ExecutionEngine *engine, const QList<QByteArr } nFormals = parameters.size(); - const quint32 *localsIndices = compiledFunction->localsTable(); + const CompiledData::LEUInt32 *localsIndices = compiledFunction->localsTable(); for (quint32 i = 0; i < compiledFunction->nLocals; ++i) internalClass = internalClass->addMember(compilationUnit->runtimeStrings[localsIndices[i]]->identifier, Attr_NotConfigurable); diff --git a/src/qml/jsruntime/qv4functionobject.cpp b/src/qml/jsruntime/qv4functionobject.cpp index 5d2c57a2ba..2cc58b74a6 100644 --- a/src/qml/jsruntime/qv4functionobject.cpp +++ b/src/qml/jsruntime/qv4functionobject.cpp @@ -68,78 +68,86 @@ using namespace QV4; DEFINE_OBJECT_VTABLE(FunctionObject); -Heap::FunctionObject::FunctionObject(QV4::ExecutionContext *scope, QV4::String *name, bool createProto) - : scope(scope->d()) - , function(Q_NULLPTR) +void Heap::FunctionObject::init(QV4::ExecutionContext *scope, QV4::String *name, bool createProto) { + Object::init(); + function = nullptr; + this->scope = scope->d(); Scope s(scope->engine()); ScopedFunctionObject f(s, this); f->init(name, createProto); } -Heap::FunctionObject::FunctionObject(QV4::ExecutionContext *scope, Function *function, bool createProto) - : scope(scope->d()) - , function(Q_NULLPTR) +void Heap::FunctionObject::init(QV4::ExecutionContext *scope, Function *function, bool createProto) { + Object::init(); + function = nullptr; + this->scope = scope->d(); Scope s(scope->engine()); ScopedString name(s, function->name()); ScopedFunctionObject f(s, this); f->init(name, createProto); } -Heap::FunctionObject::FunctionObject(QV4::ExecutionContext *scope, const QString &name, bool createProto) - : scope(scope->d()) - , function(Q_NULLPTR) +void Heap::FunctionObject::init(QV4::ExecutionContext *scope, const QString &name, bool createProto) { + Object::init(); + function = nullptr; + this->scope = scope->d(); Scope s(scope->engine()); ScopedFunctionObject f(s, this); ScopedString n(s, s.engine->newString(name)); f->init(n, createProto); } -Heap::FunctionObject::FunctionObject(ExecutionContext *scope, const QString &name, bool createProto) - : scope(scope) - , function(Q_NULLPTR) +void Heap::FunctionObject::init(ExecutionContext *scope, const QString &name, bool createProto) { + Object::init(); + function = nullptr; + this->scope = scope; Scope s(scope->engine); ScopedFunctionObject f(s, this); ScopedString n(s, s.engine->newString(name)); f->init(n, createProto); } -Heap::FunctionObject::FunctionObject(QV4::ExecutionContext *scope, const ReturnedValue name) - : scope(scope->d()) - , function(Q_NULLPTR) +void Heap::FunctionObject::init(QV4::ExecutionContext *scope, const ReturnedValue name) { + Object::init(); + function = nullptr; + this->scope = scope->d(); Scope s(scope); ScopedFunctionObject f(s, this); ScopedString n(s, name); f->init(n, false); } -Heap::FunctionObject::FunctionObject(ExecutionContext *scope, const ReturnedValue name) - : scope(scope) - , function(Q_NULLPTR) +void Heap::FunctionObject::init(ExecutionContext *scope, const ReturnedValue name) { + Object::init(); + function = nullptr; + this->scope = scope; Scope s(scope->engine); ScopedFunctionObject f(s, this); ScopedString n(s, name); f->init(n, false); } -Heap::FunctionObject::FunctionObject() - : scope(internalClass->engine->rootContext()->d()) - , function(Q_NULLPTR) +void Heap::FunctionObject::init() { + Object::init(); + function = nullptr; + this->scope = internalClass->engine->rootContext()->d(); Q_ASSERT(internalClass && internalClass->find(internalClass->engine->id_prototype()) == Index_Prototype); *propertyData(Index_Prototype) = Encode::undefined(); } -Heap::FunctionObject::~FunctionObject() +void Heap::FunctionObject::destroy() { if (function) function->compilationUnit->release(); + Object::destroy(); } void FunctionObject::init(String *n, bool createProto) @@ -166,22 +174,14 @@ ReturnedValue FunctionObject::name() const return get(scope()->engine->id_name()); } - -ReturnedValue FunctionObject::newInstance() +void FunctionObject::construct(const Managed *that, Scope &scope, CallData *) { - Scope scope(internalClass()->engine); - ScopedCallData callData(scope); - return construct(callData); + scope.result = static_cast<const FunctionObject *>(that)->engine()->throwTypeError(); } -ReturnedValue FunctionObject::construct(const Managed *that, CallData *) +void FunctionObject::call(const Managed *, Scope &scope, CallData *) { - return static_cast<const FunctionObject *>(that)->engine()->throwTypeError(); -} - -ReturnedValue FunctionObject::call(const Managed *, CallData *) -{ - return Encode::undefined(); + scope.result = Encode::undefined(); } void FunctionObject::markObjects(Heap::Base *that, ExecutionEngine *e) @@ -235,7 +235,7 @@ QQmlSourceLocation FunctionObject::sourceLocation() const { if (isBinding()) { Q_ASSERT(as<const QV4::QQmlBindingFunction>()); - return static_cast<QV4::Heap::QQmlBindingFunction *>(d())->bindingLocation; + return *static_cast<QV4::Heap::QQmlBindingFunction *>(d())->bindingLocation; } QV4::Function *function = d()->function; Q_ASSERT(function); @@ -245,15 +245,14 @@ QQmlSourceLocation FunctionObject::sourceLocation() const DEFINE_OBJECT_VTABLE(FunctionCtor); -Heap::FunctionCtor::FunctionCtor(QV4::ExecutionContext *scope) - : Heap::FunctionObject(scope, QStringLiteral("Function")) +void Heap::FunctionCtor::init(QV4::ExecutionContext *scope) { + Heap::FunctionObject::init(scope, QStringLiteral("Function")); } // 15.3.2 -ReturnedValue FunctionCtor::construct(const Managed *that, CallData *callData) +void FunctionCtor::construct(const Managed *that, Scope &scope, CallData *callData) { - Scope scope(static_cast<const Object *>(that)->engine()); Scoped<FunctionCtor> f(scope, static_cast<const FunctionCtor *>(that)); QString arguments; @@ -266,8 +265,10 @@ ReturnedValue FunctionCtor::construct(const Managed *that, CallData *callData) } body = callData->args[callData->argc - 1].toQString(); } - if (scope.engine->hasException) - return Encode::undefined(); + if (scope.engine->hasException) { + scope.result = Encode::undefined(); + return; + } QString function = QLatin1String("function(") + arguments + QLatin1String("){") + body + QLatin1Char('}'); @@ -278,15 +279,19 @@ ReturnedValue FunctionCtor::construct(const Managed *that, CallData *callData) const bool parsed = parser.parseExpression(); - if (!parsed) - return scope.engine->throwSyntaxError(QLatin1String("Parse error")); + if (!parsed) { + scope.result = scope.engine->throwSyntaxError(QLatin1String("Parse error")); + return; + } using namespace QQmlJS::AST; FunctionExpression *fe = QQmlJS::AST::cast<FunctionExpression *>(parser.rootNode()); - if (!fe) - return scope.engine->throwSyntaxError(QLatin1String("Parse error")); + if (!fe) { + scope.result = scope.engine->throwSyntaxError(QLatin1String("Parse error")); + return; + } - IR::Module module(scope.engine->debugger != 0); + IR::Module module(scope.engine->debugger() != 0); QQmlJS::RuntimeCodegen cg(scope.engine, f->strictMode()); cg.generateFromFunctionExpression(QString(), function, fe, &module); @@ -297,19 +302,20 @@ ReturnedValue FunctionCtor::construct(const Managed *that, CallData *callData) Function *vmf = compilationUnit->linkToEngine(scope.engine); ExecutionContext *global = scope.engine->rootContext(); - return FunctionObject::createScriptFunction(global, vmf)->asReturnedValue(); + scope.result = FunctionObject::createScriptFunction(global, vmf); } // 15.3.1: This is equivalent to new Function(...) -ReturnedValue FunctionCtor::call(const Managed *that, CallData *callData) +void FunctionCtor::call(const Managed *that, Scope &scope, CallData *callData) { - return construct(that, callData); + construct(that, scope, callData); } DEFINE_OBJECT_VTABLE(FunctionPrototype); -Heap::FunctionPrototype::FunctionPrototype() +void Heap::FunctionPrototype::init() { + Heap::FunctionObject::init(); } void FunctionPrototype::init(ExecutionEngine *engine, Object *ctor) @@ -376,7 +382,8 @@ ReturnedValue FunctionPrototype::method_apply(CallContext *ctx) } callData->thisObject = ctx->argument(0); - return o->call(callData); + o->call(scope, callData); + return scope.result.asReturnedValue(); } ReturnedValue FunctionPrototype::method_call(CallContext *ctx) @@ -393,7 +400,9 @@ ReturnedValue FunctionPrototype::method_call(CallContext *ctx) callData->args[i - 1] = ctx->args()[i]; } callData->thisObject = ctx->argument(0); - return o->call(callData); + + o->call(scope, callData); + return scope.result.asReturnedValue(); } ReturnedValue FunctionPrototype::method_bind(CallContext *ctx) @@ -417,24 +426,25 @@ ReturnedValue FunctionPrototype::method_bind(CallContext *ctx) DEFINE_OBJECT_VTABLE(ScriptFunction); -Heap::ScriptFunction::ScriptFunction(QV4::ExecutionContext *scope, Function *function) - : Heap::SimpleScriptFunction(scope, function, true) +void Heap::ScriptFunction::init(QV4::ExecutionContext *scope, Function *function) { + Heap::SimpleScriptFunction::init(scope, function, true); } -ReturnedValue ScriptFunction::construct(const Managed *that, CallData *callData) +void ScriptFunction::construct(const Managed *that, Scope &scope, CallData *callData) { - ExecutionEngine *v4 = static_cast<const Object *>(that)->engine(); - if (v4->hasException) - return Encode::undefined(); - CHECK_STACK_LIMITS(v4); + ExecutionEngine *v4 = scope.engine; + if (v4->hasException) { + scope.result = Encode::undefined(); + return; + } + CHECK_STACK_LIMITS(v4, scope); - Scope scope(v4); ExecutionContextSaver ctxSaver(scope); Scoped<ScriptFunction> f(scope, static_cast<const ScriptFunction *>(that)); - InternalClass *ic = scope.engine->emptyClass; + InternalClass *ic = v4->emptyClass; ScopedObject proto(scope, f->protoForConstructor()); ScopedObject obj(scope, v4->newObject(ic, proto)); @@ -442,45 +452,44 @@ ReturnedValue ScriptFunction::construct(const Managed *that, CallData *callData) Scoped<CallContext> ctx(scope, v4->currentContext->newCallContext(f, callData)); v4->pushContext(ctx); - ScopedValue result(scope, Q_V4_PROFILE(v4, f->function())); + scope.result = Q_V4_PROFILE(v4, f->function()); if (f->function()->compiledFunction->hasQmlDependencies()) - QQmlPropertyCapture::registerQmlDependencies(v4, f->function()->compiledFunction); - - if (v4->hasException) - return Encode::undefined(); + QQmlPropertyCapture::registerQmlDependencies(f->function()->compiledFunction, scope); - if (result->isObject()) - return result->asReturnedValue(); - return obj.asReturnedValue(); + if (v4->hasException) { + scope.result = Encode::undefined(); + } else if (!scope.result.isObject()) { + scope.result = obj.asReturnedValue(); + } } -ReturnedValue ScriptFunction::call(const Managed *that, CallData *callData) +void ScriptFunction::call(const Managed *that, Scope &scope, CallData *callData) { - ExecutionEngine *v4 = static_cast<const Object *>(that)->engine(); - if (v4->hasException) - return Encode::undefined(); - CHECK_STACK_LIMITS(v4); + ExecutionEngine *v4 = scope.engine; + if (v4->hasException) { + scope.result = Encode::undefined(); + return; + } + CHECK_STACK_LIMITS(v4, scope); - Scope scope(v4); ExecutionContextSaver ctxSaver(scope); Scoped<ScriptFunction> f(scope, static_cast<const ScriptFunction *>(that)); Scoped<CallContext> ctx(scope, v4->currentContext->newCallContext(f, callData)); v4->pushContext(ctx); - ScopedValue result(scope, Q_V4_PROFILE(v4, f->function())); + scope.result = Q_V4_PROFILE(v4, f->function()); if (f->function()->compiledFunction->hasQmlDependencies()) - QQmlPropertyCapture::registerQmlDependencies(scope.engine, f->function()->compiledFunction); - - return result->asReturnedValue(); + QQmlPropertyCapture::registerQmlDependencies(f->function()->compiledFunction, scope); } DEFINE_OBJECT_VTABLE(SimpleScriptFunction); -Heap::SimpleScriptFunction::SimpleScriptFunction(QV4::ExecutionContext *scope, Function *function, bool createProto) +void Heap::SimpleScriptFunction::init(QV4::ExecutionContext *scope, Function *function, bool createProto) { + FunctionObject::init(); this->scope = scope->d(); this->function = function; @@ -511,14 +520,15 @@ Heap::SimpleScriptFunction::SimpleScriptFunction(QV4::ExecutionContext *scope, F } } -ReturnedValue SimpleScriptFunction::construct(const Managed *that, CallData *callData) +void SimpleScriptFunction::construct(const Managed *that, Scope &scope, CallData *callData) { - ExecutionEngine *v4 = static_cast<const Object *>(that)->engine(); - if (v4->hasException) - return Encode::undefined(); - CHECK_STACK_LIMITS(v4); + ExecutionEngine *v4 = scope.engine; + if (v4->hasException) { + scope.result = Encode::undefined(); + return; + } + CHECK_STACK_LIMITS(v4, scope); - Scope scope(v4); ExecutionContextSaver ctxSaver(scope); Scoped<SimpleScriptFunction> f(scope, static_cast<const SimpleScriptFunction *>(that)); @@ -527,14 +537,13 @@ ReturnedValue SimpleScriptFunction::construct(const Managed *that, CallData *cal ScopedObject proto(scope, f->protoForConstructor()); callData->thisObject = v4->newObject(ic, proto); - CallContext::Data ctx(v4); - ctx.mm_data = 0; - ctx.setVtable(CallContext::staticVTable()); + CallContext::Data ctx = CallContext::Data::createOnStack(v4); ctx.strictMode = f->strictMode(); ctx.callData = callData; ctx.function = f->d(); ctx.compilationUnit = f->function()->compilationUnit; ctx.lookups = ctx.compilationUnit->runtimeLookups; + ctx.constantTable = ctx.compilationUnit->constants; ctx.outer = f->scope(); ctx.locals = scope.alloc(f->varCount()); for (int i = callData->argc; i < (int)f->formalParameterCount(); ++i) @@ -542,36 +551,38 @@ ReturnedValue SimpleScriptFunction::construct(const Managed *that, CallData *cal v4->pushContext(&ctx); Q_ASSERT(v4->current == &ctx); - ScopedObject result(scope, Q_V4_PROFILE(v4, f->function())); + scope.result = Q_V4_PROFILE(v4, f->function()); if (f->function()->compiledFunction->hasQmlDependencies()) - QQmlPropertyCapture::registerQmlDependencies(v4, f->function()->compiledFunction); + QQmlPropertyCapture::registerQmlDependencies(f->function()->compiledFunction, scope); - if (!result) - return callData->thisObject.asReturnedValue(); - return result.asReturnedValue(); + if (v4->hasException) { + scope.result = Encode::undefined(); + } else if (!scope.result.isObject()) { + scope.result = callData->thisObject; + } } -ReturnedValue SimpleScriptFunction::call(const Managed *that, CallData *callData) +void SimpleScriptFunction::call(const Managed *that, Scope &scope, CallData *callData) { - ExecutionEngine *v4 = static_cast<const SimpleScriptFunction *>(that)->internalClass()->engine; - if (v4->hasException) - return Encode::undefined(); - CHECK_STACK_LIMITS(v4); + ExecutionEngine *v4 = scope.engine; + if (v4->hasException) { + scope.result = Encode::undefined(); + return; + } + CHECK_STACK_LIMITS(v4, scope); - Scope scope(v4); ExecutionContextSaver ctxSaver(scope); Scoped<SimpleScriptFunction> f(scope, static_cast<const SimpleScriptFunction *>(that)); - CallContext::Data ctx(v4); - ctx.mm_data = 0; - ctx.setVtable(CallContext::staticVTable()); + CallContext::Data ctx = CallContext::Data::createOnStack(v4); ctx.strictMode = f->strictMode(); ctx.callData = callData; ctx.function = f->d(); ctx.compilationUnit = f->function()->compilationUnit; ctx.lookups = ctx.compilationUnit->runtimeLookups; + ctx.constantTable = ctx.compilationUnit->constants; ctx.outer = f->scope(); ctx.locals = scope.alloc(f->varCount()); for (int i = callData->argc; i < (int)f->formalParameterCount(); ++i) @@ -579,12 +590,10 @@ ReturnedValue SimpleScriptFunction::call(const Managed *that, CallData *callData v4->pushContext(&ctx); Q_ASSERT(v4->current == &ctx); - ScopedValue result(scope, Q_V4_PROFILE(v4, f->function())); + scope.result = Q_V4_PROFILE(v4, f->function()); if (f->function()->compiledFunction->hasQmlDependencies()) - QQmlPropertyCapture::registerQmlDependencies(v4, f->function()->compiledFunction); - - return result->asReturnedValue(); + QQmlPropertyCapture::registerQmlDependencies(f->function()->compiledFunction, scope); } Heap::Object *SimpleScriptFunction::protoForConstructor() @@ -600,71 +609,69 @@ Heap::Object *SimpleScriptFunction::protoForConstructor() DEFINE_OBJECT_VTABLE(BuiltinFunction); -Heap::BuiltinFunction::BuiltinFunction(QV4::ExecutionContext *scope, QV4::String *name, ReturnedValue (*code)(QV4::CallContext *)) - : Heap::FunctionObject(scope, name) - , code(code) +void Heap::BuiltinFunction::init(QV4::ExecutionContext *scope, QV4::String *name, ReturnedValue (*code)(QV4::CallContext *)) { + Heap::FunctionObject::init(scope, name); + this->code = code; } -ReturnedValue BuiltinFunction::construct(const Managed *f, CallData *) +void BuiltinFunction::construct(const Managed *f, Scope &scope, CallData *) { - return static_cast<const BuiltinFunction *>(f)->internalClass()->engine->throwTypeError(); + scope.result = static_cast<const BuiltinFunction *>(f)->internalClass()->engine->throwTypeError(); } -ReturnedValue BuiltinFunction::call(const Managed *that, CallData *callData) +void BuiltinFunction::call(const Managed *that, Scope &scope, CallData *callData) { const BuiltinFunction *f = static_cast<const BuiltinFunction *>(that); - ExecutionEngine *v4 = f->internalClass()->engine; - if (v4->hasException) - return Encode::undefined(); - CHECK_STACK_LIMITS(v4); + ExecutionEngine *v4 = scope.engine; + if (v4->hasException) { + scope.result = Encode::undefined(); + return; + } + CHECK_STACK_LIMITS(v4, scope); - Scope scope(v4); ExecutionContextSaver ctxSaver(scope); - CallContext::Data ctx(v4); - ctx.mm_data = 0; - ctx.setVtable(CallContext::staticVTable()); + CallContext::Data ctx = CallContext::Data::createOnStack(v4); ctx.strictMode = f->scope()->strictMode; // ### needed? scope or parent context? ctx.callData = callData; v4->pushContext(&ctx); Q_ASSERT(v4->current == &ctx); - return f->d()->code(static_cast<QV4::CallContext *>(v4->currentContext)); + scope.result = f->d()->code(static_cast<QV4::CallContext *>(v4->currentContext)); } -ReturnedValue IndexedBuiltinFunction::call(const Managed *that, CallData *callData) +void IndexedBuiltinFunction::call(const Managed *that, Scope &scope, CallData *callData) { const IndexedBuiltinFunction *f = static_cast<const IndexedBuiltinFunction *>(that); - ExecutionEngine *v4 = f->internalClass()->engine; - if (v4->hasException) - return Encode::undefined(); - CHECK_STACK_LIMITS(v4); + ExecutionEngine *v4 = scope.engine; + if (v4->hasException) { + scope.result = Encode::undefined(); + return; + } + CHECK_STACK_LIMITS(v4, scope); - Scope scope(v4); ExecutionContextSaver ctxSaver(scope); - CallContext::Data ctx(v4); - ctx.mm_data = 0; - ctx.setVtable(CallContext::staticVTable()); + CallContext::Data ctx = CallContext::Data::createOnStack(v4); ctx.strictMode = f->scope()->strictMode; // ### needed? scope or parent context? ctx.callData = callData; v4->pushContext(&ctx); Q_ASSERT(v4->current == &ctx); - return f->d()->code(static_cast<QV4::CallContext *>(v4->currentContext), f->d()->index); + scope.result = f->d()->code(static_cast<QV4::CallContext *>(v4->currentContext), f->d()->index); } DEFINE_OBJECT_VTABLE(IndexedBuiltinFunction); DEFINE_OBJECT_VTABLE(BoundFunction); -Heap::BoundFunction::BoundFunction(QV4::ExecutionContext *scope, QV4::FunctionObject *target, - const Value &boundThis, QV4::MemberData *boundArgs) - : Heap::FunctionObject(scope, QStringLiteral("__bound function__")) - , target(target->d()) - , boundArgs(boundArgs ? boundArgs->d() : 0) +void Heap::BoundFunction::init(QV4::ExecutionContext *scope, QV4::FunctionObject *target, + const Value &boundThis, QV4::MemberData *boundArgs) { + Heap::FunctionObject::init(scope, QStringLiteral("__bound function__")); + this->target = target->d(); + this->boundArgs = boundArgs ? boundArgs->d() : 0; this->boundThis = boundThis; Scope s(scope); @@ -685,12 +692,13 @@ Heap::BoundFunction::BoundFunction(QV4::ExecutionContext *scope, QV4::FunctionOb f->insertMember(s.engine->id_caller(), pd, Attr_Accessor|Attr_NotConfigurable|Attr_NotEnumerable); } -ReturnedValue BoundFunction::call(const Managed *that, CallData *dd) +void BoundFunction::call(const Managed *that, Scope &scope, CallData *dd) { const BoundFunction *f = static_cast<const BoundFunction *>(that); - Scope scope(f->engine()); - if (scope.hasException()) - return Encode::undefined(); + if (scope.hasException()) { + scope.result = Encode::undefined(); + return; + } Scoped<MemberData> boundArgs(scope, f->boundArgs()); ScopedCallData callData(scope, (boundArgs ? boundArgs->size() : 0) + dd->argc); @@ -702,15 +710,16 @@ ReturnedValue BoundFunction::call(const Managed *that, CallData *dd) } memcpy(argp, dd->args, dd->argc*sizeof(Value)); ScopedFunctionObject t(scope, f->target()); - return t->call(callData); + t->call(scope, callData); } -ReturnedValue BoundFunction::construct(const Managed *that, CallData *dd) +void BoundFunction::construct(const Managed *that, Scope &scope, CallData *dd) { const BoundFunction *f = static_cast<const BoundFunction *>(that); - Scope scope(f->engine()); - if (scope.hasException()) - return Encode::undefined(); + if (scope.hasException()) { + scope.result = Encode::undefined(); + return; + } Scoped<MemberData> boundArgs(scope, f->boundArgs()); ScopedCallData callData(scope, (boundArgs ? boundArgs->size() : 0) + dd->argc); @@ -721,7 +730,7 @@ ReturnedValue BoundFunction::construct(const Managed *that, CallData *dd) } memcpy(argp, dd->args, dd->argc*sizeof(Value)); ScopedFunctionObject t(scope, f->target()); - return t->construct(callData); + t->construct(scope, callData); } void BoundFunction::markObjects(Heap::Base *that, ExecutionEngine *e) diff --git a/src/qml/jsruntime/qv4functionobject_p.h b/src/qml/jsruntime/qv4functionobject_p.h index 4a4545eca4..e58b83e2c3 100644 --- a/src/qml/jsruntime/qv4functionobject_p.h +++ b/src/qml/jsruntime/qv4functionobject_p.h @@ -69,14 +69,14 @@ struct Q_QML_PRIVATE_EXPORT FunctionObject : Object { Index_ProtoConstructor = 0 }; - FunctionObject(QV4::ExecutionContext *scope, QV4::String *name, bool createProto = false); - FunctionObject(QV4::ExecutionContext *scope, QV4::Function *function, bool createProto = false); - FunctionObject(QV4::ExecutionContext *scope, const QString &name = QString(), bool createProto = false); - FunctionObject(ExecutionContext *scope, const QString &name = QString(), bool createProto = false); - FunctionObject(QV4::ExecutionContext *scope, const ReturnedValue name); - FunctionObject(ExecutionContext *scope, const ReturnedValue name); - FunctionObject(); - ~FunctionObject(); + void init(QV4::ExecutionContext *scope, QV4::String *name, bool createProto = false); + void init(QV4::ExecutionContext *scope, QV4::Function *function, bool createProto = false); + void init(QV4::ExecutionContext *scope, const QString &name = QString(), bool createProto = false); + void init(ExecutionContext *scope, const QString &name = QString(), bool createProto = false); + void init(QV4::ExecutionContext *scope, const ReturnedValue name); + void init(ExecutionContext *scope, const ReturnedValue name); + void init(); + void destroy(); unsigned int formalParameterCount() { return function ? function->nFormals : 0; } unsigned int varCount() { return function ? function->compiledFunction->nLocals : 0; } @@ -87,20 +87,20 @@ struct Q_QML_PRIVATE_EXPORT FunctionObject : Object { }; struct FunctionCtor : FunctionObject { - FunctionCtor(QV4::ExecutionContext *scope); + void init(QV4::ExecutionContext *scope); }; struct FunctionPrototype : FunctionObject { - FunctionPrototype(); + void init(); }; struct Q_QML_EXPORT BuiltinFunction : FunctionObject { - BuiltinFunction(QV4::ExecutionContext *scope, QV4::String *name, ReturnedValue (*code)(QV4::CallContext *)); + void init(QV4::ExecutionContext *scope, QV4::String *name, ReturnedValue (*code)(QV4::CallContext *)); ReturnedValue (*code)(QV4::CallContext *); }; struct IndexedBuiltinFunction : FunctionObject { - inline IndexedBuiltinFunction(QV4::ExecutionContext *scope, uint index, ReturnedValue (*code)(QV4::CallContext *ctx, uint index)); + inline void init(QV4::ExecutionContext *scope, uint index, ReturnedValue (*code)(QV4::CallContext *ctx, uint index)); ReturnedValue (*code)(QV4::CallContext *, uint index); uint index; }; @@ -110,15 +110,15 @@ struct SimpleScriptFunction : FunctionObject { Index_Name = FunctionObject::Index_Prototype + 1, Index_Length }; - SimpleScriptFunction(QV4::ExecutionContext *scope, Function *function, bool createProto); + void init(QV4::ExecutionContext *scope, Function *function, bool createProto); }; struct ScriptFunction : SimpleScriptFunction { - ScriptFunction(QV4::ExecutionContext *scope, Function *function); + void init(QV4::ExecutionContext *scope, Function *function); }; struct BoundFunction : FunctionObject { - BoundFunction(QV4::ExecutionContext *scope, QV4::FunctionObject *target, const Value &boundThis, QV4::MemberData *boundArgs); + void init(QV4::ExecutionContext *scope, QV4::FunctionObject *target, const Value &boundThis, QV4::MemberData *boundArgs); Pointer<FunctionObject> target; Value boundThis; Pointer<MemberData> boundArgs; @@ -145,12 +145,10 @@ struct Q_QML_EXPORT FunctionObject: Object { void init(String *name, bool createProto); - ReturnedValue newInstance(); - using Object::construct; using Object::call; - static ReturnedValue construct(const Managed *that, CallData *); - static ReturnedValue call(const Managed *that, CallData *d); + static void construct(const Managed *that, Scope &scope, CallData *); + static void call(const Managed *that, Scope &scope, CallData *d); static Heap::FunctionObject *createScriptFunction(ExecutionContext *scope, Function *function, bool createProto = true); static Heap::FunctionObject *createQmlFunction(QQmlContextData *qmlContext, QObject *scopeObject, QV4::Function *runtimeFunction, @@ -178,8 +176,8 @@ struct FunctionCtor: FunctionObject { V4_OBJECT2(FunctionCtor, FunctionObject) - static ReturnedValue construct(const Managed *that, CallData *callData); - static ReturnedValue call(const Managed *that, CallData *callData); + static void construct(const Managed *that, Scope &scope, CallData *callData); + static void call(const Managed *that, Scope &scope, CallData *callData); }; struct FunctionPrototype: FunctionObject @@ -202,28 +200,28 @@ struct Q_QML_EXPORT BuiltinFunction: FunctionObject { return scope->engine()->memoryManager->allocObject<BuiltinFunction>(scope, name, code); } - static ReturnedValue construct(const Managed *, CallData *); - static ReturnedValue call(const Managed *that, CallData *callData); + static void construct(const Managed *, Scope &scope, CallData *); + static void call(const Managed *that, Scope &scope, CallData *callData); }; struct IndexedBuiltinFunction: FunctionObject { V4_OBJECT2(IndexedBuiltinFunction, FunctionObject) - static ReturnedValue construct(const Managed *m, CallData *) + static void construct(const Managed *m, Scope &scope, CallData *) { - return static_cast<const IndexedBuiltinFunction *>(m)->engine()->throwTypeError(); + scope.result = static_cast<const IndexedBuiltinFunction *>(m)->engine()->throwTypeError(); } - static ReturnedValue call(const Managed *that, CallData *callData); + static void call(const Managed *that, Scope &scope, CallData *callData); }; -Heap::IndexedBuiltinFunction::IndexedBuiltinFunction(QV4::ExecutionContext *scope, uint index, - ReturnedValue (*code)(QV4::CallContext *ctx, uint index)) - : Heap::FunctionObject(scope), - code(code) - , index(index) +void Heap::IndexedBuiltinFunction::init(QV4::ExecutionContext *scope, uint index, + ReturnedValue (*code)(QV4::CallContext *ctx, uint index)) { + Heap::FunctionObject::init(scope); + this->index = index; + this->code = code; } @@ -231,8 +229,8 @@ struct SimpleScriptFunction: FunctionObject { V4_OBJECT2(SimpleScriptFunction, FunctionObject) V4_INTERNALCLASS(simpleScriptFunctionClass) - static ReturnedValue construct(const Managed *, CallData *callData); - static ReturnedValue call(const Managed *that, CallData *callData); + static void construct(const Managed *, Scope &scope, CallData *callData); + static void call(const Managed *that, Scope &scope, CallData *callData); Heap::Object *protoForConstructor(); }; @@ -240,8 +238,8 @@ struct SimpleScriptFunction: FunctionObject { struct ScriptFunction: SimpleScriptFunction { V4_OBJECT2(ScriptFunction, FunctionObject) - static ReturnedValue construct(const Managed *, CallData *callData); - static ReturnedValue call(const Managed *that, CallData *callData); + static void construct(const Managed *, Scope &scope, CallData *callData); + static void call(const Managed *that, Scope &scope, CallData *callData); }; @@ -257,8 +255,8 @@ struct BoundFunction: FunctionObject { Value boundThis() const { return d()->boundThis; } Heap::MemberData *boundArgs() const { return d()->boundArgs; } - static ReturnedValue construct(const Managed *, CallData *d); - static ReturnedValue call(const Managed *that, CallData *dd); + static void construct(const Managed *, Scope &scope, CallData *d); + static void call(const Managed *that, Scope &scope, CallData *dd); static void markObjects(Heap::Base *that, ExecutionEngine *e); }; diff --git a/src/qml/jsruntime/qv4globalobject.cpp b/src/qml/jsruntime/qv4globalobject.cpp index 2c767e3302..feb0d90d26 100644 --- a/src/qml/jsruntime/qv4globalobject.cpp +++ b/src/qml/jsruntime/qv4globalobject.cpp @@ -330,21 +330,22 @@ static QString decode(const QString &input, DecodeMode decodeMode, bool *ok) DEFINE_OBJECT_VTABLE(EvalFunction); -Heap::EvalFunction::EvalFunction(QV4::ExecutionContext *scope) - : Heap::FunctionObject(scope, scope->d()->engine->id_eval()) +void Heap::EvalFunction::init(QV4::ExecutionContext *scope) { + Heap::FunctionObject::init(scope, scope->d()->engine->id_eval()); Scope s(scope); ScopedFunctionObject f(s, this); f->defineReadonlyProperty(s.engine->id_length(), Primitive::fromInt32(1)); } -ReturnedValue EvalFunction::evalCall(CallData *callData, bool directCall) const +void EvalFunction::evalCall(Scope &scope, CallData *callData, bool directCall) const { - if (callData->argc < 1) - return Encode::undefined(); + if (callData->argc < 1) { + scope.result = Encode::undefined(); + return; + } ExecutionEngine *v4 = engine(); - Scope scope(v4); ExecutionContextSaver ctxSaver(scope); ExecutionContext *currentContext = v4->currentContext; @@ -356,8 +357,10 @@ ReturnedValue EvalFunction::evalCall(CallData *callData, bool directCall) const ctx = v4->pushGlobalContext(); } - if (!callData->args[0].isString()) - return callData->args[0].asReturnedValue(); + if (!callData->args[0].isString()) { + scope.result = callData->args[0].asReturnedValue(); + return; + } const QString code = callData->args[0].stringValue()->toQString(); bool inheritContext = !ctx->d()->strictMode; @@ -366,18 +369,23 @@ ReturnedValue EvalFunction::evalCall(CallData *callData, bool directCall) const script.strictMode = (directCall && currentContext->d()->strictMode); script.inheritContext = inheritContext; script.parse(); - if (v4->hasException) - return Encode::undefined(); + if (v4->hasException) { + scope.result = Encode::undefined(); + return; + } Function *function = script.function(); - if (!function) - return Encode::undefined(); + if (!function) { + scope.result = Encode::undefined(); + return; + } if (function->isStrict() || (ctx->d()->strictMode)) { ScopedFunctionObject e(scope, FunctionObject::createScriptFunction(ctx, function)); ScopedCallData callData(scope, 0); callData->thisObject = ctx->thisObject(); - return e->call(callData); + e->call(scope, callData); + return; } ContextStateSaver stateSaver(scope, ctx); @@ -386,14 +394,14 @@ ReturnedValue EvalFunction::evalCall(CallData *callData, bool directCall) const ctx->d()->strictMode = false; ctx->d()->compilationUnit = function->compilationUnit; - return Q_V4_PROFILE(ctx->engine(), function); + scope.result = Q_V4_PROFILE(ctx->engine(), function); } -ReturnedValue EvalFunction::call(const Managed *that, CallData *callData) +void EvalFunction::call(const Managed *that, Scope &scope, CallData *callData) { // indirect call - return static_cast<const EvalFunction *>(that)->evalCall(callData, false); + static_cast<const EvalFunction *>(that)->evalCall(scope, callData, false); } @@ -512,7 +520,7 @@ ReturnedValue GlobalFunctions::method_parseFloat(CallContext *ctx) if (trimmed.startsWith(QLatin1String("Infinity")) || trimmed.startsWith(QLatin1String("+Infinity"))) return Encode(Q_INFINITY); - if (trimmed.startsWith(QStringLiteral("-Infinity"))) + if (trimmed.startsWith(QLatin1String("-Infinity"))) return Encode(-Q_INFINITY); QByteArray ba = trimmed.toLatin1(); bool ok; diff --git a/src/qml/jsruntime/qv4globalobject_p.h b/src/qml/jsruntime/qv4globalobject_p.h index ea7a3b06ce..e8b3a92d34 100644 --- a/src/qml/jsruntime/qv4globalobject_p.h +++ b/src/qml/jsruntime/qv4globalobject_p.h @@ -60,7 +60,7 @@ namespace QV4 { namespace Heap { struct EvalFunction : FunctionObject { - EvalFunction(QV4::ExecutionContext *scope); + void init(QV4::ExecutionContext *scope); }; } @@ -69,9 +69,9 @@ struct Q_QML_EXPORT EvalFunction : FunctionObject { V4_OBJECT2(EvalFunction, FunctionObject) - ReturnedValue evalCall(CallData *callData, bool directCall) const; + void evalCall(Scope &scope, CallData *callData, bool directCall) const; - static ReturnedValue call(const Managed *that, CallData *callData); + static void call(const Managed *that, Scope &scope, CallData *callData); }; struct GlobalFunctions diff --git a/src/qml/jsruntime/qv4identifier.cpp b/src/qml/jsruntime/qv4identifier.cpp index c8d66b1254..6260fd0cc8 100644 --- a/src/qml/jsruntime/qv4identifier.cpp +++ b/src/qml/jsruntime/qv4identifier.cpp @@ -64,12 +64,33 @@ IdentifierHashData::IdentifierHashData(int numBits) memset(entries, 0, alloc*sizeof(IdentifierHashEntry)); } +IdentifierHashData::IdentifierHashData(IdentifierHashData *other) + : size(other->size) + , numBits(other->numBits) + , identifierTable(other->identifierTable) +{ + refCount.store(1); + alloc = other->alloc; + entries = (IdentifierHashEntry *)malloc(alloc*sizeof(IdentifierHashEntry)); + memcpy(entries, other->entries, alloc*sizeof(IdentifierHashEntry)); +} + IdentifierHashBase::IdentifierHashBase(ExecutionEngine *engine) { d = new IdentifierHashData(3); d->identifierTable = engine->identifierTable; } +void IdentifierHashBase::detach() +{ + if (!d || d->refCount == 1) + return; + IdentifierHashData *newData = new IdentifierHashData(d); + if (d && !d->refCount.deref()) + delete d; + d = newData; +} + IdentifierHashEntry *IdentifierHashBase::addEntry(const Identifier *identifier) { @@ -131,7 +152,7 @@ const IdentifierHashEntry *IdentifierHashBase::lookup(const QString &str) const return 0; Q_ASSERT(d->entries); - uint hash = String::createHashValue(str.constData(), str.length()); + uint hash = String::createHashValue(str.constData(), str.length(), Q_NULLPTR); uint idx = hash % d->alloc; while (1) { if (!d->entries[idx].identifier) diff --git a/src/qml/jsruntime/qv4identifier_p.h b/src/qml/jsruntime/qv4identifier_p.h index a3abfd8e13..2695bbc875 100644 --- a/src/qml/jsruntime/qv4identifier_p.h +++ b/src/qml/jsruntime/qv4identifier_p.h @@ -85,6 +85,7 @@ struct IdentifierHashEntry { struct IdentifierHashData { IdentifierHashData(int numBits); + explicit IdentifierHashData(IdentifierHashData *other); ~IdentifierHashData() { free(entries); } @@ -115,6 +116,8 @@ struct IdentifierHashBase bool contains(const QString &str) const; bool contains(String *str) const; + void detach(); + protected: IdentifierHashEntry *addEntry(const Identifier *i); const IdentifierHashEntry *lookup(const Identifier *identifier) const; @@ -141,6 +144,7 @@ struct IdentifierHash : public IdentifierHashBase } void add(const QString &str, const T &value); + void add(Heap::String *str, const T &value); inline T value(const QString &str) const; inline T value(String *str) const; @@ -198,6 +202,13 @@ void IdentifierHash<T>::add(const QString &str, const T &value) } template<typename T> +void IdentifierHash<T>::add(Heap::String *str, const T &value) +{ + IdentifierHashEntry *e = addEntry(toIdentifier(str)); + e->value = value; +} + +template<typename T> inline T IdentifierHash<T>::value(const QString &str) const { return IdentifierHashEntry::get(lookup(str), (T*)0); @@ -223,7 +234,6 @@ QString IdentifierHash<T>::findId(T value) const return QString(); } - } QT_END_NAMESPACE diff --git a/src/qml/jsruntime/qv4identifiertable.cpp b/src/qml/jsruntime/qv4identifiertable.cpp index 5adb17b4ea..3def6defbf 100644 --- a/src/qml/jsruntime/qv4identifiertable.cpp +++ b/src/qml/jsruntime/qv4identifiertable.cpp @@ -118,7 +118,8 @@ void IdentifierTable::addEntry(Heap::String *str) Heap::String *IdentifierTable::insertString(const QString &s) { - uint hash = String::createHashValue(s.constData(), s.length()); + uint subtype; + uint hash = String::createHashValue(s.constData(), s.length(), &subtype); uint idx = hash % alloc; while (Heap::String *e = entries[idx]) { if (e->stringHash == hash && e->toQString() == s) @@ -128,6 +129,8 @@ Heap::String *IdentifierTable::insertString(const QString &s) } Heap::String *str = engine->newString(s); + str->stringHash = hash; + str->subtype = subtype; addEntry(str); return str; } @@ -178,7 +181,8 @@ Identifier *IdentifierTable::identifier(const QString &s) Identifier *IdentifierTable::identifier(const char *s, int len) { - uint hash = String::createHashValue(s, len); + uint subtype; + uint hash = String::createHashValue(s, len, &subtype); if (hash == UINT_MAX) return identifier(QString::fromUtf8(s, len)); @@ -192,6 +196,8 @@ Identifier *IdentifierTable::identifier(const char *s, int len) } Heap::String *str = engine->newString(QString::fromLatin1(s, len)); + str->stringHash = hash; + str->subtype = subtype; addEntry(str); return str->identifier; } diff --git a/src/qml/jsruntime/qv4include.cpp b/src/qml/jsruntime/qv4include.cpp index 29f83da522..be8057e9f5 100644 --- a/src/qml/jsruntime/qv4include.cpp +++ b/src/qml/jsruntime/qv4include.cpp @@ -41,8 +41,10 @@ #include "qv4scopedvalue_p.h" #include <QtQml/qjsengine.h> +#if QT_CONFIG(qml_network) #include <QtNetwork/qnetworkrequest.h> #include <QtNetwork/qnetworkreply.h> +#endif #include <QtCore/qfile.h> #include <QtQml/qqmlfile.h> @@ -57,7 +59,10 @@ QT_BEGIN_NAMESPACE QV4Include::QV4Include(const QUrl &url, QV4::ExecutionEngine *engine, QV4::QmlContext *qmlContext, const QV4::Value &callback) - : v4(engine), m_network(0), m_reply(0), m_url(url), m_redirectCount(0) + : v4(engine), m_url(url) +#if QT_CONFIG(qml_network) + , m_redirectCount(0), m_network(0) , m_reply(0) +#endif { if (qmlContext) m_qmlContext.set(engine, *qmlContext); @@ -66,6 +71,7 @@ QV4Include::QV4Include(const QUrl &url, QV4::ExecutionEngine *engine, m_resultObject.set(v4, resultValue(v4)); +#if QT_CONFIG(qml_network) m_network = engine->v8Engine->networkAccessManager(); QNetworkRequest request; @@ -73,11 +79,17 @@ QV4Include::QV4Include(const QUrl &url, QV4::ExecutionEngine *engine, m_reply = m_network->get(request); QObject::connect(m_reply, SIGNAL(finished()), this, SLOT(finished())); +#else + finished(); +#endif } QV4Include::~QV4Include() { - delete m_reply; m_reply = 0; +#if QT_CONFIG(qml_network) + delete m_reply; + m_reply = 0; +#endif } QV4::ReturnedValue QV4Include::resultValue(QV4::ExecutionEngine *v4, Status status) @@ -110,7 +122,7 @@ void QV4Include::callback(const QV4::Value &callback, const QV4::Value &status) QV4::ScopedCallData callData(scope, 1); callData->thisObject = v4->globalObject->asReturnedValue(); callData->args[0] = status; - f->call(callData); + f->call(scope, callData); if (scope.hasException()) scope.engine->catchException(); } @@ -123,6 +135,7 @@ QV4::ReturnedValue QV4Include::result() #define INCLUDE_MAXIMUM_REDIRECT_RECURSION 15 void QV4Include::finished() { +#if QT_CONFIG(qml_network) m_redirectCount++; if (m_redirectCount < INCLUDE_MAXIMUM_REDIRECT_RECURSION) { @@ -166,6 +179,12 @@ void QV4Include::finished() } else { resultObj->put(status, QV4::ScopedValue(scope, QV4::Primitive::fromInt32(NetworkError))); } +#else + QV4::Scope scope(v4); + QV4::ScopedObject resultObj(scope, m_resultObject.value()); + QV4::ScopedString status(scope, v4->newString(QStringLiteral("status"))); + resultObj->put(status, QV4::ScopedValue(scope, QV4::Primitive::fromInt32(NetworkError))); +#endif // qml_network QV4::ScopedValue cb(scope, m_callbackFunction.value()); callback(cb, resultObj); @@ -188,14 +207,15 @@ QV4::ReturnedValue QV4Include::method_include(QV4::CallContext *ctx) if (!context || !context->isJSContext) V4THROW_ERROR("Qt.include(): Can only be called from JavaScript files"); - QUrl url(scope.engine->resolvedUrl(ctx->args()[0].toQStringNoThrow())); - if (scope.engine->qmlEngine() && scope.engine->qmlEngine()->urlInterceptor()) - url = scope.engine->qmlEngine()->urlInterceptor()->intercept(url, QQmlAbstractUrlInterceptor::JavaScriptFile); - QV4::ScopedValue callbackFunction(scope, QV4::Primitive::undefinedValue()); if (ctx->argc() >= 2 && ctx->args()[1].as<QV4::FunctionObject>()) callbackFunction = ctx->args()[1]; +#if QT_CONFIG(qml_network) + QUrl url(scope.engine->resolvedUrl(ctx->args()[0].toQStringNoThrow())); + if (scope.engine->qmlEngine() && scope.engine->qmlEngine()->urlInterceptor()) + url = scope.engine->qmlEngine()->urlInterceptor()->intercept(url, QQmlAbstractUrlInterceptor::JavaScriptFile); + QString localFile = QQmlFile::urlToLocalFileOrQrc(url); QV4::ScopedValue result(scope); @@ -243,6 +263,12 @@ QV4::ReturnedValue QV4Include::method_include(QV4::CallContext *ctx) } return result->asReturnedValue(); +#else + QV4::ScopedValue result(scope); + result = resultValue(scope.engine, NetworkError); + callback(callbackFunction, result); + return result->asReturnedValue(); +#endif } QT_END_NAMESPACE diff --git a/src/qml/jsruntime/qv4include_p.h b/src/qml/jsruntime/qv4include_p.h index 257dc05e65..4c601a5e7b 100644 --- a/src/qml/jsruntime/qv4include_p.h +++ b/src/qml/jsruntime/qv4include_p.h @@ -62,7 +62,9 @@ QT_BEGIN_NAMESPACE class QQmlEngine; +#if QT_CONFIG(qml_network) class QNetworkAccessManager; +#endif class QNetworkReply; class QV4Include : public QObject { @@ -90,15 +92,16 @@ private: static void callback(const QV4::Value &callback, const QV4::Value &status); QV4::ExecutionEngine *v4; - QNetworkAccessManager *m_network; - QPointer<QNetworkReply> m_reply; - QUrl m_url; + +#if QT_CONFIG(qml_network) int m_redirectCount; + QNetworkAccessManager *m_network; + QPointer<QNetworkReply> m_reply; +#endif QV4::PersistentValue m_callbackFunction; QV4::PersistentValue m_resultObject; - QV4::PersistentValue m_qmlContext; }; diff --git a/src/qml/jsruntime/qv4internalclass.cpp b/src/qml/jsruntime/qv4internalclass.cpp index 2f69defd5b..d17da9af0c 100644 --- a/src/qml/jsruntime/qv4internalclass.cpp +++ b/src/qml/jsruntime/qv4internalclass.cpp @@ -101,20 +101,6 @@ void PropertyHash::addEntry(const PropertyHash::Entry &entry, int classSize) ++d->size; } -uint PropertyHash::lookup(const Identifier *identifier) const -{ - Q_ASSERT(d->entries); - - uint idx = identifier->hashValue % d->alloc; - while (1) { - if (d->entries[idx].identifier == identifier) - return d->entries[idx].index; - if (!d->entries[idx].identifier) - return UINT_MAX; - ++idx; - idx %= d->alloc; - } -} InternalClass::InternalClass(ExecutionEngine *engine) : engine(engine) @@ -364,18 +350,6 @@ void InternalClass::removeMember(Object *object, Identifier *id) Q_ASSERT(t.lookup); } -uint InternalClass::find(const String *string) -{ - engine->identifierTable->identifier(string); - const Identifier *id = string->d()->identifier; - - uint index = propertyTable.lookup(id); - if (index < size) - return index; - - return UINT_MAX; -} - uint InternalClass::find(const Identifier *id) { uint index = propertyTable.lookup(id); @@ -433,11 +407,13 @@ InternalClass *InternalClass::propertiesFrozen() const void InternalClass::destroy() { - QList<InternalClass *> destroyStack; - destroyStack.append(this); + std::vector<InternalClass *> destroyStack; + destroyStack.reserve(64); + destroyStack.push_back(this); - while (!destroyStack.isEmpty()) { - InternalClass *next = destroyStack.takeLast(); + while (!destroyStack.empty()) { + InternalClass *next = destroyStack.back(); + destroyStack.pop_back(); if (!next->engine) continue; next->engine = 0; @@ -445,13 +421,13 @@ void InternalClass::destroy() next->nameMap.~SharedInternalClassData<Identifier *>(); next->propertyData.~SharedInternalClassData<PropertyAttributes>(); if (next->m_sealed) - destroyStack.append(next->m_sealed); + destroyStack.push_back(next->m_sealed); if (next->m_frozen) - destroyStack.append(next->m_frozen); + destroyStack.push_back(next->m_frozen); for (size_t i = 0; i < next->transitions.size(); ++i) { Q_ASSERT(next->transitions.at(i).lookup); - destroyStack.append(next->transitions.at(i).lookup); + destroyStack.push_back(next->transitions.at(i).lookup); } next->transitions.~vector<Transition>(); diff --git a/src/qml/jsruntime/qv4internalclass_p.h b/src/qml/jsruntime/qv4internalclass_p.h index c10af4ce01..dcda949c97 100644 --- a/src/qml/jsruntime/qv4internalclass_p.h +++ b/src/qml/jsruntime/qv4internalclass_p.h @@ -54,6 +54,8 @@ #include <QHash> #include <private/qqmljsmemorypool_p.h> +#include <private/qv4engine_p.h> +#include <private/qv4identifiertable_p.h> QT_BEGIN_NAMESPACE @@ -117,6 +119,20 @@ inline PropertyHash::~PropertyHash() delete d; } +inline uint PropertyHash::lookup(const Identifier *identifier) const +{ + Q_ASSERT(d->entries); + + uint idx = identifier->hashValue % d->alloc; + while (1) { + if (d->entries[idx].identifier == identifier) + return d->entries[idx].index; + if (!d->entries[idx].identifier) + return UINT_MAX; + ++idx; + idx %= d->alloc; + } +} template <typename T> struct SharedInternalClassData { @@ -245,7 +261,7 @@ struct InternalClass : public QQmlJS::Managed { InternalClass *changeMember(Identifier *identifier, PropertyAttributes data, uint *index = 0); static void changeMember(Object *object, String *string, PropertyAttributes data, uint *index = 0); static void removeMember(Object *object, Identifier *id); - uint find(const String *s); + uint find(const String *string); uint find(const Identifier *id); InternalClass *sealed(); @@ -261,6 +277,18 @@ private: InternalClass(const InternalClass &other); }; +inline uint InternalClass::find(const String *string) +{ + engine->identifierTable->identifier(string); + const Identifier *id = string->d()->identifier; + + uint index = propertyTable.lookup(id); + if (index < size) + return index; + + return UINT_MAX; +} + struct InternalClassPool : public QQmlJS::MemoryPool { void markObjects(ExecutionEngine *engine); diff --git a/src/qml/jsruntime/qv4jsonobject.cpp b/src/qml/jsruntime/qv4jsonobject.cpp index 14bbb189b6..94a6e4daa1 100644 --- a/src/qml/jsruntime/qv4jsonobject.cpp +++ b/src/qml/jsruntime/qv4jsonobject.cpp @@ -591,8 +591,7 @@ bool JsonParser::parseString(QString *string) return false; } if (QChar::requiresSurrogates(ch)) { - *string += QChar(QChar::highSurrogate(ch)); - *string += QChar(QChar::lowSurrogate(ch)); + *string += QChar(QChar::highSurrogate(ch)) + QChar(QChar::lowSurrogate(ch)); } else { *string += QChar(ch); } @@ -645,36 +644,39 @@ struct Stringify static QString quote(const QString &str) { - QString product = QStringLiteral("\""); - for (int i = 0; i < str.length(); ++i) { + QString product; + const int length = str.length(); + product.reserve(length + 2); + product += QLatin1Char('"'); + for (int i = 0; i < length; ++i) { QChar c = str.at(i); switch (c.unicode()) { case '"': - product += QStringLiteral("\\\""); + product += QLatin1String("\\\""); break; case '\\': - product += QStringLiteral("\\\\"); + product += QLatin1String("\\\\"); break; case '\b': - product += QStringLiteral("\\b"); + product += QLatin1String("\\b"); break; case '\f': - product += QStringLiteral("\\f"); + product += QLatin1String("\\f"); break; case '\n': - product += QStringLiteral("\\n"); + product += QLatin1String("\\n"); break; case '\r': - product += QStringLiteral("\\r"); + product += QLatin1String("\\r"); break; case '\t': - product += QStringLiteral("\\t"); + product += QLatin1String("\\t"); break; default: if (c.unicode() <= 0x1f) { - product += QStringLiteral("\\u00"); - product += c.unicode() > 0xf ? QLatin1Char('1') : QLatin1Char('0'); - product += QLatin1Char("0123456789abcdef"[c.unicode() & 0xf]); + product += QLatin1String("\\u00"); + product += (c.unicode() > 0xf ? QLatin1Char('1') : QLatin1Char('0')) + + QLatin1Char("0123456789abcdef"[c.unicode() & 0xf]); } else { product += c; } @@ -687,57 +689,57 @@ static QString quote(const QString &str) QString Stringify::Str(const QString &key, const Value &v) { Scope scope(v4); + scope.result = v; - ScopedValue value(scope, v); - ScopedObject o(scope, value); + ScopedObject o(scope, scope.result); if (o) { ScopedString s(scope, v4->newString(QStringLiteral("toJSON"))); ScopedFunctionObject toJSON(scope, o->get(s)); if (!!toJSON) { ScopedCallData callData(scope, 1); - callData->thisObject = value; + callData->thisObject = scope.result; callData->args[0] = v4->newString(key); - value = toJSON->call(callData); + toJSON->call(scope, callData); } } if (replacerFunction) { ScopedObject holder(scope, v4->newObject()); - holder->put(scope.engine, QString(), value); + holder->put(scope.engine, QString(), scope.result); ScopedCallData callData(scope, 2); callData->args[0] = v4->newString(key); - callData->args[1] = value; + callData->args[1] = scope.result; callData->thisObject = holder; - value = replacerFunction->call(callData); + replacerFunction->call(scope, callData); } - o = value->asReturnedValue(); + o = scope.result.asReturnedValue(); if (o) { if (NumberObject *n = o->as<NumberObject>()) - value = Encode(n->value()); + scope.result = Encode(n->value()); else if (StringObject *so = o->as<StringObject>()) - value = so->d()->string; + scope.result = so->d()->string; else if (BooleanObject *b = o->as<BooleanObject>()) - value = Encode(b->value()); + scope.result = Encode(b->value()); } - if (value->isNull()) + if (scope.result.isNull()) return QStringLiteral("null"); - if (value->isBoolean()) - return value->booleanValue() ? QStringLiteral("true") : QStringLiteral("false"); - if (value->isString()) - return quote(value->stringValue()->toQString()); - - if (value->isNumber()) { - double d = value->toNumber(); - return std::isfinite(d) ? value->toQString() : QStringLiteral("null"); + if (scope.result.isBoolean()) + return scope.result.booleanValue() ? QStringLiteral("true") : QStringLiteral("false"); + if (scope.result.isString()) + return quote(scope.result.stringValue()->toQString()); + + if (scope.result.isNumber()) { + double d = scope.result.toNumber(); + return std::isfinite(d) ? scope.result.toQString() : QStringLiteral("null"); } - if (const QV4::VariantObject *v = value->as<QV4::VariantObject>()) { - return v->d()->data.toString(); + if (const QV4::VariantObject *v = scope.result.as<QV4::VariantObject>()) { + return v->d()->data().toString(); } - o = value->asReturnedValue(); + o = scope.result.asReturnedValue(); if (o) { if (!o->as<FunctionObject>()) { if (o->as<ArrayObject>()) { @@ -812,10 +814,10 @@ QString Stringify::JO(Object *o) if (partial.isEmpty()) { result = QStringLiteral("{}"); } else if (gap.isEmpty()) { - result = QStringLiteral("{") + partial.join(QLatin1Char(',')) + QLatin1Char('}'); + result = QLatin1Char('{') + partial.join(QLatin1Char(',')) + QLatin1Char('}'); } else { - QString separator = QStringLiteral(",\n") + indent; - result = QStringLiteral("{\n") + indent + partial.join(separator) + QLatin1Char('\n') + QString separator = QLatin1String(",\n") + indent; + result = QLatin1String("{\n") + indent + partial.join(separator) + QLatin1Char('\n') + stepback + QLatin1Char('}'); } @@ -858,10 +860,10 @@ QString Stringify::JA(ArrayObject *a) if (partial.isEmpty()) { result = QStringLiteral("[]"); } else if (gap.isEmpty()) { - result = QStringLiteral("[") + partial.join(QLatin1Char(',')) + QStringLiteral("]"); + result = QLatin1Char('[') + partial.join(QLatin1Char(',')) + QLatin1Char(']'); } else { - QString separator = QStringLiteral(",\n") + indent; - result = QStringLiteral("[\n") + indent + partial.join(separator) + QStringLiteral("\n") + stepback + QStringLiteral("]"); + QString separator = QLatin1String(",\n") + indent; + result = QLatin1String("[\n") + indent + partial.join(separator) + QLatin1Char('\n') + stepback + QLatin1Char(']'); } indent = stepback; @@ -870,8 +872,9 @@ QString Stringify::JA(ArrayObject *a) } -Heap::JsonObject::JsonObject() +void Heap::JsonObject::init() { + Object::init(); Scope scope(internalClass->engine); ScopedObject o(scope, this); diff --git a/src/qml/jsruntime/qv4jsonobject_p.h b/src/qml/jsruntime/qv4jsonobject_p.h index c3a3b191c0..43248a214d 100644 --- a/src/qml/jsruntime/qv4jsonobject_p.h +++ b/src/qml/jsruntime/qv4jsonobject_p.h @@ -64,7 +64,7 @@ namespace QV4 { namespace Heap { struct JsonObject : Object { - JsonObject(); + void init(); }; } diff --git a/src/qml/jsruntime/qv4lookup.cpp b/src/qml/jsruntime/qv4lookup.cpp index 46e47307ef..84755a6402 100644 --- a/src/qml/jsruntime/qv4lookup.cpp +++ b/src/qml/jsruntime/qv4lookup.cpp @@ -306,7 +306,7 @@ ReturnedValue Lookup::getterTwoClasses(Lookup *l, ExecutionEngine *engine, const ReturnedValue v = o->getLookup(l); Lookup l2 = *l; - if (l2.getter == Lookup::getter0 || l2.getter == Lookup::getter1) { + if (l->index != UINT_MAX && (l2.getter == Lookup::getter0 || l2.getter == Lookup::getter1)) { // if we have a getter0, make sure it comes first if (l2.getter == Lookup::getter0) qSwap(l1, l2); @@ -451,7 +451,8 @@ ReturnedValue Lookup::getterAccessor0(Lookup *l, ExecutionEngine *engine, const ScopedCallData callData(scope, 0); callData->thisObject = object; - return getter->call(callData); + getter->call(scope, callData); + return scope.result.asReturnedValue(); } } l->getter = getterFallback; @@ -473,7 +474,8 @@ ReturnedValue Lookup::getterAccessor1(Lookup *l, ExecutionEngine *engine, const ScopedCallData callData(scope, 0); callData->thisObject = object; - return getter->call(callData); + getter->call(scope, callData); + return scope.result.asReturnedValue(); } } l->getter = getterFallback; @@ -498,7 +500,8 @@ ReturnedValue Lookup::getterAccessor2(Lookup *l, ExecutionEngine *engine, const ScopedCallData callData(scope, 0); callData->thisObject = object; - return getter->call(callData); + getter->call(scope, callData); + return scope.result.asReturnedValue(); } } } @@ -542,7 +545,8 @@ ReturnedValue Lookup::primitiveGetterAccessor0(Lookup *l, ExecutionEngine *engin ScopedCallData callData(scope, 0); callData->thisObject = object; - return getter->call(callData); + getter->call(scope, callData); + return scope.result.asReturnedValue(); } } l->getter = getterGeneric; @@ -562,7 +566,8 @@ ReturnedValue Lookup::primitiveGetterAccessor1(Lookup *l, ExecutionEngine *engin ScopedCallData callData(scope, 0); callData->thisObject = object; - return getter->call(callData); + getter->call(scope, callData); + return scope.result.asReturnedValue(); } } l->getter = getterGeneric; @@ -665,7 +670,8 @@ ReturnedValue Lookup::globalGetterAccessor0(Lookup *l, ExecutionEngine *engine) ScopedCallData callData(scope, 0); callData->thisObject = Primitive::undefinedValue(); - return getter->call(callData); + getter->call(scope, callData); + return scope.result.asReturnedValue(); } l->globalGetter = globalGetterGeneric; return globalGetterGeneric(l, engine); @@ -683,7 +689,8 @@ ReturnedValue Lookup::globalGetterAccessor1(Lookup *l, ExecutionEngine *engine) ScopedCallData callData(scope, 0); callData->thisObject = Primitive::undefinedValue(); - return getter->call(callData); + getter->call(scope, callData); + return scope.result.asReturnedValue(); } l->globalGetter = globalGetterGeneric; return globalGetterGeneric(l, engine); @@ -704,7 +711,8 @@ ReturnedValue Lookup::globalGetterAccessor2(Lookup *l, ExecutionEngine *engine) ScopedCallData callData(scope, 0); callData->thisObject = Primitive::undefinedValue(); - return getter->call(callData); + getter->call(scope, callData); + return scope.result.asReturnedValue(); } } } diff --git a/src/qml/jsruntime/qv4managed_p.h b/src/qml/jsruntime/qv4managed_p.h index d73b990470..1fff5a45da 100644 --- a/src/qml/jsruntime/qv4managed_p.h +++ b/src/qml/jsruntime/qv4managed_p.h @@ -74,7 +74,7 @@ inline void qYouForgotTheQ_MANAGED_Macro(T1, T2) {} #define V4_MANAGED_SIZE_TEST #endif -#define V4_NEEDS_DESTROY static void destroy(QV4::Heap::Base *b) { static_cast<Data *>(b)->~Data(); } +#define V4_NEEDS_DESTROY static void destroy(QV4::Heap::Base *b) { static_cast<Data *>(b)->destroy(); } #define V4_MANAGED_ITSELF(DataClass, superClass) \ @@ -85,7 +85,13 @@ inline void qYouForgotTheQ_MANAGED_Macro(T1, T2) {} static const QV4::VTable static_vtbl; \ static inline const QV4::VTable *staticVTable() { return &static_vtbl; } \ V4_MANAGED_SIZE_TEST \ - QV4::Heap::DataClass *d() const { return static_cast<QV4::Heap::DataClass *>(m()); } + QV4::Heap::DataClass *d_unchecked() const { return static_cast<QV4::Heap::DataClass *>(m()); } \ + QV4::Heap::DataClass *d() const { \ + QV4::Heap::DataClass *dptr = d_unchecked(); \ + dptr->_checkIsInitialized(); \ + return dptr; \ + } \ + V4_ASSERT_IS_TRIVIAL(QV4::Heap::DataClass) #define V4_MANAGED(DataClass, superClass) \ private: \ diff --git a/src/qml/jsruntime/qv4mathobject.cpp b/src/qml/jsruntime/qv4mathobject.cpp index cb17583b98..e03b2762cc 100644 --- a/src/qml/jsruntime/qv4mathobject.cpp +++ b/src/qml/jsruntime/qv4mathobject.cpp @@ -51,8 +51,9 @@ using namespace QV4; DEFINE_OBJECT_VTABLE(MathObject); -Heap::MathObject::MathObject() +void Heap::MathObject::init() { + Object::init(); Scope scope(internalClass->engine); ScopedObject m(scope, this); @@ -80,6 +81,7 @@ Heap::MathObject::MathObject() m->defineDefaultProperty(QStringLiteral("pow"), QV4::MathObject::method_pow, 2); m->defineDefaultProperty(QStringLiteral("random"), QV4::MathObject::method_random, 0); m->defineDefaultProperty(QStringLiteral("round"), QV4::MathObject::method_round, 1); + m->defineDefaultProperty(QStringLiteral("sign"), QV4::MathObject::method_sign, 1); m->defineDefaultProperty(QStringLiteral("sin"), QV4::MathObject::method_sin, 1); m->defineDefaultProperty(QStringLiteral("sqrt"), QV4::MathObject::method_sqrt, 1); m->defineDefaultProperty(QStringLiteral("tan"), QV4::MathObject::method_tan, 1); @@ -292,6 +294,19 @@ ReturnedValue MathObject::method_round(CallContext *context) return Encode(v); } +ReturnedValue MathObject::method_sign(CallContext *context) +{ + double v = context->argc() ? context->args()[0].toNumber() : qt_qnan(); + + if (std::isnan(v)) + return Encode(qt_qnan()); + + if (qIsNull(v)) + return v; + + return Encode(std::signbit(v) ? -1 : 1); +} + ReturnedValue MathObject::method_sin(CallContext *context) { double v = context->argc() ? context->args()[0].toNumber() : qt_qnan(); diff --git a/src/qml/jsruntime/qv4mathobject_p.h b/src/qml/jsruntime/qv4mathobject_p.h index 2b842a312f..f6b1a4395f 100644 --- a/src/qml/jsruntime/qv4mathobject_p.h +++ b/src/qml/jsruntime/qv4mathobject_p.h @@ -59,7 +59,7 @@ namespace QV4 { namespace Heap { struct MathObject : Object { - MathObject(); + void init(); }; } @@ -84,6 +84,7 @@ struct MathObject: Object static ReturnedValue method_pow(CallContext *context); static ReturnedValue method_random(CallContext *context); static ReturnedValue method_round(CallContext *context); + static ReturnedValue method_sign(CallContext *context); static ReturnedValue method_sin(CallContext *context); static ReturnedValue method_sqrt(CallContext *context); static ReturnedValue method_tan(CallContext *context); diff --git a/src/qml/jsruntime/qv4memberdata.cpp b/src/qml/jsruntime/qv4memberdata.cpp index 62e4f0a14d..5646a44891 100644 --- a/src/qml/jsruntime/qv4memberdata.cpp +++ b/src/qml/jsruntime/qv4memberdata.cpp @@ -58,9 +58,9 @@ static Heap::MemberData *reallocateHelper(ExecutionEngine *e, Heap::MemberData * Scope scope(e); Scoped<MemberData> newMemberData(scope, e->memoryManager->allocManaged<MemberData>(alloc)); if (old) - memcpy(newMemberData->d(), old, sizeof(Heap::MemberData) + old->size * sizeof(Value)); + memcpy(newMemberData->d_unchecked(), old, sizeof(Heap::MemberData) + old->size * sizeof(Value)); else - new (newMemberData->d()) Heap::MemberData; + newMemberData->d_unchecked()->init(); newMemberData->d()->size = n; return newMemberData->d(); } diff --git a/src/qml/jsruntime/qv4memberdata_p.h b/src/qml/jsruntime/qv4memberdata_p.h index 2742e0b212..969eee3619 100644 --- a/src/qml/jsruntime/qv4memberdata_p.h +++ b/src/qml/jsruntime/qv4memberdata_p.h @@ -66,6 +66,7 @@ struct MemberData : Base { }; Value data[1]; }; +V4_ASSERT_IS_TRIVIAL(MemberData) } diff --git a/src/qml/jsruntime/qv4numberobject.cpp b/src/qml/jsruntime/qv4numberobject.cpp index 0e653c18cb..1733df34ae 100644 --- a/src/qml/jsruntime/qv4numberobject.cpp +++ b/src/qml/jsruntime/qv4numberobject.cpp @@ -45,6 +45,7 @@ #include <QtCore/qmath.h> #include <QtCore/QDebug> #include <cassert> +#include <limits> using namespace QV4; @@ -70,22 +71,21 @@ const NumberLocale *NumberLocale::instance() return numberLocaleHolder(); } -Heap::NumberCtor::NumberCtor(QV4::ExecutionContext *scope) - : Heap::FunctionObject(scope, QStringLiteral("Number")) +void Heap::NumberCtor::init(QV4::ExecutionContext *scope) { + Heap::FunctionObject::init(scope, QStringLiteral("Number")); } -ReturnedValue NumberCtor::construct(const Managed *m, CallData *callData) +void NumberCtor::construct(const Managed *, Scope &scope, CallData *callData) { - Scope scope(m->cast<NumberCtor>()->engine()); double dbl = callData->argc ? callData->args[0].toNumber() : 0.; - return Encode(scope.engine->newNumberObject(dbl)); + scope.result = Encode(scope.engine->newNumberObject(dbl)); } -ReturnedValue NumberCtor::call(const Managed *, CallData *callData) +void NumberCtor::call(const Managed *, Scope &scope, CallData *callData) { double dbl = callData->argc ? callData->args[0].toNumber() : 0.; - return Encode(dbl); + scope.result = Encode(dbl); } void NumberPrototype::init(ExecutionEngine *engine, Object *ctor) @@ -99,12 +99,16 @@ void NumberPrototype::init(ExecutionEngine *engine, Object *ctor) ctor->defineReadonlyProperty(QStringLiteral("NEGATIVE_INFINITY"), Primitive::fromDouble(-qInf())); ctor->defineReadonlyProperty(QStringLiteral("POSITIVE_INFINITY"), Primitive::fromDouble(qInf())); ctor->defineReadonlyProperty(QStringLiteral("MAX_VALUE"), Primitive::fromDouble(1.7976931348623158e+308)); + ctor->defineReadonlyProperty(QStringLiteral("EPSILON"), Primitive::fromDouble(std::numeric_limits<double>::epsilon())); QT_WARNING_PUSH QT_WARNING_DISABLE_INTEL(239) ctor->defineReadonlyProperty(QStringLiteral("MIN_VALUE"), Primitive::fromDouble(5e-324)); QT_WARNING_POP + ctor->defineDefaultProperty(QStringLiteral("isFinite"), method_isFinite, 1); + ctor->defineDefaultProperty(QStringLiteral("isNaN"), method_isNaN, 1); + defineDefaultProperty(QStringLiteral("constructor"), (o = ctor)); defineDefaultProperty(engine->id_toString(), method_toString); defineDefaultProperty(QStringLiteral("toLocaleString"), method_toLocaleString); @@ -134,6 +138,24 @@ inline double thisNumber(ExecutionContext *ctx) return n->value(); } +ReturnedValue NumberPrototype::method_isFinite(CallContext *ctx) +{ + if (!ctx->argc()) + return Encode(false); + + double v = ctx->args()[0].toNumber(); + return Encode(!std::isnan(v) && !qt_is_inf(v)); +} + +ReturnedValue NumberPrototype::method_isNaN(CallContext *ctx) +{ + if (!ctx->argc()) + return Encode(false); + + double v = ctx->args()[0].toNumber(); + return Encode(std::isnan(v)); +} + ReturnedValue NumberPrototype::method_toString(CallContext *ctx) { Scope scope(ctx); diff --git a/src/qml/jsruntime/qv4numberobject_p.h b/src/qml/jsruntime/qv4numberobject_p.h index ca6f686304..6022b3a029 100644 --- a/src/qml/jsruntime/qv4numberobject_p.h +++ b/src/qml/jsruntime/qv4numberobject_p.h @@ -61,7 +61,7 @@ namespace QV4 { namespace Heap { struct NumberCtor : FunctionObject { - NumberCtor(QV4::ExecutionContext *scope); + void init(QV4::ExecutionContext *scope); }; } @@ -79,14 +79,16 @@ struct NumberCtor: FunctionObject { V4_OBJECT2(NumberCtor, FunctionObject) - static ReturnedValue construct(const Managed *that, CallData *callData); - static ReturnedValue call(const Managed *, CallData *callData); + static void construct(const Managed *that, Scope &scope, CallData *callData); + static void call(const Managed *, Scope &scope, CallData *callData); }; struct NumberPrototype: NumberObject { void init(ExecutionEngine *engine, Object *ctor); + static ReturnedValue method_isFinite(CallContext *ctx); + static ReturnedValue method_isNaN(CallContext *ctx); static ReturnedValue method_toString(CallContext *ctx); static ReturnedValue method_toLocaleString(CallContext *ctx); static ReturnedValue method_valueOf(CallContext *ctx); diff --git a/src/qml/jsruntime/qv4object.cpp b/src/qml/jsruntime/qv4object.cpp index 0727f35edd..00e6d230da 100644 --- a/src/qml/jsruntime/qv4object.cpp +++ b/src/qml/jsruntime/qv4object.cpp @@ -109,7 +109,8 @@ ReturnedValue Object::getValue(const Value &thisObject, const Value &v, Property Scope scope(f->engine()); ScopedCallData callData(scope); callData->thisObject = thisObject; - return f->call(callData); + f->call(scope, callData); + return scope.result.asReturnedValue(); } void Object::putValue(uint memberIndex, const Value &value) @@ -128,7 +129,7 @@ void Object::putValue(uint memberIndex, const Value &value) ScopedCallData callData(scope, 1); callData->args[0] = value; callData->thisObject = this; - setter->call(callData); + setter->call(scope, callData); return; } goto reject; @@ -389,14 +390,14 @@ bool Object::hasOwnProperty(uint index) const return false; } -ReturnedValue Object::construct(const Managed *m, CallData *) +void Object::construct(const Managed *m, Scope &scope, CallData *) { - return static_cast<const Object *>(m)->engine()->throwTypeError(); + scope.result = static_cast<const Object *>(m)->engine()->throwTypeError(); } -ReturnedValue Object::call(const Managed *m, CallData *) +void Object::call(const Managed *m, Scope &scope, CallData *) { - return static_cast<const Object *>(m)->engine()->throwTypeError(); + scope.result = static_cast<const Object *>(m)->engine()->throwTypeError(); } ReturnedValue Object::get(const Managed *m, String *name, bool *hasProperty) @@ -744,7 +745,7 @@ void Object::internalPut(String *name, const Value &value) ScopedCallData callData(scope, 1); callData->args[0] = value; callData->thisObject = this; - setter->call(callData); + setter->call(scope, callData); return; } @@ -753,9 +754,8 @@ void Object::internalPut(String *name, const Value &value) reject: if (engine()->current->strictMode) { - QString message = QStringLiteral("Cannot assign to read-only property \""); - message += name->toQString(); - message += QLatin1Char('\"'); + QString message = QLatin1String("Cannot assign to read-only property \"") + + name->toQString() + QLatin1Char('\"'); engine()->throwTypeError(message); } } @@ -815,7 +815,7 @@ void Object::internalPutIndexed(uint index, const Value &value) ScopedCallData callData(scope, 1); callData->args[0] = value; callData->thisObject = this; - setter->call(callData); + setter->call(scope, callData); return; } @@ -1160,10 +1160,10 @@ void Object::initSparseArray() DEFINE_OBJECT_VTABLE(ArrayObject); -Heap::ArrayObject::ArrayObject(const QStringList &list) - : Heap::Object() +void Heap::ArrayObject::init(const QStringList &list) { - init(); + Object::init(); + commonInit(); Scope scope(internalClass->engine); ScopedObject a(scope, this); diff --git a/src/qml/jsruntime/qv4object_p.h b/src/qml/jsruntime/qv4object_p.h index 82f75a49c6..d5195adaf0 100644 --- a/src/qml/jsruntime/qv4object_p.h +++ b/src/qml/jsruntime/qv4object_p.h @@ -56,6 +56,7 @@ #include "qv4engine_p.h" #include "qv4scopedvalue_p.h" #include "qv4value_p.h" +#include "qv4internalclass_p.h" #include <QtCore/qtypetraits.h> @@ -67,7 +68,8 @@ namespace QV4 { namespace Heap { struct Object : Base { - inline Object() {} + void init() { Base::init(); } + void destroy() { Base::destroy(); } const Value *propertyData(uint index) const { if (index < inlineMemberSize) return reinterpret_cast<const Value *>(this) + inlineMemberOffset + index; return memberData->data + index - inlineMemberSize; } Value *propertyData(uint index) { if (index < inlineMemberSize) return reinterpret_cast<Value *>(this) + inlineMemberOffset + index; return memberData->data + index - inlineMemberSize; } @@ -89,7 +91,13 @@ struct Object : Base { static const QV4::ObjectVTable static_vtbl; \ static inline const QV4::VTable *staticVTable() { return &static_vtbl.vTable; } \ V4_MANAGED_SIZE_TEST \ - Data *d() const { return static_cast<Data *>(m()); } + Data *d_unchecked() const { return static_cast<Data *>(m()); } \ + Data *d() const { \ + Data *dptr = d_unchecked(); \ + dptr->_checkIsInitialized(); \ + return dptr; \ + } \ + V4_ASSERT_IS_TRIVIAL(Data); #define V4_OBJECT2(DataClass, superClass) \ private: \ @@ -102,7 +110,13 @@ struct Object : Base { static const QV4::ObjectVTable static_vtbl; \ static inline const QV4::VTable *staticVTable() { return &static_vtbl.vTable; } \ V4_MANAGED_SIZE_TEST \ - QV4::Heap::DataClass *d() const { return static_cast<QV4::Heap::DataClass *>(m()); } + QV4::Heap::DataClass *d_unchecked() const { return static_cast<QV4::Heap::DataClass *>(m()); } \ + QV4::Heap::DataClass *d() const { \ + QV4::Heap::DataClass *dptr = d_unchecked(); \ + dptr->_checkIsInitialized(); \ + return dptr; \ + } \ + V4_ASSERT_IS_TRIVIAL(QV4::Heap::DataClass); #define V4_INTERNALCLASS(c) \ static QV4::InternalClass *defaultInternalClass(QV4::ExecutionEngine *e) \ @@ -114,8 +128,8 @@ struct Object : Base { struct ObjectVTable { VTable vTable; - ReturnedValue (*call)(const Managed *, CallData *data); - ReturnedValue (*construct)(const Managed *, CallData *data); + void (*call)(const Managed *, Scope &scope, CallData *data); + void (*construct)(const Managed *, Scope &scope, CallData *data); ReturnedValue (*get)(const Managed *, String *name, bool *hasProperty); ReturnedValue (*getIndexed)(const Managed *, uint index, bool *hasProperty); void (*put)(Managed *, String *name, const Value &value); @@ -334,14 +348,14 @@ public: { vtable()->advanceIterator(this, it, name, index, p, attributes); } uint getLength() const { return vtable()->getLength(this); } - inline ReturnedValue construct(CallData *d) const - { return vtable()->construct(this, d); } - inline ReturnedValue call(CallData *d) const - { return vtable()->call(this, d); } + inline void construct(Scope &scope, CallData *d) const + { return vtable()->construct(this, scope, d); } + inline void call(Scope &scope, CallData *d) const + { vtable()->call(this, scope, d); } protected: static void markObjects(Heap::Base *that, ExecutionEngine *e); - static ReturnedValue construct(const Managed *m, CallData *); - static ReturnedValue call(const Managed *m, CallData *); + static void construct(const Managed *m, Scope &scope, CallData *); + static void call(const Managed *m, Scope &scope, CallData *); static ReturnedValue get(const Managed *m, String *name, bool *hasProperty); static ReturnedValue getIndexed(const Managed *m, uint index, bool *hasProperty); static void put(Managed *m, String *name, const Value &value); @@ -372,18 +386,22 @@ private: namespace Heap { struct BooleanObject : Object { - BooleanObject() {} - BooleanObject(bool b) - : b(b) - {} + void init() { Object::init(); } + void init(bool b) { + Object::init(); + this->b = b; + } + bool b; }; struct NumberObject : Object { - NumberObject() {} - NumberObject(double val) - : value(val) - {} + void init() { Object::init(); } + void init(double val) { + Object::init(); + value = val; + } + double value; }; @@ -392,10 +410,15 @@ struct ArrayObject : Object { LengthPropertyIndex = 0 }; - ArrayObject() - { init(); } - ArrayObject(const QStringList &list); - void init() + void init() { + Object::init(); + commonInit(); + } + + void init(const QStringList &list); + +private: + void commonInit() { *propertyData(LengthPropertyIndex) = Primitive::fromInt32(0); } }; diff --git a/src/qml/jsruntime/qv4objectiterator.cpp b/src/qml/jsruntime/qv4objectiterator.cpp index 4354e09248..7943a13ac0 100644 --- a/src/qml/jsruntime/qv4objectiterator.cpp +++ b/src/qml/jsruntime/qv4objectiterator.cpp @@ -45,30 +45,6 @@ using namespace QV4; -ObjectIterator::ObjectIterator(ExecutionEngine *e, Value *scratch1, Value *scratch2, Object *o, uint flags) - : engine(e) - , object(scratch1) - , current(scratch2) - , arrayNode(0) - , arrayIndex(0) - , memberIndex(0) - , flags(flags) -{ - init(o); -} - -ObjectIterator::ObjectIterator(Scope &scope, const Object *o, uint flags) - : engine(scope.engine) - , object(scope.alloc(1)) - , current(scope.alloc(1)) - , arrayNode(0) - , arrayIndex(0) - , memberIndex(0) - , flags(flags) -{ - init(o); -} - void ObjectIterator::init(const Object *o) { object->setM(o ? o->m() : 0); diff --git a/src/qml/jsruntime/qv4objectiterator_p.h b/src/qml/jsruntime/qv4objectiterator_p.h index 6bef703a4d..98e94a95ea 100644 --- a/src/qml/jsruntime/qv4objectiterator_p.h +++ b/src/qml/jsruntime/qv4objectiterator_p.h @@ -57,7 +57,7 @@ QT_BEGIN_NAMESPACE namespace QV4 { -struct Q_QML_EXPORT ObjectIterator +struct Q_QML_EXPORT ObjectIteratorData { enum Flags { NoFlags = 0, @@ -72,21 +72,52 @@ struct Q_QML_EXPORT ObjectIterator uint arrayIndex; uint memberIndex; uint flags; +}; +V4_ASSERT_IS_TRIVIAL(ObjectIteratorData) + +struct Q_QML_EXPORT ObjectIterator: ObjectIteratorData +{ + ObjectIterator(ExecutionEngine *e, Value *scratch1, Value *scratch2, Object *o, uint flags) + { + engine = e; + object = scratch1; + current = scratch2; + arrayNode = nullptr; + arrayIndex = 0; + memberIndex = 0; + this->flags = flags; + init(o); + } + + ObjectIterator(Scope &scope, const Object *o, uint flags) + { + engine = scope.engine; + object = scope.alloc(1); + current = scope.alloc(1); + arrayNode = nullptr; + arrayIndex = 0; + memberIndex = 0; + this->flags = flags; + init(o); + } - ObjectIterator(ExecutionEngine *e, Value *scratch1, Value *scratch2, Object *o, uint flags); - ObjectIterator(Scope &scope, const Object *o, uint flags); - void init(const Object *o); void next(Value *name, uint *index, Property *pd, PropertyAttributes *attributes = 0); ReturnedValue nextPropertyName(Value *value); ReturnedValue nextPropertyNameAsString(Value *value); ReturnedValue nextPropertyNameAsString(); + +private: + void init(const Object *o); }; namespace Heap { struct ForEachIteratorObject : Object { - ForEachIteratorObject(QV4::Object *o); - ObjectIterator it; + void init(QV4::Object *o); + ObjectIterator &it() { return *reinterpret_cast<ObjectIterator*>(&itData); } Value workArea[2]; + +private: + ObjectIteratorData itData; }; } @@ -95,16 +126,18 @@ struct ForEachIteratorObject: Object { V4_OBJECT2(ForEachIteratorObject, Object) Q_MANAGED_TYPE(ForeachIteratorObject) - ReturnedValue nextPropertyName() { return d()->it.nextPropertyNameAsString(); } + ReturnedValue nextPropertyName() { return d()->it().nextPropertyNameAsString(); } protected: static void markObjects(Heap::Base *that, ExecutionEngine *e); }; inline -Heap::ForEachIteratorObject::ForEachIteratorObject(QV4::Object *o) - : it(internalClass->engine, workArea, workArea + 1, o, ObjectIterator::EnumerableOnly|ObjectIterator::WithProtoChain) +void Heap::ForEachIteratorObject::init(QV4::Object *o) { + Object::init(); + it() = ObjectIterator(internalClass->engine, workArea, workArea + 1, o, + ObjectIterator::EnumerableOnly | ObjectIterator::WithProtoChain); } diff --git a/src/qml/jsruntime/qv4objectproto.cpp b/src/qml/jsruntime/qv4objectproto.cpp index 015294e48a..6020c48250 100644 --- a/src/qml/jsruntime/qv4objectproto.cpp +++ b/src/qml/jsruntime/qv4objectproto.cpp @@ -54,33 +54,35 @@ using namespace QV4; DEFINE_OBJECT_VTABLE(ObjectCtor); -Heap::ObjectCtor::ObjectCtor(QV4::ExecutionContext *scope) - : Heap::FunctionObject(scope, QStringLiteral("Object")) +void Heap::ObjectCtor::init(QV4::ExecutionContext *scope) { + Heap::FunctionObject::init(scope, QStringLiteral("Object")); } -ReturnedValue ObjectCtor::construct(const Managed *that, CallData *callData) +void ObjectCtor::construct(const Managed *that, Scope &scope, CallData *callData) { const ObjectCtor *ctor = static_cast<const ObjectCtor *>(that); ExecutionEngine *v4 = ctor->engine(); - Scope scope(v4); if (!callData->argc || callData->args[0].isUndefined() || callData->args[0].isNull()) { ScopedObject obj(scope, v4->newObject()); ScopedObject proto(scope, ctor->get(v4->id_prototype())); if (!!proto) obj->setPrototype(proto); - return obj.asReturnedValue(); + scope.result = obj.asReturnedValue(); + } else { + scope.result = RuntimeHelpers::toObject(scope.engine, callData->args[0]); } - return RuntimeHelpers::toObject(scope.engine, callData->args[0]); } -ReturnedValue ObjectCtor::call(const Managed *m, CallData *callData) +void ObjectCtor::call(const Managed *m, Scope &scope, CallData *callData) { const ObjectCtor *ctor = static_cast<const ObjectCtor *>(m); ExecutionEngine *v4 = ctor->engine(); - if (!callData->argc || callData->args[0].isUndefined() || callData->args[0].isNull()) - return v4->newObject()->asReturnedValue(); - return RuntimeHelpers::toObject(v4, callData->args[0]); + if (!callData->argc || callData->args[0].isUndefined() || callData->args[0].isNull()) { + scope.result = v4->newObject()->asReturnedValue(); + } else { + scope.result = RuntimeHelpers::toObject(v4, callData->args[0]); + } } void ObjectPrototype::init(ExecutionEngine *v4, Object *ctor) @@ -413,7 +415,8 @@ ReturnedValue ObjectPrototype::method_toLocaleString(CallContext *ctx) return ctx->engine()->throwTypeError(); ScopedCallData callData(scope); callData->thisObject = o; - return f->call(callData); + f->call(scope, callData); + return scope.result.asReturnedValue(); } ReturnedValue ObjectPrototype::method_valueOf(CallContext *ctx) diff --git a/src/qml/jsruntime/qv4objectproto_p.h b/src/qml/jsruntime/qv4objectproto_p.h index ac1964103e..e3d85782d5 100644 --- a/src/qml/jsruntime/qv4objectproto_p.h +++ b/src/qml/jsruntime/qv4objectproto_p.h @@ -61,7 +61,7 @@ namespace QV4 { namespace Heap { struct ObjectCtor : FunctionObject { - ObjectCtor(QV4::ExecutionContext *scope); + void init(QV4::ExecutionContext *scope); }; } @@ -70,8 +70,8 @@ struct ObjectCtor: FunctionObject { V4_OBJECT2(ObjectCtor, FunctionObject) - static ReturnedValue construct(const Managed *that, CallData *callData); - static ReturnedValue call(const Managed *that, CallData *callData); + static void construct(const Managed *that, Scope &scope, CallData *callData); + static void call(const Managed *that, Scope &scope, CallData *callData); }; struct ObjectPrototype: Object diff --git a/src/qml/jsruntime/qv4persistent.cpp b/src/qml/jsruntime/qv4persistent.cpp index a892194df3..987c322e47 100644 --- a/src/qml/jsruntime/qv4persistent.cpp +++ b/src/qml/jsruntime/qv4persistent.cpp @@ -358,14 +358,14 @@ WeakValue::WeakValue(const WeakValue &other) : val(0) { if (other.val) { - val = other.engine()->memoryManager->m_weakValues->allocate(); + allocVal(other.engine()); *val = *other.val; } } WeakValue::WeakValue(ExecutionEngine *engine, const Value &value) { - val = engine->memoryManager->m_weakValues->allocate(); + allocVal(engine); *val = value; } @@ -374,7 +374,7 @@ WeakValue &WeakValue::operator=(const WeakValue &other) if (!val) { if (!other.val) return *this; - val = other.engine()->memoryManager->m_weakValues->allocate(); + allocVal(other.engine()); } Q_ASSERT(engine() == other.engine()); @@ -388,25 +388,9 @@ WeakValue::~WeakValue() free(); } -void WeakValue::set(ExecutionEngine *engine, const Value &value) -{ - if (!val) - val = engine->memoryManager->m_weakValues->allocate(); - *val = value; -} - -void WeakValue::set(ExecutionEngine *engine, ReturnedValue value) -{ - if (!val) - val = engine->memoryManager->m_weakValues->allocate(); - *val = value; -} - -void WeakValue::set(ExecutionEngine *engine, Heap::Base *obj) +void WeakValue::allocVal(ExecutionEngine *engine) { - if (!val) - val = engine->memoryManager->m_weakValues->allocate(); - *val = obj; + val = engine->memoryManager->m_weakValues->allocate(); } void WeakValue::markOnce(ExecutionEngine *e) diff --git a/src/qml/jsruntime/qv4persistent_p.h b/src/qml/jsruntime/qv4persistent_p.h index 5b1926468a..c1cd1f34df 100644 --- a/src/qml/jsruntime/qv4persistent_p.h +++ b/src/qml/jsruntime/qv4persistent_p.h @@ -118,7 +118,7 @@ public: Managed *asManaged() const { if (!val) return 0; - return val->as<Managed>(); + return val->managed(); } template<typename T> T *as() const { @@ -154,9 +154,26 @@ public: WeakValue &operator=(const WeakValue &other); ~WeakValue(); - void set(ExecutionEngine *engine, const Value &value); - void set(ExecutionEngine *engine, ReturnedValue value); - void set(ExecutionEngine *engine, Heap::Base *obj); + void set(ExecutionEngine *engine, const Value &value) + { + if (!val) + allocVal(engine); + *val = value; + } + + void set(ExecutionEngine *engine, ReturnedValue value) + { + if (!val) + allocVal(engine); + *val = value; + } + + void set(ExecutionEngine *engine, Heap::Base *obj) + { + if (!val) + allocVal(engine); + *val = obj; + } ReturnedValue value() const { return (val ? val->asReturnedValue() : Encode::undefined()); @@ -167,7 +184,7 @@ public: Managed *asManaged() const { if (!val) return 0; - return val->as<Managed>(); + return val->managed(); } template <typename T> T *as() const { @@ -192,6 +209,8 @@ private: Value *val; private: + Q_NEVER_INLINE void allocVal(ExecutionEngine *engine); + void free(); }; diff --git a/src/qml/jsruntime/qv4profiling.cpp b/src/qml/jsruntime/qv4profiling.cpp index a59190b846..349ec48e06 100644 --- a/src/qml/jsruntime/qv4profiling.cpp +++ b/src/qml/jsruntime/qv4profiling.cpp @@ -48,13 +48,10 @@ namespace Profiling { FunctionLocation FunctionCall::resolveLocation() const { - FunctionLocation location = { - m_function->name()->toQString(), - m_function->compilationUnit->fileName(), - m_function->compiledFunction->location.line, - m_function->compiledFunction->location.column - }; - return location; + return FunctionLocation(m_function->name()->toQString(), + m_function->compilationUnit->fileName(), + m_function->compiledFunction->location.line, + m_function->compiledFunction->location.column); } FunctionCallProperties FunctionCall::properties() const @@ -81,7 +78,8 @@ Profiler::Profiler(QV4::ExecutionEngine *engine) : featuresEnabled(0), m_engine( void Profiler::stopProfiling() { featuresEnabled = 0; - reportData(); + reportData(true); + m_sentLocations.clear(); } bool operator<(const FunctionCall &call1, const FunctionCall &call2) @@ -91,16 +89,24 @@ bool operator<(const FunctionCall &call1, const FunctionCall &call2) (call1.m_end == call2.m_end && call1.m_function < call2.m_function))); } -void Profiler::reportData() +void Profiler::reportData(bool trackLocations) { std::sort(m_data.begin(), m_data.end()); QVector<FunctionCallProperties> properties; - QHash<qint64, FunctionLocation> locations; + FunctionLocationHash locations; properties.reserve(m_data.size()); foreach (const FunctionCall &call, m_data) { properties.append(call.properties()); - locations[properties.constLast().id] = call.resolveLocation(); + Function *function = call.function(); + SentMarker &marker = m_sentLocations[reinterpret_cast<quintptr>(function)]; + if (!trackLocations || !marker.isValid()) { + FunctionLocation &location = locations[properties.constLast().id]; + if (!location.isValid()) + location = call.resolveLocation(); + if (trackLocations) + marker.setFunction(function); + } } emit dataReady(locations, properties, m_memory_data); diff --git a/src/qml/jsruntime/qv4profiling_p.h b/src/qml/jsruntime/qv4profiling_p.h index 0b4193204f..e06cb64a61 100644 --- a/src/qml/jsruntime/qv4profiling_p.h +++ b/src/qml/jsruntime/qv4profiling_p.h @@ -57,6 +57,40 @@ #include <QElapsedTimer> +#ifdef QT_NO_QML_DEBUGGER + +#define Q_V4_PROFILE_ALLOC(engine, size, type) (!engine) +#define Q_V4_PROFILE_DEALLOC(engine, size, type) (!engine) +#define Q_V4_PROFILE(engine, function) (function->code(engine, function->codeData)) + +QT_BEGIN_NAMESPACE + +namespace QV4 { +namespace Profiling { +struct Profiler {}; +} +} + +QT_END_NAMESPACE + +#else + +#define Q_V4_PROFILE_ALLOC(engine, size, type)\ + (engine->profiler() &&\ + (engine->profiler()->featuresEnabled & (1 << Profiling::FeatureMemoryAllocation)) ?\ + engine->profiler()->trackAlloc(size, type) : false) + +#define Q_V4_PROFILE_DEALLOC(engine, size, type) \ + (engine->profiler() &&\ + (engine->profiler()->featuresEnabled & (1 << Profiling::FeatureMemoryAllocation)) ?\ + engine->profiler()->trackDealloc(size, type) : false) + +#define Q_V4_PROFILE(engine, function)\ + (engine->profiler() &&\ + (engine->profiler()->featuresEnabled & (1 << Profiling::FeatureFunctionCall)) ?\ + Profiling::FunctionCallProfiler::profileCall(engine->profiler(), engine, function) :\ + function->code(engine, function->codeData)) + QT_BEGIN_NAMESPACE namespace QV4 { @@ -81,13 +115,23 @@ struct FunctionCallProperties { }; struct FunctionLocation { + FunctionLocation(const QString &name = QString(), const QString &file = QString(), + int line = -1, int column = -1) : + name(name), file(file), line(line), column(column) + {} + + bool isValid() + { + return !name.isEmpty(); + } + QString name; QString file; int line; int column; }; -typedef QHash<qint64, QV4::Profiling::FunctionLocation> FunctionLocationHash; +typedef QHash<quintptr, QV4::Profiling::FunctionLocation> FunctionLocationHash; struct MemoryAllocationProperties { qint64 timestamp; @@ -124,6 +168,11 @@ public: return *this; } + Function *function() const + { + return m_function; + } + FunctionLocation resolveLocation() const; FunctionCallProperties properties() const; @@ -135,48 +184,70 @@ private: qint64 m_end; }; -#define Q_V4_PROFILE_ALLOC(engine, size, type)\ - (engine->profiler &&\ - (engine->profiler->featuresEnabled & (1 << Profiling::FeatureMemoryAllocation)) ?\ - engine->profiler->trackAlloc(size, type) : size) - -#define Q_V4_PROFILE_DEALLOC(engine, pointer, size, type) \ - (engine->profiler &&\ - (engine->profiler->featuresEnabled & (1 << Profiling::FeatureMemoryAllocation)) ?\ - engine->profiler->trackDealloc(pointer, size, type) : pointer) - -#define Q_V4_PROFILE(engine, function)\ - (engine->profiler &&\ - (engine->profiler->featuresEnabled & (1 << Profiling::FeatureFunctionCall)) ?\ - Profiling::FunctionCallProfiler::profileCall(engine->profiler, engine, function) :\ - function->code(engine, function->codeData)) - class Q_QML_EXPORT Profiler : public QObject { Q_OBJECT - Q_DISABLE_COPY(Profiler) public: + struct SentMarker { + SentMarker() : m_function(nullptr) {} + + SentMarker(const SentMarker &other) : m_function(other.m_function) + { + if (m_function) + m_function->compilationUnit->addref(); + } + + ~SentMarker() + { + if (m_function) + m_function->compilationUnit->release(); + } + + SentMarker &operator=(const SentMarker &other) + { + if (&other != this) { + if (m_function) + m_function->compilationUnit->release(); + m_function = other.m_function; + m_function->compilationUnit->addref(); + } + return *this; + } + + void setFunction(Function *function) + { + Q_ASSERT(m_function == nullptr); + m_function = function; + m_function->compilationUnit->addref(); + } + + bool isValid() const + { return m_function != nullptr; } + + private: + Function *m_function; + }; + Profiler(QV4::ExecutionEngine *engine); - size_t trackAlloc(size_t size, MemoryType type) + bool trackAlloc(size_t size, MemoryType type) { MemoryAllocationProperties allocation = {m_timer.nsecsElapsed(), (qint64)size, type}; m_memory_data.append(allocation); - return size; + return true; } - void *trackDealloc(void *pointer, size_t size, MemoryType type) + bool trackDealloc(size_t size, MemoryType type) { MemoryAllocationProperties allocation = {m_timer.nsecsElapsed(), -(qint64)size, type}; m_memory_data.append(allocation); - return pointer; + return true; } quint64 featuresEnabled; -public slots: void stopProfiling(); void startProfiling(quint64 features); - void reportData(); + void reportData(bool trackLocations); void setTimer(const QElapsedTimer &timer) { m_timer = timer; } signals: @@ -189,6 +260,7 @@ private: QElapsedTimer m_timer; QVector<FunctionCall> m_data; QVector<MemoryAllocationProperties> m_memory_data; + QHash<quintptr, SentMarker> m_sentLocations; friend class FunctionCallProfiler; }; @@ -227,10 +299,13 @@ Q_DECLARE_TYPEINFO(QV4::Profiling::MemoryAllocationProperties, Q_MOVABLE_TYPE); Q_DECLARE_TYPEINFO(QV4::Profiling::FunctionCallProperties, Q_MOVABLE_TYPE); Q_DECLARE_TYPEINFO(QV4::Profiling::FunctionCall, Q_MOVABLE_TYPE); Q_DECLARE_TYPEINFO(QV4::Profiling::FunctionLocation, Q_MOVABLE_TYPE); +Q_DECLARE_TYPEINFO(QV4::Profiling::Profiler::SentMarker, Q_MOVABLE_TYPE); QT_END_NAMESPACE Q_DECLARE_METATYPE(QV4::Profiling::FunctionLocationHash) Q_DECLARE_METATYPE(QVector<QV4::Profiling::FunctionCallProperties>) Q_DECLARE_METATYPE(QVector<QV4::Profiling::MemoryAllocationProperties>) +#endif // QT_NO_QML_DEBUGGER + #endif // QV4PROFILING_H diff --git a/src/qml/jsruntime/qv4qobjectwrapper.cpp b/src/qml/jsruntime/qv4qobjectwrapper.cpp index 2c9fc8f9dd..d91965a350 100644 --- a/src/qml/jsruntime/qv4qobjectwrapper.cpp +++ b/src/qml/jsruntime/qv4qobjectwrapper.cpp @@ -44,7 +44,6 @@ #include <private/qqmlvmemetaobject_p.h> #include <private/qqmlbinding_p.h> #include <private/qjsvalue_p.h> -#include <private/qqmlaccessors_p.h> #include <private/qqmlexpression_p.h> #include <private/qqmlglobal_p.h> #include <private/qqmltypewrapper_p.h> @@ -54,6 +53,7 @@ #include <private/qqmlbuiltinfunctions_p.h> #include <private/qv8engine_p.h> +#include <private/qv4arraybuffer_p.h> #include <private/qv4functionobject_p.h> #include <private/qv4runtime_p.h> #include <private/qv4variantobject_p.h> @@ -74,6 +74,7 @@ #include <QtCore/qvarlengtharray.h> #include <QtCore/qtimer.h> #include <QtCore/qatomic.h> +#include <QtCore/qmetaobject.h> QT_BEGIN_NAMESPACE @@ -83,7 +84,7 @@ QT_WARNING_DISABLE_GCC("-Wstrict-aliasing") using namespace QV4; -static QPair<QObject *, int> extractQtMethod(QV4::FunctionObject *function) +QPair<QObject *, int> QObjectMethod::extractQtMethod(const QV4::FunctionObject *function) { QV4::ExecutionEngine *v4 = function->engine(); if (v4) { @@ -103,7 +104,7 @@ static QPair<QObject *, int> extractQtSignal(const Value &value) QV4::Scope scope(v4); QV4::ScopedFunctionObject function(scope, value); if (function) - return extractQtMethod(function); + return QObjectMethod::extractQtMethod(function); QV4::Scoped<QV4::QmlSignalHandler> handler(scope, value); if (handler) @@ -113,132 +114,87 @@ static QPair<QObject *, int> extractQtSignal(const Value &value) return qMakePair((QObject *)0, -1); } - -struct ReadAccessor { - static inline void Indirect(QObject *object, const QQmlPropertyData &property, - void *output, QQmlNotifier **n) - { - Q_ASSERT(n == 0); - Q_UNUSED(n); - - void *args[] = { output, 0 }; - QMetaObject::metacall(object, QMetaObject::ReadProperty, property.coreIndex, args); - } - - static inline void Direct(QObject *object, const QQmlPropertyData &property, - void *output, QQmlNotifier **n) - { - Q_ASSERT(n == 0); - Q_UNUSED(n); - - void *args[] = { output, 0 }; - object->qt_metacall(QMetaObject::ReadProperty, property.coreIndex, args); - } - - static inline void Accessor(QObject *object, const QQmlPropertyData &property, - void *output, QQmlNotifier **n) - { - Q_ASSERT(property.accessors); - - property.accessors->read(object, output); - if (n) property.accessors->notifier(object, n); - } -}; - -// Load value properties -template<void (*ReadFunction)(QObject *, const QQmlPropertyData &, - void *, QQmlNotifier **)> -static QV4::ReturnedValue LoadProperty(QV4::ExecutionEngine *v4, QObject *object, - const QQmlPropertyData &property, - QQmlNotifier **notifier) +static QV4::ReturnedValue loadProperty(QV4::ExecutionEngine *v4, QObject *object, + const QQmlPropertyData &property) { Q_ASSERT(!property.isFunction()); QV4::Scope scope(v4); if (property.isQObject()) { QObject *rv = 0; - ReadFunction(object, property, &rv, notifier); + property.readProperty(object, &rv); return QV4::QObjectWrapper::wrap(v4, rv); } else if (property.isQList()) { - return QmlListWrapper::create(v4, object, property.coreIndex, property.propType); - } else if (property.propType == QMetaType::QReal) { + return QmlListWrapper::create(v4, object, property.coreIndex(), property.propType()); + } else if (property.propType() == QMetaType::QReal) { qreal v = 0; - ReadFunction(object, property, &v, notifier); + property.readProperty(object, &v); return QV4::Encode(v); - } else if (property.propType == QMetaType::Int || property.isEnum()) { + } else if (property.propType() == QMetaType::Int || property.isEnum()) { int v = 0; - ReadFunction(object, property, &v, notifier); + property.readProperty(object, &v); return QV4::Encode(v); - } else if (property.propType == QMetaType::Bool) { + } else if (property.propType() == QMetaType::Bool) { bool v = false; - ReadFunction(object, property, &v, notifier); + property.readProperty(object, &v); return QV4::Encode(v); - } else if (property.propType == QMetaType::QString) { + } else if (property.propType() == QMetaType::QString) { QString v; - ReadFunction(object, property, &v, notifier); + property.readProperty(object, &v); return v4->newString(v)->asReturnedValue(); - } else if (property.propType == QMetaType::UInt) { + } else if (property.propType() == QMetaType::UInt) { uint v = 0; - ReadFunction(object, property, &v, notifier); + property.readProperty(object, &v); return QV4::Encode(v); - } else if (property.propType == QMetaType::Float) { + } else if (property.propType() == QMetaType::Float) { float v = 0; - ReadFunction(object, property, &v, notifier); + property.readProperty(object, &v); return QV4::Encode(v); - } else if (property.propType == QMetaType::Double) { + } else if (property.propType() == QMetaType::Double) { double v = 0; - ReadFunction(object, property, &v, notifier); + property.readProperty(object, &v); return QV4::Encode(v); } else if (property.isV4Handle()) { QQmlV4Handle handle; - ReadFunction(object, property, &handle, notifier); + property.readProperty(object, &handle); return handle; - } else if (property.propType == qMetaTypeId<QJSValue>()) { + } else if (property.propType() == qMetaTypeId<QJSValue>()) { QJSValue v; - ReadFunction(object, property, &v, notifier); + property.readProperty(object, &v); return QJSValuePrivate::convertedToValue(v4, v); } else if (property.isQVariant()) { QVariant v; - ReadFunction(object, property, &v, notifier); + property.readProperty(object, &v); if (QQmlValueTypeFactory::isValueType(v.userType())) { if (const QMetaObject *valueTypeMetaObject = QQmlValueTypeFactory::metaObjectForMetaType(v.userType())) - return QV4::QQmlValueTypeWrapper::create(v4, object, property.coreIndex, valueTypeMetaObject, v.userType()); // VariantReference value-type. + return QV4::QQmlValueTypeWrapper::create(v4, object, property.coreIndex(), valueTypeMetaObject, v.userType()); // VariantReference value-type. } return scope.engine->fromVariant(v); - } else if (QQmlValueTypeFactory::isValueType(property.propType)) { - Q_ASSERT(notifier == 0); - - if (const QMetaObject *valueTypeMetaObject = QQmlValueTypeFactory::metaObjectForMetaType(property.propType)) - return QV4::QQmlValueTypeWrapper::create(v4, object, property.coreIndex, valueTypeMetaObject, property.propType); + } else if (QQmlValueTypeFactory::isValueType(property.propType())) { + if (const QMetaObject *valueTypeMetaObject = QQmlValueTypeFactory::metaObjectForMetaType(property.propType())) + return QV4::QQmlValueTypeWrapper::create(v4, object, property.coreIndex(), valueTypeMetaObject, property.propType()); } else { - Q_ASSERT(notifier == 0); - // see if it's a sequence type bool succeeded = false; - QV4::ScopedValue retn(scope, QV4::SequencePrototype::newSequence(v4, property.propType, object, property.coreIndex, &succeeded)); + QV4::ScopedValue retn(scope, QV4::SequencePrototype::newSequence(v4, property.propType(), object, property.coreIndex(), &succeeded)); if (succeeded) return retn->asReturnedValue(); } - if (property.propType == QMetaType::UnknownType) { - QMetaProperty p = object->metaObject()->property(property.coreIndex); + if (property.propType() == QMetaType::UnknownType) { + QMetaProperty p = object->metaObject()->property(property.coreIndex()); qWarning("QMetaProperty::read: Unable to handle unregistered datatype '%s' for property " "'%s::%s'", p.typeName(), object->metaObject()->className(), p.name()); return QV4::Encode::undefined(); } else { - QVariant v(property.propType, (void *)0); - ReadFunction(object, property, v.data(), notifier); + QVariant v(property.propType(), (void *)0); + property.readProperty(object, v.data()); return scope.engine->fromVariant(v); } } -Heap::QObjectWrapper::QObjectWrapper(QObject *object) - : object(object) -{ -} - void QObjectWrapper::initializeBindings(ExecutionEngine *engine) { engine->functionPrototype()->defineDefaultProperty(QStringLiteral("connect"), method_connect); @@ -249,21 +205,21 @@ QQmlPropertyData *QObjectWrapper::findProperty(ExecutionEngine *engine, QQmlCont { Q_UNUSED(revisionMode); - QQmlData *ddata = QQmlData::get(d()->object, false); + QQmlData *ddata = QQmlData::get(d()->object(), false); if (!ddata) return 0; QQmlPropertyData *result = 0; if (ddata && ddata->propertyCache) - result = ddata->propertyCache->property(name, d()->object, qmlContext); + result = ddata->propertyCache->property(name, d()->object(), qmlContext); else - result = QQmlPropertyCache::property(engine->jsEngine(), d()->object, name, qmlContext, *local); + result = QQmlPropertyCache::property(engine->jsEngine(), d()->object(), name, qmlContext, *local); return result; } ReturnedValue QObjectWrapper::getQmlProperty(QQmlContextData *qmlContext, String *name, QObjectWrapper::RevisionMode revisionMode, bool *hasProperty, bool includeImports) const { - if (QQmlData::wasDeleted(d()->object)) { + if (QQmlData::wasDeleted(d()->object())) { if (hasProperty) *hasProperty = false; return QV4::Encode::undefined(); @@ -276,7 +232,7 @@ ReturnedValue QObjectWrapper::getQmlProperty(QQmlContextData *qmlContext, String if (hasProperty) *hasProperty = true; ExecutionContext *global = v4->rootContext(); - return QV4::QObjectMethod::create(global, d()->object, index); + return QV4::QObjectMethod::create(global, d()->object(), index); } QQmlPropertyData local; @@ -295,10 +251,10 @@ ReturnedValue QObjectWrapper::getQmlProperty(QQmlContextData *qmlContext, String if (r.scriptIndex != -1) { return QV4::Encode::undefined(); } else if (r.type) { - return QmlTypeWrapper::create(v4, d()->object, + return QmlTypeWrapper::create(v4, d()->object(), r.type, Heap::QmlTypeWrapper::ExcludeEnums); } else if (r.importNamespace) { - return QmlTypeWrapper::create(v4, d()->object, + return QmlTypeWrapper::create(v4, d()->object(), qmlContext->imports, r.importNamespace, Heap::QmlTypeWrapper::ExcludeEnums); } Q_ASSERT(!"Unreachable"); @@ -308,7 +264,7 @@ ReturnedValue QObjectWrapper::getQmlProperty(QQmlContextData *qmlContext, String return QV4::Object::get(this, name, hasProperty); } - QQmlData *ddata = QQmlData::get(d()->object, false); + QQmlData *ddata = QQmlData::get(d()->object(), false); if (revisionMode == QV4::QObjectWrapper::CheckRevision && result->hasRevision()) { if (ddata && ddata->propertyCache && !ddata->propertyCache->isAllowedInRevision(result)) { @@ -321,69 +277,44 @@ ReturnedValue QObjectWrapper::getQmlProperty(QQmlContextData *qmlContext, String if (hasProperty) *hasProperty = true; - return getProperty(v4, d()->object, result); + return getProperty(v4, d()->object(), result); } ReturnedValue QObjectWrapper::getProperty(ExecutionEngine *engine, QObject *object, QQmlPropertyData *property, bool captureRequired) { - QQmlData::flushPendingBinding(object, property->coreIndex); + QQmlData::flushPendingBinding(object, QQmlPropertyIndex(property->coreIndex())); if (property->isFunction() && !property->isVarProperty()) { if (property->isVMEFunction()) { QQmlVMEMetaObject *vmemo = QQmlVMEMetaObject::get(object); Q_ASSERT(vmemo); - return vmemo->vmeMethod(property->coreIndex); + return vmemo->vmeMethod(property->coreIndex()); } else if (property->isV4Function()) { Scope scope(engine); ScopedContext global(scope, engine->qmlContext()); if (!global) global = engine->rootContext(); - return QV4::QObjectMethod::create(global, object, property->coreIndex); + return QV4::QObjectMethod::create(global, object, property->coreIndex()); } else if (property->isSignalHandler()) { QmlSignalHandler::initProto(engine); - return engine->memoryManager->allocObject<QV4::QmlSignalHandler>(object, property->coreIndex)->asReturnedValue(); + return engine->memoryManager->allocObject<QV4::QmlSignalHandler>(object, property->coreIndex())->asReturnedValue(); } else { ExecutionContext *global = engine->rootContext(); - return QV4::QObjectMethod::create(global, object, property->coreIndex); + return QV4::QObjectMethod::create(global, object, property->coreIndex()); } } QQmlEnginePrivate *ep = engine->qmlEngine() ? QQmlEnginePrivate::get(engine->qmlEngine()) : 0; - if (property->hasAccessors()) { - QQmlNotifier *n = 0; - QQmlNotifier **nptr = 0; - - if (ep && ep->propertyCapture && property->accessors->notifier) - nptr = &n; - - Scope scope(engine); - QV4::ScopedValue rv(scope, LoadProperty<ReadAccessor::Accessor>(engine, object, *property, nptr)); - - if (captureRequired) { - if (property->accessors->notifier) { - if (n && ep->propertyCapture) - ep->propertyCapture->captureProperty(n); - } else { - if (ep->propertyCapture) - ep->propertyCapture->captureProperty(object, property->coreIndex, property->notifyIndex); - } - } - - return rv->asReturnedValue(); - } - if (captureRequired && ep && ep->propertyCapture && !property->isConstant()) - ep->propertyCapture->captureProperty(object, property->coreIndex, property->notifyIndex); + ep->propertyCapture->captureProperty(object, property->coreIndex(), property->notifyIndex()); if (property->isVarProperty()) { QQmlVMEMetaObject *vmemo = QQmlVMEMetaObject::get(object); Q_ASSERT(vmemo); - return vmemo->vmeProperty(property->coreIndex); - } else if (property->isDirect()) { - return LoadProperty<ReadAccessor::Direct>(engine, object, *property, 0); + return vmemo->vmeProperty(property->coreIndex()); } else { - return LoadProperty<ReadAccessor::Indirect>(engine, object, *property, 0); + return loadProperty(engine, object, *property); } } @@ -446,13 +377,13 @@ void QObjectWrapper::setProperty(ExecutionEngine *engine, QObject *object, QQmlP QV4::ScopedFunctionObject f(scope, value); if (f) { if (!f->isBinding()) { - if (!property->isVarProperty() && property->propType != qMetaTypeId<QJSValue>()) { + if (!property->isVarProperty() && property->propType() != qMetaTypeId<QJSValue>()) { // assigning a JS function to a non var or QJSValue property or is not allowed. QString error = QLatin1String("Cannot assign JavaScript function to "); - if (!QMetaType::typeName(property->propType)) + if (!QMetaType::typeName(property->propType())) error += QLatin1String("[unknown property type]"); else - error += QLatin1String(QMetaType::typeName(property->propType)); + error += QLatin1String(QMetaType::typeName(property->propType())); scope.engine->throwError(error); return; } @@ -463,21 +394,21 @@ void QObjectWrapper::setProperty(ExecutionEngine *engine, QObject *object, QQmlP QV4::Scoped<QQmlBindingFunction> bindingFunction(scope, (const Value &)f); bindingFunction->initBindingLocation(); - newBinding = new QQmlBinding(value, object, callingQmlContext); - newBinding->setTarget(object, *property); + newBinding = QQmlBinding::create(property, value, object, callingQmlContext); + newBinding->setTarget(object, *property, nullptr); } } if (newBinding) QQmlPropertyPrivate::setBinding(newBinding); else - QQmlPropertyPrivate::removeBinding(object, property->encodedIndex()); + QQmlPropertyPrivate::removeBinding(object, QQmlPropertyIndex(property->coreIndex())); if (!newBinding && property->isVarProperty()) { // allow assignment of "special" values (null, undefined, function) to var properties QQmlVMEMetaObject *vmemo = QQmlVMEMetaObject::get(object); Q_ASSERT(vmemo); - vmemo->setVMEProperty(property->coreIndex, value); + vmemo->setVMEProperty(property->coreIndex(), value); return; } @@ -486,44 +417,44 @@ void QObjectWrapper::setProperty(ExecutionEngine *engine, QObject *object, QQmlP int status = -1; \ int flags = 0; \ void *argv[] = { &o, 0, &status, &flags }; \ - QMetaObject::metacall(object, QMetaObject::WriteProperty, property->coreIndex, argv); + QMetaObject::metacall(object, QMetaObject::WriteProperty, property->coreIndex(), argv); if (value.isNull() && property->isQObject()) { PROPERTY_STORE(QObject*, 0); } else if (value.isUndefined() && property->isResettable()) { void *a[] = { 0 }; - QMetaObject::metacall(object, QMetaObject::ResetProperty, property->coreIndex, a); - } else if (value.isUndefined() && property->propType == qMetaTypeId<QVariant>()) { + QMetaObject::metacall(object, QMetaObject::ResetProperty, property->coreIndex(), a); + } else if (value.isUndefined() && property->propType() == qMetaTypeId<QVariant>()) { PROPERTY_STORE(QVariant, QVariant()); - } else if (value.isUndefined() && property->propType == QMetaType::QJsonValue) { + } else if (value.isUndefined() && property->propType() == QMetaType::QJsonValue) { PROPERTY_STORE(QJsonValue, QJsonValue(QJsonValue::Undefined)); - } else if (!newBinding && property->propType == qMetaTypeId<QJSValue>()) { + } else if (!newBinding && property->propType() == qMetaTypeId<QJSValue>()) { PROPERTY_STORE(QJSValue, QJSValue(scope.engine, value.asReturnedValue())); - } else if (value.isUndefined() && property->propType != qMetaTypeId<QQmlScriptString>()) { + } else if (value.isUndefined() && property->propType() != qMetaTypeId<QQmlScriptString>()) { QString error = QLatin1String("Cannot assign [undefined] to "); - if (!QMetaType::typeName(property->propType)) + if (!QMetaType::typeName(property->propType())) error += QLatin1String("[unknown property type]"); else - error += QLatin1String(QMetaType::typeName(property->propType)); + error += QLatin1String(QMetaType::typeName(property->propType())); scope.engine->throwError(error); return; } else if (value.as<FunctionObject>()) { // this is handled by the binding creation above - } else if (property->propType == QMetaType::Int && value.isNumber()) { + } else if (property->propType() == QMetaType::Int && value.isNumber()) { PROPERTY_STORE(int, value.asDouble()); - } else if (property->propType == QMetaType::QReal && value.isNumber()) { + } else if (property->propType() == QMetaType::QReal && value.isNumber()) { PROPERTY_STORE(qreal, qreal(value.asDouble())); - } else if (property->propType == QMetaType::Float && value.isNumber()) { + } else if (property->propType() == QMetaType::Float && value.isNumber()) { PROPERTY_STORE(float, float(value.asDouble())); - } else if (property->propType == QMetaType::Double && value.isNumber()) { + } else if (property->propType() == QMetaType::Double && value.isNumber()) { PROPERTY_STORE(double, double(value.asDouble())); - } else if (property->propType == QMetaType::QString && value.isString()) { + } else if (property->propType() == QMetaType::QString && value.isString()) { PROPERTY_STORE(QString, value.toQStringNoThrow()); } else if (property->isVarProperty()) { QQmlVMEMetaObject *vmemo = QQmlVMEMetaObject::get(object); Q_ASSERT(vmemo); - vmemo->setVMEProperty(property->coreIndex, value); - } else if (property->propType == qMetaTypeId<QQmlScriptString>() && (value.isUndefined() || value.isPrimitive())) { + vmemo->setVMEProperty(property->coreIndex(), value); + } else if (property->propType() == qMetaTypeId<QQmlScriptString>() && (value.isUndefined() || value.isPrimitive())) { QQmlScriptString ss(value.toQStringNoThrow(), 0 /* context */, object); if (value.isNumber()) { ss.d->numberValue = value.toNumber(); @@ -538,7 +469,7 @@ void QObjectWrapper::setProperty(ExecutionEngine *engine, QObject *object, QQmlP if (property->isQList()) v = scope.engine->toVariant(value, qMetaTypeId<QList<QObject *> >()); else - v = scope.engine->toVariant(value, property->propType); + v = scope.engine->toVariant(value, property->propType()); QQmlContextData *callingQmlContext = scope.engine->callingQmlContext(); if (!QQmlPropertyPrivate::write(object, *property, v, callingQmlContext)) { @@ -546,7 +477,7 @@ void QObjectWrapper::setProperty(ExecutionEngine *engine, QObject *object, QQmlP if (v.userType() == QVariant::Invalid) valueType = "null"; else valueType = QMetaType::typeName(v.userType()); - const char *targetTypeName = QMetaType::typeName(property->propType); + const char *targetTypeName = QMetaType::typeName(property->propType()); if (!targetTypeName) targetTypeName = "an unregistered type"; @@ -641,11 +572,14 @@ ReturnedValue QObjectWrapper::getProperty(ExecutionEngine *engine, QObject *obje void QObjectWrapper::setProperty(ExecutionEngine *engine, int propertyIndex, const Value &value) { - setProperty(engine, d()->object, propertyIndex, value); + setProperty(engine, d()->object(), propertyIndex, value); } void QObjectWrapper::setProperty(ExecutionEngine *engine, QObject *object, int propertyIndex, const Value &value) { + Q_ASSERT(propertyIndex < 0xffff); + Q_ASSERT(propertyIndex >= 0); + if (QQmlData::wasDeleted(object)) return; QQmlData *ddata = QQmlData::get(object, /*create*/false); @@ -697,12 +631,12 @@ void QObjectWrapper::put(Managed *m, String *name, const Value &value) QObjectWrapper *that = static_cast<QObjectWrapper*>(m); ExecutionEngine *v4 = that->engine(); - if (v4->hasException || QQmlData::wasDeleted(that->d()->object)) + if (v4->hasException || QQmlData::wasDeleted(that->d()->object())) return; QQmlContextData *qmlContext = v4->callingQmlContext(); - if (!setQmlProperty(v4, qmlContext, that->d()->object, name, QV4::QObjectWrapper::IgnoreRevision, value)) { - QQmlData *ddata = QQmlData::get(that->d()->object); + if (!setQmlProperty(v4, qmlContext, that->d()->object(), name, QV4::QObjectWrapper::IgnoreRevision, value)) { + QQmlData *ddata = QQmlData::get(that->d()->object()); // Types created by QML are not extensible at run-time, but for other QObjects we can store them // as regular JavaScript properties, like on JavaScript objects. if (ddata && ddata->context) { @@ -740,8 +674,8 @@ void QObjectWrapper::advanceIterator(Managed *m, ObjectIterator *it, Value *name QObjectWrapper *that = static_cast<QObjectWrapper*>(m); - if (that->d()->object) { - const QMetaObject *mo = that->d()->object->metaObject(); + if (that->d()->object()) { + const QMetaObject *mo = that->d()->object()->metaObject(); // These indices don't apply to gadgets, so don't block them. const bool preventDestruction = mo->superClass() || mo == &QObject::staticMetaObject; const int propertyCount = mo->propertyCount(); @@ -801,8 +735,8 @@ struct QObjectSlotDispatcher : public QtPrivate::QSlotObjectBase if (!v4) break; - QVarLengthArray<int, 9> dummy; - int *argsTypes = QQmlMetaObject(r).methodParameterTypes(This->signalIndex, dummy, 0); + QQmlMetaObject::ArgTypeStorage storage; + int *argsTypes = QQmlMetaObject(r).methodParameterTypes(This->signalIndex, &storage, 0); int argCount = argsTypes ? argsTypes[0]:0; @@ -820,7 +754,7 @@ struct QObjectSlotDispatcher : public QtPrivate::QSlotObjectBase } } - f->call(callData); + f->call(scope, callData); if (scope.hasException()) { QQmlError error = v4->catchExceptionAsQmlError(); if (error.description().isEmpty()) { @@ -865,7 +799,7 @@ struct QObjectSlotDispatcher : public QtPrivate::QSlotObjectBase (connection->thisObject.isUndefined() || RuntimeHelpers::strictEqual(*connection->thisObject.valueRef(), thisObject))) { QV4::ScopedFunctionObject f(scope, connection->function.value()); - QPair<QObject *, int> connectedFunctionData = extractQtMethod(f); + QPair<QObject *, int> connectedFunctionData = QObjectMethod::extractQtMethod(f); if (connectedFunctionData.first == receiverToDisconnect && connectedFunctionData.second == slotIndexToDisconnect) { *ret = true; @@ -980,7 +914,7 @@ ReturnedValue QObjectWrapper::method_disconnect(CallContext *ctx) if (!functionThisValue->isUndefined() && !functionThisValue->isObject()) V4THROW_ERROR("Function.prototype.disconnect: target this is not an object"); - QPair<QObject *, int> functionData = extractQtMethod(functionValue); + QPair<QObject *, int> functionData = QObjectMethod::extractQtMethod(functionValue); void *a[] = { ctx->d()->engine, @@ -1011,7 +945,7 @@ void QObjectWrapper::markObjects(Heap::Base *that, QV4::ExecutionEngine *e) { QObjectWrapper::Data *This = static_cast<QObjectWrapper::Data *>(that); - if (QObject *o = This->object.data()) { + if (QObject *o = This->object()) { QQmlVMEMetaObject *vme = QQmlVMEMetaObject::get(o); if (vme) vme->mark(e); @@ -1033,18 +967,18 @@ void QObjectWrapper::destroyObject(bool lastCall) if (!h->internalClass) return; // destroyObject already got called - if (h->object) { - QQmlData *ddata = QQmlData::get(h->object, false); + if (h->object()) { + QQmlData *ddata = QQmlData::get(h->object(), false); if (ddata) { - if (!h->object->parent() && !ddata->indestructible) { + if (!h->object()->parent() && !ddata->indestructible) { if (ddata && ddata->ownContext && ddata->context) ddata->context->emitDestruction(); // This object is notionally destroyed now ddata->isQueuedForDeletion = true; if (lastCall) - delete h->object; + delete h->object(); else - h->object->deleteLater(); + h->object()->deleteLater(); } else { // If the object is C++-owned, we still have to release the weak reference we have // to it. @@ -1108,6 +1042,7 @@ private: // Pointers to allocData union { QString *qstringPtr; + QByteArray *qbyteArrayPtr; QVariant *qvariantPtr; QList<QObject *> *qlistPtr; QJSValue *qjsValuePtr; @@ -1122,7 +1057,8 @@ private: } static QV4::ReturnedValue CallMethod(const QQmlObjectOrGadget &object, int index, int returnType, int argCount, - int *argTypes, QV4::ExecutionEngine *engine, QV4::CallData *callArgs) + int *argTypes, QV4::ExecutionEngine *engine, QV4::CallData *callArgs, + QMetaObject::Call callType = QMetaObject::InvokeMetaMethod) { if (argCount > 0) { // Convert all arguments. @@ -1134,7 +1070,7 @@ static QV4::ReturnedValue CallMethod(const QQmlObjectOrGadget &object, int index for (int ii = 0; ii < args.count(); ++ii) argData[ii] = args[ii].dataPtr(); - object.metacall(QMetaObject::InvokeMetaMethod, index, argData.data()); + object.metacall(callType, index, argData.data()); return args[0].toValue(engine); @@ -1145,14 +1081,14 @@ static QV4::ReturnedValue CallMethod(const QQmlObjectOrGadget &object, int index void *args[] = { arg.dataPtr() }; - object.metacall(QMetaObject::InvokeMetaMethod, index, args); + object.metacall(callType, index, args); return arg.toValue(engine); } else { void *args[] = { 0 }; - object.metacall(QMetaObject::InvokeMetaMethod, index, args); + object.metacall(callType, index, args); return Encode::undefined(); } @@ -1229,6 +1165,13 @@ static int MatchScore(const QV4::Value &actual, int conversionType) default: return 10; } + } else if (actual.as<ArrayBuffer>()) { + switch (conversionType) { + case QMetaType::QByteArray: + return 0; + default: + return 10; + } } else if (actual.as<ArrayObject>()) { switch (conversionType) { case QMetaType::QJsonArray: @@ -1246,6 +1189,7 @@ static int MatchScore(const QV4::Value &actual, int conversionType) } } else if (actual.isNull()) { switch (conversionType) { + case QMetaType::Nullptr: case QMetaType::VoidStar: case QMetaType::QObjectStar: case QMetaType::QJsonValue: @@ -1318,34 +1262,34 @@ static const QQmlPropertyData * RelatedMethod(const QQmlObjectOrGadget &object, if (!current->isOverload()) return 0; - Q_ASSERT(!current->overrideIndexIsProperty); + Q_ASSERT(!current->overrideIndexIsProperty()); if (propertyCache) { - return propertyCache->method(current->overrideIndex); + return propertyCache->method(current->overrideIndex()); } else { const QMetaObject *mo = object.metaObject(); int methodOffset = mo->methodCount() - QMetaObject_methods(mo); - while (methodOffset > current->overrideIndex) { + while (methodOffset > current->overrideIndex()) { mo = mo->superClass(); methodOffset -= QMetaObject_methods(mo); } // If we've been called before with the same override index, then // we can't go any further... - if (&dummy == current && dummy.coreIndex == current->overrideIndex) + if (&dummy == current && dummy.coreIndex() == current->overrideIndex()) return 0; - QMetaMethod method = mo->method(current->overrideIndex); + QMetaMethod method = mo->method(current->overrideIndex()); dummy.load(method); // Look for overloaded methods QByteArray methodName = method.name(); - for (int ii = current->overrideIndex - 1; ii >= methodOffset; --ii) { + for (int ii = current->overrideIndex() - 1; ii >= methodOffset; --ii) { if (methodName == mo->method(ii).name()) { - dummy.setFlags(dummy.getFlags() | QQmlPropertyData::IsOverload); - dummy.overrideIndexIsProperty = 0; - dummy.overrideIndex = ii; + dummy.setOverload(true); + dummy.setOverrideIndexIsProperty(0); + dummy.setOverrideIndex(ii); return &dummy; } } @@ -1355,7 +1299,8 @@ static const QQmlPropertyData * RelatedMethod(const QQmlObjectOrGadget &object, } static QV4::ReturnedValue CallPrecise(const QQmlObjectOrGadget &object, const QQmlPropertyData &data, - QV4::ExecutionEngine *engine, QV4::CallData *callArgs) + QV4::ExecutionEngine *engine, QV4::CallData *callArgs, + QMetaObject::Call callType = QMetaObject::InvokeMetaMethod) { QByteArray unknownTypeError; @@ -1370,9 +1315,13 @@ static QV4::ReturnedValue CallPrecise(const QQmlObjectOrGadget &object, const QQ if (data.hasArguments()) { int *args = 0; - QVarLengthArray<int, 9> dummy; + QQmlMetaObject::ArgTypeStorage storage; - args = object.methodParameterTypes(data.coreIndex, dummy, &unknownTypeError); + if (data.isConstructor()) + args = static_cast<const QQmlStaticMetaObject&>(object).constructorParameterTypes( + data.coreIndex(), &storage, &unknownTypeError); + else + args = object.methodParameterTypes(data.coreIndex(), &storage, &unknownTypeError); if (!args) { QString typeName = QString::fromLatin1(unknownTypeError); @@ -1385,11 +1334,11 @@ static QV4::ReturnedValue CallPrecise(const QQmlObjectOrGadget &object, const QQ return engine->throwError(error); } - return CallMethod(object, data.coreIndex, returnType, args[0], args + 1, engine, callArgs); + return CallMethod(object, data.coreIndex(), returnType, args[0], args + 1, engine, callArgs, callType); } else { - return CallMethod(object, data.coreIndex, returnType, 0, 0, engine, callArgs); + return CallMethod(object, data.coreIndex(), returnType, 0, 0, engine, callArgs, callType); } } @@ -1408,7 +1357,8 @@ Resolve the overloaded method to call. The algorithm works conceptually like th score is constructed by adding the matchScore() result for each of the parameters. */ static QV4::ReturnedValue CallOverloaded(const QQmlObjectOrGadget &object, const QQmlPropertyData &data, - QV4::ExecutionEngine *engine, QV4::CallData *callArgs, const QQmlPropertyCache *propertyCache) + QV4::ExecutionEngine *engine, QV4::CallData *callArgs, const QQmlPropertyCache *propertyCache, + QMetaObject::Call callType = QMetaObject::InvokeMetaMethod) { int argumentCount = callArgs->argc; @@ -1423,11 +1373,11 @@ static QV4::ReturnedValue CallOverloaded(const QQmlObjectOrGadget &object, const QV4::ScopedValue v(scope); do { - QVarLengthArray<int, 9> dummy; + QQmlMetaObject::ArgTypeStorage storage; int methodArgumentCount = 0; int *methodArgTypes = 0; if (attempt->hasArguments()) { - int *args = object.methodParameterTypes(attempt->coreIndex, dummy, 0); + int *args = object.methodParameterTypes(attempt->coreIndex(), &storage, 0); if (!args) // Must be an unknown argument continue; @@ -1458,13 +1408,13 @@ static QV4::ReturnedValue CallOverloaded(const QQmlObjectOrGadget &object, const } while ((attempt = RelatedMethod(object, attempt, dummy, propertyCache)) != 0); if (best.isValid()) { - return CallPrecise(object, best, engine, callArgs); + return CallPrecise(object, best, engine, callArgs, callType); } else { QString error = QLatin1String("Unable to determine callable overload. Candidates are:"); const QQmlPropertyData *candidate = &data; while (candidate) { error += QLatin1String("\n ") + - QString::fromUtf8(object.metaObject()->method(candidate->coreIndex) + QString::fromUtf8(object.metaObject()->method(candidate->coreIndex()) .methodSignature()); candidate = RelatedMethod(object, candidate, dummy, propertyCache); } @@ -1487,6 +1437,8 @@ void CallArgument::cleanup() { if (type == QMetaType::QString) { qstringPtr->~QString(); + } else if (type == QMetaType::QByteArray) { + qbyteArrayPtr->~QByteArray(); } else if (type == -1 || type == QMetaType::QVariant) { qvariantPtr->~QVariant(); } else if (type == qMetaTypeId<QJSValue>()) { @@ -1686,6 +1638,8 @@ QV4::ReturnedValue CallArgument::toValue(QV4::ExecutionEngine *engine) return QV4::Encode(floatValue); } else if (type == QMetaType::QString) { return QV4::Encode(engine->newString(*qstringPtr)); + } else if (type == QMetaType::QByteArray) { + return QV4::Encode(engine->newArrayBuffer(*qbyteArrayPtr)); } else if (type == QMetaType::QObjectStar) { QObject *object = qobjectPtr; if (object) @@ -1728,10 +1682,10 @@ ReturnedValue QObjectMethod::create(ExecutionContext *scope, QObject *object, in { Scope valueScope(scope); Scoped<QObjectMethod> method(valueScope, scope->d()->engine->memoryManager->allocObject<QObjectMethod>(scope)); - method->d()->object = object; + method->d()->setObject(object); if (QQmlData *ddata = QQmlData::get(object)) - method->d()->propertyCache = ddata->propertyCache; + method->d()->setPropertyCache(ddata->propertyCache); method->d()->index = index; return method.asReturnedValue(); @@ -1740,23 +1694,23 @@ ReturnedValue QObjectMethod::create(ExecutionContext *scope, QObject *object, in ReturnedValue QObjectMethod::create(ExecutionContext *scope, const QQmlValueTypeWrapper *valueType, int index) { Scope valueScope(scope); - Scoped<QObjectMethod> method(valueScope, scope->d()->engine->memoryManager->allocObject<QObjectMethod>(scope)); - method->d()->propertyCache = valueType->d()->propertyCache; + Scoped<QObjectMethod> method(valueScope, valueScope.engine->memoryManager->allocObject<QObjectMethod>(scope)); + method->d()->setPropertyCache(valueType->d()->propertyCache()); method->d()->index = index; method->d()->valueTypeWrapper = valueType->d(); return method.asReturnedValue(); } -Heap::QObjectMethod::QObjectMethod(QV4::ExecutionContext *scope) - : Heap::FunctionObject(scope) +void Heap::QObjectMethod::init(QV4::ExecutionContext *scope) { + Heap::FunctionObject::init(scope); } const QMetaObject *Heap::QObjectMethod::metaObject() { - if (propertyCache) - return propertyCache->createMetaObject(); - return object->metaObject(); + if (propertyCache()) + return propertyCache()->createMetaObject(); + return object()->metaObject(); } QV4::ReturnedValue QObjectMethod::method_toString(QV4::ExecutionContext *ctx) const @@ -1764,17 +1718,13 @@ QV4::ReturnedValue QObjectMethod::method_toString(QV4::ExecutionContext *ctx) co QString result; if (const QMetaObject *metaObject = d()->metaObject()) { - result += QString::fromUtf8(metaObject->className()); - result += QLatin1String("(0x"); - result += QString::number((quintptr)d()->object.data(),16); + result += QString::fromUtf8(metaObject->className()) + + QLatin1String("(0x") + QString::number((quintptr)d()->object(),16); - if (d()->object) { - QString objectName = d()->object->objectName(); - if (!objectName.isEmpty()) { - result += QLatin1String(", \""); - result += objectName; - result += QLatin1Char('\"'); - } + if (d()->object()) { + QString objectName = d()->object()->objectName(); + if (!objectName.isEmpty()) + result += QLatin1String(", \"") + objectName + QLatin1Char('\"'); } result += QLatin1Char(')'); @@ -1787,9 +1737,9 @@ QV4::ReturnedValue QObjectMethod::method_toString(QV4::ExecutionContext *ctx) co QV4::ReturnedValue QObjectMethod::method_destroy(QV4::ExecutionContext *ctx, const Value *args, int argc) const { - if (!d()->object) + if (!d()->object()) return Encode::undefined(); - if (QQmlData::keepAliveDuringGarbageCollection(d()->object)) + if (QQmlData::keepAliveDuringGarbageCollection(d()->object())) return ctx->engine()->throwError(QStringLiteral("Invalid attempt to destroy() an indestructible object")); int delay = 0; @@ -1797,80 +1747,90 @@ QV4::ReturnedValue QObjectMethod::method_destroy(QV4::ExecutionContext *ctx, con delay = args[0].toUInt32(); if (delay > 0) - QTimer::singleShot(delay, d()->object, SLOT(deleteLater())); + QTimer::singleShot(delay, d()->object(), SLOT(deleteLater())); else - d()->object->deleteLater(); + d()->object()->deleteLater(); return Encode::undefined(); } -ReturnedValue QObjectMethod::call(const Managed *m, CallData *callData) +void QObjectMethod::call(const Managed *m, Scope &scope, CallData *callData) { const QObjectMethod *This = static_cast<const QObjectMethod*>(m); - return This->callInternal(callData); + This->callInternal(callData, scope); } -ReturnedValue QObjectMethod::callInternal(CallData *callData) const +void QObjectMethod::callInternal(CallData *callData, Scope &scope) const { ExecutionEngine *v4 = engine(); ExecutionContext *context = v4->currentContext; - if (d()->index == DestroyMethod) - return method_destroy(context, callData->args, callData->argc); - else if (d()->index == ToStringMethod) - return method_toString(context); + if (d()->index == DestroyMethod) { + scope.result = method_destroy(context, callData->args, callData->argc); + return; + } + + else if (d()->index == ToStringMethod) { + scope.result = method_toString(context); + return; + } - QQmlObjectOrGadget object(d()->object.data()); - if (!d()->object) { - if (!d()->valueTypeWrapper) - return Encode::undefined(); + QQmlObjectOrGadget object(d()->object()); + if (!d()->object()) { + if (!d()->valueTypeWrapper) { + scope.result = Encode::undefined(); + return; + } - object = QQmlObjectOrGadget(d()->propertyCache.data(), d()->valueTypeWrapper->gadgetPtr); + object = QQmlObjectOrGadget(d()->propertyCache(), d()->valueTypeWrapper->gadgetPtr); } QQmlPropertyData method; - if (d()->propertyCache) { - QQmlPropertyData *data = d()->propertyCache->method(d()->index); - if (!data) - return QV4::Encode::undefined(); + if (d()->propertyCache()) { + QQmlPropertyData *data = d()->propertyCache()->method(d()->index); + if (!data) { + scope.result = QV4::Encode::undefined(); + return; + } method = *data; } else { - const QMetaObject *mo = d()->object->metaObject(); + const QMetaObject *mo = d()->object()->metaObject(); const QMetaMethod moMethod = mo->method(d()->index); method.load(moMethod); - if (method.coreIndex == -1) - return QV4::Encode::undefined(); + if (method.coreIndex() == -1) { + scope.result = QV4::Encode::undefined(); + return; + } // Look for overloaded methods QByteArray methodName = moMethod.name(); const int methodOffset = mo->methodOffset(); for (int ii = d()->index - 1; ii >= methodOffset; --ii) { if (methodName == mo->method(ii).name()) { - method.setFlags(method.getFlags() | QQmlPropertyData::IsOverload); - method.overrideIndexIsProperty = 0; - method.overrideIndex = ii; + method.setOverload(true); + method.setOverrideIndexIsProperty(0); + method.setOverrideIndex(ii); break; } } } if (method.isV4Function()) { - Scope scope(v4); - QV4::ScopedValue rv(scope, QV4::Primitive::undefinedValue()); - QQmlV4Function func(callData, rv, v4); + scope.result = QV4::Encode::undefined(); + QQmlV4Function func(callData, &scope.result, v4); QQmlV4Function *funcptr = &func; void *args[] = { 0, &funcptr }; - object.metacall(QMetaObject::InvokeMetaMethod, method.coreIndex, args); + object.metacall(QMetaObject::InvokeMetaMethod, method.coreIndex(), args); - return rv->asReturnedValue(); + return; } if (!method.isOverload()) { - return CallPrecise(object, method, v4, callData); + scope.result = CallPrecise(object, method, v4, callData); } else { - return CallOverloaded(object, method, v4, callData, d()->propertyCache); + scope.result = CallOverloaded(object, method, v4, callData, d()->propertyCache()); } } @@ -1885,17 +1845,193 @@ void QObjectMethod::markObjects(Heap::Base *that, ExecutionEngine *e) DEFINE_OBJECT_VTABLE(QObjectMethod); -Heap::QmlSignalHandler::QmlSignalHandler(QObject *object, int signalIndex) - : object(object) - , signalIndex(signalIndex) + +void Heap::QMetaObjectWrapper::init(const QMetaObject *metaObject) +{ + FunctionObject::init(); + this->metaObject = metaObject; + constructors = nullptr; + constructorCount = 0; +} + +void Heap::QMetaObjectWrapper::destroy() +{ + delete[] constructors; +} + +void Heap::QMetaObjectWrapper::ensureConstructorsCache() { + + const int count = metaObject->constructorCount(); + if (constructorCount != count) { + delete[] constructors; + constructorCount = count; + if (count == 0) { + constructors = nullptr; + return; + } + constructors = new QQmlPropertyData[count]; + + for (int i = 0; i < count; ++i) { + QMetaMethod method = metaObject->constructor(i); + QQmlPropertyData &d = constructors[i]; + d.load(method); + d.setCoreIndex(i); + } + } +} + + +ReturnedValue QMetaObjectWrapper::create(ExecutionEngine *engine, const QMetaObject* metaObject) { + + QV4::Scope scope(engine); + Scoped<QMetaObjectWrapper> mo(scope, engine->memoryManager->allocObject<QV4::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), Primitive::fromInt32(value)); + } + } +} + +void QMetaObjectWrapper::construct(const Managed *m, Scope &scope, CallData *callData) +{ + const QMetaObjectWrapper *This = static_cast<const QMetaObjectWrapper*>(m); + scope.result = This->constructInternal(callData); +} + +ReturnedValue QMetaObjectWrapper::constructInternal(CallData * callData) const { + + d()->ensureConstructorsCache(); + + ExecutionEngine *v4 = engine(); + const QMetaObject* mo = d()->metaObject; + if (d()->constructorCount == 0) { + return v4->throwTypeError(QStringLiteral("%1 has no invokable constructor") + .arg(QLatin1String(mo->className()))); + } + + Scope scope(v4); + Scoped<QObjectWrapper> object(scope); + + if (d()->constructorCount == 1) { + object = callConstructor(d()->constructors[0], v4, callData); + } + else { + object = callOverloadedConstructor(v4, callData); + } + Scoped<QMetaObjectWrapper> metaObject(scope, this); + object->defineDefaultProperty(v4->id_constructor(), metaObject); + object->setPrototype(const_cast<QMetaObjectWrapper*>(this)); + return object.asReturnedValue(); + +} + +ReturnedValue QMetaObjectWrapper::callConstructor(const QQmlPropertyData &data, QV4::ExecutionEngine *engine, QV4::CallData *callArgs) const { + + const QMetaObject* mo = d()->metaObject; + const QQmlStaticMetaObject object(mo); + return CallPrecise(object, data, engine, callArgs, QMetaObject::CreateInstance); +} + + +ReturnedValue QMetaObjectWrapper::callOverloadedConstructor(QV4::ExecutionEngine *engine, QV4::CallData *callArgs) const { + const int numberOfConstructors = d()->constructorCount; + const int argumentCount = callArgs->argc; + const QQmlStaticMetaObject object(d()->metaObject); + + QQmlPropertyData best; + int bestParameterScore = INT_MAX; + int bestMatchScore = INT_MAX; + + QV4::Scope scope(engine); + QV4::ScopedValue v(scope); + + for (int i = 0; i < numberOfConstructors; i++) { + const QQmlPropertyData & attempt = d()->constructors[i]; + int methodArgumentCount = 0; + int *methodArgTypes = 0; + if (attempt.hasArguments()) { + QQmlMetaObject::ArgTypeStorage storage; + int *args = object.constructorParameterTypes(attempt.coreIndex(), &storage, 0); + if (!args) // Must be an unknown argument + continue; + + methodArgumentCount = args[0]; + methodArgTypes = args + 1; + } + + if (methodArgumentCount > argumentCount) + continue; // We don't have sufficient arguments to call this method + + int methodParameterScore = argumentCount - methodArgumentCount; + if (methodParameterScore > bestParameterScore) + continue; // We already have a better option + + int methodMatchScore = 0; + for (int ii = 0; ii < methodArgumentCount; ++ii) + methodMatchScore += MatchScore((v = callArgs->args[ii]), methodArgTypes[ii]); + + if (bestParameterScore > methodParameterScore || bestMatchScore > methodMatchScore) { + best = attempt; + bestParameterScore = methodParameterScore; + bestMatchScore = methodMatchScore; + } + + if (bestParameterScore == 0 && bestMatchScore == 0) + break; // We can't get better than that + }; + + if (best.isValid()) { + return CallPrecise(object, best, engine, callArgs, QMetaObject::CreateInstance); + } else { + QString error = QLatin1String("Unable to determine callable overload. Candidates are:"); + for (int i = 0; i < numberOfConstructors; i++) { + const QQmlPropertyData & candidate = d()->constructors[i]; + error += QLatin1String("\n ") + + QString::fromUtf8(d()->metaObject->constructor(candidate.coreIndex()) + .methodSignature()); + } + + return engine->throwError(error); + } +} + +bool QMetaObjectWrapper::isEqualTo(Managed *a, Managed *b) +{ + Q_ASSERT(a->as<QMetaObjectWrapper>()); + QMetaObjectWrapper *aMetaObject = a->as<QMetaObjectWrapper>(); + QMetaObjectWrapper *bMetaObject = b->as<QMetaObjectWrapper>(); + if (!bMetaObject) + return true; + return aMetaObject->metaObject() == bMetaObject->metaObject(); +} + +DEFINE_OBJECT_VTABLE(QMetaObjectWrapper); + + + + +void Heap::QmlSignalHandler::init(QObject *object, int signalIndex) { + Object::init(); + this->signalIndex = signalIndex; + setObject(object); } DEFINE_OBJECT_VTABLE(QmlSignalHandler); void QmlSignalHandler::initProto(ExecutionEngine *engine) { - if (engine->signalHandlerPrototype()->d()) + if (engine->signalHandlerPrototype()->d_unchecked()) return; Scope scope(engine); diff --git a/src/qml/jsruntime/qv4qobjectwrapper_p.h b/src/qml/jsruntime/qv4qobjectwrapper_p.h index 9c5862b80e..504f6a69b8 100644 --- a/src/qml/jsruntime/qv4qobjectwrapper_p.h +++ b/src/qml/jsruntime/qv4qobjectwrapper_p.h @@ -78,25 +78,78 @@ namespace Heap { struct QQmlValueTypeWrapper; struct Q_QML_EXPORT QObjectWrapper : Object { - QObjectWrapper(QObject *object); - QPointer<QObject> object; + void init(QObject *object) + { + Object::init(); + qObj.init(object); + } + + void destroy() { + qObj.destroy(); + Object::destroy(); + } + + QObject *object() const { return qObj.data(); } + +private: + QQmlQPointer<QObject> qObj; }; struct QObjectMethod : FunctionObject { - QObjectMethod(QV4::ExecutionContext *scope); - QPointer<QObject> object; - QQmlRefPointer<QQmlPropertyCache> propertyCache; - int index; + void init(QV4::ExecutionContext *scope); + void destroy() + { + setPropertyCache(nullptr); + qObj.destroy(); + FunctionObject::destroy(); + } + + QQmlPropertyCache *propertyCache() const { return _propertyCache; } + void setPropertyCache(QQmlPropertyCache *c) { + if (c) + c->addref(); + if (_propertyCache) + _propertyCache->release(); + _propertyCache = c; + } Pointer<QQmlValueTypeWrapper> valueTypeWrapper; const QMetaObject *metaObject(); + QObject *object() const { return qObj.data(); } + void setObject(QObject *o) { qObj = o; } + +private: + QQmlQPointer<QObject> qObj; + QQmlPropertyCache *_propertyCache; + +public: + int index; +}; + +struct QMetaObjectWrapper : FunctionObject { + const QMetaObject* metaObject; + QQmlPropertyData *constructors; + int constructorCount; + + void init(const QMetaObject* metaObject); + void destroy(); + void ensureConstructorsCache(); }; struct QmlSignalHandler : Object { - QmlSignalHandler(QObject *object, int signalIndex); - QPointer<QObject> object; + void init(QObject *object, int signalIndex); + void destroy() { + qObj.destroy(); + Object::destroy(); + } int signalIndex; + + QObject *object() const { return qObj.data(); } + void setObject(QObject *o) { qObj = o; } + +private: + QQmlQPointer<QObject> qObj; }; } @@ -104,12 +157,13 @@ struct QmlSignalHandler : Object { struct Q_QML_EXPORT QObjectWrapper : public Object { V4_OBJECT2(QObjectWrapper, Object) + V4_NEEDS_DESTROY enum RevisionMode { IgnoreRevision, CheckRevision }; static void initializeBindings(ExecutionEngine *engine); - QObject *object() const { return d()->object.data(); } + QObject *object() const { return d()->object(); } ReturnedValue getQmlProperty(QQmlContextData *qmlContext, String *name, RevisionMode revisionMode, bool *hasProperty = 0, bool includeImports = false) const; static ReturnedValue getQmlProperty(ExecutionEngine *engine, QQmlContextData *qmlContext, QObject *object, String *name, RevisionMode revisionMode, bool *hasProperty = 0); @@ -147,7 +201,7 @@ protected: static ReturnedValue method_disconnect(CallContext *ctx); private: - static ReturnedValue wrap_slowPath(ExecutionEngine *engine, QObject *object); + Q_NEVER_INLINE static ReturnedValue wrap_slowPath(ExecutionEngine *engine, QObject *object); }; inline ReturnedValue QObjectWrapper::wrap(ExecutionEngine *engine, QObject *object) @@ -179,16 +233,38 @@ struct Q_QML_EXPORT QObjectMethod : public QV4::FunctionObject static ReturnedValue create(QV4::ExecutionContext *scope, const QQmlValueTypeWrapper *valueType, int index); int methodIndex() const { return d()->index; } - QObject *object() const { return d()->object.data(); } + QObject *object() const { return d()->object(); } QV4::ReturnedValue method_toString(QV4::ExecutionContext *ctx) const; QV4::ReturnedValue method_destroy(QV4::ExecutionContext *ctx, const Value *args, int argc) const; - static ReturnedValue call(const Managed *, CallData *callData); + static void call(const Managed *, Scope &scope, CallData *callData); - ReturnedValue callInternal(CallData *callData) const; + void callInternal(CallData *callData, Scope &scope) const; static void markObjects(Heap::Base *that, QV4::ExecutionEngine *e); + + 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 + + static ReturnedValue create(ExecutionEngine *engine, const QMetaObject* metaObject); + static void construct(const Managed *, Scope &scope, CallData *callData); + static bool isEqualTo(Managed *a, Managed *b); + + const QMetaObject *metaObject() const { return d()->metaObject; } + +private: + void init(ExecutionEngine *engine); + ReturnedValue constructInternal(CallData *callData) const; + ReturnedValue callConstructor(const QQmlPropertyData &data, QV4::ExecutionEngine *engine, QV4::CallData *callArgs) const; + ReturnedValue callOverloadedConstructor(QV4::ExecutionEngine *engine, QV4::CallData *callArgs) const; + }; struct Q_QML_EXPORT QmlSignalHandler : public QV4::Object @@ -198,7 +274,7 @@ struct Q_QML_EXPORT QmlSignalHandler : public QV4::Object V4_NEEDS_DESTROY int signalIndex() const { return d()->signalIndex; } - QObject *object() const { return d()->object.data(); } + QObject *object() const { return d()->object(); } static void initProto(ExecutionEngine *v4); }; diff --git a/src/qml/jsruntime/qv4regexp.cpp b/src/qml/jsruntime/qv4regexp.cpp index af5355c964..9e94c58432 100644 --- a/src/qml/jsruntime/qv4regexp.cpp +++ b/src/qml/jsruntime/qv4regexp.cpp @@ -62,11 +62,11 @@ uint RegExp::match(const QString &string, int start, uint *matchOffsets) WTF::String s(string); #if ENABLE(YARR_JIT) - if (!jitCode().isFallBack() && jitCode().has16BitCode()) - return uint(jitCode().execute(s.characters16(), start, s.length(), (int*)matchOffsets).start); + if (!jitCode()->isFallBack() && jitCode()->has16BitCode()) + return uint(jitCode()->execute(s.characters16(), start, s.length(), (int*)matchOffsets).start); #endif - return JSC::Yarr::interpret(byteCode().get(), s.characters16(), string.length(), start, matchOffsets); + return JSC::Yarr::interpret(byteCode(), s.characters16(), string.length(), start, matchOffsets); } Heap::RegExp *RegExp::create(ExecutionEngine* engine, const QString& pattern, bool ignoreCase, bool multiline) @@ -90,31 +90,41 @@ Heap::RegExp *RegExp::create(ExecutionEngine* engine, const QString& pattern, bo return result->d(); } -Heap::RegExp::RegExp(ExecutionEngine* engine, const QString &pattern, bool ignoreCase, bool multiline) - : pattern(pattern) - , ignoreCase(ignoreCase) - , multiLine(multiline) +void Heap::RegExp::init(ExecutionEngine* engine, const QString &pattern, bool ignoreCase, bool multiline) { + Base::init(); + this->pattern = new QString(pattern); + this->ignoreCase = ignoreCase; + this->multiLine = multiline; + const char* error = 0; JSC::Yarr::YarrPattern yarrPattern(WTF::String(pattern), ignoreCase, multiline, &error); if (error) return; subPatternCount = yarrPattern.m_numSubpatterns; - byteCode = JSC::Yarr::byteCompile(yarrPattern, engine->bumperPointerAllocator); + OwnPtr<JSC::Yarr::BytecodePattern> p = JSC::Yarr::byteCompile(yarrPattern, engine->bumperPointerAllocator); + byteCode = p.take(); #if ENABLE(YARR_JIT) + jitCode = new JSC::Yarr::YarrCodeBlock; if (!yarrPattern.m_containsBackreferences && engine->iselFactory->jitCompileRegexps()) { JSC::JSGlobalData dummy(engine->regExpAllocator); - JSC::Yarr::jitCompile(yarrPattern, JSC::Yarr::Char16, &dummy, jitCode); + JSC::Yarr::jitCompile(yarrPattern, JSC::Yarr::Char16, &dummy, *jitCode); } #endif } -Heap::RegExp::~RegExp() +void Heap::RegExp::destroy() { if (cache) { RegExpCacheKey key(this); cache->remove(key); } +#if ENABLE(YARR_JIT) + delete jitCode; +#endif + delete byteCode; + delete pattern; + Base::destroy(); } void RegExp::markObjects(Heap::Base *that, ExecutionEngine *e) diff --git a/src/qml/jsruntime/qv4regexp_p.h b/src/qml/jsruntime/qv4regexp_p.h index b99d717847..d3e63375a5 100644 --- a/src/qml/jsruntime/qv4regexp_p.h +++ b/src/qml/jsruntime/qv4regexp_p.h @@ -76,12 +76,13 @@ struct RegExpCacheKey; namespace Heap { struct RegExp : Base { - RegExp(ExecutionEngine* engine, const QString& pattern, bool ignoreCase, bool multiline); - ~RegExp(); - QString pattern; - OwnPtr<JSC::Yarr::BytecodePattern> byteCode; + void init(ExecutionEngine* engine, const QString& pattern, bool ignoreCase, bool multiline); + void destroy(); + + QString *pattern; + JSC::Yarr::BytecodePattern *byteCode; #if ENABLE(YARR_JIT) - JSC::Yarr::YarrCodeBlock jitCode; + JSC::Yarr::YarrCodeBlock *jitCode; #endif RegExpCache *cache; int subPatternCount; @@ -90,6 +91,7 @@ struct RegExp : Base { int captureCount() const { return subPatternCount + 1; } }; +V4_ASSERT_IS_TRIVIAL(RegExp) } @@ -99,10 +101,10 @@ struct RegExp : public Managed Q_MANAGED_TYPE(RegExp) V4_NEEDS_DESTROY - QString pattern() const { return d()->pattern; } - OwnPtr<JSC::Yarr::BytecodePattern> &byteCode() { return d()->byteCode; } + QString pattern() const { return *d()->pattern; } + JSC::Yarr::BytecodePattern *byteCode() { return d()->byteCode; } #if ENABLE(YARR_JIT) - JSC::Yarr::YarrCodeBlock jitCode() const { return d()->jitCode; } + JSC::Yarr::YarrCodeBlock *jitCode() const { return d()->jitCode; } #endif RegExpCache *cache() const { return d()->cache; } int subPatternCount() const { return d()->subPatternCount; } @@ -111,7 +113,7 @@ struct RegExp : public Managed static Heap::RegExp *create(ExecutionEngine* engine, const QString& pattern, bool ignoreCase = false, bool multiline = false); - bool isValid() const { return d()->byteCode.get(); } + bool isValid() const { return d()->byteCode; } uint match(const QString& string, int start, uint *matchOffsets); @@ -142,7 +144,7 @@ struct RegExpCacheKey }; inline RegExpCacheKey::RegExpCacheKey(const RegExp::Data *re) - : pattern(re->pattern) + : pattern(*re->pattern) , ignoreCase(re->ignoreCase) , multiLine(re->multiLine) {} diff --git a/src/qml/jsruntime/qv4regexpobject.cpp b/src/qml/jsruntime/qv4regexpobject.cpp index 71e2bd1a06..4022d98c3f 100644 --- a/src/qml/jsruntime/qv4regexpobject.cpp +++ b/src/qml/jsruntime/qv4regexpobject.cpp @@ -69,8 +69,9 @@ using namespace QV4; DEFINE_OBJECT_VTABLE(RegExpObject); -Heap::RegExpObject::RegExpObject() +void Heap::RegExpObject::init() { + Object::init(); Scope scope(internalClass->engine); Scoped<QV4::RegExpObject> o(scope, this); o->d()->value = QV4::RegExp::create(scope.engine, QString(), false, false); @@ -78,10 +79,11 @@ Heap::RegExpObject::RegExpObject() o->initProperties(); } -Heap::RegExpObject::RegExpObject(QV4::RegExp *value, bool global) - : value(value->d()) - , global(global) +void Heap::RegExpObject::init(QV4::RegExp *value, bool global) { + Object::init(); + this->global = global; + this->value = value->d(); Scope scope(internalClass->engine); Scoped<QV4::RegExpObject> o(scope, this); o->initProperties(); @@ -90,8 +92,9 @@ Heap::RegExpObject::RegExpObject(QV4::RegExp *value, bool global) // Converts a QRegExp to a JS RegExp. // The conversion is not 100% exact since ECMA regexp and QRegExp // have different semantics/flags, but we try to do our best. -Heap::RegExpObject::RegExpObject(const QRegExp &re) +void Heap::RegExpObject::init(const QRegExp &re) { + Object::init(); global = false; // Convert the pattern to a ECMAScript pattern. @@ -145,7 +148,7 @@ void RegExpObject::initProperties() Q_ASSERT(value()); - QString p = value()->pattern; + QString p = *value()->pattern; if (p.isEmpty()) { p = QStringLiteral("(?:)"); } else { @@ -180,13 +183,12 @@ Value *RegExpObject::lastIndexProperty() QRegExp RegExpObject::toQRegExp() const { Qt::CaseSensitivity caseSensitivity = value()->ignoreCase ? Qt::CaseInsensitive : Qt::CaseSensitive; - return QRegExp(value()->pattern, caseSensitivity, QRegExp::RegExp2); + return QRegExp(*value()->pattern, caseSensitivity, QRegExp::RegExp2); } QString RegExpObject::toString() const { - QString result = QLatin1Char('/') + source(); - result += QLatin1Char('/'); + QString result = QLatin1Char('/') + source() + QLatin1Char('/'); if (global()) result += QLatin1Char('g'); if (value()->ignoreCase) @@ -218,9 +220,9 @@ uint RegExpObject::flags() const DEFINE_OBJECT_VTABLE(RegExpCtor); -Heap::RegExpCtor::RegExpCtor(QV4::ExecutionContext *scope) - : Heap::FunctionObject(scope, QStringLiteral("RegExp")) +void Heap::RegExpCtor::init(QV4::ExecutionContext *scope) { + Heap::FunctionObject::init(scope, QStringLiteral("RegExp")); clearLastMatch(); } @@ -232,34 +234,39 @@ void Heap::RegExpCtor::clearLastMatch() lastMatchEnd = 0; } -ReturnedValue RegExpCtor::construct(const Managed *m, CallData *callData) +void RegExpCtor::construct(const Managed *, Scope &scope, CallData *callData) { - Scope scope(static_cast<const Object *>(m)->engine()); - ScopedValue r(scope, callData->argument(0)); ScopedValue f(scope, callData->argument(1)); Scoped<RegExpObject> re(scope, r); if (re) { - if (!f->isUndefined()) - return scope.engine->throwTypeError(); + if (!f->isUndefined()) { + scope.result = scope.engine->throwTypeError(); + return; + } Scoped<RegExp> regexp(scope, re->value()); - return Encode(scope.engine->newRegExpObject(regexp, re->global())); + scope.result = Encode(scope.engine->newRegExpObject(regexp, re->global())); + return; } QString pattern; if (!r->isUndefined()) pattern = r->toQString(); - if (scope.hasException()) - return Encode::undefined(); + if (scope.hasException()) { + scope.result = Encode::undefined(); + return; + } bool global = false; bool ignoreCase = false; bool multiLine = false; if (!f->isUndefined()) { f = RuntimeHelpers::toString(scope.engine, f); - if (scope.hasException()) - return Encode::undefined(); + if (scope.hasException()) { + scope.result = Encode::undefined(); + return; + } QString str = f->stringValue()->toQString(); for (int i = 0; i < str.length(); ++i) { if (str.at(i) == QLatin1Char('g') && !global) { @@ -269,26 +276,31 @@ ReturnedValue RegExpCtor::construct(const Managed *m, CallData *callData) } else if (str.at(i) == QLatin1Char('m') && !multiLine) { multiLine = true; } else { - return scope.engine->throwSyntaxError(QStringLiteral("Invalid flags supplied to RegExp constructor")); + scope.result = scope.engine->throwSyntaxError(QStringLiteral("Invalid flags supplied to RegExp constructor")); + return; } } } Scoped<RegExp> regexp(scope, RegExp::create(scope.engine, pattern, ignoreCase, multiLine)); - if (!regexp->isValid()) - return scope.engine->throwSyntaxError(QStringLiteral("Invalid regular expression")); + if (!regexp->isValid()) { + scope.result = scope.engine->throwSyntaxError(QStringLiteral("Invalid regular expression")); + return; + } - return Encode(scope.engine->newRegExpObject(regexp, global)); + scope.result = Encode(scope.engine->newRegExpObject(regexp, global)); } -ReturnedValue RegExpCtor::call(const Managed *that, CallData *callData) +void RegExpCtor::call(const Managed *that, Scope &scope, CallData *callData) { if (callData->argc > 0 && callData->args[0].as<RegExpObject>()) { - if (callData->argc == 1 || callData->args[1].isUndefined()) - return callData->args[0].asReturnedValue(); + if (callData->argc == 1 || callData->args[1].isUndefined()) { + scope.result = callData->args[0]; + return; + } } - return construct(that, callData); + construct(that, scope, callData); } void RegExpCtor::markObjects(Heap::Base *that, ExecutionEngine *e) @@ -420,7 +432,8 @@ ReturnedValue RegExpPrototype::method_compile(CallContext *ctx) ScopedCallData callData(scope, ctx->argc()); memcpy(callData->args, ctx->args(), ctx->argc()*sizeof(Value)); - Scoped<RegExpObject> re(scope, ctx->d()->engine->regExpCtor()->as<FunctionObject>()->construct(callData)); + ctx->d()->engine->regExpCtor()->as<FunctionObject>()->construct(scope, callData); + Scoped<RegExpObject> re(scope, scope.result.asReturnedValue()); r->d()->value = re->value(); r->d()->global = re->global(); diff --git a/src/qml/jsruntime/qv4regexpobject_p.h b/src/qml/jsruntime/qv4regexpobject_p.h index 4bd91bbedd..2c82cfdfd1 100644 --- a/src/qml/jsruntime/qv4regexpobject_p.h +++ b/src/qml/jsruntime/qv4regexpobject_p.h @@ -74,16 +74,16 @@ namespace QV4 { namespace Heap { struct RegExpObject : Object { - RegExpObject(); - RegExpObject(QV4::RegExp *value, bool global); - RegExpObject(const QRegExp &re); + void init(); + void init(QV4::RegExp *value, bool global); + void init(const QRegExp &re); Pointer<RegExp> value; bool global; }; struct RegExpCtor : FunctionObject { - RegExpCtor(QV4::ExecutionContext *scope); + void init(QV4::ExecutionContext *scope); Value lastMatch; Pointer<String> lastInput; int lastMatchStart; @@ -140,8 +140,8 @@ struct RegExpCtor: FunctionObject int lastMatchStart() { return d()->lastMatchStart; } int lastMatchEnd() { return d()->lastMatchEnd; } - static ReturnedValue construct(const Managed *m, CallData *callData); - static ReturnedValue call(const Managed *that, CallData *callData); + static void construct(const Managed *m, Scope &scope, CallData *callData); + static void call(const Managed *that, Scope &scope, CallData *callData); static void markObjects(Heap::Base *that, ExecutionEngine *e); }; diff --git a/src/qml/jsruntime/qv4runtime.cpp b/src/qml/jsruntime/qv4runtime.cpp index 60136a9bd9..0a71f0000a 100644 --- a/src/qml/jsruntime/qv4runtime.cpp +++ b/src/qml/jsruntime/qv4runtime.cpp @@ -252,14 +252,14 @@ void RuntimeHelpers::numberToString(QString *result, double num, int radix) result->append(QLatin1Char('+')); result->append(QString::number(decpt - 1)); } else if (decpt <= 0) { - result->prepend(QString::fromLatin1("0.%1").arg(QString().fill(zero, -decpt))); + result->prepend(QLatin1String("0.") + QString(-decpt, zero)); } else if (decpt < result->length()) { result->insert(decpt, dot); } else { - result->append(QString().fill(zero, decpt - result->length())); + result->append(QString(decpt - result->length(), zero)); } - if (sign) + if (sign && num) result->prepend(QLatin1Char('-')); return; @@ -298,14 +298,14 @@ void RuntimeHelpers::numberToString(QString *result, double num, int radix) result->prepend(QLatin1Char('-')); } -ReturnedValue Runtime::closure(ExecutionEngine *engine, int functionId) +ReturnedValue Runtime::method_closure(ExecutionEngine *engine, int functionId) { QV4::Function *clos = engine->current->compilationUnit->runtimeFunctions[functionId]; Q_ASSERT(clos); return FunctionObject::createScriptFunction(engine->currentContext, clos)->asReturnedValue(); } -ReturnedValue Runtime::deleteElement(ExecutionEngine *engine, const Value &base, const Value &index) +ReturnedValue Runtime::method_deleteElement(ExecutionEngine *engine, const Value &base, const Value &index) { Scope scope(engine); ScopedObject o(scope, base); @@ -317,17 +317,17 @@ ReturnedValue Runtime::deleteElement(ExecutionEngine *engine, const Value &base, } ScopedString name(scope, index.toString(engine)); - return Runtime::deleteMemberString(engine, base, name); + return method_deleteMemberString(engine, base, name); } -ReturnedValue Runtime::deleteMember(ExecutionEngine *engine, const Value &base, int nameIndex) +ReturnedValue Runtime::method_deleteMember(ExecutionEngine *engine, const Value &base, int nameIndex) { Scope scope(engine); ScopedString name(scope, engine->current->compilationUnit->runtimeStrings[nameIndex]); - return deleteMemberString(engine, base, name); + return method_deleteMemberString(engine, base, name); } -ReturnedValue Runtime::deleteMemberString(ExecutionEngine *engine, const Value &base, String *name) +ReturnedValue Runtime::method_deleteMemberString(ExecutionEngine *engine, const Value &base, String *name) { Scope scope(engine); ScopedObject obj(scope, base.toObject(engine)); @@ -336,14 +336,14 @@ ReturnedValue Runtime::deleteMemberString(ExecutionEngine *engine, const Value & return Encode(obj->deleteProperty(name)); } -ReturnedValue Runtime::deleteName(ExecutionEngine *engine, int nameIndex) +ReturnedValue Runtime::method_deleteName(ExecutionEngine *engine, int nameIndex) { Scope scope(engine); ScopedString name(scope, engine->current->compilationUnit->runtimeStrings[nameIndex]); return Encode(engine->currentContext->deleteProperty(name)); } -QV4::ReturnedValue Runtime::instanceof(ExecutionEngine *engine, const Value &left, const Value &right) +QV4::ReturnedValue Runtime::method_instanceof(ExecutionEngine *engine, const Value &left, const Value &right) { Scope scope(engine); ScopedFunctionObject f(scope, right.as<FunctionObject>()); @@ -373,7 +373,7 @@ QV4::ReturnedValue Runtime::instanceof(ExecutionEngine *engine, const Value &lef return Encode(false); } -QV4::ReturnedValue Runtime::in(ExecutionEngine *engine, const Value &left, const Value &right) +QV4::ReturnedValue Runtime::method_in(ExecutionEngine *engine, const Value &left, const Value &right) { if (!right.isObject()) return engine->throwTypeError(); @@ -387,7 +387,7 @@ QV4::ReturnedValue Runtime::in(ExecutionEngine *engine, const Value &left, const double RuntimeHelpers::stringToNumber(const QString &string) { - QString s = string.trimmed(); + const QStringRef s = QStringRef(&string).trimmed(); if (s.startsWith(QLatin1String("0x")) || s.startsWith(QLatin1String("0X"))) return s.toLong(0, 16); bool ok; @@ -438,9 +438,9 @@ ReturnedValue RuntimeHelpers::objectDefaultValue(const Object *object, int typeH ScopedValue conv(scope, object->get(meth1)); if (FunctionObject *o = conv->as<FunctionObject>()) { - ScopedValue r(scope, o->call(callData)); - if (r->isPrimitive()) - return r->asReturnedValue(); + o->call(scope, callData); + if (scope.result.isPrimitive()) + return scope.result.asReturnedValue(); } if (engine->hasException) @@ -448,9 +448,9 @@ ReturnedValue RuntimeHelpers::objectDefaultValue(const Object *object, int typeH conv = object->get(meth2); if (FunctionObject *o = conv->as<FunctionObject>()) { - ScopedValue r(scope, o->call(callData)); - if (r->isPrimitive()) - return r->asReturnedValue(); + o->call(scope, callData); + if (scope.result.isPrimitive()) + return scope.result.asReturnedValue(); } return engine->throwTypeError(); @@ -562,7 +562,7 @@ QV4::ReturnedValue RuntimeHelpers::addHelper(ExecutionEngine *engine, const Valu return Encode(x + y); } -QV4::ReturnedValue Runtime::addString(ExecutionEngine *engine, const Value &left, const Value &right) +QV4::ReturnedValue Runtime::method_addString(ExecutionEngine *engine, const Value &left, const Value &right) { Q_ASSERT(left.isString() || right.isString()); @@ -593,7 +593,7 @@ QV4::ReturnedValue Runtime::addString(ExecutionEngine *engine, const Value &left return (mm->alloc<String>(mm, pleft->stringValue()->d(), pright->stringValue()->d()))->asReturnedValue(); } -void Runtime::setProperty(ExecutionEngine *engine, const Value &object, int nameIndex, const Value &value) +void Runtime::method_setProperty(ExecutionEngine *engine, const Value &object, int nameIndex, const Value &value) { Scope scope(engine); ScopedString name(scope, engine->current->compilationUnit->runtimeStrings[nameIndex]); @@ -603,7 +603,7 @@ void Runtime::setProperty(ExecutionEngine *engine, const Value &object, int name o->put(name, value); } -ReturnedValue Runtime::getElement(ExecutionEngine *engine, const Value &object, const Value &index) +ReturnedValue Runtime::method_getElement(ExecutionEngine *engine, const Value &object, const Value &index) { Scope scope(engine); uint idx = index.asArrayIndex(); @@ -646,7 +646,7 @@ ReturnedValue Runtime::getElement(ExecutionEngine *engine, const Value &object, return o->get(name); } -void Runtime::setElement(ExecutionEngine *engine, const Value &object, const Value &index, const Value &value) +void Runtime::method_setElement(ExecutionEngine *engine, const Value &object, const Value &index, const Value &value) { Scope scope(engine); ScopedObject o(scope, object.toObject(engine)); @@ -670,7 +670,7 @@ void Runtime::setElement(ExecutionEngine *engine, const Value &object, const Val o->put(name, value); } -ReturnedValue Runtime::foreachIterator(ExecutionEngine *engine, const Value &in) +ReturnedValue Runtime::method_foreachIterator(ExecutionEngine *engine, const Value &in) { Scope scope(engine); ScopedObject o(scope, (Object *)0); @@ -679,7 +679,7 @@ ReturnedValue Runtime::foreachIterator(ExecutionEngine *engine, const Value &in) return engine->newForEachIteratorObject(o)->asReturnedValue(); } -ReturnedValue Runtime::foreachNextPropertyName(const Value &foreach_iterator) +ReturnedValue Runtime::method_foreachNextPropertyName(const Value &foreach_iterator) { Q_ASSERT(foreach_iterator.isObject()); @@ -690,14 +690,14 @@ ReturnedValue Runtime::foreachNextPropertyName(const Value &foreach_iterator) } -void Runtime::setActivationProperty(ExecutionEngine *engine, int nameIndex, const Value &value) +void Runtime::method_setActivationProperty(ExecutionEngine *engine, int nameIndex, const Value &value) { Scope scope(engine); ScopedString name(scope, engine->current->compilationUnit->runtimeStrings[nameIndex]); engine->currentContext->setProperty(name, value); } -ReturnedValue Runtime::getProperty(ExecutionEngine *engine, const Value &object, int nameIndex) +ReturnedValue Runtime::method_getProperty(ExecutionEngine *engine, const Value &object, int nameIndex) { Scope scope(engine); ScopedString name(scope, engine->current->compilationUnit->runtimeStrings[nameIndex]); @@ -717,7 +717,7 @@ ReturnedValue Runtime::getProperty(ExecutionEngine *engine, const Value &object, return o->get(name); } -ReturnedValue Runtime::getActivationProperty(ExecutionEngine *engine, int nameIndex) +ReturnedValue Runtime::method_getActivationProperty(ExecutionEngine *engine, int nameIndex) { Scope scope(engine); ScopedString name(scope, engine->current->compilationUnit->runtimeStrings[nameIndex]); @@ -743,9 +743,9 @@ uint RuntimeHelpers::equalHelper(const Value &x, const Value &y) double dx = RuntimeHelpers::toNumber(x); return dx == y.asDouble(); } else if (x.isBoolean()) { - return Runtime::compareEqual(Primitive::fromDouble((double) x.booleanValue()), y); + return Runtime::method_compareEqual(Primitive::fromDouble((double) x.booleanValue()), y); } else if (y.isBoolean()) { - return Runtime::compareEqual(x, Primitive::fromDouble((double) y.booleanValue())); + return Runtime::method_compareEqual(x, Primitive::fromDouble((double) y.booleanValue())); } else { #ifdef V4_BOOTSTRAP Q_UNIMPLEMENTED(); @@ -753,11 +753,11 @@ uint RuntimeHelpers::equalHelper(const Value &x, const Value &y) if ((x.isNumber() || x.isString()) && y.isObject()) { Scope scope(y.objectValue()->engine()); ScopedValue py(scope, RuntimeHelpers::toPrimitive(y, PREFERREDTYPE_HINT)); - return Runtime::compareEqual(x, py); + return Runtime::method_compareEqual(x, py); } else if (x.isObject() && (y.isNumber() || y.isString())) { Scope scope(x.objectValue()->engine()); ScopedValue px(scope, RuntimeHelpers::toPrimitive(x, PREFERREDTYPE_HINT)); - return Runtime::compareEqual(px, y); + return Runtime::method_compareEqual(px, y); } #endif } @@ -780,7 +780,7 @@ Bool RuntimeHelpers::strictEqual(const Value &x, const Value &y) return false; } -QV4::Bool Runtime::compareGreaterThan(const Value &l, const Value &r) +QV4::Bool Runtime::method_compareGreaterThan(const Value &l, const Value &r) { TRACE2(l, r); if (l.isInteger() && r.isInteger()) @@ -804,7 +804,7 @@ QV4::Bool Runtime::compareGreaterThan(const Value &l, const Value &r) QV4::Scope scope(e); QV4::ScopedValue pl(scope, RuntimeHelpers::toPrimitive(l, QV4::NUMBER_HINT)); QV4::ScopedValue pr(scope, RuntimeHelpers::toPrimitive(r, QV4::NUMBER_HINT)); - return Runtime::compareGreaterThan(pl, pr); + return Runtime::method_compareGreaterThan(pl, pr); #endif } @@ -813,7 +813,7 @@ QV4::Bool Runtime::compareGreaterThan(const Value &l, const Value &r) return dl > dr; } -QV4::Bool Runtime::compareLessThan(const Value &l, const Value &r) +QV4::Bool Runtime::method_compareLessThan(const Value &l, const Value &r) { TRACE2(l, r); if (l.isInteger() && r.isInteger()) @@ -837,7 +837,7 @@ QV4::Bool Runtime::compareLessThan(const Value &l, const Value &r) QV4::Scope scope(e); QV4::ScopedValue pl(scope, RuntimeHelpers::toPrimitive(l, QV4::NUMBER_HINT)); QV4::ScopedValue pr(scope, RuntimeHelpers::toPrimitive(r, QV4::NUMBER_HINT)); - return Runtime::compareLessThan(pl, pr); + return Runtime::method_compareLessThan(pl, pr); #endif } @@ -846,7 +846,7 @@ QV4::Bool Runtime::compareLessThan(const Value &l, const Value &r) return dl < dr; } -QV4::Bool Runtime::compareGreaterEqual(const Value &l, const Value &r) +QV4::Bool Runtime::method_compareGreaterEqual(const Value &l, const Value &r) { TRACE2(l, r); if (l.isInteger() && r.isInteger()) @@ -870,7 +870,7 @@ QV4::Bool Runtime::compareGreaterEqual(const Value &l, const Value &r) QV4::Scope scope(e); QV4::ScopedValue pl(scope, RuntimeHelpers::toPrimitive(l, QV4::NUMBER_HINT)); QV4::ScopedValue pr(scope, RuntimeHelpers::toPrimitive(r, QV4::NUMBER_HINT)); - return Runtime::compareGreaterEqual(pl, pr); + return Runtime::method_compareGreaterEqual(pl, pr); #endif } @@ -879,7 +879,7 @@ QV4::Bool Runtime::compareGreaterEqual(const Value &l, const Value &r) return dl >= dr; } -QV4::Bool Runtime::compareLessEqual(const Value &l, const Value &r) +QV4::Bool Runtime::method_compareLessEqual(const Value &l, const Value &r) { TRACE2(l, r); if (l.isInteger() && r.isInteger()) @@ -903,7 +903,7 @@ QV4::Bool Runtime::compareLessEqual(const Value &l, const Value &r) QV4::Scope scope(e); QV4::ScopedValue pl(scope, RuntimeHelpers::toPrimitive(l, QV4::NUMBER_HINT)); QV4::ScopedValue pr(scope, RuntimeHelpers::toPrimitive(r, QV4::NUMBER_HINT)); - return Runtime::compareLessEqual(pl, pr); + return Runtime::method_compareLessEqual(pl, pr); #endif } @@ -913,26 +913,26 @@ QV4::Bool Runtime::compareLessEqual(const Value &l, const Value &r) } #ifndef V4_BOOTSTRAP -Bool Runtime::compareInstanceof(ExecutionEngine *engine, const Value &left, const Value &right) +Bool Runtime::method_compareInstanceof(ExecutionEngine *engine, const Value &left, const Value &right) { TRACE2(left, right); Scope scope(engine); - ScopedValue v(scope, Runtime::instanceof(engine, left, right)); + ScopedValue v(scope, method_instanceof(engine, left, right)); return v->booleanValue(); } -uint Runtime::compareIn(ExecutionEngine *engine, const Value &left, const Value &right) +uint Runtime::method_compareIn(ExecutionEngine *engine, const Value &left, const Value &right) { TRACE2(left, right); Scope scope(engine); - ScopedValue v(scope, Runtime::in(engine, left, right)); + ScopedValue v(scope, method_in(engine, left, right)); return v->booleanValue(); } -ReturnedValue Runtime::callGlobalLookup(ExecutionEngine *engine, uint index, CallData *callData) +ReturnedValue Runtime::method_callGlobalLookup(ExecutionEngine *engine, uint index, CallData *callData) { Scope scope(engine); Q_ASSERT(callData->thisObject.isUndefined()); @@ -943,14 +943,17 @@ ReturnedValue Runtime::callGlobalLookup(ExecutionEngine *engine, uint index, Cal return engine->throwTypeError(); ScopedString name(scope, engine->current->compilationUnit->runtimeStrings[l->nameIndex]); - if (o->d() == scope.engine->evalFunction()->d() && name->equals(scope.engine->id_eval())) - return static_cast<EvalFunction *>(o.getPointer())->evalCall(callData, true); + if (o->d() == scope.engine->evalFunction()->d() && name->equals(scope.engine->id_eval())) { + static_cast<EvalFunction *>(o.getPointer())->evalCall(scope, callData, true); + } else { + o->call(scope, callData); + } - return o->call(callData); + return scope.result.asReturnedValue(); } -ReturnedValue Runtime::callActivationProperty(ExecutionEngine *engine, int nameIndex, CallData *callData) +ReturnedValue Runtime::method_callActivationProperty(ExecutionEngine *engine, int nameIndex, CallData *callData) { Q_ASSERT(callData->thisObject.isUndefined()); Scope scope(engine); @@ -974,36 +977,41 @@ ReturnedValue Runtime::callActivationProperty(ExecutionEngine *engine, int nameI } if (o->d() == scope.engine->evalFunction()->d() && name->equals(scope.engine->id_eval())) { - return static_cast<EvalFunction *>(o)->evalCall(callData, true); + static_cast<EvalFunction *>(o)->evalCall(scope, callData, true); + } else { + o->call(scope, callData); } - return o->call(callData); + return scope.result.asReturnedValue(); } -ReturnedValue Runtime::callQmlScopeObjectProperty(ExecutionEngine *engine, int propertyIndex, CallData *callData) +ReturnedValue Runtime::method_callQmlScopeObjectProperty(ExecutionEngine *engine, int propertyIndex, CallData *callData) { Scope scope(engine); - ScopedFunctionObject o(scope, getQmlScopeObjectProperty(engine, callData->thisObject, propertyIndex)); + ScopedFunctionObject o(scope, method_getQmlScopeObjectProperty(engine, callData->thisObject, propertyIndex, /*captureRequired*/true)); if (!o) { QString error = QStringLiteral("Property '%1' of scope object is not a function").arg(propertyIndex); return engine->throwTypeError(error); } - return o->call(callData); + + o->call(scope, callData); + return scope.result.asReturnedValue(); } -ReturnedValue Runtime::callQmlContextObjectProperty(ExecutionEngine *engine, int propertyIndex, CallData *callData) +ReturnedValue Runtime::method_callQmlContextObjectProperty(ExecutionEngine *engine, int propertyIndex, CallData *callData) { Scope scope(engine); - ScopedFunctionObject o(scope, getQmlContextObjectProperty(engine, callData->thisObject, propertyIndex)); + ScopedFunctionObject o(scope, method_getQmlContextObjectProperty(engine, callData->thisObject, propertyIndex, /*captureRequired*/true)); if (!o) { QString error = QStringLiteral("Property '%1' of context object is not a function").arg(propertyIndex); return engine->throwTypeError(error); } - return o->call(callData); + o->call(scope, callData); + return scope.result.asReturnedValue(); } -ReturnedValue Runtime::callProperty(ExecutionEngine *engine, int nameIndex, CallData *callData) +ReturnedValue Runtime::method_callProperty(ExecutionEngine *engine, int nameIndex, CallData *callData) { Scope scope(engine); ScopedString name(scope, engine->current->compilationUnit->runtimeStrings[nameIndex]); @@ -1022,26 +1030,31 @@ ReturnedValue Runtime::callProperty(ExecutionEngine *engine, int nameIndex, Call } ScopedFunctionObject o(scope, baseObject->get(name)); - if (!o) { + if (o) { + o->call(scope, callData); + return scope.result.asReturnedValue(); + } else { QString error = QStringLiteral("Property '%1' of object %2 is not a function").arg(name->toQString(), callData->thisObject.toQStringNoThrow()); return engine->throwTypeError(error); } - return o->call(callData); } -ReturnedValue Runtime::callPropertyLookup(ExecutionEngine *engine, uint index, CallData *callData) +ReturnedValue Runtime::method_callPropertyLookup(ExecutionEngine *engine, uint index, CallData *callData) { Lookup *l = engine->current->lookups + index; Value v; v = l->getter(l, engine, callData->thisObject); - if (!v.isObject()) + if (v.isObject()) { + Scope scope(engine); + v.objectValue()->call(scope, callData); + return scope.result.asReturnedValue(); + } else { return engine->throwTypeError(); - - return v.objectValue()->call(callData); + } } -ReturnedValue Runtime::callElement(ExecutionEngine *engine, const Value &index, CallData *callData) +ReturnedValue Runtime::method_callElement(ExecutionEngine *engine, const Value &index, CallData *callData) { Scope scope(engine); ScopedObject baseObject(scope, callData->thisObject.toObject(engine)); @@ -1055,33 +1068,38 @@ ReturnedValue Runtime::callElement(ExecutionEngine *engine, const Value &index, if (!o) return engine->throwTypeError(); - return o->call(callData); + o->call(scope, callData); + return scope.result.asReturnedValue(); } -ReturnedValue Runtime::callValue(ExecutionEngine *engine, const Value &func, CallData *callData) +ReturnedValue Runtime::method_callValue(ExecutionEngine *engine, const Value &func, CallData *callData) { if (!func.isObject()) return engine->throwTypeError(QStringLiteral("%1 is not a function").arg(func.toQStringNoThrow())); - return func.objectValue()->call(callData); + Scope scope(engine); + func.objectValue()->call(scope, callData); + return scope.result.asReturnedValue(); } -ReturnedValue Runtime::constructGlobalLookup(ExecutionEngine *engine, uint index, CallData *callData) +ReturnedValue Runtime::method_constructGlobalLookup(ExecutionEngine *engine, uint index, CallData *callData) { Scope scope(engine); Q_ASSERT(callData->thisObject.isUndefined()); Lookup *l = engine->current->lookups + index; ScopedObject f(scope, l->globalGetter(l, engine)); - if (!f) + if (f) { + f->construct(scope, callData); + return scope.result.asReturnedValue(); + } else { return engine->throwTypeError(); - - return f->construct(callData); + } } -ReturnedValue Runtime::constructActivationProperty(ExecutionEngine *engine, int nameIndex, CallData *callData) +ReturnedValue Runtime::method_constructActivationProperty(ExecutionEngine *engine, int nameIndex, CallData *callData) { Scope scope(engine); ScopedString name(scope, engine->current->compilationUnit->runtimeStrings[nameIndex]); @@ -1093,19 +1111,22 @@ ReturnedValue Runtime::constructActivationProperty(ExecutionEngine *engine, int if (!f) return engine->throwTypeError(); - return f->construct(callData); + f->construct(scope, callData); + return scope.result.asReturnedValue(); } -ReturnedValue Runtime::constructValue(ExecutionEngine *engine, const Value &func, CallData *callData) +ReturnedValue Runtime::method_constructValue(ExecutionEngine *engine, const Value &func, CallData *callData) { const Object *f = func.as<Object>(); if (!f) return engine->throwTypeError(); - return f->construct(callData); + Scope scope(engine); + f->construct(scope, callData); + return scope.result.asReturnedValue(); } -ReturnedValue Runtime::constructProperty(ExecutionEngine *engine, int nameIndex, CallData *callData) +ReturnedValue Runtime::method_constructProperty(ExecutionEngine *engine, int nameIndex, CallData *callData) { Scope scope(engine); ScopedObject thisObject(scope, callData->thisObject.toObject(engine)); @@ -1114,31 +1135,38 @@ ReturnedValue Runtime::constructProperty(ExecutionEngine *engine, int nameIndex, return Encode::undefined(); ScopedObject f(scope, thisObject->get(name)); - if (!f) + if (f) { + Scope scope(engine); + f->construct(scope, callData); + return scope.result.asReturnedValue(); + } else { return engine->throwTypeError(); - - return f->construct(callData); + } } -ReturnedValue Runtime::constructPropertyLookup(ExecutionEngine *engine, uint index, CallData *callData) +ReturnedValue Runtime::method_constructPropertyLookup(ExecutionEngine *engine, uint index, CallData *callData) { Lookup *l = engine->current->lookups + index; Value v; v = l->getter(l, engine, callData->thisObject); - if (!v.isObject()) + if (v.isObject()) { + Scope scope(engine); + ScopedValue result(scope); + v.objectValue()->construct(scope, callData); + return scope.result.asReturnedValue(); + } else { return engine->throwTypeError(); - - return v.objectValue()->construct(callData); + } } -void Runtime::throwException(ExecutionEngine *engine, const Value &value) +void Runtime::method_throwException(ExecutionEngine *engine, const Value &value) { if (!value.isEmpty()) engine->throwError(value); } -ReturnedValue Runtime::typeofValue(ExecutionEngine *engine, const Value &value) +ReturnedValue Runtime::method_typeofValue(ExecutionEngine *engine, const Value &value) { Scope scope(engine); ScopedString res(scope); @@ -1167,39 +1195,37 @@ ReturnedValue Runtime::typeofValue(ExecutionEngine *engine, const Value &value) return res.asReturnedValue(); } -QV4::ReturnedValue Runtime::typeofName(ExecutionEngine *engine, int nameIndex) +QV4::ReturnedValue Runtime::method_typeofName(ExecutionEngine *engine, int nameIndex) { Scope scope(engine); ScopedString name(scope, engine->current->compilationUnit->runtimeStrings[nameIndex]); ScopedValue prop(scope, engine->currentContext->getProperty(name)); // typeof doesn't throw. clear any possible exception scope.engine->hasException = false; - return Runtime::typeofValue(engine, prop); + return method_typeofValue(engine, prop); } #ifndef V4_BOOTSTRAP -ReturnedValue Runtime::typeofScopeObjectProperty(ExecutionEngine *engine, const Value &context, - int propertyIndex) +ReturnedValue Runtime::method_typeofScopeObjectProperty(ExecutionEngine *engine, const Value &context, int propertyIndex) { Scope scope(engine); - ScopedValue prop(scope, getQmlScopeObjectProperty(engine, context, propertyIndex)); + ScopedValue prop(scope, method_getQmlScopeObjectProperty(engine, context, propertyIndex, /*captureRequired*/true)); if (scope.engine->hasException) return Encode::undefined(); - return Runtime::typeofValue(engine, prop); + return method_typeofValue(engine, prop); } -ReturnedValue Runtime::typeofContextObjectProperty(ExecutionEngine *engine, const Value &context, - int propertyIndex) +ReturnedValue Runtime::method_typeofContextObjectProperty(ExecutionEngine *engine, const Value &context, int propertyIndex) { Scope scope(engine); - ScopedValue prop(scope, getQmlContextObjectProperty(engine, context, propertyIndex)); + ScopedValue prop(scope, method_getQmlContextObjectProperty(engine, context, propertyIndex, /*captureRequired*/true)); if (scope.engine->hasException) return Encode::undefined(); - return Runtime::typeofValue(engine, prop); + return method_typeofValue(engine, prop); } #endif // V4_BOOTSTRAP -QV4::ReturnedValue Runtime::typeofMember(ExecutionEngine *engine, const Value &base, int nameIndex) +QV4::ReturnedValue Runtime::method_typeofMember(ExecutionEngine *engine, const Value &base, int nameIndex) { Scope scope(engine); ScopedString name(scope, engine->current->compilationUnit->runtimeStrings[nameIndex]); @@ -1207,10 +1233,10 @@ QV4::ReturnedValue Runtime::typeofMember(ExecutionEngine *engine, const Value &b if (scope.engine->hasException) return Encode::undefined(); ScopedValue prop(scope, obj->get(name)); - return Runtime::typeofValue(engine, prop); + return method_typeofValue(engine, prop); } -QV4::ReturnedValue Runtime::typeofElement(ExecutionEngine *engine, const Value &base, const Value &index) +QV4::ReturnedValue Runtime::method_typeofElement(ExecutionEngine *engine, const Value &base, const Value &index) { Scope scope(engine); ScopedString name(scope, index.toString(engine)); @@ -1218,10 +1244,10 @@ QV4::ReturnedValue Runtime::typeofElement(ExecutionEngine *engine, const Value & if (scope.engine->hasException) return Encode::undefined(); ScopedValue prop(scope, obj->get(name)); - return Runtime::typeofValue(engine, prop); + return method_typeofValue(engine, prop); } -ReturnedValue Runtime::unwindException(ExecutionEngine *engine) +ReturnedValue Runtime::method_unwindException(ExecutionEngine *engine) { if (!engine->hasException) return Primitive::emptyValue().asReturnedValue(); @@ -1233,39 +1259,39 @@ ReturnedValue Runtime::unwindException(ExecutionEngine *engine) * * Instead the push/pop pair acts as a non local scope. */ -void Runtime::pushWithScope(const Value &o, ExecutionEngine *engine) +void Runtime::method_pushWithScope(const Value &o, ExecutionEngine *engine) { engine->pushContext(engine->currentContext->newWithContext(o.toObject(engine))); Q_ASSERT(engine->jsStackTop == engine->currentContext + 2); } -void Runtime::pushCatchScope(NoThrowEngine *engine, int exceptionVarNameIndex) +void Runtime::method_pushCatchScope(NoThrowEngine *engine, int exceptionVarNameIndex) { ExecutionContext *c = engine->currentContext; engine->pushContext(c->newCatchContext(c->d()->compilationUnit->runtimeStrings[exceptionVarNameIndex], engine->catchException(0))); Q_ASSERT(engine->jsStackTop == engine->currentContext + 2); } -void Runtime::popScope(ExecutionEngine *engine) +void Runtime::method_popScope(ExecutionEngine *engine) { Q_ASSERT(engine->jsStackTop == engine->currentContext + 2); engine->popContext(); engine->jsStackTop -= 2; } -void Runtime::declareVar(ExecutionEngine *engine, bool deletable, int nameIndex) +void Runtime::method_declareVar(ExecutionEngine *engine, bool deletable, int nameIndex) { Scope scope(engine); ScopedString name(scope, engine->current->compilationUnit->runtimeStrings[nameIndex]); engine->currentContext->createMutableBinding(name, deletable); } -ReturnedValue Runtime::arrayLiteral(ExecutionEngine *engine, Value *values, uint length) +ReturnedValue Runtime::method_arrayLiteral(ExecutionEngine *engine, Value *values, uint length) { return engine->newArrayObject(values, length)->asReturnedValue(); } -ReturnedValue Runtime::objectLiteral(ExecutionEngine *engine, const QV4::Value *args, int classId, int arrayValueCount, int arrayGetterSetterCountAndFlags) +ReturnedValue Runtime::method_objectLiteral(ExecutionEngine *engine, const QV4::Value *args, int classId, int arrayValueCount, int arrayGetterSetterCountAndFlags) { Scope scope(engine); QV4::InternalClass *klass = engine->current->compilationUnit->runtimeClasses[classId]; @@ -1307,7 +1333,7 @@ ReturnedValue Runtime::objectLiteral(ExecutionEngine *engine, const QV4::Value * return o.asReturnedValue(); } -QV4::ReturnedValue Runtime::setupArgumentsObject(ExecutionEngine *engine) +QV4::ReturnedValue Runtime::method_setupArgumentsObject(ExecutionEngine *engine) { Q_ASSERT(engine->current->type == Heap::ExecutionContext::Type_CallContext); QV4::CallContext *c = static_cast<QV4::CallContext *>(engine->currentContext); @@ -1317,7 +1343,7 @@ QV4::ReturnedValue Runtime::setupArgumentsObject(ExecutionEngine *engine) #endif // V4_BOOTSTRAP -QV4::ReturnedValue Runtime::increment(const Value &value) +QV4::ReturnedValue Runtime::method_increment(const Value &value) { TRACE1(value); @@ -1329,7 +1355,7 @@ QV4::ReturnedValue Runtime::increment(const Value &value) } } -QV4::ReturnedValue Runtime::decrement(const Value &value) +QV4::ReturnedValue Runtime::method_decrement(const Value &value) { TRACE1(value); @@ -1364,31 +1390,31 @@ QV4::ReturnedValue RuntimeHelpers::toObject(ExecutionEngine *engine, const Value #endif // V4_BOOTSTRAP -ReturnedValue Runtime::toDouble(const Value &value) +ReturnedValue Runtime::method_toDouble(const Value &value) { TRACE1(value); return Encode(value.toNumber()); } -int Runtime::toInt(const Value &value) +int Runtime::method_toInt(const Value &value) { TRACE1(value); return value.toInt32(); } -int Runtime::doubleToInt(const double &d) +int Runtime::method_doubleToInt(const double &d) { TRACE0(); return Primitive::toInt32(d); } -unsigned Runtime::toUInt(const Value &value) +unsigned Runtime::method_toUInt(const Value &value) { TRACE1(value); return value.toUInt32(); } -unsigned Runtime::doubleToUInt(const double &d) +unsigned Runtime::method_doubleToUInt(const double &d) { TRACE0(); return Primitive::toUInt32(d); @@ -1396,17 +1422,17 @@ unsigned Runtime::doubleToUInt(const double &d) #ifndef V4_BOOTSTRAP -ReturnedValue Runtime::getQmlContext(NoThrowEngine *engine) +ReturnedValue Runtime::method_getQmlContext(NoThrowEngine *engine) { return engine->qmlContext()->asReturnedValue(); } -ReturnedValue Runtime::regexpLiteral(ExecutionEngine *engine, int id) +ReturnedValue Runtime::method_regexpLiteral(ExecutionEngine *engine, int id) { return engine->current->compilationUnit->runtimeRegularExpressions[id].asReturnedValue(); } -ReturnedValue Runtime::getQmlQObjectProperty(ExecutionEngine *engine, const Value &object, int propertyIndex, bool captureRequired) +ReturnedValue Runtime::method_getQmlQObjectProperty(ExecutionEngine *engine, const Value &object, int propertyIndex, bool captureRequired) { Scope scope(engine); QV4::Scoped<QObjectWrapper> wrapper(scope, object); @@ -1417,7 +1443,7 @@ ReturnedValue Runtime::getQmlQObjectProperty(ExecutionEngine *engine, const Valu return QV4::QObjectWrapper::getProperty(scope.engine, wrapper->object(), propertyIndex, captureRequired); } -QV4::ReturnedValue Runtime::getQmlAttachedProperty(ExecutionEngine *engine, int attachedPropertiesId, int propertyIndex) +QV4::ReturnedValue Runtime::method_getQmlAttachedProperty(ExecutionEngine *engine, int attachedPropertiesId, int propertyIndex) { QObject *scopeObject = engine->qmlScopeObject(); QObject *attachedObject = qmlAttachedPropertiesObjectById(attachedPropertiesId, scopeObject); @@ -1427,19 +1453,19 @@ QV4::ReturnedValue Runtime::getQmlAttachedProperty(ExecutionEngine *engine, int return QV4::QObjectWrapper::getProperty(engine, attachedObject, propertyIndex, /*captureRequired*/true); } -ReturnedValue Runtime::getQmlScopeObjectProperty(ExecutionEngine *engine, const Value &context, int propertyIndex) +ReturnedValue Runtime::method_getQmlScopeObjectProperty(ExecutionEngine *engine, const Value &context, int propertyIndex, bool captureRequired) { const QmlContext &c = static_cast<const QmlContext &>(context); - return QV4::QObjectWrapper::getProperty(engine, c.d()->qml->scopeObject, propertyIndex, false); + return QV4::QObjectWrapper::getProperty(engine, c.d()->qml->scopeObject, propertyIndex, captureRequired); } -ReturnedValue Runtime::getQmlContextObjectProperty(ExecutionEngine *engine, const Value &context, int propertyIndex) +ReturnedValue Runtime::method_getQmlContextObjectProperty(ExecutionEngine *engine, const Value &context, int propertyIndex, bool captureRequired) { const QmlContext &c = static_cast<const QmlContext &>(context); - return QV4::QObjectWrapper::getProperty(engine, c.d()->qml->context->contextObject, propertyIndex, false); + return QV4::QObjectWrapper::getProperty(engine, (*c.d()->qml->context)->contextObject, propertyIndex, captureRequired); } -ReturnedValue Runtime::getQmlSingletonQObjectProperty(ExecutionEngine *engine, const Value &object, int propertyIndex, bool captureRequired) +ReturnedValue Runtime::method_getQmlSingletonQObjectProperty(ExecutionEngine *engine, const Value &object, int propertyIndex, bool captureRequired) { Scope scope(engine); QV4::Scoped<QmlTypeWrapper> wrapper(scope, object); @@ -1450,11 +1476,11 @@ ReturnedValue Runtime::getQmlSingletonQObjectProperty(ExecutionEngine *engine, c return QV4::QObjectWrapper::getProperty(scope.engine, wrapper->singletonObject(), propertyIndex, captureRequired); } -ReturnedValue Runtime::getQmlIdObject(ExecutionEngine *engine, const Value &c, uint index) +ReturnedValue Runtime::method_getQmlIdObject(ExecutionEngine *engine, const Value &c, uint index) { Scope scope(engine); const QmlContext &qmlContext = static_cast<const QmlContext &>(c); - QQmlContextData *context = qmlContext.d()->qml->context; + QQmlContextData *context = *qmlContext.d()->qml->context; if (!context || index >= (uint)context->idValueCount) return Encode::undefined(); @@ -1465,19 +1491,19 @@ ReturnedValue Runtime::getQmlIdObject(ExecutionEngine *engine, const Value &c, u return QObjectWrapper::wrap(engine, context->idValues[index].data()); } -void Runtime::setQmlScopeObjectProperty(ExecutionEngine *engine, const Value &context, int propertyIndex, const Value &value) +void Runtime::method_setQmlScopeObjectProperty(ExecutionEngine *engine, const Value &context, int propertyIndex, const Value &value) { const QmlContext &c = static_cast<const QmlContext &>(context); return QV4::QObjectWrapper::setProperty(engine, c.d()->qml->scopeObject, propertyIndex, value); } -void Runtime::setQmlContextObjectProperty(ExecutionEngine *engine, const Value &context, int propertyIndex, const Value &value) +void Runtime::method_setQmlContextObjectProperty(ExecutionEngine *engine, const Value &context, int propertyIndex, const Value &value) { const QmlContext &c = static_cast<const QmlContext &>(context); - return QV4::QObjectWrapper::setProperty(engine, c.d()->qml->context->contextObject, propertyIndex, value); + return QV4::QObjectWrapper::setProperty(engine, (*c.d()->qml->context)->contextObject, propertyIndex, value); } -void Runtime::setQmlQObjectProperty(ExecutionEngine *engine, const Value &object, int propertyIndex, const Value &value) +void Runtime::method_setQmlQObjectProperty(ExecutionEngine *engine, const Value &object, int propertyIndex, const Value &value) { Scope scope(engine); QV4::Scoped<QObjectWrapper> wrapper(scope, object); @@ -1488,7 +1514,7 @@ void Runtime::setQmlQObjectProperty(ExecutionEngine *engine, const Value &object wrapper->setProperty(engine, propertyIndex, value); } -ReturnedValue Runtime::getQmlImportedScripts(NoThrowEngine *engine) +ReturnedValue Runtime::method_getQmlImportedScripts(NoThrowEngine *engine) { QQmlContextData *context = engine->callingQmlContext(); if (!context) @@ -1496,14 +1522,14 @@ ReturnedValue Runtime::getQmlImportedScripts(NoThrowEngine *engine) return context->importedScripts.value(); } -QV4::ReturnedValue Runtime::getQmlSingleton(QV4::NoThrowEngine *engine, int nameIndex) +QV4::ReturnedValue Runtime::method_getQmlSingleton(QV4::NoThrowEngine *engine, int nameIndex) { Scope scope(engine); ScopedString name(scope, engine->current->compilationUnit->runtimeStrings[nameIndex]); return engine->qmlSingletonWrapper(name); } -void Runtime::convertThisToObject(ExecutionEngine *engine) +void Runtime::method_convertThisToObject(ExecutionEngine *engine) { Value *t = &engine->current->callData->thisObject; if (t->isObject()) @@ -1515,8 +1541,291 @@ void Runtime::convertThisToObject(ExecutionEngine *engine) } } +ReturnedValue Runtime::method_uPlus(const Value &value) +{ + TRACE1(value); + + if (value.isNumber()) + return value.asReturnedValue(); + if (value.integerCompatible()) + return Encode(value.int_32()); + + double n = value.toNumberImpl(); + return Encode(n); +} + +ReturnedValue Runtime::method_uMinus(const Value &value) +{ + TRACE1(value); + + // +0 != -0, so we need to convert to double when negating 0 + if (value.isInteger() && value.integerValue()) + return Encode(-value.integerValue()); + else { + double n = RuntimeHelpers::toNumber(value); + return Encode(-n); + } +} + +ReturnedValue Runtime::method_complement(const Value &value) +{ + TRACE1(value); + + int n = value.toInt32(); + return Encode((int)~n); +} + +ReturnedValue Runtime::method_uNot(const Value &value) +{ + TRACE1(value); + + bool b = value.toBoolean(); + return Encode(!b); +} + +// binary operators +ReturnedValue Runtime::method_bitOr(const Value &left, const Value &right) +{ + TRACE2(left, right); + + int lval = left.toInt32(); + int rval = right.toInt32(); + return Encode(lval | rval); +} + +ReturnedValue Runtime::method_bitXor(const Value &left, const Value &right) +{ + TRACE2(left, right); + + int lval = left.toInt32(); + int rval = right.toInt32(); + return Encode(lval ^ rval); +} + +ReturnedValue Runtime::method_bitAnd(const Value &left, const Value &right) +{ + TRACE2(left, right); + + int lval = left.toInt32(); + int rval = right.toInt32(); + return Encode(lval & rval); +} + +#ifndef V4_BOOTSTRAP +ReturnedValue Runtime::method_add(ExecutionEngine *engine, const Value &left, const Value &right) +{ + TRACE2(left, right); + + if (Q_LIKELY(left.isInteger() && right.isInteger())) + return add_int32(left.integerValue(), right.integerValue()); + if (left.isNumber() && right.isNumber()) + return Primitive::fromDouble(left.asDouble() + right.asDouble()).asReturnedValue(); + + return RuntimeHelpers::addHelper(engine, left, right); +} +#endif // V4_BOOTSTRAP + +ReturnedValue Runtime::method_sub(const Value &left, const Value &right) +{ + TRACE2(left, right); + + if (Q_LIKELY(left.isInteger() && right.isInteger())) + return sub_int32(left.integerValue(), right.integerValue()); + + double lval = left.isNumber() ? left.asDouble() : left.toNumberImpl(); + double rval = right.isNumber() ? right.asDouble() : right.toNumberImpl(); + + return Primitive::fromDouble(lval - rval).asReturnedValue(); +} + +ReturnedValue Runtime::method_mul(const Value &left, const Value &right) +{ + TRACE2(left, right); + + if (Q_LIKELY(left.isInteger() && right.isInteger())) + return mul_int32(left.integerValue(), right.integerValue()); + + double lval = left.isNumber() ? left.asDouble() : left.toNumberImpl(); + double rval = right.isNumber() ? right.asDouble() : right.toNumberImpl(); + + return Primitive::fromDouble(lval * rval).asReturnedValue(); +} + +ReturnedValue Runtime::method_div(const Value &left, const Value &right) +{ + TRACE2(left, right); + + if (Value::integerCompatible(left, right)) { + int lval = left.integerValue(); + int rval = right.integerValue(); + if (rval != 0 && (lval % rval == 0)) + return Encode(int(lval / rval)); + else + return Encode(double(lval) / rval); + } + + double lval = left.toNumber(); + double rval = right.toNumber(); + return Primitive::fromDouble(lval / rval).asReturnedValue(); +} + +ReturnedValue Runtime::method_mod(const Value &left, const Value &right) +{ + TRACE2(left, right); + + if (Value::integerCompatible(left, right) && right.integerValue() != 0) { + int intRes = left.integerValue() % right.integerValue(); + if (intRes != 0 || left.integerValue() >= 0) + return Encode(intRes); + } + + double lval = RuntimeHelpers::toNumber(left); + double rval = RuntimeHelpers::toNumber(right); +#ifdef fmod +# undef fmod +#endif + return Primitive::fromDouble(std::fmod(lval, rval)).asReturnedValue(); +} + +ReturnedValue Runtime::method_shl(const Value &left, const Value &right) +{ + TRACE2(left, right); + + int lval = left.toInt32(); + int rval = right.toInt32() & 0x1f; + return Encode((int)(lval << rval)); +} + +ReturnedValue Runtime::method_shr(const Value &left, const Value &right) +{ + TRACE2(left, right); + + int lval = left.toInt32(); + unsigned rval = right.toUInt32() & 0x1f; + return Encode((int)(lval >> rval)); +} + +ReturnedValue Runtime::method_ushr(const Value &left, const Value &right) +{ + TRACE2(left, right); + + unsigned lval = left.toUInt32(); + unsigned rval = right.toUInt32() & 0x1f; + uint res = lval >> rval; + + return Encode(res); +} + #endif // V4_BOOTSTRAP +ReturnedValue Runtime::method_greaterThan(const Value &left, const Value &right) +{ + TRACE2(left, right); + + bool r = method_compareGreaterThan(left, right); + return Encode(r); +} + +ReturnedValue Runtime::method_lessThan(const Value &left, const Value &right) +{ + TRACE2(left, right); + + bool r = method_compareLessThan(left, right); + return Encode(r); +} + +ReturnedValue Runtime::method_greaterEqual(const Value &left, const Value &right) +{ + TRACE2(left, right); + + bool r = method_compareGreaterEqual(left, right); + return Encode(r); +} + +ReturnedValue Runtime::method_lessEqual(const Value &left, const Value &right) +{ + TRACE2(left, right); + + bool r = method_compareLessEqual(left, right); + return Encode(r); +} + +Bool Runtime::method_compareEqual(const Value &left, const Value &right) +{ + TRACE2(left, right); + + if (left.rawValue() == right.rawValue()) + // NaN != NaN + return !left.isNaN(); + + if (left.type() == right.type()) { + if (!left.isManaged()) + return false; + if (left.isString() == right.isString()) + return left.cast<Managed>()->isEqualTo(right.cast<Managed>()); + } + + return RuntimeHelpers::equalHelper(left, right); +} + +ReturnedValue Runtime::method_equal(const Value &left, const Value &right) +{ + TRACE2(left, right); + + bool r = method_compareEqual(left, right); + return Encode(r); +} + +ReturnedValue Runtime::method_notEqual(const Value &left, const Value &right) +{ + TRACE2(left, right); + + bool r = !method_compareEqual(left, right); + return Encode(r); +} + +ReturnedValue Runtime::method_strictEqual(const Value &left, const Value &right) +{ + TRACE2(left, right); + + bool r = RuntimeHelpers::strictEqual(left, right); + return Encode(r); +} + +ReturnedValue Runtime::method_strictNotEqual(const Value &left, const Value &right) +{ + TRACE2(left, right); + + bool r = ! RuntimeHelpers::strictEqual(left, right); + return Encode(r); +} + +Bool Runtime::method_compareNotEqual(const Value &left, const Value &right) +{ + TRACE2(left, right); + + return !Runtime::method_compareEqual(left, right); +} + +Bool Runtime::method_compareStrictEqual(const Value &left, const Value &right) +{ + TRACE2(left, right); + + return RuntimeHelpers::strictEqual(left, right); +} + +Bool Runtime::method_compareStrictNotEqual(const Value &left, const Value &right) +{ + TRACE2(left, right); + + return ! RuntimeHelpers::strictEqual(left, right); +} + +Bool Runtime::method_toBoolean(const Value &value) +{ + return value.toBoolean(); +} + } // namespace QV4 QT_END_NAMESPACE diff --git a/src/qml/jsruntime/qv4runtime_p.h b/src/qml/jsruntime/qv4runtime_p.h index b63777e164..a32b3f1663 100644 --- a/src/qml/jsruntime/qv4runtime_p.h +++ b/src/qml/jsruntime/qv4runtime_p.h @@ -55,14 +55,11 @@ #include "qv4context_p.h" #include "qv4engine_p.h" #include "qv4math_p.h" - +#include "qv4runtimeapi_p.h" #include <QtCore/qnumeric.h> - QT_BEGIN_NAMESPACE -class QQmlAccessors; - #undef QV4_COUNT_RUNTIME_FUNCTIONS namespace QV4 { @@ -100,156 +97,6 @@ enum TypeHint { STRING_HINT }; -// This is a trick to tell the code generators that functions taking a NoThrowContext won't -// throw exceptions and therefore don't need a check after the call. - -#ifndef V4_BOOTSTRAP -struct NoThrowEngine : public ExecutionEngine -{ -}; -#else -struct NoThrowEngine; -#endif - -struct Q_QML_PRIVATE_EXPORT Runtime { - // call - static ReturnedValue callGlobalLookup(ExecutionEngine *engine, uint index, CallData *callData); - static ReturnedValue callActivationProperty(ExecutionEngine *engine, int nameIndex, CallData *callData); - static ReturnedValue callQmlScopeObjectProperty(ExecutionEngine *engine, int propertyIndex, CallData *callData); - static ReturnedValue callQmlContextObjectProperty(ExecutionEngine *engine, int propertyIndex, CallData *callData); - static ReturnedValue callProperty(ExecutionEngine *engine, int nameIndex, CallData *callData); - static ReturnedValue callPropertyLookup(ExecutionEngine *engine, uint index, CallData *callData); - static ReturnedValue callElement(ExecutionEngine *engine, const Value &index, CallData *callData); - static ReturnedValue callValue(ExecutionEngine *engine, const Value &func, CallData *callData); - - // construct - static ReturnedValue constructGlobalLookup(ExecutionEngine *engine, uint index, CallData *callData); - static ReturnedValue constructActivationProperty(ExecutionEngine *engine, int nameIndex, CallData *callData); - static ReturnedValue constructProperty(ExecutionEngine *engine, int nameIndex, CallData *callData); - static ReturnedValue constructPropertyLookup(ExecutionEngine *engine, uint index, CallData *callData); - static ReturnedValue constructValue(ExecutionEngine *engine, const Value &func, CallData *callData); - - // set & get - static void setActivationProperty(ExecutionEngine *engine, int nameIndex, const Value &value); - static void setProperty(ExecutionEngine *engine, const Value &object, int nameIndex, const Value &value); - static void setElement(ExecutionEngine *engine, const Value &object, const Value &index, const Value &value); - static ReturnedValue getProperty(ExecutionEngine *engine, const Value &object, int nameIndex); - static ReturnedValue getActivationProperty(ExecutionEngine *engine, int nameIndex); - static ReturnedValue getElement(ExecutionEngine *engine, const Value &object, const Value &index); - - // typeof - static ReturnedValue typeofValue(ExecutionEngine *engine, const Value &val); - static ReturnedValue typeofName(ExecutionEngine *engine, int nameIndex); - static ReturnedValue typeofScopeObjectProperty(ExecutionEngine *engine, const Value &context, int propertyIndex); - static ReturnedValue typeofContextObjectProperty(ExecutionEngine *engine, const Value &context, int propertyIndex); - static ReturnedValue typeofMember(ExecutionEngine *engine, const Value &base, int nameIndex); - static ReturnedValue typeofElement(ExecutionEngine *engine, const Value &base, const Value &index); - - // delete - static ReturnedValue deleteElement(ExecutionEngine *engine, const Value &base, const Value &index); - static ReturnedValue deleteMember(ExecutionEngine *engine, const Value &base, int nameIndex); - static ReturnedValue deleteMemberString(ExecutionEngine *engine, const Value &base, String *name); - static ReturnedValue deleteName(ExecutionEngine *engine, int nameIndex); - - // exceptions & scopes - static void throwException(ExecutionEngine *engine, const Value &value); - static ReturnedValue unwindException(ExecutionEngine *engine); - static void pushWithScope(const Value &o, ExecutionEngine *engine); - static void pushCatchScope(NoThrowEngine *engine, int exceptionVarNameIndex); - static void popScope(ExecutionEngine *engine); - - // closures - static ReturnedValue closure(ExecutionEngine *engine, int functionId); - - // function header - static void declareVar(ExecutionEngine *engine, bool deletable, int nameIndex); - static ReturnedValue setupArgumentsObject(ExecutionEngine *engine); - static void convertThisToObject(ExecutionEngine *engine); - - // literals - static ReturnedValue arrayLiteral(ExecutionEngine *engine, Value *values, uint length); - static ReturnedValue objectLiteral(ExecutionEngine *engine, const Value *args, int classId, int arrayValueCount, int arrayGetterSetterCountAndFlags); - static ReturnedValue regexpLiteral(ExecutionEngine *engine, int id); - - // foreach - static ReturnedValue foreachIterator(ExecutionEngine *engine, const Value &in); - static ReturnedValue foreachNextPropertyName(const Value &foreach_iterator); - - // unary operators - typedef ReturnedValue (*UnaryOperation)(const Value &value); - static ReturnedValue uPlus(const Value &value); - static ReturnedValue uMinus(const Value &value); - static ReturnedValue uNot(const Value &value); - static ReturnedValue complement(const Value &value); - static ReturnedValue increment(const Value &value); - static ReturnedValue decrement(const Value &value); - - // binary operators - typedef ReturnedValue (*BinaryOperation)(const Value &left, const Value &right); - typedef ReturnedValue (*BinaryOperationContext)(ExecutionEngine *engine, const Value &left, const Value &right); - - static ReturnedValue instanceof(ExecutionEngine *engine, const Value &left, const Value &right); - static ReturnedValue in(ExecutionEngine *engine, const Value &left, const Value &right); - static ReturnedValue add(ExecutionEngine *engine, const Value &left, const Value &right); - static ReturnedValue addString(ExecutionEngine *engine, const Value &left, const Value &right); - static ReturnedValue bitOr(const Value &left, const Value &right); - static ReturnedValue bitXor(const Value &left, const Value &right); - static ReturnedValue bitAnd(const Value &left, const Value &right); - static ReturnedValue sub(const Value &left, const Value &right); - static ReturnedValue mul(const Value &left, const Value &right); - static ReturnedValue div(const Value &left, const Value &right); - static ReturnedValue mod(const Value &left, const Value &right); - static ReturnedValue shl(const Value &left, const Value &right); - static ReturnedValue shr(const Value &left, const Value &right); - static ReturnedValue ushr(const Value &left, const Value &right); - static ReturnedValue greaterThan(const Value &left, const Value &right); - static ReturnedValue lessThan(const Value &left, const Value &right); - static ReturnedValue greaterEqual(const Value &left, const Value &right); - static ReturnedValue lessEqual(const Value &left, const Value &right); - static ReturnedValue equal(const Value &left, const Value &right); - static ReturnedValue notEqual(const Value &left, const Value &right); - static ReturnedValue strictEqual(const Value &left, const Value &right); - static ReturnedValue strictNotEqual(const Value &left, const Value &right); - - // comparisons - typedef Bool (*CompareOperation)(const Value &left, const Value &right); - static Bool compareGreaterThan(const Value &l, const Value &r); - static Bool compareLessThan(const Value &l, const Value &r); - static Bool compareGreaterEqual(const Value &l, const Value &r); - static Bool compareLessEqual(const Value &l, const Value &r); - static Bool compareEqual(const Value &left, const Value &right); - static Bool compareNotEqual(const Value &left, const Value &right); - static Bool compareStrictEqual(const Value &left, const Value &right); - static Bool compareStrictNotEqual(const Value &left, const Value &right); - - typedef Bool (*CompareOperationContext)(ExecutionEngine *engine, const Value &left, const Value &right); - static Bool compareInstanceof(ExecutionEngine *engine, const Value &left, const Value &right); - static Bool compareIn(ExecutionEngine *engine, const Value &left, const Value &right); - - // conversions - static Bool toBoolean(const Value &value); - static ReturnedValue toDouble(const Value &value); - static int toInt(const Value &value); - static int doubleToInt(const double &d); - static unsigned toUInt(const Value &value); - static unsigned doubleToUInt(const double &d); - - // qml - static ReturnedValue getQmlContext(NoThrowEngine *engine); - static ReturnedValue getQmlImportedScripts(NoThrowEngine *engine); - static ReturnedValue getQmlSingleton(NoThrowEngine *engine, int nameIndex); - static ReturnedValue getQmlAttachedProperty(ExecutionEngine *engine, int attachedPropertiesId, int propertyIndex); - static ReturnedValue getQmlScopeObjectProperty(ExecutionEngine *engine, const Value &context, int propertyIndex); - static ReturnedValue getQmlContextObjectProperty(ExecutionEngine *engine, const Value &context, int propertyIndex); - static ReturnedValue getQmlQObjectProperty(ExecutionEngine *engine, const Value &object, int propertyIndex, bool captureRequired); - static ReturnedValue getQmlSingletonQObjectProperty(ExecutionEngine *engine, const Value &object, int propertyIndex, bool captureRequired); - static ReturnedValue getQmlIdObject(ExecutionEngine *engine, const Value &context, uint index); - - static void setQmlScopeObjectProperty(ExecutionEngine *engine, const Value &context, int propertyIndex, const Value &value); - static void setQmlContextObjectProperty(ExecutionEngine *engine, const Value &context, int propertyIndex, const Value &value); - static void setQmlQObjectProperty(ExecutionEngine *engine, const Value &object, int propertyIndex, const Value &value); -}; - struct Q_QML_PRIVATE_EXPORT RuntimeHelpers { static ReturnedValue objectDefaultValue(const Object *object, int typeHint); static ReturnedValue toPrimitive(const Value &value, int typeHint); @@ -287,287 +134,6 @@ inline double RuntimeHelpers::toNumber(const Value &value) { return value.toNumber(); } - -inline ReturnedValue Runtime::uPlus(const Value &value) -{ - TRACE1(value); - - if (value.isNumber()) - return value.asReturnedValue(); - if (value.integerCompatible()) - return Encode(value.int_32()); - - double n = value.toNumberImpl(); - return Encode(n); -} - -inline ReturnedValue Runtime::uMinus(const Value &value) -{ - TRACE1(value); - - // +0 != -0, so we need to convert to double when negating 0 - if (value.isInteger() && value.integerValue()) - return Encode(-value.integerValue()); - else { - double n = RuntimeHelpers::toNumber(value); - return Encode(-n); - } -} - -inline ReturnedValue Runtime::complement(const Value &value) -{ - TRACE1(value); - - int n = value.toInt32(); - return Encode((int)~n); -} - -inline ReturnedValue Runtime::uNot(const Value &value) -{ - TRACE1(value); - - bool b = value.toBoolean(); - return Encode(!b); -} - -// binary operators -inline ReturnedValue Runtime::bitOr(const Value &left, const Value &right) -{ - TRACE2(left, right); - - int lval = left.toInt32(); - int rval = right.toInt32(); - return Encode(lval | rval); -} - -inline ReturnedValue Runtime::bitXor(const Value &left, const Value &right) -{ - TRACE2(left, right); - - int lval = left.toInt32(); - int rval = right.toInt32(); - return Encode(lval ^ rval); -} - -inline ReturnedValue Runtime::bitAnd(const Value &left, const Value &right) -{ - TRACE2(left, right); - - int lval = left.toInt32(); - int rval = right.toInt32(); - return Encode(lval & rval); -} - -#ifndef V4_BOOTSTRAP -inline ReturnedValue Runtime::add(ExecutionEngine *engine, const Value &left, const Value &right) -{ - TRACE2(left, right); - - if (Q_LIKELY(left.isInteger() && right.isInteger())) - return add_int32(left.integerValue(), right.integerValue()); - if (left.isNumber() && right.isNumber()) - return Primitive::fromDouble(left.asDouble() + right.asDouble()).asReturnedValue(); - - return RuntimeHelpers::addHelper(engine, left, right); -} -#endif // V4_BOOTSTRAP - -inline ReturnedValue Runtime::sub(const Value &left, const Value &right) -{ - TRACE2(left, right); - - if (Q_LIKELY(left.isInteger() && right.isInteger())) - return sub_int32(left.integerValue(), right.integerValue()); - - double lval = left.isNumber() ? left.asDouble() : left.toNumberImpl(); - double rval = right.isNumber() ? right.asDouble() : right.toNumberImpl(); - - return Primitive::fromDouble(lval - rval).asReturnedValue(); -} - -inline ReturnedValue Runtime::mul(const Value &left, const Value &right) -{ - TRACE2(left, right); - - if (Q_LIKELY(left.isInteger() && right.isInteger())) - return mul_int32(left.integerValue(), right.integerValue()); - - double lval = left.isNumber() ? left.asDouble() : left.toNumberImpl(); - double rval = right.isNumber() ? right.asDouble() : right.toNumberImpl(); - - return Primitive::fromDouble(lval * rval).asReturnedValue(); -} - -inline ReturnedValue Runtime::div(const Value &left, const Value &right) -{ - TRACE2(left, right); - - if (Value::integerCompatible(left, right)) { - int lval = left.integerValue(); - int rval = right.integerValue(); - if (rval != 0 && (lval % rval == 0)) - return Encode(int(lval / rval)); - else - return Encode(double(lval) / rval); - } - - double lval = left.toNumber(); - double rval = right.toNumber(); - return Primitive::fromDouble(lval / rval).asReturnedValue(); -} - -inline ReturnedValue Runtime::mod(const Value &left, const Value &right) -{ - TRACE2(left, right); - - if (Value::integerCompatible(left, right) && right.integerValue() != 0) { - int intRes = left.integerValue() % right.integerValue(); - if (intRes != 0 || left.integerValue() >= 0) - return Encode(intRes); - } - - double lval = RuntimeHelpers::toNumber(left); - double rval = RuntimeHelpers::toNumber(right); - return Primitive::fromDouble(std::fmod(lval, rval)).asReturnedValue(); -} - -inline ReturnedValue Runtime::shl(const Value &left, const Value &right) -{ - TRACE2(left, right); - - int lval = left.toInt32(); - int rval = right.toInt32() & 0x1f; - return Encode((int)(lval << rval)); -} - -inline ReturnedValue Runtime::shr(const Value &left, const Value &right) -{ - TRACE2(left, right); - - int lval = left.toInt32(); - unsigned rval = right.toUInt32() & 0x1f; - return Encode((int)(lval >> rval)); -} - -inline ReturnedValue Runtime::ushr(const Value &left, const Value &right) -{ - TRACE2(left, right); - - unsigned lval = left.toUInt32(); - unsigned rval = right.toUInt32() & 0x1f; - uint res = lval >> rval; - - return Encode(res); -} - -inline ReturnedValue Runtime::greaterThan(const Value &left, const Value &right) -{ - TRACE2(left, right); - - bool r = Runtime::compareGreaterThan(left, right); - return Encode(r); -} - -inline ReturnedValue Runtime::lessThan(const Value &left, const Value &right) -{ - TRACE2(left, right); - - bool r = Runtime::compareLessThan(left, right); - return Encode(r); -} - -inline ReturnedValue Runtime::greaterEqual(const Value &left, const Value &right) -{ - TRACE2(left, right); - - bool r = Runtime::compareGreaterEqual(left, right); - return Encode(r); -} - -inline ReturnedValue Runtime::lessEqual(const Value &left, const Value &right) -{ - TRACE2(left, right); - - bool r = Runtime::compareLessEqual(left, right); - return Encode(r); -} - -inline Bool Runtime::compareEqual(const Value &left, const Value &right) -{ - TRACE2(left, right); - - if (left.rawValue() == right.rawValue()) - // NaN != NaN - return !left.isNaN(); - - if (left.type() == right.type()) { - if (!left.isManaged()) - return false; - if (left.isString() == right.isString()) - return left.cast<Managed>()->isEqualTo(right.cast<Managed>()); - } - - return RuntimeHelpers::equalHelper(left, right); -} - -inline ReturnedValue Runtime::equal(const Value &left, const Value &right) -{ - TRACE2(left, right); - - bool r = Runtime::compareEqual(left, right); - return Encode(r); -} - -inline ReturnedValue Runtime::notEqual(const Value &left, const Value &right) -{ - TRACE2(left, right); - - bool r = !Runtime::compareEqual(left, right); - return Encode(r); -} - -inline ReturnedValue Runtime::strictEqual(const Value &left, const Value &right) -{ - TRACE2(left, right); - - bool r = RuntimeHelpers::strictEqual(left, right); - return Encode(r); -} - -inline ReturnedValue Runtime::strictNotEqual(const Value &left, const Value &right) -{ - TRACE2(left, right); - - bool r = ! RuntimeHelpers::strictEqual(left, right); - return Encode(r); -} - -inline Bool Runtime::compareNotEqual(const Value &left, const Value &right) -{ - TRACE2(left, right); - - return !Runtime::compareEqual(left, right); -} - -inline Bool Runtime::compareStrictEqual(const Value &left, const Value &right) -{ - TRACE2(left, right); - - return RuntimeHelpers::strictEqual(left, right); -} - -inline Bool Runtime::compareStrictNotEqual(const Value &left, const Value &right) -{ - TRACE2(left, right); - - return ! RuntimeHelpers::strictEqual(left, right); -} - -inline Bool Runtime::toBoolean(const Value &value) -{ - return value.toBoolean(); -} - } // namespace QV4 QT_END_NAMESPACE diff --git a/src/qml/jsruntime/qv4runtimeapi_p.h b/src/qml/jsruntime/qv4runtimeapi_p.h new file mode 100644 index 0000000000..040a545b83 --- /dev/null +++ b/src/qml/jsruntime/qv4runtimeapi_p.h @@ -0,0 +1,348 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtQml module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef QV4RUNTIMEAPI_P_H +#define QV4RUNTIMEAPI_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/qv4global_p.h> + +QT_BEGIN_NAMESPACE + +namespace QV4 { + +struct NoThrowEngine; + +namespace { +template <typename T> +struct ExceptionCheck { + enum { NeedsCheck = 1 }; +}; +// push_catch and pop context methods shouldn't check for exceptions +template <> +struct ExceptionCheck<void (*)(QV4::ExecutionEngine *)> { + enum { NeedsCheck = 0 }; +}; +template <typename A> +struct ExceptionCheck<void (*)(A, QV4::NoThrowEngine)> { + enum { NeedsCheck = 0 }; +}; +template <> +struct ExceptionCheck<QV4::ReturnedValue (*)(QV4::NoThrowEngine *)> { + enum { NeedsCheck = 0 }; +}; +template <typename A> +struct ExceptionCheck<QV4::ReturnedValue (*)(QV4::NoThrowEngine *, A)> { + enum { NeedsCheck = 0 }; +}; +template <typename A, typename B> +struct ExceptionCheck<QV4::ReturnedValue (*)(QV4::NoThrowEngine *, A, B)> { + enum { NeedsCheck = 0 }; +}; +template <typename A, typename B, typename C> +struct ExceptionCheck<void (*)(QV4::NoThrowEngine *, A, B, C)> { + enum { NeedsCheck = 0 }; +}; +} // anonymous namespace + +#define RUNTIME_METHOD(returnvalue, name, args) \ + typedef returnvalue (*Method_##name)args; \ + enum { Method_##name##_NeedsExceptionCheck = ExceptionCheck<Method_##name>::NeedsCheck }; \ + static returnvalue method_##name args; \ + const Method_##name name + +#define INIT_RUNTIME_METHOD(name) \ + name(method_##name) + +struct Q_QML_PRIVATE_EXPORT Runtime { + Runtime() + : INIT_RUNTIME_METHOD(callGlobalLookup) + , INIT_RUNTIME_METHOD(callActivationProperty) + , INIT_RUNTIME_METHOD(callQmlScopeObjectProperty) + , INIT_RUNTIME_METHOD(callQmlContextObjectProperty) + , INIT_RUNTIME_METHOD(callProperty) + , INIT_RUNTIME_METHOD(callPropertyLookup) + , INIT_RUNTIME_METHOD(callElement) + , INIT_RUNTIME_METHOD(callValue) + , INIT_RUNTIME_METHOD(constructGlobalLookup) + , INIT_RUNTIME_METHOD(constructActivationProperty) + , INIT_RUNTIME_METHOD(constructProperty) + , INIT_RUNTIME_METHOD(constructPropertyLookup) + , INIT_RUNTIME_METHOD(constructValue) + , INIT_RUNTIME_METHOD(setActivationProperty) + , INIT_RUNTIME_METHOD(setProperty) + , INIT_RUNTIME_METHOD(setElement) + , INIT_RUNTIME_METHOD(getProperty) + , INIT_RUNTIME_METHOD(getActivationProperty) + , INIT_RUNTIME_METHOD(getElement) + , INIT_RUNTIME_METHOD(typeofValue) + , INIT_RUNTIME_METHOD(typeofName) + , INIT_RUNTIME_METHOD(typeofScopeObjectProperty) + , INIT_RUNTIME_METHOD(typeofContextObjectProperty) + , INIT_RUNTIME_METHOD(typeofMember) + , INIT_RUNTIME_METHOD(typeofElement) + , INIT_RUNTIME_METHOD(deleteElement) + , INIT_RUNTIME_METHOD(deleteMember) + , INIT_RUNTIME_METHOD(deleteMemberString) + , INIT_RUNTIME_METHOD(deleteName) + , INIT_RUNTIME_METHOD(throwException) + , INIT_RUNTIME_METHOD(unwindException) + , INIT_RUNTIME_METHOD(pushWithScope) + , INIT_RUNTIME_METHOD(pushCatchScope) + , INIT_RUNTIME_METHOD(popScope) + , INIT_RUNTIME_METHOD(closure) + , INIT_RUNTIME_METHOD(declareVar) + , INIT_RUNTIME_METHOD(setupArgumentsObject) + , INIT_RUNTIME_METHOD(convertThisToObject) + , INIT_RUNTIME_METHOD(arrayLiteral) + , INIT_RUNTIME_METHOD(objectLiteral) + , INIT_RUNTIME_METHOD(regexpLiteral) + , INIT_RUNTIME_METHOD(foreachIterator) + , INIT_RUNTIME_METHOD(foreachNextPropertyName) + , INIT_RUNTIME_METHOD(uPlus) + , INIT_RUNTIME_METHOD(uMinus) + , INIT_RUNTIME_METHOD(uNot) + , INIT_RUNTIME_METHOD(complement) + , INIT_RUNTIME_METHOD(increment) + , INIT_RUNTIME_METHOD(decrement) + , INIT_RUNTIME_METHOD(instanceof) + , INIT_RUNTIME_METHOD(in) + , INIT_RUNTIME_METHOD(add) + , INIT_RUNTIME_METHOD(addString) + , INIT_RUNTIME_METHOD(bitOr) + , INIT_RUNTIME_METHOD(bitXor) + , INIT_RUNTIME_METHOD(bitAnd) + , INIT_RUNTIME_METHOD(sub) + , INIT_RUNTIME_METHOD(mul) + , INIT_RUNTIME_METHOD(div) + , INIT_RUNTIME_METHOD(mod) + , INIT_RUNTIME_METHOD(shl) + , INIT_RUNTIME_METHOD(shr) + , INIT_RUNTIME_METHOD(ushr) + , INIT_RUNTIME_METHOD(greaterThan) + , INIT_RUNTIME_METHOD(lessThan) + , INIT_RUNTIME_METHOD(greaterEqual) + , INIT_RUNTIME_METHOD(lessEqual) + , INIT_RUNTIME_METHOD(equal) + , INIT_RUNTIME_METHOD(notEqual) + , INIT_RUNTIME_METHOD(strictEqual) + , INIT_RUNTIME_METHOD(strictNotEqual) + , INIT_RUNTIME_METHOD(compareGreaterThan) + , INIT_RUNTIME_METHOD(compareLessThan) + , INIT_RUNTIME_METHOD(compareGreaterEqual) + , INIT_RUNTIME_METHOD(compareLessEqual) + , INIT_RUNTIME_METHOD(compareEqual) + , INIT_RUNTIME_METHOD(compareNotEqual) + , INIT_RUNTIME_METHOD(compareStrictEqual) + , INIT_RUNTIME_METHOD(compareStrictNotEqual) + , INIT_RUNTIME_METHOD(compareInstanceof) + , INIT_RUNTIME_METHOD(compareIn) + , INIT_RUNTIME_METHOD(toBoolean) + , INIT_RUNTIME_METHOD(toDouble) + , INIT_RUNTIME_METHOD(toInt) + , INIT_RUNTIME_METHOD(doubleToInt) + , INIT_RUNTIME_METHOD(toUInt) + , INIT_RUNTIME_METHOD(doubleToUInt) + , INIT_RUNTIME_METHOD(getQmlContext) + , INIT_RUNTIME_METHOD(getQmlImportedScripts) + , INIT_RUNTIME_METHOD(getQmlSingleton) + , INIT_RUNTIME_METHOD(getQmlAttachedProperty) + , INIT_RUNTIME_METHOD(getQmlScopeObjectProperty) + , INIT_RUNTIME_METHOD(getQmlContextObjectProperty) + , INIT_RUNTIME_METHOD(getQmlQObjectProperty) + , INIT_RUNTIME_METHOD(getQmlSingletonQObjectProperty) + , INIT_RUNTIME_METHOD(getQmlIdObject) + , INIT_RUNTIME_METHOD(setQmlScopeObjectProperty) + , INIT_RUNTIME_METHOD(setQmlContextObjectProperty) + , INIT_RUNTIME_METHOD(setQmlQObjectProperty) + { } + + // call + RUNTIME_METHOD(ReturnedValue, callGlobalLookup, (ExecutionEngine *engine, uint index, CallData *callData)); + RUNTIME_METHOD(ReturnedValue, callActivationProperty, (ExecutionEngine *engine, int nameIndex, CallData *callData)); + RUNTIME_METHOD(ReturnedValue, callQmlScopeObjectProperty, (ExecutionEngine *engine, int propertyIndex, CallData *callData)); + RUNTIME_METHOD(ReturnedValue, callQmlContextObjectProperty, (ExecutionEngine *engine, int propertyIndex, CallData *callData)); + RUNTIME_METHOD(ReturnedValue, callProperty, (ExecutionEngine *engine, int nameIndex, CallData *callData)); + RUNTIME_METHOD(ReturnedValue, callPropertyLookup, (ExecutionEngine *engine, uint index, CallData *callData)); + RUNTIME_METHOD(ReturnedValue, callElement, (ExecutionEngine *engine, const Value &index, CallData *callData)); + RUNTIME_METHOD(ReturnedValue, callValue, (ExecutionEngine *engine, const Value &func, CallData *callData)); + + // construct + RUNTIME_METHOD(ReturnedValue, constructGlobalLookup, (ExecutionEngine *engine, uint index, CallData *callData)); + RUNTIME_METHOD(ReturnedValue, constructActivationProperty, (ExecutionEngine *engine, int nameIndex, CallData *callData)); + RUNTIME_METHOD(ReturnedValue, constructProperty, (ExecutionEngine *engine, int nameIndex, CallData *callData)); + RUNTIME_METHOD(ReturnedValue, constructPropertyLookup, (ExecutionEngine *engine, uint index, CallData *callData)); + RUNTIME_METHOD(ReturnedValue, constructValue, (ExecutionEngine *engine, const Value &func, CallData *callData)); + + // set & get + RUNTIME_METHOD(void, setActivationProperty, (ExecutionEngine *engine, int nameIndex, const Value &value)); + RUNTIME_METHOD(void, setProperty, (ExecutionEngine *engine, const Value &object, int nameIndex, const Value &value)); + RUNTIME_METHOD(void, setElement, (ExecutionEngine *engine, const Value &object, const Value &index, const Value &value)); + RUNTIME_METHOD(ReturnedValue, getProperty, (ExecutionEngine *engine, const Value &object, int nameIndex)); + RUNTIME_METHOD(ReturnedValue, getActivationProperty, (ExecutionEngine *engine, int nameIndex)); + RUNTIME_METHOD(ReturnedValue, getElement, (ExecutionEngine *engine, const Value &object, const Value &index)); + + // typeof + RUNTIME_METHOD(ReturnedValue, typeofValue, (ExecutionEngine *engine, const Value &val)); + RUNTIME_METHOD(ReturnedValue, typeofName, (ExecutionEngine *engine, int nameIndex)); + RUNTIME_METHOD(ReturnedValue, typeofScopeObjectProperty, (ExecutionEngine *engine, const Value &context, int propertyIndex)); + RUNTIME_METHOD(ReturnedValue, typeofContextObjectProperty, (ExecutionEngine *engine, const Value &context, int propertyIndex)); + RUNTIME_METHOD(ReturnedValue, typeofMember, (ExecutionEngine *engine, const Value &base, int nameIndex)); + RUNTIME_METHOD(ReturnedValue, typeofElement, (ExecutionEngine *engine, const Value &base, const Value &index)); + + // delete + RUNTIME_METHOD(ReturnedValue, deleteElement, (ExecutionEngine *engine, const Value &base, const Value &index)); + RUNTIME_METHOD(ReturnedValue, deleteMember, (ExecutionEngine *engine, const Value &base, int nameIndex)); + RUNTIME_METHOD(ReturnedValue, deleteMemberString, (ExecutionEngine *engine, const Value &base, String *name)); + RUNTIME_METHOD(ReturnedValue, deleteName, (ExecutionEngine *engine, int nameIndex)); + + // exceptions & scopes + RUNTIME_METHOD(void, throwException, (ExecutionEngine *engine, const Value &value)); + RUNTIME_METHOD(ReturnedValue, unwindException, (ExecutionEngine *engine)); + RUNTIME_METHOD(void, pushWithScope, (const Value &o, ExecutionEngine *engine)); + RUNTIME_METHOD(void, pushCatchScope, (NoThrowEngine *engine, int exceptionVarNameIndex)); + RUNTIME_METHOD(void, popScope, (ExecutionEngine *engine)); + + // closures + RUNTIME_METHOD(ReturnedValue, closure, (ExecutionEngine *engine, int functionId)); + + // function header + RUNTIME_METHOD(void, declareVar, (ExecutionEngine *engine, bool deletable, int nameIndex)); + RUNTIME_METHOD(ReturnedValue, setupArgumentsObject, (ExecutionEngine *engine)); + RUNTIME_METHOD(void, convertThisToObject, (ExecutionEngine *engine)); + + // literals + RUNTIME_METHOD(ReturnedValue, arrayLiteral, (ExecutionEngine *engine, Value *values, uint length)); + RUNTIME_METHOD(ReturnedValue, objectLiteral, (ExecutionEngine *engine, const Value *args, int classId, int arrayValueCount, int arrayGetterSetterCountAndFlags)); + RUNTIME_METHOD(ReturnedValue, regexpLiteral, (ExecutionEngine *engine, int id)); + + // foreach + RUNTIME_METHOD(ReturnedValue, foreachIterator, (ExecutionEngine *engine, const Value &in)); + RUNTIME_METHOD(ReturnedValue, foreachNextPropertyName, (const Value &foreach_iterator)); + + // unary operators + typedef ReturnedValue (*UnaryOperation)(const Value &value); + RUNTIME_METHOD(ReturnedValue, uPlus, (const Value &value)); + RUNTIME_METHOD(ReturnedValue, uMinus, (const Value &value)); + RUNTIME_METHOD(ReturnedValue, uNot, (const Value &value)); + RUNTIME_METHOD(ReturnedValue, complement, (const Value &value)); + RUNTIME_METHOD(ReturnedValue, increment, (const Value &value)); + RUNTIME_METHOD(ReturnedValue, decrement, (const Value &value)); + + // binary operators + typedef ReturnedValue (*BinaryOperation)(const Value &left, const Value &right); + typedef ReturnedValue (*BinaryOperationContext)(ExecutionEngine *engine, const Value &left, const Value &right); + + RUNTIME_METHOD(ReturnedValue, instanceof, (ExecutionEngine *engine, const Value &left, const Value &right)); + RUNTIME_METHOD(ReturnedValue, in, (ExecutionEngine *engine, const Value &left, const Value &right)); + RUNTIME_METHOD(ReturnedValue, add, (ExecutionEngine *engine, const Value &left, const Value &right)); + RUNTIME_METHOD(ReturnedValue, addString, (ExecutionEngine *engine, const Value &left, const Value &right)); + RUNTIME_METHOD(ReturnedValue, bitOr, (const Value &left, const Value &right)); + RUNTIME_METHOD(ReturnedValue, bitXor, (const Value &left, const Value &right)); + RUNTIME_METHOD(ReturnedValue, bitAnd, (const Value &left, const Value &right)); + RUNTIME_METHOD(ReturnedValue, sub, (const Value &left, const Value &right)); + RUNTIME_METHOD(ReturnedValue, mul, (const Value &left, const Value &right)); + RUNTIME_METHOD(ReturnedValue, div, (const Value &left, const Value &right)); + RUNTIME_METHOD(ReturnedValue, mod, (const Value &left, const Value &right)); + RUNTIME_METHOD(ReturnedValue, shl, (const Value &left, const Value &right)); + RUNTIME_METHOD(ReturnedValue, shr, (const Value &left, const Value &right)); + RUNTIME_METHOD(ReturnedValue, ushr, (const Value &left, const Value &right)); + RUNTIME_METHOD(ReturnedValue, greaterThan, (const Value &left, const Value &right)); + RUNTIME_METHOD(ReturnedValue, lessThan, (const Value &left, const Value &right)); + RUNTIME_METHOD(ReturnedValue, greaterEqual, (const Value &left, const Value &right)); + RUNTIME_METHOD(ReturnedValue, lessEqual, (const Value &left, const Value &right)); + RUNTIME_METHOD(ReturnedValue, equal, (const Value &left, const Value &right)); + RUNTIME_METHOD(ReturnedValue, notEqual, (const Value &left, const Value &right)); + RUNTIME_METHOD(ReturnedValue, strictEqual, (const Value &left, const Value &right)); + RUNTIME_METHOD(ReturnedValue, strictNotEqual, (const Value &left, const Value &right)); + + // comparisons + RUNTIME_METHOD(Bool, compareGreaterThan, (const Value &l, const Value &r)); + RUNTIME_METHOD(Bool, compareLessThan, (const Value &l, const Value &r)); + RUNTIME_METHOD(Bool, compareGreaterEqual, (const Value &l, const Value &r)); + RUNTIME_METHOD(Bool, compareLessEqual, (const Value &l, const Value &r)); + RUNTIME_METHOD(Bool, compareEqual, (const Value &left, const Value &right)); + RUNTIME_METHOD(Bool, compareNotEqual, (const Value &left, const Value &right)); + RUNTIME_METHOD(Bool, compareStrictEqual, (const Value &left, const Value &right)); + RUNTIME_METHOD(Bool, compareStrictNotEqual, (const Value &left, const Value &right)); + + RUNTIME_METHOD(Bool, compareInstanceof, (ExecutionEngine *engine, const Value &left, const Value &right)); + RUNTIME_METHOD(Bool, compareIn, (ExecutionEngine *engine, const Value &left, const Value &right)); + + // conversions + RUNTIME_METHOD(Bool, toBoolean, (const Value &value)); + RUNTIME_METHOD(ReturnedValue, toDouble, (const Value &value)); + RUNTIME_METHOD(int, toInt, (const Value &value)); + RUNTIME_METHOD(int, doubleToInt, (const double &d)); + RUNTIME_METHOD(unsigned, toUInt, (const Value &value)); + RUNTIME_METHOD(unsigned, doubleToUInt, (const double &d)); + + // qml + RUNTIME_METHOD(ReturnedValue, getQmlContext, (NoThrowEngine *engine)); + RUNTIME_METHOD(ReturnedValue, getQmlImportedScripts, (NoThrowEngine *engine)); + RUNTIME_METHOD(ReturnedValue, getQmlSingleton, (NoThrowEngine *engine, int nameIndex)); + RUNTIME_METHOD(ReturnedValue, getQmlAttachedProperty, (ExecutionEngine *engine, int attachedPropertiesId, int propertyIndex)); + RUNTIME_METHOD(ReturnedValue, getQmlScopeObjectProperty, (ExecutionEngine *engine, const Value &context, int propertyIndex, bool captureRequired)); + RUNTIME_METHOD(ReturnedValue, getQmlContextObjectProperty, (ExecutionEngine *engine, const Value &context, int propertyIndex, bool captureRequired)); + RUNTIME_METHOD(ReturnedValue, getQmlQObjectProperty, (ExecutionEngine *engine, const Value &object, int propertyIndex, bool captureRequired)); + RUNTIME_METHOD(ReturnedValue, getQmlSingletonQObjectProperty, (ExecutionEngine *engine, const Value &object, int propertyIndex, bool captureRequired)); + RUNTIME_METHOD(ReturnedValue, getQmlIdObject, (ExecutionEngine *engine, const Value &context, uint index)); + + RUNTIME_METHOD(void, setQmlScopeObjectProperty, (ExecutionEngine *engine, const Value &context, int propertyIndex, const Value &value)); + RUNTIME_METHOD(void, setQmlContextObjectProperty, (ExecutionEngine *engine, const Value &context, int propertyIndex, const Value &value)); + RUNTIME_METHOD(void, setQmlQObjectProperty, (ExecutionEngine *engine, const Value &object, int propertyIndex, const Value &value)); +}; + +#undef RUNTIME_METHOD +#undef INIT_RUNTIME_METHOD + +} // namespace QV4 + +QT_END_NAMESPACE + +#endif // QV4RUNTIMEAPI_P_H diff --git a/src/qml/jsruntime/qv4scopedvalue_p.h b/src/qml/jsruntime/qv4scopedvalue_p.h index 5bc17f741b..5022d7c3bc 100644 --- a/src/qml/jsruntime/qv4scopedvalue_p.h +++ b/src/qml/jsruntime/qv4scopedvalue_p.h @@ -71,14 +71,18 @@ struct ScopedValue; struct Scope { inline Scope(ExecutionContext *ctx) : engine(ctx->d()->engine) + , mark(engine->jsStackTop) + , result(*engine->jsAlloca(1)) { - mark = engine->jsStackTop; + result = Encode::undefined(); } explicit Scope(ExecutionEngine *e) : engine(e) + , mark(engine->jsStackTop) + , result(*engine->jsAlloca(1)) { - mark = engine->jsStackTop; + result = Encode::undefined(); } ~Scope() { @@ -93,7 +97,7 @@ struct Scope { engine->jsStackTop = mark; } - Value *alloc(int nValues) { + Value *alloc(int nValues) const { return engine->jsAlloca(nValues); } @@ -103,6 +107,7 @@ struct Scope { ExecutionEngine *engine; Value *mark; + Value &result; private: Q_DISABLE_COPY(Scope) @@ -190,59 +195,59 @@ struct Scoped Scoped(const Scope &scope) { - ptr = scope.engine->jsStackTop++; - ptr->setM(0); + ptr = scope.engine->jsAlloca(1); } Scoped(const Scope &scope, const Value &v) { - ptr = scope.engine->jsStackTop++; + ptr = scope.engine->jsAlloca(1); setPointer(v.as<T>()); } Scoped(const Scope &scope, Heap::Base *o) { Value v; v = o; - ptr = scope.engine->jsStackTop++; + ptr = scope.engine->jsAlloca(1); setPointer(v.as<T>()); } Scoped(const Scope &scope, const ScopedValue &v) { - ptr = scope.engine->jsStackTop++; + ptr = scope.engine->jsAlloca(1); setPointer(v.ptr->as<T>()); } Scoped(const Scope &scope, const Value &v, ConvertType) { - ptr = scope.engine->jsStackTop++; + ptr = scope.engine->jsAlloca(1); ptr->setRawValue(value_convert<T>(scope.engine, v)); } Scoped(const Scope &scope, const Value *v) { - ptr = scope.engine->jsStackTop++; + ptr = scope.engine->jsAlloca(1); setPointer(v ? v->as<T>() : 0); } Scoped(const Scope &scope, T *t) { - ptr = scope.engine->jsStackTop++; + ptr = scope.engine->jsAlloca(1); setPointer(t); } Scoped(const Scope &scope, typename T::Data *t) { - ptr = scope.engine->jsStackTop++; + ptr = scope.engine->jsAlloca(1); *ptr = t; } Scoped(const Scope &scope, const ReturnedValue &v) { - ptr = scope.engine->jsStackTop++; + ptr = scope.engine->jsAlloca(1); setPointer(QV4::Value::fromReturnedValue(v).as<T>()); } + Scoped(const Scope &scope, const ReturnedValue &v, ConvertType) { - ptr = scope.engine->jsStackTop++; + ptr = scope.engine->jsAlloca(1); ptr->setRawValue(value_convert<T>(scope.engine, QV4::Value::fromReturnedValue(v))); } @@ -289,6 +294,10 @@ struct Scoped return ptr->cast<T>(); } + const T *operator->() const { + return ptr->cast<T>(); + } + bool operator!() const { return !ptr->m(); } @@ -311,7 +320,7 @@ struct Scoped }; struct ScopedCallData { - ScopedCallData(Scope &scope, int argc = 0) + ScopedCallData(const Scope &scope, int argc = 0) { int size = qMax(argc, (int)QV4::Global::ReservedArgumentCount) + qOffsetOf(QV4::CallData, args)/sizeof(QV4::Value); ptr = reinterpret_cast<CallData *>(scope.alloc(size)); @@ -362,19 +371,19 @@ struct ScopedProperty struct ExecutionContextSaver { - ExecutionEngine *engine; + Scope scope; // this makes sure that a reference to context on the JS stack goes out of scope as soon as the context is not used anymore. ExecutionContext *savedContext; - ExecutionContextSaver(Scope &scope) - : engine(scope.engine) + ExecutionContextSaver(const Scope &scope) + : scope(scope.engine) { - savedContext = engine->currentContext; + savedContext = scope.engine->currentContext; } ~ExecutionContextSaver() { - Q_ASSERT(engine->jsStackTop > engine->currentContext); - engine->currentContext = savedContext; - engine->current = savedContext->d(); + Q_ASSERT(scope.engine->jsStackTop > scope.engine->currentContext); + scope.engine->currentContext = savedContext; + scope.engine->current = savedContext->d(); } }; diff --git a/src/qml/jsruntime/qv4script.cpp b/src/qml/jsruntime/qv4script.cpp index 4b847600b4..787047806a 100644 --- a/src/qml/jsruntime/qv4script.cpp +++ b/src/qml/jsruntime/qv4script.cpp @@ -64,34 +64,16 @@ QT_BEGIN_NAMESPACE namespace QV4 { namespace Heap { -struct CompilationUnitHolder : Object { - inline CompilationUnitHolder(CompiledData::CompilationUnit *unit); - - QQmlRefPointer<CompiledData::CompilationUnit> unit; -}; - struct QmlBindingWrapper : FunctionObject { - QmlBindingWrapper(QV4::QmlContext *scope, Function *f); + void init(QV4::QmlContext *scope, Function *f); }; } -struct CompilationUnitHolder : public Object -{ - V4_OBJECT2(CompilationUnitHolder, Object) - V4_NEEDS_DESTROY -}; - -inline -Heap::CompilationUnitHolder::CompilationUnitHolder(CompiledData::CompilationUnit *unit) - : unit(unit) -{ -} - struct QmlBindingWrapper : FunctionObject { V4_OBJECT2(QmlBindingWrapper, FunctionObject) - static ReturnedValue call(const Managed *that, CallData *callData); + static void call(const Managed *that, Scope &scope, CallData *callData); }; } @@ -101,11 +83,11 @@ QT_END_NAMESPACE using namespace QV4; DEFINE_OBJECT_VTABLE(QmlBindingWrapper); -DEFINE_OBJECT_VTABLE(CompilationUnitHolder); -Heap::QmlBindingWrapper::QmlBindingWrapper(QV4::QmlContext *scope, Function *f) - : Heap::FunctionObject(scope, scope->d()->engine->id_eval(), /*createProto = */ false) +void Heap::QmlBindingWrapper::init(QV4::QmlContext *scope, Function *f) { + Heap::FunctionObject::init(scope, scope->d()->engine->id_eval(), /*createProto = */ false); + Q_ASSERT(scope->inUse()); function = f; @@ -113,32 +95,33 @@ Heap::QmlBindingWrapper::QmlBindingWrapper(QV4::QmlContext *scope, Function *f) function->compilationUnit->addref(); } -ReturnedValue QmlBindingWrapper::call(const Managed *that, CallData *callData) +void QmlBindingWrapper::call(const Managed *that, Scope &scope, CallData *callData) { const QmlBindingWrapper *This = static_cast<const QmlBindingWrapper *>(that); ExecutionEngine *v4 = static_cast<const Object *>(that)->engine(); - if (v4->hasException) - return Encode::undefined(); - CHECK_STACK_LIMITS(v4); + if (v4->hasException) { + scope.result = Encode::undefined(); + return; + } + CHECK_STACK_LIMITS(v4, scope); - Scope scope(v4); ExecutionContextSaver ctxSaver(scope); QV4::Function *f = This->function(); - if (!f) - return QV4::Encode::undefined(); + if (!f) { + scope.result = QV4::Encode::undefined(); + return; + } Scoped<CallContext> ctx(scope, v4->currentContext->newCallContext(This, callData)); v4->pushContext(ctx); - ScopedValue result(scope, Q_V4_PROFILE(v4, f)); - - return result->asReturnedValue(); + scope.result = Q_V4_PROFILE(v4, f); } Script::Script(ExecutionEngine *v4, QmlContext *qml, CompiledData::CompilationUnit *compilationUnit) : line(0), column(0), scope(v4->rootContext()), strictMode(false), inheritContext(true), parsed(false) - , vmFunction(0), parseAsBinding(true) + , compilationUnit(compilationUnit), vmFunction(0), parseAsBinding(true) { if (qml) qmlContext.set(v4, *qml); @@ -146,11 +129,6 @@ Script::Script(ExecutionEngine *v4, QmlContext *qml, CompiledData::CompilationUn parsed = true; vmFunction = compilationUnit ? compilationUnit->linkToEngine(v4) : 0; - if (vmFunction) { - Scope valueScope(v4); - ScopedObject holder(valueScope, v4->memoryManager->allocObject<CompilationUnitHolder>(compilationUnit)); - compilationUnitHolder.set(v4, holder); - } } Script::~Script() @@ -171,7 +149,7 @@ void Script::parse() MemoryManager::GCBlocker gcBlocker(v4->memoryManager); - IR::Module module(v4->debugger != 0); + IR::Module module(v4->debugger() != 0); QQmlJS::Engine ee, *engine = ⅇ Lexer lexer(engine); @@ -217,10 +195,8 @@ void Script::parse() QScopedPointer<EvalInstructionSelection> isel(v4->iselFactory->create(QQmlEnginePrivate::get(v4), v4->executableAllocator, &module, &jsGenerator)); if (inheritContext) isel->setUseFastLookups(false); - QQmlRefPointer<QV4::CompiledData::CompilationUnit> compilationUnit = isel->compile(); + compilationUnit = isel->compile(); vmFunction = compilationUnit->linkToEngine(v4); - ScopedObject holder(valueScope, v4->memoryManager->allocObject<CompilationUnitHolder>(compilationUnit)); - compilationUnitHolder.set(v4, holder); } if (!vmFunction) { @@ -247,6 +223,7 @@ ReturnedValue Script::run() ContextStateSaver stateSaver(valueScope, scope); scope->d()->strictMode = vmFunction->isStrict(); scope->d()->lookups = vmFunction->compilationUnit->runtimeLookups; + scope->d()->constantTable = vmFunction->compilationUnit->constants; scope->d()->compilationUnit = vmFunction->compilationUnit; return Q_V4_PROFILE(engine, vmFunction); @@ -255,7 +232,8 @@ ReturnedValue Script::run() ScopedFunctionObject f(valueScope, engine->memoryManager->allocObject<QmlBindingWrapper>(qml, vmFunction)); ScopedCallData callData(valueScope); callData->thisObject = Primitive::undefinedValue(); - return f->call(callData); + f->call(valueScope, callData); + return valueScope.result.asReturnedValue(); } } diff --git a/src/qml/jsruntime/qv4script_p.h b/src/qml/jsruntime/qv4script_p.h index d7b82218e7..2e87a7692b 100644 --- a/src/qml/jsruntime/qv4script_p.h +++ b/src/qml/jsruntime/qv4script_p.h @@ -71,22 +71,25 @@ struct ContextStateSaver { Value *savedContext; bool strictMode; Lookup *lookups; + const QV4::Value *constantTable; CompiledData::CompilationUnit *compilationUnit; int lineNumber; - ContextStateSaver(Scope &scope, ExecutionContext *context) + ContextStateSaver(const Scope &scope, ExecutionContext *context) : savedContext(scope.alloc(1)) , strictMode(context->d()->strictMode) , lookups(context->d()->lookups) + , constantTable(context->d()->constantTable) , compilationUnit(context->d()->compilationUnit) , lineNumber(context->d()->lineNumber) { savedContext->setM(context->d()); } - ContextStateSaver(Scope &scope, Heap::ExecutionContext *context) + ContextStateSaver(const Scope &scope, Heap::ExecutionContext *context) : savedContext(scope.alloc(1)) , strictMode(context->strictMode) , lookups(context->lookups) + , constantTable(context->constantTable) , compilationUnit(context->compilationUnit) , lineNumber(context->lineNumber) { @@ -98,6 +101,7 @@ struct ContextStateSaver { Heap::ExecutionContext *ctx = static_cast<Heap::ExecutionContext *>(savedContext->m()); ctx->strictMode = strictMode; ctx->lookups = lookups; + ctx->constantTable = constantTable; ctx->compilationUnit = compilationUnit; ctx->lineNumber = lineNumber; } @@ -126,7 +130,7 @@ struct Q_QML_EXPORT Script { bool inheritContext; bool parsed; QV4::PersistentValue qmlContext; - QV4::PersistentValue compilationUnitHolder; + QQmlRefPointer<CompiledData::CompilationUnit> compilationUnit; Function *vmFunction; bool parseAsBinding; diff --git a/src/qml/jsruntime/qv4sequenceobject.cpp b/src/qml/jsruntime/qv4sequenceobject.cpp index 0f2acd6bbb..58da7b9f68 100644 --- a/src/qml/jsruntime/qv4sequenceobject.cpp +++ b/src/qml/jsruntime/qv4sequenceobject.cpp @@ -216,11 +216,16 @@ namespace Heap { template <typename Container> struct QQmlSequence : Object { - QQmlSequence(const Container &container); - QQmlSequence(QObject *object, int propertyIndex); + void init(const Container &container); + void init(QObject *object, int propertyIndex); + void destroy() { + delete container; + object.destroy(); + Object::destroy(); + } - mutable Container container; - QPointer<QObject> object; + mutable Container *container; + QQmlQPointer<QObject> object; int propertyIndex; bool isReference; }; @@ -259,10 +264,10 @@ public: loadReference(); } qint32 signedIdx = static_cast<qint32>(index); - if (signedIdx < d()->container.count()) { + if (signedIdx < d()->container->count()) { if (hasProperty) *hasProperty = true; - return convertElementToValue(engine(), d()->container.at(signedIdx)); + return convertElementToValue(engine(), d()->container->at(signedIdx)); } if (hasProperty) *hasProperty = false; @@ -288,22 +293,22 @@ public: qint32 signedIdx = static_cast<qint32>(index); - int count = d()->container.count(); + int count = d()->container->count(); typename Container::value_type element = convertValueToElement<typename Container::value_type>(value); if (signedIdx == count) { - d()->container.append(element); + d()->container->append(element); } else if (signedIdx < count) { - d()->container[signedIdx] = element; + (*d()->container)[signedIdx] = element; } else { /* according to ECMA262r3 we need to insert */ /* the value at the given index, increasing length to index+1. */ - d()->container.reserve(signedIdx + 1); + d()->container->reserve(signedIdx + 1); while (signedIdx > count++) { - d()->container.append(typename Container::value_type()); + d()->container->append(typename Container::value_type()); } - d()->container.append(element); + d()->container->append(element); } if (d()->isReference) @@ -323,7 +328,7 @@ public: loadReference(); } qint32 signedIdx = static_cast<qint32>(index); - return (signedIdx < d()->container.count()) ? QV4::Attr_Data : QV4::Attr_Invalid; + return (signedIdx < d()->container->count()) ? QV4::Attr_Data : QV4::Attr_Invalid; } void containerAdvanceIterator(ObjectIterator *it, Value *name, uint *index, Property *p, PropertyAttributes *attrs) @@ -339,11 +344,11 @@ public: loadReference(); } - if (it->arrayIndex < static_cast<uint>(d()->container.count())) { + if (it->arrayIndex < static_cast<uint>(d()->container->count())) { *index = it->arrayIndex; ++it->arrayIndex; *attrs = QV4::Attr_Data; - p->value = convertElementToValue(engine(), d()->container.at(*index)); + p->value = convertElementToValue(engine(), d()->container->at(*index)); return; } QV4::Object::advanceIterator(this, it, name, index, p, attrs); @@ -361,12 +366,12 @@ public: } qint32 signedIdx = static_cast<qint32>(index); - if (signedIdx >= d()->container.count()) + if (signedIdx >= d()->container->count()) return false; /* according to ECMA262r3 it should be Undefined, */ /* but we cannot, so we insert a default-value instead. */ - d()->container.replace(signedIdx, typename Container::value_type()); + d()->container->replace(signedIdx, typename Container::value_type()); if (d()->isReference) storeReference(); @@ -411,8 +416,8 @@ public: callData->args[0] = convertElementToValue(this->m_ctx->d()->engine, lhs); callData->args[1] = convertElementToValue(this->m_ctx->d()->engine, rhs); callData->thisObject = this->m_ctx->d()->engine->globalObject; - QV4::ScopedValue result(scope, compare->call(callData)); - return result->toNumber() < 0; + compare->call(scope, callData); + return scope.result.toNumber() < 0; } private: @@ -431,10 +436,10 @@ public: QV4::Scope scope(ctx); if (ctx->argc() == 1 && ctx->args()[0].as<FunctionObject>()) { CompareFunctor cf(ctx, ctx->args()[0]); - std::sort(d()->container.begin(), d()->container.end(), cf); + std::sort(d()->container->begin(), d()->container->end(), cf); } else { DefaultCompareFunctor cf; - std::sort(d()->container.begin(), d()->container.end(), cf); + std::sort(d()->container->begin(), d()->container->end(), cf); } if (d()->isReference) @@ -453,7 +458,7 @@ public: return QV4::Encode(0); This->loadReference(); } - return QV4::Encode(This->d()->container.count()); + return QV4::Encode(This->d()->container->count()); } static QV4::ReturnedValue method_set_length(QV4::CallContext* ctx) @@ -477,23 +482,23 @@ public: } /* Determine whether we need to modify the sequence */ qint32 newCount = static_cast<qint32>(newLength); - qint32 count = This->d()->container.count(); + qint32 count = This->d()->container->count(); if (newCount == count) { return QV4::Encode::undefined(); } else if (newCount > count) { /* according to ECMA262r3 we need to insert */ /* undefined values increasing length to newLength. */ /* We cannot, so we insert default-values instead. */ - This->d()->container.reserve(newCount); + This->d()->container->reserve(newCount); while (newCount > count++) { - This->d()->container.append(typename Container::value_type()); + This->d()->container->append(typename Container::value_type()); } } else { /* according to ECMA262r3 we need to remove */ /* elements until the sequence is the required length. */ while (newCount < count) { count--; - This->d()->container.removeAt(count); + This->d()->container->removeAt(count); } } /* write back if required. */ @@ -505,7 +510,7 @@ public: } QVariant toVariant() const - { return QVariant::fromValue<Container>(d()->container); } + { return QVariant::fromValue<Container>(*d()->container); } static QVariant toVariant(QV4::ArrayObject *array) { @@ -522,7 +527,7 @@ public: { Q_ASSERT(d()->object); Q_ASSERT(d()->isReference); - void *a[] = { &d()->container, 0 }; + void *a[] = { d()->container, 0 }; QMetaObject::metacall(d()->object, QMetaObject::ReadProperty, d()->propertyIndex, a); } @@ -531,8 +536,8 @@ public: Q_ASSERT(d()->object); Q_ASSERT(d()->isReference); int status = -1; - QQmlPropertyPrivate::WriteFlags flags = QQmlPropertyPrivate::DontRemoveBinding; - void *a[] = { &d()->container, 0, &status, &flags }; + QQmlPropertyData::WriteFlags flags = QQmlPropertyData::DontRemoveBinding; + void *a[] = { d()->container, 0, &status, &flags }; QMetaObject::metacall(d()->object, QMetaObject::WriteProperty, d()->propertyIndex, a); } @@ -553,11 +558,14 @@ public: template <typename Container> -Heap::QQmlSequence<Container>::QQmlSequence(const Container &container) - : container(container) - , propertyIndex(-1) - , isReference(false) +void Heap::QQmlSequence<Container>::init(const Container &container) { + Object::init(); + this->container = new Container(container); + propertyIndex = -1; + isReference = false; + object.init(); + QV4::Scope scope(internalClass->engine); QV4::Scoped<QV4::QQmlSequence<Container> > o(scope, this); o->setArrayType(Heap::ArrayData::Custom); @@ -565,11 +573,13 @@ Heap::QQmlSequence<Container>::QQmlSequence(const Container &container) } template <typename Container> -Heap::QQmlSequence<Container>::QQmlSequence(QObject *object, int propertyIndex) - : object(object) - , propertyIndex(propertyIndex) - , isReference(true) +void Heap::QQmlSequence<Container>::init(QObject *object, int propertyIndex) { + Object::init(); + this->container = new Container; + this->propertyIndex = propertyIndex; + isReference = true; + this->object.init(object); QV4::Scope scope(internalClass->engine); QV4::Scoped<QV4::QQmlSequence<Container> > o(scope, this); o->setArrayType(Heap::ArrayData::Custom); diff --git a/src/qml/jsruntime/qv4string.cpp b/src/qml/jsruntime/qv4string.cpp index abef885249..3365ffe637 100644 --- a/src/qml/jsruntime/qv4string.cpp +++ b/src/qml/jsruntime/qv4string.cpp @@ -49,57 +49,8 @@ using namespace QV4; -static uint toArrayIndex(const QChar *ch, const QChar *end) -{ - uint i = ch->unicode() - '0'; - if (i > 9) - return UINT_MAX; - ++ch; - // reject "01", "001", ... - if (i == 0 && ch != end) - return UINT_MAX; - - while (ch < end) { - uint x = ch->unicode() - '0'; - if (x > 9) - return UINT_MAX; - uint n = i*10 + x; - if (n < i) - // overflow - return UINT_MAX; - i = n; - ++ch; - } - return i; -} - #ifndef V4_BOOTSTRAP -static uint toArrayIndex(const char *ch, const char *end) -{ - uint i = *ch - '0'; - if (i > 9) - return UINT_MAX; - ++ch; - // reject "01", "001", ... - if (i == 0 && ch != end) - return UINT_MAX; - - while (ch < end) { - uint x = *ch - '0'; - if (x > 9) - return UINT_MAX; - uint n = i*10 + x; - if (n < i) - // overflow - return UINT_MAX; - i = n; - ++ch; - } - return i; -} - - DEFINE_MANAGED_VTABLE(String); void String::markObjects(Heap::Base *that, ExecutionEngine *e) @@ -123,9 +74,11 @@ bool String::isEqualTo(Managed *t, Managed *o) } -Heap::String::String(MemoryManager *mm, const QString &t) - : mm(mm) +void Heap::String::init(MemoryManager *mm, const QString &t) { + Base::init(); + this->mm = mm; + subtype = String::StringType_Unknown; text = const_cast<QString &>(t).data_ptr(); @@ -136,9 +89,11 @@ Heap::String::String(MemoryManager *mm, const QString &t) len = text->size; } -Heap::String::String(MemoryManager *mm, String *l, String *r) - : mm(mm) +void Heap::String::init(MemoryManager *mm, String *l, String *r) { + Base::init(); + this->mm = mm; + subtype = String::StringType_Unknown; left = l; @@ -198,31 +153,6 @@ void Heap::String::simplifyString() const mm->growUnmanagedHeapSizeUsage(size_t(text->size) * sizeof(QChar)); } -void Heap::String::createHashValue() const -{ - if (largestSubLength) - simplifyString(); - Q_ASSERT(!largestSubLength); - const QChar *ch = reinterpret_cast<const QChar *>(text->data()); - const QChar *end = ch + text->size; - - // array indices get their number as hash value - stringHash = ::toArrayIndex(ch, end); - if (stringHash != UINT_MAX) { - subtype = Heap::String::StringType_ArrayIndex; - return; - } - - uint h = 0xffffffff; - while (ch < end) { - h = 31 * h + ch->unicode(); - ++ch; - } - - stringHash = h; - subtype = Heap::String::StringType_Regular; -} - void Heap::String::append(const String *data, QChar *ch) { std::vector<const String *> worklist; @@ -243,45 +173,14 @@ void Heap::String::append(const String *data, QChar *ch) } } - - - -uint String::createHashValue(const QChar *ch, int length) -{ - const QChar *end = ch + length; - - // array indices get their number as hash value - uint stringHash = ::toArrayIndex(ch, end); - if (stringHash != UINT_MAX) - return stringHash; - - uint h = 0xffffffff; - while (ch < end) { - h = 31 * h + ch->unicode(); - ++ch; - } - - return h; -} - -uint String::createHashValue(const char *ch, int length) +void Heap::String::createHashValue() const { - const char *end = ch + length; - - // array indices get their number as hash value - uint stringHash = ::toArrayIndex(ch, end); - if (stringHash != UINT_MAX) - return stringHash; - - uint h = 0xffffffff; - while (ch < end) { - if ((uchar)(*ch) >= 0x80) - return UINT_MAX; - h = 31 * h + *ch; - ++ch; - } - - return h; + if (largestSubLength) + simplifyString(); + Q_ASSERT(!largestSubLength); + const QChar *ch = reinterpret_cast<const QChar *>(text->data()); + const QChar *end = ch + text->size; + stringHash = QV4::String::calculateHashValue(ch, end, &subtype); } uint String::getLength(const Managed *m) @@ -293,6 +192,6 @@ uint String::getLength(const Managed *m) uint String::toArrayIndex(const QString &str) { - return ::toArrayIndex(str.constData(), str.constData() + str.length()); + return QV4::String::toArrayIndex(str.constData(), str.constData() + str.length()); } diff --git a/src/qml/jsruntime/qv4string_p.h b/src/qml/jsruntime/qv4string_p.h index 3196f49896..23ec3349b9 100644 --- a/src/qml/jsruntime/qv4string_p.h +++ b/src/qml/jsruntime/qv4string_p.h @@ -52,6 +52,7 @@ #include <QtCore/qstring.h> #include "qv4managed_p.h" +#include <QtCore/private/qnumeric_p.h> QT_BEGIN_NAMESPACE @@ -62,7 +63,6 @@ struct Identifier; namespace Heap { -#ifndef V4_BOOTSTRAP struct Q_QML_PRIVATE_EXPORT String : Base { enum StringType { StringType_Unknown, @@ -70,11 +70,13 @@ struct Q_QML_PRIVATE_EXPORT String : Base { StringType_ArrayIndex }; - String(MemoryManager *mm, const QString &text); - String(MemoryManager *mm, String *l, String *n); - ~String() { +#ifndef V4_BOOTSTRAP + void init(MemoryManager *mm, const QString &text); + void init(MemoryManager *mm, String *l, String *n); + void destroy() { if (!largestSubLength && !text->ref.deref()) QStringData::deallocate(text); + Base::destroy(); } void simplifyString() const; int length() const { @@ -130,8 +132,9 @@ struct Q_QML_PRIVATE_EXPORT String : Base { MemoryManager *mm; private: static void append(const String *data, QChar *ch); -}; #endif +}; +V4_ASSERT_IS_TRIVIAL(String) } @@ -183,8 +186,17 @@ struct Q_QML_PRIVATE_EXPORT String : public Managed { void makeIdentifierImpl(ExecutionEngine *e) const; - static uint createHashValue(const QChar *ch, int length); - static uint createHashValue(const char *ch, int length); + static uint createHashValue(const QChar *ch, int length, uint *subtype) + { + const QChar *end = ch + length; + return calculateHashValue(ch, end, subtype); + } + + static uint createHashValue(const char *ch, int length, uint *subtype) + { + const char *end = ch + length; + return calculateHashValue(ch, end, subtype); + } bool startsWithUpper() const { const String::Data *l = d(); @@ -203,6 +215,54 @@ protected: public: static uint toArrayIndex(const QString &str); + +private: + static inline uint toUInt(const QChar *ch) { return ch->unicode(); } + static inline uint toUInt(const char *ch) { return *ch; } + + template <typename T> + static inline uint toArrayIndex(const T *ch, const T *end) + { + uint i = toUInt(ch) - '0'; + if (i > 9) + return UINT_MAX; + ++ch; + // reject "01", "001", ... + if (i == 0 && ch != end) + return UINT_MAX; + + while (ch < end) { + uint x = toUInt(ch) - '0'; + if (x > 9) + return UINT_MAX; + if (mul_overflow(i, uint(10), &i) || add_overflow(i, x, &i)) // i = i * 10 + x + return UINT_MAX; + ++ch; + } + return i; + } + +public: + template <typename T> + static inline uint calculateHashValue(const T *ch, const T* end, uint *subtype) + { + // array indices get their number as hash value + uint h = toArrayIndex(ch, end); + if (h != UINT_MAX) { + if (subtype) + *subtype = Heap::String::StringType_ArrayIndex; + return h; + } + + while (ch < end) { + h = 31 * h + toUInt(ch); + ++ch; + } + + if (subtype) + *subtype = Heap::String::StringType_Regular; + return h; + } }; template<> diff --git a/src/qml/jsruntime/qv4stringobject.cpp b/src/qml/jsruntime/qv4stringobject.cpp index b874766655..829ada0c1a 100644 --- a/src/qml/jsruntime/qv4stringobject.cpp +++ b/src/qml/jsruntime/qv4stringobject.cpp @@ -73,15 +73,17 @@ using namespace QV4; DEFINE_OBJECT_VTABLE(StringObject); -Heap::StringObject::StringObject() +void Heap::StringObject::init() { + Object::init(); Q_ASSERT(vtable() == QV4::StringObject::staticVTable()); string = internalClass->engine->id_empty()->d(); *propertyData(LengthPropertyIndex) = Primitive::fromInt32(0); } -Heap::StringObject::StringObject(const QV4::String *str) +void Heap::StringObject::init(const QV4::String *str) { + Object::init(); string = str->d(); *propertyData(LengthPropertyIndex) = Primitive::fromInt32(length()); } @@ -152,33 +154,29 @@ void StringObject::markObjects(Heap::Base *that, ExecutionEngine *e) DEFINE_OBJECT_VTABLE(StringCtor); -Heap::StringCtor::StringCtor(QV4::ExecutionContext *scope) - : Heap::FunctionObject(scope, QStringLiteral("String")) +void Heap::StringCtor::init(QV4::ExecutionContext *scope) { + Heap::FunctionObject::init(scope, QStringLiteral("String")); } -ReturnedValue StringCtor::construct(const Managed *m, CallData *callData) +void StringCtor::construct(const Managed *m, Scope &scope, CallData *callData) { ExecutionEngine *v4 = static_cast<const Object *>(m)->engine(); - Scope scope(v4); ScopedString value(scope); if (callData->argc) value = callData->args[0].toString(v4); else value = v4->newString(); - return Encode(v4->newStringObject(value)); + scope.result = Encode(v4->newStringObject(value)); } -ReturnedValue StringCtor::call(const Managed *m, CallData *callData) +void StringCtor::call(const Managed *, Scope &scope, CallData *callData) { - ExecutionEngine *v4 = static_cast<const Object *>(m)->engine(); - Scope scope(v4); - ScopedValue value(scope); + ExecutionEngine *v4 = scope.engine; if (callData->argc) - value = callData->args[0].toString(v4); + scope.result = callData->args[0].toString(v4); else - value = v4->newString(); - return value->asReturnedValue(); + scope.result = v4->newString(); } void StringPrototype::init(ExecutionEngine *engine, Object *ctor) @@ -196,7 +194,9 @@ void StringPrototype::init(ExecutionEngine *engine, Object *ctor) defineDefaultProperty(QStringLiteral("charAt"), method_charAt, 1); defineDefaultProperty(QStringLiteral("charCodeAt"), method_charCodeAt, 1); defineDefaultProperty(QStringLiteral("concat"), method_concat, 1); + defineDefaultProperty(QStringLiteral("endsWith"), method_endsWith, 1); defineDefaultProperty(QStringLiteral("indexOf"), method_indexOf, 1); + defineDefaultProperty(QStringLiteral("includes"), method_includes, 1); defineDefaultProperty(QStringLiteral("lastIndexOf"), method_lastIndexOf, 1); defineDefaultProperty(QStringLiteral("localeCompare"), method_localeCompare, 1); defineDefaultProperty(QStringLiteral("match"), method_match, 1); @@ -204,6 +204,7 @@ void StringPrototype::init(ExecutionEngine *engine, Object *ctor) defineDefaultProperty(QStringLiteral("search"), method_search, 1); defineDefaultProperty(QStringLiteral("slice"), method_slice, 2); defineDefaultProperty(QStringLiteral("split"), method_split, 2); + defineDefaultProperty(QStringLiteral("startsWith"), method_startsWith, 1); defineDefaultProperty(QStringLiteral("substr"), method_substr, 2); defineDefaultProperty(QStringLiteral("substring"), method_substring, 2); defineDefaultProperty(QStringLiteral("toLowerCase"), method_toLowerCase); @@ -293,6 +294,30 @@ ReturnedValue StringPrototype::method_concat(CallContext *context) return context->d()->engine->newString(value)->asReturnedValue(); } +ReturnedValue StringPrototype::method_endsWith(CallContext *context) +{ + QString value = getThisString(context); + if (context->d()->engine->hasException) + return Encode::undefined(); + + QString searchString; + if (context->argc()) { + if (context->args()[0].as<RegExpObject>()) + return context->engine()->throwTypeError(); + searchString = context->args()[0].toQString(); + } + + int pos = value.length(); + if (context->argc() > 1) + pos = (int) context->args()[1].toInteger(); + + if (pos == value.length()) + return Encode(value.endsWith(searchString)); + + QStringRef stringToSearch = value.leftRef(pos); + return Encode(stringToSearch.endsWith(searchString)); +} + ReturnedValue StringPrototype::method_indexOf(CallContext *context) { QString value = getThisString(context); @@ -314,6 +339,35 @@ ReturnedValue StringPrototype::method_indexOf(CallContext *context) return Encode(index); } +ReturnedValue StringPrototype::method_includes(CallContext *context) +{ + QString value = getThisString(context); + if (context->d()->engine->hasException) + return Encode::undefined(); + + QString searchString; + if (context->argc()) { + if (context->args()[0].as<RegExpObject>()) + return context->engine()->throwTypeError(); + searchString = context->args()[0].toQString(); + } + + int pos = 0; + if (context->argc() > 1) { + Scope scope(context); + ScopedValue posArg(scope, context->argument(1)); + pos = (int) posArg->toInteger(); + if (!posArg->isInteger() && posArg->isNumber() && qIsInf(posArg->toNumber())) + pos = value.length(); + } + + if (pos == 0) + return Encode(value.contains(searchString)); + + QStringRef stringToSearch = value.midRef(pos); + return Encode(stringToSearch.contains(searchString)); +} + ReturnedValue StringPrototype::method_lastIndexOf(CallContext *context) { Scope scope(context); @@ -367,7 +421,8 @@ ReturnedValue StringPrototype::method_match(CallContext *context) if (!rx) { ScopedCallData callData(scope, 1); callData->args[0] = regexp; - rx = context->d()->engine->regExpCtor()->construct(callData); + context->d()->engine->regExpCtor()->construct(scope, callData); + rx = scope.result.asReturnedValue(); } if (!rx) @@ -383,8 +438,10 @@ ReturnedValue StringPrototype::method_match(CallContext *context) ScopedCallData callData(scope, 1); callData->thisObject = rx; callData->args[0] = s; - if (!global) - return exec->call(callData); + if (!global) { + exec->call(scope, callData); + return scope.result.asReturnedValue(); + } ScopedString lastIndex(scope, context->d()->engine->newString(QStringLiteral("lastIndex"))); rx->put(lastIndex, ScopedValue(scope, Primitive::fromInt32(0))); @@ -392,14 +449,13 @@ ReturnedValue StringPrototype::method_match(CallContext *context) double previousLastIndex = 0; uint n = 0; - ScopedValue result(scope); ScopedValue matchStr(scope); ScopedValue index(scope); while (1) { - result = exec->call(callData); - if (result->isNull()) + exec->call(scope, callData); + if (scope.result.isNull()) break; - assert(result->isObject()); + assert(scope.result.isObject()); index = rx->get(lastIndex, 0); double thisIndex = index->toInteger(); if (previousLastIndex == thisIndex) { @@ -408,7 +464,7 @@ ReturnedValue StringPrototype::method_match(CallContext *context) } else { previousLastIndex = thisIndex; } - matchStr = result->objectValue()->getIndexed(0); + matchStr = scope.result.objectValue()->getIndexed(0); a->arraySet(n, matchStr); ++n; } @@ -524,7 +580,6 @@ ReturnedValue StringPrototype::method_replace(CallContext *ctx) } QString result; - ScopedValue replacement(scope); ScopedValue replaceValue(scope, ctx->argument(1)); ScopedFunctionObject searchCallback(scope, replaceValue); if (!!searchCallback) { @@ -549,9 +604,9 @@ ReturnedValue StringPrototype::method_replace(CallContext *ctx) callData->args[numCaptures] = Primitive::fromUInt32(matchStart); callData->args[numCaptures + 1] = ctx->d()->engine->newString(string); - replacement = searchCallback->call(callData); + searchCallback->call(scope, callData); result += string.midRef(lastEnd, matchStart - lastEnd); - result += replacement->toQString(); + result += scope.result.toQString(); lastEnd = matchEnd; } result += string.midRef(lastEnd); @@ -584,17 +639,17 @@ ReturnedValue StringPrototype::method_search(CallContext *ctx) { Scope scope(ctx); QString string = getThisString(ctx); - ScopedValue regExpValue(scope, ctx->argument(0)); + scope.result = ctx->argument(0); if (scope.engine->hasException) return Encode::undefined(); - Scoped<RegExpObject> regExp(scope, regExpValue->as<RegExpObject>()); + Scoped<RegExpObject> regExp(scope, scope.result.as<RegExpObject>()); if (!regExp) { ScopedCallData callData(scope, 1); - callData->args[0] = regExpValue; - regExpValue = ctx->d()->engine->regExpCtor()->construct(callData); + callData->args[0] = scope.result; + ctx->d()->engine->regExpCtor()->construct(scope, callData); if (scope.engine->hasException) return Encode::undefined(); - regExp = regExpValue->as<RegExpObject>(); + regExp = scope.result.as<RegExpObject>(); Q_ASSERT(regExp); } Scoped<RegExp> re(scope, regExp->value()); @@ -662,7 +717,7 @@ ReturnedValue StringPrototype::method_split(CallContext *ctx) Scoped<RegExpObject> re(scope, separatorValue); if (re) { - if (re->value()->pattern.isEmpty()) { + if (re->value()->pattern->isEmpty()) { re = (RegExpObject *)0; separatorValue = ctx->d()->engine->newString(); } @@ -716,6 +771,30 @@ ReturnedValue StringPrototype::method_split(CallContext *ctx) return array.asReturnedValue(); } +ReturnedValue StringPrototype::method_startsWith(CallContext *context) +{ + QString value = getThisString(context); + if (context->d()->engine->hasException) + return Encode::undefined(); + + QString searchString; + if (context->argc()) { + if (context->args()[0].as<RegExpObject>()) + return context->engine()->throwTypeError(); + searchString = context->args()[0].toQString(); + } + + int pos = 0; + if (context->argc() > 1) + pos = (int) context->args()[1].toInteger(); + + if (pos == 0) + return Encode(value.startsWith(searchString)); + + QStringRef stringToSearch = value.midRef(pos); + return Encode(stringToSearch.startsWith(searchString)); +} + ReturnedValue StringPrototype::method_substr(CallContext *context) { const QString value = getThisString(context); diff --git a/src/qml/jsruntime/qv4stringobject_p.h b/src/qml/jsruntime/qv4stringobject_p.h index 3930a011e6..b9f9d44fe8 100644 --- a/src/qml/jsruntime/qv4stringobject_p.h +++ b/src/qml/jsruntime/qv4stringobject_p.h @@ -65,8 +65,8 @@ struct StringObject : Object { LengthPropertyIndex = 0 }; - StringObject(); - StringObject(const QV4::String *string); + void init(); + void init(const QV4::String *string); String *string; Heap::String *getIndex(uint index) const; @@ -74,7 +74,7 @@ struct StringObject : Object { }; struct StringCtor : FunctionObject { - StringCtor(QV4::ExecutionContext *scope); + void init(QV4::ExecutionContext *scope); }; } @@ -103,8 +103,8 @@ struct StringCtor: FunctionObject { V4_OBJECT2(StringCtor, FunctionObject) - static ReturnedValue construct(const Managed *m, CallData *callData); - static ReturnedValue call(const Managed *that, CallData *callData); + static void construct(const Managed *m, Scope &scope, CallData *callData); + static void call(const Managed *, Scope &scope, CallData *callData); }; struct StringPrototype: StringObject @@ -115,7 +115,9 @@ struct StringPrototype: StringObject static ReturnedValue method_charAt(CallContext *context); static ReturnedValue method_charCodeAt(CallContext *context); static ReturnedValue method_concat(CallContext *context); + static ReturnedValue method_endsWith(CallContext *ctx); static ReturnedValue method_indexOf(CallContext *context); + static ReturnedValue method_includes(CallContext *context); static ReturnedValue method_lastIndexOf(CallContext *context); static ReturnedValue method_localeCompare(CallContext *context); static ReturnedValue method_match(CallContext *context); @@ -123,6 +125,7 @@ struct StringPrototype: StringObject static ReturnedValue method_search(CallContext *ctx); static ReturnedValue method_slice(CallContext *ctx); static ReturnedValue method_split(CallContext *ctx); + static ReturnedValue method_startsWith(CallContext *ctx); static ReturnedValue method_substr(CallContext *context); static ReturnedValue method_substring(CallContext *context); static ReturnedValue method_toLowerCase(CallContext *ctx); diff --git a/src/qml/jsruntime/qv4typedarray.cpp b/src/qml/jsruntime/qv4typedarray.cpp index c86f252353..009c573bf8 100644 --- a/src/qml/jsruntime/qv4typedarray.cpp +++ b/src/qml/jsruntime/qv4typedarray.cpp @@ -202,36 +202,40 @@ const TypedArrayOperations operations[Heap::TypedArray::NTypes] = { }; -Heap::TypedArrayCtor::TypedArrayCtor(QV4::ExecutionContext *scope, TypedArray::Type t) - : Heap::FunctionObject(scope, QLatin1String(operations[t].name)) - , type(t) +void Heap::TypedArrayCtor::init(QV4::ExecutionContext *scope, TypedArray::Type t) { + Heap::FunctionObject::init(scope, QLatin1String(operations[t].name)); + type = t; } -ReturnedValue TypedArrayCtor::construct(const Managed *m, CallData *callData) +void TypedArrayCtor::construct(const Managed *m, Scope &scope, CallData *callData) { - Scope scope(static_cast<const Object *>(m)->engine()); Scoped<TypedArrayCtor> that(scope, static_cast<const TypedArrayCtor *>(m)); if (!callData->argc || !callData->args[0].isObject()) { // ECMA 6 22.2.1.1 double l = callData->argc ? callData->args[0].toNumber() : 0; - if (scope.engine->hasException) - return Encode::undefined(); + if (scope.engine->hasException) { + scope.result = Encode::undefined(); + return; + } uint len = (uint)l; if (l != len) scope.engine->throwRangeError(QStringLiteral("Non integer length for typed array.")); uint byteLength = len * operations[that->d()->type].bytesPerElement; Scoped<ArrayBuffer> buffer(scope, scope.engine->newArrayBuffer(byteLength)); - if (scope.engine->hasException) - return Encode::undefined(); + if (scope.engine->hasException) { + scope.result = Encode::undefined(); + return; + } Scoped<TypedArray > array(scope, TypedArray::create(scope.engine, that->d()->type)); array->d()->buffer = buffer->d(); array->d()->byteLength = byteLength; array->d()->byteOffset = 0; - return array.asReturnedValue(); + scope.result = array.asReturnedValue(); + return; } Scoped<TypedArray> typedArray(scope, callData->argument(0)); if (!!typedArray) { @@ -243,8 +247,10 @@ ReturnedValue TypedArrayCtor::construct(const Managed *m, CallData *callData) uint destByteLength = byteLength*destElementSize/srcElementSize; Scoped<ArrayBuffer> newBuffer(scope, scope.engine->newArrayBuffer(destByteLength)); - if (scope.engine->hasException) - return Encode::undefined(); + if (scope.engine->hasException) { + scope.result = Encode::undefined(); + return; + } Scoped<TypedArray > array(scope, TypedArray::create(scope.engine, that->d()->type)); array->d()->buffer = newBuffer->d(); @@ -269,7 +275,8 @@ ReturnedValue TypedArrayCtor::construct(const Managed *m, CallData *callData) } } - return array.asReturnedValue(); + scope.result = array.asReturnedValue(); + return; } Scoped<ArrayBuffer> buffer(scope, callData->argument(0)); if (!!buffer) { @@ -278,21 +285,29 @@ ReturnedValue TypedArrayCtor::construct(const Managed *m, CallData *callData) double dbyteOffset = callData->argc > 1 ? callData->args[1].toInteger() : 0; uint byteOffset = (uint)dbyteOffset; uint elementSize = operations[that->d()->type].bytesPerElement; - if (dbyteOffset < 0 || (byteOffset % elementSize) || dbyteOffset > buffer->byteLength()) - return scope.engine->throwRangeError(QStringLiteral("new TypedArray: invalid byteOffset")); + if (dbyteOffset < 0 || (byteOffset % elementSize) || dbyteOffset > buffer->byteLength()) { + scope.result = scope.engine->throwRangeError(QStringLiteral("new TypedArray: invalid byteOffset")); + return; + } uint byteLength; if (callData->argc < 3 || callData->args[2].isUndefined()) { byteLength = buffer->byteLength() - byteOffset; - if (buffer->byteLength() < byteOffset || byteLength % elementSize) - return scope.engine->throwRangeError(QStringLiteral("new TypedArray: invalid length")); + if (buffer->byteLength() < byteOffset || byteLength % elementSize) { + scope.result = scope.engine->throwRangeError(QStringLiteral("new TypedArray: invalid length")); + return; + } } else { double l = qBound(0., callData->args[2].toInteger(), (double)UINT_MAX); - if (scope.engine->hasException) - return Encode::undefined(); + if (scope.engine->hasException) { + scope.result = Encode::undefined(); + return; + } l *= elementSize; - if (buffer->byteLength() - byteOffset < l) - return scope.engine->throwRangeError(QStringLiteral("new TypedArray: invalid length")); + if (buffer->byteLength() - byteOffset < l) { + scope.result = scope.engine->throwRangeError(QStringLiteral("new TypedArray: invalid length")); + return; + } byteLength = (uint)l; } @@ -300,20 +315,25 @@ ReturnedValue TypedArrayCtor::construct(const Managed *m, CallData *callData) array->d()->buffer = buffer->d(); array->d()->byteLength = byteLength; array->d()->byteOffset = byteOffset; - return array.asReturnedValue(); + scope.result = array.asReturnedValue(); + return; } // ECMA 6 22.2.1.3 ScopedObject o(scope, callData->argument(0)); uint l = (uint) qBound(0., ScopedValue(scope, o->get(scope.engine->id_length()))->toInteger(), (double)UINT_MAX); - if (scope.engine->hasException) - return scope.engine->throwTypeError(); + if (scope.engine->hasException) { + scope.result = scope.engine->throwTypeError(); + return; + } uint elementSize = operations[that->d()->type].bytesPerElement; Scoped<ArrayBuffer> newBuffer(scope, scope.engine->newArrayBuffer(l * elementSize)); - if (scope.engine->hasException) - return Encode::undefined(); + if (scope.engine->hasException) { + scope.result = Encode::undefined(); + return; + } Scoped<TypedArray > array(scope, TypedArray::create(scope.engine, that->d()->type)); array->d()->buffer = newBuffer->d(); @@ -326,25 +346,28 @@ ReturnedValue TypedArrayCtor::construct(const Managed *m, CallData *callData) while (idx < l) { val = o->getIndexed(idx); array->d()->type->write(scope.engine, b, 0, val); - if (scope.engine->hasException) - return Encode::undefined(); + if (scope.engine->hasException) { + scope.result = Encode::undefined(); + return; + } ++idx; b += elementSize; } - return array.asReturnedValue(); + scope.result = array.asReturnedValue(); } -ReturnedValue TypedArrayCtor::call(const Managed *that, CallData *callData) +void TypedArrayCtor::call(const Managed *that, Scope &scope, CallData *callData) { - return construct(that, callData); + construct(that, scope, callData); } -Heap::TypedArray::TypedArray(Type t) - : type(operations + t), - arrayType(t) +void Heap::TypedArray::init(Type t) { + Object::init(); + type = operations + t; + arrayType = t; } Heap::TypedArray *TypedArray::create(ExecutionEngine *e, Heap::TypedArray::Type t) @@ -582,5 +605,6 @@ ReturnedValue TypedArrayPrototype::method_subarray(CallContext *ctx) callData->args[0] = buffer; callData->args[1] = Encode(a->d()->byteOffset + begin*a->d()->type->bytesPerElement); callData->args[2] = Encode(newLen); - return constructor->construct(callData); + constructor->construct(scope, callData); + return scope.result.asReturnedValue(); } diff --git a/src/qml/jsruntime/qv4typedarray_p.h b/src/qml/jsruntime/qv4typedarray_p.h index 757273e4ed..0112d2e4a1 100644 --- a/src/qml/jsruntime/qv4typedarray_p.h +++ b/src/qml/jsruntime/qv4typedarray_p.h @@ -86,7 +86,7 @@ struct TypedArray : Object { NTypes }; - TypedArray(Type t); + void init(Type t); const TypedArrayOperations *type; Pointer<ArrayBuffer> buffer; @@ -96,13 +96,13 @@ struct TypedArray : Object { }; struct TypedArrayCtor : FunctionObject { - TypedArrayCtor(QV4::ExecutionContext *scope, TypedArray::Type t); + void init(QV4::ExecutionContext *scope, TypedArray::Type t); TypedArray::Type type; }; struct TypedArrayPrototype : Object { - inline TypedArrayPrototype(TypedArray::Type t); + inline void init(TypedArray::Type t); TypedArray::Type type; }; @@ -140,8 +140,8 @@ struct TypedArrayCtor: FunctionObject { V4_OBJECT2(TypedArrayCtor, FunctionObject) - static ReturnedValue construct(const Managed *m, CallData *callData); - static ReturnedValue call(const Managed *that, CallData *callData); + static void construct(const Managed *m, Scope &scope, CallData *callData); + static void call(const Managed *that, Scope &scope, CallData *callData); }; @@ -160,10 +160,11 @@ struct TypedArrayPrototype : Object static ReturnedValue method_subarray(CallContext *ctx); }; -inline -Heap::TypedArrayPrototype::TypedArrayPrototype(TypedArray::Type t) - : type(t) +inline void +Heap::TypedArrayPrototype::init(TypedArray::Type t) { + Object::init(); + type = t; } } // namespace QV4 diff --git a/src/qml/jsruntime/qv4value_p.h b/src/qml/jsruntime/qv4value_p.h index 9eee34aff2..6d5cff4ecc 100644 --- a/src/qml/jsruntime/qv4value_p.h +++ b/src/qml/jsruntime/qv4value_p.h @@ -372,16 +372,22 @@ public: } Q_ALWAYS_INLINE String *stringValue() const { + if (!isString()) + return nullptr; return m() ? reinterpret_cast<String*>(const_cast<Value *>(this)) : 0; } Q_ALWAYS_INLINE Object *objectValue() const { + if (!isObject()) + return nullptr; return m() ? reinterpret_cast<Object*>(const_cast<Value *>(this)) : 0; } Q_ALWAYS_INLINE Managed *managed() const { + if (!isManaged()) + return nullptr; return m() ? reinterpret_cast<Managed*>(const_cast<Value *>(this)) : 0; } Q_ALWAYS_INLINE Heap::Base *heapObject() const { - return m(); + return isManaged() ? m() : nullptr; } static inline Value fromHeapObject(Heap::Base *m) @@ -431,7 +437,10 @@ public: } template <typename T> T *as() { - return const_cast<T *>(const_cast<const Value *>(this)->as<T>()); + if (isManaged()) + return const_cast<T *>(const_cast<const Value *>(this)->as<T>()); + else + return nullptr; } template<typename T> inline T *cast() { @@ -471,11 +480,8 @@ public: template<typename T> Value &operator=(const Scoped<T> &t); - Value &operator=(const Value &v) { - _val = v._val; - return *this; - } }; +V4_ASSERT_IS_TRIVIAL(Value) inline bool Value::isString() const { diff --git a/src/qml/jsruntime/qv4variantobject.cpp b/src/qml/jsruntime/qv4variantobject.cpp index 444c0a37e0..b26dd27913 100644 --- a/src/qml/jsruntime/qv4variantobject.cpp +++ b/src/qml/jsruntime/qv4variantobject.cpp @@ -50,20 +50,23 @@ using namespace QV4; DEFINE_OBJECT_VTABLE(VariantObject); -Heap::VariantObject::VariantObject() +void Heap::VariantObject::init() { + Object::init(); + scarceData = new ExecutionEngine::ScarceResourceData; } -Heap::VariantObject::VariantObject(const QVariant &value) +void Heap::VariantObject::init(const QVariant &value) { - data = value; + Object::init(); + scarceData = new ExecutionEngine::ScarceResourceData(value); if (isScarce()) - internalClass->engine->scarceResources.insert(this); + removeVmePropertyReference(); } bool VariantObject::Data::isScarce() const { - QVariant::Type t = data.type(); + QVariant::Type t = data().type(); return t == QVariant::Pixmap || t == QVariant::Image; } @@ -73,10 +76,10 @@ bool VariantObject::isEqualTo(Managed *m, Managed *other) QV4::VariantObject *lv = static_cast<QV4::VariantObject *>(m); if (QV4::VariantObject *rv = other->as<QV4::VariantObject>()) - return lv->d()->data == rv->d()->data; + return lv->d()->data() == rv->d()->data(); if (QV4::QQmlValueTypeWrapper *v = other->as<QQmlValueTypeWrapper>()) - return v->isEqual(lv->d()->data); + return v->isEqual(lv->d()->data()); return false; } @@ -87,7 +90,7 @@ void VariantObject::addVmePropertyReference() // remove from the ep->scarceResources list // since it is now no longer eligible to be // released automatically by the engine. - d()->node.remove(); + d()->addVmePropertyReference(); } } @@ -97,7 +100,7 @@ void VariantObject::removeVmePropertyReference() // and add to the ep->scarceResources list // since it is now eligible to be released // automatically by the engine. - internalClass()->engine->scarceResources.insert(d()); + d()->removeVmePropertyReference(); } } @@ -115,7 +118,7 @@ QV4::ReturnedValue VariantPrototype::method_preserve(CallContext *ctx) Scope scope(ctx); Scoped<VariantObject> o(scope, ctx->thisObject().as<QV4::VariantObject>()); if (o && o->d()->isScarce()) - o->d()->node.remove(); + o->d()->addVmePropertyReference(); return Encode::undefined(); } @@ -125,8 +128,8 @@ QV4::ReturnedValue VariantPrototype::method_destroy(CallContext *ctx) Scoped<VariantObject> o(scope, ctx->thisObject().as<QV4::VariantObject>()); if (o) { if (o->d()->isScarce()) - o->d()->node.remove(); - o->d()->data = QVariant(); + o->d()->addVmePropertyReference(); + o->d()->data() = QVariant(); } return Encode::undefined(); } @@ -137,9 +140,9 @@ QV4::ReturnedValue VariantPrototype::method_toString(CallContext *ctx) Scoped<VariantObject> o(scope, ctx->thisObject().as<QV4::VariantObject>()); if (!o) return Encode::undefined(); - QString result = o->d()->data.toString(); - if (result.isEmpty() && !o->d()->data.canConvert(QVariant::String)) - result = QStringLiteral("QVariant(%0)").arg(QString::fromLatin1(o->d()->data.typeName())); + QString result = o->d()->data().toString(); + if (result.isEmpty() && !o->d()->data().canConvert(QVariant::String)) + result = QStringLiteral("QVariant(%0)").arg(QString::fromLatin1(o->d()->data().typeName())); return Encode(ctx->d()->engine->newString(result)); } @@ -148,7 +151,7 @@ QV4::ReturnedValue VariantPrototype::method_valueOf(CallContext *ctx) Scope scope(ctx); Scoped<VariantObject> o(scope, ctx->thisObject().as<QV4::VariantObject>()); if (o) { - QVariant v = o->d()->data; + QVariant v = o->d()->data(); switch (v.type()) { case QVariant::Invalid: return Encode::undefined(); diff --git a/src/qml/jsruntime/qv4variantobject_p.h b/src/qml/jsruntime/qv4variantobject_p.h index e50706ef94..9a04069c12 100644 --- a/src/qml/jsruntime/qv4variantobject_p.h +++ b/src/qml/jsruntime/qv4variantobject_p.h @@ -64,16 +64,28 @@ namespace QV4 { namespace Heap { -struct VariantObject : Object, public ExecutionEngine::ScarceResourceData +struct VariantObject : Object { - VariantObject(); - VariantObject(const QVariant &value); - ~VariantObject() { + void init(); + void init(const QVariant &value); + void destroy() { + Q_ASSERT(scarceData); if (isScarce()) - node.remove(); + addVmePropertyReference(); + delete scarceData; + Object::destroy(); } bool isScarce() const; int vmePropertyReferenceCount; + + const QVariant &data() const { return scarceData->data; } + QVariant &data() { return scarceData->data; } + + void addVmePropertyReference() { scarceData->node.remove(); } + void removeVmePropertyReference() { internalClass->engine->scarceResources.insert(scarceData); } + +private: + ExecutionEngine::ScarceResourceData *scarceData; }; } diff --git a/src/qml/jsruntime/qv4vme_moth.cpp b/src/qml/jsruntime/qv4vme_moth.cpp index fbb26dc571..0f7f6b1f75 100644 --- a/src/qml/jsruntime/qv4vme_moth.cpp +++ b/src/qml/jsruntime/qv4vme_moth.cpp @@ -142,6 +142,7 @@ Q_QML_EXPORT int qt_v4DebuggerHook(const char *json); } // extern "C" +#ifndef QT_NO_QML_DEBUGGER static int qt_v4BreakpointCount = 0; static bool qt_v4IsDebugging = true; static bool qt_v4IsStepping = false; @@ -203,7 +204,7 @@ int qt_v4DebuggerHook(const char *json) QJsonDocument doc = QJsonDocument::fromJson(json); QJsonObject ob = doc.object(); - QByteArray command = ob.value(QStringLiteral("command")).toString().toUtf8(); + QByteArray command = ob.value(QLatin1String("command")).toString().toUtf8(); if (command == "protocolVersion") { return ProtocolVersion; // Version number. @@ -217,17 +218,17 @@ int qt_v4DebuggerHook(const char *json) if (command == "insertBreakpoint") { Breakpoint bp; bp.bpNumber = ++qt_v4BreakpointCount; - bp.lineNumber = ob.value(QStringLiteral("lineNumber")).toString().toInt(); - bp.engineName = ob.value(QStringLiteral("engineName")).toString(); - bp.fullName = ob.value(QStringLiteral("fullName")).toString(); - bp.condition = ob.value(QStringLiteral("condition")).toString(); + bp.lineNumber = ob.value(QLatin1String("lineNumber")).toString().toInt(); + bp.engineName = ob.value(QLatin1String("engineName")).toString(); + bp.fullName = ob.value(QLatin1String("fullName")).toString(); + bp.condition = ob.value(QLatin1String("condition")).toString(); qt_v4Breakpoints.append(bp); return bp.bpNumber; } if (command == "removeBreakpoint") { - int lineNumber = ob.value(QStringLiteral("lineNumber")).toString().toInt(); - QString fullName = ob.value(QStringLiteral("fullName")).toString(); + int lineNumber = ob.value(QLatin1String("lineNumber")).toString().toInt(); + QString fullName = ob.value(QLatin1String("fullName")).toString(); if (qt_v4Breakpoints.last().matches(fullName, lineNumber)) { qt_v4Breakpoints.removeLast(); return Success; @@ -285,6 +286,7 @@ static void qt_v4CheckForBreak(QV4::ExecutionContext *context, QV4::Value **scop } } +#endif // QT_NO_QML_DEBUGGER // End of debugger interface using namespace QV4; @@ -400,7 +402,7 @@ QV4::ReturnedValue VME::run(ExecutionEngine *engine, const uchar *code QV4::Value **scopes = static_cast<QV4::Value **>(alloca(sizeof(QV4::Value *)*(2 + 2*scopeDepth))); { - scopes[0] = const_cast<QV4::Value *>(context->d()->compilationUnit->data->constants()); + scopes[0] = const_cast<QV4::Value *>(context->d()->compilationUnit->constants); // stack gets setup in push instruction scopes[1] = 0; QV4::Heap::ExecutionContext *scope = context->d(); @@ -451,12 +453,12 @@ QV4::ReturnedValue VME::run(ExecutionEngine *engine, const uchar *code MOTH_END_INSTR(LoadRegExp) MOTH_BEGIN_INSTR(LoadClosure) - STOREVALUE(instr.result, Runtime::closure(engine, instr.value)); + STOREVALUE(instr.result, engine->runtime.closure(engine, instr.value)); MOTH_END_INSTR(LoadClosure) MOTH_BEGIN_INSTR(LoadName) TRACE(inline, "property name = %s", runtimeStrings[instr.name]->toQString().toUtf8().constData()); - STOREVALUE(instr.result, Runtime::getActivationProperty(engine, instr.name)); + STOREVALUE(instr.result, engine->runtime.getActivationProperty(engine, instr.name)); MOTH_END_INSTR(LoadName) MOTH_BEGIN_INSTR(GetGlobalLookup) @@ -466,12 +468,12 @@ QV4::ReturnedValue VME::run(ExecutionEngine *engine, const uchar *code MOTH_BEGIN_INSTR(StoreName) TRACE(inline, "property name = %s", runtimeStrings[instr.name]->toQString().toUtf8().constData()); - Runtime::setActivationProperty(engine, instr.name, VALUE(instr.source)); + engine->runtime.setActivationProperty(engine, instr.name, VALUE(instr.source)); CHECK_EXCEPTION; MOTH_END_INSTR(StoreName) MOTH_BEGIN_INSTR(LoadElement) - STOREVALUE(instr.result, Runtime::getElement(engine, VALUE(instr.base), VALUE(instr.index))); + STOREVALUE(instr.result, engine->runtime.getElement(engine, VALUE(instr.base), VALUE(instr.index))); MOTH_END_INSTR(LoadElement) MOTH_BEGIN_INSTR(LoadElementLookup) @@ -480,7 +482,7 @@ QV4::ReturnedValue VME::run(ExecutionEngine *engine, const uchar *code MOTH_END_INSTR(LoadElementLookup) MOTH_BEGIN_INSTR(StoreElement) - Runtime::setElement(engine, VALUE(instr.base), VALUE(instr.index), VALUE(instr.source)); + engine->runtime.setElement(engine, VALUE(instr.base), VALUE(instr.index), VALUE(instr.source)); CHECK_EXCEPTION; MOTH_END_INSTR(StoreElement) @@ -491,7 +493,7 @@ QV4::ReturnedValue VME::run(ExecutionEngine *engine, const uchar *code MOTH_END_INSTR(StoreElementLookup) MOTH_BEGIN_INSTR(LoadProperty) - STOREVALUE(instr.result, Runtime::getProperty(engine, VALUE(instr.base), instr.name)); + STOREVALUE(instr.result, engine->runtime.getProperty(engine, VALUE(instr.base), instr.name)); MOTH_END_INSTR(LoadProperty) MOTH_BEGIN_INSTR(GetLookup) @@ -500,7 +502,7 @@ QV4::ReturnedValue VME::run(ExecutionEngine *engine, const uchar *code MOTH_END_INSTR(GetLookup) MOTH_BEGIN_INSTR(StoreProperty) - Runtime::setProperty(engine, VALUE(instr.base), instr.name, VALUE(instr.source)); + engine->runtime.setProperty(engine, VALUE(instr.base), instr.name, VALUE(instr.source)); CHECK_EXCEPTION; MOTH_END_INSTR(StoreProperty) @@ -511,42 +513,42 @@ QV4::ReturnedValue VME::run(ExecutionEngine *engine, const uchar *code MOTH_END_INSTR(SetLookup) MOTH_BEGIN_INSTR(StoreQObjectProperty) - Runtime::setQmlQObjectProperty(engine, VALUE(instr.base), instr.propertyIndex, VALUE(instr.source)); + engine->runtime.setQmlQObjectProperty(engine, VALUE(instr.base), instr.propertyIndex, VALUE(instr.source)); CHECK_EXCEPTION; MOTH_END_INSTR(StoreQObjectProperty) MOTH_BEGIN_INSTR(LoadQObjectProperty) - STOREVALUE(instr.result, Runtime::getQmlQObjectProperty(engine, VALUE(instr.base), instr.propertyIndex, instr.captureRequired)); + STOREVALUE(instr.result, engine->runtime.getQmlQObjectProperty(engine, VALUE(instr.base), instr.propertyIndex, instr.captureRequired)); MOTH_END_INSTR(LoadQObjectProperty) MOTH_BEGIN_INSTR(StoreScopeObjectProperty) - Runtime::setQmlScopeObjectProperty(engine, VALUE(instr.base), instr.propertyIndex, VALUE(instr.source)); + engine->runtime.setQmlScopeObjectProperty(engine, VALUE(instr.base), instr.propertyIndex, VALUE(instr.source)); CHECK_EXCEPTION; MOTH_END_INSTR(StoreScopeObjectProperty) MOTH_BEGIN_INSTR(LoadScopeObjectProperty) - STOREVALUE(instr.result, Runtime::getQmlScopeObjectProperty(engine, VALUE(instr.base), instr.propertyIndex)); + STOREVALUE(instr.result, engine->runtime.getQmlScopeObjectProperty(engine, VALUE(instr.base), instr.propertyIndex, instr.captureRequired)); MOTH_END_INSTR(LoadScopeObjectProperty) MOTH_BEGIN_INSTR(StoreContextObjectProperty) - Runtime::setQmlContextObjectProperty(engine, VALUE(instr.base), instr.propertyIndex, VALUE(instr.source)); + engine->runtime.setQmlContextObjectProperty(engine, VALUE(instr.base), instr.propertyIndex, VALUE(instr.source)); CHECK_EXCEPTION; MOTH_END_INSTR(StoreContextObjectProperty) MOTH_BEGIN_INSTR(LoadContextObjectProperty) - STOREVALUE(instr.result, Runtime::getQmlContextObjectProperty(engine, VALUE(instr.base), instr.propertyIndex)); + STOREVALUE(instr.result, engine->runtime.getQmlContextObjectProperty(engine, VALUE(instr.base), instr.propertyIndex, instr.captureRequired)); MOTH_END_INSTR(LoadContextObjectProperty) MOTH_BEGIN_INSTR(LoadIdObject) - STOREVALUE(instr.result, Runtime::getQmlIdObject(engine, VALUE(instr.base), instr.index)); + STOREVALUE(instr.result, engine->runtime.getQmlIdObject(engine, VALUE(instr.base), instr.index)); MOTH_END_INSTR(LoadIdObject) MOTH_BEGIN_INSTR(LoadAttachedQObjectProperty) - STOREVALUE(instr.result, Runtime::getQmlAttachedProperty(engine, instr.attachedPropertiesId, instr.propertyIndex)); + STOREVALUE(instr.result, engine->runtime.getQmlAttachedProperty(engine, instr.attachedPropertiesId, instr.propertyIndex)); MOTH_END_INSTR(LoadAttachedQObjectProperty) MOTH_BEGIN_INSTR(LoadSingletonQObjectProperty) - STOREVALUE(instr.result, Runtime::getQmlSingletonQObjectProperty(engine, VALUE(instr.base), instr.propertyIndex, instr.captureRequired)); + STOREVALUE(instr.result, engine->runtime.getQmlSingletonQObjectProperty(engine, VALUE(instr.base), instr.propertyIndex, instr.captureRequired)); MOTH_END_INSTR(LoadSingletonQObjectProperty) MOTH_BEGIN_INSTR(Push) @@ -572,7 +574,7 @@ QV4::ReturnedValue VME::run(ExecutionEngine *engine, const uchar *code callData->tag = QV4::Value::Integer_Type_Internal; callData->argc = instr.argc; callData->thisObject = QV4::Primitive::undefinedValue(); - STOREVALUE(instr.result, Runtime::callValue(engine, VALUE(instr.dest), callData)); + STOREVALUE(instr.result, engine->runtime.callValue(engine, VALUE(instr.dest), callData)); MOTH_END_INSTR(CallValue) MOTH_BEGIN_INSTR(CallProperty) @@ -582,7 +584,7 @@ QV4::ReturnedValue VME::run(ExecutionEngine *engine, const uchar *code callData->tag = QV4::Value::Integer_Type_Internal; callData->argc = instr.argc; callData->thisObject = VALUE(instr.base); - STOREVALUE(instr.result, Runtime::callProperty(engine, instr.name, callData)); + STOREVALUE(instr.result, engine->runtime.callProperty(engine, instr.name, callData)); MOTH_END_INSTR(CallProperty) MOTH_BEGIN_INSTR(CallPropertyLookup) @@ -591,7 +593,7 @@ QV4::ReturnedValue VME::run(ExecutionEngine *engine, const uchar *code callData->tag = QV4::Value::Integer_Type_Internal; callData->argc = instr.argc; callData->thisObject = VALUE(instr.base); - STOREVALUE(instr.result, Runtime::callPropertyLookup(engine, instr.lookupIndex, callData)); + STOREVALUE(instr.result, engine->runtime.callPropertyLookup(engine, instr.lookupIndex, callData)); MOTH_END_INSTR(CallPropertyLookup) MOTH_BEGIN_INSTR(CallScopeObjectProperty) @@ -601,7 +603,7 @@ QV4::ReturnedValue VME::run(ExecutionEngine *engine, const uchar *code callData->tag = QV4::Value::Integer_Type_Internal; callData->argc = instr.argc; callData->thisObject = VALUE(instr.base); - STOREVALUE(instr.result, Runtime::callQmlScopeObjectProperty(engine, instr.index, callData)); + STOREVALUE(instr.result, engine->runtime.callQmlScopeObjectProperty(engine, instr.index, callData)); MOTH_END_INSTR(CallScopeObjectProperty) MOTH_BEGIN_INSTR(CallContextObjectProperty) @@ -611,7 +613,7 @@ QV4::ReturnedValue VME::run(ExecutionEngine *engine, const uchar *code callData->tag = QV4::Value::Integer_Type_Internal; callData->argc = instr.argc; callData->thisObject = VALUE(instr.base); - STOREVALUE(instr.result, Runtime::callQmlContextObjectProperty(engine, instr.index, callData)); + STOREVALUE(instr.result, engine->runtime.callQmlContextObjectProperty(engine, instr.index, callData)); MOTH_END_INSTR(CallContextObjectProperty) MOTH_BEGIN_INSTR(CallElement) @@ -620,7 +622,7 @@ QV4::ReturnedValue VME::run(ExecutionEngine *engine, const uchar *code callData->tag = QV4::Value::Integer_Type_Internal; callData->argc = instr.argc; callData->thisObject = VALUE(instr.base); - STOREVALUE(instr.result, Runtime::callElement(engine, VALUE(instr.index), callData)); + STOREVALUE(instr.result, engine->runtime.callElement(engine, VALUE(instr.index), callData)); MOTH_END_INSTR(CallElement) MOTH_BEGIN_INSTR(CallActivationProperty) @@ -629,7 +631,7 @@ QV4::ReturnedValue VME::run(ExecutionEngine *engine, const uchar *code callData->tag = QV4::Value::Integer_Type_Internal; callData->argc = instr.argc; callData->thisObject = QV4::Primitive::undefinedValue(); - STOREVALUE(instr.result, Runtime::callActivationProperty(engine, instr.name, callData)); + STOREVALUE(instr.result, engine->runtime.callActivationProperty(engine, instr.name, callData)); MOTH_END_INSTR(CallActivationProperty) MOTH_BEGIN_INSTR(CallGlobalLookup) @@ -638,7 +640,7 @@ QV4::ReturnedValue VME::run(ExecutionEngine *engine, const uchar *code callData->tag = QV4::Value::Integer_Type_Internal; callData->argc = instr.argc; callData->thisObject = QV4::Primitive::undefinedValue(); - STOREVALUE(instr.result, Runtime::callGlobalLookup(engine, instr.index, callData)); + STOREVALUE(instr.result, Runtime::method_callGlobalLookup(engine, instr.index, callData)); MOTH_END_INSTR(CallGlobalLookup) MOTH_BEGIN_INSTR(SetExceptionHandler) @@ -646,95 +648,95 @@ QV4::ReturnedValue VME::run(ExecutionEngine *engine, const uchar *code MOTH_END_INSTR(SetExceptionHandler) MOTH_BEGIN_INSTR(CallBuiltinThrow) - Runtime::throwException(engine, VALUE(instr.arg)); + engine->runtime.throwException(engine, VALUE(instr.arg)); CHECK_EXCEPTION; MOTH_END_INSTR(CallBuiltinThrow) MOTH_BEGIN_INSTR(CallBuiltinUnwindException) - STOREVALUE(instr.result, Runtime::unwindException(engine)); + STOREVALUE(instr.result, engine->runtime.unwindException(engine)); MOTH_END_INSTR(CallBuiltinUnwindException) MOTH_BEGIN_INSTR(CallBuiltinPushCatchScope) - Runtime::pushCatchScope(static_cast<QV4::NoThrowEngine*>(engine), instr.name); + engine->runtime.pushCatchScope(static_cast<QV4::NoThrowEngine*>(engine), instr.name); context = engine->currentContext; MOTH_END_INSTR(CallBuiltinPushCatchScope) MOTH_BEGIN_INSTR(CallBuiltinPushScope) - Runtime::pushWithScope(VALUE(instr.arg), engine); + engine->runtime.pushWithScope(VALUE(instr.arg), engine); context = engine->currentContext; CHECK_EXCEPTION; MOTH_END_INSTR(CallBuiltinPushScope) MOTH_BEGIN_INSTR(CallBuiltinPopScope) - Runtime::popScope(engine); + engine->runtime.popScope(engine); context = engine->currentContext; MOTH_END_INSTR(CallBuiltinPopScope) MOTH_BEGIN_INSTR(CallBuiltinForeachIteratorObject) - STOREVALUE(instr.result, Runtime::foreachIterator(engine, VALUE(instr.arg))); + STOREVALUE(instr.result, engine->runtime.foreachIterator(engine, VALUE(instr.arg))); MOTH_END_INSTR(CallBuiltinForeachIteratorObject) MOTH_BEGIN_INSTR(CallBuiltinForeachNextPropertyName) - STOREVALUE(instr.result, Runtime::foreachNextPropertyName(VALUE(instr.arg))); + STOREVALUE(instr.result, engine->runtime.foreachNextPropertyName(VALUE(instr.arg))); MOTH_END_INSTR(CallBuiltinForeachNextPropertyName) MOTH_BEGIN_INSTR(CallBuiltinDeleteMember) - STOREVALUE(instr.result, Runtime::deleteMember(engine, VALUE(instr.base), instr.member)); + STOREVALUE(instr.result, engine->runtime.deleteMember(engine, VALUE(instr.base), instr.member)); MOTH_END_INSTR(CallBuiltinDeleteMember) MOTH_BEGIN_INSTR(CallBuiltinDeleteSubscript) - STOREVALUE(instr.result, Runtime::deleteElement(engine, VALUE(instr.base), VALUE(instr.index))); + STOREVALUE(instr.result, engine->runtime.deleteElement(engine, VALUE(instr.base), VALUE(instr.index))); MOTH_END_INSTR(CallBuiltinDeleteSubscript) MOTH_BEGIN_INSTR(CallBuiltinDeleteName) - STOREVALUE(instr.result, Runtime::deleteName(engine, instr.name)); + STOREVALUE(instr.result, engine->runtime.deleteName(engine, instr.name)); MOTH_END_INSTR(CallBuiltinDeleteName) MOTH_BEGIN_INSTR(CallBuiltinTypeofScopeObjectProperty) - STOREVALUE(instr.result, Runtime::typeofScopeObjectProperty(engine, VALUE(instr.base), instr.index)); + STOREVALUE(instr.result, engine->runtime.typeofScopeObjectProperty(engine, VALUE(instr.base), instr.index)); MOTH_END_INSTR(CallBuiltinTypeofMember) MOTH_BEGIN_INSTR(CallBuiltinTypeofContextObjectProperty) - STOREVALUE(instr.result, Runtime::typeofContextObjectProperty(engine, VALUE(instr.base), instr.index)); + STOREVALUE(instr.result, engine->runtime.typeofContextObjectProperty(engine, VALUE(instr.base), instr.index)); MOTH_END_INSTR(CallBuiltinTypeofMember) MOTH_BEGIN_INSTR(CallBuiltinTypeofMember) - STOREVALUE(instr.result, Runtime::typeofMember(engine, VALUE(instr.base), instr.member)); + STOREVALUE(instr.result, engine->runtime.typeofMember(engine, VALUE(instr.base), instr.member)); MOTH_END_INSTR(CallBuiltinTypeofMember) MOTH_BEGIN_INSTR(CallBuiltinTypeofSubscript) - STOREVALUE(instr.result, Runtime::typeofElement(engine, VALUE(instr.base), VALUE(instr.index))); + STOREVALUE(instr.result, engine->runtime.typeofElement(engine, VALUE(instr.base), VALUE(instr.index))); MOTH_END_INSTR(CallBuiltinTypeofSubscript) MOTH_BEGIN_INSTR(CallBuiltinTypeofName) - STOREVALUE(instr.result, Runtime::typeofName(engine, instr.name)); + STOREVALUE(instr.result, engine->runtime.typeofName(engine, instr.name)); MOTH_END_INSTR(CallBuiltinTypeofName) MOTH_BEGIN_INSTR(CallBuiltinTypeofValue) - STOREVALUE(instr.result, Runtime::typeofValue(engine, VALUE(instr.value))); + STOREVALUE(instr.result, engine->runtime.typeofValue(engine, VALUE(instr.value))); MOTH_END_INSTR(CallBuiltinTypeofValue) MOTH_BEGIN_INSTR(CallBuiltinDeclareVar) - Runtime::declareVar(engine, instr.isDeletable, instr.varName); + engine->runtime.declareVar(engine, instr.isDeletable, instr.varName); MOTH_END_INSTR(CallBuiltinDeclareVar) MOTH_BEGIN_INSTR(CallBuiltinDefineArray) Q_ASSERT(instr.args + instr.argc <= stackSize); QV4::Value *args = stack + instr.args; - STOREVALUE(instr.result, Runtime::arrayLiteral(engine, args, instr.argc)); + STOREVALUE(instr.result, engine->runtime.arrayLiteral(engine, args, instr.argc)); MOTH_END_INSTR(CallBuiltinDefineArray) MOTH_BEGIN_INSTR(CallBuiltinDefineObjectLiteral) QV4::Value *args = stack + instr.args; - STOREVALUE(instr.result, Runtime::objectLiteral(engine, args, instr.internalClassId, instr.arrayValueCount, instr.arrayGetterSetterCountAndFlags)); + STOREVALUE(instr.result, engine->runtime.objectLiteral(engine, args, instr.internalClassId, instr.arrayValueCount, instr.arrayGetterSetterCountAndFlags)); MOTH_END_INSTR(CallBuiltinDefineObjectLiteral) MOTH_BEGIN_INSTR(CallBuiltinSetupArgumentsObject) - STOREVALUE(instr.result, Runtime::setupArgumentsObject(engine)); + STOREVALUE(instr.result, engine->runtime.setupArgumentsObject(engine)); MOTH_END_INSTR(CallBuiltinSetupArgumentsObject) MOTH_BEGIN_INSTR(CallBuiltinConvertThisToObject) - Runtime::convertThisToObject(engine); + engine->runtime.convertThisToObject(engine); CHECK_EXCEPTION; MOTH_END_INSTR(CallBuiltinConvertThisToObject) @@ -744,7 +746,7 @@ QV4::ReturnedValue VME::run(ExecutionEngine *engine, const uchar *code callData->tag = QV4::Value::Integer_Type_Internal; callData->argc = instr.argc; callData->thisObject = QV4::Primitive::undefinedValue(); - STOREVALUE(instr.result, Runtime::constructValue(engine, VALUE(instr.func), callData)); + STOREVALUE(instr.result, engine->runtime.constructValue(engine, VALUE(instr.func), callData)); MOTH_END_INSTR(CreateValue) MOTH_BEGIN_INSTR(CreateProperty) @@ -753,7 +755,7 @@ QV4::ReturnedValue VME::run(ExecutionEngine *engine, const uchar *code callData->tag = QV4::Value::Integer_Type_Internal; callData->argc = instr.argc; callData->thisObject = VALUE(instr.base); - STOREVALUE(instr.result, Runtime::constructProperty(engine, instr.name, callData)); + STOREVALUE(instr.result, engine->runtime.constructProperty(engine, instr.name, callData)); MOTH_END_INSTR(CreateProperty) MOTH_BEGIN_INSTR(ConstructPropertyLookup) @@ -762,7 +764,7 @@ QV4::ReturnedValue VME::run(ExecutionEngine *engine, const uchar *code callData->tag = QV4::Value::Integer_Type_Internal; callData->argc = instr.argc; callData->thisObject = VALUE(instr.base); - STOREVALUE(instr.result, Runtime::constructPropertyLookup(engine, instr.index, callData)); + STOREVALUE(instr.result, engine->runtime.constructPropertyLookup(engine, instr.index, callData)); MOTH_END_INSTR(ConstructPropertyLookup) MOTH_BEGIN_INSTR(CreateActivationProperty) @@ -771,7 +773,7 @@ QV4::ReturnedValue VME::run(ExecutionEngine *engine, const uchar *code callData->tag = QV4::Value::Integer_Type_Internal; callData->argc = instr.argc; callData->thisObject = QV4::Primitive::undefinedValue(); - STOREVALUE(instr.result, Runtime::constructActivationProperty(engine, instr.name, callData)); + STOREVALUE(instr.result, engine->runtime.constructActivationProperty(engine, instr.name, callData)); MOTH_END_INSTR(CreateActivationProperty) MOTH_BEGIN_INSTR(ConstructGlobalLookup) @@ -780,7 +782,7 @@ QV4::ReturnedValue VME::run(ExecutionEngine *engine, const uchar *code callData->tag = QV4::Value::Integer_Type_Internal; callData->argc = instr.argc; callData->thisObject = QV4::Primitive::undefinedValue(); - STOREVALUE(instr.result, Runtime::constructGlobalLookup(engine, instr.index, callData)); + STOREVALUE(instr.result, engine->runtime.constructGlobalLookup(engine, instr.index, callData)); MOTH_END_INSTR(ConstructGlobalLookup) MOTH_BEGIN_INSTR(Jump) @@ -802,7 +804,7 @@ QV4::ReturnedValue VME::run(ExecutionEngine *engine, const uchar *code MOTH_END_INSTR(JumpNe) MOTH_BEGIN_INSTR(UNot) - STOREVALUE(instr.result, Runtime::uNot(VALUE(instr.source))); + STOREVALUE(instr.result, engine->runtime.uNot(VALUE(instr.source))); MOTH_END_INSTR(UNot) MOTH_BEGIN_INSTR(UNotBool) @@ -811,15 +813,15 @@ QV4::ReturnedValue VME::run(ExecutionEngine *engine, const uchar *code MOTH_END_INSTR(UNotBool) MOTH_BEGIN_INSTR(UPlus) - STOREVALUE(instr.result, Runtime::uPlus(VALUE(instr.source))); + STOREVALUE(instr.result, engine->runtime.uPlus(VALUE(instr.source))); MOTH_END_INSTR(UPlus) MOTH_BEGIN_INSTR(UMinus) - STOREVALUE(instr.result, Runtime::uMinus(VALUE(instr.source))); + STOREVALUE(instr.result, engine->runtime.uMinus(VALUE(instr.source))); MOTH_END_INSTR(UMinus) MOTH_BEGIN_INSTR(UCompl) - STOREVALUE(instr.result, Runtime::complement(VALUE(instr.source))); + STOREVALUE(instr.result, engine->runtime.complement(VALUE(instr.source))); MOTH_END_INSTR(UCompl) MOTH_BEGIN_INSTR(UComplInt) @@ -827,31 +829,32 @@ QV4::ReturnedValue VME::run(ExecutionEngine *engine, const uchar *code MOTH_END_INSTR(UComplInt) MOTH_BEGIN_INSTR(Increment) - STOREVALUE(instr.result, Runtime::increment(VALUE(instr.source))); + STOREVALUE(instr.result, engine->runtime.increment(VALUE(instr.source))); MOTH_END_INSTR(Increment) MOTH_BEGIN_INSTR(Decrement) - STOREVALUE(instr.result, Runtime::decrement(VALUE(instr.source))); + STOREVALUE(instr.result, engine->runtime.decrement(VALUE(instr.source))); MOTH_END_INSTR(Decrement) MOTH_BEGIN_INSTR(Binop) - STOREVALUE(instr.result, instr.alu(VALUE(instr.lhs), VALUE(instr.rhs))); + QV4::Runtime::BinaryOperation op = *reinterpret_cast<QV4::Runtime::BinaryOperation *>(reinterpret_cast<char *>(&engine->runtime) + instr.alu); + STOREVALUE(instr.result, op(VALUE(instr.lhs), VALUE(instr.rhs))); MOTH_END_INSTR(Binop) MOTH_BEGIN_INSTR(Add) - STOREVALUE(instr.result, Runtime::add(engine, VALUE(instr.lhs), VALUE(instr.rhs))); + STOREVALUE(instr.result, engine->runtime.add(engine, VALUE(instr.lhs), VALUE(instr.rhs))); MOTH_END_INSTR(Add) MOTH_BEGIN_INSTR(BitAnd) - STOREVALUE(instr.result, Runtime::bitAnd(VALUE(instr.lhs), VALUE(instr.rhs))); + STOREVALUE(instr.result, engine->runtime.bitAnd(VALUE(instr.lhs), VALUE(instr.rhs))); MOTH_END_INSTR(BitAnd) MOTH_BEGIN_INSTR(BitOr) - STOREVALUE(instr.result, Runtime::bitOr(VALUE(instr.lhs), VALUE(instr.rhs))); + STOREVALUE(instr.result, engine->runtime.bitOr(VALUE(instr.lhs), VALUE(instr.rhs))); MOTH_END_INSTR(BitOr) MOTH_BEGIN_INSTR(BitXor) - STOREVALUE(instr.result, Runtime::bitXor(VALUE(instr.lhs), VALUE(instr.rhs))); + STOREVALUE(instr.result, engine->runtime.bitXor(VALUE(instr.lhs), VALUE(instr.rhs))); MOTH_END_INSTR(BitXor) MOTH_BEGIN_INSTR(Shr) @@ -886,15 +889,16 @@ QV4::ReturnedValue VME::run(ExecutionEngine *engine, const uchar *code MOTH_END_INSTR(ShlConst) MOTH_BEGIN_INSTR(Mul) - STOREVALUE(instr.result, Runtime::mul(VALUE(instr.lhs), VALUE(instr.rhs))); + STOREVALUE(instr.result, engine->runtime.mul(VALUE(instr.lhs), VALUE(instr.rhs))); MOTH_END_INSTR(Mul) MOTH_BEGIN_INSTR(Sub) - STOREVALUE(instr.result, Runtime::sub(VALUE(instr.lhs), VALUE(instr.rhs))); + STOREVALUE(instr.result, engine->runtime.sub(VALUE(instr.lhs), VALUE(instr.rhs))); MOTH_END_INSTR(Sub) MOTH_BEGIN_INSTR(BinopContext) - STOREVALUE(instr.result, instr.alu(engine, VALUE(instr.lhs), VALUE(instr.rhs))); + QV4::Runtime::BinaryOperationContext op = *reinterpret_cast<QV4::Runtime::BinaryOperationContext *>(reinterpret_cast<char *>(&engine->runtime) + instr.alu); + STOREVALUE(instr.result, op(engine, VALUE(instr.lhs), VALUE(instr.rhs))); MOTH_END_INSTR(BinopContext) MOTH_BEGIN_INSTR(Ret) @@ -902,9 +906,10 @@ QV4::ReturnedValue VME::run(ExecutionEngine *engine, const uchar *code return VALUE(instr.result).asReturnedValue(); MOTH_END_INSTR(Ret) +#ifndef QT_NO_QML_DEBUGGER MOTH_BEGIN_INSTR(Debug) engine->current->lineNumber = instr.lineNumber; - QV4::Debugging::Debugger *debugger = context->engine()->debugger; + QV4::Debugging::Debugger *debugger = context->engine()->debugger(); if (debugger && debugger->pauseAtNextOpportunity()) debugger->maybeBreakAtInstruction(); if (qt_v4IsDebugging) @@ -916,21 +921,22 @@ QV4::ReturnedValue VME::run(ExecutionEngine *engine, const uchar *code if (qt_v4IsDebugging) qt_v4CheckForBreak(context, scopes, scopeDepth); MOTH_END_INSTR(Line) +#endif // QT_NO_QML_DEBUGGER MOTH_BEGIN_INSTR(LoadThis) VALUE(instr.result) = context->thisObject(); MOTH_END_INSTR(LoadThis) MOTH_BEGIN_INSTR(LoadQmlContext) - VALUE(instr.result) = Runtime::getQmlContext(static_cast<QV4::NoThrowEngine*>(engine)); + VALUE(instr.result) = engine->runtime.getQmlContext(static_cast<QV4::NoThrowEngine*>(engine)); MOTH_END_INSTR(LoadQmlContext) MOTH_BEGIN_INSTR(LoadQmlImportedScripts) - VALUE(instr.result) = Runtime::getQmlImportedScripts(static_cast<QV4::NoThrowEngine*>(engine)); + VALUE(instr.result) = engine->runtime.getQmlImportedScripts(static_cast<QV4::NoThrowEngine*>(engine)); MOTH_END_INSTR(LoadQmlImportedScripts) MOTH_BEGIN_INSTR(LoadQmlSingleton) - VALUE(instr.result) = Runtime::getQmlSingleton(static_cast<QV4::NoThrowEngine*>(engine), instr.name); + VALUE(instr.result) = engine->runtime.getQmlSingleton(static_cast<QV4::NoThrowEngine*>(engine), instr.name); MOTH_END_INSTR(LoadQmlSingleton) #ifdef MOTH_THREADED_INTERPRETER @@ -968,7 +974,7 @@ void **VME::instructionJumpTable() QV4::ReturnedValue VME::exec(ExecutionEngine *engine, const uchar *code) { VME vme; - QV4::Debugging::Debugger *debugger = engine->debugger; + QV4::Debugging::Debugger *debugger = engine->debugger(); if (debugger) debugger->enteringFunction(); QV4::ReturnedValue retVal = vme.run(engine, code); diff --git a/src/qml/jsruntime/qv4vme_moth_p.h b/src/qml/jsruntime/qv4vme_moth_p.h index cd8d335f1c..f8893509d9 100644 --- a/src/qml/jsruntime/qv4vme_moth_p.h +++ b/src/qml/jsruntime/qv4vme_moth_p.h @@ -51,9 +51,12 @@ // We mean it. // +#include <private/qv4global_p.h> #include <private/qv4runtime_p.h> #include <private/qv4instr_moth_p.h> +QT_REQUIRE_CONFIG(qml_interpreter); + QT_BEGIN_NAMESPACE namespace QV4 { |