From f1d8d60e96c6c5dcd4032a62694534e9aa836166 Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Thu, 9 Jun 2016 02:05:36 +0300 Subject: [PATCH] Improve the string retrieval from the stack to make use of the new sq_getstringandsize API function and also obtain the size everytime. Add two new helper functions to retrieve the string representation of a certain script type or object. --- shared/Base/Utility.cpp | 52 +++++++++++++++++++++++++++++++++++++++-- shared/Base/Utility.hpp | 10 ++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/shared/Base/Utility.cpp b/shared/Base/Utility.cpp index ea950371..9132efe1 100644 --- a/shared/Base/Utility.cpp +++ b/shared/Base/Utility.cpp @@ -617,7 +617,7 @@ StackStrF::StackStrF(HSQUIRRELVM vm, SQInteger idx, bool fmt) // Keep a strong reference to the object sq_addref(vm, &mObj); // Attempt to retrieve the string value from the stack - mRes = sq_getstring(vm, idx, &mPtr); + mRes = sq_getstringandsize(vm, idx, &mPtr, &mLen); } // Did the retrieval succeeded but ended up with a null string pointer? if (SQ_SUCCEEDED(mRes) && !mPtr) @@ -641,7 +641,7 @@ StackStrF::StackStrF(HSQUIRRELVM vm, SQInteger idx, bool fmt) // Keep a strong reference to the object sq_addref(vm, &mObj); // Attempt to obtain the string pointer - mRes = sq_getstring(vm, -1, &mPtr); + mRes = sq_getstringandsize(vm, -1, &mPtr, &mLen); } } // Pop a value from the stack regardless of the result @@ -663,6 +663,54 @@ StackStrF::~StackStrF() } } +// ------------------------------------------------------------------------------------------------ +CSStr SqTypeName(SQObjectType type) +{ + switch (type) + { + case OT_NULL: return _SC("null"); + case OT_INTEGER: return _SC("integer"); + case OT_FLOAT: return _SC("float"); + case OT_BOOL: return _SC("bool"); + case OT_STRING: return _SC("string"); + case OT_TABLE: return _SC("table"); + case OT_ARRAY: return _SC("array"); + case OT_USERDATA: return _SC("userdata"); + case OT_CLOSURE: return _SC("closure"); + case OT_NATIVECLOSURE: return _SC("nativeclosure"); + case OT_GENERATOR: return _SC("generator"); + case OT_USERPOINTER: return _SC("userpointer"); + case OT_THREAD: return _SC("thread"); + case OT_FUNCPROTO: return _SC("funcproto"); + case OT_CLASS: return _SC("class"); + case OT_INSTANCE: return _SC("instance"); + case OT_WEAKREF: return _SC("weakref"); + case OT_OUTER: return _SC("outer"); + default: return _SC("unknown"); + } +} + +// ------------------------------------------------------------------------------------------------ +String SqTypeName(HSQUIRRELVM vm, SQInteger idx) +{ + // Remember the current stack size + const StackGuard sg(vm); + // Attempt to retrieve the type name of the specified value + if (SQ_FAILED(sq_typeof(vm, idx))) + { + return _SC("unknown"); + } + // Attempt to convert the obtained value to a string + StackStrF val(vm, -1, false); + // Did the conversion failed? + if (SQ_FAILED(val.mRes)) + { + return _SC("unknown"); + } + // Return the obtained string value + return String(val.mPtr, val.mLen); +} + // ------------------------------------------------------------------------------------------------ Object BufferToStrObj(const Buffer & b) { diff --git a/shared/Base/Utility.hpp b/shared/Base/Utility.hpp index 226102a0..cad273a3 100644 --- a/shared/Base/Utility.hpp +++ b/shared/Base/Utility.hpp @@ -1399,6 +1399,16 @@ struct StackStrF StackStrF & operator = (StackStrF && o) = delete; }; +/* ------------------------------------------------------------------------------------------------ + * Netrieve the string representation of a certain type. +*/ +CSStr SqTypeName(SQObjectType type); + +/* ------------------------------------------------------------------------------------------------ + * Netrieve the string representation of a certain type from a value on the stack. +*/ +String SqTypeName(HSQUIRRELVM vm, SQInteger idx); + /* ------------------------------------------------------------------------------------------------ * Create a script object from the specified value on the default VM. */