diff options
author | Lars Knoll <lars.knoll@qt.io> | 2016-11-25 10:27:02 +0100 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2016-11-29 20:00:29 +0000 |
commit | 46941afcc10209905a92e6bb8b3eba7c08bf6750 (patch) | |
tree | 85f11182e9656668aaeda6e5adcce6b1418fb672 | |
parent | 5d35573a62686aa3b0c45fc032b79670d88ebba9 (diff) |
Cleanup Value::isObject/objectValue usages
Try to avoid calling both as objectValue() already
checks isObject().
Change-Id: I1d770d4d9dabed4ea4cc3e322b8fdc5a64f5bd2b
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
-rw-r--r-- | src/qml/jsruntime/qv4engine.cpp | 4 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4lookup.cpp | 41 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4managed_p.h | 5 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4objectiterator.cpp | 19 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4runtime.cpp | 85 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4value.cpp | 4 | ||||
-rw-r--r-- | src/qml/qml/qqmlobjectcreator.cpp | 2 |
7 files changed, 87 insertions, 73 deletions
diff --git a/src/qml/jsruntime/qv4engine.cpp b/src/qml/jsruntime/qv4engine.cpp index 634b8a3e6b..5c79c4ea97 100644 --- a/src/qml/jsruntime/qv4engine.cpp +++ b/src/qml/jsruntime/qv4engine.cpp @@ -1704,10 +1704,10 @@ bool ExecutionEngine::metaTypeFromJS(const Value *value, int type, void *data) // We have T t, T* is requested, so return &t. *reinterpret_cast<void* *>(data) = var.data(); return true; - } else if (value->isObject()) { + } else if (Object *o = value->objectValue()) { // Look in the prototype chain. QV4::Scope scope(this); - QV4::ScopedObject proto(scope, value->objectValue()->prototype()); + QV4::ScopedObject proto(scope, o->prototype()); while (proto) { bool canCast = false; if (QV4::VariantObject *vo = proto->as<QV4::VariantObject>()) { diff --git a/src/qml/jsruntime/qv4lookup.cpp b/src/qml/jsruntime/qv4lookup.cpp index 5e9d4cf57c..0b0b2685b3 100644 --- a/src/qml/jsruntime/qv4lookup.cpp +++ b/src/qml/jsruntime/qv4lookup.cpp @@ -174,15 +174,15 @@ ReturnedValue Lookup::indexedGetterFallback(Lookup *l, const Value &object, cons ReturnedValue Lookup::indexedGetterObjectInt(Lookup *l, const Value &object, const Value &index) { uint idx = index.asArrayIndex(); - if (idx == UINT_MAX || !object.isObject()) - return indexedGetterFallback(l, object, index); - - Object *o = object.objectValue(); - if (o->d()->arrayData && o->d()->arrayData->type == Heap::ArrayData::Simple) { - Heap::SimpleArrayData *s = o->d()->arrayData.cast<Heap::SimpleArrayData>(); - if (idx < s->len) - if (!s->data(idx).isEmpty()) - return s->data(idx).asReturnedValue(); + if (idx != UINT_MAX) { + if (Object *o = object.objectValue()) { + if (o->d()->arrayData && o->d()->arrayData->type == Heap::ArrayData::Simple) { + Heap::SimpleArrayData *s = o->d()->arrayData.cast<Heap::SimpleArrayData>(); + if (idx < s->len) + if (!s->data(idx).isEmpty()) + return s->data(idx).asReturnedValue(); + } + } } return indexedGetterFallback(l, object, index); @@ -190,8 +190,7 @@ ReturnedValue Lookup::indexedGetterObjectInt(Lookup *l, const Value &object, con void Lookup::indexedSetterGeneric(Lookup *l, const Value &object, const Value &index, const Value &v) { - if (object.isObject()) { - Object *o = object.objectValue(); + if (Object *o = object.objectValue()) { if (o->d()->arrayData && o->d()->arrayData->type == Heap::ArrayData::Simple && index.asArrayIndex() < UINT_MAX) { l->indexedSetter = indexedSetterObjectInt; indexedSetterObjectInt(l, object, index, v); @@ -228,17 +227,15 @@ void Lookup::indexedSetterFallback(Lookup *l, const Value &object, const Value & void Lookup::indexedSetterObjectInt(Lookup *l, const Value &object, const Value &index, const Value &v) { uint idx = index.asArrayIndex(); - if (idx == UINT_MAX || !object.isObject()) { - indexedSetterGeneric(l, object, index, v); - return; - } - - Object *o = object.objectValue(); - if (o->d()->arrayData && o->d()->arrayData->type == Heap::ArrayData::Simple) { - Heap::SimpleArrayData *s = o->d()->arrayData.cast<Heap::SimpleArrayData>(); - if (idx < s->len) { - s->data(idx) = v; - return; + if (idx != UINT_MAX) { + if (Object *o = object.objectValue()) { + if (o->d()->arrayData && o->d()->arrayData->type == Heap::ArrayData::Simple) { + Heap::SimpleArrayData *s = o->d()->arrayData.cast<Heap::SimpleArrayData>(); + if (idx < s->len) { + s->data(idx) = v; + return; + } + } } } indexedSetterFallback(l, object, index, v); diff --git a/src/qml/jsruntime/qv4managed_p.h b/src/qml/jsruntime/qv4managed_p.h index 56a1fcedf5..86caac3ebe 100644 --- a/src/qml/jsruntime/qv4managed_p.h +++ b/src/qml/jsruntime/qv4managed_p.h @@ -206,6 +206,11 @@ public: bool markBit() const { return d()->isMarked(); } static void destroy(Heap::Base *) {} + + Q_ALWAYS_INLINE Heap::Base *heapObject() const { + return m(); + } + private: friend class MemoryManager; friend struct Identifiers; diff --git a/src/qml/jsruntime/qv4objectiterator.cpp b/src/qml/jsruntime/qv4objectiterator.cpp index 7943a13ac0..59115dfe21 100644 --- a/src/qml/jsruntime/qv4objectiterator.cpp +++ b/src/qml/jsruntime/qv4objectiterator.cpp @@ -70,15 +70,16 @@ void ObjectIterator::next(Value *name, uint *index, Property *pd, PropertyAttrib ScopedString n(scope); while (1) { - if (!current->as<Object>()) + Object *co = current->objectValue(); + if (!co) break; while (1) { - current->as<Object>()->advanceIterator(this, name, index, pd, attrs); + co->advanceIterator(this, name, index, pd, attrs); if (attrs->isEmpty()) break; // check the property is not already defined earlier in the proto chain - if (current->heapObject() != object->heapObject()) { + if (co->heapObject() != object->heapObject()) { o = object->as<Object>(); n = *name; bool shadowed = false; @@ -97,7 +98,7 @@ void ObjectIterator::next(Value *name, uint *index, Property *pd, PropertyAttrib } if (flags & WithProtoChain) - current->setM(current->objectValue()->prototype()); + current->setM(co->prototype()); else current->setM(0); @@ -109,7 +110,8 @@ void ObjectIterator::next(Value *name, uint *index, Property *pd, PropertyAttrib ReturnedValue ObjectIterator::nextPropertyName(Value *value) { - if (!object->as<Object>()) + Object *o = object->objectValue(); + if (!o) return Encode::null(); PropertyAttributes attrs; @@ -121,7 +123,7 @@ ReturnedValue ObjectIterator::nextPropertyName(Value *value) if (attrs.isEmpty()) return Encode::null(); - *value = object->objectValue()->getValue(p->value, attrs); + *value = o->getValue(p->value, attrs); if (!!name) return name->asReturnedValue(); @@ -131,7 +133,8 @@ ReturnedValue ObjectIterator::nextPropertyName(Value *value) ReturnedValue ObjectIterator::nextPropertyNameAsString(Value *value) { - if (!object->as<Object>()) + Object *o = object->objectValue(); + if (!o) return Encode::null(); PropertyAttributes attrs; @@ -143,7 +146,7 @@ ReturnedValue ObjectIterator::nextPropertyNameAsString(Value *value) if (attrs.isEmpty()) return Encode::null(); - *value = object->objectValue()->getValue(p->value, attrs); + *value = o->getValue(p->value, attrs); if (!!name) return name->asReturnedValue(); diff --git a/src/qml/jsruntime/qv4runtime.cpp b/src/qml/jsruntime/qv4runtime.cpp index 6d26cd8ad9..944588dd28 100644 --- a/src/qml/jsruntime/qv4runtime.cpp +++ b/src/qml/jsruntime/qv4runtime.cpp @@ -375,13 +375,14 @@ QV4::ReturnedValue Runtime::method_instanceof(ExecutionEngine *engine, const Val QV4::ReturnedValue Runtime::method_in(ExecutionEngine *engine, const Value &left, const Value &right) { - if (!right.isObject()) + Object *ro = right.objectValue(); + if (!ro) return engine->throwTypeError(); Scope scope(engine); ScopedString s(scope, left.toString(engine)); if (scope.hasException()) return Encode::undefined(); - bool r = right.objectValue()->hasProperty(s); + bool r = ro->hasProperty(s); return Encode(r); } @@ -753,13 +754,15 @@ uint RuntimeHelpers::equalHelper(const Value &x, const Value &y) #ifdef V4_BOOTSTRAP Q_UNIMPLEMENTED(); #else - if ((x.isNumber() || x.isString()) && y.isObject()) { - Scope scope(y.objectValue()->engine()); - ScopedValue py(scope, RuntimeHelpers::toPrimitive(y, PREFERREDTYPE_HINT)); + Object *xo = x.objectValue(); + Object *yo = y.objectValue(); + if (yo && (x.isNumber() || x.isString())) { + Scope scope(yo->engine()); + ScopedValue py(scope, RuntimeHelpers::objectDefaultValue(yo, PREFERREDTYPE_HINT)); 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)); + } else if (xo && (y.isNumber() || y.isString())) { + Scope scope(xo->engine()); + ScopedValue px(scope, RuntimeHelpers::objectDefaultValue(xo, PREFERREDTYPE_HINT)); return Runtime::method_compareEqual(px, y); } #endif @@ -801,14 +804,16 @@ QV4::Bool Runtime::method_compareGreaterThan(const Value &l, const Value &r) #endif } - if (l.isObject() || r.isObject()) { + Object *ro = r.objectValue(); + Object *lo = l.objectValue(); + if (ro || lo) { #ifdef V4_BOOTSTRAP Q_UNIMPLEMENTED(); #else - QV4::ExecutionEngine *e = (l.isObject() ? l.objectValue() : r.objectValue())->engine(); + QV4::ExecutionEngine *e = (lo ? lo : ro)->engine(); QV4::Scope scope(e); - QV4::ScopedValue pl(scope, RuntimeHelpers::toPrimitive(l, QV4::NUMBER_HINT)); - QV4::ScopedValue pr(scope, RuntimeHelpers::toPrimitive(r, QV4::NUMBER_HINT)); + QV4::ScopedValue pl(scope, lo ? RuntimeHelpers::objectDefaultValue(lo, QV4::NUMBER_HINT) : l.asReturnedValue()); + QV4::ScopedValue pr(scope, ro ? RuntimeHelpers::objectDefaultValue(ro, QV4::NUMBER_HINT) : r.asReturnedValue()); return Runtime::method_compareGreaterThan(pl, pr); #endif } @@ -836,14 +841,16 @@ QV4::Bool Runtime::method_compareLessThan(const Value &l, const Value &r) #endif } - if (l.isObject() || r.isObject()) { + Object *ro = r.objectValue(); + Object *lo = l.objectValue(); + if (ro || lo) { #ifdef V4_BOOTSTRAP Q_UNIMPLEMENTED(); #else - QV4::ExecutionEngine *e = (l.isObject() ? l.objectValue() : r.objectValue())->engine(); + QV4::ExecutionEngine *e = (lo ? lo : ro)->engine(); QV4::Scope scope(e); - QV4::ScopedValue pl(scope, RuntimeHelpers::toPrimitive(l, QV4::NUMBER_HINT)); - QV4::ScopedValue pr(scope, RuntimeHelpers::toPrimitive(r, QV4::NUMBER_HINT)); + QV4::ScopedValue pl(scope, lo ? RuntimeHelpers::objectDefaultValue(lo, QV4::NUMBER_HINT) : l.asReturnedValue()); + QV4::ScopedValue pr(scope, ro ? RuntimeHelpers::objectDefaultValue(ro, QV4::NUMBER_HINT) : r.asReturnedValue()); return Runtime::method_compareLessThan(pl, pr); #endif } @@ -871,14 +878,16 @@ QV4::Bool Runtime::method_compareGreaterEqual(const Value &l, const Value &r) #endif } - if (l.isObject() || r.isObject()) { + Object *ro = r.objectValue(); + Object *lo = l.objectValue(); + if (ro || lo) { #ifdef V4_BOOTSTRAP Q_UNIMPLEMENTED(); #else - QV4::ExecutionEngine *e = (l.isObject() ? l.objectValue() : r.objectValue())->engine(); + QV4::ExecutionEngine *e = (lo ? lo : ro)->engine(); QV4::Scope scope(e); - QV4::ScopedValue pl(scope, RuntimeHelpers::toPrimitive(l, QV4::NUMBER_HINT)); - QV4::ScopedValue pr(scope, RuntimeHelpers::toPrimitive(r, QV4::NUMBER_HINT)); + QV4::ScopedValue pl(scope, lo ? RuntimeHelpers::objectDefaultValue(lo, QV4::NUMBER_HINT) : l.asReturnedValue()); + QV4::ScopedValue pr(scope, ro ? RuntimeHelpers::objectDefaultValue(ro, QV4::NUMBER_HINT) : r.asReturnedValue()); return Runtime::method_compareGreaterEqual(pl, pr); #endif } @@ -906,14 +915,16 @@ QV4::Bool Runtime::method_compareLessEqual(const Value &l, const Value &r) #endif } - if (l.isObject() || r.isObject()) { + Object *ro = r.objectValue(); + Object *lo = l.objectValue(); + if (ro || lo) { #ifdef V4_BOOTSTRAP Q_UNIMPLEMENTED(); #else - QV4::ExecutionEngine *e = (l.isObject() ? l.objectValue() : r.objectValue())->engine(); + QV4::ExecutionEngine *e = (lo ? lo : ro)->engine(); QV4::Scope scope(e); - QV4::ScopedValue pl(scope, RuntimeHelpers::toPrimitive(l, QV4::NUMBER_HINT)); - QV4::ScopedValue pr(scope, RuntimeHelpers::toPrimitive(r, QV4::NUMBER_HINT)); + QV4::ScopedValue pl(scope, lo ? RuntimeHelpers::objectDefaultValue(lo, QV4::NUMBER_HINT) : l.asReturnedValue()); + QV4::ScopedValue pr(scope, ro ? RuntimeHelpers::objectDefaultValue(ro, QV4::NUMBER_HINT) : r.asReturnedValue()); return Runtime::method_compareLessEqual(pl, pr); #endif } @@ -1056,13 +1067,12 @@ ReturnedValue Runtime::method_callPropertyLookup(ExecutionEngine *engine, uint i Lookup *l = engine->current->lookups + index; Value v; v = l->getter(l, engine, callData->thisObject); - if (v.isObject()) { + if (Object *o = v.objectValue()) { Scope scope(engine); - v.objectValue()->call(scope, callData); + o->call(scope, callData); return scope.result.asReturnedValue(); - } else { - return engine->throwTypeError(); } + return engine->throwTypeError(); } ReturnedValue Runtime::method_callElement(ExecutionEngine *engine, const Value &index, CallData *callData) @@ -1085,12 +1095,12 @@ ReturnedValue Runtime::method_callElement(ExecutionEngine *engine, const Value & 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())); - - Scope scope(engine); - func.objectValue()->call(scope, callData); - return scope.result.asReturnedValue(); + if (Object *o = func.objectValue()) { + Scope scope(engine); + o->call(scope, callData); + return scope.result.asReturnedValue(); + } + return engine->throwTypeError(QStringLiteral("%1 is not a function").arg(func.toQStringNoThrow())); } @@ -1160,14 +1170,13 @@ ReturnedValue Runtime::method_constructPropertyLookup(ExecutionEngine *engine, u Lookup *l = engine->current->lookups + index; Value v; v = l->getter(l, engine, callData->thisObject); - if (v.isObject()) { + if (Object *o = v.objectValue()) { Scope scope(engine); ScopedValue result(scope); - v.objectValue()->construct(scope, callData); + o->construct(scope, callData); return scope.result.asReturnedValue(); - } else { - return engine->throwTypeError(); } + return engine->throwTypeError(); } diff --git a/src/qml/jsruntime/qv4value.cpp b/src/qml/jsruntime/qv4value.cpp index ea5a3d491f..a3cd212d3b 100644 --- a/src/qml/jsruntime/qv4value.cpp +++ b/src/qml/jsruntime/qv4value.cpp @@ -307,8 +307,8 @@ Heap::String *Value::toString(ExecutionEngine *e) const Heap::Object *Value::toObject(ExecutionEngine *e) const { - if (isObject()) - return objectValue()->d(); + if (Object *o = objectValue()) + return o->d(); return RuntimeHelpers::convertToObject(e, *this); } diff --git a/src/qml/qml/qqmlobjectcreator.cpp b/src/qml/qml/qqmlobjectcreator.cpp index 115925e34a..d07be77697 100644 --- a/src/qml/qml/qqmlobjectcreator.cpp +++ b/src/qml/qml/qqmlobjectcreator.cpp @@ -1016,7 +1016,7 @@ void QQmlObjectCreator::registerObjectWithContextById(const QV4::CompiledData::O QV4::Heap::QmlContext *QQmlObjectCreator::currentQmlContext() { - if (!_qmlContext->objectValue()) + if (!_qmlContext->isObject()) _qmlContext->setM(v4->rootContext()->newQmlContext(context, _scopeObject)); return _qmlContext->d(); |