Version: beta 2 (2013-07-06)
This is a beta release of an experimental and unofficial library. Feedback appreciated (to [email protected], or the LuaJIT mailing list).
reflect.getmetatable(ffi.metatype("struct {}", t)) == t
Possible attributes: size, alignment, const, volatile.
The primitive empty type, optionally with a const and/or volatile qualifier. The actual type is therefore determined by the const and volatile fields.reflect.typeof("void").what == "void" reflect.typeof("const void").what == "void"
Possible attributes: size, alignment, const, volatile, bool, unsigned, long.
A primitive integral type, such as bool or [const] [volatile] [u]int(8|16|32|64)_t. The in-memory type is determined by the size and unsigned fields, and the final quantified type determined also by the bool, const, and volatile fields.reflect.typeof("long").what == "int" reflect.typeof("volatile unsigned __int64").what == "int"
Possible attributes: size, alignment, const, volatile.
A primitive floating point type, either [const] [volatile] float or [const] [volatile] double.reflect.typeof("double").what == "float" reflect.typeof("const float").what == "float"
Possible attributes: name, size, alignment, type.
An enumerated type.ffi.cdef "enum E{X,Y};"
reflect.typeof("enum E").what == "enum"
Possible attributes: name, type, value.
A particular value within an enumerated type.ffi.cdef "enum Bool{False,True};"
reflect.typeof("enum Bool"):value("False").what == "constant"
Possible attributes: size, alignment, const, volatile, element_type.
A pointer type (note that this includes function pointers). The type being pointed to is given by the element_type attribute.reflect.typeof("char*").what == "ptr" reflect.typeof("int(*)(void)").what == "ptr"
Possible attributes: size, alignment, const, volatile, element_type.
A reference type. The type being referenced is given by the element_type attribute.reflect.typeof("char&").what == "ref"
Possible attributes: size, alignment, const, volatile, element_type, vla, vector, complex.
An array type. The type of each element is given by the element_type attribute. The number of elements is not directly available; instead the size attribute needs to be divided by element_type.size.reflect.typeof("char[16]").what == "array" reflect.typeof("int[?]").what == "array"
Possible attributes: name, size, alignment, const, volatile, vla, transparent.
A structure aggregate type. The members of the structure can be enumerated through the members method, or indexed through the member method.reflect.typeof("struct{int x; int y;}").what == "struct"
Possible attributes: name, size, alignment, const, volatile, transparent.
A union aggregate type. The members of the union can be enumerated through the members method, or indexed through the member method.reflect.typeof("union{int x; int y;}").what == "union"
Possible attributes: name, sym_name, return_type, nargs, vararg, sse_reg_params, convention.
A function aggregate type. Note that function pointers will be of the "ptr" kind, with a "func" kind as the element_type. The return type is available as the return_type attribute, while argument types can be enumerated through the arguments method, or indexed through the argument method. The number of arguments is determined from the nargs and vararg attributes.ffi.cdef "int strcmp(const char*, const char*);"
reflect.typeof(ffi.C.strcmp).what == "func"
reflect.typeof("int(*)(void)").element_type.what == "func"
Possible attributes: name, offset, type.
An instance of a type within a structure or union, or an occurance of a type as an argument to a function.reflect.typeof("struct{int x;}"):member("x").what == "field"
ffi.cdef "int strcmp(const char*, const char*);"
reflect.typeof(ffi.C.strcmp):argument(2).what == "field"
Possible attributes: name, size, offset, type.
An instance of a type within a structure or union, which has an offset and/or size which is not a whole number of bytes.reflect.typeof("struct{int x:2;}"):member("x").what == "bitfield"
Applies to: struct, union, enum, func, field, bitfield, constant.
The type's given name, or nil if the type has no name.reflect.typeof("struct{int x; int y;}"):member(2).name == "y" reflect.typeof("struct{int x; int y;}").name == nil
ffi.cdef 'int sc(const char*, const char*) __asm__("strcmp");'
reflect.typeof(ffi.C.sc).name == "sc"
Applies to: func.
The function's symbolic name, if different to its given name.ffi.cdef 'int sc(const char*, const char*) __asm__("strcmp");'
reflect.typeof(ffi.C.sc).sym_name == "strcmp"
ffi.cdef "int strcmp(const char*, const char*);"
reflect.typeof(ffi.C.strcmp).sym_name == nil
Applies to: int, float, struct, union, ptr, ref, array, void, enum, bitfield.
The size of the type, in bytes. For most things this will be a strictly positive integer, although that is not always the case:reflect.typeof("__int32").size == 4 reflect.typeof("__int32[2]").size == 8 reflect.typeof("__int32[]").size == "none" reflect.typeof("__int32[?]").size == "none" reflect.typeof("struct{__int32 count; __int32 data[?];}").size == 4 reflect.typeof("struct{}").size == 0 reflect.typeof("void").size == "none" reflect.typeof("struct{int f:5;}"):member("f").size == 5 / 8
reflect.typeof("struct{__int32 x; __int32 y; __int32 z;}"):member("z").offset == 8 reflect.typeof("struct{int x : 3; int y : 4; int z : 5;}"):member("z").offset == 7 / 8 reflect.typeof("int(*)(int x, int y)").element_type:argument("y").offset == 1
Applies to: int, float, struct, union, ptr, ref, array, void, enum.
The minimum alignment required by the type, in bytes. Unless explicitly overridden by an alignment qualifier, this will be the value calculated by LuaJIT's C parser. In any case, this will be a power of two.reflect.typeof("struct{__int32 a; __int32 b;}").alignment == 4 reflect.typeof("__declspec(align(16)) int").alignment == 16
Applies to: int, float, struct, union, ptr, ref, array, void.
If true, this type was declared with the const qualifier. Be aware that for pointer types, this refers to the const-ness of the pointer itself, and not the const-ness of the thing being pointed to.reflect.typeof("int").const == nil reflect.typeof("const int").const == true reflect.typeof("const char*").const == nil reflect.typeof("const char*").element_type.const == true reflect.typeof("char* const").const == true
Applies to: int, float, struct, union, ptr, ref, array, void.
If true, this type was declared with the volatile qualifier. Note that this has no meaning to the JIT compiler. Be aware that for pointer types, this refers to the volatility of the pointer itself, and not the volatility of the thing being pointed to.reflect.typeof("int").volatile == nil reflect.typeof("volatile int").volatile == true
reflect.typeof("char*").element_type.size == 1 reflect.typeof("char&").element_type.size == 1 reflect.typeof("char[32]").element_type.size == 1
Applies to: enum, field, bitfield, constant.
For (bit)fields, the type of the field.reflect.typeof("struct{float x; unsigned y;}"):member("y").type.unsigned == true reflect.typeof("int(*)(uint64_t)").element_type:argument(1).type.size == 8
Applies to: func.
The return type of the function.ffi.cdef "int strcmp(const char*, const char*);"
reflect.typeof(ffi.C.strcmp).return_type.what == "int"
reflect.typeof("void*(*)(void)").element_type.return_type.what == "ptr"
Applies to: int.
If true, reading from this type will give a Lua boolean rather than a Lua number.reflect.typeof("bool").bool == true reflect.typeof("int").bool == nil reflect.typeof("_Bool int").bool == true
Applies to: int.
If true, this type denotes an unsigned integer. Otherwise, it denotes a signed integer.reflect.typeof("int32_t").unsigned == nil reflect.typeof("uint32_t").unsigned == true
Applies to: int.
If true, this type was declared with the long qualifier. If calculating the size of the type, then use the size field rather than this field.reflect.typeof("long int").long == true reflect.typeof("short int").long == nil
reflect.typeof("int[?]").vla == true reflect.typeof("int[2]").vla == nil reflect.typeof("int[]").vla == nil reflect.typeof("struct{int num; int data[?];}").vla == true reflect.typeof("struct{int num; int data[];}").vla == nil
for refct in reflect.typeof [[
struct {
int a;
union { int b; int c; };
struct { int d; int e; };
int f;
}
]]:members() do print(refct.transparent) end --> nil, true, true, nil
Applies to: array.
Applies to: array.
Applies to: func.
The number of fixed arguments accepted by the function. If the vararg field is true, then additional arguments are accepted.ffi.cdef "int strcmp(const char*, const char*);"
reflect.typeof(ffi.C.strcmp).nargs == 2
ffi.cdef "int printf(const char*, ...);"
reflect.typeof(ffi.C.printf).nargs == 1
Applies to: func.
If true, the function accepts a variable number of arguments (i.e. the argument list declaration was terminated with ...).ffi.cdef "int strcmp(const char*, const char*);"
reflect.typeof(ffi.C.strcmp).vararg == nil
ffi.cdef "int printf(const char*, ...);"
reflect.typeof(ffi.C.printf).vararg == true
Applies to: func.
Applies to: func.
The calling convention that the function was declared with, which will be one of: "cdecl" (the default), "thiscall", "fastcall", "stdcall". Note that on Windows, LuaJIT will automatically change __cdecl to __stdcall after the first call to the function (if appropriate).reflect.typeof("int(__stdcall *)(int)").element_type.convention == "stdcall"
if not ffi.abi "win" then return "Windows-only example" end
ffi.cdef "void* LoadLibraryA(const char*)"
print(reflect.typeof(ffi.C.LoadLibraryA).convention) --> cdecl
ffi.C.LoadLibraryA("kernel32")
print(reflect.typeof(ffi.C.LoadLibraryA).convention) --> stdcall
Applies to: constant.
for refct in reflect.typeof("struct{int x; int y;}"):members() do print(refct.name) end --> x, y
for refct in reflect.typeof[[
struct {
int a;
union {
int b;
int c;
};
int d : 2;
struct {
int e;
int f;
};
}
]]:members() do print(refct.what) end --> field, union, bitfield, struct
Applies to: func.
Returns an iterator triple which can be used in a for-in statement to enumerate the arguments of the function, from left to right. Each such argument will be a refct of kind "field", having a type attribute, zero-based offset attribute, and optionally a name attribute.ffi.cdef "int strcmp(const char*, const char*);"
for refct in reflect.typeof(ffi.C.strcmp):arguments() do print(refct.type.what) end --> ptr, ptr
for refct in reflect.typeof"int(*)(int x, int y)".element_type:arguments() do print(refct.name) end --> x, y
Applies to: func.
Like arguments(), but returns the first argument whose name matches the given parameter, or the argument given by the 1-based index, or nil if nothing matches. Note that this method takes time linear in the number of arguments.Applies to: enum.
Returns an iterator triple which can be used in a for-in statement to enumerate the values which make up an enumerated type. Each such value will be a refct of kind "constant", having name and value attributes.ffi.cdef "enum EV{EV_A = 1, EV_B = 10, EV_C = 100};"
for refct in reflect.typeof("enum EV"):values() do print(refct.name) end --> EV_A, EV_B, EV_C
Applies to: enum.
Like values(), but returns the value whose name matches the given parameter, or the value given by the 1-based index, or nil if nothing matches. Note that this method takes time linear in the number of values.void | int | float | enum | constant | ptr | ref | array | struct | union | func | field | bitfield | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
name | x | x | x | x | x | x | x | ||||||
sym_name | x | ||||||||||||
size | x | x | x | x | x | x | x | x | x | x | |||
offset | x | x | |||||||||||
alignment | x | x | x | x | x | x | x | x | x | ||||
const | x | x | x | x | x | x | x | x | |||||
volatile | x | x | x | x | x | x | x | x | |||||
element_type | x | x | x | ||||||||||
type | x | x | x | x | |||||||||
return_type | x | ||||||||||||
bool | x | ||||||||||||
unsigned | x | ||||||||||||
long | x | ||||||||||||
vla | x | x | |||||||||||
transparent | x | x | |||||||||||
vector | x | ||||||||||||
complex | x | ||||||||||||
nargs | x | ||||||||||||
vararg | x | ||||||||||||
sse_reg_params | x | ||||||||||||
convention | x | ||||||||||||
value | x |
void | int | float | enum | constant | ptr | ref | array | struct | union | func | field | bitfield | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
members | x | x | |||||||||||
member | x | x | |||||||||||
arguments | x | ||||||||||||
argument | x | ||||||||||||
values | x | ||||||||||||
value | x |