fix: 修复脚本调用因参数类型可能失败的问题和内存泄漏;

This commit is contained in:
寂静的羽夏 2025-07-30 23:20:02 +08:00
parent 263e3eb7e2
commit 69ae150df5
7 changed files with 40 additions and 77 deletions

View File

@ -306,12 +306,12 @@
<location filename="../testplugin.cpp" line="246"/>
<location filename="../testplugin.cpp" line="253"/>
<location filename="../testplugin.cpp" line="260"/>
<location filename="../testplugin.cpp" line="289"/>
<location filename="../testplugin.cpp" line="297"/>
<location filename="../testplugin.cpp" line="305"/>
<location filename="../testplugin.cpp" line="313"/>
<location filename="../testplugin.cpp" line="322"/>
<location filename="../testplugin.cpp" line="329"/>
<location filename="../testplugin.cpp" line="288"/>
<location filename="../testplugin.cpp" line="296"/>
<location filename="../testplugin.cpp" line="304"/>
<location filename="../testplugin.cpp" line="312"/>
<location filename="../testplugin.cpp" line="321"/>
<location filename="../testplugin.cpp" line="328"/>
<source>InvalidParamsCount</source>
<translation></translation>
</message>
@ -322,7 +322,7 @@
<translation></translation>
</message>
<message>
<location filename="../testplugin.cpp" line="280"/>
<location filename="../testplugin.cpp" line="279"/>
<source>AllocArrayFailed</source>
<translation></translation>
</message>

View File

@ -263,7 +263,7 @@ WingHex::UNSAFE_RET TestPlugin::colorTable(const QList<void *> &params) {
void *array = nullptr;
QVector<void *> colors;
for (auto &c : colorTable()) {
colors.append(new QColor(c));
colors.append(&c);
}
auto invoked =
@ -271,7 +271,6 @@ WingHex::UNSAFE_RET TestPlugin::colorTable(const QList<void *> &params) {
qReturnArg(array), WingHex::MetaType::Meta_Color, colors);
if (invoked) {
if (array) {
qDeleteAll(colors);
return array;
}
}

View File

@ -5467,18 +5467,18 @@
<translation> AngelScript API </translation>
</message>
<message>
<location filename="../../src/class/wingangelapi.cpp" line="1307"/>
<location filename="../../src/class/wingangelapi.cpp" line="1310"/>
<source>NotSupportedQMetaType:</source>
<translation> QT </translation>
</message>
<message>
<location filename="../../src/class/wingangelapi.cpp" line="1764"/>
<location filename="../../src/class/wingangelapi.cpp" line="1848"/>
<location filename="../../src/class/wingangelapi.cpp" line="1767"/>
<location filename="../../src/class/wingangelapi.cpp" line="1851"/>
<source>Get Exception While ScriptCall: (%1) %2</source>
<translation>%1%2</translation>
</message>
<message>
<location filename="../../src/class/wingangelapi.cpp" line="1790"/>
<location filename="../../src/class/wingangelapi.cpp" line="1793"/>
<source>InvalidRetType: need </source>
<translation> </translation>
</message>

View File

@ -5467,18 +5467,18 @@
<translation> AngelScript 調 API </translation>
</message>
<message>
<location filename="../../src/class/wingangelapi.cpp" line="1307"/>
<location filename="../../src/class/wingangelapi.cpp" line="1310"/>
<source>NotSupportedQMetaType:</source>
<translation> QT </translation>
</message>
<message>
<location filename="../../src/class/wingangelapi.cpp" line="1764"/>
<location filename="../../src/class/wingangelapi.cpp" line="1848"/>
<location filename="../../src/class/wingangelapi.cpp" line="1767"/>
<location filename="../../src/class/wingangelapi.cpp" line="1851"/>
<source>Get Exception While ScriptCall: (%1) %2</source>
<translation>調%1%2</translation>
</message>
<message>
<location filename="../../src/class/wingangelapi.cpp" line="1790"/>
<location filename="../../src/class/wingangelapi.cpp" line="1793"/>
<source>InvalidRetType: need </source>
<translation> </translation>
</message>

View File

@ -600,7 +600,7 @@ bool PluginSystem::invokeServiceImpl(const QObject *sender, const QString &puid,
if (met.name() == method) {
bool err = false;
for (int i = 1; i < paramCount; ++i) {
if (met.parameterType(i) != metaTypes[i]->typeId) {
if (met.parameterTypeName(i).compare(metaTypes[i]->name)) {
err = true;
break;
}

View File

@ -1061,12 +1061,10 @@ QStringList WingAngelAPI::cArray2QStringList(const CScriptArray &array,
QStringList buffer;
buffer.reserve(array.GetSize());
array.AddRef();
for (asUINT i = 0; i < array.GetSize(); ++i) {
auto item = reinterpret_cast<const QString *>(array.At(i));
buffer.append(*item);
}
array.Release();
return buffer;
}
@ -1217,9 +1215,10 @@ void WingAngelAPI::qvariantCastOp(
assignTmpBuffer(buffer, var.toULongLong());
fn(&buffer, type);
break;
case QMetaType::QChar:
fn(new QChar(var.toChar()), type);
break;
case QMetaType::QChar: {
auto obj = var.toChar();
fn(&obj, type);
} break;
case QMetaType::Float:
assignTmpBuffer(buffer, var.toULongLong());
fn(&buffer, type);
@ -1229,21 +1228,24 @@ void WingAngelAPI::qvariantCastOp(
fn(&buffer, type);
break;
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
case QMetaType::Char16:
fn(new QChar(var.value<char16_t>()), type);
break;
case QMetaType::Char32:
fn(new QChar(var.value<char32_t>()), type);
break;
case QMetaType::Char16: {
auto obj = QChar(var.value<char16_t>());
fn(&obj, type);
} break;
case QMetaType::Char32: {
auto obj = QChar(var.value<char32_t>());
fn(&obj, type);
} break;
#endif
case QMetaType::UChar:
case QMetaType::SChar:
case QMetaType::Char:
assignTmpBuffer(buffer, var.value<uchar>());
break;
case QMetaType::QString:
fn(new QString(var.toString()), type);
break;
case QMetaType::QString: {
auto obj = var.toString();
fn(&obj, type);
} break;
case QMetaType::QByteArray: {
auto value = var.toByteArray();
auto info = engine->GetTypeInfoByDecl("array<byte>");
@ -1298,9 +1300,10 @@ void WingAngelAPI::qvariantCastOp(
fn(arr, type);
arr->Release();
} break;
case QMetaType::QColor:
fn(new QColor(var.value<QColor>()), type);
break;
case QMetaType::QColor: {
auto obj = var.value<QColor>();
fn(&obj, type);
} break;
case QMetaType::Void:
break;
default:
@ -2000,7 +2003,9 @@ void *WingAngelAPI::vector2AsArray(const WingHex::SenderInfo &sender,
if (info) {
auto len = content.length();
auto arr = CScriptArray::Create(info, len);
std::memcpy(arr->GetBuffer(), content.data(), len);
for (int i = 0; i < len; ++i) {
arr->SetValue(i, content.at(i));
}
return arr;
}
return nullptr;
@ -2009,37 +2014,8 @@ void *WingAngelAPI::vector2AsArray(const WingHex::SenderInfo &sender,
void *WingAngelAPI::list2AsArray(const WingHex::SenderInfo &sender,
WingHex::MetaType type,
const QList<void *> &content) {
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
static_assert(std::is_same_v<QList<int>, QVector<int>>);
return vector2AsArray(sender, type, content);
#else
Q_UNUSED(sender);
auto typeStr = type2AngelScriptString(MetaType(type | MetaType::Meta_Array),
false, true);
if (typeStr.isEmpty()) {
return nullptr;
}
auto engine = ScriptMachine::instance().engine();
auto info = engine->GetTypeInfoByDecl(typeStr.toUtf8());
if (info) {
auto len = content.length();
auto arr = CScriptArray::Create(info, len);
for (decltype(len) i = 0; i < len; ++i) {
arr->SetValue(i, content.at(i));
}
return arr;
}
return nullptr;
#endif
}
void WingAngelAPI::deleteAsArray(const WingHex::SenderInfo &sender,
void *array) {
Q_UNUSED(sender);
if (array) {
reinterpret_cast<CScriptArray *>(array)->Release();
}
}
void *WingAngelAPI::newAsDictionary(
@ -2063,14 +2039,6 @@ void *WingAngelAPI::newAsDictionary(
return dic;
}
void WingAngelAPI::deleteAsDictionary(const WingHex::SenderInfo &sender,
void *dic) {
Q_UNUSED(sender);
if (dic) {
reinterpret_cast<CScriptDictionary *>(dic)->Release();
}
}
void WingAngelAPI::cleanUpHandles(const QVector<int> &handles) {
for (auto &h : _handles) {
if (!handles.contains(h)) {

View File

@ -175,14 +175,10 @@ private:
WING_SERVICE void *list2AsArray(const WingHex::SenderInfo &sender,
WingHex::MetaType type,
const QList<void *> &content);
WING_SERVICE void deleteAsArray(const WingHex::SenderInfo &sender,
void *array);
WING_SERVICE void *newAsDictionary(
const WingHex::SenderInfo &sender,
const QHash<QString, QPair<WingHex::MetaType, void *>> &content);
WING_SERVICE void deleteAsDictionary(const WingHex::SenderInfo &sender,
void *dic);
// =========================================================