Skip to content

Commit

Permalink
1、优化Function和Object的内存使用,提升执行效率。
Browse files Browse the repository at this point in the history
2、允许Object的实例对象可以动态添加属性或者方法。
  • Loading branch information
vimfung committed Nov 9, 2016
1 parent 2fed30e commit a28d3bc
Show file tree
Hide file tree
Showing 27 changed files with 663 additions and 353 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.util.Log;

import cn.vimfung.luascriptcore.LuaContext;
import cn.vimfung.luascriptcore.LuaFunction;
import cn.vimfung.luascriptcore.modules.oo.LuaObjectClass;

/**
Expand All @@ -23,9 +24,4 @@ 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 not shown.
Binary file modified Sample/Android/app/src/main/libs/LuaScriptCore.jar
Binary file not shown.
50 changes: 25 additions & 25 deletions Sample/iOS_OSX/Sample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +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 */; };
7C2144BC1DD322B600782ED6 /* libLuaScriptCore.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 7C2144BB1DD322B600782ED6 /* 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 @@ -36,15 +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>"; };
7C2144B31DD322B600782ED6 /* LSCContext.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LSCContext.h; sourceTree = "<group>"; };
7C2144B41DD322B600782ED6 /* LSCFunction.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LSCFunction.h; sourceTree = "<group>"; };
7C2144B51DD322B600782ED6 /* LSCModule.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LSCModule.h; sourceTree = "<group>"; };
7C2144B61DD322B600782ED6 /* LSCObjectClass.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LSCObjectClass.h; sourceTree = "<group>"; };
7C2144B71DD322B600782ED6 /* LSCPointer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LSCPointer.h; sourceTree = "<group>"; };
7C2144B81DD322B600782ED6 /* LSCTypeDefinied.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LSCTypeDefinied.h; sourceTree = "<group>"; };
7C2144B91DD322B600782ED6 /* LSCValue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LSCValue.h; sourceTree = "<group>"; };
7C2144BA1DD322B600782ED6 /* LuaScriptCore.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LuaScriptCore.h; sourceTree = "<group>"; };
7C2144BB1DD322B600782ED6 /* 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 Down Expand Up @@ -95,34 +95,34 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
7C2539271DD1857200071394 /* libLuaScriptCore.a in Frameworks */,
7C2144BC1DD322B600782ED6 /* libLuaScriptCore.a in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
7C25391C1DD1857200071394 /* iOS */ = {
7C2144B11DD322B600782ED6 /* LuaScriptCore */ = {
isa = PBXGroup;
children = (
7C25391D1DD1857200071394 /* include */,
7C2539261DD1857200071394 /* libLuaScriptCore.a */,
7C2144B21DD322B600782ED6 /* include */,
7C2144BB1DD322B600782ED6 /* libLuaScriptCore.a */,
);
name = iOS;
name = LuaScriptCore;
path = ../../../Release/iOS;
sourceTree = "<group>";
};
7C25391D1DD1857200071394 /* include */ = {
7C2144B21DD322B600782ED6 /* 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 */,
7C2144B31DD322B600782ED6 /* LSCContext.h */,
7C2144B41DD322B600782ED6 /* LSCFunction.h */,
7C2144B51DD322B600782ED6 /* LSCModule.h */,
7C2144B61DD322B600782ED6 /* LSCObjectClass.h */,
7C2144B71DD322B600782ED6 /* LSCPointer.h */,
7C2144B81DD322B600782ED6 /* LSCTypeDefinied.h */,
7C2144B91DD322B600782ED6 /* LSCValue.h */,
7C2144BA1DD322B600782ED6 /* LuaScriptCore.h */,
);
path = include;
sourceTree = "<group>";
Expand Down Expand Up @@ -154,7 +154,7 @@
7C6B12721D6AFD1400489D9E /* Sample-iOS */ = {
isa = PBXGroup;
children = (
7C25391C1DD1857200071394 /* iOS */,
7C2144B11DD322B600782ED6 /* LuaScriptCore */,
7C6B12731D6AFD1400489D9E /* AppDelegate.h */,
7C6B12741D6AFD1400489D9E /* AppDelegate.m */,
7C6B12751D6AFD1400489D9E /* Assets.xcassets */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public static void _register(LuaContext context, Class<? extends LuaModule> modu

filterMethodNames.add("access$super");
filterMethodNames.add("createInstance");
filterMethodNames.add("getContext");

//导出字段
Field[] fields = moduleClass.getDeclaredFields();
Expand Down Expand Up @@ -218,17 +217,6 @@ static private Method _getExportInstanceMethod(Class<? extends LuaObjectClass>
return null;
}

private LuaContext _context;

/**
* 获取Lua上下文对象
* @return 上下文对象
*/
public LuaContext getContext()
{
return _context;
}

/**
* 调用方法
* @param methodName 方法名称
Expand Down
5 changes: 5 additions & 0 deletions Source/Android/luascriptcore/src/main/jni/LuaContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,11 @@ bool cn::vimfung::luascriptcore::LuaContext::isModuleRegisted(const std::string
return !retValue;
}

cn::vimfung::luascriptcore::LuaModule* cn::vimfung::luascriptcore::LuaContext::getModule(const std::string &moduleName)
{
return _moduleMap[moduleName];
}

lua_State* cn::vimfung::luascriptcore::LuaContext::getLuaState()
{
return _state;
Expand Down
9 changes: 9 additions & 0 deletions Source/Android/luascriptcore/src/main/jni/LuaContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ namespace cn
*/
bool isModuleRegisted(const std::string &moduleName);

/**
* 根据模块名称获取模块对象
*
* @param moduleName 模块名称
*
* @return 模块对象,如果不存在则返回NULL
*/
LuaModule* getModule(const std::string &moduleName);

public:

/**
Expand Down
128 changes: 92 additions & 36 deletions Source/Android/luascriptcore/src/main/jni/LuaFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@
#include "LuaContext.h"
#include "LuaDefine.h"

static int FunctionIndex = 0;
/**
* 方法种子,主要参与方法索引的生成,每次创建一个Function,该值就会自增。
*/
static int FunctionSeed = 0;

/**
* 方法表名称
*/
static std::string FunctionsTableName = "_tmpFuncs_";

cn::vimfung::luascriptcore::LuaFunction::LuaFunction(LuaContext *context, int index)
{
Expand All @@ -21,24 +29,40 @@ cn::vimfung::luascriptcore::LuaFunction::LuaFunction(LuaContext *context, int in
index = lua_gettop(state) + index + 1;
}

if (lua_istable(state, lua_upvalueindex(0)))
lua_getglobal(state, "_G");
if (lua_istable(state, -1))
{
lua_getfield(state, -1, FunctionsTableName.c_str());
if (lua_isnil(state, -1))
{
lua_pop(state, 1);

//创建引用表
lua_newtable(state);

//放入全局变量_G中
lua_pushvalue(state, -1);
lua_setfield(state, -3, FunctionsTableName.c_str());
}

//根据时间戳创建索引
struct timeval tv;
gettimeofday(&tv,NULL);
long timestamp = tv.tv_sec * 1000 + tv.tv_usec / 1000 + FunctionIndex;
long timestamp = tv.tv_sec * 1000 + tv.tv_usec / 1000 + FunctionSeed;
char buf[40];
sprintf(buf, "func%ld", timestamp);
_index = buf;
FunctionIndex ++;

lua_pushvalue(state, lua_upvalueindex(0));
FunctionSeed ++;

lua_pushvalue(state, index);
lua_pushvalue(state, (int)index);
lua_setfield(state, -2, _index.c_str());

//弹出_tmpFuncs_
lua_pop(state, 1);
}

//弹出_G
lua_pop(state, 1);
}

cn::vimfung::luascriptcore::LuaFunction::~LuaFunction()
Expand All @@ -47,27 +71,47 @@ cn::vimfung::luascriptcore::LuaFunction::~LuaFunction()
{
//移除索引中的方法
lua_State *state = _context -> getLuaState();
if (lua_istable(state, lua_upvalueindex(0)))

lua_getglobal(state, "_G");
if (lua_istable(state, -1))
{
lua_pushvalue(state, lua_upvalueindex(0));
lua_pushnil(state);
lua_setfield(state, -2, _index.c_str());
lua_getfield(state, -1, FunctionsTableName.c_str());
if (lua_istable(state, -1))
{
lua_pushnil(state);
lua_setfield(state, -2, _index.c_str());
}
lua_pop(state, 1);

}
lua_pop(state, 1);
}
}

void cn::vimfung::luascriptcore::LuaFunction::push()
{
lua_State *state = _context -> getLuaState();
if (lua_istable(state, lua_upvalueindex(0)))

lua_getglobal(state, "_G");
if (lua_istable(state, -1))
{
lua_getfield(state, lua_upvalueindex(0), _index.c_str());
lua_getfield(state, -1, FunctionsTableName.c_str());
if (lua_istable(state, -1))
{
lua_getfield(state, -1, _index.c_str());
}
else
{
lua_pushnil(state);
}
lua_remove(state, -2);

}
else
{
lua_pushnil(state);
}
lua_remove(state, -2);
}

cn::vimfung::luascriptcore::LuaValue* cn::vimfung::luascriptcore::LuaFunction::invoke(
Expand All @@ -77,39 +121,51 @@ cn::vimfung::luascriptcore::LuaValue* cn::vimfung::luascriptcore::LuaFunction::i

LuaValue *retValue = NULL;

if (lua_istable(state, lua_upvalueindex(0)))
lua_getglobal(state, "_G");
if (lua_istable(state, -1))
{
lua_pushvalue(state, lua_upvalueindex(0));
lua_getfield(state, -1, _index.c_str());

if (lua_isfunction(state, -1))
lua_getfield(state, -1, FunctionsTableName.c_str());
if (lua_istable(state, -1))
{
//初始化传递参数
for (LuaArgumentList::iterator i = arguments -> begin(); i != arguments -> end() ; ++i)
lua_getfield(state, -1, _index.c_str());
if (lua_isfunction(state, -1))
{
LuaValue *item = *i;
item->push(_context);
//初始化传递参数
for (LuaArgumentList::iterator i = arguments -> begin(); i != arguments -> end() ; ++i)
{
LuaValue *item = *i;
item->push(_context);
}

if (lua_pcall(state, (int)arguments -> size(), 1, 0) == 0)
{
//调用成功
retValue = _context -> getValueByIndex(-1);
}
else
{
//调用失败
LuaValue *value = _context -> getValueByIndex(-1);
std::string errMessage = value -> toString();
_context -> raiseException(errMessage);

value -> release();
}
}

if (lua_pcall(state, (int)arguments -> size(), 1, 0) == 0)
{
//调用成功
retValue = _context -> getValueByIndex(-1);
}
else
{
//调用失败
LuaValue *value = _context -> getValueByIndex(-1);
std::string errMessage = value -> toString();
_context -> raiseException(errMessage);
//弹出返回值
lua_pop(state, 1);

value -> release();
}
}

lua_pop(state, 2);
//弹出_tmpFuncs_
lua_pop(state, 1);

}

//弹出_G
lua_pop(state, 1);

if (!retValue)
{
retValue = new LuaValue();
Expand Down
7 changes: 7 additions & 0 deletions Source/Android/luascriptcore/src/main/jni/LuaJavaEnv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,10 @@ std::string LuaJavaEnv::getJavaClassNameByInstance(JNIEnv *env, jobject instance

return className;
}

LuaJavaObjectClass* LuaJavaEnv::getObjectClassByInstance(JNIEnv *env, jobject instance, LuaContext *context)
{
std::string className = getJavaClassNameByInstance(env, instance);
LuaModule *module = context -> getModule((const std::string)className);
return dynamic_cast<LuaJavaObjectClass *>(module);
}
Loading

0 comments on commit a28d3bc

Please sign in to comment.