iOS Bridge part

This commit is contained in:
pengfei.zhou 2019-07-30 11:25:05 +08:00
parent c1e4ccd984
commit 6e6dc4edef
8 changed files with 91 additions and 9 deletions

View File

@ -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 {

View File

@ -9,6 +9,12 @@
#import "DoricRegistry.h"
#import "DoricContextManager.h"
#import "DoricNativePlugin.h"
#import "DoricPromise.h"
#import "DoricUtil.h"
#import <JavaScriptCore/JavaScriptCore.h>
#import <objc/runtime.h>
@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

View File

@ -47,12 +47,17 @@ + (instancetype)instance{
- (DoricAsyncResult<JSValue *> *)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];

View File

@ -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"];
});
}

View File

@ -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

View File

@ -7,7 +7,7 @@
#import "DoricRegistry.h"
#import "DoricModalPlugin.h"
#import <objc/runtime.h>
#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 {

View File

@ -9,4 +9,8 @@
@implementation DoricShaderPlugin
- (void)render:(NSDictionary *)argument withPromise:(DoricPromise *)promise {
NSLog(@"%@",argument);
}
@end

View File

@ -74,6 +74,9 @@ class MyPage extends VMPanel<CountModel, CounterView>{
log("Hello.HEGO")
logw("Hello.HEGO")
loge("Hello.HEGO")
context.modal.toast('This is a toast.').then((r) => {
loge(r)
})
}
}
type SnakeNode = {