Skip to content

Commit

Permalink
Android平台LuaValue增加setObject方法用于为table进行赋值。
Browse files Browse the repository at this point in the history
  • Loading branch information
vimfung committed Mar 22, 2019
1 parent 6bb6be3 commit 306e205
Show file tree
Hide file tree
Showing 17 changed files with 601 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,14 @@ public static native void runThread(LuaContext context,
*/
public static native void scriptControllerForceExit(LuaScriptController controller);

/**
* LuaValue设置对应键名指向对象,仅用于ValueType为Map
* @param context 上下文对象
* @param value 值对象
* @param keyPath 键名路径
* @param object 对象
* @return 返回更新后对象
*/
public static native Object luaValueSetObject(LuaContext context, LuaValue value, String keyPath, LuaValue object);

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package cn.vimfung.luascriptcore;

import android.util.Log;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -16,6 +13,20 @@ public class LuaValue extends LuaBaseObject
{
private Object _valueContainer;
private LuaValueType _type;
private LuaContext _context; //从JNI层回来的对象会带上这个字段
private int _tableId; //Map和Array类型下的Table标识

@Override
protected void finalize() throws Throwable
{
if (_tableId > 0)
{
//释放table对象
LuaNativeUtil.releaseNativeObject(_tableId);
}

super.finalize();
}

/**
* 初始化一个空值的LuaValue对象
Expand Down Expand Up @@ -733,4 +744,17 @@ public Object toObject()
{
return _valueContainer;
}

/**
* 设置对象对象
* @param keyPath 键名路径,如:key1.key2.key3
* @param object 对象
*/
public void setObject(String keyPath, Object object)
{
if (valueType() == LuaValueType.Map)
{
_valueContainer = LuaNativeUtil.luaValueSetObject(_context, this, keyPath, new LuaValue(object));
}
}
}
1 change: 1 addition & 0 deletions Source/Android/luascriptcore/src/main/jni/Android-5.1.5.mk
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ LOCAL_SRC_FILES := \
../../../../../lua-common/LuaCoroutine.cpp \
../../../../../lua-common/LuaError.cpp \
../../../../../lua-common/LuaScriptController.cpp \
../../../../../lua-common/LuaTable.cpp \

LOCAL_C_INCLUDES += $(LOCAL_PATH)
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../../../../lua-core-5.1.5/src
Expand Down
1 change: 1 addition & 0 deletions Source/Android/luascriptcore/src/main/jni/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ LOCAL_SRC_FILES := \
../../../../../lua-common/LuaCoroutine.cpp \
../../../../../lua-common/LuaError.cpp \
../../../../../lua-common/LuaScriptController.cpp \
../../../../../lua-common/LuaTable.cpp \

LOCAL_C_INCLUDES += $(LOCAL_PATH)
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../../../../lua-core/src
Expand Down
3 changes: 2 additions & 1 deletion Source/Android/luascriptcore/src/main/jni/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ add_library( # Sets the name of the library.
../../../../../lua-common/LuaExportTypeDescriptor.cpp
../../../../../lua-common/LuaExportPropertyDescriptor.cpp
../../../../../lua-common/LuaError.cpp
../../../../../lua-common/LuaScriptController.cpp)
../../../../../lua-common/LuaScriptController.cpp
../../../../../lua-common/LuaTable.cpp)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because system libraries are included in the search path by
Expand Down
123 changes: 83 additions & 40 deletions Source/Android/luascriptcore/src/main/jni/LuaJavaConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "LuaContext.h"
#include "LuaValue.h"
#include "LuaPointer.h"
#include "LuaTable.hpp"

LuaContext* LuaJavaConverter::convertToContextByJLuaContext(JNIEnv *env, jobject context)
{
Expand Down Expand Up @@ -482,6 +483,8 @@ LuaValue* LuaJavaConverter::convertToLuaValueByJLuaValue(JNIEnv *env, LuaContext
static jmethodID toObjectId = env -> GetMethodID(jLuaValueClass, "toObject", "()Ljava/lang/Object;");
static jmethodID toTupleId = env -> GetMethodID(jLuaValueClass, "toTuple", "()Lcn/vimfung/luascriptcore/LuaTuple;");

static jfieldID tableIdFieldId = env -> GetFieldID(jLuaValueClass, "_tableId", "I");

jobject itemType = env -> CallObjectMethod(value, typeMethodId);
jint valueType = env -> CallIntMethod(itemType, typeValueMethodId);
env -> DeleteLocalRef(itemType);
Expand Down Expand Up @@ -528,62 +531,83 @@ LuaValue* LuaJavaConverter::convertToLuaValueByJLuaValue(JNIEnv *env, LuaContext
}
case LuaValueTypeArray:
{
static jclass jListClass = LuaJavaType::listClass(env);
static jmethodID getMethodId = env -> GetMethodID(jListClass, "get", "(I)Ljava/lang/Object;");
static jmethodID sizeMethodId = env -> GetMethodID(jListClass, "size", "()I");

LuaValueList list;
jobject arrayList = env -> CallObjectMethod(value, toListMethodId);
jint len = env -> CallIntMethod(arrayList, sizeMethodId);
for (int i = 0; i < len; ++i)
//先检查是否存在tableId
int tableId = env -> GetIntField(value, tableIdFieldId);
if (tableId > 0)
{
jobject item = env -> CallObjectMethod(arrayList, getMethodId, i);
LuaValue *valueItem = LuaJavaConverter::convertToLuaValueByJObject(env, context, item);
list.push_back(valueItem);
env -> DeleteLocalRef(item);
//直接获取LuaTable对象
LuaTable *table = (LuaTable *)LuaObjectManager::SharedInstance() -> getObject(tableId);
retValue = LuaValue::TableValue(table);
}
else
{
static jclass jListClass = LuaJavaType::listClass(env);
static jmethodID getMethodId = env -> GetMethodID(jListClass, "get", "(I)Ljava/lang/Object;");
static jmethodID sizeMethodId = env -> GetMethodID(jListClass, "size", "()I");

LuaValueList list;
jobject arrayList = env -> CallObjectMethod(value, toListMethodId);
jint len = env -> CallIntMethod(arrayList, sizeMethodId);
for (int i = 0; i < len; ++i)
{
jobject item = env -> CallObjectMethod(arrayList, getMethodId, i);
LuaValue *valueItem = LuaJavaConverter::convertToLuaValueByJObject(env, context, item);
list.push_back(valueItem);
env -> DeleteLocalRef(item);
}

retValue = LuaValue::ArrayValue(list);
retValue = LuaValue::ArrayValue(list);

env -> DeleteLocalRef(arrayList);
env -> DeleteLocalRef(arrayList);
}
break;
}
case LuaValueTypeMap:
{
static jclass jHashMapClass = LuaJavaType::mapClass(env);
static jmethodID getMethodId = env -> GetMethodID(jHashMapClass, "get", "(Ljava/lang/Object;)Ljava/lang/Object;");
static jmethodID sizeMethodId = env -> GetMethodID(jHashMapClass, "size", "()I");
static jmethodID keySetMethodId = env -> GetMethodID(jHashMapClass, "keySet", "()Ljava/util/Set;");
int tableId = env -> GetIntField(value, tableIdFieldId);
if (tableId > 0)
{
//直接获取LuaTable对象
LuaTable *table = (LuaTable *)LuaObjectManager::SharedInstance() -> getObject(tableId);
retValue = LuaValue::TableValue(table);
}
else
{
static jclass jHashMapClass = LuaJavaType::mapClass(env);
static jmethodID getMethodId = env -> GetMethodID(jHashMapClass, "get", "(Ljava/lang/Object;)Ljava/lang/Object;");
static jmethodID sizeMethodId = env -> GetMethodID(jHashMapClass, "size", "()I");
static jmethodID keySetMethodId = env -> GetMethodID(jHashMapClass, "keySet", "()Ljava/util/Set;");

static jclass jSetClass = (jclass)env -> NewGlobalRef(LuaJavaEnv::findClass(env, "java/util/Set"));
static jmethodID toArrayMethodId = env -> GetMethodID(jSetClass, "toArray", "()[Ljava/lang/Object;");
static jclass jSetClass = (jclass)env -> NewGlobalRef(LuaJavaEnv::findClass(env, "java/util/Set"));
static jmethodID toArrayMethodId = env -> GetMethodID(jSetClass, "toArray", "()[Ljava/lang/Object;");

LuaValueMap map;
jobject hashMap = env -> CallObjectMethod(value, toMapMethodId);
jint len = env -> CallIntMethod(hashMap, sizeMethodId);
LuaValueMap map;
jobject hashMap = env -> CallObjectMethod(value, toMapMethodId);
jint len = env -> CallIntMethod(hashMap, sizeMethodId);

jobject keySet= env -> CallObjectMethod(hashMap, keySetMethodId);
jobjectArray keys = (jobjectArray)env -> CallObjectMethod(keySet, toArrayMethodId);
jobject keySet= env -> CallObjectMethod(hashMap, keySetMethodId);
jobjectArray keys = (jobjectArray)env -> CallObjectMethod(keySet, toArrayMethodId);

for (int i = 0; i < len; ++i)
{
jobject key = env -> GetObjectArrayElement(keys, i);
jobject item = env -> CallObjectMethod(hashMap, getMethodId, key);
for (int i = 0; i < len; ++i)
{
jobject key = env -> GetObjectArrayElement(keys, i);
jobject item = env -> CallObjectMethod(hashMap, getMethodId, key);

const char *keyStr = env -> GetStringUTFChars((jstring)key, NULL);
LuaValue *valueItem = LuaJavaConverter::convertToLuaValueByJObject(env, context, item);
map[keyStr] = valueItem;
env -> ReleaseStringUTFChars((jstring)key, keyStr);
const char *keyStr = env -> GetStringUTFChars((jstring)key, NULL);
LuaValue *valueItem = LuaJavaConverter::convertToLuaValueByJObject(env, context, item);
map[keyStr] = valueItem;
env -> ReleaseStringUTFChars((jstring)key, keyStr);

env -> DeleteLocalRef(item);
env -> DeleteLocalRef(key);
}
env -> DeleteLocalRef(item);
env -> DeleteLocalRef(key);
}

retValue = LuaValue::DictonaryValue(map);
retValue = LuaValue::DictonaryValue(map);

env -> DeleteLocalRef(keys);
env -> DeleteLocalRef(keySet);
env -> DeleteLocalRef(hashMap);
env -> DeleteLocalRef(keys);
env -> DeleteLocalRef(keySet);
env -> DeleteLocalRef(hashMap);
}

break;
}
Expand Down Expand Up @@ -871,8 +895,11 @@ jobject LuaJavaConverter::convertToJavaLuaValueByLuaValue(JNIEnv *env, LuaContex
{
static jclass jLuaValue = LuaJavaType::luaValueClass(env);
static jmethodID jNilInitMethodId = env -> GetMethodID(jLuaValue, "<init>", "(I)V");
static jfieldID jContextFieldId = env -> GetFieldID(jLuaValue, "_context", "Lcn/vimfung/luascriptcore/LuaContext;");
jmethodID initMethodId = jNilInitMethodId;

int tableId = 0;

switch (luaValue->getType())
{
case LuaValueTypeNumber:
Expand Down Expand Up @@ -903,12 +930,18 @@ jobject LuaJavaConverter::convertToJavaLuaValueByLuaValue(JNIEnv *env, LuaContex
{
static jmethodID arrayInitMethodId = env -> GetMethodID(jLuaValue, "<init>", "(ILjava/util/List;)V");
initMethodId = arrayInitMethodId;

LuaTable *table = luaValue -> toTable();
tableId = LuaObjectManager::SharedInstance() -> putObject(table);
break;
}
case LuaValueTypeMap:
{
static jmethodID mapInitMethodId = env -> GetMethodID(jLuaValue, "<init>", "(ILjava/util/Map;)V");
initMethodId = mapInitMethodId;

LuaTable *table = luaValue -> toTable();
tableId = LuaObjectManager::SharedInstance() -> putObject(table);
break;
}
case LuaValueTypePtr:
Expand Down Expand Up @@ -948,7 +981,17 @@ jobject LuaJavaConverter::convertToJavaLuaValueByLuaValue(JNIEnv *env, LuaContex
jobject jObj = LuaJavaConverter::convertToJavaObjectByLuaValue(env, context, luaValue);
retObj = env -> NewObject(jLuaValue, initMethodId, luaValue -> objectId(), jObj);
env -> DeleteLocalRef(jObj);

if (tableId > 0)
{
//设置TableID到luaValue中
static jfieldID tableIdFieldID = env -> GetFieldID(jLuaValue, "_tableId", "I");
env -> SetIntField(retObj, tableIdFieldID, tableId);
}
}

jobject jContext = LuaJavaEnv::getJavaLuaContext(env, context);
env -> SetObjectField(retObj, jContextFieldId, jContext);
}

return retObj;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -528,4 +528,47 @@ JNIEXPORT void JNICALL Java_cn_vimfung_luascriptcore_LuaNativeUtil_scriptControl
{
scriptController -> forceExit();
}
}

JNIEXPORT jobject JNICALL Java_cn_vimfung_luascriptcore_LuaNativeUtil_luaValueSetObject(
JNIEnv *env,
jclass type,
jobject jcontext,
jobject jvalue,
jstring keyPath,
jobject jObject)
{

jobject retObject = NULL;
const char *keyPathCStr = env -> GetStringUTFChars(keyPath, 0);

LuaContext *context = LuaJavaConverter::convertToContextByJLuaContext(env, jcontext);
if (context != NULL)
{
LuaValue *value = LuaJavaConverter::convertToLuaValueByJLuaValue(env, context, jvalue);
LuaValue *object = NULL;
if (jObject != NULL)
{
object = LuaJavaConverter::convertToLuaValueByJLuaValue(env, context, jObject);
}

if (value != NULL)
{
value -> setObject(keyPathCStr, object);
retObject = LuaJavaConverter::convertToJavaObjectByLuaValue(env, context, value);

value -> release();
}

if (object != NULL)
{
object -> release();
}
}


env -> ReleaseStringUTFChars(keyPath, keyPathCStr);
env -> DeleteLocalRef(keyPath);

return retObject;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 306e205

Please sign in to comment.