add DoricJSEngine

This commit is contained in:
pengfei.zhou 2019-07-27 10:33:38 +08:00
parent bc1c283b47
commit f87aea62dc
3 changed files with 45 additions and 16 deletions

View File

@ -37,7 +37,7 @@ public class DoricConstant {
"\nreturn __module.exports;" +
"},this,[{exports:{}}])" +
"])";
public static final String TEMPLATE_CONTEXT_DESTROY = "doric.jsRelease(%s)";
public static final String TEMPLATE_CONTEXT_DESTROY = "doric.jsRelease(\"%s\")";
public static final String GLOBAL_DORIC = "doric";
public static final String DORIC_CONTEXT_RELEASE = "jsReleaseContext";
public static final String DORIC_CONTEXT_INVOKE = "jsCallEntityMethod";

View File

@ -7,8 +7,8 @@
#import "DoricConstant.h"
NSString * const DORIC_BUNDLE_SANBOX = @"";
NSString * const DORIC_BUNDLE_LIB = @"doric-lib.js";
NSString * const DORIC_BUNDLE_SANDBOX = @"doric-sandbox";
NSString * const DORIC_BUNDLE_LIB = @"doric-lib";
NSString * const DORIC_MODULE_LIB = @"./index";
@ -20,26 +20,26 @@
NSString * const TEMPLATE_CONTEXT_CREATE = @"Reflect.apply("
"function(doric,context,Entry,require,exports){" "\n"
"%s" "\n"
"},doric.jsObtainContext(\"%s\"),["
"%@" "\n"
"},doric.jsObtainContext(\"%@\"),["
"undefined,"
"doric.jsObtainContext(\"%s\"),"
"doric.jsObtainEntry(\"%s\"),"
"doric.jsObtainContext(\"%@\"),"
"doric.jsObtainEntry(\"%@\"),"
"doric.__require__"
",{}"
"])";
NSString * const TEMPLATE_MODULE = @"Reflect.apply(doric.jsRegisterModule,this,["
"\"%s\","
"\"%@\","
"Reflect.apply(function(__module){"
"(function(module,exports,require){" "\n"
"%s" "\n"
"%@" "\n"
"})(__module,__module.exports,doric.__require__);"
"\nreturn __module.exports;"
"},this,[{exports:{}}])"
"])";
NSString * const TEMPLATE_CONTEXT_DESTROY = @"doric.jsRelease(%s)";
NSString * const TEMPLATE_CONTEXT_DESTROY = @"doric.jsRelease(\"%@\"";
NSString * const GLOBAL_DORIC = @"doric";

View File

@ -30,24 +30,34 @@ -(instancetype)init {
return self;
}
-(void)initDoricEnvironment {
-(void)initJSExecutor {
[self.jsExecutor injectGlobalJSObject:INJECT_LOG obj:^(NSString * type, NSString * message){
DoricLog(@"JS:%@",message);
}];
[self.jsExecutor injectGlobalJSObject:INJECT_REQUIRE obj:^(NSString *name){
}];
NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:@"doric-sandbox" ofType:@"js"];
NSString *mainjs = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSLog(@"read:%@",mainjs);
[self.jsExecutor loadJSScript:@"nativeLog('w','log from js')" source:@""];
}
-(void)initDoricEnvironment {
[self loadBuiltinJS:DORIC_BUNDLE_SANDBOX];
NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:DORIC_BUNDLE_LIB ofType:@"js"];
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]];
}
-(void)loadBuiltinJS:(NSString *)fileName {
NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:DORIC_BUNDLE_SANDBOX ofType:@"js"];
NSString *jsContent = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[self.jsExecutor loadJSScript:jsContent source:[@"Assets://" stringByAppendingString:fileName]];
}
-(void)invokeDoricMethod:(NSString *)method,... {
NSMutableArray *array = [[NSMutableArray alloc] init];
va_list args;
va_start(args, method);
JSValue *arg = va_arg(args, JSValue *);
id arg = va_arg(args, id);
while(arg!=nil){
[array addObject:arg];
arg = va_arg(args, JSValue *);
@ -56,5 +66,24 @@ -(void)invokeDoricMethod:(NSString *)method,... {
[self.jsExecutor invokeObject:GLOBAL_DORIC method:method args:array];
}
-(NSString *)packageContextScript:(NSString *)contextId content:(NSString *)content {
NSString *ret = [NSString stringWithFormat:TEMPLATE_CONTEXT_CREATE, content, contextId, contextId, contextId];
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]
source:[@"Context://" stringByAppendingString:source]];
}
-(void)destroyContext:(NSString *)contextId {
[self.jsExecutor loadJSScript:[NSString stringWithFormat:TEMPLATE_CONTEXT_DESTROY, contextId]
source:[@"_Context://" stringByAppendingString:contextId]];
}
@end