diff options
Diffstat (limited to '')
| -rw-r--r-- | iolib.c | 13 |
1 files changed, 8 insertions, 5 deletions
| @@ -12,6 +12,8 @@ | |||
| 12 | 12 | ||
| 13 | FILE *lua_infile, *lua_outfile; | 13 | FILE *lua_infile, *lua_outfile; |
| 14 | 14 | ||
| 15 | int lua_tagio; | ||
| 16 | |||
| 15 | 17 | ||
| 16 | #ifdef POPEN | 18 | #ifdef POPEN |
| 17 | FILE *popen(); | 19 | FILE *popen(); |
| @@ -56,7 +58,7 @@ static void io_readfrom (void) | |||
| 56 | lua_Object f = lua_getparam(1); | 58 | lua_Object f = lua_getparam(1); |
| 57 | if (f == LUA_NOOBJECT) | 59 | if (f == LUA_NOOBJECT) |
| 58 | closefile(lua_infile); /* restore standart input */ | 60 | closefile(lua_infile); /* restore standart input */ |
| 59 | else if (lua_isuserdata(f)) | 61 | else if (lua_tag(f) == lua_tagio) |
| 60 | lua_infile = lua_getuserdata(f); | 62 | lua_infile = lua_getuserdata(f); |
| 61 | else { | 63 | else { |
| 62 | char *s = luaL_check_string(1, "readfrom"); | 64 | char *s = luaL_check_string(1, "readfrom"); |
| @@ -68,7 +70,7 @@ static void io_readfrom (void) | |||
| 68 | return; | 70 | return; |
| 69 | } | 71 | } |
| 70 | } | 72 | } |
| 71 | lua_pushuserdata(lua_infile); | 73 | lua_pushusertag(lua_infile, lua_tagio); |
| 72 | } | 74 | } |
| 73 | 75 | ||
| 74 | 76 | ||
| @@ -77,7 +79,7 @@ static void io_writeto (void) | |||
| 77 | lua_Object f = lua_getparam(1); | 79 | lua_Object f = lua_getparam(1); |
| 78 | if (f == LUA_NOOBJECT) | 80 | if (f == LUA_NOOBJECT) |
| 79 | closefile(lua_outfile); /* restore standart output */ | 81 | closefile(lua_outfile); /* restore standart output */ |
| 80 | else if (lua_isuserdata(f)) | 82 | else if (lua_tag(f) == lua_tagio) |
| 81 | lua_outfile = lua_getuserdata(f); | 83 | lua_outfile = lua_getuserdata(f); |
| 82 | else { | 84 | else { |
| 83 | char *s = luaL_check_string(1, "writeto"); | 85 | char *s = luaL_check_string(1, "writeto"); |
| @@ -89,7 +91,7 @@ static void io_writeto (void) | |||
| 89 | return; | 91 | return; |
| 90 | } | 92 | } |
| 91 | } | 93 | } |
| 92 | lua_pushuserdata(lua_outfile); | 94 | lua_pushusertag(lua_outfile, lua_tagio); |
| 93 | } | 95 | } |
| 94 | 96 | ||
| 95 | 97 | ||
| @@ -99,7 +101,7 @@ static void io_appendto (void) | |||
| 99 | FILE *fp = fopen (s, "a"); | 101 | FILE *fp = fopen (s, "a"); |
| 100 | if (fp != NULL) { | 102 | if (fp != NULL) { |
| 101 | lua_outfile = fp; | 103 | lua_outfile = fp; |
| 102 | lua_pushuserdata(lua_outfile); | 104 | lua_pushusertag(lua_outfile, lua_tagio); |
| 103 | } | 105 | } |
| 104 | else | 106 | else |
| 105 | pushresult(0); | 107 | pushresult(0); |
| @@ -294,6 +296,7 @@ static struct luaL_reg iolib[] = { | |||
| 294 | 296 | ||
| 295 | void iolib_open (void) | 297 | void iolib_open (void) |
| 296 | { | 298 | { |
| 299 | lua_tagio = lua_newtag("userdata"); | ||
| 297 | lua_infile=stdin; lua_outfile=stdout; | 300 | lua_infile=stdin; lua_outfile=stdout; |
| 298 | luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0]))); | 301 | luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0]))); |
| 299 | lua_setglobalmethod("error", errorfb); | 302 | lua_setglobalmethod("error", errorfb); |
