Class Globals

All Implemented Interfaces:
KryoSerializable

public final class Globals extends LuaTable
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

Java Field

Certain public fields are provided that contain the current values of important global state:

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.
See Also:
  • Field Details

  • Constructor Details

    • Globals

      public Globals()
  • Method Details

    • checkglobals

      public Globals checkglobals()
      Check that this object is a Globals object, and return it, otherwise throw an error.
      Overrides:
      checkglobals in class LuaValue
      Returns:
      this if if an instance fof Globals
    • write

      public void write(Kryo kryo, Output output)
      Specified by:
      write in interface KryoSerializable
      Overrides:
      write in class LuaTable
    • read

      public void read(Kryo kryo, Input input)
      Specified by:
      read in interface KryoSerializable
      Overrides:
      read in class LuaTable
    • setDebugLib

      public void setDebugLib(@Null DebugLib debugLib)
    • getDebugLib

      @Null public DebugLib getDebugLib()
    • getCallstack

      public CallStack getCallstack()
    • getErrorFunc

      @Null public LuaValue getErrorFunc()
    • setErrorFunc

      public void setErrorFunc(@Null LuaValue v)
    • loadfile

      public LuaValue loadfile(String filename)
      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

      public LuaValue load(String script, String chunkname)
      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

      public LuaValue load(String script)
      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

      public LuaValue load(String script, String chunkname, LuaTable environment)
      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

      public LuaValue load(Reader reader, String chunkname)
      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

      public LuaValue load(Reader reader, String chunkname, LuaTable environment)
      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

      public LuaValue load(InputStream is, String chunkname, String mode, LuaValue environment)
      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

      public Prototype loadPrototype(InputStream is, String chunkname, String mode) throws IOException
      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

      public Prototype compilePrototype(Reader reader, String chunkname) throws IOException
      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

      public Prototype compilePrototype(InputStream stream, String chunkname) throws IOException
      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