Class LibFunction
- All Implemented Interfaces:
KryoSerializable
- Direct Known Subclasses:
BaseLib.getmetatable
,BaseLib.rawequal
,BaseLib.rawget
,BaseLib.rawlen
,BaseLib.rawset
,BaseLib.setmetatable
,BaseLib.tonumber
,BaseLib.tostring
,BaseLib.type
,MathLib.random
,OneArgFunction
,TableLib.concat
,ThreeArgFunction
,TwoArgFunction
,VarArgFunction
,ZeroArgFunction
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 com.prineside.luaj.LuaValue;
import com.prineside.luaj.lib.*;
public class hyperbolic extends TwoArgFunction {
public hyperbolic() {}
public LuaValue call(LuaValue modname, LuaValue env) {
LuaValue library = tableOf();
library.set( "sinh", new sinh() );
library.set( "cosh", new cosh() );
env.set( "hyperbolic", library );
return library;
}
static class sinh extends OneArgFunction {
public LuaValue call(LuaValue x) {
return LuaValue.valueOf(Math.sinh(x.checkdouble()));
}
}
static class cosh extends OneArgFunction {
public LuaValue call(LuaValue x) {
return LuaValue.valueOf(Math.cosh(x.checkdouble()));
}
}
}
The default constructor is used to instantiate the library
in response to require 'hyperbolic'
statement,
provided it is on Java"s class path.
This instance is then invoked with 2 arguments: the name supplied to require(),
and the environment for this function. The library may ignore these, or use
them to leave side effects in the global environment, for example.
In the previous example, two functions are created, 'sinh', and 'cosh', and placed
into a global table called 'hyperbolic' using the supplied 'env' argument.
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 function: 3dbbd128
k,v sinh function: 3dbbd242
sinh(.5) 0.5210953
cosh(.5) 1.127626
See the source code in any of the library functions
such as BaseLib
or TableLib
for other examples.
-
Nested Class Summary
Nested classes/interfaces inherited from class com.prineside.luaj.LuaValue
LuaValue.NillableSerializer, LuaValue.None
Nested classes/interfaces inherited from class com.prineside.luaj.Varargs
Varargs.ArrayVarargs
-
Field Summary
Fields inherited from class com.prineside.luaj.LuaValue
ADD, CALL, CONCAT, DIV, EMPTYSTRING, ENV, EQ, FALSE, INDEX, IPAIRS, LE, LEN, LT, METATABLE, MINUSONE, MOD, MODE, MUL, NEWINDEX, NIL, NILLABLE_SERIALIZER, NILS, NONE, NOVALS, ONE, PAIRS, POW, SUB, TBOOLEAN, TFUNCTION, TINT, TLIGHTUSERDATA, TNIL, TNONE, TNUMBER, TOSTRING, TRUE, TSTRING, TTABLE, TTHREAD, TUSERDATA, TVALUE, TYPE_NAMES, UNM, ZERO
-
Method Summary
Modifier and TypeMethodDescriptioncall()
Callthis
with 0 arguments, including metatag processing, and return only the first return value.Callthis
with 1 argument, including metatag processing, and return only the first return value.Callthis
with 2 arguments, including metatag processing, and return only the first return value.Callthis
with 3 arguments, including metatag processing, and return only the first return value.Should not return args (must be new varargs)void
Convert to human readable String for any type.void
Methods inherited from class com.prineside.luaj.LuaFunction
checkfunction, classnamestub, getmetatable, isfunction, name, optfunction, strvalue, type, typename
Methods inherited from class com.prineside.luaj.LuaValue
add, add, add, and, arg, arg1, argerror, assert_, buffer, call, cBool, cDouble, cFloat, checkboolean, checkclosure, checkdouble, checkglobals, checkint, checkjstring, checklong, checknotnil, checknumber, checknumber, checkstring, checktable, checkuserdata, checkuserdata, cInt, cNcDouble, cNcFloat, cNcInt, cObject, comparemt, concat, concat, concatmt, concatTo, concatTo, concatTo, cRegObject, div, div, div, divInto, eq, eq_b, eqmtcall, equals, error, get, get, get, gt, gt, gt, gt_b, gt_b, gt_b, gteq, gteq, gteq, gteq_b, gteq_b, gteq_b, inext, initupvalue1, invoke, invoke, invoke, invoke, invokemethod, invokemethod, invokemethod, invokemethod, isboolean, isclosure, isint, isinttype, islong, isnil, isnumber, isRegObject, isstring, istable, isuserdata, isuserdata, isvalidkey, len, length, listOf, listOf, load, lt, lt, lt, lt_b, lt_b, lt_b, lteq, lteq, lteq, lteq_b, lteq_b, lteq_b, metatag, method, method, method, method, method, method, mod, mod, mod, modFrom, mul, mul, mul, narg, neg, neq, neq_b, next, not, onInvoke, optboolean, optclosure, optdouble, optint, optjstring, optlong, optnumber, optstring, opttable, optuserdata, optuserdata, optvalue, or, pow, pow, pow, powWith, powWith, presize, raweq, raweq, raweq, raweq, raweq, rawget, rawget, rawget, rawlen, rawset, rawset, rawset, rawset, rawset, rawset, rawset, rawsetlist, set, set, set, set, set, set, set, setmetatable, strcmp, strcmp, strongvalue, sub, sub, sub, subargs, subFrom, subFrom, tableOf, tableOf, tableOf, tableOf, tableOf, tableOf, tailcallOf, testfor_b, toboolean, tobyte, tochar, todouble, tofloat, toint, tolong, tonumber, toshort, tostring, toString, touserdata, touserdata, userdataOf, userdataOf, valueOf, valueOf, valueOf, valueOf, valueOf, valueOf, varargsOf, varargsOf, varargsOf, varargsOf, varargsOf, varargsOf
Methods inherited from class com.prineside.luaj.Varargs
argcheck, checkboolean, checkclosure, checkdouble, checkfunction, checkint, checkjstring, checklong, checknotnil, checknumber, checkstring, checktable, checkuserdata, checkuserdata, checkvalue, dealias, eval, isfunction, isnil, isnoneornil, isnumber, isstring, istable, isTailcall, isuserdata, isvalue, optboolean, optclosure, optdouble, optfunction, optint, optjstring, optlong, optnumber, optstring, opttable, optuserdata, optuserdata, optvalue, toboolean, tobyte, tochar, todouble, tofloat, toint, tojstring, tolong, toshort, touserdata, touserdata, type
-
Method Details
-
write
- Specified by:
write
in interfaceKryoSerializable
-
read
- Specified by:
read
in interfaceKryoSerializable
-
tojstring
Description copied from class:LuaValue
Convert to human readable String for any type.- Overrides:
tojstring
in classLuaFunction
- Returns:
- String for use by human readers based on type.
- See Also:
-
call
Description copied from class:LuaValue
Callthis
with 0 arguments, including metatag processing, and return only the first return value.If
this
is aLuaFunction
, call it, and return only its first return value, dropping any others. Otherwise, look for theLuaValue.CALL
metatag and call that.If the return value is a
Varargs
, only the 1st value will be returned. To get multiple values, useLuaValue.invoke()
instead.To call
this
as a method call, useLuaValue.method(LuaValue)
instead.- Overrides:
call
in classLuaValue
- Returns:
- First return value
(this())
, orLuaValue.NIL
if there were none. - See Also:
-
call
Description copied from class:LuaValue
Callthis
with 1 argument, including metatag processing, and return only the first return value.If
this
is aLuaFunction
, call it, and return only its first return value, dropping any others. Otherwise, look for theLuaValue.CALL
metatag and call that.If the return value is a
Varargs
, only the 1st value will be returned. To get multiple values, useLuaValue.invoke()
instead.To call
this
as a method call, useLuaValue.method(LuaValue)
instead.- Overrides:
call
in classLuaValue
- Parameters:
a
- First argument to supply to the called function- Returns:
- First return value
(this(arg))
, orLuaValue.NIL
if there were none. - See Also:
-
call
Description copied from class:LuaValue
Callthis
with 2 arguments, including metatag processing, and return only the first return value.If
this
is aLuaFunction
, call it, and return only its first return value, dropping any others. Otherwise, look for theLuaValue.CALL
metatag and call that.If the return value is a
Varargs
, only the 1st value will be returned. To get multiple values, useLuaValue.invoke()
instead.To call
this
as a method call, useLuaValue.method(LuaValue)
instead.- Overrides:
call
in classLuaValue
- Parameters:
a
- First argument to supply to the called functionb
- Second argument to supply to the called function- Returns:
- First return value
(this(arg1,arg2))
, orLuaValue.NIL
if there were none. - See Also:
-
call
Description copied from class:LuaValue
Callthis
with 3 arguments, including metatag processing, and return only the first return value.If
this
is aLuaFunction
, call it, and return only its first return value, dropping any others. Otherwise, look for theLuaValue.CALL
metatag and call that.If the return value is a
Varargs
, only the 1st value will be returned. To get multiple values, useLuaValue.invoke()
instead.To call
this
as a method call, useLuaValue.method(LuaValue)
instead.- Overrides:
call
in classLuaValue
- Parameters:
a
- First argument to supply to the called functionb
- Second argument to supply to the called functionc
- Second argument to supply to the called function- Returns:
- First return value
(this(arg1,arg2,arg3))
, orLuaValue.NIL
if there were none. - See Also:
-
call
-
invoke
Description copied from class:LuaValue
Should not return args (must be new varargs)
-