From 06f80d5c0886f6f9d4d5df31166a32f12e47532e Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Thu, 5 Sep 2013 23:17:29 -0300 Subject: [PATCH] Fixed undefined behaviour when love exceptions are converted into Lua errors (resolves issue #729) --- src/common/runtime.h | 19 ++ src/modules/event/sdl/wrap_Event.cpp | 9 +- src/modules/filesystem/physfs/Filesystem.cpp | 8 +- src/modules/filesystem/physfs/wrap_File.cpp | 73 ++++--- src/modules/filesystem/physfs/wrap_File.h | 3 + .../filesystem/physfs/wrap_Filesystem.cpp | 85 +++----- .../filesystem/physfs/wrap_Filesystem.h | 3 - src/modules/font/freetype/wrap_Font.cpp | 31 +-- src/modules/font/wrap_GlyphData.cpp | 10 +- src/modules/font/wrap_Rasterizer.cpp | 18 +- src/modules/graphics/opengl/wrap_Canvas.cpp | 21 +- src/modules/graphics/opengl/wrap_Font.cpp | 40 +--- src/modules/graphics/opengl/wrap_Geometry.cpp | 30 +-- src/modules/graphics/opengl/wrap_Graphics.cpp | 197 +++++------------- src/modules/graphics/opengl/wrap_Image.cpp | 32 +-- .../graphics/opengl/wrap_ParticleSystem.cpp | 19 +- src/modules/graphics/opengl/wrap_Shader.cpp | 46 ++-- .../graphics/opengl/wrap_SpriteBatch.cpp | 36 +--- src/modules/image/wrap_CompressedData.cpp | 33 +-- src/modules/image/wrap_Image.cpp | 49 +---- src/modules/image/wrap_ImageData.cpp | 29 +-- .../joystick/sdl/wrap_JoystickModule.cpp | 31 +-- src/modules/keyboard/wrap_Keyboard.cpp | 9 +- src/modules/math/wrap_BezierCurve.cpp | 47 +---- src/modules/math/wrap_Math.cpp | 27 +-- src/modules/math/wrap_RandomGenerator.cpp | 9 +- src/modules/mouse/wrap_Mouse.cpp | 27 +-- src/modules/physics/box2d/Physics.cpp | 11 +- src/modules/physics/box2d/wrap_Body.cpp | 33 ++- src/modules/physics/box2d/wrap_ChainShape.cpp | 8 +- src/modules/physics/box2d/wrap_Fixture.cpp | 15 +- .../physics/box2d/wrap_FrictionJoint.cpp | 4 +- src/modules/physics/box2d/wrap_GearJoint.cpp | 2 +- src/modules/physics/box2d/wrap_Joint.cpp | 9 +- src/modules/physics/box2d/wrap_Physics.cpp | 62 +++--- src/modules/physics/box2d/wrap_Physics.h | 2 - .../physics/box2d/wrap_PrismaticJoint.cpp | 6 +- .../physics/box2d/wrap_RevoluteJoint.cpp | 6 +- src/modules/physics/box2d/wrap_Shape.cpp | 4 +- src/modules/physics/box2d/wrap_World.cpp | 15 +- src/modules/sound/wrap_Sound.cpp | 46 +--- src/modules/sound/wrap_SoundData.cpp | 20 +- src/modules/thread/wrap_ThreadModule.cpp | 9 +- src/modules/timer/wrap_Timer.cpp | 10 +- src/modules/window/wrap_Window.cpp | 19 +- 45 files changed, 349 insertions(+), 873 deletions(-) diff --git a/src/common/runtime.h b/src/common/runtime.h index 62f47a2ab..347ceda08 100644 --- a/src/common/runtime.h +++ b/src/common/runtime.h @@ -470,6 +470,25 @@ T *luax_totype(lua_State *L, int idx, const char *, love::bits) Type luax_type(lua_State *L, int idx); +/** + * Macro for converting a LOVE exception into a Lua error. + * lua_error (and luaL_error) cannot be called from inside the exception handler + * because they use longjmp, which causes undefined behaviour when the + * destructor of the exception would have been called. + **/ +#define EXCEPT_GUARD(A) \ +{ \ + bool should_error = false; \ + try { A } \ + catch (love::Exception &e) \ + { \ + should_error = true; \ + lua_pushstring(L, e.what()); \ + } \ + if (should_error) \ + return lua_error(L); \ +} + } // love #endif // LOVE_RUNTIME_H diff --git a/src/modules/event/sdl/wrap_Event.cpp b/src/modules/event/sdl/wrap_Event.cpp index bcceba015..c97b36e32 100644 --- a/src/modules/event/sdl/wrap_Event.cpp +++ b/src/modules/event/sdl/wrap_Event.cpp @@ -123,14 +123,7 @@ extern "C" int luaopen_love_event(lua_State *L) { if (instance == 0) { - try - { - instance = new Event(); - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + EXCEPT_GUARD(instance = new Event();) } else instance->retain(); diff --git a/src/modules/filesystem/physfs/Filesystem.cpp b/src/modules/filesystem/physfs/Filesystem.cpp index 93dd67465..2f56bb526 100644 --- a/src/modules/filesystem/physfs/Filesystem.cpp +++ b/src/modules/filesystem/physfs/Filesystem.cpp @@ -529,15 +529,19 @@ int Filesystem::lines_i(lua_State *L) } else { - char *str; + char *str = 0; try { str = new char[linesize + 1]; } catch(std::bad_alloc &) { - return luaL_error(L, "Out of memory"); + // Can't lua_error (longjmp) in exception handlers. } + + if (!str) + return luaL_error(L, "Out of memory."); + file->seek(pos); // Read the \n anyway and save us a call to seek. diff --git a/src/modules/filesystem/physfs/wrap_File.cpp b/src/modules/filesystem/physfs/wrap_File.cpp index 4488adb2c..b4b3c47a9 100644 --- a/src/modules/filesystem/physfs/wrap_File.cpp +++ b/src/modules/filesystem/physfs/wrap_File.cpp @@ -24,7 +24,14 @@ #include "common/Exception.h" #include "common/int.h" -static int ioError(lua_State *L, const char *fmt, ...) +namespace love +{ +namespace filesystem +{ +namespace physfs +{ + +int luax_ioError(lua_State *L, const char *fmt, ...) { va_list args; va_start(args, fmt); @@ -36,13 +43,6 @@ static int ioError(lua_State *L, const char *fmt, ...) return 2; } -namespace love -{ -namespace filesystem -{ -namespace physfs -{ - File *luax_checkfile(lua_State *L, int idx) { return luax_checktype(L, idx, "File", FILESYSTEM_FILE_T); @@ -54,11 +54,12 @@ int w_File_getSize(lua_State *L) int64 size = t->getSize(); // Push nil on failure or if size does not fit into a double precision floating-point number. - if (size == -1 || size >= 0x20000000000000LL) - lua_pushnil(L); - else - lua_pushnumber(L, (lua_Number)size); + if (size == -1) + return luax_ioError(L, "Could not determine file size."); + else if (size >= 0x20000000000000LL) + return luax_ioError(L, "Size is too large."); + lua_pushnumber(L, (lua_Number) size); return 1; } @@ -77,7 +78,7 @@ int w_File_open(lua_State *L) } catch (love::Exception &e) { - return ioError(L, "%s", e.what()); + return luax_ioError(L, "%s", e.what()); } return 1; @@ -110,7 +111,7 @@ int w_File_read(lua_State *L) } catch (love::Exception &e) { - return ioError(L, "%s", e.what()); + return luax_ioError(L, "%s", e.what()); } lua_pushlstring(L, (const char *) d->getData(), d->getSize()); @@ -128,11 +129,17 @@ int w_File_write(lua_State *L) { try { - result = file->write(lua_tostring(L, 2), luaL_optint(L, 3, lua_objlen(L, 2))); + size_t datasize = 0; + const char *data = lua_tolstring(L, 2, &datasize); + + if (!lua_isnoneornil(L, 3)) + datasize = luaL_checkinteger(L, 3); + + result = file->write(data, datasize); } catch (love::Exception &e) { - return ioError(L, "%s", e.what()); + return luax_ioError(L, "%s", e.what()); } } else if (luax_istype(L, 2, DATA_T)) @@ -140,11 +147,11 @@ int w_File_write(lua_State *L) try { love::Data *data = luax_totype(L, 2, "Data", DATA_T); - result = file->write(data, luaL_optint(L, 3, data->getSize())); + result = file->write(data, luaL_optinteger(L, 3, data->getSize())); } catch (love::Exception &e) { - return ioError(L, "%s", e.what()); + return luax_ioError(L, "%s", e.what()); } } else @@ -152,7 +159,7 @@ int w_File_write(lua_State *L) return luaL_argerror(L, 2, "string or data expected"); } - lua_pushboolean(L, result); + luax_pushboolean(L, result); return 1; } @@ -166,7 +173,7 @@ int w_File_flush(lua_State *L) } catch (love::Exception &e) { - return ioError(L, "%s", e.what()); + return luax_ioError(L, "%s", e.what()); } luax_pushboolean(L, success); return 1; @@ -184,8 +191,10 @@ int w_File_tell(lua_State *L) File *file = luax_checkfile(L, 1); int64 pos = file->tell(); // Push nil on failure or if pos does not fit into a double precision floating-point number. - if (pos == -1 || pos >= 0x20000000000000LL) - lua_pushnil(L); + if (pos == -1) + return luax_ioError(L, "Invalid position."); + else if (pos >= 0x20000000000000LL) + return luax_ioError(L, "Number is too large."); else lua_pushnumber(L, (lua_Number)pos); return 1; @@ -217,15 +226,11 @@ int w_File_lines(lua_State *L) if (file->getMode() != File::CLOSED) file->close(); - try - { - if (!file->open(File::READ)) - return luaL_error(L, "Could not open file."); - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + bool success = false; + EXCEPT_GUARD(success = file->open(File::READ);) + + if (!success) + return luaL_error(L, "Could not open file."); } lua_pushcclosure(L, Filesystem::lines_i, 3); @@ -249,7 +254,7 @@ int w_File_setBuffer(lua_State *L) } catch (love::Exception &e) { - return ioError(L, "%s", e.what()); + return luax_ioError(L, "%s", e.what()); } luax_pushboolean(L, success); @@ -264,7 +269,7 @@ int w_File_getBuffer(lua_State *L) const char *str = 0; if (!File::getConstant(bufmode, str)) - return ioError(L, "Unknown file buffer mode."); + return luax_ioError(L, "Unknown file buffer mode."); lua_pushstring(L, str); lua_pushnumber(L, (lua_Number) size); @@ -279,7 +284,7 @@ int w_File_getMode(lua_State *L) const char *str = 0; if (!File::getConstant(mode, str)) - return ioError(L, "Unknown file mode."); + return luax_ioError(L, "Unknown file mode."); lua_pushstring(L, str); return 1; diff --git a/src/modules/filesystem/physfs/wrap_File.h b/src/modules/filesystem/physfs/wrap_File.h index 538fbd316..4aa86b147 100644 --- a/src/modules/filesystem/physfs/wrap_File.h +++ b/src/modules/filesystem/physfs/wrap_File.h @@ -33,6 +33,9 @@ namespace filesystem namespace physfs { +// Does not use lua_error, so it's safe to call in exception handling code. +int luax_ioError(lua_State *L, const char *fmt, ...); + File *luax_checkfile(lua_State *L, int idx); int w_File_getSize(lua_State *L); int w_File_open(lua_State *L); diff --git a/src/modules/filesystem/physfs/wrap_Filesystem.cpp b/src/modules/filesystem/physfs/wrap_Filesystem.cpp index a46007bf5..d9ce750f1 100644 --- a/src/modules/filesystem/physfs/wrap_Filesystem.cpp +++ b/src/modules/filesystem/physfs/wrap_Filesystem.cpp @@ -21,17 +21,8 @@ // LOVE #include "wrap_Filesystem.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; -} +// SDL +#include namespace love { @@ -52,16 +43,7 @@ bool hack_setupWriteDirectory() int w_init(lua_State *L) { const char *arg0 = luaL_checkstring(L, 1); - - try - { - instance->init(arg0); - } - catch(Exception &e) - { - return luaL_error(L, e.what()); - } - + EXCEPT_GUARD(instance->init(arg0);) return 0; } @@ -166,7 +148,7 @@ int w_newFile(lua_State *L) catch (love::Exception &e) { t->release(); - return ioError(L, "%s", e.what()); + return luax_ioError(L, "%s", e.what()); } } @@ -194,7 +176,7 @@ int w_newFileData(lua_State *L) } catch (love::Exception &e) { - return ioError(L, "%s", e.what()); + return luax_ioError(L, "%s", e.what()); } luax_pushtype(L, "FileData", FILESYSTEM_FILE_DATA_T, data); return 1; @@ -308,11 +290,11 @@ int w_read(lua_State *L) } catch (love::Exception &e) { - return ioError(L, "%s", e.what()); + return luax_ioError(L, "%s", e.what()); } if (data == 0) - return ioError(L, "File could not be read."); + return luax_ioError(L, "File could not be read."); // Push the string. lua_pushlstring(L, (const char *) data->getData(), data->getSize()); @@ -356,11 +338,10 @@ static int w_write_or_append(lua_State *L, File::Mode mode) } catch (love::Exception &e) { - return ioError(L, "%s", e.what()); + return luax_ioError(L, "%s", e.what()); } luax_pushboolean(L, true); - return 1; } @@ -386,19 +367,17 @@ int w_lines(lua_State *L) if (lua_isstring(L, 1)) { file = instance->newFile(lua_tostring(L, 1)); - try - { - if (!file->open(File::READ)) - return luaL_error(L, "Could not open file."); - } - catch(love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + bool success = false; + + EXCEPT_GUARD(success = file->open(File::READ);) + + if (!success) + return luaL_error(L, "Could not open file."); + luax_pushtype(L, "File", FILESYSTEM_FILE_T, file); } else - return luaL_error(L, "Expected filename."); + return luaL_argerror(L, 1, "expected filename."); lua_pushcclosure(L, Filesystem::lines_i, 1); return 1; @@ -415,7 +394,7 @@ int w_load(lua_State *L) } catch (love::Exception &e) { - return ioError(L, "%s", e.what()); + return luax_ioError(L, "%s", e.what()); } int status = luaL_loadbuffer(L, (const char *)data->getData(), data->getSize(), ("@" + filename).c_str()); @@ -445,13 +424,10 @@ int w_getLastModified(lua_State *L) } catch (love::Exception &e) { - lua_pushnil(L); - lua_pushstring(L, e.what()); - return 2; + return luax_ioError(L, "%s", e.what()); } lua_pushnumber(L, static_cast(time)); - return 1; } @@ -466,15 +442,14 @@ int w_getSize(lua_State *L) } catch (love::Exception &e) { - return ioError(L, "%s", e.what()); + return luax_ioError(L, "%s", e.what()); } // Error on failure or if size does not fit into a double precision floating-point number. if (size == -1) - return ioError(L, "Could not determine file size."); + return luax_ioError(L, "Could not determine file size."); else if (size >= 0x20000000000000LL) - return luaL_error(L, "Size too large to fit into a Lua number!"); - + return luax_ioError(L, "Size too large to fit into a Lua number!"); lua_pushnumber(L, (lua_Number) size); return 1; @@ -629,23 +604,13 @@ extern "C" int luaopen_love_filesystem(lua_State *L) { if (instance == 0) { - try - { - instance = new Filesystem(); - love::luax_register_searcher(L, loader, 1); - love::luax_register_searcher(L, extloader, 2); - } - catch(Exception &e) - { - return luaL_error(L, e.what()); - } + EXCEPT_GUARD(instance = new Filesystem();) } else - { instance->retain(); - love::luax_register_searcher(L, loader, 1); - love::luax_register_searcher(L, extloader, 2); - } + + love::luax_register_searcher(L, loader, 1); + love::luax_register_searcher(L, extloader, 2); WrappedModule w; w.module = instance; diff --git a/src/modules/filesystem/physfs/wrap_Filesystem.h b/src/modules/filesystem/physfs/wrap_Filesystem.h index f6fd08adb..4edeb6832 100644 --- a/src/modules/filesystem/physfs/wrap_Filesystem.h +++ b/src/modules/filesystem/physfs/wrap_Filesystem.h @@ -26,9 +26,6 @@ #include "wrap_File.h" #include "wrap_FileData.h" -// SDL -#include - namespace love { namespace filesystem diff --git a/src/modules/font/freetype/wrap_Font.cpp b/src/modules/font/freetype/wrap_Font.cpp index 9aa7b254f..a3f56ed71 100644 --- a/src/modules/font/freetype/wrap_Font.cpp +++ b/src/modules/font/freetype/wrap_Font.cpp @@ -42,9 +42,9 @@ int w_newRasterizer(lua_State *L) if (lua_isstring(L, 1) || luax_istype(L, 1, FILESYSTEM_FILE_T)) luax_convobj(L, 1, "filesystem", "newFileData"); - Rasterizer *t = NULL; - try - { + Rasterizer *t = 0; + + EXCEPT_GUARD( if (luax_istype(L, 1, IMAGE_IMAGE_DATA_T)) { love::image::ImageData *d = luax_checktype(L, 1, "ImageData", IMAGE_IMAGE_DATA_T); @@ -58,11 +58,7 @@ int w_newRasterizer(lua_State *L) int size = luaL_checkint(L, 2); t = instance->newRasterizer(d, size); } - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + ) luax_pushtype(L, "Rasterizer", FONT_RASTERIZER_T, t); return 1; @@ -77,14 +73,8 @@ int w_newGlyphData(lua_State *L) if (lua_type(L, 2) == LUA_TSTRING) { std::string glyph = luax_checkstring(L, 2); - try - { - t = instance->newGlyphData(r, glyph); - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + + EXCEPT_GUARD(t = instance->newGlyphData(r, glyph);) } else { @@ -115,14 +105,7 @@ extern "C" int luaopen_love_font(lua_State *L) { if (instance == 0) { - try - { - instance = new Font(); - } - catch(Exception &e) - { - return luaL_error(L, e.what()); - } + EXCEPT_GUARD(instance = new Font();) } else instance->retain(); diff --git a/src/modules/font/wrap_GlyphData.cpp b/src/modules/font/wrap_GlyphData.cpp index 9d4569f84..5a822b27f 100644 --- a/src/modules/font/wrap_GlyphData.cpp +++ b/src/modules/font/wrap_GlyphData.cpp @@ -63,14 +63,8 @@ int w_GlyphData_getGlyph(lua_State *L) int w_GlyphData_getGlyphString(lua_State *L) { GlyphData *t = luax_checkglyphdata(L, 1); - try - { - luax_pushstring(L, t->getGlyphString()); - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + + EXCEPT_GUARD(luax_pushstring(L, t->getGlyphString());) return 1; } diff --git a/src/modules/font/wrap_Rasterizer.cpp b/src/modules/font/wrap_Rasterizer.cpp index e2499c89e..43b023413 100644 --- a/src/modules/font/wrap_Rasterizer.cpp +++ b/src/modules/font/wrap_Rasterizer.cpp @@ -72,8 +72,7 @@ int w_Rasterizer_getGlyphData(lua_State *L) Rasterizer *t = luax_checkrasterizer(L, 1); GlyphData *g = 0; - try - { + EXCEPT_GUARD( // getGlyphData accepts a unicode character or a codepoint number. if (lua_type(L, 2) == LUA_TSTRING) { @@ -85,11 +84,7 @@ int w_Rasterizer_getGlyphData(lua_State *L) uint32 glyph = (uint32) luaL_checknumber(L, 2); g = t->getGlyphData(glyph); } - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + ) luax_pushtype(L, "GlyphData", FONT_GLYPH_DATA_T, g); return 1; @@ -108,17 +103,12 @@ int w_Rasterizer_hasGlyph(lua_State *L) bool hasglyph = false; - try - { + EXCEPT_GUARD( if (lua_type(L, 2) == LUA_TSTRING) hasglyph = t->hasGlyph(luax_checkstring(L, 2)); else hasglyph = t->hasGlyph((uint32) luaL_checknumber(L, 2)); - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + ) luax_pushboolean(L, hasglyph); return 1; diff --git a/src/modules/graphics/opengl/wrap_Canvas.cpp b/src/modules/graphics/opengl/wrap_Canvas.cpp index 02c575387..a53574ea6 100644 --- a/src/modules/graphics/opengl/wrap_Canvas.cpp +++ b/src/modules/graphics/opengl/wrap_Canvas.cpp @@ -46,14 +46,8 @@ int w_Canvas_renderTo(lua_State *L) Canvas *canvas = luax_checkcanvas(L, 1); luaL_checktype(L, 2, LUA_TFUNCTION); - try - { - canvas->startGrab(); - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + EXCEPT_GUARD(canvas->startGrab();) + lua_settop(L, 2); // make sure the function is on top of the stack lua_call(L, 0, 0); canvas->stopGrab(); @@ -76,14 +70,9 @@ int w_Canvas_getPixel(lua_State * L) int x = luaL_checkint(L, 2); int y = luaL_checkint(L, 3); unsigned char c[4]; - try - { - canvas->getPixel(c, x, y); - } - catch (love::Exception & e) - { - return luaL_error(L, "%s", e.what()); - } + + EXCEPT_GUARD(canvas->getPixel(c, x, y);) + lua_pushnumber(L, c[0]); lua_pushnumber(L, c[1]); lua_pushnumber(L, c[2]); diff --git a/src/modules/graphics/opengl/wrap_Font.cpp b/src/modules/graphics/opengl/wrap_Font.cpp index a14d92b2d..38c5fd218 100644 --- a/src/modules/graphics/opengl/wrap_Font.cpp +++ b/src/modules/graphics/opengl/wrap_Font.cpp @@ -44,14 +44,8 @@ int w_Font_getWidth(lua_State *L) { Font *t = luax_checkfont(L, 1); const char *str = luaL_checkstring(L, 2); - try - { - lua_pushinteger(L, t->getWidth(str)); - } - catch(love::Exception &e) - { - return luaL_error(L, e.what()); - } + + EXCEPT_GUARD(lua_pushinteger(L, t->getWidth(str));) return 1; } @@ -61,15 +55,12 @@ int w_Font_getWrap(lua_State *L) const char *str = luaL_checkstring(L, 2); float wrap = (float) luaL_checknumber(L, 3); int max_width = 0, numlines = 0; - try - { + + EXCEPT_GUARD( std::vector lines = t->getWrap(str, wrap, &max_width); numlines = lines.size(); - } - catch(love::Exception &e) - { - return luaL_error(L, e.what()); - } + ) + lua_pushinteger(L, max_width); lua_pushinteger(L, numlines); return 2; @@ -105,15 +96,7 @@ int w_Font_setFilter(lua_State *L) f.anisotropy = (float) luaL_optnumber(L, 4, 1.0); - try - { - t->setFilter(f); - } - catch(love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } - + EXCEPT_GUARD(t->setFilter(f);) return 0; } @@ -157,17 +140,12 @@ int w_Font_hasGlyph(lua_State *L) Font *t = luax_checkfont(L, 1); bool hasglyph = false; - try - { + EXCEPT_GUARD( if (lua_type(L, 2) == LUA_TSTRING) hasglyph = t->hasGlyph(luax_checkstring(L, 2)); else hasglyph = t->hasGlyph((uint32) luaL_checknumber(L, 2)); - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + ) luax_pushboolean(L, hasglyph); return 1; diff --git a/src/modules/graphics/opengl/wrap_Geometry.cpp b/src/modules/graphics/opengl/wrap_Geometry.cpp index 67c6fea14..534a73c30 100644 --- a/src/modules/graphics/opengl/wrap_Geometry.cpp +++ b/src/modules/graphics/opengl/wrap_Geometry.cpp @@ -44,9 +44,9 @@ int w_Geometry_getVertexCount(lua_State *L) int w_Geometry_getVertex(lua_State *L) { Geometry *geom = luax_checkgeometry(L, 1); - size_t i = size_t(luaL_checkint(L, 2)); - try - { + size_t i = size_t(luaL_checkinteger(L, 2)); + + EXCEPT_GUARD( const vertex &v = geom->getVertex(i-1); lua_pushnumber(L, v.x); lua_pushnumber(L, v.y); @@ -56,11 +56,7 @@ int w_Geometry_getVertex(lua_State *L) lua_pushnumber(L, v.g); lua_pushnumber(L, v.b); lua_pushnumber(L, v.a); - } - catch (Exception &e) - { - return luaL_error(L, e.what()); - } + ) return 8; } @@ -100,14 +96,7 @@ int w_Geometry_setVertex(lua_State *L) v.a = luaL_optinteger(L, 10, 255); } - try - { - geom->setVertex(i-1, v); - } - catch (Exception &e) - { - return luaL_error(L, e.what()); - } + EXCEPT_GUARD(geom->setVertex(i-1, v);) if (v.r != 255 || v.g != 255 || v.b != 255 || v.a != 255) geom->setVertexColors(true); @@ -198,14 +187,7 @@ int w_Geometry_setVertexMap(lua_State *L) vertexmap.push_back(luaL_checkinteger(L, i + 2) - 1); } - try - { - g->setElementArray(&vertexmap[0], vertexmap.size()); - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + EXCEPT_GUARD(g->setElementArray(&vertexmap[0], vertexmap.size());) return 0; } diff --git a/src/modules/graphics/opengl/wrap_Graphics.cpp b/src/modules/graphics/opengl/wrap_Graphics.cpp index 233dfd40b..31def66a4 100644 --- a/src/modules/graphics/opengl/wrap_Graphics.cpp +++ b/src/modules/graphics/opengl/wrap_Graphics.cpp @@ -174,29 +174,23 @@ int w_newImage(lua_State *L) else data = luax_checktype(L, 1, "ImageData", IMAGE_IMAGE_DATA_T); + if (!data && !data) + return luaL_error(L, "Error creating image."); + // Create the image. Image *image = 0; - try - { + EXCEPT_GUARD( if (cdata) image = instance->newImage(cdata); else if (data) image = instance->newImage(data); - else - throw love::Exception("Error creating image."); - } - catch(love::Exception &e) - { - luaL_error(L, e.what()); - } + ) if (image == 0) return luaL_error(L, "Could not load image."); - // Push the type. luax_pushtype(L, "Image", GRAPHICS_IMAGE_T, image); - return 1; } @@ -321,14 +315,7 @@ int w_newGeometry(lua_State *L) } Geometry *geom = 0; - try - { - geom = instance->newGeometry(vertices, vertexmap, mode); - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + EXCEPT_GUARD(geom = instance->newGeometry(vertices, vertexmap, mode);) if (geom == 0) return luaL_error(L, "Could not create geometry."); @@ -337,6 +324,7 @@ int w_newGeometry(lua_State *L) // Note: This should be changed to luax_pushtype if the new Geometry is ever // expected to be pushed to Lua from another C++ function! + // We're only using rawnewtype instead of pushtype for performance. luax_rawnewtype(L, "Geometry", GRAPHICS_GEOMETRY_T, geom); return 1; } @@ -354,6 +342,7 @@ int w_newQuad(lua_State *L) // Note: This should be changed to luax_pushtype if the new Geometry is ever // expected to be pushed to Lua from another C++ function! + // We're only using rawnewtype instead of pushtype for performance. luax_rawnewtype(L, "Geometry", GRAPHICS_GEOMETRY_T, quad); return 1; } @@ -373,23 +362,14 @@ int w_newFont(lua_State *L) love::font::Rasterizer *rasterizer = luax_checktype(L, 1, "Rasterizer", FONT_RASTERIZER_T); - Font *font = NULL; - try - { - // Create the font. - font = instance->newFont(rasterizer, instance->getDefaultFilter()); - } - catch (love::Exception &e) - { - return luaL_error(L, e.what()); - } + Font *font = 0; + EXCEPT_GUARD(font = instance->newFont(rasterizer, instance->getDefaultFilter());) if (font == 0) return luaL_error(L, "Could not load font."); // Push the type. luax_pushtype(L, "Font", GRAPHICS_FONT_T, font); - return 1; } @@ -444,15 +424,10 @@ int w_newSpriteBatch(lua_State *L) if (!SpriteBatch::getConstant(usagestr, usage)) return luaL_error(L, "Invalid SpriteBatch usage hint: %s", usagestr); } - SpriteBatch *t = NULL; - try - { - t = instance->newSpriteBatch(image, size, usage); - } - catch(love::Exception &e) - { - return luaL_error(L, e.what()); - } + + SpriteBatch *t = 0; + EXCEPT_GUARD(t = instance->newSpriteBatch(image, size, usage);) + luax_pushtype(L, "SpriteBatch", GRAPHICS_SPRITE_BATCH_T, t); return 1; } @@ -464,14 +439,9 @@ int w_newParticleSystem(lua_State *L) ParticleSystem *t = 0; if (size < 1.0 || size > LOVE_UINT32_MAX) return luaL_error(L, "Invalid ParticleSystem size"); - try - { - t = instance->newParticleSystem(image, size); - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + + EXCEPT_GUARD(t = instance->newParticleSystem(image, size);) + luax_pushtype(L, "ParticleSystem", GRAPHICS_PARTICLE_SYSTEM_T, t); return 1; } @@ -487,17 +457,10 @@ int w_newCanvas(lua_State *L) if (!Canvas::getConstant(str, texture_type)) return luaL_error(L, "Invalid canvas type: %s", str); - Canvas *canvas = NULL; - try - { - canvas = instance->newCanvas(width, height, texture_type); - } - catch(Exception &e) - { - return luaL_error(L, e.what()); - } + Canvas *canvas = 0; + EXCEPT_GUARD(canvas = instance->newCanvas(width, height, texture_type);) - if (NULL == canvas) + if (canvas == 0) return luaL_error(L, "Canvas not created, but no error thrown. I don't even..."); luax_pushtype(L, "Canvas", GRAPHICS_CANVAS_T, canvas); @@ -582,21 +545,25 @@ int w_newShader(lua_State *L) } } + bool should_error = false; try { Shader *shader = instance->newShader(sources); luax_pushtype(L, "Shader", GRAPHICS_SHADER_T, shader); } - catch (const love::Exception &e) + catch (love::Exception &e) { - // memory is freed in Graphics::newShader luax_getfunction(L, "graphics", "_transformGLSLErrorMessages"); lua_pushstring(L, e.what()); + + // Function pushes the new error string onto the stack. lua_pcall(L, 1, 1, 0); - const char *err = lua_tostring(L, -1); - return luaL_error(L, "%s", err); + should_error = true; } + if (should_error) + return lua_error(L); + return 1; } @@ -729,32 +696,22 @@ int w_setBlendMode(lua_State *L) if (!Graphics::getConstant(str, mode)) return luaL_error(L, "Invalid blend mode: %s", str); - try - { - instance->setBlendMode(mode); - } - catch(love::Exception &e) - { - return luaL_error(L, e.what()); - } + EXCEPT_GUARD(instance->setBlendMode(mode);) return 0; } int w_getBlendMode(lua_State *L) { - try - { - Graphics::BlendMode mode = instance->getBlendMode(); - const char *str; - if (!Graphics::getConstant(mode, str)) - return luaL_error(L, "Unknown blend mode"); - lua_pushstring(L, str); - return 1; - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + const char *str; + Graphics::BlendMode mode; + + EXCEPT_GUARD(mode = instance->getBlendMode();) + + if (!Graphics::getConstant(mode, str)) + return luaL_error(L, "Unknown blend mode"); + + lua_pushstring(L, str); + return 1; } int w_setDefaultFilter(lua_State *L) @@ -933,14 +890,9 @@ int w_newScreenshot(lua_State *L) love::image::Image *image = luax_getmodule(L, "image", MODULE_IMAGE_T); bool copyAlpha = luax_optboolean(L, 1, false); love::image::ImageData *i = 0; - try - { - i = instance->newScreenshot(image, copyAlpha); - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + + EXCEPT_GUARD(i = instance->newScreenshot(image, copyAlpha);) + luax_pushtype(L, "ImageData", IMAGE_IMAGE_DATA_T, i); return 1; } @@ -983,17 +935,12 @@ int w_setCanvas(lua_State *L) attachments.push_back(luax_checkcanvas(L, i)); } - try - { + EXCEPT_GUARD( if (attachments.size() > 0) canvas->startGrab(attachments); else canvas->startGrab(); - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + ) return 0; } @@ -1116,15 +1063,7 @@ int w_getRendererInfo(lua_State *L) if (!Graphics::getConstant(str, infotype)) return luaL_error(L, "Invalid renderer info type: %s", str); - try - { - luax_pushstring(L, instance->getRendererInfo(infotype)); - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } - + EXCEPT_GUARD(luax_pushstring(L, instance->getRendererInfo(infotype));) return 1; } @@ -1177,14 +1116,8 @@ int w_print(lua_State *L) float oy = (float)luaL_optnumber(L, 8, 0.0f); float kx = (float)luaL_optnumber(L, 9, 0.0f); float ky = (float)luaL_optnumber(L, 10, 0.0f); - try - { - instance->print(str, x, y, angle, sx, sy, ox, oy, kx,ky); - } - catch(love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + + EXCEPT_GUARD(instance->print(str, x, y, angle, sx, sy, ox, oy, kx,ky);) return 0; } @@ -1220,14 +1153,7 @@ int w_printf(lua_State *L) ky = (float) luaL_optnumber(L, 12, 0.0f); } - try - { - instance->printf(str, x, y, wrap, align, angle, sx, sy, ox, oy, kx, ky); - } - catch(love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + EXCEPT_GUARD(instance->printf(str, x, y, wrap, align, angle, sx, sy, ox, oy, kx, ky);) return 0; } @@ -1382,27 +1308,13 @@ int w_polygon(lua_State *L) int w_push(lua_State *L) { - try - { - instance->push(); - } - catch(love::Exception e) - { - return luaL_error(L, e.what()); - } + EXCEPT_GUARD(instance->push();) return 0; } int w_pop(lua_State *L) { - try - { - instance->pop(); - } - catch(love::Exception e) - { - return luaL_error(L, e.what()); - } + EXCEPT_GUARD(instance->pop();) return 0; } @@ -1551,14 +1463,7 @@ extern "C" int luaopen_love_graphics(lua_State *L) { if (instance == 0) { - try - { - instance = new Graphics(); - } - catch (love::Exception &e) - { - return luaL_error(L, e.what()); - } + EXCEPT_GUARD(instance = new Graphics();) } else instance->retain(); diff --git a/src/modules/graphics/opengl/wrap_Image.cpp b/src/modules/graphics/opengl/wrap_Image.cpp index a53aa96dd..496a3cb70 100644 --- a/src/modules/graphics/opengl/wrap_Image.cpp +++ b/src/modules/graphics/opengl/wrap_Image.cpp @@ -70,15 +70,7 @@ int w_Image_setFilter(lua_State *L) f.anisotropy = (float) luaL_optnumber(L, 4, 1.0); - try - { - t->setFilter(f); - } - catch(love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } - + EXCEPT_GUARD(t->setFilter(f);) return 0; } @@ -110,19 +102,11 @@ int w_Image_setMipmapFilter(lua_State *L) return luaL_error(L, "Invalid filter mode: %s", mipmapstr); } - try - { - t->setFilter(f); - } - catch(love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + EXCEPT_GUARD(t->setFilter(f);) float sharpness = luaL_optnumber(L, 3, 0); - t->setMipmapSharpness(sharpness); - + return 0; } @@ -139,7 +123,6 @@ int w_Image_getMipmapFilter(lua_State *L) lua_pushnil(L); // only return a mipmap filter if mipmapping is enabled lua_pushnumber(L, t->getMipmapSharpness()); - return 2; } @@ -185,14 +168,7 @@ int w_Image_isCompressed(lua_State *L) int w_Image_refresh(lua_State *L) { Image *i = luax_checkimage(L, 1); - try - { - i->refresh(); - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + EXCEPT_GUARD(i->refresh();) return 0; } diff --git a/src/modules/graphics/opengl/wrap_ParticleSystem.cpp b/src/modules/graphics/opengl/wrap_ParticleSystem.cpp index a8028120c..6b053134e 100644 --- a/src/modules/graphics/opengl/wrap_ParticleSystem.cpp +++ b/src/modules/graphics/opengl/wrap_ParticleSystem.cpp @@ -59,14 +59,8 @@ int w_ParticleSystem_setBufferSize(lua_State *L) lua_Number arg1 = luaL_checknumber(L, 2); if (arg1 < 1.0 || arg1 > ParticleSystem::MAX_PARTICLES) return luaL_error(L, "Invalid buffer size"); - try - { - t->setBufferSize((uint32) arg1); - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + + EXCEPT_GUARD(t->setBufferSize((uint32) arg1);) return 0; } @@ -104,14 +98,7 @@ int w_ParticleSystem_setEmissionRate(lua_State *L) { ParticleSystem *t = luax_checkparticlesystem(L, 1); int arg1 = luaL_checkint(L, 2); - try - { - t->setEmissionRate(arg1); - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + EXCEPT_GUARD(t->setEmissionRate(arg1);) return 0; } diff --git a/src/modules/graphics/opengl/wrap_Shader.cpp b/src/modules/graphics/opengl/wrap_Shader.cpp index 3c9407ad4..a5647706a 100644 --- a/src/modules/graphics/opengl/wrap_Shader.cpp +++ b/src/modules/graphics/opengl/wrap_Shader.cpp @@ -130,18 +130,22 @@ int w_Shader_sendInt(lua_State *L) if (!values) return luaL_error(L, "Error in arguments."); + bool should_error = false; try { shader->sendInt(name, dimension, values, count); } catch (love::Exception &e) { - delete[] values; - return luaL_error(L, "%s", e.what()); + should_error = true; + lua_pushstring(L, e.what()); } delete[] values; + if (should_error) + return lua_error(L); + return 0; } @@ -167,18 +171,22 @@ int w_Shader_sendFloat(lua_State *L) if (!values) return luaL_error(L, "Error in arguments."); + bool should_error = false; try { shader->sendFloat(name, dimension, values, count); } catch (love::Exception &e) { - delete[] values; - return luaL_error(L, "%s", e.what()); + should_error = true; + lua_pushstring(L, e.what()); } delete[] values; + if (should_error) + return lua_error(L); + return 0; } @@ -225,17 +233,23 @@ int w_Shader_sendMatrix(lua_State *L) lua_pop(L, 1 + dimension); } + bool should_error = false; + try { shader->sendMatrix(name, dimension, values, count); } catch(love::Exception &e) { - delete[] values; - return luaL_error(L, "%s", e.what()); + should_error = true; + lua_pushstring(L, e.what()); } delete[] values; + + if (should_error) + return lua_error(L); + return 0; } @@ -245,15 +259,7 @@ int w_Shader_sendImage(lua_State *L) const char *name = luaL_checkstring(L, 2); Image *img = luax_checkimage(L, 3); - try - { - shader->sendImage(name, *img); - } - catch(love::Exception &e) - { - luaL_error(L, "%s", e.what()); - } - + EXCEPT_GUARD(shader->sendImage(name, *img);) return 0; } @@ -263,15 +269,7 @@ int w_Shader_sendCanvas(lua_State *L) const char *name = luaL_checkstring(L, 2); Canvas *canvas = luax_checkcanvas(L, 3); - try - { - shader->sendCanvas(name, *canvas); - } - catch(love::Exception &e) - { - luaL_error(L, "%s", e.what()); - } - + EXCEPT_GUARD(shader->sendCanvas(name, *canvas);) return 0; } diff --git a/src/modules/graphics/opengl/wrap_SpriteBatch.cpp b/src/modules/graphics/opengl/wrap_SpriteBatch.cpp index ba99958b5..5968877c8 100644 --- a/src/modules/graphics/opengl/wrap_SpriteBatch.cpp +++ b/src/modules/graphics/opengl/wrap_SpriteBatch.cpp @@ -56,17 +56,12 @@ int w_SpriteBatch_add(lua_State *L) float ky = (float) luaL_optnumber(L, startidx + 8, 0.0); int id = 0; - try - { + EXCEPT_GUARD( if (geom) id = t->addg(geom, x, y, a, sx, sy, ox, oy, kx, ky); else id = t->add(x, y, a, sx, sy, ox, oy, kx, ky); - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + ) lua_pushinteger(L, id); return 1; @@ -96,17 +91,12 @@ int w_SpriteBatch_set(lua_State *L) float kx = (float) luaL_optnumber(L, startidx + 7, 0.0); float ky = (float) luaL_optnumber(L, startidx + 8, 0.0); - try - { + EXCEPT_GUARD( if (geom) t->addg(geom, x, y, a, sx, sy, ox, oy, kx, ky, id); else t->add(x, y, a, sx, sy, ox, oy, kx, ky, id); - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + ) return 0; } @@ -121,14 +111,7 @@ int w_SpriteBatch_clear(lua_State *L) int w_SpriteBatch_bind(lua_State *L) { SpriteBatch *t = luax_checkspritebatch(L, 1); - try - { - t->lock(); - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + EXCEPT_GUARD(t->lock();) return 0; } @@ -219,14 +202,7 @@ int w_SpriteBatch_setBufferSize(lua_State *L) { SpriteBatch *t = luax_checkspritebatch(L, 1); int size = luaL_checkint(L, 2); - try - { - t->setBufferSize(size); - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + EXCEPT_GUARD(t->setBufferSize(size);) return 0; } diff --git a/src/modules/image/wrap_CompressedData.cpp b/src/modules/image/wrap_CompressedData.cpp index 64764a6f0..968350c7d 100644 --- a/src/modules/image/wrap_CompressedData.cpp +++ b/src/modules/image/wrap_CompressedData.cpp @@ -36,14 +36,9 @@ int w_CompressedData_getWidth(lua_State *L) CompressedData *t = luax_checkcompresseddata(L, 1); int miplevel = luaL_optinteger(L, 2, 1); int width = 0; - try - { - width = t->getWidth(miplevel >= 1 ? miplevel - 1 : 0); - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + + EXCEPT_GUARD(width = t->getWidth(miplevel >= 1 ? miplevel - 1 : 0);) + lua_pushinteger(L, width); return 1; } @@ -53,14 +48,9 @@ int w_CompressedData_getHeight(lua_State *L) CompressedData *t = luax_checkcompresseddata(L, 1); int miplevel = luaL_optinteger(L, 2, 1); int height = 0; - try - { - height = t->getHeight(miplevel >= 1 ? miplevel - 1 : 0); - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + + EXCEPT_GUARD(height = t->getHeight(miplevel >= 1 ? miplevel - 1 : 0);) + lua_pushinteger(L, height); return 1; } @@ -70,15 +60,12 @@ int w_CompressedData_getDimensions(lua_State *L) CompressedData *t = luax_checkcompresseddata(L, 1); int miplevel = luaL_optinteger(L, 2, 1); int width = 0, height = 0; - try - { + + EXCEPT_GUARD( width = t->getWidth(miplevel >= 1 ? miplevel - 1 : 0); height = t->getHeight(miplevel >= 1 ? miplevel - 1 : 0); - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + ) + lua_pushinteger(L, width); lua_pushinteger(L, height); return 2; diff --git a/src/modules/image/wrap_Image.cpp b/src/modules/image/wrap_Image.cpp index 1f075f506..56c9e619e 100644 --- a/src/modules/image/wrap_Image.cpp +++ b/src/modules/image/wrap_Image.cpp @@ -43,14 +43,8 @@ int w_newImageData(lua_State *L) return luaL_error(L, "Invalid image size."); ImageData *t = 0; - try - { - t = instance->newImageData(w, h); - } - catch(love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + EXCEPT_GUARD(t = instance->newImageData(w, h);) + luax_pushtype(L, "ImageData", IMAGE_IMAGE_DATA_T, t); return 1; } @@ -64,17 +58,9 @@ int w_newImageData(lua_State *L) love::filesystem::FileData *data = luax_checktype(L, 1, "FileData", FILESYSTEM_FILE_DATA_T); ImageData *t = 0; - try - { - t = instance->newImageData(data); - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + EXCEPT_GUARD(t = instance->newImageData(data);) luax_pushtype(L, "ImageData", IMAGE_IMAGE_DATA_T, t); - return 1; } @@ -87,17 +73,9 @@ int w_newCompressedData(lua_State *L) love::filesystem::FileData *data = luax_checktype(L, 1, "FileData", FILESYSTEM_FILE_DATA_T); CompressedData *t = 0; - try - { - t = instance->newCompressedData(data); - } - catch(love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + EXCEPT_GUARD(t = instance->newCompressedData(data);) luax_pushtype(L, "CompressedData", IMAGE_COMPRESSED_DATA_T, t); - return 1; } @@ -110,14 +88,8 @@ int w_isCompressed(lua_State *L) love::filesystem::FileData *data = luax_checktype(L, 1, "FileData", FILESYSTEM_FILE_DATA_T); bool compressed = false; - try - { - compressed = instance->isCompressed(data); - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + EXCEPT_GUARD(compressed = instance->isCompressed(data);) + luax_pushboolean(L, compressed); return 1; } @@ -142,14 +114,7 @@ extern "C" int luaopen_love_image(lua_State *L) { if (instance == 0) { - try - { - instance = new love::image::magpie::Image(); - } - catch(Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + EXCEPT_GUARD(instance = new love::image::magpie::Image();) } else instance->retain(); diff --git a/src/modules/image/wrap_ImageData.cpp b/src/modules/image/wrap_ImageData.cpp index be9efe621..d47fbbc77 100644 --- a/src/modules/image/wrap_ImageData.cpp +++ b/src/modules/image/wrap_ImageData.cpp @@ -61,14 +61,9 @@ int w_ImageData_getPixel(lua_State *L) int x = luaL_checkint(L, 2); int y = luaL_checkint(L, 3); pixel c; - try - { - c = t->getPixel(x, y); - } - catch(love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + + EXCEPT_GUARD(c = t->getPixel(x, y);) + lua_pushnumber(L, c.r); lua_pushnumber(L, c.g); lua_pushnumber(L, c.b); @@ -103,14 +98,7 @@ int w_ImageData_setPixel(lua_State *L) c.a = (unsigned char)luaL_optinteger(L, 7, 255); } - try - { - t->setPixel(x, y, c); - } - catch(love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + EXCEPT_GUARD(t->setPixel(x, y, c);) return 0; } @@ -281,14 +269,7 @@ int w_ImageData_encode(lua_State *L) return luaL_error(L, "Invalid image format '%s'.", fmt); } - try - { - t->encode(file, format); - } - catch(love::Exception &e) - { - return luaL_error(L, e.what()); - } + EXCEPT_GUARD(t->encode(file, format);) return 0; } diff --git a/src/modules/joystick/sdl/wrap_JoystickModule.cpp b/src/modules/joystick/sdl/wrap_JoystickModule.cpp index c01a38a2d..a9ffca109 100644 --- a/src/modules/joystick/sdl/wrap_JoystickModule.cpp +++ b/src/modules/joystick/sdl/wrap_JoystickModule.cpp @@ -68,7 +68,7 @@ int w_setGamepadMapping(lua_State *L) // Only accept a GUID string. We don't accept a Joystick object because // the gamepad mapping applies to all joysticks with the same GUID (e.g. all // Xbox 360 controllers on the system), rather than individual objects. - std::string guid = luax_checkstring(L, 1); + const char *guid = luaL_checkstring(L, 1); const char *gpbindstr = luaL_checkstring(L, 2); Joystick::GamepadInput gpinput; @@ -107,14 +107,8 @@ int w_setGamepadMapping(lua_State *L) } bool success = false; - try - { - success = instance->setGamepadMapping(guid, gpinput, jinput); - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + EXCEPT_GUARD(success = instance->setGamepadMapping(guid, gpinput, jinput);) + luax_pushboolean(L, success); return 1; } @@ -146,14 +140,7 @@ int w_getGamepadMapping(lua_State *L) Joystick::JoystickInput jinput; jinput.type = Joystick::INPUT_TYPE_MAX_ENUM; - try - { - jinput = instance->getGamepadMapping(guid, gpinput); - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + EXCEPT_GUARD(jinput = instance->getGamepadMapping(guid, gpinput);) if (jinput.type == Joystick::INPUT_TYPE_MAX_ENUM) return 0; @@ -209,19 +196,11 @@ extern "C" int luaopen_love_joystick(lua_State *L) { if (instance == 0) { - try - { - instance = new JoystickModule(); - } - catch (Exception &e) - { - return luaL_error(L, e.what()); - } + EXCEPT_GUARD(instance = new JoystickModule();) } else instance->retain(); - WrappedModule w; w.module = instance; w.name = "joystick"; diff --git a/src/modules/keyboard/wrap_Keyboard.cpp b/src/modules/keyboard/wrap_Keyboard.cpp index 2b638f59e..32dddbe9f 100644 --- a/src/modules/keyboard/wrap_Keyboard.cpp +++ b/src/modules/keyboard/wrap_Keyboard.cpp @@ -89,14 +89,7 @@ extern "C" int luaopen_love_keyboard(lua_State *L) { if (instance == 0) { - try - { - instance = new love::keyboard::sdl::Keyboard(); - } - catch(Exception &e) - { - return luaL_error(L, e.what()); - } + EXCEPT_GUARD(instance = new love::keyboard::sdl::Keyboard();) } else instance->retain(); diff --git a/src/modules/math/wrap_BezierCurve.cpp b/src/modules/math/wrap_BezierCurve.cpp index 9b7b7db35..e712521f5 100644 --- a/src/modules/math/wrap_BezierCurve.cpp +++ b/src/modules/math/wrap_BezierCurve.cpp @@ -56,16 +56,11 @@ int w_BezierCurve_getControlPoint(lua_State *L) if (idx > 0) // 1-indexing idx--; - try - { + EXCEPT_GUARD( Vector v = curve->getControlPoint(idx); lua_pushnumber(L, v.x); lua_pushnumber(L, v.y); - } - catch (Exception &e) - { - return luaL_error(L, e.what()); - } + ) return 2; } @@ -80,15 +75,7 @@ int w_BezierCurve_setControlPoint(lua_State *L) if (idx > 0) // 1-indexing idx--; - try - { - curve->setControlPoint(idx, Vector(vx,vy)); - } - catch (Exception &e) - { - return luaL_error(L, e.what()); - } - + EXCEPT_GUARD(curve->setControlPoint(idx, Vector(vx,vy));) return 0; } @@ -102,15 +89,7 @@ int w_BezierCurve_insertControlPoint(lua_State *L) if (idx > 0) // 1-indexing idx--; - try - { - curve->insertControlPoint(Vector(vx,vy), idx); - } - catch (Exception &e) - { - return luaL_error(L, e.what()); - } - + EXCEPT_GUARD(curve->insertControlPoint(Vector(vx,vy), idx);) return 0; } @@ -148,16 +127,11 @@ int w_BezierCurve_eval(lua_State *L) BezierCurve *curve = luax_checkbeziercurve(L, 1); double t = luaL_checknumber(L, 2); - try - { + EXCEPT_GUARD( Vector v = curve->eval(t); lua_pushnumber(L, v.x); lua_pushnumber(L, v.y); - } - catch (Exception &e) - { - return luaL_error(L, e.what()); - } + ) return 2; @@ -169,14 +143,7 @@ int w_BezierCurve_render(lua_State *L) int accuracy = luaL_optinteger(L, 2, 5); std::vector points; - try - { - points = curve->render(accuracy); - } - catch (Exception &e) - { - return luaL_error(L, e.what()); - } + EXCEPT_GUARD(points = curve->render(accuracy);) lua_createtable(L, points.size()*2, 0); for (size_t i = 0; i < points.size(); ++i) diff --git a/src/modules/math/wrap_Math.cpp b/src/modules/math/wrap_Math.cpp index 2a245a077..3c7abb306 100644 --- a/src/modules/math/wrap_Math.cpp +++ b/src/modules/math/wrap_Math.cpp @@ -34,14 +34,7 @@ namespace math int w_setRandomState(lua_State *L) { - try - { - Math::instance.setRandomState(luax_checkrandomstate(L, 1)); - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + EXCEPT_GUARD(Math::instance.setRandomState(luax_checkrandomstate(L, 1));) return 0; } @@ -79,6 +72,8 @@ int w_newRandomGenerator(lua_State *L) if (lua_gettop(L) > 0) { + bool should_error = false; + try { t->setState(s); @@ -86,8 +81,12 @@ int w_newRandomGenerator(lua_State *L) catch (love::Exception &e) { t->release(); - return luaL_error(L, "%s", e.what()); + should_error = true; + lua_pushstring(L, e.what()); } + + if (should_error) + return lua_error(L); } luax_pushtype(L, "RandomGenerator", MATH_RANDOM_GENERATOR_T, t); @@ -169,17 +168,13 @@ int w_triangulate(lua_State *L) return luaL_error(L, "Need at least 3 vertices to triangulate"); std::vector triangles; - try - { + + EXCEPT_GUARD( if (vertices.size() == 3) triangles.push_back(Triangle(vertices[0], vertices[1], vertices[2])); else triangles = Math::instance.triangulate(vertices); - } - catch (love::Exception &e) - { - return luaL_error(L, e.what()); - } + ) lua_createtable(L, triangles.size(), 0); for (size_t i = 0; i < triangles.size(); ++i) diff --git a/src/modules/math/wrap_RandomGenerator.cpp b/src/modules/math/wrap_RandomGenerator.cpp index c3f7f7576..1056d6774 100644 --- a/src/modules/math/wrap_RandomGenerator.cpp +++ b/src/modules/math/wrap_RandomGenerator.cpp @@ -98,14 +98,7 @@ RandomGenerator *luax_checkrandomgenerator(lua_State *L, int idx) int w_RandomGenerator_setState(lua_State *L) { RandomGenerator *rng = luax_checkrandomgenerator(L, 1); - try - { - rng->setState(luax_checkrandomstate(L, 2)); - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + EXCEPT_GUARD(rng->setState(luax_checkrandomstate(L, 2));) return 0; } diff --git a/src/modules/mouse/wrap_Mouse.cpp b/src/modules/mouse/wrap_Mouse.cpp index f543489f2..6900cd6ac 100644 --- a/src/modules/mouse/wrap_Mouse.cpp +++ b/src/modules/mouse/wrap_Mouse.cpp @@ -45,14 +45,7 @@ int w_newCursor(lua_State *L) if (!Cursor::getConstant(str, systemCursor)) return luaL_error(L, "Invalid cursor type %s", str); - try - { - cursor = instance->newCursor(systemCursor); - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + EXCEPT_GUARD(cursor = instance->newCursor(systemCursor);) } else { @@ -61,14 +54,7 @@ int w_newCursor(lua_State *L) int hotx = luaL_optint(L, 2, 0); int hoty = luaL_optint(L, 3, 0); - try - { - cursor = instance->newCursor(data, hotx, hoty); - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + EXCEPT_GUARD(cursor = instance->newCursor(data, hotx, hoty);) } luax_pushtype(L, "Cursor", MOUSE_CURSOR_T, cursor); @@ -223,14 +209,7 @@ extern "C" int luaopen_love_mouse(lua_State *L) { if (instance == 0) { - try - { - instance = new love::mouse::sdl::Mouse(); - } - catch(Exception &e) - { - return luaL_error(L, e.what()); - } + EXCEPT_GUARD(instance = new love::mouse::sdl::Mouse();) } else instance->retain(); diff --git a/src/modules/physics/box2d/Physics.cpp b/src/modules/physics/box2d/Physics.cpp index b39aab489..f3e2f1909 100644 --- a/src/modules/physics/box2d/Physics.cpp +++ b/src/modules/physics/box2d/Physics.cpp @@ -254,8 +254,8 @@ int Physics::getDistance(lua_State *L) b2DistanceOutput o; b2SimplexCache c; c.count = 0; - try - { + + EXCEPT_GUARD( pA.Set(fixtureA->fixture->GetShape(), 0); pB.Set(fixtureB->fixture->GetShape(), 0); i.proxyA = pA; @@ -264,11 +264,8 @@ int Physics::getDistance(lua_State *L) i.transformB = fixtureB->fixture->GetBody()->GetTransform(); i.useRadii = true; b2Distance(&o, &c, &i); - } - catch (love::Exception &e) - { - luaL_error(L, "%s", e.what()); - } + ) + lua_pushnumber(L, Physics::scaleUp(o.distance)); lua_pushnumber(L, Physics::scaleUp(o.pointA.x)); lua_pushnumber(L, Physics::scaleUp(o.pointA.y)); diff --git a/src/modules/physics/box2d/wrap_Body.cpp b/src/modules/physics/box2d/wrap_Body.cpp index b2e90a75d..e78b849fd 100644 --- a/src/modules/physics/box2d/wrap_Body.cpp +++ b/src/modules/physics/box2d/wrap_Body.cpp @@ -232,7 +232,7 @@ int w_Body_setX(lua_State *L) { Body *t = luax_checkbody(L, 1); float arg1 = (float)luaL_checknumber(L, 2); - ASSERT_GUARD(t->setX(arg1);) + EXCEPT_GUARD(t->setX(arg1);) return 0; } @@ -240,7 +240,7 @@ int w_Body_setY(lua_State *L) { Body *t = luax_checkbody(L, 1); float arg1 = (float)luaL_checknumber(L, 2); - ASSERT_GUARD(t->setY(arg1);) + EXCEPT_GUARD(t->setY(arg1);) return 0; } @@ -257,7 +257,7 @@ int w_Body_setAngle(lua_State *L) { Body *t = luax_checkbody(L, 1); float arg1 = (float)luaL_checknumber(L, 2); - ASSERT_GUARD(t->setAngle(arg1);) + EXCEPT_GUARD(t->setAngle(arg1);) return 0; } @@ -274,14 +274,14 @@ int w_Body_setPosition(lua_State *L) Body *t = luax_checkbody(L, 1); float arg1 = (float)luaL_checknumber(L, 2); float arg2 = (float)luaL_checknumber(L, 3); - ASSERT_GUARD(t->setPosition(arg1, arg2);) + EXCEPT_GUARD(t->setPosition(arg1, arg2);) return 0; } int w_Body_resetMassData(lua_State *L) { Body *t = luax_checkbody(L, 1); - ASSERT_GUARD(t->resetMassData();) + EXCEPT_GUARD(t->resetMassData();) return 0; } @@ -292,7 +292,7 @@ int w_Body_setMassData(lua_State *L) float y = (float)luaL_checknumber(L, 3); float m = (float)luaL_checknumber(L, 4); float i = (float)luaL_checknumber(L, 5); - ASSERT_GUARD(t->setMassData(x, y, m, i);) + EXCEPT_GUARD(t->setMassData(x, y, m, i);) return 0; } @@ -300,7 +300,7 @@ int w_Body_setMass(lua_State *L) { Body *t = luax_checkbody(L, 1); float m = (float)luaL_checknumber(L, 2); - ASSERT_GUARD(t->setMass(m);) + EXCEPT_GUARD(t->setMass(m);) return 0; } @@ -308,7 +308,7 @@ int w_Body_setInertia(lua_State *L) { Body *t = luax_checkbody(L, 1); float i = (float)luaL_checknumber(L, 2); - ASSERT_GUARD(t->setInertia(i);) + EXCEPT_GUARD(t->setInertia(i);) return 0; } @@ -342,7 +342,7 @@ int w_Body_setType(lua_State *L) const char *typeStr = luaL_checkstring(L, 2); Body::Type type; Body::getConstant(typeStr, type); - ASSERT_GUARD(t->setType(type);) + EXCEPT_GUARD(t->setType(type);) return 0; } @@ -485,7 +485,7 @@ int w_Body_setActive(lua_State *L) { Body *t = luax_checkbody(L, 1); bool b = luax_toboolean(L, 2); - ASSERT_GUARD(t->setActive(b);) + EXCEPT_GUARD(t->setActive(b);) return 0; } @@ -501,7 +501,7 @@ int w_Body_setFixedRotation(lua_State *L) { Body *t = luax_checkbody(L, 1); bool b = luax_toboolean(L, 2); - ASSERT_GUARD(t->setFixedRotation(b);) + EXCEPT_GUARD(t->setFixedRotation(b);) return 0; } @@ -518,21 +518,14 @@ int w_Body_getFixtureList(lua_State *L) Body *t = luax_checkbody(L, 1); lua_remove(L, 1); int n = 0; - ASSERT_GUARD(n = t->getFixtureList(L);) + EXCEPT_GUARD(n = t->getFixtureList(L);) return n; } int w_Body_destroy(lua_State *L) { Body *t = luax_checkbody(L, 1); - try - { - t->destroy(); - } - catch(love::Exception &e) - { - luaL_error(L, "%s", e.what()); - } + EXCEPT_GUARD(t->destroy();) return 0; } diff --git a/src/modules/physics/box2d/wrap_ChainShape.cpp b/src/modules/physics/box2d/wrap_ChainShape.cpp index bebd030d9..da3f0d2ae 100644 --- a/src/modules/physics/box2d/wrap_ChainShape.cpp +++ b/src/modules/physics/box2d/wrap_ChainShape.cpp @@ -39,7 +39,7 @@ int w_ChainShape_setNextVertex(lua_State *L) ChainShape *c = luax_checkchainshape(L, 1); float x = (float)luaL_checknumber(L, 2); float y = (float)luaL_checknumber(L, 3); - ASSERT_GUARD(c->setNextVertex(x, y);) + EXCEPT_GUARD(c->setNextVertex(x, y);) return 0; } @@ -48,7 +48,7 @@ int w_ChainShape_setPrevVertex(lua_State *L) ChainShape *c = luax_checkchainshape(L, 1); float x = (float)luaL_checknumber(L, 2); float y = (float)luaL_checknumber(L, 3); - ASSERT_GUARD(c->setPrevVertex(x, y);) + EXCEPT_GUARD(c->setPrevVertex(x, y);) return 0; } @@ -64,7 +64,7 @@ int w_ChainShape_getChildEdge(lua_State *L) ChainShape *c = luax_checkchainshape(L, 1); int index = luaL_checkint(L, 2) - 1; // Convert from 1-based index EdgeShape *e = 0; - ASSERT_GUARD(e = c->getChildEdge(index);) + EXCEPT_GUARD(e = c->getChildEdge(index);) luax_pushtype(L, "EdgeShape", PHYSICS_EDGE_SHAPE_T, e); return 1; } @@ -82,7 +82,7 @@ int w_ChainShape_getPoint(lua_State *L) ChainShape *c = luax_checkchainshape(L, 1); int index = luaL_checkint(L, 2) - 1; // Convert from 1-based index b2Vec2 v; - ASSERT_GUARD(v = c->getPoint(index);) + EXCEPT_GUARD(v = c->getPoint(index);) lua_pushnumber(L, v.x); lua_pushnumber(L, v.y); return 2; diff --git a/src/modules/physics/box2d/wrap_Fixture.cpp b/src/modules/physics/box2d/wrap_Fixture.cpp index 2b4698bc2..2b35090c7 100644 --- a/src/modules/physics/box2d/wrap_Fixture.cpp +++ b/src/modules/physics/box2d/wrap_Fixture.cpp @@ -65,7 +65,7 @@ int w_Fixture_setDensity(lua_State *L) { Fixture *t = luax_checkfixture(L, 1); float arg1 = (float)luaL_checknumber(L, 2); - ASSERT_GUARD(t->setDensity(arg1);) + EXCEPT_GUARD(t->setDensity(arg1);) return 0; } @@ -156,7 +156,9 @@ int w_Fixture_rayCast(lua_State *L) { Fixture *t = luax_checkfixture(L, 1); lua_remove(L, 1); - ASSERT_GUARD(return t->rayCast(L);) + int ret = 0; + EXCEPT_GUARD(ret = t->rayCast(L);) + return ret; } int w_Fixture_setFilterData(lua_State *L) @@ -256,14 +258,7 @@ int w_Fixture_setGroupIndex(lua_State *L) int w_Fixture_destroy(lua_State *L) { Fixture *t = luax_checkfixture(L, 1); - try - { - t->destroy(); - } - catch(love::Exception &e) - { - luaL_error(L, "%s", e.what()); - } + EXCEPT_GUARD(t->destroy();) return 0; } diff --git a/src/modules/physics/box2d/wrap_FrictionJoint.cpp b/src/modules/physics/box2d/wrap_FrictionJoint.cpp index 89cc109f8..e0c0eb900 100644 --- a/src/modules/physics/box2d/wrap_FrictionJoint.cpp +++ b/src/modules/physics/box2d/wrap_FrictionJoint.cpp @@ -40,7 +40,7 @@ int w_FrictionJoint_setMaxForce(lua_State *L) { FrictionJoint *t = luax_checkfrictionjoint(L, 1); float arg1 = (float)luaL_checknumber(L, 2); - ASSERT_GUARD(t->setMaxForce(arg1);) + EXCEPT_GUARD(t->setMaxForce(arg1);) return 0; } @@ -55,7 +55,7 @@ int w_FrictionJoint_setMaxTorque(lua_State *L) { FrictionJoint *t = luax_checkfrictionjoint(L, 1); float arg1 = (float)luaL_checknumber(L, 2); - ASSERT_GUARD(t->setMaxTorque(arg1);) + EXCEPT_GUARD(t->setMaxTorque(arg1);) return 0; } diff --git a/src/modules/physics/box2d/wrap_GearJoint.cpp b/src/modules/physics/box2d/wrap_GearJoint.cpp index fc87d2012..760f69e5e 100644 --- a/src/modules/physics/box2d/wrap_GearJoint.cpp +++ b/src/modules/physics/box2d/wrap_GearJoint.cpp @@ -40,7 +40,7 @@ int w_GearJoint_setRatio(lua_State *L) { GearJoint *t = luax_checkgearjoint(L, 1); float arg1 = (float)luaL_checknumber(L, 2); - ASSERT_GUARD(t->setRatio(arg1);) + EXCEPT_GUARD(t->setRatio(arg1);) return 0; } diff --git a/src/modules/physics/box2d/wrap_Joint.cpp b/src/modules/physics/box2d/wrap_Joint.cpp index 4c9b1cccf..2924bb5f3 100644 --- a/src/modules/physics/box2d/wrap_Joint.cpp +++ b/src/modules/physics/box2d/wrap_Joint.cpp @@ -78,14 +78,7 @@ int w_Joint_getCollideConnected(lua_State *L) int w_Joint_destroy(lua_State *L) { Joint *t = luax_checkjoint(L, 1); - try - { - t->destroyJoint(); - } - catch(love::Exception &e) - { - luaL_error(L, "%s", e.what()); - } + EXCEPT_GUARD(t->destroyJoint();) return 0; } diff --git a/src/modules/physics/box2d/wrap_Physics.cpp b/src/modules/physics/box2d/wrap_Physics.cpp index ca1a5174d..cf2495efd 100644 --- a/src/modules/physics/box2d/wrap_Physics.cpp +++ b/src/modules/physics/box2d/wrap_Physics.cpp @@ -57,7 +57,7 @@ int w_newWorld(lua_State *L) bool sleep = luax_optboolean(L, 3, true); World *w; - ASSERT_GUARD(w = instance->newWorld(gx, gy, sleep);) + EXCEPT_GUARD(w = instance->newWorld(gx, gy, sleep);) luax_pushtype(L, "World", PHYSICS_WORLD_T, w); return 1; @@ -75,7 +75,7 @@ int w_newBody(lua_State *L) return luaL_error(L, "Invalid Body type: %s", typestr); Body *body; - ASSERT_GUARD(body = instance->newBody(world, x, y, btype);) + EXCEPT_GUARD(body = instance->newBody(world, x, y, btype);) luax_pushtype(L, "Body", PHYSICS_BODY_T, body); return 1; } @@ -86,7 +86,7 @@ int w_newFixture(lua_State *L) Shape *shape = luax_checkshape(L, 2); float density = (float)luaL_optnumber(L, 3, 1.0f); Fixture *fixture; - ASSERT_GUARD(fixture = instance->newFixture(body, shape, density);) + EXCEPT_GUARD(fixture = instance->newFixture(body, shape, density);) luax_pushtype(L, "Fixture", PHYSICS_FIXTURE_T, fixture); return 1; } @@ -99,7 +99,7 @@ int w_newCircleShape(lua_State *L) { float radius = (float)luaL_checknumber(L, 1); CircleShape *shape; - ASSERT_GUARD(shape = instance->newCircleShape(radius);) + EXCEPT_GUARD(shape = instance->newCircleShape(radius);) luax_pushtype(L, "CircleShape", PHYSICS_CIRCLE_SHAPE_T, shape); return 1; } @@ -109,7 +109,7 @@ int w_newCircleShape(lua_State *L) float y = (float)luaL_checknumber(L, 2); float radius = (float)luaL_checknumber(L, 3); CircleShape *shape; - ASSERT_GUARD(shape = instance->newCircleShape(x, y, radius);) + EXCEPT_GUARD(shape = instance->newCircleShape(x, y, radius);) luax_pushtype(L, "CircleShape", PHYSICS_CIRCLE_SHAPE_T, shape); return 1; } @@ -126,7 +126,7 @@ int w_newRectangleShape(lua_State *L) float w = (float)luaL_checknumber(L, 1); float h = (float)luaL_checknumber(L, 2); PolygonShape *shape; - ASSERT_GUARD(shape = instance->newRectangleShape(w, h);) + EXCEPT_GUARD(shape = instance->newRectangleShape(w, h);) luax_pushtype(L, "PolygonShape", PHYSICS_POLYGON_SHAPE_T, shape); return 1; } @@ -138,7 +138,7 @@ int w_newRectangleShape(lua_State *L) float h = (float)luaL_checknumber(L, 4); float angle = (float)luaL_optnumber(L, 5, 0); PolygonShape *shape; - ASSERT_GUARD(shape = instance->newRectangleShape(x, y, w, h, angle);) + EXCEPT_GUARD(shape = instance->newRectangleShape(x, y, w, h, angle);) luax_pushtype(L, "PolygonShape", PHYSICS_POLYGON_SHAPE_T, shape); return 1; } @@ -153,19 +153,23 @@ int w_newEdgeShape(lua_State *L) float x2 = (float)luaL_checknumber(L, 3); float y2 = (float)luaL_checknumber(L, 4); EdgeShape *shape; - ASSERT_GUARD(shape = instance->newEdgeShape(x1, y1, x2, y2);) + EXCEPT_GUARD(shape = instance->newEdgeShape(x1, y1, x2, y2);) luax_pushtype(L, "EdgeShape", PHYSICS_EDGE_SHAPE_T, shape); return 1; } int w_newPolygonShape(lua_State *L) { - ASSERT_GUARD(return instance->newPolygonShape(L);) + int ret = 0; + EXCEPT_GUARD(ret = instance->newPolygonShape(L);) + return ret; } int w_newChainShape(lua_State *L) { - ASSERT_GUARD(return instance->newChainShape(L);) + int ret = 0; + EXCEPT_GUARD(ret = instance->newChainShape(L);) + return ret; } int w_newDistanceJoint(lua_State *L) @@ -178,7 +182,7 @@ int w_newDistanceJoint(lua_State *L) float y2 = (float)luaL_checknumber(L, 6); bool collideConnected = luax_optboolean(L, 7, false); DistanceJoint *j; - ASSERT_GUARD(j = instance->newDistanceJoint(body1, body2, x1, y1, x2, y2, collideConnected);) + EXCEPT_GUARD(j = instance->newDistanceJoint(body1, body2, x1, y1, x2, y2, collideConnected);) luax_pushtype(L, "DistanceJoint", PHYSICS_DISTANCE_JOINT_T, j); return 1; } @@ -189,7 +193,7 @@ int w_newMouseJoint(lua_State *L) float x = (float)luaL_checknumber(L, 2); float y = (float)luaL_checknumber(L, 3); MouseJoint *j; - ASSERT_GUARD(j = instance->newMouseJoint(body, x, y);) + EXCEPT_GUARD(j = instance->newMouseJoint(body, x, y);) luax_pushtype(L, "MouseJoint", PHYSICS_MOUSE_JOINT_T, j); return 1; } @@ -202,7 +206,7 @@ int w_newRevoluteJoint(lua_State *L) float y = (float)luaL_checknumber(L, 4); bool collideConnected = luax_optboolean(L, 5, false); RevoluteJoint *j; - ASSERT_GUARD(j = instance->newRevoluteJoint(body1, body2, x, y, collideConnected);) + EXCEPT_GUARD(j = instance->newRevoluteJoint(body1, body2, x, y, collideConnected);) luax_pushtype(L, "RevoluteJoint", PHYSICS_REVOLUTE_JOINT_T, j); return 1; } @@ -232,7 +236,7 @@ int w_newPrismaticJoint(lua_State *L) collideConnected = luax_optboolean(L, 7, false); } PrismaticJoint *j; - ASSERT_GUARD(j = instance->newPrismaticJoint(body1, body2, xA, yA, xB, yB, ax, ay, collideConnected);) + EXCEPT_GUARD(j = instance->newPrismaticJoint(body1, body2, xA, yA, xB, yB, ax, ay, collideConnected);) luax_pushtype(L, "PrismaticJoint", PHYSICS_PRISMATIC_JOINT_T, j); return 1; } @@ -253,7 +257,7 @@ int w_newPulleyJoint(lua_State *L) bool collideConnected = luax_optboolean(L, 12, true); // PulleyJoints default to colliding connected bodies, see b2PulleyJoint.h PulleyJoint *j; - ASSERT_GUARD(j = instance->newPulleyJoint(body1, body2, b2Vec2(gx1,gy1), b2Vec2(gx2,gy2), b2Vec2(x1,y1), b2Vec2(x2,y2), ratio, collideConnected);) + EXCEPT_GUARD(j = instance->newPulleyJoint(body1, body2, b2Vec2(gx1,gy1), b2Vec2(gx2,gy2), b2Vec2(x1,y1), b2Vec2(x2,y2), ratio, collideConnected);) luax_pushtype(L, "PulleyJoint", PHYSICS_PULLEY_JOINT_T, j); return 1; } @@ -266,7 +270,7 @@ int w_newGearJoint(lua_State *L) bool collideConnected = luax_optboolean(L, 4, false); GearJoint *j; - ASSERT_GUARD(j = instance->newGearJoint(joint1, joint2, ratio, collideConnected);) + EXCEPT_GUARD(j = instance->newGearJoint(joint1, joint2, ratio, collideConnected);) luax_pushtype(L, "GearJoint", PHYSICS_GEAR_JOINT_T, j); return 1; } @@ -292,7 +296,7 @@ int w_newFrictionJoint(lua_State *L) collideConnected = luax_optboolean(L, 5, false); } FrictionJoint *j; - ASSERT_GUARD(j = instance->newFrictionJoint(body1, body2, xA, yA, xB, yB, collideConnected);) + EXCEPT_GUARD(j = instance->newFrictionJoint(body1, body2, xA, yA, xB, yB, collideConnected);) luax_pushtype(L, "FrictionJoint", PHYSICS_FRICTION_JOINT_T, j); return 1; } @@ -318,7 +322,7 @@ int w_newWeldJoint(lua_State *L) collideConnected = luax_optboolean(L, 5, false); } WeldJoint *j; - ASSERT_GUARD(j = instance->newWeldJoint(body1, body2, xA, yA, xB, yB, collideConnected);) + EXCEPT_GUARD(j = instance->newWeldJoint(body1, body2, xA, yA, xB, yB, collideConnected);) luax_pushtype(L, "WeldJoint", PHYSICS_WELD_JOINT_T, j); return 1; } @@ -349,7 +353,7 @@ int w_newWheelJoint(lua_State *L) } WheelJoint *j; - ASSERT_GUARD(j = instance->newWheelJoint(body1, body2, xA, yA, xB, yB, ax, ay, collideConnected);) + EXCEPT_GUARD(j = instance->newWheelJoint(body1, body2, xA, yA, xB, yB, ax, ay, collideConnected);) luax_pushtype(L, "WheelJoint", PHYSICS_WHEEL_JOINT_T, j); return 1; } @@ -365,7 +369,7 @@ int w_newRopeJoint(lua_State *L) float maxLength = (float)luaL_checknumber(L, 7); bool collideConnected = luax_optboolean(L, 8, false); RopeJoint *j; - ASSERT_GUARD(j = instance->newRopeJoint(body1, body2, x1, y1, x2, y2, maxLength, collideConnected);) + EXCEPT_GUARD(j = instance->newRopeJoint(body1, body2, x1, y1, x2, y2, maxLength, collideConnected);) luax_pushtype(L, "RopeJoint", PHYSICS_ROPE_JOINT_T, j); return 1; } @@ -378,14 +382,7 @@ int w_getDistance(lua_State *L) int w_setMeter(lua_State *L) { int arg1 = luaL_checkint(L, 1); - try - { - Physics::setMeter(arg1); - } - catch(love::Exception &e) - { - return luaL_error(L, e.what()); - } + EXCEPT_GUARD(Physics::setMeter(arg1);) return 0; } @@ -451,14 +448,7 @@ extern "C" int luaopen_love_physics(lua_State *L) { if (instance == 0) { - try - { - instance = new Physics(); - } - catch(Exception &e) - { - return luaL_error(L, e.what()); - } + EXCEPT_GUARD(instance = new Physics();) } else instance->retain(); diff --git a/src/modules/physics/box2d/wrap_Physics.h b/src/modules/physics/box2d/wrap_Physics.h index 9aac11d94..61829840a 100644 --- a/src/modules/physics/box2d/wrap_Physics.h +++ b/src/modules/physics/box2d/wrap_Physics.h @@ -25,8 +25,6 @@ #include "common/config.h" #include "Physics.h" -#define ASSERT_GUARD(A) try { A } catch (love::Exception & e) { return luaL_error(L, "%s", e.what()); } - namespace love { namespace physics diff --git a/src/modules/physics/box2d/wrap_PrismaticJoint.cpp b/src/modules/physics/box2d/wrap_PrismaticJoint.cpp index f08a41ec0..0cc24d88c 100644 --- a/src/modules/physics/box2d/wrap_PrismaticJoint.cpp +++ b/src/modules/physics/box2d/wrap_PrismaticJoint.cpp @@ -122,7 +122,7 @@ int w_PrismaticJoint_setUpperLimit(lua_State *L) { PrismaticJoint *t = luax_checkprismaticjoint(L, 1); float arg1 = (float)luaL_checknumber(L, 2); - ASSERT_GUARD(t->setUpperLimit(arg1);) + EXCEPT_GUARD(t->setUpperLimit(arg1);) return 0; } @@ -130,7 +130,7 @@ int w_PrismaticJoint_setLowerLimit(lua_State *L) { PrismaticJoint *t = luax_checkprismaticjoint(L, 1); float arg1 = (float)luaL_checknumber(L, 2); - ASSERT_GUARD(t->setLowerLimit(arg1);) + EXCEPT_GUARD(t->setLowerLimit(arg1);) return 0; } @@ -139,7 +139,7 @@ int w_PrismaticJoint_setLimits(lua_State *L) PrismaticJoint *t = luax_checkprismaticjoint(L, 1); float arg1 = (float)luaL_checknumber(L, 2); float arg2 = (float)luaL_checknumber(L, 3); - ASSERT_GUARD(t->setLimits(arg1, arg2);) + EXCEPT_GUARD(t->setLimits(arg1, arg2);) return 0; } diff --git a/src/modules/physics/box2d/wrap_RevoluteJoint.cpp b/src/modules/physics/box2d/wrap_RevoluteJoint.cpp index 9962c417e..02268310c 100644 --- a/src/modules/physics/box2d/wrap_RevoluteJoint.cpp +++ b/src/modules/physics/box2d/wrap_RevoluteJoint.cpp @@ -122,7 +122,7 @@ int w_RevoluteJoint_setUpperLimit(lua_State *L) { RevoluteJoint *t = luax_checkrevolutejoint(L, 1); float arg1 = (float)luaL_checknumber(L, 2); - ASSERT_GUARD(t->setUpperLimit(arg1);) + EXCEPT_GUARD(t->setUpperLimit(arg1);) return 0; } @@ -130,7 +130,7 @@ int w_RevoluteJoint_setLowerLimit(lua_State *L) { RevoluteJoint *t = luax_checkrevolutejoint(L, 1); float arg1 = (float)luaL_checknumber(L, 2); - ASSERT_GUARD(t->setLowerLimit(arg1);) + EXCEPT_GUARD(t->setLowerLimit(arg1);) return 0; } @@ -139,7 +139,7 @@ int w_RevoluteJoint_setLimits(lua_State *L) RevoluteJoint *t = luax_checkrevolutejoint(L, 1); float arg1 = (float)luaL_checknumber(L, 2); float arg2 = (float)luaL_checknumber(L, 3); - ASSERT_GUARD(t->setLimits(arg1, arg2);) + EXCEPT_GUARD(t->setLimits(arg1, arg2);) return 0; } diff --git a/src/modules/physics/box2d/wrap_Shape.cpp b/src/modules/physics/box2d/wrap_Shape.cpp index 958e62a44..f6a693555 100644 --- a/src/modules/physics/box2d/wrap_Shape.cpp +++ b/src/modules/physics/box2d/wrap_Shape.cpp @@ -75,7 +75,9 @@ int w_Shape_rayCast(lua_State *L) { Shape *t = luax_checkshape(L, 1); lua_remove(L, 1); - ASSERT_GUARD(return t->rayCast(L);) + int ret = 0; + EXCEPT_GUARD(ret = t->rayCast(L);) + return ret; } int w_Shape_computeAABB(lua_State *L) diff --git a/src/modules/physics/box2d/wrap_World.cpp b/src/modules/physics/box2d/wrap_World.cpp index 6039e65be..13deee20b 100644 --- a/src/modules/physics/box2d/wrap_World.cpp +++ b/src/modules/physics/box2d/wrap_World.cpp @@ -39,7 +39,7 @@ int w_World_update(lua_State *L) { World *t = luax_checkworld(L, 1); float dt = (float)luaL_checknumber(L, 2); - ASSERT_GUARD(t->update(dt);) + EXCEPT_GUARD(t->update(dt);) return 0; } @@ -162,20 +162,15 @@ int w_World_rayCast(lua_State *L) { World *t = luax_checkworld(L, 1); lua_remove(L, 1); - ASSERT_GUARD(return t->rayCast(L);) + int ret = 0; + EXCEPT_GUARD(ret = t->rayCast(L);) + return ret; } int w_World_destroy(lua_State *L) { World *t = luax_checkworld(L, 1); - try - { - t->destroy(); - } - catch(love::Exception &e) - { - luaL_error(L, "%s", e.what()); - } + EXCEPT_GUARD(t->destroy();) return 0; } diff --git a/src/modules/sound/wrap_Sound.cpp b/src/modules/sound/wrap_Sound.cpp index 795ffceb8..88d61435d 100644 --- a/src/modules/sound/wrap_Sound.cpp +++ b/src/modules/sound/wrap_Sound.cpp @@ -41,20 +41,11 @@ int w_newSoundData(lua_State *L) int bitDepth = luaL_optint(L, 3, Decoder::DEFAULT_BIT_DEPTH); int channels = luaL_optint(L, 4, Decoder::DEFAULT_CHANNELS); - try - { - t = instance->newSoundData(samples, sampleRate, bitDepth, channels); - } - catch(love::Exception &e) - { - return luaL_error(L, e.what()); - } - + EXCEPT_GUARD(t = instance->newSoundData(samples, sampleRate, bitDepth, channels);) } // Must be string or decoder. else { - // Convert to Decoder, if necessary. if (!luax_istype(L, 1, SOUND_DECODER_T)) { @@ -62,14 +53,7 @@ int w_newSoundData(lua_State *L) lua_replace(L, 1); } - try - { - t = instance->newSoundData(luax_checkdecoder(L, 1)); - } - catch(love::Exception &e) - { - return luaL_error(L, e.what()); - } + EXCEPT_GUARD(t = instance->newSoundData(luax_checkdecoder(L, 1));) } luax_pushtype(L, "SoundData", SOUND_SOUND_DATA_T, t); @@ -86,18 +70,13 @@ int w_newDecoder(lua_State *L) int bufferSize = luaL_optint(L, 2, Decoder::DEFAULT_BUFFER_SIZE); - try - { - Decoder *t = instance->newDecoder(data, bufferSize); - if (t == 0) - return luaL_error(L, "Extension \"%s\" not supported.", data->getExtension().c_str()); - luax_pushtype(L, "Decoder", SOUND_DECODER_T, t); - } - catch(love::Exception &e) - { - return luaL_error(L, e.what()); - } + Decoder *t = 0; + EXCEPT_GUARD(t = instance->newDecoder(data, bufferSize);) + if (t == 0) + return luaL_error(L, "Extension \"%s\" not supported.", data->getExtension().c_str()); + + luax_pushtype(L, "Decoder", SOUND_DECODER_T, t); return 1; } @@ -120,14 +99,7 @@ extern "C" int luaopen_love_sound(lua_State *L) { if (instance == 0) { - try - { - instance = new lullaby::Sound(); - } - catch(Exception &e) - { - return luaL_error(L, e.what()); - } + EXCEPT_GUARD(instance = new lullaby::Sound();) } else instance->retain(); diff --git a/src/modules/sound/wrap_SoundData.cpp b/src/modules/sound/wrap_SoundData.cpp index 359b02b8f..23bc9c0d3 100644 --- a/src/modules/sound/wrap_SoundData.cpp +++ b/src/modules/sound/wrap_SoundData.cpp @@ -72,14 +72,8 @@ int w_SoundData_setSample(lua_State *L) SoundData *sd = luax_checksounddata(L, 1); int i = (int)lua_tointeger(L, 2); float sample = (float)lua_tonumber(L, 3); - try - { - sd->setSample(i, sample); - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + + EXCEPT_GUARD(sd->setSample(i, sample);) return 0; } @@ -87,14 +81,8 @@ int w_SoundData_getSample(lua_State *L) { SoundData *sd = luax_checksounddata(L, 1); int i = (int)lua_tointeger(L, 2); - try - { - lua_pushnumber(L, sd->getSample(i)); - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + + EXCEPT_GUARD(lua_pushnumber(L, sd->getSample(i));) return 1; } diff --git a/src/modules/thread/wrap_ThreadModule.cpp b/src/modules/thread/wrap_ThreadModule.cpp index 56d971f10..c90fb9a6b 100644 --- a/src/modules/thread/wrap_ThreadModule.cpp +++ b/src/modules/thread/wrap_ThreadModule.cpp @@ -91,14 +91,7 @@ extern "C" int luaopen_love_thread(lua_State *L) { if (instance == 0) { - try - { - instance = new love::thread::ThreadModule(); - } - catch (Exception & e) - { - return luaL_error(L, "%s", e.what()); - } + EXCEPT_GUARD(instance = new love::thread::ThreadModule();) } else instance->retain(); diff --git a/src/modules/timer/wrap_Timer.cpp b/src/modules/timer/wrap_Timer.cpp index 4968fa6c4..068bb11ae 100644 --- a/src/modules/timer/wrap_Timer.cpp +++ b/src/modules/timer/wrap_Timer.cpp @@ -85,15 +85,7 @@ extern "C" int luaopen_love_timer(lua_State *L) { if (instance == 0) { - try - { - instance = new love::timer::sdl::Timer(); - - } - catch (Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + EXCEPT_GUARD(instance = new love::timer::sdl::Timer();) } else instance->retain(); diff --git a/src/modules/window/wrap_Window.cpp b/src/modules/window/wrap_Window.cpp index 8ffa30dd7..24eb14b66 100644 --- a/src/modules/window/wrap_Window.cpp +++ b/src/modules/window/wrap_Window.cpp @@ -105,15 +105,7 @@ int w_setMode(lua_State *L) // Display index is 1-based in Lua and 0-based internally. flags.display--; - try - { - luax_pushboolean(L, instance->setWindow(w, h, &flags)); - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } - + EXCEPT_GUARD(luax_pushboolean(L, instance->setWindow(w, h, &flags));) return 1; } @@ -339,14 +331,7 @@ static const luaL_Reg functions[] = extern "C" int luaopen_love_window(lua_State *L) { - try - { - instance = sdl::Window::createSingleton(); - } - catch (love::Exception &e) - { - return luaL_error(L, "%s", e.what()); - } + EXCEPT_GUARD(instance = sdl::Window::createSingleton();) WrappedModule w; w.module = instance;