Skip to content

Commit 1537321

Browse files
sbuggayfacebook-github-bot
authored andcommitted
Add granular control with pragma modes (#52894)
Summary: Pull Request resolved: #52894 Adds the following boolean pragma modes for more granular control: ``` fantom_native_opt true|false fantom_js_opt true|false fantom_js_bytecode true|false ``` Previously these were all set together with `fantom_mode`. These modes are mutually exclusive with `fantom_mode`. Changelog: [Internal] Reviewed By: rubennorte Differential Revision: D79151687 fbshipit-source-id: 59c3f20bccb570c0293ffd037609946a1a9bbb8f
1 parent 6d51bce commit 1537321

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

private/react-native-fantom/__docs__/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,27 @@ Available pragmas:
132132
- Possible values:
133133
- `dev`: development, default for tests.
134134
- `opt`: optimized and using Hermes bytecode, default for benchmarks.
135+
- `@fantom_native_opt`: used to define the compilation mode for native code.
136+
- Example: `@fantom_native_opt true`
137+
- Possible values:
138+
- `true`: optimized native code
139+
- `false`: development native code
140+
- `@fantom_js_opt`: used to define the compilation mode for the JS bundle.
141+
- Example: `@fantom_js_opt true`
142+
- Possible values:
143+
- `true`: optimized JS bundle
144+
- `false`: development JS bundle
145+
- `@fantom_js_bytecode`: used to define if the JS bundle should use bytecode.
146+
- Example: `@fantom_js_bytecode true`
147+
- Possible values:
148+
- `true`: using Hermes bytecode
149+
- `false`: not using Hermes bytecode
135150
- `@fantom_react_fb_flags`: used to set overrides for internal React flags set
136151
in ReactNativeInternalFeatureFlags (Meta use only)
137152

153+
Setting `@fantom_mode` is mutually exclusive with setting any of
154+
`@fantom_native_opt`, `@fantom_js_opt`, and `@fantom_js_bytecode`.
155+
138156
For all pragmas, except non-boolean feature flags, you can use a wildcard (`*`)
139157
as value to run the test with all possible values for that pragma. For example,
140158
this test:

private/react-native-fantom/runner/getFantomTestConfigs.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ const MAX_FANTOM_CONFIGURATION_VARIATIONS = 12;
7979

8080
const VALID_FANTOM_PRAGMAS = [
8181
'fantom_mode',
82+
'fantom_native_opt',
83+
'fantom_js_opt',
84+
'fantom_js_bytecode',
8285
'fantom_flags',
8386
'fantom_hermes_variant',
8487
'fantom_react_fb_flags',
@@ -230,6 +233,56 @@ export default function getFantomTestConfigs(
230233
config.isJsOptimized = true;
231234
config.isJsBytecode = true;
232235
}
236+
237+
// Allow the benchmark regex to override these to true, but if the mode isn't set
238+
// allow granular control with pragmas. Checking for both of them being set is handled by
239+
// verifyFantomPragmas().
240+
if (pragmas.fantom_native_opt !== undefined) {
241+
if (pragmas.fantom_native_opt === '*') {
242+
configVariations.push([
243+
{
244+
isNativeOptimized: false,
245+
},
246+
{
247+
isNativeOptimized: true,
248+
},
249+
]);
250+
} else {
251+
config.isNativeOptimized = parseFantomBoolean(
252+
pragmas.fantom_native_opt,
253+
);
254+
}
255+
}
256+
257+
if (pragmas.fantom_js_opt !== undefined) {
258+
if (pragmas.fantom_js_opt === '*') {
259+
configVariations.push([
260+
{
261+
isJsOptimized: false,
262+
},
263+
{
264+
isJsOptimized: true,
265+
},
266+
]);
267+
} else {
268+
config.isJsOptimized = parseFantomBoolean(pragmas.fantom_js_opt);
269+
}
270+
}
271+
272+
if (pragmas.fantom_js_bytecode !== undefined) {
273+
if (pragmas.fantom_js_bytecode === '*') {
274+
configVariations.push([
275+
{
276+
isJsBytecode: false,
277+
},
278+
{
279+
isJsBytecode: true,
280+
},
281+
]);
282+
} else {
283+
config.isJsBytecode = parseFantomBoolean(pragmas.fantom_js_bytecode);
284+
}
285+
}
233286
}
234287

235288
const maybeHermesVariant = pragmas.fantom_hermes_variant;
@@ -460,7 +513,30 @@ function parseFeatureFlagValue<T: boolean | number | string>(
460513
}
461514
}
462515

516+
function parseFantomBoolean(pragmaValue: string | Array<string>): boolean {
517+
if (Array.isArray(pragmaValue)) {
518+
throw new Error(`Expected a single value, got ${pragmaValue.join(', ')}`);
519+
}
520+
521+
if (pragmaValue !== 'true' && pragmaValue !== 'false') {
522+
throw new Error(`Expected a boolean, got ${pragmaValue}`);
523+
}
524+
525+
return pragmaValue === 'true';
526+
}
527+
463528
function verifyFantomPragmas(pragmas: DocblockPragmas): void {
529+
if (
530+
'fantom_mode' in pragmas &&
531+
('fantom_native_opt' in pragmas ||
532+
'fantom_js_opt' in pragmas ||
533+
'fantom_js_bytecode' in pragmas)
534+
) {
535+
throw new Error(
536+
'Cannot set @fantom_mode with @fantom_native_opt, @fantom_js_opt, or @fantom_js_bytecode',
537+
);
538+
}
539+
464540
for (const pragma of Object.keys(pragmas)) {
465541
if (
466542
pragma.startsWith('fantom_') &&
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
* @fantom_js_bytecode true
8+
* @flow strict-local
9+
* @format
10+
*/
11+
12+
describe('"@fantom_js_bytecode" in docblock', () => {
13+
it('should use development builds', () => {
14+
expect(__DEV__).toBe(true);
15+
});
16+
});

0 commit comments

Comments
 (0)