src/script/api/qscriptvalue.cpp
changeset 7 f7bc934e204c
parent 3 41300fa6a67c
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
     1 /****************************************************************************
     1 /****************************************************************************
     2 **
     2 **
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
     4 ** All rights reserved.
     4 ** All rights reserved.
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     6 **
     6 **
     7 ** This file is part of the QtScript module of the Qt Toolkit.
     7 ** This file is part of the QtScript module of the Qt Toolkit.
     8 **
     8 **
    52 #include "bridge/qscriptobject_p.h"
    52 #include "bridge/qscriptobject_p.h"
    53 #include "bridge/qscriptclassobject_p.h"
    53 #include "bridge/qscriptclassobject_p.h"
    54 #include "bridge/qscriptvariant_p.h"
    54 #include "bridge/qscriptvariant_p.h"
    55 #include "bridge/qscriptqobject_p.h"
    55 #include "bridge/qscriptqobject_p.h"
    56 #include "bridge/qscriptdeclarativeclass_p.h"
    56 #include "bridge/qscriptdeclarativeclass_p.h"
       
    57 #include "bridge/qscriptdeclarativeobject_p.h"
    57 
    58 
    58 /*!
    59 /*!
    59   \since 4.3
    60   \since 4.3
    60   \class QScriptValue
    61   \class QScriptValue
    61 
    62 
  1145                  "a different engine");
  1146                  "a different engine");
  1146         return false;
  1147         return false;
  1147     }
  1148     }
  1148 
  1149 
  1149     if (d->type != other.d_ptr->type) {
  1150     if (d->type != other.d_ptr->type) {
  1150         if (d->type == QScriptValuePrivate::JavaScriptCore)
  1151         if (d->type == QScriptValuePrivate::JavaScriptCore) {
  1151             return JSC::JSValue::strictEqual(d->jscValue, d->engine->scriptValueToJSCValue(other));
  1152             QScriptEnginePrivate *eng_p = d->engine ? d->engine : other.d_ptr->engine;
  1152         else if (other.d_ptr->type == QScriptValuePrivate::JavaScriptCore)
  1153             if (eng_p)
  1153             return JSC::JSValue::strictEqual(other.d_ptr->engine->scriptValueToJSCValue(*this), other.d_ptr->jscValue);
  1154                 return JSC::JSValue::strictEqual(d->jscValue, eng_p->scriptValueToJSCValue(other));
       
  1155         } else if (other.d_ptr->type == QScriptValuePrivate::JavaScriptCore) {
       
  1156             QScriptEnginePrivate *eng_p = other.d_ptr->engine ? other.d_ptr->engine : d->engine;
       
  1157             if (eng_p)
       
  1158                 return JSC::JSValue::strictEqual(eng_p->scriptValueToJSCValue(*this), other.d_ptr->jscValue);
       
  1159         }
  1154 
  1160 
  1155         return false;
  1161         return false;
  1156     }
  1162     }
  1157     switch (d->type) {
  1163     switch (d->type) {
  1158     case QScriptValuePrivate::JavaScriptCore:
  1164     case QScriptValuePrivate::JavaScriptCore:
  1568 QObject *QScriptValue::toQObject() const
  1574 QObject *QScriptValue::toQObject() const
  1569 {
  1575 {
  1570     Q_D(const QScriptValue);
  1576     Q_D(const QScriptValue);
  1571     if (isQObject()) {
  1577     if (isQObject()) {
  1572         QScriptObject *object = static_cast<QScriptObject*>(JSC::asObject(d->jscValue));
  1578         QScriptObject *object = static_cast<QScriptObject*>(JSC::asObject(d->jscValue));
  1573         return static_cast<QScript::QObjectDelegate*>(object->delegate())->value();
  1579         QScriptObjectDelegate *delegate = object->delegate();
  1574     } else if (QScriptDeclarativeClass *dc = QScriptDeclarativeClass::scriptClass(*this)) {
  1580         if (delegate->type() == QScriptObjectDelegate::DeclarativeClassObject)
  1575         return dc->toQObject(QScriptDeclarativeClass::object(*this));
  1581             return static_cast<QScript::DeclarativeObjectDelegate*>(delegate)->scriptClass()->toQObject(QScriptDeclarativeClass::object(*this));
       
  1582         return static_cast<QScript::QObjectDelegate*>(delegate)->value();
  1576     } else if (isVariant()) {
  1583     } else if (isVariant()) {
  1577         QVariant var = toVariant();
  1584         QVariant var = toVariant();
  1578         int type = var.userType();
  1585         int type = var.userType();
  1579         if ((type == QMetaType::QObjectStar) || (type == QMetaType::QWidgetStar))
  1586         if ((type == QMetaType::QObjectStar) || (type == QMetaType::QWidgetStar))
  1580             return *reinterpret_cast<QObject* const *>(var.constData());
  1587             return *reinterpret_cast<QObject* const *>(var.constData());
  1937     JSC::JSValue array = d->engine->scriptValueToJSCValue(arguments);
  1944     JSC::JSValue array = d->engine->scriptValueToJSCValue(arguments);
  1938     // copied from runtime/FunctionPrototype.cpp, functionProtoFuncApply()
  1945     // copied from runtime/FunctionPrototype.cpp, functionProtoFuncApply()
  1939     JSC::MarkedArgumentBuffer applyArgs;
  1946     JSC::MarkedArgumentBuffer applyArgs;
  1940     if (!array.isUndefinedOrNull()) {
  1947     if (!array.isUndefinedOrNull()) {
  1941         if (!array.isObject()) {
  1948         if (!array.isObject()) {
  1942             return d->engine->scriptValueFromJSCValue(JSC::throwError(exec, JSC::TypeError));
  1949             return d->engine->scriptValueFromJSCValue(JSC::throwError(exec, JSC::TypeError, "Arguments must be an array"));
  1943         }
  1950         }
  1944         if (JSC::asObject(array)->classInfo() == &JSC::Arguments::info)
  1951         if (JSC::asObject(array)->classInfo() == &JSC::Arguments::info)
  1945             JSC::asArguments(array)->fillArgList(exec, applyArgs);
  1952             JSC::asArguments(array)->fillArgList(exec, applyArgs);
  1946         else if (JSC::isJSArray(&exec->globalData(), array))
  1953         else if (JSC::isJSArray(&exec->globalData(), array))
  1947             JSC::asArray(array)->fillArgList(exec, applyArgs);
  1954             JSC::asArray(array)->fillArgList(exec, applyArgs);
  1948         else if (JSC::asObject(array)->inherits(&JSC::JSArray::info)) {
  1955         else if (JSC::asObject(array)->inherits(&JSC::JSArray::info)) {
  1949             unsigned length = JSC::asArray(array)->get(exec, exec->propertyNames().length).toUInt32(exec);
  1956             unsigned length = JSC::asArray(array)->get(exec, exec->propertyNames().length).toUInt32(exec);
  1950             for (unsigned i = 0; i < length; ++i)
  1957             for (unsigned i = 0; i < length; ++i)
  1951                 applyArgs.append(JSC::asArray(array)->get(exec, i));
  1958                 applyArgs.append(JSC::asArray(array)->get(exec, i));
  1952         } else {
  1959         } else {
  1953             Q_ASSERT_X(false, Q_FUNC_INFO, "implement me");
  1960             return d->engine->scriptValueFromJSCValue(JSC::throwError(exec, JSC::TypeError, "Arguments must be an array"));
  1954 //            return JSC::throwError(exec, JSC::TypeError);
       
  1955         }
  1961         }
  1956     }
  1962     }
  1957 
  1963 
  1958     JSC::JSValue savedException;
  1964     JSC::JSValue savedException;
  1959     QScriptValuePrivate::saveException(exec, &savedException);
  1965     QScriptValuePrivate::saveException(exec, &savedException);
  2243     Q_D(const QScriptValue);
  2249     Q_D(const QScriptValue);
  2244     if (!d || !d->isJSC() || !d->jscValue.inherits(&QScriptObject::info))
  2250     if (!d || !d->isJSC() || !d->jscValue.inherits(&QScriptObject::info))
  2245         return false;
  2251         return false;
  2246     QScriptObject *object = static_cast<QScriptObject*>(JSC::asObject(d->jscValue));
  2252     QScriptObject *object = static_cast<QScriptObject*>(JSC::asObject(d->jscValue));
  2247     QScriptObjectDelegate *delegate = object->delegate();
  2253     QScriptObjectDelegate *delegate = object->delegate();
  2248     return (delegate && (delegate->type() == QScriptObjectDelegate::QtObject));
  2254     return (delegate && (delegate->type() == QScriptObjectDelegate::QtObject ||
       
  2255                          (delegate->type() == QScriptObjectDelegate::DeclarativeClassObject &&
       
  2256                           static_cast<QScript::DeclarativeObjectDelegate*>(delegate)->scriptClass()->isQObject())));
  2249 }
  2257 }
  2250 
  2258 
  2251 /*!
  2259 /*!
  2252   Returns true if this QScriptValue is a QMetaObject; otherwise returns
  2260   Returns true if this QScriptValue is a QMetaObject; otherwise returns
  2253   false.
  2261   false.