Qt 4.8
qscriptvariant.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtScript module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL-ONLY$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser
11 ** General Public License version 2.1 as published by the Free Software
12 ** Foundation and appearing in the file LICENSE.LGPL included in the
13 ** packaging of this file. Please review the following information to
14 ** ensure the GNU Lesser General Public License version 2.1 requirements
15 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** If you have questions regarding the use of this file, please contact
18 ** us via http://www.qt-project.org/.
19 **
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 "JSFunction.h"
33 #include "NativeFunctionWrapper.h"
34 #include "JSString.h"
35 
36 namespace JSC
37 {
40 }
41 
43 
44 namespace QScript
45 {
46 
47 QVariantDelegate::QVariantDelegate(const QVariant &value)
48  : m_value(value)
49 {
50 }
51 
53 {
54 }
55 
57 {
58  return m_value;
59 }
60 
62 {
63  m_value = value;
64 }
65 
67 {
68  return Variant;
69 }
70 
71 static JSC::JSValue JSC_HOST_CALL variantProtoFuncValueOf(JSC::ExecState *exec, JSC::JSObject*,
72  JSC::JSValue thisValue, const JSC::ArgList&)
73 {
75  thisValue = engine->toUsableValue(thisValue);
76  if (!thisValue.inherits(&QScriptObject::info))
77  return throwError(exec, JSC::TypeError);
78  QScriptObjectDelegate *delegate = static_cast<QScriptObject*>(JSC::asObject(thisValue))->delegate();
79  if (!delegate || (delegate->type() != QScriptObjectDelegate::Variant))
80  return throwError(exec, JSC::TypeError);
81  const QVariant &v = static_cast<QVariantDelegate*>(delegate)->value();
82  switch (v.type()) {
83  case QVariant::Invalid:
84  return JSC::jsUndefined();
85  case QVariant::String:
86  return JSC::jsString(exec, v.toString());
87 
88  case QVariant::Int:
89  return JSC::jsNumber(exec, v.toInt());
90 
91  case QVariant::Bool:
92  return JSC::jsBoolean(v.toBool());
93 
94  case QVariant::Double:
95  return JSC::jsNumber(exec, v.toDouble());
96 
97 // case QVariant::Char:
98 // return JSC::jsNumber(exec, v.toChar().unicode());
99 
100  case QVariant::UInt:
101  return JSC::jsNumber(exec, v.toUInt());
102 
103  default:
104  ;
105  }
106  return thisValue;
107 }
108 
109 static JSC::JSValue JSC_HOST_CALL variantProtoFuncToString(JSC::ExecState *exec, JSC::JSObject *callee,
110  JSC::JSValue thisValue, const JSC::ArgList &args)
111 {
113  thisValue = engine->toUsableValue(thisValue);
114  if (!thisValue.inherits(&QScriptObject::info))
115  return throwError(exec, JSC::TypeError, "This object is not a QVariant");
116  QScriptObjectDelegate *delegate = static_cast<QScriptObject*>(JSC::asObject(thisValue))->delegate();
117  if (!delegate || (delegate->type() != QScriptObjectDelegate::Variant))
118  return throwError(exec, JSC::TypeError, "This object is not a QVariant");
119  const QVariant &v = static_cast<QVariantDelegate*>(delegate)->value();
120  JSC::UString result;
121  JSC::JSValue value = variantProtoFuncValueOf(exec, callee, thisValue, args);
122  if (value.isObject()) {
123  result = v.toString();
124  if (result.isEmpty() && !v.canConvert(QVariant::String))
125  result = QString::fromLatin1("QVariant(%0)").arg(QString::fromLatin1(v.typeName()));
126  } else {
127  result = value.toString(exec);
128  }
129  return JSC::jsString(exec, result);
130 }
131 
132 bool QVariantDelegate::compareToObject(QScriptObject *, JSC::ExecState *exec, JSC::JSObject *o2)
133 {
134  const QVariant &variant1 = value();
135  return variant1 == QScriptEnginePrivate::toVariant(exec, o2);
136 }
137 
138 QVariantPrototype::QVariantPrototype(JSC::ExecState* exec, WTF::PassRefPtr<JSC::Structure> structure,
139  JSC::Structure* prototypeFunctionStructure)
140  : QScriptObject(structure)
141 {
143 
144  putDirectFunction(exec, new (exec) JSC::NativeFunctionWrapper(exec, prototypeFunctionStructure, 0, exec->propertyNames().toString, variantProtoFuncToString), JSC::DontEnum);
145  putDirectFunction(exec, new (exec) JSC::NativeFunctionWrapper(exec, prototypeFunctionStructure, 0, exec->propertyNames().valueOf, variantProtoFuncValueOf), JSC::DontEnum);
146 }
147 
148 
149 } // namespace QScript
150 
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QScriptEnginePrivate * scriptEngineFromExec(const JSC::ExecState *exec)
virtual Type type() const =0
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
ASSERT_CLASS_FITS_IN_CELL(::QScript::QScriptActivationObject)
static QVariant toVariant(JSC::ExecState *, JSC::JSValue)
QString toString() const
Returns the variant as a QString if the variant has type() String , Bool , ByteArray ...
Definition: qvariant.cpp:2270
bool toBool() const
Returns the variant as a bool if the variant has type() Bool.
Definition: qvariant.cpp:2691
static const JSC::ClassInfo info
int toInt(bool *ok=0) const
Returns the variant as an int if the variant has type() Int , Bool , ByteArray , Char ...
Definition: qvariant.cpp:2625
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
bool canConvert(Type t) const
Returns true if the variant&#39;s type can be cast to the requested type, t.
Definition: qvariant.cpp:2886
JSC::JSValue toUsableValue(JSC::JSValue value)
If the given value is the original global object, returns the custom global object or a proxy to the ...
QVariantPrototype(JSC::ExecState *, WTF::PassRefPtr< JSC::Structure >, JSC::Structure *prototypeFunctionStructure)
static JSC::JSValue JSC_HOST_CALL variantProtoFuncValueOf(JSC::ExecState *exec, JSC::JSObject *, JSC::JSValue thisValue, const JSC::ArgList &)
const char * typeName() const
Returns the name of the type stored in the variant.
Definition: qvariant.cpp:1984
virtual bool compareToObject(QScriptObject *, JSC::ExecState *, JSC::JSObject *)
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
Type type() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1901
void setValue(const QVariant &value)
uint toUInt(bool *ok=0) const
Returns the variant as an unsigned int if the variant has type() UInt , Bool , ByteArray ...
Definition: qvariant.cpp:2644
double toDouble(bool *ok=0) const
Returns the variant as a double if the variant has type() Double , QMetaType::Float ...
Definition: qvariant.cpp:2710
#define QT_USE_NAMESPACE
This macro expands to using QT_NAMESPACE if QT_NAMESPACE is defined and nothing otherwise.
Definition: qglobal.h:88
void setDelegate(QScriptObjectDelegate *delegate)
static JSC::JSValue JSC_HOST_CALL variantProtoFuncToString(JSC::ExecState *exec, JSC::JSObject *callee, JSC::JSValue thisValue, const JSC::ArgList &args)