Class LuaClosure
- All Implemented Interfaces:
com.esotericsoftware.kryo.KryoSerializable
LuaFunction
which executes lua bytecode.
A LuaClosure
is a combination of a Prototype
and a LuaValue
to use as an environment for execution.
Normally the LuaValue
is a Globals
in which case the environment
will contain standard lua libraries.
There are three main ways LuaClosure
instances are created:
- Construct an instance using
LuaClosure(Prototype, LuaValue)
- Construct it indirectly by loading a chunk via
Globals.load(java.io.Reader, String)
- Execute the lua bytecode
Lua.OP_CLOSURE
as part of bytecode processing
To construct it directly, the Prototype
is typically created via a compiler such as
String script = "print( 'hello, world' )";
InputStream is = new ByteArrayInputStream(script.getBytes());
Prototype p = LuaC.instance.compile(is, "script");
LuaValue globals = JsePlatform.standardGlobals();
LuaClosure f = new LuaClosure(p, globals);
f.call();
To construct it indirectly, the Globals.load(java.io.Reader, String)
method may be used:
Globals globals = JsePlatform.standardGlobals();
LuaFunction f = globals.load(new StringReader(script), "script");
LuaClosure c = f.checkclosure(); // This may fail if LuaJC is installed.
c.call();
In this example, the "checkclosure()" may fail if direct lua-to-java-bytecode
compiling using LuaJC is installed, because no LuaClosure is created in that case
and the value returned is a LuaFunction
but not a LuaClosure
.
Since a LuaClosure
is a LuaFunction
which is a LuaValue
,
all the value operations can be used directly such as:
-
Nested Class Summary
Nested classes/interfaces inherited from class com.prineside.luaj.LuaValue
LuaValue.NillableSerializer, LuaValue.None
-
Field Summary
FieldsFields inherited from class com.prineside.luaj.LuaFunction
s_metatable
Fields inherited from class com.prineside.luaj.LuaValue
ADD, CALL, CONCAT, DIV, EMPTYSTRING, ENV, EQ, FALSE, INDEX, LE, LEN, LT, METATABLE, MINUSONE, MOD, MODE, MUL, NEWINDEX, NIL, NILLABLE_SERIALIZER, 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
ConstructorsConstructorDescriptionLuaClosure
(Prototype p, LuaValue env) Create a closure around a Prototype with a specific environment. -
Method Summary
Modifier and TypeMethodDescriptionfinal LuaValue
call()
Callthis
with 0 arguments, including metatag processing, and return only the first return value.final LuaValue
Callthis
with 1 argument, including metatag processing, and return only the first return value.final LuaValue
Callthis
with 2 arguments, including metatag processing, and return only the first return value.final LuaValue
Callthis
with 3 arguments, including metatag processing, and return only the first return value.Check that the value is aLuaClosure
, or throwLuaError
if notvoid
initupvalue1
(LuaValue env) Hook for implementations such as LuaJC to load the environment of the main chunk into the first upvalue location.final Varargs
Callthis
with variable arguments, including metatag processing, and retain all return values in aVarargs
.boolean
Check ifthis
is afunction
that is a closure, meaning interprets lua bytecode for its executionname()
Return a human-readable name for this function.final Varargs
Callback used during tail call processing to invoke the function once.optclosure
(LuaClosure defval) Check that optional argument is a closure and return asLuaClosure
void
read
(com.esotericsoftware.kryo.Kryo kryo, com.esotericsoftware.kryo.io.Input input) Convert to human readable String for any type.void
write
(com.esotericsoftware.kryo.Kryo kryo, com.esotericsoftware.kryo.io.Output output) Methods inherited from class com.prineside.luaj.LuaFunction
checkfunction, classnamestub, getmetatable, isfunction, optfunction, strvalue, type, typename
Methods inherited from class com.prineside.luaj.LuaValue
add, add, add, and, arg, arg1, argerror, assert_, buffer, cachedDouble, cachedInt, cachedVarargsOf, cachedVarargsOf, cachedVarargsOf, cachedVarargsOf, cachedVarargsOf, cachedVarargsOf, call, checkboolean, checkdouble, checkglobals, checkint, checkinteger, checkjstring, checklong, checknotnil, checknumber, checknumber, checkstring, checktable, checkthread, checkuserdata, checkuserdata, comparemt, concat, concat, concatmt, concatTo, concatTo, concatTo, 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, invoke, invoke, invoke, invoke, invoke, invokemethod, invokemethod, invokemethod, invokemethod, invokemethod, invokemethod, isboolean, isint, isinttype, islong, isnil, isnumber, isstring, istable, isthread, 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, optboolean, optdouble, optint, optinteger, optjstring, optlong, optnumber, optstring, opttable, optthread, 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, checkinteger, checkjstring, checklong, checknotnil, checknumber, checkstring, checktable, checkthread, checkuserdata, checkuserdata, checkvalue, dealias, 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, toboolean, tobyte, tochar, todouble, tofloat, toint, tojstring, tolong, toshort, touserdata, touserdata, type
-
Field Details
-
p
-
upValues
-
-
Constructor Details
-
LuaClosure
Create a closure around a Prototype with a specific environment. If the prototype has upvalues, the environment will be written into the first upvalue.- Parameters:
p
- the Prototype to construct this Closure for.env
- the environment to associate with the closure.
-
-
Method Details
-
write
public void write(com.esotericsoftware.kryo.Kryo kryo, com.esotericsoftware.kryo.io.Output output) - Specified by:
write
in interfacecom.esotericsoftware.kryo.KryoSerializable
-
read
public void read(com.esotericsoftware.kryo.Kryo kryo, com.esotericsoftware.kryo.io.Input input) - Specified by:
read
in interfacecom.esotericsoftware.kryo.KryoSerializable
-
initupvalue1
Description copied from class:LuaValue
Hook for implementations such as LuaJC to load the environment of the main chunk into the first upvalue location. If the function has no upvalues or is not a main chunk, calling this will be no effect.- Overrides:
initupvalue1
in classLuaValue
- Parameters:
env
- The environment to load into the first upvalue, if there is one.
-
isclosure
public boolean isclosure()Description copied from class:LuaValue
Check ifthis
is afunction
that is a closure, meaning interprets lua bytecode for its execution -
optclosure
Description copied from class:LuaValue
Check that optional argument is a closure and return asLuaClosure
A
LuaClosure
is aLuaFunction
that executes lua byteccode.- Overrides:
optclosure
in classLuaValue
- Parameters:
defval
-LuaClosure
to return ifthis
is nil or none- Returns:
this
cast toLuaClosure
if a function,defval
if nil or none, throwsLuaError
otherwise- See Also:
-
checkclosure
Description copied from class:LuaValue
Check that the value is aLuaClosure
, or throwLuaError
if notLuaClosure
is a subclass ofLuaFunction
that interprets lua bytecode.- Overrides:
checkclosure
in classLuaValue
- Returns:
this
cast asLuaClosure
- See Also:
-
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:
arg
- 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:
arg1
- First argument to supply to the called functionarg2
- 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:
arg1
- First argument to supply to the called functionarg2
- Second argument to supply to the called functionarg3
- Second argument to supply to the called function- Returns:
- First return value
(this(arg1,arg2,arg3))
, orLuaValue.NIL
if there were none. - See Also:
-
invoke
Description copied from class:LuaValue
Callthis
with variable arguments, including metatag processing, and retain all return values in aVarargs
.If
this
is aLuaFunction
, call it, and return all values. Otherwise, look for theLuaValue.CALL
metatag and call that.To get a particular return value, us
Varargs.arg(int)
To call
this
as a method call, useLuaValue.invokemethod(LuaValue)
instead. -
onInvoke
Description copied from class:LuaValue
Callback used during tail call processing to invoke the function once.This may return a
TailcallVarargs
to be evaluated by the client.This should not be called directly, instead use one of the call invocation functions.
-
name
Description copied from class:LuaFunction
Return a human-readable name for this function. Returns the last part of the class name by default. Is overridden by LuaClosure to return the source file and line, and by LibFunctions to return the name.- Overrides:
name
in classLuaFunction
- Returns:
- common name for this function.
-