summaryrefslogtreecommitdiffstats
path: root/src/script
diff options
context:
space:
mode:
authorKent Hansen <khansen@trolltech.com>2009-10-30 13:23:28 +0100
committerKent Hansen <khansen@trolltech.com>2009-10-30 14:33:20 +0100
commit8097fa53b5e0e6228bd5737de6de8cd77c04dddf (patch)
tree095b65b7b80dc8d82207d4c757f5433fc01659e9 /src/script
parent96799bf4da265835b5e574593bc5106712352ffc (diff)
Add QScriptString::toArrayIndex() function
Avoid hacking a custom toArrayIndex() in the example; instead provide an ECMA-compliant conversion that's as fast as possible (having to convert the QScriptString to a QString and then convert the result to a number is considerably slower than calling JSC's Identifier::toArrayIndex() function directly). Reviewed-by: Olivier Goffart
Diffstat (limited to 'src/script')
-rw-r--r--src/script/api/qscriptstring.cpp29
-rw-r--r--src/script/api/qscriptstring.h2
2 files changed, 31 insertions, 0 deletions
diff --git a/src/script/api/qscriptstring.cpp b/src/script/api/qscriptstring.cpp
index 1ede51c1fd..10fccd0dbf 100644
--- a/src/script/api/qscriptstring.cpp
+++ b/src/script/api/qscriptstring.cpp
@@ -68,6 +68,10 @@ QT_BEGIN_NAMESPACE
Call the toString() function to obtain the string that a
QScriptString represents.
+
+ Call the toArrayIndex() function to convert a QScriptString to an
+ array index. This is useful when using QScriptClass to implement
+ array-like objects.
*/
/*!
@@ -164,6 +168,31 @@ bool QScriptString::operator!=(const QScriptString &other) const
}
/*!
+ \since 4.6
+
+ Attempts to convert this QScriptString to a QtScript array index,
+ and returns the result.
+
+ If a conversion error occurs, *\a{ok} is set to false; otherwise
+ *\a{ok} is set to true.
+*/
+quint32 QScriptString::toArrayIndex(bool *ok) const
+{
+ Q_D(const QScriptString);
+ if (!d) {
+ if (ok)
+ *ok = false;
+ return -1;
+ }
+ bool tmp;
+ bool *okok = ok ? ok : &tmp;
+ quint32 result = d->identifier.toArrayIndex(okok);
+ if (!*okok)
+ result = -1;
+ return result;
+}
+
+/*!
Returns the string that this QScriptString represents, or a
null string if this QScriptString is not valid.
diff --git a/src/script/api/qscriptstring.h b/src/script/api/qscriptstring.h
index 40d156c0b6..bf5d1d5857 100644
--- a/src/script/api/qscriptstring.h
+++ b/src/script/api/qscriptstring.h
@@ -67,6 +67,8 @@ public:
bool operator==(const QScriptString &other) const;
bool operator!=(const QScriptString &other) const;
+ quint32 toArrayIndex(bool *ok = 0) const;
+
QString toString() const;
operator QString() const;