lua-users home
lua-l archive

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


It was thus said that the Great Dirk Laurie once stated:
> 2015-08-21 8:43 GMT+02:00 Dirk Laurie <dirk.laurie@gmail.com>:
> 
> > Something like this?
> >
> > 1. An array _METATABLES in the registry.
> > 2. The 25 unused bits in the type tag are used to index into it.
> > 0 means no metatable.
> > 3. Any value can have a metatable.
> 
> I thought this should be a fairly straightforward patch. Ha-ha.
> 
> First, the table does not belong in the registry, it belongs in
> the lua_State. There is a current array of Table *mt for the
> basic types which will not be needed any more. In its place
> must come a list of all metatables. What is the best type
> for this? Simply a TValue like l_registry?
> 
> Second, the table should be accessible as a pseudo-index
> like the registry is.
> 
> Third, when you reuse a metatable, you don't want a new entry
> into this table. You could just search through the list whenever
> setting a metatable, or you could also keep a lookup table.
> luaL_getmetatable simply uses the registry for that. Not sure
> that is still OK when the keys are the metatables themselves.
> 
> Please disagree with any of the above, especially if coding can
> be reduced that way!

  I do have a Lua module [1] where each userdata created gets its own unique
metatable.  I did that so I could associate additonal values at run time to
the userdata, like:

	X = require "Xcore"

	display = X.open_display()
	window  = display:window( ... I won't bore you with details here )

	-- now add some additional values

	window.comment = "This is the main window"
	window.child   = X.sub_window(...)

  When a window (as a userdata) goes out of scope, the __gc method will
close the window and because each userdata gets its own metatable, the
metatable itself will be subject to garbage collection and thus any
resources (like windows, or fonts or anything else where the sole reference
is in that metatable) will be reclaimed as well.

  This new method breaks that.  I'm not sure what I'm doing is *good* [1]
but it's a method that currently works.

  -spc

[1]	It wraps Xlib.  Not suitable for general consumption, even assuming
	anyone knows how to program to Xlib anymore.  It uses some
	questionable techniques [2] that I'm not sure should get out of the
	laboratory.

[2]	No, really!  I'm be surprised if you could get the code to work on
	your machine.