java启用lua的方式
摘要: version 3.0.1 /version /dependency 非常简单的启用方式String luaStr = print hello,world! Globals globals = JsePlatform.standardGlobals();LuaValue chunk = globals.load(luaStr);chunk.call();一个详细的检测类,包括lua启用...
非常简单的启用方式
String luaStr = print hello,world! Globals globals = JsePlatform.standardGlobals(); LuaValue chunk = globals.load(luaStr); chunk.call();
一个详细的检测类,包括lua启用java的方式
.junit.Test; .luaj.vm2.Globals; .luaj.vm2.LuaValue; .luaj.vm2.lib.OneArgFunction; .luaj.vm2.lib.TwoArgFunction; .luaj.vm2.lib.jse.JsePlatform; public class LuaTest { @Test public void Test1() { String s = //lua脚本制作 s += x=3\r\n s += y=4\r\n s += print ( hello world! )\r\n s += function aa()\r\n s += print ( aaa )\r\n s += end\r\n s += aa()\r\n s += c=method1(x)\r\n s += d=test.method2(x,y)\r\n s += print (x.. π= ..c)\r\n s += print ( x*y= ..d)\r\n Globals globals = JsePlatform.standardGlobals();//原始化lua globals.load(new TestMethod());//引入方式 LuaValue chunk = globals.load(s);//载入自身写的脚本制作 chunk.call();//实行脚本制作 String d = globals.get( d ).toString();//获得脚本制作里的自变量d的值 System.out.println( d: + d);
/** * The implementation of the TwoArgFunction interface. * This will be called once when the library is loaded via require(). * * @param modname LuaString containing the name used in the call to require(). * @param env LuaValue containing the environment for this function. * @return Value that will be returned in the require() call. In this case, * it is the library itself. */ public LuaValue call(LuaValue modname, LuaValue env) { //启用方法1 method1(x) env.set( method1 , new Method1()); env.set( method2 , new Method2()); //启用方法2 test.method1(x) LuaValue library = tableOf(); library.set( method1 , new Method1()); library.set( method2 , new Method2()); env.set( test , library); return null; } //一个主要参数的方式 class Method1 extends OneArgFunction { public LuaValue call(LuaValue x) { return LuaValue.valueOf(Math.PI * x.checkint()); } } //2个主要参数的方式 class Method2 extends TwoArgFunction { public LuaValue call(LuaValue x, LuaValue y) { return LuaValue.valueOf(x.checkint() * y.checkint()); } } } }
2016 嘿!编码 copy right
嘿!编码-测算机程序编写新手入门网站,关心手机软件开发设计全新技术性,收集企业网站建设受欢迎資源。
手机软件开发设计全集,企业网站建设手册,程序猿必上的程序编写新手入门网站。