Interface Lua
- All Superinterfaces:
AutoCloseable,LuaThread
lua_State * wrapper, representing a Lua thread
Most methods in this interface are wrappers around the corresponding Lua C API functions,
and requires a certain degree of familiarity with the Lua C API.
If you are not that familiar with the Lua C API, you may want to read the Lua manual first
or try out LuaValue-related API at the LuaThread interface.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic enumControls the degree of conversion from Java to Luastatic enumLua data types -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe global key used to store Java exceptions in Lua. -
Method Summary
Modifier and TypeMethodDescriptionvoidcheckStack(int extra) Ensures that there are at leastextrafree stack slots in the Lua stackvoidclose()Closes the threadvoidconcat(int n) Concatenates the n values at the top of the stack, pops them, and leaves the result at the topcreateProxy(Class<?>[] interfaces, Lua.Conversion degree) Creates a proxy object, implementing all the specified interfaces, with a Lua table / function on top of the stackvoidcreateTable(int nArr, int nRec) Creates a new empty table and pushes it onto the stack@Nullable ByteBufferdump()Dumps a function as a binary chunkbooleanequal(int i1, int i2) Returns true if the two values in acceptable indices i1 and i2 are equalintSets the Lua globalGLOBAL_THROWABLEto the throwablevoidThrows an error inside a Lua environmentvoidgc()Performs a full garbage-collection cycleget()Pops the value on top of the stack and return a LuaValue referring to itvoidPushes onto the stack the value t[key]voidPushes onto the stack the value of the globalnameintgetId()Returns the unique identifier of this Lua thread.@Nullable ThrowableFetches the most recent JavaThrowablepassed to LuaReturns the underlying native Lua interface.Returns the main Lua state.intgetMetaField(int index, String field) Pushes onto the stack the fieldfieldfrom the metatable of the object at indexindexintgetMetatable(int index) Pushes onto the stack the metatable of the value at the given acceptable indexlongReturns the pointer to the internal Lua state.voidgetRegisteredMetatable(String typeName) Pushes onto the stack the metatable associated with name tname in the registryvoidgetTable(int index) Pushes onto the stack the value t[k]intgetTop()Returns the index of the top element in the stackvoidinsert(int index) Moves the top element into the given valid index, shifting up the elements above this indexbooleanisBoolean(int index) Returns true if the value at the given index is a boolean, and false otherwisebooleanisFunction(int index) Returns true if the value at the given index is a function (either C or Lua), and false otherwisebooleanisInteger(int index) Returns true if the value at the given index is an integer, and false otherwisebooleanisJavaObject(int index) Checks if the element is a Java objectbooleanisNil(int index) Returns true if the value at the given index is nil, and false otherwisebooleanisNone(int index) Returns true if the given index is not valid, and false otherwisebooleanisNoneOrNil(int index) Returns true if the given index is not valid or if the value at this index is nil, and false otherwisebooleanisNumber(int index) Returns true if the value is a number or a string convertible to a number, and false otherwisebooleanisString(int index) Returns true if the value at the given index is a string or a numberbooleanisTable(int index) Returns true if the value at the given index is a table, and false otherwise.booleanisThread(int index) Returns true if the value at the given index is a thread, and false otherwise.booleanisUserdata(int index) Returns true if the value at the given index is userdata (either full or light), and false otherwise.booleanlessThan(int i1, int i2) Returns true if the value at acceptable index i1 is smaller than the value at i2voidLoads a string as a Lua chunkvoidLoads a buffer as a Lua chunkvoidloadExternal(String module) Loads a chunk from aExternalLoaderset bysetExternalLoader(ExternalLoader)intnewRegisteredMetatable(String typeName) Creates a new table to be used as a metatable for userdata, adds it to the registryvoidnewTable()Creates a new empty table and pushes it onto the stackCreates a new thread, pushes it on the stackintnext(int n) Pops a key from the stack, and pushes a key-value pair from the table at the given indexvoidOpens all standard Lua libraries into the given statevoidopenLibrary(String name) Opens a specific library into the given statevoidpCall(int nArgs, int nResults) Calls a function in protected modevoidpop(int n) Pops n elements from the stackvoidpush(boolean bool) Pushes a boolean value onto the stackvoidpush(long integer) Pushes an integer onto the stackvoidpush(@Nullable Object object, Lua.Conversion degree) Push an object onto the stack, converting according toLua.Conversion.voidPushes a floating-point number onto the stackvoidPushes a string onto the stack.voidpush(ByteBuffer buffer) Pushes a buffer as a raw string onto the stackvoidpush(Collection<?> collection) Push the element onto the stack, converted to lua tables (index starting from 1)voidPush the element onto the stack, converted to lua tablesvoidPush the function onto the stack, converted to a callable elementvoidpush(LuaFunction value) Push the function onto the stack, converted to a callable elementvoidPush aLuaValueonto the stack, equivalent toLuaValue.push(Lua)voidPush an array onto the stack, converted to luatablevoidpushJavaArray(Object array) Push the element onto the stack, converted as is to Java arraysvoidpushJavaClass(Class<?> clazz) Push a class onto the stack, which may be used with `java.new` on the lua sidevoidpushJavaObject(Object object) Push the element onto the stack, converted as is to Java objectsvoidpushNil()Pushes anilvalue onto the stackvoidPushes the current thread onto the stackvoidpushValue(int index) Pushes a copy of the element at the given valid index onto the stackbooleanrawEqual(int i1, int i2) Returns true if the two values in acceptable indices i1 and i2 are primitively equalvoidrawGet(int index) Similar togetTable(int), but does a raw access (i.e., without metamethods)voidrawGetI(int index, int n) Pushes onto the stack the value t[n], where t is the value at the given valid indexintrawLength(int index) Returns the raw "length" of the value at the given indexvoidrawSet(int index) Similar tosetTable(int), but does a raw assignment (i.e., without metamethods)voidrawSetI(int index, int n) Does the equivalent of t[n] = vintref()Callsref(int)with the pseudo-indexLUA_REGISTRYINDEXintref(int index) Creates and returns a reference, in the table at indexindexvoidrefGet(int ref) voidremove(int index) Removes the element at the given valid indexvoidreplace(int index) Moves the top element into the given position (and pops it)booleanresume(int nArgs) Starts and resumes a coroutine in a given threadvoidLoads and runs the given stringvoidLoads and runs a buffervoidsetExternalLoader(ExternalLoader loader) Sets aExternalLoaderfor the main statevoidDoes the equivalent to t[key] = vvoidPops a value from the stack and sets it as the new value of globalnamevoidsetMetatable(int index) Pops a table from the stack and sets it as the new metatable for the value at the given acceptable indexvoidsetTable(int index) Does the equivalent to t[k] = vvoidsetTop(int index) Accepts any index, or 0, and sets the stack top to this indexstatus()Returns the status of the threadbooleantoBoolean(int index) Converts the Lua value at the given acceptable index to a boolean value@Nullable ByteBuffertoBuffer(int index) Creates aByteBufferfrom the string at the specific index@Nullable ByteBuffertoDirectBuffer(int index) Creates a read-only directByteBufferfrom the string at the specific indexlongtoInteger(int index) Converts the Lua value at the given acceptable index to the signed integral type lua_Integer@Nullable ObjecttoJavaObject(int index) Get the element at the specified stack position, if the element is a Java object / array / class@Nullable List<?>toList(int index) Get the element at the specified stack position, converted toList@Nullable Map<?,?> toMap(int index) Get the element at the specified stack position, converted to aMapdoubletoNumber(int index) Converts the Lua value at the given acceptable index to a number@Nullable ObjecttoObject(int index) Automatically converts a value into a Java object nil is converted tonull. boolean converted tobooleanor the boxedBoolean. integer / number to any ofcharbyteshortintlongfloatdoubleor their boxed alternative. string toString. table toMap<Object, Object>, converted recursively. jclass toClass<?>. jobject to the underlying Java object. Other types are not converted and arenullon the Java side.@Nullable ObjectConverts a value at the stack index@Nullable StringtoString(int index) Converts the Lua value at the given acceptable index to a string@Nullable Lua.LuaTypetype(int index) Returns the Lua type of the element at the given index.voidunref(int ref) voidunRef(int index, int ref) Releases reference ref from the table at indexindexvoidExchange values between different threads of the same global statevoidyield(int n) Yields a coroutine
-
Field Details
-
GLOBAL_THROWABLE
The global key used to store Java exceptions in Lua.- See Also:
-
-
Method Details
-
checkStack
Ensures that there are at leastextrafree stack slots in the Lua stackIt wraps
lua_checkstack.- Parameters:
extra- the extra slots to ensure- Throws:
RuntimeException- when unable to grow the stack
-
push
Push an object onto the stack, converting according toLua.Conversion.- Parameters:
object- the object to be pushed onto the stackdegree- how the object is converted into lua values- See Also:
-
pushNil
void pushNil()Pushes anilvalue onto the stack -
push
void push(boolean bool) Pushes a boolean value onto the stack- Parameters:
bool- the boolean value
-
push
Pushes a floating-point number onto the stack- Parameters:
number- the number, whoseNumber.doubleValue()will be pushed
-
push
void push(long integer) Pushes an integer onto the stackPlease note that on some 32-bit platforms, 64-bit integers are likely to get truncated instead of getting approximated into a floating-point number. If you want to approximate an integer, cast it to double and use
push(Number).- Parameters:
integer- the number
-
push
Pushes a string onto the stack.- Parameters:
string- the string
-
push
Pushes a buffer as a raw string onto the stackThe pushed bytes are from buffer[buffer.position()] to buffer[buffer.limit() - 1]. So remember to call
ByteBuffer.flip()or set the position and limit before pushing.- Parameters:
buffer- the buffer, which might contain invalid UTF-8 characters and zeros
-
push
Push the element onto the stack, converted to lua tablesInner elements are converted with
Lua.Conversion.FULL.- Parameters:
map- the element to be pushed onto the stack
-
push
Push the element onto the stack, converted to lua tables (index starting from 1)Inner elements are converted with
Lua.Conversion.FULL.- Parameters:
collection- the element to be pushed onto the stack
-
pushArray
Push an array onto the stack, converted to luatable- Parameters:
array- a array- Throws:
IllegalArgumentException- when the object is not array
-
push
Push the function onto the stack, converted to a callable elementThe function is wrapped into a C closure, which means Lua will treat the function as a C function. Checking
isFunction(int)on the pushed element will return true.- Parameters:
function- the function to be pushed onto the stack
-
pushJavaClass
Push a class onto the stack, which may be used with `java.new` on the lua side- Parameters:
clazz- the class
-
push
Push aLuaValueonto the stack, equivalent toLuaValue.push(Lua)- Parameters:
value- the value
-
push
Push the function onto the stack, converted to a callable element- Parameters:
value- the function- See Also:
-
pushJavaObject
Push the element onto the stack, converted as is to Java objects- Parameters:
object- the element to be pushed onto the stack- Throws:
IllegalArgumentException- when argument isnullor an array
-
pushJavaArray
Push the element onto the stack, converted as is to Java arrays- Parameters:
array- the element to be pushed onto the stack- Throws:
IllegalArgumentException- when argument isnullor a non-array object
-
toNumber
double toNumber(int index) Converts the Lua value at the given acceptable index to a numberThe Lua value must be a number or a string convertible to a number; otherwise,
lua_tonumberreturns 0.- Parameters:
index- the stack index- Returns:
- the converted value, zero if not convertible
-
toInteger
long toInteger(int index) Converts the Lua value at the given acceptable index to the signed integral type lua_IntegerThe Lua value must be a number or a string convertible to a number; otherwise,
lua_tointegerreturns 0. If the number is not an integer, it is truncated in some non-specified way.- Parameters:
index- the stack index- Returns:
- the converted value, zero if not convertible
-
toBoolean
boolean toBoolean(int index) Converts the Lua value at the given acceptable index to a boolean valueLike all tests in Lua,
lua_tobooleanreturns 1 for any Lua value different fromfalseandnil; otherwise it returns 0. It also returns 0 when called with a non-valid index.- Parameters:
index- the stack index- Returns:
- the converted value,
falsewith and only withfalse,nilorTNONE
-
toObject
Automatically converts a value into a Java object- nil is converted to
null. - boolean converted to
booleanor the boxedBoolean. - integer / number to any of
charbyteshortintlongfloatdoubleor their boxed alternative. - string to
String. - table to
Map<Object, Object>, converted recursively. - jclass to
Class<?>. - jobject to the underlying Java object.
- Other types are not converted and are
nullon the Java side.
- Parameters:
index- the stack index- Returns:
- the converted object,
nullif unable to converted
- nil is converted to
-
toObject
Converts a value at the stack index- Parameters:
index- the stack indextype- the target type- Returns:
- the converted value,
nullif unable to converted - See Also:
-
toString
Converts the Lua value at the given acceptable index to a stringThe 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.
- Parameters:
index- the stack index- Returns:
- the converted string
-
toBuffer
Creates aByteBufferfrom the string at the specific indexYou may want to use this instead of
toString(int)when the string is binary (e.g., those returned bystring.dumpand contains null characters).- Parameters:
index- the stack index- Returns:
- the created buffer
-
toDirectBuffer
Creates a read-only directByteBufferfrom the string at the specific indexThe memory of this buffer is managed by Lua. So you should never use the buffer after popping the corresponding value from the Lua stack.
- Parameters:
index- the stack index- Returns:
- the created read-only buffer
-
toJavaObject
Get the element at the specified stack position, if the element is a Java object / array / class- Parameters:
index- the stack position of the element- Returns:
- the Java object or null
-
toMap
Get the element at the specified stack position, converted to aMapThe element may be a Lua table or a Java
Map, or else, it returns null.- Parameters:
index- the stack position of the element- Returns:
- the map or null
-
toList
Get the element at the specified stack position, converted toListThe element may be a Lua table or a Java
List, or else, it returns null.- Parameters:
index- the stack position of the element- Returns:
- the list or null
-
isBoolean
boolean isBoolean(int index) Returns true if the value at the given index is a boolean, and false otherwise- Parameters:
index- the stack index- Returns:
- true if the value at the given index is a boolean, and false otherwise
-
isFunction
boolean isFunction(int index) Returns true if the value at the given index is a function (either C or Lua), and false otherwiseWhen one pushes a
JFunctiononto the stack usingpush(JFunction), theJFunctionis wrapped into a C closure, so that it is treated as a C function in Lua.- Parameters:
index- the stack index- Returns:
- true if the value at the given index is a function (either C or Lua), and false otherwise
-
isJavaObject
boolean isJavaObject(int index) Checks if the element is a Java objectNote that a
JFunctionpushed withpush(JFunction)is not a Java object any more, but a C function.- Parameters:
index- the element to check type for- Returns:
trueif the element is a Java object, a Java class, or a Java array
-
isNil
boolean isNil(int index) Returns true if the value at the given index is nil, and false otherwise- Parameters:
index- the stack index- Returns:
- true if the value at the given index is nil, and false otherwise
-
isNone
boolean isNone(int index) Returns true if the given index is not valid, and false otherwise- Parameters:
index- the stack index- Returns:
- true if the given index is not valid, and false otherwise
-
isNoneOrNil
boolean isNoneOrNil(int index) Returns true if the given index is not valid or if the value at this index is nil, and false otherwise- Parameters:
index- the stack index- Returns:
- true if the given index is not valid or if the value at this index is nil, and false otherwise
-
isNumber
boolean isNumber(int index) Returns true if the value is a number or a string convertible to a number, and false otherwise- Parameters:
index- the stack index- Returns:
- true if the value is a number or a string convertible to a number, and false otherwise
-
isInteger
boolean isInteger(int index) Returns true if the value at the given index is an integer, and false otherwise(that is, the value is a number and is represented as an integer)
- Parameters:
index- the stack index- Returns:
- true if the value is an integer, and false otherwise
-
isString
boolean isString(int index) Returns true if the value at the given index is a string or a number- Parameters:
index- the stack index- Returns:
- true if the value at the given index is a string or a number, and false otherwise.
-
isTable
boolean isTable(int index) Returns true if the value at the given index is a table, and false otherwise.- Parameters:
index- the stack index- Returns:
- Returns true if the value at the given index is a table, and false otherwise.
-
isThread
boolean isThread(int index) Returns true if the value at the given index is a thread, and false otherwise.- Parameters:
index- the stack index- Returns:
- true if the value at the given index is a thread, and 0 otherwise.
-
isUserdata
boolean isUserdata(int index) Returns true if the value at the given index is userdata (either full or light), and false otherwise.- Parameters:
index- the stack index- Returns:
- true if the value at the given index is userdata (either full or light), and false otherwise.
-
type
Returns the Lua type of the element at the given index.- Parameters:
index- the element to inspect- Returns:
- the lua type of the element,
nullif unrecognized (in, for example, incompatible lua versions)
-
equal
boolean equal(int i1, int i2) Returns true if the two values in acceptable indices i1 and i2 are equalReturns true if the two values in acceptable indices index1 and index2 are equal, following the semantics of the Lua == operator (that is, may call metamethods). Otherwise returns false. Also returns false if any of the indices is non valid.
- Parameters:
i1- the index of the first elementi2- the index of the second element- Returns:
- true if the two values in acceptable indices i1 and i2 are equal
-
rawLength
int rawLength(int index) Returns the raw "length" of the value at the given indexFor strings, this is the string length; for tables, this is the result of the length operator ('#') with no metamethods; for userdata, this is the size of the block of memory allocated for the userdata. For other values, this call returns 0.
- Parameters:
index- the stack index- Returns:
- the raw length of the element
-
lessThan
boolean lessThan(int i1, int i2) Returns true if the value at acceptable index i1 is smaller than the value at i2It follows the semantics of the Lua < operator (that is, may call metamethods). Otherwise returns false. Also returns false if any of the indices is non valid.
- Parameters:
i1- the index of the first elementi2- the index of the second element- Returns:
- true if the value at acceptable index i1 is smaller than the value at i2
-
rawEqual
boolean rawEqual(int i1, int i2) Returns true if the two values in acceptable indices i1 and i2 are primitively equalIt does not call metamethods. Otherwise returns false. Also returns false if any of the indices are non valid.
- Parameters:
i1- the index of the first elementi2- the index of the second element- Returns:
- true if the two values in acceptable indices i1 and i2 are primitively equal
-
getTop
int getTop()Returns the index of the top element in the stackBecause indices start at 1, this result is equal to the number of elements in the stack (and so 0 means an empty stack).
- Returns:
- the index of the top element in the stack
-
setTop
void setTop(int index) Accepts any index, or 0, and sets the stack top to this indexIf the new top is greater than the old one, then the new elements are filled with nil. If index is 0, then all stack elements are removed.
- Parameters:
index- the new top element index
-
insert
void insert(int index) Moves the top element into the given valid index, shifting up the elements above this indexMoves 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.
- Parameters:
index- the non-pseudo index- See Also:
-
pop
void pop(int n) Pops n elements from the stack- Parameters:
n- the number of elements to pop
-
pushValue
void pushValue(int index) Pushes a copy of the element at the given valid index onto the stack- Parameters:
index- the index of the element to be copied- See Also:
-
pushThread
void pushThread()Pushes the current thread onto the stack -
remove
void remove(int index) Removes the element at the given valid indexRemoves 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.
- Parameters:
index- the index of the element to be removed
-
replace
void replace(int index) Moves the top element into the given position (and pops it)Moves the top element into the given position (and pops it), without shifting any element (therefore replacing the value at the given position).
- Parameters:
index- the index to move to- See Also:
-
xMove
Exchange values between different threads of the same global stateThis function pops n values from the stack of this thread, and pushes them onto the stack of the other thread
- Parameters:
other- the thread to move the n values ton- the number of elements to move- Throws:
IllegalArgumentException- when the two threads do not belong to the same global state
-
load
Loads a string as a Lua chunkThis function eventually uses
lua_loadto load the chunk in the stringscript. Also as lua_load, this function only loads the chunk; it does not run it.- Parameters:
script- the Lua chunk- Throws:
LuaException- See Also:
-
load
Loads a buffer as a Lua chunkThis function eventually uses
lua_loadto load the chunk in the stringscript. Also as lua_load, this function only loads the chunk; it does not run it.The used contents are from buffer[buffer.position()] to buffer[buffer.limit() - 1]. So remember to call
ByteBuffer.flip()or set the position and limit before pushing.- Parameters:
buffer- the buffer, must be a direct buffername- the chunk name, used for debug information and error messages- Throws:
LuaException- See Also:
-
run
Loads and runs the given stringIt is equivalent to first calling
load(String)and thenpCall(int, int)the loaded chunk.- Parameters:
script- the Lua chunk- Throws:
LuaException
-
run
Loads and runs a bufferIt is equivalent to first calling
load(Buffer, String)and thenpCall(int, int)the loaded chunk.The used contents are from buffer[buffer.position()] to buffer[buffer.limit() - 1]. So remember to call
ByteBuffer.flip()or set the position and limit before pushing.- Parameters:
buffer- the buffer, must be a direct buffername- the chunk name, used for debug information and error messages- Throws:
LuaException
-
dump
@Nullable ByteBuffer dump()Dumps a function as a binary chunkReceives a Lua function on the top of the stack and produces a binary chunk that, if loaded again, results in a function equivalent to the one dumped.
- Returns:
- the binary chunk, null if an error occurred
-
pCall
Calls a function in protected modeTo 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.
- Parameters:
nArgs- the number of arguments that you pushed onto the stacknResults- the number of results to adjust to- Throws:
LuaException
-
newThread
Lua newThread()Creates a new thread, pushes it on the stackThe new state returned by this function shares with the original state all global objects (such as tables), but has an independent execution stack.
- Returns:
- a new thread
-
resume
Starts and resumes a coroutine in a given threadTo start a coroutine, you first create a new thread (see lua_newthread or
newThread()); then you push onto its stack the main function plus any arguments; then you call resume, with narg being the number of arguments. This call returns when the coroutine suspends or finishes its execution. When it returns, the stack contains all values passed to lua_yield, or all values returned by the body function. lua_resume returns LUA_YIELD if the coroutine yields, 0 if the coroutine finishes its execution without errors, or an error code in case of errors (see lua_pcall). In case of errors, the stack is not unwound, so you can use the debug API over it. The error message is on the top of the stack. To restart a coroutine, you put on its stack only the values to be passed as results from yield, and then call lua_resume.- Parameters:
nArgs- the number of arguments- Returns:
trueif the thread yielded, orfalseif it ended execution- Throws:
LuaException
-
status
LuaException.LuaError status()Returns the status of the thread- Returns:
- the status of the thread
-
yield
void yield(int n) Yields a coroutineThis is not implemented because we have no way to resume execution from a Java stack through a C stack back to a Java stack.
- Parameters:
n- the number of values from the stack that are passed as results to lua_resume- Throws:
UnsupportedOperationException- always
-
createTable
void createTable(int nArr, int nRec) Creates a new empty table and pushes it onto the stackThe new table has space pre-allocated for narr array elements and nrec non-array elements. This pre-allocation is useful when you know exactly how many elements the table will have.
- Parameters:
nArr- pre-allocated array elementsnRec- pre-allocated non-array elements
-
newTable
void newTable()Creates a new empty table and pushes it onto the stackIt is equivalent to
createTable(0, 0). -
getField
Pushes onto the stack the value t[key]Pushes onto the stack the value t[key], where t is the value at the given valid index. As in Lua, this function may trigger a metamethod for the "index" event.
- Parameters:
index- the index of the table-like elementkey- the key to look up
-
setField
Does the equivalent to t[key] = vDoes the equivalent to t[key] = 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.
- Parameters:
index- the index of the table-like elementkey- the key to assign to
-
getTable
void getTable(int index) Pushes onto the stack the value t[k]Pushes onto the stack the value t[k], where t is the value at the given valid index and k is the value at the top of the stack. This function pops the key from the stack (putting the resulting value in its place). As in Lua, this function may trigger a metamethod for the "index" event.
- Parameters:
index- the index of the table-like element
-
setTable
void setTable(int index) Does the equivalent to t[k] = vDoes the equivalent to t[k] = v, where t is the value at the given valid index, v is the value at the top of the stack, and k is the value just below the top. This function pops both the key and the value from the stack. As in Lua, this function may trigger a metamethod for the "newindex" event.
- Parameters:
index- the index of the table-like element
-
next
int next(int n) Pops a key from the stack, and pushes a key-value pair from the table at the given indexPops a key from the stack, and pushes a key-value pair from the table at the given index (the "next" pair after the given key). If there are no more elements in the table, then lua_next returns 0 (and pushes nothing).
A typical traversal looks like this:
/* table is in the stack at index 't' */ lua_pushnil(L); /* first key */ while (lua_next(L, t) != 0) { /* uses 'key' (at index -2) and 'value' (at index -1) */ printf("%s - %s\n", lua_typename(L, lua_type(L, -2)), lua_typename(L, lua_type(L, -1))); /* removes 'value'; keeps 'key' for next iteration */ lua_pop(L, 1); }While traversing a table, do not call
toString(int)directly on a key, unless you know that the key is actually a string. Recall thattoString(int)changes the value at the given index; this confuses the next call to lua_next.- Parameters:
n- the index of the table- Returns:
- 0 if there are no more elements
-
rawGet
void rawGet(int index) Similar togetTable(int), but does a raw access (i.e., without metamethods)- Parameters:
index- the index of the table
-
rawGetI
void rawGetI(int index, int n) Pushes onto the stack the value t[n], where t is the value at the given valid indexThe access is raw; that is, it does not invoke metamethods.
- Parameters:
index- the index of the tablen- the key
-
rawSet
void rawSet(int index) Similar tosetTable(int), but does a raw assignment (i.e., without metamethods)- Parameters:
index- the index of the table
-
rawSetI
void rawSetI(int index, int n) Does the equivalent of t[n] = vDoes the equivalent of t[n] = 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. The assignment is raw; that is, it does not invoke metamethods.
- Parameters:
index- the index of the tablen- the key
-
ref
int ref(int index) Creates and returns a reference, in the table at indexindexCreates and returns a reference, in the table at index t, for the object at the top of the stack (and pops the object).
A reference is a unique integer key. As long as you do not manually add integer keys into table t, luaL_ref ensures the uniqueness of the key it returns. You can retrieve an object referred by reference r by calling
rawGetI(int, int). FunctionunRef(int, int)frees a reference and its associated object.- Parameters:
index- the index of the table- Returns:
- the created reference
-
ref
int ref()Callsref(int)with the pseudo-indexLUA_REGISTRYINDEX- Returns:
- the created reference
-
refGet
void refGet(int ref) - Parameters:
ref- the reference onLUA_REGISTRYINDEXtable
-
unRef
void unRef(int index, int ref) Releases reference ref from the table at indexindexThe entry is removed from the table, so that the referred object can be collected. The reference ref is also freed to be used again.
- Parameters:
index- the index of the tableref- the reference to be freed
-
unref
void unref(int ref) - Parameters:
ref- the reference to be freed
-
getGlobal
Pushes onto the stack the value of the globalname- Parameters:
name- the global name
-
setGlobal
Pops a value from the stack and sets it as the new value of globalname- Parameters:
name- the global name
-
getMetatable
int getMetatable(int index) Pushes onto the stack the metatable of the value at the given acceptable indexPushes 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 0 and pushes nothing on the stack.
- Parameters:
index- the index of the element- Returns:
- 0 if the value does not have a metatable
-
setMetatable
void setMetatable(int index) Pops a table from the stack and sets it as the new metatable for the value at the given acceptable index- Parameters:
index- the index of the element
-
getMetaField
Pushes onto the stack the fieldfieldfrom the metatable of the object at indexindexIf the object does not have a metatable, or if the metatable does not have this field, returns 0 and pushes nothing.
- Parameters:
index- the index of the elementfield- the meta field- Returns:
- 0 if no such field
-
getRegisteredMetatable
Pushes onto the stack the metatable associated with name tname in the registry- Parameters:
typeName- the name of the user-defined type- See Also:
-
newRegisteredMetatable
Creates a new table to be used as a metatable for userdata, adds it to the registryIf the registry already has the key
typeName, returns 0. Otherwise, creates a new table to be used as a metatable for userdata, adds it to the registry with keytypeName, and returns 1.In both cases pushes onto the stack the final value associated with tname in the registry.
- Parameters:
typeName- the name of the user-defined type- Returns:
- 1 if added to registry, 0 if already registered
-
openLibraries
void openLibraries()Opens all standard Lua libraries into the given stateSee
openLibrary(String)for more info. -
openLibrary
Opens a specific library into the given stateSee the corresponding Lua manual for available library. For example, in Lua 5.4, calling
L.openLibrary("base")will call theluaopen_baseC function for the base library.Note that opening a library may modify global variables. For example, opening the
baselibrary in some specific Lua versions will override built-in functions likeprint. So make sure to make your modifications after opening all the libraries.- Parameters:
name- the library name
-
concat
void concat(int n) Concatenates the n values at the top of the stack, pops them, and leaves the result at the topIf n is 1, the result is the single value on the stack (that is, the function does nothing); if n is 0, the result is the empty string. Concatenation is performed following the usual semantics of Lua.
- Parameters:
n- the number of values on top of the stack to concatenate
-
gc
void gc()Performs a full garbage-collection cycleThis also removes unneeded references created by finalized proxies and Lua values.
-
error
Throws an error inside a Lua environmentIt currently just throws a
RuntimeException.- Parameters:
message- the error message
-
createProxy
Creates a proxy object, implementing all the specified interfaces, with a Lua table / function on top of the stackThis method pops the value on top on the stack and creates reference to it with
ref().When invoking methods, the created Java object, instead of the backing Lua table, is passed as the first parameter to the Lua function.
- Parameters:
interfaces- the interfaces to implementdegree- the conversion degree when passing parameters and return values- Returns:
- a proxy object, calls to which are proxied to the underlying Lua table
- Throws:
IllegalArgumentException- if not all classes are interfaces
-
setExternalLoader
Sets aExternalLoaderfor the main stateThe provided external loader will be integrated into Lua's module resolution progress. See require (modname) for an overview.
We will register a new searcher by appending to
package.searchers(orpackage.loadersfor Lua 5.1) to load Lua files with thisExternalLoader.You need to load the
packagelibrary to make the external loader effective.- Parameters:
loader- the loader that will be used to find files
-
loadExternal
Loads a chunk from aExternalLoaderset bysetExternalLoader(ExternalLoader)- Parameters:
module- the module- Throws:
LuaException
-
getLuaNatives
LuaNatives getLuaNatives()Returns the underlying native Lua interface.- Returns:
- the underlying
LuaNativesnatives
-
getMainState
Lua getMainState()Returns the main Lua state.- Returns:
- the main Lua state
-
getPointer
long getPointer()Returns the pointer to the internal Lua state.- Returns:
- the pointer to the internal
lua_State
-
getId
int getId()Returns the unique identifier of this Lua thread.- Returns:
- the unique identifier to the Lua thread
-
getJavaError
@Nullable Throwable getJavaError()Fetches the most recent JavaThrowablepassed to Lua- Returns:
- value of the Lua global
GLOBAL_THROWABLE
-
error
Sets the Lua globalGLOBAL_THROWABLEto the throwableIf the exception is
null, it clears the global exception and pushes nothing. Otherwise, it sets the Lua globalGLOBAL_THROWABLEto the throwable, and pushesThrowable.toString()onto the stack.- Parameters:
e- the exception- Returns:
- 0 if e is null, -1 otherwise
-
close
void close()Closes the threadYou need to make sure that you call this method no more than once, or else the Lua binary may / will very likely just crash.
- Specified by:
closein interfaceAutoCloseable
-
get
LuaValue get()Pops the value on top of the stack and return a LuaValue referring to it- Returns:
- a reference to the value
-