mirror of
https://github.com/overte-org/overte.git
synced 2025-04-11 13:42:38 +02: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);
|
||||
},
|
||||
[](const ScriptValue& value, void* p) -> bool {
|
||||
Q_ASSERT(p != NULL);
|
||||
QPointer<T>& out = *(reinterpret_cast<QPointer<T>* >(p));
|
||||
[](const ScriptValue& value, QVariant &dest) -> bool {
|
||||
//Q_ASSERT(p != NULL);
|
||||
//QPointer<T>& out = *(reinterpret_cast<QPointer<T>* >(p));
|
||||
auto obj = value.toQObject();
|
||||
#ifdef SCRIPTABLE_MESH_DEBUG
|
||||
qCInfo(graphics_scripting) << "qpointer_qobject_cast" << obj << value.toString();
|
||||
#endif
|
||||
if (auto tmp = qobject_cast<T*>(obj)) {
|
||||
out = QPointer<T>(tmp);
|
||||
//out = QPointer<T>(tmp);
|
||||
dest.template setValue(QPointer<T>(tmp));
|
||||
return true;
|
||||
}
|
||||
#if 0
|
||||
|
@ -356,7 +357,7 @@ namespace scriptable {
|
|||
return true;
|
||||
}
|
||||
#endif
|
||||
out = nullptr;
|
||||
//out = nullptr;
|
||||
return false;
|
||||
}
|
||||
);
|
||||
|
@ -676,13 +677,14 @@ namespace scriptable {
|
|||
engine,
|
||||
[](ScriptEngine* engine, const void* p) -> ScriptValue {
|
||||
Q_ASSERT(p != NULL);
|
||||
// V8TODO: I'm not sure if this is safe
|
||||
const T& topology = *(reinterpret_cast<const T*>(p));
|
||||
return engine->newValue(instance.value(topology));
|
||||
},
|
||||
[](const ScriptValue& value, void* p) -> bool {
|
||||
Q_ASSERT(p != NULL);
|
||||
T& topology = *(reinterpret_cast<T*>(p));
|
||||
topology = instance.key(value.toString());
|
||||
[](const ScriptValue& value, QVariant &dest) -> bool {
|
||||
//Q_ASSERT(p != NULL);
|
||||
//T& topology = *(reinterpret_cast<T*>(p));
|
||||
dest.setValue(instance.key(value.toString()));
|
||||
return true;
|
||||
}
|
||||
);
|
||||
|
|
|
@ -49,7 +49,7 @@ class ScriptEngine {
|
|||
public:
|
||||
typedef ScriptValue (*FunctionSignature)(ScriptContext*, ScriptEngine*);
|
||||
typedef ScriptValue (*MarshalFunction)(ScriptEngine*, const void*);
|
||||
typedef bool (*DemarshalFunction)(const ScriptValue&, void*);
|
||||
typedef bool (*DemarshalFunction)(const ScriptValue&, QVariant &dest);
|
||||
|
||||
enum ValueOwnership {
|
||||
QtOwnership = 0,
|
||||
|
|
|
@ -76,10 +76,12 @@ ScriptValue toScriptValueWrapper(ScriptEngine* engine, const void *p) {
|
|||
}
|
||||
|
||||
template <typename T, bool (*f)(const ScriptValue&, T&)>
|
||||
bool fromScriptValueWrapper(const ScriptValue& val, void* p) {
|
||||
Q_ASSERT(p != NULL);
|
||||
auto &dest = *(reinterpret_cast<T*>(p));
|
||||
return f(val, dest);
|
||||
bool fromScriptValueWrapper(const ScriptValue& val, QVariant &destV) {
|
||||
//auto &dest = *(reinterpret_cast<T*>(p));
|
||||
T dest;
|
||||
bool result = f(val, dest);
|
||||
destV.setValue(dest);
|
||||
return result;
|
||||
}
|
||||
|
||||
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>
|
||||
int scriptRegisterMetaTypeWithLambdas(ScriptEngine* eng,
|
||||
ScriptValue (*toScriptValue)(ScriptEngine*, const void *),
|
||||
bool (*fromScriptValue)(const ScriptValue&, void *), const char* name = "",
|
||||
bool (*fromScriptValue)(const ScriptValue&, QVariant &dest), const char* name = "",
|
||||
T* = 0)
|
||||
{
|
||||
int id;
|
||||
|
|
|
@ -344,9 +344,8 @@ bool ScriptEngineV8::castValueToVariant(const V8ScriptValue& v8Val, QVariant& de
|
|||
if (demarshalFunc) {
|
||||
dest = QVariant(destTypeId, static_cast<void*>(NULL));
|
||||
ScriptValue wrappedVal(new ScriptValueV8Wrapper(this, v8Val));
|
||||
Q_ASSERT(dest.constData() != nullptr);
|
||||
//V8TODO: Data should never be written to dest.constData() according to Qt documentation.
|
||||
bool success = demarshalFunc(wrappedVal, const_cast<void*>(dest.constData()));
|
||||
//Q_ASSERT(dest.constData() != nullptr);
|
||||
bool success = demarshalFunc(wrappedVal, dest);
|
||||
if(!success) dest = QVariant();
|
||||
return success;
|
||||
} else {
|
||||
|
|
Loading…
Reference in a new issue