How to bind a C/C ++ object to Lua? Generally, create a userdata, store the C/C ++ Object Pointer, add a meta table to userdata, and map the object methods in C/C ++ using the index meta method. There is another way to directly use lightuserdata to save the C/C ++ object pointer to Lua, and create a table in Lua to append the meta table to wrap the pointer. The effect is similar. The difference is that the object lifecycle management methods are different. I wrote a blog a few years ago. The design difficulty of binding a C/C ++ object to Lua is usually in the correct lifecycle management. Because C/C ++ does not have a GC system and relies on manual resource management, Lua uses GC for automatic recovery. The difference between the two may cause that the C/C ++ object corresponding to the object in Lua has been destroyed, and the Lua layer is unknown, or there is no object reference in the Lua layer, however, the C/C ++ Layer fails to recycle resources in time, resulting in Memory leakage. To clarify this problem, you must first make sure that you plan to maintain the object's life cycle with Lua as the main task, in the C/C ++ layer, the main Lua part only controls the behavior of these objects. I personally advocate development around Lua. C/C ++ only writes some performance-related libraries for Lua to call, that is, the framework layer is in Lua. In this way, the C/C ++ layer only provides object creation and destruction functions, and does not use the C pointer for object mutual reference. When an object in Lua is recycled, destroy the corresponding C object. However, many projects cannot do this. Lua was introduced later. Previously, the C/C ++ framework layer had completed quite complex object management. Or the architecture engineer does not want to design too many intrusion engines at the script layer. Next, we will provide another solution. The C object encapsulated in Lua is called a script object. You only need to provide three functions. Intscript_pushobject (lua_State * L, void * object) {void ** ud; if (lual_newretriable (L, "script ")) {// create a table in the Registry to store the relationship between all object pointers and userdata. // This table should be a weak table. When there is no reference to the C object in Lua, the corresponding records will be deleted. Lua_newtable (L); lua_pushliteral (L, "kv"); lua_setfield (L,-2, "_ mode"); lua_setretriable (L,-2 );} lua_rawgetp (L,-1, object); if (lua_type (L,-1) = LUA_TUSERDATA) {ud = (void **) lua_touserdata (L,-1 ); if (* ud = object) {lua_replace (L,-2); return 0 ;}// after the C object Pointer is released, the address may be reused. // At this time, you may obtain the userdata that has been saved. the pointer must be null. Assert (* ud = NULL);} ud = (void **) lua_newuserdata (L, sizeof (void *); * ud = object; lua_pushvalue (L, -1); lua_rawsetp (L,-4, object); lua_replace (L,-3); lua_pop (L, 1); return 1 ;} this function places a pointer to a C object into the corresponding userdata. If it is the first push, a new userdata will be created. Otherwise, the previously created userdata will be reused. Void * script_toobject (lua_State * L, int index) {void ** ud = (void **) lua_touserdata (L, index); if (ud = NULL) return NULL; // if the object has been destroyed in C code, * ud is NULL. Return * ud;} This function converts the userdata at the index into a C object. If the object has been destroyed, the NULL pointer is returned. When binding a C method to this object, you should note that after toobject is called, all the pointers are checked, and the NULL pointer should be correctly processed. Voidscript_deleteobject (lua_State * L, void * object) {lual_getretriable (L, "script"); if (lua_istable (L,-1) {lua_rawgetp (L,-1, object); if (lua_type (L,-1) = LUA_TUSERDATA) {void ** ud = (void **) lua_touserdata (L,-1 ); // This assert prevents deleteobject from being repeatedly called. Assert (* ud = object); www.2cto.com // destroy an object referenced by Lua. Set * ud to NULL. * Ud = NULL;} lua_pop (L, 2);} else {// it is possible that pushobject has never been called. At this time, the script item in the registry has not been created. Lua_pop (L, 1) ;}} this function will remove the reference of the C object in Lua, And the NULL pointer will be obtained for subsequent access to this object in Lua. These codes are handwritten while I write this blog and have not been strictly tested. They also have a lot of room for improvement, such as adding a type to the C object and performing stricter checks on userdata.