How do I read an array argument from a LuaState

I have a named function named “send” which in lua accepts a table array of integers for an argument.

On the java side, how do I fetch that data from the LuaState and convert it to an int[]?

private class send implements NamedJavaFunction {
@Override
public String getName() {
return “send”;
}
@Override
public int invoke(LuaState L) {

// get array size from LuaState

int[] nums = new int[arraySize];

// get array values from LuaState

This is my current approach which doesn’t seem to work:

int count = L.tableSize(1);
L.getTable(1);
int[] nums = new int[count];
for( int i = 0; i < count; i++ ) {
nums[i] = L.toInteger( i+1 );
}

Thanks

In C / C++, I would do

size\_t n = lua\_objlen(L, 1); // assuming array is on slot 1 for (int i = 1; i \<= n; ++i) { lua\_rawgeti(L, 1, i); // array, item #i lua\_Integer integer = lua\_tointeger(L, -1); DoSomething(integer); lua\_pop(L, 1); // array }

I’ve occasionally seen some of the Java code and it usually looks similar to the C API, so maybe it would here too? Do note that all the Lua indices will be 1-based, though.

In C / C++, I would do

size\_t n = lua\_objlen(L, 1); // assuming array is on slot 1 for (int i = 1; i \<= n; ++i) { lua\_rawgeti(L, 1, i); // array, item #i lua\_Integer integer = lua\_tointeger(L, -1); DoSomething(integer); lua\_pop(L, 1); // array }

I’ve occasionally seen some of the Java code and it usually looks similar to the C API, so maybe it would here too? Do note that all the Lua indices will be 1-based, though.