From af3590b0ba8a0727862af8dbba9fe0717797c949 Mon Sep 17 00:00:00 2001 From: Insomnia <282132315@qq.com> Date: Fri, 1 Nov 2019 00:13:50 +0800 Subject: [PATCH] add: injectGlobalJSObject --- .../Classes/Engine/DoricJSRemoteExecutor.h | 1 + .../Classes/Engine/DoricJSRemoteExecutor.m | 31 +++++++++++++++---- iOS/Pod/Classes/Util/DoricUtil.h | 8 +++++ 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/iOS/Pod/Classes/Engine/DoricJSRemoteExecutor.h b/iOS/Pod/Classes/Engine/DoricJSRemoteExecutor.h index e626791e..b9145867 100644 --- a/iOS/Pod/Classes/Engine/DoricJSRemoteExecutor.h +++ b/iOS/Pod/Classes/Engine/DoricJSRemoteExecutor.h @@ -29,6 +29,7 @@ NS_ASSUME_NONNULL_BEGIN @property(nonatomic, strong) dispatch_semaphore_t semaphore; +- (void)close; @end NS_ASSUME_NONNULL_END diff --git a/iOS/Pod/Classes/Engine/DoricJSRemoteExecutor.m b/iOS/Pod/Classes/Engine/DoricJSRemoteExecutor.m index bd085a2f..57fbe9c2 100644 --- a/iOS/Pod/Classes/Engine/DoricJSRemoteExecutor.m +++ b/iOS/Pod/Classes/Engine/DoricJSRemoteExecutor.m @@ -26,8 +26,9 @@ static NSString * const kUrlStr = @"ws://192.168.24.240:2080"; @interface DoricJSRemoteExecutor () -@property(nonatomic, strong) NSMapTable *mapTable; @property(nonatomic, strong) SRWebSocket *websocket; +@property(nonatomic, strong) NSMapTable *mapTable; +@property(nonatomic, strong) dispatch_semaphore_t mapTableLock; @end @implementation DoricJSRemoteExecutor @@ -35,14 +36,15 @@ - (instancetype)init { if (self = [super init]) { [self websocket]; _semaphore = dispatch_semaphore_create(0); - dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER); + _mapTableLock = dispatch_semaphore_create(1); + DC_LOCK(self.semaphore); } return self; } - (void)webSocketDidOpen:(SRWebSocket *)webSocket { DoricLog(@"debugger webSocketDidOpen"); - dispatch_semaphore_signal(_semaphore); + DC_UNLOCK(self.semaphore); } - (void)webSocket:(SRWebSocket *)webSocket didReceivePong:(NSData *)pongPayload { @@ -64,7 +66,7 @@ - (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message { - (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error { DoricLog(@"debugger webSocketdidFailWithError"); - dispatch_semaphore_signal(_semaphore); + DC_UNLOCK(self.semaphore); } - (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean { @@ -77,7 +79,20 @@ - (NSString *)loadJSScript:(NSString *)script source:(NSString *)source { } - (void)injectGlobalJSObject:(NSString *)name obj:(id)obj { + DC_LOCK(self.mapTableLock); + [self.mapTable setObject:obj forKey:name]; + DC_UNLOCK(self.mapTableLock); + NSDictionary *jsonDic =@{ + @"cmd": @"injectGlobalJSFunction", + @"name": name + }; + NSError * err; + NSData * jsonData = [NSJSONSerialization dataWithJSONObject:jsonDic options:0 error:&err]; + if (err) { + DoricLog(@"debugger ", NSStringFromSelector(_cmd), @" failed"); + } + [self.websocket send:jsonData]; } - (JSValue *)invokeObject:(NSString *)objName method:(NSString *)funcName args:(NSArray *)args { @@ -105,11 +120,15 @@ - (JSValue *)invokeObject:(NSString *)objName method:(NSString *)funcName args:( } [self.websocket send:jsonData]; - dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER); + DC_LOCK(self.semaphore); return nil; } +- (void)close { + [self.websocket close]; +} + #pragma mark - Properties - (SRWebSocket *)websocket { if (!_websocket) { @@ -123,7 +142,7 @@ - (SRWebSocket *)websocket { - (NSMapTable *)mapTable { if (!_mapTable) { - _mapTable = [NSMapTable new]; + _mapTable = [[NSMapTable alloc] initWithKeyOptions:NSPointerFunctionsStrongMemory valueOptions:NSPointerFunctionsWeakMemory capacity:0]; } return _mapTable; } diff --git a/iOS/Pod/Classes/Util/DoricUtil.h b/iOS/Pod/Classes/Util/DoricUtil.h index dba4e355..ae606cc9 100644 --- a/iOS/Pod/Classes/Util/DoricUtil.h +++ b/iOS/Pod/Classes/Util/DoricUtil.h @@ -27,3 +27,11 @@ void DoricLog(NSString *_Nonnull format, ...); UIColor *_Nonnull DoricColor(NSNumber *_Nonnull number); NSBundle *DoricBundle(); + +#ifndef DC_LOCK +#define DC_LOCK(lock) dispatch_semaphore_wait(lock, DISPATCH_TIME_FOREVER); +#endif + +#ifndef DC_UNLOCK +#define DC_UNLOCK(lock) dispatch_semaphore_signal(lock); +#endif