Class JuaAPI

java.lang.Object
party.iroiro.luajava.JuaAPI

public abstract class JuaAPI extends Object
Provides complex functions for JNI uses

Most reflection features on the lua side rely on this class.

  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static ByteBuffer
    allocateDirect(int size)
    Allocates a direct buffer whose memory is managed by Java
    static int
    arrayIndex(int index, Object obj, int i)
    Gets an element of an array
    static int
    Returns the length of a Java array.
    static int
    arrayNew(int index, Object oClass, int size)
    Pushes a created array onto the stack
    static int
    arrayNewIndex(int index, Object obj, int i)
    Assigns to an element of an array
    static int
    classIndex(int index, Class<?> clazz, String name)
    Pushes a static field of a class onto the stack
    static int
    classInvoke(int index, Class<?> clazz, String name, int paramCount)
    Invokes a static method of a class
    static int
    classInvoke(int index, Class<?> clazz, String name, String notSignature, int paramCount)
    Invokes a static method of a class
    static int
    classNew(int index, Object oClazz, int paramCount)
    Constructs an instance of a class
    static int
    classNewIndex(int index, Class<?> clazz, String name)
    Assigns to a static field
    static @Nullable Object
    convertFromLua(Lua L, Class<?> clazz, int index)
    Converts an element on the lua stack at index to Java
    static int
    fieldIndex(Lua L, Class<?> clazz, @Nullable Object object, String name)
    Tries to fetch field from an object
    static int
    freeThreadId(int id)
    Closes a sub-thread
    static @Nullable Class<?>[]
    getClasses(@Nullable String notSignature)
    Converts a comma-separated string of class names to an array of Class objects.
    static int
    javaImport(int id, String className)
    Pushes onto the stack a jclass or a package
    static int
    load(int id, String module)
    Loads a Lua chunk according with Lua.loadExternal(String)
    static int
    loadLib(int id, String className, String methodName)
    Loads a Java static method that accepts a single Lua parameter and returns an integer
    static int
    loadModule(int id, String module)
    Loads a Java static method that accepts a single Lua parameter and returns an integer
    static int
    luaify(int id)
    Converts a value on top of the stack into a more Lua-style form
    static int
    methodInvoke(int index, Class<?> clazz, @Nullable Object obj, String name, int paramCount)
    Calls the given method {obj}.{name}(...
    static int
    methodInvoke(int index, Class<?> clazz, @Nullable Object obj, String name, String notSignature, int paramCount)
    Invokes a method
    static int
    methodInvoke(Lua L, Method method, @Nullable Object obj, Object @Nullable [] objects)
    Invokes a method and pushes the result onto the stack
    static int
    objectIndex(int index, Object object, String name)
    Obtains the value of a certain field of an object
    static int
    objectInvoke(int index, Object obj, @Nullable String name, int paramCount)
    Invokes a member method of an object
    static int
    objectInvoke(int index, Object obj, String name, String notSignature, int paramCount)
    Invokes a member method of an object
    static int
    objectNewIndex(int index, Object obj, String name)
    Sets a field of an object
    static int
    proxy(int id)
    Creates a proxy
    static int
    threadNewId(int mainId, long ptr)
    Allocates an ID for a thread created on the Lua side
    static int
    unwrap(int id, Object obj)
    Pushes on stack the backing Lua table for a proxy

    Methods inherited from class java.lang.Object

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

    • JuaAPI

      public JuaAPI()
  • Method Details

    • allocateDirect

      public static ByteBuffer allocateDirect(int size)
      Allocates a direct buffer whose memory is managed by Java
      Parameters:
      size - the buffer size
      Returns:
      a direct buffer
    • unwrap

      public static int unwrap(int id, Object obj)
      Pushes on stack the backing Lua table for a proxy
      Parameters:
      id - the Lua state id
      obj - the proxy object
      Returns:
      -1 on failure, 1 if successfully pushed
    • load

      public static int load(int id, String module)
      Loads a Lua chunk according with Lua.loadExternal(String)

      Used in jmoduleLoad in jni/luajava/juaapi.cpp

      Parameters:
      id - see AbstractLua.getInstance(int)
      module - the module name
      Returns:
      always 1
    • loadModule

      public static int loadModule(int id, String module)
      Loads a Java static method that accepts a single Lua parameter and returns an integer
      Parameters:
      id - see AbstractLua.getInstance(int)
      module - the module name, i.e., the class name and the method name joined by a dot
      Returns:
      the number of elements pushed onto the stack
    • loadLib

      public static int loadLib(int id, String className, String methodName)
      Loads a Java static method that accepts a single Lua parameter and returns an integer
      Parameters:
      id - see AbstractLua.getInstance(int)
      className - the class name
      methodName - the method name
      Returns:
      the number of elements pushed onto the stack
    • proxy

      public static int proxy(int id)
      Creates a proxy

      See also the java.proxy API and javaProxy in jni/luajava/lualib.cpp.

      Parameters:
      id - the Lua state id
      Returns:
      the number of return values pushed on stack (i.e., 1 if successful)
    • javaImport

      public static int javaImport(int id, String className)
      Pushes onto the stack a jclass or a package

      See the java.import API and javaImport in jni/luajava/jualib.cpp.

      Parameters:
      id - the lua state id
      className - name of a class or a package
      Returns:
      the number of values pushed onto the stack
    • luaify

      public static int luaify(int id)
      Converts a value on top of the stack into a more Lua-style form
      Parameters:
      id - the state id
      Returns:
      the number of return values, always 1
    • threadNewId

      public static int threadNewId(int mainId, long ptr)
      Allocates an ID for a thread created on the Lua side
      Parameters:
      mainId - the main thread id
      ptr - the pointer to the lua state who does not have an id
      Returns:
      an allocated id
    • freeThreadId

      public static int freeThreadId(int id)
      Closes a sub-thread
      Parameters:
      id - the thread id
      Returns:
      1
    • objectIndex

      public static int objectIndex(int index, Object object, String name)
      Obtains the value of a certain field of an object

      This method is mainly used by the JNI side. See jni/luajava/jua.cpp.

      For static fields, use classIndex(int, Class, String) instead.

      Parameters:
      index - the id of Jua thread calling this method
      object - the object
      name - the name of the field
      Returns:
      1 if a field is found, 2 otherwise
      See Also:
    • objectInvoke

      public static int objectInvoke(int index, Object obj, @Nullable String name, int paramCount)
      Invokes a member method of an object

      This method is mainly used by the JNI side. See jni/luajava/jua.cpp. If name is null, we assume that the object is a JFunction.

      For static fields, use classInvoke(int, Class, String, int) instead.

      Parameters:
      index - the id of Jua thread calling this method
      obj - the object
      name - the name of the field
      paramCount - number of parameters (on the lua stack)
      Returns:
      the number result pushed on stack
      See Also:
    • objectInvoke

      public static int objectInvoke(int index, Object obj, String name, String notSignature, int paramCount)
      Invokes a member method of an object
      Parameters:
      index - the lua state index
      obj - the object
      name - the method name
      notSignature - the method signature, comma separated
      paramCount - the parameter count
      Returns:
      the number of values pushed onto the stack
    • objectNewIndex

      public static int objectNewIndex(int index, Object obj, String name)
      Sets a field of an object
      Parameters:
      index - the lua state index
      obj - the object
      name - the field name
      Returns:
      always 0
    • classNew

      public static int classNew(int index, Object oClazz, int paramCount)
      Constructs an instance of a class
      Parameters:
      index - the lua state index
      oClazz - the class (Object typed to manually handle mismatched types)
      paramCount - the parameter count
      Returns:
      the number of values pushed onto the stack
    • classIndex

      public static int classIndex(int index, Class<?> clazz, String name)
      Pushes a static field of a class onto the stack

      Used by jobjectIndex in jni/luajava/luaapi.cpp

      Parameters:
      index - the lua state index
      clazz - the class
      name - the field name
      Returns:
      the number of values pushed onto the stack
    • classInvoke

      public static int classInvoke(int index, Class<?> clazz, String name, int paramCount)
      Invokes a static method of a class
      Parameters:
      index - the lua state index
      clazz - the class
      name - the method name
      paramCount - the parameter count
      Returns:
      the number of values pushed onto the stack
    • classInvoke

      public static int classInvoke(int index, Class<?> clazz, String name, String notSignature, int paramCount)
      Invokes a static method of a class
      Parameters:
      index - the lua state index
      clazz - the class
      name - the method name
      notSignature - the signature, comma separated
      paramCount - the parameter count
      Returns:
      the number of values pushed onto the stack
    • classNewIndex

      public static int classNewIndex(int index, Class<?> clazz, String name)
      Assigns to a static field
      Parameters:
      index - the lua state index
      clazz - the class
      name - the field name
      Returns:
      the number of values pushed onto the stack
    • arrayNew

      public static int arrayNew(int index, Object oClass, int size)
      Pushes a created array onto the stack

      Use negative size to indicate that we should create a multi-dimensional array, with the dimensions read from the stack.

      Parameters:
      index - the lua state index
      oClass - the class
      size - the array size
      Returns:
      the number of values pushed onto the stack
    • arrayIndex

      public static int arrayIndex(int index, Object obj, int i)
      Gets an element of an array
      Parameters:
      index - the lua state index
      obj - the array
      i - the index (lua index, starting from 1)
      Returns:
      the number of values pushed onto the stack
    • arrayNewIndex

      public static int arrayNewIndex(int index, Object obj, int i)
      Assigns to an element of an array
      Parameters:
      index - the lua state index
      obj - the array
      i - the index (lua index, starting from 1)
      Returns:
      the number of values pushed onto the stack
    • arrayLength

      public static int arrayLength(Object obj)
      Returns the length of a Java array.
      Parameters:
      obj - the array
      Returns:
      the array length
    • methodInvoke

      public static int methodInvoke(int index, Class<?> clazz, @Nullable Object obj, String name, int paramCount)
      Calls the given method {obj}.{name}(... params from stack)

      Judge param types from the lua stack and pushes results afterwards. The params are expected to be on stack positions from -paramCount to -1. For example, if you have two params, then the first is expected to be at -2, and the second be at -1.

      Parameters:
      index - the index of Java-side Jua
      clazz - the Class
      obj - the object (nullable when calling static methods)
      name - the method name
      paramCount - number of supplied params
      Returns:
      the number result pushed on stack
    • methodInvoke

      public static int methodInvoke(int index, Class<?> clazz, @Nullable Object obj, String name, String notSignature, int paramCount)
      Invokes a method
      Parameters:
      index - the lua state index
      clazz - the class
      obj - the object, null if calling a static method
      name - the method name
      notSignature - method signature, comma separated
      paramCount - the parameter count
      Returns:
      the number of values pushed onto the stack
    • methodInvoke

      public static int methodInvoke(Lua L, Method method, @Nullable Object obj, Object @Nullable [] objects)
      Invokes a method and pushes the result onto the stack
      Parameters:
      L - the lua state
      method - the method
      obj - the object
      objects - the parameters
      Returns:
      the number of values pushed onto the stack
    • fieldIndex

      public static int fieldIndex(Lua L, Class<?> clazz, @Nullable Object object, String name)
      Tries to fetch field from an object

      When a matching field is found, it is pushed to the corresponding lua stack

      Parameters:
      L - the Lua state
      clazz - the Class
      object - the object
      name - the name of the field / method
      Returns:
      1 if a field is found, 2 otherwise
    • convertFromLua

      public static @Nullable Object convertFromLua(Lua L, Class<?> clazz, int index) throws IllegalArgumentException
      Converts an element on the lua stack at index to Java
      Parameters:
      L - the lua state
      clazz - the expected return type
      index - a lua index (that is, starts from 1)
      Returns:
      the converted element
      Throws:
      IllegalArgumentException - when unable to convert
    • getClasses

      public static @Nullable Class<?>[] getClasses(@Nullable String notSignature)
      Converts a comma-separated string of class names to an array of Class objects.
      Parameters:
      notSignature - comma-separated class names
      Returns:
      array of Class objects