sync Luaj improved code in 3.7

This commit is contained in:
u0u0
2020-04-16 14:49:33 +08:00
parent 815934d1b4
commit a6936cac6b
3 changed files with 1199 additions and 575 deletions

View File

@@ -1,5 +1,6 @@
/****************************************************************************
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2020 cocos2d-lua.org
http://www.cocos2d-x.org
@@ -28,7 +29,9 @@
#include <jni.h>
#include <string.h>
#include <string>
#include <map>
#include <vector>
#include "cocos2d.h"
using namespace std;
@@ -36,6 +39,7 @@ extern "C" {
#include "lua.h"
}
using namespace cocos2d;
#define LUAJ_ERR_OK 0
#define LUAJ_ERR_TYPE_NOT_SUPPORT (-1)
@@ -59,159 +63,183 @@ extern "C" {
*/
class LuaJavaBridge
{
public:
///@cond
/**
* Bind callStaticMethod of LuaJavaBridge to Lua.
* In current mechanism, we could call LuaJavaBridge.callStaticMethod(className, methodName, args) in Lua directly.
* Meanwhile the callStaticMethod of LuaObjcBridge binding function is wrapped in the luaj.lua
*/
static void luaopen_luaj(lua_State *L);
///@endcond
public:
///@cond
/**
* Bind callStaticMethod of LuaJavaBridge to Lua.
* In current mechanism, we could call LuaJavaBridge.callStaticMethod(className, methodName, args) in Lua directly.
* Meanwhile the callStaticMethod of LuaObjcBridge binding function is wrapped in the luaj.lua
*/
static void luaopen_luaj(lua_State *L);
///@endcond
/**
* Add a reference count for the Lua functionId,and save this reference in the Lua table named luaj_function_id_retain.
*
* @param functionId the id of Lua function.
* @return the reference count of the functionId if luaj_function_id_retain table exists and the corresponding value for functionId exists, otherwise return 0.
*
* @lua NA
* @js NA
*/
static int retainLuaFunctionById(int functionId);
/**
* Add a reference count for the Lua functionId,and save this reference in the Lua table named luaj_function_id_retain.
*
* @param functionId the id of Lua function.
* @return the reference count of the functionId if luaj_function_id_retain table exists and the corresponding value for functionId exists, otherwise return 0.
*
* @lua NA
* @js NA
*/
static int retainLuaFunctionById(int functionId);
/**
* Release a reference count for the Lua functionId, If the reference count is still greater than 0,save this reference in the Lua table named luaj_function_id_retain.
* Otherwise, remove the reference about this functionId in the luaj_function_id table and the luaj_function_id_retain table by set the corresponding value nil.
*
* @param functionId the id of Lua function.
* @return the reference count of the functionId if the luaj_function_id table, the luaj_function_id_retain table and the corresponding value for functionId exists a reference count for the Lua functionId is still greater than 0, and otherwise return 0.
*
* @lua NA
* @js NA
*/
static int releaseLuaFunctionById(int functionId);
/**
* Release a reference count for the Lua functionId, If the reference count is still greater than 0,save this reference in the Lua table named luaj_function_id_retain.
* Otherwise, remove the reference about this functionId in the luaj_function_id table and the luaj_function_id_retain table by set the corresponding value nil.
*
* @param functionId the id of Lua function.
* @return the reference count of the functionId if the luaj_function_id table, the luaj_function_id_retain table and the corresponding value for functionId exists a reference count for the Lua functionId is still greater than 0, and otherwise return 0.
*
* @lua NA
* @js NA
*/
static int releaseLuaFunctionById(int functionId);
/**
* Call the Lua function corresponding to the functionId with the string pointer arg.
*
* @param functionId the values corresponding to the Lua function.
* @param arg the string pointer point to the argument.
* @return a number value returned from the Lua function when call successfully, otherwise return -1 or the opposite number for one of the three numbers LUA_ERRRUN, LUA_ERRMEM and LUA_ERRERR.
*
* @lua NA
* @js NA
*/
static int callLuaFunctionById(int functionId, const char *arg);
/**
* Call the Lua function corresponding to the functionId with the string pointer arg.
*
* @param functionId the values corresponding to the Lua function.
* @param arg the string pointer point to the argument.
* @return a number value returned from the Lua function when call successfully, otherwise return -1 or the opposite number for one of the three numbers LUA_ERRRUN, LUA_ERRMEM and LUA_ERRERR.
*
* @lua NA
* @js NA
*/
static int callLuaFunctionById(int functionId, const char *arg);
/**
* Call a global Lua function named functionName with the string pointer arg.
*
* @param functionName the name of global function.
* @param arg the string pointer point to the argument.
* @return a number value returned from the Lua function when call successfully, otherwise return -1 or the opposite number for one of the three numbers LUA_ERRRUN, LUA_ERRMEM and LUA_ERRERR.
*
* @lua NA
* @js NA
*/
static int callLuaGlobalFunction(const char *functionName, const char *arg);
/**
* Call a global Lua function named functionName with the string pointer arg.
*
* @param functionName the name of global function.
* @param arg the string pointer point to the argument.
* @return a number value returned from the Lua function when call successfully, otherwise return -1 or the opposite number for one of the three numbers LUA_ERRRUN, LUA_ERRMEM and LUA_ERRERR.
*
* @lua NA
* @js NA
*/
static int callLuaGlobalFunction(const char *functionName, const char *arg);
private:
typedef enum
{
TypeInvalid = -1,
TypeVoid = 0,
TypeInteger = 1,
TypeFloat = 2,
TypeBoolean = 3,
TypeString = 4,
TypeVector = 5,
TypeFunction= 6,
} ValueType;
// added by guorui.chen
static jobject checkHashMap(lua_State *L);
static jobject checkArrayList(lua_State *L);
static jobject checkVector(lua_State *L);
static string checkObj(lua_State *L);
typedef vector<ValueType> ValueTypes;
private:
typedef enum
{
TypeInvalid = -1,
TypeVoid = 0,
TypeInteger = 1,
TypeFloat = 2,
TypeBoolean = 3,
TypeString = 4,
TypeVector = 5,
TypeFunction= 6,
TypeMap = 7,
TypeArrayList = 8,
} ValueType;
typedef union
{
int intValue;
float floatValue;
int boolValue;
string *stringValue;
} ReturnValue;
typedef vector<ValueType> ValueTypes;
class CallInfo
{
public:
CallInfo(const char *className, const char *methodName, const char *methodSig)
: m_valid(false)
, m_error(LUAJ_ERR_OK)
, m_className(className)
, m_methodName(methodName)
, m_methodSig(methodSig)
, m_returnType(TypeVoid)
, m_argumentsCount(0)
, m_retjs(NULL)
, m_env(NULL)
, m_classID(NULL)
, m_methodID(NULL)
{
memset(&m_ret, 0, sizeof(m_ret));
m_valid = validateMethodSig() && getMethodInfo();
}
~CallInfo();
typedef union
{
int intValue;
float floatValue;
int boolValue;
string *stringValue;
jobject objectValue;
} ReturnValue;
bool isValid() {
return m_valid;
}
class CallInfo
{
public:
CallInfo(const char *className, const char *methodName, const char *methodSig)
: m_valid(false)
, m_error(LUAJ_ERR_OK)
, m_className(className)
, m_methodName(methodName)
, m_methodSig(methodSig)
, m_returnType(TypeVoid)
, m_argumentsCount(0)
, m_retjs(NULL)
, m_env(NULL)
, m_classID(NULL)
, m_methodID(NULL)
{
memset(&m_ret, 0, sizeof(m_ret));
m_valid = validateMethodSig() && getMethodInfo();
}
~CallInfo();
int getErrorCode() {
return m_error;
}
bool isValid() {
return m_valid;
}
JNIEnv *getEnv() {
return m_env;
}
int getErrorCode() {
return m_error;
}
int argumentTypeAtIndex(size_t index) {
return m_argumentsType.at(index);
}
JNIEnv *getEnv() {
return m_env;
}
bool execute();
bool executeWithArgs(jvalue *args);
int pushReturnValue(lua_State *L);
int argumentTypeAtIndex(size_t index) {
return m_argumentsType.at(index);
}
bool execute();
bool executeWithArgs(jvalue *args);
int pushReturnValue(lua_State *L);
private:
bool m_valid;
int m_error;
private:
bool m_valid;
int m_error;
string m_className;
string m_methodName;
string m_methodSig;
int m_argumentsCount;
ValueTypes m_argumentsType;
ValueType m_returnType;
string m_className;
string m_methodName;
string m_methodSig;
int m_argumentsCount;
ValueTypes m_argumentsType;
ValueType m_returnType;
ReturnValue m_ret;
jstring m_retjs;
ReturnValue m_ret;
jstring m_retjs;
JNIEnv *m_env;
jclass m_classID;
jmethodID m_methodID;
JNIEnv *m_env;
jclass m_classID;
jmethodID m_methodID;
bool validateMethodSig();
bool getMethodInfo();
ValueType checkType(const string& sig, size_t *pos);
};
bool validateMethodSig();
bool getMethodInfo();
ValueType checkType(const string& sig, size_t *pos);
};
static int callJavaStaticMethod(lua_State *L);
static int retainLuaFunction(lua_State *L, int functionIndex, int *retainCountReturn);
static int getMethodInfo(CallInfo *call, const char *className, const char *methodName, const char *paramCode);
static int fetchArrayElements(lua_State *L, int index);
static int callAndPushReturnValue(lua_State *L, CallInfo *call, jvalue *args);
static int callJavaStaticMethod(lua_State *L);
static int retainLuaFunction(lua_State *L, int functionIndex, int *retainCountReturn);
static int getMethodInfo(CallInfo *call, const char *className, const char *methodName, const char *paramCode);
static int fetchArrayElements(lua_State *L, int index);
static int callAndPushReturnValue(lua_State *L, CallInfo *call, jvalue *args);
static lua_State *s_luaState;
static int s_newFunctionId;
static lua_State *s_luaState;
static int s_newFunctionId;
class PSJNIHelper {
public:
static void createHashMap();
static jobject getHashMap();
static void pushHashMapElement(string key, string value);
static void createVector();
static jobject getVector();
static void pushVectorElement(string value);
static void createArrayList();
static jobject getArrayList();
static void pushArrayListElement(string value);
};
};
// end group