PageRenderTime 93ms CodeModel.GetById 24ms app.highlight 64ms RepoModel.GetById 2ms app.codeStats 0ms

/binding/lua.d

http://github.com/wilkie/djehuty
D | 542 lines | 348 code | 112 blank | 82 comment | 13 complexity | 0f42ccf93e9886ab71ce4c4e83f5b106 MD5 | raw file
  1/*
  2 * lua.d
  3 *
  4 * This module implements the bindings to allow
  5 * utilization of the Lua scripting language in a D application.
  6 *
  7 * Author: Dave Wilkinson
  8 * Originated: May 15, 2009
  9 *
 10 */
 11
 12module binding.lua;
 13
 14version(PlatformWindows) {
 15	pragma(lib, `"lua5.1.lib"`);
 16}
 17else {
 18}
 19
 20 // lib: liblua5.1
 21
 22 // Copyright for original set of files is reproduced wholely as follows:
 23
 24 /******************************************************************************
 25 * Copyright (C) 1994-2008 Lua.org, PUC-Rio.  All rights reserved.
 26 *
 27 * Permission is hereby granted, free of charge, to any person obtaining
 28 * a copy of this software and associated documentation files (the
 29 * "Software"), to deal in the Software without restriction, including
 30 * without limitation the rights to use, copy, modify, merge, publish,
 31 * distribute, sublicense, and/or sell copies of the Software, and to
 32 * permit persons to whom the Software is furnished to do so, subject to
 33 * the following conditions:
 34 *
 35 * The above copyright notice and this permission notice shall be
 36 * included in all copies or substantial portions of the Software.
 37 *
 38 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 39 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 40 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 41 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 42 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 43 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 44 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 45 ******************************************************************************/
 46
 47
 48// variadic
 49import C = binding.c;
 50
 51const int BUFSIZ = 0x4000; // add (BUFSIZ)
 52alias BUFSIZ LUAL_BUFFERSIZE;
 53
 54// configuration (luaconf.h)
 55
 56const char[] LUA_PATH	= "LUA_PATH";
 57const char[] LUA_CPATH	= "LUA_CPATH";
 58const char[] LUA_INIT	= "LUA_INIT";
 59
 60alias ptrdiff_t LUA_INTEGER;
 61alias double LUA_NUMBER;
 62
 63// ---
 64
 65const char[] LUA_VERSION = "Lua 5.1";
 66const char[] LUA_RELEASE = "Lua 5.1.4";
 67const auto LUA_VERSION_NUM = 501;
 68const char[] LUA_COPYRIGHT = "Copyright (C) 1994-2008 Lua.org, PUC-Rio";
 69const char[] LUA_AUTHORS = "R. Ierusalimschy, L. H. de Figueiredo & W. Celes";
 70
 71// mark for precompiled code (`<esc>Lua')
 72const char[] LUA_SIGNATURE = "\033Lua";
 73
 74// option for multiple returns in `lua_pcall' and `lua_call'
 75const auto LUA_MULTRET = (-1);
 76
 77// pseudo-indices
 78const auto LUA_REGISTRYINDEX	= (-10000);
 79const auto LUA_ENVIRONINDEX		= (-10001);
 80const auto LUA_GLOBALSINDEX		= (-10002);
 81int lua_upvalueindex(int i) {
 82	return LUA_GLOBALSINDEX-(i);
 83}
 84
 85// thread status; 0 is OK
 86const auto LUA_YIELD		= 1;
 87const auto LUA_ERRRUN		= 2;
 88const auto LUA_ERRSYNTAX	= 3;
 89const auto LUA_ERRMEM		= 4;
 90const auto LUA_ERRERR		= 5;
 91
 92alias void* lua_State;
 93
 94typedef int function(lua_State *L) lua_CFunction;
 95
 96// functions that read/write blocks when loading/dumping Lua chunks
 97typedef char* function(lua_State *L, void *ud, size_t *sz) lua_Reader;
 98typedef int function(lua_State *L, void* p, size_t sz, void* ud) lua_Writer;
 99
100// prototype for memory-allocation functions
101typedef void* function(void* ud, void* ptr, size_t osize, size_t nsize) lua_Alloc;
102
103// basic types
104enum : int {
105	LUA_TNONE = -1,
106	LUA_TNIL,
107	LUA_TBOOLEAN,
108	LUA_TLIGHTUSERDATA,
109	LUA_TNUMBER,
110	LUA_TSTRING,
111	LUA_TTABLE,
112	LUA_TFUNCTION,
113	LUA_TUSERDATA,
114	LUA_TTHREAD
115}
116
117// garbage collection options
118enum : int {
119	LUA_GCSTOP,
120	LUA_GCRESTART,
121	LUA_GCCOLLECT,
122	LUA_GCCOUNT,
123	LUA_GCCOUNTB,
124	LUA_GCSTEP,
125	LUA_GCSETPAUSE,
126	LUA_GCSETSTEPMUL
127}
128
129// minimum Lua stack available to a C function
130const auto LUA_MINSTACK	= 20;
131
132// type of numbers in Lua
133alias LUA_NUMBER lua_Number;
134
135// type for integer functions
136alias LUA_INTEGER lua_Integer;
137
138extern(System) {
139
140	// state manipulation
141	lua_State*		lua_newstate(lua_Alloc f, void* ud);
142	void			lua_close(lua_State* L);
143	lua_State*		lua_newthread(lua_State* L);
144
145	lua_CFunction	lua_atpanic(lua_State* L, lua_CFunction panicf);
146
147	// basic stack manipulation
148	int				lua_gettop(lua_State* L);
149	void			lua_settop(lua_State* L, int idx);
150	void			lua_pushvalue(lua_State* L, int idx);
151	void			lua_remove(lua_State* L, int idx);
152	void			lua_insert(lua_State* L, int idx);
153	void			lua_replace(lua_State* L, int idx);
154	int				lua_checkstack(lua_State* L, int sz);
155
156	void			lua_xmove(lua_State* from, lua_State* to, int n);
157
158	// access functions (stack -> C)
159	int				lua_isnumber(lua_State* L, int idx);
160	int				lua_isstring(lua_State* L, int idx);
161	int				lua_iscfunction(lua_State* L, int idx);
162	int				lua_isuserdata(lua_State* L, int idx);
163	int				lua_type(lua_State* L, int idx);
164	char*			lua_typename(lua_State* L, int tp);
165
166	int				lua_equal(lua_State* L, int idx1, int idx2);
167	int				lua_rawequal(lua_State* L, int idx1, int idx2);
168	int				lua_lessthan(lua_State* L, int idx1, int idx2);
169
170	lua_Number		lua_tonumber(lua_State* L, int idx);
171	lua_Integer		lua_tointeger(lua_State* L, int idx);
172	int				lua_toboolean(lua_State* L, int idx);
173	char*			lua_tolstring(lua_State* L, int idx, size_t *len);
174	size_t			lua_objlen(lua_State* L, int idx);
175	lua_CFunction	lua_tocfunction(lua_State* L, int idx);
176	void*			lua_touserdata(lua_State* L, int idx);
177	lua_State*		lua_tothread(lua_State* L, int idx);
178	void*			lua_topointer(lua_State* L, int idx);
179
180	// push functions (C -> stack)
181	void			lua_pushnil(lua_State* L);
182	void			lua_pushnumber(lua_State* L, lua_Number n);
183	void			lua_pushinteger(lua_State* L, lua_Integer n);
184	void			lua_pushlstring(lua_State* L, char* s, size_t l);
185	void			lua_pushstring(lua_State* L, char* s);
186	char*			lua_pushvfstring(lua_State* L, char* fmt, C.va_list argp);
187	char*			lua_pushfstring(lua_State* L, char* fmt, ...);
188	void			lua_pushcclosure(lua_State* L, lua_CFunction fn, int n);
189	void			lua_pushboolean(lua_State* L, int b);
190	void			lua_pushlightuserdata(lua_State* L, void* p);
191	int				lua_pushthread(lua_State *L);
192
193	// get functions (Lua -> stack)
194	void			lua_gettable(lua_State *L, int idx);
195	void			lua_getfield(lua_State *L, int idx, char* k);
196	void			lua_rawget(lua_State *L, int idx);
197	void			lua_rawgeti(lua_State *L, int idx, int n);
198	void			lua_createtable(lua_State *L, int narr, int nrec);
199	void*			lua_newuserdata(lua_State *L, size_t sz);
200	int				lua_getmetatable(lua_State *L, int objindex);
201	void			lua_getfenv(lua_State *L, int idx);
202
203	// set funcctions (stack -> Lua)
204	void			lua_settable(lua_State *L, int idx);
205	void			lua_setfield(lua_State *L, int idx, char* k);
206	void			lua_rawset(lua_State *L, int idx);
207	void			lua_rawseti(lua_State *L, int idx, int n);
208	int				lua_setmetatable(lua_State *L, int objindex);
209	int				lua_setfenv(lua_State *L, int idx);
210
211	// load and call functions (load and run Lua code)
212	void			lua_call(lua_State *L, int nargs, int nresults);
213	int				lua_pcall(lua_State *L, int nargs, int nresults, int errfunc);
214	int				lua_cpcall(lua_State *L, lua_CFunction func, void* ud);
215	int				lua_load(lua_State *L, lua_Reader reader, void* dt, char* chunkname);
216
217	int				lua_dump(lua_State *L, lua_Writer writer, void* data);
218
219	// coroutine functions
220	int				lua_yield(lua_State *L, int nresults);
221	int				lua_resume(lua_State *L, int narg);
222	int				lua_status(lua_State *L);
223
224	// garbage-collection function and options
225	int				lua_gc(lua_State* L, int what, int data);
226
227	// miscellaneous functions
228	int				lua_error(lua_State *L);
229	int				lua_next(lua_State *L, int idx);
230
231	void			lua_concat(lua_State *L, int n);
232
233	lua_Alloc		lua_getallocf(lua_State *L, void** ud);
234	void			lua_setallocf(lua_State *L, lua_Alloc f, void* ud);
235
236	// hack
237	void			lua_setlevel(lua_State* from, lua_State* to);
238
239}
240
241// -- DEBUG API -- //
242const auto LUA_IDSIZE = 60;
243
244struct lua_Debug {
245	int event;
246	char *name;						/* (n) */
247	char *namewhat;					/* (n) `global', `local', `field', `method' */
248	char *what;						/* (S) `Lua', `C', `main', `tail' */
249	char *source;					/* (S) */
250	int currentline;				/* (l) */
251	int nups;						/* (u) number of upvalues */
252	int linedefined;				/* (S) */
253	int lastlinedefined;			/* (S) */
254	char short_src[LUA_IDSIZE];		/* (S) */
255
256	/* private part */
257	int i_ci;						/* active function */
258}
259
260// Event codes
261enum : int {
262	LUA_HOOKCALL,
263	LUA_HOOKRET,
264	LUA_HOOKLINE,
265	LUA_HOOKCOUNT,
266	LUA_HOOKTAILRET
267}
268
269// Event masks
270enum : int {
271	LUA_MASKCALL	= (1 << LUA_HOOKCALL),
272	LUA_MASKRET		= (1 << LUA_HOOKRET),
273	LUA_MASKLINE	= (1 << LUA_HOOKLINE),
274	LUA_MASKCOUNT	= (1 << LUA_HOOKCOUNT)
275}
276
277// Functions to be called by the debuger in specific events
278typedef void function(lua_State* L, lua_Debug* ar) lua_Hook;
279
280extern(System) {
281
282	int				lua_getstack(lua_State *L, int level, lua_Debug *ar);
283	int				lua_getinfo(lua_State *L, char *what, lua_Debug *ar);
284	char*			lua_getlocal(lua_State *L, lua_Debug *ar, int n);
285	char*			lua_setlocal(lua_State *L, lua_Debug *ar, int n);
286	char*			lua_getupvalue(lua_State *L, int funcindex, int n);
287	char*			lua_setupvalue(lua_State *L, int funcindex, int n);
288
289	int				lua_sethook(lua_State *L, lua_Hook func, int mask, int count);
290	lua_Hook		lua_gethook(lua_State *L);
291	int				lua_gethookmask(lua_State *L);
292	int				lua_gethookcount(lua_State *L);
293
294}
295
296// -- USEFUL MACROS -- //
297
298void lua_newtable(lua_State* L) {
299	lua_createtable(L, 0, 0);
300}
301
302/*void lua_register(lua_State* L, idx n, idx f) {
303	lua_pushcfunction(L, f);
304	lua_setglobal(L, (n));
305}*/
306
307//#define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n)))
308
309void lua_pushcfunction(lua_State* L, lua_CFunction f) {
310	lua_pushcclosure(L, f, 0);
311}
312
313size_t lua_strlen(lua_State* L, int idx) {
314	return lua_objlen(L, idx);
315}
316
317void lua_pop(lua_State* L, int idx) {
318//	Console.putln("good", idx);
319	lua_settop(L, 0);
320	//Console.putln("what?");
321}
322
323bool lua_isfunction(lua_State* L, int idx) {
324	return lua_type(L, idx) == LUA_TFUNCTION;
325}
326
327bool lua_istable(lua_State* L, int idx) {
328	return lua_type(L, idx) == LUA_TTABLE;
329}
330
331bool lua_islightuserdata(lua_State* L, int idx) {
332	return lua_type(L, idx) == LUA_TLIGHTUSERDATA;
333}
334
335bool lua_isnil(lua_State* L, int idx) {
336	return lua_type(L, idx) == LUA_TNIL;
337}
338
339bool lua_isboolean(lua_State* L, int idx) {
340	return lua_type(L, idx) == LUA_TBOOLEAN;
341}
342
343bool lua_isthread(lua_State* L, int idx) {
344	return lua_type(L, idx) == LUA_TTHREAD;
345}
346
347bool lua_isnone(lua_State* L, int idx) {
348	return lua_type(L, idx) == LUA_TNONE;
349}
350
351bool lua_isnoneornil(lua_State* L, int idx) {
352	return lua_type(L, idx) <= 0;
353}
354
355void lua_pushliteral(lua_State* L, char* s) {
356	int len;
357	char* ptr = s;
358	while(*ptr != '\0') {
359		len++;
360		ptr++;
361	}
362	lua_pushlstring(L, s, len);
363}
364
365void lua_setglobal(lua_State* L, char* s) {
366	lua_setfield(L, LUA_GLOBALSINDEX, s);
367}
368
369void lua_getglobal(lua_State* L, char* s) {
370	lua_setfield(L, LUA_GLOBALSINDEX, s);
371}
372
373char* lua_tostring(lua_State* L, int idx) {
374	size_t bugger;
375	return lua_tolstring(L, idx, &bugger);
376}
377
378// -- LUA LIB (lualib.h) -- //
379
380const char[] LUA_COLIBNAME		= "coroutine";
381const char[] LUA_TABLIBNAME		= "table";
382const char[] LUA_IOLIBNAME		= "io";
383const char[] LUA_OSLIBNAME		= "os";
384const char[] LUA_STRLIBNAME		= "string";
385const char[] LUA_MATHLIBNAME	= "math";
386const char[] LUA_DBLIBNAME		= "debug";
387const char[] LUA_LOADLIBNAME	= "package";
388
389extern(System) {
390	int	luaopen_base(lua_State *L);
391	int luaopen_table(lua_State *L);
392	int luaopen_io(lua_State *L);
393	int luaopen_os(lua_State *L);
394	int luaopen_string(lua_State *L);
395	int luaopen_math(lua_State *L);
396	int luaopen_debug(lua_State *L);
397	int luaopen_package(lua_State *L);
398
399	// open all previous libraries
400	void luaL_openlibs(lua_State *L);
401}
402
403// -- LUA AUXILIARY LIB (lauxlib.h) -- //
404
405struct luaL_Reg {
406	char *name;
407	lua_CFunction func;
408}
409
410extern(System) {
411	void		luaI_openlib(lua_State *L, char *libname,
412	                                luaL_Reg *l, int nup);
413	void		luaL_register(lua_State *L, char *libname,
414	                                luaL_Reg *l);
415	int			luaL_getmetafield(lua_State *L, int obj, char *e);
416	int			luaL_callmeta(lua_State *L, int obj, char *e);
417	int			luaL_typerror(lua_State *L, int narg, char *tname);
418	int			luaL_argerror(lua_State *L, int numarg, char *extramsg);
419	char*		luaL_checklstring(lua_State *L, int numArg,
420	                                                          size_t *l);
421	char*		luaL_optlstring(lua_State *L, int numArg,
422	                                          char *def, size_t *l);
423	lua_Number	luaL_checknumber(lua_State *L, int numArg);
424	lua_Number	luaL_optnumber(lua_State *L, int nArg, lua_Number def);
425
426	lua_Integer	luaL_checkinteger(lua_State *L, int numArg);
427	lua_Integer	luaL_optinteger(lua_State *L, int nArg,
428	                                          lua_Integer def);
429
430	void		luaL_checkstack(lua_State *L, int sz, char *msg);
431	void		luaL_checktype(lua_State *L, int narg, int t);
432	void		luaL_checkany(lua_State *L, int narg);
433
434	int			luaL_newmetatable(lua_State *L, char *tname);
435	void*		luaL_checkudata(lua_State *L, int ud, char *tname);
436
437	void		luaL_where(lua_State *L, int lvl);
438	int			luaL_error(lua_State *L, char *fmt, ...);
439
440	int			luaL_checkoption(lua_State *L, int narg, char *def,
441	                                   char * lst[]);
442
443	int			luaL_ref(lua_State *L, int t);
444	void		luaL_unref(lua_State *L, int t, int rref);
445
446	int			luaL_loadfile(lua_State *L, char *filename);
447	int			luaL_loadbuffer(lua_State *L, char *buff, size_t sz,
448	                                  char *name);
449	int			luaL_loadstring(lua_State *L, char *s);
450
451	lua_State*	luaL_newstate();
452
453
454	char*		luaL_gsub(	lua_State *L, char *s, char *p,
455							char *r);
456
457	char*		luaL_findtable(	lua_State *L, int idx,
458								char *fname, int szhint);
459}
460
461// -- USEFUL MACROS [LAUXLIB] -- //
462
463
464bool luaL_argcheck(lua_State* L, bool cond, int numarg, char* extramsg) {
465	return (cond || luaL_argerror(L, numarg, extramsg));
466}
467
468char* luaL_checkstring(lua_State* L, int numArg) {
469	return luaL_checklstring(L, numArg, null);
470}
471
472char* luaL_optstring(lua_State* L, int numArg, char* def) {
473	return luaL_optlstring(L, numArg, def, null);
474}
475
476int luaL_checkint(lua_State* L, int numArg) {
477	return luaL_checkinteger(L, numArg);
478}
479
480int luaL_optint(lua_State* L, int nArg, lua_Integer def) {
481	return luaL_optinteger(L, nArg, def);
482}
483
484long luaL_checklong(lua_State* L, int nArg) {
485	return luaL_checkinteger(L, nArg);
486}
487
488long luaL_optlong(lua_State* L, int nArg, lua_Integer def) {
489	return luaL_optinteger(L, nArg, def);
490}
491
492char* luaL_typename(lua_State* L, int tp) {
493	return lua_typename(L, lua_type(L, tp));
494}
495
496bool luaL_dofile(lua_State* L, char* filename) {
497	return luaL_loadfile(L, filename) || lua_pcall(L, 0, LUA_MULTRET, 0);
498}
499
500bool luaL_dostring(lua_State* L, char* s) {
501	return luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0);
502}
503
504void luaL_getmetatable(lua_State* L, char* name) {
505	lua_getfield(L, LUA_REGISTRYINDEX, name);
506}
507
508// -- GENERIC BUFFER MANIPULATION
509
510struct luaL_Buffer {
511	char *p;			/* current position in buffer */
512	int lvl;			/* number of strings in the stack (level) */
513	lua_State *L;
514	char buffer[LUAL_BUFFERSIZE];
515}
516
517void luaL_addchar(luaL_Buffer* B, char c) {
518	if (!(B.p < (B.buffer.ptr + LUAL_BUFFERSIZE))) {
519		luaL_prepbuffer(B);
520	}
521	*B.p++ = c;
522}
523/*
524#define luaL_addchar(B,c) \
525	  ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \
526	   (*(B)->p++ = (char)(c)))
527*/
528
529char* luaL_addsize(luaL_Buffer* B, int n) {
530	B.p += n;
531	return B.p;
532}
533
534extern(System) {
535	void	luaL_buffinit(lua_State *L, luaL_Buffer *B);
536	char*	luaL_prepbuffer(luaL_Buffer *B);
537	void	luaL_addlstring(luaL_Buffer *B, char *s, size_t l);
538	void	luaL_addstring(luaL_Buffer *B, char *s);
539	void	luaL_addvalue(luaL_Buffer *B);
540	void	luaL_pushresult(luaL_Buffer *B);
541}
542