diff options
| author | Joerg Bornemann <joerg.bornemann@digia.com> | 2013-07-25 12:50:44 +0200 |
|---|---|---|
| committer | Christian Kandeler <christian.kandeler@digia.com> | 2013-07-25 13:40:44 +0200 |
| commit | ed02be4d99442a395e9e10db666d1efba68310cd (patch) | |
| tree | 36adf641e472457331b556486bd4f6c658abbefa | |
| parent | bfae7b1363558de6f60538204f7e326293922ab8 (diff) | |
| download | qbs-ed02be4d99442a395e9e10db666d1efba68310cd.tar.gz | |
automatic type conversion for property assignments
When assigning a string to a stringList property we automatically
convert the JS variable to an Array of strings.
Similar conversions are implemented for Bool, Number and String.
Task-number: QBS-38
Change-Id: I19f54f83e540b20f18ca505d8cd8011735d6418d
Reviewed-by: Christian Kandeler <christian.kandeler@digia.com>
| -rw-r--r-- | src/lib/language/evaluatorscriptclass.cpp | 40 | ||||
| -rw-r--r-- | src/lib/language/propertydeclaration.h | 2 |
2 files changed, 40 insertions, 2 deletions
diff --git a/src/lib/language/evaluatorscriptclass.cpp b/src/lib/language/evaluatorscriptclass.cpp index 1d019c898..e1026e8a3 100644 --- a/src/lib/language/evaluatorscriptclass.cpp +++ b/src/lib/language/evaluatorscriptclass.cpp @@ -28,11 +28,13 @@ ****************************************************************************/ #include "evaluatorscriptclass.h" + #include "builtinvalue.h" #include "evaluationdata.h" #include "evaluator.h" -#include "item.h" #include "filecontext.h" +#include "item.h" +#include "propertydeclaration.h" #include <tools/qbsassert.h> #include <QScriptEngine> @@ -302,6 +304,39 @@ Item *EvaluatorScriptClass::findParentOfType(const Item *item, const QString &ty return 0; } +inline void convertToPropertyType(const PropertyDeclaration::Type t, QScriptValue &v) +{ + if (v.isUndefined()) + return; + switch (t) { + case PropertyDeclaration::UnknownType: + case PropertyDeclaration::Variant: + case PropertyDeclaration::Verbatim: + break; + case PropertyDeclaration::Boolean: + if (!v.isBool()) + v = v.toBool(); + break; + case PropertyDeclaration::Integer: + if (!v.isNumber()) + v = v.toNumber(); + break; + case PropertyDeclaration::Path: + case PropertyDeclaration::String: + if (!v.isString()) + v = v.toString(); + break; + case PropertyDeclaration::PathList: + case PropertyDeclaration::StringList: + if (!v.isArray()) { + QScriptValue x = v.engine()->newArray(1); + x.setProperty(0, v.isString() ? v : v.toString()); + v = x; + } + break; + } +} + QScriptValue EvaluatorScriptClass::property(const QScriptValue &object, const QScriptString &name, uint id) { @@ -339,6 +374,9 @@ QScriptValue EvaluatorScriptClass::property(const QScriptValue &object, const QS converter.result = &result; converter.start(); + const PropertyDeclaration decl = data->item->propertyDeclarations().value(name.toString()); + convertToPropertyType(decl.type, result); + if (debugProperties) m_logger.qbsTrace() << "[SC] cache miss " << name << ": " << resultToString(result); data->valueCache.insert(name, result); diff --git a/src/lib/language/propertydeclaration.h b/src/lib/language/propertydeclaration.h index efadbc32f..d0e4e117d 100644 --- a/src/lib/language/propertydeclaration.h +++ b/src/lib/language/propertydeclaration.h @@ -66,7 +66,7 @@ public: bool isValid() const; - static PropertyDeclaration::Type propertyTypeFromString(const QString &typeName); + static Type propertyTypeFromString(const QString &typeName); QString name; Type type; |
