facebook / hermes

A JavaScript engine optimized for running React Native.
https://hermesengine.dev/
MIT License
9.41k stars 596 forks source link

Add queueMicrotask method to JSI #1331

Open rubennorte opened 2 months ago

rubennorte commented 2 months ago

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.

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.

Differential Revision: D54302536

facebook-github-bot commented 2 months ago

This pull request was exported from Phabricator. Differential Revision: D54302536

facebook-github-bot commented 2 months ago

This pull request was exported from Phabricator. Differential Revision: D54302536

facebook-github-bot commented 2 months ago

This pull request was exported from Phabricator. Differential Revision: D54302536