[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Checking for userdata subtypes
- From: Dirk Laurie <dirk.laurie@...>
- Date: Sat, 10 Nov 2012 23:31:25 +0200
2012/11/10 Kevin Martin <kev82@khn.org.uk>:
> I don't quite follow the situation you describe, but I think you're saying
> that each object created by the library has a different metatable, even
> though it is the same type?
No, only that the metatable is not known at compile time —
neither by name nor by value.
The "usual" (i.e. as in PiL Ch26) way is to create a new metatable
`mymeta` in the C program. I.e. the C program already knows the
metatable by name and by value at compile time, and it is easy to
check it.
This foreign module looks for `mymeta` in the calling environment.
I.e. the C program only knows the metatable at runtime, when the
userdata object is created. It's still only one metatable per
userdata type, but the C program does not know the metatable.
It is possible to create a sample object and read off its metatable.
One can memoize it. Essentially the C API equivalent of
file_meta = file_meta or getmetatable(io.open("/dev/null"))
function isfile(val) return getmetatable(val)==file_meta end
Since up to this moment nobody has come up with a standard way
of doing it, maybe that's what I'll do.
Thanks for responding.
Dirk