Skip to content

Commit 036f47e

Browse files
rubennortefacebook-github-bot
authored andcommitted
Add queueMicrotask method to JSI
Summary: Changelog: [internal] ## Context Microtasks are an important aspect of JavaScript and they will become increasingly important in the hosts where we're currently using JSI. For example, React Native is going to adopt an event loop processing model similar to the one on the Web, which means it would need the ability to schedule and execute microtasks in every iteration of the loop. See react-native-community/discussions-and-proposals#744 for details. JSI already has a method to execute all pending microtasks (`drainMicrotasks`) but without a method to schedule microtasks this is incomplete. We're currently testing microtasks with Hermes using an internal method to schedule microtasks (`HermesInternal.enqueueJob`) but we need a method in JSI so this also works in other runtimes like JSC and V8. ## Changes This adds the `queueMicrotask` to the Runtime API in JSI so we have symmetric API for microtasks and we can implement the necessary functionality. The expectation for JSI implementations is to queue microtasks from this method and from built-ins like Promises and async functions in the same queue, and not drain that queue until explicitly done via `drainMicrotasks` in JSI. This also modifies Hermes and JSC to provide stubs for those methods, and the actual implementation will be done in following diffs. Reviewed By: neildhar Differential Revision: D54302536 fbshipit-source-id: 25f52f91d7ef1a51687c431d2c7562c373dc72a5
1 parent 58b45e8 commit 036f47e

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

packages/react-native/ReactCommon/jsi/jsi/decorator.h

+7
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ class RuntimeDecorator : public Base, private jsi::Instrumentation {
126126
const std::shared_ptr<const PreparedJavaScript>& js) override {
127127
return plain().evaluatePreparedJavaScript(js);
128128
}
129+
void queueMicrotask(const jsi::Function& callback) override {
130+
return plain().queueMicrotask(callback);
131+
}
129132
bool drainMicrotasks(int maxMicrotasksHint) override {
130133
return plain().drainMicrotasks(maxMicrotasksHint);
131134
}
@@ -544,6 +547,10 @@ class WithRuntimeDecorator : public RuntimeDecorator<Plain, Base> {
544547
Around around{with_};
545548
return RD::evaluatePreparedJavaScript(js);
546549
}
550+
void queueMicrotask(const Function& callback) override {
551+
Around around{with_};
552+
RD::queueMicrotask(callback);
553+
}
547554
bool drainMicrotasks(int maxMicrotasksHint) override {
548555
Around around{with_};
549556
return RD::drainMicrotasks(maxMicrotasksHint);

packages/react-native/ReactCommon/jsi/jsi/jsi.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ NativeState::~NativeState() {}
8787

8888
Runtime::~Runtime() {}
8989

90+
void Runtime::queueMicrotask(const jsi::Function& /*callback*/) {
91+
throw JSINativeException("queueMicrotask is not implemented in this runtime");
92+
}
93+
9094
Instrumentation& Runtime::instrumentation() {
9195
class NoInstrumentation : public Instrumentation {
9296
std::string getRecordedGCStats() override {

packages/react-native/ReactCommon/jsi/jsi/jsi.h

+7
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,13 @@ class JSI_EXPORT Runtime {
209209
virtual Value evaluatePreparedJavaScript(
210210
const std::shared_ptr<const PreparedJavaScript>& js) = 0;
211211

212+
/// Queues a microtask in the JavaScript VM internal Microtask (a.k.a. Job in
213+
/// ECMA262) queue, to be executed when the host drains microtasks in
214+
/// its event loop implementation.
215+
///
216+
/// \param callback a function to be executed as a microtask.
217+
virtual void queueMicrotask(const jsi::Function& callback);
218+
212219
/// Drain the JavaScript VM internal Microtask (a.k.a. Job in ECMA262) queue.
213220
///
214221
/// \param maxMicrotasksHint a hint to tell an implementation that it should

0 commit comments

Comments
 (0)