mirror of
https://github.com/love2d/love.git
synced 2026-08-19 04:06:17 +02:00
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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user