Improved error checking for some enum strings (resolves issue #690).

Also moved love.audio.newSource from Lua code into C++ code
This commit is contained in:
Alex Szpakowski
2013-08-03 21:24:43 -03:00
parent 0c053e09d4
commit a4ff39e0d4
9 changed files with 44 additions and 200 deletions
+18 -10
View File
@@ -24,8 +24,6 @@
#include "openal/Audio.h"
#include "null/Audio.h"
#include "scripts/audio.lua.h"
#include "common/runtime.h"
namespace love
@@ -41,8 +39,23 @@ int w_getSourceCount(lua_State *L)
return 1;
}
int w_newSource1(lua_State *L)
int w_newSource(lua_State *L)
{
if (lua_isstring(L, 1) || luax_istype(L, 1, FILESYSTEM_FILE_T))
luax_convobj(L, 1, "filesystem", "newFileData");
if (luax_istype(L, 1, FILESYSTEM_FILE_DATA_T))
luax_convobj(L, 1, "sound", "newDecoder");
Source::Type stype = Source::TYPE_STREAM;
const char *stypestr = lua_isnoneornil(L, 2) ? 0 : lua_tostring(L, 2);
if (stypestr && !Source::getConstant(stypestr, stype))
return luaL_error(L, "Invalid source type: %s", stypestr);
if (stype == Source::TYPE_STATIC && luax_istype(L, 1, SOUND_DECODER_T))
luax_convobj(L, 1, "sound", "newSoundData");
Source *t = 0;
if (luax_istype(L, 1, SOUND_SOUND_DATA_T))
@@ -56,9 +69,7 @@ int w_newSource1(lua_State *L)
return 1;
}
else
return luaL_error(L, "No matching overload");
return 0;
return luax_typerror(L, 1, "Decoder or SoundData");
}
int w_play(lua_State *L)
@@ -263,7 +274,7 @@ int w_getDistanceModel(lua_State *L)
static const luaL_Reg functions[] =
{
{ "getSourceCount", w_getSourceCount },
{ "newSource1", w_newSource1 },
{ "newSource", w_newSource },
{ "play", w_play },
{ "stop", w_stop },
{ "pause", w_pause },
@@ -333,9 +344,6 @@ extern "C" int luaopen_love_audio(lua_State *L)
int n = luax_register_module(L, w);
if (luaL_loadbuffer(L, (const char *)audio_lua, sizeof(audio_lua), "audio.lua") == 0)
lua_call(L, 0, 0);
return n;
}
+1 -1
View File
@@ -33,7 +33,7 @@ namespace audio
{
int w_getSourceCount(lua_State *L);
int w_newSource1(lua_State *L);
int w_newSource(lua_State *L);
int w_play(lua_State *L);
int w_stop(lua_State *L);
int w_pause(lua_State *L);
+12 -7
View File
@@ -100,11 +100,13 @@ int w_Source_seek(lua_State *L)
Source *t = luax_checksource(L, 1);
float offset = (float)luaL_checknumber(L, 2);
if (offset < 0)
return luaL_error(L, "Can't seek to a negative position");
return luaL_argerror(L, 2, "can't seek to a negative position");
Source::Unit u = Source::UNIT_SECONDS;
const char *unit = lua_isnoneornil(L, 3) ? 0 : lua_tostring(L, 3);
if (unit && !t->getConstant(unit, u))
return luaL_error(L, "Invalid Source time unit: %s", unit);
const char *unit = luaL_optstring(L, 3, "seconds");
Source::Unit u;
t->getConstant(unit, u);
t->seek(offset, u);
return 0;
}
@@ -112,9 +114,12 @@ int w_Source_seek(lua_State *L)
int w_Source_tell(lua_State *L)
{
Source *t = luax_checksource(L, 1);
const char *unit = luaL_optstring(L, 2, "seconds");
Source::Unit u;
t->getConstant(unit, u);
Source::Unit u = Source::UNIT_SECONDS;
const char *unit = lua_isnoneornil(L, 2) ? 0 : lua_tostring(L, 2);
if (unit && !t->getConstant(unit, u))
return luaL_error(L, "Invalid Source time unit: %s", unit);
lua_pushnumber(L, t->tell(u));
return 1;
}
@@ -186,8 +186,8 @@ int w_newFileData(lua_State *L)
FileData::Decoder decoder = FileData::FILE;
if (decstr)
FileData::getConstant(decstr, decoder);
if (decstr && !FileData::getConstant(decstr, decoder))
return luaL_error(L, "Invalid FileData decoder: %s", decstr);
FileData *t = 0;
@@ -200,7 +200,7 @@ int w_newFileData(lua_State *L)
t = instance->newFileData(str, filename);
break;
default:
return luaL_error(L, "Unrecognized FileData decoder: %s", decstr);
return luaL_error(L, "Invalid FileData decoder: %s", decstr);
}
luax_newtype(L, "FileData", FILESYSTEM_FILE_DATA_T, (void *)t);
@@ -433,8 +433,9 @@ int w_newSpriteBatch(lua_State *L)
SpriteBatch::UsageHint usage = SpriteBatch::USAGE_DYNAMIC;
if (lua_gettop(L) > 2)
{
if (!SpriteBatch::getConstant(luaL_checkstring(L, 3), usage))
usage = SpriteBatch::USAGE_DYNAMIC;
const char *usagestr = luaL_checkstring(L, 3);
if (!SpriteBatch::getConstant(usagestr, usage))
return luaL_error(L, "Invalid SpriteBatch usage: %s", usagestr);
}
SpriteBatch *t = NULL;
try
+7 -4
View File
@@ -68,11 +68,14 @@ int w_newBody(lua_State *L)
World *world = luax_checktype<World>(L, 1, "World", PHYSICS_WORLD_T);
float x = (float)luaL_optnumber(L, 2, 0.0);
float y = (float)luaL_optnumber(L, 3, 0.0);
const char *type = luaL_optstring(L, 4, "static");
Body::Type bodyType;
Body::getConstant(type, bodyType);
Body::Type btype = Body::BODY_STATIC;
const char *typestr = lua_isnoneornil(L, 4) ? 0 : lua_tostring(L, 4);
if (typestr && !Body::getConstant(typestr, btype))
return luaL_error(L, "Invalid Body type: %s", typestr);
Body *body;
ASSERT_GUARD(body = instance->newBody(world, x, y, bodyType);)
ASSERT_GUARD(body = instance->newBody(world, x, y, btype);)
luax_newtype(L, "Body", PHYSICS_BODY_T, (void *)body);
return 1;
}