a59dbf91e95a7bec7b6578a7656391da2c582e3e
[supertux.git] / src / squirrel / include / squirrel.h
1 /*\r
2 Copyright (c) 2003-2005 Alberto Demichelis\r
3 \r
4 This software is provided 'as-is', without any \r
5 express or implied warranty. In no event will the \r
6 authors be held liable for any damages arising from \r
7 the use of this software.\r
8 \r
9 Permission is granted to anyone to use this software \r
10 for any purpose, including commercial applications, \r
11 and to alter it and redistribute it freely, subject \r
12 to the following restrictions:\r
13 \r
14                 1. The origin of this software must not be \r
15                 misrepresented; you must not claim that \r
16                 you wrote the original software. If you \r
17                 use this software in a product, an \r
18                 acknowledgment in the product \r
19                 documentation would be appreciated but is \r
20                 not required.\r
21 \r
22                 2. Altered source versions must be plainly \r
23                 marked as such, and must not be \r
24                 misrepresented as being the original \r
25                 software.\r
26 \r
27                 3. This notice may not be removed or \r
28                 altered from any source distribution.\r
29 \r
30 */\r
31 #ifndef _SQUIRREL_H_\r
32 #define _SQUIRREL_H_\r
33 \r
34 #ifdef __cplusplus\r
35 extern "C" {\r
36 #endif\r
37 \r
38 #ifndef SQUIRREL_API\r
39 #define SQUIRREL_API extern\r
40 #endif\r
41 \r
42 typedef float SQFloat;\r
43 typedef int SQInteger;\r
44 typedef void* SQUserPointer;\r
45 typedef unsigned int SQBool;\r
46 typedef int SQRESULT;\r
47 \r
48 #define SQTrue  1\r
49 #define SQFalse 0\r
50 \r
51 \r
52 struct SQVM;\r
53 struct SQTable;\r
54 struct SQArray;\r
55 struct SQString;\r
56 struct SQClosure;\r
57 struct SQGenerator;\r
58 struct SQNativeClosure;\r
59 struct SQUserData;\r
60 struct SQFunctionProto;\r
61 struct SQRefCounted;\r
62 struct SQClass;\r
63 struct SQInstance;\r
64 struct SQDelegable;\r
65 \r
66 #ifdef _UNICODE\r
67 #define SQUNICODE\r
68 #endif\r
69 \r
70 #ifdef SQUNICODE\r
71 typedef unsigned short SQChar;\r
72 #define _SC(a) L##a\r
73 #define scstrcmp        wcscmp\r
74 #define scsprintf       swprintf\r
75 #define scstrlen        wcslen\r
76 #define scstrtod        wcstod\r
77 #define scatoi          _wtoi\r
78 #define scstrtoul       wcstoul\r
79 #define scvsprintf      vswprintf\r
80 #define scstrstr        wcsstr\r
81 #define scisspace       iswspace\r
82 #define scisdigit       iswdigit\r
83 #define scisalpha       iswalpha\r
84 #define sciscntrl       iswcntrl\r
85 #define scisalnum       iswalnum\r
86 #define scprintf        wprintf\r
87 #define MAX_CHAR 0xFFFF\r
88 #else\r
89 typedef char SQChar;\r
90 #define _SC(a) a\r
91 #define scstrcmp        strcmp\r
92 #define scsprintf       sprintf\r
93 #define scstrlen        strlen\r
94 #define scstrtod        strtod\r
95 #define scatoi          atoi\r
96 #define scstrtoul       strtoul\r
97 #define scvsprintf      vsprintf\r
98 #define scstrstr        strstr\r
99 #define scisspace       isspace\r
100 #define scisdigit       isdigit\r
101 #define sciscntrl       iscntrl\r
102 #define scisalpha       isalpha\r
103 #define scisalnum       isalnum\r
104 #define scprintf        printf\r
105 #define MAX_CHAR 0xFF\r
106 #endif\r
107 \r
108 #define SQUIRREL_VERSION        _SC("Squirrel 2.0.1 stable")\r
109 #define SQUIRREL_COPYRIGHT      _SC("Copyright (C) 2003-2005 Alberto Demichelis")\r
110 #define SQUIRREL_AUTHOR         _SC("Alberto Demichelis")\r
111 \r
112 #define SQ_VMSTATE_IDLE                 0\r
113 #define SQ_VMSTATE_RUNNING              1\r
114 #define SQ_VMSTATE_SUSPENDED    2\r
115 \r
116 #define SQUIRREL_EOB 0\r
117 #define SQ_BYTECODE_STREAM_TAG  0xFAFA\r
118 \r
119 #define SQOBJECT_REF_COUNTED    0x00800000\r
120 #define SQOBJECT_NUMERIC                0x00080000\r
121 #define SQOBJECT_DELEGABLE              0x08000000\r
122 #define SQOBJECT_CANBEFALSE             0x80000000\r
123 //typedef unsigned int SQObjectType;\r
124 \r
125 #define _RT_MASK 0x0000FFFF\r
126 #define _RAW_TYPE(type) (type&_RT_MASK)\r
127 \r
128 #define _RT_NULL                        0x00000000\r
129 #define _RT_INTEGER                     0x00000001\r
130 #define _RT_FLOAT                       0x00000002\r
131 #define _RT_BOOL                        0x00000004\r
132 #define _RT_STRING                      0x00000008\r
133 #define _RT_TABLE                       0x00000010\r
134 #define _RT_ARRAY                       0x00000020\r
135 #define _RT_USERDATA            0x00000040\r
136 #define _RT_CLOSURE                     0x00000080\r
137 #define _RT_NATIVECLOSURE       0x00000100\r
138 #define _RT_GENERATOR           0x00000200\r
139 #define _RT_USERPOINTER         0x00000400\r
140 #define _RT_THREAD                      0x00000800\r
141 #define _RT_FUNCPROTO           0x00001000\r
142 #define _RT_CLASS                       0x00002000\r
143 #define _RT_INSTANCE            0x00004000\r
144 \r
145 typedef enum {\r
146         OT_NULL =                       (_RT_NULL|SQOBJECT_CANBEFALSE),\r
147         OT_INTEGER =            (_RT_INTEGER|SQOBJECT_NUMERIC|SQOBJECT_CANBEFALSE),\r
148         OT_FLOAT =                      (_RT_FLOAT|SQOBJECT_NUMERIC|SQOBJECT_CANBEFALSE),\r
149         OT_BOOL =                       (_RT_BOOL|SQOBJECT_CANBEFALSE),\r
150         OT_STRING =                     (_RT_STRING|SQOBJECT_REF_COUNTED),\r
151         OT_TABLE =                      (_RT_TABLE|SQOBJECT_REF_COUNTED|SQOBJECT_DELEGABLE),\r
152         OT_ARRAY =                      (_RT_ARRAY|SQOBJECT_REF_COUNTED),\r
153         OT_USERDATA =           (_RT_USERDATA|SQOBJECT_REF_COUNTED|SQOBJECT_DELEGABLE),\r
154         OT_CLOSURE =            (_RT_CLOSURE|SQOBJECT_REF_COUNTED),\r
155         OT_NATIVECLOSURE =      (_RT_NATIVECLOSURE|SQOBJECT_REF_COUNTED),\r
156         OT_GENERATOR =          (_RT_GENERATOR|SQOBJECT_REF_COUNTED),\r
157         OT_USERPOINTER =        _RT_USERPOINTER,\r
158         OT_THREAD =                     (_RT_THREAD|SQOBJECT_REF_COUNTED) ,\r
159         OT_FUNCPROTO =          (_RT_FUNCPROTO|SQOBJECT_REF_COUNTED), //internal usage only\r
160         OT_CLASS =                      (_RT_CLASS|SQOBJECT_REF_COUNTED),\r
161         OT_INSTANCE =           (_RT_INSTANCE|SQOBJECT_REF_COUNTED|SQOBJECT_DELEGABLE)\r
162 }SQObjectType;\r
163 \r
164 #define ISREFCOUNTED(t) (t&SQOBJECT_REF_COUNTED)\r
165 \r
166 \r
167 typedef union tagSQObjectValue\r
168 {\r
169         struct SQTable *pTable;\r
170         struct SQArray *pArray;\r
171         struct SQClosure *pClosure;\r
172         struct SQGenerator *pGenerator;\r
173         struct SQNativeClosure *pNativeClosure;\r
174         struct SQString *pString;\r
175         struct SQUserData *pUserData;\r
176         SQInteger nInteger;\r
177         SQFloat fFloat;\r
178         SQUserPointer pUserPointer;\r
179         struct SQFunctionProto *pFunctionProto;\r
180         struct SQRefCounted *pRefCounted;\r
181         struct SQDelegable *pDelegable;\r
182         struct SQVM *pThread;\r
183         struct SQClass *pClass;\r
184         struct SQInstance *pInstance;\r
185 }SQObjectValue;\r
186 \r
187 \r
188 typedef struct tagSQObject\r
189 {\r
190         SQObjectValue _unVal;\r
191         SQObjectType _type;\r
192 }SQObject;\r
193 \r
194 typedef struct tagSQStackInfos{\r
195         const SQChar* funcname;\r
196         const SQChar* source;\r
197         int line;\r
198 }SQStackInfos;\r
199 \r
200 typedef struct SQVM* HSQUIRRELVM;\r
201 typedef SQObject HSQOBJECT;\r
202 typedef int (*SQFUNCTION)(HSQUIRRELVM);\r
203 typedef int (*SQRELEASEHOOK)(SQUserPointer,int size);\r
204 typedef void (*SQCOMPILERERROR)(HSQUIRRELVM,const SQChar * /*desc*/,const SQChar * /*source*/,int /*line*/,int /*column*/);\r
205 typedef void (*SQPRINTFUNCTION)(HSQUIRRELVM,const SQChar * ,...);\r
206 \r
207 typedef int (*SQWRITEFUNC)(SQUserPointer,SQUserPointer,int);\r
208 typedef int (*SQREADFUNC)(SQUserPointer,SQUserPointer,int);\r
209 \r
210 typedef SQInteger (*SQLEXREADFUNC)(SQUserPointer);\r
211 \r
212 typedef struct tagSQRegFunction{\r
213         const SQChar *name;\r
214         SQFUNCTION f;\r
215         int nparamscheck;\r
216         const SQChar *typemask;\r
217 }SQRegFunction;\r
218 \r
219 /*vm*/\r
220 SQUIRREL_API HSQUIRRELVM sq_open(int initialstacksize);\r
221 SQUIRREL_API HSQUIRRELVM sq_newthread(HSQUIRRELVM friendvm, int initialstacksize);\r
222 SQUIRREL_API void sq_seterrorhandler(HSQUIRRELVM v);\r
223 SQUIRREL_API void sq_close(HSQUIRRELVM v);\r
224 SQUIRREL_API void sq_setforeignptr(HSQUIRRELVM v,SQUserPointer p);\r
225 SQUIRREL_API SQUserPointer sq_getforeignptr(HSQUIRRELVM v);\r
226 SQUIRREL_API void sq_setprintfunc(HSQUIRRELVM v, SQPRINTFUNCTION printfunc);\r
227 SQUIRREL_API SQPRINTFUNCTION sq_getprintfunc(HSQUIRRELVM v);\r
228 SQUIRREL_API SQRESULT sq_suspendvm(HSQUIRRELVM v);\r
229 SQUIRREL_API SQRESULT sq_wakeupvm(HSQUIRRELVM v,SQBool resumedret,SQBool retval);\r
230 SQUIRREL_API int sq_getvmstate(HSQUIRRELVM v);\r
231 \r
232 /*compiler*/\r
233 SQUIRREL_API SQRESULT sq_compile(HSQUIRRELVM v,SQLEXREADFUNC read,SQUserPointer p,const SQChar *sourcename,SQBool raiseerror);\r
234 SQUIRREL_API SQRESULT sq_compilebuffer(HSQUIRRELVM v,const SQChar *s,int size,const SQChar *sourcename,SQBool raiseerror);\r
235 SQUIRREL_API void sq_enabledebuginfo(HSQUIRRELVM v, SQBool debuginfo);\r
236 SQUIRREL_API void sq_setcompilererrorhandler(HSQUIRRELVM v,SQCOMPILERERROR f);\r
237 \r
238 /*stack operations*/\r
239 SQUIRREL_API void sq_push(HSQUIRRELVM v,int idx);\r
240 SQUIRREL_API void sq_pop(HSQUIRRELVM v,int nelemstopop);\r
241 SQUIRREL_API void sq_remove(HSQUIRRELVM v,int idx);\r
242 SQUIRREL_API int sq_gettop(HSQUIRRELVM v);\r
243 SQUIRREL_API void sq_settop(HSQUIRRELVM v,int newtop);\r
244 SQUIRREL_API void sq_reservestack(HSQUIRRELVM v,int nsize);\r
245 SQUIRREL_API int sq_cmp(HSQUIRRELVM v);\r
246 SQUIRREL_API void sq_move(HSQUIRRELVM dest,HSQUIRRELVM src,int idx);\r
247 \r
248 /*object creation handling*/\r
249 SQUIRREL_API SQUserPointer sq_newuserdata(HSQUIRRELVM v,unsigned int size);\r
250 SQUIRREL_API void sq_newtable(HSQUIRRELVM v);\r
251 SQUIRREL_API void sq_newarray(HSQUIRRELVM v,int size);\r
252 SQUIRREL_API void sq_newclosure(HSQUIRRELVM v,SQFUNCTION func,unsigned int nfreevars);\r
253 SQUIRREL_API SQRESULT sq_setparamscheck(HSQUIRRELVM v,int nparamscheck,const SQChar *typemask);\r
254 SQUIRREL_API void sq_pushstring(HSQUIRRELVM v,const SQChar *s,int len);\r
255 SQUIRREL_API void sq_pushfloat(HSQUIRRELVM v,SQFloat f);\r
256 SQUIRREL_API void sq_pushinteger(HSQUIRRELVM v,SQInteger n);\r
257 SQUIRREL_API void sq_pushbool(HSQUIRRELVM v,SQBool b);\r
258 SQUIRREL_API void sq_pushuserpointer(HSQUIRRELVM v,SQUserPointer p);\r
259 SQUIRREL_API void sq_pushnull(HSQUIRRELVM v);\r
260 SQUIRREL_API SQObjectType sq_gettype(HSQUIRRELVM v,int idx);\r
261 SQUIRREL_API SQInteger sq_getsize(HSQUIRRELVM v,int idx);\r
262 SQUIRREL_API SQRESULT sq_getstring(HSQUIRRELVM v,int idx,const SQChar **c);\r
263 SQUIRREL_API SQRESULT sq_getinteger(HSQUIRRELVM v,int idx,SQInteger *i);\r
264 SQUIRREL_API SQRESULT sq_getfloat(HSQUIRRELVM v,int idx,SQFloat *f);\r
265 SQUIRREL_API SQRESULT sq_getbool(HSQUIRRELVM v,int idx,SQBool *b);\r
266 SQUIRREL_API SQRESULT sq_getthread(HSQUIRRELVM v,int idx,HSQUIRRELVM *thread);\r
267 SQUIRREL_API SQRESULT sq_getuserpointer(HSQUIRRELVM v,int idx,SQUserPointer *p);\r
268 SQUIRREL_API SQRESULT sq_getuserdata(HSQUIRRELVM v,int idx,SQUserPointer *p,unsigned int *typetag);\r
269 SQUIRREL_API SQRESULT sq_settypetag(HSQUIRRELVM v,int idx,unsigned int typetag);\r
270 SQUIRREL_API SQRESULT sq_gettypetag(HSQUIRRELVM v,int idx,unsigned int *typetag);\r
271 SQUIRREL_API void sq_setreleasehook(HSQUIRRELVM v,int idx,SQRELEASEHOOK hook);\r
272 SQUIRREL_API SQChar *sq_getscratchpad(HSQUIRRELVM v,int minsize);\r
273 SQUIRREL_API SQRESULT sq_getclosureinfo(HSQUIRRELVM v,int idx,unsigned int *nparams,unsigned int *nfreevars);\r
274 SQUIRREL_API SQRESULT sq_setnativeclosurename(HSQUIRRELVM v,int idx,const SQChar *name);\r
275 SQUIRREL_API SQRESULT sq_setinstanceup(HSQUIRRELVM v, int idx, SQUserPointer p);\r
276 SQUIRREL_API SQRESULT sq_getinstanceup(HSQUIRRELVM v, int idx, SQUserPointer *p,unsigned int typetag);\r
277 SQUIRREL_API SQRESULT sq_newclass(HSQUIRRELVM v,SQBool hasbase);\r
278 SQUIRREL_API SQRESULT sq_createinstance(HSQUIRRELVM v,int idx);\r
279 SQUIRREL_API SQRESULT sq_setattributes(HSQUIRRELVM v,int idx);\r
280 SQUIRREL_API SQRESULT sq_getattributes(HSQUIRRELVM v,int idx);\r
281 SQUIRREL_API SQRESULT sq_getclass(HSQUIRRELVM v,int idx);\r
282 \r
283 /*object manipulation*/\r
284 SQUIRREL_API void sq_pushroottable(HSQUIRRELVM v);\r
285 SQUIRREL_API void sq_pushregistrytable(HSQUIRRELVM v);\r
286 SQUIRREL_API SQRESULT sq_setroottable(HSQUIRRELVM v);\r
287 SQUIRREL_API SQRESULT sq_createslot(HSQUIRRELVM v,int idx);\r
288 SQUIRREL_API SQRESULT sq_deleteslot(HSQUIRRELVM v,int idx,SQBool pushval);\r
289 SQUIRREL_API SQRESULT sq_set(HSQUIRRELVM v,int idx);\r
290 SQUIRREL_API SQRESULT sq_get(HSQUIRRELVM v,int idx);\r
291 SQUIRREL_API SQRESULT sq_rawget(HSQUIRRELVM v,int idx);\r
292 SQUIRREL_API SQRESULT sq_rawset(HSQUIRRELVM v,int idx);\r
293 SQUIRREL_API SQRESULT sq_rawdeleteslot(HSQUIRRELVM v,int idx,SQBool pushval);\r
294 SQUIRREL_API SQRESULT sq_arrayappend(HSQUIRRELVM v,int idx);\r
295 SQUIRREL_API SQRESULT sq_arraypop(HSQUIRRELVM v,int idx,SQBool pushval); \r
296 SQUIRREL_API SQRESULT sq_arrayresize(HSQUIRRELVM v,int idx,int newsize); \r
297 SQUIRREL_API SQRESULT sq_arrayreverse(HSQUIRRELVM v,int idx); \r
298 SQUIRREL_API SQRESULT sq_setdelegate(HSQUIRRELVM v,int idx);\r
299 SQUIRREL_API SQRESULT sq_getdelegate(HSQUIRRELVM v,int idx);\r
300 SQUIRREL_API SQRESULT sq_clone(HSQUIRRELVM v,int idx);\r
301 SQUIRREL_API SQRESULT sq_setfreevariable(HSQUIRRELVM v,int idx,unsigned int nval);\r
302 SQUIRREL_API SQRESULT sq_next(HSQUIRRELVM v,int idx);\r
303 \r
304 /*calls*/\r
305 SQUIRREL_API SQRESULT sq_call(HSQUIRRELVM v,int params,SQBool retval);\r
306 SQUIRREL_API SQRESULT sq_resume(HSQUIRRELVM v,SQBool retval);\r
307 SQUIRREL_API const SQChar *sq_getlocal(HSQUIRRELVM v,unsigned int level,unsigned int idx);\r
308 SQUIRREL_API SQRESULT sq_throwerror(HSQUIRRELVM v,const SQChar *err);\r
309 SQUIRREL_API void sq_reseterror(HSQUIRRELVM v);\r
310 SQUIRREL_API void sq_getlasterror(HSQUIRRELVM v);\r
311 \r
312 /*raw object handling*/\r
313 SQUIRREL_API SQRESULT sq_getstackobj(HSQUIRRELVM v,int idx,HSQOBJECT *po);\r
314 SQUIRREL_API void sq_pushobject(HSQUIRRELVM v,HSQOBJECT obj);\r
315 SQUIRREL_API void sq_addref(HSQUIRRELVM v,HSQOBJECT *po);\r
316 SQUIRREL_API SQBool sq_release(HSQUIRRELVM v,HSQOBJECT *po);\r
317 SQUIRREL_API void sq_resetobject(HSQOBJECT *po);\r
318 SQUIRREL_API const SQChar *sq_objtostring(HSQOBJECT *o);\r
319 SQUIRREL_API SQInteger sq_objtointeger(HSQOBJECT *o);\r
320 SQUIRREL_API SQFloat sq_objtofloat(HSQOBJECT *o);\r
321 \r
322 /*GC*/\r
323 SQUIRREL_API int sq_collectgarbage(HSQUIRRELVM v);\r
324 \r
325 /*serialization*/\r
326 SQUIRREL_API SQRESULT sq_writeclosure(HSQUIRRELVM vm,SQWRITEFUNC writef,SQUserPointer up);\r
327 SQUIRREL_API SQRESULT sq_readclosure(HSQUIRRELVM vm,SQREADFUNC readf,SQUserPointer up);\r
328 \r
329 /*mem allocation*/\r
330 SQUIRREL_API void *sq_malloc(unsigned int size);\r
331 SQUIRREL_API void *sq_realloc(void* p,unsigned int oldsize,unsigned int newsize);\r
332 SQUIRREL_API void sq_free(void *p,unsigned int size);\r
333 \r
334 /*debug*/\r
335 SQUIRREL_API SQRESULT sq_stackinfos(HSQUIRRELVM v,int level,SQStackInfos *si);\r
336 SQUIRREL_API void sq_setdebughook(HSQUIRRELVM v);\r
337 \r
338 /*UTILITY MACRO*/\r
339 #define sq_isnumeric(o) ((o)._type&SQOBJECT_NUMERIC)\r
340 #define sq_istable(o) ((o)._type==OT_TABLE)\r
341 #define sq_isarray(o) ((o)._type==OT_ARRAY)\r
342 #define sq_isfunction(o) ((o)._type==OT_FUNCPROTO)\r
343 #define sq_isclosure(o) ((o)._type==OT_CLOSURE)\r
344 #define sq_isgenerator(o) ((o)._type==OT_GENERATOR)\r
345 #define sq_isnativeclosure(o) ((o)._type==OT_NATIVECLOSURE)\r
346 #define sq_isstring(o) ((o)._type==OT_STRING)\r
347 #define sq_isinteger(o) ((o)._type==OT_INTEGER)\r
348 #define sq_isfloat(o) ((o)._type==OT_FLOAT)\r
349 #define sq_isuserpointer(o) ((o)._type==OT_USERPOINTER)\r
350 #define sq_isuserdata(o) ((o)._type==OT_USERDATA)\r
351 #define sq_isthread(o) ((o)._type==OT_THREAD)\r
352 #define sq_isnull(o) ((o)._type==OT_NULL)\r
353 #define sq_isclass(o) ((o)._type==OT_CLASS)\r
354 #define sq_isinstance(o) ((o)._type==OT_INSTANCE)\r
355 #define sq_isbool(o) ((o)._type==OT_BOOL)\r
356 #define sq_type(o) ((o)._type)\r
357 \r
358 #define SQ_OK (0)\r
359 #define SQ_ERROR (-1)\r
360 \r
361 #define SQ_FAILED(res) (res<0)\r
362 #define SQ_SUCCEEDED(res) (res>=0)\r
363 \r
364 #ifdef __cplusplus\r
365 } /*extern "C"*/\r
366 #endif\r
367 \r
368 #endif /*_SQUIRREL_H_*/\r