Changeset - r28648:68b32971fab7
[Not reviewed]
master
0 6 0
frosch - 3 months ago 2024-02-02 20:30:40
frosch@openttd.org
Codechange: Simplify error throwing/catching in squirrel compiler.
6 files changed with 17 insertions and 35 deletions:
0 comments (0 inline, 0 general)
src/3rdparty/squirrel/squirrel/sqcompiler.cpp
Show inline comments
 
@@ -57,19 +57,15 @@ typedef sqvector<ExpState> ExpStateVec;
 
class SQCompiler
 
{
 
public:
 
	SQCompiler(SQVM *v, SQLEXREADFUNC rg, SQUserPointer up, const SQChar* sourcename, bool raiseerror, bool lineinfo) : _token(0), _fs(nullptr), _lex(_ss(v), rg, up, ThrowError, this), _debugline(0), _debugop(0)
 
	SQCompiler(SQVM *v, SQLEXREADFUNC rg, SQUserPointer up, const SQChar* sourcename, bool raiseerror, bool lineinfo) : _token(0), _fs(nullptr), _lex(_ss(v), rg, up), _debugline(0), _debugop(0)
 
	{
 
		_vm=v;
 
		_sourcename = SQString::Create(_ss(v), sourcename);
 
		_lineinfo = lineinfo;_raiseerror = raiseerror;
 
	}
 
	NORETURN static void ThrowError(void *ud, const SQChar *s) {
 
		SQCompiler *c = (SQCompiler *)ud;
 
		c->Error(s);
 
	}
 
	NORETURN void Error(const std::string &msg)
 
	[[noreturn]] void Error(const std::string &msg)
 
	{
 
		throw msg;
 
		throw CompileException(msg);
 
	}
 
	void Lex(){	_token = _lex.Lex();}
 
	void PushExpState(){ _expstates.push_back(ExpState()); }
 
@@ -159,7 +155,7 @@ public:
 
		_debugline = 1;
 
		_debugop = 0;
 

	
 
		SQFuncState funcstate(_ss(_vm), nullptr,ThrowError,this);
 
		SQFuncState funcstate(_ss(_vm), nullptr);
 
		funcstate._name = SQString::Create(_ss(_vm), "main");
 
		_fs = &funcstate;
 
		_fs->AddParameter(_fs->CreateString("this"));
 
@@ -181,12 +177,12 @@ public:
 
#endif
 
			return true;
 
		}
 
		catch (const std::string &compilererror) {
 
		catch (const CompileException &compilererror) {
 
			if(_raiseerror && _ss(_vm)->_compilererrorhandler) {
 
				_ss(_vm)->_compilererrorhandler(_vm, compilererror.c_str(), type(_sourcename) == OT_STRING ? _stringval(_sourcename) : "unknown",
 
				_ss(_vm)->_compilererrorhandler(_vm, compilererror.what(), type(_sourcename) == OT_STRING ? _stringval(_sourcename) : "unknown",
 
					_lex._currentline, _lex._currentcolumn);
 
			}
 
			_vm->_lasterror = SQString::Create(_ss(_vm), compilererror);
 
			_vm->_lasterror = SQString::Create(_ss(_vm), compilererror.what());
 
			return false;
 
		}
 
	}
src/3rdparty/squirrel/squirrel/sqcompiler.h
Show inline comments
 
@@ -71,12 +71,7 @@ struct SQVM;
 
#define TK_ENUM 323
 
#define TK_CONST 324
 

	
 
/* MSVC doesn't like NORETURN for function prototypes, but we kinda need it for GCC. */
 
#if defined(_MSC_VER) && !defined(__clang__)
 
typedef void(*CompilerErrorFunc)(void *ud, const SQChar *s);
 
#else
 
typedef NORETURN void(*CompilerErrorFunc)(void *ud, const SQChar *s);
 
#endif
 
using CompileException = std::runtime_error;
 

	
 
bool Compile(SQVM *vm, SQLEXREADFUNC rg, SQUserPointer up, const SQChar *sourcename, SQObjectPtr &out, bool raiseerror, bool lineinfo);
 
#endif //_SQCOMPILER_H_
src/3rdparty/squirrel/squirrel/sqfuncstate.cpp
Show inline comments
 
@@ -93,7 +93,7 @@ void DumpLiteral(SQObjectPtr &o)
 
}
 
#endif
 

	
 
SQFuncState::SQFuncState(SQSharedState *ss,SQFuncState *parent,CompilerErrorFunc efunc,void *ed)
 
SQFuncState::SQFuncState(SQSharedState *ss,SQFuncState *parent)
 
{
 
		_nliterals = 0;
 
		_literals = SQTable::Create(ss,0);
 
@@ -106,15 +106,13 @@ SQFuncState::SQFuncState(SQSharedState *
 
		_traps = 0;
 
		_returnexp = 0;
 
		_varparams = false;
 
		_errfunc = efunc;
 
		_errtarget = ed;
 
		_bgenerator = false;
 

	
 
}
 

	
 
void SQFuncState::Error(const SQChar *err)
 
{
 
	_errfunc(_errtarget,err);
 
	throw CompileException(err);
 
}
 

	
 
#ifdef _DEBUG_DUMP
 
@@ -550,7 +548,7 @@ SQFunctionProto *SQFuncState::BuildProto
 
SQFuncState *SQFuncState::PushChildState(SQSharedState *ss)
 
{
 
	SQFuncState *child = (SQFuncState *)sq_malloc(sizeof(SQFuncState));
 
	new (child) SQFuncState(ss,this,_errfunc,_errtarget);
 
	new (child) SQFuncState(ss,this);
 
	_childstates.push_back(child);
 
	return child;
 
}
src/3rdparty/squirrel/squirrel/sqfuncstate.h
Show inline comments
 
@@ -6,12 +6,12 @@
 

	
 
struct SQFuncState
 
{
 
	SQFuncState(SQSharedState *ss,SQFuncState *parent,CompilerErrorFunc efunc,void *ed);
 
	SQFuncState(SQSharedState *ss,SQFuncState *parent);
 
	~SQFuncState();
 
#ifdef _DEBUG_DUMP
 
	void Dump(SQFunctionProto *func);
 
#endif
 
	void Error(const SQChar *err);
 
	[[noreturn]] void Error(const SQChar *err);
 
	SQFuncState *PushChildState(SQSharedState *ss);
 
	void PopChildState();
 
	void AddInstruction(SQOpcode _op,SQInteger arg0=0,SQInteger arg1=0,SQInteger arg2=0,SQInteger arg3=0){SQInstruction i(_op,arg0,arg1,arg2,arg3);AddInstruction(i);}
 
@@ -75,9 +75,6 @@ struct SQFuncState
 
	SQSharedState *_sharedstate;
 
	sqvector<SQFuncState*> _childstates;
 
	SQInteger GetConstant(const SQObject &cons);
 
private:
 
	CompilerErrorFunc _errfunc;
 
	void *_errtarget;
 
};
 

	
 

	
src/3rdparty/squirrel/squirrel/sqlexer.cpp
Show inline comments
 
@@ -35,10 +35,8 @@ void SQLexer::APPEND_CHAR(char32_t c)
 
	}
 
}
 

	
 
SQLexer::SQLexer(SQSharedState *ss, SQLEXREADFUNC rg, SQUserPointer up,CompilerErrorFunc efunc,void *ed)
 
SQLexer::SQLexer(SQSharedState *ss, SQLEXREADFUNC rg, SQUserPointer up)
 
{
 
	_errfunc = efunc;
 
	_errtarget = ed;
 
	_sharedstate = ss;
 
	_keywords = SQTable::Create(ss, 26);
 
	ADD_KEYWORD(while, TK_WHILE);
 
@@ -96,7 +94,7 @@ SQLexer::SQLexer(SQSharedState *ss, SQLE
 

	
 
NORETURN void SQLexer::Error(const SQChar *err)
 
{
 
	_errfunc(_errtarget,err);
 
	throw CompileException(err);
 
}
 

	
 
void SQLexer::Next()
src/3rdparty/squirrel/squirrel/sqlexer.h
Show inline comments
 
@@ -5,8 +5,8 @@
 
struct SQLexer
 
{
 
	~SQLexer();
 
	SQLexer(SQSharedState *ss,SQLEXREADFUNC rg,SQUserPointer up,CompilerErrorFunc efunc,void *ed);
 
	NORETURN void Error(const SQChar *err);
 
	SQLexer(SQSharedState *ss,SQLEXREADFUNC rg,SQUserPointer up);
 
	[[noreturn]] void Error(const SQChar *err);
 
	SQInteger Lex();
 
	const SQChar *Tok2Str(SQInteger tok);
 
private:
 
@@ -35,8 +35,6 @@ public:
 
	char32_t _currdata;
 
	SQSharedState *_sharedstate;
 
	sqvector<SQChar> _longstr;
 
	CompilerErrorFunc _errfunc;
 
	void *_errtarget;
 
};
 

	
 
#endif
0 comments (0 inline, 0 general)