Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / src / declarative / qml / v8 / qjsvalue_impl_p.h
index adff6ce..28b928d 100644 (file)
@@ -1,8 +1,7 @@
 /****************************************************************************
 **
-** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
-** All rights reserved.
-** Contact: Nokia Corporation (qt-info@nokia.com)
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
 **
 ** This file is part of the QtScript module of the Qt Toolkit.
 **
@@ -16,7 +15,8 @@
 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 **
 ** If you have questions regarding the use of this file, please contact
-** Nokia at qt-info@nokia.com.
+** us via http://www.qt-project.org/.
+**
 ** $QT_END_LICENSE$
 **
 ****************************************************************************/
 
 QT_BEGIN_NAMESPACE
 
+// This template is used indirectly by the Q_GLOBAL_STATIC macro below
+template<>
+class QGlobalStaticDeleter<QJSValuePrivate>
+{
+public:
+    QGlobalStatic<QJSValuePrivate> &globalStatic;
+    QGlobalStaticDeleter(QGlobalStatic<QJSValuePrivate> &_globalStatic)
+        : globalStatic(_globalStatic)
+    {
+        globalStatic.pointer.load()->ref.ref();
+    }
+
+    inline ~QGlobalStaticDeleter()
+    {
+        if (!globalStatic.pointer.load()->ref.deref()) { // Logic copy & paste from SharedDataPointer
+            delete globalStatic.pointer.load();
+        }
+        globalStatic.pointer.store(0);
+        globalStatic.destroyed = true;
+    }
+};
+
+Q_GLOBAL_STATIC(QJSValuePrivate, InvalidValue)
 
 QJSValuePrivate* QJSValuePrivate::get(const QJSValue& q) { Q_ASSERT(q.d_ptr.data()); return q.d_ptr.data(); }
 
@@ -803,6 +826,8 @@ inline QScriptPassPointer<QJSValuePrivate> QJSValuePrivate::property(const QStri
 {
     if (!name.length())
         return InvalidValue();
+    if (!isObject())
+        return InvalidValue();
 
     v8::HandleScope handleScope;
     return property(QJSConverter::toString(name));
@@ -855,6 +880,26 @@ inline bool QJSValuePrivate::deleteProperty(const QString& name)
     return self->Delete(QJSConverter::toString(name));
 }
 
+inline bool QJSValuePrivate::hasProperty(const QString &name) const
+{
+    if (!isObject())
+        return false;
+
+    v8::HandleScope handleScope;
+    v8::Handle<v8::Object> self(v8::Handle<v8::Object>::Cast(m_value));
+    return self->Has(QJSConverter::toString(name));
+}
+
+inline bool QJSValuePrivate::hasOwnProperty(const QString &name) const
+{
+    if (!isObject())
+        return false;
+
+    v8::HandleScope handleScope;
+    v8::Handle<v8::Object> self(v8::Handle<v8::Object>::Cast(m_value));
+    return self->HasOwnProperty(QJSConverter::toString(name));
+}
+
 inline QJSValue::PropertyFlags QJSValuePrivate::propertyFlags(const QString& name) const
 {
     if (!isObject())
@@ -929,7 +974,7 @@ QScriptPassPointer<QJSValuePrivate> QJSValuePrivate::call(QJSValuePrivate* thisO
     return new QJSValuePrivate(e, result);
 }
 
-inline QScriptPassPointer<QJSValuePrivate> QJSValuePrivate::construct(int argc, v8::Handle<v8::Value> *argv)
+inline QScriptPassPointer<QJSValuePrivate> QJSValuePrivate::callAsConstructor(int argc, v8::Handle<v8::Value> *argv)
 {
     QV8Engine *e = engine();
 
@@ -950,7 +995,7 @@ inline QScriptPassPointer<QJSValuePrivate> QJSValuePrivate::construct(int argc,
     return new QJSValuePrivate(e, result);
 }
 
-inline QScriptPassPointer<QJSValuePrivate> QJSValuePrivate::construct(const QJSValueList& args)
+inline QScriptPassPointer<QJSValuePrivate> QJSValuePrivate::callAsConstructor(const QJSValueList& args)
 {
     if (!isCallable())
         return InvalidValue();
@@ -961,11 +1006,11 @@ inline QScriptPassPointer<QJSValuePrivate> QJSValuePrivate::construct(const QJSV
     int argc = args.size();
     QVarLengthArray<v8::Handle<v8::Value>, 8> argv(argc);
     if (!prepareArgumentsForCall(argv.data(), args)) {
-        qWarning("QJSValue::construct() failed: cannot construct function with argument created in a different engine");
+        qWarning("QJSValue::callAsConstructor() failed: cannot construct function with argument created in a different engine");
         return InvalidValue();
     }
 
-    return construct(argc, argv.data());
+    return callAsConstructor(argc, argv.data());
 }
 
 /*! \internal
@@ -1013,12 +1058,14 @@ bool QJSValuePrivate::assignEngine(QV8Engine* engine)
 
 /*!
   \internal
-  reinitialize this value to an invalid value.
+  Invalidates this value.
+
+  Does not remove the value from the engine's list of
+  registered values; that's the responsibility of the caller.
 */
-void QJSValuePrivate::reinitialize()
+void QJSValuePrivate::invalidate()
 {
     if (isJSBased()) {
-        m_engine->unregisterValue(this);
         m_value.Dispose();
         m_value.Clear();
     } else if (isStringBased()) {
@@ -1028,26 +1075,6 @@ void QJSValuePrivate::reinitialize()
     m_state = Invalid;
 }
 
-/*!
-  \internal
-  reinitialize this value to an JSValue.
-*/
-void QJSValuePrivate::reinitialize(QV8Engine* engine, v8::Handle<v8::Value> value)
-{
-    Q_ASSERT_X(this != InvalidValue(), Q_FUNC_INFO, "static invalid can't be reinitialized to a different value");
-    if (isJSBased()) {
-        m_value.Dispose();
-        // avoid double registration
-        m_engine->unregisterValue(this);
-    } else if (isStringBased()) {
-        delete u.m_string;
-    }
-    m_engine = engine;
-    m_state = JSValue;
-    m_value = v8::Persistent<v8::Value>::New(value);
-    m_engine->registerValue(this);
-}
-
 QV8Engine* QJSValuePrivate::engine() const
 {
     return m_engine;