Class LuaInterpreter
- All Implemented Interfaces:
Tokens
public class LuaInterpreter extends Object implements Tokens
Lua
class, which allows you to construct a Lua
interpreter using a Reader
. The reader is fully
read and interpreted as Lua code. Which can then be executed in a
myriad of fashions. There can be various types of variables and
objects that can be directly tied to Java methods, fields, or code.- Author:
- theKayani
- See Also:
Lua.interpreter()
,Lua.reader(java.io.Reader, java.lang.String)
,LuaFactory.build()
, https://www.lua.org/manual/5.3/
-
Field Summary
Fields inherited from interface com.hk.lua.Tokens
F_DO, F_ELSE, F_ELSEIF, F_FOR, F_FUNCTION, F_IF, F_LOOP, F_REPEAT, F_WHILE, T_AND, T_BAND, T_BOOLEAN, T_BOR, T_BREAK, T_BXOR, T_CLSE_BKT, T_CLSE_BRC, T_CLSE_PTS, T_COLON, T_COMMA, T_CONCAT, T_DIVIDE, T_DO, T_DOUBLECOLON, T_EEQUALS, T_ELSE, T_ELSEIF, T_END, T_EQUALS, T_FLR_DIVIDE, T_FOR, T_FUNCTION, T_GENERIC_FOR, T_GOTO, T_GREQ_THAN, T_GRTR_THAN, T_IDENTIFIER, T_IF, T_IN, T_INC_NUMBER, T_INC_STRING, T_LESS_THAN, T_LOCAL, T_LSEQ_THAN, T_MINUS, T_MODULO, T_NEGATE, T_NEQUALS, T_NIL, T_NOT, T_NULL, T_NUMBER, T_NUMERIC_FOR, T_OPEN_BKT, T_OPEN_BRC, T_OPEN_PTS, T_OR, T_PERIOD, T_PLUS, T_POUND, T_POW, T_REPEAT, T_RETURN, T_SEMIC, T_SHL, T_SHR, T_STRING, T_TABLE, T_THEN, T_THREAD, T_TIMES, T_UBNOT, T_UNTIL, T_USERDATA, T_VARARGS, T_WHILE
-
Method Summary
Modifier and Type Method Description void
compile()
Attempt to fully read the providedReader
and interpret it as Lua code.Object
execute(Object... args)
Execute the interpreter using the previously provided reader.Object
getExtra(String key)
Get the extra data under a certain key.<T> T
getExtra(String key, Class<T> cls)
Get the extra data under a certain key.<T> T
getExtra(String key, Class<T> cls, T def)
Get the extra data under a certain key, or a default if there data found under the key was null or not found.LuaObject
getExtraLua(String key)
Get the extra data under a certain key, cast as aLuaObject
.String
getExtraProp(String key)
Get the extra data under a certain key, cast as aString
.Environment
getGlobals()
Get the global Lua environment with global variables and functions.boolean
hasExtra(String key)
Check whether a certain key has a value under it, whether null or not.boolean
hasModule(String module)
<T extends Enum<T> & BiConsumer<Environment, LuaObject>>
voidimportLib(LuaLibrary<T> lib)
Import the individual elements of a Lua Library directly into the global environment.LuaInterpreter
removeExtra(String key)
Remove a certain key from the extra data.LuaObject
require(Reader reader)
Immediately execute a reader of Lua code using this global environment under a new Lua chunk.LuaObject
require(CharSequence cs)
Immediately execute a string of Lua code using this global environment under a new Lua chunk.LuaObject
require(String module, Reader reader)
Immediately execute a reader of Lua code using this global environment under a new Lua chunk.LuaInterpreter
setExtra(String key, Object value)
Set the data under a certain key.
-
Method Details
-
hasExtra
Check whether a certain key has a value under it, whether null or not.
Extra data it data that is tied to this specific interpreter. Completely optional and has no effect on execution of Lua. Just for utility and data consistency sake.
- Parameters:
key
- aString
to match to a key- Returns:
- if this certain key has data under it
-
getExtra
Get the extra data under a certain key.
Extra data it data that is tied to this specific interpreter. Completely optional and has no effect on execution of Lua. Just for utility and data consistency sake.
-
getExtra
Get the extra data under a certain key. The data returned should be able to be cast to the
cls
specified. If so, it is boxed and returned.Extra data it data that is tied to this specific interpreter. Completely optional and has no effect on execution of Lua. Just for utility and data consistency sake.
-
getExtra
Get the extra data under a certain key, or a default if there data found under the key was null or not found.
Extra data it data that is tied to this specific interpreter. Completely optional and has no effect on execution of Lua. Just for utility and data consistency sake.
-
getExtraLua
Get the extra data under a certain key, cast as a
LuaObject
. This is useful because it will not return null, it will instead returnLua.NIL
if there was no valid data found.Extra data it data that is tied to this specific interpreter. Completely optional and has no effect on execution of Lua. Just for utility and data consistency sake.
-
getExtraProp
Get the extra data under a certain key, cast as a
String
. This will return null if there was no valid data found.Extra data it data that is tied to this specific interpreter. Completely optional and has no effect on execution of Lua. Just for utility and data consistency sake.
-
setExtra
Set the data under a certain key. This value can then later be retrieved using the
getExtra(java.lang.String)
methods. These have various overloaded helper functions to return the value cast as another type.Extra data it data that is tied to this specific interpreter. Completely optional and has no effect on execution of Lua. Just for utility and data consistency sake.
-
removeExtra
Remove a certain key from the extra data. This will result in the default value being used (if passed) or a null value when using the
getExtra(java.lang.String)
methods.Extra data it data that is tied to this specific interpreter. Completely optional and has no effect on execution of Lua. Just for utility and data consistency sake.
- Parameters:
key
- aString
to match to a key- Returns:
- this
-
getGlobals
Get the global Lua environment with global variables and functions. Conventionally, the developer would use this method to set the various global variables for this specific environment before calling the
execute(java.lang.Object...)
method, or calling a specific method by name.Calling this before
execute
will only contain the library imported methods and data. After calling theexecute
method, will the environment be populated by the variables from the Lua code interpreted.- Returns:
- the global Lua
Environment
-
require
Immediately execute a string of Lua code using this global environment under a new Lua chunk.- Parameters:
cs
- the Lua code to interpret- Returns:
- the result from this execution. If this code had a return statement.
-
require
Immediately execute a reader of Lua code using this global environment under a new Lua chunk.- Parameters:
reader
- aReader
to provide the Lua code to interpret- Returns:
- the result from this execution. If this code had a return statement.
-
require
Immediately execute a reader of Lua code using this global environment under a new Lua chunk. This function executes the Lua code under a certain module, the result is then stored in the case that this function is called again with the same module. If so, the reader is ignored and the saved value is returned instead.- Parameters:
module
- a key to match the result object toreader
- aReader
to provide the Lua code to interpret- Returns:
- the result from this execution. If this code had a return statement.
-
hasModule
-
importLib
Import the individual elements of a Lua Library directly into the global environment. Whether the elements are placed in the global space, or under a certain table is defined by the constructor when instantiating the library object.
To use any of the default libraries, you don't need to define any functions yourself. All you need to do is import the various standard libraries under
LuaLibrary
.- Type Parameters:
T
- a T class- Parameters:
lib
- aLuaLibrary
object- See Also:
LuaLibrary(String, Class)
-
execute
Execute the interpreter using the previously provided reader. If the
compile()
was not called first, the code is first interpreted and compiled before executed.The
args
parameter is for providing a variable amount of arguments to the Lua code. According to The Lua 5.3 Manual, the provided arguments are converted into their LuaObject equivalents and stored in the...
varargs variable in the Lua code.- Parameters:
args
- any vararg parameters to pass to the Lua code.- Returns:
- a result of the execution.
- Throws:
LuaException
- if any.UncheckedIOException
- if any.
-
compile
Attempt to fully read the provided
Reader
and interpret it as Lua code. If there are any Lua syntax errors, this function will throw aLuaException
.If not, the code is saved to be executed using the
execute(java.lang.Object...)
command. If this is not called beforehand, it is called by default by theexecute
function.- Throws:
LuaException
- if any Lua syntax errors.UncheckedIOException
- if any.
-