mirror of
https://github.com/Armored-Dragon/overte.git
synced 2025-03-11 16:13:16 +01:00
Safer demarshal functions with no void pointers
This commit is contained in:
parent
acbec55b70
commit
89f29ce5ca
4 changed files with 21 additions and 18 deletions
|
@ -336,15 +336,16 @@ namespace scriptable {
|
||||||
}
|
}
|
||||||
return engine->newQObject(object, ScriptEngine::QtOwnership, ScriptEngine::AutoCreateDynamicProperties);
|
return engine->newQObject(object, ScriptEngine::QtOwnership, ScriptEngine::AutoCreateDynamicProperties);
|
||||||
},
|
},
|
||||||
[](const ScriptValue& value, void* p) -> bool {
|
[](const ScriptValue& value, QVariant &dest) -> bool {
|
||||||
Q_ASSERT(p != NULL);
|
//Q_ASSERT(p != NULL);
|
||||||
QPointer<T>& out = *(reinterpret_cast<QPointer<T>* >(p));
|
//QPointer<T>& out = *(reinterpret_cast<QPointer<T>* >(p));
|
||||||
auto obj = value.toQObject();
|
auto obj = value.toQObject();
|
||||||
#ifdef SCRIPTABLE_MESH_DEBUG
|
#ifdef SCRIPTABLE_MESH_DEBUG
|
||||||
qCInfo(graphics_scripting) << "qpointer_qobject_cast" << obj << value.toString();
|
qCInfo(graphics_scripting) << "qpointer_qobject_cast" << obj << value.toString();
|
||||||
#endif
|
#endif
|
||||||
if (auto tmp = qobject_cast<T*>(obj)) {
|
if (auto tmp = qobject_cast<T*>(obj)) {
|
||||||
out = QPointer<T>(tmp);
|
//out = QPointer<T>(tmp);
|
||||||
|
dest.template setValue(QPointer<T>(tmp));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
#if 0
|
#if 0
|
||||||
|
@ -356,7 +357,7 @@ namespace scriptable {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
out = nullptr;
|
//out = nullptr;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
@ -676,13 +677,14 @@ namespace scriptable {
|
||||||
engine,
|
engine,
|
||||||
[](ScriptEngine* engine, const void* p) -> ScriptValue {
|
[](ScriptEngine* engine, const void* p) -> ScriptValue {
|
||||||
Q_ASSERT(p != NULL);
|
Q_ASSERT(p != NULL);
|
||||||
|
// V8TODO: I'm not sure if this is safe
|
||||||
const T& topology = *(reinterpret_cast<const T*>(p));
|
const T& topology = *(reinterpret_cast<const T*>(p));
|
||||||
return engine->newValue(instance.value(topology));
|
return engine->newValue(instance.value(topology));
|
||||||
},
|
},
|
||||||
[](const ScriptValue& value, void* p) -> bool {
|
[](const ScriptValue& value, QVariant &dest) -> bool {
|
||||||
Q_ASSERT(p != NULL);
|
//Q_ASSERT(p != NULL);
|
||||||
T& topology = *(reinterpret_cast<T*>(p));
|
//T& topology = *(reinterpret_cast<T*>(p));
|
||||||
topology = instance.key(value.toString());
|
dest.setValue(instance.key(value.toString()));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
|
@ -49,7 +49,7 @@ class ScriptEngine {
|
||||||
public:
|
public:
|
||||||
typedef ScriptValue (*FunctionSignature)(ScriptContext*, ScriptEngine*);
|
typedef ScriptValue (*FunctionSignature)(ScriptContext*, ScriptEngine*);
|
||||||
typedef ScriptValue (*MarshalFunction)(ScriptEngine*, const void*);
|
typedef ScriptValue (*MarshalFunction)(ScriptEngine*, const void*);
|
||||||
typedef bool (*DemarshalFunction)(const ScriptValue&, void*);
|
typedef bool (*DemarshalFunction)(const ScriptValue&, QVariant &dest);
|
||||||
|
|
||||||
enum ValueOwnership {
|
enum ValueOwnership {
|
||||||
QtOwnership = 0,
|
QtOwnership = 0,
|
||||||
|
|
|
@ -76,10 +76,12 @@ ScriptValue toScriptValueWrapper(ScriptEngine* engine, const void *p) {
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, bool (*f)(const ScriptValue&, T&)>
|
template <typename T, bool (*f)(const ScriptValue&, T&)>
|
||||||
bool fromScriptValueWrapper(const ScriptValue& val, void* p) {
|
bool fromScriptValueWrapper(const ScriptValue& val, QVariant &destV) {
|
||||||
Q_ASSERT(p != NULL);
|
//auto &dest = *(reinterpret_cast<T*>(p));
|
||||||
auto &dest = *(reinterpret_cast<T*>(p));
|
T dest;
|
||||||
return f(val, dest);
|
bool result = f(val, dest);
|
||||||
|
destV.setValue(dest);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, ScriptValue (*toScriptValue)(ScriptEngine*, const T&), bool (*fromScriptValue)(const ScriptValue&, T&)>
|
template <typename T, ScriptValue (*toScriptValue)(ScriptEngine*, const T&), bool (*fromScriptValue)(const ScriptValue&, T&)>
|
||||||
|
@ -101,7 +103,7 @@ int scriptRegisterMetaType(ScriptEngine* eng, const char* name = "",
|
||||||
template <typename T>
|
template <typename T>
|
||||||
int scriptRegisterMetaTypeWithLambdas(ScriptEngine* eng,
|
int scriptRegisterMetaTypeWithLambdas(ScriptEngine* eng,
|
||||||
ScriptValue (*toScriptValue)(ScriptEngine*, const void *),
|
ScriptValue (*toScriptValue)(ScriptEngine*, const void *),
|
||||||
bool (*fromScriptValue)(const ScriptValue&, void *), const char* name = "",
|
bool (*fromScriptValue)(const ScriptValue&, QVariant &dest), const char* name = "",
|
||||||
T* = 0)
|
T* = 0)
|
||||||
{
|
{
|
||||||
int id;
|
int id;
|
||||||
|
|
|
@ -344,9 +344,8 @@ bool ScriptEngineV8::castValueToVariant(const V8ScriptValue& v8Val, QVariant& de
|
||||||
if (demarshalFunc) {
|
if (demarshalFunc) {
|
||||||
dest = QVariant(destTypeId, static_cast<void*>(NULL));
|
dest = QVariant(destTypeId, static_cast<void*>(NULL));
|
||||||
ScriptValue wrappedVal(new ScriptValueV8Wrapper(this, v8Val));
|
ScriptValue wrappedVal(new ScriptValueV8Wrapper(this, v8Val));
|
||||||
Q_ASSERT(dest.constData() != nullptr);
|
//Q_ASSERT(dest.constData() != nullptr);
|
||||||
//V8TODO: Data should never be written to dest.constData() according to Qt documentation.
|
bool success = demarshalFunc(wrappedVal, dest);
|
||||||
bool success = demarshalFunc(wrappedVal, const_cast<void*>(dest.constData()));
|
|
||||||
if(!success) dest = QVariant();
|
if(!success) dest = QVariant();
|
||||||
return success;
|
return success;
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue