01: package org.keplerproject.luajava.test;
02:
03: import java.util.Date;
04:
05: import org.keplerproject.luajava.JavaFunction;
06: import org.keplerproject.luajava.LuaException;
07: import org.keplerproject.luajava.LuaState;
08:
09: /**
10: * Example of a library in Java openned in Lua using loadLib
11: *
12: * @author Thiago Ponte
13: */
14: public class LoadLibExample {
15: private final static String LIB_NAME = "eg";
16:
17: private static void getLibTable(LuaState L) {
18: L.getGlobal(LIB_NAME);
19: if (L.isNil(-1)) {
20: L.pop(1);
21: L.newTable();
22: L.pushValue(-1);
23: L.setGlobal(LIB_NAME);
24: }
25: }
26:
27: /**
28: * Method called by loadLib
29: */
30: public static void open(LuaState L) throws LuaException {
31: getLibTable(L);
32:
33: L.pushString("example");
34:
35: L.pushJavaFunction(new JavaFunction(L) {
36: /**
37: * Example for loadLib.
38: * Prints the time and the first parameter, if any.
39: */
40: public int execute() throws LuaException {
41: System.out.println(new Date().toString());
42:
43: if (L.getTop() > 1) {
44: System.out.println(getParam(2));
45: }
46:
47: return 0;
48: }
49: });
50:
51: L.setTable(-3);
52:
53: L.pop(1);
54: }
55: }
|