iOS Doric Registry and so on
This commit is contained in:
@@ -11,6 +11,8 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface DoricContext : NSObject
|
||||
|
||||
@property (nonatomic,strong) NSString *contextId;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
@@ -6,11 +6,27 @@
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "DoricAsyncResult.h"
|
||||
#import "DoricRegistry.h"
|
||||
|
||||
typedef NS_ENUM(NSInteger, QueueMode) {
|
||||
JS = 0,
|
||||
UI,
|
||||
INDEPENDENT
|
||||
};
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface DoricDriver : NSObject
|
||||
+(instancetype) instance;
|
||||
|
||||
@property (nonatomic,strong) DoricRegistry *registry;
|
||||
|
||||
-(DoricAsyncResult *)createContext:(NSString *)contextId script:(NSString *)script source:(NSString *)source;
|
||||
-(DoricAsyncResult *)destroyContext:(NSString *)contextId;
|
||||
|
||||
-(DoricAsyncResult *)invokeDoricMethod:(NSString *)method, ...;
|
||||
-(DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString *)method, ...;
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
@@ -6,15 +6,21 @@
|
||||
//
|
||||
|
||||
#import "DoricDriver.h"
|
||||
#import "DoricJSCoreExecutor.h"
|
||||
#import "DoricJSExecutorProtocal.h"
|
||||
#import "DoricJSEngine.h"
|
||||
#import "DoricConstant.h"
|
||||
|
||||
@interface DoricDriver()
|
||||
@property (nonatomic, strong) id<DoricJSExecutorProtocal> jsExecutor;
|
||||
@property (nonatomic, strong) DoricJSEngine *jsExecutor;
|
||||
@end
|
||||
|
||||
@implementation DoricDriver
|
||||
|
||||
@dynamic registry;
|
||||
|
||||
-(DoricRegistry *)registry {
|
||||
return self.jsExecutor.registry;
|
||||
}
|
||||
|
||||
+ (instancetype)instance{
|
||||
static DoricDriver *_instance;
|
||||
static dispatch_once_t onceToken;
|
||||
@@ -24,4 +30,89 @@ + (instancetype)instance{
|
||||
return _instance;
|
||||
}
|
||||
|
||||
-(DoricAsyncResult<JSValue *> *)invokeDoricMethod:(NSString *)method, ... {
|
||||
va_list args;
|
||||
va_start(args, method);
|
||||
DoricAsyncResult *ret = [self invokeDoricMethod:method arguments:args];
|
||||
va_end(args);
|
||||
return ret;
|
||||
}
|
||||
|
||||
-(DoricAsyncResult<JSValue *> *)invokeDoricMethod:(NSString *)method arguments:(va_list)args {
|
||||
DoricAsyncResult *ret = [[DoricAsyncResult alloc] init];
|
||||
__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 arguments:args];
|
||||
[ret setupResult:jsValue];
|
||||
} @catch (NSException *exception) {
|
||||
[ret setupError:exception];
|
||||
}
|
||||
});
|
||||
return ret;
|
||||
}
|
||||
|
||||
-(DoricAsyncResult<JSValue *> *)invokeContextEntity:(NSString *)contextId method:(NSString *)method,... {
|
||||
DoricAsyncResult *ret = [[DoricAsyncResult alloc] init];
|
||||
NSMutableArray *array = [[NSMutableArray alloc] init];
|
||||
[array addObject:DORIC_CONTEXT_INVOKE];
|
||||
[array addObject:contextId];
|
||||
[array addObject:method];
|
||||
va_list args;
|
||||
va_start(args, method);
|
||||
id arg = va_arg(args, id);
|
||||
while(arg != nil){
|
||||
[array addObject:arg];
|
||||
arg = va_arg(args, JSValue *);
|
||||
}
|
||||
va_end(args);
|
||||
__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];
|
||||
}
|
||||
});
|
||||
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:[NSNumber numberWithBool:YES]];
|
||||
} @catch (NSException *exception) {
|
||||
[ret setupError:exception];
|
||||
}
|
||||
});
|
||||
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:[NSNumber numberWithBool:YES]];
|
||||
} @catch (NSException *exception) {
|
||||
[ret setupError:exception];
|
||||
}
|
||||
});
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
@end
|
||||
|
@@ -34,7 +34,7 @@ -(NSString *)loadJSScript:(NSString *)script source:(NSString *)source {
|
||||
return ret;
|
||||
}
|
||||
|
||||
-(void)injectGlobalJSObject:(NSString *)name obj:(NSDictionary *)obj {
|
||||
-(void)injectGlobalJSObject:(NSString *)name obj:(id)obj {
|
||||
self.jsContext[name] = obj;
|
||||
[self checkJSException];
|
||||
}
|
||||
|
@@ -6,11 +6,26 @@
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <JavaScriptCore/JavaScriptCore.h>
|
||||
#import "DoricRegistry.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface DoricJSEngine : NSObject
|
||||
|
||||
@property(nonatomic,strong) dispatch_queue_t jsQueue;
|
||||
|
||||
@property(nonatomic,strong) DoricRegistry *registry;
|
||||
|
||||
-(void)prepareContext:(NSString *)contextId script:(NSString *)script source:(NSString *)source;
|
||||
|
||||
-(void)destroyContext:(NSString *)contextId;
|
||||
|
||||
-(JSValue *)invokeDoricMethod:(NSString *)method, ...;
|
||||
|
||||
-(JSValue *)invokeDoricMethod:(NSString *)method arguments:(va_list)args;
|
||||
|
||||
-(JSValue *)invokeDoricMethod:(NSString *)method argumentsArray:(NSArray *)args;
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
@@ -12,9 +12,7 @@
|
||||
#import "DoricUtil.h"
|
||||
|
||||
@interface DoricJSEngine()
|
||||
|
||||
@property(nonatomic,strong) id<DoricJSExecutorProtocal> jsExecutor;
|
||||
@property(nonatomic,strong) dispatch_queue_t jsQueue;
|
||||
@end
|
||||
|
||||
@implementation DoricJSEngine
|
||||
@@ -24,6 +22,7 @@ -(instancetype)init {
|
||||
_jsQueue = dispatch_queue_create("doric.jsengine", DISPATCH_QUEUE_SERIAL);
|
||||
dispatch_async(_jsQueue, ^(){
|
||||
self.jsExecutor = [[DoricJSCoreExecutor alloc] init];
|
||||
self.registry = [[DoricRegistry alloc] init];
|
||||
[self initDoricEnvironment];
|
||||
});
|
||||
}
|
||||
@@ -31,12 +30,30 @@ -(instancetype)init {
|
||||
}
|
||||
|
||||
-(void)initJSExecutor {
|
||||
__weak typeof(self) _self = self;
|
||||
|
||||
[self.jsExecutor injectGlobalJSObject:INJECT_LOG obj:^(NSString * type, NSString * message){
|
||||
DoricLog(@"JS:%@",message);
|
||||
}];
|
||||
|
||||
[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){
|
||||
DoricLog(@"require js bundle:%@ is empty", name);
|
||||
return NO;
|
||||
}
|
||||
@try{
|
||||
[self.jsExecutor loadJSScript:[self packageModuleScript:name content:content]
|
||||
source:[@"Module://" stringByAppendingString:name]];
|
||||
}@catch(NSException *e){
|
||||
DoricLog(@"require js bundle:%@ error,for %@", name, e.reason);
|
||||
}
|
||||
return YES;
|
||||
}];
|
||||
|
||||
|
||||
}
|
||||
|
||||
-(void)initDoricEnvironment {
|
||||
@@ -53,17 +70,26 @@ -(void)loadBuiltinJS:(NSString *)fileName {
|
||||
[self.jsExecutor loadJSScript:jsContent source:[@"Assets://" stringByAppendingString:fileName]];
|
||||
}
|
||||
|
||||
-(void)invokeDoricMethod:(NSString *)method,... {
|
||||
NSMutableArray *array = [[NSMutableArray alloc] init];
|
||||
-(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){
|
||||
while(arg != nil){
|
||||
[array addObject:arg];
|
||||
arg = va_arg(args, JSValue *);
|
||||
}
|
||||
va_end(args);
|
||||
[self.jsExecutor invokeObject:GLOBAL_DORIC method:method args:array];
|
||||
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 {
|
||||
|
18
iOS/Pod/Classes/DoricRegistry.h
Normal file
18
iOS/Pod/Classes/DoricRegistry.h
Normal file
@@ -0,0 +1,18 @@
|
||||
//
|
||||
// DoricRegistry.h
|
||||
// Doric
|
||||
//
|
||||
// Created by pengfei.zhou on 2019/7/27.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface DoricRegistry : NSObject
|
||||
|
||||
-(NSString *)acquireJSBundle:(NSString *)bundleName;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
12
iOS/Pod/Classes/DoricRegistry.m
Normal file
12
iOS/Pod/Classes/DoricRegistry.m
Normal file
@@ -0,0 +1,12 @@
|
||||
//
|
||||
// DoricRegistry.m
|
||||
// Doric
|
||||
//
|
||||
// Created by pengfei.zhou on 2019/7/27.
|
||||
//
|
||||
|
||||
#import "DoricRegistry.h"
|
||||
|
||||
@implementation DoricRegistry
|
||||
|
||||
@end
|
Reference in New Issue
Block a user