What is the best way to send an array(table) from Lua to Native(Obj-C or Java)?

Hi, all!

I used the enterprise only several weeks.

So please understand if it is silly question.

I have to send a bit of large array from Lua to Native and vice versa.

I’ve look over LuaPluginLibrary.mm could send integer and string argument to Lua from Objective-C.

How to send an table variable from Lua to Native, that is, Objective-C and Java?

Have a look at Corona Enterprise’s sample project “SimpleLuaExtension”.  Particularly the “GetRandomArrayLuaFunction.java” and the “PrintArrayLuaFunction.java” files.  Those 2 source files contain the answers that you are looking for, with plenty of comments explaining how it works.  :)

In fact, that sample project has a lot of great examples on how to work with the LuaState in Java and C in general.  It’ll help you learn the fundamentals of working with Lua from native code.

You can also find documentation for the Java version of the LuaState here…

   http://docs.coronalabs.com/daily/native/android/html/com/naef/jnlua/package-summary.html

I hope this helps!

Thanks for reply, Jushua.

I need that for iOS, however, all of method are not implemented for iOS.

So I’ve checked that Android source code of  “SimpleLuaExtention”.

I found luaL_checktype(lua_State *L, int narg, int t) but I cannot found how to fit into 2nd and 3rd argument.

Original checkType code for Android like followings:

 // Check if the Lua function's first argument is a Lua table. // Will throw an exception if it is not a table or if no argument was given. int luaTableStackIndex = 1; luaState.checkType(luaTableStackIndex, com.naef.jnlua.LuaType.TABLE); // Print all of the key/value paris in the Lua table. System.out.println("printTable()");

You can find the official Lua API documentation, as written by Lua’s creator, here…

   http://www.lua.org/manual/5.1

There’s also a 3rd party API documentation site for Lua which sometimes provides better detail about the Lua’s C functions which I find helpful.

   http://pgl.yoyo.org/luai/i/_

Notice that the Java LuaState methods are near 1-to-1 with the C function equivalents.  The difference being is in C you have to pass the lua_State pointer as the first argument since C is not an object oriented language.

So, in regards to the luaL_checktype() function, you need to provide the following arguments as described by the documentation I provided links to up above…

  • Argument 1:  The lua_State pointer.

  • Argument 2:  Index to the Lua object on the stack that you want to check the type of.

  • Argument 3:  The Lua type to check for such as LUA_TNUMBER, LUA_TSTRING, etc.

Thank you very much, Joshua!

Have a look at Corona Enterprise’s sample project “SimpleLuaExtension”.  Particularly the “GetRandomArrayLuaFunction.java” and the “PrintArrayLuaFunction.java” files.  Those 2 source files contain the answers that you are looking for, with plenty of comments explaining how it works.  :)

In fact, that sample project has a lot of great examples on how to work with the LuaState in Java and C in general.  It’ll help you learn the fundamentals of working with Lua from native code.

You can also find documentation for the Java version of the LuaState here…

   http://docs.coronalabs.com/daily/native/android/html/com/naef/jnlua/package-summary.html

I hope this helps!

Thanks for reply, Jushua.

I need that for iOS, however, all of method are not implemented for iOS.

So I’ve checked that Android source code of  “SimpleLuaExtention”.

I found luaL_checktype(lua_State *L, int narg, int t) but I cannot found how to fit into 2nd and 3rd argument.

Original checkType code for Android like followings:

 // Check if the Lua function's first argument is a Lua table. // Will throw an exception if it is not a table or if no argument was given. int luaTableStackIndex = 1; luaState.checkType(luaTableStackIndex, com.naef.jnlua.LuaType.TABLE); // Print all of the key/value paris in the Lua table. System.out.println("printTable()");

You can find the official Lua API documentation, as written by Lua’s creator, here…

   http://www.lua.org/manual/5.1

There’s also a 3rd party API documentation site for Lua which sometimes provides better detail about the Lua’s C functions which I find helpful.

   http://pgl.yoyo.org/luai/i/_

Notice that the Java LuaState methods are near 1-to-1 with the C function equivalents.  The difference being is in C you have to pass the lua_State pointer as the first argument since C is not an object oriented language.

So, in regards to the luaL_checktype() function, you need to provide the following arguments as described by the documentation I provided links to up above…

  • Argument 1:  The lua_State pointer.

  • Argument 2:  Index to the Lua object on the stack that you want to check the type of.

  • Argument 3:  The Lua type to check for such as LUA_TNUMBER, LUA_TSTRING, etc.

Thank you very much, Joshua!

For what it’s worth, I just rewrote the PrintTableLuaFunction.java for iOS. Probably not the best rewrite but hopefully it helps someone!

[lua]

    //check type. if it’s not a table it will throw an error/exception.

    luaL_checktype(L, 1, LUA_TTABLE);

    

    //print all of the key/value pairs in the Lua table

 

    NSLog(@“printTable()”);

    NSLog(@"{");

    

    for (lua_pushnil(L); lua_next(L, 1); lua_pop(L, 1)) {

        const char *keyName = NULL;

        int luaType = lua_type(L, -2);

        switch (luaType) {

            case LUA_TSTRING:

                // Fetch the table entry’s string key.

                keyName = lua_tostring(L, -2);

                break;

            case LUA_TNUMBER:

                // The key will be a number if the given Lua table is really an array.

                // In this case, the key is an array index. Do not call lua_tostring() on the

                // numeric key or else Lua will convert the key to a string from within the Lua table.

                keyName = [[NSString stringWithFormat:@"%d", lua_tointeger(L, -2)] cStringUsingEncoding:NSASCIIStringEncoding];

                break;

        }

        if (keyName == NULL) {

            //a valid key was not found. skip this table entry.

            continue;

        }

        

        //fetch the table entry’s value in string form.

        //an index of -1 accesses the entry’s value that was pushed into the lua stack by lua_next() above.

        const char *valueString = NULL;

        luaType = lua_type(L, -1);

        switch (luaType) {

            case LUA_TSTRING:

                valueString = lua_tostring(L, -1);

                break;

            case LUA_TBOOLEAN:

                valueString = [[NSString stringWithFormat:@"%d", lua_toboolean(L, -1)] cStringUsingEncoding:NSASCIIStringEncoding];

                break;

            case LUA_TNUMBER:

                valueString = [[NSString stringWithFormat:@"%f", lua_tonumber(L, -1)] cStringUsingEncoding:NSASCIIStringEncoding];

                break;

        }

        if (valueString == NULL) {

            valueString = “”;

        }

        

        NSLog(@"[%s] = %s", keyName, valueString);

    }

    NSLog(@"}");

[/lua]

For what it’s worth, I just rewrote the PrintTableLuaFunction.java for iOS. Probably not the best rewrite but hopefully it helps someone!

[lua]

    //check type. if it’s not a table it will throw an error/exception.

    luaL_checktype(L, 1, LUA_TTABLE);

    

    //print all of the key/value pairs in the Lua table

 

    NSLog(@“printTable()”);

    NSLog(@"{");

    

    for (lua_pushnil(L); lua_next(L, 1); lua_pop(L, 1)) {

        const char *keyName = NULL;

        int luaType = lua_type(L, -2);

        switch (luaType) {

            case LUA_TSTRING:

                // Fetch the table entry’s string key.

                keyName = lua_tostring(L, -2);

                break;

            case LUA_TNUMBER:

                // The key will be a number if the given Lua table is really an array.

                // In this case, the key is an array index. Do not call lua_tostring() on the

                // numeric key or else Lua will convert the key to a string from within the Lua table.

                keyName = [[NSString stringWithFormat:@"%d", lua_tointeger(L, -2)] cStringUsingEncoding:NSASCIIStringEncoding];

                break;

        }

        if (keyName == NULL) {

            //a valid key was not found. skip this table entry.

            continue;

        }

        

        //fetch the table entry’s value in string form.

        //an index of -1 accesses the entry’s value that was pushed into the lua stack by lua_next() above.

        const char *valueString = NULL;

        luaType = lua_type(L, -1);

        switch (luaType) {

            case LUA_TSTRING:

                valueString = lua_tostring(L, -1);

                break;

            case LUA_TBOOLEAN:

                valueString = [[NSString stringWithFormat:@"%d", lua_toboolean(L, -1)] cStringUsingEncoding:NSASCIIStringEncoding];

                break;

            case LUA_TNUMBER:

                valueString = [[NSString stringWithFormat:@"%f", lua_tonumber(L, -1)] cStringUsingEncoding:NSASCIIStringEncoding];

                break;

        }

        if (valueString == NULL) {

            valueString = “”;

        }

        

        NSLog(@"[%s] = %s", keyName, valueString);

    }

    NSLog(@"}");

[/lua]