Andreas Rozek      

LuaJava_06 - will Lua types (and values) be preserved?

For a callback from within Java, it might be sometimes necessary to store a Lua value and pass it back to a Lua function later (unmodified). LuaJava_06 tests if Java is able to preserve the original type (and content) of such a Lua value.

Please, also consider my "Hints for Reading" and the "List of Recent Changes"!
(Problems displaying this page? Ugly graphics? Please, click here)

LuaJava_06

The script itself is extremely simple and should not require any further explanation. The accompanying Java source may be compiled using
 

  javac LuaJava_06.java

In contrast to the previous examples, the LuaJava_06 class needs the luajava package which should therefore be available to the compiler. The resulting class file should be copied to a place where it can be found by the Java class loader (e.g., into a directory which is automatically scanned by the Java extension mechanism).

After an invocation of the form
 

  java luna.LuaJava LuaJava_06.lua

the script produces the following output

LuaJava_06 - will Lua types (and values) be preserved?

when Java accepts generic objects:
  Java2Lua(nil)            -> nil (nil)
  Java2Lua(true)           -> boolean (true)
  Java2Lua(123.456)        -> number (123)
    (as a comparison: tostring(123.456) -> 123.456)
  Java2Lua('Test')         -> string (Test)
  Java2Lua({})             -> table (table: 006EF6F0)
  Java2Lua(function() end) -> function (function: 006EF5F0)

when Java accepts specific types (boolean, double, String, Object):
  Java2Lua2(nil)            -> (crashes)
  Java2Lua2(true)           -> boolean (true)
  Java2Lua2(123.456)        -> number (123.456)
  Java2Lua2('Test')         -> string (Test)
  Java2Lua2({})             -> table (table: 006EF0F0)
  Java2Lua2(function() end) -> function (function: 006EF4F0)

when Java accepts generic Lua objects:
  Java2Lua3(nil)      -> nil (nil)
  Java2Lua3(true)     -> (crashes)
  Java2Lua3(123.456)  -> (crashes)
  Java2Lua3('Test')   -> (crashes)
  Java2Lua3({})       -> table (table: 006EF370)
  Java2Lua3(function() end) -> function (function: 006EEC50)

it seems impossible to combine both approaches:
  Java2Lua4(nil)            -> (still crashes)
  Java2Lua4(true)           -> boolean (true)
  Java2Lua4(123.456)        -> number (123.456)
  Java2Lua4('Test')         -> string (Test)
  Java2Lua4({})             -> table (table: 006EEDF0)
  Java2Lua4(function() end) -> function (function: 006EECF0)

which is, again, really interesting to analyze (look at the coloured lines):

  • when passed as a generic Java Object, Lua numbers seem to get truncated (this will be investigated further in LuaJava_07) - but the Lua value nil seems to be accepted;
  • when Lua(Java) has the choice between the Java (argument) types boolean, double, String and Object, numbers will be handled properly, but passing the Lua value nil will crash the program;
  • accepting a luajava.LuaObject on the Java side does not seem to be a good idea - although the Lua value nil will get accepted again;
  • it does not seem to be possible to combine the second and the third approach - nil will not be accepted again.

The basic recommendations should therefore be:

  • you will have to know in advance what kind of Lua value you will have to accept in your Java method;
  • use boolean, double, String or Object as argument types on the Java side;
  • never pass the Lua value nil to a Java method.

Source Code

The source code of this script and its accompanying Java class is publically available:

Disclaimer

Please, also consider the author's Disclaimer!


http://www.Andreas-Rozek.de/LuaJava/Acquainting/LuaJava_06_en.html   (last Modification: 25.11.2004)