diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2003-03-18 09:25:32 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2003-03-18 09:25:32 -0300 |
| commit | 40cfb0691e233e164efee27873454cf457255a47 (patch) | |
| tree | e41e02ce562545e700837f5c64ed0ac3a645ec81 | |
| parent | 9b7af7e45bfe3432be8fea27e70de076b22f805f (diff) | |
| download | lua-40cfb0691e233e164efee27873454cf457255a47.tar.gz lua-40cfb0691e233e164efee27873454cf457255a47.tar.bz2 lua-40cfb0691e233e164efee27873454cf457255a47.zip | |
new auxiliary functions for `type' manipulation
| -rw-r--r-- | lauxlib.c | 54 | ||||
| -rw-r--r-- | lauxlib.h | 11 | ||||
| -rw-r--r-- | liolib.c | 30 |
3 files changed, 56 insertions, 39 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lauxlib.c,v 1.94 2003/02/11 09:44:38 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.95 2003/02/11 15:32:31 roberto Exp roberto $ |
| 3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -103,6 +103,45 @@ LUALIB_API int luaL_findstring (const char *name, const char *const list[]) { | |||
| 103 | } | 103 | } |
| 104 | 104 | ||
| 105 | 105 | ||
| 106 | LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) { | ||
| 107 | lua_pushstring(L, tname); | ||
| 108 | lua_rawget(L, LUA_REGISTRYINDEX); /* get registry.name */ | ||
| 109 | if (!lua_isnil(L, -1)) /* name already in use? */ | ||
| 110 | return 0; /* leave previous value on top, but return 0 */ | ||
| 111 | lua_pop(L, 1); | ||
| 112 | lua_newtable(L); /* create metatable */ | ||
| 113 | lua_pushstring(L, tname); | ||
| 114 | lua_pushvalue(L, -2); | ||
| 115 | lua_rawset(L, LUA_REGISTRYINDEX); /* registry.name = metatable */ | ||
| 116 | lua_pushvalue(L, -1); | ||
| 117 | lua_pushstring(L, tname); | ||
| 118 | lua_rawset(L, LUA_REGISTRYINDEX); /* registry[metatable] = name */ | ||
| 119 | return 1; | ||
| 120 | } | ||
| 121 | |||
| 122 | |||
| 123 | LUALIB_API void luaL_getmetatable (lua_State *L, const char *tname) { | ||
| 124 | lua_pushstring(L, tname); | ||
| 125 | lua_rawget(L, LUA_REGISTRYINDEX); | ||
| 126 | } | ||
| 127 | |||
| 128 | |||
| 129 | LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) { | ||
| 130 | const char *tn; | ||
| 131 | if (!lua_getmetatable(L, ud)) return NULL; /* no metatable? */ | ||
| 132 | lua_rawget(L, LUA_REGISTRYINDEX); /* get registry[metatable] */ | ||
| 133 | tn = lua_tostring(L, -1); | ||
| 134 | if (tn && (strcmp(tn, tname) == 0)) { | ||
| 135 | lua_pop(L, 1); | ||
| 136 | return lua_touserdata(L, ud); | ||
| 137 | } | ||
| 138 | else { | ||
| 139 | lua_pop(L, 1); | ||
| 140 | return NULL; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | |||
| 106 | LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *mes) { | 145 | LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *mes) { |
| 107 | if (!lua_checkstack(L, space)) | 146 | if (!lua_checkstack(L, space)) |
| 108 | luaL_error(L, "stack overflow (%s)", mes); | 147 | luaL_error(L, "stack overflow (%s)", mes); |
| @@ -121,19 +160,6 @@ LUALIB_API void luaL_checkany (lua_State *L, int narg) { | |||
| 121 | } | 160 | } |
| 122 | 161 | ||
| 123 | 162 | ||
| 124 | LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) { | ||
| 125 | if (!lua_getmetatable(L, ud)) return NULL; /* no metatable? */ | ||
| 126 | lua_pushstring(L, tname); | ||
| 127 | lua_rawget(L, LUA_REGISTRYINDEX); | ||
| 128 | if (!lua_rawequal(L, -1, -2)) { | ||
| 129 | lua_pop(L, 2); | ||
| 130 | return NULL; | ||
| 131 | } | ||
| 132 | lua_pop(L, 2); | ||
| 133 | return lua_touserdata(L, ud); | ||
| 134 | } | ||
| 135 | |||
| 136 | |||
| 137 | LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) { | 163 | LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) { |
| 138 | const char *s = lua_tostring(L, narg); | 164 | const char *s = lua_tostring(L, narg); |
| 139 | if (!s) tag_error(L, narg, LUA_TSTRING); | 165 | if (!s) tag_error(L, narg, LUA_TSTRING); |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lauxlib.h,v 1.57 2003/01/27 13:46:16 roberto Exp roberto $ | 2 | ** $Id: lauxlib.h,v 1.58 2003/02/11 15:32:31 roberto Exp roberto $ |
| 3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -16,7 +16,7 @@ | |||
| 16 | 16 | ||
| 17 | 17 | ||
| 18 | #ifndef LUALIB_API | 18 | #ifndef LUALIB_API |
| 19 | #define LUALIB_API extern | 19 | #define LUALIB_API LUA_API |
| 20 | #endif | 20 | #endif |
| 21 | 21 | ||
| 22 | 22 | ||
| @@ -33,7 +33,6 @@ 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); | 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); | 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); | 35 | LUALIB_API int luaL_argerror (lua_State *L, int numarg, const char *extramsg); |
| 36 | LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname); | ||
| 37 | LUALIB_API const char *luaL_checklstring (lua_State *L, int numArg, size_t *l); | 36 | LUALIB_API const char *luaL_checklstring (lua_State *L, int numArg, size_t *l); |
| 38 | LUALIB_API const char *luaL_optlstring (lua_State *L, int numArg, | 37 | LUALIB_API const char *luaL_optlstring (lua_State *L, int numArg, |
| 39 | const char *def, size_t *l); | 38 | const char *def, size_t *l); |
| @@ -44,6 +43,10 @@ LUALIB_API void luaL_checkstack (lua_State *L, int sz, const char *msg); | |||
| 44 | LUALIB_API void luaL_checktype (lua_State *L, int narg, int t); | 43 | LUALIB_API void luaL_checktype (lua_State *L, int narg, int t); |
| 45 | LUALIB_API void luaL_checkany (lua_State *L, int narg); | 44 | LUALIB_API void luaL_checkany (lua_State *L, int narg); |
| 46 | 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 | |||
| 47 | LUALIB_API void luaL_where (lua_State *L, int lvl); | 50 | LUALIB_API void luaL_where (lua_State *L, int lvl); |
| 48 | LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...); | 51 | LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...); |
| 49 | 52 | ||
| @@ -115,7 +118,7 @@ LUALIB_API void luaL_pushresult (luaL_Buffer *B); | |||
| 115 | 118 | ||
| 116 | 119 | ||
| 117 | /* | 120 | /* |
| 118 | ** Compatibility macros | 121 | ** Compatibility macros and functions |
| 119 | */ | 122 | */ |
| 120 | 123 | ||
| 121 | LUALIB_API int lua_dofile (lua_State *L, const char *filename); | 124 | LUALIB_API int lua_dofile (lua_State *L, const char *filename); |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: liolib.c,v 2.36 2003/03/14 19:00:16 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 2.37 2003/03/14 19:08:11 roberto Exp roberto $ |
| 3 | ** Standard I/O (and system) library | 3 | ** Standard I/O (and system) library |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -87,21 +87,15 @@ static int pushresult (lua_State *L, int i, const char *filename) { | |||
| 87 | 87 | ||
| 88 | 88 | ||
| 89 | static FILE **topfile (lua_State *L, int findex) { | 89 | static FILE **topfile (lua_State *L, int findex) { |
| 90 | FILE **f = (FILE **)lua_touserdata(L, findex); | 90 | FILE **f = (FILE **)luaL_checkudata(L, findex, FILEHANDLE); |
| 91 | if (f == NULL || !lua_getmetatable(L, findex) || | 91 | if (f == NULL) luaL_argerror(L, findex, "bad file"); |
| 92 | !lua_rawequal(L, -1, lua_upvalueindex(1))) { | ||
| 93 | luaL_argerror(L, findex, "bad file"); | ||
| 94 | } | ||
| 95 | lua_pop(L, 1); | ||
| 96 | return f; | 92 | return f; |
| 97 | } | 93 | } |
| 98 | 94 | ||
| 99 | 95 | ||
| 100 | static int io_type (lua_State *L) { | 96 | static int io_type (lua_State *L) { |
| 101 | FILE **f = (FILE **)lua_touserdata(L, 1); | 97 | FILE **f = (FILE **)luaL_checkudata(L, 1, FILEHANDLE); |
| 102 | if (f == NULL || !lua_getmetatable(L, 1) || | 98 | if (f == NULL) lua_pushnil(L); |
| 103 | !lua_rawequal(L, -1, lua_upvalueindex(1))) | ||
| 104 | lua_pushnil(L); | ||
| 105 | else if (*f == NULL) | 99 | else if (*f == NULL) |
| 106 | lua_pushliteral(L, "closed file"); | 100 | lua_pushliteral(L, "closed file"); |
| 107 | else | 101 | else |
| @@ -127,8 +121,7 @@ static FILE *tofile (lua_State *L, int findex) { | |||
| 127 | static FILE **newfile (lua_State *L) { | 121 | static FILE **newfile (lua_State *L) { |
| 128 | FILE **pf = (FILE **)lua_newuserdata(L, sizeof(FILE *)); | 122 | FILE **pf = (FILE **)lua_newuserdata(L, sizeof(FILE *)); |
| 129 | *pf = NULL; /* file handle is currently `closed' */ | 123 | *pf = NULL; /* file handle is currently `closed' */ |
| 130 | lua_pushliteral(L, FILEHANDLE); | 124 | luaL_getmetatable(L, FILEHANDLE); |
| 131 | lua_rawget(L, LUA_REGISTRYINDEX); | ||
| 132 | lua_setmetatable(L, -2); | 125 | lua_setmetatable(L, -2); |
| 133 | return pf; | 126 | return pf; |
| 134 | } | 127 | } |
| @@ -527,15 +520,12 @@ static const luaL_reg flib[] = { | |||
| 527 | 520 | ||
| 528 | 521 | ||
| 529 | static void createmeta (lua_State *L) { | 522 | static void createmeta (lua_State *L) { |
| 530 | lua_pushliteral(L, FILEHANDLE); | 523 | luaL_newmetatable(L, FILEHANDLE); /* create new metatable for file handles */ |
| 531 | lua_newtable(L); /* push new metatable for file handles */ | ||
| 532 | /* file methods */ | 524 | /* file methods */ |
| 533 | lua_pushliteral(L, "__index"); | 525 | lua_pushliteral(L, "__index"); |
| 534 | lua_pushvalue(L, -2); /* push metatable */ | 526 | lua_pushvalue(L, -2); /* push metatable */ |
| 535 | lua_rawset(L, -3); /* metatable.__index = metatable */ | 527 | lua_rawset(L, -3); /* metatable.__index = metatable */ |
| 536 | lua_pushvalue(L, -1); /* push metatable (will be upvalue for library) */ | 528 | luaL_openlib(L, NULL, flib, 0); |
| 537 | luaL_openlib(L, NULL, flib, 1); | ||
| 538 | lua_rawset(L, LUA_REGISTRYINDEX); /* registry.FILEHANDLE = metatable */ | ||
| 539 | } | 529 | } |
| 540 | 530 | ||
| 541 | /* }====================================================== */ | 531 | /* }====================================================== */ |
| @@ -748,10 +738,8 @@ static const luaL_reg syslib[] = { | |||
| 748 | 738 | ||
| 749 | 739 | ||
| 750 | LUALIB_API int luaopen_io (lua_State *L) { | 740 | LUALIB_API int luaopen_io (lua_State *L) { |
| 751 | createmeta(L); | ||
| 752 | luaL_openlib(L, LUA_OSLIBNAME, syslib, 0); | 741 | luaL_openlib(L, LUA_OSLIBNAME, syslib, 0); |
| 753 | lua_pushliteral(L, FILEHANDLE); | 742 | createmeta(L); |
| 754 | lua_rawget(L, LUA_REGISTRYINDEX); | ||
| 755 | lua_pushvalue(L, -1); | 743 | lua_pushvalue(L, -1); |
| 756 | luaL_openlib(L, LUA_IOLIBNAME, iolib, 1); | 744 | luaL_openlib(L, LUA_IOLIBNAME, iolib, 1); |
| 757 | /* put predefined file handles into `io' table */ | 745 | /* put predefined file handles into `io' table */ |
