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:
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:
|
|
|
|