Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file not shown.
9 changes: 9 additions & 0 deletions dsbridge/InternalApis.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
#import "JSBUtil.h"

@implementation InternalApis

+ (NSArray *)ds_allMethodsForJS{
return @[@"hasNativeMethod:",
@"closePage:",
@"returnValue:",
@"dsinit:",
@"disableJavascriptDialogBlock:"];
}

- (id) hasNativeMethod:(id) args
{
return [self.webview onMessage:args type: DSB_API_HASNATIVEMETHOD];
Expand Down
7 changes: 7 additions & 0 deletions dsbridge/JSBUtil.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#import <Foundation/Foundation.h>

@protocol JSBUtilDelegate <NSObject>
/**
All methods for JS in a class.
*/
+ (NSArray *)ds_allMethodsForJS;
@end

enum{
DSB_API_HASNATIVEMETHOD,
DSB_API_CLOSEPAGE,
Expand Down
81 changes: 67 additions & 14 deletions dsbridge/JSBUtil.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#import <objc/runtime.h>
#import "DWKWebView.h"

/// 缓存类的方法
static NSMutableDictionary *_JSBUtilClassMethodsCache;

@implementation JSBUtil
+ (NSString *)objToJsonString:(id)dict
Expand All @@ -29,25 +31,76 @@ + (NSString *)objToJsonString:(id)dict
}

//get this class all method
+ (NSArray *)allMethodFromClass:(Class)class {
NSMutableArray *methods = [NSMutableArray array];
while (class) {
unsigned int count = 0;
Method *method = class_copyMethodList(class, &count);
for (unsigned int i = 0; i < count; i++) {
SEL name1 = method_getName(method[i]);
const char *selName= sel_getName(name1);
NSString *strName = [NSString stringWithCString:selName encoding:NSUTF8StringEncoding];
[methods addObject:strName];
#if 0
+(NSArray *)allMethodFromClass:(Class)class
{
NSMutableArray *arr = [NSMutableArray array];
u_int count;
Method *methods = class_copyMethodList(class, &count);
for (int i =0; i<count; i++) {
SEL name1 = method_getName(methods[i]);
const char *selName= sel_getName(name1);
NSString *strName = [NSString stringWithCString:selName encoding:NSUTF8StringEncoding];
//NSLog(@"%@",strName);
[arr addObject:strName];
}
free(methods);
return arr;
}
#else
+(NSArray *)allMethodFromClass:(Class)class
{
// fetch from cache
NSString *key = NSStringFromClass(class);
NSArray *allMethods = [_JSBUtilClassMethodsCache valueForKey:key];
if (allMethods) {
return allMethods;
}

// fetch from protocol
NSArray *array;
NSMutableSet *methodSet = [NSMutableSet set];
Class cls = [class class];
while ([cls respondsToSelector:@selector(ds_allMethodsForJS)]) {
NSArray *array = [(id<JSBUtilDelegate>)cls ds_allMethodsForJS];
[methodSet addObjectsFromArray:array];

cls = class_getSuperclass(cls);
}
array = methodSet.allObjects;

// fetch from class, superclass...
if (array.count == 0) {
NSString *className = NSStringFromClass(class);
NSMutableSet *methods = [NSMutableSet set];
while (class) {
unsigned int count = 0;
Method *method = class_copyMethodList(class, &count);
for (unsigned int i = 0; i < count; i++) {
SEL name1 = method_getName(method[i]);
const char *selName= sel_getName(name1);
NSString *strName = [NSString stringWithCString:selName encoding:NSUTF8StringEncoding];
[methods addObject:strName];
}
free(method);

Class cls = class_getSuperclass(class);
className = NSStringFromClass(cls);
class = [className hasPrefix:@"NS"]?nil:cls;
}
free(method);

Class cls = class_getSuperclass(class);
class = [NSStringFromClass(cls) isEqualToString:NSStringFromClass([NSObject class])] ? nil : cls;
array = methods.allObjects;
}

return [NSArray arrayWithArray:methods];
if (!_JSBUtilClassMethodsCache) {
_JSBUtilClassMethodsCache = @{}.mutableCopy;
}

[_JSBUtilClassMethodsCache setValue:array forKey:key];

return array;
}
#endif

//return method name for xxx: or xxx:handle:
+(NSString *)methodByNameArg:(NSInteger)argNum selName:(NSString *)selName class:(Class)class
Expand Down