Make most love.filesystem functions return nil, err instead of erroring (when used correctly)

Note that argument exceptions, for instance, are still thrown as errors.
Also fixes a potential uncaught exception with getSize being called for the optional
size arg in read, now it just uses the ALL constant instead.
File:read no longer opens and closes a file automagically (this was undocumented, and weird)

NOTE: the love.filesystem.lines iterator can still error, but this should only happen in
case there's something really, really wrong (otherwise love.filesystem.lines would've failed
already)
NOTE: love.filesystem.lines still errors, because otherwise you'd get weird errors from the for
loop (namely 'attempt to call nil value') instead of seeing anything useful
This commit is contained in:
Bart van Strien
2013-05-10 16:39:02 +02:00
parent 00060af4c1
commit 721d50c2ef
3 changed files with 41 additions and 24 deletions
+17 -7
View File
@@ -24,6 +24,18 @@
#include "common/Exception.h"
#include "common/int.h"
static int ioError(lua_State *L, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
lua_pushnil(L);
lua_pushvfstring(L, fmt, args);
va_end(args);
return 2;
}
namespace love
{
namespace filesystem
@@ -65,7 +77,7 @@ int w_File_open(lua_State *L)
}
catch (love::Exception &e)
{
return luaL_error(L, "%s", e.what());
return ioError(L, "%s", e.what());
}
return 1;
@@ -83,7 +95,7 @@ int w_File_read(lua_State *L)
File *file = luax_checkfile(L, 1);
Data *d = 0;
int64 size = (int64)luaL_optnumber(L, 2, (lua_Number) file->getSize());
int64 size = (int64)luaL_optnumber(L, 2, File::ALL);
try
{
@@ -91,7 +103,7 @@ int w_File_read(lua_State *L)
}
catch (love::Exception &e)
{
return luaL_error(L, "%s", e.what());
return ioError(L, "%s", e.what());
}
lua_pushlstring(L, (const char *) d->getData(), d->getSize());
@@ -104,8 +116,6 @@ int w_File_write(lua_State *L)
{
File *file = luax_checkfile(L, 1);
bool result;
if (file->getMode() == File::CLOSED)
return luaL_error(L, "File is not open.");
if (lua_isstring(L, 2))
{
@@ -115,7 +125,7 @@ int w_File_write(lua_State *L)
}
catch (love::Exception &e)
{
return luaL_error(L, "%s", e.what());
return ioError(L, "%s", e.what());
}
}
@@ -128,7 +138,7 @@ int w_File_write(lua_State *L)
}
catch (love::Exception &e)
{
return luaL_error(L, "%s", e.what());
return ioError(L, "%s", e.what());
}
}
else