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_CLOSUREas 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_metatableFields 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 LuaValuecall()Callthiswith 0 arguments, including metatag processing, and return only the first return value.final LuaValueCallthiswith 1 argument, including metatag processing, and return only the first return value.final LuaValueCallthiswith 2 arguments, including metatag processing, and return only the first return value.final LuaValueCallthiswith 3 arguments, including metatag processing, and return only the first return value.Check that the value is aLuaClosure, or throwLuaErrorif notvoidinitupvalue1(LuaValue env) Hook for implementations such as LuaJC to load the environment of the main chunk into the first upvalue location.final VarargsCallthiswith variable arguments, including metatag processing, and retain all return values in aVarargs.booleanCheck ifthisis afunctionthat is a closure, meaning interprets lua bytecode for its executionname()Return a human-readable name for this function.final VarargsCallback used during tail call processing to invoke the function once.optclosure(LuaClosure defval) Check that optional argument is a closure and return asLuaClosurevoidread(com.esotericsoftware.kryo.Kryo kryo, com.esotericsoftware.kryo.io.Input input) Convert to human readable String for any type.voidwrite(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, typenameMethods 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, varargsOfMethods 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:
writein interfacecom.esotericsoftware.kryo.KryoSerializable
-
read
public void read(com.esotericsoftware.kryo.Kryo kryo, com.esotericsoftware.kryo.io.Input input) - Specified by:
readin interfacecom.esotericsoftware.kryo.KryoSerializable
-
initupvalue1
Description copied from class:LuaValueHook 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:
initupvalue1in classLuaValue- Parameters:
env- The environment to load into the first upvalue, if there is one.
-
isclosure
public boolean isclosure()Description copied from class:LuaValueCheck ifthisis afunctionthat is a closure, meaning interprets lua bytecode for its execution -
optclosure
Description copied from class:LuaValueCheck that optional argument is a closure and return asLuaClosureA
LuaClosureis aLuaFunctionthat executes lua byteccode.- Overrides:
optclosurein classLuaValue- Parameters:
defval-LuaClosureto return ifthisis nil or none- Returns:
thiscast toLuaClosureif a function,defvalif nil or none, throwsLuaErrorotherwise- See Also:
-
checkclosure
Description copied from class:LuaValueCheck that the value is aLuaClosure, or throwLuaErrorif notLuaClosureis a subclass ofLuaFunctionthat interprets lua bytecode.- Overrides:
checkclosurein classLuaValue- Returns:
thiscast asLuaClosure- See Also:
-
tojstring
Description copied from class:LuaValueConvert to human readable String for any type.- Overrides:
tojstringin classLuaFunction- Returns:
- String for use by human readers based on type.
- See Also:
-
call
Description copied from class:LuaValueCallthiswith 0 arguments, including metatag processing, and return only the first return value.If
thisis aLuaFunction, call it, and return only its first return value, dropping any others. Otherwise, look for theLuaValue.CALLmetatag 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
thisas a method call, useLuaValue.method(LuaValue)instead.- Overrides:
callin classLuaValue- Returns:
- First return value
(this()), orLuaValue.NILif there were none. - See Also:
-
call
Description copied from class:LuaValueCallthiswith 1 argument, including metatag processing, and return only the first return value.If
thisis aLuaFunction, call it, and return only its first return value, dropping any others. Otherwise, look for theLuaValue.CALLmetatag 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
thisas a method call, useLuaValue.method(LuaValue)instead.- Overrides:
callin classLuaValue- Parameters:
arg- First argument to supply to the called function- Returns:
- First return value
(this(arg)), orLuaValue.NILif there were none. - See Also:
-
call
Description copied from class:LuaValueCallthiswith 2 arguments, including metatag processing, and return only the first return value.If
thisis aLuaFunction, call it, and return only its first return value, dropping any others. Otherwise, look for theLuaValue.CALLmetatag 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
thisas a method call, useLuaValue.method(LuaValue)instead.- Overrides:
callin 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.NILif there were none. - See Also:
-
call
Description copied from class:LuaValueCallthiswith 3 arguments, including metatag processing, and return only the first return value.If
thisis aLuaFunction, call it, and return only its first return value, dropping any others. Otherwise, look for theLuaValue.CALLmetatag 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
thisas a method call, useLuaValue.method(LuaValue)instead.- Overrides:
callin 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.NILif there were none. - See Also:
-
invoke
Description copied from class:LuaValueCallthiswith variable arguments, including metatag processing, and retain all return values in aVarargs.If
thisis aLuaFunction, call it, and return all values. Otherwise, look for theLuaValue.CALLmetatag and call that.To get a particular return value, us
Varargs.arg(int)To call
thisas a method call, useLuaValue.invokemethod(LuaValue)instead. -
onInvoke
Description copied from class:LuaValueCallback used during tail call processing to invoke the function once.This may return a
TailcallVarargsto be evaluated by the client.This should not be called directly, instead use one of the call invocation functions.
-
name
Description copied from class:LuaFunctionReturn 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:
namein classLuaFunction- Returns:
- common name for this function.
-