|
@@ -36,15 +36,55 @@ namespace mouse
|
|
int w_newCursor(lua_State *L)
|
|
int w_newCursor(lua_State *L)
|
|
{
|
|
{
|
|
Cursor *cursor = nullptr;
|
|
Cursor *cursor = nullptr;
|
|
|
|
+ std::vector<love::image::ImageData *> data;
|
|
|
|
|
|
- if (lua_isstring(L, 1) || luax_istype(L, 1, love::filesystem::File::type) || luax_istype(L, 1, love::filesystem::FileData::type))
|
|
|
|
- luax_convobj(L, 1, "image", "newImageData");
|
|
|
|
|
|
+ if (lua_istable(L, 1))
|
|
|
|
+ {
|
|
|
|
+ // Do some type checking first, because memory will leak if we hit an error in the loop after this.
|
|
|
|
+ for (size_t i = 1; i <= luax_objlen(L, 1); i++)
|
|
|
|
+ {
|
|
|
|
+ lua_rawgeti(L, 1, i);
|
|
|
|
+
|
|
|
|
+ if (!luax_istype(L, -1, love::image::ImageData::type)
|
|
|
|
+ && !(lua_isstring(L, -1) || luax_istype(L, -1, love::filesystem::File::type) || luax_istype(L, -1, love::filesystem::FileData::type)))
|
|
|
|
+ {
|
|
|
|
+ luax_checktype<love::image::ImageData>(L, -1);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ lua_pop(L, 1);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for (size_t i = 1; i <= luax_objlen(L, 1); i++)
|
|
|
|
+ {
|
|
|
|
+ lua_rawgeti(L, 1, i);
|
|
|
|
+
|
|
|
|
+ if (lua_isstring(L, -1) || luax_istype(L, -1, love::filesystem::File::type) || luax_istype(L, -1, love::filesystem::FileData::type))
|
|
|
|
+ luax_convobj(L, -1, "image", "newImageData");
|
|
|
|
+
|
|
|
|
+ data.push_back(luax_checktype<love::image::ImageData>(L, -1));
|
|
|
|
+
|
|
|
|
+ // If a GC step happens within the loop, previous ImageData objects created within the loop may be released.
|
|
|
|
+ data.back()->retain();
|
|
|
|
+
|
|
|
|
+ lua_pop(L, 1);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ if (lua_isstring(L, 1) || luax_istype(L, 1, love::filesystem::File::type) || luax_istype(L, 1, love::filesystem::FileData::type))
|
|
|
|
+ luax_convobj(L, 1, "image", "newImageData");
|
|
|
|
+
|
|
|
|
+ data.push_back(luax_checktype<love::image::ImageData>(L, 1));
|
|
|
|
+ data.back()->retain();
|
|
|
|
+ }
|
|
|
|
|
|
- love::image::ImageData *data = luax_checktype<love::image::ImageData>(L, 1);
|
|
|
|
int hotx = (int) luaL_optinteger(L, 2, 0);
|
|
int hotx = (int) luaL_optinteger(L, 2, 0);
|
|
int hoty = (int) luaL_optinteger(L, 3, 0);
|
|
int hoty = (int) luaL_optinteger(L, 3, 0);
|
|
|
|
|
|
- luax_catchexcept(L, [&](){ cursor = instance()->newCursor(data, hotx, hoty); });
|
|
|
|
|
|
+ luax_catchexcept(L,
|
|
|
|
+ [&](){ cursor = instance()->newCursor(data, hotx, hoty); },
|
|
|
|
+ [&](bool /*shoulderror*/) { for (auto d : data) d->release(); }
|
|
|
|
+ );
|
|
|
|
|
|
luax_pushtype(L, cursor);
|
|
luax_pushtype(L, cursor);
|
|
cursor->release();
|
|
cursor->release();
|