28 #if !defined(_SCRAT_VM_H_)
39 #include <sqstdblob.h>
40 #include <sqstdmath.h>
41 #include <sqstdsystem.h>
42 #include <sqstdstring.h>
48 #define scvprintf vwprintf
50 #define scvprintf vprintf
64 Sqrat::string m_lastErrorMsg;
66 static void s_addVM(HSQUIRRELVM vm,
SqratVM* sqratvm)
69 (*ms_sqratVMs()).insert(std::make_pair(vm, sqratvm));
72 static void s_deleteVM(HSQUIRRELVM vm)
75 (*ms_sqratVMs()).erase(vm);
78 static SqratVM* s_getVM(HSQUIRRELVM vm)
81 return (*ms_sqratVMs())[vm];
87 static std::map<HSQUIRRELVM, SqratVM*> *ms_sqratVMs()
89 static std::map<HSQUIRRELVM, SqratVM*> *ms = 0;
91 ms =
new std::map<HSQUIRRELVM, SqratVM*> ;
95 static void printFunc(HSQUIRRELVM v,
const SQChar *s, ...)
103 static SQInteger runtimeErrorHandler(HSQUIRRELVM v)
105 const SQChar *sErr = 0;
106 if(sq_gettop(v) >= 1)
108 Sqrat::string& errStr = s_getVM(v)->m_lastErrorMsg;
109 if(SQ_SUCCEEDED(sq_getstring(v, 2, &sErr)))
118 errStr = _SC(
"An Unknown RuntimeError Occured.");
124 static void compilerErrorHandler(HSQUIRRELVM v,
126 const SQChar* source,
132 scsprintf(buf, _SC(
"%s(%d:%d): %s"), source, (
int) line, (
int) column, desc);
133 buf[
sizeof(buf) - 1] = 0;
134 s_getVM(v)->m_lastErrorMsg = buf;
149 static const unsigned char LIB_IO = 0x01;
163 SqratVM(
int initialStackSize = 1024,
unsigned char libsToLoad =
LIB_ALL): m_vm(sq_open(initialStackSize))
164 , m_rootTable(new Sqrat::
RootTable(m_vm))
165 , m_script(new Sqrat::
Script(m_vm))
170 sq_pushroottable(m_vm);
172 sqstd_register_iolib(m_vm);
174 sqstd_register_bloblib(m_vm);
176 sqstd_register_mathlib(m_vm);
178 sqstd_register_systemlib(m_vm);
180 sqstd_register_stringlib(m_vm);
239 return m_lastErrorMsg;
250 m_lastErrorMsg = str;
265 sq_setprintfunc(m_vm, printFunc, errFunc);
277 sq_newclosure(m_vm, runErr, 0);
278 sq_seterrorhandler(m_vm);
279 sq_setcompilererrorhandler(m_vm, comErr);
293 m_lastErrorMsg.clear();
296 if(m_lastErrorMsg.empty())
298 m_lastErrorMsg = msg;
302 if(!m_script->
Run(msg))
304 if(m_lastErrorMsg.empty())
306 m_lastErrorMsg = msg;
324 m_lastErrorMsg.clear();
327 if(m_lastErrorMsg.empty())
329 m_lastErrorMsg = msg;
333 if(!m_script->
Run(msg))
335 if(m_lastErrorMsg.empty())
337 m_lastErrorMsg = msg;
ERROR_STATE
Definition: sqratVM.h:142
static const unsigned char LIB_STR
String library.
Definition: sqratVM.h:153
Sqrat::string GetLastErrorMsg()
Definition: sqratVM.h:237
Sqrat::RootTable & GetRootTable()
Definition: sqratVM.h:215
static const unsigned char LIB_MATH
Math library.
Definition: sqratVM.h:151
void SetErrorHandler(SQFUNCTION runErr, SQCOMPILERERROR comErr)
Definition: sqratVM.h:275
HSQUIRRELVM GetVM()
Definition: sqratVM.h:204
void SetPrintFunc(SQPRINTFUNCTION printFunc, SQPRINTFUNCTION errFunc)
Definition: sqratVM.h:263
void CompileFile(const string &path)
Definition: sqratScript.h:117
Helper class that wraps a Squirrel virtual machine in a C++ API.
Definition: sqratVM.h:56
Definition: sqratTable.h:407
~SqratVM()
Definition: sqratVM.h:190
SqratVM(int initialStackSize=1024, unsigned char libsToLoad=LIB_ALL)
Definition: sqratVM.h:163
static const unsigned char LIB_BLOB
Blob library.
Definition: sqratVM.h:150
void Run()
Definition: sqratScript.h:170
static const unsigned char LIB_ALL
All libraries.
Definition: sqratVM.h:154
static const unsigned char LIB_IO
Input/Output library.
Definition: sqratVM.h:149
For when a script compiling error has occurred.
Definition: sqratVM.h:145
For when a script running error has occurred.
Definition: sqratVM.h:146
Helper class for managing Squirrel scripts.
Definition: sqratScript.h:42
ERROR_STATE DoString(const Sqrat::string &str)
Definition: sqratVM.h:290
Sqrat::Script & GetScript()
Definition: sqratVM.h:226
ERROR_STATE DoFile(const Sqrat::string &file)
Definition: sqratVM.h:321
For when no error has occurred.
Definition: sqratVM.h:144
void SetLastErrorMsg(const Sqrat::string &str)
Definition: sqratVM.h:248
static const unsigned char LIB_SYST
System library.
Definition: sqratVM.h:152
void CompileString(const string &script, const string &name=_SC(""))
Definition: sqratScript.h:62