summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOlivier Goffart <ogoffart@trolltech.com>2009-07-15 14:48:03 (GMT)
committerOlivier Goffart <ogoffart@trolltech.com>2009-07-16 09:54:57 (GMT)
commitadb1182d4cb600f48b5858dd78b4b6a6c43a89cd (patch)
treee28b463adc196baf0f507ea5239bdff5d10f76bf
parent7496ce1de03c9e11e710dbd4dae2056a21f5addd (diff)
downloadQt-adb1182d4cb600f48b5858dd78b4b6a6c43a89cd.zip
Qt-adb1182d4cb600f48b5858dd78b4b6a6c43a89cd.tar.gz
Qt-adb1182d4cb600f48b5858dd78b4b6a6c43a89cd.tar.bz2
Convert the strings or number value to jscvalue when they are used with an engine
Also change the stringValue not to be a pointer. This fixes a memory leak.
-rw-r--r--src/script/api/qscriptengine.cpp20
-rw-r--r--src/script/api/qscriptvalue.cpp26
-rw-r--r--src/script/api/qscriptvalue_p.h2
-rw-r--r--tests/auto/qscriptvalue/tst_qscriptvalue.cpp17
4 files changed, 34 insertions, 31 deletions
diff --git a/src/script/api/qscriptengine.cpp b/src/script/api/qscriptengine.cpp
index 702851a..116e449 100644
--- a/src/script/api/qscriptengine.cpp
+++ b/src/script/api/qscriptengine.cpp
@@ -869,15 +869,17 @@ JSC::JSValue QScriptEnginePrivate::scriptValueToJSCValue(const QScriptValue &val
QScriptValuePrivate *vv = QScriptValuePrivate::get(value);
if (!vv)
return JSC::JSValue();
- switch (vv->type) {
- case QScriptValuePrivate::JSC:
- return vv->jscValue;
- case QScriptValuePrivate::Number:
- return JSC::jsNumber(currentFrame, vv->numberValue);
- case QScriptValuePrivate::String:
- return JSC::jsString(currentFrame, QScript::qtStringToJSCUString(*vv->stringValue));
+ if (vv->type != QScriptValuePrivate::JSC) {
+ Q_Q(QScriptEngine);
+ Q_ASSERT(!vv->engine || vv->engine == q);
+ vv->engine = q;
+ if (vv->type == QScriptValuePrivate::Number) {
+ vv->initFromJSCValue(JSC::jsNumber(currentFrame, vv->numberValue));
+ } else { //QScriptValuePrivate::String
+ vv->initFromJSCValue(JSC::jsString(currentFrame, QScript::qtStringToJSCUString(vv->stringValue)));
+ }
}
- return JSC::JSValue();
+ return vv->jscValue;
}
void QScriptEnginePrivate::releaseJSCValue(JSC::JSValue value)
@@ -933,7 +935,7 @@ JSC::JSValue QScriptEnginePrivate::jscValueFromVariant(const QVariant &v)
case QScriptValuePrivate::Number:
return JSC::jsNumber(currentFrame, p->numberValue);
case QScriptValuePrivate::String: {
- JSC::UString str = QScript::qtStringToJSCUString(*p->stringValue);
+ JSC::UString str = QScript::qtStringToJSCUString(p->stringValue);
return JSC::jsString(currentFrame, str);
}
}
diff --git a/src/script/api/qscriptvalue.cpp b/src/script/api/qscriptvalue.cpp
index 68ad93a..4d5361e 100644
--- a/src/script/api/qscriptvalue.cpp
+++ b/src/script/api/qscriptvalue.cpp
@@ -299,7 +299,7 @@ void QScriptValuePrivate::initFromNumber(double value)
void QScriptValuePrivate::initFromString(const QString &value)
{
type = String;
- stringValue = new QString(value);
+ stringValue = value;
}
void QScriptValuePrivate::initFromJSCValue(QScriptValue &result,
@@ -1168,7 +1168,7 @@ bool QScriptValue::strictlyEquals(const QScriptValue &other) const
case QScriptValuePrivate::Number:
return (d->numberValue == other.d_ptr->numberValue);
case QScriptValuePrivate::String:
- return (*d->stringValue == *other.d_ptr->stringValue);
+ return (d->stringValue == other.d_ptr->stringValue);
}
return false;
}
@@ -1200,7 +1200,7 @@ QString QScriptValue::toString() const
case QScriptValuePrivate::Number:
return QScript::qtStringFromJSCUString(JSC::UString::from(d->numberValue));
case QScriptValuePrivate::String:
- return *d->stringValue;
+ return d->stringValue;
}
return QString();
}
@@ -1231,7 +1231,7 @@ qsreal QScriptValue::toNumber() const
case QScriptValuePrivate::Number:
return d->numberValue;
case QScriptValuePrivate::String:
- return QScript::qtStringToJSCUString(*d->stringValue).toDouble();
+ return QScript::qtStringToJSCUString(d->stringValue).toDouble();
}
return 0;
}
@@ -1255,7 +1255,7 @@ bool QScriptValue::toBoolean() const
case QScriptValuePrivate::Number:
return (d->numberValue != 0) && !qIsNaN(d->numberValue);
case QScriptValuePrivate::String:
- return (d->stringValue->length() != 0);
+ return (!d->stringValue.isEmpty());
}
return false;
}
@@ -1288,7 +1288,7 @@ bool QScriptValue::toBool() const
case QScriptValuePrivate::Number:
return (d->numberValue != 0) && !qIsNaN(d->numberValue);
case QScriptValuePrivate::String:
- return (d->stringValue->length() != 0);
+ return (!d->stringValue.isEmpty());
}
return false;
}
@@ -1319,7 +1319,7 @@ qint32 QScriptValue::toInt32() const
case QScriptValuePrivate::Number:
return QScript::ToInt32(d->numberValue);
case QScriptValuePrivate::String:
- return QScript::ToInt32(QScript::qtStringToJSCUString(*d->stringValue).toDouble());
+ return QScript::ToInt32(QScript::qtStringToJSCUString(d->stringValue).toDouble());
}
return 0;
}
@@ -1350,7 +1350,7 @@ quint32 QScriptValue::toUInt32() const
case QScriptValuePrivate::Number:
return QScript::ToUint32(d->numberValue);
case QScriptValuePrivate::String:
- return QScript::ToUint32(QScript::qtStringToJSCUString(*d->stringValue).toDouble());
+ return QScript::ToUint32(QScript::qtStringToJSCUString(d->stringValue).toDouble());
}
return 0;
}
@@ -1380,7 +1380,7 @@ quint16 QScriptValue::toUInt16() const
case QScriptValuePrivate::Number:
return QScript::ToUint16(d->numberValue);
case QScriptValuePrivate::String:
- return QScript::ToUint16(QScript::qtStringToJSCUString(*d->stringValue).toDouble());
+ return QScript::ToUint16(QScript::qtStringToJSCUString(d->stringValue).toDouble());
}
return 0;
}
@@ -1411,7 +1411,7 @@ qsreal QScriptValue::toInteger() const
case QScriptValuePrivate::Number:
return QScript::ToInteger(d->numberValue);
case QScriptValuePrivate::String:
- return QScript::ToInteger(QScript::qtStringToJSCUString(*d->stringValue).toDouble());
+ return QScript::ToInteger(QScript::qtStringToJSCUString(d->stringValue).toDouble());
}
return 0;
}
@@ -1477,7 +1477,7 @@ QVariant QScriptValue::toVariant() const
case QScriptValuePrivate::Number:
return QVariant(d->numberValue);
case QScriptValuePrivate::String:
- return QVariant(*d->stringValue);
+ return QVariant(d->stringValue);
}
return QVariant();
}
@@ -1608,13 +1608,15 @@ const QMetaObject *QScriptValue::toQMetaObject() const
\sa property()
*/
+
void QScriptValue::setProperty(const QString &name, const QScriptValue &value,
const PropertyFlags &flags)
{
Q_D(QScriptValue);
if (!d || !d->isJSC() || !d->jscValue.isObject())
return;
- if (value.engine() && (value.engine() != engine())) {
+ QScriptEngine *valueEngine = value.engine();
+ if (valueEngine && (valueEngine != d->engine)) {
qWarning("QScriptValue::setProperty(%s) failed: "
"cannot set value created in a different engine",
qPrintable(name));
diff --git a/src/script/api/qscriptvalue_p.h b/src/script/api/qscriptvalue_p.h
index bbd54fa..87e8e18 100644
--- a/src/script/api/qscriptvalue_p.h
+++ b/src/script/api/qscriptvalue_p.h
@@ -126,7 +126,7 @@ public:
Type type;
JSC::JSValue jscValue;
double numberValue;
- QString *stringValue;
+ QString stringValue;
QBasicAtomicInt ref;
bool valid; //object is valid ?
diff --git a/tests/auto/qscriptvalue/tst_qscriptvalue.cpp b/tests/auto/qscriptvalue/tst_qscriptvalue.cpp
index 0417252..0986fd3 100644
--- a/tests/auto/qscriptvalue/tst_qscriptvalue.cpp
+++ b/tests/auto/qscriptvalue/tst_qscriptvalue.cpp
@@ -1730,7 +1730,6 @@ void tst_QScriptValue::getSetProperty()
QCOMPARE(strstr.engine(), (QScriptEngine *)0);
object.setProperty("foo", strstr);
QCOMPARE(object.property("foo").toString(), strstr.toString());
- QEXPECT_FAIL("", "String engine binding not implemented", Continue);
QCOMPARE(strstr.engine(), &eng); // the value has been bound to the engine
QScriptValue numnum = QScriptValue(123.0);
@@ -1838,7 +1837,7 @@ void tst_QScriptValue::getSetProperty()
QScriptValue::PropertyGetter | QScriptValue::UserRange);
object4.setProperty("x", num);
QCOMPARE(object4.property("foo").strictlyEquals(num), true);
-
+
// setter() sets this.x
object4.setProperty("foo", eng.newFunction(setter),
QScriptValue::PropertySetter | QScriptValue::UserRange);
@@ -1848,16 +1847,16 @@ void tst_QScriptValue::getSetProperty()
object4.setProperty("foo", str);
QCOMPARE(object4.property("x").strictlyEquals(str), true);
QCOMPARE(object4.property("foo").strictlyEquals(str), true);
-
+
// kill the getter
object4.setProperty("foo", QScriptValue(), QScriptValue::PropertyGetter);
QCOMPARE(object4.property("foo").isValid(), false);
-
+
// setter should still work
object4.setProperty("foo", num);
QEXPECT_FAIL("", "Setter isn't called", Continue);
QCOMPARE(object4.property("x").strictlyEquals(num), true);
-
+
// kill the setter too
object4.setProperty("foo", QScriptValue(), QScriptValue::PropertySetter);
// now foo is just a regular property
@@ -1875,21 +1874,21 @@ void tst_QScriptValue::getSetProperty()
QCOMPARE(object4.property("x").strictlyEquals(str), true);
QEXPECT_FAIL("", "Property should be invalid now", Continue);
QCOMPARE(object4.property("foo").isValid(), false);
-
+
// getter() returns this.x
object4.setProperty("foo", eng.newFunction(getter), QScriptValue::PropertyGetter);
object4.setProperty("x", num);
QCOMPARE(object4.property("foo").strictlyEquals(num), true);
-
+
// kill the setter
object4.setProperty("foo", QScriptValue(), QScriptValue::PropertySetter);
QTest::ignoreMessage(QtWarningMsg, "QScriptValue::setProperty() failed: property 'foo' has a getter but no setter");
object4.setProperty("foo", str);
-
+
// getter should still work
QEXPECT_FAIL("", "Getter should still work", Continue);
QCOMPARE(object4.property("foo").strictlyEquals(num), true);
-
+
// kill the getter too
object4.setProperty("foo", QScriptValue(), QScriptValue::PropertyGetter);
// now foo is just a regular property