Changed luax_pushtype to retain the pushed object itself, but only if the object isn't in the Lua state already.

Previously, calling e.g. love.graphics.getFont() 10,000 times would retain the object 10,000 times (and release it 10,000 times when the Font object was garbage-collected in the Lua state.) Now it will only retain it once and release it once.
This commit is contained in:
Alex Szpakowski
2014-08-09 16:04:59 -03:00
parent 191b910f23
commit da7cd267ea
30 changed files with 113 additions and 118 deletions
@@ -68,6 +68,7 @@ int w_Canvas_getImageData(lua_State *L)
love::image::Image *image = luax_getmodule<love::image::Image>(L, "image", MODULE_IMAGE_T);
love::image::ImageData *img = canvas->getImageData(image);
luax_pushtype(L, "ImageData", IMAGE_IMAGE_DATA_T, img);
img->release();
return 1;
}
+15 -12
View File
@@ -222,6 +222,7 @@ int w_newImage(lua_State *L)
// Push the type.
luax_pushtype(L, "Image", GRAPHICS_IMAGE_T, image);
image->release();
return 1;
}
@@ -238,6 +239,7 @@ int w_newQuad(lua_State *L)
Quad *quad = instance()->newQuad(v, sw, sh);
luax_pushtype(L, "Quad", GRAPHICS_QUAD_T, quad);
quad->release();
return 1;
}
@@ -262,6 +264,7 @@ int w_newFont(lua_State *L)
// Push the type.
luax_pushtype(L, "Font", GRAPHICS_FONT_T, font);
font->release();
return 1;
}
@@ -302,6 +305,7 @@ int w_newImageFont(lua_State *L)
// Push the type.
luax_pushtype(L, "Font", GRAPHICS_FONT_T, font);
font->release();
return 1;
}
@@ -324,6 +328,7 @@ int w_newSpriteBatch(lua_State *L)
);
luax_pushtype(L, "SpriteBatch", GRAPHICS_SPRITE_BATCH_T, t);
t->release();
return 1;
}
@@ -340,6 +345,7 @@ int w_newParticleSystem(lua_State *L)
);
luax_pushtype(L, "ParticleSystem", GRAPHICS_PARTICLE_SYSTEM_T, t);
t->release();
return 1;
}
@@ -364,6 +370,7 @@ int w_newCanvas(lua_State *L)
return luaL_error(L, "Canvas not created, but no error thrown. I don't even...");
luax_pushtype(L, "Canvas", GRAPHICS_CANVAS_T, canvas);
canvas->release();
return 1;
}
@@ -450,6 +457,7 @@ int w_newShader(lua_State *L)
{
Shader *shader = instance()->newShader(sources);
luax_pushtype(L, "Shader", GRAPHICS_SHADER_T, shader);
shader->release();
}
catch (love::Exception &e)
{
@@ -542,6 +550,7 @@ int w_newMesh(lua_State *L)
t->setTexture(tex);
luax_pushtype(L, "Mesh", GRAPHICS_MESH_T, t);
t->release();
return 1;
}
@@ -631,7 +640,6 @@ int w_getFont(lua_State *L)
if (f == 0)
return 0;
f->retain();
luax_pushtype(L, "Font", GRAPHICS_FONT_T, f);
return 1;
}
@@ -883,6 +891,7 @@ int w_newScreenshot(lua_State *L)
luax_catchexcept(L, [&](){ i = instance()->newScreenshot(image, copyAlpha); });
luax_pushtype(L, "ImageData", IMAGE_IMAGE_DATA_T, i);
i->release();
return 1;
}
@@ -931,16 +940,13 @@ int w_getCanvas(lua_State *L)
const std::vector<Canvas *> canvases = instance()->getCanvas();
int n = 0;
if (!canvases.empty())
for (Canvas *c : canvases)
{
for (Canvas *c : canvases)
{
c->retain();
luax_pushtype(L, "Canvas", GRAPHICS_CANVAS_T, c);
n++;
}
luax_pushtype(L, "Canvas", GRAPHICS_CANVAS_T, c);
n++;
}
else
if (n == 0)
{
lua_pushnil(L);
n = 1;
@@ -966,10 +972,7 @@ int w_getShader(lua_State *L)
{
Shader *shader = instance()->getShader();
if (shader)
{
shader->retain();
luax_pushtype(L, "Shader", GRAPHICS_SHADER_T, shader);
}
else
lua_pushnil(L);
@@ -93,10 +93,7 @@ int w_Image_getData(lua_State *L)
{
love::image::CompressedData *t = i->getCompressedData();
if (t)
{
t->retain();
luax_pushtype(L, "CompressedData", IMAGE_COMPRESSED_DATA_T, t);
}
else
lua_pushnil(L);
}
@@ -104,10 +101,7 @@ int w_Image_getData(lua_State *L)
{
love::image::ImageData *t = i->getImageData();
if (t)
{
t->retain();
luax_pushtype(L, "ImageData", IMAGE_IMAGE_DATA_T, t);
}
else
lua_pushnil(L);
}
@@ -263,18 +263,13 @@ int w_Mesh_getTexture(lua_State *L)
if (tex == nullptr)
return 0;
tex->retain();
// FIXME: big hack right here.
if (typeid(*tex) == typeid(Image))
luax_pushtype(L, "Image", GRAPHICS_IMAGE_T, tex);
else if (typeid(*tex) == typeid(Canvas))
luax_pushtype(L, "Canvas", GRAPHICS_CANVAS_T, tex);
else
{
tex->release();
return luaL_error(L, "Unable to determine texture type.");
}
return 1;
}
@@ -52,6 +52,7 @@ int w_ParticleSystem_clone(lua_State *L)
luax_catchexcept(L, [&](){ clone = t->clone(); });
luax_pushtype(L, "ParticleSystem", GRAPHICS_PARTICLE_SYSTEM_T, clone);
clone->release();
return 1;
}
@@ -67,7 +68,6 @@ int w_ParticleSystem_getTexture(lua_State *L)
{
ParticleSystem *t = luax_checkparticlesystem(L, 1);
Texture *tex = t->getTexture();
tex->retain();
// FIXME: big hack right here.
if (typeid(*tex) == typeid(Image))
@@ -75,10 +75,7 @@ int w_ParticleSystem_getTexture(lua_State *L)
else if (typeid(*tex) == typeid(Canvas))
luax_pushtype(L, "Canvas", GRAPHICS_CANVAS_T, tex);
else
{
tex->release();
return luaL_error(L, "Unable to determine texture type.");
}
return 1;
}
@@ -608,7 +605,6 @@ int w_ParticleSystem_getQuads(lua_State *L)
for (size_t i = 0; i < quads.size(); i++)
{
quads[i]->retain();
luax_pushtype(L, "Quad", GRAPHICS_QUAD_T, quads[i]);
lua_rawseti(L, -2, i + 1);
}
@@ -143,7 +143,6 @@ int w_SpriteBatch_getTexture(lua_State *L)
{
SpriteBatch *t = luax_checkspritebatch(L, 1);
Texture *tex = t->getTexture();
tex->retain();
// FIXME: big hack right here.
if (typeid(*tex) == typeid(Image))
@@ -151,10 +150,7 @@ int w_SpriteBatch_getTexture(lua_State *L)
else if (typeid(*tex) == typeid(Canvas))
luax_pushtype(L, "Canvas", GRAPHICS_CANVAS_T, tex);
else
{
tex->release();
return luaL_error(L, "Unable to determine texture type.");
}
return 1;
}