util/src/script/bridge/qscriptvariant.cpp
changeset 7 f7bc934e204c
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtScript module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL-ONLY$
       
    10 ** GNU Lesser General Public License Usage
       
    11 ** This file may be used under the terms of the GNU Lesser
       
    12 ** General Public License version 2.1 as published by the Free Software
       
    13 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    14 ** packaging of this file.  Please review the following information to
       
    15 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    16 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    17 **
       
    18 ** If you have questions regarding the use of this file, please contact
       
    19 ** Nokia at qt-info@nokia.com.
       
    20 ** $QT_END_LICENSE$
       
    21 **
       
    22 ****************************************************************************/
       
    23 
       
    24 #include "config.h"
       
    25 #include "qscriptvariant_p.h"
       
    26 
       
    27 #include "../api/qscriptengine.h"
       
    28 #include "../api/qscriptengine_p.h"
       
    29 
       
    30 #include "Error.h"
       
    31 #include "PrototypeFunction.h"
       
    32 #include "JSString.h"
       
    33 
       
    34 namespace JSC
       
    35 {
       
    36 QT_USE_NAMESPACE
       
    37 ASSERT_CLASS_FITS_IN_CELL(QScript::QVariantPrototype);
       
    38 }
       
    39 
       
    40 QT_BEGIN_NAMESPACE
       
    41 
       
    42 namespace QScript
       
    43 {
       
    44 
       
    45 QVariantDelegate::QVariantDelegate(const QVariant &value)
       
    46     : m_value(value)
       
    47 {
       
    48 }
       
    49 
       
    50 QVariantDelegate::~QVariantDelegate()
       
    51 {
       
    52 }
       
    53 
       
    54 QVariant &QVariantDelegate::value()
       
    55 {
       
    56     return m_value;
       
    57 }
       
    58 
       
    59 void QVariantDelegate::setValue(const QVariant &value)
       
    60 {
       
    61     m_value = value;
       
    62 }
       
    63 
       
    64 QScriptObjectDelegate::Type QVariantDelegate::type() const
       
    65 {
       
    66     return Variant;
       
    67 }
       
    68 
       
    69 static JSC::JSValue JSC_HOST_CALL variantProtoFuncValueOf(JSC::ExecState *exec, JSC::JSObject*,
       
    70                                                           JSC::JSValue thisValue, const JSC::ArgList&)
       
    71 {
       
    72     QScriptEnginePrivate *engine = scriptEngineFromExec(exec);
       
    73     thisValue = engine->toUsableValue(thisValue);
       
    74     if (!thisValue.inherits(&QScriptObject::info))
       
    75         return throwError(exec, JSC::TypeError);
       
    76     QScriptObjectDelegate *delegate = static_cast<QScriptObject*>(JSC::asObject(thisValue))->delegate();
       
    77     if (!delegate || (delegate->type() != QScriptObjectDelegate::Variant))
       
    78         return throwError(exec, JSC::TypeError);
       
    79     const QVariant &v = static_cast<QVariantDelegate*>(delegate)->value();
       
    80     switch (v.type()) {
       
    81     case QVariant::Invalid:
       
    82         return JSC::jsUndefined();
       
    83     case QVariant::String:
       
    84         return JSC::jsString(exec, v.toString());
       
    85 
       
    86     case QVariant::Int:
       
    87         return JSC::jsNumber(exec, v.toInt());
       
    88 
       
    89     case QVariant::Bool:
       
    90         return JSC::jsBoolean(v.toBool());
       
    91 
       
    92     case QVariant::Double:
       
    93         return JSC::jsNumber(exec, v.toDouble());
       
    94 
       
    95 //    case QVariant::Char:
       
    96 //        return JSC::jsNumber(exec, v.toChar().unicode());
       
    97 
       
    98     case QVariant::UInt:
       
    99         return JSC::jsNumber(exec, v.toUInt());
       
   100 
       
   101     default:
       
   102         ;
       
   103     }
       
   104     return thisValue;
       
   105 }
       
   106 
       
   107 static JSC::JSValue JSC_HOST_CALL variantProtoFuncToString(JSC::ExecState *exec, JSC::JSObject *callee,
       
   108                                                            JSC::JSValue thisValue, const JSC::ArgList &args)
       
   109 {
       
   110     QScriptEnginePrivate *engine = scriptEngineFromExec(exec);
       
   111     thisValue = engine->toUsableValue(thisValue);
       
   112     if (!thisValue.inherits(&QScriptObject::info))
       
   113         return throwError(exec, JSC::TypeError, "This object is not a QVariant");
       
   114     QScriptObjectDelegate *delegate = static_cast<QScriptObject*>(JSC::asObject(thisValue))->delegate();
       
   115     if (!delegate || (delegate->type() != QScriptObjectDelegate::Variant))
       
   116         return throwError(exec, JSC::TypeError, "This object is not a QVariant");
       
   117     const QVariant &v = static_cast<QVariantDelegate*>(delegate)->value();
       
   118     JSC::UString result;
       
   119     JSC::JSValue value = variantProtoFuncValueOf(exec, callee, thisValue, args);
       
   120     if (value.isObject()) {
       
   121         result = v.toString();
       
   122         if (result.isEmpty() && !v.canConvert(QVariant::String)) {
       
   123             result = "QVariant(";
       
   124             result += v.typeName();
       
   125             result += ")";
       
   126         }
       
   127     } else {
       
   128         result = value.toString(exec);
       
   129     }
       
   130     return JSC::jsString(exec, result);
       
   131 }
       
   132 
       
   133 bool QVariantDelegate::compareToObject(QScriptObject *, JSC::ExecState *exec, JSC::JSObject *o2)
       
   134 {
       
   135     const QVariant &variant1 = value();
       
   136     return variant1 == scriptEngineFromExec(exec)->scriptValueFromJSCValue(o2).toVariant();
       
   137 }
       
   138 
       
   139 QVariantPrototype::QVariantPrototype(JSC::ExecState* exec, WTF::PassRefPtr<JSC::Structure> structure,
       
   140                                      JSC::Structure* prototypeFunctionStructure)
       
   141     : QScriptObject(structure)
       
   142 {
       
   143     setDelegate(new QVariantDelegate(QVariant()));
       
   144 
       
   145     putDirectFunction(exec, new (exec) JSC::PrototypeFunction(exec, prototypeFunctionStructure, 0, exec->propertyNames().toString, variantProtoFuncToString), JSC::DontEnum);
       
   146     putDirectFunction(exec, new (exec) JSC::PrototypeFunction(exec, prototypeFunctionStructure, 0, exec->propertyNames().valueOf, variantProtoFuncValueOf), JSC::DontEnum);
       
   147 }
       
   148 
       
   149 
       
   150 } // namespace QScript
       
   151 
       
   152 QT_END_NAMESPACE