Package com.prineside.luaj
Class Globals
java.lang.Object
com.prineside.luaj.Varargs
com.prineside.luaj.LuaValue
com.prineside.luaj.LuaTable
com.prineside.luaj.Globals
- All Implemented Interfaces:
com.esotericsoftware.kryo.KryoSerializable
Global environment used by luaj. Contains global variables referenced by executing lua.
Constructing and Initializing Instances: Typically, this is constructed indirectly by a call to and then used to load lua scripts for execution as in the following example.
Globals globals = JsePlatform.standardGlobals();
globals.load( new StringReader("print 'hello'"), "main.lua" ).call();
The creates a complete global environment with the standard libraries loaded.
For specialized circumstances, the Globals may be constructed directly and loaded
with only those libraries that are needed, for example.
Globals globals = new Globals();
globals.load( new BaseLib() );
Loading and Executing Lua Code
Globals contains convenience functions to load and execute lua source code given a Reader. A simple example is:
globals.load( new StringReader("print 'hello'"), "main.lua" ).call();
Fine-Grained Control of Compiling and Loading Lua
Executable LuaFunctions are created from lua code in several steps- find the resource using the platform's
ResourceFinder
- compile lua to lua bytecode using
Globals.Compiler
- load lua bytecode to a
Prototype
usingGlobals.Undumper
- construct
LuaClosure
fromPrototype
withGlobals
usingGlobals.Loader
- compile lua to lua bytecode using
Globals.Compiler
or load precompiled code usingGlobals.Undumper
Java Field
Certain public fields are provided that contain the current values of important global state:STDIN
Current value for standard input in the laadedIoLib
, if any.STDOUT
Current value for standard output in the loadedIoLib
, if any.STDERR
Current value for standard error in the loadedIoLib
, if any.finder
Current loadedResourceFinder
, if any.compiler
Current loadedGlobals.Compiler
, if any.undumper
Current loadedGlobals.Undumper
, if any.loader
Current loadedGlobals.Loader
, if any.
Lua Environment Variables
these environment variables are created within the Globals.- "_G" Pointer to this Globals.
- "_VERSION" String containing the version of luaj.
Use in Multithreaded Environments
In a multi-threaded server environment, each server thread should create one Globals instance, which will be logically distinct and not interfere with each other, but share certain static immutable resources such as class data and string data.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
Interface for module that converts lua source text into a prototype.static interface
Interface for module that converts a Prototype into a LuaFunction with an environment.static interface
Interface for module that loads lua binary chunk into a prototype.Nested classes/interfaces inherited from class com.prineside.luaj.LuaTable
LuaTable.DeadSlot, LuaTable.IntKeyEntry, LuaTable.LinkSlot, LuaTable.NormalEntry, LuaTable.NumberValueEntry, LuaTable.Slot
Nested classes/interfaces inherited from class com.prineside.luaj.LuaValue
LuaValue.NillableSerializer, LuaValue.None
Nested classes/interfaces inherited from class com.prineside.luaj.Varargs
Varargs.ArrayVarargs
-
Field Summary
FieldsModifier and TypeFieldDescriptionThe BaseLib instance loaded into this GlobalsThe installed compiler.The DebugLib instance loaded into this Globals, or null if debugging is not enabledThe installed ResourceFinder for looking files by name.The installed loader.final com.badlogic.gdx.utils.Pool<LuaClosure.ExecutionState>
The PackageLib instance loaded into this GlobalsThe currently running thread.The current default error stream.The current default input stream.The current default output stream.The installed undumper.Fields inherited from class com.prineside.luaj.LuaTable
CLASS_COMPARATOR
Fields inherited from class com.prineside.luaj.LuaValue
ADD, CALL, CONCAT, DIV, EMPTYSTRING, ENV, EQ, FALSE, INDEX, LE, LEN, LT, METATABLE, MINUSONE, MOD, MODE, MUL, NEWINDEX, NIL, NILLABLE_SERIALIZER, NILS, NONE, NOVALS, ONE, POW, SUB, TBOOLEAN, TFUNCTION, TINT, TLIGHTUSERDATA, TNIL, TNONE, TNUMBER, TOSTRING, TRUE, TSTRING, TTABLE, TTHREAD, TUSERDATA, TVALUE, TYPE_NAMES, UNM, ZERO
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionCheck that this object is a Globals object, and return it, otherwise throw an error.compilePrototype
(InputStream stream, String chunkname) Compile lua source from an InputStream into a Prototype.compilePrototype
(Reader reader, String chunkname) Compile lua source from a Reader into a Prototype.void
freeStackCache
(LuaValue[] stack) load
(InputStream is, String chunkname, String mode, LuaValue environment) Load the content form an input stream as a binary chunk or text file.Load the content form a reader as a text file.Load the content form a reader as a text file, supplying a custom environment.Convenience function to load a string value as a script.Convenience function to load a string value as a script.Convenience function to load a string value as a script with a custom environment.Convenience function for loading a file that is either binary lua or lua source.loadPrototype
(InputStream is, String chunkname, String mode) Load lua source or lua binary from an input stream into a Prototype.LuaValue[]
obtainStackCache
(int maxSize) void
read
(com.esotericsoftware.kryo.Kryo kryo, com.esotericsoftware.kryo.io.Input input) void
write
(com.esotericsoftware.kryo.Kryo kryo, com.esotericsoftware.kryo.io.Output output) Function which yields the current thread.Methods inherited from class com.prineside.luaj.LuaTable
arrayget, checktable, concat, entry, eq, eq_b, get, get, getHashEntries, getmetatable, hashmod, hashpow2, hashset, hashSlot, inext, insert, istable, keyCount, keys, len, length, next, opttable, presize, presize, rawget, rawget, rawlen, rawset, rawset, remove, set, set, setmetatable, sort, toLuaValue, type, typename, unpack, unpack, unpack, useWeakKeys, useWeakValues, wrap
Methods inherited from class com.prineside.luaj.LuaValue
add, add, add, and, arg, arg1, argerror, assert_, buffer, call, call, call, call, call, cBool, cDouble, cFloat, checkboolean, checkclosure, checkdouble, checkfunction, checkint, checkjstring, checklong, checknotnil, checknumber, checknumber, checkstring, checkthread, checkuserdata, checkuserdata, cInt, cNcDouble, cNcFloat, cNcInt, cObject, comparemt, concat, concat, concatmt, concatTo, concatTo, concatTo, cRegObject, cVarargs, cVarargs, cVarargs, cVarargs, cVarargs, cVarargs, cVarargs, cVarargs, div, div, div, divInto, eqmtcall, equals, error, get, gt, gt, gt, gt_b, gt_b, gt_b, gteq, gteq, gteq, gteq_b, gteq_b, gteq_b, initupvalue1, invoke, invoke, invoke, invoke, invokemethod, invokemethod, invokemethod, invokemethod, isboolean, isclosure, isfunction, isint, isinttype, islong, isnil, isnumber, isRegObject, isstring, isthread, isuserdata, isuserdata, isvalidkey, listOf, listOf, load, lt, lt, lt, lt_b, lt_b, lt_b, lteq, lteq, lteq, lteq_b, lteq_b, lteq_b, markAsConst, metatag, method, method, method, method, method, method, mod, mod, mod, modFrom, mul, mul, mul, narg, neg, neq, neq_b, not, onInvoke, optboolean, optclosure, optdouble, optfunction, optint, optjstring, optlong, optnumber, optstring, optthread, optuserdata, optuserdata, optvalue, or, pow, pow, pow, powWith, powWith, raweq, raweq, raweq, raweq, raweq, rawget, rawset, rawset, rawset, rawset, rawset, rawsetlist, set, set, set, set, set, strcmp, strcmp, strongvalue, strvalue, sub, sub, sub, subargs, subFrom, subFrom, tableOf, tableOf, tableOf, tableOf, tableOf, tableOf, tailcallOf, testfor_b, toboolean, tobyte, tochar, todouble, tofloat, toint, tojstring, tolong, tonumber, toshort, tostring, toString, touserdata, touserdata, userdataOf, userdataOf, valueOf, valueOf, valueOf, valueOf, valueOf, valueOf
Methods inherited from class com.prineside.luaj.Varargs
argcheck, checkboolean, checkclosure, checkdouble, checkfunction, checkint, checkjstring, checklong, checknotnil, checknumber, checkstring, checktable, checkthread, checkuserdata, checkuserdata, checkvalue, dealias, eval, isfunction, isnil, isnoneornil, isnumber, isstring, istable, isTailcall, isthread, isuserdata, isvalue, optboolean, optclosure, optdouble, optfunction, optint, optjstring, optlong, optnumber, optstring, opttable, optthread, optuserdata, optuserdata, optvalue, toboolean, tobyte, tochar, todouble, tofloat, toint, tojstring, tolong, toshort, touserdata, touserdata, type
-
Field Details
-
STDIN
The current default input stream. -
STDOUT
The current default output stream. -
STDERR
The current default error stream. -
finder
The installed ResourceFinder for looking files by name. -
running
The currently running thread. Should not be changed by non-library code. -
baselib
The BaseLib instance loaded into this Globals -
package_
The PackageLib instance loaded into this Globals -
debuglib
The DebugLib instance loaded into this Globals, or null if debugging is not enabled -
luaClosureExecutionStatePool
-
loader
The installed loader.- See Also:
-
compiler
The installed compiler.- See Also:
-
undumper
The installed undumper.- See Also:
-
-
Constructor Details
-
Globals
public Globals()
-
-
Method Details
-
checkglobals
Check that this object is a Globals object, and return it, otherwise throw an error.- Overrides:
checkglobals
in classLuaValue
- Returns:
this
if if an instance fofGlobals
-
obtainStackCache
-
freeStackCache
-
write
public void write(com.esotericsoftware.kryo.Kryo kryo, com.esotericsoftware.kryo.io.Output output) -
read
public void read(com.esotericsoftware.kryo.Kryo kryo, com.esotericsoftware.kryo.io.Input input) -
loadfile
Convenience function for loading a file that is either binary lua or lua source.- Parameters:
filename
- Name of the file to load.- Returns:
- LuaValue that can be call()'ed or invoke()'ed.
- Throws:
LuaError
- if the file could not be loaded.
-
load
Convenience function to load a string value as a script. Must be lua source.- Parameters:
script
- Contents of a lua script, such as "print 'hello, world.'"chunkname
- Name that will be used within the chunk as the source.- Returns:
- LuaValue that may be executed via .call(), .invoke(), or .method() calls.
- Throws:
LuaError
- if the script could not be compiled.
-
load
Convenience function to load a string value as a script. Must be lua source.- Parameters:
script
- Contents of a lua script, such as "print 'hello, world.'"- Returns:
- LuaValue that may be executed via .call(), .invoke(), or .method() calls.
- Throws:
LuaError
- if the script could not be compiled.
-
load
Convenience function to load a string value as a script with a custom environment. Must be lua source.- Parameters:
script
- Contents of a lua script, such as "print 'hello, world.'"chunkname
- Name that will be used within the chunk as the source.environment
- LuaTable to be used as the environment for the loaded function.- Returns:
- LuaValue that may be executed via .call(), .invoke(), or .method() calls.
- Throws:
LuaError
- if the script could not be compiled.
-
load
Load the content form a reader as a text file. Must be lua source. The source is converted to UTF-8, so any characters appearing in quoted literals above the range 128 will be converted into multiple bytes.- Parameters:
reader
- Reader containing text of a lua script, such as "print 'hello, world.'"chunkname
- Name that will be used within the chunk as the source.- Returns:
- LuaValue that may be executed via .call(), .invoke(), or .method() calls.
- Throws:
LuaError
- if the script could not be compiled.
-
load
Load the content form a reader as a text file, supplying a custom environment. Must be lua source. The source is converted to UTF-8, so any characters appearing in quoted literals above the range 128 will be converted into multiple bytes.- Parameters:
reader
- Reader containing text of a lua script, such as "print 'hello, world.'"chunkname
- Name that will be used within the chunk as the source.environment
- LuaTable to be used as the environment for the loaded function.- Returns:
- LuaValue that may be executed via .call(), .invoke(), or .method() calls.
- Throws:
LuaError
- if the script could not be compiled.
-
load
Load the content form an input stream as a binary chunk or text file.- Parameters:
is
- InputStream containing a lua script or compiled lua"chunkname
- Name that will be used within the chunk as the source.mode
- String containing 'b' or 't' or both to control loading as binary or text or either.environment
- LuaTable to be used as the environment for the loaded function.
-
loadPrototype
Load lua source or lua binary from an input stream into a Prototype. The InputStream is either a binary lua chunk starting with the lua binary chunk signature, or a text input file. If it is a text input file, it is interpreted as a UTF-8 byte sequence.- Parameters:
is
- Input stream containing a lua script or compiled lua"chunkname
- Name that will be used within the chunk as the source.mode
- String containing 'b' or 't' or both to control loading as binary or text or either.- Throws:
IOException
-
compilePrototype
Compile lua source from a Reader into a Prototype. The characters in the reader are converted to bytes using the UTF-8 encoding, so a string literal containing characters with codepoints 128 or above will be converted into multiple bytes.- Throws:
IOException
-
compilePrototype
Compile lua source from an InputStream into a Prototype. The input is assumed to be UTf-8, but since bytes in the range 128-255 are passed along as literal bytes, any ASCII-compatible encoding such as ISO 8859-1 may also be used.- Throws:
IOException
-
yield
Function which yields the current thread.- Parameters:
args
- Arguments to supply as return values in the resume function of the resuming thread.- Returns:
- Values supplied as arguments to the resume() call that reactivates this thread.
-