History

first pass creating Qt <-> QtScript interface

Heather Anderson committed 4 years ago

Commit bc6eaf21 parent bd0455463 files · +1707 −294Browse files

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

Unified · Split

libraries/script-engine/src/qtscript/ScriptEngineQtScript_cast.cpp+538−0
@@ -0,0 +1,538 @@
1//
2// ScriptEngineQtScript_cast.cpp
3// libraries/script-engine/src/qtscript
4//
5// Created by Heather Anderson 12/9/2021
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 "ScriptEngineQtScript.h"
13
14#include <QtCore/QJsonArray>
15#include <QtCore/QJsonObject>
16#include <QtCore/QJsonValue>
17#include <QtScript/QScriptEngine>
18
19#include "../ScriptValueIterator.h"
20
21#include "ScriptObjectQtProxy.h"
22#include "ScriptValueQtWrapper.h"
23
24template <int i>
25class CustomTypeInstance {
26public:
27 static ScriptEngine::MarshalFunction marshalFunc;
28 static ScriptEngine::DemarshalFunction demarshalFunc;
29
30 static QScriptValue internalMarshalFunc(QScriptEngine* engine, const void* src) {
31 ScriptEngineQtScript* unwrappedEngine = static_cast<ScriptEngineQtScript*>(engine);
32 ScriptValue dest = marshalFunc(unwrappedEngine, src);
33 return ScriptValueQtWrapper::fullUnwrap(unwrappedEngine, dest);
34 }
35
36 static void internalDemarshalFunc(const QScriptValue& src, void* dest) {
37 ScriptEngineQtScript* unwrappedEngine = static_cast<ScriptEngineQtScript*>(src.engine());
38 ScriptValue wrappedSrc(new ScriptValueQtWrapper(unwrappedEngine, src));
39 demarshalFunc(wrappedSrc, dest);
40 }
41};
42template <int i>
43ScriptEngine::MarshalFunction CustomTypeInstance<i>::marshalFunc;
44template <int i>
45ScriptEngine::DemarshalFunction CustomTypeInstance<i>::demarshalFunc;
46
47// I would *LOVE* it if there was a different way to do this, jeez!
48// Qt requires two functions that have no parameters that give any context,
49// one of the must return a QScriptValue (so we can't void* them into generics and stick them in the templates).
50// This *has* to be done via templates but the whole point of this is to avoid leaking types into the rest of
51// the system that would require anyone other than us to have a dependency on QtScript
52#define CUSTOM_TYPE_ENTRY(idx) \
53 case idx: \
54 CustomTypeInstance<idx>::marshalFunc = marshalFunc; \
55 CustomTypeInstance<idx>::demarshalFunc = demarshalFunc; \
56 internalMarshalFunc = CustomTypeInstance<idx>::internalMarshalFunc; \
57 internalDemarshalFunc = CustomTypeInstance<idx>::internalDemarshalFunc; \
58 break;
59#define CUSTOM_TYPE_ENTRY_10(idx) \
60 CUSTOM_TYPE_ENTRY((idx * 10)); \
61 CUSTOM_TYPE_ENTRY((idx * 10) + 1); \
62 CUSTOM_TYPE_ENTRY((idx * 10) + 2); \
63 CUSTOM_TYPE_ENTRY((idx * 10) + 3); \
64 CUSTOM_TYPE_ENTRY((idx * 10) + 4); \
65 CUSTOM_TYPE_ENTRY((idx * 10) + 5); \
66 CUSTOM_TYPE_ENTRY((idx * 10) + 6); \
67 CUSTOM_TYPE_ENTRY((idx * 10) + 7); \
68 CUSTOM_TYPE_ENTRY((idx * 10) + 8); \
69 CUSTOM_TYPE_ENTRY((idx * 10) + 9);
70
71void ScriptEngineQtScript::setDefaultPrototype(int metaTypeId, const ScriptValue& prototype) {
72 ScriptValueQtWrapper* unwrappedPrototype = ScriptValueQtWrapper::unwrap(prototype);
73 if (unwrappedPrototype) {
74 const QScriptValue& scriptPrototype = unwrappedPrototype->toQtValue();
75 QScriptEngine::setDefaultPrototype(metaTypeId, scriptPrototype);
76
77 QMutexLocker guard(&_customTypeProtect);
78 _customPrototypes.insert(metaTypeId, scriptPrototype);
79 }
80}
81
82void ScriptEngineQtScript::registerCustomType(int type,
83 ScriptEngine::MarshalFunction marshalFunc,
84 ScriptEngine::DemarshalFunction demarshalFunc)
85{
86 QScriptEngine::MarshalFunction internalMarshalFunc;
87 QScriptEngine::DemarshalFunction internalDemarshalFunc;
88
89 {
90 QMutexLocker guard(&_customTypeProtect);
91
92 // storing it in a map for our own benefit
93 CustomMarshal& customType = _customTypes.insert(type, CustomMarshal()).value();
94 customType.demarshalFunc = demarshalFunc;
95 customType.marshalFunc = marshalFunc;
96
97 // creating a conversion for QtScript's benefit
98 if (_nextCustomType >= 300) { // have we ran out of translators?
99 Q_ASSERT(false);
100 return;
101 }
102
103 switch (_nextCustomType++) {
104 CUSTOM_TYPE_ENTRY_10(0);
105 CUSTOM_TYPE_ENTRY_10(1);
106 CUSTOM_TYPE_ENTRY_10(2);
107 CUSTOM_TYPE_ENTRY_10(3);
108 CUSTOM_TYPE_ENTRY_10(4);
109 CUSTOM_TYPE_ENTRY_10(5);
110 CUSTOM_TYPE_ENTRY_10(6);
111 CUSTOM_TYPE_ENTRY_10(7);
112 CUSTOM_TYPE_ENTRY_10(8);
113 CUSTOM_TYPE_ENTRY_10(9);
114 CUSTOM_TYPE_ENTRY_10(10);
115 CUSTOM_TYPE_ENTRY_10(11);
116 CUSTOM_TYPE_ENTRY_10(12);
117 CUSTOM_TYPE_ENTRY_10(13);
118 CUSTOM_TYPE_ENTRY_10(14);
119 CUSTOM_TYPE_ENTRY_10(15);
120 CUSTOM_TYPE_ENTRY_10(16);
121 CUSTOM_TYPE_ENTRY_10(17);
122 CUSTOM_TYPE_ENTRY_10(18);
123 CUSTOM_TYPE_ENTRY_10(19);
124 CUSTOM_TYPE_ENTRY_10(20);
125 CUSTOM_TYPE_ENTRY_10(21);
126 CUSTOM_TYPE_ENTRY_10(22);
127 CUSTOM_TYPE_ENTRY_10(23);
128 CUSTOM_TYPE_ENTRY_10(24);
129 CUSTOM_TYPE_ENTRY_10(25);
130 CUSTOM_TYPE_ENTRY_10(26);
131 CUSTOM_TYPE_ENTRY_10(27);
132 CUSTOM_TYPE_ENTRY_10(28);
133 CUSTOM_TYPE_ENTRY_10(29);
134 CUSTOM_TYPE_ENTRY_10(30);
135 }
136 }
137
138 qScriptRegisterMetaType_helper(this, type, internalMarshalFunc, internalDemarshalFunc, QScriptValue());
139}
140
141Q_DECLARE_METATYPE(ScriptValue);
142
143static QScriptValue ScriptValueToQScriptValue(QScriptEngine* engine, const ScriptValue& src) {
144 return ScriptValueQtWrapper::fullUnwrap(static_cast<ScriptEngineQtScript*>(engine), src);
145}
146
147static void ScriptValueFromQScriptValue(const QScriptValue& src, ScriptValue& dest) {
148 ScriptEngineQtScript* engine = static_cast<ScriptEngineQtScript*>(src.engine());
149 dest = ScriptValue(new ScriptValueQtWrapper(engine, src));
150}
151
152static ScriptValue StringListToScriptValue(ScriptEngine* engine, const void* pSrc) {
153 const QStringList& src = *reinterpret_cast<const QStringList*>(pSrc);
154 int len = src.length();
155 ScriptValue dest = engine->newArray(len);
156 for (int idx = 0; idx < len; ++idx) {
157 dest.setProperty(idx, engine->newValue(src.at(idx)));
158 }
159 return dest;
160}
161
162static bool StringListFromScriptValue(const ScriptValue& src, void* pDest) {
163 if(!src.isArray()) return false;
164 QStringList& dest = *reinterpret_cast<QStringList*>(pDest);
165 int len = src.property("length").toInteger();
166 dest.clear();
167 for (int idx = 0; idx < len; ++idx) {
168 dest.append(src.property(idx).toString());
169 }
170 return true;
171}
172
173static ScriptValue VariantListToScriptValue(ScriptEngine* engine, const void* pSrc) {
174 const QVariantList& src = *reinterpret_cast<const QVariantList*>(pSrc);
175 int len = src.length();
176 ScriptValue dest = engine->newArray(len);
177 for (int idx = 0; idx < len; ++idx) {
178 dest.setProperty(idx, engine->newVariant(src.at(idx)));
179 }
180 return dest;
181}
182
183static bool VariantListFromScriptValue(const ScriptValue& src, void* pDest) {
184 if(!src.isArray()) return false;
185 QVariantList& dest = *reinterpret_cast<QVariantList*>(pDest);
186 int len = src.property("length").toInteger();
187 dest.clear();
188 for (int idx = 0; idx < len; ++idx) {
189 dest.append(src.property(idx).toVariant());
190 }
191 return true;
192}
193
194static ScriptValue VariantMapToScriptValue(ScriptEngine* engine, const void* pSrc) {
195 const QVariantMap& src = *reinterpret_cast<const QVariantMap*>(pSrc);
196 ScriptValue dest = engine->newObject();
197 for (QVariantMap::const_iterator iter = src.cbegin(); iter != src.cend(); ++iter) {
198 dest.setProperty(iter.key(), engine->newVariant(iter.value()));
199 }
200 return dest;
201}
202
203static bool VariantMapFromScriptValue(const ScriptValue& src, void* pDest) {
204 QVariantMap& dest = *reinterpret_cast<QVariantMap*>(pDest);
205 dest.clear();
206 ScriptValueIteratorPointer iter = src.newIterator();
207 while (iter->hasNext()) {
208 iter->next();
209 dest.insert(iter->name(), iter->value().toVariant());
210 }
211 return true;
212}
213
214static ScriptValue VariantHashToScriptValue(ScriptEngine* engine, const void* pSrc) {
215 const QVariantHash& src = *reinterpret_cast<const QVariantHash*>(pSrc);
216 ScriptValue dest = engine->newObject();
217 for (QVariantHash::const_iterator iter = src.cbegin(); iter != src.cend(); ++iter) {
218 dest.setProperty(iter.key(), engine->newVariant(iter.value()));
219 }
220 return dest;
221}
222
223static bool VariantHashFromScriptValue(const ScriptValue& src, void* pDest) {
224 QVariantHash& dest = *reinterpret_cast<QVariantHash*>(pDest);
225 dest.clear();
226 ScriptValueIteratorPointer iter = src.newIterator();
227 while (iter->hasNext()) {
228 iter->next();
229 dest.insert(iter->name(), iter->value().toVariant());
230 }
231 return true;
232}
233
234static ScriptValue JsonValueToScriptValue(ScriptEngine* engine, const void* pSrc) {
235 const QJsonValue& src = *reinterpret_cast<const QJsonValue*>(pSrc);
236 return engine->newVariant(src.toVariant());
237}
238
239static bool JsonValueFromScriptValue(const ScriptValue& src, void* pDest) {
240 QJsonValue& dest = *reinterpret_cast<QJsonValue*>(pDest);
241 dest = QJsonValue::fromVariant(src.toVariant());
242 return true;
243}
244
245static ScriptValue JsonObjectToScriptValue(ScriptEngine* engine, const void* pSrc) {
246 const QJsonObject& src = *reinterpret_cast<const QJsonObject*>(pSrc);
247 QVariantMap map = src.toVariantMap();
248 ScriptValue dest = engine->newObject();
249 for (QVariantMap::const_iterator iter = map.cbegin(); iter != map.cend(); ++iter) {
250 dest.setProperty(iter.key(), engine->newVariant(iter.value()));
251 }
252 return dest;
253}
254
255static bool JsonObjectFromScriptValue(const ScriptValue& src, void* pDest) {
256 QJsonObject& dest = *reinterpret_cast<QJsonObject*>(pDest);
257 QVariantMap map;
258 ScriptValueIteratorPointer iter = src.newIterator();
259 while (iter->hasNext()) {
260 iter->next();
261 map.insert(iter->name(), iter->value().toVariant());
262 }
263 dest = QJsonObject::fromVariantMap(map);
264 return true;
265}
266
267static ScriptValue JsonArrayToScriptValue(ScriptEngine* engine, const void* pSrc) {
268 const QJsonArray& src = *reinterpret_cast<const QJsonArray*>(pSrc);
269 QVariantList list = src.toVariantList();
270 int len = list.length();
271 ScriptValue dest = engine->newArray(len);
272 for (int idx = 0; idx < len; ++idx) {
273 dest.setProperty(idx, engine->newVariant(list.at(idx)));
274 }
275 return dest;
276}
277
278static bool JsonArrayFromScriptValue(const ScriptValue& src, void* pDest) {
279 if(!src.isArray()) return false;
280 QJsonArray& dest = *reinterpret_cast<QJsonArray*>(pDest);
281 QVariantList list;
282 int len = src.property("length").toInteger();
283 for (int idx = 0; idx < len; ++idx) {
284 list.append(src.property(idx).toVariant());
285 }
286 dest = QJsonArray::fromVariantList(list);
287 return true;
288}
289
290// QMetaType::QJsonArray
291
292void ScriptEngineQtScript::registerSystemTypes() {
293 qScriptRegisterMetaType(this, ScriptValueToQScriptValue, ScriptValueFromQScriptValue);
294
295 QMutexLocker guard(&_customTypeProtect);
296
297 {
298 CustomMarshal& customType = _customTypes.insert(QMetaType::QStringList, CustomMarshal()).value();
299 customType.demarshalFunc = StringListFromScriptValue;
300 customType.marshalFunc = StringListToScriptValue;
301 }
302 {
303 CustomMarshal& customType = _customTypes.insert(QMetaType::QVariantList, CustomMarshal()).value();
304 customType.demarshalFunc = VariantListFromScriptValue;
305 customType.marshalFunc = VariantListToScriptValue;
306 }
307 {
308 CustomMarshal& customType = _customTypes.insert(QMetaType::QVariantMap, CustomMarshal()).value();
309 customType.demarshalFunc = VariantMapFromScriptValue;
310 customType.marshalFunc = VariantMapToScriptValue;
311 }
312 {
313 CustomMarshal& customType = _customTypes.insert(QMetaType::QVariantHash, CustomMarshal()).value();
314 customType.demarshalFunc = VariantHashFromScriptValue;
315 customType.marshalFunc = VariantHashToScriptValue;
316 }
317 {
318 CustomMarshal& customType = _customTypes.insert(QMetaType::QJsonValue, CustomMarshal()).value();
319 customType.demarshalFunc = JsonValueFromScriptValue;
320 customType.marshalFunc = JsonValueToScriptValue;
321 }
322 {
323 CustomMarshal& customType = _customTypes.insert(QMetaType::QJsonObject, CustomMarshal()).value();
324 customType.demarshalFunc = JsonObjectFromScriptValue;
325 customType.marshalFunc = JsonObjectToScriptValue;
326 }
327 {
328 CustomMarshal& customType = _customTypes.insert(QMetaType::QJsonArray, CustomMarshal()).value();
329 customType.demarshalFunc = JsonArrayFromScriptValue;
330 customType.marshalFunc = JsonArrayToScriptValue;
331 }
332}
333
334bool ScriptEngineQtScript::castValueToVariant(const QScriptValue& val, QVariant& dest, int destType) {
335
336 // if we're not particularly interested in a specific type, try to detect if we're dealing with a registered type
337 if (destType == QMetaType::UnknownType) {
338 QObject* obj = ScriptObjectQtProxy::unwrap(val);
339 if (obj) {
340 for (const QMetaObject* metaObject = obj->metaObject(); metaObject; metaObject = metaObject->superClass()) {
341 QByteArray typeName = QByteArray(metaObject->className()) + "*";
342 int typeId = QMetaType::type(typeName.constData());
343 if (typeId != QMetaType::UnknownType) {
344 destType = typeId;
345 break;
346 }
347 }
348 }
349 }
350
351 if (destType == qMetaTypeId<ScriptValue>()) {
352 dest = QVariant::fromValue(ScriptValue(new ScriptValueQtWrapper(this, val)));
353 return true;
354 }
355
356 // do we have a registered handler for this type?
357 ScriptEngine::DemarshalFunction demarshalFunc = nullptr;
358 {
359 QMutexLocker guard(&_customTypeProtect);
360 CustomMarshalMap::const_iterator lookup = _customTypes.find(destType);
361 if (lookup != _customTypes.cend()) {
362 demarshalFunc = lookup.value().demarshalFunc;
363 }
364 }
365 if (demarshalFunc) {
366 void* destStorage = QMetaType::create(destType);
367 ScriptValue wrappedVal(new ScriptValueQtWrapper(this, val));
368 bool success = demarshalFunc(wrappedVal, destStorage);
369 dest = success ? QVariant(destType, destStorage) : QVariant();
370 QMetaType::destroy(destType, destStorage);
371 return success;
372 } else {
373 switch (destType) {
374 case QMetaType::UnknownType:
375 if (val.isUndefined()) {
376 dest = QVariant();
377 break;
378 }
379 if (val.isNull()) {
380 dest = QVariant::fromValue(nullptr);
381 break;
382 }
383 if (val.isBool()) {
384 dest = QVariant::fromValue(val.toBool());
385 break;
386 }
387 if (val.isString()) {
388 dest = QVariant::fromValue(val.toString());
389 break;
390 }
391 if (val.isNumber()) {
392 dest = QVariant::fromValue(val.toNumber());
393 break;
394 }
395 {
396 QObject* obj = ScriptObjectQtProxy::unwrap(val);
397 if (obj) {
398 dest = QVariant::fromValue(obj);
399 break;
400 }
401 }
402 {
403 QVariant var = ScriptVariantQtProxy::unwrap(val);
404 if (var.isValid()) {
405 dest = var;
406 break;
407 }
408 }
409 dest = val.toVariant();
410 break;
411 case QMetaType::Bool:
412 dest = QVariant::fromValue(val.toBool());
413 break;
414 case QMetaType::QDateTime:
415 case QMetaType::QDate:
416 Q_ASSERT(val.isDate());
417 dest = QVariant::fromValue(val.toDateTime());
418 break;
419 case QMetaType::UInt:
420 case QMetaType::ULong:
421 dest = QVariant::fromValue(val.toUInt32());
422 break;
423 case QMetaType::Int:
424 case QMetaType::Long:
425 case QMetaType::Short:
426 dest = QVariant::fromValue(val.toInt32());
427 break;
428 case QMetaType::Double:
429 case QMetaType::Float:
430 case QMetaType::ULongLong:
431 case QMetaType::LongLong:
432 dest = QVariant::fromValue(val.toNumber());
433 break;
434 case QMetaType::QString:
435 case QMetaType::QByteArray:
436 dest = QVariant::fromValue(val.toString());
437 break;
438 case QMetaType::UShort:
439 dest = QVariant::fromValue(val.toUInt16());
440 break;
441 case QMetaType::QObjectStar:
442 dest = QVariant::fromValue(ScriptObjectQtProxy::unwrap(val));
443 break;
444 default:
445 // check to see if this is a pointer to a QObject-derived object
446 if (QMetaType::typeFlags(destType) & QMetaType::PointerToQObject) {
447 dest = QVariant::fromValue(ScriptObjectQtProxy::unwrap(val));
448 break;
449 }
450 // check to see if we have a registered prototype
451 {
452 QVariant var = ScriptVariantQtProxy::unwrap(val);
453 if (var.isValid()) {
454 dest = var;
455 break;
456 }
457 }
458 // last chance, just convert it to a variant
459 dest = val.toVariant();
460 break;
461 }
462 }
463
464 return destType == QMetaType::UnknownType || dest.userType() == destType || dest.convert(destType);
465}
466
467QScriptValue ScriptEngineQtScript::castVariantToValue(const QVariant& val) {
468 int valTypeId = val.userType();
469
470 if (valTypeId == qMetaTypeId<ScriptValue>()) {
471 // this is a wrapped ScriptValue, so just unwrap it and call it good
472 ScriptValue innerVal = val.value<ScriptValue>();
473 return ScriptValueQtWrapper::fullUnwrap(this, innerVal);
474 }
475
476 // do we have a registered handler for this type?
477 ScriptEngine::MarshalFunction marshalFunc = nullptr;
478 {
479 QMutexLocker guard(&_customTypeProtect);
480 CustomMarshalMap::const_iterator lookup = _customTypes.find(valTypeId);
481 if (lookup != _customTypes.cend()) {
482 marshalFunc = lookup.value().marshalFunc;
483 }
484 }
485 if (marshalFunc) {
486 ScriptValue wrappedVal = marshalFunc(this, val.constData());
487 return ScriptValueQtWrapper::fullUnwrap(this, wrappedVal);
488 }
489
490 switch (valTypeId) {
491 case QMetaType::UnknownType:
492 case QMetaType::Void:
493 return QScriptValue(this, QScriptValue::UndefinedValue);
494 case QMetaType::Nullptr:
495 return QScriptValue(this, QScriptValue::NullValue);
496 case QMetaType::Bool:
497 return QScriptValue(this, val.toBool());
498 case QMetaType::Int:
499 case QMetaType::Long:
500 case QMetaType::Short:
501 return QScriptValue(this, val.toInt());
502 case QMetaType::UInt:
503 case QMetaType::ULong:
504 case QMetaType::UShort:
505 return QScriptValue(this, val.toUInt());
506 case QMetaType::Float:
507 case QMetaType::LongLong:
508 case QMetaType::ULongLong:
509 case QMetaType::Double:
510 return QScriptValue(this, val.toFloat());
511 case QMetaType::QString:
512 case QMetaType::QByteArray:
513 return QScriptValue(this, val.toString());
514 case QMetaType::QVariant:
515 return castVariantToValue(val.value<QVariant>());
516 case QMetaType::QObjectStar:
517 return ScriptObjectQtProxy::newQObject(this, val.value<QObject*>(), ScriptEngine::QtOwnership);
518 case QMetaType::QDateTime:
519 return static_cast<QScriptEngine*>(this)->newDate(val.value<QDateTime>());
520 case QMetaType::QDate:
521 return static_cast<QScriptEngine*>(this)->newDate(val.value<QDate>().startOfDay());
522 default:
523 // check to see if this is a pointer to a QObject-derived object
524 if (QMetaType::typeFlags(valTypeId) & QMetaType::PointerToQObject) {
525 return ScriptObjectQtProxy::newQObject(this, val.value<QObject*>(), ScriptEngine::QtOwnership);
526 }
527 // have we set a prototype'd variant?
528 {
529 QMutexLocker guard(&_customTypeProtect);
530 CustomPrototypeMap::const_iterator lookup = _customPrototypes.find(valTypeId);
531 if (lookup != _customPrototypes.cend()) {
532 return ScriptVariantQtProxy::newVariant(this, val, lookup.value());
533 }
534 }
535 // just do a generic variant
536 return QScriptEngine::newVariant(val);
537 }
538}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t