Changeset 930
- Timestamp:
- 05/27/20 12:14:12 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/vm/classes/collectionobj.cpp
r868 r930 1 1 // This file is a part of Framsticks SDK. http://www.framsticks.com/ 2 // Copyright (C) 1999-20 19Maciej Komosinski and Szymon Ulatowski.2 // Copyright (C) 1999-2020 Maciej Komosinski and Szymon Ulatowski. 3 3 // See LICENSE.txt for details. 4 4 … … 270 270 VMachine *vm; 271 271 VMVEComparator(VMachine::JumpTargetObject *_jto) :jto(_jto), vm(jto->vm) {} 272 bool operator()(const ExtValue *a, const ExtValue *b);272 static int compare(const void *a, const void *b, void* _this); 273 273 }; 274 274 275 bool VMVEComparator::operator()(const ExtValue *a, const ExtValue *b) 276 { 275 int VMVEComparator::compare(const void *a, const void *b, void* _this) 276 { 277 VMachine *vm = ((VMVEComparator*)_this)->vm; 278 VMachine::JumpTargetObject *jto = ((VMVEComparator*)_this)->jto; 277 279 if (!VMCode::prepareDynamicJumpTarget(jto->pc, jto->code)) 278 280 return false; 279 281 280 vm->push(* a);281 vm->push(* b);282 vm->push(**(const ExtValue **)a); 283 vm->push(**(const ExtValue **)b); 282 284 vm->pushNewCallState(); 283 285 vm->jumpDynamicJumpTarget(jto->pc); 284 286 vm->run(); 285 287 vm->popCallState(); 286 boolret;288 int ret; 287 289 ExtValue& retval = vm->getValue(); 288 290 if (retval.type == TInvalid) 289 291 { 290 ret = false;292 ret = 0; 291 293 logPrintf("VectorElementComparator", "", LOG_ERROR, "Comparison function returned no value"); 292 294 } 293 295 else 294 ret = (retval.getInt() != 0) ;296 ret = (retval.getInt() != 0) ? -1 : 1; 295 297 vm->drop(2); 296 298 return ret; … … 306 308 VMVEComparator cmp(jto); 307 309 ExtValue **first = (ExtValue**)&data.getref(0); 308 std::sort(first, first + data.size(), cmp); 310 //Originally was the same as below: std::sort(first, first + data.size(), cmp); 311 //However, std::sort() requires "strict weak ordering" and may crash (and indeed crashes, "undefined behavior") when given a non-compliant comparator. 312 //We use qsort() instead because we can't control what kind of user script comparator function will be passed to Vector.sort(), and qsort() seems to behave safely for every function. 313 qsort_r(first, data.size(), sizeof(ExtValue*), cmp.compare, &cmp); 309 314 } 310 315 else
Note: See TracChangeset
for help on using the changeset viewer.