org.luaj.vm2
Class LoadState

java.lang.Object
  extended by org.luaj.vm2.LoadState

public class LoadState
extends java.lang.Object

Class to manage loading of Prototype instances.

The LoadState class exposes one main function, namely load(InputStream, String, LuaValue), to be used to load code from a particular input stream.

A simple pattern for loading and executing code is

 LuaValue _G = JsePlatform.standardGlobals();
 LoadState.load( new FileInputStream("main.lua"), "main.lua", _G ).call();
  
This should work regardless of which LoadState.LuaCompiler has been installed.

Prior to loading code, a compiler should be installed.

By default, when using JsePlatform or to construct globals, the LuaC compiler is installed.

To override the default compiler with, say, the LuaJC lua-to-java bytecode compiler, install it before loading, for example:

 LuaValue _G = JsePlatform.standardGlobals();
 LuaJC.install();
 LoadState.load( new FileInputStream("main.lua"), "main.lua", _G ).call();
  

See Also:
LoadState.LuaCompiler, LuaClosure, LuaFunction, compiler, load(InputStream, String, LuaValue), LuaC, LuaJC

Nested Class Summary
static interface LoadState.LuaCompiler
          Interface for the compiler, if it is installed.
 
Field Summary
static LoadState.LuaCompiler compiler
          Compiler instance, if installed
 java.io.DataInputStream is
          input stream from which we are loading
static int LUA_TBOOLEAN
           
static int LUA_TFUNCTION
           
static int LUA_TINT
           
static int LUA_TLIGHTUSERDATA
           
static int LUA_TNIL
           
static int LUA_TNONE
           
static int LUA_TNUMBER
           
static int LUA_TSTRING
           
static int LUA_TTABLE
           
static int LUA_TTHREAD
           
static int LUA_TUSERDATA
           
static int LUA_TVALUE
           
static int LUAC_FORMAT
          for header of binary files -- this is the official format
static int LUAC_HEADERSIZE
          size of header of binary files
static int LUAC_VERSION
          for header of binary files -- this is Lua 5.1
static int NUMBER_FORMAT_FLOATS_OR_DOUBLES
          format corresponding to non-number-patched lua, all numbers are floats or doubles
static int NUMBER_FORMAT_INTS_ONLY
          format corresponding to non-number-patched lua, all numbers are ints
static int NUMBER_FORMAT_NUM_PATCH_INT32
          format corresponding to number-patched lua, all numbers are 32-bit (4 byte) ints
static java.lang.String SOURCE_BINARY_STRING
          Name for compiled chunks
 
Method Summary
static java.lang.String getSourceName(java.lang.String name)
          Construct a source name from a supplied chunk name
static LuaFunction load(java.io.InputStream stream, java.lang.String name, LuaValue env)
          Load lua in either binary or text form from an input stream.
static Prototype loadBinaryChunk(int firstByte, java.io.InputStream stream, java.lang.String name)
          Load lua thought to be a binary chunk from its first byte from an input stream.
 Prototype loadFunction(LuaString p)
          Load a function prototype from the input stream
 void loadHeader()
          Load the lua chunk header values.
static LuaValue longBitsToLuaNumber(long bits)
          Convert bits in a long value to a LuaValue.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NUMBER_FORMAT_FLOATS_OR_DOUBLES

public static final int NUMBER_FORMAT_FLOATS_OR_DOUBLES
format corresponding to non-number-patched lua, all numbers are floats or doubles

See Also:
Constant Field Values

NUMBER_FORMAT_INTS_ONLY

public static final int NUMBER_FORMAT_INTS_ONLY
format corresponding to non-number-patched lua, all numbers are ints

See Also:
Constant Field Values

NUMBER_FORMAT_NUM_PATCH_INT32

public static final int NUMBER_FORMAT_NUM_PATCH_INT32
format corresponding to number-patched lua, all numbers are 32-bit (4 byte) ints

See Also:
Constant Field Values

LUA_TINT

public static final int LUA_TINT
See Also:
Constant Field Values

LUA_TNONE

public static final int LUA_TNONE
See Also:
Constant Field Values

LUA_TNIL

public static final int LUA_TNIL
See Also:
Constant Field Values

LUA_TBOOLEAN

public static final int LUA_TBOOLEAN
See Also:
Constant Field Values

LUA_TLIGHTUSERDATA

public static final int LUA_TLIGHTUSERDATA
See Also:
Constant Field Values

LUA_TNUMBER

public static final int LUA_TNUMBER
See Also:
Constant Field Values

LUA_TSTRING

public static final int LUA_TSTRING
See Also:
Constant Field Values

LUA_TTABLE

public static final int LUA_TTABLE
See Also:
Constant Field Values

LUA_TFUNCTION

public static final int LUA_TFUNCTION
See Also:
Constant Field Values

LUA_TUSERDATA

public static final int LUA_TUSERDATA
See Also:
Constant Field Values

LUA_TTHREAD

public static final int LUA_TTHREAD
See Also:
Constant Field Values

LUA_TVALUE

public static final int LUA_TVALUE
See Also:
Constant Field Values

compiler

public static LoadState.LuaCompiler compiler
Compiler instance, if installed


SOURCE_BINARY_STRING

public static final java.lang.String SOURCE_BINARY_STRING
Name for compiled chunks

See Also:
Constant Field Values

LUAC_VERSION

public static final int LUAC_VERSION
for header of binary files -- this is Lua 5.1

See Also:
Constant Field Values

LUAC_FORMAT

public static final int LUAC_FORMAT
for header of binary files -- this is the official format

See Also:
Constant Field Values

LUAC_HEADERSIZE

public static final int LUAC_HEADERSIZE
size of header of binary files

See Also:
Constant Field Values

is

public final java.io.DataInputStream is
input stream from which we are loading

Method Detail

longBitsToLuaNumber

public static LuaValue longBitsToLuaNumber(long bits)
Convert bits in a long value to a LuaValue.

Parameters:
bits - long value containing the bits
Returns:
LuaInteger or LuaDouble whose value corresponds to the bits provided.

loadFunction

public Prototype loadFunction(LuaString p)
                       throws java.io.IOException
Load a function prototype from the input stream

Parameters:
p - name of the source
Returns:
Prototype instance that was loaded
Throws:
java.io.IOException

loadHeader

public void loadHeader()
                throws java.io.IOException
Load the lua chunk header values.

Throws:
java.io.IOException - if an i/o exception occurs.

load

public static LuaFunction load(java.io.InputStream stream,
                               java.lang.String name,
                               LuaValue env)
                        throws java.io.IOException
Load lua in either binary or text form from an input stream.

Parameters:
firstByte - the first byte of the input stream
stream - InputStream to read, after having read the first byte already
name - Name to apply to the loaded chunk
Returns:
Prototype that was loaded
Throws:
java.lang.IllegalArgumentException - if the signature is bac
java.io.IOException - if an IOException occurs

loadBinaryChunk

public static Prototype loadBinaryChunk(int firstByte,
                                        java.io.InputStream stream,
                                        java.lang.String name)
                                 throws java.io.IOException
Load lua thought to be a binary chunk from its first byte from an input stream.

Parameters:
firstByte - the first byte of the input stream
stream - InputStream to read, after having read the first byte already
name - Name to apply to the loaded chunk
Returns:
Prototype that was loaded
Throws:
java.lang.IllegalArgumentException - if the signature is bac
java.io.IOException - if an IOException occurs

getSourceName

public static java.lang.String getSourceName(java.lang.String name)
Construct a source name from a supplied chunk name

Parameters:
name - String name that appears in the chunk
Returns:
source file name


Copyright © 2007-2008 Luaj.org. All Rights Reserved.