Cocos LUA binding sentiment---tolua_isusertable and how LUA accesses cocos static functions

Source: Internet
Author: User

Now still a novice, to many still do not know very much, so the language estimate will be a bit obscure, hope forgive Ah!!

<pre name= "code" class= "HTML" >TOLUA_API int tolua_isusertable (lua_state* L, int lo, const char* type, int def, Tolu a_error* err) {    if (def && lua_gettop (L) <abs (LO))        return 1;    if (lua_isusertable (l,lo,type))        return 1;    Err->index = lo;    Err->array = 0;    Err->type = type;    return 0;}


static  int lua_isusertable (lua_state* L, int lo, const char* type) {    int r = 0;    if (Lo < 0) lo = lua_gettop (L) +lo+1;    Lua_pushvalue (L,lo);    Lua_rawget (l,lua_registryindex);  /* Get registry[t] *    /if (lua_isstring (l,-1))    {        r = strcmp (lua_tostring (l,-1), type) ==0;        if (!r)        {/            * Try const *            /lua_pushstring (L, "const");            Lua_insert (l,-2);            Lua_concat (l,2);            R = lua_isstring (l,-1) && strcmp (lua_tostring (l,-1), type) ==0;        }    }    Lua_pop (L, 1);    return r;}

The meaning of the code is very simple, nothing more than to determine the LOC position in the stack data is not usertable. The approximate idea of the code is to fetch the value A from the LOC position of the stack, to find the value B in the registry, and then to compare the string to B to determine if it is type or const type. So it's obvious that a is the table of type or const type. When will the tolua_isusertable be called?

Simply search the tolua_isusertable exactly where it was quoted. See that basically all are referenced in the Create method, there are some other methods, careful observation of these functions, found to be static functions, so guess is not static function in the generation of Lua and Cocos Middle-tier code will be generated? I have a question. A static method is also defined in the custom class, and it is found that tolua_isusertable is generated.

A static method can be called by a class, so it is not the type of the class that LUA passes over before execution. This is the role of tolua_isusertable. The other member function is to judge whether the pass-through is UserData, and the UserData is not the type of the class.

Let's take an example:

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:14PX;" >local test = CCC. Myclass:create () </span>

Here CCC. MyClass is a table that is the data passed to the LOC location of Cocos, and create is a static function in the MyClass of C + +.


Summary: Tolua_isusertable is used in a function called by LUA directly from a table (a C + + Class) to determine if the table is a corresponding C + + type. Instead, Tolua_isuserdata is used in functions that are called through the object.


So the question is, can I access the static function through the LUA variable? I tried, found not to be the pit daddy.


<pre name= "code" class= "html" >local test = CCC. Myclass:create () print ("The first Cocos bind Lua result".) Test:foo (2)) print (Test:getdata ())


program crashes ...

So there's no way to solve this problem? In fact, it is quite simple to change. First put down the getdata of the middle code it:

<pre name= "code" class= "HTML" >int lua_myclass_myclass_getdata (lua_state* tolua_s) {    int argc = 0;    bool OK  = true; #if cocos2d_debug >= 1    tolua_error tolua_err; #endif # if Cocos2d_debug >= 1  if (!tolua_ Isusertable (tolua_s,1, "GLS. MyClass ", 0,&tolua_err))) goto Tolua_lerror; #endif    argc = lua_gettop (tolua_s)-1;    if (argc = = 0)    {        if (!ok)            return 0        ; int ret = Gamelogic::myclass::getdata ();        Tolua_pushnumber (tolua_s, (lua_number) ret);        return 1;    }    Cclog ("%s had wrong number of arguments:%d, was expecting%d\n", "GetData", argc, 0);    return 0; #if cocos2d_debug >= 1    tolua_lerror:    tolua_error (tolua_s, "#ferror in function" Lua_myclass_ Myclass_getdata '. ", &tolua_err); #endif    return 0;}

if (!tolua_isusertable (tolua_s,1, "GLS. MyClass ", 0,&tolua_err))) goto Tolua_lerror;

The program is here to collapse, Tolua found to pass over is not a table, in fact, it is a userdata, so error!! We change is also here, add a judge whether it is UserData can be!!

Change that line to:

if (!tolua_isusertable (tolua_s,1, "GLS. MyClass ", 0,&tolua_err) &&!tolua_isusertype (tolua_s,1," GLS. MyClass ", 0,&tolua_err)) goto Tolua_lerror;


It's done!!


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Cocos LUA binding sentiment---tolua_isusertable and how LUA accesses cocos static functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.