diff --git a/iOS/Example/Example/ViewController.m b/iOS/Example/Example/ViewController.m index 331913a0..5e7f1ab4 100644 --- a/iOS/Example/Example/ViewController.m +++ b/iOS/Example/Example/ViewController.m @@ -33,6 +33,8 @@ - (void)viewDidLoad { NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:@"demo" ofType:@"js"]; NSString *jsContent = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; self.doricContext = [[DoricContext alloc] initWithScript:jsContent source:@"demo"]; + [self.doricContext callEntity:@"__init__",@{@"width": [NSNumber numberWithFloat:self.view.width], + @"height":[NSNumber numberWithFloat:self.view.height]},nil]; } - (void)test:(UIView *)view { diff --git a/iOS/Pod/Classes/DoricBridgeExtension.m b/iOS/Pod/Classes/DoricBridgeExtension.m index f53fde8e..bc277c31 100644 --- a/iOS/Pod/Classes/DoricBridgeExtension.m +++ b/iOS/Pod/Classes/DoricBridgeExtension.m @@ -9,6 +9,12 @@ #import "DoricRegistry.h" #import "DoricContextManager.h" #import "DoricNativePlugin.h" +#import "DoricPromise.h" +#import "DoricUtil.h" + +#import + +#import @implementation DoricBridgeExtension @@ -17,11 +23,71 @@ - (id)callNativeWithContextId:(NSString *)contextId module:(NSString *)module me DoricRegistry *registry = context.driver.registry; Class pluginClass = [registry acquireNativePlugin:module]; DoricNativePlugin *nativePlugin = [context.pluginInstanceMap objectForKey:module]; - if(nativePlugin == nil){ + if(nativePlugin == nil) { nativePlugin = [[pluginClass alloc] initWithContext:context]; [context.pluginInstanceMap setObject:nativePlugin forKey:module]; } - - return nil; + unsigned int count; + id ret = nil; + Method *methods = class_copyMethodList(pluginClass, &count); + for (int i=0; i < count; i++) { + NSString *methodName = [NSString stringWithCString:sel_getName(method_getName(methods[i])) encoding:NSUTF8StringEncoding]; + NSArray *array = [methodName componentsSeparatedByString:@":"]; + if(array && [array count]>0) { + if([array[0] isEqualToString:method]) { + SEL selector = NSSelectorFromString(methodName); + NSMethodSignature *methodSignature = [nativePlugin methodSignatureForSelector:selector]; + if (methodSignature) { + NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature]; + invocation.selector = selector; + invocation.target = nativePlugin; + __weak __typeof__ (self) _self = self; + void(^block)(void) = ^(){ + __strong __typeof__ (_self) self = _self; + @try { + for(int i = 2;i < methodSignature.numberOfArguments;i++) { + if(i-2 > [array count]) { + break; + } + id args = [self createParamWithMethodName:array[i-2] context:context callbackId:callbackId argument:argument]; + [invocation setArgument:&args atIndex:i]; + } + [invocation invoke]; + } @catch (NSException *exception) { + DoricLog(@"CallNative Error:%@", exception.reason); + } + }; + + const char *retType = methodSignature.methodReturnType; + if (!strcmp(retType, @encode(void))) { + ret = nil; + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), block); + } else if (!strcmp(retType, @encode(id))) { + void *retValue; + block(); + [invocation getReturnValue:&retValue]; + id returnValue = (__bridge id)retValue; + ret = [JSValue valueWithObject:[returnValue copy] inContext:[JSContext currentContext]]; + } else { + DoricLog(@"CallNative Error:%@", @"Must return object type"); + ret = nil; + } + } + break; + } + } + } + if (methods) { + free(methods); + } + return ret; } + +- (id) createParamWithMethodName:(NSString *)method context:(DoricContext *)context callbackId:(NSString *)callbackId argument:(id)argument { + if([method isEqualToString:@"withPromise"]){ + return [[DoricPromise alloc] initWithContext:context callbackId:callbackId]; + } + return argument; +} + @end diff --git a/iOS/Pod/Classes/DoricDriver.m b/iOS/Pod/Classes/DoricDriver.m index 2c3b8867..88acb57c 100644 --- a/iOS/Pod/Classes/DoricDriver.m +++ b/iOS/Pod/Classes/DoricDriver.m @@ -47,12 +47,17 @@ + (instancetype)instance{ - (DoricAsyncResult *)invokeDoricMethod:(NSString *)method arguments:(va_list)args { DoricAsyncResult *ret = [[DoricAsyncResult alloc] init]; + NSMutableArray *array = [[NSMutableArray alloc] init]; + id arg; + while((arg = va_arg(args, id)) != nil){ + [array addObject:arg]; + } __weak typeof(self) _self = self; dispatch_async(self.jsExecutor.jsQueue, ^(){ __strong typeof(_self) self = _self; if (!self) return; @try { - JSValue *jsValue = [self.jsExecutor invokeDoricMethod:method arguments:args]; + JSValue *jsValue = [self.jsExecutor invokeDoricMethod:method argumentsArray:array]; [ret setupResult:jsValue]; } @catch (NSException *exception) { [ret setupError:exception]; diff --git a/iOS/Pod/Classes/DoricModalPlugin.m b/iOS/Pod/Classes/DoricModalPlugin.m index 71ed7a0e..5b71cf1b 100644 --- a/iOS/Pod/Classes/DoricModalPlugin.m +++ b/iOS/Pod/Classes/DoricModalPlugin.m @@ -11,9 +11,10 @@ @implementation DoricModalPlugin -- (void)toast:(NSString *)message promise:(DoricPromise *)promise { +- (void)toast:(NSString *)message withPromise:(DoricPromise *)promise { dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"toast:%@",message); + [promise resolve:@"Resolved"]; }); } diff --git a/iOS/Pod/Classes/DoricPromise.m b/iOS/Pod/Classes/DoricPromise.m index 85f6eaa7..10589594 100644 --- a/iOS/Pod/Classes/DoricPromise.m +++ b/iOS/Pod/Classes/DoricPromise.m @@ -17,7 +17,7 @@ @interface DoricPromise() @implementation DoricPromise - (instancetype)initWithContext:(DoricContext *)context callbackId:(NSString *)callbackId { - if(self = [super init]){ + if(self = [super init]) { _context = context; _callbackId = callbackId; } @@ -25,10 +25,10 @@ - (instancetype)initWithContext:(DoricContext *)context callbackId:(NSString *)c } - (void)resolve:(id)result { - [self.context.driver invokeDoricMethod:DORIC_BRIDGE_RESOLVE,self.context.contextId,self.callbackId,result,nil]; + [self.context.driver invokeDoricMethod:DORIC_BRIDGE_RESOLVE, self.context.contextId, self.callbackId, result,nil]; } - (void)reject:(id)result { - [self.context.driver invokeDoricMethod:DORIC_BRIDGE_REJECT,self.context.contextId,self.callbackId,result,nil]; + [self.context.driver invokeDoricMethod:DORIC_BRIDGE_REJECT, self.context.contextId, self.callbackId, result,nil]; } @end diff --git a/iOS/Pod/Classes/DoricRegistry.m b/iOS/Pod/Classes/DoricRegistry.m index d0b2e982..f269bb95 100644 --- a/iOS/Pod/Classes/DoricRegistry.m +++ b/iOS/Pod/Classes/DoricRegistry.m @@ -7,7 +7,7 @@ #import "DoricRegistry.h" #import "DoricModalPlugin.h" -#import +#import "DoricShaderPlugin.h" @interface DoricRegistry () @@ -29,6 +29,7 @@ - (instancetype)init { - (void)innerRegister { [self registerNativePlugin:DoricModalPlugin.class withName:@"modal"]; + [self registerNativePlugin:DoricShaderPlugin.class withName:@"shader"]; } - (void)registerJSBundle:(NSString *)bundle withName:(NSString *)name { diff --git a/iOS/Pod/Classes/DoricShaderPlugin.m b/iOS/Pod/Classes/DoricShaderPlugin.m index ae7f4fab..e9dc92b9 100644 --- a/iOS/Pod/Classes/DoricShaderPlugin.m +++ b/iOS/Pod/Classes/DoricShaderPlugin.m @@ -9,4 +9,8 @@ @implementation DoricShaderPlugin +- (void)render:(NSDictionary *)argument withPromise:(DoricPromise *)promise { + NSLog(@"%@",argument); +} + @end diff --git a/js-framework/demo.ts b/js-framework/demo.ts index 1197c59b..90056bdc 100644 --- a/js-framework/demo.ts +++ b/js-framework/demo.ts @@ -74,6 +74,9 @@ class MyPage extends VMPanel{ log("Hello.HEGO") logw("Hello.HEGO") loge("Hello.HEGO") + context.modal.toast('This is a toast.').then((r) => { + loge(r) + }) } } type SnakeNode = {