1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 00:37:15 +01:00

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.
This commit is contained in:
Sandu Liviu Catalin 2016-06-09 02:05:36 +03:00
parent 94fb58cc5d
commit f1d8d60e96
2 changed files with 60 additions and 2 deletions

View File

@ -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)
{

View File

@ -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.
*/