History

working to clean up the QtScript implementation and move towards completion of the proxy interface

Heather Anderson committed 5 years ago

Commit 2dd9d781 parent 1e018db38 files · +2240 −923Browse files

Showing the changes to libraries/script-engine/src/qtscript/ScriptValueQtWrapper.cpp (1 of 38 files). Show all

Unified · Split

libraries/script-engine/src/qtscript/ScriptValueQtWrapper.cpp+216−0
@@ -0,0 +1,216 @@
1//
2// ScriptValueQtWrapper.cpp
3// libraries/script-engine/src
4//
5// Created by Heather Anderson on 5/16/21.
6// Copyright 2021 Vircadia contributors.
7//
8// Distributed under the Apache License, Version 2.0.
9// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10//
11
12#include "ScriptValueQtWrapper.h"
13
14#include "ScriptValueIteratorQtWrapper.h"
15
16ScriptValueQtWrapper* ScriptValueQtWrapper::unwrap(ScriptValuePointer val) {
17 if (!val) {
18 return nullptr;
19 }
20
21 return dynamic_cast<ScriptValueQtWrapper*>(val.data());
22}
23
24QScriptValue ScriptValueQtWrapper::fullUnwrap(const ScriptValuePointer& value) const {
25 if (!value) {
26 return QScriptValue();
27 }
28 ScriptValueQtWrapper* unwrapped = unwrap(value);
29 if (unwrapped) {
30 return unwrapped->toQtValue();
31 }
32 QVariant varValue = value->toVariant();
33 return static_cast<QScriptEngine*>(_engine)->newVariant(varValue);
34}
35
36QScriptValue ScriptValueQtWrapper::fullUnwrap(ScriptEngineQtScript* engine, const ScriptValuePointer& value) {
37 if (!value) {
38 return QScriptValue();
39 }
40 ScriptValueQtWrapper* unwrapped = unwrap(value);
41 if (unwrapped) {
42 return unwrapped->toQtValue();
43 }
44 QVariant varValue = value->toVariant();
45 return static_cast<QScriptEngine*>(engine)->newVariant(varValue);
46}
47
48ScriptValuePointer ScriptValueQtWrapper::call(const ScriptValuePointer& thisObject, const ScriptValueList& args) {
49 QScriptValue qThis = fullUnwrap(thisObject);
50 QScriptValueList qArgs;
51 for (ScriptValueList::const_iterator iter = args.begin(); iter != args.end(); ++iter) {
52 qArgs.push_back(fullUnwrap(*iter));
53 }
54 QScriptValue result = _value.call(qThis, qArgs);
55 return ScriptValuePointer(new ScriptValueQtWrapper(_engine, std::move(result)));
56}
57
58ScriptValuePointer ScriptValueQtWrapper::call(const ScriptValuePointer& thisObject, const ScriptValuePointer& arguments) {
59 QScriptValue qThis = fullUnwrap(thisObject);
60 QScriptValue qArgs = fullUnwrap(arguments);
61 QScriptValue result = _value.call(qThis, qArgs);
62 return ScriptValuePointer(new ScriptValueQtWrapper(_engine, std::move(result)));
63}
64
65ScriptValuePointer ScriptValueQtWrapper::construct(const ScriptValueList& args) {
66 QScriptValueList qArgs;
67 for (ScriptValueList::const_iterator iter = args.begin(); iter != args.end(); ++iter) {
68 qArgs.push_back(fullUnwrap(*iter));
69 }
70 QScriptValue result = _value.construct(qArgs);
71 return ScriptValuePointer(new ScriptValueQtWrapper(_engine, std::move(result)));
72}
73
74ScriptValuePointer ScriptValueQtWrapper::construct(const ScriptValuePointer& arguments) {
75 QScriptValue unwrapped = fullUnwrap(arguments);
76 QScriptValue result = _value.construct(unwrapped);
77 return ScriptValuePointer(new ScriptValueQtWrapper(_engine, std::move(result)));
78}
79
80ScriptValuePointer ScriptValueQtWrapper::data() const {
81 QScriptValue result = _value.data();
82 return ScriptValuePointer(new ScriptValueQtWrapper(_engine, std::move(result)));
83}
84
85ScriptEnginePointer ScriptValueQtWrapper::engine() const {
86 if (!_engine) {
87 return ScriptEnginePointer();
88 }
89 return _engine->sharedFromThis();
90}
91
92ScriptValueIteratorPointer ScriptValueQtWrapper::newIterator() {
93 return ScriptValueIteratorPointer(new ScriptValueIteratorQtWrapper(_engine, _value));
94}
95
96ScriptValuePointer ScriptValueQtWrapper::property(const QString& name, const ResolveFlags& mode) const {
97 QScriptValue result = _value.property(name, (QScriptValue::ResolveFlags)(int)mode);
98 return ScriptValuePointer(new ScriptValueQtWrapper(_engine, std::move(result)));
99}
100
101ScriptValuePointer ScriptValueQtWrapper::property(quint32 arrayIndex, const ResolveFlags& mode) const {
102 QScriptValue result = _value.property(arrayIndex, (QScriptValue::ResolveFlags)(int)mode);
103 return ScriptValuePointer(new ScriptValueQtWrapper(_engine, std::move(result)));
104}
105
106void ScriptValueQtWrapper::setData(const ScriptValuePointer& value) {
107 QScriptValue unwrapped = fullUnwrap(value);
108 _value.setData(unwrapped);
109}
110
111void ScriptValueQtWrapper::setProperty(const QString& name, const ScriptValuePointer& value, const PropertyFlags& flags) {
112 QScriptValue unwrapped = fullUnwrap(value);
113 _value.setProperty(name, unwrapped, (QScriptValue::PropertyFlags)(int)flags);
114}
115
116void ScriptValueQtWrapper::setProperty(quint32 arrayIndex, const ScriptValuePointer& value, const PropertyFlags& flags) {
117 QScriptValue unwrapped = fullUnwrap(value);
118 _value.setProperty(arrayIndex, unwrapped, (QScriptValue::PropertyFlags)(int)flags);
119}
120
121void ScriptValueQtWrapper::setPrototype(const ScriptValuePointer& prototype) {
122 ScriptValueQtWrapper* unwrappedPrototype = unwrap(prototype);
123 if (unwrappedPrototype) {
124 _value.setPrototype(unwrappedPrototype->toQtValue());
125 }
126}
127
128bool ScriptValueQtWrapper::strictlyEquals(const ScriptValuePointer& other) const {
129 ScriptValueQtWrapper* unwrappedOther = unwrap(other);
130 return unwrappedOther ? _value.strictlyEquals(unwrappedOther->toQtValue()) : false;
131}
132
133bool ScriptValueQtWrapper::toBool() const {
134 return _value.toBool();
135}
136
137qint32 ScriptValueQtWrapper::toInt32() const {
138 return _value.toInt32();
139}
140
141double ScriptValueQtWrapper::toInteger() const {
142 return _value.toInteger();
143}
144
145double ScriptValueQtWrapper::toNumber() const {
146 return _value.toNumber();
147}
148
149QString ScriptValueQtWrapper::toString() const {
150 return _value.toString();
151}
152
153quint16 ScriptValueQtWrapper::toUInt16() const {
154 return _value.toUInt16();
155}
156
157quint32 ScriptValueQtWrapper::toUInt32() const {
158 return _value.toUInt32();
159}
160
161QVariant ScriptValueQtWrapper::toVariant() const {
162 return _value.toVariant();
163}
164
165QObject* ScriptValueQtWrapper::toQObject() const {
166 return _value.toQObject();
167}
168
169bool ScriptValueQtWrapper::equalsInternal(const ScriptValuePointer& other) const {
170 ScriptValueQtWrapper* unwrappedOther = unwrap(other);
171 return unwrappedOther ? _value.equals(unwrappedOther->toQtValue()) : false;
172}
173
174bool ScriptValueQtWrapper::isArrayInternal() const {
175 return _value.isArray();
176}
177
178bool ScriptValueQtWrapper::isBoolInternal() const {
179 return _value.isBool();
180}
181
182bool ScriptValueQtWrapper::isErrorInternal() const {
183 return _value.isError();
184}
185
186bool ScriptValueQtWrapper::isFunctionInternal() const {
187 return _value.isFunction();
188}
189
190bool ScriptValueQtWrapper::isNumberInternal() const {
191 return _value.isNumber();
192}
193
194bool ScriptValueQtWrapper::isNullInternal() const {
195 return _value.isNull();
196}
197
198bool ScriptValueQtWrapper::isObjectInternal() const {
199 return _value.isObject();
200}
201
202bool ScriptValueQtWrapper::isStringInternal() const {
203 return _value.isString();
204}
205
206bool ScriptValueQtWrapper::isUndefinedInternal() const {
207 return _value.isUndefined();
208}
209
210bool ScriptValueQtWrapper::isValidInternal() const {
211 return _value.isValid();
212}
213
214bool ScriptValueQtWrapper::isVariantInternal() const {
215 return _value.isVariant();
216}