package.d 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // Copyright 2019 - 2021 Michael D. Parker
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. module bindbc.lua.v53;
  6. version(LUA_53):
  7. public import bindbc.lua.v53.types;
  8. version(BindBC_Static) version = BindLua_Static;
  9. version(BindLua_Static) {
  10. public import bindbc.lua.v53.bindstatic;
  11. }
  12. else public import bindbc.lua.v53.binddynamic;
  13. import core.stdc.config : c_long;
  14. // compatibility function aliases
  15. // luaconf.h
  16. alias lua_strlen = lua_rawlen;
  17. alias lua_objlen = lua_rawlen;
  18. // Macros
  19. @nogc nothrow {
  20. // luaconf.h
  21. int lua_equal(lua_State* L, int idx1, int idx2) {
  22. pragma(inline, true)
  23. return lua_compare(L, idx1, idx2, LUA_OPEQ);
  24. }
  25. int lua_lessthan(lua_State* L, int idx1, int idx2) {
  26. pragma(inline, true)
  27. return lua_compare(L, idx1, idx2, LUA_OPLT);
  28. }
  29. // lauxlib.h
  30. void luaL_checkversion(lua_State* L) {
  31. pragma(inline, true)
  32. luaL_checkversion_(L, LUA_VERSION_NUM, LUAL_NUMSIZES);
  33. }
  34. int luaL_loadfile(lua_State* L, const(char)* filename) {
  35. pragma(inline, true)
  36. return luaL_loadfilex(L, filename, null);
  37. }
  38. void luaL_newlibtable(lua_State* L, const(luaL_Reg)[] l) {
  39. pragma(inline, true)
  40. lua_createtable(L, 0, cast(int)l.length - 1);
  41. }
  42. void luaL_newlib(lua_State* L, const(luaL_Reg)[] l) {
  43. pragma(inline, true)
  44. luaL_newlibtable(L, l);
  45. luaL_setfuncs(L, l.ptr, 0);
  46. }
  47. void luaL_argcheck(lua_State* L, bool cond, int arg, const(char)* extramsg) {
  48. pragma(inline, true)
  49. if(!cond) luaL_argerror(L, arg, extramsg);
  50. }
  51. const(char)* luaL_checkstring(lua_State* L, int arg) {
  52. pragma(inline, true)
  53. return luaL_checklstring(L, arg, null);
  54. }
  55. const(char)* luaL_optstring(lua_State* L, int arg, const(char)* d) {
  56. pragma(inline, true)
  57. return luaL_optlstring(L, arg, d, null);
  58. }
  59. const(char)* luaL_typename(lua_State* L, int i) {
  60. pragma(inline, true)
  61. return lua_typename(L, lua_type(L, i));
  62. }
  63. bool luaL_dofile(lua_State* L, const(char)* filename) {
  64. pragma(inline, true)
  65. return luaL_loadfile(L, filename) != 0 || lua_pcall(L, 0, LUA_MULTRET, 0) != 0;
  66. }
  67. bool luaL_dostring(lua_State* L, const(char)* str) {
  68. pragma(inline, true)
  69. return luaL_loadstring(L, str) != 0 || lua_pcall(L, 0, LUA_MULTRET, 0) != 0;
  70. }
  71. void luaL_getmetatable(lua_State* L, const(char)* tname) {
  72. pragma(inline, true)
  73. lua_getfield(L, LUA_REGISTRYINDEX, tname);
  74. }
  75. // TODO: figure out what luaL_opt is supposed to do
  76. int luaL_loadbuffer(lua_State *L, const(char)* buff, size_t sz, const(char)* name) {
  77. pragma(inline, true)
  78. return luaL_loadbufferx(L, buff, sz, name, null);
  79. }
  80. void luaL_addchar(luaL_Buffer* B, char c) {
  81. pragma(inline, true)
  82. if(B.n < B.size || luaL_prepbuffsize(B, 1)) {
  83. B.b[B.n++] = c;
  84. }
  85. }
  86. void luaL_addsize(luaL_Buffer* B, size_t s) {
  87. pragma(inline, true)
  88. B.n += s;
  89. }
  90. char* luaL_prepbuffer(luaL_Buffer* B) {
  91. pragma(inline, true)
  92. return luaL_prepbuffsize(B, LUAL_BUFFERSIZE);
  93. }
  94. lua_Unsigned luaL_checkunsigned(lua_State* L, int a) {
  95. pragma(inline, true)
  96. return cast(lua_Unsigned)luaL_checkinteger(L, a);
  97. }
  98. lua_Unsigned luaL_optunsigned(lua_State* L, int a, lua_Unsigned d) {
  99. pragma(inline, true)
  100. return cast(lua_Unsigned)luaL_optinteger(L, a, d);
  101. }
  102. int luaL_checkint(lua_State* L, int a) {
  103. pragma(inline, true)
  104. return cast(int)luaL_checkinteger(L, a);
  105. }
  106. int luaL_optint(lua_State* L, int a, int d) {
  107. pragma(inline, true)
  108. return cast(int)luaL_optinteger(L, a, d);
  109. }
  110. c_long luaL_checklong(lua_State* L, int a) {
  111. pragma(inline, true)
  112. return cast(c_long)luaL_checkinteger(L, a);
  113. }
  114. c_long luaL_optlong(lua_State* L, int a, int d) {
  115. pragma(inline, true)
  116. return cast(c_long)luaL_optinteger(L, a, d);
  117. }
  118. // lua.h
  119. int lua_upvalueindex(int i) {
  120. pragma(inline, true)
  121. return LUA_REGISTRYINDEX - i;
  122. }
  123. void lua_call(lua_State* L, int n, int r) {
  124. pragma(inline, true)
  125. lua_callk(L, n, r, 0, null);
  126. }
  127. int lua_pcall(lua_State* L, int n, int r, int f) {
  128. pragma(inline, true)
  129. return lua_pcallk(L, n, r, f, 0, null);
  130. }
  131. int lua_yield(lua_State* L, int n) {
  132. pragma(inline, true)
  133. return lua_yieldk(L, n, 0, null);
  134. }
  135. void* lua_getextraspace(lua_State* L) {
  136. pragma(inline, true)
  137. return cast(void*)((cast(char*)L) - LUA_EXTRASPACE);
  138. }
  139. lua_Number lua_tonumber(lua_State* L, int i) {
  140. pragma(inline, true)
  141. return lua_tonumberx(L, i, null);
  142. }
  143. lua_Integer lua_tointeger(lua_State* L, int i) {
  144. pragma(inline, true)
  145. return lua_tointegerx(L, i, null);
  146. }
  147. void lua_pop(lua_State* L, int n) {
  148. pragma(inline, true)
  149. lua_settop(L, -n - 1);
  150. }
  151. void lua_newtable(lua_State* L) {
  152. pragma(inline, true)
  153. lua_createtable(L, 0, 0);
  154. }
  155. void lua_register(lua_State* L, const(char)* n, lua_CFunction f) {
  156. pragma(inline, true)
  157. lua_pushcfunction(L, f);
  158. lua_setglobal(L, n);
  159. }
  160. void lua_pushcfunction(lua_State* L, lua_CFunction f) {
  161. pragma(inline, true)
  162. lua_pushcclosure(L, f, 0);
  163. }
  164. bool lua_isfunction(lua_State* L, int n) {
  165. pragma(inline, true)
  166. return lua_type(L, n) == LUA_TFUNCTION;
  167. }
  168. bool lua_istable(lua_State* L, int n) {
  169. pragma(inline, true)
  170. return lua_type(L, n) == LUA_TTABLE;
  171. }
  172. bool lua_islightuserdata(lua_State* L, int n) {
  173. pragma(inline, true)
  174. return lua_type(L, n) == LUA_TLIGHTUSERDATA;
  175. }
  176. bool lua_isnil(lua_State* L, int n) {
  177. pragma(inline, true)
  178. return lua_type(L, n) == LUA_TNIL;
  179. }
  180. bool lua_isboolean(lua_State* L, int n) {
  181. pragma(inline, true)
  182. return lua_type(L, n) == LUA_TBOOLEAN;
  183. }
  184. bool lua_isthread(lua_State* L, int n) {
  185. pragma(inline, true)
  186. return lua_type(L, n) == LUA_TTHREAD;
  187. }
  188. bool lua_isnone(lua_State* L, int n) {
  189. pragma(inline, true)
  190. return lua_type(L, n) == LUA_TNONE;
  191. }
  192. bool lua_isnoneornil(lua_State* L, int n) {
  193. pragma(inline, true)
  194. return lua_type(L, n) <= 0;
  195. }
  196. void lua_pushliteral(lua_State* L, const(char)[] s) {
  197. pragma(inline, true)
  198. lua_pushlstring(L, s.ptr, s.length);
  199. }
  200. void lua_pushglobaltable(lua_State* L) {
  201. pragma(inline, true)
  202. lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS);
  203. }
  204. const(char)* lua_tostring(lua_State* L, int i) {
  205. pragma(inline, true)
  206. return lua_tolstring(L, i, null);
  207. }
  208. void lua_insert(lua_State* L, int idx) {
  209. pragma(inline, true)
  210. lua_rotate(L, idx, 1);
  211. }
  212. void lua_remove(lua_State* L, int idx) {
  213. pragma(inline, true)
  214. lua_rotate(L, idx, -1);
  215. lua_pop(L, 1);
  216. }
  217. void lua_replace(lua_State* L, int idx) {
  218. pragma(inline, true)
  219. lua_copy(L, -1, idx);
  220. lua_pop(L, 1);
  221. }
  222. void lua_pushunsigned(lua_State* L, lua_Unsigned n) {
  223. pragma(inline, true)
  224. lua_pushinteger(L, cast(lua_Integer)n);
  225. }
  226. lua_Unsigned lua_tounsignedx(lua_State* L, int i, int* pi) {
  227. pragma(inline, true)
  228. return cast(lua_Unsigned)lua_tointegerx(L, i, pi);
  229. }
  230. lua_Unsigned lua_tounsigned(lua_State* L, int i) {
  231. pragma(inline, true)
  232. return lua_tounsignedx(L, i, null);
  233. }
  234. }