diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/utils/DoricConstant.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/utils/DoricConstant.java index 9e163c44..a5255be9 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/utils/DoricConstant.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/utils/DoricConstant.java @@ -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"; diff --git a/iOS/Pod/Classes/DoricConstant.m b/iOS/Pod/Classes/DoricConstant.m index 83a47267..01953522 100644 --- a/iOS/Pod/Classes/DoricConstant.m +++ b/iOS/Pod/Classes/DoricConstant.m @@ -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"; diff --git a/iOS/Pod/Classes/DoricJSEngine.m b/iOS/Pod/Classes/DoricJSEngine.m index f4bd573a..f72d6e40 100644 --- a/iOS/Pod/Classes/DoricJSEngine.m +++ b/iOS/Pod/Classes/DoricJSEngine.m @@ -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