Files
react-native/private/react-native-fantom/runner/entrypoint-template.js
Rubén Norte 789fc57254 Improve API to take JS heap snapshots in Fantom (#53071)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/53071

Changelog: [internal]

The current API to take JS heap snapshots has some problems:
1. Ergonomics: it requires you to input the filepath where you want to store the snapshot. This isn't aligned with the behavior we have for JS traces where the output path is provided to you.
2. It doesn't work in optimized builds, as it requires a specific option in Hermes.

For 1), this replaces `Fantom.saveJSMemoryHeapSnapshot(filePath)` with `Fantom.takeJSMemoryHeapSnapshot()` that outputs the snapshot in a predefined path and prints it to the console.

For 2), this adds a new environment variable to force building Hermes with memory instrumentation (`FANTOM_ENABLE_JS_MEMORY_INSTRUMENTATION`). This is exposed as an option and not set by default because it has a performance overhead at runtime that we don't want to pay (especially in benchmarks).

This option only works when using Buck in development, because we want to generate this new binary type on demand when necessary, instead of making it part of the prebuilts we do before running tests in OSS and CI.

Reviewed By: lenaic

Differential Revision: D79642314

fbshipit-source-id: a2980616a495bd6dca29c0709a9581db6fb3f2cc
2025-08-06 05:40:02 -07:00

89 lines
2.5 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/
import type {SnapshotConfig} from '../runtime/snapshotContext';
import type {FantomRuntimeConstants} from '../src/Constants';
import type {FantomTestConfig} from './getFantomTestConfigs';
import * as EnvironmentOptions from './EnvironmentOptions';
import formatFantomConfig from './formatFantomConfig';
module.exports = function entrypointTemplate({
testPath,
setupModulePath,
featureFlagsModulePath,
testConfig,
snapshotConfig,
jsTraceOutputPath,
jsHeapSnapshotOutputPathTemplate,
jsHeapSnapshotOutputPathTemplateToken,
}: {
testPath: string,
setupModulePath: string,
featureFlagsModulePath: string,
testConfig: FantomTestConfig,
snapshotConfig: SnapshotConfig,
jsHeapSnapshotOutputPathTemplate: string,
jsHeapSnapshotOutputPathTemplateToken: string,
jsTraceOutputPath: ?string,
}): string {
const constants: FantomRuntimeConstants = {
isOSS: EnvironmentOptions.isOSS,
isRunningFromCI: EnvironmentOptions.isCI,
forceTestModeForBenchmarks: EnvironmentOptions.forceTestModeForBenchmarks,
fantomConfigSummary: formatFantomConfig(testConfig),
jsTraceOutputPath,
jsHeapSnapshotOutputPathTemplate,
jsHeapSnapshotOutputPathTemplateToken,
};
return `/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* ${'@'}generated
* @noformat
* @noflow
*/
import {registerTest} from '${setupModulePath}';
import {setConstants} from '@react-native/fantom/src/Constants';
${
Object.keys(testConfig.flags.jsOnly).length > 0
? `import * as ReactNativeFeatureFlags from '${featureFlagsModulePath}';
ReactNativeFeatureFlags.override({
${Object.entries(testConfig.flags.jsOnly)
.map(([name, value]) => ` ${name}: () => ${JSON.stringify(value)},`)
.join('\n')}
});`
: ''
}
${
Object.keys(testConfig.flags.reactInternal).length > 0
? `import ReactNativeInternalFeatureFlags from 'ReactNativeInternalFeatureFlags';
${Object.entries(testConfig.flags.reactInternal)
.map(
([name, value]) =>
`ReactNativeInternalFeatureFlags.${name} = ${JSON.stringify(value)};`,
)
.join('\n')}`
: ''
}
setConstants(${JSON.stringify(constants)});
registerTest(() => require('${testPath}'), ${JSON.stringify(snapshotConfig)});
`;
};