This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Doric/doric-iOS/Devkit/Classes/DoricRemoteJSExecutor.m

219 lines
7.7 KiB
Mathematica
Raw Normal View History

2021-02-22 19:03:34 +08:00
/*
* Copyright [2019] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// DoricRemoteJSExecutor.m
// DoricDevkit
//
// Created by pengfei.zhou on 2021/2/22.
//
#import "DoricRemoteJSExecutor.h"
#import "NSString+JsonString.h"
#import <DoricCore/Doric.h>
typedef NS_ENUM(NSUInteger, DoricJSRemoteArgType) {
DoricJSRemoteArgTypeNil = 0,
DoricJSRemoteArgTypeNumber,
DoricJSRemoteArgTypeBool,
DoricJSRemoteArgTypeString,
DoricJSRemoteArgTypeObject,
DoricJSRemoteArgTypeArray,
};
typedef id (^Block0)(void);
typedef id (^Block1)(id arg0);
typedef id (^Block2)(id arg0, id arg1);
typedef id (^Block3)(id arg0, id arg1, id arg2);
typedef id (^Block4)(id arg0, id arg1, id arg2, id arg3);
typedef id (^Block5)(id arg0, id arg1, id arg2, id arg3, id arg4);
@interface DoricRemoteJSExecutor () <DoricWSClientInterceptor>
@property(nonatomic, weak) DoricWSClient *wsClient;
@property(nonatomic, strong) NSMutableDictionary <NSString *, id> *blockMDic;
2021-03-04 10:02:29 +08:00
@property(nonatomic) NSInteger counter;
@property(nonatomic, strong) NSMutableDictionary <NSNumber *, dispatch_semaphore_t> *semaphores;
@property(nonatomic, strong) NSMutableDictionary <NSNumber *, JSValue *> *results;
@property(nonatomic, strong) JSContext *jsContext;
2021-02-22 19:03:34 +08:00
@end
@implementation DoricRemoteJSExecutor
- (instancetype)initWithWSClient:(DoricWSClient *)wsClient {
if (self = [super init]) {
_wsClient = wsClient;
[_wsClient addInterceptor:self];
_blockMDic = [NSMutableDictionary new];
2021-03-04 10:02:29 +08:00
_semaphores = [NSMutableDictionary new];
_results = [NSMutableDictionary new];
_counter = 0;
_jsContext = [[JSContext alloc] init];
2021-02-22 19:03:34 +08:00
}
return self;
}
- (NSString *)loadJSScript:(NSString *)script source:(NSString *)source {
return nil;
}
- (void)injectGlobalJSObject:(NSString *)name obj:(id)obj {
if ([obj isKindOfClass:NSClassFromString(@"NSBlock")]) {
self.blockMDic[name] = obj;
[self.wsClient sendToDebugger:@"injectGlobalJSFunction" payload:@{
@"name": name,
}];
} else if ([obj isKindOfClass:NSNumber.class]) {
[self.wsClient sendToDebugger:@"injectGlobalJSObject" payload:@{
@"name": name,
@"type": @(DoricJSRemoteArgTypeNumber),
2021-02-23 11:06:57 +08:00
@"value": [NSString stringWithFormat:@"%@", obj],
2021-02-22 19:03:34 +08:00
}];
} else if ([obj isKindOfClass:NSString.class]) {
[self.wsClient sendToDebugger:@"injectGlobalJSObject" payload:@{
@"name": name,
@"type": @(DoricJSRemoteArgTypeString),
@"value": obj,
}];
} else if ([obj isKindOfClass:NSObject.class]) {
[self.wsClient sendToDebugger:@"injectGlobalJSObject" payload:@{
@"name": name,
@"type": @(DoricJSRemoteArgTypeObject),
2021-02-23 11:06:57 +08:00
@"value": [NSString dc_convertToJsonWithDic:obj],
2021-02-22 19:03:34 +08:00
}];
} else if ([obj isKindOfClass:NSArray.class]) {
[self.wsClient sendToDebugger:@"injectGlobalJSObject" payload:@{
@"name": name,
@"type": @(DoricJSRemoteArgTypeArray),
2021-02-23 11:06:57 +08:00
@"value": [NSString dc_convertToJsonWithDic:obj],
2021-02-22 19:03:34 +08:00
}];
} else if (obj == nil) {
[self.wsClient sendToDebugger:@"injectGlobalJSObject" payload:@{
@"name": name,
@"type": @(DoricJSRemoteArgTypeNil),
}];
}
}
- (JSValue *)invokeObject:(NSString *)objName method:(NSString *)funcName args:(NSArray *)args {
NSMutableArray *argsMArr = [NSMutableArray new];
for (id arg in args) {
NSDictionary *dic = [self dicForArg:arg];
[argsMArr addObject:dic];
}
2021-03-04 10:02:29 +08:00
NSInteger callId = ++self.counter;
2021-02-22 19:03:34 +08:00
[self.wsClient sendToDebugger:@"invokeMethod" payload:@{
@"cmd": @"invokeMethod",
@"objectName": objName,
@"functionName": funcName,
2021-03-04 10:02:29 +08:00
@"callId": @(callId),
2021-02-22 19:03:34 +08:00
@"values": [argsMArr copy]
}];
2021-03-04 10:02:29 +08:00
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
self.semaphores[@(callId)] = semaphore;
self.invokingMethod = YES;
DoricLog(@"Lock %@", @(callId));
2021-03-04 10:02:29 +08:00
DC_LOCK(semaphore);
JSValue *result = self.results[@(callId)];
[self.results removeObjectForKey:@(callId)];
self.invokingMethod = NO;
return result;
2021-02-22 19:03:34 +08:00
}
- (NSDictionary *)dicForArg:(id)arg {
DoricJSRemoteArgType type = [self argType:arg];
if (type == DoricJSRemoteArgTypeObject || type == DoricJSRemoteArgTypeArray) {
NSString *jsonStr = [NSString dc_convertToJsonWithDic:arg];
arg = jsonStr;
}
NSDictionary *dic = @{
@"type": @(type),
@"value": arg
};
return dic;
}
- (DoricJSRemoteArgType)argType:(id)arg {
DoricJSRemoteArgType type = DoricJSRemoteArgTypeNil;
if ([arg isKindOfClass:[NSNumber class]]) {
type = DoricJSRemoteArgTypeNumber;
} else if ([arg isKindOfClass:[NSString class]]) {
type = DoricJSRemoteArgTypeString;
} else if ([arg isKindOfClass:[NSDictionary class]]) {
type = DoricJSRemoteArgTypeObject;
} else if ([arg isKindOfClass:[NSMutableArray class]]) {
type = DoricJSRemoteArgTypeArray;
}
return type;
}
- (BOOL)interceptType:(NSString *)type command:(NSString *)cmd payload:(NSDictionary *)payload {
if ([type isEqualToString:@"D2C"]) {
if ([cmd isEqualToString:@"injectGlobalJSFunction"]) {
NSString *name = payload[@"name"];
NSArray *argsArr = payload[@"arguments"];
id tmpBlk = self.blockMDic[name];
if (!tmpBlk) {
DoricLog(@"Cannot find inject function:%@", name);
} else if (argsArr.count == 0) {
2021-03-04 10:02:29 +08:00
((Block0) tmpBlk)();
2021-02-22 19:03:34 +08:00
} else if (argsArr.count == 1) {
2021-03-04 10:02:29 +08:00
((Block1) tmpBlk)(argsArr[0]);
2021-02-22 19:03:34 +08:00
} else if (argsArr.count == 2) {
2021-03-04 10:02:29 +08:00
((Block2) tmpBlk)(argsArr[0], argsArr[1]);
2021-02-22 19:03:34 +08:00
} else if (argsArr.count == 3) {
2021-03-04 10:02:29 +08:00
((Block3) tmpBlk)(argsArr[0], argsArr[1], argsArr[2]);
2021-02-22 19:03:34 +08:00
} else if (argsArr.count == 4) {
2021-03-04 10:02:29 +08:00
((Block4) tmpBlk)(argsArr[0], argsArr[1], argsArr[2], argsArr[3]);
2021-02-22 19:03:34 +08:00
} else if (argsArr.count == 5) {
2021-02-23 11:06:57 +08:00
((Block5) tmpBlk)(argsArr[0], argsArr[1], argsArr[2], argsArr[3], argsArr[4]);
2021-02-22 19:03:34 +08:00
} else {
DoricLog(@"error:args to more than 5. args:%@", argsArr);
}
} else if ([cmd isEqualToString:@"invokeMethod"]) {
2021-03-04 10:02:29 +08:00
NSNumber *callId = payload[@"callId"];
2021-02-22 19:03:34 +08:00
@try {
JSValue *value = [JSValue valueWithObject:payload[@"result"] inContext:self.jsContext];
2021-03-04 10:02:29 +08:00
self.results[callId] = value;
2021-02-22 19:03:34 +08:00
} @catch (NSException *exception) {
DoricLog(@"debugger ", NSStringFromSelector(_cmd), exception.reason);
} @finally {
DoricLog(@"Unlock:%@", payload);
2021-03-04 10:02:29 +08:00
dispatch_semaphore_t semaphore = self.semaphores[callId];
if (semaphore) {
[self.semaphores removeObjectForKey:callId];
DC_UNLOCK(semaphore);
}
2021-02-22 19:03:34 +08:00
}
}
}
return NO;
}
2021-03-04 10:02:29 +08:00
- (void)teardown {
[self.blockMDic removeAllObjects];
[self.semaphores enumerateKeysAndObjectsUsingBlock:^(NSNumber *key, dispatch_semaphore_t obj, BOOL *stop) {
DC_UNLOCK(obj);
}];
}
2021-02-22 19:03:34 +08:00
@end