Skip to content

Commit

Permalink
refactor(ios): remove all unnecessary autorelease pool (#4186)
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwcg authored Feb 14, 2025
1 parent 743f6d0 commit f416a7a
Show file tree
Hide file tree
Showing 8 changed files with 370 additions and 423 deletions.
8 changes: 3 additions & 5 deletions framework/ios/base/bridge/HippyBridge+BundleLoad.mm
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,9 @@ - (void)stopLoadingWithError:(NSError *)error scriptSourceURL:(NSURL *)sourceURL
}
__weak HippyBridge *weakSelf = self;
[self.javaScriptExecutor executeBlockOnJavaScriptQueue:^{
@autoreleasepool {
HippyBridge *strongSelf = weakSelf;
if (!strongSelf || ![strongSelf isValid]) {
[strongSelf.javaScriptExecutor invalidate];
}
HippyBridge *strongSelf = weakSelf;
if (!strongSelf || ![strongSelf isValid]) {
[strongSelf.javaScriptExecutor invalidate];
}
}];
if ([error userInfo][HippyJSStackTraceKey]) {
Expand Down
119 changes: 58 additions & 61 deletions framework/ios/base/executors/HippyJSExecutor.mm
Original file line number Diff line number Diff line change
Expand Up @@ -110,81 +110,78 @@ - (void)setup {

__weak __typeof(self)weakSelf = self;
hippy::base::RegisterFunction taskEndCB = [weakSelf](void *) {
@autoreleasepool {
HippyJSExecutor *strongSelf = weakSelf;
if (strongSelf) {
handleJsExcepiton(strongSelf.pScope);
}
}
};
scope->RegisterExtraCallback(hippy::kAsyncTaskEndKey, taskEndCB);

dispatch_semaphore_t scopeSemaphore = dispatch_semaphore_create(0);
footstone::TimePoint startPoint = footstone::TimePoint::SystemNow();
engine->GetEngine()->GetJsTaskRunner()->PostTask([weakSelf, scopeSemaphore, startPoint](){
@autoreleasepool {
__strong __typeof(weakSelf)strongSelf = weakSelf;
__strong __typeof(weakSelf)strongSelf = weakSelf;
if (!strongSelf) {
return;
}
HippyBridge *bridge = strongSelf.bridge;
if (!bridge) {
return;
}

dispatch_semaphore_wait(scopeSemaphore, DISPATCH_TIME_FOREVER);
auto scope = strongSelf.pScope;
scope->CreateContext();
auto context = scope->GetContext();
auto global_object = context->GetGlobalObject();

// add `global` property to global object
auto user_global_object_key = context->CreateString(kGlobalKey);
context->SetProperty(global_object, user_global_object_key, global_object);

// add `Hippy` property to global object
auto hippy_key = context->CreateString(kHippyKey);
context->SetProperty(global_object, hippy_key, context->CreateObject());

// inject device info to `__HIPPYNATIVEGLOBAL__`
[strongSelf injectDeviceInfoAsHippyNativeGlobal:bridge context:context globalObject:global_object];

// register `nativeRequireModuleConfig` function
[strongSelf registerRequiredModuleConfigFuncToJS:context globalObject:global_object scope:scope];

// register `nativeFlushQueueImmediate` function
[strongSelf registerFlushQueueImmediateFuncToJS:context globalObject:global_object scope:scope];

// register `getTurboModule` function
[strongSelf registerGetTurboModuleFuncToJS:context globalObject:global_object scope:scope];

// call finish block
if (strongSelf.contextCreatedBlock) {
strongSelf.contextCreatedBlock();
}
scope->SyncInitialize();

// performance record
footstone::TimePoint endTime = footstone::TimePoint::SystemNow();
auto entry = scope->GetPerformance()->PerformanceNavigation(hippy::kPerfNavigationHippyInit);
if (entry) {
entry->SetHippyJsEngineInitStart(startPoint);
entry->SetHippyJsEngineInitEnd(endTime);
entry->SetHippyNativeInitStart(strongSelf.bridge.startTime);
entry->SetHippyNativeInitEnd(endTime);
}

// the last, execute pending blocks
NSArray<dispatch_block_t> *pendingCalls;
@synchronized (strongSelf) {
strongSelf.ready = YES;
pendingCalls = [strongSelf.pendingCalls copy];
[strongSelf.pendingCalls removeAllObjects];
}
[pendingCalls enumerateObjectsUsingBlock:^(dispatch_block_t _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[strongSelf executeBlockOnJavaScriptQueue:obj];
}];
HippyBridge *bridge = strongSelf.bridge;
if (!bridge) {
return;
}

dispatch_semaphore_wait(scopeSemaphore, DISPATCH_TIME_FOREVER);
auto scope = strongSelf.pScope;
scope->CreateContext();
auto context = scope->GetContext();
auto global_object = context->GetGlobalObject();


// add `global` property to global object
auto user_global_object_key = context->CreateString(kGlobalKey);
context->SetProperty(global_object, user_global_object_key, global_object);

// add `Hippy` property to global object
auto hippy_key = context->CreateString(kHippyKey);
context->SetProperty(global_object, hippy_key, context->CreateObject());

// inject device info to `__HIPPYNATIVEGLOBAL__`
[strongSelf injectDeviceInfoAsHippyNativeGlobal:bridge context:context globalObject:global_object];

// register `nativeRequireModuleConfig` function
[strongSelf registerRequiredModuleConfigFuncToJS:context globalObject:global_object scope:scope];

// register `nativeFlushQueueImmediate` function
[strongSelf registerFlushQueueImmediateFuncToJS:context globalObject:global_object scope:scope];

// register `getTurboModule` function
[strongSelf registerGetTurboModuleFuncToJS:context globalObject:global_object scope:scope];

// call finish block
if (strongSelf.contextCreatedBlock) {
strongSelf.contextCreatedBlock();
}
scope->SyncInitialize();

// performance record
footstone::TimePoint endTime = footstone::TimePoint::SystemNow();
auto entry = scope->GetPerformance()->PerformanceNavigation(hippy::kPerfNavigationHippyInit);
if (entry) {
entry->SetHippyJsEngineInitStart(startPoint);
entry->SetHippyJsEngineInitEnd(endTime);
entry->SetHippyNativeInitStart(strongSelf.bridge.startTime);
entry->SetHippyNativeInitEnd(endTime);
}

// the last, execute pending blocks
NSArray<dispatch_block_t> *pendingCalls;
@synchronized (strongSelf) {
strongSelf.ready = YES;
pendingCalls = [strongSelf.pendingCalls copy];
[strongSelf.pendingCalls removeAllObjects];
}
[pendingCalls enumerateObjectsUsingBlock:^(dispatch_block_t _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[strongSelf executeBlockOnJavaScriptQueue:obj];
}];
});
_pScope = scope;
dispatch_semaphore_signal(scopeSemaphore);
Expand Down
Loading

0 comments on commit f416a7a

Please sign in to comment.