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