Package com.hk.lua

Class LuaFactory

java.lang.Object
com.hk.lua.LuaFactory

public class LuaFactory
extends Object

Lua factories can be used to create various LuaInterpreter objects without the need of recompiling. This can be created using any of the Lua.factory(java.io.Reader, java.lang.String) methods.

Once the factory is created, compile() can be used to parse the reader and close it. Once compiled, the build() method retrieves a new LuaInterpreter every time.

Prior to building any interpreters, handlers can be attached which are called before the interpreter is returned by build(). Libraries can also be added which would be injected into each interpreter built.

Author:
theKayani
  • Method Details

    • addLibrary

      public void addLibrary​(LuaLibrary<?> lib)

      Add a library to be injected into each built interpreter. This means that the retrieved interpreter does not need this library added to it.

      Parameters:
      lib - a LuaLibrary
    • addHandler

      public void addHandler​(Consumer<LuaInterpreter> handler)

      Handlers are called on each interpreter built before they are returned. This happens after libraries are injected.

      Parameters:
      handler - a Consumer object
    • removeLibrary

      public void removeLibrary​(LuaLibrary<?> lib)

      Remove a specific library if it was previously added using the addLibrary(LuaLibrary) method. If the given library doesn't exist, this doesn't do anything.

      Parameters:
      lib - a LuaLibrary object
    • removeHandler

      public void removeHandler​(Consumer<LuaInterpreter> handler)

      Remove a handler from this factory. The handler would only be removed if it was previously added using the addHandler(Consumer) method.

      Parameters:
      handler - a Consumer object
    • compile

      public void compile() throws IOException

      Fully read the reader and attempt to compile the Lua source into an executable chunk. If there are any issues during compilation, this method will throw them.

      This must be called before build() can be called.

      Throws:
      IOException - if any.
    • build

      public LuaInterpreter build()

      Build and return a new LuaInterpreter object containing the given libraries as well as executing the handlers that were added previously.

      Returns:
      a LuaInterpreter object