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);
int currentline() { if ( !f.isclosure() ) return -1; int[] li = f.checkclosure().p.lineinfo; return li==null || pc<0 || pc>=li.length? -1: li[pc]; } String sourceline() {
String sourceline() { if ( !f.isclosure() ) return f.tojstring(); return f.checkclosure().p.shortsource() + ":" + currentline(); } private int linedefined() {
static NameWhat getfuncname(DebugLib.CallFrame frame) { if (!frame.f.isclosure()) return new NameWhat(frame.f.classnamestub(), "Java"); Prototype p = frame.f.checkclosure().p; int pc = frame.pc; int i = p.code[pc]; /* calling instruction */
public void funcinfo(LuaFunction f) { if (f.isclosure()) { Prototype p = f.checkclosure().p; this.source = p.source != null ? p.source.tojstring() : "=?"; this.linedefined = p.linedefined; this.lastlinedefined = p.lastlinedefined; this.what = (this.linedefined == 0) ? "main" : "Lua"; this.short_src = p.shortsource(); } else { this.source = "=[Java]"; this.linedefined = -1; this.lastlinedefined = -1; this.what = "Java"; this.short_src = f.name(); } } }
@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; } });
/** Return a human-readable name for this function. Returns the last part of the class name by default. * Is overridden by LuaClosure to return the source file and line, and by LibFunctions to return the name. * @return common name for this function. */ public String name() { return classnamestub(); } }
public LuaMap merge(LuaMap map2, LuaFunction func) { HashMap<LuaValue,LuaValue> target = new HashMap<LuaValue,LuaValue>(map.size() + map2.map.size()); target.putAll(map); boolean hasFunc = !(func == null || func.isnil()); for (Entry<LuaValue,LuaValue> entry : map2.map.entrySet()) { if (hasFunc) { LuaValue value = map.get(entry.getKey()); if (value != null) { Varargs ret = func.invoke(value, entry.getValue()); target.put(entry.getKey(), (LuaValue)ret); continue; } } target.put(entry.getKey(), entry.getValue()); } return new LuaMap(instance, target); }
public boolean call(LuaFunction function, Object... args){ if( function == null || function.isnil() ){ return false; 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]) );
@Override public String tojstring() { return name != null? name: super.tojstring(); }
void instr(int pc, Varargs v, int top) { this.pc = pc; this.v = v; this.top = top; if (TRACE) Print.printState(f.checkclosure(), pc, stack, top, v); } Varargs getLocal(int i) {
public Varargs invoke(Varargs args) { String name = args.checkjstring(1); String classname = toClassname( name ); Class c = null; LuaValue v = null; try { c = Class.forName(classname); v = (LuaValue) c.newInstance(); if (v.isfunction()) ((LuaFunction)v).initupvalue1(globals); return varargsOf(v, globals); } catch ( ClassNotFoundException cnfe ) { return valueOf("\n\tno class '"+classname+"'" ); } catch ( Exception e ) { return valueOf("\n\tjava load failed on '"+classname+"', "+e ); } } }
@Override public boolean execute(CommandSender sender, String command, String[] args) { if (!this.testPermissionSilent(sender)) { sender.sendMessage(ERROR_NO_PERMISSION); return true; } try { if (args.length > maxArgs && maxArgs >= 0) { sender.sendMessage(ERROR_TOO_MANY_ARGS); } else if (args.length < minArgs) { sender.sendMessage(ERROR_MISSING_ARGS); } else { if (runAsync) { BukkitScheduler scheduler = Bukkit.getScheduler(); scheduler.runTaskAsynchronously(plugin, () -> function.invoke(new LuaValue[]{new CommandEvent(sender, command, args)})); } else { function.invoke(new LuaValue[]{new CommandEvent(sender, command, args)}); } } } catch (LukkitPluginException e) { e.printStackTrace(); LuaEnvironment.addError(e); } return true; }
int currentline() { if ( !f.isclosure() ) return -1; int[] li = f.checkclosure().p.lineinfo; return li==null || pc<0 || pc>=li.length? -1: li[pc]; } String sourceline() {
String sourceline() { if ( !f.isclosure() ) return f.tojstring(); return f.checkclosure().p.shortsource() + ":" + currentline(); } private int linedefined() {
static NameWhat getfuncname(DebugLib.CallFrame frame) { if (!frame.f.isclosure()) return new NameWhat(frame.f.classnamestub(), "Java"); Prototype p = frame.f.checkclosure().p; int pc = frame.pc; int i = p.code[pc]; /* calling instruction */
public void funcinfo(LuaFunction f) { if (f.isclosure()) { Prototype p = f.checkclosure().p; this.source = p.source != null ? p.source.tojstring() : "=?"; this.linedefined = p.linedefined; this.lastlinedefined = p.lastlinedefined; this.what = (this.linedefined == 0) ? "main" : "Lua"; this.short_src = p.shortsource(); } else { this.source = "=[Java]"; this.linedefined = -1; this.lastlinedefined = -1; this.what = "Java"; this.short_src = f.name(); } } }
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); } }