Skip to content

Commit 5f7b508

Browse files
rubennortefacebook-github-bot
authored andcommitted
Implement microtasks using new JSI method (#43397)
Summary: Pull Request resolved: #43397 Changelog: [internal] This migrates the Hermes-specific use of microtasks to an engine agnostic implementation based on the new JSI method to queue microtasks. Reviewed By: sammy-SC Differential Revision: D54687056
1 parent dc023c0 commit 5f7b508

File tree

12 files changed

+200
-31
lines changed

12 files changed

+200
-31
lines changed

packages/react-native/Libraries/AppDelegate/RCTAppDelegate.mm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#import <ReactCommon/RCTJscInstance.h>
3030
#endif
3131
#import <react/nativemodule/featureflags/NativeReactNativeFeatureFlags.h>
32+
#import <react/nativemodule/microtasks/NativeMicrotasks.h>
3233

3334
@interface RCTAppDelegate () <RCTComponentViewFactoryComponentProvider>
3435
@end
@@ -210,6 +211,10 @@ - (Class)getModuleClassFromName:(const char *)name
210211
return std::make_shared<facebook::react::NativeReactNativeFeatureFlags>(jsInvoker);
211212
}
212213

214+
if (name == facebook::react::NativeMicrotasks::kModuleName) {
215+
return std::make_shared<facebook::react::NativeMicrotasks>(jsInvoker);
216+
}
217+
213218
return nullptr;
214219
}
215220

packages/react-native/Libraries/AppDelegate/React-RCTAppDelegate.podspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ Pod::Spec.new do |s|
7878
s.dependency "React-nativeconfig"
7979
s.dependency "ReactCodegen"
8080
s.dependency "React-featureflagsnativemodule"
81+
s.dependency "React-microtasksnativemodule"
8182

8283
add_dependency(s, "ReactCommon", :subspec => "turbomodule/core", :additional_framework_paths => ["react/nativemodule/core"])
8384
add_dependency(s, "React-NativeModulesApple")

packages/react-native/Libraries/Core/setUpTimers.js

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
'use strict';
1212

13-
const {isNativeFunction} = require('../Utilities/FeatureDetection');
13+
const ReactNativeFeatureFlags = require('../../src/private/featureflags/ReactNativeFeatureFlags');
14+
const NativeReactNativeFeatureFlags =
15+
require('../../src/private/featureflags/specs/NativeReactNativeFeatureFlags').default;
1416
const {polyfillGlobal} = require('../Utilities/PolyfillFunctions');
1517

1618
if (__DEV__) {
@@ -19,14 +21,6 @@ if (__DEV__) {
1921
}
2022
}
2123

22-
// Currently, Hermes `Promise` is implemented via Internal Bytecode.
23-
const hasHermesPromiseQueuedToJSVM =
24-
global.HermesInternal?.hasPromise?.() === true &&
25-
global.HermesInternal?.useEngineQueue?.() === true;
26-
27-
const hasNativePromise = isNativeFunction(Promise);
28-
const hasPromiseQueuedToJSVM = hasNativePromise || hasHermesPromiseQueuedToJSVM;
29-
3024
// In bridgeless mode, timers are host functions installed from cpp.
3125
if (global.RN$Bridgeless !== true) {
3226
/**
@@ -56,13 +50,33 @@ if (global.RN$Bridgeless !== true) {
5650
defineLazyTimer('cancelIdleCallback');
5751
}
5852

59-
/**
60-
* Set up immediate APIs, which is required to use the same microtask queue
61-
* as the Promise.
62-
*/
63-
if (hasPromiseQueuedToJSVM) {
64-
// When promise queues to the JSVM microtasks queue, we shim the immediate
65-
// APIs via `queueMicrotask` to maintain the backward compatibility.
53+
// We need to check if the native module is available before accessing the
54+
// feature flag, because otherwise the API would throw an error in the legacy
55+
// architecture in OSS, where the native module isn't available.
56+
if (
57+
NativeReactNativeFeatureFlags != null &&
58+
ReactNativeFeatureFlags.enableMicrotasks()
59+
) {
60+
// This is the flag that tells React to use `queueMicrotask` to batch state
61+
// updates, instead of using the scheduler to schedule a regular task.
62+
// We use a global variable because we don't currently have any other
63+
// mechanism to pass feature flags from RN to React in OSS.
64+
global.RN$enableMicrotasksInReact = true;
65+
66+
polyfillGlobal('queueMicrotask', () => {
67+
const nativeQueueMicrotask =
68+
require('../../src/private/webapis/microtasks/specs/NativeMicrotasks')
69+
.default?.queueMicrotask;
70+
if (nativeQueueMicrotask) {
71+
return nativeQueueMicrotask;
72+
} else {
73+
// For backwards-compatibility
74+
return global.HermesInternal?.enqueueJob;
75+
}
76+
});
77+
78+
// We shim the immediate APIs via `queueMicrotask` to maintain the backward
79+
// compatibility.
6680
polyfillGlobal(
6781
'setImmediate',
6882
() => require('./Timers/immediateShim').setImmediate,
@@ -72,6 +86,12 @@ if (hasPromiseQueuedToJSVM) {
7286
() => require('./Timers/immediateShim').clearImmediate,
7387
);
7488
} else {
89+
// Polyfill it with promise (regardless it's polyfilled or native) otherwise.
90+
polyfillGlobal(
91+
'queueMicrotask',
92+
() => require('./Timers/queueMicrotask.js').default,
93+
);
94+
7595
// When promise was polyfilled hence is queued to the RN microtask queue,
7696
// we polyfill the immediate APIs as aliases to the ReactNativeMicrotask APIs.
7797
// Note that in bridgeless mode, immediate APIs are installed from cpp.
@@ -86,18 +106,3 @@ if (hasPromiseQueuedToJSVM) {
86106
);
87107
}
88108
}
89-
90-
/**
91-
* Set up the microtask queueing API, which is required to use the same
92-
* microtask queue as the Promise.
93-
*/
94-
if (hasHermesPromiseQueuedToJSVM) {
95-
// Fast path for Hermes.
96-
polyfillGlobal('queueMicrotask', () => global.HermesInternal?.enqueueJob);
97-
} else {
98-
// Polyfill it with promise (regardless it's polyfilled or native) otherwise.
99-
polyfillGlobal(
100-
'queueMicrotask',
101-
() => require('./Timers/queueMicrotask.js').default,
102-
);
103-
}

packages/react-native/ReactAndroid/src/main/jni/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ add_react_common_subdir(react/bridging)
102102
add_react_common_subdir(react/renderer/mapbuffer)
103103
add_react_common_subdir(react/nativemodule/core)
104104
add_react_common_subdir(react/nativemodule/featureflags)
105+
add_react_common_subdir(react/nativemodule/microtasks)
105106
add_react_common_subdir(jserrorhandler)
106107
add_react_common_subdir(react/runtime)
107108
add_react_common_subdir(react/runtime/hermes)

packages/react-native/ReactAndroid/src/main/jni/react/newarchdefaults/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ target_link_libraries(react_newarchdefaults
2222
react_codegen_rncore
2323
react_cxxreactpackage
2424
react_nativemodule_featureflags
25+
react_nativemodule_microtasks
2526
jsi)

packages/react-native/ReactAndroid/src/main/jni/react/newarchdefaults/DefaultTurboModuleManagerDelegate.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <algorithm>
1111

1212
#include <react/nativemodule/featureflags/NativeReactNativeFeatureFlags.h>
13+
#include <react/nativemodule/microtasks/NativeMicrotasks.h>
1314
#include <rncore.h>
1415

1516
namespace facebook::react {
@@ -78,6 +79,10 @@ std::shared_ptr<TurboModule> DefaultTurboModuleManagerDelegate::getTurboModule(
7879
return std::make_shared<NativeReactNativeFeatureFlags>(jsInvoker);
7980
}
8081

82+
if (name == NativeMicrotasks::kModuleName) {
83+
return std::make_shared<NativeMicrotasks>(jsInvoker);
84+
}
85+
8186
return nullptr;
8287
}
8388

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright (c) Meta Platforms, Inc. and 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+
cmake_minimum_required(VERSION 3.13)
7+
set(CMAKE_VERBOSE_MAKEFILE on)
8+
9+
add_compile_options(
10+
-fexceptions
11+
-frtti
12+
-std=c++20
13+
-Wall
14+
-Wpedantic
15+
-DLOG_TAG=\"ReactNative\")
16+
17+
file(GLOB react_nativemodule_microtasks_SRC CONFIGURE_DEPENDS *.cpp)
18+
add_library(react_nativemodule_microtasks SHARED ${react_nativemodule_microtasks_SRC})
19+
20+
target_include_directories(react_nativemodule_microtasks PUBLIC ${REACT_COMMON_DIR})
21+
22+
target_link_libraries(react_nativemodule_microtasks
23+
react_codegen_rncore
24+
reactnative)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and 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 "NativeMicrotasks.h"
9+
10+
#ifdef RN_DISABLE_OSS_PLUGIN_HEADER
11+
#include "Plugins.h"
12+
#endif
13+
14+
std::shared_ptr<facebook::react::TurboModule> NativeMicrotasksModuleProvider(
15+
std::shared_ptr<facebook::react::CallInvoker> jsInvoker) {
16+
return std::make_shared<facebook::react::NativeMicrotasks>(
17+
std::move(jsInvoker));
18+
}
19+
20+
namespace facebook::react {
21+
22+
NativeMicrotasks::NativeMicrotasks(std::shared_ptr<CallInvoker> jsInvoker)
23+
: NativeMicrotasksCxxSpec(std::move(jsInvoker)) {}
24+
25+
void NativeMicrotasks::queueMicrotask(
26+
jsi::Runtime& runtime,
27+
jsi::Function callback) {
28+
runtime.queueMicrotask(callback);
29+
}
30+
31+
} // namespace facebook::react
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and 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+
#if __has_include("rncoreJSI.h") // Cmake headers on Android
11+
#include "rncoreJSI.h"
12+
#elif __has_include("FBReactNativeSpecJSI.h") // CocoaPod headers on Apple
13+
#include "FBReactNativeSpecJSI.h"
14+
#else
15+
#include <FBReactNativeSpec/FBReactNativeSpecJSI.h>
16+
#endif
17+
18+
namespace facebook::react {
19+
20+
class NativeMicrotasks : public NativeMicrotasksCxxSpec<NativeMicrotasks>,
21+
std::enable_shared_from_this<NativeMicrotasks> {
22+
public:
23+
NativeMicrotasks(std::shared_ptr<CallInvoker> jsInvoker);
24+
25+
void queueMicrotask(jsi::Runtime& runtime, jsi::Function callback);
26+
};
27+
28+
} // namespace facebook::react
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright (c) Meta Platforms, Inc. and 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+
require "json"
7+
8+
package = JSON.parse(File.read(File.join(__dir__, "..", "..", "..", "..", "package.json")))
9+
version = package['version']
10+
11+
source = { :git => 'https://github.com/facebook/react-native.git' }
12+
if version == '1000.0.0'
13+
# This is an unpublished version, use the latest commit hash of the react-native repo, which we’re presumably in.
14+
source[:commit] = `git rev-parse HEAD`.strip if system("git rev-parse --git-dir > /dev/null 2>&1")
15+
else
16+
source[:tag] = "v#{version}"
17+
end
18+
19+
header_search_paths = []
20+
21+
if ENV['USE_FRAMEWORKS']
22+
header_search_paths << "\"$(PODS_TARGET_SRCROOT)/../../..\"" # this is needed to allow the microtasks module access its own files
23+
end
24+
25+
Pod::Spec.new do |s|
26+
s.name = "React-microtasksnativemodule"
27+
s.version = version
28+
s.summary = "React Native microtasks native module"
29+
s.homepage = "https://reactnative.dev/"
30+
s.license = package["license"]
31+
s.author = "Meta Platforms, Inc. and its affiliates"
32+
s.platforms = min_supported_versions
33+
s.source = source
34+
s.source_files = "*.{cpp,h}"
35+
s.header_dir = "react/nativemodule/microtasks"
36+
s.pod_target_xcconfig = { "CLANG_CXX_LANGUAGE_STANDARD" => "c++20",
37+
"HEADER_SEARCH_PATHS" => header_search_paths.join(' '),
38+
"DEFINES_MODULE" => "YES" }
39+
40+
if ENV['USE_FRAMEWORKS']
41+
s.module_name = "React_microtasksnativemodule"
42+
s.header_mappings_dir = "../.."
43+
end
44+
45+
install_modules_dependencies(s)
46+
47+
s.dependency "ReactCommon/turbomodule/core"
48+
end

0 commit comments

Comments
 (0)