01: /*
02: * $Id: JavaFunction.java,v 1.6 2006/12/22 14:06:40 thiago Exp $
03: * Copyright (C) 2003-2007 Kepler Project.
04: *
05: * Permission is hereby granted, free of charge, to any person obtaining
06: * a copy of this software and associated documentation files (the
07: * "Software"), to deal in the Software without restriction, including
08: * without limitation the rights to use, copy, modify, merge, publish,
09: * 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: * @return The number of values pushed onto the stack.
47: */
48: public abstract int execute() throws LuaException;
49:
50: /**
51: * Constructor that receives a LuaState.
52: * @param L LuaState object associated with this JavaFunction object
53: */
54: public JavaFunction(LuaState L) {
55: this .L = L;
56: }
57:
58: /**
59: * Returns a parameter received from Lua. Parameters are numbered from 1.
60: * A reference to the JavaFunction itself is always the first parameter
61: * received (the same as <code>this</code>).
62: * @param idx Index of the parameter.
63: * @return Reference to parameter.
64: * @see LuaObject
65: */
66: public LuaObject getParam(int idx) {
67: return L.getLuaObject(idx);
68: }
69:
70: /**
71: * Register a JavaFunction with a given name. This method registers in a
72: * global variable the JavaFunction specified.
73: * @param name name of the function.
74: */
75: public void register(String name) throws LuaException {
76: synchronized (L) {
77: L.pushJavaFunction(this);
78: L.setGlobal(name);
79: }
80: }
81: }
|