Internal
| 1310 | |
| 1311 | // Internal |
| 1312 | void CScriptArray::Sort(std::uint32_t startAt, std::uint32_t count, bool asc) |
| 1313 | { |
| 1314 | // Subtype isn't primitive and doesn't have opCmp |
| 1315 | SArrayCache* cache = static_cast<SArrayCache*>(objType->GetUserData(ARRAY_CACHE)); |
| 1316 | if (subTypeId & ~asTYPEID_MASK_SEQNBR) { |
| 1317 | if (cache == nullptr || cache->cmpFunc == 0) { |
| 1318 | asIScriptContext* ctx = asGetActiveContext(); |
| 1319 | asITypeInfo* subType = objType->GetEngine()->GetTypeInfoById(subTypeId); |
| 1320 | |
| 1321 | // Throw an exception |
| 1322 | if (ctx != nullptr) { |
| 1323 | char tmp[256]; |
| 1324 | |
| 1325 | if (cache && cache->cmpFuncReturnCode == asMULTIPLE_FUNCTIONS) { |
| 1326 | #if defined(DEATH_TARGET_MSVC) |
| 1327 | sprintf_s(tmp, arraySize(tmp), "Type '%s' has multiple matching opCmp methods", subType->GetName()); |
| 1328 | #else |
| 1329 | sprintf(tmp, "Type '%s' has multiple matching opCmp methods", subType->GetName()); |
| 1330 | #endif |
| 1331 | } else { |
| 1332 | #if defined(DEATH_TARGET_MSVC) |
| 1333 | sprintf_s(tmp, arraySize(tmp), "Type '%s' does not have a matching opCmp method", subType->GetName()); |
| 1334 | #else |
| 1335 | sprintf(tmp, "Type '%s' does not have a matching opCmp method", subType->GetName()); |
| 1336 | #endif |
| 1337 | } |
| 1338 | ctx->SetException(tmp); |
| 1339 | } |
| 1340 | |
| 1341 | return; |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | // No need to sort |
| 1346 | if (count < 2) { |
| 1347 | return; |
| 1348 | } |
| 1349 | |
| 1350 | std::int32_t start = startAt; |
| 1351 | std::int32_t end = startAt + count; |
| 1352 | |
| 1353 | // Check if we could access invalid item while sorting |
| 1354 | if (start >= (std::int32_t)buffer->numElements || end > (std::int32_t)buffer->numElements) { |
| 1355 | asIScriptContext* ctx = asGetActiveContext(); |
| 1356 | |
| 1357 | // Throw an exception |
| 1358 | if (ctx) { |
| 1359 | ctx->SetException("Index out of bounds"); |
| 1360 | } |
| 1361 | |
| 1362 | return; |
| 1363 | } |
| 1364 | |
| 1365 | if (subTypeId & ~asTYPEID_MASK_SEQNBR) { |
| 1366 | asIScriptContext* cmpContext = 0; |
| 1367 | bool isNested = false; |
| 1368 | |
| 1369 | // Try to reuse the active context |