diff --git a/iOS/Pod/Classes/DoricContextManager.m b/iOS/Pod/Classes/DoricContextManager.m index 5e0e4de9..a6ddc2cf 100644 --- a/iOS/Pod/Classes/DoricContextManager.m +++ b/iOS/Pod/Classes/DoricContextManager.m @@ -51,7 +51,7 @@ + (instancetype)instance { } - (void)createContext:(DoricContext *)context script:(NSString *)script source:(NSString *)source { - context.contextId = [NSString stringWithFormat:@"%ld", (long) self.counter++]; + context.contextId = [NSString stringWithFormat:@"%ld", (long) ++self.counter]; [context.driver createContext:context.contextId script:script source:source]; dispatch_sync(self.mapQueue, ^() { NSValue *value = [NSValue valueWithNonretainedObject:context]; diff --git a/iOS/Pod/Classes/Engine/DoricJSRemoteExecutor.m b/iOS/Pod/Classes/Engine/DoricJSRemoteExecutor.m index f949e98d..4875126f 100644 --- a/iOS/Pod/Classes/Engine/DoricJSRemoteExecutor.m +++ b/iOS/Pod/Classes/Engine/DoricJSRemoteExecutor.m @@ -22,6 +22,8 @@ #import "DoricJSRemoteExecutor.h" #import #import "DoricUtil.h" +#import "DoricJSRemoteArgType.h" +#import "NSString+JsonString.h" static NSString * const kUrlStr = @"ws://192.168.24.240:2080"; @@ -121,19 +123,18 @@ - (NSString *)loadJSScript:(NSString *)script source:(NSString *)source { - (void)injectGlobalJSObject:(NSString *)name obj:(id)obj { if ([obj isKindOfClass:NSClassFromString(@"NSBlock")]) { self.blockMDic[name] = obj; - } else { - NSDictionary *jsonDic = @{ - @"cmd": @"injectGlobalJSFunction", - @"name": name - }; - NSError *err; - NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDic options:0 error:&err]; - if (err) { - DoricLog(@"debugger ", NSStringFromSelector(_cmd), @" failed"); - return; - } - [self.srWebSocket send:jsonData]; } + NSDictionary *jsonDic = @{ + @"cmd": @"injectGlobalJSFunction", + @"name": name + }; + + NSString *jsonStr = [NSString dc_convertToJsonWithDic:jsonDic]; + if (!jsonStr) { + return; + } + + [self.srWebSocket send:jsonStr]; } - (JSValue *)invokeObject:(NSString *)objName method:(NSString *)funcName args:(NSArray *)args { @@ -141,27 +142,27 @@ - (JSValue *)invokeObject:(NSString *)objName method:(NSString *)funcName args:( NSMutableArray *argsMArr = [NSMutableArray new]; for (id arg in args) { NSDictionary *dic = @{ - @"type": [arg class], + @"type": @(DoricargTypeWithArg(arg)), @"value": arg }; [argsMArr addObject:dic]; } + NSArray *argsArr = [argsMArr copy]; + NSDictionary *jsonDic = @{ @"cmd": @"invokeMethod", - @"obj": objName, + @"objectName": objName, @"functionName": funcName, - @"javaValues": argsMArr + @"javaValues": argsArr }; - NSError * err; - NSData * jsonData = [NSJSONSerialization dataWithJSONObject:jsonDic options:0 error:&err]; - if (err) { - DoricLog(@"debugger ", NSStringFromSelector(_cmd), @" failed"); + NSString *jsonStr = [NSString dc_convertToJsonWithDic:jsonDic]; + if (!jsonStr) { return nil; } - [self.srWebSocket send:jsonData]; + [self.srWebSocket send:jsonStr]; DC_LOCK(self.semaphore); return self.temp; diff --git a/iOS/Pod/Classes/Util/Category/NSString+JsonString.h b/iOS/Pod/Classes/Util/Category/NSString+JsonString.h new file mode 100644 index 00000000..9581e9a3 --- /dev/null +++ b/iOS/Pod/Classes/Util/Category/NSString+JsonString.h @@ -0,0 +1,18 @@ +// +// NSString+JsonString.h +// Doric +// +// Created by Insomnia on 2019/11/7. +// + + + +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface NSString (JsonString) ++ (NSString *)dc_convertToJsonWithDic:(NSDictionary *)dic; +@end + +NS_ASSUME_NONNULL_END diff --git a/iOS/Pod/Classes/Util/Category/NSString+JsonString.m b/iOS/Pod/Classes/Util/Category/NSString+JsonString.m new file mode 100644 index 00000000..bcdd6437 --- /dev/null +++ b/iOS/Pod/Classes/Util/Category/NSString+JsonString.m @@ -0,0 +1,23 @@ +// +// NSString+JsonString.m +// Doric +// +// Created by Insomnia on 2019/11/7. +// + +#import "NSString+JsonString.h" +#import "DoricUtil.h" + +@implementation NSString (JsonString) ++ (NSString *)dc_convertToJsonWithDic:(NSDictionary *)dic { + NSError *err; + NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&err]; + NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; + + if (err) { + DoricLog(NSStringFromSelector(_cmd), @"Convert dictionary to json string failed."); + return nil; + } + return jsonStr; +} +@end diff --git a/iOS/Pod/Classes/Util/DoricJSRemoteArgType.h b/iOS/Pod/Classes/Util/DoricJSRemoteArgType.h new file mode 100644 index 00000000..f18ea41d --- /dev/null +++ b/iOS/Pod/Classes/Util/DoricJSRemoteArgType.h @@ -0,0 +1,23 @@ +// +// DoricJSRemoteArgType.h +// Doric +// +// Created by Insomnia on 2019/11/7. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +typedef NS_ENUM(NSUInteger, DoricJSRemoteArgType) { + DoricJSRemoteArgTypeNil = 0, + DoricJSRemoteArgTypeInteger, + DoricJSRemoteArgTypeBool, + DoricJSRemoteArgTypeString, + DoricJSRemoteArgTypeObject, + DoricJSRemoteArgTypeArray, +}; + +DoricJSRemoteArgType DoricargTypeWithArg(id arg); + +NS_ASSUME_NONNULL_END diff --git a/iOS/Pod/Classes/Util/DoricJSRemoteArgType.m b/iOS/Pod/Classes/Util/DoricJSRemoteArgType.m new file mode 100644 index 00000000..1b018109 --- /dev/null +++ b/iOS/Pod/Classes/Util/DoricJSRemoteArgType.m @@ -0,0 +1,12 @@ +// +// DoricJSRemoteArgType.m +// Doric +// +// Created by Insomnia on 2019/11/7. +// + +#import "DoricJSRemoteArgType.h" +DoricJSRemoteArgType DoricargTypeWithArg(id arg) { + // TODO: 类型缺失 + return DoricJSRemoteArgTypeString; +}