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