iOS Bridge part
This commit is contained in:
parent
c1e4ccd984
commit
6e6dc4edef
@ -33,6 +33,8 @@ - (void)viewDidLoad {
|
|||||||
NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:@"demo" ofType:@"js"];
|
NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:@"demo" ofType:@"js"];
|
||||||
NSString *jsContent = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
|
NSString *jsContent = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
|
||||||
self.doricContext = [[DoricContext alloc] initWithScript:jsContent source:@"demo"];
|
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 {
|
- (void)test:(UIView *)view {
|
||||||
|
@ -9,6 +9,12 @@
|
|||||||
#import "DoricRegistry.h"
|
#import "DoricRegistry.h"
|
||||||
#import "DoricContextManager.h"
|
#import "DoricContextManager.h"
|
||||||
#import "DoricNativePlugin.h"
|
#import "DoricNativePlugin.h"
|
||||||
|
#import "DoricPromise.h"
|
||||||
|
#import "DoricUtil.h"
|
||||||
|
|
||||||
|
#import <JavaScriptCore/JavaScriptCore.h>
|
||||||
|
|
||||||
|
#import <objc/runtime.h>
|
||||||
|
|
||||||
@implementation DoricBridgeExtension
|
@implementation DoricBridgeExtension
|
||||||
|
|
||||||
@ -17,11 +23,71 @@ - (id)callNativeWithContextId:(NSString *)contextId module:(NSString *)module me
|
|||||||
DoricRegistry *registry = context.driver.registry;
|
DoricRegistry *registry = context.driver.registry;
|
||||||
Class pluginClass = [registry acquireNativePlugin:module];
|
Class pluginClass = [registry acquireNativePlugin:module];
|
||||||
DoricNativePlugin *nativePlugin = [context.pluginInstanceMap objectForKey:module];
|
DoricNativePlugin *nativePlugin = [context.pluginInstanceMap objectForKey:module];
|
||||||
if(nativePlugin == nil){
|
if(nativePlugin == nil) {
|
||||||
nativePlugin = [[pluginClass alloc] initWithContext:context];
|
nativePlugin = [[pluginClass alloc] initWithContext:context];
|
||||||
[context.pluginInstanceMap setObject:nativePlugin forKey:module];
|
[context.pluginInstanceMap setObject:nativePlugin forKey:module];
|
||||||
}
|
}
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return nil;
|
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
|
@end
|
||||||
|
@ -47,12 +47,17 @@ + (instancetype)instance{
|
|||||||
|
|
||||||
- (DoricAsyncResult<JSValue *> *)invokeDoricMethod:(NSString *)method arguments:(va_list)args {
|
- (DoricAsyncResult<JSValue *> *)invokeDoricMethod:(NSString *)method arguments:(va_list)args {
|
||||||
DoricAsyncResult *ret = [[DoricAsyncResult alloc] init];
|
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;
|
__weak typeof(self) _self = self;
|
||||||
dispatch_async(self.jsExecutor.jsQueue, ^(){
|
dispatch_async(self.jsExecutor.jsQueue, ^(){
|
||||||
__strong typeof(_self) self = _self;
|
__strong typeof(_self) self = _self;
|
||||||
if (!self) return;
|
if (!self) return;
|
||||||
@try {
|
@try {
|
||||||
JSValue *jsValue = [self.jsExecutor invokeDoricMethod:method arguments:args];
|
JSValue *jsValue = [self.jsExecutor invokeDoricMethod:method argumentsArray:array];
|
||||||
[ret setupResult:jsValue];
|
[ret setupResult:jsValue];
|
||||||
} @catch (NSException *exception) {
|
} @catch (NSException *exception) {
|
||||||
[ret setupError:exception];
|
[ret setupError:exception];
|
||||||
|
@ -11,9 +11,10 @@
|
|||||||
|
|
||||||
@implementation DoricModalPlugin
|
@implementation DoricModalPlugin
|
||||||
|
|
||||||
- (void)toast:(NSString *)message promise:(DoricPromise *)promise {
|
- (void)toast:(NSString *)message withPromise:(DoricPromise *)promise {
|
||||||
dispatch_async(dispatch_get_main_queue(), ^{
|
dispatch_async(dispatch_get_main_queue(), ^{
|
||||||
NSLog(@"toast:%@",message);
|
NSLog(@"toast:%@",message);
|
||||||
|
[promise resolve:@"Resolved"];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@ @interface DoricPromise()
|
|||||||
@implementation DoricPromise
|
@implementation DoricPromise
|
||||||
|
|
||||||
- (instancetype)initWithContext:(DoricContext *)context callbackId:(NSString *)callbackId {
|
- (instancetype)initWithContext:(DoricContext *)context callbackId:(NSString *)callbackId {
|
||||||
if(self = [super init]){
|
if(self = [super init]) {
|
||||||
_context = context;
|
_context = context;
|
||||||
_callbackId = callbackId;
|
_callbackId = callbackId;
|
||||||
}
|
}
|
||||||
@ -25,10 +25,10 @@ - (instancetype)initWithContext:(DoricContext *)context callbackId:(NSString *)c
|
|||||||
}
|
}
|
||||||
|
|
||||||
- (void)resolve:(id)result {
|
- (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 {
|
- (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
|
@end
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
#import "DoricRegistry.h"
|
#import "DoricRegistry.h"
|
||||||
#import "DoricModalPlugin.h"
|
#import "DoricModalPlugin.h"
|
||||||
#import <objc/runtime.h>
|
#import "DoricShaderPlugin.h"
|
||||||
|
|
||||||
@interface DoricRegistry ()
|
@interface DoricRegistry ()
|
||||||
|
|
||||||
@ -29,6 +29,7 @@ - (instancetype)init {
|
|||||||
|
|
||||||
- (void)innerRegister {
|
- (void)innerRegister {
|
||||||
[self registerNativePlugin:DoricModalPlugin.class withName:@"modal"];
|
[self registerNativePlugin:DoricModalPlugin.class withName:@"modal"];
|
||||||
|
[self registerNativePlugin:DoricShaderPlugin.class withName:@"shader"];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)registerJSBundle:(NSString *)bundle withName:(NSString *)name {
|
- (void)registerJSBundle:(NSString *)bundle withName:(NSString *)name {
|
||||||
|
@ -9,4 +9,8 @@
|
|||||||
|
|
||||||
@implementation DoricShaderPlugin
|
@implementation DoricShaderPlugin
|
||||||
|
|
||||||
|
- (void)render:(NSDictionary *)argument withPromise:(DoricPromise *)promise {
|
||||||
|
NSLog(@"%@",argument);
|
||||||
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
@ -74,6 +74,9 @@ class MyPage extends VMPanel<CountModel, CounterView>{
|
|||||||
log("Hello.HEGO")
|
log("Hello.HEGO")
|
||||||
logw("Hello.HEGO")
|
logw("Hello.HEGO")
|
||||||
loge("Hello.HEGO")
|
loge("Hello.HEGO")
|
||||||
|
context.modal.toast('This is a toast.').then((r) => {
|
||||||
|
loge(r)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
type SnakeNode = {
|
type SnakeNode = {
|
||||||
|
Reference in New Issue
Block a user