| 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"! |
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):
The basic recommendations should therefore be:
|
Source CodeThe source code of this script and its accompanying Java class is publically available: |
DisclaimerPlease, also consider the author's Disclaimer! |
|
|
|
| http://www.Andreas-Rozek.de/LuaJava/Acquainting/LuaJava_06_en.html | (last Modification: 25.11.2004) |