org.luaj.vm
Class LuaState

java.lang.Object
  extended by org.luaj.vm.Lua
      extended by org.luaj.vm.LuaState

public class LuaState
extends Lua


LuaState

 typedef struct LuaState;
 

Opaque structure that keeps the whole state of a Lua interpreter. The Lua library is fully reentrant: it has no global variables. All information about a state is kept in this structure.

Here we list all functions and types from the C API in alphabetical order. Each function has an indicator like this: [-o, +p, x]

The first field, o, is how many elements the function pops from the stack. The second field, p, is how many elements the function pushes onto the stack. (Any function always pushes its results after popping its arguments.) A field in the form x|y means the function may push (or pop) x or y elements, depending on the situation; an interrogation mark '?' means that we cannot know how many elements the function pops/pushes by looking only at its arguments (e.g., they may depend on what is on the stack). The third field, x, tells whether the function may throw errors: '-' means the function never throws any error; 'm' means the function may throw an error only due to not enough memory; 'e' means the function may throw other kinds of errors; 'v' means the function may throw an error on purpose.


Field Summary
 LTable _G
           
 int base
           
 CallInfo[] calls
           
 int cc
           
static int LUA_ERRERR
           
static int LUA_ERRMEM
           
static int LUA_ERRRUN
           
static int LUA_ERRSYNTAX
           
static int LUA_MASKCALL
           
static int LUA_MASKLINE
           
static int LUA_MASKRET
           
static int LUA_YIELD
           
protected  int nresults
           
protected  LFunction panic
           
 LValue[] stack
           
 int top
           
protected  java.util.Stack upvals
           
 
Fields inherited from class org.luaj.vm.Lua
_VERSION, BITRK, iABC, iABx, iAsBx, LFIELDS_PER_FLUSH, LUA_MULTRET, LUA_TBOOLEAN, LUA_TFUNCTION, LUA_TINT, LUA_TLIGHTUSERDATA, LUA_TNIL, LUA_TNONE, LUA_TNUMBER, LUA_TSTRING, LUA_TTABLE, LUA_TTHREAD, LUA_TUSERDATA, LUA_TVALUE, luaP_opmodes, MASK_A, MASK_B, MASK_Bx, MASK_C, MASK_NOT_A, MASK_NOT_B, MASK_NOT_Bx, MASK_NOT_C, MASK_NOT_OP, MASK_OP, MAX_OP, MAXARG_A, MAXARG_B, MAXARG_Bx, MAXARG_C, MAXARG_sBx, MAXINDEXRK, NO_REG, NUM_OPCODES, OP_ADD, OP_CALL, OP_CLOSE, OP_CLOSURE, OP_CONCAT, OP_DIV, OP_EQ, OP_FORLOOP, OP_FORPREP, OP_GETGLOBAL, OP_GETTABLE, OP_GETUPVAL, OP_JMP, OP_LE, OP_LEN, OP_LOADBOOL, OP_LOADK, OP_LOADNIL, OP_LT, OP_MOD, OP_MOVE, OP_MUL, OP_NEWTABLE, OP_NOT, OP_POW, OP_RETURN, OP_SELF, OP_SETGLOBAL, OP_SETLIST, OP_SETTABLE, OP_SETUPVAL, OP_SUB, OP_TAILCALL, OP_TEST, OP_TESTSET, OP_TFORLOOP, OP_UNM, OP_VARARG, OpArgK, OpArgN, OpArgR, OpArgU, POS_A, POS_B, POS_Bx, POS_C, POS_OP, SIZE_A, SIZE_B, SIZE_Bx, SIZE_C, SIZE_OP, TYPE_NAMES, VARARG_HASARG, VARARG_ISVARARG, VARARG_NEEDSARG
 
Constructor Summary
protected LuaState()
          Creates a new, independent LuaState instance.
 
Method Summary
 void argcheck(boolean cond, int narg, java.lang.String extramsg)
          Conditionally report an error with an argument.
 void argerror(int narg, java.lang.String extramsg)
          Report an error with an argument.
 void call(int nargs, int nreturns)
          Calls a function.
 LValue call(LFunction function)
          Call a function with no arguments and one return value.
 LValue call(LFunction function, LValue arg0)
          Call a function with one argument and one return value This may change values in the current stack frame.
 LValue checkany(int narg)
          Checks whether the function has an argument of any type (including nil) at position narg.
 double checkdouble(int narg)
          Checks whether the function argument narg is a number and returns this number cast to a double.
 LFunction checkfunction(int narg)
          Checks whether the function argument narg is a function and returns this function.
 int checkint(int narg)
          Checks whether the function argument narg is a number and returns this number cast to an int.
 LInteger checkinteger(int narg)
          Checks whether the function argument narg is a number and returns this number cast to a LInteger.
 long checklong(int narg)
          Checks whether the function argument narg is a number and returns this number cast to a long.
 LString checklstring(int narg)
          Checks whether the function argument narg is a string and returns this string as a lua string.
 LNumber checknumber(int narg)
          Checks whether the function argument narg is a number and returns this number.
 void checkstack(int extra)
          Ensures that there are at least extra free stack slots in the stack.
 java.lang.String checkstring(int narg)
          Checks whether the function argument narg is a string and returns this string as a Java String.
 LTable checktable(int narg)
          Checks whether the function argument narg is a table and returns this table.
 LThread checkthread(int narg)
          Checks whether the function argument narg is a thread and returns this thread.
 LValue checktype(int narg, int t)
          Checks whether the function argument narg has type t and return it as an LValue.
 java.lang.Object checkudata(int ud, java.lang.Class expected)
          Check that the type of userdata on the stack matches the required type, and if so, return the Java Object the userdata value points to.
 void closeUpVals(int limit)
           
protected  void debugAssert(boolean b)
           
protected  void debugHooks(int pc)
           
 void doCall(LClosure c, LValue[] args)
          Put the closure on the stack with arguments, then perform the call.
 void error(java.lang.String message)
          Raises an error with the default level.
 void error(java.lang.String message, int level)
          Raises an error.
 void exec()
          execute instructions up to a yield, return, or call
 void execute()
          Execute bytecodes until the current call completes or the vm yields.
 UpVal findUpVal(int target)
           
 void getfield(int index, LString k)
          Dereference a tables field.
protected  java.lang.String getFileLine(int level)
          Get the file line number info for a particular call frame.
 void getglobal(java.lang.String s)
          Look up a global value.
 LFunction gethook()
          Get the current hook function, if any.
 int gethookcount()
          Get the current hook count.
 int gethookmask()
          Get the current hook mask.
 boolean getmetatable(int index)
          Get a value's metatable.
 java.lang.String getSourceFileName(LString s)
           
protected  java.lang.String getSourceFileName(java.lang.String s)
           
 CallInfo getStackFrame(int callStackDepth)
           
 int gettop()
          Returns the index of the top element in the stack.
 void init()
          Performs the initialization.
 void insert(int index)
          Insert the top item somewhere in the stack.
 void installStandardLibs()
          Install the standard set of libraries used by most implementations: BaseLib, CoroutineLib, MathLib, PackageLib, TableLib, StringLib
 void invokeJavaFunction(LFunction javaFunction)
          Invoke a LFunction being called via prepStackCall()
 boolean isboolean(int index)
          Test if a value is boolean.
 boolean isfunction(int index)
          Test if a value is a function.
 boolean isnil(int index)
          Test if a value is nil [-0, +0, -]
 boolean isnoneornil(int index)
          Test if a value is nil or not valid [-0, +0, -]
 boolean isnumber(int index)
          Test if a value is a number [-0, +0, -]
 boolean isstring(int index)
          Test if a value is a string [-0, +0, m]
 boolean istable(int index)
          Test if a value is a table [-0, +0, -]
 boolean isthread(int index)
          Test if a value is a thread [-0, +0, -]
 boolean isuserdata(int index)
          Test if a value is a userdata [-0, +0, -]
 int load(java.io.InputStream is, java.lang.String chunkname)
          Loads a Lua chunk.
 LValue luaV_call_index(LFunction function, LValue table, LValue key)
          Call an index function with two arguments and one return value Values in the current stack frame will be preserved.
 LValue luaV_call_newindex(LFunction function, LValue table, LValue key, LValue value)
          Call a newindex function with three arguments and one return value Values in the current stack frame will be preserved.
static LValue luaV_getmetafield(LValue t, LString tag)
           
 LValue luaV_gettable(LValue table, LValue key)
          Get a key from a table using full metatable processing
 void luaV_settable(LValue table, LValue key, LValue val)
          Get a key from a table using full metatable processing
 int optint(int narg, int d)
          If the function argument narg is a number, returns this number cast to an int.
 LInteger optinteger(int narg, int d)
          If the function argument narg is a number, returns this number cast to a lua_Integer.
 long optlong(int narg, long d)
          If the function argument narg is a number, returns this number cast to a long.
 LString optlstring(int narg, LString d)
          If the function argument narg is a string, returns this string.
 LNumber optnumber(int narg, LNumber d)
          If the function argument narg is a number, returns this number.
 java.lang.String optstring(int narg, java.lang.String d)
          If the function argument narg is a string, returns this string.
 int pcall(int nargs, int nreturns, int errfunc)
          Calls a function in protected mode.
 void pop(int n)
          Pops n elements from the stack.
 LValue poplvalue()
           
 void prepStackCall()
          Create a call frame for a call that has been set up on the stack.
 void pushboolean(boolean b)
          Pushes a boolean value with value b onto the stack.
 void pushboolean(java.lang.Boolean b)
          Push a Java Boolean value, or nil if the value is null.
 void pushfunction(LFunction f)
          Pushes a function onto the stack.
 void pushinteger(java.lang.Byte b)
          Push a Java Byte value, or nil if the value is null.
 void pushinteger(java.lang.Character c)
          Push a Java Character value, or nil if the value is null.
 void pushinteger(int n)
          Pushes a number with value n onto the stack.
 void pushinteger(java.lang.Integer i)
          Push a Java Integer value, or nil if the value is null.
 void pushinteger(java.lang.Short s)
          Push a Java Short value, or nil if the value is null.
 void pushlstring(byte[] byteArray)
          Push string bytes onto the stack as a string.
 void pushlstring(byte[] bytes, int offset, int length)
          Push string bytes onto the stack as a string.
 void pushlstring(LString s)
          Push an LString onto the stack.
 void pushlvalue(LValue value)
          Push an LValue onto the stack.
 void pushnil()
          Pushes a nil value onto the stack.
 void pushnumber(double d)
          Pushes a number with value d onto the stack.
 void pushnumber(java.lang.Double d)
          Push a Java Double as a double, or nil if the value is null.
 void pushnumber(java.lang.Float f)
          Push a Java Float value, or nil if the value is null.
 void pushnumber(java.lang.Long l)
          Push a Java Long value, or nil if the value is null.
 void pushstring(java.lang.String s)
          Push a String onto the stack.
 void pushuserdata(java.lang.Object o)
          Push a Java Object as userdata, or nil if the value is null.
 void pushvalue(int index)
          Push a value from the stack onto the stack.
 void rawgeti(int index, int n)
          Deprecated. should get the table and do a raw get instead
 void remove(int index)
          Remove an element from the stack.
 void replace(int index)
          Replace an element on the stack.
 void resettop()
          Set the top to the base.
 void setfield(int index, LString k)
          Set the value of a table field.
 void setglobal(java.lang.String name)
          Set the value of a global variable.
 void sethook(LFunction func, int mask, int count)
          Set the hook function.
 void settop(int nt)
          Set the top of the stack.
 void shutdown()
          Perform any shutdown/clean up tasks if needed
 boolean toboolean(int index)
          Get a value as a boolean.
 java.lang.Boolean toboxedboolean(int index)
          Convert a value to a Java Boolean value, or null if the value is nil.
 java.lang.Byte toboxedbyte(int index)
          Convert a value to a Java Byte value, or null if the value is not a number.
 java.lang.Double toboxeddouble(int index)
          Convert a value to a Java Double value, or null if the value is not a number.
 java.lang.Float toboxedfloat(int index)
          Convert a value to a Java Float value, or null if the value is not a number.
 java.lang.Integer toboxedinteger(int index)
          Convert a value to a Java Integer value, or null if the value is not a number.
 java.lang.Long toboxedlong(int index)
          Convert a value to a Java Long value, or null if the value is nil.
 LValue tofunction(int index)
          Get a value as a LFunction.
 int tointeger(int index)
          Get a value as an int.
 LValue tolnumber(int index)
          Convert a value to an LNumber[-0, +0, -]
 LString tolstring(int index)
          Gets the value of a string as byte array.
 double tonumber(int index)
          Convert a value to a double.
 LValue topointer(int index)
          Get the raw Object at a stack location.
 java.lang.String tostring(int index)
          Get a stack value as a String.
 LTable totable(int index)
          Get a value from the stack as a lua table.
 java.lang.Object touserdata(int index)
          Get the Object from a userdata value.
 int type(int index)
          Get the type of a value.
 java.lang.String typename(int index)
          Get the type name for a value.
 void typerror(int narg, int typenum)
          Report a type error.
 void typerror(int narg, java.lang.String typename)
          Report a type error.
static void vmerror(java.lang.String description)
          Method to indicate a vm internal error has occurred.
 void xmove(LuaState to, int n)
          Exchange values between threads.
 
Methods inherited from class org.luaj.vm.Lua
GET_OPCODE, GETARG_A, GETARG_B, GETARG_Bx, GETARG_C, GETARG_sBx, getBMode, getCMode, getOpMode, INDEXK, ISK, RKASK, testAMode, testTMode
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LUA_YIELD

public static final int LUA_YIELD
See Also:
Constant Field Values

LUA_ERRRUN

public static final int LUA_ERRRUN
See Also:
Constant Field Values

LUA_ERRSYNTAX

public static final int LUA_ERRSYNTAX
See Also:
Constant Field Values

LUA_ERRMEM

public static final int LUA_ERRMEM
See Also:
Constant Field Values

LUA_ERRERR

public static final int LUA_ERRERR
See Also:
Constant Field Values

LUA_MASKCALL

public static final int LUA_MASKCALL
See Also:
Constant Field Values

LUA_MASKRET

public static final int LUA_MASKRET
See Also:
Constant Field Values

LUA_MASKLINE

public static final int LUA_MASKLINE
See Also:
Constant Field Values

base

public int base

top

public int top

nresults

protected int nresults

stack

public LValue[] stack

cc

public int cc

calls

public CallInfo[] calls

upvals

protected java.util.Stack upvals

panic

protected LFunction panic

_G

public LTable _G
Constructor Detail

LuaState

protected LuaState()
Creates a new, independent LuaState instance. [-0, +0, -]

Returns NULL if cannot create the state (due to lack of memory). The argument f is the allocator function; Lua does all memory allocation for this state through this function. The second argument, ud, is an opaque pointer that Lua simply passes to the allocator in every call.

Method Detail

debugHooks

protected void debugHooks(int pc)

debugAssert

protected void debugAssert(boolean b)

init

public void init()
Performs the initialization.


shutdown

public void shutdown()
Perform any shutdown/clean up tasks if needed


installStandardLibs

public void installStandardLibs()
Install the standard set of libraries used by most implementations: BaseLib, CoroutineLib, MathLib, PackageLib, TableLib, StringLib


prepStackCall

public void prepStackCall()
Create a call frame for a call that has been set up on the stack. The first value on the stack must be a Closure, and subsequent values are arguments to the closure.


execute

public void execute()
Execute bytecodes until the current call completes or the vm yields.


doCall

public void doCall(LClosure c,
                   LValue[] args)
Put the closure on the stack with arguments, then perform the call. Leave return values on the stack for later querying.

Parameters:
c -
values -

invokeJavaFunction

public void invokeJavaFunction(LFunction javaFunction)
Invoke a LFunction being called via prepStackCall()

Parameters:
javaFunction -

call

public void call(int nargs,
                 int nreturns)
Calls a function. [-(nargs + 1), +nresults, e]

To call a function you must use the following protocol: first, the function to be called is pushed onto the stack; then, the arguments to the function are pushed in direct order; that is, the first argument is pushed first. Finally you call lua_call; nargs is the number of arguments that you pushed onto the stack. All arguments and the function value are popped from the stack when the function is called. The function results are pushed onto the stack when the function returns. The number of results is adjusted to nresults, unless nresults is LUA_MULTRET. In this case, all results from the function are pushed. Lua takes care that the returned values fit into the stack space. The function results are pushed onto the stack in direct order (the first result is pushed first), so that after the call the last result is on the top of the stack.

Any error inside the called function is propagated upwards (with a longjmp).

The following example shows how the host program may do the equivalent to this Lua code:

 a = f("how", t.x, 14)
 

Here it is in C:

 lua_getfield(L, LUA_GLOBALSINDEX, "f"); // function to be called 
 lua_pushstring(L, "how"); // 1st argument 
 lua_getfield(L, LUA_GLOBALSINDEX, "t"); // table to be indexed 
 lua_getfield(L, -1, "x"); // push result of t.x (2nd arg) 
 lua_remove(L, -2); // remove 't' from the stack 
 lua_pushinteger(L, 14); // 3rd argument 
 lua_call(L, 3, 1); // call 'f' with 3 arguments and 1 result 
 lua_setfield(L, LUA_GLOBALSINDEX, "a"); // set global 'a' 
 

Note that the code above is "balanced": at its end, the stack is back to its original configuration. This is considered good programming practice.


pcall

public int pcall(int nargs,
                 int nreturns,
                 int errfunc)
Calls a function in protected mode. [-(nargs + 1), +(nresults|1), -]

Both nargs and nresults have the same meaning as in lua_call. If there are no errors during the call, lua_pcall behaves exactly like lua_call. However, if there is any error, lua_pcall catches it, pushes a single value on the stack (the error message), and returns an error code. Like lua_call, lua_pcall always removes the function and its arguments from the stack.

If errfunc is 0, then the error message returned on the stack is exactly the original error message. Otherwise, errfunc is the stack index of an error handler function. (In the current implementation, this index cannot be a pseudo-index.) In case of runtime errors, this function will be called with the error message and its return value will be the message returned on the stack by lua_pcall.

Typically, the error handler function is used to add more debug information to the error message, such as a stack traceback. Such information cannot be gathered after the return of lua_pcall, since by then the stack has unwound.

The lua_pcall function returns 0 in case of success or one of the following error codes (defined in lua.h):


load

public int load(java.io.InputStream is,
                java.lang.String chunkname)
Loads a Lua chunk. [-0, +1, -]

If there are no errors, lua_load pushes the compiled chunk as a Lua function on top of the stack. Otherwise, it pushes an error message. The return values of lua_load are:

This function only loads a chunk; it does not run it.

lua_load automatically detects whether the chunk is text or binary, and loads it accordingly (see program luac).

The lua_load function uses a user-supplied reader function to read the chunk (see lua_Reader). The data argument is an opaque value passed to the reader function.

The chunkname argument gives a name to the chunk, which is used for error messages and in debug information (see §3.8).


exec

public void exec()
execute instructions up to a yield, return, or call


findUpVal

public UpVal findUpVal(int target)

closeUpVals

public void closeUpVals(int limit)

getStackFrame

public CallInfo getStackFrame(int callStackDepth)

luaV_getmetafield

public static LValue luaV_getmetafield(LValue t,
                                       LString tag)

luaV_gettable

public LValue luaV_gettable(LValue table,
                            LValue key)
Get a key from a table using full metatable processing


luaV_settable

public void luaV_settable(LValue table,
                          LValue key,
                          LValue val)
Get a key from a table using full metatable processing


getSourceFileName

public java.lang.String getSourceFileName(LString s)

getSourceFileName

protected java.lang.String getSourceFileName(java.lang.String s)

getFileLine

protected java.lang.String getFileLine(int level)
Get the file line number info for a particular call frame.

Parameters:
cindex - index into call stack, or -1 to get first lua location
Returns:

error

public void error(java.lang.String message,
                  int level)
Raises an error. The message is pushed onto the stack and used as the error message. It also adds at the beginning of the message the file name and the line number where the error occurred, if this information is available. In the java implementation this throws a LuaErrorException after filling line number information first when level > 0.


error

public void error(java.lang.String message)
Raises an error with the default level.


checkstack

public void checkstack(int extra)
Ensures that there are at least extra free stack slots in the stack. [-0, +0, -]

It returns false if it cannot grow the stack to that size. This function never shrinks the stack; if the stack is already larger than the new size, it is left unchanged.


getfield

public void getfield(int index,
                     LString k)
Dereference a tables field. [-0, +1, e]

Pushes onto the stack the value t[k], where t is the value at the given valid index. As in Lua, this function may trigger a metamethod for the "index" event (see §2.8).


getglobal

public void getglobal(java.lang.String s)
Look up a global value. [-0, +1, e]

Pushes onto the stack the value of the global name. It is defined as a macro:

         #define lua_getglobal(L,s)  lua_getfield(L, LUA_GLOBALSINDEX, s)
        
 


getmetatable

public boolean getmetatable(int index)
Get a value's metatable. [-0, +(0|1), -]

Pushes onto the stack the metatable of the value at the given acceptable index. If the index is not valid, or if the value does not have a metatable, the function returns false and pushes nothing on the stack.

Returns:
true if the metatable was pushed onto the stack, false otherwise

insert

public void insert(int index)
Insert the top item somewhere in the stack. [-1, +1, -]

Moves the top element into the given valid index, shifting up the elements above this index to open space. Cannot be called with a pseudo-index, because a pseudo-index is not an actual stack position.


isboolean

public boolean isboolean(int index)
Test if a value is boolean. [-0, +0, -]

Returns 1 if the value at the given acceptable index has type boolean, and 0 otherwise.


isfunction

public boolean isfunction(int index)
Test if a value is a function. [-0, +0, -]

Returns true if the value at the given acceptable index is a function (either C or Lua), and false otherwise.


isnil

public boolean isnil(int index)
Test if a value is nil [-0, +0, -]

Returns 1 if the value at the given acceptable index is nil, and 0 otherwise.


isnoneornil

public boolean isnoneornil(int index)
Test if a value is nil or not valid [-0, +0, -]

Returns 1 if the the given acceptable index is not valid (that is, it refers to an element outside the current stack) or if the value at this index is nil, and 0 otherwise.


isnumber

public boolean isnumber(int index)
Test if a value is a number [-0, +0, -]

Returns 1 if the value at the given acceptable index is a number or a string convertible to a number, and 0 otherwise.


tolnumber

public LValue tolnumber(int index)
Convert a value to an LNumber[-0, +0, -]

Returns an LNumber if the value at the given acceptable index is a number or a string convertible to a number, and LNil.NIL otherwise.


isstring

public boolean isstring(int index)
Test if a value is a string [-0, +0, m]

Returns 1 if the value at the given acceptable index is a string or a number (which is always convertible to a string), and 0 otherwise.


istable

public boolean istable(int index)
Test if a value is a table [-0, +0, -]

Returns 1 if the value at the given acceptable index is a table, and 0 otherwise.


isthread

public boolean isthread(int index)
Test if a value is a thread [-0, +0, -]

Returns 1 if the value at the given acceptable index is a thread, and 0 otherwise.


isuserdata

public boolean isuserdata(int index)
Test if a value is a userdata [-0, +0, -]

Returns 1 if the value at the given acceptable index is a userdata (either full or light), and 0 otherwise.


pop

public void pop(int n)
Pops n elements from the stack. [-n, +0, -]


poplvalue

public LValue poplvalue()

pushlvalue

public void pushlvalue(LValue value)
Push an LValue onto the stack. [-0, +1, m]


pushboolean

public void pushboolean(boolean b)
Pushes a boolean value with value b onto the stack. [-0, +1, -]


pushinteger

public void pushinteger(int n)
Pushes a number with value n onto the stack. [-0, +1, -]


pushfunction

public void pushfunction(LFunction f)
Pushes a function onto the stack. [-0, +1, m]

This function receives an LFunction and pushes onto the stack a Lua value of type function that, when called, invokes the corresponding function.

Any function to be registered in Lua must follow the correct protocol to receive its parameters and return its results

See Also:
LFunction

pushlstring

public void pushlstring(LString s)
Push an LString onto the stack. [-0, +1, m]


pushlstring

public void pushlstring(byte[] bytes,
                        int offset,
                        int length)
Push string bytes onto the stack as a string. [-0, +1, m] Pushes the string pointed to by s with size len onto the stack. Lua makes (or reuses) an internal copy of the given string, so the memory at s can be freed or reused immediately after the function returns. The string can contain embedded zeros.


pushlstring

public void pushlstring(byte[] byteArray)
Push string bytes onto the stack as a string. [-0, +1, m] Pushes the bytes in byteArray onto the stack as a lua string.


pushnil

public void pushnil()
Pushes a nil value onto the stack. [-0, +1, -]


pushnumber

public void pushnumber(double d)
Pushes a number with value d onto the stack. [-0, +1, -]


pushstring

public void pushstring(java.lang.String s)
Push a String onto the stack. [-0, +1, m]

Pushes the String s onto the stack. Lua makes (or reuses) an internal copy of the given string, so the memory at s can be freed or reused immediately after the function returns. The string cannot contain embedded zeros; it is assumed to end at the first zero.


pushvalue

public void pushvalue(int index)
Push a value from the stack onto the stack. [-0, +1, -]

Pushes a copy of the element at the given valid index onto the stack.


rawgeti

public void rawgeti(int index,
                    int n)
Deprecated. should get the table and do a raw get instead

Do a integer-key table get without metadata calls. [-0, +1, -]

Pushes onto the stack the value t[n], where t is the value at the given valid index. The access is raw; that is, it does not invoke metamethods.


remove

public void remove(int index)
Remove an element from the stack. [-1, +0, -]

Removes the element at the given valid index, shifting down the elements above this index to fill the gap. Cannot be called with a pseudo-index, because a pseudo-index is not an actual stack position.


replace

public void replace(int index)
Replace an element on the stack. [-1, +0, -]

Moves the top element into the given position (and pops it), without shifting any element (therefore replacing the value at the given position).


setfield

public void setfield(int index,
                     LString k)
Set the value of a table field. [-1, +0, e]

Does the equivalent to t[k] = v, where t is the value at the given valid index and v is the value at the top of the stack.

This function pops the value from the stack. As in Lua, this function may trigger a metamethod for the "newindex" event (see §2.8).


setglobal

public void setglobal(java.lang.String name)
Set the value of a global variable. [-1, +0, e]

Pops a value from the stack and sets it as the new value of global name. It is defined as a macro:

         #define lua_setglobal(L,s)   lua_setfield(L, LUA_GLOBALSINDEX, s)
        
 


toboolean

public boolean toboolean(int index)
Get a value as a boolean. [-0, +0, -]

Converts the Lua value at the given acceptable index to a C boolean value (0 or 1). Like all tests in Lua, lua_toboolean returns 1 for any Lua value different from false and nil; otherwise it returns 0. It also returns 0 when called with a non-valid index. (If you want to accept only actual boolean values, use lua_isboolean to test the value's type.)


tointeger

public int tointeger(int index)
Get a value as an int. [-0, +0, -]

Converts the Lua value at the given acceptable index to the signed integral type lua_Integer. The Lua value must be a number or a string convertible to a number (see §2.2.1); otherwise, lua_tointeger returns 0.

If the number is not an integer, it is truncated in some non-specified way.


tofunction

public LValue tofunction(int index)
Get a value as a LFunction.

tofunction

[-0, +0, -]

 LFunction tofunction (lua_State *L, int index);
 

Converts a value at the given acceptable index to a C function. That value must be a function; otherwise, returns NULL.


tolstring

public LString tolstring(int index)
Gets the value of a string as byte array. [-0, +0, m]

Converts the Lua value at the given acceptable index to a C string. If len is not NULL, it also sets *len with the string length. The Lua value must be a string or a number; otherwise, the function returns NULL. If the value is a number, then lua_tolstring also changes the actual value in the stack to a string. (This change confuses lua_next when lua_tolstring is applied to keys during a table traversal.)

lua_tolstring returns a fully aligned pointer to a string inside the Lua state. This string always has a zero ('\0') after its last character (as in C), but may contain other zeros in its body. Because Lua has garbage collection, there is no guarantee that the pointer returned by lua_tolstring will be valid after the corresponding value is removed from the stack.


tonumber

public double tonumber(int index)
Convert a value to a double. [-0, +0, -]

Converts the Lua value at the given acceptable index to the C type lua_Number (see lua_Number). The Lua value must be a number or a string convertible to a number (see §2.2.1); otherwise, lua_tonumber returns 0.


gettop

public int gettop()
Returns the index of the top element in the stack. [-0, +0, -]

Because indices start at 1, this result is equal to the number of elements in the stack (and so 0 means an empty stack).


settop

public void settop(int nt)
Set the top of the stack. [-?, +?, -]

Accepts any acceptable index, or 0, and sets the stack top to this index. If the new top is larger than the old one, then the new elements are filled with nil. If index is 0, then all stack elements are removed.


resettop

public void resettop()
Set the top to the base. Equivalent to settop(0)


topointer

public LValue topointer(int index)
Get the raw Object at a stack location. [-0, +0, -]

Converts the value at the given acceptable index to a generic C pointer (void*). The value may be a userdata, a table, a thread, or a function; otherwise, lua_topointer returns NULL. Different objects will give different pointers. There is no way to convert the pointer back to its original value.

Typically this function is used only for debug information.


tostring

public java.lang.String tostring(int index)
Get a stack value as a String. [-0, +0, m]

Equivalent to lua_tolstring with len equal to NULL.


totable

public LTable totable(int index)
Get a value from the stack as a lua table. [-0, +0, -]

Converts the value at the given acceptable index to a Lua table This value must be a table otherwise, the function returns NIL.


touserdata

public java.lang.Object touserdata(int index)
Get the Object from a userdata value. [-0, +0, -]

If the value at the given acceptable index is a full userdata, returns its block address. If the value is a light userdata, returns its pointer. Otherwise, returns NULL.


type

public int type(int index)
Get the type of a value. [-0, +0, -]

Returns the type of the value in the given acceptable index, or LUA_TNONE for a non-valid index (that is, an index to an "empty" stack position). The types returned by lua_type are coded by the following constants defined in lua.h: LUA_TNIL, LUA_TNUMBER, LUA_TBOOLEAN, LUA_TSTRING, LUA_TTABLE, LUA_TFUNCTION, LUA_TUSERDATA, LUA_TTHREAD, and LUA_TLIGHTUSERDATA.


typename

public java.lang.String typename(int index)
Get the type name for a value. [-0, +0, -]

Returns the name of the type encoded by the value tp, which must be one the values returned by lua_type.


xmove

public void xmove(LuaState to,
                  int n)
Exchange values between threads. [-?, +?, -]

Exchange values between different threads of the same global state.

This function pops n values from the stack from, and pushes them onto the stack to.


pushboolean

public void pushboolean(java.lang.Boolean b)
Push a Java Boolean value, or nil if the value is null.

Parameters:
b - Boolean value to convert, or null to to nil.

pushinteger

public void pushinteger(java.lang.Byte b)
Push a Java Byte value, or nil if the value is null.

Parameters:
b - Byte value to convert, or null to to nil.

pushinteger

public void pushinteger(java.lang.Character c)
Push a Java Character value, or nil if the value is null.

Parameters:
c - Character value to convert, or null to to nil.

pushnumber

public void pushnumber(java.lang.Double d)
Push a Java Double as a double, or nil if the value is null.

Parameters:
d - Double value to convert, or null to to nil.

pushnumber

public void pushnumber(java.lang.Float f)
Push a Java Float value, or nil if the value is null.

Parameters:
f - Float value to convert, or null to to nil.

pushinteger

public void pushinteger(java.lang.Integer i)
Push a Java Integer value, or nil if the value is null.

Parameters:
i - Integer value to convert, or null to to nil.

pushinteger

public void pushinteger(java.lang.Short s)
Push a Java Short value, or nil if the value is null.

Parameters:
s - Short value to convert, or null to to nil.

pushnumber

public void pushnumber(java.lang.Long l)
Push a Java Long value, or nil if the value is null.

Parameters:
l - Long value to convert, or null to to nil.

pushuserdata

public void pushuserdata(java.lang.Object o)
Push a Java Object as userdata, or nil if the value is null.

Parameters:
o - Object value to push, or null to to nil.

toboxedboolean

public java.lang.Boolean toboxedboolean(int index)
Convert a value to a Java Boolean value, or null if the value is nil.

Parameters:
index - index of the parameter to convert.
Returns:
Boolean value at the index, or null if the value was not a boolean.

toboxedbyte

public java.lang.Byte toboxedbyte(int index)
Convert a value to a Java Byte value, or null if the value is not a number.

Parameters:
index - index of the parameter to convert.
Returns:
Byte value at the index, or null if the value was not a number.

toboxeddouble

public java.lang.Double toboxeddouble(int index)
Convert a value to a Java Double value, or null if the value is not a number.

Parameters:
index - index of the parameter to convert.
Returns:
Double value at the index, or null if the value was not a number.

toboxedfloat

public java.lang.Float toboxedfloat(int index)
Convert a value to a Java Float value, or null if the value is not a number.

Parameters:
index - index of the parameter to convert.
Returns:
Float value at the index, or null if the value was not a boolean.

toboxedinteger

public java.lang.Integer toboxedinteger(int index)
Convert a value to a Java Integer value, or null if the value is not a number.

Parameters:
index - index of the parameter to convert.
Returns:
Integer value at the index, or null if the value was not a number.

toboxedlong

public java.lang.Long toboxedlong(int index)
Convert a value to a Java Long value, or null if the value is nil.

Parameters:
index - index of the parameter to convert.
Returns:
Long value at the index, or null if the value was not a number.

argerror

public void argerror(int narg,
                     java.lang.String extramsg)
Report an error with an argument.

Parameters:
narg - Stack index of the bad argument
extramsg - String to include in error message

argcheck

public void argcheck(boolean cond,
                     int narg,
                     java.lang.String extramsg)
Conditionally report an error with an argument.

Parameters:
cond - boolean condition that generates an error when false
narg - Stack index of the bad argument
extramsg - String to include in error message

typerror

public void typerror(int narg,
                     java.lang.String typename)
Report a type error.

Parameters:
narg - Stack index of the bad argument
typename - Name of the type that was expected, such as "string"

typerror

public void typerror(int narg,
                     int typenum)
Report a type error.

Parameters:
narg - Stack index of the bad argument
typenum - Constant value specifying the type of argument that was expected (i.e. LUA_TSTRING).

checkany

public LValue checkany(int narg)
Checks whether the function has an argument of any type (including nil) at position narg.

Parameters:
narg - the argument number
Returns:
the value at the index
Throws:
LuaErrorException - if there is no argument at position narg

checkfunction

public LFunction checkfunction(int narg)
Checks whether the function argument narg is a function and returns this function.

Parameters:
narg - the argument number
Returns:
LFunction value if the argument is a function
Throws:
LuaErrorException - if the value is not a function
See Also:
LFunction

checkthread

public LThread checkthread(int narg)
Checks whether the function argument narg is a thread and returns this thread.

Parameters:
narg - the argument number
Returns:
LThread value if the argument is a thread
Throws:
LuaErrorException - if the value is not a thread
See Also:
LThread

checkint

public int checkint(int narg)
Checks whether the function argument narg is a number and returns this number cast to an int.

Parameters:
narg - the argument number
Returns:
int value if the argument is an int or can be converted to one
Throws:
LuaErrorException - if the number cannot be converted to an int

checkinteger

public LInteger checkinteger(int narg)
Checks whether the function argument narg is a number and returns this number cast to a LInteger.

Parameters:
narg - the argument number
Returns:
LInteger value if the argument is an int or can be converted to one
Throws:
LuaErrorException - if the value cannot be converted to an int
See Also:
LInteger

checklong

public long checklong(int narg)
Checks whether the function argument narg is a number and returns this number cast to a long.

Parameters:
narg - the argument number
Returns:
long value if the argument is a number or can be converted to long
Throws:
LuaErrorException - if the value cannot be converted to a long

checkdouble

public double checkdouble(int narg)
Checks whether the function argument narg is a number and returns this number cast to a double.

Parameters:
narg - the argument number
Returns:
long value if the argument is a number or can be converted to double
Throws:
LuaErrorException - if the value cannot be converted to a double

checknumber

public LNumber checknumber(int narg)
Checks whether the function argument narg is a number and returns this number.

Parameters:
narg - the argument number
Returns:
LNumber value if the argument is a number or can be converted to one
Throws:
LuaErrorException - if the value cannot be converted to a number
See Also:
LNumber

checklstring

public LString checklstring(int narg)
Checks whether the function argument narg is a string and returns this string as a lua string.

Parameters:
narg - the argument number
Returns:
LString value if the argument is a string or can be converted to one
Throws:
LuaErrorException - if the value cannot be converted to a string
See Also:
LString

checkstring

public java.lang.String checkstring(int narg)
Checks whether the function argument narg is a string and returns this string as a Java String.

Parameters:
narg - the argument number
Returns:
String value if the argument is a string or can be converted to one
Throws:
LuaErrorException - if the value cannot be converted to a string

checktable

public LTable checktable(int narg)
Checks whether the function argument narg is a table and returns this table.

Parameters:
narg - the argument number
Returns:
LTable value if the argument is a table
Throws:
LuaErrorException - if the value is not a table
See Also:
LTable

checktype

public LValue checktype(int narg,
                        int t)
Checks whether the function argument narg has type t and return it as an LValue.

Parameters:
narg - the argument number
t - the type number to check against
Returns:
the lua value
Throws:
LuaErrorException - if the value is not of type t

checkudata

public java.lang.Object checkudata(int ud,
                                   java.lang.Class expected)
Check that the type of userdata on the stack matches the required type, and if so, return the Java Object the userdata value points to.

Parameters:
ud - Stack index of the argument to check
expected - Class that the userdata is expected to have an instance of.

optint

public int optint(int narg,
                  int d)
If the function argument narg is a number, returns this number cast to an int. If this argument is absent or is nil, returns d. Otherwise, raises an error.

Parameters:
narg - the argument number
d - the default value when the argument is nil or not supplied
Returns:
int value if the argument is an int, or d
Throws:
LuaErrorException - if the value cannot be converted to an int

optinteger

public LInteger optinteger(int narg,
                           int d)
If the function argument narg is a number, returns this number cast to a lua_Integer. If this argument is absent or is nil, returns d. Otherwise, raises an error.

Parameters:
narg - the argument number
d - the default value when the argument is nil or not supplied
Returns:
int value if the argument is an int, or d
Throws:
LuaErrorException - if the value cannot be converted to an int

optlong

public long optlong(int narg,
                    long d)
If the function argument narg is a number, returns this number cast to a long. If this argument is absent or is nil, returns d. Otherwise, raises an error.

Parameters:
narg - the argument number
d - the default value when the argument is nil or not supplied
Returns:
int value if the argument is an number, or d
Throws:
LuaErrorException - if the value cannot be converted to an number

optnumber

public LNumber optnumber(int narg,
                         LNumber d)
If the function argument narg is a number, returns this number. If this argument is absent or is nil, returns d. Otherwise, raises an error.

Parameters:
narg - the argument number
d - the default value when the argument is nil or not supplied
Returns:
int value if the argument is an number, or d
Throws:
LuaErrorException - if the value cannot be converted to an number

optlstring

public LString optlstring(int narg,
                          LString d)
If the function argument narg is a string, returns this string. If this argument is absent or is nil, returns d. Otherwise, raises an error.


optstring

public java.lang.String optstring(int narg,
                                  java.lang.String d)
If the function argument narg is a string, returns this string. If this argument is absent or is nil, returns d. Otherwise, raises an error.


vmerror

public static void vmerror(java.lang.String description)
Method to indicate a vm internal error has occurred. Generally, this is not recoverable, so we convert to a lua error during production so that the code may recover.


call

public LValue call(LFunction function)
Call a function with no arguments and one return value. This may change values in the current stack frame.

Parameters:
function -
Returns:

call

public LValue call(LFunction function,
                   LValue arg0)
Call a function with one argument and one return value This may change values in the current stack frame.

Parameters:
function -
arg0 -
Returns:

luaV_call_index

public LValue luaV_call_index(LFunction function,
                              LValue table,
                              LValue key)
Call an index function with two arguments and one return value Values in the current stack frame will be preserved.

Parameters:
function - the __index function to call
table - the table on which the metadata operation is taking place
key - the key used in the index operation
Returns:
the value that results from the metatable operation

luaV_call_newindex

public LValue luaV_call_newindex(LFunction function,
                                 LValue table,
                                 LValue key,
                                 LValue value)
Call a newindex function with three arguments and one return value Values in the current stack frame will be preserved.

Parameters:
function - the __newindex function to call
table - the table on which the metadata operation is taking place
key - the key used in the newindex operation
value - the value beting set in the newindex operation
Returns:
the value that results from the metatable operation

sethook

public void sethook(LFunction func,
                    int mask,
                    int count)
Set the hook function.

Parameters:
func - LFunction to call on the hook event
mask - combination of LuaState.LUA_MASKLINE, LuaState.LUA_MASKCALL, and LuaState.LUA_MASKRET
count - 0, or number of bytecodes between count events.

gethook

public LFunction gethook()
Get the current hook function, if any.

Returns:
LFunction that is set as the current hook function, or null if not set.

gethookcount

public int gethookcount()
Get the current hook count.

Returns:
current count, which is # of bytecodes between "count" hook calls

gethookmask

public int gethookmask()
Get the current hook mask.

Returns:
current mask as a combination of LuaState.LUA_MASKLINE, LuaState.LUA_MASKCALL, and LuaState.LUA_MASKRET


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