tolua ++ realization analysis

The project is using cocos2dx lua binding, binding way tolua ++. For large-scale use lua code that confidence is not very full, spent some time reading tolua ++ code, we want to understand the binding implementation, the project will help to lua code to control. From reading the results, at least in terms of memory management, help is still very large.

How 1.tolua ++ c ++ objects into lua in

tolua ++ lua for each incoming object the establishment of a userdata, userdata value, is the address of c ++ objects. userdata of metatable, is a tolua ++ established record userdata table corresponding to c ++ types of information, including information derived member variables, member functions and so on.

For reading assignments member variables, tolua ++ is built .set two tables and .get in metatable years. The two tables are stored in a variable named keys, function c to read entries set value. In metatable of __index and __newindex, the key to a variable named, made from .get function and read the settings and call .set table.

For the member function calls, simply a function called a key, a function of the value stored in metatable in just fine.

Incoming c ++ object tolua ++ function is tolua_pushusertype. Under normal circumstances, the first time you use this function to a c ++ object push to the lua stack, will create a new userdata. tolua ++ c ++ object address will be a bond, UserData value, the key value table stored in tolua_ubox. When the next c ++ to push the same object taken from this list can be pushed onto the stack userdata.

—————————————————

How to deal with the type of inheritance 2.tolua ++

metatable parent class is a subclass of metatable of metatable. When this call the parent class method, it will go metatable parent's searched.

tolua ++ also maintains a tolua_super table that type of metatable to c ++ is the key to a table as the value. The values ​​in the table type name is a bond to the value true, recording corresponding to a metatable c ++ what type parent. This form can be used to help determine whether the object is a certain type (sub-class instance can be considered the parent type)

—————————————————

How 3.tolua ++ object lifecycle management

Generally, when the inside of lua c ++ object reference variables can be garbage, tolua ++ simply release userdata 4 byte pointer address of the memory occupied. But can also bind or code specifies the way for the release of the object share tolua ++ real memory.

Binding mode, means when the type c ++ constructor is used to derive lua in tolua ++, tolua ++ automatically generated new_local method. If memory recycling target in lua code, when an object using this new method, tolua ++ calls tolua_register_gc method, indicating recovery of the object.

In c ++ code, using tolua_pushusertype_and_takeownership; lua code in use tolua.takeownership, can achieve the same purpose.

For these objects by a specified tolua ++ reclaim memory, if its type destructor derived by tolua ++, at the time of recovery of memory, will delete operator by calling the destructor of an object. Otherwise it will use the free method of recovering.

tolua_register_gc way to do things, is key for the object pointer to the object metatable the value, the key value stored in tolua_gc table. In __gc method object types metatable table where, tolua ++ checks tolua_gc table contains the key to this address entry. It will be included in the above words of memory recovery work.

4. Other

Sometimes, after obtaining a c ++ objects lua, we would like to assign it some meaningful only in lua environmental attributes. Or, we would like to extend a c ++ class in lua. tolua ++ also provides a mechanism to achieve this demand.

tolua ++ in LUA_REGISTRY maintained in a tolua_peers table. This table to represent the c ++ object userdata is key to a table t is the value. t there on the record of this object in the extended properties in lua.

cocos2dx did not fully use tolua ++ way, but some amendments themselves. Next blog should be amended record why and how, and introduce some cocos2dx lua bind specific things.
----------------
Disclaimer: This article is CSDN blogger "wtyqm 'original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement. .
Original link: https: //blog.csdn.net/wtyqm/article/details/8977975

Published 56 original articles · won praise 10 · views 60000 +

Guess you like

Origin blog.csdn.net/GJQI12/article/details/104923541