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