sqrat  0.9
sqrat
 All Classes Functions Variables Enumerations Enumerator Pages
sqratVM.h
1 //
2 // wrapper for the Squirrel VM under Sqrat
3 //
4 
5 //
6 // Copyright (c) 2011 Alston Chen
7 //
8 // This software is provided 'as-is', without any express or implied
9 // warranty. In no event will the authors be held liable for any damages
10 // arising from the use of this software.
11 //
12 // Permission is granted to anyone to use this software for any purpose,
13 // including commercial applications, and to alter it and redistribute it
14 // freely, subject to the following restrictions:
15 //
16 // 1. The origin of this software must not be misrepresented; you must not
17 // claim that you wrote the original software. If you use this software
18 // in a product, an acknowledgment in the product documentation would be
19 // appreciated but is not required.
20 //
21 // 2. Altered source versions must be plainly marked as such, and must not be
22 // misrepresented as being the original software.
23 //
24 // 3. This notice may not be removed or altered from any source
25 // distribution.
26 //
27 
28 #if !defined(_SCRAT_VM_H_)
29 #define _SCRAT_VM_H_
30 
31 #include <squirrel.h>
32 #include <sqrat.h>
33 #include <map>
34 
35 #include <iostream>
36 #include <stdarg.h>
37 
38 #include <sqstdio.h>
39 #include <sqstdblob.h>
40 #include <sqstdmath.h>
41 #include <sqstdsystem.h>
42 #include <sqstdstring.h>
43 
44 namespace Sqrat
45 {
46 
47 #ifdef SQUNICODE
48 #define scvprintf vwprintf
49 #else
50 #define scvprintf vprintf
51 #endif
52 
56 class SqratVM
57 {
58 private:
59  //static std::map<HSQUIRRELVM, SqratVM*> ms_sqratVMs;
60 
61  HSQUIRRELVM m_vm;
62  Sqrat::RootTable* m_rootTable;
63  Sqrat::Script* m_script;
64  Sqrat::string m_lastErrorMsg;
65 
66  static void s_addVM(HSQUIRRELVM vm, SqratVM* sqratvm)
67  {
68  //TODO: use mutex to lock ms_sqratVMs
69  (*ms_sqratVMs()).insert(std::make_pair(vm, sqratvm));
70  }
71 
72  static void s_deleteVM(HSQUIRRELVM vm)
73  {
74  //TODO: use mutex to lock ms_sqratVMs
75  (*ms_sqratVMs()).erase(vm);
76  }
77 
78  static SqratVM* s_getVM(HSQUIRRELVM vm)
79  {
80  //TODO: use mutex to lock ms_sqratVMs
81  return (*ms_sqratVMs())[vm];
82  }
83 
84 
85 private:
86 
87  static std::map<HSQUIRRELVM, SqratVM*> *ms_sqratVMs()
88  {
89  static std::map<HSQUIRRELVM, SqratVM*> *ms = 0;
90  if (ms == 0)
91  ms = new std::map<HSQUIRRELVM, SqratVM*> ;
92  return ms;
93  }
94 
95  static void printFunc(HSQUIRRELVM v, const SQChar *s, ...)
96  {
97  va_list vl;
98  va_start(vl, s);
99  scvprintf(s, vl);
100  va_end(vl);
101  }
102 
103  static SQInteger runtimeErrorHandler(HSQUIRRELVM v)
104  {
105  const SQChar *sErr = 0;
106  if(sq_gettop(v) >= 1)
107  {
108  Sqrat::string& errStr = s_getVM(v)->m_lastErrorMsg;
109  if(SQ_SUCCEEDED(sq_getstring(v, 2, &sErr)))
110  {
111  //scprintf(_SC("RuntimeError: %s\n"), sErr);
112  //errStr = _SC("RuntimeError: ") + sErr;
113  errStr = sErr;
114  }
115  else
116  {
117  //scprintf(_SC("An Unknown RuntimeError Occured.\n"));
118  errStr = _SC("An Unknown RuntimeError Occured.");
119  }
120  }
121  return 0;
122  }
123 
124  static void compilerErrorHandler(HSQUIRRELVM v,
125  const SQChar* desc,
126  const SQChar* source,
127  SQInteger line,
128  SQInteger column)
129  {
130  //scprintf(_SC("%s(%d:%d): %s\n"), source, line, column, desc);
131  SQChar buf[512];
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;
135  }
136 
137 public:
143  {
147  };
148 
149  static const unsigned char LIB_IO = 0x01;
150  static const unsigned char LIB_BLOB = 0x02;
151  static const unsigned char LIB_MATH = 0x04;
152  static const unsigned char LIB_SYST = 0x08;
153  static const unsigned char LIB_STR = 0x10;
154  static const unsigned char LIB_ALL = LIB_IO | LIB_BLOB | LIB_MATH | LIB_SYST | LIB_STR;
155 
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))
166  , m_lastErrorMsg()
167  {
168  s_addVM(m_vm, this);
169  //register std libs
170  sq_pushroottable(m_vm);
171  if (libsToLoad & LIB_IO)
172  sqstd_register_iolib(m_vm);
173  if (libsToLoad & LIB_BLOB)
174  sqstd_register_bloblib(m_vm);
175  if (libsToLoad & LIB_MATH)
176  sqstd_register_mathlib(m_vm);
177  if (libsToLoad & LIB_SYST)
178  sqstd_register_systemlib(m_vm);
179  if (libsToLoad & LIB_STR)
180  sqstd_register_stringlib(m_vm);
181  sq_pop(m_vm, 1);
182  SetPrintFunc(printFunc, printFunc);
183  SetErrorHandler(runtimeErrorHandler, compilerErrorHandler);
184  }
185 
191  {
192  s_deleteVM(m_vm);
193  delete m_script;
194  delete m_rootTable;
195  sq_close(m_vm);
196  }
197 
204  HSQUIRRELVM GetVM()
205  {
206  return m_vm;
207  }
208 
216  {
217  return *m_rootTable;
218  }
219 
227  {
228  return *m_script;
229  }
230 
237  Sqrat::string GetLastErrorMsg()
238  {
239  return m_lastErrorMsg;
240  }
241 
248  void SetLastErrorMsg(const Sqrat::string& str)
249  {
250  m_lastErrorMsg = str;
251  }
252 
263  void SetPrintFunc(SQPRINTFUNCTION printFunc, SQPRINTFUNCTION errFunc)
264  {
265  sq_setprintfunc(m_vm, printFunc, errFunc);
266  }
267 
275  void SetErrorHandler(SQFUNCTION runErr, SQCOMPILERERROR comErr)
276  {
277  sq_newclosure(m_vm, runErr, 0);
278  sq_seterrorhandler(m_vm);
279  sq_setcompilererrorhandler(m_vm, comErr);
280  }
281 
290  ERROR_STATE DoString(const Sqrat::string& str)
291  {
292  Sqrat::string msg;
293  m_lastErrorMsg.clear();
294  if(!m_script->CompileString(str, msg))
295  {
296  if(m_lastErrorMsg.empty())
297  {
298  m_lastErrorMsg = msg;
299  }
300  return SQRAT_COMPILE_ERROR;
301  }
302  if(!m_script->Run(msg))
303  {
304  if(m_lastErrorMsg.empty())
305  {
306  m_lastErrorMsg = msg;
307  }
308  return SQRAT_RUNTIME_ERROR;
309  }
310  return SQRAT_NO_ERROR;
311  }
312 
321  ERROR_STATE DoFile(const Sqrat::string& file)
322  {
323  Sqrat::string msg;
324  m_lastErrorMsg.clear();
325  if(!m_script->CompileFile(file, msg))
326  {
327  if(m_lastErrorMsg.empty())
328  {
329  m_lastErrorMsg = msg;
330  }
331  return SQRAT_COMPILE_ERROR;
332  }
333  if(!m_script->Run(msg))
334  {
335  if(m_lastErrorMsg.empty())
336  {
337  m_lastErrorMsg = msg;
338  }
339  return SQRAT_RUNTIME_ERROR;
340  }
341  return SQRAT_NO_ERROR;
342  }
343 
344 };
345 
346 //std::map<HSQUIRRELVM, SqratVM*> SqratVM::ms_sqratVMs;
347 
348 }
349 
350 #endif
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