sqrat  0.9
sqrat
 All Classes Functions Variables Enumerations Enumerator Pages
sqratScript.h
1 //
2 // SqratScript: Script Compilation and Execution
3 //
4 
5 //
6 // Copyright (c) 2009 Brandon Jones
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_SCRIPT_H_)
29 #define _SCRAT_SCRIPT_H_
30 
31 #include <squirrel.h>
32 #include <sqstdio.h>
33 #include <string.h>
34 
35 #include "sqratObject.h"
36 
37 namespace Sqrat {
38 
42 class Script : public Object {
43 public:
50  Script(HSQUIRRELVM v = DefaultVM::Get()) : Object(v, true) {
51  }
52 
62  void CompileString(const string& script, const string& name = _SC("")) {
63  if(!sq_isnull(obj)) {
64  sq_release(vm, &obj);
65  sq_resetobject(&obj);
66  }
67 
68 #if !defined (SCRAT_NO_ERROR_CHECKING)
69  if(SQ_FAILED(sq_compilebuffer(vm, script.c_str(), static_cast<SQInteger>(script.size() ), name.c_str(), true))) {
70  Error::Instance().Throw(vm, LastErrorString(vm));
71  return;
72  }
73 #else
74  sq_compilebuffer(vm, script.c_str(), static_cast<SQInteger>(script.size() ), name.c_str(), true);
75 #endif
76  sq_getstackobj(vm,-1,&obj);
77  sq_addref(vm, &obj);
78  sq_pop(vm, 1);
79  }
80 
88  bool CompileString(const string& script, string& errMsg, const string& name = _SC("")) {
89  if(!sq_isnull(obj)) {
90  sq_release(vm, &obj);
91  sq_resetobject(&obj);
92  }
93 
94 #if !defined (SCRAT_NO_ERROR_CHECKING)
95  if(SQ_FAILED(sq_compilebuffer(vm, script.c_str(), static_cast<SQInteger>(script.size() ), name.c_str(), true))) {
96  errMsg = LastErrorString(vm);
97  return false;
98  }
99 #else
100  sq_compilebuffer(vm, script.c_str(), static_cast<SQInteger>(script.size() ), name.c_str(), true);
101 #endif
102  sq_getstackobj(vm,-1,&obj);
103  sq_addref(vm, &obj);
104  sq_pop(vm, 1);
105  return true;
106  }
107 
117  void CompileFile(const string& path) {
118  if(!sq_isnull(obj)) {
119  sq_release(vm, &obj);
120  sq_resetobject(&obj);
121  }
122 
123 #if !defined (SCRAT_NO_ERROR_CHECKING)
124  if(SQ_FAILED(sqstd_loadfile(vm, path.c_str(), true))) {
125  Error::Instance().Throw(vm, LastErrorString(vm));
126  return;
127  }
128 #else
129  sqstd_loadfile(vm, path.c_str(), true);
130 #endif
131  sq_getstackobj(vm,-1,&obj);
132  sq_addref(vm, &obj);
133  sq_pop(vm, 1);
134  }
135 
143  bool CompileFile(const string& path, string& errMsg) {
144  if(!sq_isnull(obj)) {
145  sq_release(vm, &obj);
146  sq_resetobject(&obj);
147  }
148 
149 #if !defined (SCRAT_NO_ERROR_CHECKING)
150  if(SQ_FAILED(sqstd_loadfile(vm, path.c_str(), true))) {
151  errMsg = LastErrorString(vm);
152  return false;
153  }
154 #else
155  sqstd_loadfile(vm, path.c_str(), true);
156 #endif
157  sq_getstackobj(vm,-1,&obj);
158  sq_addref(vm, &obj);
159  sq_pop(vm, 1);
160  return true;
161  }
162 
170  void Run() {
171 #if !defined (SCRAT_NO_ERROR_CHECKING)
172  if(!sq_isnull(obj)) {
173  SQRESULT result;
174  sq_pushobject(vm, obj);
175  sq_pushroottable(vm);
176  result = sq_call(vm, 1, false, true);
177  sq_pop(vm, 1);
178  if(SQ_FAILED(result)) {
179  Error::Instance().Throw(vm, LastErrorString(vm));
180  return;
181  }
182  }
183 #else
184  sq_pushobject(vm, obj);
185  sq_pushroottable(vm);
186  sq_call(vm, 1, false, true);
187  sq_pop(vm, 1);
188 #endif
189  }
190 
191 #if !defined (SCRAT_NO_ERROR_CHECKING)
192  bool Run(string& errMsg) {
199  if(!sq_isnull(obj)) {
200  SQRESULT result;
201  sq_pushobject(vm, obj);
202  sq_pushroottable(vm);
203  result = sq_call(vm, 1, false, true);
204  sq_pop(vm, 1);
205  if(SQ_FAILED(result)) {
206  errMsg = LastErrorString(vm);
207  return false;
208  }
209  return true;
210  }
211  return false;
212  }
213 #endif
214 
221  void WriteCompiledFile(const string& path) {
222 #if !defined (SCRAT_NO_ERROR_CHECKING)
223  if(!sq_isnull(obj)) {
224  sq_pushobject(vm, obj);
225  sqstd_writeclosuretofile(vm, path.c_str());
226  }
227 #else
228  sq_pushobject(vm, obj);
229  sqstd_writeclosuretofile(vm, path.c_str());
230 #endif
231  sq_pop(vm, 1); // needed?
232  }
233 };
234 
235 }
236 
237 #endif
static HSQUIRRELVM Get()
Definition: sqratUtil.h:92
void CompileFile(const string &path)
Definition: sqratScript.h:117
Script(HSQUIRRELVM v=DefaultVM::Get())
Definition: sqratScript.h:50
void WriteCompiledFile(const string &path)
Definition: sqratScript.h:221
void Run()
Definition: sqratScript.h:170
Definition: sqratObject.h:48
static Error & Instance()
Definition: sqratUtil.h:134
void Throw(HSQUIRRELVM vm, const string &err)
Definition: sqratUtil.h:210
bool CompileString(const string &script, string &errMsg, const string &name=_SC(""))
Definition: sqratScript.h:88
Helper class for managing Squirrel scripts.
Definition: sqratScript.h:42
bool CompileFile(const string &path, string &errMsg)
Definition: sqratScript.h:143
void CompileString(const string &script, const string &name=_SC(""))
Definition: sqratScript.h:62