Skip to content

Commit

Permalink
1、调整映射对象的内存管理,优化内存的使用。
Browse files Browse the repository at this point in the history
2、移除ObjectClass的createInstance方法,新版本中可以直接使用初始化方法来构造对象。
  • Loading branch information
vimfung committed Nov 8, 2016
1 parent 0e34275 commit 2fed30e
Show file tree
Hide file tree
Showing 34 changed files with 677 additions and 329 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ public void walk()
{
Log.v("luascriptcore", String.format("%s walk", name));
}

public static Person test()
{
return new Person();
}

}
Binary file not shown.
Binary file not shown.
Binary file modified Sample/Android/app/src/main/jniLibs/armeabi/libLuaScriptCore.so
Binary file not shown.
Binary file modified Sample/Android/app/src/main/jniLibs/mips/libLuaScriptCore.so
Binary file not shown.
Binary file modified Sample/Android/app/src/main/jniLibs/mips64/libLuaScriptCore.so
Binary file not shown.
Binary file modified Sample/Android/app/src/main/jniLibs/x86/libLuaScriptCore.so
Binary file not shown.
Binary file modified Sample/Android/app/src/main/jniLibs/x86_64/libLuaScriptCore.so
Binary file not shown.
Binary file modified Sample/Android/app/src/main/libs/LuaScriptCore.jar
Binary file not shown.
74 changes: 37 additions & 37 deletions Sample/iOS_OSX/Sample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
7C210E041DCB5FD3000838F9 /* test.lua in Resources */ = {isa = PBXBuildFile; fileRef = 7C210E031DCB5FD3000838F9 /* test.lua */; };
7C210E051DCB5FD3000838F9 /* test.lua in Resources */ = {isa = PBXBuildFile; fileRef = 7C210E031DCB5FD3000838F9 /* test.lua */; };
7C2539271DD1857200071394 /* libLuaScriptCore.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 7C2539261DD1857200071394 /* libLuaScriptCore.a */; };
7C5A61811D6AE2A3007D4308 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 7C5A61801D6AE2A3007D4308 /* AppDelegate.m */; };
7C5A61841D6AE2A3007D4308 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 7C5A61831D6AE2A3007D4308 /* main.m */; };
7C5A61871D6AE2A3007D4308 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 7C5A61861D6AE2A3007D4308 /* ViewController.m */; };
Expand All @@ -20,7 +21,6 @@
7C6B12811D6AFD1400489D9E /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 7C6B12781D6AFD1400489D9E /* Main.storyboard */; };
7C6B12831D6AFD1400489D9E /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 7C6B127B1D6AFD1400489D9E /* main.m */; };
7C6B12841D6AFD1400489D9E /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 7C6B127D1D6AFD1400489D9E /* ViewController.m */; };
7C75B1601DCC63D100112E35 /* libLuaScriptCore.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 7C75B15F1DCC63D100112E35 /* libLuaScriptCore.a */; };
7C96366F1DC9BF8F00E423CF /* libLuaScriptCore.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 7C96366E1DC9BF8F00E423CF /* libLuaScriptCore.a */; };
7CE625121D93B3AA007533D7 /* dkjson.lua in Resources */ = {isa = PBXBuildFile; fileRef = 7CE6250F1D93B3AA007533D7 /* dkjson.lua */; };
7CE625131D93B3AA007533D7 /* dkjson.lua in Resources */ = {isa = PBXBuildFile; fileRef = 7CE6250F1D93B3AA007533D7 /* dkjson.lua */; };
Expand All @@ -36,6 +36,15 @@

/* Begin PBXFileReference section */
7C210E031DCB5FD3000838F9 /* test.lua */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = test.lua; sourceTree = "<group>"; };
7C25391E1DD1857200071394 /* LSCContext.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LSCContext.h; sourceTree = "<group>"; };
7C25391F1DD1857200071394 /* LSCFunction.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LSCFunction.h; sourceTree = "<group>"; };
7C2539201DD1857200071394 /* LSCModule.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LSCModule.h; sourceTree = "<group>"; };
7C2539211DD1857200071394 /* LSCObjectClass.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LSCObjectClass.h; sourceTree = "<group>"; };
7C2539221DD1857200071394 /* LSCPointer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LSCPointer.h; sourceTree = "<group>"; };
7C2539231DD1857200071394 /* LSCTypeDefinied.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LSCTypeDefinied.h; sourceTree = "<group>"; };
7C2539241DD1857200071394 /* LSCValue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LSCValue.h; sourceTree = "<group>"; };
7C2539251DD1857200071394 /* LuaScriptCore.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LuaScriptCore.h; sourceTree = "<group>"; };
7C2539261DD1857200071394 /* libLuaScriptCore.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libLuaScriptCore.a; sourceTree = "<group>"; };
7C5A617D1D6AE2A3007D4308 /* Sample-OSX.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Sample-OSX.app"; sourceTree = BUILT_PRODUCTS_DIR; };
7C5A617F1D6AE2A3007D4308 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
7C5A61801D6AE2A3007D4308 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
Expand All @@ -54,15 +63,6 @@
7C6B127B1D6AFD1400489D9E /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
7C6B127C1D6AFD1400489D9E /* ViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = "<group>"; };
7C6B127D1D6AFD1400489D9E /* ViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = "<group>"; };
7C75B1571DCC63D100112E35 /* LSCContext.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LSCContext.h; sourceTree = "<group>"; };
7C75B1581DCC63D100112E35 /* LSCFunction.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LSCFunction.h; sourceTree = "<group>"; };
7C75B1591DCC63D100112E35 /* LSCModule.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LSCModule.h; sourceTree = "<group>"; };
7C75B15A1DCC63D100112E35 /* LSCObjectClass.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LSCObjectClass.h; sourceTree = "<group>"; };
7C75B15B1DCC63D100112E35 /* LSCPointer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LSCPointer.h; sourceTree = "<group>"; };
7C75B15C1DCC63D100112E35 /* LSCTypeDefinied.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LSCTypeDefinied.h; sourceTree = "<group>"; };
7C75B15D1DCC63D100112E35 /* LSCValue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LSCValue.h; sourceTree = "<group>"; };
7C75B15E1DCC63D100112E35 /* LuaScriptCore.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LuaScriptCore.h; sourceTree = "<group>"; };
7C75B15F1DCC63D100112E35 /* libLuaScriptCore.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libLuaScriptCore.a; sourceTree = "<group>"; };
7C8C2DE01D38C345009345EB /* Sample-iOS.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Sample-iOS.app"; sourceTree = BUILT_PRODUCTS_DIR; };
7C9636661DC9BF8F00E423CF /* LSCContext.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LSCContext.h; sourceTree = "<group>"; };
7C9636671DC9BF8F00E423CF /* LSCFunction.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LSCFunction.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -95,13 +95,38 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
7C75B1601DCC63D100112E35 /* libLuaScriptCore.a in Frameworks */,
7C2539271DD1857200071394 /* libLuaScriptCore.a in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
7C25391C1DD1857200071394 /* iOS */ = {
isa = PBXGroup;
children = (
7C25391D1DD1857200071394 /* include */,
7C2539261DD1857200071394 /* libLuaScriptCore.a */,
);
name = iOS;
path = ../../../Release/iOS;
sourceTree = "<group>";
};
7C25391D1DD1857200071394 /* include */ = {
isa = PBXGroup;
children = (
7C25391E1DD1857200071394 /* LSCContext.h */,
7C25391F1DD1857200071394 /* LSCFunction.h */,
7C2539201DD1857200071394 /* LSCModule.h */,
7C2539211DD1857200071394 /* LSCObjectClass.h */,
7C2539221DD1857200071394 /* LSCPointer.h */,
7C2539231DD1857200071394 /* LSCTypeDefinied.h */,
7C2539241DD1857200071394 /* LSCValue.h */,
7C2539251DD1857200071394 /* LuaScriptCore.h */,
);
path = include;
sourceTree = "<group>";
};
7C5A617E1D6AE2A3007D4308 /* Sample-OSX */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -129,7 +154,7 @@
7C6B12721D6AFD1400489D9E /* Sample-iOS */ = {
isa = PBXGroup;
children = (
7C75B1551DCC63D100112E35 /* LuaScriptCore */,
7C25391C1DD1857200071394 /* iOS */,
7C6B12731D6AFD1400489D9E /* AppDelegate.h */,
7C6B12741D6AFD1400489D9E /* AppDelegate.m */,
7C6B12751D6AFD1400489D9E /* Assets.xcassets */,
Expand All @@ -143,31 +168,6 @@
path = "Sample-iOS";
sourceTree = "<group>";
};
7C75B1551DCC63D100112E35 /* LuaScriptCore */ = {
isa = PBXGroup;
children = (
7C75B1561DCC63D100112E35 /* include */,
7C75B15F1DCC63D100112E35 /* libLuaScriptCore.a */,
);
name = LuaScriptCore;
path = ../../../Release/iOS;
sourceTree = "<group>";
};
7C75B1561DCC63D100112E35 /* include */ = {
isa = PBXGroup;
children = (
7C75B1571DCC63D100112E35 /* LSCContext.h */,
7C75B1581DCC63D100112E35 /* LSCFunction.h */,
7C75B1591DCC63D100112E35 /* LSCModule.h */,
7C75B15A1DCC63D100112E35 /* LSCObjectClass.h */,
7C75B15B1DCC63D100112E35 /* LSCPointer.h */,
7C75B15C1DCC63D100112E35 /* LSCTypeDefinied.h */,
7C75B15D1DCC63D100112E35 /* LSCValue.h */,
7C75B15E1DCC63D100112E35 /* LuaScriptCore.h */,
);
path = include;
sourceTree = "<group>";
};
7C8C2DD71D38C345009345EB = {
isa = PBXGroup;
children = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,6 @@ public static String moduleName ()
return "Object";
}

/**
* 创建类实例对象
* @param context Lua的上下文对象
* @return 实例对象
*/
public static <T> T createInstance(LuaContext context, Class<T> objectClass)
{
if (LuaObjectClass.class.isAssignableFrom(objectClass))
{
LuaValue instance = context.evalScript(String.format("return %s.create();", _getModuleName((Class<? extends LuaModule>) objectClass)));
return (T) instance.toObject();
}

return null;
}

/**
* 注册模块
* @param context Lua上下文对象
Expand Down
8 changes: 5 additions & 3 deletions Source/Android/luascriptcore/src/main/jni/LuaContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "LuaModule.h"
#include "LuaDefine.h"
#include "../../../../../lua-core/src/lua.hpp"
#include "LuaPointer.h"
#include <map>
#include <list>
#include <iostream>
Expand Down Expand Up @@ -200,16 +201,17 @@ cn::vimfung::luascriptcore::LuaValue* cn::vimfung::luascriptcore::LuaContext::ge
}
case LUA_TLIGHTUSERDATA:
{
LuaPointer *pointer = new LuaPointer(lua_topointer(_state, index));
LuaUserdataRef ref = (LuaUserdataRef)lua_topointer(_state, index);
LuaPointer *pointer = new LuaPointer(ref);
value = LuaValue::PointerValue(pointer);
pointer -> release();

break;
}
case LUA_TUSERDATA:
{
LuaObjectDescriptor **ref = (LuaObjectDescriptor **)lua_touserdata(_state, index);
value = LuaValue::ObjectValue(*ref);
LuaUserdataRef ref = (LuaUserdataRef)lua_touserdata(_state, index);
value = LuaValue::ObjectValue((LuaObjectDescriptor *)ref -> value);
break;
}
case LUA_TFUNCTION:
Expand Down
49 changes: 47 additions & 2 deletions Source/Android/luascriptcore/src/main/jni/LuaContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,42 @@ namespace cn
namespace luascriptcore
{
/**
* Lua上下文对象
* Lua上下文环境, 维护原生代码与Lua之间交互的核心类型。
*/
class LuaContext : public LuaObject
{
private:

/**
* Lua状态机
*/
lua_State *_state;

/**
* Lua运行异常处理器
*/
LuaExceptionHandler _exceptionHandler;

/**
* 方法映射表
*/
LuaMethodMap _methodMap;

/**
* 模块映射表
*/
LuaModuleMap _moduleMap;

public:

/**
* 初始化上下文对象
*/
LuaContext();

/**
* 销毁上下文对象
*/
~LuaContext();

public:
Expand Down Expand Up @@ -105,9 +129,30 @@ namespace cn
bool isModuleRegisted(const std::string &moduleName);

public:
//获取方法处理器,方法名称

/**
* 根据方法名称获取对应的方法处理器
*
* @param methodName 方法名称
*
* @return 方法处理器
*/
LuaMethodHandler getMethodHandler(std::string methodName);

/**
* 获取数据栈中对应索引的值
*
* @param index 数据栈索引
*
* @return 值对象
*/
LuaValue* getValueByIndex(int index);

/**
* 获取Lua状态机
*
* @return Lua状态机
*/
lua_State* getLuaState();

};
Expand Down
22 changes: 21 additions & 1 deletion Source/Android/luascriptcore/src/main/jni/LuaFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,39 @@ namespace cn {
class LuaValue;

/**
* 方法对象
* 方法对象, 表示了一个对应在Lua中的function,在lua中传入一个function到本地方法,将会自动转换为此类型的实例对象。
*/
class LuaFunction : public LuaObject
{
private:

/**
* 方法索引
*/
std::string _index;

/**
* Lua上下文环境
*/
LuaContext *_context;

public:

/**
* 初始化方法对象
*
* @param context 上下文环境
* @param index 数据栈索引
*/
LuaFunction (LuaContext *context, int index);

/**
* 销毁方法对象
*/
~LuaFunction();

public:

/**
* 入栈数据
*/
Expand Down
34 changes: 30 additions & 4 deletions Source/Android/luascriptcore/src/main/jni/LuaJavaConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "LuaObjectManager.h"
#include "LuaJavaObjectDescriptor.h"
#include "LuaJavaEnv.h"
#include "LuaDefine.h"

LuaContext* LuaJavaConverter::convertToContextByJLuaContext(JNIEnv *env, jobject context)
{
Expand Down Expand Up @@ -117,9 +118,22 @@ LuaValue* LuaJavaConverter::convertToLuaValueByJObject(JNIEnv *env, jobject obje
else if (env -> IsSameObject(object, NULL) != JNI_TRUE)
{
//对象类型
LuaJavaObjectDescriptor *objDesc = new LuaJavaObjectDescriptor(object);
//查找对象是否存在ObjectDescriptor
bool needRelease = false;
LuaObjectDescriptor *objDesc = LuaJavaEnv::getAssociateInstanceRef(env, object);
if (objDesc == NULL)
{
//不存在则创建对象
objDesc = new LuaJavaObjectDescriptor(env, object);
needRelease = true;
}

value = new LuaValue(objDesc);
objDesc -> release();

if (needRelease)
{
objDesc->release();
}
}

return value;
Expand Down Expand Up @@ -276,9 +290,21 @@ LuaValue* LuaJavaConverter::convertToLuaValueByJLuaValue(JNIEnv *env, jobject va
jobject obj = env -> CallObjectMethod(value, toObjectId);
if (env -> IsSameObject(obj, NULL) != JNI_TRUE)
{
LuaJavaObjectDescriptor *objDesc = new LuaJavaObjectDescriptor(obj);
bool needRelease = false;
LuaObjectDescriptor *objDesc = LuaJavaEnv::getAssociateInstanceRef(env, obj);
if (objDesc == NULL)
{
//不存在则创建对象
objDesc = new LuaJavaObjectDescriptor(env, obj);
needRelease = true;
}

retValue = new LuaValue(objDesc);
objDesc -> release();

if (needRelease)
{
objDesc->release();
}
}
break;
}
Expand Down
Loading

0 comments on commit 2fed30e

Please sign in to comment.