From 2a53b24affa364dd75896b2556dc31a57e1b4db8 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Mon, 29 Jul 2019 13:48:27 +0800 Subject: [PATCH] success run js on iOS --- iOS/Example/Example/ViewController.m | 42 ++++---------- .../Pods/Pods.xcodeproj/project.pbxproj | 24 ++++++++ iOS/Pod/Classes/DoricAsyncResult.h | 12 ++-- iOS/Pod/Classes/DoricAsyncResult.m | 12 ++-- iOS/Pod/Classes/DoricComponent.h | 15 +++++ iOS/Pod/Classes/DoricComponent.m | 12 ++++ iOS/Pod/Classes/DoricContext.h | 6 ++ iOS/Pod/Classes/DoricContext.m | 24 ++++++++ iOS/Pod/Classes/DoricContextManager.h | 21 +++++++ iOS/Pod/Classes/DoricContextManager.m | 57 +++++++++++++++++++ iOS/Pod/Classes/DoricDriver.h | 12 ++-- iOS/Pod/Classes/DoricDriver.m | 37 +++++++----- iOS/Pod/Classes/DoricJSCoreExecutor.m | 10 ++-- iOS/Pod/Classes/DoricJSEngine.h | 10 ++-- iOS/Pod/Classes/DoricJSEngine.m | 25 ++++---- iOS/Pod/Classes/DoricJSExecutorProtocal.h | 6 +- iOS/Pod/Classes/DoricNativePlugin.h | 16 ++++++ iOS/Pod/Classes/DoricNativePlugin.m | 12 ++++ iOS/Pod/Classes/DoricRegistry.h | 2 +- iOS/Pod/Classes/UIView+Doric.m | 28 ++++----- js-framework/demo.ts | 2 +- 21 files changed, 282 insertions(+), 103 deletions(-) create mode 100644 iOS/Pod/Classes/DoricComponent.h create mode 100644 iOS/Pod/Classes/DoricComponent.m create mode 100644 iOS/Pod/Classes/DoricContextManager.h create mode 100644 iOS/Pod/Classes/DoricContextManager.m create mode 100644 iOS/Pod/Classes/DoricNativePlugin.h create mode 100644 iOS/Pod/Classes/DoricNativePlugin.m diff --git a/iOS/Example/Example/ViewController.m b/iOS/Example/Example/ViewController.m index 847f0af8..85e51607 100644 --- a/iOS/Example/Example/ViewController.m +++ b/iOS/Example/Example/ViewController.m @@ -10,7 +10,7 @@ #import "UIView+Doric.h" #import "DoricJSCoreExecutor.h" #import "DoricUtil.h" -#import "DoricJSEngine.h" +#import "DoricContext.h" @interface ViewController () @@ -26,39 +26,17 @@ - (void)viewDidLoad { label.centerX = self.view.width/2; label.centerY = self.view.height/2; [self.view addSubview:label]; -// DoricJSCoreExecutor *jse = [[DoricJSCoreExecutor alloc] init]; -// @try{ -// NSString *ret = [jse loadJSScript:@"typef Reflect" source:@"test"]; -// NSLog(@"js result %@", ret); -// }@catch(NSException *e){ -// NSLog(@"catch Exception: %@,reason is %@",e.name,e.reason); -// } -// DoricLog(@"%@",@"testxxxxx"); -// DoricLog(@"test2rwr"); - DoricJSEngine *jsengine = [[DoricJSEngine alloc] init]; - [self test:@"method",@"1",@"2",nil]; + UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(test:)]; + [label addGestureRecognizer:recognizer]; + label.userInteractionEnabled = YES; +} + +- (void)test:(UIView *)view { + NSLog(@"test"); NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:@"demo" ofType:@"js"]; NSString *jsContent = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; - NSLog(@"%@",jsContent); + DoricContext *context = [[DoricContext alloc] initWithScript:jsContent source:@"demo"]; + [context callEntity:@"log",nil]; } - --(void)test:(NSString *)method,... { - va_list args; - va_start(args, method); - [self test2:method args:args]; - va_end(args); -} --(void)test2:(NSString *)method args:(va_list)args { - NSMutableArray *array = [[NSMutableArray alloc] init]; - id arg = va_arg(args, id); - while(arg != nil){ - [array addObject:arg]; - arg = va_arg(args, id); - } - for(id obj in array){ - DoricLog(@"test:%@",obj); - } -} - @end diff --git a/iOS/Example/Pods/Pods.xcodeproj/project.pbxproj b/iOS/Example/Pods/Pods.xcodeproj/project.pbxproj index 51147098..149a5419 100644 --- a/iOS/Example/Pods/Pods.xcodeproj/project.pbxproj +++ b/iOS/Example/Pods/Pods.xcodeproj/project.pbxproj @@ -26,6 +26,12 @@ B11353DC82971A59A3B9A964C10C27E8 /* DoricConstant.m in Sources */ = {isa = PBXBuildFile; fileRef = 90BA27E3131D8AD1F16CAAB652BC8A6F /* DoricConstant.m */; }; C7ED691918B3C06F6BC8F1AB6053EF9B /* DoricRegistry.m in Sources */ = {isa = PBXBuildFile; fileRef = 1AD3A731BCB183E444C5710694419A75 /* DoricRegistry.m */; }; DFF3C5963916D9D1639FC2AD82CE565D /* DoricDriver.m in Sources */ = {isa = PBXBuildFile; fileRef = 62660F1C7619EBD08D60183EBCA25264 /* DoricDriver.m */; }; + E2D4F74022EE989100F3505C /* DoricNativePlugin.h in Headers */ = {isa = PBXBuildFile; fileRef = E2D4F73E22EE989100F3505C /* DoricNativePlugin.h */; }; + E2D4F74122EE989100F3505C /* DoricNativePlugin.m in Sources */ = {isa = PBXBuildFile; fileRef = E2D4F73F22EE989100F3505C /* DoricNativePlugin.m */; }; + E2D4F74422EE98BE00F3505C /* DoricComponent.h in Headers */ = {isa = PBXBuildFile; fileRef = E2D4F74222EE98BE00F3505C /* DoricComponent.h */; }; + E2D4F74522EE98BE00F3505C /* DoricComponent.m in Sources */ = {isa = PBXBuildFile; fileRef = E2D4F74322EE98BE00F3505C /* DoricComponent.m */; }; + E2D4F74822EE993700F3505C /* DoricContextManager.h in Headers */ = {isa = PBXBuildFile; fileRef = E2D4F74622EE993700F3505C /* DoricContextManager.h */; }; + E2D4F74922EE993700F3505C /* DoricContextManager.m in Sources */ = {isa = PBXBuildFile; fileRef = E2D4F74722EE993700F3505C /* DoricContextManager.m */; }; EA731CD398F00F7D323997F93BFD41AF /* DoricRegistry.h in Headers */ = {isa = PBXBuildFile; fileRef = 59A0EF08B92B94EA54769E3E1FF457F1 /* DoricRegistry.h */; settings = {ATTRIBUTES = (Project, ); }; }; F4E16288FC129140C02208B679E8B542 /* DoricJSEngine.h in Headers */ = {isa = PBXBuildFile; fileRef = C15B34E4FFB9B99AB3F6D59A598F790A /* DoricJSEngine.h */; settings = {ATTRIBUTES = (Project, ); }; }; FB12A9CBA1D8C27712FA842DF1AFB1F5 /* DoricJSEngine.m in Sources */ = {isa = PBXBuildFile; fileRef = 88511A56B36DE729D1795F4F8DD34260 /* DoricJSEngine.m */; }; @@ -101,6 +107,12 @@ CB6504BD07E3990E6F058E4073421ADA /* libDoric.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libDoric.a; sourceTree = BUILT_PRODUCTS_DIR; }; DA28ABFF81EEE93AEFD6DEE2A3C006DD /* DoricJSCoreExecutor.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = DoricJSCoreExecutor.m; path = Pod/Classes/DoricJSCoreExecutor.m; sourceTree = ""; }; DFFAD17E7567EE856A59F246D50A9B0B /* Pods-Example-acknowledgements.plist */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.plist.xml; path = "Pods-Example-acknowledgements.plist"; sourceTree = ""; }; + E2D4F73E22EE989100F3505C /* DoricNativePlugin.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = DoricNativePlugin.h; path = Pod/Classes/DoricNativePlugin.h; sourceTree = ""; }; + E2D4F73F22EE989100F3505C /* DoricNativePlugin.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = DoricNativePlugin.m; path = Pod/Classes/DoricNativePlugin.m; sourceTree = ""; }; + E2D4F74222EE98BE00F3505C /* DoricComponent.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = DoricComponent.h; path = Pod/Classes/DoricComponent.h; sourceTree = ""; }; + E2D4F74322EE98BE00F3505C /* DoricComponent.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = DoricComponent.m; path = Pod/Classes/DoricComponent.m; sourceTree = ""; }; + E2D4F74622EE993700F3505C /* DoricContextManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = DoricContextManager.h; path = Pod/Classes/DoricContextManager.h; sourceTree = ""; }; + E2D4F74722EE993700F3505C /* DoricContextManager.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = DoricContextManager.m; path = Pod/Classes/DoricContextManager.m; sourceTree = ""; }; E340F908A05867E06E975FDCDD32315C /* Pods-Example.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; path = "Pods-Example.debug.xcconfig"; sourceTree = ""; }; ED84F8134A4F3E235AB8C90C513CCE8D /* DoricAsyncResult.h */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.h; name = DoricAsyncResult.h; path = Pod/Classes/DoricAsyncResult.h; sourceTree = ""; }; FE99AB24E829DD35491764E2DED6ED75 /* Pods-ExampleUITests-dummy.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; path = "Pods-ExampleUITests-dummy.m"; sourceTree = ""; }; @@ -194,6 +206,12 @@ 6BCC7D6EBF85DA7FAFF5BFC78EA00CBE /* Pod */, 768CE4306033647F2B1F5CD75DAFB96C /* Resources */, 2E38CAB4D62BB195E976B365C3873983 /* Support Files */, + E2D4F73E22EE989100F3505C /* DoricNativePlugin.h */, + E2D4F73F22EE989100F3505C /* DoricNativePlugin.m */, + E2D4F74222EE98BE00F3505C /* DoricComponent.h */, + E2D4F74322EE98BE00F3505C /* DoricComponent.m */, + E2D4F74622EE993700F3505C /* DoricContextManager.h */, + E2D4F74722EE993700F3505C /* DoricContextManager.m */, ); name = Doric; path = ../..; @@ -306,7 +324,9 @@ buildActionMask = 2147483647; files = ( B077ABFB167A93C6D567B40768552671 /* DoricAsyncResult.h in Headers */, + E2D4F74022EE989100F3505C /* DoricNativePlugin.h in Headers */, 239169CBB6F7EB156C14D028FDF45116 /* DoricConstant.h in Headers */, + E2D4F74822EE993700F3505C /* DoricContextManager.h in Headers */, 88B52F11DA33AC294B0218D33528AE40 /* DoricContext.h in Headers */, 588D3C18C53399B7326E2C3C51EC9ED6 /* DoricDriver.h in Headers */, 091F9B16A4F4143E4B2455D01DB9E702 /* DoricJSCoreExecutor.h in Headers */, @@ -314,6 +334,7 @@ 0D09DCE69AE127709FC4DFDF99C20A43 /* DoricJSExecutorProtocal.h in Headers */, EA731CD398F00F7D323997F93BFD41AF /* DoricRegistry.h in Headers */, FCE039F2439D368237828ADB77E8B6A2 /* DoricUtil.h in Headers */, + E2D4F74422EE98BE00F3505C /* DoricComponent.h in Headers */, 44E1706571817930D669A7B0D370E182 /* UIView+Doric.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; @@ -474,13 +495,16 @@ 9D89A3FF79363D73AA1A2AA18BEF1892 /* Doric-dummy.m in Sources */, 230F21EB13596FBD921AAC0F03564DF6 /* DoricAsyncResult.m in Sources */, B11353DC82971A59A3B9A964C10C27E8 /* DoricConstant.m in Sources */, + E2D4F74122EE989100F3505C /* DoricNativePlugin.m in Sources */, 379D57B4BA7F04B71421ADD9E1A4D17F /* DoricContext.m in Sources */, DFF3C5963916D9D1639FC2AD82CE565D /* DoricDriver.m in Sources */, 2AD7CC40AC77844B33FF143A3607DC04 /* DoricJSCoreExecutor.m in Sources */, + E2D4F74922EE993700F3505C /* DoricContextManager.m in Sources */, FB12A9CBA1D8C27712FA842DF1AFB1F5 /* DoricJSEngine.m in Sources */, C7ED691918B3C06F6BC8F1AB6053EF9B /* DoricRegistry.m in Sources */, 92CB625B157F7D9ED55DFD3718726F18 /* DoricUtil.m in Sources */, 54D16DC1BDD43F45E8DFFE38CC5323FC /* UIView+Doric.m in Sources */, + E2D4F74522EE98BE00F3505C /* DoricComponent.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/iOS/Pod/Classes/DoricAsyncResult.h b/iOS/Pod/Classes/DoricAsyncResult.h index dfec4ad4..50ddda96 100644 --- a/iOS/Pod/Classes/DoricAsyncResult.h +++ b/iOS/Pod/Classes/DoricAsyncResult.h @@ -15,12 +15,12 @@ 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; +- (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 diff --git a/iOS/Pod/Classes/DoricAsyncResult.m b/iOS/Pod/Classes/DoricAsyncResult.m index 7d9c21d1..e52a28b9 100644 --- a/iOS/Pod/Classes/DoricAsyncResult.m +++ b/iOS/Pod/Classes/DoricAsyncResult.m @@ -16,7 +16,7 @@ @interface DoricAsyncResult() @implementation DoricAsyncResult --(void)setupResult:(id)result { +- (void)setupResult:(id)result { self.result = result; if(self.resultCallback){ self.resultCallback(result); @@ -26,7 +26,7 @@ -(void)setupResult:(id)result { } } --(void)setupError:(NSException *)exception { +- (void)setupError:(NSException *)exception { self.result = exception; if(self.exceptionCallback){ self.exceptionCallback(exception); @@ -35,15 +35,15 @@ -(void)setupError:(NSException *)exception { self.finishCallback(); } } --(BOOL)hasResult { +- (BOOL)hasResult { return self.result; } --(id)getResult { +- (id)getResult { return self.result; } --(void)setResultCallback:(DoricResultCallback)callback exceptionCallback:(DoricExceptionCallback)exceptionCallback { +- (void)setResultCallback:(DoricResultCallback)callback exceptionCallback:(DoricExceptionCallback)exceptionCallback { self.resultCallback = callback; self.exceptionCallback = exceptionCallback; if([self.result isKindOfClass: [NSException class]]){ @@ -53,7 +53,7 @@ -(void)setResultCallback:(DoricResultCallback)callback exceptionCallback:(DoricE } } --(void)setFinishCallback:(DoricFinishCallback)callback { +- (void)setFinishCallback:(DoricFinishCallback)callback { self.finishCallback = callback; if(self.result){ callback(); diff --git a/iOS/Pod/Classes/DoricComponent.h b/iOS/Pod/Classes/DoricComponent.h new file mode 100644 index 00000000..68209d46 --- /dev/null +++ b/iOS/Pod/Classes/DoricComponent.h @@ -0,0 +1,15 @@ +// +// DoricComponent.h +// Doric +// +// Created by pengfei.zhou on 2019/7/29. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface DoricComponent : NSObject +@end + +NS_ASSUME_NONNULL_END diff --git a/iOS/Pod/Classes/DoricComponent.m b/iOS/Pod/Classes/DoricComponent.m new file mode 100644 index 00000000..738cf8f4 --- /dev/null +++ b/iOS/Pod/Classes/DoricComponent.m @@ -0,0 +1,12 @@ +// +// DoricComponent.m +// Doric +// +// Created by pengfei.zhou on 2019/7/29. +// + +#import "DoricComponent.h" + +@implementation DoricComponent + +@end diff --git a/iOS/Pod/Classes/DoricContext.h b/iOS/Pod/Classes/DoricContext.h index 2eb7a9b8..fb9dd871 100644 --- a/iOS/Pod/Classes/DoricContext.h +++ b/iOS/Pod/Classes/DoricContext.h @@ -6,12 +6,18 @@ // #import +#import "DoricDriver.h" NS_ASSUME_NONNULL_BEGIN @interface DoricContext : NSObject @property (nonatomic,strong) NSString *contextId; +@property (nonatomic,strong) DoricDriver *driver; + +- (instancetype)initWithScript:(NSString *)script source:(NSString *)source; + +- (DoricAsyncResult *)callEntity:(NSString *)method, ...; @end diff --git a/iOS/Pod/Classes/DoricContext.m b/iOS/Pod/Classes/DoricContext.m index f5c47341..2c0f3161 100644 --- a/iOS/Pod/Classes/DoricContext.m +++ b/iOS/Pod/Classes/DoricContext.m @@ -6,7 +6,31 @@ // #import "DoricContext.h" +#import "DoricContextManager.h" @implementation DoricContext +- (instancetype)init { + if(self = [super init]){ + _driver = [DoricDriver instance]; + } + return self; +} + +- (instancetype)initWithScript:(NSString *)script source:(NSString *)source { + return [[DoricContextManager instance] createContext:script source:source]; +} + +- (void)dealloc { + [[DoricContextManager instance] destroyContext:self]; +} + +- (DoricAsyncResult *)callEntity:(NSString *)method, ... { + va_list args; + va_start(args, method); + DoricAsyncResult *ret = [self.driver invokeContextEntity:self.contextId method:method arguments:args]; + va_end(args); + return ret; +} + @end diff --git a/iOS/Pod/Classes/DoricContextManager.h b/iOS/Pod/Classes/DoricContextManager.h new file mode 100644 index 00000000..a4064200 --- /dev/null +++ b/iOS/Pod/Classes/DoricContextManager.h @@ -0,0 +1,21 @@ +// +// DoricContextManager.h +// Doric +// +// Created by pengfei.zhou on 2019/7/29. +// + +#import +#import "DoricContext.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface DoricContextManager : NSObject ++ (instancetype) instance; + +- (DoricContext *)createContext:(NSString *)script source:(NSString *)source; + +- (void) destroyContext:(DoricContext *)context; +@end + +NS_ASSUME_NONNULL_END diff --git a/iOS/Pod/Classes/DoricContextManager.m b/iOS/Pod/Classes/DoricContextManager.m new file mode 100644 index 00000000..768257d5 --- /dev/null +++ b/iOS/Pod/Classes/DoricContextManager.m @@ -0,0 +1,57 @@ +// +// DoricContextManager.m +// Doric +// +// Created by pengfei.zhou on 2019/7/29. +// + +#import "DoricContextManager.h" +#import "DoricContext.h" + +@interface DoricContextManager() + +@property (nonatomic) NSInteger counter; +@property (nonatomic,strong) NSMutableDictionary *doricContextMap; +@property (nonatomic,strong) dispatch_queue_t mapQueue; +@end + +@implementation DoricContextManager + +- (instancetype)init { + if(self = [super init]){ + _doricContextMap = [[NSMutableDictionary alloc] init]; + _counter = 0; + _mapQueue = dispatch_queue_create("doric.contextmap", DISPATCH_QUEUE_SERIAL); + } + return self; +} + ++ (instancetype)instance{ + static DoricContextManager *_instance; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + _instance = [[DoricContextManager alloc] init]; + }); + return _instance; +} + +- (DoricContext *)createContext:(NSString *)script source:(NSString *)source { + DoricContext *doricContext = [[DoricContext alloc] init]; + doricContext.contextId = [NSString stringWithFormat:@"%ld", (long)self.counter++]; + [doricContext.driver createContext:doricContext.contextId script:script source:source]; + dispatch_sync(self.mapQueue, ^(){ + [self.doricContextMap setValue:doricContext forKey:doricContext.contextId]; + }); + return doricContext; +} + +- (void) destroyContext:(DoricContext *)context { + NSString *contextId = context.contextId; + [[context.driver destroyContext:contextId] setFinishCallback:^(){ + dispatch_sync(self.mapQueue, ^(){ + [self.doricContextMap removeObjectForKey:contextId]; + }); + }]; +} + +@end diff --git a/iOS/Pod/Classes/DoricDriver.h b/iOS/Pod/Classes/DoricDriver.h index c24d0558..b79333b0 100644 --- a/iOS/Pod/Classes/DoricDriver.h +++ b/iOS/Pod/Classes/DoricDriver.h @@ -18,15 +18,17 @@ typedef NS_ENUM(NSInteger, QueueMode) { NS_ASSUME_NONNULL_BEGIN @interface DoricDriver : NSObject -+(instancetype) instance; ++ (instancetype) instance; @property (nonatomic,strong) DoricRegistry *registry; --(DoricAsyncResult *)createContext:(NSString *)contextId script:(NSString *)script source:(NSString *)source; --(DoricAsyncResult *)destroyContext:(NSString *)contextId; +- (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, ...; +- (DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString *)method arguments:(va_list) args; --(DoricAsyncResult *)invokeDoricMethod:(NSString *)method, ...; --(DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString *)method, ...; @end NS_ASSUME_NONNULL_END diff --git a/iOS/Pod/Classes/DoricDriver.m b/iOS/Pod/Classes/DoricDriver.m index 9d2296df..2c3b8867 100644 --- a/iOS/Pod/Classes/DoricDriver.m +++ b/iOS/Pod/Classes/DoricDriver.m @@ -17,7 +17,14 @@ @implementation DoricDriver @dynamic registry; --(DoricRegistry *)registry { +- (instancetype)init { + if(self = [super init]){ + _jsExecutor = [[DoricJSEngine alloc] init]; + } + return self; +} + +- (DoricRegistry *)registry { return self.jsExecutor.registry; } @@ -30,7 +37,7 @@ + (instancetype)instance{ return _instance; } --(DoricAsyncResult *)invokeDoricMethod:(NSString *)method, ... { +- (DoricAsyncResult *)invokeDoricMethod:(NSString *)method, ... { va_list args; va_start(args, method); DoricAsyncResult *ret = [self invokeDoricMethod:method arguments:args]; @@ -38,7 +45,7 @@ + (instancetype)instance{ return ret; } --(DoricAsyncResult *)invokeDoricMethod:(NSString *)method arguments:(va_list)args { +- (DoricAsyncResult *)invokeDoricMethod:(NSString *)method arguments:(va_list)args { DoricAsyncResult *ret = [[DoricAsyncResult alloc] init]; __weak typeof(self) _self = self; dispatch_async(self.jsExecutor.jsQueue, ^(){ @@ -54,20 +61,24 @@ + (instancetype)instance{ return ret; } --(DoricAsyncResult *)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]; +- (DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString *)method,... { va_list args; va_start(args, method); + DoricAsyncResult *ret = [self invokeContextEntity:contextId method:method arguments:args]; + va_end(args); + return ret; +} + +- (DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString *)method arguments:(va_list)args { + DoricAsyncResult *ret = [[DoricAsyncResult alloc] init]; + NSMutableArray *array = [[NSMutableArray alloc] init]; + [array addObject:contextId]; + [array addObject: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; @@ -76,13 +87,13 @@ + (instancetype)instance{ JSValue *jsValue = [self.jsExecutor invokeDoricMethod:DORIC_CONTEXT_INVOKE argumentsArray:array]; [ret setupResult:jsValue]; } @catch (NSException *exception) { - [ret setupError:exception]; + [ret setupError:exception]; } }); return ret; } --(DoricAsyncResult *)createContext:(NSString *)contextId script:(NSString *)script source:(NSString *)source { +- (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, ^(){ @@ -98,7 +109,7 @@ -(DoricAsyncResult *)createContext:(NSString *)contextId script:(NSString *)scri return ret; } --(DoricAsyncResult *)destroyContext:(NSString *)contextId { +- (DoricAsyncResult *)destroyContext:(NSString *)contextId { DoricAsyncResult *ret = [[DoricAsyncResult alloc] init]; __weak typeof(self) _self = self; dispatch_async(self.jsExecutor.jsQueue, ^(){ diff --git a/iOS/Pod/Classes/DoricJSCoreExecutor.m b/iOS/Pod/Classes/DoricJSCoreExecutor.m index c170da03..40f61bbc 100644 --- a/iOS/Pod/Classes/DoricJSCoreExecutor.m +++ b/iOS/Pod/Classes/DoricJSCoreExecutor.m @@ -14,32 +14,32 @@ @interface DoricJSCoreExecutor() @end @implementation DoricJSCoreExecutor --(instancetype)init { +- (instancetype)init { if(self = [super init]){ _jsContext = [[JSContext alloc] init]; } return self; } --(void)checkJSException { +- (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 *)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:(id)obj { +- (void)injectGlobalJSObject:(NSString *)name obj:(id)obj { self.jsContext[name] = obj; [self checkJSException]; } --(JSValue *)invokeObject:(NSString *)objName method:(NSString *)funcName args:(NSArray *)args { +- (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]; diff --git a/iOS/Pod/Classes/DoricJSEngine.h b/iOS/Pod/Classes/DoricJSEngine.h index fe80d7e3..4bb1a144 100644 --- a/iOS/Pod/Classes/DoricJSEngine.h +++ b/iOS/Pod/Classes/DoricJSEngine.h @@ -17,15 +17,15 @@ NS_ASSUME_NONNULL_BEGIN @property(nonatomic,strong) DoricRegistry *registry; --(void)prepareContext:(NSString *)contextId script:(NSString *)script source:(NSString *)source; +- (void)prepareContext:(NSString *)contextId script:(NSString *)script source:(NSString *)source; --(void)destroyContext:(NSString *)contextId; +- (void)destroyContext:(NSString *)contextId; --(JSValue *)invokeDoricMethod:(NSString *)method, ...; +- (JSValue *)invokeDoricMethod:(NSString *)method, ...; --(JSValue *)invokeDoricMethod:(NSString *)method arguments:(va_list)args; +- (JSValue *)invokeDoricMethod:(NSString *)method arguments:(va_list)args; --(JSValue *)invokeDoricMethod:(NSString *)method argumentsArray:(NSArray *)args; +- (JSValue *)invokeDoricMethod:(NSString *)method argumentsArray:(NSArray *)args; @end NS_ASSUME_NONNULL_END diff --git a/iOS/Pod/Classes/DoricJSEngine.m b/iOS/Pod/Classes/DoricJSEngine.m index 4214259d..5f6c47fd 100644 --- a/iOS/Pod/Classes/DoricJSEngine.m +++ b/iOS/Pod/Classes/DoricJSEngine.m @@ -18,20 +18,21 @@ @interface DoricJSEngine() @implementation DoricJSEngine --(instancetype)init { +- (instancetype)init { if(self = [super init]){ _jsQueue = dispatch_queue_create("doric.jsengine", DISPATCH_QUEUE_SERIAL); dispatch_async(_jsQueue, ^(){ self.timers = [[NSMutableDictionary alloc] init]; self.jsExecutor = [[DoricJSCoreExecutor alloc] init]; self.registry = [[DoricRegistry alloc] init]; + [self initJSExecutor]; [self initDoricEnvironment]; }); } return self; } --(void)initJSExecutor { +- (void)initJSExecutor { __weak typeof(self) _self = self; [self.jsExecutor injectGlobalJSObject:INJECT_LOG obj:^(NSString * type, NSString * message) { @@ -78,7 +79,7 @@ -(void)initJSExecutor { } --(void)initDoricEnvironment { +- (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]; @@ -86,13 +87,13 @@ -(void)initDoricEnvironment { source: [@"Module://" stringByAppendingString:DORIC_MODULE_LIB]]; } --(void)loadBuiltinJS:(NSString *)fileName { +- (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]]; } --(JSValue *)invokeDoricMethod:(NSString *)method,... { +- (JSValue *)invokeDoricMethod:(NSString *)method,... { va_list args; va_start(args, method); JSValue *ret = [self invokeDoricMethod:method arguments:args]; @@ -100,7 +101,7 @@ -(JSValue *)invokeDoricMethod:(NSString *)method,... { return ret; } --(JSValue *)invokeDoricMethod:(NSString *)method arguments:(va_list)args { +- (JSValue *)invokeDoricMethod:(NSString *)method arguments:(va_list)args { NSMutableArray *array = [[NSMutableArray alloc] init]; id arg = va_arg(args, id); while(arg != nil){ @@ -110,31 +111,31 @@ -(JSValue *)invokeDoricMethod:(NSString *)method arguments:(va_list)args { return [self.jsExecutor invokeObject:GLOBAL_DORIC method:method args:array]; } --(JSValue *)invokeDoricMethod:(NSString *)method argumentsArray:(NSArray *)args { +- (JSValue *)invokeDoricMethod:(NSString *)method argumentsArray:(NSArray *)args { return [self.jsExecutor invokeObject:GLOBAL_DORIC method:method args:args]; } --(NSString *)packageContextScript:(NSString *)contextId content:(NSString *)content { +- (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 *)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 { +- (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 { +- (void)destroyContext:(NSString *)contextId { [self.jsExecutor loadJSScript:[NSString stringWithFormat:TEMPLATE_CONTEXT_DESTROY, contextId] source:[@"_Context://" stringByAppendingString:contextId]]; } --(void)callbackTimer:(NSTimer *)timer { +- (void)callbackTimer:(NSTimer *)timer { NSDictionary *userInfo = timer.userInfo; NSNumber *timerId = [userInfo valueForKey:@"timerId"]; NSNumber *repeat = [userInfo valueForKey:@"repeat"]; diff --git a/iOS/Pod/Classes/DoricJSExecutorProtocal.h b/iOS/Pod/Classes/DoricJSExecutorProtocal.h index b394e344..9c193e4a 100644 --- a/iOS/Pod/Classes/DoricJSExecutorProtocal.h +++ b/iOS/Pod/Classes/DoricJSExecutorProtocal.h @@ -12,11 +12,11 @@ NS_ASSUME_NONNULL_BEGIN @protocol DoricJSExecutorProtocal --(NSString *) loadJSScript:(NSString *)script source:(NSString *)source; +- (NSString *) loadJSScript:(NSString *)script source:(NSString *)source; --(void) injectGlobalJSObject:(NSString *)name obj:(id)obj; +- (void) injectGlobalJSObject:(NSString *)name obj:(id)obj; --(JSValue *) invokeObject: (NSString *)objName method:(NSString *)funcName args:(NSArray *)args; +- (JSValue *) invokeObject: (NSString *)objName method:(NSString *)funcName args:(NSArray *)args; @end diff --git a/iOS/Pod/Classes/DoricNativePlugin.h b/iOS/Pod/Classes/DoricNativePlugin.h new file mode 100644 index 00000000..ca69b437 --- /dev/null +++ b/iOS/Pod/Classes/DoricNativePlugin.h @@ -0,0 +1,16 @@ +// +// DoricNativePlugin.h +// Doric +// +// Created by pengfei.zhou on 2019/7/29. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface DoricNativePlugin : NSObject + +@end + +NS_ASSUME_NONNULL_END diff --git a/iOS/Pod/Classes/DoricNativePlugin.m b/iOS/Pod/Classes/DoricNativePlugin.m new file mode 100644 index 00000000..7b172aec --- /dev/null +++ b/iOS/Pod/Classes/DoricNativePlugin.m @@ -0,0 +1,12 @@ +// +// DoricNativePlugin.m +// Doric +// +// Created by pengfei.zhou on 2019/7/29. +// + +#import "DoricNativePlugin.h" + +@implementation DoricNativePlugin + +@end diff --git a/iOS/Pod/Classes/DoricRegistry.h b/iOS/Pod/Classes/DoricRegistry.h index bc5814de..a3875afd 100644 --- a/iOS/Pod/Classes/DoricRegistry.h +++ b/iOS/Pod/Classes/DoricRegistry.h @@ -11,7 +11,7 @@ NS_ASSUME_NONNULL_BEGIN @interface DoricRegistry : NSObject --(NSString *)acquireJSBundle:(NSString *)bundleName; +- (NSString *)acquireJSBundle:(NSString *)bundleName; @end diff --git a/iOS/Pod/Classes/UIView+Doric.m b/iOS/Pod/Classes/UIView+Doric.m index 18ae8c98..bacb2bb1 100644 --- a/iOS/Pod/Classes/UIView+Doric.m +++ b/iOS/Pod/Classes/UIView+Doric.m @@ -10,47 +10,47 @@ @implementation UIView(Doric) --(CGFloat)x { +- (CGFloat)x { return self.frame.origin.x; } --(void)setX:(CGFloat)x { +- (void)setX:(CGFloat)x { CGRect frame = self.frame; frame.origin.x = x; [self setFrame:frame]; } --(CGFloat)y { +- (CGFloat)y { return self.frame.origin.y; } --(void)setY:(CGFloat)y { +- (void)setY:(CGFloat)y { CGRect frame = self.frame; frame.origin.y = y; [self setFrame:frame]; } --(CGFloat)left { +- (CGFloat)left { return self.frame.origin.x; } --(void)setLeft:(CGFloat)left { +- (void)setLeft:(CGFloat)left { CGRect frame = self.frame; frame.origin.x = left; [self setFrame:frame]; } --(CGFloat)right { +- (CGFloat)right { return self.frame.origin.x + self.frame.size.width; } --(void)setRight:(CGFloat)right { +- (void)setRight:(CGFloat)right { CGRect frame = self.frame; frame.origin.x = right - self.frame.size.width; [self setFrame:frame]; } --(CGFloat)top { +- (CGFloat)top { return self.frame.origin.y; } @@ -60,11 +60,11 @@ - (void)setTop:(CGFloat)top { [self setFrame:frame]; } --(CGFloat)bottom { +- (CGFloat)bottom { return self.frame.origin.y + self.frame.size.height; } --(void)setBottom:(CGFloat)bottom { +- (void)setBottom:(CGFloat)bottom { CGRect frame = self.frame; frame.origin.y = bottom - self.frame.size.height; [self setFrame:frame]; @@ -74,17 +74,17 @@ - (CGFloat)width { return self.frame.size.width; } --(void)setWidth:(CGFloat)width { +- (void)setWidth:(CGFloat)width { CGRect frame = self.frame; frame.size.width = width; self.frame = frame; } --(CGFloat)height { +- (CGFloat)height { return self.frame.size.height; } --(void)setHeight:(CGFloat)height { +- (void)setHeight:(CGFloat)height { CGRect frame = self.frame; frame.size.height = height; self.frame = frame; diff --git a/js-framework/demo.ts b/js-framework/demo.ts index 612ebebe..1197c59b 100644 --- a/js-framework/demo.ts +++ b/js-framework/demo.ts @@ -48,6 +48,7 @@ class CounterVM extends ViewModel { } } +@Entry class MyPage extends VMPanel{ getVMClass() { @@ -369,7 +370,6 @@ class SnakeVM extends ViewModel{ } } -@Entry class SnakePanel extends VMPanel{ getVMClass() {