Skip to content

Commit 9ad2da3

Browse files
rubennortefacebook-github-bot
authored andcommitted
Implement microtasks using new JSI method
Summary: Changelog: [internal] This migrates the Hermes-specific use of microtasks to an engine agnostic implementation based on the new JSI method to queue microtasks. Differential Revision: D54687056
1 parent b24b949 commit 9ad2da3

File tree

10 files changed

+158
-70
lines changed

10 files changed

+158
-70
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: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
'use strict';
1212

13-
const {isNativeFunction} = require('../Utilities/FeatureDetection');
13+
const ReactNativeFeatureFlags = require('../../src/private/featureflags/ReactNativeFeatureFlags');
1414
const {polyfillGlobal} = require('../Utilities/PolyfillFunctions');
1515

1616
if (__DEV__) {
@@ -19,14 +19,6 @@ if (__DEV__) {
1919
}
2020
}
2121

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-
3022
// In bridgeless mode, timers are host functions installed from cpp.
3123
if (global.RN$Bridgeless !== true) {
3224
/**
@@ -60,7 +52,19 @@ if (global.RN$Bridgeless !== true) {
6052
* Set up immediate APIs, which is required to use the same microtask queue
6153
* as the Promise.
6254
*/
63-
if (hasPromiseQueuedToJSVM) {
55+
if (ReactNativeFeatureFlags.enableMicrotasks()) {
56+
polyfillGlobal('queueMicrotask', () => {
57+
const nativeQueueMicrotask =
58+
require('../../src/private/webapis/microtasks/specs/NativeMicrotasks')
59+
.default?.queueMicrotask;
60+
if (nativeQueueMicrotask) {
61+
return nativeQueueMicrotask;
62+
} else {
63+
// For backwards-compatibility
64+
return global.HermesInternal?.enqueueJob;
65+
}
66+
});
67+
6468
// When promise queues to the JSVM microtasks queue, we shim the immediate
6569
// APIs via `queueMicrotask` to maintain the backward compatibility.
6670
polyfillGlobal(
@@ -72,6 +76,12 @@ if (hasPromiseQueuedToJSVM) {
7276
() => require('./Timers/immediateShim').clearImmediate,
7377
);
7478
} else {
79+
// Polyfill it with promise (regardless it's polyfilled or native) otherwise.
80+
polyfillGlobal(
81+
'queueMicrotask',
82+
() => require('./Timers/queueMicrotask.js').default,
83+
);
84+
7585
// When promise was polyfilled hence is queued to the RN microtask queue,
7686
// we polyfill the immediate APIs as aliases to the ReactNativeMicrotask APIs.
7787
// Note that in bridgeless mode, immediate APIs are installed from cpp.
@@ -86,18 +96,3 @@ if (hasPromiseQueuedToJSVM) {
8696
);
8797
}
8898
}
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/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: 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+
const 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

packages/react-native/scripts/react_native_pods.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ def use_react_native! (
126126
pod 'React-utils', :path => "#{prefix}/ReactCommon/react/utils"
127127
pod 'React-featureflags', :path => "#{prefix}/ReactCommon/react/featureflags"
128128
pod 'React-featureflagsnativemodule', :path => "#{prefix}/ReactCommon/react/nativemodule/featureflags"
129+
pod 'React-microtasksnativemodule', :path => "#{prefix}/ReactCommon/react/nativemodule/microtasks"
129130
pod 'React-Mapbuffer', :path => "#{prefix}/ReactCommon"
130131
pod 'React-jserrorhandler', :path => "#{prefix}/ReactCommon/jserrorhandler"
131132
pod 'React-nativeconfig', :path => "#{prefix}/ReactCommon"

packages/react-native/src/private/featureflags/NativeReactNativeFeatureFlags.js

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
* @flow strict
8+
* @format
9+
*/
10+
11+
import type {TurboModule} from '../../../../../Libraries/TurboModule/RCTExport';
12+
13+
import * as TurboModuleRegistry from '../../../../../Libraries/TurboModule/TurboModuleRegistry';
14+
15+
export interface Spec extends TurboModule {
16+
+queueMicrotask: (callback: () => mixed) => void;
17+
}
18+
19+
export default (TurboModuleRegistry.get<Spec>('NativeMicrotasksCxx'): ?Spec);

0 commit comments

Comments
 (0)