|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 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$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "config.h" |
|
43 #include "qscriptvariant_p.h" |
|
44 |
|
45 #include "../api/qscriptengine.h" |
|
46 #include "../api/qscriptengine_p.h" |
|
47 |
|
48 #include "Error.h" |
|
49 #include "PrototypeFunction.h" |
|
50 #include "JSString.h" |
|
51 |
|
52 namespace JSC |
|
53 { |
|
54 QT_USE_NAMESPACE |
|
55 ASSERT_CLASS_FITS_IN_CELL(QScript::QVariantPrototype); |
|
56 } |
|
57 |
|
58 QT_BEGIN_NAMESPACE |
|
59 |
|
60 namespace QScript |
|
61 { |
|
62 |
|
63 QVariantDelegate::QVariantDelegate(const QVariant &value) |
|
64 : m_value(value) |
|
65 { |
|
66 } |
|
67 |
|
68 QVariantDelegate::~QVariantDelegate() |
|
69 { |
|
70 } |
|
71 |
|
72 QVariant &QVariantDelegate::value() |
|
73 { |
|
74 return m_value; |
|
75 } |
|
76 |
|
77 void QVariantDelegate::setValue(const QVariant &value) |
|
78 { |
|
79 m_value = value; |
|
80 } |
|
81 |
|
82 QScriptObjectDelegate::Type QVariantDelegate::type() const |
|
83 { |
|
84 return Variant; |
|
85 } |
|
86 |
|
87 static JSC::JSValue JSC_HOST_CALL variantProtoFuncValueOf(JSC::ExecState *exec, JSC::JSObject*, |
|
88 JSC::JSValue thisValue, const JSC::ArgList&) |
|
89 { |
|
90 QScriptEnginePrivate *engine = scriptEngineFromExec(exec); |
|
91 thisValue = engine->toUsableValue(thisValue); |
|
92 if (!thisValue.inherits(&QScriptObject::info)) |
|
93 return throwError(exec, JSC::TypeError); |
|
94 QScriptObjectDelegate *delegate = static_cast<QScriptObject*>(JSC::asObject(thisValue))->delegate(); |
|
95 if (!delegate || (delegate->type() != QScriptObjectDelegate::Variant)) |
|
96 return throwError(exec, JSC::TypeError); |
|
97 const QVariant &v = static_cast<QVariantDelegate*>(delegate)->value(); |
|
98 switch (v.type()) { |
|
99 case QVariant::Invalid: |
|
100 return JSC::jsUndefined(); |
|
101 case QVariant::String: |
|
102 return JSC::jsString(exec, v.toString()); |
|
103 |
|
104 case QVariant::Int: |
|
105 return JSC::jsNumber(exec, v.toInt()); |
|
106 |
|
107 case QVariant::Bool: |
|
108 return JSC::jsBoolean(v.toBool()); |
|
109 |
|
110 case QVariant::Double: |
|
111 return JSC::jsNumber(exec, v.toDouble()); |
|
112 |
|
113 // case QVariant::Char: |
|
114 // return JSC::jsNumber(exec, v.toChar().unicode()); |
|
115 |
|
116 case QVariant::UInt: |
|
117 return JSC::jsNumber(exec, v.toUInt()); |
|
118 |
|
119 default: |
|
120 ; |
|
121 } |
|
122 return thisValue; |
|
123 } |
|
124 |
|
125 static JSC::JSValue JSC_HOST_CALL variantProtoFuncToString(JSC::ExecState *exec, JSC::JSObject *callee, |
|
126 JSC::JSValue thisValue, const JSC::ArgList &args) |
|
127 { |
|
128 QScriptEnginePrivate *engine = scriptEngineFromExec(exec); |
|
129 thisValue = engine->toUsableValue(thisValue); |
|
130 if (!thisValue.inherits(&QScriptObject::info)) |
|
131 return throwError(exec, JSC::TypeError, "This object is not a QVariant"); |
|
132 QScriptObjectDelegate *delegate = static_cast<QScriptObject*>(JSC::asObject(thisValue))->delegate(); |
|
133 if (!delegate || (delegate->type() != QScriptObjectDelegate::Variant)) |
|
134 return throwError(exec, JSC::TypeError, "This object is not a QVariant"); |
|
135 const QVariant &v = static_cast<QVariantDelegate*>(delegate)->value(); |
|
136 JSC::UString result; |
|
137 JSC::JSValue value = variantProtoFuncValueOf(exec, callee, thisValue, args); |
|
138 if (value.isObject()) { |
|
139 result = v.toString(); |
|
140 if (result.isEmpty() && !v.canConvert(QVariant::String)) { |
|
141 result = "QVariant("; |
|
142 result += v.typeName(); |
|
143 result += ")"; |
|
144 } |
|
145 } else { |
|
146 result = value.toString(exec); |
|
147 } |
|
148 return JSC::jsString(exec, result); |
|
149 } |
|
150 |
|
151 bool QVariantDelegate::compareToObject(QScriptObject *, JSC::ExecState *exec, JSC::JSObject *o2) |
|
152 { |
|
153 const QVariant &variant1 = value(); |
|
154 return variant1 == scriptEngineFromExec(exec)->scriptValueFromJSCValue(o2).toVariant(); |
|
155 } |
|
156 |
|
157 QVariantPrototype::QVariantPrototype(JSC::ExecState* exec, WTF::PassRefPtr<JSC::Structure> structure, |
|
158 JSC::Structure* prototypeFunctionStructure) |
|
159 : QScriptObject(structure) |
|
160 { |
|
161 setDelegate(new QVariantDelegate(QVariant())); |
|
162 |
|
163 putDirectFunction(exec, new (exec) JSC::PrototypeFunction(exec, prototypeFunctionStructure, 0, exec->propertyNames().toString, variantProtoFuncToString), JSC::DontEnum); |
|
164 putDirectFunction(exec, new (exec) JSC::PrototypeFunction(exec, prototypeFunctionStructure, 0, exec->propertyNames().valueOf, variantProtoFuncValueOf), JSC::DontEnum); |
|
165 } |
|
166 |
|
167 |
|
168 } // namespace QScript |
|
169 |
|
170 QT_END_NAMESPACE |