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
10131013 }
10141014
10151015 ScriptValuePointer 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
10161037 QScriptValue result = qScriptValueFromValue_helper(this, type, ptr);
10171038 return ScriptValuePointer(new ScriptValueQtWrapper(const_cast<ScriptEngineQtScript*>(this), std::move(result)));
10181039 }
10191040
10201041 bool 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
10211057 ScriptValueQtWrapper* unwrapped = ScriptValueQtWrapper::unwrap(value);
10221058 if (unwrapped == nullptr) {
10231059 return false;
10241060 }
10251061 return qscriptvalue_cast_helper(unwrapped->toQtValue(), type, ptr);
10261062 }
1027−/*
1063+
10281064 void ScriptEngineQtScript::registerCustomType(int type,
1029− ScriptEngine::MarshalFunction mf,
1030− ScriptEngine::DemarshalFunction df,
1031− const ScriptValuePointer& prototype) {
1065+ ScriptEngine::MarshalFunction marshalFunc,
1066+ ScriptEngine::DemarshalFunction demarshalFunc,
1067+ const ScriptValuePointer& prototype)
1068+{
10321069 ScriptValueQtWrapper* unwrapped = ScriptValueQtWrapper::unwrap(prototype);
1033− if (unwrapped != nullptr) {
1034− qScriptRegisterMetaType_helper(this, type, mf, df, unwrapped->toQtValue());
1070+ if (unwrapped == nullptr) {
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;
10351078 }
1079+ CustomTypeInfo& typeInfo = loc->second;
1080+ typeInfo.marshalFunc = marshalFunc;
1081+ typeInfo.demarshalFunc = demarshalFunc;
10361082 }
1037−*/