From b72acc2313db90b414e53b4ede533071284de4c2 Mon Sep 17 00:00:00 2001 From: Robert Payne Date: Thu, 23 Apr 2015 09:28:09 -0700 Subject: [PATCH] Add support for exporting Swift modules Summary: External modules are any Objective-C class in which the implementation is private. This currently will be most useful for Swift classes but also has potential to allow exposing methods on 3rd party libraries to the bridge. Closes https://github.com/facebook/react-native/pull/982 Github Author: Robert Payne Test Plan: Imported from GitHub, without a `Test Plan:` line. --- React/Base/RCTBridgeModule.h | 59 ++++++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/React/Base/RCTBridgeModule.h b/React/Base/RCTBridgeModule.h index 12f7803ea73108..dd6f61e235e279 100644 --- a/React/Base/RCTBridgeModule.h +++ b/React/Base/RCTBridgeModule.h @@ -73,12 +73,67 @@ typedef void (^RCTResponseSenderBlock)(NSArray *response); * { ... } */ #define RCT_REMAP_METHOD(js_name, method) \ + RCT_EXTERN_REMAP_METHOD(js_name, method) \ + - (void)method + +/** + * Use this macro in a private Objective-C implementation file to automatically + * register an external module with the bridge when it loads. This allows you to + * register Swift or private Objective-C classes with the bridge. + * + * For example if one wanted to export a Swift class to the bridge: + * + * MyModule.swift: + * + * @objc(MyModule) class MyModule: NSObject { + * + * @objc func doSomething(string: String! withFoo a: Int, bar b: Int) { ... } + * + * } + * + * MyModuleExport.m: + * + * #import "RCTBridgeModule.h" + * + * @interface RCT_EXTERN_MODULE(MyModule, NSObject) + * + * RCT_EXTERN_METHOD(doSomething:(NSString *)string withFoo:(NSInteger)a bar:(NSInteger)b) + * + * @end + * + * This will now expose MyModule and the method to JavaScript via + * `NativeModules.MyModule.doSomething` + */ +#define RCT_EXTERN_MODULE(objc_name, objc_supername) \ + RCT_EXTERN_REMAP_MODULE(, objc_name, objc_supername) + +/** + * Similar to RCT_EXTERN_MODULE but allows setting a custom JavaScript name + */ +#define RCT_EXTERN_REMAP_MODULE(js_name, objc_name, objc_supername) \ + objc_name : objc_supername \ + @end \ + @interface objc_name (RCTExternModule) \ + @end \ + @implementation objc_name (RCTExternModule) \ + RCT_EXPORT_MODULE(js_name) + +/** + * Use this macro in accordance with RCT_EXTERN_MODULE to export methods + * of an external module. + */ +#define RCT_EXTERN_METHOD(method) \ + RCT_EXTERN_REMAP_METHOD(, method) + +/** + * Similar to RCT_EXTERN_REMAP_METHOD but allows setting a custom JavaScript name + */ +#define RCT_EXTERN_REMAP_METHOD(js_name, method) \ - (void)__rct_export__##method { \ __attribute__((used, section("__DATA,RCTExport"))) \ __attribute__((__aligned__(1))) \ static const char *__rct_export_entry__[] = { __func__, #method, #js_name }; \ - } \ - - (void)method + } /** * Deprecated, do not use.