Skip to content

Commit

Permalink
Squirrel: Use fmt instead of printf for Raise_Error
Browse files Browse the repository at this point in the history
  • Loading branch information
JGRennison committed Oct 6, 2024
1 parent 9596325 commit 18c51b0
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/3rdparty/squirrel/squirrel/sqapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ bool sq_aux_gettypedarg(HSQUIRRELVM v,SQInteger idx,SQObjectType type,SQObjectPt
*o = &stack_get(v,idx);
if(type(**o) != type){
SQObjectPtr oval = v->PrintObjVal(**o);
v->Raise_Error("wrong argument type, expected '%s' got '%.50s'",IdType2Name(type),_stringval(oval));
v->Raise_Error(fmt::format("wrong argument type, expected '{}' got '{:.50s}'",IdType2Name(type),_stringval(oval)));
return false;
}
return true;
Expand Down
3 changes: 2 additions & 1 deletion src/3rdparty/squirrel/squirrel/sqbaselib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

#include "../../../stdafx.h"
#include "../../../core/format.hpp"

#include "sqpcheader.h"
#include "sqvm.h"
Expand Down Expand Up @@ -213,7 +214,7 @@ static SQInteger base_array(HSQUIRRELVM v)
SQInteger nInitialSize = tointeger(stack_get(v,2));
SQInteger ret = 1;
if (nInitialSize < 0) {
v->Raise_Error("can't create/resize array with/to size " OTTD_PRINTF64, nInitialSize);
v->Raise_Error(fmt::format("can't create/resize array with/to size {}", nInitialSize));
nInitialSize = 0;
ret = -1;
}
Expand Down
17 changes: 5 additions & 12 deletions src/3rdparty/squirrel/squirrel/sqdebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,9 @@ SQRESULT sq_stackinfos(HSQUIRRELVM v, SQInteger level, SQStackInfos *si)
return SQ_ERROR;
}

void SQVM::Raise_Error(const SQChar *s, ...)
void SQVM::Raise_Error(std::string_view str)
{
va_list vl;
va_start(vl, s);
size_t len = strlen(s)+(NUMBER_MAX_CHAR*2);
char *buffer = MallocT<char>(len + 1);
vseprintf(buffer, buffer + len, s, vl);
va_end(vl);
_lasterror = SQString::Create(_ss(this),buffer,-1);
free(buffer);
_lasterror = SQString::Create(_ss(this),str.data(),str.size());
}

void SQVM::Raise_Error(SQObjectPtr &desc)
Expand Down Expand Up @@ -101,13 +94,13 @@ SQString *SQVM::PrintObjVal(const SQObject &o)
void SQVM::Raise_IdxError(const SQObject &o)
{
SQObjectPtr oval = PrintObjVal(o);
Raise_Error("the index '%.50s' does not exist", _stringval(oval));
Raise_Error(fmt::format("the index '{:.50s}' does not exist", _stringval(oval)));
}

void SQVM::Raise_CompareError(const SQObject &o1, const SQObject &o2)
{
SQObjectPtr oval1 = PrintObjVal(o1), oval2 = PrintObjVal(o2);
Raise_Error("comparison between '%.50s' and '%.50s'", _stringval(oval1), _stringval(oval2));
Raise_Error(fmt::format("comparison between '{:.50s}' and '{:.50s}'", _stringval(oval1), _stringval(oval2)));
}


Expand All @@ -124,5 +117,5 @@ void SQVM::Raise_ParamTypeError(SQInteger nparam,SQInteger typemask,SQInteger ty
StringCat(exptypes,SQString::Create(_ss(this), IdType2Name((SQObjectType)mask), -1), exptypes);
}
}
Raise_Error("parameter " OTTD_PRINTF64 " has an invalid type '%s' ; expected: '%s'", nparam, IdType2Name((SQObjectType)type), _stringval(exptypes));
Raise_Error(fmt::format("parameter {} has an invalid type '{}' ; expected: '{}'", nparam, IdType2Name((SQObjectType)type), _stringval(exptypes)));
}
5 changes: 3 additions & 2 deletions src/3rdparty/squirrel/squirrel/sqobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

#include "../../../stdafx.h"
#include "../../../core/format.hpp"

#include "sqpcheader.h"
#include "sqvm.h"
Expand Down Expand Up @@ -283,7 +284,7 @@ bool WriteObject(HSQUIRRELVM v,SQUserPointer up,SQWRITEFUNC write,SQObjectPtr &o
case OT_NULL:
break;
default:
v->Raise_Error("cannot serialize a %s",GetTypeName(o));
v->Raise_Error(fmt::format("cannot serialize a {}",GetTypeName(o)));
return false;
}
return true;
Expand Down Expand Up @@ -313,7 +314,7 @@ bool ReadObject(HSQUIRRELVM v,SQUserPointer up,SQREADFUNC read,SQObjectPtr &o)
o=_null_;
break;
default:
v->Raise_Error("cannot serialize a %s",IdType2Name(t));
v->Raise_Error(fmt::format("cannot serialize a {}",IdType2Name(t)));
return false;
}
return true;
Expand Down
46 changes: 23 additions & 23 deletions src/3rdparty/squirrel/squirrel/sqvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ bool SQVM::BW_OP(SQUnsignedInteger op,SQObjectPtr &trg,const SQObjectPtr &o1,con
default: { Raise_Error("internal vm error bitwise op failed"); return false; }
}
}
else { Raise_Error("bitwise op between '%s' and '%s'",GetTypeName(o1),GetTypeName(o2)); return false;}
else { Raise_Error(fmt::format("bitwise op between '{}' and '{}'",GetTypeName(o1),GetTypeName(o2))); return false;}
trg = res;
return true;
}
Expand Down Expand Up @@ -94,7 +94,7 @@ bool SQVM::ARITH_OP(SQUnsignedInteger op,SQObjectPtr &trg,const SQObjectPtr &o1,
if(!StringCat(o1, o2, trg)) return false;
}
else if(!ArithMetaMethod(op,o1,o2,trg)) {
Raise_Error("arith op %c on between '%s' and '%s'",(char)op,GetTypeName(o1),GetTypeName(o2)); return false;
Raise_Error(fmt::format("arith op {} on between '{}' and '{}'",(char)op,GetTypeName(o1),GetTypeName(o2))); return false;
}
}
return true;
Expand Down Expand Up @@ -185,7 +185,7 @@ bool SQVM::NEG_OP(SQObjectPtr &trg,const SQObjectPtr &o)
}
default:break; //shutup compiler
}
Raise_Error("attempt to negate a %s", GetTypeName(o));
Raise_Error(fmt::format("attempt to negate a {}", GetTypeName(o)));
return false;
}

Expand Down Expand Up @@ -542,14 +542,14 @@ bool SQVM::FOREACH_OP(SQObjectPtr &o1,SQObjectPtr &o2,SQObjectPtr
}
[[fallthrough]];
default:
Raise_Error("cannot iterate %s", GetTypeName(o1));
Raise_Error(fmt::format("cannot iterate {}", GetTypeName(o1)));
}
return false; //cannot be hit(just to avoid warnings)
}

bool SQVM::DELEGATE_OP(SQObjectPtr &trg,SQObjectPtr &o1,SQObjectPtr &o2)
{
if(type(o1) != OT_TABLE) { Raise_Error("delegating a '%s'", GetTypeName(o1)); return false; }
if(type(o1) != OT_TABLE) { Raise_Error(fmt::format("delegating a '{}'", GetTypeName(o1))); return false; }
switch(type(o2)) {
case OT_TABLE:
if(!_table(o1)->SetDelegate(_table(o2))){
Expand All @@ -561,7 +561,7 @@ bool SQVM::DELEGATE_OP(SQObjectPtr &trg,SQObjectPtr &o1,SQObjectPtr &o2)
_table(o1)->SetDelegate(nullptr);
break;
default:
Raise_Error("using '%s' as delegate", GetTypeName(o2));
Raise_Error(fmt::format("using '{}' as delegate", GetTypeName(o2)));
return false;
break;
}
Expand Down Expand Up @@ -617,7 +617,7 @@ bool SQVM::GETVARGV_OP(SQObjectPtr &target,SQObjectPtr &index,CallInfo *ci)
return false;
}
if(!sq_isnumeric(index)){
Raise_Error("indexing 'vargv' with %s",GetTypeName(index));
Raise_Error(fmt::format("indexing 'vargv' with {}",GetTypeName(index)));
return false;
}
SQInteger idx = tointeger(index);
Expand All @@ -631,7 +631,7 @@ bool SQVM::CLASS_OP(SQObjectPtr &target,SQInteger baseclass,SQInteger attributes
SQClass *base = nullptr;
SQObjectPtr attrs;
if(baseclass != -1) {
if(type(_stack._vals[_stackbase+baseclass]) != OT_CLASS) { Raise_Error("trying to inherit from a %s",GetTypeName(_stack._vals[_stackbase+baseclass])); return false; }
if(type(_stack._vals[_stackbase+baseclass]) != OT_CLASS) { Raise_Error(fmt::format("trying to inherit from a {}",GetTypeName(_stack._vals[_stackbase+baseclass]))); return false; }
base = _class(_stack._vals[_stackbase + baseclass]);
}
if(attributes != MAX_FUNC_STACKSIZE) {
Expand Down Expand Up @@ -686,7 +686,7 @@ bool SQVM::GETPARENT_OP(SQObjectPtr &o,SQObjectPtr &target)
case OT_CLASS: target = _class(o)->_base?_class(o)->_base:_null_;
break;
default:
Raise_Error("the %s type doesn't have a parent slot", GetTypeName(o));
Raise_Error(fmt::format("the {} type doesn't have a parent slot", GetTypeName(o)));
return false;
}
return true;
Expand Down Expand Up @@ -746,7 +746,7 @@ bool SQVM::Execute(SQObjectPtr &closure, SQInteger target, SQInteger nargs, SQIn
DecreaseOps(1);
if (ShouldSuspend()) { _suspended = SQTrue; _suspended_traps = traps; return true; }
if (IsOpsTillSuspendError()) {
Raise_Error("excessive CPU usage in %s", _ops_till_suspend_error_label);
Raise_Error(fmt::format("excessive CPU usage in {}", _ops_till_suspend_error_label));
SQ_THROW();
}

Expand Down Expand Up @@ -845,11 +845,11 @@ bool SQVM::Execute(SQObjectPtr &closure, SQInteger target, SQInteger nargs, SQIn
STK(ct_target) = clo;
break;
}
Raise_Error("attempt to call '%s'", GetTypeName(clo));
Raise_Error(fmt::format("attempt to call '{}'", GetTypeName(clo)));
SQ_THROW();
}
default:
Raise_Error("attempt to call '%s'", GetTypeName(clo));
Raise_Error(fmt::format("attempt to call '{}'", GetTypeName(clo)));
SQ_THROW();
}
}
Expand Down Expand Up @@ -951,7 +951,7 @@ bool SQVM::Execute(SQObjectPtr &closure, SQInteger target, SQInteger nargs, SQIn
case _OP_EXISTS: TARGET = Get(STK(arg1), STK(arg2), temp_reg, true,false)?_true_:_false_;continue;
case _OP_INSTANCEOF:
if(type(STK(arg1)) != OT_CLASS || type(STK(arg2)) != OT_INSTANCE)
{Raise_Error("cannot apply instanceof between a %s and a %s",GetTypeName(STK(arg1)),GetTypeName(STK(arg2))); SQ_THROW();}
{Raise_Error(fmt::format("cannot apply instanceof between a {} and a {}",GetTypeName(STK(arg1)),GetTypeName(STK(arg2)))); SQ_THROW();}
TARGET = _instance(STK(arg2))->InstanceOf(_class(STK(arg1)))?_true_:_false_;
continue;
case _OP_AND:
Expand All @@ -974,7 +974,7 @@ bool SQVM::Execute(SQObjectPtr &closure, SQInteger target, SQInteger nargs, SQIn
TARGET = SQInteger(~t);
continue;
}
Raise_Error("attempt to perform a bitwise op on a %s", GetTypeName(STK(arg1)));
Raise_Error(fmt::format("attempt to perform a bitwise op on a {}", GetTypeName(STK(arg1))));
SQ_THROW();
case _OP_CLOSURE: {
SQClosure *c = ci->_closure._unVal.pClosure;
Expand All @@ -989,7 +989,7 @@ bool SQVM::Execute(SQObjectPtr &closure, SQInteger target, SQInteger nargs, SQIn
traps -= ci->_etraps;
if(sarg1 != MAX_FUNC_STACKSIZE) STK(arg1) = temp_reg;
}
else { Raise_Error("trying to yield a '%s',only genenerator can be yielded", GetTypeName(ci->_closure)); SQ_THROW();}
else { Raise_Error(fmt::format("trying to yield a '{}',only genenerator can be yielded", GetTypeName(ci->_closure))); SQ_THROW();}
if(Return(arg0, arg1, temp_reg)){
assert(traps == 0);
outres = temp_reg;
Expand All @@ -999,7 +999,7 @@ bool SQVM::Execute(SQObjectPtr &closure, SQInteger target, SQInteger nargs, SQIn
}
continue;
case _OP_RESUME:
if(type(STK(arg1)) != OT_GENERATOR){ Raise_Error("trying to resume a '%s',only genenerator can be resumed", GetTypeName(STK(arg1))); SQ_THROW();}
if(type(STK(arg1)) != OT_GENERATOR){ Raise_Error(fmt::format("trying to resume a '{}',only genenerator can be resumed", GetTypeName(STK(arg1)))); SQ_THROW();}
_GUARD(_generator(STK(arg1))->Resume(this, arg0));
traps += ci->_etraps;
continue;
Expand All @@ -1015,7 +1015,7 @@ bool SQVM::Execute(SQObjectPtr &closure, SQInteger target, SQInteger nargs, SQIn
case _OP_DELEGATE: _GUARD(DELEGATE_OP(TARGET,STK(arg1),STK(arg2))); continue;
case _OP_CLONE:
if(!Clone(STK(arg1), TARGET))
{ Raise_Error("cloning a %s", GetTypeName(STK(arg1))); SQ_THROW();}
{ Raise_Error(fmt::format("cloning a {}", GetTypeName(STK(arg1)))); SQ_THROW();}
continue;
case _OP_TYPEOF: TypeOf(STK(arg1), TARGET); continue;
case _OP_PUSHTRAP:{
Expand Down Expand Up @@ -1354,10 +1354,10 @@ bool SQVM::Set(const SQObjectPtr &self,const SQObjectPtr &key,const SQObjectPtr
}
break;
case OT_ARRAY:
if(!sq_isnumeric(key)) {Raise_Error("indexing %s with %s",GetTypeName(self),GetTypeName(key)); return false; }
if(!sq_isnumeric(key)) {Raise_Error(fmt::format("indexing {} with {}",GetTypeName(self),GetTypeName(key))); return false; }
return _array(self)->Set(tointeger(key),val);
default:
Raise_Error("trying to set '%s'",GetTypeName(self));
Raise_Error(fmt::format("trying to set '{}'",GetTypeName(self)));
return false;
}
if(fetchroot) {
Expand Down Expand Up @@ -1426,13 +1426,13 @@ bool SQVM::NewSlot(const SQObjectPtr &self,const SQObjectPtr &key,const SQObject
}
else {
SQObjectPtr oval = PrintObjVal(key);
Raise_Error("the property '%s' already exists",_stringval(oval));
Raise_Error(fmt::format("the property '{}' already exists",_stringval(oval)));
return false;
}
}
break;
default:
Raise_Error("indexing %s with %s",GetTypeName(self),GetTypeName(key));
Raise_Error(fmt::format("indexing {} with {}",GetTypeName(self),GetTypeName(key)));
return false;
break;
}
Expand Down Expand Up @@ -1463,15 +1463,15 @@ bool SQVM::DeleteSlot(const SQObjectPtr &self,const SQObjectPtr &key,SQObjectPtr
}
}
else {
Raise_Error("cannot delete a slot from %s",GetTypeName(self));
Raise_Error(fmt::format("cannot delete a slot from {}",GetTypeName(self)));
return false;
}
}
res = t;
}
break;
default:
Raise_Error("attempt to delete a slot from a %s",GetTypeName(self));
Raise_Error(fmt::format("attempt to delete a slot from a {}",GetTypeName(self)));
return false;
}
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/3rdparty/squirrel/squirrel/sqvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ typedef sqvector<CallInfo> CallInfoVec;
SQString *PrintObjVal(const SQObject &o);


void Raise_Error(const SQChar *s, ...) WARN_FORMAT(2, 3);
void Raise_Error(std::string_view str);
void Raise_Error(SQObjectPtr &desc);
void Raise_IdxError(const SQObject &o);
void Raise_CompareError(const SQObject &o1, const SQObject &o2);
Expand Down

0 comments on commit 18c51b0

Please sign in to comment.