Skip to content

Commit 7ac7256

Browse files
committed
[CxxBridge] Add Hermes engine as option for macOS
This removes the need to maintain this in our fork. See: microsoft#473
1 parent 12543d5 commit 7ac7256

File tree

15 files changed

+170
-61
lines changed

15 files changed

+170
-61
lines changed

React-Core.podspec

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ Pod::Spec.new do |s|
6969
ss.private_header_files = "React/Cxx*/*.h"
7070
end
7171

72+
s.subspec "Hermes" do |ss|
73+
ss.platforms = { :osx => "10.14" }
74+
ss.source_files = "ReactCommon/hermes/executor/*.{cpp,h}"
75+
ss.dependency "hermes", "~> 0.6.0"
76+
end
77+
7278
s.subspec "DevSupport" do |ss|
7379
ss.source_files = "React/DevSupport/*.{h,mm,m}",
7480
"React/Inspector/*.{h,mm,m}"

React/CxxBridge/JSCExecutorFactory.mm

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
#include "JSCExecutorFactory.h"
99

10-
#import <React/RCTLog.h>
1110
#import <jsi/JSCRuntime.h>
1211

1312
#import <memory>
@@ -19,25 +18,8 @@
1918
std::shared_ptr<ExecutorDelegate> delegate,
2019
std::shared_ptr<MessageQueueThread> __unused jsQueue)
2120
{
22-
auto installBindings = [runtimeInstaller = runtimeInstaller_](jsi::Runtime &runtime) {
23-
react::Logger iosLoggingBinder = [](const std::string &message, unsigned int logLevel) {
24-
_RCTLogJavaScriptInternal(static_cast<RCTLogLevel>(logLevel), [NSString stringWithUTF8String:message.c_str()]);
25-
};
26-
react::bindNativeLogger(runtime, iosLoggingBinder);
27-
28-
react::PerformanceNow iosPerformanceNowBinder = []() {
29-
// CACurrentMediaTime() returns the current absolute time, in seconds
30-
return CACurrentMediaTime() * 1000;
31-
};
32-
react::bindNativePerformanceNow(runtime, iosPerformanceNowBinder);
33-
34-
// Wrap over the original runtimeInstaller
35-
if (runtimeInstaller) {
36-
runtimeInstaller(runtime);
37-
}
38-
};
3921
return std::make_unique<JSIExecutor>(
40-
facebook::jsc::makeJSCRuntime(), delegate, JSIExecutor::defaultTimeoutInvoker, std::move(installBindings));
22+
facebook::jsc::makeJSCRuntime(), delegate, JSIExecutor::defaultTimeoutInvoker, runtimeInstaller_);
4123
}
4224

4325
} // namespace react

React/CxxBridge/RCTCxxBridge.mm

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,16 @@
3737
#import <jsireact/JSIExecutor.h>
3838
#import <reactperflogger/BridgeNativeModulePerfLogger.h>
3939

40+
#if TARGET_OS_OSX && __has_include(<hermes/hermes.h>)
41+
#define RCT_USE_HERMES 1
42+
#endif
43+
#if RCT_USE_HERMES
44+
#import "HermesExecutorFactory.h"
45+
#else
4046
#import "JSCExecutorFactory.h"
47+
#endif
48+
#import "RCTJSIExecutorRuntimeInstaller.h"
49+
4150
#import "NSDataBigString.h"
4251
#import "RCTMessageThread.h"
4352
#import "RCTObjcExecutor.h"
@@ -370,7 +379,12 @@ - (void)start
370379
executorFactory = [cxxDelegate jsExecutorFactoryForBridge:self];
371380
}
372381
if (!executorFactory) {
373-
executorFactory = std::make_shared<JSCExecutorFactory>(nullptr);
382+
auto installBindings = RCTJSIExecutorRuntimeInstaller(nullptr);
383+
#if RCT_USE_HERMES
384+
executorFactory = std::make_shared<HermesExecutorFactory>(installBindings);
385+
#else
386+
executorFactory = std::make_shared<JSCExecutorFactory>(installBindings);
387+
#endif
374388
}
375389
} else {
376390
id<RCTJavaScriptExecutor> objcExecutor = [self moduleForClass:self.executorClass];
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#pragma once
9+
10+
#include <jsireact/JSIExecutor.h>
11+
12+
namespace facebook {
13+
namespace react {
14+
15+
/**
16+
* Creates a lambda used to bind a JSIRuntime in the context of
17+
* Apple platforms, such as console logging, performance metrics, etc.
18+
*/
19+
JSIExecutor::RuntimeInstaller RCTJSIExecutorRuntimeInstaller(JSIExecutor::RuntimeInstaller runtimeInstallerToWrap);
20+
21+
} // namespace react
22+
} // namespace facebook
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#include "RCTJSIExecutorRuntimeInstaller.h"
9+
10+
#import <React/RCTLog.h>
11+
12+
namespace facebook {
13+
namespace react {
14+
15+
JSIExecutor::RuntimeInstaller RCTJSIExecutorRuntimeInstaller(JSIExecutor::RuntimeInstaller runtimeInstallerToWrap) {
16+
return [runtimeInstaller = runtimeInstallerToWrap](jsi::Runtime &runtime) {
17+
Logger iosLoggingBinder = [](const std::string &message, unsigned int logLevel) {
18+
_RCTLogJavaScriptInternal(static_cast<RCTLogLevel>(logLevel), [NSString stringWithUTF8String:message.c_str()]);
19+
};
20+
bindNativeLogger(runtime, iosLoggingBinder);
21+
22+
PerformanceNow iosPerformanceNowBinder = []() {
23+
// CACurrentMediaTime() returns the current absolute time, in seconds
24+
return CACurrentMediaTime() * 1000;
25+
};
26+
bindNativePerformanceNow(runtime, iosPerformanceNowBinder);
27+
28+
// Wrap over the original runtimeInstaller
29+
if (runtimeInstaller) {
30+
runtimeInstaller(runtime);
31+
}
32+
};
33+
}
34+
35+
} // namespace react
36+
} // namespace facebook

ReactAndroid/src/main/java/com/facebook/hermes/reactexecutor/Android.mk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ LOCAL_C_INCLUDES := $(LOCAL_PATH) $(REACT_NATIVE)/ReactCommon/jsi $(call find-no
1717

1818
LOCAL_CPP_FEATURES := exceptions
1919

20-
LOCAL_STATIC_LIBRARIES := libjsireact libjsi
20+
LOCAL_STATIC_LIBRARIES := libjsireact libjsi libhermes-executor-common-release
2121
LOCAL_SHARED_LIBRARIES := libfolly_json libfb libfbjni libreactnativejni libhermes
2222

2323
include $(BUILD_SHARED_LIBRARY)
@@ -34,7 +34,7 @@ LOCAL_C_INCLUDES := $(LOCAL_PATH) $(REACT_NATIVE)/ReactCommon/jsi $(call find-no
3434

3535
LOCAL_CPP_FEATURES := exceptions
3636

37-
LOCAL_STATIC_LIBRARIES := libjsireact libjsi libhermes-inspector
37+
LOCAL_STATIC_LIBRARIES := libjsireact libjsi libhermes-executor-common-debug libhermes-inspector
3838
LOCAL_SHARED_LIBRARIES := libfolly_json libfb libfbjni libreactnativejni libhermes
3939

4040
include $(BUILD_SHARED_LIBRARY)

ReactCommon/cxxreact/Android.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ $(call import-module,glog)
3333
$(call import-module,jsi)
3434
$(call import-module,jsinspector)
3535
$(call import-module,hermes/inspector)
36+
$(call import-module,hermes/executor)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright (c) Facebook, Inc. and its affiliates.
2+
#
3+
# This source code is licensed under the MIT license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
LOCAL_PATH := $(call my-dir)
7+
REACT_NATIVE := $(LOCAL_PATH)/../../..
8+
9+
include $(REACT_NATIVE)/ReactCommon/common.mk
10+
include $(CLEAR_VARS)
11+
12+
LOCAL_MODULE := hermes-executor-common-release
13+
14+
LOCAL_SRC_FILES := $(wildcard $(LOCAL_PATH)/*.cpp)
15+
16+
LOCAL_C_INCLUDES := $(LOCAL_PATH) $(REACT_NATIVE)/ReactCommon/jsi $(call find-node-module,$(LOCAL_PATH),hermes-engine)/android/include
17+
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
18+
19+
LOCAL_STATIC_LIBRARIES := libjsi libjsireact
20+
LOCAL_SHARED_LIBRARIES := libhermes
21+
22+
include $(BUILD_SHARED_LIBRARY)
23+
24+
include $(CLEAR_VARS)
25+
26+
LOCAL_MODULE := hermes-executor-common-debug
27+
LOCAL_CFLAGS := -DHERMES_ENABLE_DEBUGGER=1
28+
29+
LOCAL_SRC_FILES := $(wildcard $(LOCAL_PATH)/*.cpp)
30+
31+
LOCAL_C_INCLUDES := $(LOCAL_PATH) $(REACT_NATIVE)/ReactCommon/jsi $(call find-node-module,$(LOCAL_PATH),hermes-engine)/android/include
32+
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
33+
34+
LOCAL_STATIC_LIBRARIES := libjsi libjsireact libhermes-inspector
35+
LOCAL_SHARED_LIBRARIES := libhermes
36+
37+
include $(BUILD_SHARED_LIBRARY)

0 commit comments

Comments
 (0)