lua-users home
lua-l archive

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


I'm sure someone has done this before, but so far I haven't found any
clear examples from the mailing archives or online. Maybe my search
terms are off.

Basically, I want to patch the Lua interpreter to support an
additional type (alongside 'number', 'table', etc).

>From reading the code, it seems that I need to add an entry to the
"Value" union in lobject.h, and follow the various implications of
that.

Some brief details:

The type I'm adding is a "vec2" (x,y pair), which conveniently fits
into the same space as a 64-bit integer in my case, so it does not
need to be a "reference type". No heap allocation needed. Also, I'm
not planning on changing the parser, if I can avoid it - not adding
any "vec2" keyword or anything, just a global "vec2(x,y)" constructor
function. However, I *do* want to be able to write "foo.x = 3", so
there has to be some sort of awareness of ".x" syntax - maybe a
metatable, similar to how the 'string' type has methods attached to
it?

Does anyone have any example of doing something like this? It would
save me some time tracking down all the details (and probably getting
some wrong). Maybe a guide/tutorial, or a patch that someone has made
previously?

I'm using Lua 5.3, but would still be interested in other versions, as
a point of reference.

Thanks
-John