From dfa319e01a89396767e2fad4f031af0b968b074a Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Tue, 3 Jun 2014 19:27:00 -0300 Subject: [PATCH] Replaced the EXCEPT_GUARD macro for converting love exceptions into Lua errors with the new luax_catchexcept function, which takes lambda function arguments. --- src/common/runtime.h | 70 +++++++++++------- src/modules/audio/wrap_Source.cpp | 2 +- src/modules/event/sdl/wrap_Event.cpp | 2 +- src/modules/filesystem/wrap_File.cpp | 2 +- src/modules/filesystem/wrap_Filesystem.cpp | 30 ++------ src/modules/font/freetype/wrap_Font.cpp | 11 +-- src/modules/font/wrap_GlyphData.cpp | 2 +- src/modules/font/wrap_Rasterizer.cpp | 8 +-- src/modules/graphics/opengl/wrap_Canvas.cpp | 4 +- src/modules/graphics/opengl/wrap_Font.cpp | 12 ++-- src/modules/graphics/opengl/wrap_Graphics.cpp | 72 ++++++++++++------- src/modules/graphics/opengl/wrap_Image.cpp | 4 +- src/modules/graphics/opengl/wrap_Mesh.cpp | 12 ++-- .../graphics/opengl/wrap_ParticleSystem.cpp | 6 +- src/modules/graphics/opengl/wrap_Shader.cpp | 10 +-- .../graphics/opengl/wrap_SpriteBatch.cpp | 12 ++-- src/modules/graphics/opengl/wrap_Texture.cpp | 2 +- src/modules/image/CompressedData.cpp | 2 +- src/modules/image/wrap_CompressedData.cpp | 13 ++-- src/modules/image/wrap_Image.cpp | 14 ++-- src/modules/image/wrap_ImageData.cpp | 6 +- .../joystick/sdl/wrap_JoystickModule.cpp | 6 +- src/modules/keyboard/wrap_Keyboard.cpp | 28 +++++++- src/modules/math/wrap_BezierCurve.cpp | 14 ++-- src/modules/math/wrap_Math.cpp | 8 +-- src/modules/math/wrap_RandomGenerator.cpp | 4 +- src/modules/mouse/wrap_Mouse.cpp | 6 +- src/modules/physics/box2d/Physics.cpp | 4 +- src/modules/physics/box2d/wrap_Body.cpp | 26 +++---- src/modules/physics/box2d/wrap_ChainShape.cpp | 8 +-- src/modules/physics/box2d/wrap_Fixture.cpp | 6 +- .../physics/box2d/wrap_FrictionJoint.cpp | 4 +- src/modules/physics/box2d/wrap_GearJoint.cpp | 2 +- src/modules/physics/box2d/wrap_Joint.cpp | 2 +- src/modules/physics/box2d/wrap_MotorJoint.cpp | 6 +- src/modules/physics/box2d/wrap_Physics.cpp | 68 +++++++++++------- .../physics/box2d/wrap_PrismaticJoint.cpp | 6 +- .../physics/box2d/wrap_RevoluteJoint.cpp | 6 +- src/modules/physics/box2d/wrap_Shape.cpp | 2 +- src/modules/physics/box2d/wrap_World.cpp | 14 ++-- src/modules/sound/wrap_Sound.cpp | 11 +-- src/modules/sound/wrap_SoundData.cpp | 4 +- src/modules/thread/wrap_ThreadModule.cpp | 2 +- src/modules/timer/wrap_Timer.cpp | 2 +- src/modules/window/wrap_Window.cpp | 7 +- 45 files changed, 315 insertions(+), 227 deletions(-) diff --git a/src/common/runtime.h b/src/common/runtime.h index 3b7d4b282..c9c4ffafd 100644 --- a/src/common/runtime.h +++ b/src/common/runtime.h @@ -33,6 +33,9 @@ extern "C" { #include } +// C++ +#include + namespace love { @@ -471,36 +474,55 @@ T *luax_totype(lua_State *L, int idx, const char * /* name */, love::bits /* typ Type luax_type(lua_State *L, int idx); /** - * Macro for converting a LOVE exception into a Lua error. + * Converts any exceptions thrown by the passed lambda function 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 luaL_error(L, "%s", lua_tostring(L, -1)); \ +template +int luax_catchexcept(lua_State *L, const T& func) +{ + bool should_error = false; + + try + { + func(); + } + catch (const std::exception &e) + { + should_error = true; + lua_pushstring(L, e.what()); + } + + if (should_error) + return luaL_error(L, "%s", lua_tostring(L, -1)); + + return 0; + } -#define EXCEPT_GUARD_FINALLY(A, B) \ -{ \ - bool should_error = false; \ - try { A } \ - catch (love::Exception &e) \ - { \ - should_error = true; \ - lua_pushstring(L, e.what()); \ - } \ - { B } \ - if (should_error) \ - return luaL_error(L, "%s", lua_tostring(L, -1)); \ +template +int luax_catchexcept(lua_State *L, const T& func, const F& finallyfunc) +{ + bool should_error = false; + + try + { + func(); + } + catch (const std::exception &e) + { + should_error = true; + lua_pushstring(L, e.what()); + } + + finallyfunc(); + + if (should_error) + return luaL_error(L, "%s", lua_tostring(L, -1)); + + return 0; + } } // love diff --git a/src/modules/audio/wrap_Source.cpp b/src/modules/audio/wrap_Source.cpp index b6b152b2b..6bdd71d0d 100644 --- a/src/modules/audio/wrap_Source.cpp +++ b/src/modules/audio/wrap_Source.cpp @@ -36,7 +36,7 @@ int w_Source_clone(lua_State *L) { Source *t = luax_checksource(L, 1); Source *clone = nullptr; - EXCEPT_GUARD(clone = t->clone();) + luax_catchexcept(L, [&](){ clone = t->clone(); }); luax_pushtype(L, "Source", AUDIO_SOURCE_T, clone); return 1; } diff --git a/src/modules/event/sdl/wrap_Event.cpp b/src/modules/event/sdl/wrap_Event.cpp index f33bd6619..67481f3f2 100644 --- a/src/modules/event/sdl/wrap_Event.cpp +++ b/src/modules/event/sdl/wrap_Event.cpp @@ -123,7 +123,7 @@ extern "C" int luaopen_love_event(lua_State *L) { if (instance == 0) { - EXCEPT_GUARD(instance = new Event();) + luax_catchexcept(L, [&](){ instance = new Event(); }); } else instance->retain(); diff --git a/src/modules/filesystem/wrap_File.cpp b/src/modules/filesystem/wrap_File.cpp index 28213008b..dda6da74a 100644 --- a/src/modules/filesystem/wrap_File.cpp +++ b/src/modules/filesystem/wrap_File.cpp @@ -236,7 +236,7 @@ int w_File_lines(lua_State *L) file->close(); bool success = false; - EXCEPT_GUARD(success = file->open(File::READ);) + luax_catchexcept(L, [&](){ success = file->open(File::READ); }); if (!success) return luaL_error(L, "Could not open file."); diff --git a/src/modules/filesystem/wrap_Filesystem.cpp b/src/modules/filesystem/wrap_Filesystem.cpp index 38321f996..780c28d3e 100644 --- a/src/modules/filesystem/wrap_Filesystem.cpp +++ b/src/modules/filesystem/wrap_Filesystem.cpp @@ -45,7 +45,7 @@ bool hack_setupWriteDirectory() int w_init(lua_State *L) { const char *arg0 = luaL_checkstring(L, 1); - EXCEPT_GUARD(instance->init(arg0);) + luax_catchexcept(L, [&](){ instance->init(arg0); }); return 0; } @@ -177,26 +177,10 @@ FileData *luax_getFileData(lua_State *L, int idx) if (file) { - bool should_error = false; - - // We don't use EXCEPT_GUARD_FINALLY because it returns int. - try - { - data = file->read(); - } - catch (love::Exception &e) - { - should_error = true; - lua_pushstring(L, e.what()); - } - - file->release(); - - if (should_error) - { - luaL_error(L, "%s", lua_tostring(L, -1)); - return nullptr; // Never reached. - } + luax_catchexcept(L, + [&]() { data = file->read(); }, + [&]() { file->release(); } + ); } return data; @@ -416,7 +400,7 @@ int w_lines(lua_State *L) file = instance->newFile(lua_tostring(L, 1)); bool success = false; - EXCEPT_GUARD(success = file->open(File::READ);) + luax_catchexcept(L, [&](){ success = file->open(File::READ); }); if (!success) return luaL_error(L, "Could not open file."); @@ -676,7 +660,7 @@ extern "C" int luaopen_love_filesystem(lua_State *L) { if (instance == 0) { - EXCEPT_GUARD(instance = new physfs::Filesystem();) + luax_catchexcept(L, [&](){ instance = new physfs::Filesystem(); }); } else instance->retain(); diff --git a/src/modules/font/freetype/wrap_Font.cpp b/src/modules/font/freetype/wrap_Font.cpp index 8be1bc1fd..ac0996a36 100644 --- a/src/modules/font/freetype/wrap_Font.cpp +++ b/src/modules/font/freetype/wrap_Font.cpp @@ -47,13 +47,16 @@ int w_newRasterizer(lua_State *L) love::image::ImageData *d = luax_checktype(L, 1, "ImageData", IMAGE_IMAGE_DATA_T); const char *g = luaL_checkstring(L, 2); std::string glyphs(g); - EXCEPT_GUARD(t = instance->newRasterizer(d, glyphs);) + luax_catchexcept(L, [&](){ t = instance->newRasterizer(d, glyphs); }); } else if (lua_isstring(L, 1) || luax_istype(L, 1, FILESYSTEM_FILE_T) || luax_istype(L, 1, FILESYSTEM_FILE_DATA_T)) { love::filesystem::FileData *d = love::filesystem::luax_getFileData(L, 1); int size = luaL_checkint(L, 2); - EXCEPT_GUARD_FINALLY(t = instance->newRasterizer(d, size);, d->release();) + luax_catchexcept(L, + [&]() { t = instance->newRasterizer(d, size); }, + [&]() { d->release(); } + ); } luax_pushtype(L, "Rasterizer", FONT_RASTERIZER_T, t); @@ -69,7 +72,7 @@ int w_newGlyphData(lua_State *L) if (lua_type(L, 2) == LUA_TSTRING) { std::string glyph = luax_checkstring(L, 2); - EXCEPT_GUARD(t = instance->newGlyphData(r, glyph);) + luax_catchexcept(L, [&](){ t = instance->newGlyphData(r, glyph); }); } else { @@ -100,7 +103,7 @@ extern "C" int luaopen_love_font(lua_State *L) { if (instance == nullptr) { - EXCEPT_GUARD(instance = new Font();) + luax_catchexcept(L, [](){ instance = new Font(); }); } else instance->retain(); diff --git a/src/modules/font/wrap_GlyphData.cpp b/src/modules/font/wrap_GlyphData.cpp index c71dcb844..9a8bcfb60 100644 --- a/src/modules/font/wrap_GlyphData.cpp +++ b/src/modules/font/wrap_GlyphData.cpp @@ -64,7 +64,7 @@ int w_GlyphData_getGlyphString(lua_State *L) { GlyphData *t = luax_checkglyphdata(L, 1); - EXCEPT_GUARD(luax_pushstring(L, t->getGlyphString());) + luax_catchexcept(L, [&](){ luax_pushstring(L, t->getGlyphString()); }); return 1; } diff --git a/src/modules/font/wrap_Rasterizer.cpp b/src/modules/font/wrap_Rasterizer.cpp index 0f483a7d9..048c9edc9 100644 --- a/src/modules/font/wrap_Rasterizer.cpp +++ b/src/modules/font/wrap_Rasterizer.cpp @@ -72,7 +72,7 @@ int w_Rasterizer_getGlyphData(lua_State *L) Rasterizer *t = luax_checkrasterizer(L, 1); GlyphData *g = 0; - EXCEPT_GUARD( + luax_catchexcept(L, [&]() { // getGlyphData accepts a unicode character or a codepoint number. if (lua_type(L, 2) == LUA_TSTRING) { @@ -84,7 +84,7 @@ int w_Rasterizer_getGlyphData(lua_State *L) uint32 glyph = (uint32) luaL_checknumber(L, 2); g = t->getGlyphData(glyph); } - ) + }); luax_pushtype(L, "GlyphData", FONT_GLYPH_DATA_T, g); return 1; @@ -106,7 +106,7 @@ int w_Rasterizer_hasGlyphs(lua_State *L) int count = lua_gettop(L) - 1; count = count < 1 ? 1 : count; - EXCEPT_GUARD( + luax_catchexcept(L, [&]() { for (int i = 2; i < count + 2; i++) { if (lua_type(L, i) == LUA_TSTRING) @@ -117,7 +117,7 @@ int w_Rasterizer_hasGlyphs(lua_State *L) if (!hasglyph) break; } - ) + }); 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 4efc610e1..9bbf8e2ae 100644 --- a/src/modules/graphics/opengl/wrap_Canvas.cpp +++ b/src/modules/graphics/opengl/wrap_Canvas.cpp @@ -40,7 +40,7 @@ int w_Canvas_renderTo(lua_State *L) // Save the current Canvas so we can restore it when we're done. Canvas *oldcanvas = Canvas::current; - EXCEPT_GUARD(canvas->startGrab();) + luax_catchexcept(L, [&](){ canvas->startGrab(); }); lua_settop(L, 2); // make sure the function is on top of the stack lua_call(L, 0, 0); @@ -69,7 +69,7 @@ int w_Canvas_getPixel(lua_State * L) int y = luaL_checkint(L, 3); unsigned char c[4]; - EXCEPT_GUARD(canvas->getPixel(c, x, y);) + luax_catchexcept(L, [&](){ canvas->getPixel(c, x, y); }); lua_pushnumber(L, c[0]); lua_pushnumber(L, c[1]); diff --git a/src/modules/graphics/opengl/wrap_Font.cpp b/src/modules/graphics/opengl/wrap_Font.cpp index e30fa12c2..379a5245f 100644 --- a/src/modules/graphics/opengl/wrap_Font.cpp +++ b/src/modules/graphics/opengl/wrap_Font.cpp @@ -45,7 +45,7 @@ int w_Font_getWidth(lua_State *L) Font *t = luax_checkfont(L, 1); const char *str = luaL_checkstring(L, 2); - EXCEPT_GUARD(lua_pushinteger(L, t->getWidth(str));) + luax_catchexcept(L, [&](){ lua_pushinteger(L, t->getWidth(str)); }); return 1; } @@ -56,10 +56,10 @@ int w_Font_getWrap(lua_State *L) float wrap = (float) luaL_checknumber(L, 3); int max_width = 0, numlines = 0; - EXCEPT_GUARD( + luax_catchexcept(L, [&]() { std::vector lines = t->getWrap(str, wrap, &max_width); numlines = lines.size(); - ) + }); lua_pushinteger(L, max_width); lua_pushinteger(L, numlines); @@ -96,7 +96,7 @@ int w_Font_setFilter(lua_State *L) f.anisotropy = (float) luaL_optnumber(L, 4, 1.0); - EXCEPT_GUARD(t->setFilter(f);) + luax_catchexcept(L, [&](){ t->setFilter(f); }); return 0; } @@ -143,7 +143,7 @@ int w_Font_hasGlyphs(lua_State *L) int count = lua_gettop(L) - 1; count = count < 1 ? 1 : count; - EXCEPT_GUARD( + luax_catchexcept(L, [&]() { for (int i = 2; i < count + 2; i++) { if (lua_type(L, i) == LUA_TSTRING) @@ -154,7 +154,7 @@ int w_Font_hasGlyphs(lua_State *L) if (!hasglyph) break; } - ) + }); luax_pushboolean(L, hasglyph); return 1; diff --git a/src/modules/graphics/opengl/wrap_Graphics.cpp b/src/modules/graphics/opengl/wrap_Graphics.cpp index 26b272aaa..fa629f377 100644 --- a/src/modules/graphics/opengl/wrap_Graphics.cpp +++ b/src/modules/graphics/opengl/wrap_Graphics.cpp @@ -176,11 +176,17 @@ int w_newImage(lua_State *L) if (image->isCompressed(fdata)) { - EXCEPT_GUARD_FINALLY(cdata = image->newCompressedData(fdata);, fdata->release();) + luax_catchexcept(L, + [&]() { cdata = image->newCompressedData(fdata); }, + [&]() { fdata->release(); } + ); } else { - EXCEPT_GUARD_FINALLY(data = image->newImageData(fdata);, fdata->release();) + luax_catchexcept(L, + [&]() { data = image->newImageData(fdata); }, + [&]() { fdata->release(); } + ); } // Lua's GC won't release the image data, so we should do it ourselves. @@ -196,20 +202,20 @@ int w_newImage(lua_State *L) // Create the image. Image *image = nullptr; - EXCEPT_GUARD_FINALLY( - { + luax_catchexcept(L, + [&]() { if (cdata) image = instance->newImage(cdata, format); else if (data) image = instance->newImage(data, format); }, - { + [&]() { if (releasedata && data) data->release(); else if (releasedata && cdata) cdata->release(); } - ) + ); if (image == nullptr) return luaL_error(L, "Could not load image."); @@ -247,7 +253,9 @@ int w_newFont(lua_State *L) love::font::Rasterizer *rasterizer = luax_checktype(L, 1, "Rasterizer", FONT_RASTERIZER_T); Font *font = 0; - EXCEPT_GUARD(font = instance->newFont(rasterizer, instance->getDefaultFilter());) + luax_catchexcept(L, [&]() { + font = instance->newFont(rasterizer, instance->getDefaultFilter()); } + ); if (font == 0) return luaL_error(L, "Could not load font."); @@ -311,7 +319,9 @@ int w_newSpriteBatch(lua_State *L) } SpriteBatch *t = nullptr; - EXCEPT_GUARD(t = instance->newSpriteBatch(texture, size, usage);) + luax_catchexcept(L, + [&](){ t = instance->newSpriteBatch(texture, size, usage); } + ); luax_pushtype(L, "SpriteBatch", GRAPHICS_SPRITE_BATCH_T, t); return 1; @@ -325,7 +335,9 @@ int w_newParticleSystem(lua_State *L) if (size < 1.0 || size > ParticleSystem::MAX_PARTICLES) return luaL_error(L, "Invalid ParticleSystem size"); - EXCEPT_GUARD(t = instance->newParticleSystem(texture, int(size));) + luax_catchexcept(L, + [&](){ t = instance->newParticleSystem(texture, int(size)); } + ); luax_pushtype(L, "ParticleSystem", GRAPHICS_PARTICLE_SYSTEM_T, t); return 1; @@ -344,7 +356,9 @@ int w_newCanvas(lua_State *L) return luaL_error(L, "Invalid Canvas format: %s", str); Canvas *canvas = nullptr; - EXCEPT_GUARD(canvas = instance->newCanvas(width, height, format, fsaa);) + luax_catchexcept(L, + [&](){ canvas = instance->newCanvas(width, height, format, fsaa); } + ); if (canvas == nullptr) return luaL_error(L, "Canvas not created, but no error thrown. I don't even..."); @@ -515,13 +529,13 @@ int w_newMesh(lua_State *L) vertices.push_back(v); } - EXCEPT_GUARD(t = instance->newMesh(vertices, mode);) + luax_catchexcept(L, [&](){ t = instance->newMesh(vertices, mode); }); t->setVertexColors(use_colors); } else { int count = luaL_checkint(L, 1); - EXCEPT_GUARD(t = instance->newMesh(count, mode);) + luax_catchexcept(L, [&](){ t = instance->newMesh(count, mode); }); } if (tex) @@ -660,7 +674,7 @@ int w_setBlendMode(lua_State *L) if (!Graphics::getConstant(str, mode)) return luaL_error(L, "Invalid blend mode: %s", str); - EXCEPT_GUARD(instance->setBlendMode(mode);) + luax_catchexcept(L, [&](){ instance->setBlendMode(mode); }); return 0; } @@ -669,7 +683,7 @@ int w_getBlendMode(lua_State *L) const char *str; Graphics::BlendMode mode; - EXCEPT_GUARD(mode = instance->getBlendMode();) + luax_catchexcept(L, [&](){ mode = instance->getBlendMode(); }); if (!Graphics::getConstant(mode, str)) return luaL_error(L, "Unknown blend mode"); @@ -867,7 +881,7 @@ int w_newScreenshot(lua_State *L) bool copyAlpha = luax_optboolean(L, 1, false); love::image::ImageData *i = 0; - EXCEPT_GUARD(i = instance->newScreenshot(image, copyAlpha);) + luax_catchexcept(L, [&](){ i = instance->newScreenshot(image, copyAlpha); }); luax_pushtype(L, "ImageData", IMAGE_IMAGE_DATA_T, i); return 1; @@ -911,12 +925,12 @@ int w_setCanvas(lua_State *L) attachments.push_back(luax_checkcanvas(L, i)); } - EXCEPT_GUARD( + luax_catchexcept(L, [&]() { if (attachments.size() > 0) canvas->startGrab(attachments); else canvas->startGrab(); - ) + }); return 0; } @@ -1055,10 +1069,12 @@ int w_getRendererInfo(lua_State *L) { std::string name, version, vendor, device; - EXCEPT_GUARD(name = instance->getRendererInfo(Graphics::RENDERER_INFO_NAME);) - EXCEPT_GUARD(version = instance->getRendererInfo(Graphics::RENDERER_INFO_VERSION);) - EXCEPT_GUARD(vendor = instance->getRendererInfo(Graphics::RENDERER_INFO_VENDOR);) - EXCEPT_GUARD(device = instance->getRendererInfo(Graphics::RENDERER_INFO_DEVICE);) + luax_catchexcept(L, [&]() { + name = instance->getRendererInfo(Graphics::RENDERER_INFO_NAME); + version = instance->getRendererInfo(Graphics::RENDERER_INFO_VERSION); + vendor = instance->getRendererInfo(Graphics::RENDERER_INFO_VENDOR); + device = instance->getRendererInfo(Graphics::RENDERER_INFO_DEVICE); + }); luax_pushstring(L, name); luax_pushstring(L, version); @@ -1134,7 +1150,9 @@ int w_print(lua_State *L) float kx = (float)luaL_optnumber(L, 9, 0.0f); float ky = (float)luaL_optnumber(L, 10, 0.0f); - EXCEPT_GUARD(instance->print(str, x, y, angle, sx, sy, ox, oy, kx,ky);) + luax_catchexcept(L, + [&](){ instance->print(str, x, y, angle, sx, sy, ox, oy, kx,ky); } + ); return 0; } @@ -1170,7 +1188,9 @@ int w_printf(lua_State *L) ky = (float) luaL_optnumber(L, 12, 0.0f); } - EXCEPT_GUARD(instance->printf(str, x, y, wrap, align, angle, sx, sy, ox, oy, kx, ky);) + luax_catchexcept(L, + [&](){ instance->printf(str, x, y, wrap, align, angle, sx, sy, ox, oy, kx, ky); } + ); return 0; } @@ -1326,13 +1346,13 @@ int w_polygon(lua_State *L) int w_push(lua_State *L) { - EXCEPT_GUARD(instance->push();) + luax_catchexcept(L, [&](){ instance->push(); }); return 0; } int w_pop(lua_State *L) { - EXCEPT_GUARD(instance->pop();) + luax_catchexcept(L, [&](){ instance->pop(); }); return 0; } @@ -1488,7 +1508,7 @@ extern "C" int luaopen_love_graphics(lua_State *L) { if (instance == 0) { - EXCEPT_GUARD(instance = new Graphics();) + luax_catchexcept(L, [&](){ 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 be95571e7..da9107e5a 100644 --- a/src/modules/graphics/opengl/wrap_Image.cpp +++ b/src/modules/graphics/opengl/wrap_Image.cpp @@ -47,7 +47,7 @@ int w_Image_setMipmapFilter(lua_State *L) return luaL_error(L, "Invalid filter mode: %s", mipmapstr); } - EXCEPT_GUARD(t->setFilter(f);) + luax_catchexcept(L, [&](){ t->setFilter(f); }); float sharpness = (float) luaL_optnumber(L, 3, 0); t->setMipmapSharpness(sharpness); @@ -81,7 +81,7 @@ int w_Image_isCompressed(lua_State *L) int w_Image_refresh(lua_State *L) { Image *i = luax_checkimage(L, 1); - EXCEPT_GUARD(i->refresh();) + luax_catchexcept(L, [&](){ i->refresh(); }); return 0; } diff --git a/src/modules/graphics/opengl/wrap_Mesh.cpp b/src/modules/graphics/opengl/wrap_Mesh.cpp index 031b2ae66..ea2928a4c 100644 --- a/src/modules/graphics/opengl/wrap_Mesh.cpp +++ b/src/modules/graphics/opengl/wrap_Mesh.cpp @@ -74,7 +74,7 @@ int w_Mesh_setVertex(lua_State *L) v.a = luaL_optinteger(L, 10, 255); } - EXCEPT_GUARD(t->setVertex(i, v);) + luax_catchexcept(L, [&](){ t->setVertex(i, v); }); return 0; } @@ -84,7 +84,7 @@ int w_Mesh_getVertex(lua_State *L) size_t i = (size_t) (luaL_checkinteger(L, 2) - 1); Vertex v; - EXCEPT_GUARD(v = t->getVertex(i);) + luax_catchexcept(L, [&](){ v = t->getVertex(i); }); lua_pushnumber(L, v.x); lua_pushnumber(L, v.y); @@ -134,7 +134,7 @@ int w_Mesh_setVertices(lua_State *L) vertices.push_back(v); } - EXCEPT_GUARD(t->setVertices(vertices);) + luax_catchexcept(L, [&](){ t->setVertices(vertices); }); return 0; } @@ -216,7 +216,7 @@ int w_Mesh_setVertexMap(lua_State *L) vertexmap.push_back(uint32(luaL_checkinteger(L, i + 2) - 1)); } - EXCEPT_GUARD(t->setVertexMap(vertexmap);) + luax_catchexcept(L, [&](){ t->setVertexMap(vertexmap); }); return 0; } @@ -225,7 +225,7 @@ int w_Mesh_getVertexMap(lua_State *L) Mesh *t = luax_checkmesh(L, 1); std::vector vertex_map; - EXCEPT_GUARD(t->getVertexMap(vertex_map);) + luax_catchexcept(L, [&](){ t->getVertexMap(vertex_map); }); size_t element_count = vertex_map.size(); @@ -315,7 +315,7 @@ int w_Mesh_setDrawRange(lua_State *L) { int rangemin = luaL_checkint(L, 2) - 1; int rangemax = luaL_checkint(L, 3) - 1; - EXCEPT_GUARD(t->setDrawRange(rangemin, rangemax);) + luax_catchexcept(L, [&](){ t->setDrawRange(rangemin, rangemax); }); } return 0; diff --git a/src/modules/graphics/opengl/wrap_ParticleSystem.cpp b/src/modules/graphics/opengl/wrap_ParticleSystem.cpp index 6cb3c0416..f813774ae 100644 --- a/src/modules/graphics/opengl/wrap_ParticleSystem.cpp +++ b/src/modules/graphics/opengl/wrap_ParticleSystem.cpp @@ -49,7 +49,7 @@ int w_ParticleSystem_clone(lua_State *L) ParticleSystem *t = luax_checkparticlesystem(L, 1); ParticleSystem *clone = nullptr; - EXCEPT_GUARD(clone = t->clone();) + luax_catchexcept(L, [&](){ clone = t->clone(); }); luax_pushtype(L, "ParticleSystem", GRAPHICS_PARTICLE_SYSTEM_T, clone); return 1; @@ -90,7 +90,7 @@ int w_ParticleSystem_setBufferSize(lua_State *L) if (arg1 < 1.0 || arg1 > ParticleSystem::MAX_PARTICLES) return luaL_error(L, "Invalid buffer size"); - EXCEPT_GUARD(t->setBufferSize((uint32) arg1);) + luax_catchexcept(L, [&](){ t->setBufferSize((uint32) arg1); }); return 0; } @@ -128,7 +128,7 @@ int w_ParticleSystem_setEmissionRate(lua_State *L) { ParticleSystem *t = luax_checkparticlesystem(L, 1); float arg1 = (float) luaL_checknumber(L, 2); - EXCEPT_GUARD(t->setEmissionRate(arg1);) + luax_catchexcept(L, [&](){ t->setEmissionRate(arg1); }); return 0; } diff --git a/src/modules/graphics/opengl/wrap_Shader.cpp b/src/modules/graphics/opengl/wrap_Shader.cpp index c48249bb5..e862d02d1 100644 --- a/src/modules/graphics/opengl/wrap_Shader.cpp +++ b/src/modules/graphics/opengl/wrap_Shader.cpp @@ -232,10 +232,10 @@ int w_Shader_sendMatrix(lua_State *L) lua_pop(L, 1 + dimension); } - EXCEPT_GUARD_FINALLY( - { shader->sendMatrix(name, dimension, values, count); }, - { delete[] values; } - ) + luax_catchexcept(L, + [&]() { shader->sendMatrix(name, dimension, values, count); }, + [&]() { delete[] values; } + ); return 0; } @@ -246,7 +246,7 @@ int w_Shader_sendTexture(lua_State *L) const char *name = luaL_checkstring(L, 2); Texture *texture = luax_checktexture(L, 3); - EXCEPT_GUARD(shader->sendTexture(name, texture);) + luax_catchexcept(L, [&](){ shader->sendTexture(name, texture); }); return 0; } diff --git a/src/modules/graphics/opengl/wrap_SpriteBatch.cpp b/src/modules/graphics/opengl/wrap_SpriteBatch.cpp index 68cb07853..1200743e8 100644 --- a/src/modules/graphics/opengl/wrap_SpriteBatch.cpp +++ b/src/modules/graphics/opengl/wrap_SpriteBatch.cpp @@ -64,12 +64,12 @@ int w_SpriteBatch_add(lua_State *L) float ky = (float) luaL_optnumber(L, startidx + 8, 0.0); int id = 0; - EXCEPT_GUARD( + luax_catchexcept(L, [&]() { if (quad) id = t->addq(quad, x, y, a, sx, sy, ox, oy, kx, ky); else id = t->add(x, y, a, sx, sy, ox, oy, kx, ky); - ) + }); lua_pushinteger(L, id); return 1; @@ -101,12 +101,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); - EXCEPT_GUARD( + luax_catchexcept(L, [&]() { if (quad) t->addq(quad, x, y, a, sx, sy, ox, oy, kx, ky, id); else t->add(x, y, a, sx, sy, ox, oy, kx, ky, id); - ) + }); return 0; } @@ -121,7 +121,7 @@ int w_SpriteBatch_clear(lua_State *L) int w_SpriteBatch_bind(lua_State *L) { SpriteBatch *t = luax_checkspritebatch(L, 1); - EXCEPT_GUARD(t->lock();) + luax_catchexcept(L, [&](){ t->lock(); }); return 0; } @@ -223,7 +223,7 @@ int w_SpriteBatch_setBufferSize(lua_State *L) { SpriteBatch *t = luax_checkspritebatch(L, 1); int size = luaL_checkint(L, 2); - EXCEPT_GUARD(t->setBufferSize(size);) + luax_catchexcept(L, [&]() {t->setBufferSize(size); }); return 0; } diff --git a/src/modules/graphics/opengl/wrap_Texture.cpp b/src/modules/graphics/opengl/wrap_Texture.cpp index 3401115a5..830c72659 100644 --- a/src/modules/graphics/opengl/wrap_Texture.cpp +++ b/src/modules/graphics/opengl/wrap_Texture.cpp @@ -69,7 +69,7 @@ int w_Texture_setFilter(lua_State *L) f.anisotropy = (float) luaL_optnumber(L, 4, 1.0); - EXCEPT_GUARD(t->setFilter(f);) + luax_catchexcept(L, [&](){ t->setFilter(f); }); return 0; } diff --git a/src/modules/image/CompressedData.cpp b/src/modules/image/CompressedData.cpp index e4d47331c..d72ab5b0d 100644 --- a/src/modules/image/CompressedData.cpp +++ b/src/modules/image/CompressedData.cpp @@ -87,7 +87,7 @@ CompressedData::Format CompressedData::getFormat() const void CompressedData::checkMipmapLevelExists(int miplevel) const { if (miplevel < 0 || miplevel >= (int) dataImages.size()) - throw love::Exception("Mipmap level %d does not exist", miplevel); + throw love::Exception("Mipmap level %d does not exist", miplevel + 1); } bool CompressedData::getConstant(const char *in, CompressedData::Format &out) diff --git a/src/modules/image/wrap_CompressedData.cpp b/src/modules/image/wrap_CompressedData.cpp index f4b0c5752..3b0fd65ce 100644 --- a/src/modules/image/wrap_CompressedData.cpp +++ b/src/modules/image/wrap_CompressedData.cpp @@ -37,7 +37,7 @@ int w_CompressedData_getWidth(lua_State *L) int miplevel = luaL_optinteger(L, 2, 1); int width = 0; - EXCEPT_GUARD(width = t->getWidth(miplevel >= 1 ? miplevel - 1 : 0);) + luax_catchexcept(L, [&](){ width = t->getWidth(miplevel - 1); }); lua_pushinteger(L, width); return 1; @@ -49,7 +49,7 @@ int w_CompressedData_getHeight(lua_State *L) int miplevel = luaL_optinteger(L, 2, 1); int height = 0; - EXCEPT_GUARD(height = t->getHeight(miplevel >= 1 ? miplevel - 1 : 0);) + luax_catchexcept(L, [&](){ height = t->getHeight(miplevel - 1); }); lua_pushinteger(L, height); return 1; @@ -61,10 +61,11 @@ int w_CompressedData_getDimensions(lua_State *L) int miplevel = luaL_optinteger(L, 2, 1); int width = 0, height = 0; - EXCEPT_GUARD( - width = t->getWidth(miplevel >= 1 ? miplevel - 1 : 0); - height = t->getHeight(miplevel >= 1 ? miplevel - 1 : 0); - ) + luax_catchexcept(L, [&]() + { + width = t->getWidth(miplevel - 1); + height = t->getHeight(miplevel - 1); + }); lua_pushinteger(L, width); lua_pushinteger(L, height); diff --git a/src/modules/image/wrap_Image.cpp b/src/modules/image/wrap_Image.cpp index dec04c8f5..bd37950cc 100644 --- a/src/modules/image/wrap_Image.cpp +++ b/src/modules/image/wrap_Image.cpp @@ -45,7 +45,7 @@ int w_newImageData(lua_State *L) return luaL_error(L, "Invalid image size."); ImageData *t = nullptr; - EXCEPT_GUARD(t = instance->newImageData(w, h);) + luax_catchexcept(L, [&](){ t = instance->newImageData(w, h); }); luax_pushtype(L, "ImageData", IMAGE_IMAGE_DATA_T, t); return 1; @@ -55,7 +55,10 @@ int w_newImageData(lua_State *L) love::filesystem::FileData *data = love::filesystem::luax_getFileData(L, 1); ImageData *t = nullptr; - EXCEPT_GUARD_FINALLY(t = instance->newImageData(data);, data->release();) + luax_catchexcept(L, + [&]() { t = instance->newImageData(data); }, + [&]() { data->release(); } + ); luax_pushtype(L, "ImageData", IMAGE_IMAGE_DATA_T, t); return 1; @@ -66,7 +69,10 @@ int w_newCompressedData(lua_State *L) love::filesystem::FileData *data = love::filesystem::luax_getFileData(L, 1); CompressedData *t = nullptr; - EXCEPT_GUARD_FINALLY(t = instance->newCompressedData(data);, data->release();) + luax_catchexcept(L, + [&]() { t = instance->newCompressedData(data); }, + [&]() { data->release(); } + ); luax_pushtype(L, "CompressedData", IMAGE_COMPRESSED_DATA_T, t); return 1; @@ -102,7 +108,7 @@ extern "C" int luaopen_love_image(lua_State *L) { if (instance == nullptr) { - EXCEPT_GUARD(instance = new love::image::magpie::Image();) + luax_catchexcept(L, [&](){ 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 401e4b718..cb3c941a1 100644 --- a/src/modules/image/wrap_ImageData.cpp +++ b/src/modules/image/wrap_ImageData.cpp @@ -62,7 +62,7 @@ int w_ImageData_getPixel(lua_State *L) int y = luaL_checkint(L, 3); pixel c; - EXCEPT_GUARD(c = t->getPixel(x, y);) + luax_catchexcept(L, [&](){ c = t->getPixel(x, y); }); lua_pushnumber(L, c.r); lua_pushnumber(L, c.g); @@ -98,7 +98,7 @@ int w_ImageData_setPixel(lua_State *L) c.a = (unsigned char)luaL_optinteger(L, 7, 255); } - EXCEPT_GUARD(t->setPixel(x, y, c);) + luax_catchexcept(L, [&](){ t->setPixel(x, y, c); }); return 0; } @@ -261,7 +261,7 @@ int w_ImageData_encode(lua_State *L) return luaL_error(L, "Invalid image format '%s'.", fmt); } - EXCEPT_GUARD(t->encode(file, format);) + luax_catchexcept(L, [&](){ 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 7a438ff16..305a5a9d9 100644 --- a/src/modules/joystick/sdl/wrap_JoystickModule.cpp +++ b/src/modules/joystick/sdl/wrap_JoystickModule.cpp @@ -107,7 +107,7 @@ int w_setGamepadMapping(lua_State *L) } bool success = false; - EXCEPT_GUARD(success = instance->setGamepadMapping(guid, gpinput, jinput);) + luax_catchexcept(L, [&](){ success = instance->setGamepadMapping(guid, gpinput, jinput); }); luax_pushboolean(L, success); return 1; @@ -140,7 +140,7 @@ int w_getGamepadMapping(lua_State *L) Joystick::JoystickInput jinput; jinput.type = Joystick::INPUT_TYPE_MAX_ENUM; - EXCEPT_GUARD(jinput = instance->getGamepadMapping(guid, gpinput);) + luax_catchexcept(L, [&](){ jinput = instance->getGamepadMapping(guid, gpinput); }); if (jinput.type == Joystick::INPUT_TYPE_MAX_ENUM) return 0; @@ -196,7 +196,7 @@ extern "C" int luaopen_love_joystick(lua_State *L) { if (instance == nullptr) { - EXCEPT_GUARD(instance = new JoystickModule();) + luax_catchexcept(L, [&](){ instance = new JoystickModule(); }); } else instance->retain(); diff --git a/src/modules/keyboard/wrap_Keyboard.cpp b/src/modules/keyboard/wrap_Keyboard.cpp index da46ba3d3..de48c52bd 100644 --- a/src/modules/keyboard/wrap_Keyboard.cpp +++ b/src/modules/keyboard/wrap_Keyboard.cpp @@ -68,8 +68,34 @@ int w_setTextInput(lua_State *L) return 0; } +template +static int throwExcept(lua_State *L, const T& func) +{ + bool should_error = false; + + try + { + func(); + } + catch (love::Exception &e) + { + should_error = true; + lua_pushstring(L, e.what()); + } + + if (should_error) + return luaL_error(L, lua_tostring(L, -1)); + + return 0; +} + int w_hasTextInput(lua_State *L) { + bool asdf = true; + throwExcept(L, [&](){ instance->setTextInput(asdf); }); + +// throwExcept(L, L); + luax_pushboolean(L, instance->hasTextInput()); return 1; } @@ -89,7 +115,7 @@ extern "C" int luaopen_love_keyboard(lua_State *L) { if (instance == nullptr) { - EXCEPT_GUARD(instance = new love::keyboard::sdl::Keyboard();) + luax_catchexcept(L, [&](){ 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 4ab196455..e13d14701 100644 --- a/src/modules/math/wrap_BezierCurve.cpp +++ b/src/modules/math/wrap_BezierCurve.cpp @@ -56,11 +56,11 @@ int w_BezierCurve_getControlPoint(lua_State *L) if (idx > 0) // 1-indexing idx--; - EXCEPT_GUARD( + luax_catchexcept(L, [&]() { Vector v = curve->getControlPoint(idx); lua_pushnumber(L, v.x); lua_pushnumber(L, v.y); - ) + }); return 2; } @@ -75,7 +75,7 @@ int w_BezierCurve_setControlPoint(lua_State *L) if (idx > 0) // 1-indexing idx--; - EXCEPT_GUARD(curve->setControlPoint(idx, Vector(vx,vy));) + luax_catchexcept(L, [&](){ curve->setControlPoint(idx, Vector(vx,vy)); }); return 0; } @@ -89,7 +89,7 @@ int w_BezierCurve_insertControlPoint(lua_State *L) if (idx > 0) // 1-indexing idx--; - EXCEPT_GUARD(curve->insertControlPoint(Vector(vx,vy), idx);) + luax_catchexcept(L, [&](){ curve->insertControlPoint(Vector(vx,vy), idx); }); return 0; } @@ -134,11 +134,11 @@ int w_BezierCurve_evaluate(lua_State *L) BezierCurve *curve = luax_checkbeziercurve(L, 1); double t = luaL_checknumber(L, 2); - EXCEPT_GUARD( + luax_catchexcept(L, [&]() { Vector v = curve->evaluate(t); lua_pushnumber(L, v.x); lua_pushnumber(L, v.y); - ) + }); return 2; @@ -150,7 +150,7 @@ int w_BezierCurve_render(lua_State *L) int accuracy = luaL_optinteger(L, 2, 5); std::vector points; - EXCEPT_GUARD(points = curve->render(accuracy);) + luax_catchexcept(L, [&](){ 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 093f30e42..402d7af4f 100644 --- a/src/modules/math/wrap_Math.cpp +++ b/src/modules/math/wrap_Math.cpp @@ -49,7 +49,7 @@ int w_randomNormal(lua_State *L) int w_setRandomSeed(lua_State *L) { - EXCEPT_GUARD(Math::instance.setRandomSeed(luax_checkrandomseed(L, 1));) + luax_catchexcept(L, [&](){ Math::instance.setRandomSeed(luax_checkrandomseed(L, 1)); }); return 0; } @@ -63,7 +63,7 @@ int w_getRandomSeed(lua_State *L) int w_setRandomState(lua_State *L) { - EXCEPT_GUARD(Math::instance.setRandomState(luax_checkstring(L, 1));) + luax_catchexcept(L, [&](){ Math::instance.setRandomState(luax_checkstring(L, 1)); }); return 0; } @@ -180,12 +180,12 @@ int w_triangulate(lua_State *L) std::vector triangles; - EXCEPT_GUARD( + luax_catchexcept(L, [&]() { if (vertices.size() == 3) triangles.push_back(Triangle(vertices[0], vertices[1], vertices[2])); else triangles = Math::instance.triangulate(vertices); - ) + }); 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 ea029df2a..6935d8591 100644 --- a/src/modules/math/wrap_RandomGenerator.cpp +++ b/src/modules/math/wrap_RandomGenerator.cpp @@ -108,7 +108,7 @@ int w_RandomGenerator_randomNormal(lua_State *L) int w_RandomGenerator_setSeed(lua_State *L) { RandomGenerator *rng = luax_checkrandomgenerator(L, 1); - EXCEPT_GUARD(rng->setSeed(luax_checkrandomseed(L, 2));) + luax_catchexcept(L, [&](){ rng->setSeed(luax_checkrandomseed(L, 2)); }); return 0; } @@ -124,7 +124,7 @@ int w_RandomGenerator_getSeed(lua_State *L) int w_RandomGenerator_setState(lua_State *L) { RandomGenerator *rng = luax_checkrandomgenerator(L, 1); - EXCEPT_GUARD(rng->setState(luax_checkstring(L, 2));) + luax_catchexcept(L, [&](){ rng->setState(luax_checkstring(L, 2)); }); return 0; } diff --git a/src/modules/mouse/wrap_Mouse.cpp b/src/modules/mouse/wrap_Mouse.cpp index ea68f31d3..849117cda 100644 --- a/src/modules/mouse/wrap_Mouse.cpp +++ b/src/modules/mouse/wrap_Mouse.cpp @@ -43,7 +43,7 @@ int w_newCursor(lua_State *L) int hotx = luaL_optint(L, 2, 0); int hoty = luaL_optint(L, 3, 0); - EXCEPT_GUARD(cursor = instance->newCursor(data, hotx, hoty);) + luax_catchexcept(L, [&](){ cursor = instance->newCursor(data, hotx, hoty); }); luax_pushtype(L, "Cursor", MOUSE_CURSOR_T, cursor); return 1; @@ -58,7 +58,7 @@ int w_getSystemCursor(lua_State *L) return luaL_error(L, "Invalid system cursor type: %s", str); Cursor *cursor = 0; - EXCEPT_GUARD(cursor = instance->getSystemCursor(systemCursor);) + luax_catchexcept(L, [&](){ cursor = instance->getSystemCursor(systemCursor); }); cursor->retain(); luax_pushtype(L, "Cursor", MOUSE_CURSOR_T, cursor); @@ -214,7 +214,7 @@ extern "C" int luaopen_love_mouse(lua_State *L) { if (instance == nullptr) { - EXCEPT_GUARD(instance = new love::mouse::sdl::Mouse();) + luax_catchexcept(L, [&](){ 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 a016c7a05..96bc49424 100644 --- a/src/modules/physics/box2d/Physics.cpp +++ b/src/modules/physics/box2d/Physics.cpp @@ -246,7 +246,7 @@ int Physics::getDistance(lua_State *L) b2SimplexCache c; c.count = 0; - EXCEPT_GUARD( + luax_catchexcept(L, [&]() { pA.Set(fixtureA->fixture->GetShape(), 0); pB.Set(fixtureB->fixture->GetShape(), 0); i.proxyA = pA; @@ -255,7 +255,7 @@ int Physics::getDistance(lua_State *L) i.transformB = fixtureB->fixture->GetBody()->GetTransform(); i.useRadii = true; b2Distance(&o, &c, &i); - ) + }); lua_pushnumber(L, Physics::scaleUp(o.distance)); lua_pushnumber(L, Physics::scaleUp(o.pointA.x)); diff --git a/src/modules/physics/box2d/wrap_Body.cpp b/src/modules/physics/box2d/wrap_Body.cpp index 61eb7529d..f3551030c 100644 --- a/src/modules/physics/box2d/wrap_Body.cpp +++ b/src/modules/physics/box2d/wrap_Body.cpp @@ -241,7 +241,7 @@ int w_Body_setX(lua_State *L) { Body *t = luax_checkbody(L, 1); float arg1 = (float)luaL_checknumber(L, 2); - EXCEPT_GUARD(t->setX(arg1);) + luax_catchexcept(L, [&](){ t->setX(arg1); }); return 0; } @@ -249,7 +249,7 @@ int w_Body_setY(lua_State *L) { Body *t = luax_checkbody(L, 1); float arg1 = (float)luaL_checknumber(L, 2); - EXCEPT_GUARD(t->setY(arg1);) + luax_catchexcept(L, [&](){ t->setY(arg1); }); return 0; } @@ -266,7 +266,7 @@ int w_Body_setAngle(lua_State *L) { Body *t = luax_checkbody(L, 1); float arg1 = (float)luaL_checknumber(L, 2); - EXCEPT_GUARD(t->setAngle(arg1);) + luax_catchexcept(L, [&](){ t->setAngle(arg1); }); return 0; } @@ -283,14 +283,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); - EXCEPT_GUARD(t->setPosition(arg1, arg2);) + luax_catchexcept(L, [&](){ t->setPosition(arg1, arg2); }); return 0; } int w_Body_resetMassData(lua_State *L) { Body *t = luax_checkbody(L, 1); - EXCEPT_GUARD(t->resetMassData();) + luax_catchexcept(L, [&](){ t->resetMassData(); }); return 0; } @@ -301,7 +301,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); - EXCEPT_GUARD(t->setMassData(x, y, m, i);) + luax_catchexcept(L, [&](){ t->setMassData(x, y, m, i); }); return 0; } @@ -309,7 +309,7 @@ int w_Body_setMass(lua_State *L) { Body *t = luax_checkbody(L, 1); float m = (float)luaL_checknumber(L, 2); - EXCEPT_GUARD(t->setMass(m);) + luax_catchexcept(L, [&](){ t->setMass(m); }); return 0; } @@ -317,7 +317,7 @@ int w_Body_setInertia(lua_State *L) { Body *t = luax_checkbody(L, 1); float i = (float)luaL_checknumber(L, 2); - EXCEPT_GUARD(t->setInertia(i);) + luax_catchexcept(L, [&](){ t->setInertia(i); }); return 0; } @@ -351,7 +351,7 @@ int w_Body_setType(lua_State *L) const char *typeStr = luaL_checkstring(L, 2); Body::Type type; Body::getConstant(typeStr, type); - EXCEPT_GUARD(t->setType(type);) + luax_catchexcept(L, [&](){ t->setType(type); }); return 0; } @@ -494,7 +494,7 @@ int w_Body_setActive(lua_State *L) { Body *t = luax_checkbody(L, 1); bool b = luax_toboolean(L, 2); - EXCEPT_GUARD(t->setActive(b);) + luax_catchexcept(L, [&](){ t->setActive(b); }); return 0; } @@ -510,7 +510,7 @@ int w_Body_setFixedRotation(lua_State *L) { Body *t = luax_checkbody(L, 1); bool b = luax_toboolean(L, 2); - EXCEPT_GUARD(t->setFixedRotation(b);) + luax_catchexcept(L, [&](){ t->setFixedRotation(b); }); return 0; } @@ -527,14 +527,14 @@ int w_Body_getFixtureList(lua_State *L) Body *t = luax_checkbody(L, 1); lua_remove(L, 1); int n = 0; - EXCEPT_GUARD(n = t->getFixtureList(L);) + luax_catchexcept(L, [&](){ n = t->getFixtureList(L); }); return n; } int w_Body_destroy(lua_State *L) { Body *t = luax_checkbody(L, 1); - EXCEPT_GUARD(t->destroy();) + luax_catchexcept(L, [&](){ t->destroy(); }); return 0; } diff --git a/src/modules/physics/box2d/wrap_ChainShape.cpp b/src/modules/physics/box2d/wrap_ChainShape.cpp index 245d4c0f4..76a1a5b90 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); - EXCEPT_GUARD(c->setNextVertex(x, y);) + luax_catchexcept(L, [&](){ c->setNextVertex(x, y); }); return 0; } @@ -48,7 +48,7 @@ int w_ChainShape_setPreviousVertex(lua_State *L) ChainShape *c = luax_checkchainshape(L, 1); float x = (float)luaL_checknumber(L, 2); float y = (float)luaL_checknumber(L, 3); - EXCEPT_GUARD(c->setPreviousVertex(x, y);) + luax_catchexcept(L, [&](){ c->setPreviousVertex(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; - EXCEPT_GUARD(e = c->getChildEdge(index);) + luax_catchexcept(L, [&](){ 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; - EXCEPT_GUARD(v = c->getPoint(index);) + luax_catchexcept(L, [&](){ 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 953f68069..621e58bd9 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); - EXCEPT_GUARD(t->setDensity(arg1);) + luax_catchexcept(L, [&](){ t->setDensity(arg1); }); return 0; } @@ -157,7 +157,7 @@ int w_Fixture_rayCast(lua_State *L) Fixture *t = luax_checkfixture(L, 1); lua_remove(L, 1); int ret = 0; - EXCEPT_GUARD(ret = t->rayCast(L);) + luax_catchexcept(L, [&](){ ret = t->rayCast(L); }); return ret; } @@ -258,7 +258,7 @@ int w_Fixture_setGroupIndex(lua_State *L) int w_Fixture_destroy(lua_State *L) { Fixture *t = luax_checkfixture(L, 1); - EXCEPT_GUARD(t->destroy();) + luax_catchexcept(L, [&](){ t->destroy(); }); return 0; } diff --git a/src/modules/physics/box2d/wrap_FrictionJoint.cpp b/src/modules/physics/box2d/wrap_FrictionJoint.cpp index ce5336fac..7124a8530 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); - EXCEPT_GUARD(t->setMaxForce(arg1);) + luax_catchexcept(L, [&](){ 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); - EXCEPT_GUARD(t->setMaxTorque(arg1);) + luax_catchexcept(L, [&](){ t->setMaxTorque(arg1); }); return 0; } diff --git a/src/modules/physics/box2d/wrap_GearJoint.cpp b/src/modules/physics/box2d/wrap_GearJoint.cpp index e8189f7a2..c6f84114f 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); - EXCEPT_GUARD(t->setRatio(arg1);) + luax_catchexcept(L, [&](){ t->setRatio(arg1); }); return 0; } diff --git a/src/modules/physics/box2d/wrap_Joint.cpp b/src/modules/physics/box2d/wrap_Joint.cpp index 259a09b23..053f8b45d 100644 --- a/src/modules/physics/box2d/wrap_Joint.cpp +++ b/src/modules/physics/box2d/wrap_Joint.cpp @@ -78,7 +78,7 @@ int w_Joint_getCollideConnected(lua_State *L) int w_Joint_destroy(lua_State *L) { Joint *t = luax_checkjoint(L, 1); - EXCEPT_GUARD(t->destroyJoint();) + luax_catchexcept(L, [&](){ t->destroyJoint(); }); return 0; } diff --git a/src/modules/physics/box2d/wrap_MotorJoint.cpp b/src/modules/physics/box2d/wrap_MotorJoint.cpp index b264e17a9..4403ddc88 100644 --- a/src/modules/physics/box2d/wrap_MotorJoint.cpp +++ b/src/modules/physics/box2d/wrap_MotorJoint.cpp @@ -69,7 +69,7 @@ int w_MotorJoint_setMaxForce(lua_State *L) { MotorJoint *t = luax_checkmotorjoint(L, 1); float arg1 = (float) luaL_checknumber(L, 2); - EXCEPT_GUARD(t->setMaxForce(arg1);) + luax_catchexcept(L, [&](){ t->setMaxForce(arg1); }); return 0; } @@ -84,7 +84,7 @@ int w_MotorJoint_setMaxTorque(lua_State *L) { MotorJoint *t = luax_checkmotorjoint(L, 1); float arg1 = (float) luaL_checknumber(L, 2); - EXCEPT_GUARD(t->setMaxTorque(arg1);) + luax_catchexcept(L, [&](){ t->setMaxTorque(arg1); }); return 0; } @@ -99,7 +99,7 @@ int w_MotorJoint_setCorrectionFactor(lua_State *L) { MotorJoint *t = luax_checkmotorjoint(L, 1); float arg1 = (float) luaL_checknumber(L, 2); - EXCEPT_GUARD(t->setCorrectionFactor(arg1);) + luax_catchexcept(L, [&](){ t->setCorrectionFactor(arg1); }); return 0; } diff --git a/src/modules/physics/box2d/wrap_Physics.cpp b/src/modules/physics/box2d/wrap_Physics.cpp index b53bb5c78..331e7d2f3 100644 --- a/src/modules/physics/box2d/wrap_Physics.cpp +++ b/src/modules/physics/box2d/wrap_Physics.cpp @@ -58,7 +58,7 @@ int w_newWorld(lua_State *L) bool sleep = luax_optboolean(L, 3, true); World *w; - EXCEPT_GUARD(w = instance->newWorld(gx, gy, sleep);) + luax_catchexcept(L, [&](){ w = instance->newWorld(gx, gy, sleep); }); luax_pushtype(L, "World", PHYSICS_WORLD_T, w); return 1; @@ -76,7 +76,7 @@ int w_newBody(lua_State *L) return luaL_error(L, "Invalid Body type: %s", typestr); Body *body; - EXCEPT_GUARD(body = instance->newBody(world, x, y, btype);) + luax_catchexcept(L, [&](){ body = instance->newBody(world, x, y, btype); }); luax_pushtype(L, "Body", PHYSICS_BODY_T, body); return 1; } @@ -87,7 +87,7 @@ int w_newFixture(lua_State *L) Shape *shape = luax_checkshape(L, 2); float density = (float)luaL_optnumber(L, 3, 1.0f); Fixture *fixture; - EXCEPT_GUARD(fixture = instance->newFixture(body, shape, density);) + luax_catchexcept(L, [&](){ fixture = instance->newFixture(body, shape, density); }); luax_pushtype(L, "Fixture", PHYSICS_FIXTURE_T, fixture); return 1; } @@ -100,7 +100,7 @@ int w_newCircleShape(lua_State *L) { float radius = (float)luaL_checknumber(L, 1); CircleShape *shape; - EXCEPT_GUARD(shape = instance->newCircleShape(radius);) + luax_catchexcept(L, [&](){ shape = instance->newCircleShape(radius); }); luax_pushtype(L, "CircleShape", PHYSICS_CIRCLE_SHAPE_T, shape); return 1; } @@ -110,7 +110,7 @@ int w_newCircleShape(lua_State *L) float y = (float)luaL_checknumber(L, 2); float radius = (float)luaL_checknumber(L, 3); CircleShape *shape; - EXCEPT_GUARD(shape = instance->newCircleShape(x, y, radius);) + luax_catchexcept(L, [&](){ shape = instance->newCircleShape(x, y, radius); }); luax_pushtype(L, "CircleShape", PHYSICS_CIRCLE_SHAPE_T, shape); return 1; } @@ -127,7 +127,7 @@ int w_newRectangleShape(lua_State *L) float w = (float)luaL_checknumber(L, 1); float h = (float)luaL_checknumber(L, 2); PolygonShape *shape; - EXCEPT_GUARD(shape = instance->newRectangleShape(w, h);) + luax_catchexcept(L, [&](){ shape = instance->newRectangleShape(w, h); }); luax_pushtype(L, "PolygonShape", PHYSICS_POLYGON_SHAPE_T, shape); return 1; } @@ -139,7 +139,7 @@ int w_newRectangleShape(lua_State *L) float h = (float)luaL_checknumber(L, 4); float angle = (float)luaL_optnumber(L, 5, 0); PolygonShape *shape; - EXCEPT_GUARD(shape = instance->newRectangleShape(x, y, w, h, angle);) + luax_catchexcept(L, [&](){ shape = instance->newRectangleShape(x, y, w, h, angle); }); luax_pushtype(L, "PolygonShape", PHYSICS_POLYGON_SHAPE_T, shape); return 1; } @@ -154,7 +154,7 @@ int w_newEdgeShape(lua_State *L) float x2 = (float)luaL_checknumber(L, 3); float y2 = (float)luaL_checknumber(L, 4); EdgeShape *shape; - EXCEPT_GUARD(shape = instance->newEdgeShape(x1, y1, x2, y2);) + luax_catchexcept(L, [&](){ shape = instance->newEdgeShape(x1, y1, x2, y2); }); luax_pushtype(L, "EdgeShape", PHYSICS_EDGE_SHAPE_T, shape); return 1; } @@ -162,14 +162,14 @@ int w_newEdgeShape(lua_State *L) int w_newPolygonShape(lua_State *L) { int ret = 0; - EXCEPT_GUARD(ret = instance->newPolygonShape(L);) + luax_catchexcept(L, [&](){ ret = instance->newPolygonShape(L); }); return ret; } int w_newChainShape(lua_State *L) { int ret = 0; - EXCEPT_GUARD(ret = instance->newChainShape(L);) + luax_catchexcept(L, [&](){ ret = instance->newChainShape(L); }); return ret; } @@ -183,7 +183,9 @@ int w_newDistanceJoint(lua_State *L) float y2 = (float)luaL_checknumber(L, 6); bool collideConnected = luax_optboolean(L, 7, false); DistanceJoint *j; - EXCEPT_GUARD(j = instance->newDistanceJoint(body1, body2, x1, y1, x2, y2, collideConnected);) + luax_catchexcept(L, [&]() { + j = instance->newDistanceJoint(body1, body2, x1, y1, x2, y2, collideConnected); + }); luax_pushtype(L, "DistanceJoint", PHYSICS_DISTANCE_JOINT_T, j); return 1; } @@ -194,7 +196,7 @@ int w_newMouseJoint(lua_State *L) float x = (float)luaL_checknumber(L, 2); float y = (float)luaL_checknumber(L, 3); MouseJoint *j; - EXCEPT_GUARD(j = instance->newMouseJoint(body, x, y);) + luax_catchexcept(L, [&](){ j = instance->newMouseJoint(body, x, y); }); luax_pushtype(L, "MouseJoint", PHYSICS_MOUSE_JOINT_T, j); return 1; } @@ -207,7 +209,9 @@ int w_newRevoluteJoint(lua_State *L) float y = (float)luaL_checknumber(L, 4); bool collideConnected = luax_optboolean(L, 5, false); RevoluteJoint *j; - EXCEPT_GUARD(j = instance->newRevoluteJoint(body1, body2, x, y, collideConnected);) + luax_catchexcept(L, [&]() { + j = instance->newRevoluteJoint(body1, body2, x, y, collideConnected); + }); luax_pushtype(L, "RevoluteJoint", PHYSICS_REVOLUTE_JOINT_T, j); return 1; } @@ -237,7 +241,9 @@ int w_newPrismaticJoint(lua_State *L) collideConnected = luax_optboolean(L, 7, false); } PrismaticJoint *j; - EXCEPT_GUARD(j = instance->newPrismaticJoint(body1, body2, xA, yA, xB, yB, ax, ay, collideConnected);) + luax_catchexcept(L, [&]() { + j = instance->newPrismaticJoint(body1, body2, xA, yA, xB, yB, ax, ay, collideConnected); + }); luax_pushtype(L, "PrismaticJoint", PHYSICS_PRISMATIC_JOINT_T, j); return 1; } @@ -258,7 +264,9 @@ int w_newPulleyJoint(lua_State *L) bool collideConnected = luax_optboolean(L, 12, true); // PulleyJoints default to colliding connected bodies, see b2PulleyJoint.h PulleyJoint *j; - EXCEPT_GUARD(j = instance->newPulleyJoint(body1, body2, b2Vec2(gx1,gy1), b2Vec2(gx2,gy2), b2Vec2(x1,y1), b2Vec2(x2,y2), ratio, collideConnected);) + luax_catchexcept(L, [&]() { + 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; } @@ -271,7 +279,9 @@ int w_newGearJoint(lua_State *L) bool collideConnected = luax_optboolean(L, 4, false); GearJoint *j; - EXCEPT_GUARD(j = instance->newGearJoint(joint1, joint2, ratio, collideConnected);) + luax_catchexcept(L, [&]() { + j = instance->newGearJoint(joint1, joint2, ratio, collideConnected); + }); luax_pushtype(L, "GearJoint", PHYSICS_GEAR_JOINT_T, j); return 1; } @@ -297,7 +307,9 @@ int w_newFrictionJoint(lua_State *L) collideConnected = luax_optboolean(L, 5, false); } FrictionJoint *j; - EXCEPT_GUARD(j = instance->newFrictionJoint(body1, body2, xA, yA, xB, yB, collideConnected);) + luax_catchexcept(L, [&]() { + j = instance->newFrictionJoint(body1, body2, xA, yA, xB, yB, collideConnected); + }); luax_pushtype(L, "FrictionJoint", PHYSICS_FRICTION_JOINT_T, j); return 1; } @@ -323,7 +335,9 @@ int w_newWeldJoint(lua_State *L) collideConnected = luax_optboolean(L, 5, false); } WeldJoint *j; - EXCEPT_GUARD(j = instance->newWeldJoint(body1, body2, xA, yA, xB, yB, collideConnected);) + luax_catchexcept(L, [&]() { + j = instance->newWeldJoint(body1, body2, xA, yA, xB, yB, collideConnected); + }); luax_pushtype(L, "WeldJoint", PHYSICS_WELD_JOINT_T, j); return 1; } @@ -354,7 +368,9 @@ int w_newWheelJoint(lua_State *L) } WheelJoint *j; - EXCEPT_GUARD(j = instance->newWheelJoint(body1, body2, xA, yA, xB, yB, ax, ay, collideConnected);) + luax_catchexcept(L, [&]() { + j = instance->newWheelJoint(body1, body2, xA, yA, xB, yB, ax, ay, collideConnected); + }); luax_pushtype(L, "WheelJoint", PHYSICS_WHEEL_JOINT_T, j); return 1; } @@ -370,7 +386,9 @@ int w_newRopeJoint(lua_State *L) float maxLength = (float)luaL_checknumber(L, 7); bool collideConnected = luax_optboolean(L, 8, false); RopeJoint *j; - EXCEPT_GUARD(j = instance->newRopeJoint(body1, body2, x1, y1, x2, y2, maxLength, collideConnected);) + luax_catchexcept(L, [&]() { + j = instance->newRopeJoint(body1, body2, x1, y1, x2, y2, maxLength, collideConnected); + }); luax_pushtype(L, "RopeJoint", PHYSICS_ROPE_JOINT_T, j); return 1; } @@ -383,11 +401,13 @@ int w_newMotorJoint(lua_State *L) if (!lua_isnoneornil(L, 3)) { float correctionFactor = (float)luaL_checknumber(L, 3); - EXCEPT_GUARD(j = instance->newMotorJoint(body1, body2, correctionFactor);) + luax_catchexcept(L, [&]() { + j = instance->newMotorJoint(body1, body2, correctionFactor); + }); } else { - EXCEPT_GUARD(j = instance->newMotorJoint(body1, body2);) + luax_catchexcept(L, [&](){ j = instance->newMotorJoint(body1, body2); }); } luax_pushtype(L, "MotorJoint", PHYSICS_MOTOR_JOINT_T, j); return 1; @@ -401,7 +421,7 @@ int w_getDistance(lua_State *L) int w_setMeter(lua_State *L) { int arg1 = luaL_checkint(L, 1); - EXCEPT_GUARD(Physics::setMeter(arg1);) + luax_catchexcept(L, [&](){ Physics::setMeter(arg1); }); return 0; } @@ -469,7 +489,7 @@ extern "C" int luaopen_love_physics(lua_State *L) { if (instance == 0) { - EXCEPT_GUARD(instance = new Physics();) + luax_catchexcept(L, [&](){ instance = new Physics(); }); } else instance->retain(); diff --git a/src/modules/physics/box2d/wrap_PrismaticJoint.cpp b/src/modules/physics/box2d/wrap_PrismaticJoint.cpp index e3514bf41..ed3b6838f 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); - EXCEPT_GUARD(t->setUpperLimit(arg1);) + luax_catchexcept(L, [&](){ 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); - EXCEPT_GUARD(t->setLowerLimit(arg1);) + luax_catchexcept(L, [&](){ 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); - EXCEPT_GUARD(t->setLimits(arg1, arg2);) + luax_catchexcept(L, [&](){ 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 e510dcf3b..82203f0ad 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); - EXCEPT_GUARD(t->setUpperLimit(arg1);) + luax_catchexcept(L, [&](){ 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); - EXCEPT_GUARD(t->setLowerLimit(arg1);) + luax_catchexcept(L, [&](){ 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); - EXCEPT_GUARD(t->setLimits(arg1, arg2);) + luax_catchexcept(L, [&](){ 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 d31d0fae5..c4d7679ba 100644 --- a/src/modules/physics/box2d/wrap_Shape.cpp +++ b/src/modules/physics/box2d/wrap_Shape.cpp @@ -76,7 +76,7 @@ int w_Shape_rayCast(lua_State *L) Shape *t = luax_checkshape(L, 1); lua_remove(L, 1); int ret = 0; - EXCEPT_GUARD(ret = t->rayCast(L);) + luax_catchexcept(L, [&](){ ret = t->rayCast(L); }); return ret; } diff --git a/src/modules/physics/box2d/wrap_World.cpp b/src/modules/physics/box2d/wrap_World.cpp index fb45ead88..775f6ecf5 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); - EXCEPT_GUARD(t->update(dt);) + luax_catchexcept(L, [&](){ t->update(dt); }); return 0; } @@ -92,7 +92,7 @@ int w_World_translateOrigin(lua_State *L) World *t = luax_checkworld(L, 1); float arg1 = (float)luaL_checknumber(L, 2); float arg2 = (float)luaL_checknumber(L, 3); - EXCEPT_GUARD(t->translateOrigin(arg1, arg2);) + luax_catchexcept(L, [&](){ t->translateOrigin(arg1, arg2); }); return 0; } @@ -144,7 +144,7 @@ int w_World_getBodyList(lua_State *L) World *t = luax_checkworld(L, 1); lua_remove(L, 1); int ret = 0; - EXCEPT_GUARD(ret = t->getBodyList(L);) + luax_catchexcept(L, [&](){ ret = t->getBodyList(L); }); return ret; } @@ -153,7 +153,7 @@ int w_World_getJointList(lua_State *L) World *t = luax_checkworld(L, 1); lua_remove(L, 1); int ret = 0; - EXCEPT_GUARD(ret = t->getJointList(L);) + luax_catchexcept(L, [&](){ ret = t->getJointList(L); }); return ret; } @@ -162,7 +162,7 @@ int w_World_getContactList(lua_State *L) World *t = luax_checkworld(L, 1); lua_remove(L, 1); int ret = 0; - EXCEPT_GUARD(ret = t->getContactList(L);) + luax_catchexcept(L, [&](){ ret = t->getContactList(L); }); return ret; } @@ -178,14 +178,14 @@ int w_World_rayCast(lua_State *L) World *t = luax_checkworld(L, 1); lua_remove(L, 1); int ret = 0; - EXCEPT_GUARD(ret = t->rayCast(L);) + luax_catchexcept(L, [&](){ ret = t->rayCast(L); }); return ret; } int w_World_destroy(lua_State *L) { World *t = luax_checkworld(L, 1); - EXCEPT_GUARD(t->destroy();) + luax_catchexcept(L, [&](){ t->destroy(); }); return 0; } diff --git a/src/modules/sound/wrap_Sound.cpp b/src/modules/sound/wrap_Sound.cpp index d12a42d6c..eaedb9aa0 100644 --- a/src/modules/sound/wrap_Sound.cpp +++ b/src/modules/sound/wrap_Sound.cpp @@ -43,7 +43,7 @@ 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); - EXCEPT_GUARD(t = instance->newSoundData(samples, sampleRate, bitDepth, channels);) + luax_catchexcept(L, [&](){ t = instance->newSoundData(samples, sampleRate, bitDepth, channels); }); } // Must be string or decoder. else @@ -55,7 +55,7 @@ int w_newSoundData(lua_State *L) lua_replace(L, 1); } - EXCEPT_GUARD(t = instance->newSoundData(luax_checkdecoder(L, 1));) + luax_catchexcept(L, [&](){ t = instance->newSoundData(luax_checkdecoder(L, 1)); }); } luax_pushtype(L, "SoundData", SOUND_SOUND_DATA_T, t); @@ -68,7 +68,10 @@ int w_newDecoder(lua_State *L) int bufferSize = luaL_optint(L, 2, Decoder::DEFAULT_BUFFER_SIZE); Decoder *t = nullptr; - EXCEPT_GUARD_FINALLY(t = instance->newDecoder(data, bufferSize);, data->release();) + luax_catchexcept(L, + [&]() { t = instance->newDecoder(data, bufferSize); }, + [&]() { data->release(); } + ); if (t == nullptr) return luaL_error(L, "Extension \"%s\" not supported.", data->getExtension().c_str()); @@ -96,7 +99,7 @@ extern "C" int luaopen_love_sound(lua_State *L) { if (instance == 0) { - EXCEPT_GUARD(instance = new lullaby::Sound();) + luax_catchexcept(L, [&](){ instance = new lullaby::Sound(); }); } else instance->retain(); diff --git a/src/modules/sound/wrap_SoundData.cpp b/src/modules/sound/wrap_SoundData.cpp index 1c5d26a99..dee6db00e 100644 --- a/src/modules/sound/wrap_SoundData.cpp +++ b/src/modules/sound/wrap_SoundData.cpp @@ -73,7 +73,7 @@ int w_SoundData_setSample(lua_State *L) int i = (int) luaL_checkinteger(L, 2); float sample = (float) luaL_checknumber(L, 3); - EXCEPT_GUARD(sd->setSample(i, sample);) + luax_catchexcept(L, [&](){ sd->setSample(i, sample); }); return 0; } @@ -82,7 +82,7 @@ int w_SoundData_getSample(lua_State *L) SoundData *sd = luax_checksounddata(L, 1); int i = (int) luaL_checkinteger(L, 2); - EXCEPT_GUARD(lua_pushnumber(L, sd->getSample(i));) + luax_catchexcept(L, [&](){ 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 c90fb9a6b..58b644959 100644 --- a/src/modules/thread/wrap_ThreadModule.cpp +++ b/src/modules/thread/wrap_ThreadModule.cpp @@ -91,7 +91,7 @@ extern "C" int luaopen_love_thread(lua_State *L) { if (instance == 0) { - EXCEPT_GUARD(instance = new love::thread::ThreadModule();) + luax_catchexcept(L, [&](){ 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 247ae1922..701bc6566 100644 --- a/src/modules/timer/wrap_Timer.cpp +++ b/src/modules/timer/wrap_Timer.cpp @@ -85,7 +85,7 @@ extern "C" int luaopen_love_timer(lua_State *L) { if (instance == 0) { - EXCEPT_GUARD(instance = new love::timer::sdl::Timer();) + luax_catchexcept(L, [&](){ 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 42c14fa74..0252400fb 100644 --- a/src/modules/window/wrap_Window.cpp +++ b/src/modules/window/wrap_Window.cpp @@ -101,7 +101,10 @@ int w_setMode(lua_State *L) // Display index is 1-based in Lua and 0-based internally. settings.display--; - EXCEPT_GUARD(luax_pushboolean(L, instance->setWindow(w, h, &settings));) + luax_catchexcept(L, + [&](){ luax_pushboolean(L, instance->setWindow(w, h, &settings)); } + ); + return 1; } @@ -339,7 +342,7 @@ static const luaL_Reg functions[] = extern "C" int luaopen_love_window(lua_State *L) { - EXCEPT_GUARD(instance = sdl::Window::createSingleton();) + luax_catchexcept(L, [&](){ instance = sdl::Window::createSingleton(); }); WrappedModule w; w.module = instance;