Skip to content

Commit 6ed473a

Browse files
committed
优化iOS代码
1 parent f105867 commit 6ed473a

6 files changed

Lines changed: 76 additions & 87 deletions

File tree

Sample/iOS_OSX/Common/LogModule.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818
*
1919
* @param message 日志信息
2020
*/
21-
+ (void)writeLog:(NSString *)message value:(NSInteger)value;
21+
+ (void)writeLog:(NSString *)message;
2222

2323
@end

Sample/iOS_OSX/Common/LogModule.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
@implementation LogModule
1212

13-
+ (void)writeLog:(NSString *)message value:(NSInteger)value
13+
+ (void)writeLog:(NSString *)message
1414
{
1515
NSLog(@"** message = %@", message);
1616
}

Sample/iOS_OSX/Sample-iOS/ViewController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ - (IBAction)registerModuleClickedHandler:(id)sender
143143
[self.context registerModuleWithClass:[LogModule class]];
144144
}
145145

146-
[self.context evalScriptFromString:@"LogModule.writeLog('Hello Lua Module!', 10.1);"];
146+
[self.context evalScriptFromString:@"LogModule.writeLog('Hello Lua Module!');"];
147147
}
148148

149149

Sample/iOS_OSX/Sample.xcodeproj/project.pbxproj

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
7C219E361DC2EBF40002EC1D /* libLuaScriptCore-iOS.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 7C219E311DC2EAA70002EC1D /* libLuaScriptCore-iOS.a */; };
1011
7C5A61811D6AE2A3007D4308 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 7C5A61801D6AE2A3007D4308 /* AppDelegate.m */; };
1112
7C5A61841D6AE2A3007D4308 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 7C5A61831D6AE2A3007D4308 /* main.m */; };
1213
7C5A61871D6AE2A3007D4308 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 7C5A61861D6AE2A3007D4308 /* ViewController.m */; };
@@ -29,34 +30,34 @@
2930
7CE625271D93B3DA007533D7 /* libLuaScriptCore.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 7CE625261D93B3DA007533D7 /* libLuaScriptCore.a */; };
3031
7CE6252A1D93B435007533D7 /* LSCTPerson.m in Sources */ = {isa = PBXBuildFile; fileRef = 7CE625291D93B435007533D7 /* LSCTPerson.m */; };
3132
7CE6252B1D93B435007533D7 /* LSCTPerson.m in Sources */ = {isa = PBXBuildFile; fileRef = 7CE625291D93B435007533D7 /* LSCTPerson.m */; };
32-
7CE8EA491DC1F76900AE2342 /* libLuaScriptCore-iOS.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 7CE8EA441DC1F75F00AE2342 /* libLuaScriptCore-iOS.a */; };
3333
/* End PBXBuildFile section */
3434

3535
/* Begin PBXContainerItemProxy section */
36-
7CE8EA431DC1F75F00AE2342 /* PBXContainerItemProxy */ = {
36+
7C219E301DC2EAA70002EC1D /* PBXContainerItemProxy */ = {
3737
isa = PBXContainerItemProxy;
38-
containerPortal = 7CE8EA3C1DC1F75F00AE2342 /* LuaScriptCore.xcodeproj */;
38+
containerPortal = 7C219E291DC2EAA70002EC1D /* LuaScriptCore.xcodeproj */;
3939
proxyType = 2;
4040
remoteGlobalIDString = 7C8C2D511D38C0C0009345EB;
4141
remoteInfo = "LuaScriptCore-iOS";
4242
};
43-
7CE8EA451DC1F75F00AE2342 /* PBXContainerItemProxy */ = {
43+
7C219E321DC2EAA70002EC1D /* PBXContainerItemProxy */ = {
4444
isa = PBXContainerItemProxy;
45-
containerPortal = 7CE8EA3C1DC1F75F00AE2342 /* LuaScriptCore.xcodeproj */;
45+
containerPortal = 7C219E291DC2EAA70002EC1D /* LuaScriptCore.xcodeproj */;
4646
proxyType = 2;
4747
remoteGlobalIDString = 7C6B152E1D6B2B2500489D9E;
4848
remoteInfo = "LuaScriptCore-OSX";
4949
};
50-
7CE8EA471DC1F76500AE2342 /* PBXContainerItemProxy */ = {
50+
7C219E341DC2EBF00002EC1D /* PBXContainerItemProxy */ = {
5151
isa = PBXContainerItemProxy;
52-
containerPortal = 7CE8EA3C1DC1F75F00AE2342 /* LuaScriptCore.xcodeproj */;
52+
containerPortal = 7C219E291DC2EAA70002EC1D /* LuaScriptCore.xcodeproj */;
5353
proxyType = 1;
5454
remoteGlobalIDString = 7C8C2D501D38C0C0009345EB;
5555
remoteInfo = "LuaScriptCore-iOS";
5656
};
5757
/* End PBXContainerItemProxy section */
5858

5959
/* Begin PBXFileReference section */
60+
7C219E291DC2EAA70002EC1D /* LuaScriptCore.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = LuaScriptCore.xcodeproj; path = ../../../Source/iOS_OSX/LuaScriptCore.xcodeproj; sourceTree = "<group>"; };
6061
7C5A617D1D6AE2A3007D4308 /* Sample-OSX.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Sample-OSX.app"; sourceTree = BUILT_PRODUCTS_DIR; };
6162
7C5A617F1D6AE2A3007D4308 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
6263
7C5A61801D6AE2A3007D4308 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
@@ -91,7 +92,6 @@
9192
7CE625261D93B3DA007533D7 /* libLuaScriptCore.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libLuaScriptCore.a; sourceTree = "<group>"; };
9293
7CE625281D93B435007533D7 /* LSCTPerson.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LSCTPerson.h; sourceTree = "<group>"; };
9394
7CE625291D93B435007533D7 /* LSCTPerson.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LSCTPerson.m; sourceTree = "<group>"; };
94-
7CE8EA3C1DC1F75F00AE2342 /* LuaScriptCore.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = LuaScriptCore.xcodeproj; path = ../../../Source/iOS_OSX/LuaScriptCore.xcodeproj; sourceTree = "<group>"; };
9595
/* End PBXFileReference section */
9696

9797
/* Begin PBXFrameworksBuildPhase section */
@@ -107,13 +107,22 @@
107107
isa = PBXFrameworksBuildPhase;
108108
buildActionMask = 2147483647;
109109
files = (
110-
7CE8EA491DC1F76900AE2342 /* libLuaScriptCore-iOS.a in Frameworks */,
110+
7C219E361DC2EBF40002EC1D /* libLuaScriptCore-iOS.a in Frameworks */,
111111
);
112112
runOnlyForDeploymentPostprocessing = 0;
113113
};
114114
/* End PBXFrameworksBuildPhase section */
115115

116116
/* Begin PBXGroup section */
117+
7C219E2A1DC2EAA70002EC1D /* Products */ = {
118+
isa = PBXGroup;
119+
children = (
120+
7C219E311DC2EAA70002EC1D /* libLuaScriptCore-iOS.a */,
121+
7C219E331DC2EAA70002EC1D /* libLuaScriptCore-OSX.a */,
122+
);
123+
name = Products;
124+
sourceTree = "<group>";
125+
};
117126
7C5A617E1D6AE2A3007D4308 /* Sample-OSX */ = {
118127
isa = PBXGroup;
119128
children = (
@@ -141,7 +150,7 @@
141150
7C6B12721D6AFD1400489D9E /* Sample-iOS */ = {
142151
isa = PBXGroup;
143152
children = (
144-
7CE8EA3C1DC1F75F00AE2342 /* LuaScriptCore.xcodeproj */,
153+
7C219E291DC2EAA70002EC1D /* LuaScriptCore.xcodeproj */,
145154
7C6B12731D6AFD1400489D9E /* AppDelegate.h */,
146155
7C6B12741D6AFD1400489D9E /* AppDelegate.m */,
147156
7C6B12751D6AFD1400489D9E /* Assets.xcassets */,
@@ -220,15 +229,6 @@
220229
path = ../../../Release/OSX;
221230
sourceTree = "<group>";
222231
};
223-
7CE8EA3D1DC1F75F00AE2342 /* Products */ = {
224-
isa = PBXGroup;
225-
children = (
226-
7CE8EA441DC1F75F00AE2342 /* libLuaScriptCore-iOS.a */,
227-
7CE8EA461DC1F75F00AE2342 /* libLuaScriptCore-OSX.a */,
228-
);
229-
name = Products;
230-
sourceTree = "<group>";
231-
};
232232
/* End PBXGroup section */
233233

234234
/* Begin PBXNativeTarget section */
@@ -260,7 +260,7 @@
260260
buildRules = (
261261
);
262262
dependencies = (
263-
7CE8EA481DC1F76500AE2342 /* PBXTargetDependency */,
263+
7C219E351DC2EBF00002EC1D /* PBXTargetDependency */,
264264
);
265265
name = "Sample-iOS";
266266
productName = Sample;
@@ -298,8 +298,8 @@
298298
projectDirPath = "";
299299
projectReferences = (
300300
{
301-
ProductGroup = 7CE8EA3D1DC1F75F00AE2342 /* Products */;
302-
ProjectRef = 7CE8EA3C1DC1F75F00AE2342 /* LuaScriptCore.xcodeproj */;
301+
ProductGroup = 7C219E2A1DC2EAA70002EC1D /* Products */;
302+
ProjectRef = 7C219E291DC2EAA70002EC1D /* LuaScriptCore.xcodeproj */;
303303
},
304304
);
305305
projectRoot = "";
@@ -311,18 +311,18 @@
311311
/* End PBXProject section */
312312

313313
/* Begin PBXReferenceProxy section */
314-
7CE8EA441DC1F75F00AE2342 /* libLuaScriptCore-iOS.a */ = {
314+
7C219E311DC2EAA70002EC1D /* libLuaScriptCore-iOS.a */ = {
315315
isa = PBXReferenceProxy;
316316
fileType = archive.ar;
317317
path = "libLuaScriptCore-iOS.a";
318-
remoteRef = 7CE8EA431DC1F75F00AE2342 /* PBXContainerItemProxy */;
318+
remoteRef = 7C219E301DC2EAA70002EC1D /* PBXContainerItemProxy */;
319319
sourceTree = BUILT_PRODUCTS_DIR;
320320
};
321-
7CE8EA461DC1F75F00AE2342 /* libLuaScriptCore-OSX.a */ = {
321+
7C219E331DC2EAA70002EC1D /* libLuaScriptCore-OSX.a */ = {
322322
isa = PBXReferenceProxy;
323323
fileType = archive.ar;
324324
path = "libLuaScriptCore-OSX.a";
325-
remoteRef = 7CE8EA451DC1F75F00AE2342 /* PBXContainerItemProxy */;
325+
remoteRef = 7C219E321DC2EAA70002EC1D /* PBXContainerItemProxy */;
326326
sourceTree = BUILT_PRODUCTS_DIR;
327327
};
328328
/* End PBXReferenceProxy section */
@@ -383,10 +383,10 @@
383383
/* End PBXSourcesBuildPhase section */
384384

385385
/* Begin PBXTargetDependency section */
386-
7CE8EA481DC1F76500AE2342 /* PBXTargetDependency */ = {
386+
7C219E351DC2EBF00002EC1D /* PBXTargetDependency */ = {
387387
isa = PBXTargetDependency;
388388
name = "LuaScriptCore-iOS";
389-
targetProxy = 7CE8EA471DC1F76500AE2342 /* PBXContainerItemProxy */;
389+
targetProxy = 7C219E341DC2EBF00002EC1D /* PBXContainerItemProxy */;
390390
};
391391
/* End PBXTargetDependency section */
392392

Source/iOS_OSX/Code/LSCModule.m

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@ static int ModuleMethodRouteHandler(lua_State *state)
2121
{
2222
//修复float类型在Invocation中会丢失问题,需要定义该结构体来提供给带float参数的方法。同时返回值处理也一样。
2323
typedef struct {float f;} LSCFloatStruct;
24-
id obj =nil;
24+
id obj = nil;
2525

2626
LSCContext *context = (__bridge LSCContext *)lua_topointer(state, lua_upvalueindex(1));
2727
Class moduleClass = (__bridge Class)lua_topointer(state, lua_upvalueindex(2));
2828
NSString *methodName = [NSString stringWithUTF8String:lua_tostring(state, lua_upvalueindex(3))];
29-
NSString *returnType = [NSString stringWithUTF8String:lua_tostring(state, lua_upvalueindex(4))];
3029
SEL selector = NSSelectorFromString(methodName);
3130

3231
NSMethodSignature *sign = [moduleClass methodSignatureForSelector:selector];
@@ -95,24 +94,24 @@ static int ModuleMethodRouteHandler(lua_State *state)
9594

9695
[invocation invoke];
9796

98-
// char *returnType = method_copyReturnType(m);
97+
char *returnType = method_copyReturnType(m);
9998
LSCValue *retValue = nil;
10099

101-
if ([returnType isEqualToString:@"@"])
100+
if (strcmp(returnType, @encode(id)) == 0)
102101
{
103102
//返回值为对象
104103
id __unsafe_unretained retObj = nil;
105104
[invocation getReturnValue:&retObj];
106105
retValue = [LSCValue objectValue:retObj];
107106
}
108-
else if ([returnType isEqualToString:@"i"]
109-
|| [returnType isEqualToString:@"I"]
110-
|| [returnType isEqualToString:@"q"]
111-
|| [returnType isEqualToString:@"Q"]
112-
|| [returnType isEqualToString:@"s"]
113-
|| [returnType isEqualToString:@"S"]
114-
|| [returnType isEqualToString:@"c"]
115-
|| [returnType isEqualToString:@"C"])
107+
else if (strcmp(returnType, @encode(int)) == 0
108+
|| strcmp(returnType, @encode(unsigned int)) == 0
109+
|| strcmp(returnType, @encode(long)) == 0
110+
|| strcmp(returnType, @encode(unsigned long)) == 0
111+
|| strcmp(returnType, @encode(short)) == 0
112+
|| strcmp(returnType, @encode(unsigned short)) == 0
113+
|| strcmp(returnType, @encode(char)) == 0
114+
|| strcmp(returnType, @encode(unsigned char)) == 0)
116115
{
117116
// i 整型
118117
// I 无符号整型
@@ -126,22 +125,22 @@ static int ModuleMethodRouteHandler(lua_State *state)
126125
[invocation getReturnValue:&intValue];
127126
retValue = [LSCValue integerValue:intValue];
128127
}
129-
else if ([returnType isEqualToString:@"f"])
128+
else if (strcmp(returnType, @encode(float)) == 0)
130129
{
131130
// f 浮点型,需要将值保存到floatStruct结构中传入给方法,否则会导致数据丢失
132131
LSCFloatStruct floatStruct = {0};
133132
[invocation getReturnValue:&floatStruct];
134133
retValue = [LSCValue numberValue:@(floatStruct.f)];
135134

136135
}
137-
else if ([returnType isEqualToString:@"d"])
136+
else if (strcmp(returnType, @encode(double)) == 0)
138137
{
139138
// d 双精度浮点型
140139
double doubleValue = 0.0;
141140
[invocation getReturnValue:&doubleValue];
142141
retValue = [LSCValue numberValue:@(doubleValue)];
143142
}
144-
else if ([returnType isEqualToString:@"B"])
143+
else if (strcmp(returnType, @encode(BOOL)) == 0)
145144
{
146145
//B 布尔类型
147146
BOOL boolValue = NO;
@@ -153,6 +152,7 @@ static int ModuleMethodRouteHandler(lua_State *state)
153152
//结构体和其他类型暂时认为和v一样无返回值
154153
retValue = nil;
155154
}
155+
free(returnType);
156156

157157
if (retValue)
158158
{
@@ -253,10 +253,6 @@ + (void)_exportModuleMethod:(Class)thiz module:(Class)module context:(LSCContext
253253
{
254254
SEL selector = method_getName(*m);
255255

256-
size_t returnTypeLen = 256;
257-
char returnType[256];
258-
method_getReturnType(*m, returnType, returnTypeLen);
259-
260256
NSString *methodName = NSStringFromSelector(selector);
261257
if (![methodName hasPrefix:@"_"]
262258
&& ![methodName hasPrefix:@"."]
@@ -278,8 +274,7 @@ + (void)_exportModuleMethod:(Class)thiz module:(Class)module context:(LSCContext
278274
lua_pushlightuserdata(state, (__bridge void *)context);
279275
lua_pushlightuserdata(state, (__bridge void *)thiz);
280276
lua_pushstring(state, [methodName UTF8String]);
281-
lua_pushstring(state, returnType);
282-
lua_pushcclosure(state, ModuleMethodRouteHandler, 4);
277+
lua_pushcclosure(state, ModuleMethodRouteHandler, 3);
283278

284279
lua_setfield(state, -2, [luaMethodName UTF8String]);
285280
}

0 commit comments

Comments
 (0)