Skip to content

Commit

Permalink
Registering native modules optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
ikhvorost committed Jul 16, 2024
1 parent 93e0318 commit fae7980
Showing 1 changed file with 27 additions and 23 deletions.
50 changes: 27 additions & 23 deletions Sources/ReactBridgeUtils/ReactBridgeUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,37 @@
BOOL isSync;
} RCTMethodInfo;

@implementation ReactBridgeUtils

+ (void)load {
SEL selector = @selector(_registerModule);

int numClasses = objc_getClassList(NULL, 0);
Class* classes = (Class *)malloc(sizeof(Class) * numClasses);
numClasses = objc_getClassList(classes, numClasses);

for (int i = 0; i < numClasses; i++) {
Class class = classes[i];

unsigned int numMethods = 0;
Method *methods = class_copyMethodList(object_getClass(class), &numMethods);
for (int j = 0; j < numMethods; j++) {
Method method = methods[j];
if (sel_isEqual(method_getName(method), selector)) {
IMP imp = method_getImplementation(method);
((void (*)(Class, SEL))imp)(class, selector);
continue;
}
static void class_performClassSelector(Class class, SEL selector) {
unsigned int numMethods = 0;
Method *methods = class_copyMethodList(object_getClass(class), &numMethods);
for (int i = 0; i < numMethods; i++) {
Method method = methods[i];
if (sel_isEqual(method_getName(method), selector)) {
IMP imp = method_getImplementation(method);
((void (*)(Class, SEL))imp)(class, selector);
break;
}
free(methods);
}
free(classes);
free(methods);
}

__attribute__((constructor))
static void load() {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
SEL selector = @selector(_registerModule);
int numClasses = objc_getClassList(NULL, 0);
Class* classes = (Class *)malloc(sizeof(Class) * numClasses);
numClasses = objc_getClassList(classes, numClasses);
for (int i = 0; i < numClasses; i++) {
Class class = classes[i];
class_performClassSelector(class, selector);
}
free(classes);
});
}

@implementation ReactBridgeUtils

+ (const void *)methodInfo:(NSString *)jsName objcName:(NSString *)objcName isSync:(BOOL)isSync {
RCTMethodInfo* methodInfo = malloc(sizeof(RCTMethodInfo));
methodInfo->jsName = strdup([jsName cStringUsingEncoding: NSUTF8StringEncoding]);
Expand Down

0 comments on commit fae7980

Please sign in to comment.