source: trunk/src/script/bridge/qscriptvariant.cpp@ 1028

Last change on this file since 1028 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

  • Property svn:eol-style set to native
File size: 5.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
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 [email protected].
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
36namespace JSC
37{
38QT_USE_NAMESPACE
39ASSERT_CLASS_FITS_IN_CELL(QScript::QVariantPrototype);
40}
41
42QT_BEGIN_NAMESPACE
43
44namespace QScript
45{
46
47QVariantDelegate::QVariantDelegate(const QVariant &value)
48 : m_value(value)
49{
50}
51
52QVariantDelegate::~QVariantDelegate()
53{
54}
55
56QVariant &QVariantDelegate::value()
57{
58 return m_value;
59}
60
61void QVariantDelegate::setValue(const QVariant &value)
62{
63 m_value = value;
64}
65
66QScriptObjectDelegate::Type QVariantDelegate::type() const
67{
68 return Variant;
69}
70
71static JSC::JSValue JSC_HOST_CALL variantProtoFuncValueOf(JSC::ExecState *exec, JSC::JSObject*,
72 JSC::JSValue thisValue, const JSC::ArgList&)
73{
74 QScriptEnginePrivate *engine = scriptEngineFromExec(exec);
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
109static JSC::JSValue JSC_HOST_CALL variantProtoFuncToString(JSC::ExecState *exec, JSC::JSObject *callee,
110 JSC::JSValue thisValue, const JSC::ArgList &args)
111{
112 QScriptEnginePrivate *engine = scriptEngineFromExec(exec);
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
132bool QVariantDelegate::compareToObject(QScriptObject *, JSC::ExecState *exec, JSC::JSObject *o2)
133{
134 const QVariant &variant1 = value();
135 return variant1 == QScriptEnginePrivate::toVariant(exec, o2);
136}
137
138QVariantPrototype::QVariantPrototype(JSC::ExecState* exec, WTF::PassRefPtr<JSC::Structure> structure,
139 JSC::Structure* prototypeFunctionStructure)
140 : QScriptObject(structure)
141{
142 setDelegate(new QVariantDelegate(QVariant()));
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
151QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.