Showing the changes to libraries/script-engine/src/qtscript/ScriptValueQtWrapper.cpp (1 of 38 files). Show all
Unified · Split
| @@ -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 | |||
| 16 | ScriptValueQtWrapper* ScriptValueQtWrapper::unwrap(ScriptValuePointer val) { | ||
| 17 | if (!val) { | ||
| 18 | return nullptr; | ||
| 19 | } | ||
| 20 | |||
| 21 | return dynamic_cast<ScriptValueQtWrapper*>(val.data()); | ||
| 22 | } | ||
| 23 | |||
| 24 | QScriptValue 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 | |||
| 36 | QScriptValue 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 | |||
| 48 | ScriptValuePointer 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 | |||
| 58 | ScriptValuePointer 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 | |||
| 65 | ScriptValuePointer 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 | |||
| 74 | ScriptValuePointer 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 | |||
| 80 | ScriptValuePointer ScriptValueQtWrapper::data() const { | ||
| 81 | QScriptValue result = _value.data(); | ||
| 82 | return ScriptValuePointer(new ScriptValueQtWrapper(_engine, std::move(result))); | ||
| 83 | } | ||
| 84 | |||
| 85 | ScriptEnginePointer ScriptValueQtWrapper::engine() const { | ||
| 86 | if (!_engine) { | ||
| 87 | return ScriptEnginePointer(); | ||
| 88 | } | ||
| 89 | return _engine->sharedFromThis(); | ||
| 90 | } | ||
| 91 | |||
| 92 | ScriptValueIteratorPointer ScriptValueQtWrapper::newIterator() { | ||
| 93 | return ScriptValueIteratorPointer(new ScriptValueIteratorQtWrapper(_engine, _value)); | ||
| 94 | } | ||
| 95 | |||
| 96 | ScriptValuePointer 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 | |||
| 101 | ScriptValuePointer 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 | |||
| 106 | void ScriptValueQtWrapper::setData(const ScriptValuePointer& value) { | ||
| 107 | QScriptValue unwrapped = fullUnwrap(value); | ||
| 108 | _value.setData(unwrapped); | ||
| 109 | } | ||
| 110 | |||
| 111 | void 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 | |||
| 116 | void 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 | |||
| 121 | void ScriptValueQtWrapper::setPrototype(const ScriptValuePointer& prototype) { | ||
| 122 | ScriptValueQtWrapper* unwrappedPrototype = unwrap(prototype); | ||
| 123 | if (unwrappedPrototype) { | ||
| 124 | _value.setPrototype(unwrappedPrototype->toQtValue()); | ||
| 125 | } | ||
| 126 | } | ||
| 127 | |||
| 128 | bool ScriptValueQtWrapper::strictlyEquals(const ScriptValuePointer& other) const { | ||
| 129 | ScriptValueQtWrapper* unwrappedOther = unwrap(other); | ||
| 130 | return unwrappedOther ? _value.strictlyEquals(unwrappedOther->toQtValue()) : false; | ||
| 131 | } | ||
| 132 | |||
| 133 | bool ScriptValueQtWrapper::toBool() const { | ||
| 134 | return _value.toBool(); | ||
| 135 | } | ||
| 136 | |||
| 137 | qint32 ScriptValueQtWrapper::toInt32() const { | ||
| 138 | return _value.toInt32(); | ||
| 139 | } | ||
| 140 | |||
| 141 | double ScriptValueQtWrapper::toInteger() const { | ||
| 142 | return _value.toInteger(); | ||
| 143 | } | ||
| 144 | |||
| 145 | double ScriptValueQtWrapper::toNumber() const { | ||
| 146 | return _value.toNumber(); | ||
| 147 | } | ||
| 148 | |||
| 149 | QString ScriptValueQtWrapper::toString() const { | ||
| 150 | return _value.toString(); | ||
| 151 | } | ||
| 152 | |||
| 153 | quint16 ScriptValueQtWrapper::toUInt16() const { | ||
| 154 | return _value.toUInt16(); | ||
| 155 | } | ||
| 156 | |||
| 157 | quint32 ScriptValueQtWrapper::toUInt32() const { | ||
| 158 | return _value.toUInt32(); | ||
| 159 | } | ||
| 160 | |||
| 161 | QVariant ScriptValueQtWrapper::toVariant() const { | ||
| 162 | return _value.toVariant(); | ||
| 163 | } | ||
| 164 | |||
| 165 | QObject* ScriptValueQtWrapper::toQObject() const { | ||
| 166 | return _value.toQObject(); | ||
| 167 | } | ||
| 168 | |||
| 169 | bool ScriptValueQtWrapper::equalsInternal(const ScriptValuePointer& other) const { | ||
| 170 | ScriptValueQtWrapper* unwrappedOther = unwrap(other); | ||
| 171 | return unwrappedOther ? _value.equals(unwrappedOther->toQtValue()) : false; | ||
| 172 | } | ||
| 173 | |||
| 174 | bool ScriptValueQtWrapper::isArrayInternal() const { | ||
| 175 | return _value.isArray(); | ||
| 176 | } | ||
| 177 | |||
| 178 | bool ScriptValueQtWrapper::isBoolInternal() const { | ||
| 179 | return _value.isBool(); | ||
| 180 | } | ||
| 181 | |||
| 182 | bool ScriptValueQtWrapper::isErrorInternal() const { | ||
| 183 | return _value.isError(); | ||
| 184 | } | ||
| 185 | |||
| 186 | bool ScriptValueQtWrapper::isFunctionInternal() const { | ||
| 187 | return _value.isFunction(); | ||
| 188 | } | ||
| 189 | |||
| 190 | bool ScriptValueQtWrapper::isNumberInternal() const { | ||
| 191 | return _value.isNumber(); | ||
| 192 | } | ||
| 193 | |||
| 194 | bool ScriptValueQtWrapper::isNullInternal() const { | ||
| 195 | return _value.isNull(); | ||
| 196 | } | ||
| 197 | |||
| 198 | bool ScriptValueQtWrapper::isObjectInternal() const { | ||
| 199 | return _value.isObject(); | ||
| 200 | } | ||
| 201 | |||
| 202 | bool ScriptValueQtWrapper::isStringInternal() const { | ||
| 203 | return _value.isString(); | ||
| 204 | } | ||
| 205 | |||
| 206 | bool ScriptValueQtWrapper::isUndefinedInternal() const { | ||
| 207 | return _value.isUndefined(); | ||
| 208 | } | ||
| 209 | |||
| 210 | bool ScriptValueQtWrapper::isValidInternal() const { | ||
| 211 | return _value.isValid(); | ||
| 212 | } | ||
| 213 | |||
| 214 | bool ScriptValueQtWrapper::isVariantInternal() const { | ||
| 215 | return _value.isVariant(); | ||
| 216 | } | ||