Code Monkey home page Code Monkey logo

lua-for-android's Introduction

Build Status Download

General

This is a friendly and high performance bridge for java and lua in android. With it, you can call java functions, use java objects easily in lua. It supports auto method deduction, auto data conversion, import java package/class, multi-thread, exception handling, class extension/implementation and other facilitative features. See wiki for more detailed description.

Module luaffi, luasec, luasocket are optional package. And module lfs is embeded.

Both lua(5.1-5.3) and luajit(2.0.5 and 2.1.0-beta3) are supported.

Configure Android.mk by setting LUA_LIB :=(lua or luajit) to choose the proper version.

Currently, lua 5.3.5 is in lua dir and luajit 2.1.0 beta-3 is in luajit dir.

Load the lib by LibLoader.java in the module app or add System.loadLibrary("luadroid.so") to your code.

Module lib is the true library that you can embedded into your code.

Module app is a lua editor for running test in android.

Module luaffi is the ffi lib for luadroid to invoke c apis, and it requires luadroid.so to be statically linked

Performance

Under release mode, the bridge around 3+ times slower than jni because of consumption in method deduction and data conversion. Yet, if no argument provided, it is almost as fast as jni.

Around 2500000 method call(Math.abs) per second on my Oneplus 5 device. Around 1800000 member access plus with method call(Math.abs) per second. Note that if you run it in debug mode,CheckJni mode is enabled by the vm,and it will take away more than half of the efficiency.

Here is the test script:

using "java.lang"
--for method call test
local abs=Math.abs
local t=os.clock()
for i=1,2.5e6 do
    abs(0)
end
print(os.clock()-t)

--for member access test
t=os.clock()
for i=1,1.8e6 do
    Math.abs(0)
end
print(os.clock()-t)

Documentation

For documentation,see wiki.

Email:

[email protected]

lua-for-android's People

Contributors

qtiuto avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

lua-for-android's Issues

How to import/use static inner class

How to import or use static inner class like android.view.ViewGroup.LayoutParams

I have try:

import "android.view.ViewGroup.LayoutParams"
-- showing error like "no static field LayoutParams"

import "android.view.ViewGroup$LayoutParams"
-- showing error like "error field ... ViewGroup$LayoutParams"

import "android.view.ViewGroup"
print(ViewGroup.LayoutParams)
-- showing error like "no static field LayoutParams"

There is an update from LuaJIT missing

Hello !
In file dasm_x86.lua there is an update from LuaJIT missing:

  vroundpd_3 =	"rmioy:660F3AV09rMU",
  vroundps_3 =	"rmioy:660F3AV08rMU",

Is now in LuaJIT:

  vroundpd_3 =	"rmioy:660F3Au09rMU",
  vroundps_3 =	"rmioy:660F3Au08rMU",

Cheers !

Now there is an error with meta methods

Hello !
Thanks for fix the issues reported before.
But now with the changes you've made to the meta methods it compiles fine when applied to the original lua-ffi but when try to test it "lua test.lua" we get this error:

ljs: test.ljs:685: type struct vls has no member key
stack traceback:
	[C]: in metamethod '__index'
	test.ljs:685: in main chunk
	[C]: in ?

Note I'm using it with https://github.com/mingodad/ljs but otherwise it's the same in Lua. And it was working before the latest changes.

Cheers !

compile faild

In file included from E:/Project/Android/lua-for-android-1.0/lib/src/main/cpp/java_type.cpp:2:
In file included from E:/Project/Android/lua-for-android-1.0/lib/src/main/cpp/java_type.h:7:
In file included from E:/Project/Android/lua-for-android-1.0/lib/src/main/cpp/myarray.h:7:
In file included from E:/Project/Android/lua-for-android-1.0/lib/src/main/cpp/java_type.h:7:
In file included from E:/Project/Android/lua-for-android-1.0/lib/src/main/cpp/common.h:10:
In file included from E:/Project/Android/lua-for-android-1.0/lib/src/main/cpp/myarray.h:7:
In file included from E:/Project/Android/lua-for-android-1.0/lib/src/main/cpp/macros.h:10:
In file included from E:/Project/Android/lua-for-android-1.0/lib/src/main/cpp/common.h:10:
E:/Project/Android/lua-for-android-1.0/lib/src/main/cpp/stdcpp/functional:217:60: error: reference to 'pair' is ambiguous
inline bool operator==(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)

Code formating

Hello !
I've noticed that you did some code reformatting on some dynasm files making it a bit hard to see what you've changed when comparing with LuaJIT, was it intentional ?
Do you plan to submit your changes back to LuaJIT or your changes are only specific to use with Lua ?
Cheers !

Error when building

root@localhost:~/lua-for-android# ./gradlew --stacktrace
                                                                  FAILURE: Build failed with an exception.

* What went wrong:                                                Could not initialize class org.codehaus.groovy.runtime.InvokerHelper
> Exception java.lang.NoClassDefFoundError: Could not initialize class org.codehaus.groovy.reflection.ReflectionCache [in thread "Daemon worker"]

* Try:
Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Exception is:
java.lang.NoClassDefFoundError: Could not initialize class org.codehaus.groovy.runtime.InvokerHelper
        at org.gradle.internal.extensibility.DefaultExtraPropertiesExtension.<init>(DefaultExtraPropertiesExtension.java:29)
        at org.gradle.internal.extensibility.DefaultConvention.<init>(DefaultConvention.java:48)
        at org.gradle.api.internal.plugins.DefaultConvention.<init>(DefaultConvention.java:28)
        at org.gradle.internal.extensibility.ExtensibleDynamicObject.<init>(ExtensibleDynamicObject.java:60)
        at org.gradle.internal.instantiation.MixInExtensibleDynamicObject.<init>(MixInExtensibleDynamicObject.java:34)
        at org.gradle.initialization.DefaultSettings_Decorated.getAsDynamicObject(Unknown Source)
        at org.gradle.initialization.SettingsFactory.createSettings(SettingsFactory.java:58)
        at org.gradle.initialization.ScriptEvaluatingSettingsProcessor.process(ScriptEvaluatingSettingsProcessor.java:60)
        at org.gradle.initialization.PropertiesLoadingSettingsProcessor.process(PropertiesLoadingSettingsProcessor.java:37)
        at org.gradle.initialization.SettingsEvaluatedCallbackFiringSettingsProcessor.process(SettingsEvaluatedCallbackFiringSettingsProcessor.java:34)
        at org.gradle.initialization.RootBuildCac...

Idk is it how it builds, bcs i didn't found any guide

Error on arm32

Hello !
Testing your project with with the apk on android arm32 (nexus7) this program fail:

local ffi = require('ffi')
local buf = ffi.new('char[256]')
local c = ffi.C
ffi.cdef('int sprintf(char *buf, const char *fmt, ...);')
print(c.sprintf(buf, '%g', 5.3), ffi.string(buf));

Expected:

3    5.3

Output:

11   2.14609e+41

The above and the following are from the original test.lua and it also fail the following with segfault:

-- unsigned should be ignored for pointer rules
ffi.cdef([=[
int strncmp(const signed char *s1, const unsigned char *s2, size_t n);
]=]);
assert(ffi.C.strncmp("two", "three", 3) ~= 0); -- segfault here
...
-- Test io.tmpfile()
ffi.cdef ([=[
    int fprintf ( FILE * stream, const char * format, ... );
]=]);
local f = io.tmpfile();
ffi.C.fprintf(f, "test: %s\n", "foo"); -- segfault here

f:seek("set", 0);
local str = f:read('*l');
assert(str == 'test: foo', str);
f:close();

Commenting the above mentioned tests all the rest of test.lua pass.
Great work !

No original dex files found for dex location /system/framework/mms-common.jar

image

With the code.

findViewById(R.id.test).setOnClickListener(new View.OnClickListener() {
@OverRide
public void onClick(View v) {
executeLua("using "java.lang"\n" +
"--for method call test\n" +
"local abs=Math.abs\n" +
"local t=os.clock()\n" +
"for i=1,2.5e6 do\n" +
" abs(0)\n" +
"end\n" +
"print(os.clock()-t)\n" +
"\n" +
"--for member access test\n" +
"t=os.clock()\n" +
"for i=1,1.8e6 do\n" +
" Math.abs(0)\n" +
"end\n" +
"print(os.clock()-t)");
}
});
}
private void executeLua(String str){
ScriptContext context = new ScriptContext();
context.addToLua("context", this);
context.run(str);
}

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.