lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Good evening,

My partner and I are trying to achieve a system by which we have one global table of objects which contain strong references. Any objects accessed through that global table will be weak references to the objects returned, essentially proxies of the objects. 

We attempted the following code. Essentially, there is a table (hiddenEntities) which will not be referenced which contains the strong references. There is a table (entities) that will be used to access the weak table proxies of the objects in hiddenEntities.

When we create a proxy, we set it's metatable __index value to point to an entry in hiddenEntities. BUT, we also make that __mode on the metatable of the metatable "v" so that the __index is just a weak reference to the entity in hiddenEntities. 

The problem is that __index in the metatable is keeping a strong reference to entity despite it being declared as a weak table. So, can metatables be weak tables? If not, has anyone accomplished something similar to this?

Thanks in advance,

Christian Pena


hiddenEntities = {}

entities = {} = {
	__index = function(table, key)
		proxy = {}
		mt = {}
		setmetatable(mt, { __mode = "v" })
		mt.__index = hiddenEntities[key]
		setmetatable(proxy, mt)
		mt = nil
		return proxy
setmetatable(entities, = nil