|
@@ -58,12 +58,12 @@ static IOFileUD *io_tofile(lua_State *L)
|
|
return iof;
|
|
return iof;
|
|
}
|
|
}
|
|
|
|
|
|
-static FILE *io_stdfile(lua_State *L, ptrdiff_t id)
|
|
|
|
|
|
+static IOFileUD *io_stdfile(lua_State *L, ptrdiff_t id)
|
|
{
|
|
{
|
|
IOFileUD *iof = IOSTDF_IOF(L, id);
|
|
IOFileUD *iof = IOSTDF_IOF(L, id);
|
|
if (iof->fp == NULL)
|
|
if (iof->fp == NULL)
|
|
lj_err_caller(L, LJ_ERR_IOSTDCL);
|
|
lj_err_caller(L, LJ_ERR_IOSTDCL);
|
|
- return iof->fp;
|
|
|
|
|
|
+ return iof;
|
|
}
|
|
}
|
|
|
|
|
|
static IOFileUD *io_file_new(lua_State *L)
|
|
static IOFileUD *io_file_new(lua_State *L)
|
|
@@ -187,8 +187,9 @@ static int io_file_readlen(lua_State *L, FILE *fp, MSize m)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-static int io_file_read(lua_State *L, FILE *fp, int start)
|
|
|
|
|
|
+static int io_file_read(lua_State *L, IOFileUD *iof, int start)
|
|
{
|
|
{
|
|
|
|
+ FILE *fp = iof->fp;
|
|
int ok, n, nargs = (int)(L->top - L->base) - start;
|
|
int ok, n, nargs = (int)(L->top - L->base) - start;
|
|
clearerr(fp);
|
|
clearerr(fp);
|
|
if (nargs == 0) {
|
|
if (nargs == 0) {
|
|
@@ -225,8 +226,9 @@ static int io_file_read(lua_State *L, FILE *fp, int start)
|
|
return n - start;
|
|
return n - start;
|
|
}
|
|
}
|
|
|
|
|
|
-static int io_file_write(lua_State *L, FILE *fp, int start)
|
|
|
|
|
|
+static int io_file_write(lua_State *L, IOFileUD *iof, int start)
|
|
{
|
|
{
|
|
|
|
+ FILE *fp = iof->fp;
|
|
cTValue *tv;
|
|
cTValue *tv;
|
|
int status = 1;
|
|
int status = 1;
|
|
for (tv = L->base+start; tv < L->top; tv++) {
|
|
for (tv = L->base+start; tv < L->top; tv++) {
|
|
@@ -266,7 +268,7 @@ static int io_file_iter(lua_State *L)
|
|
memcpy(L->top, &fn->c.upvalue[1], n*sizeof(TValue));
|
|
memcpy(L->top, &fn->c.upvalue[1], n*sizeof(TValue));
|
|
L->top += n;
|
|
L->top += n;
|
|
}
|
|
}
|
|
- n = io_file_read(L, iof->fp, 0);
|
|
|
|
|
|
+ n = io_file_read(L, iof, 0);
|
|
if (ferror(iof->fp))
|
|
if (ferror(iof->fp))
|
|
lj_err_callermsg(L, strVdata(L->top-2));
|
|
lj_err_callermsg(L, strVdata(L->top-2));
|
|
if (tvisnil(L->base) && (iof->type & IOFILE_FLAG_CLOSE)) {
|
|
if (tvisnil(L->base) && (iof->type & IOFILE_FLAG_CLOSE)) {
|
|
@@ -292,18 +294,18 @@ static int io_file_lines(lua_State *L)
|
|
LJLIB_CF(io_method_close)
|
|
LJLIB_CF(io_method_close)
|
|
{
|
|
{
|
|
IOFileUD *iof = L->base < L->top ? io_tofile(L) :
|
|
IOFileUD *iof = L->base < L->top ? io_tofile(L) :
|
|
- IOSTDF_IOF(L, GCROOT_IO_OUTPUT);
|
|
|
|
|
|
+ io_stdfile(L, GCROOT_IO_OUTPUT);
|
|
return io_file_close(L, iof);
|
|
return io_file_close(L, iof);
|
|
}
|
|
}
|
|
|
|
|
|
LJLIB_CF(io_method_read)
|
|
LJLIB_CF(io_method_read)
|
|
{
|
|
{
|
|
- return io_file_read(L, io_tofile(L)->fp, 1);
|
|
|
|
|
|
+ return io_file_read(L, io_tofile(L), 1);
|
|
}
|
|
}
|
|
|
|
|
|
LJLIB_CF(io_method_write) LJLIB_REC(io_write 0)
|
|
LJLIB_CF(io_method_write) LJLIB_REC(io_write 0)
|
|
{
|
|
{
|
|
- return io_file_write(L, io_tofile(L)->fp, 1);
|
|
|
|
|
|
+ return io_file_write(L, io_tofile(L), 1);
|
|
}
|
|
}
|
|
|
|
|
|
LJLIB_CF(io_method_flush) LJLIB_REC(io_flush 0)
|
|
LJLIB_CF(io_method_flush) LJLIB_REC(io_flush 0)
|
|
@@ -457,7 +459,7 @@ LJLIB_CF(io_write) LJLIB_REC(io_write GCROOT_IO_OUTPUT)
|
|
|
|
|
|
LJLIB_CF(io_flush) LJLIB_REC(io_flush GCROOT_IO_OUTPUT)
|
|
LJLIB_CF(io_flush) LJLIB_REC(io_flush GCROOT_IO_OUTPUT)
|
|
{
|
|
{
|
|
- return luaL_fileresult(L, fflush(io_stdfile(L, GCROOT_IO_OUTPUT)) == 0, NULL);
|
|
|
|
|
|
+ return luaL_fileresult(L, fflush(io_stdfile(L, GCROOT_IO_OUTPUT)->fp) == 0, NULL);
|
|
}
|
|
}
|
|
|
|
|
|
static int io_std_getset(lua_State *L, ptrdiff_t id, const char *mode)
|
|
static int io_std_getset(lua_State *L, ptrdiff_t id, const char *mode)
|