feat: 1. ios debugger remote connected

TODO:
fix arg type
This commit is contained in:
Insomnia 2019-11-08 10:30:06 +08:00
parent 70a6c9193a
commit e5106a25ba
6 changed files with 98 additions and 21 deletions

View File

@ -51,7 +51,7 @@ + (instancetype)instance {
} }
- (void)createContext:(DoricContext *)context script:(NSString *)script source:(NSString *)source { - (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]; [context.driver createContext:context.contextId script:script source:source];
dispatch_sync(self.mapQueue, ^() { dispatch_sync(self.mapQueue, ^() {
NSValue *value = [NSValue valueWithNonretainedObject:context]; NSValue *value = [NSValue valueWithNonretainedObject:context];

View File

@ -22,6 +22,8 @@
#import "DoricJSRemoteExecutor.h" #import "DoricJSRemoteExecutor.h"
#import <SocketRocket/SRWebSocket.h> #import <SocketRocket/SRWebSocket.h>
#import "DoricUtil.h" #import "DoricUtil.h"
#import "DoricJSRemoteArgType.h"
#import "NSString+JsonString.h"
static NSString * const kUrlStr = @"ws://192.168.24.240:2080"; 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 { - (void)injectGlobalJSObject:(NSString *)name obj:(id)obj {
if ([obj isKindOfClass:NSClassFromString(@"NSBlock")]) { if ([obj isKindOfClass:NSClassFromString(@"NSBlock")]) {
self.blockMDic[name] = obj; self.blockMDic[name] = obj;
} else { }
NSDictionary *jsonDic = @{ NSDictionary *jsonDic = @{
@"cmd": @"injectGlobalJSFunction", @"cmd": @"injectGlobalJSFunction",
@"name": name @"name": name
}; };
NSError *err;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDic options:0 error:&err]; NSString *jsonStr = [NSString dc_convertToJsonWithDic:jsonDic];
if (err) { if (!jsonStr) {
DoricLog(@"debugger ", NSStringFromSelector(_cmd), @" failed");
return; return;
} }
[self.srWebSocket send:jsonData];
} [self.srWebSocket send:jsonStr];
} }
- (JSValue *)invokeObject:(NSString *)objName method:(NSString *)funcName args:(NSArray *)args { - (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]; NSMutableArray *argsMArr = [NSMutableArray new];
for (id arg in args) { for (id arg in args) {
NSDictionary *dic = @{ NSDictionary *dic = @{
@"type": [arg class], @"type": @(DoricargTypeWithArg(arg)),
@"value": arg @"value": arg
}; };
[argsMArr addObject:dic]; [argsMArr addObject:dic];
} }
NSArray *argsArr = [argsMArr copy];
NSDictionary *jsonDic = @{ NSDictionary *jsonDic = @{
@"cmd": @"invokeMethod", @"cmd": @"invokeMethod",
@"obj": objName, @"objectName": objName,
@"functionName": funcName, @"functionName": funcName,
@"javaValues": argsMArr @"javaValues": argsArr
}; };
NSError * err; NSString *jsonStr = [NSString dc_convertToJsonWithDic:jsonDic];
NSData * jsonData = [NSJSONSerialization dataWithJSONObject:jsonDic options:0 error:&err]; if (!jsonStr) {
if (err) {
DoricLog(@"debugger ", NSStringFromSelector(_cmd), @" failed");
return nil; return nil;
} }
[self.srWebSocket send:jsonData]; [self.srWebSocket send:jsonStr];
DC_LOCK(self.semaphore); DC_LOCK(self.semaphore);
return self.temp; return self.temp;

View File

@ -0,0 +1,18 @@
//
// NSString+JsonString.h
// Doric
//
// Created by Insomnia on 2019/11/7.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface NSString (JsonString)
+ (NSString *)dc_convertToJsonWithDic:(NSDictionary *)dic;
@end
NS_ASSUME_NONNULL_END

View File

@ -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

View File

@ -0,0 +1,23 @@
//
// DoricJSRemoteArgType.h
// Doric
//
// Created by Insomnia on 2019/11/7.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
typedef NS_ENUM(NSUInteger, DoricJSRemoteArgType) {
DoricJSRemoteArgTypeNil = 0,
DoricJSRemoteArgTypeInteger,
DoricJSRemoteArgTypeBool,
DoricJSRemoteArgTypeString,
DoricJSRemoteArgTypeObject,
DoricJSRemoteArgTypeArray,
};
DoricJSRemoteArgType DoricargTypeWithArg(id arg);
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,12 @@
//
// DoricJSRemoteArgType.m
// Doric
//
// Created by Insomnia on 2019/11/7.
//
#import "DoricJSRemoteArgType.h"
DoricJSRemoteArgType DoricargTypeWithArg(id arg) {
// TODO:
return DoricJSRemoteArgTypeString;
}