Extension of {@link LuaFunction} which executes lua bytecode.
A {@link LuaClosure} is a combination of a {@link Prototype} and a {@link LuaValue} to use as an environment for execution.
There are three main ways {@link LuaClosure} instances are created:
- Construct an instance using {@link #LuaClosure(Prototype,LuaValue)}
- Construct it indirectly by loading a chunk via {@link LuaCompiler#load(java.io.InputStream,String,LuaValue)}
- Execute the lua bytecode {@link Lua#OP_CLOSURE} as part of bytecode processing
To construct it directly, the {@link Prototype} is typically created via a compiler such as {@link LuaC}:
{@code InputStream is = new ByteArrayInputStream("print('hello,world').getBytes()); Prototype p = LuaC.instance.compile(is, "script"); LuaValue _G = JsePlatform.standardGlobals() LuaClosure f = new LuaClosure(p, _G);}
To construct it indirectly, the {@link LuaC} compiler may be used, which implements the {@link LuaCompiler} interface:
{@code LuaFunction f = LuaC.instance.load(is, "script", _G);}
Typically, a closure that has just been loaded needs to be initialized by executing it, and its return value can be saved if needed:
{@code LuaValue r = f.call(); _G.set( "mypkg", r ) }
In the preceding, the loaded value is typed as {@link LuaFunction} to allow for the possibility of other compilers such as {@link LuaJC}producing {@link LuaFunction} directly without creating a {@link Prototype} or {@link LuaClosure}.
Since a {@link LuaClosure} is a {@link LuaFunction} which is a {@link LuaValue}, all the value operations can be used directly such as:
- {@link LuaValue#setfenv(LuaValue)}
- {@link LuaValue#call()}
- {@link LuaValue#call(LuaValue)}
- {@link LuaValue#invoke()}
- {@link LuaValue#invoke(Varargs)}
- {@link LuaValue#method(String)}
- {@link LuaValue#method(String,LuaValue)}
- {@link LuaValue#invokemethod(String)}
- {@link LuaValue#invokemethod(String,Varargs)}
- ...
@see LuaValue
@see LuaFunction
@see LuaValue#isclosure()
@see LuaValue#checkclosure()
@see LuaValue#optclosure(LuaClosure)
@see LoadState
@see LoadState#compiler