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 |
|
---|
36 | namespace JSC
|
---|
37 | {
|
---|
38 | QT_USE_NAMESPACE
|
---|
39 | ASSERT_CLASS_FITS_IN_CELL(QScript::QVariantPrototype);
|
---|
40 | }
|
---|
41 |
|
---|
42 | QT_BEGIN_NAMESPACE
|
---|
43 |
|
---|
44 | namespace QScript
|
---|
45 | {
|
---|
46 |
|
---|
47 | QVariantDelegate::QVariantDelegate(const QVariant &value)
|
---|
48 | : m_value(value)
|
---|
49 | {
|
---|
50 | }
|
---|
51 |
|
---|
52 | QVariantDelegate::~QVariantDelegate()
|
---|
53 | {
|
---|
54 | }
|
---|
55 |
|
---|
56 | QVariant &QVariantDelegate::value()
|
---|
57 | {
|
---|
58 | return m_value;
|
---|
59 | }
|
---|
60 |
|
---|
61 | void QVariantDelegate::setValue(const QVariant &value)
|
---|
62 | {
|
---|
63 | m_value = value;
|
---|
64 | }
|
---|
65 |
|
---|
66 | QScriptObjectDelegate::Type QVariantDelegate::type() const
|
---|
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 | {
|
---|
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 |
|
---|
109 | static 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 |
|
---|
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 | {
|
---|
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 |
|
---|
151 | QT_END_NAMESPACE
|
---|