3 * Copyright (C) 2008-2009 pancake, Frederik
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 * pancake <youterm.com>
24 [CCode (lower_case_cprefix = "lua_", cheader_filename = "lua.h")]
29 public const string VERSION;
30 public const string RELEASE;
31 public const int VERSION_NUM;
32 public const string COPYRIGHT;
33 public const string AUTHORS;
35 // mark for precompiled code (`<esc>Lua')
36 public const string SIGNATURE;
38 // option for multiple returns in `lua_pcall' and `lua_call'
39 public const int MULTRET;
43 [CCode (cheader_filename = "lua.h")]
44 namespace PseudoIndex {
45 [CCode (cname = "LUA_REGISTRYINDEX")]
46 public const int REGISTRY;
47 [CCode (cname = "LUA_ENVIRONINDEX")]
48 public const int ENVIRONMENT;
49 [CCode (cname = "LUA_GLOBALSINDEX")]
50 public const int GLOBALS;
51 [CCode (cname = "lua_upvalueindex")]
52 public int for_upvalue (int i);
57 [CCode (cprefix = "LUA_", cname = "int")]
58 public enum ThreadStatus {
68 [CCode (cprefix = "LUA_T", cname = "int")]
82 [CCode (cprefix = "LUA_GC", cname = "int")]
83 public enum GarbageCollection {
90 [CCode (cname = "LUA_GCSETPAUSE")]
92 [CCode (cname = "LUA_GCSETSTEPMUL")]
98 [CCode (cprefix = "LUA_HOOK", cname = "int")]
99 public enum EventHook {
110 [CCode (cprefix = "LUA_MASK", cname = "int")]
111 public enum EventMask {
118 // minimum Lua stack available to a C function
119 public const int MINSTACK;
122 /* Function prototypes */
124 [CCode (cname = "lua_CFunction")]
125 public static delegate int CallbackFunc (LuaVM vm);
127 // functions that read/write blocks when loading/dumping Lua chunks
129 // NOTE - implicitly passed: user data (instance) as 2rd parameter
130 // implicitly returned: resulting array length as out parameter (3rd)
131 [CCode (cname = "lua_Reader", instance_pos = 1.1)]
132 public delegate char[] ReaderFunc (LuaVM vm);
134 // NOTE - implicitly passed: array length as 3rd parameter,
135 // user data (instance) as last parameter
136 [CCode (cname = "lua_Writer", instance_pos = -1)]
137 public delegate int WriterFunc (LuaVM vm, char[] p);
139 // prototype for memory-allocation functions
140 // NOTE - implicitly passed: user data (instance) as 1st parameter
141 [CCode (cname = "lua_Alloc", instance_pos = 0.9)]
142 public delegate void* AllocFunc (void* ptr, size_t osize, size_t nsize);
144 // Function to be called by the debuger in specific events
145 [CCode (cname = "lua_Hook")]
146 public static delegate void HookFunc (LuaVM vm, ref Debug ar);
149 [CCode (cname = "lua_Debug")]
150 public struct Debug {
151 public EventHook event;
152 public unowned string name;
153 [CCode (cname = "namewhat")]
154 public unowned string name_what;
155 public unowned string what;
156 public unowned string source;
157 [CCode (cname = "currentline")]
158 public int current_line;
160 [CCode (cname = "linedefined")]
161 public int line_defined;
162 [CCode (cname = "lastlinedefined")]
163 public int last_line_defined;
167 [CCode (free_function = "lua_close", cname = "lua_State", cprefix = "lua_")]
170 // state manipulation
172 [CCode (cname = "(lua_State*) luaL_newstate")]
174 [CCode (cname = "lua_newstate")]
175 // NOTE: user data (instance) gets passed implicitly
176 public LuaVM.with_alloc_func (AllocFunc f);
177 [CCode (cname = "lua_newthread")]
178 public unowned LuaVM new_thread ();
180 [CCode (cname = "lua_atpanic")]
181 public CallbackFunc at_panic (CallbackFunc f);
183 // basic stack manipulation
185 [CCode (cname = "lua_gettop")]
186 public int get_top ();
187 [CCode (cname = "lua_settop")]
188 public void set_top (int index);
189 [CCode (cname = "lua_pushvalue")]
190 public void push_value (int index);
191 public void remove (int index);
192 public void insert (int index);
193 public void replace (int index);
194 [CCode (cname = "lua_checkstack")]
195 public bool check_stack (int size);
197 public static void xmove (LuaVM from, LuaVM to, int n);
199 // access functions (stack -> C)
201 [CCode (cname = "lua_isnumber")]
202 public bool is_number (int index);
203 [CCode (cname = "lua_isstring")]
204 public bool is_string (int index);
205 [CCode (cname = "lua_iscfunction")]
206 public bool is_cfunction (int index);
207 [CCode (cname = "lua_isuserdata")]
208 public bool is_userdata (int index);
209 public Lua.Type type (int index);
210 [CCode (cname = "lua_typename")]
211 public unowned string type_name (Lua.Type type);
213 public bool equal (int index1, int index2);
214 [CCode (cname = "lua_rawequal")]
215 public bool raw_equal (int index1, int index2);
216 [CCode (cname = "lua_lessthan")]
217 public bool less_than (int index1, int index2);
219 [CCode (cname = "lua_tonumber")]
220 public double to_number (int index);
221 [CCode (cname = "lua_tointeger")]
222 public int to_integer (int index);
223 [CCode (cname = "lua_toboolean")]
224 public bool to_boolean (int index);
225 [CCode (cname = "lua_tolstring")]
226 public unowned string to_lstring (int index, out size_t size);
227 [CCode (cname = "lua_tocfunction")]
228 public CallbackFunc to_cfunction (int index);
229 [CCode (cname = "lua_touserdata")]
230 public void* to_userdata (int index);
231 [CCode (cname = "lua_tothread")]
232 public unowned LuaVM? to_thread (int index);
233 [CCode (cname = "lua_topointer")]
234 public /* const */ void* to_pointer (int index);
236 // push functions (C -> stack)
237 [CCode (cname = "lua_pushnil")]
238 public void push_nil ();
239 [CCode (cname = "lua_pushnumber")]
240 public void push_number (double n);
241 [CCode (cname = "lua_pushinteger")]
242 public void push_integer (int n);
243 [CCode (cname = "lua_pushlstring")]
244 public void push_lstring (string s, size_t size);
245 [CCode (cname = "lua_pushstring")]
246 public void push_string (string s);
247 [CCode (cname = "lua_pushfstring")]
248 public unowned string push_fstring (string fmt, ...);
249 [CCode (cname = "lua_pushcclosure")]
250 public void push_cclosure (CallbackFunc f, int n);
251 [CCode (cname = "lua_pushboolean")]
252 public void push_boolean (int b);
253 [CCode (cname = "lua_pushlightuserdata")]
254 public void push_lightuserdata (void* p);
255 [CCode (cname = "lua_pushthread")]
256 public bool push_thread ();
258 // get functions (Lua -> stack)
259 [CCode (cname = "lua_gettable")]
260 public void get_table (int index);
261 [CCode (cname = "lua_getfield")]
262 public void get_field (int index, string k);
263 [CCode (cname = "lua_rawget")]
264 public void raw_get (int index);
265 [CCode (cname = "lua_rawgeti")]
266 public void raw_geti (int index, int n);
267 [CCode (cname = "lua_createtable")]
268 public void create_table (int narr, int nrec);
269 [CCode (cname = "lua_newuserdata")]
270 public void* new_userdata (size_t sz);
271 [CCode (cname = "lua_getmetatable")]
272 public int get_metatable (int objindex);
273 [CCode (cname = "lua_getfenv")]
274 public void get_fenv (int index);
276 // set functions (stack -> Lua)
277 [CCode (cname = "lua_settable")]
278 public void set_table (int index);
279 [CCode (cname = "lua_setfield")]
280 public void set_field (int index, string k);
281 [CCode (cname = "lua_rawset")]
282 public void raw_set (int index);
283 [CCode (cname = "lua_rawseti")]
284 public void raw_seti (int index, int n);
285 [CCode (cname = "lua_setmetatable")]
286 public int set_metatable (int objindex);
287 [CCode (cname = "lua_setfenv")]
288 public bool set_fenv (int index);
291 public void call (int nargs = 0, int nresults = 0);
292 public int pcall (int nargs = 0, int nresults = 0, int errfunc = 0);
293 public int cpcall (CallbackFunc fun, void* ud = null);
295 // NOTE: user data (instance) gets passed implicitly
296 public int load (ReaderFunc reader, string chunkname);
297 // NOTE: user data (instance) gets passed implicitly
298 public int dump (WriterFunc writer);
300 // coroutine functions
301 public int yield (int nresults);
302 public int resume (int narg);
303 public int status ();
305 // garbage-collection function and options
307 public int gc (GarbageCollection what, int data);
309 // miscellaneous functions
312 public int next (int index);
313 public void concat (int n);
314 [CCode (cname = "lua_getallocf")]
315 // NOTE: user data (instance) implicitly returned as out parameter
316 public AllocFunc get_alloc_func ();
317 [CCode (cname = "lua_setallocf")]
318 // NOTE: user data (instance) gets passed implicitly
319 public void set_alloc_func (AllocFunc f);
321 // some useful macros
323 public void pop (int n);
324 [CCode (cname = "lua_newtable")]
325 public void new_table ();
326 public void register (string name, CallbackFunc f);
327 [CCode (cname = "lua_pushcfunction")]
328 public void push_cfunction (CallbackFunc f);
329 public size_t strlen (int index);
331 [CCode (cname = "lua_isfunction")]
332 public bool is_function (int n);
333 [CCode (cname = "lua_istable")]
334 public bool is_table (int n);
335 [CCode (cname = "lua_islightuserdata")]
336 public bool is_lightuserdata (int n);
337 [CCode (cname = "lua_isnil")]
338 public bool is_nil (int n);
339 [CCode (cname = "lua_isboolean")]
340 public bool is_boolean (int n);
341 [CCode (cname = "lua_isthread")]
342 public bool is_thread (int n);
343 [CCode (cname = "lua_isnone")]
344 public bool is_none (int n);
345 [CCode (cname = "lua_isnoneornil")]
346 public bool is_none_or_nil (int n);
348 [CCode (cname = "lua_pushliteral")]
349 public void push_literal (string s);
351 [CCode (cname = "lua_setglobal")]
352 public void set_global (string name);
353 [CCode (cname = "lua_getglobal")]
354 public void get_global (string name);
356 [CCode (cname = "lua_tostring")]
357 public unowned string to_string (int index);
361 [CCode (cname = "lua_getstack")]
362 public bool get_stack (int level, ref Debug ar);
363 [CCode (cname = "lua_getinfo")]
364 public bool get_info (string what, ref Debug ar);
365 [CCode (cname = "lua_getlocal")]
366 public unowned string? get_local (ref Debug ar, int n);
367 [CCode (cname = "lua_setlocal")]
368 public unowned string? set_local (ref Debug ar, int n);
369 [CCode (cname = "lua_getupvalue")]
370 public unowned string? get_upvalue (int funcindex, int n);
371 [CCode (cname = "lua_setupvalue")]
372 public unowned string? set_upvalue (int funcindex, int n);
374 [CCode (cname = "lua_sethook")]
375 public bool set_hook (HookFunc func, EventMask mask, int count);
376 [CCode (cname = "lua_gethook")]
377 public HookFunc get_hook ();
378 [CCode (cname = "lua_gethookmask")]
379 public EventMask get_hook_mask ();
380 [CCode (cname = "lua_gethookcount")]
381 public int get_hook_count ();
383 // Auxiliary library functions
385 [CCode (cname = "luaL_openlibs")]
386 public void open_libs ();
387 [CCode (cname = "luaL_loadbuffer")]
388 public int load_buffer (char[] buffer, string? name = null);
389 [CCode (cname = "luaL_loadstring")]
390 public int load_string (string s);
391 [CCode (cname = "luaL_loadfile")]
392 public int load_file (string filename);
393 [CCode (cname = "luaL_dofile", cheader_filename = "lauxlib.h")]
394 public bool do_file (string filename);
395 [CCode (cname = "luaL_dostring", cheader_filename = "lauxlib.h")]
396 public bool do_string (string str);