From 4dc2072fc659221dc6398309ec6edf92ee2b6be5 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Thu, 23 May 2013 17:59:15 -0300 Subject: [PATCH] Fixed error messages in functions where love.filesystem.newFile(Data) is called internally --- src/common/runtime.cpp | 24 ++++++++++++++++++++---- src/common/runtime.h | 8 ++++++++ src/scripts/audio.lua | 6 +++++- src/scripts/audio.lua.h | 9 +++++++-- 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/src/common/runtime.cpp b/src/common/runtime.cpp index c7a685dc2..e63f2baef 100644 --- a/src/common/runtime.cpp +++ b/src/common/runtime.cpp @@ -179,13 +179,25 @@ int luax_assert_argc(lua_State *L, int min, int max) return 0; } -int luax_assert_function(lua_State *L, int n) +int luax_assert_function(lua_State *L, int idx) { - if (!lua_isfunction(L, n)) + if (!lua_isfunction(L, idx)) return luaL_error(L, "Argument must be of type \"function\"."); return 0; } +int luax_assert_nilerror(lua_State *L, int idx) +{ + if (lua_isnoneornil(L, idx)) + { + if (lua_isstring(L, idx + 1)) + return luaL_error(L, lua_tostring(L, idx + 1)); + else + return luaL_error(L, "assertion failed!"); + } + return 0; +} + int luax_register_module(lua_State *L, const WrappedModule &m) { // Put a reference to the C++ module in Lua. @@ -359,7 +371,9 @@ int luax_convobj(lua_State *L, int idx, const char *mod, const char *fn) // Convert string to a file. luax_getfunction(L, mod, fn); lua_pushvalue(L, idx); // The initial argument. - lua_call(L, 1, 1); // Call the function, one arg, one return value. + lua_call(L, 1, 2); // Call the function, one arg, one return value (plus optional errstring.) + luax_assert_nilerror(L, -2); // Make sure the function returned something. + lua_pop(L, 1); // Pop the second return value now that we don't need it. lua_replace(L, idx); // Replace the initial argument with the new object. return 0; } @@ -371,7 +385,9 @@ int luax_convobj(lua_State *L, int idxs[], int n, const char *mod, const char *f { lua_pushvalue(L, idxs[i]); // The arguments. } - lua_call(L, n, 1); // Call the function, n args, one return value. + lua_call(L, n, 2); // Call the function, n args, one return value (plus optional errstring.) + luax_assert_nilerror(L, -2); // Make sure the function returned something. + lua_pop(L, 1); // Pop the second return value now that we don't need it. lua_replace(L, idxs[0]); // Replace the initial argument with the new object. return 0; } diff --git a/src/common/runtime.h b/src/common/runtime.h index 74ed6595e..6e01e1968 100644 --- a/src/common/runtime.h +++ b/src/common/runtime.h @@ -208,6 +208,14 @@ int luax_assert_argc(lua_State *L, int min, int max); **/ int luax_assert_function(lua_State *L, int idx); +/** + * Require that the value at idx is not nil. If it is, the function throws an + * error using an optional error string at idx+1. + * @param L The Lua state. + * @param idx The index on the stack. + **/ +int luax_assert_nilerror(lua_State *L, int idx); + /** * Register a module in the love table. The love table will be created if it does not exist. * @param L The Lua state. diff --git a/src/scripts/audio.lua b/src/scripts/audio.lua index 31d20c26c..4e611d014 100644 --- a/src/scripts/audio.lua +++ b/src/scripts/audio.lua @@ -20,7 +20,11 @@ freely, subject to the following restrictions: function love.audio.newSource(a, b) if type(a) == "string" then - a = love.filesystem.newFileData(a) + local err + a, err = love.filesystem.newFileData(a) + if not a then + error(err, 2) + end end if type(a) == "userdata" then if a:typeOf("File") or a:typeOf("FileData") then diff --git a/src/scripts/audio.lua.h b/src/scripts/audio.lua.h index 0ab50e7df..41dd7c4e0 100644 --- a/src/scripts/audio.lua.h +++ b/src/scripts/audio.lua.h @@ -83,8 +83,13 @@ const unsigned char audio_lua[] = 0x6f, 0x2e, 0x6e, 0x65, 0x77, 0x53, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x28, 0x61, 0x2c, 0x20, 0x62, 0x29, 0x0a, 0x09, 0x69, 0x66, 0x20, 0x74, 0x79, 0x70, 0x65, 0x28, 0x61, 0x29, 0x20, 0x3d, 0x3d, 0x20, 0x22, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x22, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a, - 0x09, 0x09, 0x61, 0x20, 0x3d, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x79, 0x73, - 0x74, 0x65, 0x6d, 0x2e, 0x6e, 0x65, 0x77, 0x46, 0x69, 0x6c, 0x65, 0x44, 0x61, 0x74, 0x61, 0x28, 0x61, 0x29, 0x0a, + 0x09, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x20, 0x65, 0x72, 0x72, 0x0a, + 0x09, 0x09, 0x61, 0x2c, 0x20, 0x65, 0x72, 0x72, 0x20, 0x3d, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x66, 0x69, + 0x6c, 0x65, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x6e, 0x65, 0x77, 0x46, 0x69, 0x6c, 0x65, 0x44, 0x61, + 0x74, 0x61, 0x28, 0x61, 0x29, 0x0a, + 0x09, 0x09, 0x69, 0x66, 0x20, 0x6e, 0x6f, 0x74, 0x20, 0x61, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a, + 0x09, 0x09, 0x09, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x28, 0x65, 0x72, 0x72, 0x2c, 0x20, 0x32, 0x29, 0x0a, + 0x09, 0x09, 0x65, 0x6e, 0x64, 0x0a, 0x09, 0x65, 0x6e, 0x64, 0x0a, 0x09, 0x69, 0x66, 0x20, 0x74, 0x79, 0x70, 0x65, 0x28, 0x61, 0x29, 0x20, 0x3d, 0x3d, 0x20, 0x22, 0x75, 0x73, 0x65, 0x72, 0x64, 0x61, 0x74, 0x61, 0x22, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a,