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.v52;
8 
9 version(LUA_52):
10 
11 public import bindbc.lua.v52.types;
12 
13 version(BindBC_Static) version = BindLua_Static;
14 version(BindLua_Static) {
15     public import bindbc.lua.v52.bindstatic;
16 }
17 else public import bindbc.lua.v52.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);
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     int luaL_checkint(lua_State* L, int arg) {
77         pragma(inline, true)
78         return cast(int)luaL_checkinteger(L, arg);
79     }
80 
81     int luaL_optint(lua_State* L, int arg, int d) {
82         pragma(inline, true)
83         return cast(int)luaL_optinteger(L, arg, d);
84     }
85 
86     c_long luaL_checklong(lua_State* L, int arg) {
87         pragma(inline, true)
88         return cast(c_long)luaL_checkinteger(L, arg);
89     }
90 
91     c_long luaL_optlong(lua_State* L, int arg, int d) {
92         pragma(inline, true)
93         return cast(c_long)luaL_optinteger(L, arg, d);
94     }
95 
96     const(char)* luaL_typename(lua_State* L, int i) {
97         pragma(inline, true)
98         return lua_typename(L, lua_type(L, i));
99     }
100 
101     bool luaL_dofile(lua_State* L, const(char)* filename) {
102         pragma(inline, true)
103         return luaL_loadfile(L, filename) != 0 || lua_pcall(L, 0, LUA_MULTRET, 0) != 0;
104     }
105 
106     bool luaL_dostring(lua_State* L, const(char)* str) {
107         pragma(inline, true)
108         return luaL_loadstring(L, str) != 0 || lua_pcall(L, 0, LUA_MULTRET, 0) != 0;
109     }
110 
111     void luaL_getmetatable(lua_State* L, const(char)* tname) {
112         pragma(inline, true)
113         lua_getfield(L, LUA_REGISTRYINDEX, tname);
114     }
115 
116     // TODO: figure out what luaL_opt is supposed to do
117 
118     int luaL_loadbuffer(lua_State *L, const(char)* buff, size_t sz, const(char)* name) {
119         pragma(inline, true)
120         return luaL_loadbufferx(L, buff, sz, name, null);
121     }
122 
123     void luaL_addchar(luaL_Buffer* B, char c) {
124         pragma(inline, true)
125         if(B.n < B.size || luaL_prepbuffsize(B, 1)) {
126             B.b[B.n++] = c;
127         }
128     }
129 
130     void luaL_addsize(luaL_Buffer* B, size_t s) {
131         pragma(inline, true)
132         B.n += s;
133     }
134 
135     char* luaL_prepbuffer(luaL_Buffer* B) {
136         pragma(inline, true)
137         return luaL_prepbuffsize(B, LUAL_BUFFERSIZE);
138     }
139 
140     // lua.h
141     int lua_upvalueindex(int i) {
142         pragma(inline, true)
143         return LUA_REGISTRYINDEX - i;
144     }
145 
146     void lua_call(lua_State* L, int n, int r) {
147         pragma(inline, true)
148         lua_callk(L, n, r, 0, null);
149     }
150 
151     int lua_pcall(lua_State* L, int n, int r, int f) {
152         pragma(inline, true)
153         return lua_pcallk(L, n, r, f, 0, null);
154     }
155 
156     int lua_yield(lua_State* L, int n) {
157         pragma(inline, true)
158         return lua_yieldk(L, n, 0, null);
159     }
160 
161     lua_Number lua_tonumber(lua_State* L, int i) {
162         pragma(inline, true)
163         return lua_tonumberx(L, i, null);
164     }
165 
166     lua_Integer lua_tointeger(lua_State* L, int i) {
167         pragma(inline, true)
168         return lua_tointegerx(L, i, null);
169     }
170 
171     lua_Unsigned lua_tounsigned(lua_State* L, int i) {
172         pragma(inline, true)
173         return lua_tounsignedx(L, i, null);
174     }
175 
176     void lua_pop(lua_State* L, int n) {
177         pragma(inline, true)
178         lua_settop(L, -n - 1);
179     }
180 
181     void lua_newtable(lua_State* L) {
182         pragma(inline, true)
183         lua_createtable(L, 0, 0);
184     }
185 
186     void lua_register(lua_State* L, const(char)* n, lua_CFunction f) {
187         pragma(inline, true)
188         lua_pushcfunction(L, f);
189         lua_setglobal(L, n);
190     }
191 
192     void lua_pushcfunction(lua_State* L, lua_CFunction f) {
193         pragma(inline, true)
194         lua_pushcclosure(L, f, 0);
195     }
196 
197     bool lua_isfunction(lua_State* L, int n) {
198         pragma(inline, true)
199         return lua_type(L, n) == LUA_TFUNCTION;
200     }
201 
202     bool lua_istable(lua_State* L, int n) {
203         pragma(inline, true)
204         return lua_type(L, n) == LUA_TTABLE;
205     }
206 
207     bool lua_islightuserdata(lua_State* L, int n) {
208         pragma(inline, true)
209         return lua_type(L, n) == LUA_TLIGHTUSERDATA;
210     }
211 
212     bool lua_isnil(lua_State* L, int n) {
213         pragma(inline, true)
214         return lua_type(L, n) == LUA_TNIL;
215     }
216 
217     bool lua_isboolean(lua_State* L, int n) {
218         pragma(inline, true)
219         return lua_type(L, n) == LUA_TBOOLEAN;
220     }
221 
222     bool lua_isthread(lua_State* L, int n) {
223         pragma(inline, true)
224         return lua_type(L, n) == LUA_TTHREAD;
225     }
226 
227     bool lua_isnone(lua_State* L, int n) {
228         pragma(inline, true)
229         return lua_type(L, n) == LUA_TNONE;
230     }
231 
232     bool lua_isnoneornil(lua_State* L, int n) {
233         pragma(inline, true)
234         return lua_type(L, n) <= 0;
235     }
236 
237     void lua_pushliteral(lua_State* L, const(char)[] s) {
238         pragma(inline, true)
239         lua_pushlstring(L, s.ptr, s.length);
240     }
241 
242     void lua_pushglobaltable(lua_State* L) {
243         pragma(inline, true)
244         lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS);
245     }
246 
247     const(char)* lua_tostring(lua_State* L, int i) {
248         pragma(inline, true)
249         return lua_tolstring(L, i, null);
250     }
251 
252     void lua_getregistry(lua_State* L) {
253         pragma(inline, true)
254         lua_pushvalue(L, LUA_REGISTRYINDEX);
255     }
256 
257     int lua_getgccount(lua_State* L) {
258         pragma(inline, true)
259         return lua_gc(L, LUA_GCCOUNT, 0);
260     }
261 }