Update readme.md
[openttd-joker.git] / src / 3rdparty / squirrel / include / squirrel.h
blob87e0ac0d69f41f09679054e0075dfe3b975cb62f
1 /*
2 * Copyright (c) 2003-2011 Alberto Demichelis
4 * This software is provided 'as-is', without any
5 * express or implied warranty. In no event will the
6 * authors be held liable for any damages arising from
7 * the use of this software.
9 * Permission is granted to anyone to use this software
10 * for any purpose, including commercial applications,
11 * and to alter it and redistribute it freely, subject
12 * to the following restrictions:
14 * 1. The origin of this software must not be
15 * misrepresented; you must not claim that
16 * you wrote the original software. If you
17 * use this software in a product, an
18 * acknowledgment in the product
19 * documentation would be appreciated but is
20 * not required.
22 * 2. Altered source versions must be plainly
23 * marked as such, and must not be
24 * misrepresented as being the original
25 * software.
27 * 3. This notice may not be removed or
28 * altered from any source distribution.
30 #ifndef _SQUIRREL_H_
31 #define _SQUIRREL_H_
33 #include "../../../string_type.h"
35 typedef __int64 SQInteger;
36 typedef unsigned __int64 SQUnsignedInteger;
37 typedef unsigned __int64 SQHash; /*should be the same size of a pointer*/
38 typedef int SQInt32;
41 #ifdef SQUSEDOUBLE
42 typedef double SQFloat;
43 #else
44 typedef float SQFloat;
45 #endif
47 typedef __int64 SQRawObjectVal; //must be 64bits
48 #define SQ_OBJECT_RAWINIT() { _unVal.raw = 0; }
50 typedef void* SQUserPointer;
51 typedef SQUnsignedInteger SQBool;
52 typedef SQInteger SQRESULT;
54 #define SQTrue (1)
55 #define SQFalse (0)
57 struct SQVM;
58 struct SQTable;
59 struct SQArray;
60 struct SQString;
61 struct SQClosure;
62 struct SQGenerator;
63 struct SQNativeClosure;
64 struct SQUserData;
65 struct SQFunctionProto;
66 struct SQRefCounted;
67 struct SQClass;
68 struct SQInstance;
69 struct SQDelegable;
71 typedef char SQChar;
72 #define MAX_CHAR 0xFFFF
74 #define SQUIRREL_VERSION "Squirrel 2.2.5 stable - With custom OpenTTD modifications"
75 #define SQUIRREL_COPYRIGHT "Copyright (C) 2003-2010 Alberto Demichelis"
76 #define SQUIRREL_AUTHOR "Alberto Demichelis"
77 #define SQUIRREL_VERSION_NUMBER 225
79 #define SQ_VMSTATE_IDLE 0
80 #define SQ_VMSTATE_RUNNING 1
81 #define SQ_VMSTATE_SUSPENDED 2
83 #define SQUIRREL_EOB 0
84 #define SQ_BYTECODE_STREAM_TAG 0xFAFA
86 #define SQOBJECT_REF_COUNTED 0x08000000
87 #define SQOBJECT_NUMERIC 0x04000000
88 #define SQOBJECT_DELEGABLE 0x02000000
89 #define SQOBJECT_CANBEFALSE 0x01000000
91 #define SQ_MATCHTYPEMASKSTRING (-99999)
93 #define _RT_MASK 0x00FFFFFF
94 #define _RAW_TYPE(type) (type&_RT_MASK)
96 #define _RT_NULL 0x00000001
97 #define _RT_INTEGER 0x00000002
98 #define _RT_FLOAT 0x00000004
99 #define _RT_BOOL 0x00000008
100 #define _RT_STRING 0x00000010
101 #define _RT_TABLE 0x00000020
102 #define _RT_ARRAY 0x00000040
103 #define _RT_USERDATA 0x00000080
104 #define _RT_CLOSURE 0x00000100
105 #define _RT_NATIVECLOSURE 0x00000200
106 #define _RT_GENERATOR 0x00000400
107 #define _RT_USERPOINTER 0x00000800
108 #define _RT_THREAD 0x00001000
109 #define _RT_FUNCPROTO 0x00002000
110 #define _RT_CLASS 0x00004000
111 #define _RT_INSTANCE 0x00008000
112 #define _RT_WEAKREF 0x00010000
114 typedef enum tagSQObjectType{
115 OT_NULL = (_RT_NULL|SQOBJECT_CANBEFALSE),
116 OT_INTEGER = (_RT_INTEGER|SQOBJECT_NUMERIC|SQOBJECT_CANBEFALSE),
117 OT_FLOAT = (_RT_FLOAT|SQOBJECT_NUMERIC|SQOBJECT_CANBEFALSE),
118 OT_BOOL = (_RT_BOOL|SQOBJECT_CANBEFALSE),
119 OT_STRING = (_RT_STRING|SQOBJECT_REF_COUNTED),
120 OT_TABLE = (_RT_TABLE|SQOBJECT_REF_COUNTED|SQOBJECT_DELEGABLE),
121 OT_ARRAY = (_RT_ARRAY|SQOBJECT_REF_COUNTED),
122 OT_USERDATA = (_RT_USERDATA|SQOBJECT_REF_COUNTED|SQOBJECT_DELEGABLE),
123 OT_CLOSURE = (_RT_CLOSURE|SQOBJECT_REF_COUNTED),
124 OT_NATIVECLOSURE = (_RT_NATIVECLOSURE|SQOBJECT_REF_COUNTED),
125 OT_GENERATOR = (_RT_GENERATOR|SQOBJECT_REF_COUNTED),
126 OT_USERPOINTER = _RT_USERPOINTER,
127 OT_THREAD = (_RT_THREAD|SQOBJECT_REF_COUNTED) ,
128 OT_FUNCPROTO = (_RT_FUNCPROTO|SQOBJECT_REF_COUNTED), //internal usage only
129 OT_CLASS = (_RT_CLASS|SQOBJECT_REF_COUNTED),
130 OT_INSTANCE = (_RT_INSTANCE|SQOBJECT_REF_COUNTED|SQOBJECT_DELEGABLE),
131 OT_WEAKREF = (_RT_WEAKREF|SQOBJECT_REF_COUNTED)
132 }SQObjectType;
134 #define ISREFCOUNTED(t) (t&SQOBJECT_REF_COUNTED)
137 typedef union tagSQObjectValue
139 struct SQTable *pTable;
140 struct SQArray *pArray;
141 struct SQClosure *pClosure;
142 struct SQGenerator *pGenerator;
143 struct SQNativeClosure *pNativeClosure;
144 struct SQString *pString;
145 struct SQUserData *pUserData;
146 SQInteger nInteger;
147 SQFloat fFloat;
148 SQUserPointer pUserPointer;
149 struct SQFunctionProto *pFunctionProto;
150 struct SQRefCounted *pRefCounted;
151 struct SQDelegable *pDelegable;
152 struct SQVM *pThread;
153 struct SQClass *pClass;
154 struct SQInstance *pInstance;
155 struct SQWeakRef *pWeakRef;
156 SQRawObjectVal raw;
157 }SQObjectValue;
160 typedef struct tagSQObject
162 SQObjectType _type;
163 SQObjectValue _unVal;
164 }SQObject;
166 typedef struct tagSQStackInfos{
167 const SQChar* funcname;
168 const SQChar* source;
169 SQInteger line;
170 }SQStackInfos;
172 typedef struct SQVM* HSQUIRRELVM;
173 typedef SQObject HSQOBJECT;
174 typedef SQInteger (*SQFUNCTION)(HSQUIRRELVM);
175 typedef SQInteger (*SQRELEASEHOOK)(SQUserPointer,SQInteger size);
176 typedef void (*SQCOMPILERERROR)(HSQUIRRELVM,const SQChar * /*desc*/,const SQChar * /*source*/,SQInteger /*line*/,SQInteger /*column*/);
177 typedef void (*SQPRINTFUNCTION)(HSQUIRRELVM,const SQChar * ,...);
179 typedef SQInteger (*SQWRITEFUNC)(SQUserPointer,SQUserPointer,SQInteger);
180 typedef SQInteger (*SQREADFUNC)(SQUserPointer,SQUserPointer,SQInteger);
182 typedef WChar (*SQLEXREADFUNC)(SQUserPointer);
184 typedef struct tagSQRegFunction{
185 const SQChar *name;
186 SQFUNCTION f;
187 SQInteger nparamscheck;
188 const SQChar *typemask;
189 }SQRegFunction;
191 typedef struct tagSQFunctionInfo {
192 SQUserPointer funcid;
193 const SQChar *name;
194 const SQChar *source;
195 }SQFunctionInfo;
198 /*vm*/
199 bool sq_can_suspend(HSQUIRRELVM v);
200 HSQUIRRELVM sq_open(SQInteger initialstacksize);
201 HSQUIRRELVM sq_newthread(HSQUIRRELVM friendvm, SQInteger initialstacksize);
202 void sq_seterrorhandler(HSQUIRRELVM v);
203 void sq_close(HSQUIRRELVM v);
204 void sq_setforeignptr(HSQUIRRELVM v,SQUserPointer p);
205 SQUserPointer sq_getforeignptr(HSQUIRRELVM v);
206 void sq_setprintfunc(HSQUIRRELVM v, SQPRINTFUNCTION printfunc);
207 SQPRINTFUNCTION sq_getprintfunc(HSQUIRRELVM v);
208 SQRESULT sq_suspendvm(HSQUIRRELVM v);
209 bool sq_resumecatch(HSQUIRRELVM v, int suspend = -1);
210 bool sq_resumeerror(HSQUIRRELVM v);
211 SQRESULT sq_wakeupvm(HSQUIRRELVM v,SQBool resumedret,SQBool retval,SQBool raiseerror,SQBool throwerror);
212 SQInteger sq_getvmstate(HSQUIRRELVM v);
213 void sq_decreaseops(HSQUIRRELVM v, int amount);
215 /*compiler*/
216 SQRESULT sq_compile(HSQUIRRELVM v,SQLEXREADFUNC read,SQUserPointer p,const SQChar *sourcename,SQBool raiseerror);
217 SQRESULT sq_compilebuffer(HSQUIRRELVM v,const SQChar *s,SQInteger size,const SQChar *sourcename,SQBool raiseerror);
218 void sq_enabledebuginfo(HSQUIRRELVM v, SQBool enable);
219 void sq_notifyallexceptions(HSQUIRRELVM v, SQBool enable);
220 void sq_setcompilererrorhandler(HSQUIRRELVM v,SQCOMPILERERROR f);
222 /*stack operations*/
223 void sq_push(HSQUIRRELVM v,SQInteger idx);
224 void sq_pop(HSQUIRRELVM v,SQInteger nelemstopop);
225 void sq_poptop(HSQUIRRELVM v);
226 void sq_remove(HSQUIRRELVM v,SQInteger idx);
227 SQInteger sq_gettop(HSQUIRRELVM v);
228 void sq_settop(HSQUIRRELVM v,SQInteger newtop);
229 void sq_reservestack(HSQUIRRELVM v,SQInteger nsize);
230 SQInteger sq_cmp(HSQUIRRELVM v);
231 void sq_move(HSQUIRRELVM dest,HSQUIRRELVM src,SQInteger idx);
233 /*object creation handling*/
234 SQUserPointer sq_newuserdata(HSQUIRRELVM v,SQUnsignedInteger size);
235 void sq_newtable(HSQUIRRELVM v);
236 void sq_newarray(HSQUIRRELVM v,SQInteger size);
237 void sq_newclosure(HSQUIRRELVM v,SQFUNCTION func,SQUnsignedInteger nfreevars);
238 SQRESULT sq_setparamscheck(HSQUIRRELVM v,SQInteger nparamscheck,const SQChar *typemask);
239 SQRESULT sq_bindenv(HSQUIRRELVM v,SQInteger idx);
240 void sq_pushstring(HSQUIRRELVM v,const SQChar *s,SQInteger len);
241 void sq_pushfloat(HSQUIRRELVM v,SQFloat f);
242 void sq_pushinteger(HSQUIRRELVM v,SQInteger n);
243 void sq_pushbool(HSQUIRRELVM v,SQBool b);
244 void sq_pushuserpointer(HSQUIRRELVM v,SQUserPointer p);
245 void sq_pushnull(HSQUIRRELVM v);
246 SQObjectType sq_gettype(HSQUIRRELVM v,SQInteger idx);
247 SQInteger sq_getsize(HSQUIRRELVM v,SQInteger idx);
248 SQRESULT sq_getbase(HSQUIRRELVM v,SQInteger idx);
249 SQBool sq_instanceof(HSQUIRRELVM v);
250 void sq_tostring(HSQUIRRELVM v,SQInteger idx);
251 void sq_tobool(HSQUIRRELVM v, SQInteger idx, SQBool *b);
252 SQRESULT sq_getstring(HSQUIRRELVM v,SQInteger idx,const SQChar **c);
253 SQRESULT sq_getinteger(HSQUIRRELVM v,SQInteger idx,SQInteger *i);
254 SQRESULT sq_getfloat(HSQUIRRELVM v,SQInteger idx,SQFloat *f);
255 SQRESULT sq_getbool(HSQUIRRELVM v,SQInteger idx,SQBool *b);
256 SQRESULT sq_getthread(HSQUIRRELVM v,SQInteger idx,HSQUIRRELVM *thread);
257 SQRESULT sq_getuserpointer(HSQUIRRELVM v,SQInteger idx,SQUserPointer *p);
258 SQRESULT sq_getuserdata(HSQUIRRELVM v,SQInteger idx,SQUserPointer *p,SQUserPointer *typetag);
259 SQRESULT sq_settypetag(HSQUIRRELVM v,SQInteger idx,SQUserPointer typetag);
260 SQRESULT sq_gettypetag(HSQUIRRELVM v,SQInteger idx,SQUserPointer *typetag);
261 void sq_setreleasehook(HSQUIRRELVM v,SQInteger idx,SQRELEASEHOOK hook);
262 SQChar *sq_getscratchpad(HSQUIRRELVM v,SQInteger minsize);
263 SQRESULT sq_getfunctioninfo(HSQUIRRELVM v,SQInteger idx,SQFunctionInfo *fi);
264 SQRESULT sq_getclosureinfo(HSQUIRRELVM v,SQInteger idx,SQUnsignedInteger *nparams,SQUnsignedInteger *nfreevars);
265 SQRESULT sq_setnativeclosurename(HSQUIRRELVM v,SQInteger idx,const SQChar *name);
266 SQRESULT sq_setinstanceup(HSQUIRRELVM v, SQInteger idx, SQUserPointer p);
267 SQRESULT sq_getinstanceup(HSQUIRRELVM v, SQInteger idx, SQUserPointer *p,SQUserPointer typetag);
268 SQRESULT sq_setclassudsize(HSQUIRRELVM v, SQInteger idx, SQInteger udsize);
269 SQRESULT sq_newclass(HSQUIRRELVM v,SQBool hasbase);
270 SQRESULT sq_createinstance(HSQUIRRELVM v,SQInteger idx);
271 SQRESULT sq_setattributes(HSQUIRRELVM v,SQInteger idx);
272 SQRESULT sq_getattributes(HSQUIRRELVM v,SQInteger idx);
273 SQRESULT sq_getclass(HSQUIRRELVM v,SQInteger idx);
274 void sq_weakref(HSQUIRRELVM v,SQInteger idx);
275 SQRESULT sq_getdefaultdelegate(HSQUIRRELVM v,SQObjectType t);
277 /*object manipulation*/
278 void sq_pushroottable(HSQUIRRELVM v);
279 void sq_pushregistrytable(HSQUIRRELVM v);
280 void sq_pushconsttable(HSQUIRRELVM v);
281 SQRESULT sq_setroottable(HSQUIRRELVM v);
282 SQRESULT sq_setconsttable(HSQUIRRELVM v);
283 SQRESULT sq_newslot(HSQUIRRELVM v, SQInteger idx, SQBool bstatic);
284 SQRESULT sq_deleteslot(HSQUIRRELVM v,SQInteger idx,SQBool pushval);
285 SQRESULT sq_set(HSQUIRRELVM v,SQInteger idx);
286 SQRESULT sq_get(HSQUIRRELVM v,SQInteger idx);
287 SQRESULT sq_rawget(HSQUIRRELVM v,SQInteger idx);
288 SQRESULT sq_rawset(HSQUIRRELVM v,SQInteger idx);
289 SQRESULT sq_rawdeleteslot(HSQUIRRELVM v,SQInteger idx,SQBool pushval);
290 SQRESULT sq_arrayappend(HSQUIRRELVM v,SQInteger idx);
291 SQRESULT sq_arraypop(HSQUIRRELVM v,SQInteger idx,SQBool pushval);
292 SQRESULT sq_arrayresize(HSQUIRRELVM v,SQInteger idx,SQInteger newsize);
293 SQRESULT sq_arrayreverse(HSQUIRRELVM v,SQInteger idx);
294 SQRESULT sq_arrayremove(HSQUIRRELVM v,SQInteger idx,SQInteger itemidx);
295 SQRESULT sq_arrayinsert(HSQUIRRELVM v,SQInteger idx,SQInteger destpos);
296 SQRESULT sq_setdelegate(HSQUIRRELVM v,SQInteger idx);
297 SQRESULT sq_getdelegate(HSQUIRRELVM v,SQInteger idx);
298 SQRESULT sq_clone(HSQUIRRELVM v,SQInteger idx);
299 SQRESULT sq_setfreevariable(HSQUIRRELVM v,SQInteger idx,SQUnsignedInteger nval);
300 SQRESULT sq_next(HSQUIRRELVM v,SQInteger idx);
301 SQRESULT sq_getweakrefval(HSQUIRRELVM v,SQInteger idx);
302 SQRESULT sq_clear(HSQUIRRELVM v,SQInteger idx);
304 /*calls*/
305 SQRESULT sq_call(HSQUIRRELVM v,SQInteger params,SQBool retval,SQBool raiseerror, int suspend = -1);
306 SQRESULT sq_resume(HSQUIRRELVM v,SQBool retval,SQBool raiseerror);
307 const SQChar *sq_getlocal(HSQUIRRELVM v,SQUnsignedInteger level,SQUnsignedInteger idx);
308 const SQChar *sq_getfreevariable(HSQUIRRELVM v,SQInteger idx,SQUnsignedInteger nval);
309 SQRESULT sq_throwerror(HSQUIRRELVM v,const SQChar *err);
310 void sq_reseterror(HSQUIRRELVM v);
311 void sq_getlasterror(HSQUIRRELVM v);
313 /*raw object handling*/
314 SQRESULT sq_getstackobj(HSQUIRRELVM v,SQInteger idx,HSQOBJECT *po);
315 void sq_pushobject(HSQUIRRELVM v,HSQOBJECT obj);
316 void sq_addref(HSQUIRRELVM v,HSQOBJECT *po);
317 SQBool sq_release(HSQUIRRELVM v,HSQOBJECT *po);
318 void sq_resetobject(HSQOBJECT *po);
319 const SQChar *sq_objtostring(HSQOBJECT *o);
320 SQBool sq_objtobool(HSQOBJECT *o);
321 SQInteger sq_objtointeger(HSQOBJECT *o);
322 SQFloat sq_objtofloat(HSQOBJECT *o);
323 SQRESULT sq_getobjtypetag(HSQOBJECT *o,SQUserPointer * typetag);
325 /*GC*/
326 SQInteger sq_collectgarbage(HSQUIRRELVM v);
328 /*serialization*/
329 SQRESULT sq_writeclosure(HSQUIRRELVM vm,SQWRITEFUNC writef,SQUserPointer up);
330 SQRESULT sq_readclosure(HSQUIRRELVM vm,SQREADFUNC readf,SQUserPointer up);
332 /*mem allocation*/
333 void *sq_malloc(SQUnsignedInteger size);
334 void *sq_realloc(void* p,SQUnsignedInteger oldsize,SQUnsignedInteger newsize);
335 void sq_free(void *p,SQUnsignedInteger size);
337 /*debug*/
338 SQRESULT sq_stackinfos(HSQUIRRELVM v,SQInteger level,SQStackInfos *si);
339 void sq_setdebughook(HSQUIRRELVM v);
341 /*UTILITY MACRO*/
342 #define sq_isnumeric(o) ((o)._type&SQOBJECT_NUMERIC)
343 #define sq_istable(o) ((o)._type==OT_TABLE)
344 #define sq_isarray(o) ((o)._type==OT_ARRAY)
345 #define sq_isfunction(o) ((o)._type==OT_FUNCPROTO)
346 #define sq_isclosure(o) ((o)._type==OT_CLOSURE)
347 #define sq_isgenerator(o) ((o)._type==OT_GENERATOR)
348 #define sq_isnativeclosure(o) ((o)._type==OT_NATIVECLOSURE)
349 #define sq_isstring(o) ((o)._type==OT_STRING)
350 #define sq_isinteger(o) ((o)._type==OT_INTEGER)
351 #define sq_isfloat(o) ((o)._type==OT_FLOAT)
352 #define sq_isuserpointer(o) ((o)._type==OT_USERPOINTER)
353 #define sq_isuserdata(o) ((o)._type==OT_USERDATA)
354 #define sq_isthread(o) ((o)._type==OT_THREAD)
355 #define sq_isnull(o) ((o)._type==OT_NULL)
356 #define sq_isclass(o) ((o)._type==OT_CLASS)
357 #define sq_isinstance(o) ((o)._type==OT_INSTANCE)
358 #define sq_isbool(o) ((o)._type==OT_BOOL)
359 #define sq_isweakref(o) ((o)._type==OT_WEAKREF)
360 #define sq_type(o) ((o)._type)
362 /* deprecated */
363 #define sq_createslot(v,n) sq_newslot(v,n,SQFalse)
365 #define SQ_OK (0)
366 #define SQ_ERROR (-1)
368 #define SQ_FAILED(res) (res<0)
369 #define SQ_SUCCEEDED(res) (res>=0)
371 #endif /*_SQUIRREL_H_*/