CompiledScript script = ((Compilable) scriptEngine).compile(reader); Bindings sb = new SimpleBindings(); script.eval(sb); // Put the Lua functions into the sb environment LuaValue luaDog = CoerceJavaToLua.coerce(dog); // Java to Lua LuaFunction onTalk = (LuaFunction) sb.get("onTalk"); // Get Lua function LuaValue b = onTalk.call(luaDog); // Call the function System.out.println("onTalk answered: " + b); LuaFunction onWalk = (LuaFunction) sb.get("onWalk"); LuaValue[] dogs = { luaDog }; Varargs dist = onWalk.invoke(LuaValue.varargsOf(dogs)); // Alternative System.out.println("onWalk returned: " + dist);
@Override public LuaValue call(LuaValue function, LuaValue delay) { Thread thread = new Thread(() -> { try { if (delay != LuaValue.NIL) Thread.sleep(delay.checklong()); function.checkfunction().call(); } catch (InterruptedException ignored) { } }); thread.start(); return LuaValue.NIL; } });
public LuaWrapper(String script, Map<String, Object> extras) { try { this.env = JsePlatform.standardGlobals(); LoadState.load(getClass().getResourceAsStream("/lua/kitten.lua"), "kitten.lua", env).call(); for (Map.Entry<String, Object> e : extras.entrySet()) { env.set(e.getKey(), CoerceJavaToLua.coerce(e.getValue())); } InputStream luaCode = LocalDataHelper.getFileOrResource(script); LoadState.load(luaCode, script, env).call(); } catch (IOException e) { LOG.error("Lua initialization error", e); throw new RuntimeException(e); } }
@Override public void onLoad() { try { if (this.loadCB != null) this.loadCB.call(); } catch (LukkitPluginException e) { e.printStackTrace(); LuaEnvironment.addError(e); } }
function.call(); function.call( args[0] instanceof LuaValue ? (LuaValue) args[0] : CoerceJavaToLua.coerce(args[0]) ); function.call( args[0] instanceof LuaValue ? (LuaValue) args[0] : CoerceJavaToLua.coerce(args[0]), args[1] instanceof LuaValue ? (LuaValue) args[1] : CoerceJavaToLua.coerce(args[1]) ); function.call( args[0] instanceof LuaValue ? (LuaValue) args[0] : CoerceJavaToLua.coerce(args[0]), args[1] instanceof LuaValue ? (LuaValue) args[1] : CoerceJavaToLua.coerce(args[1]), args[2] instanceof LuaValue ? (LuaValue) args[2] : CoerceJavaToLua.coerce(args[2]) );
public Listener registerEvent(Class<? extends Event> event, LuaFunction function) { getEventListeners(event).add(function); if (this.enabled) { Listener listener = new Listener() { }; this.getServer().getPluginManager().registerEvent(event, listener, EventPriority.NORMAL, (l, e) -> function.call(CoerceJavaToLua.coerce(e)), this, false); } return null; }
@Override public void onEnable() { this.enabled = true; try { if (this.enableCB != null) this.enableCB.call(CoerceJavaToLua.coerce(this)); } catch (LukkitPluginException e) { e.printStackTrace(); LuaEnvironment.addError(e); } eventListeners.forEach((event, list) -> list.forEach(function -> this.getServer().getPluginManager().registerEvent(event, new Listener() { }, EventPriority.NORMAL, (l, e) -> function.call(CoerceJavaToLua.coerce(e)), this, false) )); }
@Override public void onDisable() { this.enabled = false; try { if (this.disableCB != null) this.disableCB.call(CoerceJavaToLua.coerce(this)); } catch (LukkitPluginException e) { e.printStackTrace(); LuaEnvironment.addError(e); } unregisterAllCommands(); }
mainFunc.call(CoerceJavaToLua.coerce(library));
protected LuaValue loadFunction(String functionName, Prototype prototype, Frame frame) throws ScribuntoException { try { currentFrame = frame; LuaValue function = new LuaClosure(prototype, globals).checkfunction().call().get(functionName); if (function.isnil()) { throw new ScribuntoException("no such function '"+functionName+"'"); } return function; } catch (LuaError e) { throw new ScribuntoException(e); } finally { currentFrame = null; } }
@Override public Varargs invoke(Varargs args) { LuaFunction chunk = args.arg(1).checkfunction(); LuaValue name = args.arg(2); final LuaValue res = chunk.call(); if (name.isnil()) { return LuaValue.varargsOf(new LuaValue[]{LuaValue.TRUE, res}); } else { if (!res.istable()) { return LuaValue.varargsOf(new LuaValue[]{FALSE, toLuaString(res.typename())}); } else { return LuaValue.varargsOf(new LuaValue[]{LuaValue.TRUE, res.checktable().get(name)}); } } } });