Skip to content

Commit 5ae315c

Browse files
committed
lua lib ok
1 parent d199351 commit 5ae315c

File tree

18 files changed

+1099
-1173
lines changed

18 files changed

+1099
-1173
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.

README.MD

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
学习 Android 和lua 互相调用
2-
使用方式和 api 熟悉
1+
use in blog
2+
自己学习 Android 和lua 互相调用
3+
使用方式和 api 熟悉

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/org/keplerproject/luajava/CPtr.java

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,45 +27,44 @@
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-
{
34-
32+
public class CPtr {
33+
3534
/**
3635
* Compares this <code>CPtr</code> to the specified object.
3736
*
3837
* @param other a <code>CPtr</code>
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.
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.
4342
*/
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-
}
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+
}
5452

5553

5654
/* Pointer value of the real C pointer. Use long to be 64-bit safe. */
5755
private long peer;
58-
56+
5957
/**
6058
* Gets the value of the C pointer abstraction
59+
*
6160
* @return long
6261
*/
63-
protected long getPeer()
64-
{
65-
return peer;
62+
protected long getPeer() {
63+
return peer;
6664
}
6765

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

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

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

2525
package org.keplerproject.luajava;
2626

27-
2827
import java.io.BufferedReader;
2928
import java.io.InputStreamReader;
3029

3130
/**
3231
* Simple LuaJava console.
3332
* This is also an example on how to use the Java side of LuaJava and how to startup
3433
* a LuaJava application.
35-
*
34+
*
3635
* @author Thiago Ponte
3736
*/
38-
public class Console
39-
{
37+
public class Console {
4038

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();
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();
5248

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-
}
66-
}
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+
}
6759

68-
return;
69-
}
60+
return;
61+
}
7062

71-
System.out.println("API Lua Java - console mode.");
63+
System.out.println("API Lua Java - console mode.");
7264

73-
BufferedReader inp = new BufferedReader(new InputStreamReader(System.in));
65+
BufferedReader inp = new BufferedReader(new InputStreamReader(System.in));
7466

75-
String line;
67+
String line;
7668

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));
89-
}
9069
System.out.print("> ");
91-
}
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("> ");
80+
}
9281

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

100-
}
87+
}
10188
}

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

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

2525
package org.keplerproject.luajava;
2626

27-
2827
/**
2928
* JavaFunction is a class that can be used to implement a Lua function in Java.
3029
* 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: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,25 @@
2626

2727
/**
2828
* LuaJava exception
29-
*
30-
* @author Thiago Ponte
3129
*
30+
* @author Thiago Ponte
3231
*/
33-
public class LuaException extends Exception
34-
{
35-
/**
36-
*
37-
*/
38-
private static final long serialVersionUID = 1L;
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+
}
3941

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-
}
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+
}
5450
}

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

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
package org.keplerproject.luajava;
2626

27-
2827
import java.lang.reflect.InvocationHandler;
2928
import java.lang.reflect.Method;
3029

@@ -33,53 +32,45 @@
3332
* This class is used in the LuaJava's proxy system.
3433
* When a proxy object is accessed, the method invoked is
3534
* called from Lua
35+
*
3636
* @author Rizzato
3737
* @author Thiago Ponte
3838
*/
39-
public class LuaInvocationHandler implements InvocationHandler
40-
{
41-
private LuaObject obj;
39+
public class LuaInvocationHandler implements InvocationHandler {
40+
private LuaObject obj;
41+
42+
43+
public LuaInvocationHandler(LuaObject obj) {
44+
this.obj = obj;
45+
}
46+
47+
/**
48+
* Function called when a proxy object function is invoked.
49+
*/
50+
public Object invoke(Object proxy, Method method, Object[] args) throws LuaException {
51+
synchronized (obj.L) {
52+
String methodName = method.getName();
53+
LuaObject func = obj.getField(methodName);
4254

55+
if (func.isNil()) {
56+
return null;
57+
}
4358

44-
public LuaInvocationHandler(LuaObject obj)
45-
{
46-
this.obj = obj;
47-
}
59+
Class retType = method.getReturnType();
60+
Object ret;
4861

49-
/**
50-
* Function called when a proxy object function is invoked.
51-
*/
52-
public Object invoke(Object proxy, Method method, Object[] args) throws LuaException
53-
{
54-
synchronized(obj.L)
55-
{
56-
String methodName = method.getName();
57-
LuaObject func = obj.getField(methodName);
58-
59-
if ( func.isNil() )
60-
{
61-
return null;
62-
}
63-
64-
Class retType = method.getReturnType();
65-
Object ret;
62+
// Checks if returned type is void. if it is returns null.
63+
if (retType.equals(Void.class) || retType.equals(void.class)) {
64+
func.call(args, 0);
65+
ret = null;
66+
} else {
67+
ret = func.call(args, 1)[0];
68+
if (ret != null && ret instanceof Double) {
69+
ret = LuaState.convertLuaNumber((Double) ret, retType);
70+
}
71+
}
6672

67-
// Checks if returned type is void. if it is returns null.
68-
if ( retType.equals( Void.class ) || retType.equals( void.class ) )
69-
{
70-
func.call( args , 0 );
71-
ret = null;
72-
}
73-
else
74-
{
75-
ret = func.call(args, 1)[0];
76-
if( ret != null && ret instanceof Double)
77-
{
78-
ret = LuaState.convertLuaNumber((Double) ret, retType);
79-
}
80-
}
81-
82-
return ret;
83-
}
84-
}
73+
return ret;
74+
}
75+
}
8576
}

0 commit comments

Comments
 (0)