Skip to content

Commit

Permalink
refactor(ses): commons async gen fn instance to avoid name dance
Browse files Browse the repository at this point in the history
Co-authored-by: legobeat <109787230+legobeat@users.noreply.github.com>
  • Loading branch information
leotm and legobeat committed Sep 23, 2024
1 parent 0bff9a0 commit a8e6593
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions packages/ses/src/commons.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,31 +379,34 @@ export const FERAL_STACK_GETTER = feralStackGetter;
*/
export const FERAL_STACK_SETTER = feralStackSetter;

// Test for async generator function syntax support.
let AsyncGeneratorNewFunctionInstance;
try {
// Wrapping one in an new Function lets the `hermesc` binary file
// parse the Metro js bundle without SyntaxError, to generate the
// optimised Hermes bytecode bundle, when `gradlew` is called to
// assemble the release build APK for React Native prod Android apps.
// Delaying the error until runtime lets us customise lockdown behaviour.
AsyncGeneratorNewFunctionInstance = new FERAL_FUNCTION(
'return (async function* AsyncGeneratorFunctionInstance() {})',
)();
} catch (e) {
// @ts-expect-error ts(2339) Property 'jsEngine' does not exist on type 'Error'. However it exists on Hermes.
if (Error.prototype.jsEngine === 'hermes' && e.name === 'SyntaxError') {
// Swallows Hermes error `async generators are unsupported` at runtime.
// @ts-expect-error ts(2554) Expected 0 arguments, but got 1. It refers to the Web API Window object, but on Hermes we expect 1 argument.
// eslint-disable-next-line no-undef
print('SES: Skipping async generators, unsupported on Hermes');
// Note: `console` is not a JS built-in, so Hermes engine throws:
// Uncaught ReferenceError: Property 'console' doesn't exist
// See: https://github.com/facebook/hermes/issues/675
// However React Native provides a `console` implementation:
// https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/InitializeCore.js
} else {
throw e;
// eslint-disable-next-line consistent-return
const getAsyncGeneratorFunctionInstance = () => {
// Test for async generator function syntax support.
try {
// Wrapping one in an new Function lets the `hermesc` binary file
// parse the Metro js bundle without SyntaxError, to generate the
// optimised Hermes bytecode bundle, when `gradlew` is called to
// assemble the release build APK for React Native prod Android apps.
// Delaying the error until runtime lets us customise lockdown behaviour.
return new FERAL_FUNCTION(
'return (async function* AsyncGeneratorFunctionInstance() {})',
)();
} catch (e) {
// @ts-expect-error ts(2339) Property 'jsEngine' does not exist on type 'Error'. However it exists on Hermes.
if (Error.prototype.jsEngine === 'hermes' && e.name === 'SyntaxError') {
// Swallows Hermes error `async generators are unsupported` at runtime.
// @ts-expect-error ts(2554) Expected 0 arguments, but got 1. It refers to the Web API Window object, but on Hermes we expect 1 argument.
// eslint-disable-next-line no-undef
print('SES: Skipping async generators, unsupported on Hermes');
// Note: `console` is not a JS built-in, so Hermes engine throws:
// Uncaught ReferenceError: Property 'console' doesn't exist
// See: https://github.com/facebook/hermes/issues/675
// However React Native provides a `console` implementation:
// https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/InitializeCore.js
} else {
throw e;
}
}
}
export const AsyncGeneratorFunctionInstance = AsyncGeneratorNewFunctionInstance;
};
export const AsyncGeneratorFunctionInstance =
getAsyncGeneratorFunctionInstance();

0 comments on commit a8e6593

Please sign in to comment.