Skip to content

Commit 9c52942

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

File tree

17 files changed

+2707
-0
lines changed

17 files changed

+2707
-0
lines changed

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/libs/arm64-v8a/libluajava.so

284 KB
Binary file not shown.

app/libs/armeabi-v7a/libluajava.so

139 KB
Binary file not shown.

app/libs/armeabi/libluajava.so

143 KB
Binary file not shown.

app/libs/mips/libluajava.so

392 KB
Binary file not shown.

app/libs/mips64/libluajava.so

407 KB
Binary file not shown.

app/libs/x86/libluajava.so

283 KB
Binary file not shown.

app/libs/x86_64/libluajava.so

276 KB
Binary file not shown.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* $Id: CPtr.java,v 1.4 2006/12/22 14:06:40 thiago Exp $
3+
* Copyright (C) 2003-2007 Kepler Project.
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining
6+
* a copy of this software and associated documentation files (the
7+
* "Software"), to deal in the Software without restriction, including
8+
* without limitation the rights to use, copy, modify, merge, publish,
9+
* distribute, sublicense, and/or sell copies of the Software, and to
10+
* permit persons to whom the Software is furnished to do so, subject to
11+
* the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be
14+
* included in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20+
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23+
*/
24+
25+
package org.keplerproject.luajava;
26+
27+
/**
28+
* An abstraction for a C pointer data type. A CPtr instance represents, on
29+
* the Java side, a C pointer. The C pointer could be any <em>type</em> of C
30+
* pointer.
31+
*/
32+
public class CPtr {
33+
34+
/**
35+
* Compares this <code>CPtr</code> to the specified object.
36+
*
37+
* @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.
42+
*/
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+
}
52+
53+
54+
/* Pointer value of the real C pointer. Use long to be 64-bit safe. */
55+
private long peer;
56+
57+
/**
58+
* Gets the value of the C pointer abstraction
59+
*
60+
* @return long
61+
*/
62+
protected long getPeer() {
63+
return peer;
64+
}
65+
66+
/* No-args constructor. */
67+
CPtr() {
68+
}
69+
70+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* $Id: Console.java,v 1.7 2006/12/22 14:06:40 thiago Exp $
3+
* Copyright (C) 2003-2007 Kepler Project.
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining
6+
* a copy of this software and associated documentation files (the
7+
* "Software"), to deal in the Software without restriction, including
8+
* without limitation the rights to use, copy, modify, merge, publish,
9+
* distribute, sublicense, and/or sell copies of the Software, and to
10+
* permit persons to whom the Software is furnished to do so, subject to
11+
* the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be
14+
* included in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20+
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23+
*/
24+
25+
package org.keplerproject.luajava;
26+
27+
import java.io.BufferedReader;
28+
import java.io.InputStreamReader;
29+
30+
/**
31+
* Simple LuaJava console.
32+
* This is also an example on how to use the Java side of LuaJava and how to startup
33+
* a LuaJava application.
34+
*
35+
* @author Thiago Ponte
36+
*/
37+
public class Console {
38+
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();
48+
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;
61+
}
62+
63+
System.out.println("API Lua Java - console mode.");
64+
65+
BufferedReader inp = new BufferedReader(new InputStreamReader(System.in));
66+
67+
String line;
68+
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("> ");
80+
}
81+
82+
L.close();
83+
} catch (Exception e) {
84+
e.printStackTrace();
85+
}
86+
87+
}
88+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* $Id: JavaFunction.java,v 1.6 2006/12/22 14:06:40 thiago Exp $
3+
* Copyright (C) 2003-2007 Kepler Project.
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining
6+
* a copy of this software and associated documentation files (the
7+
* "Software"), to deal in the Software without restriction, including
8+
* without limitation the rights to use, copy, modify, merge, publish,
9+
* distribute, sublicense, and/or sell copies of the Software, and to
10+
* permit persons to whom the Software is furnished to do so, subject to
11+
* the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be
14+
* included in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20+
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23+
*/
24+
25+
package org.keplerproject.luajava;
26+
27+
/**
28+
* JavaFunction is a class that can be used to implement a Lua function in Java.
29+
* JavaFunction is an abstract class, so in order to use it you must extend this
30+
* class and implement the <code>execute</code> method. This <code>execute</code>
31+
* method is the method that will be called when you call the function from Lua.
32+
* To register the JavaFunction in Lua use the method <code>register(String name)</code>.
33+
*/
34+
public abstract class JavaFunction {
35+
36+
/**
37+
* This is the state in which this function will exist.
38+
*/
39+
protected LuaState L;
40+
41+
/**
42+
* This method is called from Lua. Any parameters can be taken with
43+
* <code>getParam</code>. A reference to the JavaFunctionWrapper itself is
44+
* always the first parameter received. Values passed back as results
45+
* of the function must be pushed onto the stack.
46+
*
47+
* @return The number of values pushed onto the stack.
48+
*/
49+
public abstract int execute() throws LuaException;
50+
51+
/**
52+
* Constructor that receives a LuaState.
53+
*
54+
* @param L LuaState object associated with this JavaFunction object
55+
*/
56+
public JavaFunction(LuaState L) {
57+
this.L = L;
58+
}
59+
60+
/**
61+
* Returns a parameter received from Lua. Parameters are numbered from 1.
62+
* A reference to the JavaFunction itself is always the first parameter
63+
* received (the same as <code>this</code>).
64+
*
65+
* @param idx Index of the parameter.
66+
* @return Reference to parameter.
67+
* @see LuaObject
68+
*/
69+
public LuaObject getParam(int idx) {
70+
return L.getLuaObject(idx);
71+
}
72+
73+
/**
74+
* Register a JavaFunction with a given name. This method registers in a
75+
* global variable the JavaFunction specified.
76+
*
77+
* @param name name of the function.
78+
*/
79+
public void register(String name) throws LuaException {
80+
synchronized (L) {
81+
L.pushJavaFunction(this);
82+
L.setGlobal(name);
83+
}
84+
}
85+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* $Id: LuaException.java,v 1.6 2006/12/22 14:06:40 thiago Exp $
3+
* Copyright (C) 2003-2007 Kepler Project.
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining
6+
* a copy of this software and associated documentation files (the
7+
* "Software"), to deal in the Software without restriction, including
8+
* without limitation the rights to use, copy, modify, merge, publish,
9+
* distribute, sublicense, and/or sell copies of the Software, and to
10+
* permit persons to whom the Software is furnished to do so, subject to
11+
* the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be
14+
* included in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20+
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23+
*/
24+
25+
package org.keplerproject.luajava;
26+
27+
/**
28+
* LuaJava exception
29+
*
30+
* @author Thiago Ponte
31+
*/
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+
}
41+
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+
}
50+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* $Id: LuaInvocationHandler.java,v 1.4 2006/12/22 14:06:40 thiago Exp $
3+
* Copyright (C) 2003-2007 Kepler Project.
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining
6+
* a copy of this software and associated documentation files (the
7+
* "Software"), to deal in the Software without restriction, including
8+
* without limitation the rights to use, copy, modify, merge, publish,
9+
* distribute, sublicense, and/or sell copies of the Software, and to
10+
* permit persons to whom the Software is furnished to do so, subject to
11+
* the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be
14+
* included in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20+
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23+
*/
24+
25+
package org.keplerproject.luajava;
26+
27+
import java.lang.reflect.InvocationHandler;
28+
import java.lang.reflect.Method;
29+
30+
/**
31+
* Class that implements the InvocationHandler interface.
32+
* This class is used in the LuaJava's proxy system.
33+
* When a proxy object is accessed, the method invoked is
34+
* called from Lua
35+
*
36+
* @author Rizzato
37+
* @author Thiago Ponte
38+
*/
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);
54+
55+
if (func.isNil()) {
56+
return null;
57+
}
58+
59+
Class retType = method.getReturnType();
60+
Object ret;
61+
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+
}
72+
73+
return ret;
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)