From b02bf4ef805e33a763d86ec8ff496a27fddc8ad8 Mon Sep 17 00:00:00 2001 From: Aaron Kennedy Date: Tue, 25 May 2010 16:23:40 +1000 Subject: Revert "Reading/writing a non-existent property throws an exception" This reverts commit 2b3e7706f4459569520c77b9fb3ff2bc006e60f1. --- .../qml/qdeclarativeglobalscriptclass.cpp | 23 +++++++++++++++++++--- .../qml/qdeclarativeglobalscriptclass_p.h | 2 ++ .../qml/qdeclarativeobjectscriptclass.cpp | 14 ++++--------- .../qml/qdeclarativeobjectscriptclass_p.h | 1 - .../qdeclarativeecmascript/data/eval.qml | 2 +- .../qdeclarativeecmascript/data/function.qml | 3 +-- .../data/libraryScriptAssert.js | 2 +- .../tst_qdeclarativeecmascript.cpp | 15 +++++--------- 8 files changed, 34 insertions(+), 28 deletions(-) diff --git a/src/declarative/qml/qdeclarativeglobalscriptclass.cpp b/src/declarative/qml/qdeclarativeglobalscriptclass.cpp index 35cb2b4..6e107fb 100644 --- a/src/declarative/qml/qdeclarativeglobalscriptclass.cpp +++ b/src/declarative/qml/qdeclarativeglobalscriptclass.cpp @@ -98,9 +98,7 @@ QDeclarativeGlobalScriptClass::property(const QScriptValue &object, Q_UNUSED(object); Q_UNUSED(name); Q_UNUSED(id); - QString error = QLatin1String("Cannot access non-existent property \"") + - name.toString() + QLatin1Char('\"'); - return engine()->currentContext()->throwError(error); + return engine()->undefinedValue(); } void QDeclarativeGlobalScriptClass::setProperty(QScriptValue &object, @@ -115,5 +113,24 @@ void QDeclarativeGlobalScriptClass::setProperty(QScriptValue &object, engine()->currentContext()->throwError(error); } +/* This method is for the use of tst_qdeclarativeecmascript::callQtInvokables() only */ +void QDeclarativeGlobalScriptClass::explicitSetProperty(const QString &name, const QScriptValue &value) +{ + QScriptValue globalObject = engine()->globalObject(); + + QScriptValue v = engine()->newObject(); + + QScriptValueIterator iter(v); + while (iter.hasNext()) { + iter.next(); + v.setProperty(iter.scriptName(), iter.value()); + } + + v.setProperty(name, value); + v.setScriptClass(this); + + engine()->setGlobalObject(v); +} + QT_END_NAMESPACE diff --git a/src/declarative/qml/qdeclarativeglobalscriptclass_p.h b/src/declarative/qml/qdeclarativeglobalscriptclass_p.h index 3fe766f..7690edd 100644 --- a/src/declarative/qml/qdeclarativeglobalscriptclass_p.h +++ b/src/declarative/qml/qdeclarativeglobalscriptclass_p.h @@ -73,6 +73,8 @@ public: virtual void setProperty(QScriptValue &object, const QScriptString &name, uint id, const QScriptValue &value); + void explicitSetProperty(const QString &, const QScriptValue &); + const QScriptValue &globalObject() const { return m_globalObject; } const QSet &illegalNames() const { return m_illegalNames; } diff --git a/src/declarative/qml/qdeclarativeobjectscriptclass.cpp b/src/declarative/qml/qdeclarativeobjectscriptclass.cpp index be2be8b..aca01b2 100644 --- a/src/declarative/qml/qdeclarativeobjectscriptclass.cpp +++ b/src/declarative/qml/qdeclarativeobjectscriptclass.cpp @@ -93,7 +93,6 @@ QDeclarativeObjectScriptClass::QDeclarativeObjectScriptClass(QDeclarativeEngine m_destroyId = createPersistentIdentifier(QLatin1String("destroy")); m_toString = scriptEngine->newFunction(tostring); m_toStringId = createPersistentIdentifier(QLatin1String("toString")); - m_valueOfId = createPersistentIdentifier(QLatin1String("valueOf")); } QDeclarativeObjectScriptClass::~QDeclarativeObjectScriptClass() @@ -158,8 +157,7 @@ QDeclarativeObjectScriptClass::queryProperty(QObject *obj, const Identifier &nam lastTNData = 0; if (name == m_destroyId.identifier || - name == m_toStringId.identifier || - name == m_valueOfId.identifier) + name == m_toStringId.identifier) return QScriptClass::HandlesReadAccess; if (!obj) @@ -213,15 +211,11 @@ QDeclarativeObjectScriptClass::property(QObject *obj, const Identifier &name) if (name == m_destroyId.identifier) return Value(scriptEngine, m_destroy); - else if (name == m_toStringId.identifier || - name == m_valueOfId.identifier) + else if (name == m_toStringId.identifier) return Value(scriptEngine, m_toString); - if (lastData && !lastData->isValid()) { - QString error = QLatin1String("Cannot access non-existent property \"") + - toString(name) + QLatin1Char('\"'); - return Value(scriptEngine, context()->throwError(error)); - } + if (lastData && !lastData->isValid()) + return Value(); Q_ASSERT(obj); diff --git a/src/declarative/qml/qdeclarativeobjectscriptclass_p.h b/src/declarative/qml/qdeclarativeobjectscriptclass_p.h index 34c71a0..4b27e53 100644 --- a/src/declarative/qml/qdeclarativeobjectscriptclass_p.h +++ b/src/declarative/qml/qdeclarativeobjectscriptclass_p.h @@ -139,7 +139,6 @@ private: PersistentIdentifier m_destroyId; PersistentIdentifier m_toStringId; - PersistentIdentifier m_valueOfId; QScriptValue m_destroy; QScriptValue m_toString; diff --git a/tests/auto/declarative/qdeclarativeecmascript/data/eval.qml b/tests/auto/declarative/qdeclarativeecmascript/data/eval.qml index faa5106..bc2df98 100644 --- a/tests/auto/declarative/qdeclarativeecmascript/data/eval.qml +++ b/tests/auto/declarative/qdeclarativeecmascript/data/eval.qml @@ -16,7 +16,7 @@ QtObject { test1 = (eval("a") == 7); test2 = (eval("b") == 9); - try { eval("c") } catch(e) { test3 = true; } + test3 = (eval("c") == undefined); test4 = (eval("console") == console); test5 = (eval("Qt") == Qt); } diff --git a/tests/auto/declarative/qdeclarativeecmascript/data/function.qml b/tests/auto/declarative/qdeclarativeecmascript/data/function.qml index e524189..b435f58 100644 --- a/tests/auto/declarative/qdeclarativeecmascript/data/function.qml +++ b/tests/auto/declarative/qdeclarativeecmascript/data/function.qml @@ -14,7 +14,6 @@ QtObject { test1 = (func1(4) == 11); test2 = (func2("Hello World!") == Qt.atob("Hello World!")); - - try { func3(); } catch(e) { test3 = true; } + test3 = (func3() == undefined); } } diff --git a/tests/auto/declarative/qdeclarativeecmascript/data/libraryScriptAssert.js b/tests/auto/declarative/qdeclarativeecmascript/data/libraryScriptAssert.js index a20fc28..3ffdb33 100644 --- a/tests/auto/declarative/qdeclarativeecmascript/data/libraryScriptAssert.js +++ b/tests/auto/declarative/qdeclarativeecmascript/data/libraryScriptAssert.js @@ -2,5 +2,5 @@ function test(target) { - try { var a = target.a; } catch(e) {} + var a = target.a; } diff --git a/tests/auto/declarative/qdeclarativeecmascript/tst_qdeclarativeecmascript.cpp b/tests/auto/declarative/qdeclarativeecmascript/tst_qdeclarativeecmascript.cpp index 217ed11..e75abac 100644 --- a/tests/auto/declarative/qdeclarativeecmascript/tst_qdeclarativeecmascript.cpp +++ b/tests/auto/declarative/qdeclarativeecmascript/tst_qdeclarativeecmascript.cpp @@ -997,11 +997,11 @@ void tst_qdeclarativeecmascript::scriptErrors() QString url = component.url().toString(); QString warning1 = url.left(url.length() - 3) + "js:2: Error: Invalid write to global property \"a\""; - QString warning2 = url + ":5: Error: Cannot access non-existent property \"a\""; + QString warning2 = url + ":5: TypeError: Result of expression 'a' [undefined] is not an object."; QString warning3 = url.left(url.length() - 3) + "js:4: Error: Invalid write to global property \"a\""; - QString warning4 = url + ":10: Error: Cannot access non-existent property \"a\""; - QString warning5 = url + ":8: Error: Cannot access non-existent property \"a\""; - QString warning6 = url + ":7: Error: Cannot access non-existent property \"undefinedObject\""; + QString warning4 = url + ":10: TypeError: Result of expression 'a' [undefined] is not an object."; + QString warning5 = url + ":8: TypeError: Result of expression 'a' [undefined] is not an object."; + QString warning6 = url + ":7: Unable to assign [undefined] to int x"; QString warning7 = url + ":12: Error: Cannot assign to read-only property \"trueProperty\""; QString warning8 = url + ":13: Error: Cannot assign to non-existent property \"fakeProperty\""; @@ -1317,12 +1317,7 @@ void tst_qdeclarativeecmascript::callQtInvokables() QDeclarativeEngine qmlengine; QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(&qmlengine); QScriptEngine *engine = &ep->scriptEngine; - QScriptContext *scriptContext = QScriptDeclarativeClass::pushCleanContext(engine); - scriptContext->pushScope(ep->globalClass->globalObject()); - QScriptValue scope = engine->newObject(); - scope.setProperty("object", ep->objectClass->newQObject(&o)); - scriptContext->setActivationObject(scope); - scriptContext->pushScope(scope); + ep->globalClass->explicitSetProperty("object", ep->objectClass->newQObject(&o)); // Non-existent methods o.reset(); -- cgit v0.12