Doric Async and so on

This commit is contained in:
pengfei.zhou
2019-07-26 14:19:42 +08:00
parent 40165bca14
commit f0b003cb66
25 changed files with 523 additions and 152 deletions

View File

@@ -0,0 +1,26 @@
//
// DoricAsyncResult.h
// Doric
//
// Created by pengfei.zhou on 2019/7/26.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface DoricAsyncResult <R>: NSObject
typedef void(^DoricResultCallback)(R);
typedef void(^DoricExceptionCallback)(NSException *);
typedef void(^DoricFinishCallback)(void);
-(void)setupResult:(R)result;
-(void)setupError:(NSException*)exception;
-(BOOL)hasResult;
-(R)getResult;
-(void)setResultCallback:(DoricResultCallback) callback exceptionCallback:(DoricExceptionCallback) exceptionCallback;
-(void)setFinishCallback:(DoricFinishCallback) callback;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,62 @@
//
// DoricAsyncResult.m
// Doric
//
// Created by pengfei.zhou on 2019/7/26.
//
#import "DoricAsyncResult.h"
@interface DoricAsyncResult()
@property(nonatomic,strong) id result;
@property(nonatomic,strong) DoricResultCallback resultCallback;
@property(nonatomic,strong) DoricExceptionCallback exceptionCallback;
@property(nonatomic,strong) DoricFinishCallback finishCallback;
@end
@implementation DoricAsyncResult
-(void)setupResult:(id)result {
self.result = result;
if(self.resultCallback){
self.resultCallback(result);
}
if(self.finishCallback){
self.finishCallback();
}
}
-(void)setupError:(NSException *)exception {
self.result = exception;
if(self.exceptionCallback){
self.exceptionCallback(exception);
}
if(self.finishCallback){
self.finishCallback();
}
}
-(BOOL)hasResult {
return self.result;
}
-(id)getResult {
return self.result;
}
-(void)setResultCallback:(DoricResultCallback)callback exceptionCallback:(DoricExceptionCallback)exceptionCallback {
self.resultCallback = callback;
self.exceptionCallback = exceptionCallback;
if([self.result isKindOfClass: [NSException class]]){
self.exceptionCallback(self.result);
}else if(self.result){
self.resultCallback(self.result);
}
}
-(void)setFinishCallback:(DoricFinishCallback)callback {
self.finishCallback = callback;
if(self.result){
callback();
}
}
@end

View File

@@ -0,0 +1,39 @@
//
// DoricConstant.h
// Doric
//
// Created by pengfei.zhou on 2019/7/26.
//
#import <Foundation/Foundation.h>
extern NSString * const DORIC_BUNDLE_SANDBOX;
extern NSString * const DORIC_BUNDLE_LIB;
extern NSString * const DORIC_MODULE_LIB;
extern NSString * const INJECT_LOG;
extern NSString * const INJECT_REQUIRE;
extern NSString * const INJECT_TIMER_SET;
extern NSString * const INJECT_TIMER_CLEAR;
extern NSString * const INJECT_BRIDGE;
extern NSString * const TEMPLATE_CONTEXT_CREATE;
extern NSString * const TEMPLATE_MODULE;
extern NSString * const TEMPLATE_CONTEXT_DESTROY;
extern NSString * const GLOBAL_DORIC;
extern NSString * const DORIC_CONTEXT_RELEASE;
extern NSString * const DORIC_CONTEXT_INVOKE;
extern NSString * const DORIC_TIMER_CALLBACK;
extern NSString * const DORIC_BRIDGE_RESOLVE;
extern NSString * const DORIC_BRIDGE_REJECT;
extern NSString * const DORIC_ENTITY_RESPONSE;

View File

@@ -0,0 +1,56 @@
//
// DoricConstant.m
// Doric
//
// Created by pengfei.zhou on 2019/7/26.
//
#import "DoricConstant.h"
NSString * const DORIC_BUNDLE_SANBOX = @"";
NSString * const DORIC_BUNDLE_LIB = @"doric-lib.js";
NSString * const DORIC_MODULE_LIB = @"./index";
NSString * const INJECT_LOG = @"nativeLog";
NSString * const INJECT_REQUIRE = @"nativeRequire";
NSString * const INJECT_TIMER_SET = @"nativeSetTimer";
NSString * const INJECT_TIMER_CLEAR = @"nativeClearTimer";
NSString * const INJECT_BRIDGE = @"nativeBridge";
NSString * const TEMPLATE_CONTEXT_CREATE = @"Reflect.apply("
"function(doric,context,Entry,require,exports){" "\n"
"%s" "\n"
"},doric.jsObtainContext(\"%s\"),["
"undefined,"
"doric.jsObtainContext(\"%s\"),"
"doric.jsObtainEntry(\"%s\"),"
"doric.__require__"
",{}"
"])";
NSString * const TEMPLATE_MODULE = @"Reflect.apply(doric.jsRegisterModule,this,["
"\"%s\","
"Reflect.apply(function(__module){"
"(function(module,exports,require){" "\n"
"%s" "\n"
"})(__module,__module.exports,doric.__require__);"
"\nreturn __module.exports;"
"},this,[{exports:{}}])"
"])";
NSString * const TEMPLATE_CONTEXT_DESTROY = @"doric.jsRelease(%s)";
NSString * const GLOBAL_DORIC = @"doric";
NSString * const DORIC_CONTEXT_RELEASE = @"jsReleaseContext";
NSString * const DORIC_CONTEXT_INVOKE = @"jsCallEntityMethod";
NSString * const DORIC_TIMER_CALLBACK = @"jsCallbackTimer";
NSString * const DORIC_BRIDGE_RESOLVE = @"jsCallResolve";
NSString * const DORIC_BRIDGE_REJECT = @"jsCallReject";
NSString * const DORIC_ENTITY_RESPONSE = @"__response__";

View File

@@ -0,0 +1,16 @@
//
// DoricDriver.h
// Doric
//
// Created by pengfei.zhou on 2019/7/26.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface DoricDriver : NSObject
+(instancetype) instance;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,27 @@
//
// DoricDriver.m
// Doric
//
// Created by pengfei.zhou on 2019/7/26.
//
#import "DoricDriver.h"
#import "DoricJSCoreExecutor.h"
#import "DoricJSExecutorProtocal.h"
@interface DoricDriver()
@property (nonatomic, strong) id<DoricJSExecutorProtocal> jsExecutor;
@end
@implementation DoricDriver
+ (instancetype)instance{
static DoricDriver *_instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [[DoricDriver alloc] init];
});
return _instance;
}
@end

View File

@@ -39,11 +39,6 @@ -(void)injectGlobalJSObject:(NSString *)name obj:(NSDictionary *)obj {
[self checkJSException];
}
-(void)injectGlobalJSFunction:(NSString *)name func:(DoricOCFunction)func {
self.jsContext[name] = func;
[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];

View File

@@ -0,0 +1,16 @@
//
// DoricJSEngine.h
// Doric
//
// Created by pengfei.zhou on 2019/7/26.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface DoricJSEngine : NSObject
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,57 @@
//
// DoricJSEngine.m
// Doric
//
// Created by pengfei.zhou on 2019/7/26.
//
#import "DoricJSEngine.h"
#import "DoricJSExecutorProtocal.h"
#import "DoricJSCoreExecutor.h"
#import "DoricConstant.h"
#import "DoricUtil.h"
@interface DoricJSEngine()
@property(nonatomic,strong) id<DoricJSExecutorProtocal> jsExecutor;
@property(nonatomic,strong) dispatch_queue_t jsQueue;
@end
@implementation DoricJSEngine
-(instancetype)init {
if(self = [super init]){
_jsQueue = dispatch_queue_create("doric.jsengine", DISPATCH_QUEUE_SERIAL);
dispatch_async(_jsQueue, ^(){
self.jsExecutor = [[DoricJSCoreExecutor alloc] init];
[self initDoricEnvironment];
});
}
return self;
}
-(void)initDoricEnvironment {
[self.jsExecutor injectGlobalJSObject:INJECT_LOG obj:^(NSString * type, NSString * message){
DoricLog(@"JS:%@",message);
}];
[self.jsExecutor injectGlobalJSObject:INJECT_REQUIRE obj:^(NSString *name){
}];
[self.jsExecutor loadJSScript:@"nativeLog('w','log from js')" source:@""];
}
-(void)invokeDoricMethod:(NSString *)method,... {
NSMutableArray *array = [[NSMutableArray alloc] init];
va_list args;
va_start(args, method);
JSValue *arg = va_arg(args, JSValue *);
while(arg!=nil){
[array addObject:arg];
arg = va_arg(args, JSValue *);
}
va_end(args);
[self.jsExecutor invokeObject:GLOBAL_DORIC method:method args:array];
}
@end

View File

@@ -10,15 +10,11 @@
NS_ASSUME_NONNULL_BEGIN
typedef JSValue*(^DoricOCFunction)();
@protocol DoricJSExecutorProtocal <NSObject>
-(NSString *) loadJSScript:(NSString *)script source:(NSString *)source;
-(void) injectGlobalJSFunction:(NSString *)name func:(DoricOCFunction)func;
-(void) injectGlobalJSObject:(NSString *)name obj:(NSDictionary *)obj;
-(void) injectGlobalJSObject:(NSString *)name obj:(id)obj;
-(JSValue *) invokeObject: (NSString *)objName method:(NSString *)funcName args:(NSArray *)args;

View File

@@ -0,0 +1,10 @@
//
// DoricUtil.h
// Doric
//
// Created by pengfei.zhou on 2019/7/26.
//
#import <Foundation/Foundation.h>
void DoricLog(NSString * _Nonnull format, ...);

View File

@@ -0,0 +1,15 @@
//
// DoricUtil.m
// Doric
//
// Created by pengfei.zhou on 2019/7/26.
//
#import "DoricUtil.h"
void DoricLog(NSString * _Nonnull format, ...) {
va_list args;
va_start(args, format);
NSLogv([NSString stringWithFormat:@"Doric:%@",format],args);
va_end(args);
}