1
0
Fork 0

Codechange: use string_view for squirrel parameter checks

pull/14211/head
Rubidium 2025-05-03 22:40:56 +02:00 committed by rubidium42
parent 588b0de3de
commit 1f411f8a16
7 changed files with 41 additions and 45 deletions

View File

@ -185,7 +185,7 @@ typedef struct tagSQRegFunction{
std::string_view name; std::string_view name;
SQFUNCTION f; SQFUNCTION f;
SQInteger nparamscheck; SQInteger nparamscheck;
const SQChar *typemask; std::optional<std::string_view> typemask;
}SQRegFunction; }SQRegFunction;
typedef struct tagSQFunctionInfo { typedef struct tagSQFunctionInfo {
@ -235,7 +235,7 @@ SQUserPointer sq_newuserdata(HSQUIRRELVM v,SQUnsignedInteger size);
void sq_newtable(HSQUIRRELVM v); void sq_newtable(HSQUIRRELVM v);
void sq_newarray(HSQUIRRELVM v,SQInteger size); void sq_newarray(HSQUIRRELVM v,SQInteger size);
void sq_newclosure(HSQUIRRELVM v,SQFUNCTION func,SQUnsignedInteger nfreevars); void sq_newclosure(HSQUIRRELVM v,SQFUNCTION func,SQUnsignedInteger nfreevars);
SQRESULT sq_setparamscheck(HSQUIRRELVM v,SQInteger nparamscheck,const SQChar *typemask); SQRESULT sq_setparamscheck(HSQUIRRELVM v,SQInteger nparamscheck,std::optional<std::string_view> typemask);
SQRESULT sq_bindenv(HSQUIRRELVM v,SQInteger idx); SQRESULT sq_bindenv(HSQUIRRELVM v,SQInteger idx);
void sq_pushstring(HSQUIRRELVM v, std::string_view str); void sq_pushstring(HSQUIRRELVM v, std::string_view str);
void sq_pushfloat(HSQUIRRELVM v,SQFloat f); void sq_pushfloat(HSQUIRRELVM v,SQFloat f);

View File

@ -84,7 +84,7 @@ static const std::initializer_list<SQRegFunction> mathlib_funcs = {
_DECL_FUNC(exp,2,".n"), _DECL_FUNC(exp,2,".n"),
#ifdef EXPORT_DEFAULT_SQUIRREL_FUNCTIONS #ifdef EXPORT_DEFAULT_SQUIRREL_FUNCTIONS
_DECL_FUNC(srand,2,".n"), _DECL_FUNC(srand,2,".n"),
_DECL_FUNC(rand,1,nullptr), _DECL_FUNC(rand,1,std::nullopt),
#endif /* EXPORT_DEFAULT_SQUIRREL_FUNCTIONS */ #endif /* EXPORT_DEFAULT_SQUIRREL_FUNCTIONS */
_DECL_FUNC(fabs,2,".n"), _DECL_FUNC(fabs,2,".n"),
_DECL_FUNC(abs,2,".n"), _DECL_FUNC(abs,2,".n"),

View File

@ -385,16 +385,16 @@ SQRESULT sq_setnativeclosurename(HSQUIRRELVM v,SQInteger idx,std::string_view na
return sq_throwerror(v,"the object is not a nativeclosure"); return sq_throwerror(v,"the object is not a nativeclosure");
} }
SQRESULT sq_setparamscheck(HSQUIRRELVM v,SQInteger nparamscheck,const SQChar *typemask) SQRESULT sq_setparamscheck(HSQUIRRELVM v,SQInteger nparamscheck,std::optional<std::string_view> typemask)
{ {
SQObject o = stack_get(v, -1); SQObject o = stack_get(v, -1);
if(!sq_isnativeclosure(o)) if(!sq_isnativeclosure(o))
return sq_throwerror(v, "native closure expected"); return sq_throwerror(v, "native closure expected");
SQNativeClosure *nc = _nativeclosure(o); SQNativeClosure *nc = _nativeclosure(o);
nc->_nparamscheck = nparamscheck; nc->_nparamscheck = nparamscheck;
if(typemask) { if(typemask.has_value()) {
SQIntVec res; SQIntVec res;
if(!CompileTypemask(res, typemask)) if(!CompileTypemask(res, *typemask))
return sq_throwerror(v, "invalid typemask"); return sq_throwerror(v, "invalid typemask");
nc->_typecheck.copy(res); nc->_typecheck.copy(res);
} }

View File

@ -241,26 +241,26 @@ static SQInteger base_type(HSQUIRRELVM v)
static const std::initializer_list<SQRegFunction> base_funcs={ static const std::initializer_list<SQRegFunction> base_funcs={
//generic //generic
#ifdef EXPORT_DEFAULT_SQUIRREL_FUNCTIONS #ifdef EXPORT_DEFAULT_SQUIRREL_FUNCTIONS
{"seterrorhandler",base_seterrorhandler,2, nullptr}, {"seterrorhandler",base_seterrorhandler,2, std::nullopt},
{"setdebughook",base_setdebughook,2, nullptr}, {"setdebughook",base_setdebughook,2, std::nullopt},
{"enabledebuginfo",base_enabledebuginfo,2, nullptr}, {"enabledebuginfo",base_enabledebuginfo,2, std::nullopt},
{"getstackinfos",base_getstackinfos,2, ".n"}, {"getstackinfos",base_getstackinfos,2, ".n"},
{"getroottable",base_getroottable,1, nullptr}, {"getroottable",base_getroottable,1, std::nullopt},
{"setroottable",base_setroottable,2, nullptr}, {"setroottable",base_setroottable,2, std::nullopt},
{"getconsttable",base_getconsttable,1, nullptr}, {"getconsttable",base_getconsttable,1, std::nullopt},
{"setconsttable",base_setconsttable,2, nullptr}, {"setconsttable",base_setconsttable,2, std::nullopt},
#endif #endif
{"assert",base_assert,2, nullptr}, {"assert",base_assert,2, std::nullopt},
{"print",base_print,2, nullptr}, {"print",base_print,2, std::nullopt},
#ifdef EXPORT_DEFAULT_SQUIRREL_FUNCTIONS #ifdef EXPORT_DEFAULT_SQUIRREL_FUNCTIONS
{"compilestring",base_compilestring,-2, ".ss"}, {"compilestring",base_compilestring,-2, ".ss"},
{"newthread",base_newthread,2, ".c"}, {"newthread",base_newthread,2, ".c"},
{"suspend",base_suspend,-1, nullptr}, {"suspend",base_suspend,-1, std::nullopt},
#endif #endif
{"array",base_array,-2, ".n"}, {"array",base_array,-2, ".n"},
{"type",base_type,2, nullptr}, {"type",base_type,2, std::nullopt},
#ifdef EXPORT_DEFAULT_SQUIRREL_FUNCTIONS #ifdef EXPORT_DEFAULT_SQUIRREL_FUNCTIONS
{"dummy",base_dummy,0,nullptr}, {"dummy",base_dummy,0,std::nullopt},
#ifndef NO_GARBAGE_COLLECTOR #ifndef NO_GARBAGE_COLLECTOR
{"collectgarbage",base_collectgarbage,1, "t"}, {"collectgarbage",base_collectgarbage,1, "t"},
#endif #endif
@ -414,7 +414,7 @@ static SQInteger table_rawget(HSQUIRRELVM v)
{"rawset",table_rawset,3, "t"}, {"rawset",table_rawset,3, "t"},
{"rawdelete",table_rawdelete,2, "t"}, {"rawdelete",table_rawdelete,2, "t"},
{"rawin",container_rawexists,2, "t"}, {"rawin",container_rawexists,2, "t"},
{"weakref",obj_delegate_weakref,1, nullptr }, {"weakref",obj_delegate_weakref,1, std::nullopt },
{"tostring",default_delegate_tostring,1, "."}, {"tostring",default_delegate_tostring,1, "."},
{"clear",obj_clear,1, "."}, {"clear",obj_clear,1, "."},
}; };
@ -622,7 +622,7 @@ static SQInteger array_slice(HSQUIRRELVM v)
{"reverse",array_reverse,1, "a"}, {"reverse",array_reverse,1, "a"},
{"sort",array_sort,-1, "ac"}, {"sort",array_sort,-1, "ac"},
{"slice",array_slice,-1, "ann"}, {"slice",array_slice,-1, "ann"},
{"weakref",obj_delegate_weakref,1, nullptr }, {"weakref",obj_delegate_weakref,1, std::nullopt },
{"tostring",default_delegate_tostring,1, "."}, {"tostring",default_delegate_tostring,1, "."},
{"clear",obj_clear,1, "."}, {"clear",obj_clear,1, "."},
}; };
@ -684,7 +684,7 @@ STRING_TOFUNCZ(toupper)
{"find",string_find,-2, "s s n "}, {"find",string_find,-2, "s s n "},
{"tolower",string_tolower,1, "s"}, {"tolower",string_tolower,1, "s"},
{"toupper",string_toupper,1, "s"}, {"toupper",string_toupper,1, "s"},
{"weakref",obj_delegate_weakref,1, nullptr }, {"weakref",obj_delegate_weakref,1, std::nullopt },
}; };
//INTEGER DEFAULT DELEGATE////////////////////////// //INTEGER DEFAULT DELEGATE//////////////////////////
@ -693,7 +693,7 @@ STRING_TOFUNCZ(toupper)
{"tofloat",default_delegate_tofloat,1, "n|b"}, {"tofloat",default_delegate_tofloat,1, "n|b"},
{"tostring",default_delegate_tostring,1, "."}, {"tostring",default_delegate_tostring,1, "."},
{"tochar",number_delegate_tochar,1, "n|b"}, {"tochar",number_delegate_tochar,1, "n|b"},
{"weakref",obj_delegate_weakref,1, nullptr }, {"weakref",obj_delegate_weakref,1, std::nullopt },
}; };
//CLOSURE DEFAULT DELEGATE////////////////////////// //CLOSURE DEFAULT DELEGATE//////////////////////////
@ -777,7 +777,7 @@ static SQInteger closure_getinfos(HSQUIRRELVM v) {
{"pcall",closure_pcall,-1, "c"}, {"pcall",closure_pcall,-1, "c"},
{"acall",closure_acall,2, "ca"}, {"acall",closure_acall,2, "ca"},
{"pacall",closure_pacall,2, "ca"}, {"pacall",closure_pacall,2, "ca"},
{"weakref",obj_delegate_weakref,1, nullptr }, {"weakref",obj_delegate_weakref,1, std::nullopt },
{"tostring",default_delegate_tostring,1, "."}, {"tostring",default_delegate_tostring,1, "."},
{"bindenv",closure_bindenv,2, "c x|y|t"}, {"bindenv",closure_bindenv,2, "c x|y|t"},
{"getinfos",closure_getinfos,1, "c"}, {"getinfos",closure_getinfos,1, "c"},
@ -797,7 +797,7 @@ static SQInteger generator_getstatus(HSQUIRRELVM v)
/* static */ const std::initializer_list<SQRegFunction> SQSharedState::_generator_default_delegate_funcz={ /* static */ const std::initializer_list<SQRegFunction> SQSharedState::_generator_default_delegate_funcz={
{"getstatus",generator_getstatus,1, "g"}, {"getstatus",generator_getstatus,1, "g"},
{"weakref",obj_delegate_weakref,1, nullptr }, {"weakref",obj_delegate_weakref,1, std::nullopt },
{"tostring",default_delegate_tostring,1, "."}, {"tostring",default_delegate_tostring,1, "."},
}; };
@ -882,7 +882,7 @@ static SQInteger thread_getstatus(HSQUIRRELVM v)
{"call", thread_call, -1, "v"}, {"call", thread_call, -1, "v"},
{"wakeup", thread_wakeup, -1, "v"}, {"wakeup", thread_wakeup, -1, "v"},
{"getstatus", thread_getstatus, 1, "v"}, {"getstatus", thread_getstatus, 1, "v"},
{"weakref",obj_delegate_weakref,1, nullptr }, {"weakref",obj_delegate_weakref,1, std::nullopt },
{"tostring",default_delegate_tostring,1, "."}, {"tostring",default_delegate_tostring,1, "."},
}; };
@ -911,7 +911,7 @@ static SQInteger class_instance(HSQUIRRELVM v)
{"getattributes", class_getattributes, 2, "y."}, {"getattributes", class_getattributes, 2, "y."},
{"setattributes", class_setattributes, 3, "y.."}, {"setattributes", class_setattributes, 3, "y.."},
{"rawin",container_rawexists,2, "y"}, {"rawin",container_rawexists,2, "y"},
{"weakref",obj_delegate_weakref,1, nullptr }, {"weakref",obj_delegate_weakref,1, std::nullopt },
{"tostring",default_delegate_tostring,1, "."}, {"tostring",default_delegate_tostring,1, "."},
{"instance",class_instance,1, "y"}, {"instance",class_instance,1, "y"},
}; };
@ -926,7 +926,7 @@ static SQInteger instance_getclass(HSQUIRRELVM v)
/* static */ const std::initializer_list<SQRegFunction> SQSharedState::_instance_default_delegate_funcz = { /* static */ const std::initializer_list<SQRegFunction> SQSharedState::_instance_default_delegate_funcz = {
{"getclass", instance_getclass, 1, "x"}, {"getclass", instance_getclass, 1, "x"},
{"rawin",container_rawexists,2, "x"}, {"rawin",container_rawexists,2, "x"},
{"weakref",obj_delegate_weakref,1, nullptr }, {"weakref",obj_delegate_weakref,1, std::nullopt },
{"tostring",default_delegate_tostring,1, "."}, {"tostring",default_delegate_tostring,1, "."},
}; };
@ -939,7 +939,7 @@ static SQInteger weakref_ref(HSQUIRRELVM v)
/* static */ const std::initializer_list<SQRegFunction> SQSharedState::_weakref_default_delegate_funcz = { /* static */ const std::initializer_list<SQRegFunction> SQSharedState::_weakref_default_delegate_funcz = {
{"ref",weakref_ref,1, "r"}, {"ref",weakref_ref,1, "r"},
{"weakref",obj_delegate_weakref,1, nullptr }, {"weakref",obj_delegate_weakref,1, std::nullopt },
{"tostring",default_delegate_tostring,1, "."}, {"tostring",default_delegate_tostring,1, "."},
}; };

View File

@ -14,6 +14,7 @@
#include "sqarray.h" #include "sqarray.h"
#include "squserdata.h" #include "squserdata.h"
#include "sqclass.h" #include "sqclass.h"
#include "../../../core/string_consumer.hpp"
#include "../../../safeguards.h" #include "../../../safeguards.h"
@ -32,14 +33,12 @@ SQObjectPtr _minusone_((SQInteger)-1);
_table(_metamethodsmap)->NewSlot(_metamethods->back(),(SQInteger)(_metamethods->size()-1)); \ _table(_metamethodsmap)->NewSlot(_metamethods->back(),(SQInteger)(_metamethods->size()-1)); \
} }
bool CompileTypemask(SQIntVec &res,const SQChar *typemask) bool CompileTypemask(SQIntVec &res,std::string_view typemask)
{ {
SQInteger i = 0;
SQInteger mask = 0; SQInteger mask = 0;
while(typemask[i] != 0) { StringConsumer consumer{typemask};
while (consumer.AnyBytesLeft()) {
switch(typemask[i]){ switch(consumer.ReadChar()){
case 'o': mask |= _RT_NULL; break; case 'o': mask |= _RT_NULL; break;
case 'i': mask |= _RT_INTEGER; break; case 'i': mask |= _RT_INTEGER; break;
case 'f': mask |= _RT_FLOAT; break; case 'f': mask |= _RT_FLOAT; break;
@ -56,21 +55,18 @@ bool CompileTypemask(SQIntVec &res,const SQChar *typemask)
case 'x': mask |= _RT_INSTANCE; break; case 'x': mask |= _RT_INSTANCE; break;
case 'y': mask |= _RT_CLASS; break; case 'y': mask |= _RT_CLASS; break;
case 'r': mask |= _RT_WEAKREF; break; case 'r': mask |= _RT_WEAKREF; break;
case '.': mask = -1; res.push_back(mask); i++; mask = 0; continue; case '.': mask = -1; res.push_back(mask); mask = 0; continue;
case ' ': i++; continue; //ignores spaces case ' ': continue; //ignores spaces
default: default:
return false; return false;
} }
i++;
if(typemask[i] == '|') { if(consumer.ReadCharIf('|')) {
i++; if(!consumer.AnyBytesLeft()) return false;
if(typemask[i] == 0)
return false;
continue; continue;
} }
res.push_back(mask); res.push_back(mask);
mask = 0; mask = 0;
} }
return true; return true;
} }
@ -82,7 +78,7 @@ SQTable *CreateDefaultDelegate(SQSharedState *ss,const std::initializer_list<SQR
SQNativeClosure *nc = SQNativeClosure::Create(ss,func.f); SQNativeClosure *nc = SQNativeClosure::Create(ss,func.f);
nc->_nparamscheck = func.nparamscheck; nc->_nparamscheck = func.nparamscheck;
nc->_name = SQString::Create(ss,func.name); nc->_name = SQString::Create(ss,func.name);
if(func.typemask && !CompileTypemask(nc->_typecheck,func.typemask)) if(func.typemask.has_value() && !CompileTypemask(nc->_typecheck,*func.typemask))
return nullptr; return nullptr;
t->NewSlot(SQString::Create(ss,func.name),nc); t->NewSlot(SQString::Create(ss,func.name),nc);
} }

View File

@ -129,6 +129,6 @@ extern SQObjectPtr _false_;
extern SQObjectPtr _one_; extern SQObjectPtr _one_;
extern SQObjectPtr _minusone_; extern SQObjectPtr _minusone_;
bool CompileTypemask(SQIntVec &res,const SQChar *typemask); bool CompileTypemask(SQIntVec &res,std::string_view typemask);
#endif //_SQSTATE_H_ #endif //_SQSTATE_H_

View File

@ -265,7 +265,7 @@ void Squirrel::AddMethod(std::string_view method_name, SQFUNCTION proc, std::str
} }
sq_newclosure(this->vm, proc, size != 0 ? 1 : 0); sq_newclosure(this->vm, proc, size != 0 ? 1 : 0);
if (!params.empty()) sq_setparamscheck(this->vm, params.size(), params.data()); if (!params.empty()) sq_setparamscheck(this->vm, params.size(), params);
sq_setnativeclosurename(this->vm, -1, method_name); sq_setnativeclosurename(this->vm, -1, method_name);
sq_newslot(this->vm, -3, SQFalse); sq_newslot(this->vm, -3, SQFalse);
} }