| 1 | /* |
|---|
| 2 | ** $Id: lauxlib.h,v 1.60 2003/04/03 13:35:34 roberto Exp $ |
|---|
| 3 | ** Auxiliary functions for building Lua libraries |
|---|
| 4 | ** See Copyright Notice in lua.h |
|---|
| 5 | */ |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | #ifndef lauxlib_h |
|---|
| 9 | #define lauxlib_h |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | #include <stddef.h> |
|---|
| 13 | #include <stdio.h> |
|---|
| 14 | |
|---|
| 15 | #include "lua.h" |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | #ifndef LUALIB_API |
|---|
| 19 | #define LUALIB_API LUA_API |
|---|
| 20 | #endif |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | typedef struct luaL_reg { |
|---|
| 25 | const char *name; |
|---|
| 26 | lua_CFunction func; |
|---|
| 27 | } luaL_reg; |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | LUALIB_API void luaL_openlib (lua_State *L, const char *libname, |
|---|
| 31 | const luaL_reg *l, int nup); |
|---|
| 32 | LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *e); |
|---|
| 33 | LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *e); |
|---|
| 34 | LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname); |
|---|
| 35 | LUALIB_API int luaL_argerror (lua_State *L, int numarg, const char *extramsg); |
|---|
| 36 | LUALIB_API const char *luaL_checklstring (lua_State *L, int numArg, size_t *l); |
|---|
| 37 | LUALIB_API const char *luaL_optlstring (lua_State *L, int numArg, |
|---|
| 38 | const char *def, size_t *l); |
|---|
| 39 | LUALIB_API lua_Number luaL_checknumber (lua_State *L, int numArg); |
|---|
| 40 | LUALIB_API lua_Number luaL_optnumber (lua_State *L, int nArg, lua_Number def); |
|---|
| 41 | |
|---|
| 42 | LUALIB_API void luaL_checkstack (lua_State *L, int sz, const char *msg); |
|---|
| 43 | LUALIB_API void luaL_checktype (lua_State *L, int narg, int t); |
|---|
| 44 | LUALIB_API void luaL_checkany (lua_State *L, int narg); |
|---|
| 45 | |
|---|
| 46 | LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname); |
|---|
| 47 | LUALIB_API void luaL_getmetatable (lua_State *L, const char *tname); |
|---|
| 48 | LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname); |
|---|
| 49 | |
|---|
| 50 | LUALIB_API void luaL_where (lua_State *L, int lvl); |
|---|
| 51 | LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...); |
|---|
| 52 | |
|---|
| 53 | LUALIB_API int luaL_findstring (const char *st, const char *const lst[]); |
|---|
| 54 | |
|---|
| 55 | LUALIB_API int luaL_ref (lua_State *L, int t); |
|---|
| 56 | LUALIB_API void luaL_unref (lua_State *L, int t, int ref); |
|---|
| 57 | |
|---|
| 58 | LUALIB_API int luaL_getn (lua_State *L, int t); |
|---|
| 59 | LUALIB_API void luaL_setn (lua_State *L, int t, int n); |
|---|
| 60 | |
|---|
| 61 | LUALIB_API int luaL_loadfile (lua_State *L, const char *filename); |
|---|
| 62 | LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t sz, |
|---|
| 63 | const char *name); |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | /* |
|---|
| 68 | ** =============================================================== |
|---|
| 69 | ** some useful macros |
|---|
| 70 | ** =============================================================== |
|---|
| 71 | */ |
|---|
| 72 | |
|---|
| 73 | #define luaL_argcheck(L, cond,numarg,extramsg) if (!(cond)) \ |
|---|
| 74 | luaL_argerror(L, numarg,extramsg) |
|---|
| 75 | #define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) |
|---|
| 76 | #define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) |
|---|
| 77 | #define luaL_checkint(L,n) ((int)luaL_checknumber(L, n)) |
|---|
| 78 | #define luaL_checklong(L,n) ((long)luaL_checknumber(L, n)) |
|---|
| 79 | #define luaL_optint(L,n,d) ((int)luaL_optnumber(L, n,(lua_Number)(d))) |
|---|
| 80 | #define luaL_optlong(L,n,d) ((long)luaL_optnumber(L, n,(lua_Number)(d))) |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | /* |
|---|
| 84 | ** {====================================================== |
|---|
| 85 | ** Generic Buffer manipulation |
|---|
| 86 | ** ======================================================= |
|---|
| 87 | */ |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | #ifndef LUAL_BUFFERSIZE |
|---|
| 91 | #define LUAL_BUFFERSIZE BUFSIZ |
|---|
| 92 | #endif |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | typedef struct luaL_Buffer { |
|---|
| 96 | char *p; /* current position in buffer */ |
|---|
| 97 | int lvl; /* number of strings in the stack (level) */ |
|---|
| 98 | lua_State *L; |
|---|
| 99 | char buffer[LUAL_BUFFERSIZE]; |
|---|
| 100 | } luaL_Buffer; |
|---|
| 101 | |
|---|
| 102 | #define luaL_putchar(B,c) \ |
|---|
| 103 | ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \ |
|---|
| 104 | (*(B)->p++ = (char)(c))) |
|---|
| 105 | |
|---|
| 106 | #define luaL_addsize(B,n) ((B)->p += (n)) |
|---|
| 107 | |
|---|
| 108 | LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B); |
|---|
| 109 | LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B); |
|---|
| 110 | LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l); |
|---|
| 111 | LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s); |
|---|
| 112 | LUALIB_API void luaL_addvalue (luaL_Buffer *B); |
|---|
| 113 | LUALIB_API void luaL_pushresult (luaL_Buffer *B); |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | /* }====================================================== */ |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | /* |
|---|
| 121 | ** Compatibility macros and functions |
|---|
| 122 | */ |
|---|
| 123 | |
|---|
| 124 | LUALIB_API int lua_dofile (lua_State *L, const char *filename); |
|---|
| 125 | LUALIB_API int lua_dostring (lua_State *L, const char *str); |
|---|
| 126 | LUALIB_API int lua_dobuffer (lua_State *L, const char *buff, size_t sz, |
|---|
| 127 | const char *n); |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | #define luaL_check_lstr luaL_checklstring |
|---|
| 131 | #define luaL_opt_lstr luaL_optlstring |
|---|
| 132 | #define luaL_check_number luaL_checknumber |
|---|
| 133 | #define luaL_opt_number luaL_optnumber |
|---|
| 134 | #define luaL_arg_check luaL_argcheck |
|---|
| 135 | #define luaL_check_string luaL_checkstring |
|---|
| 136 | #define luaL_opt_string luaL_optstring |
|---|
| 137 | #define luaL_check_int luaL_checkint |
|---|
| 138 | #define luaL_check_long luaL_checklong |
|---|
| 139 | #define luaL_opt_int luaL_optint |
|---|
| 140 | #define luaL_opt_long luaL_optlong |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | #endif |
|---|
| 144 | |
|---|
| 145 | |
|---|