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

Notice: as of version 1.0, this requires a direct hook into the consoles-computers plugin.

Function Mapping

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.

Library Mapping

If you want to create a whole library, you have the option of using LibraryCreator, which can be used to create Lua libraries from a pure Java class. You do not need to use any of the LuaJ classes or map anything else out:

LibraryCreator.link(NetLibrary::new, "net", true);

This is an example of how the net library is registered in Consoles. We pass NetLibrary::new to tell Consoles how to create an instance of your library, then "net" as the name of our library. The last argument tells Consoles whether this library should be restricted to privileged programs that have successfully ran removeRestrictions().

And that's it! All methods from the NetLibrary class are mapped as Lua functions and can be accessed from the net table. You can also safely return Java objects from your methods, and use LuaValue parameters (as a direct way to access a value), as well as FunctionBind, which is casted from a function argument in Lua.

Type mapping

  • 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.