From d5ccdc040a1d29fe8c1968e9f60ad829c780125b Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Sat, 9 Aug 2014 16:18:22 -0300 Subject: [PATCH] Cleaned up some Proxy-related code. --- src/common/runtime.cpp | 16 ++++++---------- src/common/runtime.h | 18 +++++++----------- src/modules/graphics/opengl/wrap_Graphics.cpp | 4 ++-- 3 files changed, 15 insertions(+), 23 deletions(-) diff --git a/src/common/runtime.cpp b/src/common/runtime.cpp index dd9156909..3d634660e 100644 --- a/src/common/runtime.cpp +++ b/src/common/runtime.cpp @@ -52,8 +52,7 @@ static int w__gc(lua_State *L) thread::Lock lock(gcmutex); - if (p->own) - object->release(); + object->release(); return 0; } @@ -218,7 +217,6 @@ int luax_register_module(lua_State *L, const WrappedModule &m) luax_insistregistry(L, REGISTRY_MODULES); Proxy *p = (Proxy *)lua_newuserdata(L, sizeof(Proxy)); - p->own = true; p->data = m.module; p->flags = m.flags; @@ -384,22 +382,20 @@ int luax_register_searcher(lua_State *L, lua_CFunction f, int pos) return 0; } -void luax_rawnewtype(lua_State *L, const char *name, bits flags, love::Object *object, bool own) +void luax_rawnewtype(lua_State *L, const char *name, bits flags, love::Object *object) { Proxy *u = (Proxy *)lua_newuserdata(L, sizeof(Proxy)); - if (own) - object->retain(); + object->retain(); u->data = (void *) object; u->flags = flags; - u->own = own; luaL_newmetatable(L, name); lua_setmetatable(L, -2); } -void luax_pushtype(lua_State *L, const char *name, bits flags, love::Object *object, bool own) +void luax_pushtype(lua_State *L, const char *name, bits flags, love::Object *object) { if (object == nullptr) lua_pushnil(L); @@ -411,7 +407,7 @@ void luax_pushtype(lua_State *L, const char *name, bits flags, love::Object *obj if (!lua_istable(L, -1)) { lua_pop(L, 1); - return luax_rawnewtype(L, name, flags, object, own); + return luax_rawnewtype(L, name, flags, object); } // Get the value of lovetypes[data] on the stack. @@ -423,7 +419,7 @@ void luax_pushtype(lua_State *L, const char *name, bits flags, love::Object *obj { lua_pop(L, 1); - luax_rawnewtype(L, name, flags, object, own); + luax_rawnewtype(L, name, flags, object); lua_pushlightuserdata(L, (void *) object); lua_pushvalue(L, -2); diff --git a/src/common/runtime.h b/src/common/runtime.h index 40c7bb0f4..2bb907fa3 100644 --- a/src/common/runtime.h +++ b/src/common/runtime.h @@ -67,9 +67,6 @@ struct Proxy // The light userdata (pointer to the love::Object). void *data; - - // Whether release() should be called on GC. - bool own; }; /** @@ -228,6 +225,9 @@ void luax_setfuncs(lua_State *L, const luaL_Reg *l); /** * Register a module in the love table. The love table will be created if it does not exist. + * NOTE: The module-object is expected to have a +1 reference count before calling + * this function, as it doesn't retain the object itself but Lua will release it + * upon garbage collection. * @param L The Lua state. **/ int luax_register_module(lua_State *L, const WrappedModule &m); @@ -268,15 +268,13 @@ int luax_register_searcher(lua_State *L, lua_CFunction f, int pos = -1); /** * Pushes a Lua representation of the given object onto the stack, creating and * storing the Lua representation in a weak table if it doesn't exist yet. - * NOTE: If own is true, the object will be retained by Lua. + * NOTE: The object will be retained by Lua and released upon garbage collection. * @param L The Lua state. * @param name The name of the type. This must match the name used with luax_register_type. * @param flags The type information of the object. * @param object The pointer to the actual object. - * @param own Set this to true (default) if the object should be retained by - * Lua and released upon garbage collection. **/ -void luax_pushtype(lua_State *L, const char *name, bits flags, love::Object *object, bool own = true); +void luax_pushtype(lua_State *L, const char *name, bits flags, love::Object *object); /** * Creates a new Lua representation of the given object *without* checking if it @@ -284,15 +282,13 @@ void luax_pushtype(lua_State *L, const char *name, bits flags, love::Object *obj * This should only be used when performance is an extreme concern and the * object is not ever expected to be pushed to Lua again, as it prevents the * Lua-side objects from working in some cases when used as keys in tables. - * NOTE: if own is true, the object will be retained by Lua. + * NOTE: The object will be retained by Lua and released upon garbage collection. * @param L The Lua state. * @param name The name of the type. This must match the name used with luax_register_type. * @param flags The type information of the object. * @param object The pointer to the actual object. - * @param own Set this to true (default) if the object should be retained by - * Lua and released upon garbage collection. **/ -void luax_rawnewtype(lua_State *L, const char *name, bits flags, love::Object *object, bool own = true); +void luax_rawnewtype(lua_State *L, const char *name, bits flags, love::Object *object); /** * Checks whether the value at idx is a certain type. diff --git a/src/modules/graphics/opengl/wrap_Graphics.cpp b/src/modules/graphics/opengl/wrap_Graphics.cpp index 80c0dde59..677427ec5 100644 --- a/src/modules/graphics/opengl/wrap_Graphics.cpp +++ b/src/modules/graphics/opengl/wrap_Graphics.cpp @@ -282,8 +282,8 @@ int w_newImageFont(lua_State *L) filter = i->getFilter(); love::image::ImageData *id = i->getImageData(); if (!id) - return luaL_argerror(L, 1, "Image cannot be compressed."); - luax_pushtype(L, "ImageData", IMAGE_IMAGE_DATA_T, id, false); + return luaL_argerror(L, 1, "Image must not be compressed."); + luax_pushtype(L, "ImageData", IMAGE_IMAGE_DATA_T, id); lua_replace(L, 1); }