From 1c84c7f35ade766142993d7e9b75047790e4ed71 Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Sat, 30 Jan 2021 19:39:44 +0200 Subject: [PATCH] Add sq_arrayreserve extension to squirrel API. --- vendor/Squirrel/include/squirrelex.h | 1 + vendor/Squirrel/sqapiex.cpp | 30 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/vendor/Squirrel/include/squirrelex.h b/vendor/Squirrel/include/squirrelex.h index 7f631206..b332889b 100644 --- a/vendor/Squirrel/include/squirrelex.h +++ b/vendor/Squirrel/include/squirrelex.h @@ -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"*/ diff --git a/vendor/Squirrel/sqapiex.cpp b/vendor/Squirrel/sqapiex.cpp index 2c14644a..6a4f85cc 100644 --- a/vendor/Squirrel/sqapiex.cpp +++ b/vendor/Squirrel/sqapiex.cpp @@ -16,6 +16,24 @@ #include #include +#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")); +}