Replaced the EXCEPT_GUARD macro for converting love exceptions into Lua errors with the new luax_catchexcept function, which takes lambda function arguments.

This commit is contained in:
Alex Szpakowski
2014-06-03 19:27:00 -03:00
parent 724bdbd296
commit dfa319e01a
45 changed files with 315 additions and 227 deletions
+2 -2
View File
@@ -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]);
+6 -6
View File
@@ -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<std::string> 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;
+46 -26
View File
@@ -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<love::font::Rasterizer>(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();
+2 -2
View File
@@ -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;
}
+6 -6
View File
@@ -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<uint32> 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;
@@ -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;
}
+5 -5
View File
@@ -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;
}
@@ -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;
}
+1 -1
View File
@@ -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;
}