2019-12-04 13:29:26 +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.
|
|
|
|
*/
|
|
|
|
//
|
|
|
|
// DoricJSEngine.m
|
|
|
|
// Doric
|
|
|
|
//
|
|
|
|
// Created by pengfei.zhou on 2019/7/26.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "DoricJSEngine.h"
|
|
|
|
#import "DoricJSCoreExecutor.h"
|
|
|
|
#import "DoricConstant.h"
|
|
|
|
#import "DoricUtil.h"
|
|
|
|
#import "DoricBridgeExtension.h"
|
2020-03-14 10:54:13 +08:00
|
|
|
#import <sys/utsname.h>
|
2020-03-18 15:57:29 +08:00
|
|
|
#import "DoricContext.h"
|
2021-07-07 12:44:40 +08:00
|
|
|
#import "DoricContextManager.h"
|
2020-03-18 15:57:29 +08:00
|
|
|
|
|
|
|
@interface DoricDefaultMonitor : NSObject <DoricMonitorProtocol>
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation DoricDefaultMonitor
|
|
|
|
- (void)onException:(NSException *)exception inContext:(DoricContext *)context {
|
|
|
|
DoricLog(@"DefaultMonitor - source: %@- onException - %@", context.source, exception.reason);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)onLog:(DoricLogType)type message:(NSString *)message {
|
|
|
|
DoricLog(message);
|
|
|
|
}
|
|
|
|
@end
|
2019-12-04 13:29:26 +08:00
|
|
|
|
|
|
|
@interface DoricJSEngine ()
|
2021-02-23 15:58:49 +08:00
|
|
|
@property(nonatomic, strong) NSMutableDictionary <NSNumber *, NSTimer *> *timers;
|
2019-12-04 13:29:26 +08:00
|
|
|
@property(nonatomic, strong) DoricBridgeExtension *bridgeExtension;
|
2021-07-06 17:35:56 +08:00
|
|
|
@property(nonatomic, strong) NSMutableDictionary *environmentDictionary;
|
2020-05-13 10:02:22 +08:00
|
|
|
@property(nonatomic, strong) NSThread *jsThread;
|
|
|
|
@property(nonatomic, assign) BOOL destroyed;
|
2021-07-07 12:44:40 +08:00
|
|
|
@property(nonatomic, assign) BOOL initialized;
|
2019-12-04 13:29:26 +08:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation DoricJSEngine
|
|
|
|
|
|
|
|
- (instancetype)init {
|
|
|
|
if (self = [super init]) {
|
2021-07-07 12:44:40 +08:00
|
|
|
_initialized = NO;
|
2020-05-13 10:02:22 +08:00
|
|
|
_jsThread = [[NSThread alloc] initWithTarget:self selector:@selector(threadRun) object:nil];
|
|
|
|
[_jsThread start];
|
2020-02-27 14:50:26 +08:00
|
|
|
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
|
2020-03-14 10:54:13 +08:00
|
|
|
struct utsname systemInfo;
|
|
|
|
uname(&systemInfo);
|
|
|
|
NSString *platform = [NSString stringWithCString:systemInfo.machine encoding:NSASCIIStringEncoding];
|
|
|
|
if (TARGET_OS_SIMULATOR == 1) {
|
|
|
|
platform = [NSProcessInfo new].environment[@"SIMULATOR_MODEL_IDENTIFIER"];
|
|
|
|
}
|
2021-02-23 11:06:57 +08:00
|
|
|
|
2020-08-25 17:15:21 +08:00
|
|
|
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
|
|
|
|
CGFloat screenWidth;
|
|
|
|
CGFloat screenHeight;
|
|
|
|
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
|
|
|
|
screenWidth = [[UIScreen mainScreen] bounds].size.width;
|
|
|
|
screenHeight = [[UIScreen mainScreen] bounds].size.height;
|
|
|
|
} else {
|
|
|
|
screenWidth = [[UIScreen mainScreen] bounds].size.height;
|
|
|
|
screenHeight = [[UIScreen mainScreen] bounds].size.width;
|
|
|
|
}
|
2021-07-06 17:35:56 +08:00
|
|
|
_environmentDictionary = @{
|
2020-02-27 14:50:26 +08:00
|
|
|
@"platform": @"iOS",
|
|
|
|
@"platformVersion": [[UIDevice currentDevice] systemVersion],
|
|
|
|
@"appName": infoDictionary[@"CFBundleName"],
|
|
|
|
@"appVersion": infoDictionary[@"CFBundleShortVersionString"],
|
2020-08-25 17:15:21 +08:00
|
|
|
@"screenWidth": @(screenWidth),
|
|
|
|
@"screenHeight": @(screenHeight),
|
2020-04-23 15:38:37 +08:00
|
|
|
@"screenScale": @([[UIScreen mainScreen] scale]),
|
2020-02-27 14:50:26 +08:00
|
|
|
@"statusBarHeight": @([[UIApplication sharedApplication] statusBarFrame].size.height),
|
2020-03-19 11:52:02 +08:00
|
|
|
@"hasNotch": @(hasNotch()),
|
2020-03-14 10:54:13 +08:00
|
|
|
@"deviceBrand": @"Apple",
|
|
|
|
@"deviceModel": platform,
|
2021-07-06 17:35:56 +08:00
|
|
|
@"localeLanguage": [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode],
|
|
|
|
@"localeCountry": [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode],
|
|
|
|
}.mutableCopy;
|
2021-07-07 12:44:40 +08:00
|
|
|
self.registry = [[DoricRegistry alloc] initWithJSEngine:self];
|
2020-05-13 10:02:22 +08:00
|
|
|
[self ensureRunOnJSThread:^() {
|
2019-12-04 13:29:26 +08:00
|
|
|
self.timers = [[NSMutableDictionary alloc] init];
|
2020-04-23 17:42:32 +08:00
|
|
|
self.bridgeExtension = [DoricBridgeExtension new];
|
|
|
|
self.bridgeExtension.registry = self.registry;
|
2020-03-18 15:57:29 +08:00
|
|
|
[self initJSEngine];
|
2019-12-04 13:29:26 +08:00
|
|
|
[self initJSExecutor];
|
|
|
|
[self initDoricEnvironment];
|
2021-07-07 12:44:40 +08:00
|
|
|
self.initialized = YES;
|
2020-05-13 10:02:22 +08:00
|
|
|
}];
|
2021-02-22 19:03:34 +08:00
|
|
|
[self.registry registerMonitor:[DoricDefaultMonitor new]];
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2021-07-07 17:30:08 +08:00
|
|
|
- (void)setEnvironmentValue:(NSDictionary *)value {
|
2021-07-07 12:44:40 +08:00
|
|
|
[self ensureRunOnJSThread:^{
|
2021-07-07 17:30:08 +08:00
|
|
|
[self.environmentDictionary addEntriesFromDictionary:value];
|
2021-07-07 12:44:40 +08:00
|
|
|
if (self.initialized) {
|
|
|
|
[self.jsExecutor injectGlobalJSObject:INJECT_ENVIRONMENT obj:[self.environmentDictionary copy]];
|
|
|
|
for (DoricContext *doricContext in DoricContextManager.instance.aliveContexts) {
|
|
|
|
[doricContext onEnvChanged];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2021-03-04 10:02:29 +08:00
|
|
|
- (void)teardown {
|
2020-05-13 10:02:22 +08:00
|
|
|
_destroyed = YES;
|
2021-03-04 12:53:42 +08:00
|
|
|
//To ensure runloop continue.
|
|
|
|
[self ensureRunOnJSThread:^{
|
|
|
|
}];
|
2021-03-04 10:02:29 +08:00
|
|
|
}
|
2020-05-13 10:02:22 +08:00
|
|
|
|
|
|
|
- (void)ensureRunOnJSThread:(dispatch_block_t)block {
|
|
|
|
if (NSThread.currentThread == _jsThread) {
|
|
|
|
block();
|
|
|
|
} else {
|
|
|
|
[self performSelector:@selector(ensureRunOnJSThread:)
|
|
|
|
onThread:_jsThread
|
|
|
|
withObject:[block copy]
|
|
|
|
waitUntilDone:NO];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)threadRun {
|
|
|
|
[[NSRunLoop currentRunLoop] addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];
|
|
|
|
[NSThread currentThread].name = @"doric.js.engine";
|
|
|
|
while (!_destroyed) {
|
|
|
|
@autoreleasepool {
|
|
|
|
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-26 11:50:18 +08:00
|
|
|
- (void)initJSEngine {
|
|
|
|
self.jsExecutor = [DoricJSCoreExecutor new];
|
|
|
|
}
|
2020-03-07 10:38:42 +08:00
|
|
|
|
2019-12-04 13:29:26 +08:00
|
|
|
- (void)initJSExecutor {
|
|
|
|
__weak typeof(self) _self = self;
|
2021-07-06 17:35:56 +08:00
|
|
|
[self.jsExecutor injectGlobalJSObject:INJECT_ENVIRONMENT obj:[self.environmentDictionary copy]];
|
2019-12-04 13:29:26 +08:00
|
|
|
[self.jsExecutor injectGlobalJSObject:INJECT_LOG obj:^(NSString *type, NSString *message) {
|
2020-01-11 11:01:38 +08:00
|
|
|
if ([type isEqualToString:@"e"]) {
|
|
|
|
[self.registry onLog:DoricLogTypeError message:message];
|
|
|
|
} else if ([type isEqualToString:@"w"]) {
|
|
|
|
[self.registry onLog:DoricLogTypeWarning message:message];
|
|
|
|
} else {
|
|
|
|
[self.registry onLog:DoricLogTypeDebug message:message];
|
|
|
|
}
|
2019-12-04 13:29:26 +08:00
|
|
|
}];
|
|
|
|
[self.jsExecutor injectGlobalJSObject:INJECT_EMPTY obj:^() {
|
2019-12-10 20:30:43 +08:00
|
|
|
|
2019-12-04 13:29:26 +08:00
|
|
|
}];
|
|
|
|
[self.jsExecutor injectGlobalJSObject:INJECT_REQUIRE obj:^(NSString *name) {
|
|
|
|
__strong typeof(_self) self = _self;
|
|
|
|
if (!self) return NO;
|
|
|
|
NSString *content = [self.registry acquireJSBundle:name];
|
|
|
|
if (!content) {
|
2020-01-11 11:01:38 +08:00
|
|
|
[self.registry onLog:DoricLogTypeError message:[NSString stringWithFormat:@"require js bundle:%@ is empty", name]];
|
2019-12-04 13:29:26 +08:00
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
@try {
|
|
|
|
[self.jsExecutor loadJSScript:[self packageModuleScript:name content:content]
|
|
|
|
source:[@"Module://" stringByAppendingString:name]];
|
|
|
|
} @catch (NSException *e) {
|
2020-01-11 11:01:38 +08:00
|
|
|
[self.registry onLog:DoricLogTypeError
|
|
|
|
message:[NSString stringWithFormat:@"require js bundle:%@ error,for %@", name, e.reason]];
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
return YES;
|
|
|
|
}];
|
|
|
|
[self.jsExecutor injectGlobalJSObject:INJECT_TIMER_SET
|
|
|
|
obj:^(NSNumber *timerId, NSNumber *interval, NSNumber *isInterval) {
|
|
|
|
__strong typeof(_self) self = _self;
|
|
|
|
BOOL repeat = [isInterval boolValue];
|
|
|
|
NSTimer *timer = [NSTimer timerWithTimeInterval:[interval doubleValue] / 1000 target:self selector:@selector(callbackTimer:) userInfo:@{@"timerId": timerId, @"repeat": isInterval} repeats:repeat];
|
2021-02-23 15:58:49 +08:00
|
|
|
self.timers[timerId] = timer;
|
2019-12-04 13:29:26 +08:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^() {
|
|
|
|
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
|
|
|
|
});
|
|
|
|
}];
|
|
|
|
|
|
|
|
[self.jsExecutor injectGlobalJSObject:INJECT_TIMER_CLEAR
|
2021-02-23 15:58:49 +08:00
|
|
|
obj:^(NSNumber *timerId) {
|
2019-12-04 13:29:26 +08:00
|
|
|
__strong typeof(_self) self = _self;
|
2021-02-23 15:58:49 +08:00
|
|
|
NSTimer *timer = self.timers[timerId];
|
2019-12-04 13:29:26 +08:00
|
|
|
if (timer) {
|
|
|
|
[self.timers removeObjectForKey:timerId];
|
2020-05-08 16:51:19 +08:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
[timer invalidate];
|
|
|
|
});
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
}];
|
|
|
|
|
|
|
|
[self.jsExecutor injectGlobalJSObject:INJECT_BRIDGE obj:^(NSString *contextId, NSString *module, NSString *method, NSString *callbackId, id argument) {
|
|
|
|
return [self.bridgeExtension callNativeWithContextId:contextId module:module method:method callbackId:callbackId argument:argument];
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)initDoricEnvironment {
|
2020-01-11 11:01:38 +08:00
|
|
|
@try {
|
|
|
|
[self loadBuiltinJS:DORIC_BUNDLE_SANDBOX];
|
2020-01-15 18:15:10 +08:00
|
|
|
NSString *path;
|
|
|
|
if (@available(iOS 10.0, *)) {
|
|
|
|
path = [DoricBundle() pathForResource:DORIC_BUNDLE_LIB ofType:@"js"];
|
|
|
|
} else {
|
|
|
|
path = [DoricBundle() pathForResource:[NSString stringWithFormat:@"%@.es5", DORIC_BUNDLE_LIB] ofType:@"js"];
|
|
|
|
}
|
2020-01-11 11:01:38 +08:00
|
|
|
NSString *jsContent = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
|
|
|
|
[self.jsExecutor loadJSScript:[self packageModuleScript:DORIC_MODULE_LIB content:jsContent]
|
|
|
|
source:[@"Module://" stringByAppendingString:DORIC_MODULE_LIB]];
|
|
|
|
} @catch (NSException *exception) {
|
2020-03-07 10:38:42 +08:00
|
|
|
[self.registry onException:exception inContext:nil];
|
2020-01-11 11:01:38 +08:00
|
|
|
}
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)loadBuiltinJS:(NSString *)fileName {
|
2020-01-15 18:15:10 +08:00
|
|
|
NSString *path;
|
|
|
|
if (@available(iOS 10.0, *)) {
|
|
|
|
path = [DoricBundle() pathForResource:DORIC_BUNDLE_SANDBOX ofType:@"js"];
|
|
|
|
} else {
|
|
|
|
path = [DoricBundle() pathForResource:[NSString stringWithFormat:@"%@.es5", DORIC_BUNDLE_SANDBOX] ofType:@"js"];
|
|
|
|
}
|
2019-12-04 13:29:26 +08:00
|
|
|
NSString *jsContent = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
|
|
|
|
[self.jsExecutor loadJSScript:jsContent source:[@"Assets://" stringByAppendingString:fileName]];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (JSValue *)invokeDoricMethod:(NSString *)method, ... {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, method);
|
|
|
|
JSValue *ret = [self invokeDoricMethod:method arguments:args];
|
|
|
|
va_end(args);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (JSValue *)invokeDoricMethod:(NSString *)method arguments:(va_list)args {
|
|
|
|
NSMutableArray *array = [[NSMutableArray alloc] init];
|
|
|
|
id arg = va_arg(args, id);
|
|
|
|
while (arg != nil) {
|
|
|
|
[array addObject:arg];
|
|
|
|
arg = va_arg(args, JSValue *);
|
|
|
|
}
|
|
|
|
return [self.jsExecutor invokeObject:GLOBAL_DORIC method:method args:array];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (JSValue *)invokeDoricMethod:(NSString *)method argumentsArray:(NSArray *)args {
|
|
|
|
return [self.jsExecutor invokeObject:GLOBAL_DORIC method:method args:args];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)packageContextScript:(NSString *)contextId content:(NSString *)content {
|
2020-01-08 20:18:19 +08:00
|
|
|
NSString *ret = [NSString stringWithFormat:TEMPLATE_CONTEXT_CREATE, content, contextId, contextId];
|
2019-12-04 13:29:26 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)packageModuleScript:(NSString *)moduleName content:(NSString *)content {
|
|
|
|
NSString *ret = [NSString stringWithFormat:TEMPLATE_MODULE, moduleName, content];
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)prepareContext:(NSString *)contextId script:(NSString *)script source:(NSString *)source {
|
|
|
|
[self.jsExecutor loadJSScript:[self packageContextScript:contextId content:script]
|
2020-02-26 17:17:09 +08:00
|
|
|
source:[@"Context://" stringByAppendingString:source ?: contextId]];
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)destroyContext:(NSString *)contextId {
|
|
|
|
[self.jsExecutor loadJSScript:[NSString stringWithFormat:TEMPLATE_CONTEXT_DESTROY, contextId]
|
|
|
|
source:[@"_Context://" stringByAppendingString:contextId]];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)callbackTimer:(NSTimer *)timer {
|
|
|
|
__weak typeof(self) _self = self;
|
2020-04-20 15:03:34 +08:00
|
|
|
if (!timer.isValid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NSDictionary *userInfo = timer.userInfo;
|
|
|
|
NSNumber *timerId = [userInfo valueForKey:@"timerId"];
|
|
|
|
NSNumber *repeat = [userInfo valueForKey:@"repeat"];
|
2020-05-13 10:02:22 +08:00
|
|
|
[self ensureRunOnJSThread:^{
|
2019-12-04 13:29:26 +08:00
|
|
|
__strong typeof(_self) self = _self;
|
|
|
|
@try {
|
|
|
|
[self invokeDoricMethod:DORIC_TIMER_CALLBACK, timerId, nil];
|
|
|
|
} @catch (NSException *exception) {
|
2020-03-07 10:38:42 +08:00
|
|
|
[self.registry onException:exception inContext:nil];
|
2020-01-11 11:01:38 +08:00
|
|
|
[self.registry onLog:DoricLogTypeError
|
|
|
|
message:[NSString stringWithFormat:@"Timer Callback error:%@", exception.reason]];
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
if (![repeat boolValue]) {
|
2021-02-23 15:58:49 +08:00
|
|
|
[self.timers removeObjectForKey:timerId];
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
2020-05-13 10:02:22 +08:00
|
|
|
}];
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
@end
|