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

Add sq_arrayreserve extension to squirrel API.

This commit is contained in:
Sandu Liviu Catalin 2021-01-30 19:39:44 +02:00
parent 4a6bfc086c
commit 1c84c7f35a
2 changed files with 31 additions and 0 deletions

View File

@ -15,6 +15,7 @@ SQUIRREL_API SQRESULT sq_throwerrorf(HSQUIRRELVM v,const SQChar *err,...);
SQUIRREL_API SQRESULT sq_pushstringf(HSQUIRRELVM v,const SQChar *s,...);
SQUIRREL_API SQRESULT sq_vpushstringf(HSQUIRRELVM v,const SQChar *s,va_list l);
SQUIRREL_API SQRESULT sq_getnativeclosurepointer(HSQUIRRELVM v,SQInteger idx,SQFUNCTION *f);
SQUIRREL_API SQRESULT sq_arrayreserve(HSQUIRRELVM v,SQInteger idx,SQInteger newcap);
#ifdef __cplusplus
} /*extern "C"*/

View File

@ -16,6 +16,24 @@
#include <squirrelex.h>
#include <stdarg.h>
#define _GETSAFE_OBJ(v,idx,type,o) { if(!sq_aux_gettypedarg(v,idx,type,&o)) return SQ_ERROR; }
#define sq_aux_paramscheck(v,count) \
{ \
if(sq_gettop(v) < count){ v->Raise_Error(_SC("not enough params in the stack")); return SQ_ERROR; }\
}
static bool sq_aux_gettypedarg(HSQUIRRELVM v,SQInteger idx,SQObjectType type,SQObjectPtr **o)
{
*o = &stack_get(v,idx);
if(sq_type(**o) != type){
SQObjectPtr oval = v->PrintObjVal(**o);
v->Raise_Error(_SC("wrong argument type, expected '%s' got '%.50s'"),IdType2Name(type),_stringval(oval));
return false;
}
return true;
}
SQRESULT sq_throwerrorf(HSQUIRRELVM v,const SQChar *err,...)
{
SQInteger n=256;
@ -89,3 +107,15 @@ SQRESULT sq_getnativeclosurepointer(HSQUIRRELVM v,SQInteger idx,SQFUNCTION *f)
}
return sq_throwerror(v,_SC("the object is not a native closure"));
}
SQRESULT sq_arrayreserve(HSQUIRRELVM v,SQInteger idx,SQInteger newcap)
{
sq_aux_paramscheck(v,1);
SQObjectPtr *arr;
_GETSAFE_OBJ(v, idx, OT_ARRAY,arr);
if(newcap >= 0) {
_array(*arr)->Reserve(newcap);
return SQ_OK;
}
return sq_throwerror(v,_SC("negative capacity"));
}