diff --git a/src/common/runtime.cpp b/src/common/runtime.cpp index 11738b1eb..b0a06dcba 100644 --- a/src/common/runtime.cpp +++ b/src/common/runtime.cpp @@ -343,6 +343,30 @@ int luax_convobj(lua_State *L, int idxs[], int n, const char *mod, const char *f return 0; } +int luax_pconvobj(lua_State *L, int idx, const char *mod, const char *fn) +{ + // Convert string to a file. + luax_getfunction(L, mod, fn); + lua_pushvalue(L, idx); // The initial argument. + int ret = lua_pcall(L, 1, 1, 0); // Call the function, one arg, one return value. + if (ret == 0) + lua_replace(L, idx); // Replace the initial argument with the new object. + return ret; +} + +int luax_pconvobj(lua_State *L, int idxs[], int n, const char *mod, const char *fn) +{ + luax_getfunction(L, mod, fn); + for (int i = 0; i < n; i++) + { + lua_pushvalue(L, idxs[i]); // The arguments. + } + int ret = lua_pcall(L, n, 1, 0); // Call the function, n args, one return value. + if (ret == 0) + lua_replace(L, idxs[0]); // Replace the initial argument with the new object. + return ret; +} + int luax_strtofile(lua_State *L, int idx) { return luax_convobj(L, idx, "filesystem", "newFile"); diff --git a/src/common/runtime.h b/src/common/runtime.h index 3d3c2a5e1..926631a92 100644 --- a/src/common/runtime.h +++ b/src/common/runtime.h @@ -267,6 +267,10 @@ int luax_convobj(lua_State *L, int idx, const char *module, const char *function **/ int luax_convobj(lua_State *L, int idxs[], int n, const char *module, const char *function); +// pcall versions of the above +int luax_pconvobj(lua_State *L, int idx, const char *module, const char *function); +int luax_pconvobj(lua_State *L, int idxs[], int n, const char *module, const char *function); + /** * 'Insist' that a table 'k' exists in the table at idx. Insistence involves that the * table (k) is created if it does not exist in the table at idx. The table at idx must diff --git a/src/modules/font/freetype/wrap_Font.cpp b/src/modules/font/freetype/wrap_Font.cpp index 441276e49..019930426 100644 --- a/src/modules/font/freetype/wrap_Font.cpp +++ b/src/modules/font/freetype/wrap_Font.cpp @@ -39,18 +39,25 @@ static Font *instance = 0; int w_newRasterizer(lua_State *L) { Rasterizer *t = NULL; - if (luax_istype(L, 1, IMAGE_IMAGE_DATA_T)) + try { - love::image::ImageData *d = luax_checktype(L, 1, "ImageData", IMAGE_IMAGE_DATA_T); - const char *g = luaL_checkstring(L, 2); - std::string glyphs(g); - t = instance->newRasterizer(d, glyphs); + if (luax_istype(L, 1, IMAGE_IMAGE_DATA_T)) + { + love::image::ImageData *d = luax_checktype(L, 1, "ImageData", IMAGE_IMAGE_DATA_T); + const char *g = luaL_checkstring(L, 2); + std::string glyphs(g); + t = instance->newRasterizer(d, glyphs); + } + else if (luax_istype(L, 1, DATA_T)) + { + Data *d = luax_checkdata(L, 1); + int size = luaL_checkint(L, 2); + t = instance->newRasterizer(d, size); + } } - else if (luax_istype(L, 1, DATA_T)) + catch (love::Exception &e) { - Data *d = luax_checkdata(L, 1); - int size = luaL_checkint(L, 2); - t = instance->newRasterizer(d, size); + return luaL_error(L, "%s", e.what()); } luax_newtype(L, "Rasterizer", FONT_RASTERIZER_T, t); diff --git a/src/modules/graphics/opengl/wrap_Graphics.cpp b/src/modules/graphics/opengl/wrap_Graphics.cpp index cd87f2e3e..5bd68e7f2 100644 --- a/src/modules/graphics/opengl/wrap_Graphics.cpp +++ b/src/modules/graphics/opengl/wrap_Graphics.cpp @@ -277,7 +277,12 @@ int w_newFont(lua_State *L) if (luax_istype(L, 1, DATA_T)) { int idxs[] = {1, 2}; - luax_convobj(L, idxs, 2, "font", "newRasterizer"); + int ret = luax_pconvobj(L, idxs, 2, "font", "newRasterizer"); + if (ret != 0) + { + font_data->release(); + return lua_error(L); + } } if (font_data)