package.d 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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.v51;
  6. version(LUA_51):
  7. public import bindbc.lua.v51.types;
  8. version(BindBC_Static) version = BindLua_Static;
  9. version(BindLua_Static) {
  10. public import bindbc.lua.v51.bindstatic;
  11. }
  12. else public import bindbc.lua.v51.binddynamic;
  13. import core.stdc.config : c_long;
  14. // compatibility function aliases
  15. // lua.h
  16. alias lua_strlen = lua_objlen;
  17. alias lua_open = lua_newstate;
  18. // Macros
  19. @nogc nothrow {
  20. // lauxlib.h
  21. void luaL_argcheck(lua_State* L, bool cond, int arg, const(char)* extramsg) {
  22. pragma(inline, true)
  23. if(!cond) luaL_argerror(L, arg, extramsg);
  24. }
  25. const(char)* luaL_checkstring(lua_State* L, int arg) {
  26. pragma(inline, true)
  27. return luaL_checklstring(L, arg, null);
  28. }
  29. const(char)* luaL_optstring(lua_State* L, int arg, const(char)* d) {
  30. pragma(inline, true)
  31. return luaL_optlstring(L, arg, d, null);
  32. }
  33. int luaL_checkint(lua_State* L, int arg) {
  34. pragma(inline, true)
  35. return cast(int)luaL_checkinteger(L, arg);
  36. }
  37. int luaL_optint(lua_State* L, int arg, int d) {
  38. pragma(inline, true)
  39. return cast(int)luaL_optinteger(L, arg, d);
  40. }
  41. c_long luaL_checklong(lua_State* L, int arg) {
  42. pragma(inline, true)
  43. return cast(c_long)luaL_checkinteger(L, arg);
  44. }
  45. c_long luaL_optlong(lua_State* L, int arg, int d) {
  46. pragma(inline, true)
  47. return cast(c_long)luaL_optinteger(L, arg, d);
  48. }
  49. const(char)* luaL_typename(lua_State* L, int i) {
  50. pragma(inline, true)
  51. return lua_typename(L, lua_type(L, i));
  52. }
  53. bool luaL_dofile(lua_State* L, const(char)* filename) {
  54. pragma(inline, true)
  55. return luaL_loadfile(L, filename) != 0 || lua_pcall(L, 0, LUA_MULTRET, 0) != 0;
  56. }
  57. bool luaL_dostring(lua_State* L, const(char)* str) {
  58. pragma(inline, true)
  59. return luaL_loadstring(L, str) != 0 || lua_pcall(L, 0, LUA_MULTRET, 0) != 0;
  60. }
  61. void luaL_getmetatable(lua_State* L, const(char)* tname) {
  62. pragma(inline, true)
  63. lua_getfield(L, LUA_REGISTRYINDEX, tname);
  64. }
  65. // TODO: figure out what luaL_opt is supposed to do
  66. void luaL_addchar(luaL_Buffer* B, char c) {
  67. pragma(inline, true)
  68. if(B.p < (B.buffer.ptr + LUAL_BUFFERSIZE) || luaL_prepbuffer(B)) {
  69. *B.p++ = c;
  70. }
  71. }
  72. void luaL_addsize(luaL_Buffer* B, size_t n) {
  73. pragma(inline, true)
  74. B.p += n;
  75. }
  76. // lua.h
  77. int lua_upvalueindex(int i) {
  78. pragma(inline, true)
  79. return LUA_GLOBALSINDEX - i;
  80. }
  81. void lua_pop(lua_State* L, int n) {
  82. pragma(inline, true)
  83. lua_settop(L, -n - 1);
  84. }
  85. void lua_newtable(lua_State* L) {
  86. pragma(inline, true)
  87. lua_createtable(L, 0, 0);
  88. }
  89. void lua_register(lua_State* L, const(char)* n, lua_CFunction f) {
  90. pragma(inline, true)
  91. lua_pushcfunction(L, f);
  92. lua_setglobal(L, n);
  93. }
  94. void lua_pushcfunction(lua_State* L, lua_CFunction f) {
  95. pragma(inline, true)
  96. lua_pushcclosure(L, f, 0);
  97. }
  98. bool lua_isfunction(lua_State* L, int n) {
  99. pragma(inline, true)
  100. return lua_type(L, n) == LUA_TFUNCTION;
  101. }
  102. bool lua_istable(lua_State* L, int n) {
  103. pragma(inline, true)
  104. return lua_type(L, n) == LUA_TTABLE;
  105. }
  106. bool lua_islightuserdata(lua_State* L, int n) {
  107. pragma(inline, true)
  108. return lua_type(L, n) == LUA_TLIGHTUSERDATA;
  109. }
  110. bool lua_isnil(lua_State* L, int n) {
  111. pragma(inline, true)
  112. return lua_type(L, n) == LUA_TNIL;
  113. }
  114. bool lua_isboolean(lua_State* L, int n) {
  115. pragma(inline, true)
  116. return lua_type(L, n) == LUA_TBOOLEAN;
  117. }
  118. bool lua_isthread(lua_State* L, int n) {
  119. pragma(inline, true)
  120. return lua_type(L, n) == LUA_TTHREAD;
  121. }
  122. bool lua_isnone(lua_State* L, int n) {
  123. pragma(inline, true)
  124. return lua_type(L, n) == LUA_TNONE;
  125. }
  126. bool lua_isnoneornil(lua_State* L, int n) {
  127. pragma(inline, true)
  128. return lua_type(L, n) <= 0;
  129. }
  130. void lua_pushliteral(lua_State* L, const(char)[] s) {
  131. pragma(inline, true)
  132. lua_pushlstring(L, s.ptr, s.length);
  133. }
  134. void lua_setglobal(lua_State* L, const(char)* s) {
  135. pragma(inline, true)
  136. lua_setfield(L, LUA_GLOBALSINDEX, s);
  137. }
  138. void lua_getglobal(lua_State* L, const(char)* s) {
  139. pragma(inline, true)
  140. lua_getfield(L, LUA_GLOBALSINDEX, s);
  141. }
  142. const(char)* lua_tostring(lua_State* L, int i) {
  143. pragma(inline, true)
  144. return lua_tolstring(L, i, null);
  145. }
  146. void lua_getregistry(lua_State* L) {
  147. pragma(inline, true)
  148. lua_pushvalue(L, LUA_REGISTRYINDEX);
  149. }
  150. int lua_getgccount(lua_State* L) {
  151. pragma(inline, true)
  152. return lua_gc(L, LUA_GCCOUNT, 0);
  153. }
  154. }