Skip to content
Levi Webb edited this page Apr 17, 2015 · 3 revisions

As stated in the Lua programming page, Consoles uses LuaJ as its interpreter implementation. LuaJ is simple and powerful by itself, but there's also added function mapping in Consoles using lambdas from Java 8 and TypeTools to resolve types from lambdas.

It's really easy to map a function in Java:

Lua.map(MyClass::myMethod, "luaName");

Do not map a function multiple times, like in the constructor of an object. This line of code should only be in a static block, like so:

static {
    Lua.map(Computer::lua_session, "switchSession");
    Lua.map(Computer::lua_dialog, "dialog");
}

(excerpt from Computer.class)

This maps a function statically, and it automatically added to the global function pool when a program is initialized. Inside of your mapped function, you can obtain some context about where the function was executed from:

public static void doSomethingInLua(String input) {
    Computer computer = Lua.context();
}

This, if the function is being executed from a program, will return the computer the program was executed from.

If you want to map objects for a specific program instead, you will need to do it using the pool from InterpretedProgram and Lua.link(method, name, pool). See the source code for details.

Type mapping

  • Primitive mapping does not work. You have to use the immutable wrapper classes like Integer, Character and Short for arguments in your java functions that are being mapped to lua functions.

  • You can read pure lua values if you have an argument parameter of type LuaValue, or an implementing class.

  • You can read a generic lua function parameter if you have an argument of type FunctionBind (cannot be implementing), which will allow you to use call(Object...) on the bind.

  • If you return a LuaValue, that value will not be translated in any way.

  • If you return an object that does not have a lua counterpart, then it is wrapped into a lua object, which will leave all of its public fields and functions visible to the lua code.

  • Single and multi-dimensional arrays as return values are translated smoothly into lua arrays. No need to worry about this.