luaj安卓框架
本项目由luaj的jse版本为基础修改而来。语法上与luaj并无不一样,主要修改了几处安卓平台上的引用错误。
文档
简单使用
引入库
compile 'com.reizx:luaj-android:3.0.4'
JAVA调用LUA例子
以下例子都会输出以luaj-tag为tag的日志。
执行文件(demo)
调用方法:
public void invokeFile() {
String path = "/sdcard/SimpleExample.lua";
// copy the script to path
ResourceUtils.copyFileFromAssets("SimpleExample.lua", path);
// init global before
// create an environment to run in
// Globals globals = JsePlatform.standardGlobals();
// Use the convenience function on Globals to load a chunk.
LuaValue chunk = globals.loadfile(path);
// Use any of the "call()" or "invoke()" functions directly on the chunk.
chunk.invoke();
}
SimpleExample.lua
---
--- 例子执行请执行rlua模块中AndroidTest的SimpleExample测试用例
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by kig.
--- DateTime: 2018/3/6 17:34
---
-- 引入类
LuajLog = luajava.bindClass("com.reizx.luaj.component.LuajLog") --Log打印类
-- 调用类的静态接口
LuajLog:d("hello luaj...1")
LuajLog:d("hello luaj...2")
LuajLog:d("hello luaj...3")
执行脚本流
public void invokeStream() {
String path = "/sdcard/SimpleExample.lua";//请查看"执行文件"中的SimpleExample.lua
// get the script InputStream
InputStream in = new ByteArrayInputStream(ResourceUtils.readAssets2String("SimpleExample.lua").getBytes());
// init global before
// create an environment to run in
// Globals globals = JsePlatform.standardGlobals();
// Use the convenience function on Globals to load a chunk.
/** Load the content form an input stream as a binary chunk or text file.
* @param is InputStream containing a lua script or compiled lua"
* @param chunkname Name that will be used within the chunk as the source.
* @param mode String containing 'b' or 't' or both to control loading as binary or text or either.
* @param environment LuaTable to be used as the environment for the loaded function.
* */
LuaValue chunk = globals.load(in, "@"+"Simple", "bt", globals);
// Use any of the "call()" or "invoke()" functions directly on the chunk.
chunk.invoke();
}
自定义接口
public void invokeCustom(){
Globals globals = customEvn();
// get the script InputStream
InputStream in = new ByteArrayInputStream(ResourceUtils.readAssets2String("hyperbolicapp.lua").getBytes());
LuaValue chunk = globals.load(in, "@"+"hyperbolicapp", "bt", globals);
// Use any of the "call()" or "invoke()" functions directly on the chunk.
chunk.invoke();
}
/**
* 自定义lua环境,可以自定义API
* @return
*/
public Globals customEvn(){
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 LuajavaLib());
//todo register library
globals.load(new hyperbolic());
LoadState.install(globals);
LuaC.install(globals);
return globals;
}
hyperbolicapp.lua
-- Sample luaj code to try loading and executing the 'hyperbolic' sample library
--
-- The sample library source is in examples/jse/hyperbolic.java.
-- For this sample to work, that source must be compiled, and the class must
-- be on the class path.
--
-- First load the library via require(). This will call the public constructor
-- for the class named 'hyperbolic' if it exists, and then initialize the
-- library by invoking LuaValue.call('hyperbolic') on that instance
require 'hyperbolic'
LuajLog = luajava.bindClass("com.reizx.luaj.component.LuajLog") --Log打印类
-- Test that the table is in the globals, and the functions exist.
-- LuajLog:d('hyperbolic : '..hyperbolic)
-- LuajLog:d('hyperbolic.sinh : '..hyperbolic.sinh)
-- LuajLog:d('hyperbolic.cosh : '..hyperbolic.cosh)
-- Try exercising the functions.
LuajLog:d('sinh(0.5) : '..hyperbolic.sinh(0.5))
LuajLog:d('cosh(0.5) : '..hyperbolic.cosh(0.5))
修改日志
修复classloader引用错误。