luaj / luaj

Lightweight, fast, Java-centric Lua interpreter written for JME and JSE, with string, table, package, math, io, os, debug, coroutine & luajava libraries, JSR-223 bindings, all metatags, weak tables and unique direct lua-to-java-bytecode compiling.
http://luaj.sourceforge.net/
MIT License
919 stars 175 forks source link

Luajavalib #75

Open Nogitsu opened 4 years ago

Nogitsu commented 4 years ago

I can't find any references or help for the Luajavalib, I only have the things from http://luaj.org/luaj/3.0/api/index.html but that's not helping me a lot..

jplwill commented 4 years ago

See the swing example at https://github.com/luaj/luaj/blob/master/examples/lua/swingapp.lua, and the docs for CoerceJavaToLua.coerce(). I'm not seeing anything else. (Not an expert, me, started looking at LuaJ last week.)

Nogitsu commented 4 years ago

I started looking at it in the same period, I looked at it but it's not worth a wiki or documentation..

zhuxl13817468272 commented 2 years ago

In Java, my solution to this problem is override JsePlatform and LuajavaLib

public class JsePlatformFix extends JsePlatform{

public static Globals standardGlobals() {
    Globals globals = new Globals();
    globals.load(new JseBaseLib());
    globals.load(new PackageLib());
    globals.load(new Bit32Lib());
    globals.load(new TableLib());
    globals.load(new StringLib());
    globals.load(new CoroutineLib());
    globals.load(new JseMathLib());
    globals.load(new JseIoLib());
    globals.load(new JseOsLib());
    globals.load(new LuajavaLibFix());
    LoadState.install(globals);
    LuaC.install(globals);
    return globals;
}

public class LuajavaLibFix extends LuajavaLib {

public LuajavaLibFix() {}

// load classes using app loader to allow luaj to be used as an extension
@Override
protected Class classForName(String name) throws ClassNotFoundException {
    return Class.forName(name, true, Thread.currentThread().getContextClassLoader());
}

}

Globals globals = JsePlatformFix.standardGlobals(); LuaValue load = globals.load(luaScript); LuaValue call = load.call();

In LuaScript, use like that : local instance = luajava.newInstance('com.xxx.xxx')

In practice, it can execute successful. Wish can help you