2019-10-21 09:59:22 +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.
|
|
|
|
*/
|
2019-07-26 14:19:42 +08:00
|
|
|
//
|
2020-02-26 11:50:18 +08:00
|
|
|
// DoricNativeDriver.m
|
2019-07-26 14:19:42 +08:00
|
|
|
// Doric
|
|
|
|
//
|
|
|
|
// Created by pengfei.zhou on 2019/7/26.
|
|
|
|
//
|
|
|
|
|
2020-02-26 11:50:18 +08:00
|
|
|
#import "DoricNativeDriver.h"
|
2019-07-27 14:36:41 +08:00
|
|
|
#import "DoricJSEngine.h"
|
|
|
|
#import "DoricConstant.h"
|
2020-02-22 15:37:35 +08:00
|
|
|
#import "DoricContextManager.h"
|
2021-03-29 17:53:27 +08:00
|
|
|
#import "DoricPerformanceProfile.h"
|
2019-07-26 14:19:42 +08:00
|
|
|
|
2020-02-26 11:50:18 +08:00
|
|
|
@interface DoricNativeDriver ()
|
2019-10-12 14:48:19 +08:00
|
|
|
@property(nonatomic, strong) DoricJSEngine *jsExecutor;
|
2019-07-26 14:19:42 +08:00
|
|
|
@end
|
|
|
|
|
2020-02-26 11:50:18 +08:00
|
|
|
@implementation DoricNativeDriver
|
2019-07-26 14:19:42 +08:00
|
|
|
|
2019-07-27 14:36:41 +08:00
|
|
|
@dynamic registry;
|
|
|
|
|
2019-07-29 13:48:27 +08:00
|
|
|
- (instancetype)init {
|
2019-10-12 14:48:19 +08:00
|
|
|
if (self = [super init]) {
|
2019-07-29 13:48:27 +08:00
|
|
|
_jsExecutor = [[DoricJSEngine alloc] init];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (DoricRegistry *)registry {
|
2019-07-27 14:36:41 +08:00
|
|
|
return self.jsExecutor.registry;
|
|
|
|
}
|
|
|
|
|
2019-10-12 14:48:19 +08:00
|
|
|
+ (instancetype)instance {
|
2020-02-26 11:50:18 +08:00
|
|
|
static DoricNativeDriver *_instance;
|
2019-07-26 14:19:42 +08:00
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
2020-02-26 11:50:18 +08:00
|
|
|
_instance = [[DoricNativeDriver alloc] init];
|
2019-07-26 14:19:42 +08:00
|
|
|
});
|
|
|
|
return _instance;
|
|
|
|
}
|
|
|
|
|
2020-04-20 11:03:48 +08:00
|
|
|
- (DoricAsyncResult *)invokeDoricMethod:(NSString *)method argumentsArray:(NSArray *)args {
|
2021-07-21 15:04:55 +08:00
|
|
|
id contextId = args.count > 0 ? args[0] : nil;
|
|
|
|
DoricPerformanceProfile *profile = nil;
|
|
|
|
if ([contextId isKindOfClass:NSString.class]) {
|
|
|
|
profile = [DoricContextManager.instance getContext:contextId].performanceProfile;
|
|
|
|
}
|
|
|
|
NSMutableArray *printedArgs = [[NSMutableArray alloc] init];
|
|
|
|
for (id arg in args) {
|
|
|
|
if (arg == contextId) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
[printedArgs addObject:arg];
|
|
|
|
}
|
|
|
|
NSString *anchorName = [NSString stringWithFormat:@"Call:%@", [printedArgs componentsJoinedByString:@","]];
|
|
|
|
[profile prepare:anchorName];
|
2020-04-20 11:03:48 +08:00
|
|
|
DoricAsyncResult *ret = [[DoricAsyncResult alloc] init];
|
|
|
|
__weak typeof(self) _self = self;
|
2020-05-13 10:02:22 +08:00
|
|
|
[self.jsExecutor ensureRunOnJSThread:^{
|
2020-04-20 11:03:48 +08:00
|
|
|
__strong typeof(_self) self = _self;
|
|
|
|
if (!self) return;
|
|
|
|
@try {
|
2021-07-21 15:04:55 +08:00
|
|
|
[profile start:anchorName];
|
2020-04-20 11:03:48 +08:00
|
|
|
JSValue *jsValue = [self.jsExecutor invokeDoricMethod:method argumentsArray:args];
|
2021-07-21 15:04:55 +08:00
|
|
|
[profile end:anchorName];
|
2020-04-20 11:03:48 +08:00
|
|
|
[ret setupResult:jsValue];
|
|
|
|
} @catch (NSException *exception) {
|
|
|
|
[ret setupError:exception];
|
|
|
|
}
|
2020-05-13 10:02:22 +08:00
|
|
|
}];
|
2020-04-20 11:03:48 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-10-12 14:48:19 +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-10-12 14:48:19 +08:00
|
|
|
- (DoricAsyncResult<JSValue *> *)invokeDoricMethod:(NSString *)method arguments:(va_list)args {
|
2019-07-30 11:25:05 +08:00
|
|
|
NSMutableArray *array = [[NSMutableArray alloc] init];
|
|
|
|
id arg;
|
2019-10-12 14:48:19 +08:00
|
|
|
while ((arg = va_arg(args, id)) != nil) {
|
2019-07-30 11:25:05 +08:00
|
|
|
[array addObject:arg];
|
|
|
|
}
|
2021-07-21 15:04:55 +08:00
|
|
|
return [self invokeDoricMethod:method argumentsArray:array];
|
2019-07-27 14:36:41 +08:00
|
|
|
}
|
|
|
|
|
2019-10-12 14:48:19 +08:00
|
|
|
- (DoricAsyncResult<JSValue *> *)invokeContextEntity:(NSString *)contextId method:(NSString *)method, ... {
|
2019-07-29 13:48:27 +08:00
|
|
|
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
|
|
|
NSMutableArray *array = [[NSMutableArray alloc] init];
|
|
|
|
[array addObject:contextId];
|
|
|
|
[array addObject:method];
|
|
|
|
id arg = va_arg(args, id);
|
2019-10-12 14:48:19 +08:00
|
|
|
while (arg != nil) {
|
2019-07-27 14:36:41 +08:00
|
|
|
[array addObject:arg];
|
|
|
|
arg = va_arg(args, JSValue *);
|
|
|
|
}
|
2021-07-21 15:04:55 +08:00
|
|
|
return [self invokeContextEntity:contextId method:method argumentsArray:array];
|
2019-07-27 14:36:41 +08:00
|
|
|
}
|
2019-10-12 14:48:19 +08:00
|
|
|
|
2019-07-30 21:05:27 +08:00
|
|
|
- (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];
|
2021-07-21 15:04:55 +08:00
|
|
|
[array addObjectsFromArray:args];
|
|
|
|
DoricAsyncResult *result = [self invokeDoricMethod:DORIC_CONTEXT_INVOKE argumentsArray:array];
|
|
|
|
result.resultCallback = ^(id result) {
|
|
|
|
[ret setupResult:result];
|
|
|
|
};
|
|
|
|
result.exceptionCallback = ^(NSException *e) {
|
|
|
|
[ret setupError:e];
|
|
|
|
[self.jsExecutor.registry onException:e inContext:[[DoricContextManager instance] getContext:contextId]];
|
|
|
|
};
|
2019-07-30 21:05:27 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2019-07-27 14:36:41 +08:00
|
|
|
|
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];
|
2021-07-08 12:46:15 +08:00
|
|
|
DoricPerformanceProfile *performanceProfile = [DoricContextManager.instance getContext:contextId].performanceProfile
|
|
|
|
?: [[DoricPerformanceProfile alloc] initWithName:contextId];
|
2021-03-29 17:53:27 +08:00
|
|
|
[performanceProfile prepare:@"Create"];
|
2019-07-27 14:36:41 +08:00
|
|
|
__weak typeof(self) _self = self;
|
2020-05-13 10:02:22 +08:00
|
|
|
[self.jsExecutor ensureRunOnJSThread:^{
|
2019-07-27 14:36:41 +08:00
|
|
|
__strong typeof(_self) self = _self;
|
2019-10-12 14:48:19 +08:00
|
|
|
if (!self) return;
|
|
|
|
@try {
|
2021-03-29 17:53:27 +08:00
|
|
|
[performanceProfile start:@"Create"];
|
2019-07-27 14:36:41 +08:00
|
|
|
[self.jsExecutor prepareContext:contextId script:script source:source];
|
2020-01-11 11:01:38 +08:00
|
|
|
[ret setupResult:@YES];
|
2021-03-29 17:53:27 +08:00
|
|
|
[performanceProfile end:@"Create"];
|
2019-07-27 14:36:41 +08:00
|
|
|
} @catch (NSException *exception) {
|
|
|
|
[ret setupError:exception];
|
2020-03-07 10:38:42 +08:00
|
|
|
[self.registry onException:exception inContext:[[DoricContextManager instance] getContext:contextId]];
|
2019-07-27 14:36:41 +08:00
|
|
|
}
|
2020-05-13 10:02:22 +08:00
|
|
|
}];
|
2019-07-27 14:36:41 +08:00
|
|
|
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];
|
2021-07-08 12:46:15 +08:00
|
|
|
DoricPerformanceProfile *performanceProfile = [DoricContextManager.instance getContext:contextId].performanceProfile
|
|
|
|
?: [[DoricPerformanceProfile alloc] initWithName:contextId];
|
2021-03-29 17:53:27 +08:00
|
|
|
[performanceProfile prepare:@"Destroy"];
|
2019-07-27 14:36:41 +08:00
|
|
|
__weak typeof(self) _self = self;
|
2020-05-13 10:02:22 +08:00
|
|
|
[self.jsExecutor ensureRunOnJSThread:^{
|
2019-07-27 14:36:41 +08:00
|
|
|
__strong typeof(_self) self = _self;
|
2019-10-12 14:48:19 +08:00
|
|
|
if (!self) return;
|
|
|
|
@try {
|
2021-03-29 17:53:27 +08:00
|
|
|
[performanceProfile start:@"Destroy"];
|
2019-07-27 14:36:41 +08:00
|
|
|
[self.jsExecutor destroyContext:contextId];
|
2020-01-11 11:01:38 +08:00
|
|
|
[ret setupResult:@YES];
|
2021-03-29 17:53:27 +08:00
|
|
|
[performanceProfile end:@"Destroy"];
|
2019-07-27 14:36:41 +08:00
|
|
|
} @catch (NSException *exception) {
|
|
|
|
[ret setupError:exception];
|
2020-03-07 10:38:42 +08:00
|
|
|
[self.jsExecutor.registry onException:exception inContext:[[DoricContextManager instance] getContext:contextId]];
|
2019-07-27 14:36:41 +08:00
|
|
|
}
|
2020-05-13 10:02:22 +08:00
|
|
|
}];
|
2019-07-27 14:36:41 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-11-19 16:15:54 +08:00
|
|
|
- (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);
|
|
|
|
}
|
|
|
|
}
|
2021-03-04 10:02:29 +08:00
|
|
|
|
|
|
|
- (void)dealloc {
|
|
|
|
[self.jsExecutor teardown];
|
|
|
|
}
|
2019-07-26 14:19:42 +08:00
|
|
|
@end
|