From be03a2e9d79e8fa4440f5c6b2760e8b37ff5fa8a Mon Sep 17 00:00:00 2001 From: Kent Hansen Date: Thu, 6 Aug 2009 13:21:18 +0200 Subject: finish implementation of QVariant.prototype.toString() Behave like the old back-end. --- src/script/bridge/qscriptvariant.cpp | 40 +++++++++++++++----------- tests/auto/qscriptengine/tst_qscriptengine.cpp | 1 - 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/script/bridge/qscriptvariant.cpp b/src/script/bridge/qscriptvariant.cpp index f69a4ea..87f22d9 100644 --- a/src/script/bridge/qscriptvariant.cpp +++ b/src/script/bridge/qscriptvariant.cpp @@ -84,21 +84,6 @@ QScriptObjectDelegate::Type QVariantDelegate::type() const return Variant; } -static JSC::JSValue JSC_HOST_CALL variantProtoFuncToString(JSC::ExecState *exec, JSC::JSObject*, - JSC::JSValue thisValue, const JSC::ArgList&) -{ - QScriptEnginePrivate *engine = scriptEngineFromExec(exec); - thisValue = engine->toUsableValue(thisValue); - if (!thisValue.isObject(&QScriptObject::info)) - return throwError(exec, JSC::TypeError, "This object is not a QVariant"); - QScriptObjectDelegate *delegate = static_cast(JSC::asObject(thisValue))->delegate(); - if (!delegate || (delegate->type() != QScriptObjectDelegate::Variant)) - return throwError(exec, JSC::TypeError, "This object is not a QVariant"); - const QVariant &v = static_cast(delegate)->value(); - // ### call valueOf() - return JSC::jsString(exec, QScript::qtStringToJSCUString(v.toString())); -} - static JSC::JSValue JSC_HOST_CALL variantProtoFuncValueOf(JSC::ExecState *exec, JSC::JSObject*, JSC::JSValue thisValue, const JSC::ArgList&) { @@ -113,7 +98,6 @@ static JSC::JSValue JSC_HOST_CALL variantProtoFuncValueOf(JSC::ExecState *exec, switch (v.type()) { case QVariant::Invalid: return JSC::jsUndefined(); - case QVariant::String: return JSC::jsString(exec, QScript::qtStringToJSCUString(v.toString())); @@ -131,12 +115,36 @@ static JSC::JSValue JSC_HOST_CALL variantProtoFuncValueOf(JSC::ExecState *exec, case QVariant::UInt: return JSC::jsNumber(exec, v.toUInt()); + default: ; } return thisValue; } +static JSC::JSValue JSC_HOST_CALL variantProtoFuncToString(JSC::ExecState *exec, JSC::JSObject *callee, + JSC::JSValue thisValue, const JSC::ArgList &args) +{ + QScriptEnginePrivate *engine = scriptEngineFromExec(exec); + thisValue = engine->toUsableValue(thisValue); + if (!thisValue.isObject(&QScriptObject::info)) + return throwError(exec, JSC::TypeError, "This object is not a QVariant"); + QScriptObjectDelegate *delegate = static_cast(JSC::asObject(thisValue))->delegate(); + if (!delegate || (delegate->type() != QScriptObjectDelegate::Variant)) + return throwError(exec, JSC::TypeError, "This object is not a QVariant"); + const QVariant &v = static_cast(delegate)->value(); + JSC::UString result; + JSC::JSValue value = variantProtoFuncValueOf(exec, callee, thisValue, args); + if (value && !value.isObject()) + result = value.toString(exec); + if (result.isEmpty()) { + result = "QVariant("; + result += v.typeName(); + result += ")"; + } + return JSC::jsString(exec, result); +} + QVariantPrototype::QVariantPrototype(JSC::ExecState* exec, WTF::PassRefPtr structure, JSC::Structure* prototypeFunctionStructure) : QScriptObject(structure) diff --git a/tests/auto/qscriptengine/tst_qscriptengine.cpp b/tests/auto/qscriptengine/tst_qscriptengine.cpp index fca0983..896a860 100644 --- a/tests/auto/qscriptengine/tst_qscriptengine.cpp +++ b/tests/auto/qscriptengine/tst_qscriptengine.cpp @@ -444,7 +444,6 @@ void tst_QScriptEngine::newVariant() QScriptValue value = object.property("valueOf").call(object); QVERIFY(value.isObject()); QVERIFY(value.strictlyEquals(object)); - QEXPECT_FAIL("", "QVariant.prototype.toString is not correctly implemented", Continue); QCOMPARE(object.toString(), QString::fromLatin1("QVariant(QPoint)")); } } -- cgit v0.12