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