[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Luajit and curstom data structures
- From: Mike Pall <mikelu-1104@...>
- Date: Wed, 13 Apr 2011 12:19:13 +0200
Daurnimator wrote:
> It seems you can't apply a metatype to a pointer:
Yes. It's even documented. :-)
> Will metamethods on a struct activate when I do it on a pointer to
> them? (Otherwise, I would like to request some sort of automatic
> de-referencing)
Yes, unless the pre-defined pointer operations override it:
local ffi = require("ffi")
ffi.cdef[[typedef struct { int x; } foo_t]]
local foo = ffi.metatype("foo_t", {
__index = function() return 1234 end,
__sub = function() return 9876 end,
})
local s = foo()
print(s.bar) --> 1234
print(s[0]) --> 1234
print(s - s) --> 9876
local p = ffi.new("foo_t *")
print(p.bar) --> 1234
print(p[0]) --> cdata<struct 95 &>: ... (pointer dereference)
print(p - p) --> 0 (pointer difference)
-- But:
print(p[0][0]) --> 1234
--Mike