How to wrap a C function whose parameters are pointer to structs, so that it can be called from Lua?

Posted by pierr on Stack Overflow See other posts from Stack Overflow or by pierr
Published on 2010-03-26T05:06:31Z Indexed on 2010/03/26 7:53 UTC
Read the original article Hit count: 220

Filed under:
|
|
|

I have the follwing C function. How should I wrap it so it can be called from a Lua script?

typedef struct tagT{
    int a ; 
    int b ;
} type_t;

int lib_a_f_4(type_t *t)
{
     return t->a * t->b ;
}

I know how to wrapr it if the function parameter type were int or char *. Should I use table type for a C structure?

EDIT: I am using SWIG for the wraping , according to this doc, It seems that I should automatically have this funtion new_type_t(2,3) , but it is not the case.

If you wrap a C structure, it is also mapped to a Lua userdata. By adding a metatable to the userdata, this provides a very natural interface. For example,

struct Point{ int x,y; };

is used as follows:

p=example.new_Point() p.x=3 p.y=5 print(p.x,p.y) 3 5

Similar access is provided for unions and the data members of C++ classes. C structures are created using a function new_Point(), but for C++ classes are created using just the name Point().

© Stack Overflow or respective owner

Related posts about lua

Related posts about c