Debian Codegraph @

src/script/bridge/qscriptvariant.cpp

raw symbols SCIP
1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtScript module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include "config.h"
41 #include "qscriptvariant_p.h"
42
43 #include "../api/qscriptengine.h"
44 #include "../api/qscriptengine_p.h"
45
46 #include "Error.h"
47 #include "PrototypeFunction.h"
48 #include "JSFunction.h"
49 #include "NativeFunctionWrapper.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 = QString::fromLatin1("QVariant(%0)").arg(QString::fromLatin1(v.typeName()));
142 } else {
143 result = value.toString(exec);
144 }
145 return JSC::jsString(exec, result);
146 }
147
148 bool QVariantDelegate::compareToObject(QScriptObject *, JSC::ExecState *exec, JSC::JSObject *o2)
149 {
150 const QVariant &variant1 = value();
151 return variant1 == QScriptEnginePrivate::toVariant(exec, o2);
152 }
153
154 QVariantPrototype::QVariantPrototype(JSC::ExecState* exec, WTF::PassRefPtr<JSC::Structure> structure,
155 JSC::Structure* prototypeFunctionStructure)
156 : QScriptObject(structure)
157 {
158 setDelegate(new QVariantDelegate(QVariant()));
159
160 putDirectFunction(exec, new (exec) JSC::NativeFunctionWrapper(exec, prototypeFunctionStructure, 0, exec->propertyNames().toString, variantProtoFuncToString), JSC::DontEnum);
161 putDirectFunction(exec, new (exec) JSC::NativeFunctionWrapper(exec, prototypeFunctionStructure, 0, exec->propertyNames().valueOf, variantProtoFuncValueOf), JSC::DontEnum);
162 }
163
164
165 } // namespace QScript
166
167 QT_END_NAMESPACE