lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hello,

I'm using tolua++ to embedd LUA in my C++ application. Works for most stuff, but now I like to give a array with dynamic size from LUA to C++. tolua++ is only able to handle fixed size arrays. So I try to mix tolua++ with the LUA C-API. I used some example code to get starting.

This is my C++ function:

void TestClass::X(lua_State *L, lua_Object t, lua_Object o2)
{
    if (lua_istable (L, t))
    {
        printf ("1: is a table");
    }
    else
    {
        printf ("1: isn't a table");
    }

     /* table is in the stack at index 't' */
     lua_pushnil(L);  /* first key */
     while (lua_next(L, t) != 0) {
       /* uses 'key' (at index -2) and 'value' (at index -1) */
       printf("%s - %s : %d\n",
              lua_typename(L, lua_type(L, -2)),
              lua_typename(L, lua_type(L, -1)),
              lua_tonumber(L, -1)
              );
       /* removes 'value'; keeps 'key' for next iteration */
       lua_pop(L, 1);
     }
    
    double number = lua_tonumber(L, o2);
    printf ("Parameter1: %f", number);

    return;
}

This is my LUA function:

function eb_test2()
    tc=TestClass:new()
    a={4,5,"hello",7}
    tc:X(a,8)
end

Here is the result:

C++: TestClass::TestClass ()
1: is a table
number - number : 1023
number - number : 1024
number - string : 1
number - number : 1025
Parameter1: 8.000000

The C++ code does detect the table type, the correct table size and the element types (string/number). But I don't understand the values I get (1023,1024,1,1025). Ok, the string is for sure handled wrong with lua_tonumber, but also the other numbers are wrong. Could anyone tell me why this happens?

regards
Andreas Volz

--  
Dipl.-Inf. (FH) Andreas Volz, ES1
Elektrobit 
Phone: +49 (9131) 7701 167, mailto:Andreas.Volz@elektrobit.com
Fax: +49 (9131) 7701 333, http://www.elektrobit.com

Elektrobit Automotive GmbH, Am Wolfsmantel 46, 91058 Erlangen, Germany
Managing Director Otto Fößel
Register Court Fürth HRB 4886


----------------------------------------------------------------
Please note: This e-mail may contain confidential information
intended solely for the addressee. If you have received this
e-mail in error, please do not disclose it to anyone, notify
the sender promptly, and delete the message from your system.
Thank you.