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.v54;
8 
9 version(LUA_54):
10 
11 public import bindbc.lua.v54.types;
12 
13 version(BindBC_Static) version = BindLua_Static;
14 version(BindLua_Static) {
15     public import bindbc.lua.v54.bindstatic;
16 }
17 else public import bindbc.lua.v54.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     void luaL_argexpected(lua_State* L, bool cond, int arg, const(char)* tname) {
67         pragma(inline, true)
68         if(!cond) luaL_typeerror(L, arg, tname);
69     }
70 
71     const(char)* luaL_checkstring(lua_State* L, int arg) {
72         pragma(inline, true)
73         return luaL_checklstring(L, arg, null);
74     }
75 
76     const(char)* luaL_optstring(lua_State* L, int arg, const(char)* d) {
77         pragma(inline, true)
78         return luaL_optlstring(L, arg, d, null);
79     }
80 
81     const(char)* luaL_typename(lua_State* L, int i) {
82         pragma(inline, true)
83         return lua_typename(L, lua_type(L, i));
84     }
85 
86     bool luaL_dofile(lua_State* L, const(char)* filename) {
87         pragma(inline, true)
88         return luaL_loadfile(L, filename) != 0 || lua_pcall(L, 0, LUA_MULTRET, 0) != 0;
89     }
90 
91     bool luaL_dostring(lua_State* L, const(char)* str) {
92         pragma(inline, true)
93         return luaL_loadstring(L, str) != 0 || lua_pcall(L, 0, LUA_MULTRET, 0) != 0;
94     }
95 
96     void luaL_getmetatable(lua_State* L, const(char)* tname) {
97         pragma(inline, true)
98         lua_getfield(L, LUA_REGISTRYINDEX, tname);
99     }
100 
101     // TODO: figure out what luaL_opt is supposed to do
102 
103     int luaL_loadbuffer(lua_State *L, const(char)* buff, size_t sz, const(char)* name) {
104         pragma(inline, true)
105         return luaL_loadbufferx(L, buff, sz, name, null);
106     }
107 
108     alias luaL_pushfail = lua_pushnil;
109 
110     size_t luaL_bufflen(luaL_Buffer* B) {
111         pragma(inline, true)
112         return B.n;
113     }
114 
115     char* luaL_buffaddr(luaL_Buffer* B) {
116         pragma(inline, true)
117         return B.b;
118     }
119 
120     void luaL_addchar(luaL_Buffer* B, char c) {
121         pragma(inline, true)
122         if(B.n < B.size || luaL_prepbuffsize(B, 1)) {
123             B.b[B.n++] = c;
124         }
125     }
126 
127     void luaL_addsize(luaL_Buffer* B, size_t s) {
128         pragma(inline, true)
129         B.n += s;
130     }
131 
132     void luaL_buffsub(luaL_Buffer* B, size_t s) {
133         pragma(inline, true)
134         B.n -= s;
135     }
136 
137     char* luaL_prepbuffer(luaL_Buffer* B) {
138         pragma(inline, true)
139         return luaL_prepbuffsize(B, LUAL_BUFFERSIZE);
140     }
141 
142     lua_Unsigned luaL_checkunsigned(lua_State* L, int a) {
143         pragma(inline, true)
144         return cast(lua_Unsigned)luaL_checkinteger(L, a);
145     }
146 
147     lua_Unsigned luaL_optunsigned(lua_State* L, int a, lua_Unsigned d) {
148         pragma(inline, true)
149         return cast(lua_Unsigned)luaL_optinteger(L, a, d);
150     }
151 
152     int luaL_checkint(lua_State* L, int a) {
153         pragma(inline, true)
154         return cast(int)luaL_checkinteger(L, a);
155     }
156 
157     int luaL_optint(lua_State* L, int a, int d) {
158         pragma(inline, true)
159         return cast(int)luaL_optinteger(L, a, d);
160     }
161 
162     c_long luaL_checklong(lua_State* L, int a) {
163         pragma(inline, true)
164         return cast(c_long)luaL_checkinteger(L, a);
165     }
166 
167     c_long luaL_optlong(lua_State* L, int a, int d) {
168         pragma(inline, true)
169         return cast(c_long)luaL_optinteger(L, a, d);
170     }
171 
172     // lua.h
173     int lua_upvalueindex(int i) {
174         pragma(inline, true)
175         return LUA_REGISTRYINDEX - i;
176     }
177 
178     void lua_call(lua_State* L, int n, int r) {
179         pragma(inline, true)
180         lua_callk(L, n, r, 0, null);
181     }
182 
183     int lua_pcall(lua_State* L, int n, int r, int f) {
184         pragma(inline, true)
185         return lua_pcallk(L, n, r, f, 0, null);
186     }
187 
188     int lua_yield(lua_State* L, int n) {
189         pragma(inline, true)
190         return lua_yieldk(L, n, 0, null);
191     }
192 
193     void* lua_getextraspace(lua_State* L) {
194         pragma(inline, true)
195         return cast(void*)((cast(char*)L) - LUA_EXTRASPACE);
196     }
197 
198     lua_Number lua_tonumber(lua_State* L, int i) {
199         pragma(inline, true)
200         return lua_tonumberx(L, i, null);
201     }
202 
203     lua_Integer lua_tointeger(lua_State* L, int i) {
204         pragma(inline, true)
205         return lua_tointegerx(L, i, null);
206     }
207 
208     void lua_pop(lua_State* L, int n) {
209         pragma(inline, true)
210         lua_settop(L, -n - 1);
211     }
212 
213     void lua_newtable(lua_State* L) {
214         pragma(inline, true)
215         lua_createtable(L, 0, 0);
216     }
217 
218     void lua_register(lua_State* L, const(char)* n, lua_CFunction f) {
219         pragma(inline, true)
220         lua_pushcfunction(L, f);
221         lua_setglobal(L, n);
222     }
223 
224     void lua_pushcfunction(lua_State* L, lua_CFunction f) {
225         pragma(inline, true)
226         lua_pushcclosure(L, f, 0);
227     }
228 
229     bool lua_isfunction(lua_State* L, int n) {
230         pragma(inline, true)
231         return lua_type(L, n) == LUA_TFUNCTION;
232     }
233 
234     bool lua_istable(lua_State* L, int n) {
235         pragma(inline, true)
236         return lua_type(L, n) == LUA_TTABLE;
237     }
238 
239     bool lua_islightuserdata(lua_State* L, int n) {
240         pragma(inline, true)
241         return lua_type(L, n) == LUA_TLIGHTUSERDATA;
242     }
243 
244     bool lua_isnil(lua_State* L, int n) {
245         pragma(inline, true)
246         return lua_type(L, n) == LUA_TNIL;
247     }
248 
249     bool lua_isboolean(lua_State* L, int n) {
250         pragma(inline, true)
251         return lua_type(L, n) == LUA_TBOOLEAN;
252     }
253 
254     bool lua_isthread(lua_State* L, int n) {
255         pragma(inline, true)
256         return lua_type(L, n) == LUA_TTHREAD;
257     }
258 
259     bool lua_isnone(lua_State* L, int n) {
260         pragma(inline, true)
261         return lua_type(L, n) == LUA_TNONE;
262     }
263 
264     bool lua_isnoneornil(lua_State* L, int n) {
265         pragma(inline, true)
266         return lua_type(L, n) <= 0;
267     }
268 
269     void lua_pushliteral(lua_State* L, const(char)[] s) {
270         pragma(inline, true)
271         lua_pushlstring(L, s.ptr, s.length);
272     }
273 
274     void lua_pushglobaltable(lua_State* L) {
275         pragma(inline, true)
276         lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS);
277     }
278 
279     const(char)* lua_tostring(lua_State* L, int i) {
280         pragma(inline, true)
281         return lua_tolstring(L, i, null);
282     }
283 
284     void lua_insert(lua_State* L, int idx) {
285         pragma(inline, true)
286         lua_rotate(L, idx, 1);
287     }
288 
289     void lua_remove(lua_State* L, int idx) {
290         pragma(inline, true)
291         lua_rotate(L, idx, -1);
292         lua_pop(L, 1);
293     }
294 
295     void lua_replace(lua_State* L, int idx) {
296         pragma(inline, true)
297         lua_copy(L, -1, idx);
298         lua_pop(L, 1);
299     }
300 
301     void lua_pushunsigned(lua_State* L, lua_Unsigned n) {
302         pragma(inline, true)
303         lua_pushinteger(L, cast(lua_Integer)n);
304     }
305 
306     lua_Unsigned lua_tounsignedx(lua_State* L, int i, int* pi) {
307         pragma(inline, true)
308         return cast(lua_Unsigned)lua_tointegerx(L, i, pi);
309     }
310 
311     lua_Unsigned lua_tounsigned(lua_State* L, int i) {
312         pragma(inline, true)
313         return lua_tounsignedx(L, i, null);
314     }
315 
316     void* lua_newuserdata(lua_State* L, size_t s) {
317         pragma(inline, true)
318         return lua_newuserdatauv(L, s, 1);
319     }
320 
321     int lua_getuservalue(lua_State* L, int idx) {
322         pragma(inline, true)
323         return lua_getiuservalue(L, idx, 1);
324     }
325 
326     int lua_setuservalue(lua_State* L, int idx) {
327         pragma(inline, true)
328         return lua_setiuservalue(L, idx, 1);
329     }
330 }