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