1 /*
2 ** Auxiliary functions for building Lua libraries
3 ** See Copyright Notice in lua.d
4 */
5 
6 module lua.lauxlib;
7 
8 import lua.lua;
9 
10 import core.stdc.stdio : stdout, stderr, FILE, fwrite, fflush;
11 
12 extern (C) @safe nothrow:
13 
14 /* extra error code for 'luaL_load' */
15 enum LUA_ERRFILE = LUA_ERRERR + 1;
16 
17 /* key, in the registry, for table of loaded modules */
18 enum LUA_LOADED_TABLE = "_LOADED";
19 
20 /* key, in the registry, for table of preloaded loaders */
21 enum LUA_PRELOAD_TABLE = "_PRELOAD";
22 
23 struct luaL_Reg {
24     const(char)* name;
25     lua_CFunction func;
26 }
27 
28 enum LUAL_NUMSIZES = lua_Integer.sizeof * 16 + lua_Number.sizeof;
29 
30 void luaL_checkversion_(lua_State* L, lua_Number ver, size_t sz);
31 void luaL_checkversion(lua_State* L) {
32     luaL_checkversion_(L, LUA_VERSION_NUM, LUAL_NUMSIZES);
33 }
34 
35 int luaL_getmetafield(lua_State* L, int obj, const(char)* e);
36 int luaL_callmeta(lua_State* L, int obj, const(char)* e);
37 const(char)* luaL_tolstring(lua_State* L, int idx, size_t* len);
38 int luaL_argerror(lua_State* L, int arg, const(char)* extramsg);
39 const(char)* luaL_checklstring(lua_State* L, int arg, size_t* l);
40 const(char)* luaL_optlstring(lua_State* L, int arg, const(char)* def, size_t* l);
41 lua_Number luaL_checknumber(lua_State* L, int arg);
42 lua_Number luaL_optnumber(lua_State* L, int arg, lua_Number def);
43 
44 lua_Integer luaL_checkinteger(lua_State* L, int arg);
45 lua_Integer luaL_optinteger(lua_State* L, int arg, lua_Integer def);
46 
47 void luaL_checkstack(lua_State* L, int sz, const(char)* msg);
48 void luaL_checktype(lua_State* L, int arg, int t);
49 void luaL_checkany(lua_State* L, int arg);
50 
51 int luaL_newmetatable(lua_State* L, const(char)* tname);
52 void luaL_setmetatable(lua_State* L, const(char)* tname);
53 void* luaL_testudata(lua_State* L, int ud, const(char)* tname);
54 void* luaL_checkudata(lua_State* L, int ud, const(char)* tname);
55 
56 void luaL_where(lua_State* L, int lvl);
57 int luaL_error(lua_State* L, const(char)* fmt, ...);
58 
59 int luaL_checkoption(lua_State* L, int arg, const(char)* def, const(const(char)*)* lst);
60 
61 int luaL_fileresult(lua_State* L, int stat, const(char)* fname);
62 int luaL_execresult(lua_State* L, int stat);
63 
64 /* predefined references */
65 enum LUA_NOREF = -2;
66 enum LUA_REFNIL = -1;
67 
68 int luaL_ref(lua_State* L, int t);
69 void luaL_unref(lua_State* L, int t, int ref_);
70 
71 int luaL_loadfilex(lua_State* L, const(char)* filename, const(char)* mode);
72 
73 pragma(inline, true) int luaL_loadfile(lua_State* L, const(char)* filename) {
74     return luaL_loadfilex(L, filename, null);
75 }
76 
77 int luaL_loadbufferx(lua_State* L, const(char)* buff, size_t sz,
78     const(char)* name, const(char)* mode);
79 int luaL_loadstring(lua_State* L, const(char)* s);
80 
81 lua_State* luaL_newstate();
82 
83 lua_Integer luaL_len(lua_State* L, int idx);
84 
85 const(char)* luaL_gsub(lua_State* L, const(char)* s, const(char)* p, const(char)* r);
86 
87 void luaL_setfuncs(lua_State* L, const(luaL_Reg)* l, int nup);
88 
89 int luaL_getsubtable(lua_State* L, int idx, const(char)* fname);
90 
91 void luaL_traceback(lua_State* L, lua_State* L1, const(char)* msg, int level);
92 
93 void luaL_requiref(lua_State* L, const(char)* modname, lua_CFunction openf, int glb);
94 
95 /*
96 ** ===============================================================
97 ** some useful macros
98 ** ===============================================================
99 */
100 
101 pragma(inline, true) {
102 
103     void luaL_newlibtable(lua_State* L, luaL_Reg[] l) {
104         lua_createtable(L, 0, cast(int)(l.length - 1));
105     }
106 
107     void luaL_newlib(lua_State* L, luaL_Reg[] l) {
108         luaL_checkversion(L);
109         luaL_newlibtable(L, l);
110         luaL_setfuncs(L, &l[0], 0);
111     }
112 
113     void luaL_argcheck(lua_State* L, bool cond, int arg, const(char)* extramsg) {
114         (cond) || luaL_argerror(L, arg, extramsg);
115     }
116 
117     const(char)* luaL_checkstring(lua_State* L, int arg) {
118         return luaL_checklstring(L, arg, null);
119     }
120 
121     const(char)* luaL_optstring(lua_State* L, int arg, const(char)* def) {
122         return luaL_optlstring(L, arg, def, null);
123     }
124 
125     const(char)* luaL_typename(lua_State* L, int tp) {
126         return lua_typename(L, lua_type(L, tp));
127     }
128 
129     int luaL_dofile(lua_State* L, const(char)* filename) {
130         return cast(int)(luaL_loadfile(L, filename) || lua_pcall(L, 0, LUA_MULTRET,
131             0));
132     }
133 
134     int luaL_dostring(lua_State* L, const(char)* s) {
135         return cast(int)(luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0));
136     }
137 
138     int luaL_getmetatable(lua_State* L, const(char)* k) {
139         return lua_getfield(L, LUA_REGISTRYINDEX, k);
140     }
141 
142     auto luaL_opt(F, Default)(lua_State* L, F f, int n, lazy Default d) {
143         return lua_isnoneornil(L, n) ? d : f(L, n);
144     }
145 
146     int luaL_loadbuffer(lua_State* L, const(char)* buff, size_t sz, const(char)* name) {
147         return luaL_loadbufferx(L, buff, sz, name, null);
148     }
149 
150 } // pragma(inline, true)
151 
152 /*
153 ** {======================================================
154 ** Generic Buffer manipulation
155 ** =======================================================
156 */
157 enum LUAL_BUFFERSIZE = cast(int)(0x80 * (void*).sizeof * lua_Integer.sizeof);
158 
159 struct luaL_Buffer {
160     char* b; /* buffer address */
161     size_t size; /* buffer size */
162     size_t n; /* number of characters in buffer */
163     lua_State* L;
164     char[LUAL_BUFFERSIZE] initb; /* initial buffer */
165 }
166 
167 pragma(inline, true) @trusted void luaL_addchar(luaL_Buffer* B, char c) {
168     B.n < B.size || luaL_prepbuffsize(B, 1);
169     B.b[B.n++] = c;
170 }
171 
172 pragma(inline, true) void luaL_addsize(luaL_Buffer* B, size_t s) {
173     B.n += s;
174 }
175 
176 void luaL_buffinit(lua_State* L, luaL_Buffer* B);
177 char* luaL_prepbuffsize(luaL_Buffer* B, size_t sz);
178 void luaL_addlstring(luaL_Buffer* B, const(char)* s, size_t l);
179 void luaL_addstring(luaL_Buffer* B, const(char)* s);
180 void luaL_addvalue(luaL_Buffer* B);
181 void luaL_pushresult(luaL_Buffer* B);
182 void luaL_pushresultsize(luaL_Buffer* B, size_t sz);
183 char* luaL_buffinitsize(lua_State* L, luaL_Buffer* B, size_t sz);
184 
185 pragma(inline, true) char* luaL_prepbuffer(luaL_Buffer* B) {
186     return luaL_prepbuffsize(B, LUAL_BUFFERSIZE);
187 }
188 
189 /* }====================================================== */
190 
191 /*
192 ** {======================================================
193 ** File handles for IO library
194 ** =======================================================
195 */
196 
197 /*
198 ** A file handle is a userdata with metatable 'LUA_FILEHANDLE' and
199 ** initial structure 'luaL_Stream' (it may contain other fields
200 ** after that initial structure).
201 */
202 
203 enum LUA_FILEHANDLE = "FILE*";
204 
205 struct luaL_Stream {
206     FILE* f; /* stream (NULL for incompletely created streams) */
207     lua_CFunction closef; /* to close stream (NULL for closed streams) */
208 }
209 
210 /* }====================================================== */
211 
212 /* compatibility with old module system */
213 deprecated {
214 
215     void luaL_pushmodule(lua_State* L, const(char)* modname, int sizehint);
216     void luaL_openlib(lua_State* L, const(char)* libname, const(luaL_Reg)* l, int nup);
217 
218     pragma(inline, true) void luaL_openlib(lua_State* L, const(char)* libname, const(luaL_Reg)* l) {
219         luaL_openlib(L, libname, l, 0);
220     }
221 
222 }
223 
224 /*
225 ** {==================================================================
226 ** "Abstraction Layer" for basic report of messages and errors
227 ** ===================================================================
228 */
229 
230 /* print a string */
231 @trusted ulong lua_writestring(const(char)* s, size_t l) {
232     return fwrite(s, char.sizeof, l, stdout);
233 }
234 
235 pragma(inline, true) {
236 
237     /* print a newline and flush the output */
238     void lua_writeline() {
239         lua_writestring("\n", 1);
240         stdout.fflush;
241     }
242 
243     /* print an error message */
244     void lua_writestringerror(Args...)(const(char)* s, auto ref Args args) {
245         fprintf(stderr, s, args);
246         stderr.fflush;
247     }
248 
249 } // pragma(inline, true)
250 
251 /* }================================================================== */
252 
253 /*
254 ** {============================================================
255 ** Compatibility with deprecated conversions
256 ** =============================================================
257 */
258 deprecated pragma(inline, true) {
259 
260     lua_Unsigned luaL_checkunsigned(lua_State* L, int arg) {
261         return cast(lua_Unsigned) luaL_checkinteger(L, arg);
262     }
263 
264     lua_Unsigned luaL_optunsigned(lua_State* L, int arg, lua_Integer def) {
265         return cast(lua_Unsigned) luaL_optinteger(L, arg, def);
266     }
267 
268     int luaL_checkint(lua_State* L, int arg) {
269         return cast(int) luaL_checkinteger(L, arg);
270     }
271 
272     int luaL_optint(lua_State* L, int arg, lua_Integer def) {
273         return cast(int) luaL_optinteger(L, arg, def);
274     }
275 
276     long luaL_checklong(lua_State* L, int arg) {
277         return cast(long) luaL_checkinteger(L, arg);
278     }
279 
280     long luaL_optlong(lua_State* L, int arg, lua_Integer def) {
281         return cast(long) luaL_optinteger(L, arg, def);
282     }
283 
284 }
285 /* }============================================================ */