2019-07-26 14:19:42 +08:00
|
|
|
//
|
|
|
|
// DoricDriver.m
|
|
|
|
// Doric
|
|
|
|
//
|
|
|
|
// Created by pengfei.zhou on 2019/7/26.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "DoricDriver.h"
|
2019-07-27 14:36:41 +08:00
|
|
|
#import "DoricJSEngine.h"
|
|
|
|
#import "DoricConstant.h"
|
2019-07-26 14:19:42 +08:00
|
|
|
|
|
|
|
@interface DoricDriver()
|
2019-07-27 14:36:41 +08:00
|
|
|
@property (nonatomic, strong) DoricJSEngine *jsExecutor;
|
2019-07-26 14:19:42 +08:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation DoricDriver
|
|
|
|
|
2019-07-27 14:36:41 +08:00
|
|
|
@dynamic registry;
|
|
|
|
|
2019-07-29 13:48:27 +08:00
|
|
|
- (instancetype)init {
|
|
|
|
if(self = [super init]){
|
|
|
|
_jsExecutor = [[DoricJSEngine alloc] init];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (DoricRegistry *)registry {
|
2019-07-27 14:36:41 +08:00
|
|
|
return self.jsExecutor.registry;
|
|
|
|
}
|
|
|
|
|
2019-07-26 14:19:42 +08:00
|
|
|
+ (instancetype)instance{
|
|
|
|
static DoricDriver *_instance;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
_instance = [[DoricDriver alloc] init];
|
|
|
|
});
|
|
|
|
return _instance;
|
|
|
|
}
|
|
|
|
|
2019-07-29 13:48:27 +08:00
|
|
|
- (DoricAsyncResult<JSValue *> *)invokeDoricMethod:(NSString *)method, ... {
|
2019-07-27 14:36:41 +08:00
|
|
|
va_list args;
|
|
|
|
va_start(args, method);
|
|
|
|
DoricAsyncResult *ret = [self invokeDoricMethod:method arguments:args];
|
|
|
|
va_end(args);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-07-29 13:48:27 +08:00
|
|
|
- (DoricAsyncResult<JSValue *> *)invokeDoricMethod:(NSString *)method arguments:(va_list)args {
|
2019-07-27 14:36:41 +08:00
|
|
|
DoricAsyncResult *ret = [[DoricAsyncResult alloc] init];
|
2019-07-30 11:25:05 +08:00
|
|
|
NSMutableArray *array = [[NSMutableArray alloc] init];
|
|
|
|
id arg;
|
|
|
|
while((arg = va_arg(args, id)) != nil){
|
|
|
|
[array addObject:arg];
|
|
|
|
}
|
2019-07-27 14:36:41 +08:00
|
|
|
__weak typeof(self) _self = self;
|
|
|
|
dispatch_async(self.jsExecutor.jsQueue, ^(){
|
|
|
|
__strong typeof(_self) self = _self;
|
|
|
|
if (!self) return;
|
|
|
|
@try {
|
2019-07-30 11:25:05 +08:00
|
|
|
JSValue *jsValue = [self.jsExecutor invokeDoricMethod:method argumentsArray:array];
|
2019-07-27 14:36:41 +08:00
|
|
|
[ret setupResult:jsValue];
|
|
|
|
} @catch (NSException *exception) {
|
|
|
|
[ret setupError:exception];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-07-29 13:48:27 +08:00
|
|
|
- (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 {
|
2019-07-27 14:36:41 +08:00
|
|
|
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) {
|
2019-07-29 13:48:27 +08:00
|
|
|
[ret setupError:exception];
|
2019-07-27 14:36:41 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-07-29 13:48:27 +08:00
|
|
|
- (DoricAsyncResult *)createContext:(NSString *)contextId script:(NSString *)script source:(NSString *)source {
|
2019-07-27 14:36:41 +08:00
|
|
|
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:[NSNumber numberWithBool:YES]];
|
|
|
|
} @catch (NSException *exception) {
|
|
|
|
[ret setupError:exception];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-07-29 13:48:27 +08:00
|
|
|
- (DoricAsyncResult *)destroyContext:(NSString *)contextId {
|
2019-07-27 14:36:41 +08:00
|
|
|
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:[NSNumber numberWithBool:YES]];
|
|
|
|
} @catch (NSException *exception) {
|
|
|
|
[ret setupError:exception];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-26 14:19:42 +08:00
|
|
|
@end
|