Skip to content

Commit 1b1b68b

Browse files
committed
feat: support cpp language with turbo modules
1 parent da49e96 commit 1b1b68b

File tree

15 files changed

+71
-93
lines changed

15 files changed

+71
-93
lines changed

.github/workflows/build-templates.yml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,18 @@ jobs:
3535
- type: module-legacy
3636
language: cpp
3737
example: native
38+
- type: module-mixed
39+
language: cpp
40+
example: native
41+
- type: module-turbo
42+
language: cpp
43+
example: native
3844
- type: library
3945
language: js
40-
example: expo
46+
example: native
4147
- type: library
4248
language: js
43-
example: native
49+
example: expo
4450

4551
concurrency:
4652
group: ${{ github.workflow }}-${{ github.ref }}-${{ matrix.type }}-${{ matrix.language }}-${{ matrix.example }}
@@ -89,7 +95,7 @@ jobs:
8995
echo "work_dir=$WORK_DIR" >> $GITHUB_ENV
9096
9197
# Build Android for only some matrices to skip redundant builds
92-
if [[ ${{ matrix.type }} == view && ${{ matrix.language }} == *-objc ]] || [[ ${{ matrix.type }} == module-* && ${{ matrix.language }} == *-objc ]] || [[ ${{ matrix.type }} == module-legacy && ${{ matrix.language }} == cpp ]]; then
98+
if [[ ${{ matrix.type }} == view && ${{ matrix.language }} == *-objc ]] || [[ ${{ matrix.type }} == module-* && ${{ matrix.language }} == *-objc ]] || [[ ${{ matrix.type }} == module-* && ${{ matrix.language }} == cpp ]]; then
9399
echo "build_android=1" >> $GITHUB_ENV
94100
fi
95101

packages/create-react-native-library/src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,14 @@ async function create(argv: yargs.Arguments<any>) {
301301
if (values.type !== 'module-turbo' && values.type !== 'module-mixed') {
302302
languages.push(
303303
{ title: 'Java & Swift', value: 'java-swift' },
304-
{ title: 'Kotlin & Swift', value: 'kotlin-swift' },
305-
{ title: 'C++ for both iOS & Android', value: 'cpp' }
304+
{ title: 'Kotlin & Swift', value: 'kotlin-swift' }
306305
);
307306
}
308307

308+
if (values.type !== 'view') {
309+
languages.push({ title: 'C++ for Android & iOS', value: 'cpp' });
310+
}
311+
309312
return languages;
310313
},
311314
},

packages/create-react-native-library/templates/cpp-library/android/cpp-adapter.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
extern "C"
55
JNIEXPORT jint JNICALL
6+
<% if (project.architecture == 'mixed') { -%>
7+
Java_com_<%- project.package -%>_<%- project.name -%>ModuleImpl_multiply(JNIEnv *env, jclass type, jdouble a, jdouble b) {
8+
<% } else { -%>
69
Java_com_<%- project.package -%>_<%- project.name -%>Module_nativeMultiply(JNIEnv *env, jclass type, jdouble a, jdouble b) {
7-
return example::multiply(a, b);
10+
<% } -%>
11+
return <%- project.package -%>::multiply(a, b);
812
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "<%- project.identifier -%>.h"
22

3-
namespace example {
4-
int multiply(double a, double b) {
3+
namespace <%- project.package -%> {
4+
double multiply(double a, double b) {
55
return a * b;
66
}
77
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#ifndef EXAMPLE_H
2-
#define EXAMPLE_H
1+
#ifndef <%- project.package -%>_H
2+
#define <%- project.package -%>_H
33

4-
namespace example {
5-
int multiply(double a, double b);
4+
namespace <%- project.package -%> {
5+
double multiply(double a, double b);
66
}
77

8-
#endif /* EXAMPLE_H */
8+
#endif /* <%- project.package -%>_H */

packages/create-react-native-library/templates/cpp-library/ios/{%- project.name %}.h

Lines changed: 0 additions & 11 deletions
This file was deleted.

packages/create-react-native-library/templates/cpp-library/ios/{%- project.name %}.mm

Lines changed: 0 additions & 19 deletions
This file was deleted.

packages/create-react-native-library/templates/java-library-legacy/android/src/main/java/com/{%- project.package %}/{%- project.name %}Module.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,7 @@ public String getName() {
2424

2525
<% if (project.cpp) { -%>
2626
static {
27-
try {
28-
// Used to load the 'native-lib' library on application startup.
29-
System.loadLibrary("cpp");
30-
} catch (Exception ignored) {
31-
}
27+
System.loadLibrary("cpp");
3228
}
3329
<% } -%>
3430

@@ -44,6 +40,6 @@ public void multiply(double a, double b, Promise promise) {
4440
}
4541

4642
<% if (project.cpp) { -%>
47-
public static native double nativeMultiply(double a, double b);
43+
private static native double nativeMultiply(double a, double b);
4844
<% } -%>
4945
}

packages/create-react-native-library/templates/java-library-mixed/android/src/legacy/{%- project.name %}Module.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ public String getName() {
2323
// See https://reactnative.dev/docs/native-modules-android
2424
@ReactMethod
2525
public void multiply(double a, double b, Promise promise) {
26-
<%- project.name -%>ModuleImpl.multiply(a, b, promise);
26+
promise.resolve(<%- project.name -%>ModuleImpl.multiply(a, b));
2727
}
2828
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
package com.<%- project.package -%>;
22

3-
import com.facebook.react.bridge.Promise;
4-
import com.facebook.react.bridge.ReactMethod;
5-
63
/**
74
* This is where the module implementation lives
85
* The exposed methods can be defined in the `turbo` and `legacy` folders
96
*/
107
public class <%- project.name -%>ModuleImpl {
118
public static final String NAME = "<%- project.name -%>";
129

13-
public static void multiply(double a, double b, Promise promise) {
14-
promise.resolve(a * b);
10+
<% if (project.cpp) { -%>
11+
static {
12+
System.loadLibrary("cpp");
13+
}
14+
15+
public static native double multiply(double a, double b);
16+
<% } else { -%>
17+
public static double multiply(double a, double b) {
18+
return a * b;
1519
}
20+
<% } -%>
1621
}

packages/create-react-native-library/templates/java-library-mixed/android/src/turbo/{%- project.name %}Module.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ public String getName() {
2323
@Override
2424
@ReactMethod
2525
public void multiply(double a, double b, Promise promise) {
26-
<%- project.name -%>ModuleImpl.multiply(a, b, promise);
26+
promise.resolve(<%- project.name -%>ModuleImpl.multiply(a, b));
2727
}
2828
}

packages/create-react-native-library/templates/java-library-turbo/android/src/main/java/com/{%- project.package %}/{%- project.name %}Module.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,24 @@ public String getName() {
1919
return NAME;
2020
}
2121

22+
<% if (project.cpp) { -%>
23+
static {
24+
System.loadLibrary("cpp");
25+
}
26+
<% } -%>
27+
2228
// Example method
2329
// See https://reactnative.dev/docs/native-modules-android
2430
@Override
2531
public double multiply(double a, double b) {
32+
<% if (project.cpp) { -%>
33+
return nativeMultiply(a, b);
34+
<% } else { -%>
2635
return a * b;
36+
<% } -%>
2737
}
38+
39+
<% if (project.cpp) { -%>
40+
private static native double nativeMultiply(double a, double b);
41+
<% } -%>
2842
}

packages/create-react-native-library/templates/kotlin-library-legacy/android/src/main/java/com/{%- project.package %}/{%- project.name %}Module.kt

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,7 @@ class <%- project.name -%>Module(reactContext: ReactApplicationContext) : ReactC
1414
// Example method
1515
// See https://reactnative.dev/docs/native-modules-android
1616
@ReactMethod
17-
fun multiply(a: Int, b: Int, promise: Promise) {
18-
<% if (project.cpp) { -%>
19-
promise.resolve(nativeMultiply(a, b));
20-
<% } else { -%>
17+
fun multiply(a: Double, b: Double, promise: Promise) {
2118
promise.resolve(a * b)
22-
<% } -%>
2319
}
24-
25-
<% if (project.cpp) { -%>
26-
external fun nativeMultiply(a: Int, b: Int): Int;
27-
28-
companion object
29-
{
30-
31-
// Used to load the 'native-lib' library on application startup.
32-
init
33-
{
34-
System.loadLibrary("cpp")
35-
}
36-
}
37-
<% } -%>
3820
}

packages/create-react-native-library/templates/objc-library/ios/{%- project.name %}.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
<% if (project.cpp) { -%>
2+
#ifdef __cplusplus
3+
#import "<%- project.identifier -%>.h"
4+
#endif
5+
<% } -%>
6+
17
#ifdef RCT_NEW_ARCH_ENABLED
28
#import "RN<%- project.name -%>Spec.h"
39

packages/create-react-native-library/templates/objc-library/ios/{%- project.name %}.mm

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,14 @@ @implementation <%- project.name -%>
66

77
<% if (project.architecture == 'turbo') { -%>
88
- (NSNumber *)multiply:(double)a b:(double)b {
9+
<% if (project.cpp) { -%>
10+
NSNumber *result = @(<%- project.package -%>::multiply(a, b));
11+
<% } else { -%>
912
NSNumber *result = @(a * b);
13+
<% } -%>
1014

1115
return result;
1216
}
13-
<% } else if (project.architecture == 'mixed') { -%>
14-
// Example method
15-
// See // https://reactnative.dev/docs/native-modules-ios
16-
RCT_REMAP_METHOD(multiply,
17-
multiplyWithA:(double)a withB:(double)b
18-
withResolver:(RCTPromiseResolveBlock)resolve
19-
withRejecter:(RCTPromiseRejectBlock)reject)
20-
{
21-
[self multiply:a b:b resolve:resolve reject:reject];
22-
}
23-
24-
- (void)multiply:(double)a b:(double)b resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
25-
NSNumber *result = @(a * b);
26-
27-
resolve(result);
28-
}
2917
<% } else { -%>
3018
// Example method
3119
// See // https://reactnative.dev/docs/native-modules-ios
@@ -34,9 +22,13 @@ - (void)multiply:(double)a b:(double)b resolve:(RCTPromiseResolveBlock)resolve r
3422
withResolver:(RCTPromiseResolveBlock)resolve
3523
withRejecter:(RCTPromiseRejectBlock)reject)
3624
{
37-
NSNumber *result = @(a * b);
25+
<% if (project.cpp) { -%>
26+
NSNumber *result = @(<%- project.package -%>::multiply(a, b));
27+
<% } else { -%>
28+
NSNumber *result = @(a * b);
29+
<% } -%>
3830

39-
resolve(result);
31+
resolve(result);
4032
}
4133
<% } -%>
4234

0 commit comments

Comments
 (0)