Browse Source

Fixed error messages in functions where love.filesystem.newFile(Data) is called internally

Alex Szpakowski 12 years ago
parent
commit
4dc2072fc6
4 changed files with 40 additions and 7 deletions
  1. 20 4
      src/common/runtime.cpp
  2. 8 0
      src/common/runtime.h
  3. 5 1
      src/scripts/audio.lua
  4. 7 2
      src/scripts/audio.lua.h

+ 20 - 4
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;
 }

+ 8 - 0
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.

+ 5 - 1
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

+ 7 - 2
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,