mirror of
https://github.com/love2d/love.git
synced 2026-08-16 16:20:42 +02:00
Replaced luax_newtype with luax_pushtype (and added luax_rawnewtype for the old functionality of luax_newtype.)
luax_pushtype keeps the object in a weak table and checks the table before creating a new userdata, so it re-uses the object's existing wrapper userdata if it can, whenever the object is pushed to Lua. This fixes some subtle issues with love objects, where using them as keys in tables would not always work as expected (https://love2d.org/forums/viewtopic.php?f=4&t=39398&p=112388#p112415). It also decreases the amount of new Lua objects created, saving the garbage collector some work.
This commit is contained in:
@@ -227,7 +227,7 @@ void Variant::toLua(lua_State *L)
|
||||
const char *name = NULL;
|
||||
love::types.find(udatatype, name);
|
||||
((love::Object *) data.userdata)->retain();
|
||||
luax_newtype(L, name, flags, data.userdata);
|
||||
luax_pushtype(L, name, flags, (love::Object *) data.userdata);
|
||||
}
|
||||
else
|
||||
lua_pushlightuserdata(L, data.userdata);
|
||||
|
||||
+132
-10
@@ -18,6 +18,7 @@
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
**/
|
||||
|
||||
#include "config.h"
|
||||
#include "runtime.h"
|
||||
|
||||
// LOVE
|
||||
@@ -36,6 +37,7 @@ namespace love
|
||||
|
||||
static thread::Mutex *gcmutex = 0;
|
||||
void *_gcmutex = 0;
|
||||
|
||||
/**
|
||||
* Called when an object is collected. The object is released
|
||||
* once in this function, possibly deleting it.
|
||||
@@ -47,13 +49,21 @@ static int w__gc(lua_State *L)
|
||||
gcmutex = thread::newMutex();
|
||||
_gcmutex = (void *) gcmutex;
|
||||
}
|
||||
|
||||
Proxy *p = (Proxy *)lua_touserdata(L, 1);
|
||||
Object *t = (Object *)p->data;
|
||||
if (p->own)
|
||||
{
|
||||
thread::Lock lock(gcmutex);
|
||||
|
||||
thread::Lock lock(gcmutex);
|
||||
|
||||
int numretains = p->retains;
|
||||
if (numretains >= 0)
|
||||
numretains = std::min(numretains, t->getReferenceCount());
|
||||
|
||||
for (int i = numretains; i > 0; i--)
|
||||
t->release();
|
||||
}
|
||||
|
||||
// Signal that this Proxy is dead.
|
||||
p->retains = -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -214,10 +224,10 @@ void luax_setfuncs(lua_State *L, const luaL_Reg *l)
|
||||
int luax_register_module(lua_State *L, const WrappedModule &m)
|
||||
{
|
||||
// Put a reference to the C++ module in Lua.
|
||||
luax_getregistry(L, REGISTRY_MODULES);
|
||||
luax_insistregistry(L, REGISTRY_MODULES);
|
||||
|
||||
Proxy *p = (Proxy *)lua_newuserdata(L, sizeof(Proxy));
|
||||
p->own = true;
|
||||
p->retains = 1;
|
||||
p->data = m.module;
|
||||
p->flags = m.flags;
|
||||
|
||||
@@ -273,6 +283,31 @@ int luax_register_type(lua_State *L, const char *tname, const luaL_Reg *f)
|
||||
if (!love::getType(tname, ltype))
|
||||
printf("Missing type entry for type name: %s\n", tname);
|
||||
|
||||
// Get the place for storing and re-using instantiated love types.
|
||||
luax_getregistry(L, REGISTRY_TYPES);
|
||||
|
||||
// Create registry._lovetypes if it doesn't exist yet.
|
||||
if (!lua_istable(L, -1))
|
||||
{
|
||||
lua_newtable(L);
|
||||
lua_replace(L, -2);
|
||||
|
||||
// Create a metatable.
|
||||
lua_newtable(L);
|
||||
|
||||
// metatable.__mode = "v". Weak userdata values.
|
||||
lua_pushliteral(L, "v");
|
||||
lua_setfield(L, -2, "__mode");
|
||||
|
||||
// setmetatable(newtable, metatable)
|
||||
lua_setmetatable(L, -2);
|
||||
|
||||
// registry._lovetypes = newtable
|
||||
lua_setfield(L, LUA_REGISTRYINDEX, "_lovetypes");
|
||||
}
|
||||
else
|
||||
lua_pop(L, 1);
|
||||
|
||||
luaL_newmetatable(L, tname);
|
||||
|
||||
// m.__index = m
|
||||
@@ -358,18 +393,74 @@ int luax_register_searcher(lua_State *L, lua_CFunction f, int pos)
|
||||
return 0;
|
||||
}
|
||||
|
||||
void luax_newtype(lua_State *L, const char *name, bits flags, void *data, bool own)
|
||||
void luax_rawnewtype(lua_State *L, const char *name, bits flags, love::Object *data, bool own)
|
||||
{
|
||||
Proxy *u = (Proxy *)lua_newuserdata(L, sizeof(Proxy));
|
||||
|
||||
u->data = data;
|
||||
u->data = (void *) data;
|
||||
u->flags = flags;
|
||||
u->own = own;
|
||||
u->retains = own ? 1 : 0;
|
||||
|
||||
luaL_newmetatable(L, name);
|
||||
lua_setmetatable(L, -2);
|
||||
}
|
||||
|
||||
void luax_pushtype(lua_State *L, const char *name, bits flags, love::Object *data, bool own)
|
||||
{
|
||||
// Fetch the registry table of instantiated types.
|
||||
luax_getregistry(L, REGISTRY_TYPES);
|
||||
|
||||
// The table might not exist - it should be insisted in luax_register_type.
|
||||
if (!lua_istable(L, -1))
|
||||
{
|
||||
lua_pop(L, 1);
|
||||
return luax_rawnewtype(L, name, flags, data, own);
|
||||
}
|
||||
|
||||
// Get the value of lovetypes[data] on the stack.
|
||||
lua_pushlightuserdata(L, (void *) data);
|
||||
lua_gettable(L, -2);
|
||||
|
||||
// If the Proxy userdata isn't in the instantiated types table yet, add it.
|
||||
if (lua_type(L, -1) != LUA_TUSERDATA)
|
||||
{
|
||||
lua_pop(L, 1);
|
||||
|
||||
luax_rawnewtype(L, name, flags, data, own);
|
||||
|
||||
lua_pushlightuserdata(L, (void *) data);
|
||||
lua_pushvalue(L, -2);
|
||||
|
||||
// lovetypes[data] = Proxy.
|
||||
lua_settable(L, -4);
|
||||
|
||||
// Remove the lovetypes table from the stack.
|
||||
lua_remove(L, -2);
|
||||
|
||||
// The Proxy userdata remains at the top of the stack.
|
||||
return;
|
||||
}
|
||||
|
||||
// Remove the lovetypes table from the stack.
|
||||
lua_remove(L, -2);
|
||||
|
||||
// If the object should be released on GC and we already have a stored
|
||||
// Proxy, we should tell the Proxy that the object was retained again.
|
||||
if (own)
|
||||
{
|
||||
Proxy *p = (Proxy *) lua_touserdata(L, -1);
|
||||
|
||||
thread::EmptyLock lock;
|
||||
if (gcmutex)
|
||||
lock.setLock(gcmutex);
|
||||
|
||||
if (p->retains >= 0)
|
||||
++(p->retains);
|
||||
}
|
||||
|
||||
// Keep the Proxy userdata on the stack.
|
||||
}
|
||||
|
||||
bool luax_istype(lua_State *L, int idx, love::bits type)
|
||||
{
|
||||
if (lua_isuserdata(L, idx) == 0)
|
||||
@@ -499,7 +590,20 @@ int luax_insistlove(lua_State *L, const char *k)
|
||||
return 1;
|
||||
}
|
||||
|
||||
int luax_getregistry(lua_State *L, Registry r)
|
||||
int luax_getlove(lua_State *L, const char *k)
|
||||
{
|
||||
lua_getglobal(L, "love");
|
||||
|
||||
if (!lua_isnil(L, -1))
|
||||
{
|
||||
lua_getfield(L, -1, k);
|
||||
lua_replace(L, -2);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int luax_insistregistry(lua_State *L, Registry r)
|
||||
{
|
||||
switch (r)
|
||||
{
|
||||
@@ -507,6 +611,24 @@ int luax_getregistry(lua_State *L, Registry r)
|
||||
return luax_insistlove(L, "_gc");
|
||||
case REGISTRY_MODULES:
|
||||
return luax_insistlove(L, "_modules");
|
||||
case REGISTRY_TYPES:
|
||||
return luax_insist(L, LUA_REGISTRYINDEX, "_lovetypes");
|
||||
default:
|
||||
return luaL_error(L, "Attempted to use invalid registry.");
|
||||
}
|
||||
}
|
||||
|
||||
int luax_getregistry(lua_State *L, Registry r)
|
||||
{
|
||||
switch (r)
|
||||
{
|
||||
case REGISTRY_GC:
|
||||
return luax_getlove(L, "_gc");
|
||||
case REGISTRY_MODULES:
|
||||
return luax_getlove(L, "_modules");
|
||||
case REGISTRY_TYPES:
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, "_lovetypes");
|
||||
return 1;
|
||||
default:
|
||||
return luaL_error(L, "Attempted to use invalid registry.");
|
||||
}
|
||||
|
||||
+47
-16
@@ -23,6 +23,7 @@
|
||||
|
||||
// LOVE
|
||||
#include "types.h"
|
||||
#include "Object.h"
|
||||
|
||||
// Lua
|
||||
extern "C" {
|
||||
@@ -41,16 +42,16 @@ class Reference;
|
||||
|
||||
// Exposed mutex of the GC
|
||||
extern void *_gcmutex;
|
||||
extern unsigned int _gcthread;
|
||||
|
||||
/**
|
||||
* Registries represent special tables which can be accessed with
|
||||
* luax_getregistry.
|
||||
* luax_insistregistry and luax_getregistry.
|
||||
**/
|
||||
enum Registry
|
||||
{
|
||||
REGISTRY_GC = 1,
|
||||
REGISTRY_MODULES
|
||||
REGISTRY_MODULES,
|
||||
REGISTRY_TYPES
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -64,11 +65,11 @@ struct Proxy
|
||||
// Holds type information (see types.h).
|
||||
bits flags;
|
||||
|
||||
// The light userdata.
|
||||
// The light userdata (pointer to the love::Object).
|
||||
void *data;
|
||||
|
||||
// True if Lua should delete on GC.
|
||||
bool own;
|
||||
// The number of times release() should be called on GC.
|
||||
int retains;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -99,7 +100,7 @@ struct WrappedModule
|
||||
*
|
||||
* In any case, the top stack element is popped, regardless of its type.
|
||||
**/
|
||||
Reference *luax_refif (lua_State *L, int type);
|
||||
Reference *luax_refif(lua_State *L, int type);
|
||||
|
||||
/**
|
||||
* Prints the current contents of the stack. Only useful for debugging.
|
||||
@@ -265,14 +266,29 @@ int luax_table_insert(lua_State *L, int tindex, int vindex, int pos = -1);
|
||||
int luax_register_searcher(lua_State *L, lua_CFunction f, int pos = -1);
|
||||
|
||||
/**
|
||||
* Creates a new Lua-accessible object of the given type, and put it on the stack.
|
||||
* 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.
|
||||
* @param L The Lua state.
|
||||
* @param name The name of the type. This must match the used earlier with luax_register_type.
|
||||
* @param flags The type information.
|
||||
* @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 data The pointer to the actual object.
|
||||
* @own Set this to true (default) if the object should be released upon garbage collection.
|
||||
* @param own Set this to true (default) if the object should be released upon garbage collection.
|
||||
**/
|
||||
void luax_newtype(lua_State *L, const char *name, bits flags, void *data, bool own = true);
|
||||
void luax_pushtype(lua_State *L, const char *name, bits flags, love::Object *data, bool own = true);
|
||||
|
||||
/**
|
||||
* Creates a new Lua representation of the given object *without* checking if it
|
||||
* exists yet, and *without* storing it in a weak table.
|
||||
* 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 all cases when used as keys in tables.
|
||||
* @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 data The pointer to the actual object.
|
||||
* @param own Set this to true (default) if the object should be released upon garbage collection.
|
||||
**/
|
||||
void luax_rawnewtype(lua_State *L, const char *name, bits flags, love::Object *data, bool own = true);
|
||||
|
||||
/**
|
||||
* Checks whether the value at idx is a certain type.
|
||||
@@ -348,8 +364,23 @@ int luax_insistglobal(lua_State *L, const char *k);
|
||||
int luax_insistlove(lua_State *L, const char *k);
|
||||
|
||||
/**
|
||||
* Gets (creates if needed) the specified Registry, and puts it on top
|
||||
* of the stack.
|
||||
* Pushes the table 'k' in the love table onto the stack. Pushes nil if the
|
||||
* table doesn't exist.
|
||||
* @param k The name of the table we want to get.
|
||||
**/
|
||||
int luax_getlove(lua_State *L, const char *k);
|
||||
|
||||
/**
|
||||
* Gets (creates if needed) the specified Registry, and pushes it into the
|
||||
* stack.
|
||||
* @param L The Lua state.
|
||||
* @param r The Registry to get.
|
||||
**/
|
||||
int luax_insistregistry(lua_State *L, Registry r);
|
||||
|
||||
/**
|
||||
* Gets the specified Registry, and pushes it onto the stack. Pushes nil if the
|
||||
* registry hasn't been created (see luax_insistregistry.)
|
||||
* @param L The Lua state.
|
||||
* @param r The Registry to get.
|
||||
**/
|
||||
@@ -384,7 +415,7 @@ T *luax_checktype(lua_State *L, int idx, const char *name, love::bits type)
|
||||
template <typename T>
|
||||
T *luax_getmodule(lua_State *L, const char *k, love::bits type)
|
||||
{
|
||||
luax_getregistry(L, REGISTRY_MODULES);
|
||||
luax_insistregistry(L, REGISTRY_MODULES);
|
||||
lua_getfield(L, -1, k);
|
||||
|
||||
if (!lua_isuserdata(L, -1))
|
||||
@@ -403,7 +434,7 @@ T *luax_getmodule(lua_State *L, const char *k, love::bits type)
|
||||
template <typename T>
|
||||
T *luax_optmodule(lua_State *L, const char *k, love::bits type)
|
||||
{
|
||||
luax_getregistry(L, REGISTRY_MODULES);
|
||||
luax_insistregistry(L, REGISTRY_MODULES);
|
||||
lua_getfield(L, -1, k);
|
||||
|
||||
if (!lua_isuserdata(L, -1))
|
||||
|
||||
Reference in New Issue
Block a user