lua-users home
lua-l archive

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


On 28/06/2011 23.57, Sean Conner wrote:
It was thus said that the Great Lorenzo Donati once stated:

BTW, I have also wondered why (full) userdata creation is only possible
from C API. Wouldn't it be possible to devise a safe way to define and
manage userdata from Lua? It could be useful for lots of things (maybe a
bit advanced, but no more than those of "goto"), without resorting to C,
therefore pushing farther the boundary where Lua could be used instead
of C (leaving C only for high performance tasks or very tight system
integration/communication).

   Could you give an example of what you mean?  From my perspective, a
userdata is typically a C structure, alignment of fields dependant upon the
architecture.  A structure like:

	struct foo
	{
	  char c;
	  int  i;
	  char *s;
	}

   could be anywhere from 5 bytes (say, from a C compiler for a 6809 or 8088)
to 24 bytes (a modern 64-bit system), with each field at a different offset
from the base address of the structure.

   -spc (In other words, I don't quite understand what you're asking for)

Well, actually It wasn't a clear cut request. More like a curiosity expressed loudly.


What I meant, more or less, was whether it could be useful (on Lua side) to have a chunk of memory (managed by the gc) where one could store arbitrary bytes with a simple interface (a byte array?), but with the option to attach a metatable to it, so you could treat it as an opaque object.

IIRC the need for a byte array has already been mentioned in another thread, not long ago. Adding a metatable on such a byte array could allow mimicking an arbitrarily modifiable structure.

Yes, there are tables for that, but I wondered why, then, userdata are available at all from C side and not from Lua side.

Sorry for expressing my thought so badly, but mine was a sort of half-baked "philosophical" rambling on Lua design about userdata :-)

So any enlightment is welcome (I only played shortly with C API and although I know C, I cannot really call myself a C programmer, thus it may well be that I missed completely the point of userdata and why their usefulness is limited to C side!)

Cheers,
-- Lorenzo