Skip to content

Commit 14018e4

Browse files
committed
Conditionally make WebKit allocations
1 parent b780014 commit 14018e4

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

FirebaseDynamicLinks/Sources/FIRDLJavaScriptExecutor.m

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#import <TargetConditionals.h>
1818
#if TARGET_OS_IOS
1919

20+
#import <sys/sysctl.h>
21+
2022
#import <WebKit/WebKit.h>
2123

2224
#import "FirebaseDynamicLinks/Sources/FIRDLJavaScriptExecutor.h"
@@ -66,7 +68,20 @@ - (instancetype)initWithDelegate:(id<FIRDLJavaScriptExecutorDelegate>)delegate
6668
if (self = [super init]) {
6769
_delegate = delegate;
6870
_script = [script copy];
71+
72+
// A WebKit memory allocation error occurs on Apple Silicon when the
73+
// target is below iOS 14. The issue only occurs on the simulator.
74+
#if TARGET_OS_SIMULATOR
75+
// Only make WebKit related memory allocations if the process
76+
// is not running under Rosetta translation.
77+
if (!processIsTranslated()) {
78+
// The `start:` method allocates a WebKit object.
79+
[self start];
80+
}
81+
#else
6982
[self start];
83+
#endif
84+
7085
}
7186
return self;
7287
}
@@ -135,6 +150,21 @@ - (void)webView:(WKWebView *)webView
135150
[self handleExecutionError:error];
136151
}
137152

153+
// Determine whether a process is running on a Rosetta translation.
154+
// Return 0 for a native process, 1 for a translated process,
155+
// and -1 when an error occurs.
156+
// From: https://developer.apple.com/documentation/apple-silicon/about-the-rosetta-translation-environment
157+
int processIsTranslated() {
158+
int ret = 0;
159+
size_t size = sizeof(ret);
160+
if (sysctlbyname("sysctl.proc_translated", &ret, &size, NULL, 0) == -1) {
161+
if (errno == ENOENT)
162+
return 0;
163+
return -1;
164+
}
165+
return ret;
166+
}
167+
138168
@end
139169

140170
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)