add DoricJSCore
This commit is contained in:
16
iOS/Pod/Classes/DoricContext.h
Normal file
16
iOS/Pod/Classes/DoricContext.h
Normal file
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// DoricContext.h
|
||||
// Doric
|
||||
//
|
||||
// Created by pengfei.zhou on 2019/7/25.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface DoricContext : NSObject
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
12
iOS/Pod/Classes/DoricContext.m
Normal file
12
iOS/Pod/Classes/DoricContext.m
Normal file
@@ -0,0 +1,12 @@
|
||||
//
|
||||
// DoricContext.m
|
||||
// Doric
|
||||
//
|
||||
// Created by pengfei.zhou on 2019/7/25.
|
||||
//
|
||||
|
||||
#import "DoricContext.h"
|
||||
|
||||
@implementation DoricContext
|
||||
|
||||
@end
|
16
iOS/Pod/Classes/DoricJSCoreExecutor.h
Normal file
16
iOS/Pod/Classes/DoricJSCoreExecutor.h
Normal file
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// DoricJSCoreExecutor.h
|
||||
// Doric
|
||||
//
|
||||
// Created by pengfei.zhou on 2019/7/25.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "DoricJSEngineProtocal.h"
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface DoricJSCoreExecutor : NSObject<DoricJSEngineProtocal>
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
54
iOS/Pod/Classes/DoricJSCoreExecutor.m
Normal file
54
iOS/Pod/Classes/DoricJSCoreExecutor.m
Normal file
@@ -0,0 +1,54 @@
|
||||
//
|
||||
// DoricJSCoreExecutor.m
|
||||
// Doric
|
||||
//
|
||||
// Created by pengfei.zhou on 2019/7/25.
|
||||
//
|
||||
|
||||
#import "DoricJSCoreExecutor.h"
|
||||
|
||||
@interface DoricJSCoreExecutor()
|
||||
|
||||
@property(nonatomic,strong) JSContext *jsContext;
|
||||
|
||||
@end
|
||||
|
||||
@implementation DoricJSCoreExecutor
|
||||
-(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:(NSDictionary *)obj {
|
||||
self.jsContext[name] = 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];
|
||||
[self checkJSException];
|
||||
return ret;
|
||||
}
|
||||
|
||||
@end
|
27
iOS/Pod/Classes/DoricJSEngineProtocal.h
Normal file
27
iOS/Pod/Classes/DoricJSEngineProtocal.h
Normal file
@@ -0,0 +1,27 @@
|
||||
//
|
||||
// DoricJSEngineProtocal.h
|
||||
// Doric
|
||||
//
|
||||
// Created by pengfei.zhou on 2019/7/25.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <JavaScriptCore/JavaScriptCore.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
typedef JSValue*(^DoricOCFunction)();
|
||||
|
||||
@protocol DoricJSEngineProtocal <NSObject>
|
||||
|
||||
-(NSString *) loadJSScript:(NSString *)script source:(NSString *)source;
|
||||
|
||||
-(void) injectGlobalJSFunction:(NSString *)name func:(DoricOCFunction)func;
|
||||
|
||||
-(void) injectGlobalJSObject:(NSString *)name obj:(NSDictionary *)obj;
|
||||
|
||||
-(JSValue *) invokeObject: (NSString *)objName method:(NSString *)funcName args:(NSArray *)args;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
@@ -109,5 +109,5 @@ - (void)setCenterY:(CGFloat)centerY {
|
||||
frame.origin.y = centerY - self.frame.size.height/2;
|
||||
[self setFrame:frame];
|
||||
}
|
||||
|
||||
|
||||
@end
|
||||
|
Reference in New Issue
Block a user