luaj web サイトから簡単な例を作成しました。LuaJ現在 使用されている現在のオブジェクトで関数を実行しようとしています。しかし、luaJ は新しいオブジェクトを作成しています。
新しいオブジェクトを作成するのではなく、現在のオブジェクトで関数を実行するにはどうすればよいですか。
次のコードを検討しています...
import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.TwoArgFunction;
import org.luaj.vm2.lib.ZeroArgFunction;
import org.luaj.vm2.lib.jse.JsePlatform;
public class luaTest extends TwoArgFunction {
public luaTest() {}
changeInt mytest=new changeInt();
public LuaValue call(LuaValue modname, LuaValue env) {
LuaValue library = tableOf();
library.set( "countup", new countup(mytest) );
env.set( "luaTest", library );
return library;
}
void run() {
Globals globals = JsePlatform.standardGlobals();
mytest.setA(10); // Setting it 10 before calling the script
LuaValue chunk = globals.loadfile("script.lua");
chunk.call();
}
class countup extends ZeroArgFunction {
changeInt mytest;
public countup(changeInt a)
{
mytest=a;
}
public LuaValue call() {
return LuaValue.valueOf(mytest.countup());
}
}
}
changeInt クラスは単純な 1 つの変数です...
public class changeInt {
int a = 1;
public int countup(){
return a++;
}
public void setA(int x)
{
a=x;
}
}
luaScript はシンプルです。
require 'luaTest'
print('count',luaTest.countup())
print('count',luaTest.countup())
print('count',luaTest.countup())
それを回避する方法はありますか..