|
KahluaManual
Kahlua Manual
Featured Contents
Building KahluaKahlua uses Apache Ant to build itself. Simply run ant from the project root to compile, package and run all the test cases. TestingKahlua is developed by Test Driven Development and uses its own test suite (implemented in Lua) to verify that it's always working correctly. It currently has about 98% code coverage. After having built Kahlua, you can find a test report inside testreport.html and a coverage report inside testsuite/coverage/coverage.html. Differences compared with regular LuaKahlua is not fully compatible with Lua, which is to be expected as it only runs from Java CLDC 1.1. Some functions in the standard library are missing and some functions do not behave exactly the same way. This is all documented in order to help the transition from Lua to Kahlua. Basic Kahlua typesThe fundamental types in Lua of course have their corresponding type in Kahlua. nil, number, boolean and string map directly to null, Double, Boolean and String. table and coroutine map to Kahlua's own LuaTable and LuaThread. function maps to either the JavaFunction interface if it's implemented in Java and LuaClosure if it's a Lua function. This means that you can pass doubles, booleans and strings directly into Lua without any problems. Garbage collectionKahlua uses Javas own garbage collection with means that all of the collectgarbage options are not supported, as well as the __gc metatable mode. UserdataKahlua does not share Luas concept of userdata. Instead, you can simply pass any object into Kahlua. In order to operate on them, you must set the metatable for the class, which will then apply to all objects of exactly that class (not subclasses). The only way to operate on userdata is to export java functions that can manipulate them. (See example of this in the UserdataArray class.) StringsStrings in Kahlua are represented in UTF-16, just like Java strings (this is because Kahlua strings are in fact represented as Java strings). Missing functionsThe following functions are not implemented in Kahlua:
Functions with different behaviourThese functions behave almost like in Lua: pcallpcall only differs in the return values on failure. pcall in Lua returns false, errormessage pcall in Kahlua returns:
collectgarbagecollectgarbage([opt]) works like this for the following values of opt:
errorInstead of being error(message, [level]) as in Lua, Kahlua has error(message, [stacktrace]). stacktrace is appended to the total stacktrace when caught by pcall. Just like in Lua, if error is called with zero parameters, it does nothing. Functions that behave the same as in Lua
Extra functionsdebugstacktraceThere is already a similar function in Lua, called debug.traceback ([thread,] [message] [, level]). debugstacktrace in Kahlua is used like this: debugstacktrace(thread, level, count, halt_at).
Compat optionsKahlua implements no compat-options, i.e. no support for arg as in Lua 5.0 CompilerKahlua currently does not have its own compiler, but it has support for using LuaJs compiler. This is very easy to do, just add LuaJ to the classpath, and run LuaCompiler.register(state) You will then get access to loadstring inside Lua. Also take a look at the source code inside testsuite/src for examples on how to use it. Kahlua can also natively load precompiled Lua 5.1b bytecode. Use luac -o output.lbc input.lua to compile a lua source file to bytecode. You can then use LuaPrototype.loadBytecode to get a lua closure. Thread safetyKahlua is not thread safe in any way. Do not attempt to use a LuaState concurrently from different threads. Also do not try to move a LuaFunction or JavaFunction into a different state. Sharing of booleans, numbers, strings and tables is safe, but you must manually ensure that no thread is writing to a LuaTable while another is reading (or writing), since the data structure is not thread safe. However, you can create as many LuaStates as you want to run in different threads if they do not communicate directly. All LuaStates are completely independent of one another and do not share any writeable static fields. Java APIIt's quite easy to add Kahlua to your Java application. For a quick start guide, I recommend looking at Test.java and the midlet demo distributed with Kahlua. They are both quite simple classes. The midlet even shows how to export its own function to Kahlua. Exporting your own functions to LuaKahlua defines an interface called JavaFunction, which needs only one method: int call(LuaCallFrame callFrame, int nArguments); callFrame contains all the input arguments, which can be reached by using callFrame.get(i), where 0 <= i < nArguments Note that nArguments is how many arguments it was called with, not how many you expect. You should be prepared to handle both when you receive fewer or more arguments than expected. Also note that all numbers from Lua will be in the type Double, so if you're expecting an int or a long, you have to do the conversion yourself. It's safe to throw any kind of RuntimeException from here - Lua will catch it and convert it to a proper Lua error. Input is only half of the fun though, you may also want to send some output back. The outputs are placed at the same place as the inputs, so you can either choose to overwrite them directly, or push the return values above them. Kahlua will take care of removing the unwanted objects after the return. Use callFrame.push(object) to push as many objects that you want to return. Then, return that same number. You can also use callFrame.setTop(nReturnValues) to specify how many return values you want, and then use callFrame.set(index, object) to fill up the list of return values. As with the input, 0 <= index < nReturnValues. In either case, ALWAYS remember to return the number of return values, which must be an integer >= 0. For examples on how JavaFunctions typically look, see any of the classes in se.krka.kahlua.stdlib Inspecting Lua from JavaUnlike Lua, Kahlua does not need a large set of API functions to be useful. While Lua provides a clear distinction between Lua space and C space, Kahlua does not do the same for Java. Lua objects are not directly available to C code, so instead you must manipulate them by using the API helper functions. For instance, to get a value out of a table in Lua, you must do something like: lua_State *L = myState;
lua_getglobal(L, "myTable");
lua_pushstring(L, "myKey");
lua_gettable(L, stack_index);
lua_gettable(L, -2);
// the value is now at stack index -1, to inspect it, use
if (lua_isboolean(L, -1)) {
bool value = lua_toboolean(L, -1);
}In Kahlua, you don't need such helper functions, you can use the Java objects directly: LuaState state = myState;
LuaTable table = (LuaTable) state.getEnvironment().rawget("myTable");
Object value = table.rawget("myKey");
if (value instanceof Boolean) {
boolean value = ((Boolean) value).booleanValue();
}The only helper functions you really need are LuaState.getEnvironment() and LuaState.pcall(). |
Dear Sir., How can I extract a lua function from a lua script file and invoke it with Kalua? In fact,if I have a lua chunk like this:
print("Hello, lua function call with kahlua.") function f1(a,b) return a+b end print(f1(3,5)),and I load it with "loadis(InputStream?, String, LuaState?)", how can I get kahlua invoke this lua function "f1(a,b)"?
A small note - No where in the documentation it is mentioned which version of Lua compiler is required. The example MIDlet shipped works fine if you use Lua 5.1 compiler. It doesn't work with 5.2.