Class AbstractLua

java.lang.Object
party.iroiro.luajava.AbstractLua
All Implemented Interfaces:
AutoCloseable, Lua, LuaThread
Direct Known Subclasses:
Lua51, Lua52, Lua53, Lua54, Lua55, LuaJ, LuaJit

public abstract class AbstractLua extends Object implements Lua
An implementation that relies on LuaNatives for most of the features independent of Lua versions
  • Nested Class Summary

    Nested classes/interfaces inherited from interface party.iroiro.luajava.Lua

    Lua.Conversion, Lua.LuaType
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final LuaNatives
     
    protected int
     
    protected static final LuaInstances<AbstractLua>
    Registry of all active Lua instances.
    protected final long
     
    protected @Nullable ExternalLoader
    External loader for Lua modules.
    protected final AbstractLua
     
    protected final @Nullable ConcurrentHashMap<Integer,party.iroiro.luajava.cleaner.LuaReference<?>>
    Map tracking all active Lua references.
    protected final @Nullable ReferenceQueue<party.iroiro.luajava.cleaner.LuaReferable>
    Queue for tracking references that can be garbage collected.
    protected @Nullable LuaValue
    Cached reference to the Lua require function.
    protected final @Nullable List<Lua>
     

    Fields inherited from interface party.iroiro.luajava.Lua

    GLOBAL_THROWABLE
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Creates a new Lua (main) state
    protected
    AbstractLua(LuaNatives luaNative, long L, int id, AbstractLua mainThread)
    Adopts a Lua sub-state, wrapping it up with the Lua interface
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    Adds a sub-thread to this main thread's tracking list.
    protected void
    checkError(int code, boolean runtime)
    Throws LuaException if the code is not LuaException.LuaError.OK.
    void
    checkStack(int extra)
    Ensures that there are at least extra free stack slots in the Lua stack
    void
    Closes the thread
    void
    concat(int n)
    Concatenates the n values at the top of the stack, pops them, and leaves the result at the top
    convertError(int code)
    Converts a native error code to a LuaError enum value.
    abstract Lua.LuaType
    convertType(int code)
    Converts a native type code to a LuaType enum value.
    createProxy(Class<?>[] interfaces, Lua.Conversion degree)
    Creates a proxy object, implementing all the specified interfaces, with a Lua table / function on top of the stack
    void
    createTable(int nArr, int nRec)
    Creates a new empty table and pushes it onto the stack
    @Nullable ByteBuffer
    Dumps a function as a binary chunk
    boolean
    equal(int i1, int i2)
    Returns true if the two values in acceptable indices i1 and i2 are equal
    int
    error(@Nullable Throwable e)
    Sets the Lua global Lua.GLOBAL_THROWABLE to the throwable
    void
    error(String message)
    Throws an error inside a Lua environment
    eval(String command)
    Executes Lua code
    from(boolean b)
    Creates a boolean Lua value from a Java boolean.
    from(double n)
    Creates a number Lua value from a Java double.
    from(long n)
    Creates a number Lua value from a Java long.
    Creates a string Lua value from a Java string.
    from(ByteBuffer buffer)
    Creates a raw string Lua value from a byte buffer.
    Creates a nil Lua value.
    void
    gc()
    Performs a full garbage-collection cycle
    get()
    Pops the value on top of the stack and return a LuaValue referring to it
    get(String globalName)
    Gets a references to a global object
    void
    getField(int index, String key)
    Pushes onto the stack the value t[key]
    void
    Pushes onto the stack the value of the global name
    int
    Returns the unique identifier of this Lua thread.
    @Nullable Throwable
    Fetches the most recent Java Throwable passed to Lua
    Returns the underlying native Lua interface.
    Returns the main Lua state.
    int
    getMetaField(int index, String field)
    Pushes onto the stack the field field from the metatable of the object at index index
    int
    getMetatable(int index)
    Pushes onto the stack the metatable of the value at the given acceptable index
    long
    Returns the pointer to the internal Lua state.
    void
    Pushes onto the stack the metatable associated with name tname in the registry
    void
    getTable(int index)
    Pushes onto the stack the value t[k]
    int
    Returns the index of the top element in the stack
    void
    insert(int index)
    Moves the top element into the given valid index, shifting up the elements above this index
    protected @Nullable Object
    invokeSpecial(@Nullable Object object, Method method, @Nullable Object @Nullable [] params)
    Calls a method on an object, equivalent to invokespecial
    boolean
    isBoolean(int index)
    Returns true if the value at the given index is a boolean, and false otherwise
    protected boolean
    Checks if this Lua state has been closed.
    boolean
    isFunction(int index)
    Returns true if the value at the given index is a function (either C or Lua), and false otherwise
    boolean
    isInteger(int index)
    Returns true if the value at the given index is an integer, and false otherwise
    boolean
    isJavaObject(int index)
    Checks if the element is a Java object
    boolean
    isNil(int index)
    Returns true if the value at the given index is nil, and false otherwise
    boolean
    isNone(int index)
    Returns true if the given index is not valid, and false otherwise
    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
    boolean
    isNumber(int index)
    Returns true if the value is a number or a string convertible to a number, and false otherwise
    boolean
    isString(int index)
    Returns true if the value at the given index is a string or a number
    boolean
    isTable(int index)
    Returns true if the value at the given index is a table, and false otherwise.
    boolean
    isThread(int index)
    Returns true if the value at the given index is a thread, and false otherwise.
    boolean
    isUserdata(int index)
    Returns true if the value at the given index is userdata (either full or light), and false otherwise.
    boolean
    lessThan(int i1, int i2)
    Returns true if the value at acceptable index i1 is smaller than the value at i2
    void
    load(String script)
    Loads a string as a Lua chunk
    void
    load(Buffer buffer, String name)
    Loads a buffer as a Lua chunk
    void
    int
    Creates a new table to be used as a metatable for userdata, adds it to the registry
    void
    Creates a new empty table and pushes it onto the stack
    Creates a new thread, pushes it on the stack
    protected abstract AbstractLua
    newThread(long L, int id, AbstractLua mainThread)
    Creates a new sub-thread attached to this main thread.
    int
    next(int n)
    Pops a key from the stack, and pushes a key-value pair from the table at the given index
    void
    Opens all standard Lua libraries into the given state
    void
    Opens a specific library into the given state
    void
    pCall(int nArgs, int nResults)
    Calls a function in protected mode
    void
    pop(int n)
    Pops n elements from the stack
    void
    push(boolean bool)
    Pushes a boolean value onto the stack
    void
    push(long integer)
    Pushes an integer onto the stack
    void
    push(@Nullable Object object, Lua.Conversion degree)
    Push an object onto the stack, converting according to Lua.Conversion.
    void
    push(Number number)
    Pushes a floating-point number onto the stack
    void
    push(String string)
    Pushes a string onto the stack.
    void
    push(ByteBuffer buffer)
    Pushes a buffer as a raw string onto the stack
    void
    push(Collection<?> collection)
    Push the element onto the stack, converted to lua tables (index starting from 1)
    void
    push(Map<?,?> map)
    Push the element onto the stack, converted to lua tables
    void
    push(JFunction function)
    Push the function onto the stack, converted to a callable element
    void
    push(LuaFunction function)
    Push the function onto the stack, converted to a callable element
    void
    push(LuaValue value)
    Push a LuaValue onto the stack, equivalent to LuaValue.push(Lua)
    void
    Push an array onto the stack, converted to luatable
    void
    Push the element onto the stack, converted as is to Java arrays
    void
    pushJavaClass(Class<?> clazz)
    Push a class onto the stack, which may be used with `java.new` on the lua side
    void
    Push the element onto the stack, converted as is to Java objects
    protected void
    Pushes a Java object or array onto the Lua stack.
    void
    Pushes a nil value onto the stack
    void
    Pushes the current thread onto the stack
    void
    pushValue(int index)
    Pushes a copy of the element at the given valid index onto the stack
    boolean
    rawEqual(int i1, int i2)
    Returns true if the two values in acceptable indices i1 and i2 are primitively equal
    void
    rawGet(int index)
    Similar to Lua.getTable(int), but does a raw access (i.e., without metamethods)
    void
    rawGetI(int index, int n)
    Pushes onto the stack the value t[n], where t is the value at the given valid index
    int
    rawLength(int index)
    Returns the raw "length" of the value at the given index
    void
    rawSet(int index)
    Similar to Lua.setTable(int), but does a raw assignment (i.e., without metamethods)
    void
    rawSetI(int index, int n)
    Does the equivalent of t[n] = v
    int
    ref()
    Calls Lua.ref(int) with the pseudo-index LUA_REGISTRYINDEX
    int
    ref(int index)
    Creates and returns a reference, in the table at index index
    void
    refGet(int ref)
    Calls Lua.rawGetI(int, int) with the pseudo-index LUA_REGISTRYINDEX and the given ref
    void
    register(String name, LuaFunction function)
    Registers the function to a global name
    void
    remove(int index)
    Removes the element at the given valid index
    void
    replace(int index)
    Moves the top element into the given position (and pops it)
    require(String module)
    Loads a module, similar to the Lua `require` function
    boolean
    resume(int nArgs)
    Starts and resumes a coroutine in a given thread
    void
    run(String script)
    Loads and runs the given string
    void
    run(Buffer buffer, String name)
    Loads and runs a buffer
    void
    set(String key, Object value)
    Sets a global variable to the given value
    void
    Sets a ExternalLoader for the main state
    void
    setField(int index, String key)
    Does the equivalent to t[key] = v
    void
    Pops a value from the stack and sets it as the new value of global name
    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
    void
    setTable(int index)
    Does the equivalent to t[k] = v
    void
    setTop(int index)
    Accepts any index, or 0, and sets the stack top to this index
    protected boolean
    A method specifically for working around deadlocks caused by LuaJ.
    Returns the status of the thread
    int
    toAbsoluteIndex(int index)
    Converts a stack index into an absolute index.
    boolean
    toBoolean(int index)
    Converts the Lua value at the given acceptable index to a boolean value
    @Nullable ByteBuffer
    toBuffer(int index)
    Creates a ByteBuffer from the string at the specific index
    @Nullable ByteBuffer
    toDirectBuffer(int index)
    Creates a read-only direct ByteBuffer from the string at the specific index
    long
    toInteger(int index)
    Converts the Lua value at the given acceptable index to the signed integral type lua_Integer
    @Nullable Object
    toJavaObject(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 to List
    @Nullable Map<?,?>
    toMap(int index)
    Get the element at the specified stack position, converted to a Map
    double
    toNumber(int index)
    Converts the Lua value at the given acceptable index to a number
    @Nullable Object
    toObject(int index)
    Automatically converts a value into a Java object nil is converted to null. boolean converted to boolean or the boxed Boolean. integer / number to any of char byte short int long float double or 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 null on the Java side.
    @Nullable Object
    toObject(int index, Class<?> type)
    Converts a value at the stack index
    @Nullable String
    toString(int index)
    Converts the Lua value at the given acceptable index to a string
    @Nullable Lua.LuaType
    type(int index)
    Returns the Lua type of the element at the given index.
    void
    unref(int ref)
    Calls Lua.unRef(int, int) with the pseudo-index LUA_REGISTRYINDEX and the given ref
    void
    unRef(int index, int ref)
    Releases reference ref from the table at index index
    void
    xMove(Lua other, int n)
    Exchange values between different threads of the same global state
    void
    yield(int n)
    Yields a coroutine

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • instances

      protected static final LuaInstances<AbstractLua> instances
      Registry of all active Lua instances.
    • loader

      protected volatile @Nullable ExternalLoader loader
      External loader for Lua modules.
    • requireFunction

      protected volatile @Nullable LuaValue requireFunction
      Cached reference to the Lua require function.
    • recyclableReferences

      protected final @Nullable ReferenceQueue<party.iroiro.luajava.cleaner.LuaReferable> recyclableReferences
      Queue for tracking references that can be garbage collected.
    • recordedReferences

      protected final @Nullable ConcurrentHashMap<Integer,party.iroiro.luajava.cleaner.LuaReference<?>> recordedReferences
      Map tracking all active Lua references.
    • C

      protected final LuaNatives C
    • L

      protected final long L
    • id

      protected volatile int id
    • mainThread

      protected final AbstractLua mainThread
    • subThreads

      protected final @Nullable List<Lua> subThreads
  • Constructor Details

    • AbstractLua

      protected AbstractLua(LuaNatives luaNative)
      Creates a new Lua (main) state
      Parameters:
      luaNative - the Lua native wrapper
    • AbstractLua

      protected AbstractLua(LuaNatives luaNative, long L, int id, AbstractLua mainThread)
      Adopts a Lua sub-state, wrapping it up with the Lua interface
      Parameters:
      luaNative - the Lua native wrapper
      L - the new Lua state pointer
      id - the Lua state id (see LuaInstances)
      mainThread - the main state of this sub-state
  • Method Details

    • checkStack

      public void checkStack(int extra) throws RuntimeException
      Description copied from interface: Lua
      Ensures that there are at least extra free stack slots in the Lua stack

      It wraps lua_checkstack.

      Specified by:
      checkStack in interface Lua
      Parameters:
      extra - the extra slots to ensure
      Throws:
      RuntimeException - when unable to grow the stack
    • push

      public void push(@Nullable Object object, Lua.Conversion degree)
      Description copied from interface: Lua
      Push an object onto the stack, converting according to Lua.Conversion.
      Specified by:
      push in interface Lua
      Parameters:
      object - the object to be pushed onto the stack
      degree - how the object is converted into lua values
      See Also:
    • pushJavaObjectOrArray

      protected void pushJavaObjectOrArray(Object object)
      Pushes a Java object or array onto the Lua stack.
      Parameters:
      object - the Java object or array to push
    • pushNil

      public void pushNil()
      Description copied from interface: Lua
      Pushes a nil value onto the stack
      Specified by:
      pushNil in interface Lua
    • push

      public void push(boolean bool)
      Description copied from interface: Lua
      Pushes a boolean value onto the stack
      Specified by:
      push in interface Lua
      Parameters:
      bool - the boolean value
    • push

      public void push(Number number)
      Description copied from interface: Lua
      Pushes a floating-point number onto the stack
      Specified by:
      push in interface Lua
      Parameters:
      number - the number, whose Number.doubleValue() will be pushed
    • push

      public void push(long integer)
      Description copied from interface: Lua
      Pushes an integer onto the stack

      Please 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 Lua.push(Number).

      Specified by:
      push in interface Lua
      Parameters:
      integer - the number
    • push

      public void push(String string)
      Description copied from interface: Lua
      Pushes a string onto the stack.
      Specified by:
      push in interface Lua
      Parameters:
      string - the string
    • push

      public void push(ByteBuffer buffer)
      Description copied from interface: Lua
      Pushes a buffer as a raw string onto the stack

      The 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.

      Specified by:
      push in interface Lua
      Parameters:
      buffer - the buffer, which might contain invalid UTF-8 characters and zeros
    • push

      public void push(Map<?,?> map)
      Description copied from interface: Lua
      Push the element onto the stack, converted to lua tables

      Inner elements are converted with Lua.Conversion.FULL.

      Specified by:
      push in interface Lua
      Parameters:
      map - the element to be pushed onto the stack
    • push

      public void push(Collection<?> collection)
      Description copied from interface: Lua
      Push the element onto the stack, converted to lua tables (index starting from 1)

      Inner elements are converted with Lua.Conversion.FULL.

      Specified by:
      push in interface Lua
      Parameters:
      collection - the element to be pushed onto the stack
    • pushArray

      public void pushArray(Object array) throws IllegalArgumentException
      Description copied from interface: Lua
      Push an array onto the stack, converted to luatable
      Specified by:
      pushArray in interface Lua
      Parameters:
      array - a array
      Throws:
      IllegalArgumentException - when the object is not array
    • push

      public void push(JFunction function)
      Description copied from interface: Lua
      Push the function onto the stack, converted to a callable element

      The function is wrapped into a C closure, which means Lua will treat the function as a C function. Checking Lua.isFunction(int) on the pushed element will return true.

      Specified by:
      push in interface Lua
      Parameters:
      function - the function to be pushed onto the stack
    • push

      public void push(LuaValue value)
      Description copied from interface: Lua
      Push a LuaValue onto the stack, equivalent to LuaValue.push(Lua)
      Specified by:
      push in interface Lua
      Parameters:
      value - the value
    • push

      public void push(LuaFunction function)
      Description copied from interface: Lua
      Push the function onto the stack, converted to a callable element
      Specified by:
      push in interface Lua
      Parameters:
      function - the function
      See Also:
    • pushJavaObject

      public void pushJavaObject(Object object) throws IllegalArgumentException
      Description copied from interface: Lua
      Push the element onto the stack, converted as is to Java objects
      Specified by:
      pushJavaObject in interface Lua
      Parameters:
      object - the element to be pushed onto the stack
      Throws:
      IllegalArgumentException - when argument is null or an array
    • pushJavaArray

      public void pushJavaArray(Object array) throws IllegalArgumentException
      Description copied from interface: Lua
      Push the element onto the stack, converted as is to Java arrays
      Specified by:
      pushJavaArray in interface Lua
      Parameters:
      array - the element to be pushed onto the stack
      Throws:
      IllegalArgumentException - when argument is null or a non-array object
    • pushJavaClass

      public void pushJavaClass(Class<?> clazz)
      Description copied from interface: Lua
      Push a class onto the stack, which may be used with `java.new` on the lua side
      Specified by:
      pushJavaClass in interface Lua
      Parameters:
      clazz - the class
    • toAbsoluteIndex

      public int toAbsoluteIndex(int index)
      Converts a stack index into an absolute index.
      Parameters:
      index - a stack index
      Returns:
      an absolute positive stack index
    • toNumber

      public double toNumber(int index)
      Description copied from interface: Lua
      Converts the Lua value at the given acceptable index to a number

      The Lua value must be a number or a string convertible to a number; otherwise, lua_tonumber returns 0.

      Specified by:
      toNumber in interface Lua
      Parameters:
      index - the stack index
      Returns:
      the converted value, zero if not convertible
    • toInteger

      public long toInteger(int index)
      Description copied from interface: Lua
      Converts the Lua value at the given acceptable index to the signed integral type lua_Integer

      The Lua value must be a number or a string convertible to a number; otherwise, lua_tointeger returns 0. If the number is not an integer, it is truncated in some non-specified way.

      Specified by:
      toInteger in interface Lua
      Parameters:
      index - the stack index
      Returns:
      the converted value, zero if not convertible
    • toBoolean

      public boolean toBoolean(int index)
      Description copied from interface: Lua
      Converts the Lua value at the given acceptable index to a boolean value

      Like all tests in Lua, lua_toboolean returns 1 for any Lua value different from false and nil; otherwise it returns 0. It also returns 0 when called with a non-valid index.

      Specified by:
      toBoolean in interface Lua
      Parameters:
      index - the stack index
      Returns:
      the converted value, false with and only with false, nil or TNONE
    • toObject

      public @Nullable Object toObject(int index)
      Description copied from interface: Lua
      Automatically converts a value into a Java object
      1. nil is converted to null.
      2. boolean converted to boolean or the boxed Boolean.
      3. integer / number to any of char byte short int long float double or their boxed alternative.
      4. string to String.
      5. table to Map<Object, Object>, converted recursively.
      6. jclass to Class<?>.
      7. jobject to the underlying Java object.
      8. Other types are not converted and are null on the Java side.
      Specified by:
      toObject in interface Lua
      Parameters:
      index - the stack index
      Returns:
      the converted object, null if unable to converted
    • toObject

      public @Nullable Object toObject(int index, Class<?> type)
      Description copied from interface: Lua
      Converts a value at the stack index
      Specified by:
      toObject in interface Lua
      Parameters:
      index - the stack index
      type - the target type
      Returns:
      the converted value, null if unable to converted
      See Also:
    • toString

      public @Nullable String toString(int index)
      Description copied from interface: Lua
      Converts the Lua value at the given acceptable index to a string

      The 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.

      Specified by:
      toString in interface Lua
      Parameters:
      index - the stack index
      Returns:
      the converted string
    • toBuffer

      public @Nullable ByteBuffer toBuffer(int index)
      Description copied from interface: Lua
      Creates a ByteBuffer from the string at the specific index

      You may want to use this instead of Lua.toString(int) when the string is binary (e.g., those returned by string.dump and contains null characters).

      Specified by:
      toBuffer in interface Lua
      Parameters:
      index - the stack index
      Returns:
      the created buffer
    • toDirectBuffer

      public @Nullable ByteBuffer toDirectBuffer(int index)
      Description copied from interface: Lua
      Creates a read-only direct ByteBuffer from the string at the specific index

      The memory of this buffer is managed by Lua. So you should never use the buffer after popping the corresponding value from the Lua stack.

      Specified by:
      toDirectBuffer in interface Lua
      Parameters:
      index - the stack index
      Returns:
      the created read-only buffer
    • toJavaObject

      public @Nullable Object toJavaObject(int index)
      Description copied from interface: Lua
      Get the element at the specified stack position, if the element is a Java object / array / class
      Specified by:
      toJavaObject in interface Lua
      Parameters:
      index - the stack position of the element
      Returns:
      the Java object or null
    • toMap

      public @Nullable Map<?,?> toMap(int index)
      Description copied from interface: Lua
      Get the element at the specified stack position, converted to a Map

      The element may be a Lua table or a Java Map, or else, it returns null.

      Specified by:
      toMap in interface Lua
      Parameters:
      index - the stack position of the element
      Returns:
      the map or null
    • toList

      public @Nullable List<?> toList(int index)
      Description copied from interface: Lua
      Get the element at the specified stack position, converted to List

      The element may be a Lua table or a Java List, or else, it returns null.

      Specified by:
      toList in interface Lua
      Parameters:
      index - the stack position of the element
      Returns:
      the list or null
    • isBoolean

      public boolean isBoolean(int index)
      Description copied from interface: Lua
      Returns true if the value at the given index is a boolean, and false otherwise
      Specified by:
      isBoolean in interface Lua
      Parameters:
      index - the stack index
      Returns:
      true if the value at the given index is a boolean, and false otherwise
    • isFunction

      public boolean isFunction(int index)
      Description copied from interface: Lua
      Returns true if the value at the given index is a function (either C or Lua), and false otherwise

      When one pushes a JFunction onto the stack using Lua.push(JFunction), the JFunction is wrapped into a C closure, so that it is treated as a C function in Lua.

      Specified by:
      isFunction in interface 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

      public boolean isJavaObject(int index)
      Description copied from interface: Lua
      Checks if the element is a Java object

      Note that a JFunction pushed with Lua.push(JFunction) is not a Java object any more, but a C function.

      Specified by:
      isJavaObject in interface Lua
      Parameters:
      index - the element to check type for
      Returns:
      true if the element is a Java object, a Java class, or a Java array
    • isNil

      public boolean isNil(int index)
      Description copied from interface: Lua
      Returns true if the value at the given index is nil, and false otherwise
      Specified by:
      isNil in interface Lua
      Parameters:
      index - the stack index
      Returns:
      true if the value at the given index is nil, and false otherwise
    • isNone

      public boolean isNone(int index)
      Description copied from interface: Lua
      Returns true if the given index is not valid, and false otherwise
      Specified by:
      isNone in interface Lua
      Parameters:
      index - the stack index
      Returns:
      true if the given index is not valid, and false otherwise
    • isNoneOrNil

      public boolean isNoneOrNil(int index)
      Description copied from interface: Lua
      Returns true if the given index is not valid or if the value at this index is nil, and false otherwise
      Specified by:
      isNoneOrNil in interface Lua
      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

      public boolean isNumber(int index)
      Description copied from interface: Lua
      Returns true if the value is a number or a string convertible to a number, and false otherwise
      Specified by:
      isNumber in interface Lua
      Parameters:
      index - the stack index
      Returns:
      true if the value is a number or a string convertible to a number, and false otherwise
    • isInteger

      public boolean isInteger(int index)
      Description copied from interface: Lua
      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)

      Specified by:
      isInteger in interface Lua
      Parameters:
      index - the stack index
      Returns:
      true if the value is an integer, and false otherwise
    • isString

      public boolean isString(int index)
      Description copied from interface: Lua
      Returns true if the value at the given index is a string or a number
      Specified by:
      isString in interface Lua
      Parameters:
      index - the stack index
      Returns:
      true if the value at the given index is a string or a number, and false otherwise.
    • isTable

      public boolean isTable(int index)
      Description copied from interface: Lua
      Returns true if the value at the given index is a table, and false otherwise.
      Specified by:
      isTable in interface Lua
      Parameters:
      index - the stack index
      Returns:
      Returns true if the value at the given index is a table, and false otherwise.
    • isThread

      public boolean isThread(int index)
      Description copied from interface: Lua
      Returns true if the value at the given index is a thread, and false otherwise.
      Specified by:
      isThread in interface Lua
      Parameters:
      index - the stack index
      Returns:
      true if the value at the given index is a thread, and 0 otherwise.
    • isUserdata

      public boolean isUserdata(int index)
      Description copied from interface: Lua
      Returns true if the value at the given index is userdata (either full or light), and false otherwise.
      Specified by:
      isUserdata in interface Lua
      Parameters:
      index - the stack index
      Returns:
      true if the value at the given index is userdata (either full or light), and false otherwise.
    • type

      public @Nullable Lua.LuaType type(int index)
      Description copied from interface: Lua
      Returns the Lua type of the element at the given index.
      Specified by:
      type in interface Lua
      Parameters:
      index - the element to inspect
      Returns:
      the lua type of the element, null if unrecognized (in, for example, incompatible lua versions)
    • equal

      public boolean equal(int i1, int i2)
      Description copied from interface: Lua
      Returns true if the two values in acceptable indices i1 and i2 are equal

      Returns 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.

      Specified by:
      equal in interface Lua
      Parameters:
      i1 - the index of the first element
      i2 - the index of the second element
      Returns:
      true if the two values in acceptable indices i1 and i2 are equal
    • rawLength

      public int rawLength(int index)
      Description copied from interface: Lua
      Returns the raw "length" of the value at the given index

      For 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.

      Specified by:
      rawLength in interface Lua
      Parameters:
      index - the stack index
      Returns:
      the raw length of the element
    • lessThan

      public boolean lessThan(int i1, int i2)
      Description copied from interface: Lua
      Returns true if the value at acceptable index i1 is smaller than the value at i2

      It 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.

      Specified by:
      lessThan in interface Lua
      Parameters:
      i1 - the index of the first element
      i2 - the index of the second element
      Returns:
      true if the value at acceptable index i1 is smaller than the value at i2
    • rawEqual

      public boolean rawEqual(int i1, int i2)
      Description copied from interface: Lua
      Returns true if the two values in acceptable indices i1 and i2 are primitively equal

      It does not call metamethods. Otherwise returns false. Also returns false if any of the indices are non valid.

      Specified by:
      rawEqual in interface Lua
      Parameters:
      i1 - the index of the first element
      i2 - the index of the second element
      Returns:
      true if the two values in acceptable indices i1 and i2 are primitively equal
    • getTop

      public int getTop()
      Description copied from interface: Lua
      Returns the index of the top element in the stack

      Because indices start at 1, this result is equal to the number of elements in the stack (and so 0 means an empty stack).

      Specified by:
      getTop in interface Lua
      Returns:
      the index of the top element in the stack
    • setTop

      public void setTop(int index)
      Description copied from interface: Lua
      Accepts any index, or 0, and sets the stack top to this index

      If 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.

      Specified by:
      setTop in interface Lua
      Parameters:
      index - the new top element index
    • insert

      public void insert(int index)
      Description copied from interface: Lua
      Moves the top element into the given valid index, shifting up the elements above this index

      Moves 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.

      Specified by:
      insert in interface Lua
      Parameters:
      index - the non-pseudo index
      See Also:
    • pop

      public void pop(int n)
      Description copied from interface: Lua
      Pops n elements from the stack
      Specified by:
      pop in interface Lua
      Parameters:
      n - the number of elements to pop
    • pushValue

      public void pushValue(int index)
      Description copied from interface: Lua
      Pushes a copy of the element at the given valid index onto the stack
      Specified by:
      pushValue in interface Lua
      Parameters:
      index - the index of the element to be copied
      See Also:
    • pushThread

      public void pushThread()
      Description copied from interface: Lua
      Pushes the current thread onto the stack
      Specified by:
      pushThread in interface Lua
    • remove

      public void remove(int index)
      Description copied from interface: Lua
      Removes the element at the given valid index

      Removes 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.

      Specified by:
      remove in interface Lua
      Parameters:
      index - the index of the element to be removed
    • replace

      public void replace(int index)
      Description copied from interface: Lua
      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).

      Specified by:
      replace in interface Lua
      Parameters:
      index - the index to move to
      See Also:
    • xMove

      public void xMove(Lua other, int n) throws IllegalArgumentException
      Description copied from interface: Lua
      Exchange values between different threads of the same global state

      This function pops n values from the stack of this thread, and pushes them onto the stack of the other thread

      Specified by:
      xMove in interface Lua
      Parameters:
      other - the thread to move the n values to
      n - the number of elements to move
      Throws:
      IllegalArgumentException - when the two threads do not belong to the same global state
    • load

      public void load(String script) throws LuaException
      Description copied from interface: Lua
      Loads a string as a Lua chunk

      This function eventually uses lua_load to load the chunk in the string script. Also as lua_load, this function only loads the chunk; it does not run it.

      Specified by:
      load in interface Lua
      Parameters:
      script - the Lua chunk
      Throws:
      LuaException
      See Also:
    • load

      public void load(Buffer buffer, String name) throws LuaException
      Description copied from interface: Lua
      Loads a buffer as a Lua chunk

      This function eventually uses lua_load to load the chunk in the string script. 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.

      Specified by:
      load in interface Lua
      Parameters:
      buffer - the buffer, must be a direct buffer
      name - the chunk name, used for debug information and error messages
      Throws:
      LuaException
      See Also:
    • run

      public void run(String script) throws LuaException
      Description copied from interface: Lua
      Loads and runs the given string

      It is equivalent to first calling Lua.load(String) and then Lua.pCall(int, int) the loaded chunk.

      Specified by:
      run in interface Lua
      Parameters:
      script - the Lua chunk
      Throws:
      LuaException
    • run

      public void run(Buffer buffer, String name) throws LuaException
      Description copied from interface: Lua
      Loads and runs a buffer

      It is equivalent to first calling Lua.load(Buffer, String) and then Lua.pCall(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.

      Specified by:
      run in interface Lua
      Parameters:
      buffer - the buffer, must be a direct buffer
      name - the chunk name, used for debug information and error messages
      Throws:
      LuaException
    • dump

      public @Nullable ByteBuffer dump()
      Description copied from interface: Lua
      Dumps a function as a binary chunk

      Receives 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.

      Specified by:
      dump in interface Lua
      Returns:
      the binary chunk, null if an error occurred
    • pCall

      public void pCall(int nArgs, int nResults) throws LuaException
      Description copied from interface: Lua
      Calls a function in protected mode

      To 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.

      Specified by:
      pCall in interface Lua
      Parameters:
      nArgs - the number of arguments that you pushed onto the stack
      nResults - the number of results to adjust to
      Throws:
      LuaException
    • newThread

      public AbstractLua newThread()
      Description copied from interface: Lua
      Creates a new thread, pushes it on the stack

      The new state returned by this function shares with the original state all global objects (such as tables), but has an independent execution stack.

      Specified by:
      newThread in interface Lua
      Returns:
      a new thread
    • addSubThread

      protected void addSubThread(Lua lua)
      Adds a sub-thread to this main thread's tracking list.
      Parameters:
      lua - the sub-thread to add
    • newThread

      protected abstract AbstractLua newThread(long L, int id, AbstractLua mainThread)
      Creates a new sub-thread attached to this main thread.
      Parameters:
      L - the pointer to the new Lua state
      id - the unique identifier for the new state
      mainThread - the main thread this state belongs to
      Returns:
      the new sub-thread
    • resume

      public boolean resume(int nArgs) throws LuaException
      Description copied from interface: Lua
      Starts and resumes a coroutine in a given thread

      To start a coroutine, you first create a new thread (see lua_newthread or Lua.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.

      Specified by:
      resume in interface Lua
      Parameters:
      nArgs - the number of arguments
      Returns:
      true if the thread yielded, or false if it ended execution
      Throws:
      LuaException
    • status

      public LuaException.LuaError status()
      Description copied from interface: Lua
      Returns the status of the thread
      Specified by:
      status in interface Lua
      Returns:
      the status of the thread
    • yield

      public void yield(int n)
      Description copied from interface: Lua
      Yields a coroutine

      This is not implemented because we have no way to resume execution from a Java stack through a C stack back to a Java stack.

      Specified by:
      yield in interface Lua
      Parameters:
      n - the number of values from the stack that are passed as results to lua_resume
    • newTable

      public void newTable()
      Description copied from interface: Lua
      Creates a new empty table and pushes it onto the stack

      It is equivalent to createTable(0, 0).

      Specified by:
      newTable in interface Lua
    • createTable

      public void createTable(int nArr, int nRec)
      Description copied from interface: Lua
      Creates a new empty table and pushes it onto the stack

      The 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.

      Specified by:
      createTable in interface Lua
      Parameters:
      nArr - pre-allocated array elements
      nRec - pre-allocated non-array elements
    • getField

      public void getField(int index, String key)
      Description copied from interface: Lua
      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.

      Specified by:
      getField in interface Lua
      Parameters:
      index - the index of the table-like element
      key - the key to look up
    • setField

      public void setField(int index, String key)
      Description copied from interface: Lua
      Does the equivalent to t[key] = v

      Does 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.

      Specified by:
      setField in interface Lua
      Parameters:
      index - the index of the table-like element
      key - the key to assign to
    • getTable

      public void getTable(int index)
      Description copied from interface: Lua
      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.

      Specified by:
      getTable in interface Lua
      Parameters:
      index - the index of the table-like element
    • setTable

      public void setTable(int index)
      Description copied from interface: Lua
      Does the equivalent to t[k] = v

      Does 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.

      Specified by:
      setTable in interface Lua
      Parameters:
      index - the index of the table-like element
    • next

      public int next(int n)
      Description copied from interface: Lua
      Pops a key from the stack, and pushes a key-value pair from the table at the given index

      Pops 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 Lua.toString(int) directly on a key, unless you know that the key is actually a string. Recall that Lua.toString(int) changes the value at the given index; this confuses the next call to lua_next.

      Specified by:
      next in interface Lua
      Parameters:
      n - the index of the table
      Returns:
      0 if there are no more elements
    • rawGet

      public void rawGet(int index)
      Description copied from interface: Lua
      Similar to Lua.getTable(int), but does a raw access (i.e., without metamethods)
      Specified by:
      rawGet in interface Lua
      Parameters:
      index - the index of the table
    • rawGetI

      public void rawGetI(int index, int n)
      Description copied from interface: Lua
      Pushes onto the stack the value t[n], where t is the value at the given valid index

      The access is raw; that is, it does not invoke metamethods.

      Specified by:
      rawGetI in interface Lua
      Parameters:
      index - the index of the table
      n - the key
    • rawSet

      public void rawSet(int index)
      Description copied from interface: Lua
      Similar to Lua.setTable(int), but does a raw assignment (i.e., without metamethods)
      Specified by:
      rawSet in interface Lua
      Parameters:
      index - the index of the table
    • rawSetI

      public void rawSetI(int index, int n)
      Description copied from interface: Lua
      Does the equivalent of t[n] = v

      Does 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.

      Specified by:
      rawSetI in interface Lua
      Parameters:
      index - the index of the table
      n - the key
    • ref

      public int ref(int index)
      Description copied from interface: Lua
      Creates and returns a reference, in the table at index index

      Creates 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 Lua.rawGetI(int, int). Function Lua.unRef(int, int) frees a reference and its associated object.

      Specified by:
      ref in interface Lua
      Parameters:
      index - the index of the table
      Returns:
      the created reference
    • unRef

      public void unRef(int index, int ref)
      Description copied from interface: Lua
      Releases reference ref from the table at index index

      The entry is removed from the table, so that the referred object can be collected. The reference ref is also freed to be used again.

      Specified by:
      unRef in interface Lua
      Parameters:
      index - the index of the table
      ref - the reference to be freed
    • getGlobal

      public void getGlobal(String name)
      Description copied from interface: Lua
      Pushes onto the stack the value of the global name
      Specified by:
      getGlobal in interface Lua
      Parameters:
      name - the global name
    • setGlobal

      public void setGlobal(String name)
      Description copied from interface: Lua
      Pops a value from the stack and sets it as the new value of global name
      Specified by:
      setGlobal in interface Lua
      Parameters:
      name - the global name
    • getMetatable

      public int getMetatable(int index)
      Description copied from interface: Lua
      Pushes onto the stack the metatable of the value at the given acceptable index

      Pushes 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.

      Specified by:
      getMetatable in interface Lua
      Parameters:
      index - the index of the element
      Returns:
      0 if the value does not have a metatable
    • setMetatable

      public void setMetatable(int index)
      Description copied from interface: Lua
      Pops a table from the stack and sets it as the new metatable for the value at the given acceptable index
      Specified by:
      setMetatable in interface Lua
      Parameters:
      index - the index of the element
    • getMetaField

      public int getMetaField(int index, String field)
      Description copied from interface: Lua
      Pushes onto the stack the field field from the metatable of the object at index index

      If the object does not have a metatable, or if the metatable does not have this field, returns 0 and pushes nothing.

      Specified by:
      getMetaField in interface Lua
      Parameters:
      index - the index of the element
      field - the meta field
      Returns:
      0 if no such field
    • getRegisteredMetatable

      public void getRegisteredMetatable(String typeName)
      Description copied from interface: Lua
      Pushes onto the stack the metatable associated with name tname in the registry
      Specified by:
      getRegisteredMetatable in interface Lua
      Parameters:
      typeName - the name of the user-defined type
      See Also:
    • newRegisteredMetatable

      public int newRegisteredMetatable(String typeName)
      Description copied from interface: Lua
      Creates a new table to be used as a metatable for userdata, adds it to the registry

      If 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 key typeName, and returns 1.

      In both cases pushes onto the stack the final value associated with tname in the registry.

      Specified by:
      newRegisteredMetatable in interface Lua
      Parameters:
      typeName - the name of the user-defined type
      Returns:
      1 if added to registry, 0 if already registered
    • openLibraries

      public void openLibraries()
      Description copied from interface: Lua
      Opens all standard Lua libraries into the given state

      See Lua.openLibrary(String) for more info.

      Specified by:
      openLibraries in interface Lua
    • openLibrary

      public void openLibrary(String name)
      Description copied from interface: Lua
      Opens a specific library into the given state

      See the corresponding Lua manual for available library. For example, in Lua 5.4, calling L.openLibrary("base") will call the luaopen_base C function for the base library.

      Note that opening a library may modify global variables. For example, opening the base library in some specific Lua versions will override built-in functions like print. So make sure to make your modifications after opening all the libraries.

      Specified by:
      openLibrary in interface Lua
      Parameters:
      name - the library name
    • concat

      public void concat(int n)
      Description copied from interface: Lua
      Concatenates the n values at the top of the stack, pops them, and leaves the result at the top

      If 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.

      Specified by:
      concat in interface Lua
      Parameters:
      n - the number of values on top of the stack to concatenate
    • gc

      public void gc()
      Description copied from interface: Lua
      Performs a full garbage-collection cycle

      This also removes unneeded references created by finalized proxies and Lua values.

      Specified by:
      gc in interface Lua
    • error

      public void error(String message)
      Description copied from interface: Lua
      Throws an error inside a Lua environment

      It currently just throws a RuntimeException.

      Specified by:
      error in interface Lua
      Parameters:
      message - the error message
    • createProxy

      public Object createProxy(Class<?>[] interfaces, Lua.Conversion degree) throws IllegalArgumentException
      Description copied from interface: Lua
      Creates a proxy object, implementing all the specified interfaces, with a Lua table / function on top of the stack

      This method pops the value on top on the stack and creates reference to it with Lua.ref().

      When invoking methods, the created Java object, instead of the backing Lua table, is passed as the first parameter to the Lua function.

      Specified by:
      createProxy in interface Lua
      Parameters:
      interfaces - the interfaces to implement
      degree - 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
    • register

      public void register(String name, LuaFunction function)
      Description copied from interface: LuaThread
      Registers the function to a global name
      Specified by:
      register in interface LuaThread
      Parameters:
      name - the global name
      function - the function
    • setExternalLoader

      public void setExternalLoader(ExternalLoader loader)
      Description copied from interface: Lua
      Sets a ExternalLoader for the main state

      The 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 (or package.loaders for Lua 5.1) to load Lua files with this ExternalLoader.

      You need to load the package library to make the external loader effective.

      Specified by:
      setExternalLoader in interface Lua
      Parameters:
      loader - the loader that will be used to find files
    • loadExternal

      public void loadExternal(String module) throws LuaException
      Description copied from interface: Lua
      Specified by:
      loadExternal in interface Lua
      Parameters:
      module - the module
      Throws:
      LuaException
    • getLuaNatives

      public LuaNatives getLuaNatives()
      Description copied from interface: Lua
      Returns the underlying native Lua interface.
      Specified by:
      getLuaNatives in interface Lua
      Returns:
      the underlying LuaNatives natives
    • getMainState

      public AbstractLua getMainState()
      Description copied from interface: Lua
      Returns the main Lua state.
      Specified by:
      getMainState in interface Lua
      Returns:
      the main Lua state
    • getPointer

      public long getPointer()
      Description copied from interface: Lua
      Returns the pointer to the internal Lua state.
      Specified by:
      getPointer in interface Lua
      Returns:
      the pointer to the internal lua_State
    • getId

      public int getId()
      Description copied from interface: Lua
      Returns the unique identifier of this Lua thread.
      Specified by:
      getId in interface Lua
      Returns:
      the unique identifier to the Lua thread
    • getJavaError

      public @Nullable Throwable getJavaError()
      Description copied from interface: Lua
      Fetches the most recent Java Throwable passed to Lua
      Specified by:
      getJavaError in interface Lua
      Returns:
      value of the Lua global Lua.GLOBAL_THROWABLE
    • error

      public int error(@Nullable Throwable e)
      Description copied from interface: Lua
      Sets the Lua global Lua.GLOBAL_THROWABLE to the throwable

      If the exception is null, it clears the global exception and pushes nothing. Otherwise, it sets the Lua global Lua.GLOBAL_THROWABLE to the throwable, and pushes Throwable.toString() onto the stack.

      Specified by:
      error in interface Lua
      Parameters:
      e - the exception
      Returns:
      0 if e is null, -1 otherwise
    • invokeSpecial

      protected @Nullable Object invokeSpecial(@Nullable Object object, Method method, @Nullable Object @Nullable [] params) throws Throwable
      Calls a method on an object, equivalent to invokespecial

      Internally it uses LuaNatives.luaJ_invokespecial(long, Class, String, String, Object, String) which then uses CallNonvirtual<Type>MethodA functions to avoid tons of restrictions imposed by the JVM.

      Parameters:
      object - the this object
      method - the method
      params - the parameters
      Returns:
      the return value
      Throws:
      Throwable - whenever the method call throw exceptions
    • isClosed

      protected boolean isClosed()
      Checks if this Lua state has been closed.
      Returns:
      true if the state is closed, false otherwise
    • close

      public void close()
      Description copied from interface: Lua
      Closes the thread

      You 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:
      close in interface AutoCloseable
      Specified by:
      close in interface Lua
    • ref

      public int ref()
      Description copied from interface: Lua
      Calls Lua.ref(int) with the pseudo-index LUA_REGISTRYINDEX
      Specified by:
      ref in interface Lua
      Returns:
      the created reference
    • refGet

      public void refGet(int ref)
      Description copied from interface: Lua
      Calls Lua.rawGetI(int, int) with the pseudo-index LUA_REGISTRYINDEX and the given ref
      Specified by:
      refGet in interface Lua
      Parameters:
      ref - the reference on LUA_REGISTRYINDEX table
    • unref

      public void unref(int ref)
      Description copied from interface: Lua
      Calls Lua.unRef(int, int) with the pseudo-index LUA_REGISTRYINDEX and the given ref
      Specified by:
      unref in interface Lua
      Parameters:
      ref - the reference to be freed
    • checkError

      protected void checkError(int code, boolean runtime) throws LuaException
      Throws LuaException if the code is not LuaException.LuaError.OK.

      Most Lua C API functions attaches along an error message on the stack. If this method finds a string on the top of the stack, it pops the string and uses it as the exception message.

      Parameters:
      code - the error code returned by Lua C API
      runtime - if true, treat non-zero code values as runtime errors
      Throws:
      LuaException
    • convertError

      public abstract LuaException.LuaError convertError(int code)
      Converts a native error code to a LuaError enum value.
      Parameters:
      code - the native error code
      Returns:
      the corresponding LuaError value
    • convertType

      public abstract Lua.LuaType convertType(int code)
      Converts a native type code to a LuaType enum value.
      Parameters:
      code - the native type code
      Returns:
      the corresponding LuaType value
    • get

      public LuaValue get(String globalName)
      Description copied from interface: LuaThread
      Gets a references to a global object
      Specified by:
      get in interface LuaThread
      Parameters:
      globalName - the global name
      Returns:
      a reference to the value
    • set

      public void set(String key, Object value)
      Description copied from interface: LuaThread
      Sets a global variable to the given value
      Specified by:
      set in interface LuaThread
      Parameters:
      key - the global variable name
      value - the value
    • eval

      public LuaValue[] eval(String command) throws LuaException
      Description copied from interface: LuaThread
      Executes Lua code
      Specified by:
      eval in interface LuaThread
      Parameters:
      command - the command
      Returns:
      the return values
      Throws:
      LuaException
    • require

      public LuaValue require(String module) throws LuaException
      Description copied from interface: LuaThread
      Loads a module, similar to the Lua `require` function

      Please note that this method will attempt to call Lua.openLibrary("package") first and cache the global require function.

      Specified by:
      require in interface LuaThread
      Parameters:
      module - the module name
      Returns:
      the module
      Throws:
      LuaException
    • get

      public LuaValue get()
      Description copied from interface: Lua
      Pops the value on top of the stack and return a LuaValue referring to it
      Specified by:
      get in interface Lua
      Returns:
      a reference to the value
    • fromNull

      public LuaValue fromNull()
      Description copied from interface: LuaThread
      Creates a nil Lua value.
      Specified by:
      fromNull in interface LuaThread
      Returns:
      a nil Lua value
    • from

      public LuaValue from(boolean b)
      Description copied from interface: LuaThread
      Creates a boolean Lua value from a Java boolean.
      Specified by:
      from in interface LuaThread
      Parameters:
      b - the boolean
      Returns:
      a boolean Lua value
    • from

      public LuaValue from(double n)
      Description copied from interface: LuaThread
      Creates a number Lua value from a Java double.
      Specified by:
      from in interface LuaThread
      Parameters:
      n - the number
      Returns:
      a number Lua value
    • from

      public LuaValue from(long n)
      Description copied from interface: LuaThread
      Creates a number Lua value from a Java long.
      Specified by:
      from in interface LuaThread
      Parameters:
      n - the number
      Returns:
      a number Lua value
    • from

      public LuaValue from(String s)
      Description copied from interface: LuaThread
      Creates a string Lua value from a Java string.
      Specified by:
      from in interface LuaThread
      Parameters:
      s - the string
      Returns:
      a string Lua value
    • from

      public LuaValue from(ByteBuffer buffer)
      Description copied from interface: LuaThread
      Creates a raw string Lua value from a byte buffer.
      Specified by:
      from in interface LuaThread
      Parameters:
      buffer - the content of the raw string
      Returns:
      a raw string Lua value
    • shouldSynchronize

      protected boolean shouldSynchronize()
      A method specifically for working around deadlocks caused by LuaJ.

      In LuaJ bindings, without this work-around, deadlocks can happen when:

      
       1. (Thread#A) The user synchronizes on mainThread as is required by LuaJava when used
          in multi-threaded environment.
       2. (Thread#A) The user calls run(String) for example, to run a Lua snippet.
       3. The snippet creates a coroutine, mandating LuaJ to create a Java thread (#B).
       4. Inside the coroutine (i.e., the Java thread#B), the code calls a Lua proxy object.
       5. (Thread#B) LuaProxy.invoke(Object, Method, Object[]) tries to synchronizes
          on mainThread.
       6. Since thread#A is already inside a synchronization block, the two threads deadlocks.
       

      This work-around asks LuaProxy.invoke(Object, Method, Object[]) to avoid synchronization when it detects that it is called from a coroutine thread created by LuaJ.

      Returns:
      false only when invoke within a coroutine thread created by LuaJ