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 /liolib.c | |
parent | 9b7af7e45bfe3432be8fea27e70de076b22f805f (diff) | |
download | lua-40cfb0691e233e164efee27873454cf457255a47.tar.gz lua-40cfb0691e233e164efee27873454cf457255a47.tar.bz2 lua-40cfb0691e233e164efee27873454cf457255a47.zip |
new auxiliary functions for `type' manipulation
Diffstat (limited to 'liolib.c')
-rw-r--r-- | liolib.c | 30 |
1 files changed, 9 insertions, 21 deletions
@@ -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 */ |