1 module luajit.lauxlib;
2 /*
3 ** $Id: lauxlib.h,v 1.88.1.1 2007/12/27 13:02:25 roberto Exp $
4 ** Auxiliary functions for building Lua libraries
5 ** See Copyright Notice in lua.h
6 */
7 
8 import core.stdc.config;
9 
10 import core.stdc.stdio;
11 
12 import luajit.lua;
13 import luajit.luaconf;
14 
15 extern (C):
16 
17 extern (D) int luaL_getn(lua_State* L, int i)
18 {
19     return cast(int) lua_objlen(L, i);
20 }
21 
22 extern (D) auto luaL_setn(T2)(lua_State* L, int i, auto ref T2 j)
23 {
24     return cast(void) 0;
25 } /* no op! */
26 
27 /* extra error code for `luaL_load' */
28 enum LUA_ERRFILE = LUA_ERRERR + 1;
29 
30 struct luaL_Reg
31 {
32     const(char)* name;
33     lua_CFunction func;
34 }
35 
36 void luaL_openlib (
37     lua_State* L,
38     const(char)* libname,
39     const(luaL_Reg)* l,
40     int nup);
41 void luaL_register (lua_State* L, const(char)* libname, const(luaL_Reg)* l);
42 int luaL_getmetafield (lua_State* L, int obj, const(char)* e);
43 int luaL_callmeta (lua_State* L, int obj, const(char)* e);
44 int luaL_typerror (lua_State* L, int narg, const(char)* tname);
45 int luaL_argerror (lua_State* L, int numarg, const(char)* extramsg);
46 const(char)* luaL_checklstring (lua_State* L, int numArg, size_t* l);
47 const(char)* luaL_optlstring (
48     lua_State* L,
49     int numArg,
50     const(char)* def,
51     size_t* l);
52 lua_Number luaL_checknumber (lua_State* L, int numArg);
53 lua_Number luaL_optnumber (lua_State* L, int nArg, lua_Number def);
54 
55 lua_Integer luaL_checkinteger (lua_State* L, int numArg);
56 lua_Integer luaL_optinteger (lua_State* L, int nArg, lua_Integer def);
57 
58 void luaL_checkstack (lua_State* L, int sz, const(char)* msg);
59 void luaL_checktype (lua_State* L, int narg, int t);
60 void luaL_checkany (lua_State* L, int narg);
61 
62 int luaL_newmetatable (lua_State* L, const(char)* tname);
63 void* luaL_checkudata (lua_State* L, int ud, const(char)* tname);
64 
65 void luaL_where (lua_State* L, int lvl);
66 int luaL_error (lua_State* L, const(char)* fmt, ...);
67 
68 int luaL_checkoption (
69     lua_State* L,
70     int narg,
71     const(char)* def,
72     const(char*)* lst);
73 
74 int luaL_ref (lua_State* L, int t);
75 void luaL_unref (lua_State* L, int t, int ref_);
76 
77 int luaL_loadfile (lua_State* L, const(char)* filename);
78 int luaL_loadbuffer (
79     lua_State* L,
80     const(char)* buff,
81     size_t sz,
82     const(char)* name);
83 int luaL_loadstring (lua_State* L, const(char)* s);
84 
85 lua_State* luaL_newstate ();
86 
87 const(char)* luaL_gsub (
88     lua_State* L,
89     const(char)* s,
90     const(char)* p,
91     const(char)* r);
92 
93 const(char)* luaL_findtable (
94     lua_State* L,
95     int idx,
96     const(char)* fname,
97     int szhint);
98 
99 /* From Lua 5.2. */
100 int luaL_fileresult (lua_State* L, int stat, const(char)* fname);
101 int luaL_execresult (lua_State* L, int stat);
102 int luaL_loadfilex (lua_State* L, const(char)* filename, const(char)* mode);
103 int luaL_loadbufferx (
104     lua_State* L,
105     const(char)* buff,
106     size_t sz,
107     const(char)* name,
108     const(char)* mode);
109 void luaL_traceback (lua_State* L, lua_State* L1, const(char)* msg, int level);
110 
111 /*
112 ** ===============================================================
113 ** some useful macros
114 ** ===============================================================
115 */
116 
117 extern (D) auto luaL_argcheck(T0, T1, T2, T3)(auto ref T0 L, auto ref T1 cond, auto ref T2 numarg, auto ref T3 extramsg)
118 {
119     return cast(void) cond || luaL_argerror(L, numarg, extramsg);
120 }
121 
122 extern (D) const (char*) luaL_checkstring (lua_State *L, int n){return luaL_checklstring(L, (n), null);}
123 
124 extern (D) auto luaL_optstring(T0, T1, T2)(auto ref T0 L, auto ref T1 n, auto ref T2 d)
125 {
126     return luaL_optlstring(L, n, d, NULL);
127 }
128 
129 extern (D) auto luaL_checkint(T0, T1)(auto ref T0 L, auto ref T1 n)
130 {
131     return cast(int) luaL_checkinteger(L, n);
132 }
133 
134 extern (D) auto luaL_optint(T0, T1, T2)(auto ref T0 L, auto ref T1 n, auto ref T2 d)
135 {
136     return cast(int) luaL_optinteger(L, n, d);
137 }
138 
139 extern (D) auto luaL_checklong(T0, T1)(auto ref T0 L, auto ref T1 n)
140 {
141     return cast(c_long) luaL_checkinteger(L, n);
142 }
143 
144 extern (D) auto luaL_optlong(T0, T1, T2)(auto ref T0 L, auto ref T1 n, auto ref T2 d)
145 {
146     return cast(c_long) luaL_optinteger(L, n, d);
147 }
148 
149 extern (D) auto luaL_typename(T0, T1)(auto ref T0 L, auto ref T1 i)
150 {
151     return lua_typename(L, lua_type(L, i));
152 }
153 
154 extern (D) int luaL_dofile(lua_State* L, const(char)* fn){return luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0);}
155 
156 extern (D) auto luaL_dostring(T0, T1)(auto ref T0 L, auto ref T1 s)
157 {
158     return luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0);
159 }
160 
161 extern (D) auto luaL_getmetatable(T0, T1)(auto ref T0 L, auto ref T1 n)
162 {
163     return lua_getfield(L, LUA_REGISTRYINDEX, n);
164 }
165 
166 extern (D) auto luaL_opt(T0, T1, T2, T3)(auto ref T0 L, auto ref T1 f, auto ref T2 n, auto ref T3 d)
167 {
168     return lua_isnoneornil(L, n) ? d : f(L, n);
169 }
170 
171 /*
172 ** {======================================================
173 ** Generic Buffer manipulation
174 ** =======================================================
175 */
176 
177 struct luaL_Buffer
178 {
179     char* p; /* current position in buffer */
180     int lvl; /* number of strings in the stack (level) */
181     lua_State* L;
182     char[LUAL_BUFFERSIZE] buffer;
183 }
184 
185 extern (D) auto luaL_addchar(luaL_Buffer* B, char c)
186 {
187 	cast(void)(B.p < (B.buffer.ptr+LUAL_BUFFERSIZE) || luaL_prepbuffer(B));
188 	*B.p++ = cast(char)(c);
189 }
190 
191 /* compatibility only */
192 alias luaL_putchar = luaL_addchar;
193 
194 void luaL_buffinit (lua_State* L, luaL_Buffer* B);
195 char* luaL_prepbuffer (luaL_Buffer* B);
196 void luaL_addlstring (luaL_Buffer* B, const(char)* s, size_t l);
197 void luaL_addstring (luaL_Buffer* B, const(char)* s);
198 void luaL_addvalue (luaL_Buffer* B);
199 void luaL_pushresult (luaL_Buffer* B);
200 
201 /* }====================================================== */
202 
203 /* compatibility with ref system */
204 
205 /* pre-defined references */
206 enum LUA_NOREF = -2;
207 enum LUA_REFNIL = -1;
208 
209 extern (D) auto lua_unref(T0, T1)(auto ref T0 L, auto ref T1 ref_)
210 {
211     return luaL_unref(L, LUA_REGISTRYINDEX, ref_);
212 }
213 
214 extern (D) auto lua_getref(T0, T1)(auto ref T0 L, auto ref T1 ref_)
215 {
216     return lua_rawgeti(L, LUA_REGISTRYINDEX, ref_);
217 }
218 
219 alias luaL_reg = luaL_Reg;
220