@@ -35,31 +35,37 @@ public class NativeInstrumentation: NSObject, RCTBridgeModule {
35
35
36
36
@objc
37
37
public func getStartupTime( _ resolve: @escaping RCTPromiseResolveBlock , rejecter reject: @escaping RCTPromiseRejectBlock ) {
38
- var mib = [ CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid ( ) ]
39
- var size = MemoryLayout< kinfo_proc> . size
40
- var kp = kinfo_proc ( )
41
-
42
- let result = mib. withUnsafeMutableBytes { mibBytes in
43
- withUnsafeMutablePointer ( to: & size) { sizeBytes in
44
- withUnsafeMutablePointer ( to: & kp) { kpBytes in
45
- sysctl ( mibBytes. baseAddress? . assumingMemoryBound ( to: Int32 . self) , 4 ,
46
- kpBytes,
47
- sizeBytes,
48
- nil , 0 )
38
+ do {
39
+ var mib = [ CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid ( ) ]
40
+ var size = MemoryLayout< kinfo_proc> . size
41
+ var kp = kinfo_proc ( )
42
+
43
+ let result = mib. withUnsafeMutableBytes { mibBytes in
44
+ withUnsafeMutablePointer ( to: & size) { sizeBytes in
45
+ withUnsafeMutablePointer ( to: & kp) { kpBytes in
46
+ sysctl ( mibBytes. baseAddress? . assumingMemoryBound ( to: Int32 . self) , 4 ,
47
+ kpBytes,
48
+ sizeBytes,
49
+ nil , 0 )
50
+ }
49
51
}
50
52
}
53
+
54
+ let startTimeMs : Int64
55
+ if result == 0 {
56
+ let startTime = kp. kp_proc. p_un. __p_starttime
57
+ startTimeMs = Int64 ( startTime. tv_sec) * 1000 + Int64( startTime. tv_usec) / 1000
58
+ } else {
59
+ throw NSError ( domain: " NativeInstrumentation " ,
60
+ code: Int ( result) ,
61
+ userInfo: [ NSLocalizedDescriptionKey: " Failed to get process info " ] )
62
+ }
63
+
64
+ let response = [ " startupTime " : startTimeMs]
65
+ resolve ( response)
66
+ } catch {
67
+ reject ( " STARTUP_TIME_ERROR " , " Failed to get startup time: \( error. localizedDescription) " , error)
51
68
}
52
-
53
- let startTimeMs : Int64
54
- if result == 0 {
55
- let startTime = kp. kp_proc. p_un. __p_starttime
56
- startTimeMs = Int64 ( startTime. tv_sec) * 1000 + Int64( startTime. tv_usec) / 1000
57
- } else {
58
- startTimeMs = Int64 ( Date ( ) . timeIntervalSince1970 * 1000 )
59
- }
60
-
61
- let response = [ " startupTime " : startTimeMs]
62
- resolve ( response)
63
69
}
64
70
65
71
@objc
0 commit comments