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/iOS/Pod/Classes/Engine/DoricJSEngine.m

182 lines
7.7 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
//
// DoricJSEngine.m
// Doric
//
// Created by pengfei.zhou on 2019/7/26.
//
#import "DoricJSEngine.h"
2019-11-23 17:14:50 +08:00
#import "DoricJSExecutorProtocol.h"
2019-07-26 14:19:42 +08:00
#import "DoricJSCoreExecutor.h"
2019-10-31 14:26:08 +08:00
#import "DoricJSRemoteExecutor.h"
2019-07-26 14:19:42 +08:00
#import "DoricConstant.h"
#import "DoricUtil.h"
2019-07-29 20:51:53 +08:00
#import "DoricBridgeExtension.h"
2019-07-26 14:19:42 +08:00
2019-10-12 14:48:19 +08:00
@interface DoricJSEngine ()
2019-11-23 17:14:50 +08:00
@property(nonatomic, strong) id <DoricJSExecutorProtocol> jsExecutor;
2019-10-12 14:48:19 +08:00
@property(nonatomic, strong) NSMutableDictionary *timers;
@property(nonatomic, strong) DoricBridgeExtension *bridgeExtension;
2019-07-26 14:19:42 +08:00
@end
@implementation DoricJSEngine
2019-07-29 13:48:27 +08:00
- (instancetype)init {
2019-10-12 14:48:19 +08:00
if (self = [super init]) {
2019-07-26 14:19:42 +08:00
_jsQueue = dispatch_queue_create("doric.jsengine", DISPATCH_QUEUE_SERIAL);
2019-07-29 20:51:53 +08:00
_bridgeExtension = [[DoricBridgeExtension alloc] init];
2019-10-12 14:48:19 +08:00
dispatch_async(_jsQueue, ^() {
2019-07-27 19:16:02 +08:00
self.timers = [[NSMutableDictionary alloc] init];
2019-11-08 10:35:12 +08:00
// Debug:
// self.jsExecutor = [[DoricJSRemoteExecutor alloc] init];
self.jsExecutor = [DoricJSCoreExecutor new];
2019-07-27 14:36:41 +08:00
self.registry = [[DoricRegistry alloc] init];
2019-07-29 13:48:27 +08:00
[self initJSExecutor];
2019-07-26 14:19:42 +08:00
[self initDoricEnvironment];
});
}
return self;
}
2019-07-29 13:48:27 +08:00
- (void)initJSExecutor {
2019-07-27 14:36:41 +08:00
__weak typeof(self) _self = self;
2019-10-12 14:48:19 +08:00
[self.jsExecutor injectGlobalJSObject:INJECT_LOG obj:^(NSString *type, NSString *message) {
DoricLog(@"JS:%@", message);
2019-07-26 14:19:42 +08:00
}];
[self.jsExecutor injectGlobalJSObject:INJECT_EMPTY obj:^() {
}];
2019-10-12 14:48:19 +08:00
[self.jsExecutor injectGlobalJSObject:INJECT_REQUIRE obj:^(NSString *name) {
2019-07-27 14:36:41 +08:00
__strong typeof(_self) self = _self;
2019-10-12 14:48:19 +08:00
if (!self) return NO;
2019-07-27 14:36:41 +08:00
NSString *content = [self.registry acquireJSBundle:name];
2019-10-12 14:48:19 +08:00
if (!content) {
2019-07-27 14:36:41 +08:00
DoricLog(@"require js bundle:%@ is empty", name);
return NO;
}
2019-10-12 14:48:19 +08:00
@try {
2019-07-27 14:36:41 +08:00
[self.jsExecutor loadJSScript:[self packageModuleScript:name content:content]
source:[@"Module://" stringByAppendingString:name]];
2019-10-12 14:48:19 +08:00
} @catch (NSException *e) {
2019-07-27 14:36:41 +08:00
DoricLog(@"require js bundle:%@ error,for %@", name, e.reason);
}
return YES;
2019-07-26 14:19:42 +08:00
}];
2019-07-27 19:16:02 +08:00
[self.jsExecutor injectGlobalJSObject:INJECT_TIMER_SET
2019-10-12 14:48:19 +08:00
obj:^(NSNumber *timerId, NSNumber *interval, NSNumber *isInterval) {
__strong typeof(_self) self = _self;
2019-07-27 19:16:02 +08:00
NSString *timerId_str = [timerId stringValue];
BOOL repeat = [isInterval boolValue];
2019-10-12 14:48:19 +08:00
NSTimer *timer = [NSTimer timerWithTimeInterval:[interval doubleValue] / 1000 target:self selector:@selector(callbackTimer:) userInfo:@{@"timerId": timerId, @"repeat": isInterval} repeats:repeat];
2019-07-27 19:16:02 +08:00
[self.timers setValue:timer forKey:timerId_str];
2019-10-12 14:48:19 +08:00
dispatch_async(dispatch_get_main_queue(), ^() {
2019-07-27 19:16:02 +08:00
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
});
}];
2019-10-12 14:48:19 +08:00
2019-07-27 19:16:02 +08:00
[self.jsExecutor injectGlobalJSObject:INJECT_TIMER_CLEAR
obj:^(NSString *timerId) {
2019-10-12 14:48:19 +08:00
__strong typeof(_self) self = _self;
2019-07-27 19:16:02 +08:00
NSTimer *timer = [self.timers valueForKey:timerId];
2019-10-12 14:48:19 +08:00
if (timer) {
2019-07-27 19:16:02 +08:00
[timer invalidate];
[self.timers removeObjectForKey:timerId];
}
}];
2019-10-12 14:48:19 +08:00
[self.jsExecutor injectGlobalJSObject:INJECT_BRIDGE obj:^(NSString *contextId, NSString *module, NSString *method, NSString *callbackId, id argument) {
2019-07-29 20:51:53 +08:00
return [self.bridgeExtension callNativeWithContextId:contextId module:module method:method callbackId:callbackId argument:argument];
}];
2019-07-26 14:19:42 +08:00
}
2019-07-29 13:48:27 +08:00
- (void)initDoricEnvironment {
2019-07-27 10:33:38 +08:00
[self loadBuiltinJS:DORIC_BUNDLE_SANDBOX];
2019-08-14 14:13:08 +08:00
NSString *path = [DoricBundle() pathForResource:DORIC_BUNDLE_LIB ofType:@"js" inDirectory:@"bundle"];
2019-07-27 10:33:38 +08:00
NSString *jsContent = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
2019-10-12 14:48:19 +08:00
[self.jsExecutor loadJSScript:[self packageModuleScript:DORIC_MODULE_LIB content:jsContent]
source:[@"Module://" stringByAppendingString:DORIC_MODULE_LIB]];
2019-07-27 10:33:38 +08:00
}
2019-07-29 13:48:27 +08:00
- (void)loadBuiltinJS:(NSString *)fileName {
2019-08-14 14:13:08 +08:00
NSString *path = [DoricBundle() pathForResource:DORIC_BUNDLE_SANDBOX ofType:@"js" inDirectory:@"bundle"];
2019-07-27 10:33:38 +08:00
NSString *jsContent = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[self.jsExecutor loadJSScript:jsContent source:[@"Assets://" stringByAppendingString:fileName]];
}
2019-10-12 14:48:19 +08:00
- (JSValue *)invokeDoricMethod:(NSString *)method, ... {
2019-07-26 14:19:42 +08:00
va_list args;
va_start(args, method);
2019-07-27 14:36:41 +08:00
JSValue *ret = [self invokeDoricMethod:method arguments:args];
va_end(args);
return ret;
}
2019-07-29 13:48:27 +08:00
- (JSValue *)invokeDoricMethod:(NSString *)method arguments:(va_list)args {
2019-07-27 14:36:41 +08:00
NSMutableArray *array = [[NSMutableArray alloc] init];
2019-07-27 10:33:38 +08:00
id arg = va_arg(args, id);
2019-10-12 14:48:19 +08:00
while (arg != nil) {
2019-07-26 14:19:42 +08:00
[array addObject:arg];
arg = va_arg(args, JSValue *);
}
2019-07-27 14:36:41 +08:00
return [self.jsExecutor invokeObject:GLOBAL_DORIC method:method args:array];
}
2019-07-29 13:48:27 +08:00
- (JSValue *)invokeDoricMethod:(NSString *)method argumentsArray:(NSArray *)args {
2019-07-27 14:36:41 +08:00
return [self.jsExecutor invokeObject:GLOBAL_DORIC method:method args:args];
2019-07-26 14:19:42 +08:00
}
2019-07-29 13:48:27 +08:00
- (NSString *)packageContextScript:(NSString *)contextId content:(NSString *)content {
2019-07-27 10:33:38 +08:00
NSString *ret = [NSString stringWithFormat:TEMPLATE_CONTEXT_CREATE, content, contextId, contextId, contextId];
return ret;
}
2019-07-29 13:48:27 +08:00
- (NSString *)packageModuleScript:(NSString *)moduleName content:(NSString *)content {
2019-07-27 10:33:38 +08:00
NSString *ret = [NSString stringWithFormat:TEMPLATE_MODULE, moduleName, content];
return ret;
}
2019-07-29 13:48:27 +08:00
- (void)prepareContext:(NSString *)contextId script:(NSString *)script source:(NSString *)source {
2019-07-27 10:33:38 +08:00
[self.jsExecutor loadJSScript:[self packageContextScript:contextId content:script]
source:[@"Context://" stringByAppendingString:source]];
}
2019-07-29 13:48:27 +08:00
- (void)destroyContext:(NSString *)contextId {
2019-07-27 10:33:38 +08:00
[self.jsExecutor loadJSScript:[NSString stringWithFormat:TEMPLATE_CONTEXT_DESTROY, contextId]
source:[@"_Context://" stringByAppendingString:contextId]];
}
2019-07-26 14:19:42 +08:00
2019-07-29 13:48:27 +08:00
- (void)callbackTimer:(NSTimer *)timer {
2019-07-28 01:26:19 +08:00
NSDictionary *userInfo = timer.userInfo;
NSNumber *timerId = [userInfo valueForKey:@"timerId"];
NSNumber *repeat = [userInfo valueForKey:@"repeat"];
__weak typeof(self) _self = self;
2019-10-12 14:48:19 +08:00
dispatch_async(self.jsQueue, ^() {
2019-07-28 01:26:19 +08:00
__strong typeof(_self) self = _self;
@try {
2019-07-30 19:07:46 +08:00
[self invokeDoricMethod:DORIC_TIMER_CALLBACK, timerId, nil];
2019-07-28 01:26:19 +08:00
} @catch (NSException *exception) {
DoricLog(@"Timer Callback error:%@", exception.reason);
}
2019-10-12 14:48:19 +08:00
if (![repeat boolValue]) {
2019-07-28 01:26:19 +08:00
[self.timers removeObjectForKey:[timerId stringValue]];
}
});
2019-07-27 19:16:02 +08:00
}
2019-07-26 14:19:42 +08:00
@end