Showing the changes to libraries/script-engine/src/qtscript/ScriptEngineQtScript.cpp (1 of 63 files). Show all
Unified · Split
| @@ -49,17 +49,13 @@ · 48 unmodified lines | |||
| 49 | #include "../ScriptValue.h" | 49 | #include "../ScriptValue.h" |
| 50 | 50 | ||
| 51 | #include "ScriptContextQtWrapper.h" | 51 | #include "ScriptContextQtWrapper.h" |
| 52 | #include "ScriptObjectQtProxy.h" | ||
| 52 | #include "ScriptProgramQtWrapper.h" | 53 | #include "ScriptProgramQtWrapper.h" |
| 53 | #include "ScriptValueQtWrapper.h" | 54 | #include "ScriptValueQtWrapper.h" |
| 54 | #include "ScriptContextQtAgent.h" | 55 | #include "ScriptContextQtAgent.h" |
| 55 | 56 | ||
| 56 | static const int MAX_DEBUG_VALUE_LENGTH { 80 }; | 57 | static const int MAX_DEBUG_VALUE_LENGTH { 80 }; |
| 57 | 58 | ||
| 58 | static const QScriptEngine::QObjectWrapOptions DEFAULT_QOBJECT_WRAP_OPTIONS = | ||
| 59 | QScriptEngine::ExcludeDeleteLater | QScriptEngine::ExcludeChildObjects; | ||
| 60 | |||
| 61 | Q_DECLARE_METATYPE(ScriptValue); | ||
| 62 | |||
| 63 | Q_DECLARE_METATYPE(QScriptEngine::FunctionSignature) | 59 | Q_DECLARE_METATYPE(QScriptEngine::FunctionSignature) |
| 64 | int qfunctionSignatureMetaID = qRegisterMetaType<QScriptEngine::FunctionSignature>(); | 60 | int qfunctionSignatureMetaID = qRegisterMetaType<QScriptEngine::FunctionSignature>(); |
| 65 | 61 | ||
| @@ -311,21 +307,12 @@ void ScriptEngineQtScript::_debugDump(const QString& header, const QScriptValue& · 245 unmodified lines | |||
| 311 | } | 307 | } |
| 312 | #endif | 308 | #endif |
| 313 | 309 | ||
| 314 | static QScriptValue ScriptValueToQScriptValue(QScriptEngine* engine, const ScriptValue& src) { | ||
| 315 | return ScriptValueQtWrapper::fullUnwrap(static_cast<ScriptEngineQtScript*>(engine), src); | ||
| 316 | } | ||
| 317 | |||
| 318 | static void ScriptValueFromQScriptValue(const QScriptValue& src, ScriptValue& dest) { | ||
| 319 | ScriptEngineQtScript* engine = static_cast<ScriptEngineQtScript*>(src.engine()); | ||
| 320 | dest = ScriptValue(new ScriptValueQtWrapper(engine, src)); | ||
| 321 | } | ||
| 322 | |||
| 323 | ScriptEngineQtScript::ScriptEngineQtScript(ScriptManager* scriptManager) : | 310 | ScriptEngineQtScript::ScriptEngineQtScript(ScriptManager* scriptManager) : |
| 324 | QScriptEngine(), | 311 | QScriptEngine(), |
| 325 | _scriptManager(scriptManager), | 312 | _scriptManager(scriptManager), |
| 326 | _arrayBufferClass(new ArrayBufferClass(this)) | 313 | _arrayBufferClass(new ArrayBufferClass(this)) |
| 327 | { | 314 | { |
| 328 | qScriptRegisterMetaType(this, ScriptValueToQScriptValue, ScriptValueFromQScriptValue); | 315 | registerSystemTypes(); |
| 329 | 316 | ||
| 330 | if (_scriptManager) { | 317 | if (_scriptManager) { |
| 331 | connect(this, &QScriptEngine::signalHandlerException, this, [this](const QScriptValue& exception) { | 318 | connect(this, &QScriptEngine::signalHandlerException, this, [this](const QScriptValue& exception) { |
| @@ -343,10 +330,10 @@ ScriptEngineQtScript::ScriptEngineQtScript(ScriptManager* scriptManager) : · 11 unmodified lines | |||
| 343 | } | 330 | } |
| 344 | 331 | ||
| 345 | QScriptValue null = QScriptEngine::nullValue(); | 332 | QScriptValue null = QScriptEngine::nullValue(); |
| 346 | _nullValue = ScriptValue(new ScriptValueQtWrapper(const_cast<ScriptEngineQtScript*>(this), std::move(null))); | 333 | _nullValue = ScriptValue(new ScriptValueQtWrapper(this, std::move(null))); |
| 347 | 334 | ||
| 348 | QScriptValue undefined = QScriptEngine::undefinedValue(); | 335 | QScriptValue undefined = QScriptEngine::undefinedValue(); |
| 349 | _undefinedValue = ScriptValue(new ScriptValueQtWrapper(const_cast<ScriptEngineQtScript*>(this), std::move(undefined))); | 336 | _undefinedValue = ScriptValue(new ScriptValueQtWrapper(this, std::move(undefined))); |
| 350 | 337 | ||
| 351 | QScriptEngine::setProcessEventsInterval(MSECS_PER_SECOND); | 338 | QScriptEngine::setProcessEventsInterval(MSECS_PER_SECOND); |
| 352 | 339 | ||
| @@ -417,7 +404,7 @@ void ScriptEngineQtScript::registerGlobalObject(const QString& name, QObject* ob · 64 unmodified lines | |||
| 417 | 404 | ||
| 418 | if (!QScriptEngine::globalObject().property(name).isValid()) { | 405 | if (!QScriptEngine::globalObject().property(name).isValid()) { |
| 419 | if (object) { | 406 | if (object) { |
| 420 | QScriptValue value = QScriptEngine::newQObject(object, QScriptEngine::QtOwnership, DEFAULT_QOBJECT_WRAP_OPTIONS); | 407 | QScriptValue value = ScriptObjectQtProxy::newQObject(this, object, ScriptEngine::QtOwnership); |
| 421 | QScriptEngine::globalObject().setProperty(name, value); | 408 | QScriptEngine::globalObject().setProperty(name, value); |
| 422 | } else { | 409 | } else { |
| 423 | QScriptEngine::globalObject().setProperty(name, QScriptValue()); | 410 | QScriptEngine::globalObject().setProperty(name, QScriptValue()); |
| @@ -747,7 +734,7 @@ void ScriptEngineQtScript::updateMemoryCost(const qint64& deltaSize) { · 323 unmodified lines | |||
| 747 | 734 | ||
| 748 | ScriptValue ScriptEngineQtScript::globalObject() const { | 735 | ScriptValue ScriptEngineQtScript::globalObject() const { |
| 749 | QScriptValue global = QScriptEngine::globalObject(); // can't cache the value as it may change | 736 | QScriptValue global = QScriptEngine::globalObject(); // can't cache the value as it may change |
| 750 | return ScriptValue(new ScriptValueQtWrapper(const_cast < ScriptEngineQtScript*>(this), std::move(global))); | 737 | return ScriptValue(new ScriptValueQtWrapper(const_cast<ScriptEngineQtScript*>(this), std::move(global))); |
| 751 | } | 738 | } |
| 752 | 739 | ||
| 753 | ScriptManager* ScriptEngineQtScript::manager() const { | 740 | ScriptManager* ScriptEngineQtScript::manager() const { |
| @@ -783,8 +770,7 @@ ScriptProgramPointer ScriptEngineQtScript::newProgram(const QString& sourceCode, · 29 unmodified lines | |||
| 783 | ScriptValue ScriptEngineQtScript::newQObject(QObject* object, | 770 | ScriptValue ScriptEngineQtScript::newQObject(QObject* object, |
| 784 | ScriptEngine::ValueOwnership ownership, | 771 | ScriptEngine::ValueOwnership ownership, |
| 785 | const ScriptEngine::QObjectWrapOptions& options) { | 772 | const ScriptEngine::QObjectWrapOptions& options) { |
| 786 | QScriptValue result = QScriptEngine::newQObject(object, static_cast<QScriptEngine::ValueOwnership>(ownership), | 773 | QScriptValue result = ScriptObjectQtProxy::newQObject(this, object, ownership, options); |
| 787 | (QScriptEngine::QObjectWrapOptions)((int)options | DEFAULT_QOBJECT_WRAP_OPTIONS)); | ||
| 788 | return ScriptValue(new ScriptValueQtWrapper(this, std::move(result))); | 774 | return ScriptValue(new ScriptValueQtWrapper(this, std::move(result))); |
| 789 | } | 775 | } |
| 790 | 776 | ||
| @@ -824,7 +810,7 @@ ScriptValue ScriptEngineQtScript::newValue(const char* value) { · 33 unmodified lines | |||
| 824 | } | 810 | } |
| 825 | 811 | ||
| 826 | ScriptValue ScriptEngineQtScript::newVariant(const QVariant& value) { | 812 | ScriptValue ScriptEngineQtScript::newVariant(const QVariant& value) { |
| 827 | QScriptValue result = QScriptEngine::newVariant(value); | 813 | QScriptValue result = castVariantToValue(value); |
| 828 | return ScriptValue(new ScriptValueQtWrapper(this, std::move(result))); | 814 | return ScriptValue(new ScriptValueQtWrapper(this, std::move(result))); |
| 829 | } | 815 | } |
| 830 | 816 | ||
| @@ -832,13 +818,6 @@ ScriptValue ScriptEngineQtScript::nullValue() { · 1 unmodified line | |||
| 832 | return _nullValue; | 818 | return _nullValue; |
| 833 | } | 819 | } |
| 834 | 820 | ||
| 835 | void ScriptEngineQtScript::setDefaultPrototype(int metaTypeId, const ScriptValue& prototype){ | ||
| 836 | ScriptValueQtWrapper* unwrappedPrototype = ScriptValueQtWrapper::unwrap(prototype); | ||
| 837 | if (unwrappedPrototype) { | ||
| 838 | QScriptEngine::setDefaultPrototype(metaTypeId, unwrappedPrototype->toQtValue()); | ||
| 839 | } | ||
| 840 | } | ||
| 841 | |||
| 842 | ScriptValue ScriptEngineQtScript::undefinedValue() { | 821 | ScriptValue ScriptEngineQtScript::undefinedValue() { |
| 843 | return _undefinedValue; | 822 | return _undefinedValue; |
| 844 | } | 823 | } |
| @@ -928,112 +907,26 @@ bool ScriptEngineQtScript::raiseException(const ScriptValue& exception) { · 83 unmodified lines | |||
| 928 | } | 907 | } |
| 929 | 908 | ||
| 930 | ScriptValue ScriptEngineQtScript::create(int type, const void* ptr) { | 909 | ScriptValue ScriptEngineQtScript::create(int type, const void* ptr) { |
| 931 | QScriptValue result = qScriptValueFromValue_helper(this, type, ptr); | 910 | QVariant variant(type, ptr); |
| 932 | return ScriptValue(new ScriptValueQtWrapper(const_cast<ScriptEngineQtScript*>(this), std::move(result))); | 911 | QScriptValue scriptValue = castVariantToValue(variant); |
| 912 | return ScriptValue(new ScriptValueQtWrapper(this, std::move(scriptValue))); | ||
| 933 | } | 913 | } |
| 934 | 914 | ||
| 935 | bool ScriptEngineQtScript::convert(const ScriptValue& value, int type, void* ptr) { | 915 | QVariant ScriptEngineQtScript::convert(const ScriptValue& value, int type) { |
| 936 | ScriptValueQtWrapper* unwrapped = ScriptValueQtWrapper::unwrap(value); | 916 | ScriptValueQtWrapper* unwrapped = ScriptValueQtWrapper::unwrap(value); |
| 937 | if (unwrapped == nullptr) { | 917 | if (unwrapped == nullptr) { |
| 938 | return false; | 918 | return QVariant(); |
| 939 | } | 919 | } |
| 940 | return qscriptvalue_cast_helper(unwrapped->toQtValue(), type, ptr); | ||
| 941 | } | ||
| 942 | 920 | ||
| 943 | template <int i> | 921 | QVariant var; |
| 944 | class CustomTypeInstance { | 922 | if (!castValueToVariant(unwrapped->toQtValue(), var, type)) { |
| 945 | public: | 923 | return QVariant(); |
| 946 | static ScriptEngine::MarshalFunction marshalFunc; | 924 | } |
| 947 | static ScriptEngine::DemarshalFunction demarshalFunc; | ||
| 948 | |||
| 949 | static QScriptValue internalMarshalFunc(QScriptEngine* engine, const void* src) { | ||
| 950 | ScriptEngineQtScript* unwrappedEngine = static_cast<ScriptEngineQtScript*>(engine); | ||
| 951 | ScriptValue dest = marshalFunc(unwrappedEngine, src); | ||
| 952 | return ScriptValueQtWrapper::fullUnwrap(unwrappedEngine, dest); | ||
| 953 | } | ||
| 954 | |||
| 955 | static void internalDemarshalFunc(const QScriptValue& src, void* dest) { | ||
| 956 | ScriptEngineQtScript* unwrappedEngine = static_cast<ScriptEngineQtScript*>(src.engine()); | ||
| 957 | ScriptValue wrappedSrc(new ScriptValueQtWrapper(unwrappedEngine, src)); | ||
| 958 | demarshalFunc(wrappedSrc, dest); | ||
| 959 | } | ||
| 960 | }; | ||
| 961 | template <int i> | ||
| 962 | ScriptEngine::MarshalFunction CustomTypeInstance<i>::marshalFunc; | ||
| 963 | template <int i> | ||
| 964 | ScriptEngine::DemarshalFunction CustomTypeInstance<i>::demarshalFunc; | ||
| 965 | |||
| 966 | // I would *LOVE* it if there was a different way to do this, jeez! | ||
| 967 | // Qt requires two functions that have no parameters that give any context, | ||
| 968 | // one of the must return a QScriptValue (so we can't void* them into generics and stick them in the templates). | ||
| 969 | // This *has* to be done via templates but the whole point of this is to avoid leaking types into the rest of | ||
| 970 | // the system that would require anyone other than us to have a dependency on QtScript | ||
| 971 | #define CUSTOM_TYPE_ENTRY(idx) \ | ||
| 972 | case idx: \ | ||
| 973 | CustomTypeInstance<idx>::marshalFunc = marshalFunc; \ | ||
| 974 | CustomTypeInstance<idx>::demarshalFunc = demarshalFunc; \ | ||
| 975 | internalMarshalFunc = CustomTypeInstance<idx>::internalMarshalFunc; \ | ||
| 976 | internalDemarshalFunc = CustomTypeInstance<idx>::internalDemarshalFunc; \ | ||
| 977 | break; | ||
| 978 | #define CUSTOM_TYPE_ENTRY_10(idx) \ | ||
| 979 | CUSTOM_TYPE_ENTRY((idx * 10)); \ | ||
| 980 | CUSTOM_TYPE_ENTRY((idx * 10) + 1); \ | ||
| 981 | CUSTOM_TYPE_ENTRY((idx * 10) + 2); \ | ||
| 982 | CUSTOM_TYPE_ENTRY((idx * 10) + 3); \ | ||
| 983 | CUSTOM_TYPE_ENTRY((idx * 10) + 4); \ | ||
| 984 | CUSTOM_TYPE_ENTRY((idx * 10) + 5); \ | ||
| 985 | CUSTOM_TYPE_ENTRY((idx * 10) + 6); \ | ||
| 986 | CUSTOM_TYPE_ENTRY((idx * 10) + 7); \ | ||
| 987 | CUSTOM_TYPE_ENTRY((idx * 10) + 8); \ | ||
| 988 | CUSTOM_TYPE_ENTRY((idx * 10) + 9); | ||
| 989 | |||
| 990 | void ScriptEngineQtScript::registerCustomType(int type, | ||
| 991 | ScriptEngine::MarshalFunction marshalFunc, | ||
| 992 | ScriptEngine::DemarshalFunction demarshalFunc, | ||
| 993 | const ScriptValue& prototype) | ||
| 994 | { | ||
| 995 | QScriptValue unwrapped = ScriptValueQtWrapper::fullUnwrap(this, prototype); | ||
| 996 | QScriptEngine::MarshalFunction internalMarshalFunc; | ||
| 997 | QScriptEngine::DemarshalFunction internalDemarshalFunc; | ||
| 998 | 925 | ||
| 999 | if (_nextCustomType >= 300) { // have we ran out of translators? | 926 | int destType = var.userType(); |
| 1000 | Q_ASSERT(false); | 927 | if (destType != type) { |
| 1001 | return; | 928 | var.convert(type); // if conversion fails then var is set to QVariant() |
| 1002 | } | 929 | } |
| 1003 | 930 | ||
| 1004 | switch (_nextCustomType++) { | 931 | return var; |
| 1005 | CUSTOM_TYPE_ENTRY_10(0); | ||
| 1006 | CUSTOM_TYPE_ENTRY_10(1); | ||
| 1007 | CUSTOM_TYPE_ENTRY_10(2); | ||
| 1008 | CUSTOM_TYPE_ENTRY_10(3); | ||
| 1009 | CUSTOM_TYPE_ENTRY_10(4); | ||
| 1010 | CUSTOM_TYPE_ENTRY_10(5); | ||
| 1011 | CUSTOM_TYPE_ENTRY_10(6); | ||
| 1012 | CUSTOM_TYPE_ENTRY_10(7); | ||
| 1013 | CUSTOM_TYPE_ENTRY_10(8); | ||
| 1014 | CUSTOM_TYPE_ENTRY_10(9); | ||
| 1015 | CUSTOM_TYPE_ENTRY_10(10); | ||
| 1016 | CUSTOM_TYPE_ENTRY_10(11); | ||
| 1017 | CUSTOM_TYPE_ENTRY_10(12); | ||
| 1018 | CUSTOM_TYPE_ENTRY_10(13); | ||
| 1019 | CUSTOM_TYPE_ENTRY_10(14); | ||
| 1020 | CUSTOM_TYPE_ENTRY_10(15); | ||
| 1021 | CUSTOM_TYPE_ENTRY_10(16); | ||
| 1022 | CUSTOM_TYPE_ENTRY_10(17); | ||
| 1023 | CUSTOM_TYPE_ENTRY_10(18); | ||
| 1024 | CUSTOM_TYPE_ENTRY_10(19); | ||
| 1025 | CUSTOM_TYPE_ENTRY_10(20); | ||
| 1026 | CUSTOM_TYPE_ENTRY_10(21); | ||
| 1027 | CUSTOM_TYPE_ENTRY_10(22); | ||
| 1028 | CUSTOM_TYPE_ENTRY_10(23); | ||
| 1029 | CUSTOM_TYPE_ENTRY_10(24); | ||
| 1030 | CUSTOM_TYPE_ENTRY_10(25); | ||
| 1031 | CUSTOM_TYPE_ENTRY_10(26); | ||
| 1032 | CUSTOM_TYPE_ENTRY_10(27); | ||
| 1033 | CUSTOM_TYPE_ENTRY_10(28); | ||
| 1034 | CUSTOM_TYPE_ENTRY_10(29); | ||
| 1035 | CUSTOM_TYPE_ENTRY_10(30); | ||
| 1036 | } | ||
| 1037 | |||
| 1038 | qScriptRegisterMetaType_helper(this, type, internalMarshalFunc, internalDemarshalFunc, unwrapped); | ||
| 1039 | } | 932 | } |