Class JuaAPI
Most reflection features on the lua side rely on this class.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ByteBufferallocateDirect(int size) Allocates a direct buffer whose memory is managed by Javastatic intarrayIndex(int index, Object obj, int i) Gets an element of an arraystatic intarrayLength(Object obj) Returns the length of a Java array.static intPushes a created array onto the stackstatic intarrayNewIndex(int index, Object obj, int i) Assigns to an element of an arraystatic intclassIndex(int index, Class<?> clazz, String name) Pushes a static field of a class onto the stackstatic intclassInvoke(int index, Class<?> clazz, String name, int paramCount) Invokes a static method of a classstatic intclassInvoke(int index, Class<?> clazz, String name, String notSignature, int paramCount) Invokes a static method of a classstatic intConstructs an instance of a classstatic intclassNewIndex(int index, Class<?> clazz, String name) Assigns to a static fieldstatic @Nullable ObjectconvertFromLua(Lua L, Class<?> clazz, int index) Converts an element on the lua stack atindexto Javastatic intfieldIndex(Lua L, Class<?> clazz, @Nullable Object object, String name) Tries to fetch field from an objectstatic intfreeThreadId(int id) Closes a sub-threadstatic @Nullable Class<?>[]getClasses(@Nullable String notSignature) Converts a comma-separated string of class names to an array of Class objects.static intjavaImport(int id, String className) Pushes onto the stack a jclass or a packagestatic intLoads a Lua chunk according withLua.loadExternal(String)static intLoads a Java static method that accepts a singleLuaparameter and returns an integerstatic intloadModule(int id, String module) Loads a Java static method that accepts a singleLuaparameter and returns an integerstatic intluaify(int id) Converts a value on top of the stack into a more Lua-style formstatic intmethodInvoke(int index, Class<?> clazz, @Nullable Object obj, String name, int paramCount) Calls the given method{obj}.{name}(...static intmethodInvoke(int index, Class<?> clazz, @Nullable Object obj, String name, String notSignature, int paramCount) Invokes a methodstatic intmethodInvoke(Lua L, Method method, @Nullable Object obj, Object @Nullable [] objects) Invokes a method and pushes the result onto the stackstatic intobjectIndex(int index, Object object, String name) Obtains the value of a certain field of an objectstatic intobjectInvoke(int index, Object obj, @Nullable String name, int paramCount) Invokes a member method of an objectstatic intobjectInvoke(int index, Object obj, String name, String notSignature, int paramCount) Invokes a member method of an objectstatic intobjectNewIndex(int index, Object obj, String name) Sets a field of an objectstatic intproxy(int id) Creates a proxystatic intthreadNewId(int mainId, long ptr) Allocates an ID for a thread created on the Lua sidestatic intPushes on stack the backing Lua table for a proxy
-
Constructor Details
-
JuaAPI
public JuaAPI()
-
-
Method Details
-
allocateDirect
Allocates a direct buffer whose memory is managed by Java- Parameters:
size- the buffer size- Returns:
- a direct buffer
-
unwrap
Pushes on stack the backing Lua table for a proxy- Parameters:
id- the Lua state idobj- the proxy object- Returns:
- -1 on failure, 1 if successfully pushed
-
load
Loads a Lua chunk according withLua.loadExternal(String)Used in
jmoduleLoadinjni/luajava/juaapi.cpp- Parameters:
id- seeAbstractLua.getInstance(int)module- the module name- Returns:
- always 1
-
loadModule
Loads a Java static method that accepts a singleLuaparameter and returns an integer- Parameters:
id- seeAbstractLua.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
Loads a Java static method that accepts a singleLuaparameter and returns an integer- Parameters:
id- seeAbstractLua.getInstance(int)className- the class namemethodName- the method name- Returns:
- the number of elements pushed onto the stack
-
proxy
public static int proxy(int id) Creates a proxySee also the
java.proxyAPI andjavaProxyinjni/luajava/lualib.cpp.- Parameters:
id- the Lua state id- Returns:
- the number of return values pushed on stack (i.e., 1 if successful)
-
javaImport
Pushes onto the stack a jclass or a packageSee the
java.importAPI andjavaImportinjni/luajava/jualib.cpp.- Parameters:
id- the lua state idclassName- 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 idptr- 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
Obtains the value of a certain field of an objectThis 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 ofJuathread calling this methodobject- the objectname- the name of the field- Returns:
1if a field is found,2otherwise- See Also:
-
objectInvoke
Invokes a member method of an objectThis method is mainly used by the JNI side. See
jni/luajava/jua.cpp. Ifnameisnull, we assume that the object is aJFunction.For static fields, use
classInvoke(int, Class, String, int)instead.- Parameters:
index- the id ofJuathread calling this methodobj- the objectname- the name of the fieldparamCount- 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 indexobj- the objectname- the method namenotSignature- the method signature, comma separatedparamCount- the parameter count- Returns:
- the number of values pushed onto the stack
-
objectNewIndex
Sets a field of an object- Parameters:
index- the lua state indexobj- the objectname- the field name- Returns:
- always 0
-
classNew
Constructs an instance of a class- Parameters:
index- the lua state indexoClazz- the class (Objecttyped to manually handle mismatched types)paramCount- the parameter count- Returns:
- the number of values pushed onto the stack
-
classIndex
Pushes a static field of a class onto the stackUsed by
jobjectIndexinjni/luajava/luaapi.cpp- Parameters:
index- the lua state indexclazz- the classname- the field name- Returns:
- the number of values pushed onto the stack
-
classInvoke
Invokes a static method of a class- Parameters:
index- the lua state indexclazz- the classname- the method nameparamCount- 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 indexclazz- the classname- the method namenotSignature- the signature, comma separatedparamCount- the parameter count- Returns:
- the number of values pushed onto the stack
-
classNewIndex
Assigns to a static field- Parameters:
index- the lua state indexclazz- the classname- the field name- Returns:
- the number of values pushed onto the stack
-
arrayNew
Pushes a created array onto the stackUse negative
sizeto indicate that we should create a multi-dimensional array, with the dimensions read from the stack.- Parameters:
index- the lua state indexoClass- the classsize- the array size- Returns:
- the number of values pushed onto the stack
-
arrayIndex
Gets an element of an array- Parameters:
index- the lua state indexobj- the arrayi- the index (lua index, starting from 1)- Returns:
- the number of values pushed onto the stack
-
arrayNewIndex
Assigns to an element of an array- Parameters:
index- the lua state indexobj- the arrayi- the index (lua index, starting from 1)- Returns:
- the number of values pushed onto the stack
-
arrayLength
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
-paramCountto-1. For example, if you have two params, then the first is expected to be at -2, and the second be at -1. -
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 indexclazz- the classobj- the object,nullif calling a static methodname- the method namenotSignature- method signature, comma separatedparamCount- 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 statemethod- the methodobj- the objectobjects- the parameters- Returns:
- the number of values pushed onto the stack
-
fieldIndex
Tries to fetch field from an objectWhen a matching field is found, it is pushed to the corresponding lua stack
-
convertFromLua
public static @Nullable Object convertFromLua(Lua L, Class<?> clazz, int index) throws IllegalArgumentException Converts an element on the lua stack atindexto Java- Parameters:
L- the lua stateclazz- the expected return typeindex- a lua index (that is, starts from 1)- Returns:
- the converted element
- Throws:
IllegalArgumentException- when unable to convert
-
getClasses
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
-