lua-users home
lua-l archive

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


2012/11/13 Rena <hyperhacker@gmail.com>:

> Recently I was thinking about how you might add a 64-bit integer type to Lua.

This is only a pressing question if you need IEEE floats and 64-bit integers
in the same program, otherwise you can build Lua with 64-bit integers
as your `number`.

But apart from that, the generalization you propose, of

> more "primitive" types, that function like light userdata. Each
> type having its own metatable and data size (up to 8 bytes?).

is interesting.  The gap between "each object has its own
metatable" and "each type has one metatable" is a wide one when
"type" is a pointer to a C-defined data type.

I have an application with several userdata types.  At present I use
the following technique to create them:

1. Make Lua allocate the storage as a newuserdata.
2. Initialize it, including the allocation of a metatable.
3. Push it back on the stack as a lightuserdata.
4. Store it in the registry index using the lightuserdata as a key.
5. Return it to Lua.

All the objects contain pointers to other objects in their structure.
To access these, I call a member function that does this:

1. Push the pointer from the data structure on the stack as a lightuserdata.
2. Retrieve the userdata from the registry.
3. Return it to Lua.