From 86e070d61c9fb014253f6c244a1f571e89d70315 Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Sat, 20 Mar 2021 11:49:58 +0200 Subject: [PATCH] Use optimized type identification. --- module/Core/Common.cpp | 78 +++++++++++++++++++++++++++++------------- 1 file changed, 54 insertions(+), 24 deletions(-) diff --git a/module/Core/Common.cpp b/module/Core/Common.cpp index ed6f1cc6..b057ec2b 100644 --- a/module/Core/Common.cpp +++ b/module/Core/Common.cpp @@ -318,26 +318,41 @@ SQInteger PopStackInteger(HSQUIRRELVM vm, SQInteger idx) } case OT_INSTANCE: { - // Attempt to treat the value as a signed long instance - try + SQUserPointer tag; + // Attempt to retrieve the type tag + if (SQ_FAILED(sq_gettypetag(vm, -1, &tag))) { - return ConvTo< SQInteger >::From(Var< const SLongInt & >(vm, idx).value.GetNum()); + break; } - catch (...) + // Is the instance SLongInt? (signed long) + else if (static_cast< AbstractStaticClassData * >(tag) == StaticClassTypeTag< SLongInt >::Get()) { - // Just ignore it... + try + { + return ConvTo< SQInteger >::From(Var< const SLongInt & >(vm, idx).value.GetNum()); + } + catch (...) + { + // Just ignore it... + } } - // Attempt to treat the value as a unsigned long instance - try + // Is the instance ULongInt? (unsigned long) + else if (static_cast< AbstractStaticClassData * >(tag) == StaticClassTypeTag< ULongInt >::Get()) { - return ConvTo< SQInteger >::From(Var< const ULongInt & >(vm, idx).value.GetNum()); + try + { + return ConvTo< SQInteger >::From(Var< const ULongInt & >(vm, idx).value.GetNum()); + } + catch (...) + { + // Just ignore it... + } } - catch (...) + else { - // Just ignore it... + // Attempt to get the size of the instance as a fall back + return sq_getsize(vm, idx); } - // Attempt to get the size of the instance as a fall back - return sq_getsize(vm, idx); } default: break; } @@ -391,26 +406,41 @@ SQFloat PopStackFloat(HSQUIRRELVM vm, SQInteger idx) } case OT_INSTANCE: { - // Attempt to treat the value as a signed long instance - try + SQUserPointer tag; + // Attempt to retrieve the type tag + if (SQ_FAILED(sq_gettypetag(vm, -1, &tag))) { - return ConvTo< SQFloat >::From(Var< const SLongInt & >(vm, idx).value.GetNum()); + break; } - catch (...) + // Is the instance SLongInt? (signed long) + else if (static_cast< AbstractStaticClassData * >(tag) == StaticClassTypeTag< SLongInt >::Get()) { - // Just ignore it... + try + { + return ConvTo< SQFloat >::From(Var< const SLongInt & >(vm, idx).value.GetNum()); + } + catch (...) + { + // Just ignore it... + } } - // Attempt to treat the value as a unsigned long instance - try + // Is the instance ULongInt? (unsigned long) + else if (static_cast< AbstractStaticClassData * >(tag) == StaticClassTypeTag< ULongInt >::Get()) { - return ConvTo< SQFloat >::From(Var< const ULongInt & >(vm, idx).value.GetNum()); + try + { + return ConvTo< SQFloat >::From(Var< const ULongInt & >(vm, idx).value.GetNum()); + } + catch (...) + { + // Just ignore it... + } } - catch (...) + else { - // Just ignore it... + // Attempt to get the size of the instance as a fall back + return ConvTo< SQFloat >::From(sq_getsize(vm, idx)); } - // Attempt to get the size of the instance as a fall back - return ConvTo< SQFloat >::From(sq_getsize(vm, idx)); } default: break; }