diff --git a/doric-iOS/Devkit/Classes/DoricDebugDriver.h b/doric-iOS/Devkit/Classes/DoricDebugDriver.h new file mode 100644 index 00000000..21e64d38 --- /dev/null +++ b/doric-iOS/Devkit/Classes/DoricDebugDriver.h @@ -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 +#import "DoricDriverProtocol.h" + +typedef NS_ENUM(NSInteger, DoricQueueMode) { + JS = 0, + UI, + INDEPENDENT +}; + +NS_ASSUME_NONNULL_BEGIN + +@interface DoricDebugDriver : NSObject ++ (instancetype)instance; +@end + +NS_ASSUME_NONNULL_END diff --git a/doric-iOS/Devkit/Classes/DoricDebugDriver.m b/doric-iOS/Devkit/Classes/DoricDebugDriver.m new file mode 100644 index 00000000..87506ae6 --- /dev/null +++ b/doric-iOS/Devkit/Classes/DoricDebugDriver.m @@ -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 *)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 *)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 *)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 diff --git a/doric-iOS/Devkit/Classes/DoricDebugJSEngine.h b/doric-iOS/Devkit/Classes/DoricDebugJSEngine.h new file mode 100644 index 00000000..b83ad5ea --- /dev/null +++ b/doric-iOS/Devkit/Classes/DoricDebugJSEngine.h @@ -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 +#import + +#import "DoricJSEngine.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface DoricDebugJSEngine : DoricJSEngine + +@end + +NS_ASSUME_NONNULL_END diff --git a/doric-iOS/Devkit/Classes/DoricDebugJSEngine.m b/doric-iOS/Devkit/Classes/DoricDebugJSEngine.m new file mode 100644 index 00000000..3fa2fcac --- /dev/null +++ b/doric-iOS/Devkit/Classes/DoricDebugJSEngine.m @@ -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 diff --git a/doric-iOS/Pod/Classes/Engine/DoricJSRemoteExecutor.h b/doric-iOS/Devkit/Classes/DoricJSRemoteExecutor.h similarity index 100% rename from doric-iOS/Pod/Classes/Engine/DoricJSRemoteExecutor.h rename to doric-iOS/Devkit/Classes/DoricJSRemoteExecutor.h diff --git a/doric-iOS/Pod/Classes/Engine/DoricJSRemoteExecutor.m b/doric-iOS/Devkit/Classes/DoricJSRemoteExecutor.m similarity index 100% rename from doric-iOS/Pod/Classes/Engine/DoricJSRemoteExecutor.m rename to doric-iOS/Devkit/Classes/DoricJSRemoteExecutor.m diff --git a/doric-iOS/Pod/Classes/DoricContext.h b/doric-iOS/Pod/Classes/DoricContext.h index 01ce3bba..d94efaf6 100644 --- a/doric-iOS/Pod/Classes/DoricContext.h +++ b/doric-iOS/Pod/Classes/DoricContext.h @@ -21,7 +21,7 @@ // #import -#import "DoricDriver.h" +#import "DoricDriverProtocol.h" #import "DoricNavigatorDelegate.h" #import "DoricNavBarDelegate.h" @@ -34,7 +34,7 @@ NS_ASSUME_NONNULL_BEGIN @property(nonatomic, weak) id navigator; @property(nonatomic, weak) id navBar; @property(nonatomic, strong) NSString *contextId; -@property(nonatomic, strong) DoricDriver *driver; +@property(nonatomic, strong) id driver; @property(nonatomic, strong) NSMutableDictionary *pluginInstanceMap; @property(nonatomic, strong) NSString *source; @property(nonatomic, strong) NSString *script;; diff --git a/doric-iOS/Pod/Classes/DoricContext.m b/doric-iOS/Pod/Classes/DoricContext.m index 56e15d8a..9fa4f426 100644 --- a/doric-iOS/Pod/Classes/DoricContext.m +++ b/doric-iOS/Pod/Classes/DoricContext.m @@ -25,12 +25,13 @@ #import "DoricRootNode.h" #import "DoricConstant.h" #import "DoricExtensions.h" +#import "DoricNativeDriver.h" @implementation DoricContext - (instancetype)initWithScript:(NSString *)script source:(NSString *)source extra:(NSString *)extra { if (self = [super init]) { - _driver = [DoricDriver instance]; + _driver = [DoricNativeDriver instance]; _pluginInstanceMap = [NSMutableDictionary new]; [[DoricContextManager instance] createContext:self script:script source:source]; _headNodes = [NSMutableDictionary new]; diff --git a/doric-iOS/Pod/Classes/DoricDriver.h b/doric-iOS/Pod/Classes/DoricDriverProtocol.h similarity index 85% rename from doric-iOS/Pod/Classes/DoricDriver.h rename to doric-iOS/Pod/Classes/DoricDriverProtocol.h index 6f9c7e56..1c13afb0 100644 --- a/doric-iOS/Pod/Classes/DoricDriver.h +++ b/doric-iOS/Pod/Classes/DoricDriverProtocol.h @@ -14,26 +14,17 @@ * limitations under the License. */ // -// DoricDriver.h +// DoricDriverProtocol.h // Doric // -// Created by pengfei.zhou on 2019/7/26. +// Created by jingpeng.wang on 2020/2/25. // #import #import "DoricAsyncResult.h" #import "DoricRegistry.h" -typedef NS_ENUM(NSInteger, DoricQueueMode) { - JS = 0, - UI, - INDEPENDENT -}; - -NS_ASSUME_NONNULL_BEGIN - -@interface DoricDriver : NSObject -+ (instancetype)instance; +@protocol DoricDriverProtocol @property(nonatomic, strong) DoricRegistry *registry; @@ -52,6 +43,5 @@ NS_ASSUME_NONNULL_BEGIN - (void)ensureSyncInMainQueue:(dispatch_block_t)block; - (NSString *)aliasWithContextId:(NSString *)contextId; -@end -NS_ASSUME_NONNULL_END +@end diff --git a/doric-iOS/Pod/Classes/DoricNativeDriver.h b/doric-iOS/Pod/Classes/DoricNativeDriver.h new file mode 100644 index 00000000..72850603 --- /dev/null +++ b/doric-iOS/Pod/Classes/DoricNativeDriver.h @@ -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. + */ +// +// DoricNativeDriver.h +// Doric +// +// Created by pengfei.zhou on 2019/7/26. +// + +#import +#import "DoricDriverProtocol.h" + +typedef NS_ENUM(NSInteger, DoricQueueMode) { + JS = 0, + UI, + INDEPENDENT +}; + +NS_ASSUME_NONNULL_BEGIN + +@interface DoricNativeDriver : NSObject ++ (instancetype)instance; +@end + +NS_ASSUME_NONNULL_END diff --git a/doric-iOS/Pod/Classes/DoricDriver.m b/doric-iOS/Pod/Classes/DoricNativeDriver.m similarity index 96% rename from doric-iOS/Pod/Classes/DoricDriver.m rename to doric-iOS/Pod/Classes/DoricNativeDriver.m index 37b478f9..dfe6e472 100644 --- a/doric-iOS/Pod/Classes/DoricDriver.m +++ b/doric-iOS/Pod/Classes/DoricNativeDriver.m @@ -14,22 +14,22 @@ * limitations under the License. */ // -// DoricDriver.m +// DoricNativeDriver.m // Doric // // Created by pengfei.zhou on 2019/7/26. // -#import "DoricDriver.h" +#import "DoricNativeDriver.h" #import "DoricJSEngine.h" #import "DoricConstant.h" #import "DoricContextManager.h" -@interface DoricDriver () +@interface DoricNativeDriver () @property(nonatomic, strong) DoricJSEngine *jsExecutor; @end -@implementation DoricDriver +@implementation DoricNativeDriver @dynamic registry; @@ -45,10 +45,10 @@ - (DoricRegistry *)registry { } + (instancetype)instance { - static DoricDriver *_instance; + static DoricNativeDriver *_instance; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ - _instance = [[DoricDriver alloc] init]; + _instance = [[DoricNativeDriver alloc] init]; }); return _instance; } diff --git a/doric-iOS/Pod/Classes/Engine/DoricJSEngine.h b/doric-iOS/Pod/Classes/Engine/DoricJSEngine.h index 94404064..cdd954bf 100644 --- a/doric-iOS/Pod/Classes/Engine/DoricJSEngine.h +++ b/doric-iOS/Pod/Classes/Engine/DoricJSEngine.h @@ -22,12 +22,16 @@ #import #import + #import "DoricRegistry.h" +#import "DoricJSExecutorProtocol.h" NS_ASSUME_NONNULL_BEGIN @interface DoricJSEngine : NSObject +@property(nonatomic, strong) id jsExecutor; + @property(nonatomic, strong) dispatch_queue_t jsQueue; @property(nonatomic, strong) DoricRegistry *registry; diff --git a/doric-iOS/Pod/Classes/Engine/DoricJSEngine.m b/doric-iOS/Pod/Classes/Engine/DoricJSEngine.m index 682ebbc2..0d3f0eee 100644 --- a/doric-iOS/Pod/Classes/Engine/DoricJSEngine.m +++ b/doric-iOS/Pod/Classes/Engine/DoricJSEngine.m @@ -21,14 +21,12 @@ // #import "DoricJSEngine.h" -#import "DoricJSExecutorProtocol.h" #import "DoricJSCoreExecutor.h" #import "DoricConstant.h" #import "DoricUtil.h" #import "DoricBridgeExtension.h" @interface DoricJSEngine () -@property(nonatomic, strong) id jsExecutor; @property(nonatomic, strong) NSMutableDictionary *timers; @property(nonatomic, strong) DoricBridgeExtension *bridgeExtension; @end @@ -41,9 +39,7 @@ - (instancetype)init { _bridgeExtension = [[DoricBridgeExtension alloc] init]; dispatch_async(_jsQueue, ^() { self.timers = [[NSMutableDictionary alloc] init]; - // Debug: 切换 - // self.jsExecutor = [[DoricJSRemoteExecutor alloc] init]; - self.jsExecutor = [DoricJSCoreExecutor new]; + [self initJSEngine]; self.registry = [[DoricRegistry alloc] init]; [self initJSExecutor]; [self initDoricEnvironment]; @@ -52,6 +48,10 @@ - (instancetype)init { return self; } +- (void)initJSEngine { + self.jsExecutor = [DoricJSCoreExecutor new]; +} + - (void)initJSExecutor { __weak typeof(self) _self = self; NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];