org.luaj.vm2.lib.jse.CoerceJavaToLua Java Examples

The following examples show how to use org.luaj.vm2.lib.jse.CoerceJavaToLua. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LuaUtil.java    From VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
public static Varargs callFunction(LuaValue target, Object... objs) {
    if (target != null && target.isfunction()) {
        LuaValue[] args = null;
        if (objs != null && objs.length > 0) {
            args = new LuaValue[objs.length];

            for (int i = 0; i < objs.length; i++) {
                args[i] = CoerceJavaToLua.coerce(objs[i]);
            }
        }
        if (args != null) {
            return target.invoke(LuaValue.varargsOf(args));
        } else {
            return target.call();
        }
    }
    return LuaValue.NIL;
}
 
Example #2
Source File: LukkitPlugin.java    From Lukkit with MIT License 6 votes vote down vote up
@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)
            ));
}
 
Example #3
Source File: LukkitCommand.java    From Lukkit with MIT License 6 votes vote down vote up
@Override
public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
    List<String> def = super.tabComplete(sender, alias, args);

    if (tabComleteFunction != null) {
        LuaValue val = tabComleteFunction.invoke(
                CoerceJavaToLua.coerce(sender),
                CoerceJavaToLua.coerce(alias),
                CoerceJavaToLua.coerce(args)
        ).arg1();
        if (val != LuaValue.NIL) {
            LuaTable tbl = val.checktable();
            Object o = Utilities.convertTable(tbl);
            if (o instanceof List)
                return (List<String>) o;
        }
    }
    return def;
}
 
Example #4
Source File: LuaViewCore.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 注册一个名称到该lua对象的命名空间中
 *
 * @param luaName
 * @param obj
 * @return
 */
public synchronized LuaViewCore register(String luaName, Object obj) {
    if (mGlobals != null && !TextUtils.isEmpty(luaName)) {
        LuaValue value = mGlobals.get(luaName);
        if (obj != value) {
            mGlobals.set(luaName, CoerceJavaToLua.coerce(obj));
        }
    }
    return this;
}
 
Example #5
Source File: LukkitPlugin.java    From Lukkit with MIT License 5 votes vote down vote up
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;
}
 
Example #6
Source File: LukkitPlugin.java    From Lukkit with MIT License 5 votes vote down vote up
@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();
    utilitiesWrapper.close();
}
 
Example #7
Source File: LuaEngine.java    From remixed-dungeon with GNU General Public License v3.0 5 votes vote down vote up
public LuaValue call(String method, Object arg1) {
	try {
		LuaValue methodForData = globals.get(method);
		return methodForData.call(CoerceJavaToLua.coerce(arg1));
	} catch (LuaError err) {
		reportLuaError(err);
	}
	return LuaValue.NIL;
}
 
Example #8
Source File: Trap.java    From remixed-dungeon with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void doTrigger(int cell, Char ch) {
	LuaTable trap = LuaEngine.getEngine().call("require", scriptFile).checktable();

	trap.get("setData").call(trap,LuaValue.valueOf(data));
	trap.get("trigger").call(trap,LuaValue.valueOf(cell), CoerceJavaToLua.coerce(ch));
}
 
Example #9
Source File: LuaScript.java    From remixed-dungeon with GNU General Public License v3.0 5 votes vote down vote up
public <T> T runOptional(String method, T defaultValue, Object... args) {
    try {
        if (!getScript().get(method).isfunction()) {
            return defaultValue;
        }

        int startIndex = 1;

        if(parent==null) {
            startIndex = 0;
        }

        LuaValue []luaArgs = new LuaValue[args.length+startIndex];

        if(parent!=null) {
            luaArgs[0] = CoerceJavaToLua.coerce(parent);
        }


        for (int i = startIndex;i<luaArgs.length;++i) {
            luaArgs[i] = CoerceJavaToLua.coerce(args[i-startIndex]);
        }

        if(defaultValue==null) {
            run(method, luaArgs);
            return null;
        }

        return (T) CoerceLuaToJava.coerce(
                run(method, luaArgs),
                defaultValue.getClass());
    } catch (LuaError e) {
        throw new ModError("Error when call:" + method+"."+scriptFile,e);
    }
}
 
Example #10
Source File: LuaScript.java    From remixed-dungeon with GNU General Public License v3.0 5 votes vote down vote up
public LuaValue run(String method, Object arg1, Object arg2, Object arg3) {
    return run(method,new LuaValue[]{
            CoerceJavaToLua.coerce(parent),
            CoerceJavaToLua.coerce(arg1),
            CoerceJavaToLua.coerce(arg2),
            CoerceJavaToLua.coerce(arg3)});
}
 
Example #11
Source File: LuaScript.java    From remixed-dungeon with GNU General Public License v3.0 5 votes vote down vote up
public LuaValue run(String method, Object arg1, Object arg2) {
    if(parent!=null) {
        return run(method, new LuaValue[]{
                CoerceJavaToLua.coerce(parent),
                CoerceJavaToLua.coerce(arg1),
                CoerceJavaToLua.coerce(arg2)});
    } else {
        return run(method, new LuaValue[]{
                CoerceJavaToLua.coerce(arg1),
                CoerceJavaToLua.coerce(arg2)});
    }
}
 
Example #12
Source File: LuaScript.java    From remixed-dungeon with GNU General Public License v3.0 5 votes vote down vote up
public LuaValue run(String method, Object arg1) {
    if(parent!=null) {
        return run(method, new LuaValue[]{
                CoerceJavaToLua.coerce(parent),
                CoerceJavaToLua.coerce(arg1)});
    } else {
        return run(method, new LuaValue[]{
                CoerceJavaToLua.coerce(arg1)});
    }
}
 
Example #13
Source File: DefaultLauncher.java    From luaj with MIT License 5 votes vote down vote up
private Object[] launchChunk(LuaValue chunk, Object[] arg) {
	LuaValue args[] = new LuaValue[arg.length];
	for (int i = 0; i < args.length; ++i)
		args[i] = CoerceJavaToLua.coerce(arg[i]);
	Varargs results = chunk.invoke(LuaValue.varargsOf(args));

	final int n = results.narg();
	Object return_values[] = new Object[n];
	for (int i = 0; i < n; ++i) {
		LuaValue r = results.arg(i+1);
		switch (r.type()) {
		case LuaValue.TBOOLEAN:
			return_values[i] = r.toboolean();
			break;
		case LuaValue.TNUMBER:
			return_values[i] = r.todouble();
			break;
		case LuaValue.TINT:
			return_values[i] = r.toint();
			break;
		case LuaValue.TNIL:
			return_values[i] = null;
			break;
		case LuaValue.TSTRING:
			return_values[i] = r.tojstring();
			break;
		case LuaValue.TUSERDATA:
			return_values[i] = r.touserdata();
			break;
		default:
			return_values[i] = r;
		}
	}
	return return_values;
}
 
Example #14
Source File: LuajActivity.java    From luaj with MIT License 5 votes vote down vote up
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	LuajView view = new LuajView(this);
	setContentView(view);
	try {
		LuaValue activity = CoerceJavaToLua.coerce(this);
		LuaValue viewobj = CoerceJavaToLua.coerce(view);
		view.globals.loadfile("activity.lua").call(activity, viewobj);
	} catch ( Exception e ) {
		e.printStackTrace();
	}
}
 
Example #15
Source File: LuajView.java    From luaj with MIT License 5 votes vote down vote up
public void onWindowSystemUiVisibilityChanged(int visible) {
	LuaValue f = globals.get("onWindowSystemUiVisibilityChanged");
	if (!f.isnil())
		try {
			f.call(CoerceJavaToLua.coerce(visible));
		} catch (Exception e) {
			e.printStackTrace();
		}
}
 
Example #16
Source File: LuajView.java    From luaj with MIT License 5 votes vote down vote up
public void onWindowFocusChanged(boolean hasWindowFocus) {
	LuaValue f = globals.get("onWindowFocusChanged");
	if (!f.isnil())
		try {
			f.call(CoerceJavaToLua.coerce(hasWindowFocus));
		} catch (Exception e) {
			e.printStackTrace();
		}
}
 
Example #17
Source File: LuajView.java    From luaj with MIT License 5 votes vote down vote up
public boolean onTrackballEvent(MotionEvent event) {
	LuaValue f = globals.get("onTrackballEvent");
	if (!f.isnil())
		try {
			return f.call(CoerceJavaToLua.coerce(event)).toboolean();
		} catch (Exception e) {
			e.printStackTrace();
			return true;
		}
	else
		return super.onTrackballEvent(event);
}
 
Example #18
Source File: LuajView.java    From luaj with MIT License 5 votes vote down vote up
public boolean onTouchEvent(MotionEvent event) {
	LuaValue f = globals.get("onTouchEvent");
	if (!f.isnil())
		try {
			return f.call(CoerceJavaToLua.coerce(event)).toboolean();
		} catch (Exception e) {
			e.printStackTrace();
			return true;
		}
	else
		return super.onTouchEvent(event);
}
 
Example #19
Source File: LuajView.java    From luaj with MIT License 5 votes vote down vote up
public boolean onKeyUp(int keyCode, KeyEvent event) {
	LuaValue f = globals.get("onKeyUp");
	if (!f.isnil())
		try {
			return f.call(CoerceJavaToLua.coerce(keyCode),
					CoerceJavaToLua.coerce(event)).toboolean();
		} catch (Exception e) {
			e.printStackTrace();
			return true;
		}
	else
		return super.onKeyUp(keyCode, event);
}
 
Example #20
Source File: LuajView.java    From luaj with MIT License 5 votes vote down vote up
public void draw(Canvas canvas) {
	LuaValue f = globals.get("draw");
	if (!f.isnil())
		try {
			f.call(CoerceJavaToLua.coerce(canvas));
		} catch (Exception e) {
			e.printStackTrace();
		}
	else
		super.draw(canvas);
}
 
Example #21
Source File: SampleApplet.java    From luaj with MIT License 5 votes vote down vote up
public void paint(Graphics g) {
	LuaValue p = globals.get("paint");
	if (!p.isfunction())
		super.paint(g);
	else
		pcall.call(
				p,
				coerced == g ? graphics : (graphics = CoerceJavaToLua
						.coerce(coerced = g)));
}
 
Example #22
Source File: SampleApplet.java    From luaj with MIT License 5 votes vote down vote up
public void update(Graphics g) {
	LuaValue u = globals.get("update");
	if (!u.isfunction())
		super.update(g);
	else
		pcall.call(
				u,
				coerced == g ? graphics : (graphics = CoerceJavaToLua
						.coerce(coerced = g)));
}
 
Example #23
Source File: SampleApplet.java    From luaj with MIT License 5 votes vote down vote up
public void init() {
	// Find the script to load from the applet parameters.
	String script = getParameter(script_parameter);
	if (script == null)
		script = default_script;
	System.out.println("Loading " + script);

	// Construct globals specific to the applet platform.
	globals = new Globals();
	globals.load(new JseBaseLib());
	globals.load(new PackageLib());
	globals.load(new Bit32Lib());
	globals.load(new TableLib());
	globals.load(new JseStringLib());
	globals.load(new CoroutineLib());
	globals.load(new JseMathLib());
	globals.load(new JseIoLib());
	globals.load(new JseOsLib());
	globals.load(new AppletLuajavaLib());
	LoadState.install(globals);
	LuaC.install(globals);

	// Use custom resource finder.
	globals.finder = this;

	// Look up and save the handy pcall method.
	pcall = globals.get("pcall");

	// Load and execute the script, supplying this Applet as the only
	// argument.
	globals.loadfile(script).call(CoerceJavaToLua.coerce(this));
}
 
Example #24
Source File: LuaViewCore.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
/**
 * load prototype (lua bytecode or sourcecode)
 *
 * @param inputStream
 * @return
 */
public LuaViewCore loadPrototype(final InputStream inputStream, final String name, final LuaScriptLoader.ScriptExecuteCallback callback) {
    new SimpleTask1<LuaValue>() {
        @Override
        protected LuaValue doInBackground(Object... params) {
            try {
                if (mGlobals != null) {
                    Prototype prototype = mGlobals.loadPrototype(inputStream, name, "bt");
                    if (prototype != null) {
                        return mGlobals.load(prototype, name);
                    }
                }
            } catch (IOException e) {
            }
            return null;
        }

        @Override
        protected void onPostExecute(LuaValue value) {
            LuaValue activity = CoerceJavaToLua.coerce(mContext);
            LuaValue viewObj = CoerceJavaToLua.coerce(LuaViewCore.this);
            if (callback == null || !callback.onScriptCompiled(value, activity, viewObj)) {
                executeScript(value, activity, viewObj, callback);
            }
        }
    }.executeInPool();
    return this;
}
 
Example #25
Source File: LuaUtil.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
/**
 * convert object to LuaValue
 *
 * @param value
 * @return
 */
public static LuaValue toLuaValue(Object value) {
    try {
        if (value instanceof Integer) {
            return LuaValue.valueOf((Integer) value);
        } else if (value instanceof Long) {
            return LuaValue.valueOf((Long) value);
        } else if (value instanceof Double) {
            return LuaValue.valueOf((Double) value);
        } else if (value instanceof String) {
            return LuaValue.valueOf((String) value);
        } else if (value instanceof Boolean) {
            return LuaValue.valueOf((Boolean) value);
        } else if (value instanceof byte[]) {
            return LuaValue.valueOf((byte[]) value);
        } else if (value instanceof List) {
            return toTable((List) value);
        } else if (value instanceof Map) {
            return toTable((Map) value);
        } else {
            return CoerceJavaToLua.coerce(value);
        }
    } catch (Exception e) {

        return LuaValue.NIL;
    }
}
 
Example #26
Source File: UDView.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
/**
 * get native view
 *
 * @return
 */
public LuaValue getNativeView() {
    View view = getView();
    if (view instanceof ILVNativeViewProvider) {
        view = ((ILVNativeViewProvider) view).getNativeView();
    }
    return view != null ? CoerceJavaToLua.coerce(view) : LuaValue.NIL;
}
 
Example #27
Source File: YamlStorage.java    From Lukkit with MIT License 4 votes vote down vote up
@Override
public LuaValue getValue(LuaString path) throws StorageObjectException {
    return CoerceJavaToLua.coerce(this.yamlConfiguration.get(path.tojstring()));
}
 
Example #28
Source File: JsonStorage.java    From Lukkit with MIT License 4 votes vote down vote up
@Override
public LuaValue getValue(LuaString path) throws StorageObjectException {
    return CoerceJavaToLua.coerce(object.get(path.checkjstring()));
}
 
Example #29
Source File: SkullWrapper.java    From Lukkit with MIT License 4 votes vote down vote up
public SkullWrapper(ItemStack skullItem) {
    this.skull = (skullItem == null) ? new ItemStack(Material.SKULL_ITEM, 1) : skull;
    this.self = this; // Allows access from setOwner() since this is populate by the function.

    if (skullItem == null) {
        this.skull.setDurability((short) 3);
        this.meta = (SkullMeta) Bukkit.getItemFactory().getItemMeta(Material.SKULL_ITEM);
    } else {
        this.meta = (SkullMeta) this.skull.getItemMeta();
    }

    this.set("setOwner", new OneArgFunction() {
        @Override
        public LuaValue call(LuaValue username) {
            meta.setOwner(username.checkjstring());
            return CoerceJavaToLua.coerce(self);
        }
    });

    this.set("hasOwner", new ZeroArgFunction() {
        @Override
        public LuaValue call() {
            return LuaValue.valueOf(meta.hasOwner());
        }
    });

    this.set("getOwner", new ZeroArgFunction() {
        @Override
        public LuaValue call() {
            if (meta.getOwner() == null) {
                return LuaValue.NIL;
            }
            return LuaValue.valueOf(meta.getOwner());
        }
    });

    this.set("getItemStack", new ZeroArgFunction() {
        @Override
        public LuaValue call() {
            skull.setItemMeta(meta);
            return CoerceJavaToLua.coerce(skull);
        }
    });
}
 
Example #30
Source File: CommandEvent.java    From Lukkit with MIT License 4 votes vote down vote up
public CommandEvent(CommandSender sender, String command, String... args) {


        this.set("isPlayerSender", new ZeroArgFunction() {
            @Override
            public LuaValue call() {
                return sender instanceof Player ? TRUE : FALSE;
            }
        });

        this.set("isConsoleSender", new ZeroArgFunction() {
            @Override
            public LuaValue call() {
                return sender instanceof ConsoleCommandSender ? TRUE : FALSE;
            }
        });

        this.set("isBlockSender", new ZeroArgFunction() {
            @Override
            public LuaValue call() {
                return sender instanceof BlockCommandSender ? TRUE : FALSE;
            }
        });

        this.set("isEntitySender", new ZeroArgFunction() {
            @Override
            public LuaValue call() {
                return sender instanceof Entity ? TRUE : FALSE;
            }
        });

        this.set("getSender", new ZeroArgFunction() {
            @Override
            public LuaValue call() {
                return CoerceJavaToLua.coerce(sender);
            }
        });

        this.set("getArgs", new ZeroArgFunction() {
            @Override
            public LuaValue call() {
                LuaTable tbl = new LuaTable();
                for (int i = 0; i < args.length; i++)
                    tbl.set(i + 1, args[i]);
                return tbl;
            }
        });

        this.set("getCommand", new ZeroArgFunction() {
            @Override
            public LuaValue call() {
                return CoerceJavaToLua.coerce(command);
            }
        });

    }