Skip to content

Commit

Permalink
1、优化Android的模块与面向对象模块功能,让其行为与在iOS上一致。
Browse files Browse the repository at this point in the history
2、修复iOS上LuaValue如果为Object类型时,可能丢失Object引用问题而产生崩溃。
  • Loading branch information
vimfung committed Nov 1, 2016
1 parent 6ed473a commit 44fe2d0
Show file tree
Hide file tree
Showing 58 changed files with 1,958 additions and 1,219 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static String version()
return "1.0.0";
}

public void writeLog(String message)
public static void writeLog(String message)
{
Log.v("luascriptcore", message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.HashMap;

import cn.vimfung.luascriptcore.LuaContext;
import cn.vimfung.luascriptcore.LuaFunction;
import cn.vimfung.luascriptcore.LuaMethodHandler;
import cn.vimfung.luascriptcore.LuaValue;
import cn.vimfung.luascriptcore.modules.oo.LuaObjectClass;
Expand Down Expand Up @@ -128,6 +129,7 @@ public LuaValue onExecute(LuaValue[] arguments) {
devInfoMap.put("systemVersion", Build.VERSION.RELEASE);

return new LuaValue(devInfoMap);

}
});

Expand Down Expand Up @@ -193,7 +195,7 @@ public void onClick(View v) {
_luaContext.registerModule(Person.class);
}

_luaContext.evalScript("local person = Person:create(); person:setName('vimfung'); person:speak(); person:walk();");
_luaContext.evalScript("local person = Person.create(); person:setName('vimfung'); print(person:name()); person:speak(); person:walk();");

}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.util.Log;

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

/**
Expand All @@ -21,4 +22,9 @@ public void walk()
{
Log.v("luascriptcore", String.format("%s walk", name));
}

public Person(LuaContext context)
{
super(context);
};
}
Binary file not shown.
Binary file not shown.
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.
2 changes: 1 addition & 1 deletion Sample/Android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
classpath 'com.android.tools.build:gradle:2.2.2'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
2 changes: 1 addition & 1 deletion Sample/iOS_OSX/LuaScript/todo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ function add (a, b)

return a+b;

end
end
96 changes: 32 additions & 64 deletions Sample/iOS_OSX/Sample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
7C219E361DC2EBF40002EC1D /* libLuaScriptCore-iOS.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 7C219E311DC2EAA70002EC1D /* libLuaScriptCore-iOS.a */; };
7C236D0E1DC87E7900E1F9CF /* libLuaScriptCore.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 7C236D0D1DC87E7900E1F9CF /* 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 @@ -32,32 +32,16 @@
7CE6252B1D93B435007533D7 /* LSCTPerson.m in Sources */ = {isa = PBXBuildFile; fileRef = 7CE625291D93B435007533D7 /* LSCTPerson.m */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
7C219E301DC2EAA70002EC1D /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 7C219E291DC2EAA70002EC1D /* LuaScriptCore.xcodeproj */;
proxyType = 2;
remoteGlobalIDString = 7C8C2D511D38C0C0009345EB;
remoteInfo = "LuaScriptCore-iOS";
};
7C219E321DC2EAA70002EC1D /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 7C219E291DC2EAA70002EC1D /* LuaScriptCore.xcodeproj */;
proxyType = 2;
remoteGlobalIDString = 7C6B152E1D6B2B2500489D9E;
remoteInfo = "LuaScriptCore-OSX";
};
7C219E341DC2EBF00002EC1D /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 7C219E291DC2EAA70002EC1D /* LuaScriptCore.xcodeproj */;
proxyType = 1;
remoteGlobalIDString = 7C8C2D501D38C0C0009345EB;
remoteInfo = "LuaScriptCore-iOS";
};
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
7C219E291DC2EAA70002EC1D /* LuaScriptCore.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = LuaScriptCore.xcodeproj; path = ../../../Source/iOS_OSX/LuaScriptCore.xcodeproj; sourceTree = "<group>"; };
7C236D051DC87E7900E1F9CF /* LSCContext.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LSCContext.h; sourceTree = "<group>"; };
7C236D061DC87E7900E1F9CF /* LSCFunction.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LSCFunction.h; sourceTree = "<group>"; };
7C236D071DC87E7900E1F9CF /* LSCModule.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LSCModule.h; sourceTree = "<group>"; };
7C236D081DC87E7900E1F9CF /* LSCObjectClass.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LSCObjectClass.h; sourceTree = "<group>"; };
7C236D091DC87E7900E1F9CF /* LSCPointer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LSCPointer.h; sourceTree = "<group>"; };
7C236D0A1DC87E7900E1F9CF /* LSCTypeDefinied.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LSCTypeDefinied.h; sourceTree = "<group>"; };
7C236D0B1DC87E7900E1F9CF /* LSCValue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LSCValue.h; sourceTree = "<group>"; };
7C236D0C1DC87E7900E1F9CF /* LuaScriptCore.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LuaScriptCore.h; sourceTree = "<group>"; };
7C236D0D1DC87E7900E1F9CF /* 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 @@ -107,20 +91,36 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
7C219E361DC2EBF40002EC1D /* libLuaScriptCore-iOS.a in Frameworks */,
7C236D0E1DC87E7900E1F9CF /* libLuaScriptCore.a in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
7C219E2A1DC2EAA70002EC1D /* Products */ = {
7C236D031DC87E7900E1F9CF /* LuaScriptCore */ = {
isa = PBXGroup;
children = (
7C219E311DC2EAA70002EC1D /* libLuaScriptCore-iOS.a */,
7C219E331DC2EAA70002EC1D /* libLuaScriptCore-OSX.a */,
7C236D041DC87E7900E1F9CF /* include */,
7C236D0D1DC87E7900E1F9CF /* libLuaScriptCore.a */,
);
name = Products;
name = LuaScriptCore;
path = ../../../Release/iOS;
sourceTree = "<group>";
};
7C236D041DC87E7900E1F9CF /* include */ = {
isa = PBXGroup;
children = (
7C236D051DC87E7900E1F9CF /* LSCContext.h */,
7C236D061DC87E7900E1F9CF /* LSCFunction.h */,
7C236D071DC87E7900E1F9CF /* LSCModule.h */,
7C236D081DC87E7900E1F9CF /* LSCObjectClass.h */,
7C236D091DC87E7900E1F9CF /* LSCPointer.h */,
7C236D0A1DC87E7900E1F9CF /* LSCTypeDefinied.h */,
7C236D0B1DC87E7900E1F9CF /* LSCValue.h */,
7C236D0C1DC87E7900E1F9CF /* LuaScriptCore.h */,
);
path = include;
sourceTree = "<group>";
};
7C5A617E1D6AE2A3007D4308 /* Sample-OSX */ = {
Expand Down Expand Up @@ -150,7 +150,7 @@
7C6B12721D6AFD1400489D9E /* Sample-iOS */ = {
isa = PBXGroup;
children = (
7C219E291DC2EAA70002EC1D /* LuaScriptCore.xcodeproj */,
7C236D031DC87E7900E1F9CF /* LuaScriptCore */,
7C6B12731D6AFD1400489D9E /* AppDelegate.h */,
7C6B12741D6AFD1400489D9E /* AppDelegate.m */,
7C6B12751D6AFD1400489D9E /* Assets.xcassets */,
Expand Down Expand Up @@ -260,7 +260,6 @@
buildRules = (
);
dependencies = (
7C219E351DC2EBF00002EC1D /* PBXTargetDependency */,
);
name = "Sample-iOS";
productName = Sample;
Expand Down Expand Up @@ -296,12 +295,6 @@
mainGroup = 7C8C2DD71D38C345009345EB;
productRefGroup = 7C8C2DE11D38C345009345EB /* Products */;
projectDirPath = "";
projectReferences = (
{
ProductGroup = 7C219E2A1DC2EAA70002EC1D /* Products */;
ProjectRef = 7C219E291DC2EAA70002EC1D /* LuaScriptCore.xcodeproj */;
},
);
projectRoot = "";
targets = (
7C8C2DDF1D38C345009345EB /* Sample-iOS */,
Expand All @@ -310,23 +303,6 @@
};
/* End PBXProject section */

/* Begin PBXReferenceProxy section */
7C219E311DC2EAA70002EC1D /* libLuaScriptCore-iOS.a */ = {
isa = PBXReferenceProxy;
fileType = archive.ar;
path = "libLuaScriptCore-iOS.a";
remoteRef = 7C219E301DC2EAA70002EC1D /* PBXContainerItemProxy */;
sourceTree = BUILT_PRODUCTS_DIR;
};
7C219E331DC2EAA70002EC1D /* libLuaScriptCore-OSX.a */ = {
isa = PBXReferenceProxy;
fileType = archive.ar;
path = "libLuaScriptCore-OSX.a";
remoteRef = 7C219E321DC2EAA70002EC1D /* PBXContainerItemProxy */;
sourceTree = BUILT_PRODUCTS_DIR;
};
/* End PBXReferenceProxy section */

/* Begin PBXResourcesBuildPhase section */
7C5A617B1D6AE2A3007D4308 /* Resources */ = {
isa = PBXResourcesBuildPhase;
Expand Down Expand Up @@ -382,14 +358,6 @@
};
/* End PBXSourcesBuildPhase section */

/* Begin PBXTargetDependency section */
7C219E351DC2EBF00002EC1D /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
name = "LuaScriptCore-iOS";
targetProxy = 7C219E341DC2EBF00002EC1D /* PBXContainerItemProxy */;
};
/* End PBXTargetDependency section */

/* Begin PBXVariantGroup section */
7C5A618A1D6AE2A3007D4308 /* Main.storyboard */ = {
isa = PBXVariantGroup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;

Expand All @@ -35,7 +36,6 @@ public class LuaContext extends LuaBaseObject
{
private Context _context;
private HashMap<String, LuaMethodHandler> _methods;
private HashMap<String, LuaModule> _modules;

/**
* 创建上下文对象
Expand All @@ -44,6 +44,8 @@ public class LuaContext extends LuaBaseObject
protected LuaContext(int nativeId)
{
super(nativeId);

this._methods = new HashMap<String, LuaMethodHandler>();
}

/**
Expand All @@ -53,10 +55,7 @@ protected LuaContext(int nativeId)
public static LuaContext create(Context context)
{
LuaContext luaContext = LuaNativeUtil.createContext();

luaContext._context = context;
luaContext._methods = new HashMap<String, LuaMethodHandler>();
luaContext._modules = new HashMap<String, LuaModule>();

File cacheDir = context.getExternalCacheDir();
if (cacheDir != null && cacheDir.exists())
Expand Down Expand Up @@ -168,18 +167,8 @@ public void registerModule(Class<? extends LuaModule> moduleClass)
{
try
{
Log.v("lsc", "begin register module");
String moduleName = LuaModule.getModuleName(moduleClass);
if (!LuaNativeUtil.isModuleRegisted(_nativeId, moduleName))
{
Log.v("lsc", String.format("register module = %s", moduleName));
Method regMethod = moduleClass.getMethod("register", LuaContext.class, String.class, moduleClass.getClass());
LuaModule module = (LuaModule) regMethod.invoke(moduleClass, this, moduleName, moduleClass);
if (module != null)
{
_modules.put(moduleName, module);
}
}
Method regMethod = moduleClass.getMethod("_register", LuaContext.class, moduleClass.getClass());
regMethod.invoke(moduleClass, this, moduleClass);
}
catch (NoSuchMethodException e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cn.vimfung.luascriptcore;

/**
* Lua方法对象
* Created by vimfung on 16/11/1.
*/
public class LuaFunction extends LuaBaseObject
{
private LuaContext _context;

/**
* 创建方法对象
* @param nativeId 本地对象标识
* @param context 上下文对象
*/
protected LuaFunction(int nativeId, LuaContext context)
{
super(nativeId);
_context = context;
}

/**
* 调用方法
* @param arguments 参数列表
* @return 返回值
*/
public LuaValue invoke(LuaValue[] arguments)
{
return LuaNativeUtil.invokeFunction(_context, this, arguments);
}
}
Loading

0 comments on commit 44fe2d0

Please sign in to comment.