From 08bfd71eae6edd1ebc23aa8df57c3212e897a081 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8A=B2=E9=B9=8F?= Date: Thu, 31 Oct 2019 14:26:08 +0800 Subject: [PATCH] add doric remote js executor on ios --- iOS/Pod/Classes/Engine/DoricJSEngine.m | 3 +- .../Classes/Engine/DoricJSRemoteExecutor.h | 32 ++++++++++ .../Classes/Engine/DoricJSRemoteExecutor.m | 63 +++++++++++++++++++ 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 iOS/Pod/Classes/Engine/DoricJSRemoteExecutor.h create mode 100644 iOS/Pod/Classes/Engine/DoricJSRemoteExecutor.m diff --git a/iOS/Pod/Classes/Engine/DoricJSEngine.m b/iOS/Pod/Classes/Engine/DoricJSEngine.m index 146d6148..7d6928d3 100644 --- a/iOS/Pod/Classes/Engine/DoricJSEngine.m +++ b/iOS/Pod/Classes/Engine/DoricJSEngine.m @@ -23,6 +23,7 @@ #import "DoricJSEngine.h" #import "DoricJSExecutorProtocal.h" #import "DoricJSCoreExecutor.h" +#import "DoricJSRemoteExecutor.h" #import "DoricConstant.h" #import "DoricUtil.h" #import "DoricBridgeExtension.h" @@ -41,7 +42,7 @@ - (instancetype)init { _bridgeExtension = [[DoricBridgeExtension alloc] init]; dispatch_async(_jsQueue, ^() { self.timers = [[NSMutableDictionary alloc] init]; - self.jsExecutor = [[DoricJSCoreExecutor alloc] init]; + self.jsExecutor = [[DoricJSRemoteExecutor alloc] init]; self.registry = [[DoricRegistry alloc] init]; [self initJSExecutor]; [self initDoricEnvironment]; diff --git a/iOS/Pod/Classes/Engine/DoricJSRemoteExecutor.h b/iOS/Pod/Classes/Engine/DoricJSRemoteExecutor.h new file mode 100644 index 00000000..f41bd5df --- /dev/null +++ b/iOS/Pod/Classes/Engine/DoricJSRemoteExecutor.h @@ -0,0 +1,32 @@ +/* +* 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 +#import "DoricJSExecutorProtocal.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface DoricJSRemoteExecutor : NSObject + +@end + +NS_ASSUME_NONNULL_END diff --git a/iOS/Pod/Classes/Engine/DoricJSRemoteExecutor.m b/iOS/Pod/Classes/Engine/DoricJSRemoteExecutor.m new file mode 100644 index 00000000..8ecc1f83 --- /dev/null +++ b/iOS/Pod/Classes/Engine/DoricJSRemoteExecutor.m @@ -0,0 +1,63 @@ +/* +* 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" + +@interface DoricJSRemoteExecutor () + +@property(nonatomic, strong) JSContext *jsContext; + +@end + +@implementation DoricJSRemoteExecutor +- (instancetype)init { + if (self = [super init]) { + _jsContext = [[JSContext alloc] init]; + } + return self; +} + +- (void)checkJSException { + if (self.jsContext.exception) { + NSString *errMsg = [NSString stringWithFormat:@"%@ (line %@ in the generated bundle)\n/***StackTrace***/\n%@/***StackTrace***/", self.jsContext.exception, self.jsContext.exception[@"line"], self.jsContext.exception[@"stack"]]; + @throw [[NSException alloc] initWithName:@"DoricJS" reason:errMsg userInfo:nil]; + } +} + +- (NSString *)loadJSScript:(NSString *)script source:(NSString *)source { + NSString *ret = [[self.jsContext evaluateScript:script withSourceURL:[NSURL URLWithString:source]] toString]; + [self checkJSException]; + return ret; +} + +- (void)injectGlobalJSObject:(NSString *)name obj:(id)obj { + self.jsContext[name] = obj; + [self checkJSException]; +} + +- (JSValue *)invokeObject:(NSString *)objName method:(NSString *)funcName args:(NSArray *)args { + JSValue *obj = [self.jsContext objectForKeyedSubscript:objName]; + JSValue *ret = [obj invokeMethod:funcName withArguments:args]; + [self checkJSException]; + return ret; +} + +@end