|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.luaj.vm2.Varargs
org.luaj.vm2.LuaValue
org.luaj.vm2.LuaFunction
org.luaj.vm2.lib.LibFunction
public abstract class LibFunction
Subclass of LuaFunction
common to Java functions exposed to lua.
To provide for common implementations in JME and JSE, library functions are typically grouped on one or more library classes and an opcode per library function is defined and used to key the switch to the correct function within the library.
Since lua functions can be called with too few or too many arguments,
and there are overloaded LuaValue.call()
functions with varying
number of arguments, a Java function exposed in lua needs to handle the
argument fixup when a function is called with a number of arguments
differs from that expected.
To simplify the creation of library functions, there are 5 direct subclasses to handle common cases based on number of argument values and number of return return values.
To be a Java library that can be loaded via require
, it should have
a public constructor that returns a LuaValue
that, when executed,
initializes the library.
For example, the following code will implement a library called "hyperbolic" with two functions, "sinh", and "cosh":
import org.luaj.vm2.LuaValue;
public class hyperbolic extends org.luaj.vm2.lib.OneArgFunction {
public hyperbolic() {}
public LuaValue call(LuaValue arg) {
switch ( opcode ) {
case 0: {
LuaValue t = tableOf();
this.bind(t, hyperbolic.class, new String[] { "sinh", "cosh" }, 1 );
env.set("hyperbolic", t);
return t;
}
case 1: return valueOf(Math.sinh(arg.todouble()));
case 2: return valueOf(Math.cosh(arg.todouble()));
default: return error("bad opcode: "+opcode);
}
}
}
The default constructor is both to instantiate the library
in response to require 'hyperbolic'
statement,
provided it is on Javas class path,
and to instantiate copies of the hyperbolic
class when initializing library instances. .
The instance returned by the default constructor will be invoked
as part of library loading.
In response, it creates two more instances, one for each library function,
in the body of the switch
statement case 0
via the bind(LuaValue, Class, String[], int)
utility method.
It also registers the table in the globals via the LuaFunction.env
local variable, which should be the global environment unless
it has been changed.
case 1
and case 2
will be called when hyperbolic.sinh
hyperbolic.sinh
and hyperbolic.cosh
are invoked.
To test it, a script such as this can be used:
local t = require('hyperbolic')
print( 't', t )
print( 'hyperbolic', hyperbolic )
for k,v in pairs(t) do
print( 'k,v', k,v )
end
print( 'sinh(.5)', hyperbolic.sinh(.5) )
print( 'cosh(.5)', hyperbolic.cosh(.5) )
It should produce something like:
t table: 3dbbd23f
hyperbolic table: 3dbbd23f
k,v cosh cosh
k,v sinh sinh
sinh(.5) 0.5210953
cosh(.5) 1.127626
See the source code in any of the library functions
such as BaseLib
or TableLib
for specific examples.
Field Summary | |
---|---|
protected java.lang.String |
name
The common name for this function, useful for debugging. |
protected int |
opcode
User-defined opcode to differentiate between instances of the library function class. |
Fields inherited from class org.luaj.vm2.LuaFunction |
---|
env, s_metatable |
Fields inherited from class org.luaj.vm2.LuaValue |
---|
ADD, CALL, CONCAT, DIV, EMPTYSTRING, EQ, FALSE, INDEX, LE, LEN, LT, METATABLE, MINUSONE, MOD, MODE, MUL, NEWINDEX, NIL, NILS, NONE, NOVALS, ONE, POW, SUB, TBOOLEAN, TFUNCTION, TINT, TLIGHTUSERDATA, TNIL, TNONE, TNUMBER, TOSTRING, TRUE, TSTRING, TTABLE, TTHREAD, TUSERDATA, TVALUE, TYPE_NAMES, UNM, ZERO |
Constructor Summary | |
---|---|
protected |
LibFunction()
Default constructor for use by subclasses |
Method Summary | |
---|---|
protected void |
bind(LuaValue env,
java.lang.Class factory,
java.lang.String[] names)
Bind a set of library functions. |
protected void |
bind(LuaValue env,
java.lang.Class factory,
java.lang.String[] names,
int firstopcode)
Bind a set of library functions, with an offset |
protected static LuaValue[] |
newupe()
Java code generation utility to allocate storage for upvalue, leave it empty |
protected static LuaValue[] |
newupl(LuaValue v)
Java code generation utility to allocate storage for upvalue, initialize with value |
protected static LuaValue[] |
newupn()
Java code generation utility to allocate storage for upvalue, initialize with nil |
java.lang.String |
tojstring()
Convert to human readable String for any type. |
Methods inherited from class org.luaj.vm2.LuaFunction |
---|
checkfunction, getfenv, getmetatable, isfunction, optfunction, setfenv, type, typename |
Methods inherited from class org.luaj.vm2.Varargs |
---|
argcheck, checkboolean, checkclosure, checkdouble, checkfunction, checkint, checkinteger, checkjstring, checklong, checknotnil, checknumber, checkstring, checktable, checkthread, checkuserdata, checkuserdata, checkvalue, eval, isfunction, isnil, isnoneornil, isnumber, isstring, istable, isTailcall, isthread, isuserdata, isvalue, optboolean, optclosure, optdouble, optfunction, optint, optinteger, optjstring, optlong, optnumber, optstring, opttable, optthread, optuserdata, optuserdata, optvalue, subargs, toboolean, tobyte, tochar, todouble, tofloat, toint, tojstring, tolong, toshort, touserdata, touserdata, type |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
protected int opcode
Subclass will typicall switch on this value to provide the specific behavior for each function.
protected java.lang.String name
Binding functions initialize this to the name to which it is bound.
Constructor Detail |
---|
protected LibFunction()
Method Detail |
---|
public java.lang.String tojstring()
LuaValue
tojstring
in class LuaValue
LuaValue.tostring()
,
LuaValue.optjstring(String)
,
LuaValue.checkjstring()
,
LuaValue.isstring()
,
TSTRING
protected void bind(LuaValue env, java.lang.Class factory, java.lang.String[] names)
An array of names is provided, and the first name is bound with opcode = 0, second with 1, etc.
env
- The environment to apply to each bound functionfactory
- the Class to instantiate for each bound functionnames
- array of String names, one for each function.bind(LuaValue, Class, String[], int)
protected void bind(LuaValue env, java.lang.Class factory, java.lang.String[] names, int firstopcode)
An array of names is provided, and the first name is bound
with opcode = firstopcode
, second with firstopcode+1
, etc.
env
- The environment to apply to each bound functionfactory
- the Class to instantiate for each bound functionnames
- array of String names, one for each function.firstopcode
- the first opcode to usebind(LuaValue, Class, String[])
protected static LuaValue[] newupe()
protected static LuaValue[] newupn()
protected static LuaValue[] newupl(LuaValue v)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |