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;
41 [CCode (cheader_filename = "lauxlib.h", has_type_id = false)]
44 [CCode (cname = "LUA_REFNIL")]
46 [CCode (cname = "LUA_NOREF")]
52 [CCode (cheader_filename = "lua.h")]
53 namespace PseudoIndex {
54 [CCode (cname = "LUA_REGISTRYINDEX")]
55 public const int REGISTRY;
56 [CCode (cname = "LUA_ENVIRONINDEX")]
57 public const int ENVIRONMENT;
58 [CCode (cname = "LUA_GLOBALSINDEX")]
59 public const int GLOBALS;
60 [CCode (cname = "lua_upvalueindex")]
61 public int for_upvalue (int i);
66 [CCode (cprefix = "LUA_", cname = "int", has_type_id = false)]
67 public enum ThreadStatus {
77 [CCode (cprefix = "LUA_T", cname = "int", has_type_id = false)]
91 [CCode (cprefix = "LUA_GC", cname = "int", has_type_id = false)]
92 public enum GarbageCollection {
99 [CCode (cname = "LUA_GCSETPAUSE")]
101 [CCode (cname = "LUA_GCSETSTEPMUL")]
107 [CCode (cprefix = "LUA_HOOK", cname = "int", has_type_id = false)]
108 public enum EventHook {
119 [CCode (cprefix = "LUA_MASK", cname = "int", has_type_id = false)]
120 public enum EventMask {
127 // minimum Lua stack available to a C function
128 public const int MINSTACK;
131 /* Function prototypes */
133 [CCode (cname = "lua_CFunction", has_target = false)]
134 public delegate int CallbackFunc (LuaVM vm);
136 // functions that read/write blocks when loading/dumping Lua chunks
138 // NOTE - implicitly passed: user data (instance) as 2rd parameter
139 // implicitly returned: resulting array length as out parameter (3rd)
140 [CCode (cname = "lua_Reader", instance_pos = 1.1)]
141 public delegate char[] ReaderFunc (LuaVM vm);
143 // NOTE - implicitly passed: array length as 3rd parameter,
144 // user data (instance) as last parameter
145 [CCode (cname = "lua_Writer", instance_pos = -1)]
146 public delegate int WriterFunc (LuaVM vm, char[] p);
148 // prototype for memory-allocation functions
149 // NOTE - implicitly passed: user data (instance) as 1st parameter
150 [CCode (cname = "lua_Alloc", instance_pos = 0.9)]
151 public delegate void* AllocFunc (void* ptr, size_t osize, size_t nsize);
153 // Function to be called by the debuger in specific events
154 [CCode (cname = "lua_Hook", has_target = false)]
155 public delegate void HookFunc (LuaVM vm, ref Debug ar);
158 [CCode (cname = "lua_Debug", has_type_id = false)]
159 public struct Debug {
160 public EventHook event;
161 public unowned string name;
162 [CCode (cname = "namewhat")]
163 public unowned string name_what;
164 public unowned string what;
165 public unowned string source;
166 [CCode (cname = "currentline")]
167 public int current_line;
169 [CCode (cname = "linedefined")]
170 public int line_defined;
171 [CCode (cname = "lastlinedefined")]
172 public int last_line_defined;
176 [CCode (free_function = "lua_close", cname = "lua_State", cprefix = "lua_")]
179 // state manipulation
181 [CCode (cname = "(lua_State*) luaL_newstate")]
183 [CCode (cname = "lua_newstate")]
184 // NOTE: user data (instance) gets passed implicitly
185 public LuaVM.with_alloc_func (AllocFunc f);
186 [CCode (cname = "lua_newthread")]
187 public unowned LuaVM new_thread ();
189 [CCode (cname = "lua_atpanic")]
190 public CallbackFunc at_panic (CallbackFunc f);
192 // basic stack manipulation
194 [CCode (cname = "lua_gettop")]
195 public int get_top ();
196 [CCode (cname = "lua_settop")]
197 public void set_top (int index);
198 [CCode (cname = "lua_pushvalue")]
199 public void push_value (int index);
200 public void remove (int index);
201 public void insert (int index);
202 public void replace (int index);
203 [CCode (cname = "lua_checkstack")]
204 public bool check_stack (int size);
206 public static void xmove (LuaVM from, LuaVM to, int n);
208 // access functions (stack -> C)
210 [CCode (cname = "lua_isnumber")]
211 public bool is_number (int index);
212 [CCode (cname = "lua_isstring")]
213 public bool is_string (int index);
214 [CCode (cname = "lua_iscfunction")]
215 public bool is_cfunction (int index);
216 [CCode (cname = "lua_isuserdata")]
217 public bool is_userdata (int index);
218 public Lua.Type type (int index);
219 [CCode (cname = "lua_typename")]
220 public unowned string type_name (Lua.Type type);
222 public bool equal (int index1, int index2);
223 [CCode (cname = "lua_rawequal")]
224 public bool raw_equal (int index1, int index2);
225 [CCode (cname = "lua_lessthan")]
226 public bool less_than (int index1, int index2);
228 [CCode (cname = "lua_tonumber")]
229 public double to_number (int index);
230 [CCode (cname = "lua_tointeger")]
231 public int to_integer (int index);
232 [CCode (cname = "lua_toboolean")]
233 public bool to_boolean (int index);
234 [CCode (cname = "lua_tolstring")]
235 public unowned string to_lstring (int index, out size_t size);
236 [CCode (cname = "lua_tocfunction")]
237 public CallbackFunc to_cfunction (int index);
238 [CCode (cname = "lua_touserdata")]
239 public void* to_userdata (int index);
240 [CCode (cname = "lua_tothread")]
241 public unowned LuaVM? to_thread (int index);
242 [CCode (cname = "lua_topointer")]
243 public /* const */ void* to_pointer (int index);
245 // push functions (C -> stack)
246 [CCode (cname = "lua_pushnil")]
247 public void push_nil ();
248 [CCode (cname = "lua_pushnumber")]
249 public void push_number (double n);
250 [CCode (cname = "lua_pushinteger")]
251 public void push_integer (int n);
252 [CCode (cname = "lua_pushlstring")]
253 public void push_lstring (string s, size_t size);
254 [CCode (cname = "lua_pushstring")]
255 public void push_string (string s);
256 [CCode (cname = "lua_pushfstring")]
257 public unowned string push_fstring (string fmt, ...);
258 [CCode (cname = "lua_pushcclosure")]
259 public void push_cclosure (CallbackFunc f, int n);
260 [CCode (cname = "lua_pushboolean")]
261 public void push_boolean (int b);
262 [CCode (cname = "lua_pushlightuserdata")]
263 public void push_lightuserdata (void* p);
264 [CCode (cname = "lua_pushthread")]
265 public bool push_thread ();
267 // get functions (Lua -> stack)
268 [CCode (cname = "lua_gettable")]
269 public void get_table (int index);
270 [CCode (cname = "lua_getfield")]
271 public void get_field (int index, string k);
272 [CCode (cname = "lua_rawget")]
273 public void raw_get (int index);
274 [CCode (cname = "lua_rawgeti")]
275 public void raw_geti (int index, int n);
276 [CCode (cname = "lua_createtable")]
277 public void create_table (int narr, int nrec);
278 [CCode (cname = "lua_newuserdata")]
279 public void* new_userdata (size_t sz);
280 [CCode (cname = "lua_getmetatable")]
281 public int get_metatable (int objindex);
282 [CCode (cname = "lua_getfenv")]
283 public void get_fenv (int index);
285 // set functions (stack -> Lua)
286 [CCode (cname = "lua_settable")]
287 public void set_table (int index);
288 [CCode (cname = "lua_setfield")]
289 public void set_field (int index, string k);
290 [CCode (cname = "lua_rawset")]
291 public void raw_set (int index);
292 [CCode (cname = "lua_rawseti")]
293 public void raw_seti (int index, int n);
294 [CCode (cname = "lua_setmetatable")]
295 public int set_metatable (int objindex);
296 [CCode (cname = "lua_setfenv")]
297 public bool set_fenv (int index);
300 public void call (int nargs = 0, int nresults = 0);
301 public int pcall (int nargs = 0, int nresults = 0, int errfunc = 0);
302 public int cpcall (CallbackFunc fun, void* ud = null);
304 // NOTE: user data (instance) gets passed implicitly
305 public int load (ReaderFunc reader, string chunkname);
306 // NOTE: user data (instance) gets passed implicitly
307 public int dump (WriterFunc writer);
309 // coroutine functions
310 public int yield (int nresults);
311 public int resume (int narg);
312 public int status ();
314 // garbage-collection function and options
316 public int gc (GarbageCollection what, int data);
318 // miscellaneous functions
321 public int next (int index);
322 public void concat (int n);
323 [CCode (cname = "lua_getallocf")]
324 // NOTE: user data (instance) implicitly returned as out parameter
325 public AllocFunc get_alloc_func ();
326 [CCode (cname = "lua_setallocf")]
327 // NOTE: user data (instance) gets passed implicitly
328 public void set_alloc_func (AllocFunc f);
330 // some useful macros
332 public void pop (int n);
333 [CCode (cname = "lua_newtable")]
334 public void new_table ();
335 public void register (string name, CallbackFunc f);
336 [CCode (cname = "lua_pushcfunction")]
337 public void push_cfunction (CallbackFunc f);
338 public size_t strlen (int index);
340 [CCode (cname = "lua_isfunction")]
341 public bool is_function (int n);
342 [CCode (cname = "lua_istable")]
343 public bool is_table (int n);
344 [CCode (cname = "lua_islightuserdata")]
345 public bool is_lightuserdata (int n);
346 [CCode (cname = "lua_isnil")]
347 public bool is_nil (int n);
348 [CCode (cname = "lua_isboolean")]
349 public bool is_boolean (int n);
350 [CCode (cname = "lua_isthread")]
351 public bool is_thread (int n);
352 [CCode (cname = "lua_isnone")]
353 public bool is_none (int n);
354 [CCode (cname = "lua_isnoneornil")]
355 public bool is_none_or_nil (int n);
357 [CCode (cname = "lua_pushliteral")]
358 public void push_literal (string s);
360 [CCode (cname = "lua_setglobal")]
361 public void set_global (string name);
362 [CCode (cname = "lua_getglobal")]
363 public void get_global (string name);
365 [CCode (cname = "lua_tostring")]
366 public unowned string to_string (int index);
370 [CCode (cname = "lua_getstack")]
371 public bool get_stack (int level, ref Debug ar);
372 [CCode (cname = "lua_getinfo")]
373 public bool get_info (string what, ref Debug ar);
374 [CCode (cname = "lua_getlocal")]
375 public unowned string? get_local (ref Debug ar, int n);
376 [CCode (cname = "lua_setlocal")]
377 public unowned string? set_local (ref Debug ar, int n);
378 [CCode (cname = "lua_getupvalue")]
379 public unowned string? get_upvalue (int funcindex, int n);
380 [CCode (cname = "lua_setupvalue")]
381 public unowned string? set_upvalue (int funcindex, int n);
383 [CCode (cname = "lua_sethook")]
384 public bool set_hook (HookFunc func, EventMask mask, int count);
385 [CCode (cname = "lua_gethook")]
386 public HookFunc get_hook ();
387 [CCode (cname = "lua_gethookmask")]
388 public EventMask get_hook_mask ();
389 [CCode (cname = "lua_gethookcount")]
390 public int get_hook_count ();
392 // Auxiliary library functions
394 [CCode (cname = "luaL_openlibs")]
395 public void open_libs ();
396 [CCode (cname = "luaL_loadbuffer")]
397 public int load_buffer (char[] buffer, string? name = null);
398 [CCode (cname = "luaL_loadstring")]
399 public int load_string (string s);
400 [CCode (cname = "luaL_loadfile", cheader_filename = "lauxlib.h")]
401 public int load_file (string filename);
402 [CCode (cname = "luaL_dofile", cheader_filename = "lauxlib.h")]
403 public bool do_file (string filename);
404 [CCode (cname = "luaL_dostring", cheader_filename = "lauxlib.h")]
405 public bool do_string (string str);
406 [CCode (cname = "luaL_ref", cheader_filename = "lauxlib.h")]
407 public int reference (int t);
408 [CCode (cname = "luaL_unref", cheader_filename = "lauxlib.h")]
409 public void unreference (int t);