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/DoricDebugDriver.m

170 lines
5.4 KiB
Mathematica
Raw Normal View History

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
//
// DoricDebugDriver.m
2019-07-26 14:19:42 +08:00
// Doric
//
// Created by jingpeng.wang on 2020/2/25.
2019-07-26 14:19:42 +08:00
//
#import "DoricDebugDriver.h"
#import "DoricDebugJSEngine.h"
2019-07-27 14:36:41 +08:00
#import "DoricConstant.h"
#import "DoricContextManager.h"
2019-07-26 14:19:42 +08:00
@interface DoricDebugDriver ()
2019-10-12 14:48:19 +08:00
@property(nonatomic, strong) DoricJSEngine *jsExecutor;
2019-07-26 14:19:42 +08:00
@end
@implementation DoricDebugDriver
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]) {
_jsExecutor = [[DoricDebugJSEngine alloc] init];
2019-07-29 13:48:27 +08:00
}
return self;
}
- (DoricRegistry *)registry {
2019-07-27 14:36:41 +08:00
return self.jsExecutor.registry;
}
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-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;
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];
}
2019-07-27 14:36:41 +08:00
__weak typeof(self) _self = self;
2019-10-12 14:48:19 +08:00
dispatch_async(self.jsExecutor.jsQueue, ^() {
2019-07-27 14:36:41 +08:00
__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-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
DoricAsyncResult *ret = [[DoricAsyncResult alloc] init];
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 *);
}
__weak typeof(self) _self = self;
2019-10-12 14:48:19 +08:00
dispatch_async(self.jsExecutor.jsQueue, ^() {
2019-07-27 14:36:41 +08:00
__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-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];
2019-10-12 14:48:19 +08:00
for (id arg in args) {
[array addObject:arg];
2019-07-30 21:05:27 +08:00
}
__weak typeof(self) _self = self;
2019-10-12 14:48:19 +08:00
dispatch_async(self.jsExecutor.jsQueue, ^() {
2019-07-30 21:05:27 +08:00
__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];
}
});
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];
__weak typeof(self) _self = self;
2019-10-12 14:48:19 +08:00
dispatch_async(self.jsExecutor.jsQueue, ^() {
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 {
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];
2019-07-27 14:36:41 +08:00
} @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;
2019-10-12 14:48:19 +08:00
dispatch_async(self.jsExecutor.jsQueue, ^() {
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 {
2019-07-27 14:36:41 +08:00
[self.jsExecutor destroyContext:contextId];
2020-01-11 11:01:38 +08:00
[ret setupResult:@YES];
2019-07-27 14:36:41 +08:00
} @catch (NSException *exception) {
[ret setupError:exception];
}
});
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);
}
}
2019-07-26 14:19:42 +08:00
@end