Giter Site home page Giter Site logo

androidlua's Introduction

Android Lua

Lua 的 Android 加强版实现,基于 Lua5.3.1 和 LuaJava,改进并增加了部分功能。

相对于LuaJava的改进

1.使用 "包名+类名" 访问 Java 类

import "bin.androidlua.TestObject"
test = TestObject()
-- 也可以写成
test = bin.androidlua.TestObject()

但不支持内部类,例如访问 TestObject.TwoMethod 时,得这么写

import "bin.androidlua.*"
TwoMethod = TestObject_TwoMethod -- TestObject$TwoMethod

包名的开头(bin、java、javax、com 等)需要在 LuaRunner.java 中添加

2.优化方法重载

在开始的测试中发现 Lua 中调用 Java 方法时,对于方法重载(即方法名相同但参数不同),匹配机制十分糟糕。

例如方法 a(Object) 和 a(String),参数传入 "abc" 时,两个方法都可以接受该参数,但明显应该匹配并调用 a(String) 才对。

而原先的匹配机制是按顺序匹配,然后调用第一个匹配成功的方法,只要 a(Object) 在 a(String) 前面,传入 "abc" 时就会调用成 a(Object) 了。

为此我设计了一套评分模式,具体实现可查看 AssignScorer.java

Lua 测试例子在 OverloadTest.lua

3.增加 Getter 和 Setter 功能

-- 对于 Java 对象 a

x = a.getName()
-- 可简写成
x = a.name

a.setName(x)
-- 可简写成
a.name = x

只做了 get 和 set,is 暂时没做,有兴趣的可以修改 LuaJavaAPI.java 中的 javaGetter 方法。

Lua 测试例子在 GetterSetterTest.lua

4.增加 Java 接口实现功能

通过 Lua 代码实现 Java 的接口,具体看 InterfaceTest.lua,已经写得很详细了。

5.增强异常处理功能

不论是 Lua 代码出现异常,还是调用 Java 代码时出现异常,你都可以在 Lua 或者 Java 中捕获它,并且可以得到异常时的调用栈。

模仿 Java 粗糙地做了 try 和 catch 功能,支持 throw 来重新抛出异常。

具体请参考 ErrorTest.lua 中的例子。

运行说明

我没有写 UI 界面,上面提到的例子均在单元测试 LuaJavaTest.java 中进行调用。

运行后请在 Logcat 查看输出,建议选择 No Filters 并输入 Lua 来过滤日志。

参考项目

androidlua's People

Contributors

l-jinbin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.