Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
ikhvorost committed Jun 17, 2024
2 parents 7c46eda + 00f8ff1 commit c21deb4
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 33 deletions.
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax.git",
"state" : {
"revision" : "fa8f95c2d536d6620cc2f504ebe8a6167c9fc2dd",
"version" : "510.0.1"
"revision" : "303e5c5c36d6a558407d364878df131c3546fad8",
"version" : "510.0.2"
}
}
],
Expand Down
6 changes: 3 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ let package = Package(
.library(name: "ReactBridge", targets: ["ReactBridge"])
],
dependencies: [
.package(url: "https://github.com/apple/swift-syntax.git", from: "510.0.1")
.package(url: "https://github.com/apple/swift-syntax.git", from: "510.0.2")
],
targets: [
.macro(
Expand All @@ -52,9 +52,9 @@ let package = Package(
),
.target(
name: "ReactBridge",
dependencies: ["ReactBridgeMacros", "RegisterModules"]
dependencies: ["ReactBridgeMacros", "ReactBridgeUtils"]
),
.target(name: "RegisterModules"),
.target(name: "ReactBridgeUtils"),
.testTarget(
name: "ReactBridgeTests",
dependencies: [
Expand Down
1 change: 1 addition & 0 deletions Sources/ReactBridge/ReactBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
//

import Foundation
@_exported import ReactBridgeUtils


/// The macro exports and registers a class as a native module for React Native.
Expand Down
19 changes: 3 additions & 16 deletions Sources/ReactBridgeMacros/ReactMethod.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,22 @@ import SwiftSyntax
import SwiftSyntaxMacros
import SwiftDiagnostics


fileprivate extension String {
var uppercasedFirst: String {
prefix(1).uppercased() + dropFirst()
}
}


struct ReactMethod {
}

extension ReactMethod: PeerMacro {

private static var nonisolatedUnsafe: String = {
#if swift(>=5.10)
"nonisolated(unsafe) "
#else
""
#endif
}()

static func reactExport(funcName: String, jsName: String, objcName: String, isSync: Bool) -> DeclSyntax {
"""
@objc static func __rct_export__\(raw: funcName)() -> UnsafePointer<RCTMethodInfo>? {
struct Static {
static let jsName = strdup(\(raw: jsName))
static let objcName = strdup("\(raw: objcName)")
\(raw: Self.nonisolatedUnsafe)static var methodInfo = RCTMethodInfo(jsName: jsName, objcName: objcName, isSync: \(raw: isSync))
}
return withUnsafePointer(to: &Static.methodInfo) { $0 }
@objc static func __rct_export__\(raw: funcName)() -> UnsafeRawPointer {
ReactBridgeUtils.methodInfo(\(raw: jsName), objcName: "\(raw: objcName)", isSync: \(raw: isSync))
}
"""
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// RegisterModules.m
// ReactBridgeUtils.m
//
// Created by Iurii Khvorost <[email protected]> on 2023/07/24.
// Copyright © 2023 Iurii Khvorost. All rights reserved.
Expand All @@ -24,9 +24,20 @@
//

#import <objc/runtime.h>
#import <Foundation/Foundation.h>
#import "ReactBridgeUtils.h"

__attribute__((constructor))
static void registerModules(void) {

// From RCTBridgeModule.h
typedef struct RCTMethodInfo {
const char * jsName;
const char * objcName;
BOOL isSync;
} RCTMethodInfo;

@implementation ReactBridgeUtils

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

int numClasses = objc_getClassList(NULL, 0);
Expand All @@ -50,3 +61,13 @@ static void registerModules(void) {
}
free(classes);
}

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

@end
11 changes: 11 additions & 0 deletions Sources/ReactBridgeUtils/include/ReactBridgeUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface ReactBridgeUtils: NSObject

+ (const void *)methodInfo:(NSString *)jsName objcName:(NSString *)objcName isSync:(BOOL)isSync;

@end

NS_ASSUME_NONNULL_END
3 changes: 3 additions & 0 deletions Sources/ReactBridgeUtils/include/module.modulemap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module ReactBridgeUtils {
header "ReactBridgeUtils.h"
}
Empty file.
11 changes: 2 additions & 9 deletions Tests/ReactBridgeTests/ReactBridgeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,8 @@ final class ReactMethodTests: XCTestCase {

func rct_export(name: String, selector: String, isSync: Bool = false, jsName: String? = nil) -> String {
"""
@objc static func __rct_export__\(name)() -> UnsafePointer<RCTMethodInfo>? {
struct Static {
static let jsName = strdup("\(jsName ?? name)")
static let objcName = strdup("\(selector)")
\(Self.nonisolatedUnsafe)static var methodInfo = RCTMethodInfo(jsName: jsName, objcName: objcName, isSync: \(isSync))
}
return withUnsafePointer(to: &Static.methodInfo) {
$0
}
@objc static func __rct_export__\(name)() -> UnsafeRawPointer {
ReactBridgeUtils.methodInfo("\(jsName ?? name)", objcName: "\(selector)", isSync: \(isSync))
}
"""
}
Expand Down

0 comments on commit c21deb4

Please sign in to comment.