In my application, I need my lua script to index a java array. For some reason I don't understand, I can't index the array at all within Lua. Here is my Lua file (test_lua.lua): https://pastebin.com/zQUPVArz
Here is the Java file calling this code (experiment.Experimental.class): https://pastebin.com/Gr9EsvbM
And here is the Bobject interface: https://pastebin.com/agdS41sc
Using this method, I am unable to do anything with Java arrays from Lua. I even tried using reflection and CoerceLuaToJava to index the array, but to no avail. Below is the offending Lua code:
behavior_table.doArray = function(array)
print("Length:", array.length) -- Prints array length correctly
print(array) -- Prints the array using toString(), resulting in [Ljava.lang.Object;@2d363fb3
print(array[0]) -- prints 'nil'
Array = luajava.bindClass("java.lang.reflect.Array")
Coerce = luajava.bindClass("org.luaj.vm2.lib.jse.CoerceLuaToJava")
array[0] = 4 -- Throws a LuaError: index expected, got userdata
print(array[0]) -- prints 'nil', as before
print(Array.get(Coerce.coerce(array), 0)) -- Throws a NullPointerException
end
Prior to this code, I also tried loading the behavior_table itself as a LuaTable object in Java, using explicitly typed coercions, with something like this:
table.get("doArray").invoke(new LuaValue[]{
CoerceJavaToLua.coerce(array)
}).arg1()
...Thus calling the function from the table and coercing the Java objects myself, as opposed to having the script return a Bobject instance with overidden functions. This method didn't change the outcome at all.
Am I just using the improper syntax for indexing the coerced array? I feel like it's being coerced properly, given that I can print it using toString() and access its length. I think the array is coerced into a lua table, correct? If that's the case, how do I index its values? I'm still very new to the concept of Lua's "tables"
Thank you for your time.