embedded-react 0.15.0 → 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/aot/compile.mjs +173 -8272
- package/aot/compiler/animations.mjs +1001 -0
- package/aot/compiler/assemble.mjs +922 -0
- package/aot/compiler/boot.mjs +441 -0
- package/aot/compiler/branch-merge.mjs +319 -0
- package/aot/compiler/c/animations.mjs +362 -0
- package/aot/compiler/c/boot.mjs +127 -0
- package/aot/compiler/c/expressions.mjs +270 -0
- package/aot/compiler/c/functions.mjs +268 -0
- package/aot/compiler/c/keyboard.mjs +96 -0
- package/aot/compiler/c/nodes.mjs +294 -0
- package/aot/compiler/c/pan-responder.mjs +136 -0
- package/aot/compiler/c/statements.mjs +271 -0
- package/aot/compiler/c/text.mjs +83 -0
- package/aot/compiler/c/vectors.mjs +381 -0
- package/aot/compiler/c-syntax.mjs +127 -0
- package/aot/compiler/check.mjs +165 -0
- package/aot/compiler/collect.mjs +785 -0
- package/aot/compiler/control-flow.mjs +696 -0
- package/aot/compiler/diagnostics.mjs +137 -0
- package/aot/compiler/elements/activity-indicator.mjs +160 -0
- package/aot/compiler/elements/dial.mjs +346 -0
- package/aot/compiler/elements/flat-list.mjs +139 -0
- package/aot/compiler/elements/image.mjs +151 -0
- package/aot/compiler/elements/modal.mjs +158 -0
- package/aot/compiler/elements/props.mjs +474 -0
- package/aot/compiler/elements/switch.mjs +183 -0
- package/aot/compiler/elements/text-input.mjs +171 -0
- package/aot/compiler/expressions.mjs +1579 -0
- package/aot/compiler/handlers.mjs +1072 -0
- package/aot/compiler/ir/animations.mjs +16 -0
- package/aot/compiler/ir/app.mjs +16 -0
- package/aot/compiler/ir/boot.mjs +16 -0
- package/aot/compiler/ir/expressions.mjs +16 -0
- package/aot/compiler/ir/functions.mjs +16 -0
- package/aot/compiler/ir/keyboard.mjs +16 -0
- package/aot/compiler/ir/nodes.mjs +107 -0
- package/aot/compiler/ir/pan-responder.mjs +16 -0
- package/aot/compiler/ir/reads.mjs +351 -0
- package/aot/compiler/ir/source.mjs +16 -0
- package/aot/compiler/ir/statements.mjs +16 -0
- package/aot/compiler/ir/text.mjs +16 -0
- package/aot/compiler/ir/types.mjs +185 -0
- package/aot/compiler/ir/vectors.mjs +16 -0
- package/aot/compiler/keyboard.mjs +152 -0
- package/aot/compiler/lower.mjs +311 -0
- package/aot/compiler/nodes.mjs +688 -0
- package/aot/compiler/options.mjs +55 -0
- package/aot/compiler/out.mjs +94 -0
- package/aot/compiler/pan-responder.mjs +354 -0
- package/aot/compiler/parse.mjs +300 -0
- package/aot/compiler/static-eval.mjs +454 -0
- package/aot/compiler/style-text.mjs +807 -0
- package/aot/compiler/svg.mjs +1167 -0
- package/aot/compiler/types.mjs +16 -0
- package/aot/screenshot-smoke.mjs +95 -55
- package/aot/style-map.mjs +366 -304
- package/cli.mjs +5 -8
- package/package.json +14 -6
- package/sim/embedded-react.wasm +0 -0
- package/src/embedded-react/FlatList.js +4 -4
- package/src/props.js +1 -1
package/aot/style-map.mjs
CHANGED
|
@@ -13,339 +13,401 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
// This is the build-time mirror of what native_ui_bridge.c does at runtime in Flow A: it turns a
|
|
20
|
-
// flattened style object into ERProps field writes. The AOT compiler emits those writes as C source
|
|
21
|
-
// (e.g. `p.background_color = 0xFF0F172Au;`) so the generated program builds byte-identical prop bags
|
|
22
|
-
// to Flow A — same defaults (er_props_default), same field values → pixel-identical rendering.
|
|
23
|
-
//
|
|
24
|
-
// Scope: the minimal subset for the first vertical slice (the scaffold counter). Unsupported keys
|
|
25
|
-
// throw with a clear message so the compiler fails loudly instead of silently dropping a style — the
|
|
26
|
-
// supported set grows demo by demo.
|
|
27
|
-
|
|
16
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
17
|
+
- Constants
|
|
18
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
28
19
|
/** A few CSS/RN named colors (extend as demos need them); everything else must be hex. */
|
|
29
20
|
const NAMED_COLORS = {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
21
|
+
transparent: 0x00000000,
|
|
22
|
+
white: 0xffffffff,
|
|
23
|
+
black: 0xff000000,
|
|
24
|
+
red: 0xffff0000,
|
|
25
|
+
green: 0xff00ff00,
|
|
26
|
+
blue: 0xff0000ff,
|
|
36
27
|
};
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Parses a color string to a packed ARGB8888 unsigned int (matching ERProps color fields).
|
|
40
|
-
*
|
|
41
|
-
* @param {string} s '#rgb' | '#rrggbb' | '#rrggbbaa' (RN order) | a named color.
|
|
42
|
-
* @returns {number} ARGB8888 as an unsigned 32-bit integer.
|
|
43
|
-
*/
|
|
44
|
-
export function parseColorValue(s) {
|
|
45
|
-
if (typeof s !== 'string')
|
|
46
|
-
throw new Error(`color must be a string, got ${typeof s}`);
|
|
47
|
-
const key = s.trim().toLowerCase();
|
|
48
|
-
if (key in NAMED_COLORS) return NAMED_COLORS[key] >>> 0;
|
|
49
|
-
const m = /^#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i.exec(key);
|
|
50
|
-
if (!m)
|
|
51
|
-
throw new Error(
|
|
52
|
-
`unsupported color "${s}" (use #rgb / #rrggbb / #rrggbbaa or a named color)`,
|
|
53
|
-
);
|
|
54
|
-
let hex = m[1];
|
|
55
|
-
if (hex.length === 3)
|
|
56
|
-
hex = hex
|
|
57
|
-
.split('')
|
|
58
|
-
.map(c => c + c)
|
|
59
|
-
.join(''); // #rgb → #rrggbb
|
|
60
|
-
let r, g, b, a;
|
|
61
|
-
if (hex.length === 6) {
|
|
62
|
-
a = 0xff;
|
|
63
|
-
r = parseInt(hex.slice(0, 2), 16);
|
|
64
|
-
g = parseInt(hex.slice(2, 4), 16);
|
|
65
|
-
b = parseInt(hex.slice(4, 6), 16);
|
|
66
|
-
} else {
|
|
67
|
-
// RN 8-digit is #rrggbbaa (alpha last).
|
|
68
|
-
r = parseInt(hex.slice(0, 2), 16);
|
|
69
|
-
g = parseInt(hex.slice(2, 4), 16);
|
|
70
|
-
b = parseInt(hex.slice(4, 6), 16);
|
|
71
|
-
a = parseInt(hex.slice(6, 8), 16);
|
|
72
|
-
}
|
|
73
|
-
return ((a << 24) | (r << 16) | (g << 8) | b) >>> 0;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/** Formats an ARGB int as a C unsigned hex literal, e.g. 0xFF0F172Au. */
|
|
77
|
-
export function colorLiteral(s) {
|
|
78
|
-
return `0x${parseColorValue(s).toString(16).toUpperCase().padStart(8, '0')}u`;
|
|
79
|
-
}
|
|
80
|
-
|
|
28
|
+
// RN enum values → ER_* constants, shared by the static (KEYS) and state-driven (ENUM_FIELDS) lowerings.
|
|
81
29
|
const ALIGN = {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
30
|
+
auto: 'ER_ALIGN_AUTO',
|
|
31
|
+
'flex-start': 'ER_ALIGN_FLEX_START',
|
|
32
|
+
center: 'ER_ALIGN_CENTER',
|
|
33
|
+
'flex-end': 'ER_ALIGN_FLEX_END',
|
|
34
|
+
stretch: 'ER_ALIGN_STRETCH',
|
|
87
35
|
};
|
|
88
36
|
const JUSTIFY = {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
37
|
+
'flex-start': 'ER_JUSTIFY_FLEX_START',
|
|
38
|
+
center: 'ER_JUSTIFY_CENTER',
|
|
39
|
+
'flex-end': 'ER_JUSTIFY_FLEX_END',
|
|
40
|
+
'space-between': 'ER_JUSTIFY_SPACE_BETWEEN',
|
|
41
|
+
'space-around': 'ER_JUSTIFY_SPACE_AROUND',
|
|
42
|
+
'space-evenly': 'ER_JUSTIFY_SPACE_EVENLY',
|
|
95
43
|
};
|
|
96
44
|
const FLEX_DIRECTION = {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
45
|
+
column: 'ER_FLEX_COL',
|
|
46
|
+
row: 'ER_FLEX_ROW',
|
|
47
|
+
'row-reverse': 'ER_FLEX_ROW_REVERSE',
|
|
48
|
+
'column-reverse': 'ER_FLEX_COL_REVERSE',
|
|
49
|
+
};
|
|
50
|
+
const POSITION = {
|
|
51
|
+
relative: 'ER_POS_RELATIVE',
|
|
52
|
+
absolute: 'ER_POS_ABSOLUTE',
|
|
101
53
|
};
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
return c;
|
|
54
|
+
const DISPLAY = { flex: 'ER_DISPLAY_FLEX', none: 'ER_DISPLAY_NONE' };
|
|
55
|
+
/** Maps a JSX host tag to its ERNodeType enum, for the components the generic node emitter handles. */
|
|
56
|
+
export const NODE_TYPES = {
|
|
57
|
+
View: 'ER_NODE_VIEW',
|
|
58
|
+
Text: 'ER_NODE_TEXT',
|
|
59
|
+
Pressable: 'ER_NODE_PRESSABLE',
|
|
60
|
+
TouchableOpacity: 'ER_NODE_PRESSABLE',
|
|
61
|
+
Image: 'ER_NODE_IMAGE',
|
|
62
|
+
ScrollView: 'ER_NODE_SCROLL_VIEW',
|
|
112
63
|
};
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
64
|
+
/** Numeric style keys that can be state-driven and the ERProps field each one writes. */
|
|
65
|
+
const NUM_FIELDS = {
|
|
66
|
+
width: 'width',
|
|
67
|
+
height: 'height',
|
|
68
|
+
minWidth: 'min_width',
|
|
69
|
+
maxWidth: 'max_width',
|
|
70
|
+
minHeight: 'min_height',
|
|
71
|
+
maxHeight: 'max_height',
|
|
72
|
+
padding: 'padding',
|
|
73
|
+
paddingHorizontal: 'padding_horizontal',
|
|
74
|
+
paddingVertical: 'padding_vertical',
|
|
75
|
+
paddingLeft: 'padding_left',
|
|
76
|
+
paddingTop: 'padding_top',
|
|
77
|
+
paddingRight: 'padding_right',
|
|
78
|
+
paddingBottom: 'padding_bottom',
|
|
79
|
+
margin: 'margin',
|
|
80
|
+
marginHorizontal: 'margin_horizontal',
|
|
81
|
+
marginVertical: 'margin_vertical',
|
|
82
|
+
marginLeft: 'margin_left',
|
|
83
|
+
marginTop: 'margin_top',
|
|
84
|
+
marginRight: 'margin_right',
|
|
85
|
+
marginBottom: 'margin_bottom',
|
|
86
|
+
gap: 'gap',
|
|
87
|
+
rowGap: 'row_gap',
|
|
88
|
+
columnGap: 'column_gap',
|
|
89
|
+
flexGrow: 'flex_grow',
|
|
90
|
+
flexShrink: 'flex_shrink',
|
|
91
|
+
borderRadius: 'border_radius',
|
|
92
|
+
borderTopLeftRadius: 'border_top_left_radius',
|
|
93
|
+
borderTopRightRadius: 'border_top_right_radius',
|
|
94
|
+
borderBottomRightRadius: 'border_bottom_right_radius',
|
|
95
|
+
borderBottomLeftRadius: 'border_bottom_left_radius',
|
|
96
|
+
borderWidth: 'border_width',
|
|
97
|
+
zIndex: 'z_index',
|
|
98
|
+
fontSize: 'font_size',
|
|
99
|
+
lineHeight: 'line_height',
|
|
100
|
+
letterSpacing: 'letter_spacing',
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* Enum style keys that can be state-driven: the value (a string literal or a ternary of them) lowers to the
|
|
104
|
+
* matching ER_* constant via its table. Changing one in app_update re-runs layout (props_hash covers it).
|
|
105
|
+
*/
|
|
106
|
+
const ENUM_FIELDS = {
|
|
107
|
+
flexDirection: { field: 'flex_direction', table: FLEX_DIRECTION },
|
|
108
|
+
alignItems: { field: 'align_items', table: ALIGN },
|
|
109
|
+
alignSelf: { field: 'align_self', table: ALIGN },
|
|
110
|
+
justifyContent: { field: 'justify_content', table: JUSTIFY },
|
|
111
|
+
position: { field: 'position', table: POSITION },
|
|
112
|
+
display: { field: 'display', table: DISPLAY },
|
|
113
|
+
};
|
|
114
|
+
/**
|
|
115
|
+
* Every style key whose value can be state-driven, with its ERProps field and how app_update() lowers it (see
|
|
116
|
+
* DynamicStyleField). Keys absent here (such as the `flex` shorthand, `flexBasis`, the four insets and
|
|
117
|
+
* `fontWeight`) can only be static.
|
|
118
|
+
*/
|
|
119
|
+
export const DYN_FIELDS = {
|
|
120
|
+
...Object.fromEntries(Object.entries(NUM_FIELDS).map(([styleKey, field]) => [
|
|
121
|
+
styleKey,
|
|
122
|
+
{ field, kind: 'num' },
|
|
123
|
+
])),
|
|
124
|
+
...Object.fromEntries(Object.entries(ENUM_FIELDS).map(([styleKey, enumField]) => [
|
|
125
|
+
styleKey,
|
|
126
|
+
{ field: enumField.field, kind: 'enum', table: enumField.table },
|
|
127
|
+
])),
|
|
128
|
+
backgroundColor: { field: 'background_color', kind: 'color' },
|
|
129
|
+
color: { field: 'color', kind: 'color' },
|
|
130
|
+
borderColor: { field: 'border_color', kind: 'color' },
|
|
131
|
+
opacity: { field: 'opacity', kind: 'opacity' },
|
|
132
|
+
};
|
|
133
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
134
|
+
- Implementation
|
|
135
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
136
|
+
/**
|
|
137
|
+
* Parses a color string to a packed ARGB8888 unsigned int (matching ERProps color fields).
|
|
138
|
+
*
|
|
139
|
+
* @param color '#rgb' | '#rrggbb' | '#rrggbbaa' (RN order) | a named color.
|
|
140
|
+
*
|
|
141
|
+
* @returns ARGB8888 as an unsigned 32-bit integer.
|
|
142
|
+
*/
|
|
143
|
+
export function parseColorValue(color) {
|
|
144
|
+
if (typeof color !== 'string') {
|
|
145
|
+
throw new Error(`color must be a string, got ${typeof color}`);
|
|
146
|
+
}
|
|
147
|
+
// Accept a named color, else a hex string, expanding the 3-digit form to 6.
|
|
148
|
+
const normalized = color.trim().toLowerCase();
|
|
149
|
+
if (normalized in NAMED_COLORS)
|
|
150
|
+
return NAMED_COLORS[normalized] >>> 0;
|
|
151
|
+
const hexMatch = /^#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i.exec(normalized);
|
|
152
|
+
if (!hexMatch) {
|
|
153
|
+
throw new Error(`unsupported color "${color}" (use #rgb / #rrggbb / #rrggbbaa or a named color)`);
|
|
154
|
+
}
|
|
155
|
+
// #rgb → #rrggbb
|
|
156
|
+
let hex = hexMatch[1];
|
|
157
|
+
if (hex.length === 3) {
|
|
158
|
+
hex = hex
|
|
159
|
+
.split('')
|
|
160
|
+
.map(digit => digit + digit)
|
|
161
|
+
.join('');
|
|
162
|
+
}
|
|
163
|
+
// Split out the channels; a 6-digit color is opaque.
|
|
164
|
+
let red, green, blue, alpha;
|
|
165
|
+
if (hex.length === 6) {
|
|
166
|
+
alpha = 0xff;
|
|
167
|
+
red = parseInt(hex.slice(0, 2), 16);
|
|
168
|
+
green = parseInt(hex.slice(2, 4), 16);
|
|
169
|
+
blue = parseInt(hex.slice(4, 6), 16);
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
// RN 8-digit is #rrggbbaa (alpha last).
|
|
173
|
+
red = parseInt(hex.slice(0, 2), 16);
|
|
174
|
+
green = parseInt(hex.slice(2, 4), 16);
|
|
175
|
+
blue = parseInt(hex.slice(4, 6), 16);
|
|
176
|
+
alpha = parseInt(hex.slice(6, 8), 16);
|
|
177
|
+
}
|
|
178
|
+
return ((alpha << 24) | (red << 16) | (green << 8) | blue) >>> 0;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Formats an ARGB int as a C unsigned hex literal, e.g. 0xFF0F172Au.
|
|
182
|
+
*
|
|
183
|
+
* @param color A color string (see parseColorValue).
|
|
184
|
+
*
|
|
185
|
+
* @returns Its C literal.
|
|
186
|
+
*/
|
|
187
|
+
export function colorLiteral(color) {
|
|
188
|
+
return `0x${parseColorValue(color).toString(16).toUpperCase().padStart(8, '0')}u`;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Looks an enum style value up in its table.
|
|
192
|
+
*
|
|
193
|
+
* @param table The RN value → ER_* constant table.
|
|
194
|
+
* @param name The style key, for the error.
|
|
195
|
+
*
|
|
196
|
+
* @returns A lookup that throws on a value the table does not have.
|
|
197
|
+
*/
|
|
198
|
+
const enumKey = (table, name) => (value) => {
|
|
199
|
+
const constant = table[value];
|
|
200
|
+
if (!constant) {
|
|
201
|
+
throw new Error(`${name}: unsupported value "${value}" (one of ${Object.keys(table).join(', ')})`);
|
|
202
|
+
}
|
|
203
|
+
return constant;
|
|
204
|
+
};
|
|
205
|
+
/**
|
|
206
|
+
* A pixel dimension as C.
|
|
207
|
+
*
|
|
208
|
+
* @param value The style value; must be a finite number.
|
|
209
|
+
*
|
|
210
|
+
* @returns It rounded to a whole pixel.
|
|
211
|
+
*/
|
|
212
|
+
const dimension = (value) => {
|
|
213
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) {
|
|
214
|
+
throw new Error(`expected a numeric dimension, got ${JSON.stringify(value)}`);
|
|
215
|
+
}
|
|
216
|
+
return String(Math.round(value));
|
|
117
217
|
};
|
|
118
|
-
|
|
119
218
|
/**
|
|
120
219
|
* A dimension that may be a percentage. `'50%'` → the engine's float `*_pct` field (% of the parent);
|
|
121
220
|
* a number → the pixel field. Used for width / height / flexBasis and the four insets, all of which the
|
|
122
221
|
* engine resolves at layout.
|
|
123
222
|
*
|
|
124
|
-
* @param
|
|
125
|
-
* @param
|
|
223
|
+
* @param pxField ERProps pixel field (e.g. 'width').
|
|
224
|
+
* @param pctField ERProps percentage field (e.g. 'width_pct').
|
|
225
|
+
*
|
|
226
|
+
* @returns The lowering for that key.
|
|
126
227
|
*/
|
|
127
|
-
const pctOrPx = (pxField, pctField) =>
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
228
|
+
const pctOrPx = (pxField, pctField) => value => {
|
|
229
|
+
if (typeof value === 'string' && value.trim().endsWith('%')) {
|
|
230
|
+
const percent = parseFloat(value);
|
|
231
|
+
if (!Number.isFinite(percent)) {
|
|
232
|
+
throw new Error(`expected a percentage like '50%', got ${JSON.stringify(value)}`);
|
|
233
|
+
}
|
|
234
|
+
// The engine reads a 0.0 percentage as "not set", so lower 0% to the pixel field, where 0 means the same.
|
|
235
|
+
if (percent === 0)
|
|
236
|
+
return [{ field: pxField, expr: '0' }];
|
|
237
|
+
// A valid C float literal needs a decimal point — `50f` is a syntax error, `50.0f` is not.
|
|
238
|
+
const floatLiteral = Number.isInteger(percent)
|
|
239
|
+
? `${percent}.0f`
|
|
240
|
+
: `${percent}f`;
|
|
241
|
+
return [{ field: pctField, expr: floatLiteral }];
|
|
242
|
+
}
|
|
243
|
+
return [{ field: pxField, expr: dimension(value) }];
|
|
142
244
|
};
|
|
143
|
-
|
|
144
245
|
/**
|
|
145
246
|
* Per-style-key lowering. Each entry maps a style value to one or more { field, expr } ERProps writes
|
|
146
247
|
* (field = ERProps C member, expr = C source for the value). `flex` expands to several fields.
|
|
147
248
|
*/
|
|
148
249
|
const KEYS = {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
{
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
},
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
{field: '
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
250
|
+
// Layout
|
|
251
|
+
width: pctOrPx('width', 'width_pct'),
|
|
252
|
+
height: pctOrPx('height', 'height_pct'),
|
|
253
|
+
flexBasis: pctOrPx('flex_basis', 'flex_basis_pct'),
|
|
254
|
+
minWidth: width => [{ field: 'min_width', expr: dimension(width) }],
|
|
255
|
+
maxWidth: width => [{ field: 'max_width', expr: dimension(width) }],
|
|
256
|
+
minHeight: height => [{ field: 'min_height', expr: dimension(height) }],
|
|
257
|
+
maxHeight: height => [{ field: 'max_height', expr: dimension(height) }],
|
|
258
|
+
padding: padding => [{ field: 'padding', expr: dimension(padding) }],
|
|
259
|
+
paddingHorizontal: padding => [
|
|
260
|
+
{ field: 'padding_horizontal', expr: dimension(padding) },
|
|
261
|
+
],
|
|
262
|
+
paddingVertical: padding => [
|
|
263
|
+
{ field: 'padding_vertical', expr: dimension(padding) },
|
|
264
|
+
],
|
|
265
|
+
paddingLeft: padding => [{ field: 'padding_left', expr: dimension(padding) }],
|
|
266
|
+
paddingTop: padding => [{ field: 'padding_top', expr: dimension(padding) }],
|
|
267
|
+
paddingRight: padding => [{ field: 'padding_right', expr: dimension(padding) }],
|
|
268
|
+
paddingBottom: padding => [
|
|
269
|
+
{ field: 'padding_bottom', expr: dimension(padding) },
|
|
270
|
+
],
|
|
271
|
+
margin: margin => [{ field: 'margin', expr: dimension(margin) }],
|
|
272
|
+
marginHorizontal: margin => [
|
|
273
|
+
{ field: 'margin_horizontal', expr: dimension(margin) },
|
|
274
|
+
],
|
|
275
|
+
marginVertical: margin => [
|
|
276
|
+
{ field: 'margin_vertical', expr: dimension(margin) },
|
|
277
|
+
],
|
|
278
|
+
marginLeft: margin => [{ field: 'margin_left', expr: dimension(margin) }],
|
|
279
|
+
marginTop: margin => [{ field: 'margin_top', expr: dimension(margin) }],
|
|
280
|
+
marginRight: margin => [{ field: 'margin_right', expr: dimension(margin) }],
|
|
281
|
+
marginBottom: margin => [{ field: 'margin_bottom', expr: dimension(margin) }],
|
|
282
|
+
gap: gap => [{ field: 'gap', expr: dimension(gap) }],
|
|
283
|
+
rowGap: gap => [{ field: 'row_gap', expr: dimension(gap) }],
|
|
284
|
+
columnGap: gap => [{ field: 'column_gap', expr: dimension(gap) }],
|
|
285
|
+
flexGrow: grow => [{ field: 'flex_grow', expr: dimension(grow) }],
|
|
286
|
+
flexShrink: shrink => [{ field: 'flex_shrink', expr: dimension(shrink) }],
|
|
287
|
+
flexDirection: direction => [
|
|
288
|
+
{
|
|
289
|
+
field: 'flex_direction',
|
|
290
|
+
expr: enumKey(FLEX_DIRECTION, 'flexDirection')(direction),
|
|
291
|
+
},
|
|
292
|
+
],
|
|
293
|
+
alignItems: alignment => [
|
|
294
|
+
{ field: 'align_items', expr: enumKey(ALIGN, 'alignItems')(alignment) },
|
|
295
|
+
],
|
|
296
|
+
alignSelf: alignment => [
|
|
297
|
+
{ field: 'align_self', expr: enumKey(ALIGN, 'alignSelf')(alignment) },
|
|
298
|
+
],
|
|
299
|
+
justifyContent: justification => [
|
|
300
|
+
{
|
|
301
|
+
field: 'justify_content',
|
|
302
|
+
expr: enumKey(JUSTIFY, 'justifyContent')(justification),
|
|
303
|
+
},
|
|
304
|
+
],
|
|
305
|
+
// Positioning: `position: 'absolute'` takes a node out of flow; left/top/right/bottom are its anchors.
|
|
306
|
+
position: position => [
|
|
307
|
+
{ field: 'position', expr: enumKey(POSITION, 'position')(position) },
|
|
308
|
+
],
|
|
309
|
+
display: display => [
|
|
310
|
+
{ field: 'display', expr: enumKey(DISPLAY, 'display')(display) },
|
|
311
|
+
],
|
|
312
|
+
left: pctOrPx('left', 'left_pct'),
|
|
313
|
+
top: pctOrPx('top', 'top_pct'),
|
|
314
|
+
right: pctOrPx('right', 'right_pct'),
|
|
315
|
+
bottom: pctOrPx('bottom', 'bottom_pct'),
|
|
316
|
+
// `flex: n` → grow=n, shrink=1, basis=0 (RN semantics; matches native_ui_bridge apply_flex).
|
|
317
|
+
flex: flexValue => {
|
|
318
|
+
const factor = Number(flexValue);
|
|
319
|
+
if (factor > 0) {
|
|
320
|
+
return [
|
|
321
|
+
{ field: 'flex_grow', expr: dimension(factor) },
|
|
322
|
+
{ field: 'flex_shrink', expr: '1' },
|
|
323
|
+
{ field: 'flex_basis', expr: '0' },
|
|
324
|
+
];
|
|
325
|
+
}
|
|
326
|
+
if (factor === 0) {
|
|
327
|
+
return [
|
|
328
|
+
{ field: 'flex_grow', expr: '0' },
|
|
329
|
+
{ field: 'flex_shrink', expr: '0' },
|
|
330
|
+
];
|
|
331
|
+
}
|
|
332
|
+
return [
|
|
333
|
+
{ field: 'flex_grow', expr: '0' },
|
|
334
|
+
{ field: 'flex_shrink', expr: '1' },
|
|
335
|
+
];
|
|
234
336
|
},
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
337
|
+
// View visual
|
|
338
|
+
backgroundColor: color => [
|
|
339
|
+
{ field: 'background_color', expr: colorLiteral(color) },
|
|
340
|
+
],
|
|
341
|
+
borderRadius: radius => [{ field: 'border_radius', expr: dimension(radius) }],
|
|
342
|
+
// Per-corner radii; the engine reads 0 in one of these as "use borderRadius" (same as Flow A).
|
|
343
|
+
borderTopLeftRadius: radius => [
|
|
344
|
+
{ field: 'border_top_left_radius', expr: dimension(radius) },
|
|
345
|
+
],
|
|
346
|
+
borderTopRightRadius: radius => [
|
|
347
|
+
{ field: 'border_top_right_radius', expr: dimension(radius) },
|
|
348
|
+
],
|
|
349
|
+
borderBottomRightRadius: radius => [
|
|
350
|
+
{ field: 'border_bottom_right_radius', expr: dimension(radius) },
|
|
351
|
+
],
|
|
352
|
+
borderBottomLeftRadius: radius => [
|
|
353
|
+
{ field: 'border_bottom_left_radius', expr: dimension(radius) },
|
|
354
|
+
],
|
|
355
|
+
borderWidth: width => [{ field: 'border_width', expr: dimension(width) }],
|
|
356
|
+
borderColor: color => [{ field: 'border_color', expr: colorLiteral(color) }],
|
|
357
|
+
opacity: opacity => [
|
|
358
|
+
{
|
|
359
|
+
field: 'opacity',
|
|
360
|
+
expr: String(Math.round(Math.max(0, Math.min(1, Number(opacity))) * 255)),
|
|
361
|
+
},
|
|
362
|
+
],
|
|
363
|
+
zIndex: zIndex => [{ field: 'z_index', expr: dimension(zIndex) }],
|
|
364
|
+
// Text
|
|
365
|
+
color: color => [{ field: 'color', expr: colorLiteral(color) }],
|
|
366
|
+
fontSize: size => [{ field: 'font_size', expr: dimension(size) }],
|
|
367
|
+
fontWeight: weight => [
|
|
368
|
+
{
|
|
369
|
+
field: 'font_weight',
|
|
370
|
+
expr: weight === 'bold' || Number(weight) >= 600 ? '1' : '0',
|
|
371
|
+
},
|
|
372
|
+
],
|
|
373
|
+
lineHeight: lineHeight => [
|
|
374
|
+
{ field: 'line_height', expr: dimension(lineHeight) },
|
|
375
|
+
],
|
|
376
|
+
letterSpacing: spacing => [
|
|
377
|
+
{ field: 'letter_spacing', expr: dimension(spacing) },
|
|
378
|
+
],
|
|
246
379
|
};
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
380
|
+
/**
|
|
381
|
+
* Whether the static lowering knows `key` at all (vs. knowing it but rejecting its value).
|
|
382
|
+
*
|
|
383
|
+
* @param key A style key.
|
|
384
|
+
*
|
|
385
|
+
* @returns Whether lowerStyle has a lowering for it.
|
|
386
|
+
*/
|
|
387
|
+
export const isStyleKey = (key) => Object.hasOwn(KEYS, key);
|
|
251
388
|
/** Every style key the AOT lowers statically — listed in the "unsupported style key" diagnostic. */
|
|
252
389
|
export const STYLE_KEYS = Object.keys(KEYS);
|
|
253
|
-
|
|
254
390
|
/**
|
|
255
|
-
* Lowers one flattened style object to a list of ERProps field assignments.
|
|
391
|
+
* Lowers one flattened style object to a list of ERProps field assignments. This is the build-time mirror
|
|
392
|
+
* of what native_ui_bridge.c does at runtime in Flow A: the generated C (e.g.
|
|
393
|
+
* `p.background_color = 0xFF0F172Au;`) builds the same prop bags as Flow A, from the same defaults
|
|
394
|
+
* (er_props_default), so both render the same pixels. An unsupported key throws, so the compiler fails
|
|
395
|
+
* loudly instead of silently dropping a style.
|
|
256
396
|
*
|
|
257
|
-
* @param
|
|
258
|
-
*
|
|
397
|
+
* @param style Flattened style (plain key→value; values already statically resolved).
|
|
398
|
+
*
|
|
399
|
+
* @returns ERProps writes in declaration order.
|
|
259
400
|
*/
|
|
260
401
|
export function lowerStyle(style) {
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
402
|
+
const assigns = [];
|
|
403
|
+
for (const [key, value] of Object.entries(style)) {
|
|
404
|
+
if (value === undefined || value === null)
|
|
405
|
+
continue;
|
|
406
|
+
const lowering = Object.hasOwn(KEYS, key) ? KEYS[key] : undefined;
|
|
407
|
+
if (!lowering) {
|
|
408
|
+
throw new Error(`AOT: unsupported style key "${key}" (not yet lowered to ERProps)`);
|
|
409
|
+
}
|
|
410
|
+
assigns.push(...lowering(value));
|
|
411
|
+
}
|
|
412
|
+
return assigns;
|
|
272
413
|
}
|
|
273
|
-
|
|
274
|
-
/** Maps a JSX component tag to its ERNodeType enum (the subset the slice supports). */
|
|
275
|
-
export const NODE_TYPES = {
|
|
276
|
-
View: 'ER_NODE_VIEW',
|
|
277
|
-
Text: 'ER_NODE_TEXT',
|
|
278
|
-
Pressable: 'ER_NODE_PRESSABLE',
|
|
279
|
-
TouchableOpacity: 'ER_NODE_PRESSABLE',
|
|
280
|
-
Image: 'ER_NODE_IMAGE',
|
|
281
|
-
ScrollView: 'ER_NODE_SCROLL_VIEW',
|
|
282
|
-
};
|
|
283
|
-
|
|
284
|
-
// Style keys whose value can be DYNAMIC (driven by state). Each maps to its ERProps field + a kind the
|
|
285
|
-
// AOT compiler uses to lower a runtime expression: 'num' → assign a C numeric expression directly;
|
|
286
|
-
// 'opacity' → scale a 0–1 expression to 0–255; 'color' → a (ternary of) color literal(s). Keys absent
|
|
287
|
-
// here (enums like flexDirection, the `flex` shorthand) can only be static for now.
|
|
288
|
-
const NUM_FIELDS = {
|
|
289
|
-
width: 'width',
|
|
290
|
-
height: 'height',
|
|
291
|
-
minWidth: 'min_width',
|
|
292
|
-
maxWidth: 'max_width',
|
|
293
|
-
minHeight: 'min_height',
|
|
294
|
-
maxHeight: 'max_height',
|
|
295
|
-
padding: 'padding',
|
|
296
|
-
paddingHorizontal: 'padding_horizontal',
|
|
297
|
-
paddingVertical: 'padding_vertical',
|
|
298
|
-
paddingLeft: 'padding_left',
|
|
299
|
-
paddingTop: 'padding_top',
|
|
300
|
-
paddingRight: 'padding_right',
|
|
301
|
-
paddingBottom: 'padding_bottom',
|
|
302
|
-
margin: 'margin',
|
|
303
|
-
marginHorizontal: 'margin_horizontal',
|
|
304
|
-
marginVertical: 'margin_vertical',
|
|
305
|
-
marginLeft: 'margin_left',
|
|
306
|
-
marginTop: 'margin_top',
|
|
307
|
-
marginRight: 'margin_right',
|
|
308
|
-
marginBottom: 'margin_bottom',
|
|
309
|
-
gap: 'gap',
|
|
310
|
-
rowGap: 'row_gap',
|
|
311
|
-
columnGap: 'column_gap',
|
|
312
|
-
flexGrow: 'flex_grow',
|
|
313
|
-
flexShrink: 'flex_shrink',
|
|
314
|
-
borderRadius: 'border_radius',
|
|
315
|
-
borderTopLeftRadius: 'border_top_left_radius',
|
|
316
|
-
borderTopRightRadius: 'border_top_right_radius',
|
|
317
|
-
borderBottomRightRadius: 'border_bottom_right_radius',
|
|
318
|
-
borderBottomLeftRadius: 'border_bottom_left_radius',
|
|
319
|
-
borderWidth: 'border_width',
|
|
320
|
-
zIndex: 'z_index',
|
|
321
|
-
fontSize: 'font_size',
|
|
322
|
-
lineHeight: 'line_height',
|
|
323
|
-
letterSpacing: 'letter_spacing',
|
|
324
|
-
};
|
|
325
|
-
|
|
326
|
-
// Enum style keys that can be state-driven: the value (a string literal or a ternary of them) lowers to the
|
|
327
|
-
// matching ER_* enum constant via its table. Changing one in app_update re-runs layout (props_hash covers it).
|
|
328
|
-
const ENUM_FIELDS = {
|
|
329
|
-
flexDirection: {field: 'flex_direction', table: FLEX_DIRECTION},
|
|
330
|
-
alignItems: {field: 'align_items', table: ALIGN},
|
|
331
|
-
alignSelf: {field: 'align_self', table: ALIGN},
|
|
332
|
-
justifyContent: {field: 'justify_content', table: JUSTIFY},
|
|
333
|
-
position: {field: 'position', table: POSITION},
|
|
334
|
-
display: {field: 'display', table: DISPLAY},
|
|
335
|
-
};
|
|
336
|
-
|
|
337
|
-
export const DYN_FIELDS = {
|
|
338
|
-
...Object.fromEntries(
|
|
339
|
-
Object.entries(NUM_FIELDS).map(([k, f]) => [k, {field: f, kind: 'num'}]),
|
|
340
|
-
),
|
|
341
|
-
...Object.fromEntries(
|
|
342
|
-
Object.entries(ENUM_FIELDS).map(([k, m]) => [
|
|
343
|
-
k,
|
|
344
|
-
{field: m.field, kind: 'enum', table: m.table},
|
|
345
|
-
]),
|
|
346
|
-
),
|
|
347
|
-
backgroundColor: {field: 'background_color', kind: 'color'},
|
|
348
|
-
color: {field: 'color', kind: 'color'},
|
|
349
|
-
borderColor: {field: 'border_color', kind: 'color'},
|
|
350
|
-
opacity: {field: 'opacity', kind: 'opacity'},
|
|
351
|
-
};
|