Skip to content

Commit 37c34b8

Browse files
committed
nonisolated(unsafe) for swift > = 5.10
1 parent a36c42b commit 37c34b8

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

Sources/ReactBridgeMacros/ReactMethod.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,21 @@ struct ReactMethod {
3939

4040
extension ReactMethod: PeerMacro {
4141

42-
private static func reactExport(funcName: String, jsName: String, objcName: String, isSync: Bool) -> DeclSyntax {
42+
private static var nonisolatedUnsafe: String = {
43+
#if swift(>=5.10)
44+
"nonisolated(unsafe) "
45+
#else
46+
""
47+
#endif
48+
}()
49+
50+
static func reactExport(funcName: String, jsName: String, objcName: String, isSync: Bool) -> DeclSyntax {
4351
"""
4452
@objc static func __rct_export__\(raw: funcName)() -> UnsafePointer<RCTMethodInfo>? {
4553
struct Static {
4654
static let jsName = strdup(\(raw: jsName))
4755
static let objcName = strdup("\(raw: objcName)")
48-
nonisolated (unsafe) static var methodInfo = RCTMethodInfo(jsName: jsName, objcName: objcName, isSync: \(raw: isSync))
56+
\(raw: Self.nonisolatedUnsafe)static var methodInfo = RCTMethodInfo(jsName: jsName, objcName: objcName, isSync: \(raw: isSync))
4957
}
5058
return withUnsafePointer(to: &Static.methodInfo) { $0 }
5159
}

Tests/ReactBridgeTests/ReactBridgeTests.swift

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import SwiftSyntaxMacros
22
import SwiftSyntaxMacrosTestSupport
33
import XCTest
44

5-
// Macro implementations build for the host, so the corresponding module is not available when cross-compiling.
5+
6+
// Macro implementations build for the host, so the corresponding module is not available when cross-compiling.
67
// Cross-compiled tests may still make use of the macro itself in end-to-end tests.
78
#if DEBUG && canImport(ReactBridgeMacros)
89

@@ -14,13 +15,21 @@ final class ReactMethodTests: XCTestCase {
1415
"ReactMethod": ReactMethod.self,
1516
]
1617

18+
private static var nonisolatedUnsafe: String = {
19+
#if swift(>=5.10)
20+
"nonisolated (unsafe) "
21+
#else
22+
""
23+
#endif
24+
}()
25+
1726
func rct_export(name: String, selector: String, isSync: Bool = false, jsName: String? = nil) -> String {
1827
"""
1928
@objc static func __rct_export__\(name)() -> UnsafePointer<RCTMethodInfo>? {
2029
struct Static {
2130
static let jsName = strdup("\(jsName ?? name)")
2231
static let objcName = strdup("\(selector)")
23-
nonisolated (unsafe) static var methodInfo = RCTMethodInfo(jsName: jsName, objcName: objcName, isSync: \(isSync))
32+
\(Self.nonisolatedUnsafe)static var methodInfo = RCTMethodInfo(jsName: jsName, objcName: objcName, isSync: \(isSync))
2433
}
2534
return withUnsafePointer(to: &Static.methodInfo) {
2635
$0
@@ -489,13 +498,13 @@ final class ReactPropertyTests: XCTestCase {
489498
"""
490499
class View {
491500
@ReactProperty
492-
var a, b: Int?
501+
let a: Int = 10, b: Int = 20
493502
}
494503
""",
495504
expandedSource:
496505
"""
497506
class View {
498-
var a, b: Int?
507+
let a: Int = 10, b: Int = 20
499508
}
500509
""",
501510
diagnostics: [diagnostic],
@@ -571,7 +580,13 @@ final class ReactPropertyTests: XCTestCase {
571580
var set: Set<String>?
572581
573582
@ReactProperty
574-
var onData: RCTBubblingEventBlock?
583+
var onDirect: RCTDirectEventBlock?
584+
585+
@ReactProperty
586+
var onBubbling: RCTBubblingEventBlock?
587+
588+
@ReactProperty
589+
var onCapturing: RCTCapturingEventBlock?
575590
576591
@ReactProperty(keyPath: "muted")
577592
var isMute: Bool?
@@ -607,9 +622,15 @@ final class ReactPropertyTests: XCTestCase {
607622
var set: Set<String>?
608623
609624
\(propConfig(name: "set", objcType: "NSSet"))
610-
var onData: RCTBubblingEventBlock?
625+
var onDirect: RCTDirectEventBlock?
626+
627+
\(propConfig(name: "onDirect", objcType: "RCTDirectEventBlock"))
628+
var onBubbling: RCTBubblingEventBlock?
629+
630+
\(propConfig(name: "onBubbling", objcType: "RCTBubblingEventBlock"))
631+
var onCapturing: RCTCapturingEventBlock?
611632
612-
\(propConfig(name: "onData", objcType: "RCTBubblingEventBlock"))
633+
\(propConfig(name: "onCapturing", objcType: "RCTCapturingEventBlock"))
613634
var isMute: Bool?
614635
615636
\(propConfig(name: "isMute", objcType: "BOOL", keyPath: "muted"))

Tests/ReactBridgeTests/SandboxTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//*
1+
/*
22
import Foundation
33
import ReactBridge
44

0 commit comments

Comments
 (0)