Class LuaJ

java.lang.Object
party.iroiro.luajava.AbstractLua
party.iroiro.luajava.luaj.LuaJ
All Implemented Interfaces:
AutoCloseable, Lua, LuaThread

public class LuaJ extends AbstractLua
A thin wrapper around LuaJ to provide Lua C API-like API.

Please note that LuaJ bindings are limited in its capabilities. Other bindings provided by LuaJava utilizes JNI functions to provide functionalities like calling default methods in interfaces. However, LuaJ uses no JNI, and thus won't be able to do things beyond the Java reflection API.

  • Constructor Details

    • LuaJ

      public LuaJ(long L, int id, AbstractLua mainThread)
    • LuaJ

      public LuaJ()
  • Method Details

    • newThread

      protected AbstractLua newThread(long L, int id, AbstractLua mainThread)
      Description copied from class: AbstractLua
      Creates a new sub-thread attached to this main thread.
      Specified by:
      newThread in class AbstractLua
      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
    • convertError

      public LuaException.LuaError convertError(int code)
      Description copied from class: AbstractLua
      Converts a native error code to a LuaError enum value.
      Specified by:
      convertError in class AbstractLua
      Parameters:
      code - the native error code
      Returns:
      the corresponding LuaError value
    • convertType

      public Lua.LuaType convertType(int code)
      Description copied from class: AbstractLua
      Converts a native type code to a LuaType enum value.
      Specified by:
      convertType in class AbstractLua
      Parameters:
      code - the native type code
      Returns:
      the corresponding LuaType value
    • shouldSynchronize

      protected boolean shouldSynchronize()
      Description copied from class: AbstractLua
      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 AbstractLua.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.

      Overrides:
      shouldSynchronize in class AbstractLua
      Returns:
      false only when invoke within a coroutine thread created by LuaJ