add different jse in core & devkit pods

This commit is contained in:
王劲鹏
2020-02-26 11:50:18 +08:00
committed by osborn
parent 3facc3fdee
commit d215926387
13 changed files with 360 additions and 28 deletions

View File

@@ -0,0 +1,38 @@
/*
* 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.
*/
//
// DoricDebugDriver.h
// Doric
//
// Created by jingpeng.wang on 2020/2/25.
//
#import <Foundation/Foundation.h>
#import "DoricDriverProtocol.h"
typedef NS_ENUM(NSInteger, DoricQueueMode) {
JS = 0,
UI,
INDEPENDENT
};
NS_ASSUME_NONNULL_BEGIN
@interface DoricDebugDriver : NSObject <DoricDriverProtocol>
+ (instancetype)instance;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,186 @@
/*
* 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.
*/
//
// DoricDebugDriver.m
// Doric
//
// Created by jingpeng.wang on 2020/2/25.
//
#import "DoricDebugDriver.h"
#import "DoricDebugJSEngine.h"
#import "DoricConstant.h"
#import "DoricContextManager.h"
@interface DoricDebugDriver ()
@property(nonatomic, strong) DoricJSEngine *jsExecutor;
@end
@implementation DoricDebugDriver
@dynamic registry;
- (instancetype)init {
if (self = [super init]) {
_jsExecutor = [[DoricDebugJSEngine alloc] init];
}
return self;
}
- (DoricRegistry *)registry {
return self.jsExecutor.registry;
}
+ (instancetype)instance {
static DoricDebugDriver *_instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [[DoricDebugDriver alloc] init];
});
return _instance;
}
- (DoricAsyncResult<JSValue *> *)invokeDoricMethod:(NSString *)method, ... {
va_list args;
va_start(args, method);
DoricAsyncResult *ret = [self invokeDoricMethod:method arguments:args];
va_end(args);
return ret;
}
- (NSString *)aliasWithContextId:(NSString *)contextId {
return [[DoricContextManager instance] getContext:contextId].source;
}
- (DoricAsyncResult<JSValue *> *)invokeDoricMethod:(NSString *)method arguments:(va_list)args {
DoricAsyncResult *ret = [[DoricAsyncResult alloc] init];
NSMutableArray *array = [[NSMutableArray alloc] init];
id arg;
while ((arg = va_arg(args, id)) != nil) {
[array addObject:arg];
}
__weak typeof(self) _self = self;
dispatch_async(self.jsExecutor.jsQueue, ^() {
__strong typeof(_self) self = _self;
if (!self) return;
@try {
JSValue *jsValue = [self.jsExecutor invokeDoricMethod:method argumentsArray:array];
[ret setupResult:jsValue];
} @catch (NSException *exception) {
[ret setupError:exception];
}
});
return ret;
}
- (DoricAsyncResult<JSValue *> *)invokeContextEntity:(NSString *)contextId method:(NSString *)method, ... {
va_list args;
va_start(args, method);
DoricAsyncResult *ret = [self invokeContextEntity:contextId method:method arguments:args];
va_end(args);
return ret;
}
- (DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString *)method arguments:(va_list)args {
DoricAsyncResult *ret = [[DoricAsyncResult alloc] init];
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:contextId];
[array addObject:method];
id arg = va_arg(args, id);
while (arg != nil) {
[array addObject:arg];
arg = va_arg(args, JSValue *);
}
__weak typeof(self) _self = self;
dispatch_async(self.jsExecutor.jsQueue, ^() {
__strong typeof(_self) self = _self;
if (!self) return;
@try {
JSValue *jsValue = [self.jsExecutor invokeDoricMethod:DORIC_CONTEXT_INVOKE argumentsArray:array];
[ret setupResult:jsValue];
} @catch (NSException *exception) {
[ret setupError:exception];
[self.jsExecutor.registry onException:exception source:[self aliasWithContextId:contextId]];
}
});
return ret;
}
- (DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString *)method argumentsArray:(NSArray *)args {
DoricAsyncResult *ret = [[DoricAsyncResult alloc] init];
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:contextId];
[array addObject:method];
for (id arg in args) {
[array addObject:arg];
}
__weak typeof(self) _self = self;
dispatch_async(self.jsExecutor.jsQueue, ^() {
__strong typeof(_self) self = _self;
if (!self) return;
@try {
JSValue *jsValue = [self.jsExecutor invokeDoricMethod:DORIC_CONTEXT_INVOKE argumentsArray:array];
[ret setupResult:jsValue];
} @catch (NSException *exception) {
[ret setupError:exception];
[self.jsExecutor.registry onException:exception source:[self aliasWithContextId:contextId]];
}
});
return ret;
}
- (DoricAsyncResult *)createContext:(NSString *)contextId script:(NSString *)script source:(NSString *)source {
DoricAsyncResult *ret = [[DoricAsyncResult alloc] init];
__weak typeof(self) _self = self;
dispatch_async(self.jsExecutor.jsQueue, ^() {
__strong typeof(_self) self = _self;
if (!self) return;
@try {
[self.jsExecutor prepareContext:contextId script:script source:source];
[ret setupResult:@YES];
} @catch (NSException *exception) {
[ret setupError:exception];
[self.jsExecutor.registry onException:exception source:source];
}
});
return ret;
}
- (DoricAsyncResult *)destroyContext:(NSString *)contextId {
DoricAsyncResult *ret = [[DoricAsyncResult alloc] init];
__weak typeof(self) _self = self;
dispatch_async(self.jsExecutor.jsQueue, ^() {
__strong typeof(_self) self = _self;
if (!self) return;
@try {
[self.jsExecutor destroyContext:contextId];
[ret setupResult:@YES];
} @catch (NSException *exception) {
[ret setupError:exception];
[self.jsExecutor.registry onException:exception source:[self aliasWithContextId:contextId]];
}
});
return ret;
}
- (void)ensureSyncInMainQueue:(dispatch_block_t)block {
if (strcmp(dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL), dispatch_queue_get_label(dispatch_get_main_queue())) == 0) {
block();
} else {
dispatch_async(dispatch_get_main_queue(), block);
}
}
@end

View File

@@ -0,0 +1,34 @@
/*
* 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.
*/
//
// DoricDebugJSEngine.h
// Doric
//
// Created by jingpeng.wang on 2020/2/25.
//
#import <Foundation/Foundation.h>
#import <JavaScriptCore/JavaScriptCore.h>
#import "DoricJSEngine.h"
NS_ASSUME_NONNULL_BEGIN
@interface DoricDebugJSEngine : DoricJSEngine
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,41 @@
/*
* 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.
*/
//
// DoricDebugJSEngine.m
// Doric
//
// Created by jingpeng.wang on 2020/2/25.
//
#import "DoricDebugJSEngine.h"
#import "DoricJSRemoteExecutor.h"
@interface DoricDebugJSEngine ()
@end
@implementation DoricDebugJSEngine
- (instancetype)init {
if (self = [super init]) {
}
return self;
}
- (void)initJSEngine {
self.jsExecutor = [[DoricJSRemoteExecutor alloc] init];
}
@end

View File

@@ -0,0 +1,35 @@
/*
* 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.
*/
//
// DoricJSRemoteExecutor.h
// Pods
//
// Created by 王劲鹏 on 2019/10/31.
//
#import <Foundation/Foundation.h>
#import "DoricJSExecutorProtocol.h"
NS_ASSUME_NONNULL_BEGIN
@interface DoricJSRemoteExecutor : NSObject <DoricJSExecutorProtocol>
@property(nonatomic, strong) dispatch_semaphore_t semaphore;
- (void)close;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,207 @@
/*
* 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.
*/
//
// DoricJSRemoteExecutor.m
// Doric
//
// Created by on 2019/10/31.
//
#import "DoricJSRemoteExecutor.h"
#import <SocketRocket/SRWebSocket.h>
#import "DoricUtil.h"
#import "DoricJSRemoteArgType.h"
#import "NSString+JsonString.h"
static NSString * const kUrlStr = @"ws://192.168.24.240:2080";
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 DoricJSRemoteExecutor () <SRWebSocketDelegate>
@property(nonatomic, strong) SRWebSocket *srWebSocket;
@property(nonatomic, strong) NSMutableDictionary <NSString *, id> *blockMDic;
@property(nonatomic, strong) JSValue *temp;
@end
@implementation DoricJSRemoteExecutor
- (instancetype)init {
if (self = [super init]) {
[self srWebSocket];
_semaphore = dispatch_semaphore_create(0);
DC_LOCK(self.semaphore);
}
return self;
}
#pragma mark - SRWebSocketDelegate
- (void)webSocketDidOpen:(SRWebSocket *)webSocket {
DoricLog(@"debugger webSocketDidOpen");
DC_UNLOCK(self.semaphore);
}
- (void)webSocket:(SRWebSocket *)webSocket didReceivePong:(NSData *)pongPayload {
DoricLog(@"debugger webSocketdidReceivePong");
}
- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message {
NSData *jsonData = [message dataUsingEncoding:NSUTF8StringEncoding];
NSError *err;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
error:&err];
if (err) {
DoricLog(@"debugger webSocketdidReceiveMessage parse error%@", err);
return;
}
NSString *cmd = [[dic valueForKey:@"cmd"] copy];
if ([cmd isEqualToString:@"injectGlobalJSFunction"]) {
NSString *name = [dic valueForKey:@"name"];
NSArray *argsArr = [dic valueForKey:@"arguments"];
NSMutableArray *argsMarr = [NSMutableArray new];
for (NSUInteger i = 0; i < argsArr.count; i++) {
[argsMarr addObject:argsArr[i]];
}
id result;
id tmpBlk = self.blockMDic[name];
if (argsArr.count == 0) {
result = ((Block0) tmpBlk)();
} else if (argsArr.count == 1) {
result = ((Block1) tmpBlk)(argsArr[0]);
} else if (argsArr.count == 2) {
result = ((Block2)tmpBlk)(argsArr[0], argsArr[1]);
} else if (argsArr.count == 3) {
result = ((Block3)tmpBlk)(argsArr[0], argsArr[1], argsArr[2]);
} else if (argsArr.count == 4) {
result = ((Block4)tmpBlk)(argsArr[0], argsArr[1], argsArr[2], argsArr[3]);
} else if (argsArr.count == 5) {
result = ((Block5)tmpBlk)(argsArr[0], argsArr[1], argsArr[2], argsArr[3], argsArr[4]);
}else {
DoricLog(@"error:args to more than 5. args:%@",argsArr);
}
} else if ([cmd isEqualToString:@"invokeMethod"]) {
@try {
self.temp = [JSValue valueWithObject:[dic valueForKey:@"result"] inContext:nil];
} @catch (NSException *exception) {
DoricLog(@"debugger ", NSStringFromSelector(_cmd), exception.reason);
} @finally {
DC_UNLOCK(self.semaphore);
}
}
}
- (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error {
DoricLog(@"debugger webSocketdidFailWithError");
DC_UNLOCK(self.semaphore);
}
- (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean {
DoricLog(@"debugger webSocketdidCloseWithCode");
}
#pragma mark - DoricJSExecutorProtocol
- (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;
}
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 {
NSMutableArray *argsMArr = [NSMutableArray new];
for (id arg in args) {
NSDictionary *dic = [self dicForArg:arg];
[argsMArr addObject:dic];
}
NSArray *argsArr = [argsMArr copy];
NSDictionary *jsonDic = @{
@"cmd": @"invokeMethod",
@"objectName": objName,
@"functionName": funcName,
@"values": argsArr
};
NSString *jsonStr = [NSString dc_convertToJsonWithDic:jsonDic];
if (!jsonStr) {
return nil;
}
[self.srWebSocket send:jsonStr];
DC_LOCK(self.semaphore);
return self.temp;
}
- (NSDictionary *)dicForArg:(id)arg {
DoricJSRemoteArgType type = DoricargTypeWithArg(arg);
if (type == DoricJSRemoteArgTypeObject || type == DoricJSRemoteArgTypeArray) {
NSString *jsonStr = [NSString dc_convertToJsonWithDic:(NSDictionary *)arg];
arg = jsonStr;
}
NSDictionary *dic = @{
@"type": @(type),
@"value": arg
};
return dic;
}
- (void)close {
[self.srWebSocket close];
}
#pragma mark - Properties
- (SRWebSocket *)srWebSocket {
if (!_srWebSocket) {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:kUrlStr] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10];
_srWebSocket = [[SRWebSocket alloc] initWithURLRequest:request];
_srWebSocket.delegate = self;
[_srWebSocket open];
}
return _srWebSocket;
}
- (NSMutableDictionary *)blockMDic {
if (!_blockMDic) {
_blockMDic = [NSMutableDictionary new];
}
return _blockMDic;
}
@end