Skip to content

Commit d199351

Browse files
committed
add lua lib
1 parent 9c52942 commit d199351

File tree

19 files changed

+1206
-1097
lines changed

19 files changed

+1206
-1097
lines changed

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ android {
1616
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
1717
}
1818
}
19+
20+
sourceSets {
21+
main {
22+
jniLibs.srcDirs = ['libs']
23+
}
24+
}
25+
1926
}
2027

2128
dependencies {

app/libs/arm64-v8a/libluajava.so

432 KB
Binary file not shown.

app/libs/armeabi-v7a/libluajava.so

485 KB
Binary file not shown.

app/libs/armeabi/libluajava.so

503 KB
Binary file not shown.

app/libs/mips/libluajava.so

576 KB
Binary file not shown.

app/libs/mips64/libluajava.so

424 KB
Binary file not shown.

app/libs/x86/libluajava.so

532 KB
Binary file not shown.

app/libs/x86_64/libluajava.so

407 KB
Binary file not shown.

app/src/main/java/com/example/zhangpeng/androidlua/MainActivity.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,39 @@
22

33
import android.support.v7.app.AppCompatActivity;
44
import android.os.Bundle;
5+
import android.util.Log;
56

6-
public class MainActivity extends AppCompatActivity {
7+
import org.keplerproject.luajava.LuaException;
8+
import org.keplerproject.luajava.LuaState;
9+
import org.keplerproject.luajava.LuaStateFactory;
710

11+
public class MainActivity extends AppCompatActivity {
12+
private LuaState mLuaState;//Lua解析和执行由此对象完成
813
@Override
914
protected void onCreate(Bundle savedInstanceState) {
1015
super.onCreate(savedInstanceState);
1116
setContentView(R.layout.activity_main);
17+
initLua();
18+
}
19+
20+
21+
22+
23+
/**
24+
* 只是在第一次调用,如果升级脚本也不需要重复初始化
25+
*/
26+
private void initLua(){
27+
mLuaState = LuaStateFactory.newLuaState();
28+
mLuaState.openLibs();
29+
//为了lua能使用系统日志,传入Log
30+
try {
31+
//push一个对象到对象到栈中
32+
mLuaState.pushObjectValue(Log.class);
33+
//设置为全局变量
34+
mLuaState.setGlobal("Log");
35+
} catch (LuaException e1) {
36+
// TODO Auto-generated catch block
37+
e1.printStackTrace();
38+
}
1239
}
1340
}

app/src/main/java/org/keplerproject/luajava/CPtr.java

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,44 +27,45 @@
2727
/**
2828
* An abstraction for a C pointer data type. A CPtr instance represents, on
2929
* the Java side, a C pointer. The C pointer could be any <em>type</em> of C
30-
* pointer.
30+
* pointer.
3131
*/
32-
public class CPtr {
33-
32+
public class CPtr
33+
{
34+
3435
/**
3536
* Compares this <code>CPtr</code> to the specified object.
3637
*
3738
* @param other a <code>CPtr</code>
38-
* @return true if the class of this <code>CPtr</code> object and the
39-
* class of <code>other</code> are exactly equal, and the C
40-
* pointers being pointed to by these objects are also
41-
* equal. Returns false otherwise.
39+
* @return true if the class of this <code>CPtr</code> object and the
40+
* class of <code>other</code> are exactly equal, and the C
41+
* pointers being pointed to by these objects are also
42+
* equal. Returns false otherwise.
4243
*/
43-
public boolean equals(Object other) {
44-
if (other == null)
45-
return false;
46-
if (other == this)
47-
return true;
48-
if (CPtr.class != other.getClass())
49-
return false;
50-
return peer == ((CPtr) other).peer;
51-
}
44+
public boolean equals(Object other)
45+
{
46+
if (other == null)
47+
return false;
48+
if (other == this)
49+
return true;
50+
if (CPtr.class != other.getClass())
51+
return false;
52+
return peer == ((CPtr)other).peer;
53+
}
5254

5355

5456
/* Pointer value of the real C pointer. Use long to be 64-bit safe. */
5557
private long peer;
56-
58+
5759
/**
5860
* Gets the value of the C pointer abstraction
59-
*
6061
* @return long
6162
*/
62-
protected long getPeer() {
63-
return peer;
63+
protected long getPeer()
64+
{
65+
return peer;
6466
}
6567

6668
/* No-args constructor. */
67-
CPtr() {
68-
}
69-
69+
CPtr() {}
70+
7071
}

app/src/main/java/org/keplerproject/luajava/Console.java

Lines changed: 55 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -24,65 +24,78 @@
2424

2525
package org.keplerproject.luajava;
2626

27+
2728
import java.io.BufferedReader;
2829
import java.io.InputStreamReader;
2930

3031
/**
3132
* Simple LuaJava console.
3233
* This is also an example on how to use the Java side of LuaJava and how to startup
3334
* a LuaJava application.
34-
*
35+
*
3536
* @author Thiago Ponte
3637
*/
37-
public class Console {
38+
public class Console
39+
{
3840

39-
/**
40-
* Creates a console for user interaction.
41-
*
42-
* @param args names of the lua files to be executed
43-
*/
44-
public static void main(String[] args) {
45-
try {
46-
LuaState L = LuaStateFactory.newLuaState();
47-
L.openLibs();
41+
/**
42+
* Creates a console for user interaction.
43+
*
44+
* @param args names of the lua files to be executed
45+
*/
46+
public static void main(String[] args)
47+
{
48+
try
49+
{
50+
LuaState L = LuaStateFactory.newLuaState();
51+
L.openLibs();
4852

49-
if (args.length > 0) {
50-
for (int i = 0; i < args.length; i++) {
51-
int res = L.LloadFile(args[i]);
52-
if (res == 0) {
53-
res = L.pcall(0, 0, 0);
54-
}
55-
if (res != 0) {
56-
throw new LuaException("Error on file: " + args[i] + ". " + L.toString(-1));
57-
}
58-
}
59-
60-
return;
53+
if (args.length > 0)
54+
{
55+
for (int i = 0; i < args.length; i++)
56+
{
57+
int res = L.LloadFile(args[i]);
58+
if (res == 0)
59+
{
60+
res = L.pcall(0, 0, 0);
61+
}
62+
if (res != 0)
63+
{
64+
throw new LuaException("Error on file: " + args[i] + ". " + L.toString(-1));
65+
}
6166
}
6267

63-
System.out.println("API Lua Java - console mode.");
68+
return;
69+
}
6470

65-
BufferedReader inp = new BufferedReader(new InputStreamReader(System.in));
71+
System.out.println("API Lua Java - console mode.");
6672

67-
String line;
73+
BufferedReader inp = new BufferedReader(new InputStreamReader(System.in));
6874

69-
System.out.print("> ");
70-
while ((line = inp.readLine()) != null && !line.equals("exit")) {
71-
int ret = L.LloadBuffer(line.getBytes(), "from console");
72-
if (ret == 0) {
73-
ret = L.pcall(0, 0, 0);
74-
}
75-
if (ret != 0) {
76-
System.err.println("Error on line: " + line);
77-
System.err.println(L.toString(-1));
78-
}
79-
System.out.print("> ");
75+
String line;
76+
77+
System.out.print("> ");
78+
while ((line = inp.readLine()) != null && !line.equals("exit"))
79+
{
80+
int ret = L.LloadBuffer(line.getBytes(), "from console");
81+
if (ret == 0)
82+
{
83+
ret = L.pcall(0, 0, 0);
84+
}
85+
if (ret != 0)
86+
{
87+
System.err.println("Error on line: " + line);
88+
System.err.println(L.toString(-1));
8089
}
90+
System.out.print("> ");
91+
}
8192

82-
L.close();
83-
} catch (Exception e) {
84-
e.printStackTrace();
85-
}
93+
L.close();
94+
}
95+
catch (Exception e)
96+
{
97+
e.printStackTrace();
98+
}
8699

87-
}
100+
}
88101
}

app/src/main/java/org/keplerproject/luajava/JavaFunction.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
package org.keplerproject.luajava;
2626

27+
2728
/**
2829
* JavaFunction is a class that can be used to implement a Lua function in Java.
2930
* JavaFunction is an abstract class, so in order to use it you must extend this

app/src/main/java/org/keplerproject/luajava/LuaException.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,29 @@
2626

2727
/**
2828
* LuaJava exception
29-
*
29+
*
3030
* @author Thiago Ponte
31+
*
3132
*/
32-
public class LuaException extends Exception {
33-
/**
34-
*
35-
*/
36-
private static final long serialVersionUID = 1L;
37-
38-
public LuaException(String str) {
39-
super(str);
40-
}
33+
public class LuaException extends Exception
34+
{
35+
/**
36+
*
37+
*/
38+
private static final long serialVersionUID = 1L;
4139

42-
/**
43-
* Will work only on Java 1.4 or later.
44-
* To work with Java 1.3, comment the first line and uncomment the second one.
45-
*/
46-
public LuaException(Exception e) {
47-
super((e.getCause() != null) ? e.getCause() : e);
48-
//super(e.getMessage());
49-
}
40+
public LuaException(String str)
41+
{
42+
super(str);
43+
}
44+
45+
/**
46+
* Will work only on Java 1.4 or later.
47+
* To work with Java 1.3, comment the first line and uncomment the second one.
48+
*/
49+
public LuaException(Exception e)
50+
{
51+
super((e.getCause() != null) ? e.getCause() : e);
52+
//super(e.getMessage());
53+
}
5054
}

0 commit comments

Comments
 (0)