History

added custom type support

Heather Anderson committed 5 years ago

Commit 5f2b2c91 parent 45ef4582 files · +64 −8Browse files

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

Unified · Split

libraries/script-engine/src/qtscript/ScriptEngineQtScript.cpp+52−7
@@ -1013,25 +1013,70 @@ bool ScriptEngineQtScript::raiseException(const ScriptValuePointer& exception) { · 1012 unmodified lines
1013}1013}
1014 1014
1015ScriptValuePointer ScriptEngineQtScript::create(int type, const void* ptr) {1015ScriptValuePointer ScriptEngineQtScript::create(int type, const void* ptr) {
1016 // first we'll try custom types registered with us
1017 ScriptEngine::MarshalFunction marshalFunc = nullptr;
1018 ScriptValuePointer prototype;
1019 {
1020 std::lock_guard<std::mutex> guard(_customTypeProtect);
1021 TCustomTypeMap::const_iterator loc = _customTypes.find(type);
1022 if (loc != _customTypes.end()) {
1023 const CustomTypeInfo& typeInfo = loc->second;
1024 marshalFunc = typeInfo.marshalFunc;
1025 prototype = typeInfo.prototype;
1026 }
1027 }
1028 if (marshalFunc) {
1029 ScriptValuePointer result = marshalFunc(this, ptr);
1030 if (result && prototype) {
1031 result->setPrototype(prototype);
1032 }
1033 return result;
1034 }
1035
1036 // falling back to having QtScript handle it
1016 QScriptValue result = qScriptValueFromValue_helper(this, type, ptr);1037 QScriptValue result = qScriptValueFromValue_helper(this, type, ptr);
1017 return ScriptValuePointer(new ScriptValueQtWrapper(const_cast<ScriptEngineQtScript*>(this), std::move(result)));1038 return ScriptValuePointer(new ScriptValueQtWrapper(const_cast<ScriptEngineQtScript*>(this), std::move(result)));
1018}1039}
1019 1040
1020bool ScriptEngineQtScript::convert(const ScriptValuePointer& value, int type, void* ptr) {1041bool ScriptEngineQtScript::convert(const ScriptValuePointer& value, int type, void* ptr) {
1042 // first we'll try custom types registered with us
1043 ScriptEngine::DemarshalFunction demarshalFunc = nullptr;
1044 {
1045 std::lock_guard<std::mutex> guard(_customTypeProtect);
1046 TCustomTypeMap::const_iterator loc = _customTypes.find(type);
1047 if (loc != _customTypes.end()) {
1048 demarshalFunc = loc->second.demarshalFunc;
1049 }
1050 }
1051 if (demarshalFunc) {
1052 demarshalFunc(value, ptr);
1053 return true;
1054 }
1055
1056 // falling back to having QtScript handle it
1021 ScriptValueQtWrapper* unwrapped = ScriptValueQtWrapper::unwrap(value);1057 ScriptValueQtWrapper* unwrapped = ScriptValueQtWrapper::unwrap(value);
1022 if (unwrapped == nullptr) {1058 if (unwrapped == nullptr) {
1023 return false;1059 return false;
1024 }1060 }
1025 return qscriptvalue_cast_helper(unwrapped->toQtValue(), type, ptr);1061 return qscriptvalue_cast_helper(unwrapped->toQtValue(), type, ptr);
1026}1062}
1027/*1063
1028void ScriptEngineQtScript::registerCustomType(int type,1064void ScriptEngineQtScript::registerCustomType(int type,
1029 ScriptEngine::MarshalFunction mf,1065 ScriptEngine::MarshalFunction marshalFunc,
1030 ScriptEngine::DemarshalFunction df,1066 ScriptEngine::DemarshalFunction demarshalFunc,
1031 const ScriptValuePointer& prototype) {1067 const ScriptValuePointer& prototype)
1068{
1032 ScriptValueQtWrapper* unwrapped = ScriptValueQtWrapper::unwrap(prototype);1069 ScriptValueQtWrapper* unwrapped = ScriptValueQtWrapper::unwrap(prototype);
1033 if (unwrapped != nullptr) {1070 if (unwrapped == nullptr) {
1034 qScriptRegisterMetaType_helper(this, type, mf, df, unwrapped->toQtValue());1071 return;
1072 }
1073
1074 std::lock_guard<std::mutex> guard(_customTypeProtect);
1075 TCustomTypeMap::iterator loc = _customTypes.find(type);
1076 if(loc == _customTypes.end()) {
1077 _customTypes.insert(TCustomTypeMap::value_type(type, CustomTypeInfo())).first;
1035 }1078 }
1079 CustomTypeInfo& typeInfo = loc->second;
1080 typeInfo.marshalFunc = marshalFunc;
1081 typeInfo.demarshalFunc = demarshalFunc;
1036}1082}
1037*/

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