Package com.prineside.luaj
Class LoadState
java.lang.Object
com.prineside.luaj.LoadState
Class to undump compiled lua bytecode into a
Prototype instances.
The LoadState class provides the default Globals.Undumper
which is used to undump a string of bytes that represent a lua binary file
using either the C-based lua compiler, or luaj's
The canonical method to load and execute code is done indirectly using the Globals:
Globals globals = JsePlatform.standardGlobals();
LuaValue chunk = globasl.load("print('hello, world')", "main.lua");
chunk.call();
This should work regardless of which Globals.Compiler or Globals.Undumper
have been installed.
to construct globals, the LoadState default undumper is installed
as the default Globals.Undumper.
Globals globals = JsePlatform.standardGlobals();
Prototype p = globals.compilePrototype(new StringReader("print('hello, world')"), "main.lua");
ByteArrayOutputStream o = new ByteArrayOutputStream();
org.luaj.vm2.compiler.DumpState.dump(p, o, false);
byte[] lua_binary_file_bytes = o.toByteArray();
The LoadState's default undumper instance
may be used directly to undump these bytes:
Prototypep = LoadState.instance.undump(new ByteArrayInputStream(lua_binary_file_bytes), "main.lua");
LuaClosure c = new LuaClosure(p, globals);
c.call();
More commonly, the Globals.Undumper may be used to undump them:
Prototype p = globals.loadPrototype(new ByteArrayInputStream(lua_binary_file_bytes), "main.lua", "b");
LuaClosure c = new LuaClosure(p, globals);
c.call();
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic StringThe character encoding to use for file encoding.static final Globals.UndumperShared instance of Globals.Undumper to use loading prototypes from binary lua filesfinal DataInputStreaminput stream from which we are loadingstatic final byte[]Signature byte indicating the file is a compiled binary chunkstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intfor header of binary files -- this is the official formatstatic final intsize of header of binary filesstatic final byte[]Data to catch conversion errorsstatic final intfor header of binary files -- this is Lua 5.2static final intformat corresponding to non-number-patched lua, all numbers are floats or doublesstatic final intformat corresponding to non-number-patched lua, all numbers are intsstatic final intformat corresponding to number-patched lua, all numbers are 32-bit (4 byte) intsstatic final StringName for compiled chunks -
Method Summary
Modifier and TypeMethodDescriptionstatic StringgetSourceName(String name) Construct a source name from a supplied chunk namestatic voidInstall this class as the standard Globals.Undumper for the supplied GlobalsLoad a function prototype from the input streamvoidLoad the lua chunk header values.static LuaValuelongBitsToLuaNumber(long bits) Convert bits in a long value to aLuaValue.static Prototypeundump(InputStream stream, String chunkname) Load input stream as a lua binary chunk if the first 4 bytes are the lua binary signature.
-
Field Details
-
instance
Shared instance of Globals.Undumper to use loading prototypes from binary lua files -
NUMBER_FORMAT_FLOATS_OR_DOUBLES
public static final int NUMBER_FORMAT_FLOATS_OR_DOUBLESformat corresponding to non-number-patched lua, all numbers are floats or doubles- See Also:
-
NUMBER_FORMAT_INTS_ONLY
public static final int NUMBER_FORMAT_INTS_ONLYformat corresponding to non-number-patched lua, all numbers are ints- See Also:
-
NUMBER_FORMAT_NUM_PATCH_INT32
public static final int NUMBER_FORMAT_NUM_PATCH_INT32format corresponding to number-patched lua, all numbers are 32-bit (4 byte) ints- See Also:
-
LUA_TINT
public static final int LUA_TINT- See Also:
-
LUA_TNONE
public static final int LUA_TNONE- See Also:
-
LUA_TNIL
public static final int LUA_TNIL- See Also:
-
LUA_TBOOLEAN
public static final int LUA_TBOOLEAN- See Also:
-
LUA_TLIGHTUSERDATA
public static final int LUA_TLIGHTUSERDATA- See Also:
-
LUA_TNUMBER
public static final int LUA_TNUMBER- See Also:
-
LUA_TSTRING
public static final int LUA_TSTRING- See Also:
-
LUA_TTABLE
public static final int LUA_TTABLE- See Also:
-
LUA_TFUNCTION
public static final int LUA_TFUNCTION- See Also:
-
LUA_TUSERDATA
public static final int LUA_TUSERDATA- See Also:
-
LUA_TTHREAD
public static final int LUA_TTHREAD- See Also:
-
LUA_TVALUE
public static final int LUA_TVALUE- See Also:
-
encoding
The character encoding to use for file encoding. Null means the default encoding -
LUA_SIGNATURE
public static final byte[] LUA_SIGNATURESignature byte indicating the file is a compiled binary chunk -
LUAC_TAIL
public static final byte[] LUAC_TAILData to catch conversion errors -
SOURCE_BINARY_STRING
Name for compiled chunks- See Also:
-
LUAC_VERSION
public static final int LUAC_VERSIONfor header of binary files -- this is Lua 5.2- See Also:
-
LUAC_FORMAT
public static final int LUAC_FORMATfor header of binary files -- this is the official format- See Also:
-
LUAC_HEADERSIZE
public static final int LUAC_HEADERSIZEsize of header of binary files- See Also:
-
is
input stream from which we are loading
-
-
Method Details
-
install
Install this class as the standard Globals.Undumper for the supplied Globals -
longBitsToLuaNumber
Convert bits in a long value to aLuaValue.- Parameters:
bits- long value containing the bits- Returns:
LuaIntegerorLuaDoublewhose value corresponds to the bits provided.
-
loadFunction
Load a function prototype from the input stream- Parameters:
p- name of the source- Returns:
Prototypeinstance that was loaded- Throws:
IOException
-
loadHeader
Load the lua chunk header values.- Throws:
IOException- if an i/o exception occurs.
-
undump
Load input stream as a lua binary chunk if the first 4 bytes are the lua binary signature.- Parameters:
stream- InputStream to read, after having read the first byte alreadychunkname- Name to apply to the loaded chunk- Returns:
Prototypethat was loaded, or null if the first 4 bytes were not the lua signature.- Throws:
IOException- if an IOException occurs
-
getSourceName
Construct a source name from a supplied chunk name- Parameters:
name- String name that appears in the chunk- Returns:
- source file name
-