embedded-react 0.21.0 → 0.22.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.
Files changed (99) hide show
  1. package/README.md +2 -2
  2. package/aot/compile.mjs +1 -1
  3. package/aot/compiler/alerts.mjs +604 -0
  4. package/aot/compiler/check.mjs +1 -1
  5. package/aot/compiler/collect.mjs +1 -1
  6. package/aot/compiler/elements/button.mjs +3 -3
  7. package/aot/compiler/elements/image-background.mjs +1 -1
  8. package/aot/compiler/elements/props.mjs +10 -6
  9. package/aot/compiler/elements/section-list.mjs +1 -1
  10. package/aot/compiler/expressions.mjs +29 -2
  11. package/aot/compiler/handlers.mjs +73 -16
  12. package/aot/compiler/keyboard-dismiss.mjs +99 -0
  13. package/aot/compiler/keyboard.mjs +1 -1
  14. package/aot/compiler/lower.mjs +12 -0
  15. package/aot/compiler/nodes.mjs +17 -9
  16. package/aot/compiler/out.mjs +4 -0
  17. package/aot/compiler/package/codegen.mjs +36 -2
  18. package/aot/compiler/package/format.mjs +5 -3
  19. package/aot/compiler/package/lower.mjs +53 -19
  20. package/aot/compiler/scroll-to.mjs +112 -0
  21. package/aot/compiler/style-text.mjs +2 -2
  22. package/aot/compiler/svg.mjs +6 -3
  23. package/aot/compiler/updaters.mjs +1 -0
  24. package/aot/style-map.mjs +44 -1
  25. package/assets/bake-font.mjs +187 -244
  26. package/assets/bake-image.mjs +47 -71
  27. package/assets/bake-svg.mjs +465 -533
  28. package/assets/build-builtin-font.mjs +14 -27
  29. package/assets/emit-c.mjs +124 -204
  30. package/assets/emit-container.mjs +129 -197
  31. package/assets/emit-pack.mjs +92 -136
  32. package/assets/font-config.mjs +19 -27
  33. package/assets/font-sizes.mjs +432 -507
  34. package/assets/glyph-coverage.mjs +170 -214
  35. package/assets/index.mjs +52 -97
  36. package/assets/rasterize.mjs +173 -194
  37. package/assets/svg-loader.mjs +42 -48
  38. package/build.mjs +84 -101
  39. package/cli.mjs +482 -621
  40. package/compile-bin.mjs +24 -36
  41. package/flash.mjs +297 -401
  42. package/hotreload/dev-device.mjs +229 -270
  43. package/hotreload/pack-app.mjs +118 -159
  44. package/hotreload/pack-split.mjs +144 -200
  45. package/hotreload/package-transfer.mjs +128 -131
  46. package/hotreload/protocol.mjs +118 -118
  47. package/hotreload/uploader.mjs +132 -159
  48. package/hotreload/watch-tree.mjs +56 -65
  49. package/pack-container.mjs +93 -123
  50. package/package.json +19 -16
  51. package/persist-transform.mjs +89 -105
  52. package/qjsc-wasm.mjs +47 -72
  53. package/sim/embedded-react.wasm +0 -0
  54. package/sim/index.html +1 -1
  55. package/sim-server.mjs +260 -354
  56. package/src/embedded-react/Alert.mjs +112 -0
  57. package/src/embedded-react/Animated.mjs +412 -0
  58. package/src/embedded-react/{AppRegistry.js → AppRegistry.mjs} +26 -25
  59. package/src/embedded-react/{Button.js → Button.mjs} +29 -53
  60. package/src/embedded-react/{Dimensions.js → Dimensions.mjs} +12 -17
  61. package/src/embedded-react/Easing.mjs +34 -0
  62. package/src/embedded-react/{FlatList.js → FlatList.mjs} +25 -42
  63. package/src/embedded-react/{ImageBackground.js → ImageBackground.mjs} +11 -22
  64. package/src/embedded-react/Keyboard.mjs +82 -0
  65. package/src/embedded-react/{LayoutAnimation.js → LayoutAnimation.mjs} +17 -28
  66. package/src/embedded-react/PanResponder.mjs +256 -0
  67. package/src/embedded-react/{Platform.js → Platform.mjs} +8 -9
  68. package/src/embedded-react/{Pressable.js → Pressable.mjs} +12 -25
  69. package/src/embedded-react/SectionList.mjs +103 -0
  70. package/src/embedded-react/{StyleSheet.js → StyleSheet.mjs} +16 -20
  71. package/src/embedded-react/{TouchableOpacity.js → TouchableOpacity.mjs} +47 -83
  72. package/src/embedded-react/background-image.mjs +321 -0
  73. package/src/embedded-react/{components.js → components.mjs} +1 -3
  74. package/src/embedded-react/{imperative.js → imperative.mjs} +27 -38
  75. package/src/embedded-react/index.d.ts +155 -2
  76. package/src/embedded-react/index.js +26 -19
  77. package/src/embedded-react/layout-anim-config.mjs +85 -0
  78. package/src/embedded-react/{list-child.js → list-child.mjs} +11 -20
  79. package/src/embedded-react/{named-colors.js → named-colors.mjs} +2 -6
  80. package/src/embedded-react/nativeModule.mjs +371 -0
  81. package/src/embedded-react/split-style.mjs +57 -0
  82. package/src/embedded-react/svg-ops.mjs +1075 -0
  83. package/src/embedded-react/{useHostValue.js → useHostValue.mjs} +2 -4
  84. package/src/embedded-react/{usePersistentState.js → usePersistentState.mjs} +34 -38
  85. package/src/embedded-react/{warn-props.js → warn-props.mjs} +32 -39
  86. package/src/host-config.mjs +512 -0
  87. package/src/{native-ui.js → native-ui.mjs} +1 -8
  88. package/src/props.mjs +341 -0
  89. package/src/{renderer.js → renderer.mjs} +30 -44
  90. package/src/embedded-react/Animated.js +0 -440
  91. package/src/embedded-react/Easing.js +0 -39
  92. package/src/embedded-react/PanResponder.js +0 -330
  93. package/src/embedded-react/SectionList.js +0 -146
  94. package/src/embedded-react/layout-anim-config.js +0 -101
  95. package/src/embedded-react/nativeModule.js +0 -462
  96. package/src/embedded-react/split-style.js +0 -58
  97. package/src/embedded-react/svg-ops.js +0 -1295
  98. package/src/host-config.js +0 -548
  99. package/src/props.js +0 -306
package/README.md CHANGED
@@ -71,10 +71,10 @@ build one of the apps in `demos/` against the in-repo library, `npm test` runs t
71
71
 
72
72
  ```
73
73
  src/embedded-react/ the public package surface (components, StyleSheet, Animated, PanResponder, hooks)
74
- src/host-config.js the react-reconciler host config → NativeUI.* (the C bridge)
74
+ src/host-config.mts the react-reconciler host config → NativeUI.* (the C bridge)
75
75
  aot/ the Flow B JSX→package compiler
76
76
  assets/ build-time bakers: images, fonts, SVG, the ERPK container writer
77
- cli.mjs, sim-server.mjs the `embedded-react` CLI and the shared simulator dev server
77
+ cli.mts, sim-server.mts the `embedded-react` CLI and the shared simulator dev server
78
78
  sim/ the prebuilt engine .wasm the simulator runs
79
79
  test/runtime/ end-to-end tests that run inside QuickJS + the real engine
80
80
  ```
package/aot/compile.mjs CHANGED
@@ -65,7 +65,7 @@
65
65
  * compiler/check.mts the typing and check passes — what lowering recorded, run in order
66
66
  * compiler/package/*.mts the package backend: the IR as a Flow B program, its container and report
67
67
  *
68
- * The compiler is TypeScript (.mts), which Node runs by stripping the types. tools/stage-npm-package.mjs
68
+ * The compiler is TypeScript (.mts), which Node runs by stripping the types. tools/stage-npm-package.mts
69
69
  * compiles it to JavaScript for the npm package.
70
70
  */
71
71
  /*----------------------------------------------------------------------------------------------------------------------
@@ -0,0 +1,604 @@
1
+ /*
2
+ * Copyright 2026 Cory Lamming
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ /**
17
+ * @file compiler/alerts.mts
18
+ * @brief Alert.alert(title, message, buttons, options): each call becomes a dialog of its own under the root,
19
+ * built only while a hidden state names it, and the call sets that state.
20
+ */
21
+ import { traverse } from '@babel/core';
22
+ import { isFn } from './collect.mjs';
23
+ import { emitChildren } from './control-flow.mjs';
24
+ import { aotError } from './diagnostics.mjs';
25
+ import { attributeOf, elementOf, expressionChild, literalObject, } from './elements/rewrite.mjs';
26
+ import { evalStatic, rethrowFoldLimit } from './static-eval.mjs';
27
+ /*----------------------------------------------------------------------------------------------------------------------
28
+ - Constants
29
+ ---------------------------------------------------------------------------------------------------------------------*/
30
+ /** The package Alert is imported from. */
31
+ const PACKAGE = 'embedded-react';
32
+ // None of these is a name JS allows, so no binding of the app's can be mistaken for one.
33
+ /** What a call is rewritten to. */
34
+ export const ALERT_CALL = '#Alert.alert';
35
+ /** The hidden state: the number of the alert that is up, or 0. */
36
+ const ACTIVE = '#alert';
37
+ const SET_ACTIVE = '#setAlert';
38
+ // Alert.mts's styles. Flow B's <Modal> already fills the root and centres its content.
39
+ const FILL_STYLE = {
40
+ position: 'absolute',
41
+ left: 0,
42
+ top: 0,
43
+ right: 0,
44
+ bottom: 0,
45
+ alignItems: 'center',
46
+ justifyContent: 'center',
47
+ };
48
+ const SHEET_STYLE = {
49
+ backgroundColor: '#ffffff',
50
+ borderRadius: 4,
51
+ padding: 16,
52
+ gap: 8,
53
+ };
54
+ const SHEET_MAX_WIDTH = 280;
55
+ const SHEET_MARGIN = 16;
56
+ const TITLE_STYLE = { color: '#000000', fontWeight: 'bold' };
57
+ const MESSAGE_STYLE = { color: '#444444' };
58
+ const BUTTONS_STYLE = {
59
+ flexDirection: 'row',
60
+ flexWrap: 'wrap',
61
+ justifyContent: 'flex-end',
62
+ gap: 8,
63
+ marginTop: 8,
64
+ };
65
+ const BUTTON_TEXT_STYLE = { color: '#2196F3', fontWeight: '500', padding: 8 };
66
+ const DESTRUCTIVE_COLOR = '#d32f2f';
67
+ const BUTTON_STYLES = ['default', 'cancel', 'destructive'];
68
+ const HERE_HINT = 'call it from an event handler, an effect, a timer, or a helper one of those calls.';
69
+ /*----------------------------------------------------------------------------------------------------------------------
70
+ - Implementation
71
+ ---------------------------------------------------------------------------------------------------------------------*/
72
+ const identifier = (name) => ({ type: 'Identifier', name });
73
+ const numberOf = (value) => ({
74
+ type: 'NumericLiteral',
75
+ value,
76
+ });
77
+ const located = (error, node) => {
78
+ if (node?.loc && !error.aotLoc)
79
+ error.aotLoc = node.loc.start;
80
+ return error;
81
+ };
82
+ /**
83
+ * Whether a call runs as its component renders: at module level, or straight in the body of a component or a hook.
84
+ *
85
+ * @param path The call's Alert.
86
+ *
87
+ * @returns Whether it does.
88
+ */
89
+ function rendersIn(path) {
90
+ const owner = path.getFunctionParent();
91
+ if (!owner)
92
+ return true;
93
+ const { node, parent } = owner;
94
+ const name = node.type === 'FunctionDeclaration'
95
+ ? node.id?.name
96
+ : parent.type === 'VariableDeclarator' && parent.id.type === 'Identifier'
97
+ ? parent.id.name
98
+ : undefined;
99
+ return name !== undefined && /^([A-Z]|use[A-Z])/.test(name);
100
+ }
101
+ /**
102
+ * Rewrites every `Alert.alert(…)` to a call of ALERT_CALL, and refuses any other use of Alert.
103
+ *
104
+ * @param ast The parsed file, rewritten in place.
105
+ *
106
+ * @returns Whether the app raises an alert.
107
+ */
108
+ export function rewriteAlerts(ast) {
109
+ const isAlert = (path, node) => {
110
+ if (node.type !== 'Identifier')
111
+ return false;
112
+ const binding = path.scope.getBinding(node.name);
113
+ const specifier = binding?.path.node;
114
+ const declaration = binding?.path.parent;
115
+ if (specifier?.type !== 'ImportSpecifier' ||
116
+ declaration?.type !== 'ImportDeclaration' ||
117
+ declaration.source.value !== PACKAGE) {
118
+ return false;
119
+ }
120
+ const { imported } = specifier;
121
+ return ((imported.type === 'Identifier' ? imported.name : imported.value) ===
122
+ 'Alert');
123
+ };
124
+ let found = false;
125
+ traverse.cache.clear();
126
+ traverse(ast, {
127
+ Identifier(path) {
128
+ if (!path.isReferencedIdentifier() || !isAlert(path, path.node))
129
+ return;
130
+ const member = path.parent;
131
+ const call = path.parentPath?.parent;
132
+ if (member.type === 'MemberExpression' &&
133
+ member.object === path.node &&
134
+ !member.computed &&
135
+ member.property.type === 'Identifier' &&
136
+ member.property.name === 'alert' &&
137
+ call?.type === 'CallExpression' &&
138
+ call.callee === member) {
139
+ if (rendersIn(path)) {
140
+ throw located(aotError("AOT: Alert.alert can't be called while rendering", HERE_HINT), call);
141
+ }
142
+ call.callee = { ...identifier(ALERT_CALL), loc: member.loc };
143
+ found = true;
144
+ path.skip();
145
+ return;
146
+ }
147
+ throw located(aotError('AOT: Alert has only Alert.alert(title, message, buttons, options)', 'call it directly: Alert.alert(…). It cannot be passed around, and Alert.prompt is iOS only.'), path.node);
148
+ },
149
+ });
150
+ traverse.cache.clear();
151
+ return found;
152
+ }
153
+ /**
154
+ * The hidden state that names the alert that is up.
155
+ *
156
+ * @returns The state, to add to the App's.
157
+ */
158
+ export function alertState() {
159
+ return {
160
+ name: ACTIVE,
161
+ slotName: ACTIVE,
162
+ setter: SET_ACTIVE,
163
+ kind: 'scalar',
164
+ slotKind: 'int',
165
+ isBoolean: false,
166
+ initial: 0,
167
+ host: false,
168
+ };
169
+ }
170
+ /**
171
+ * What lowers where a handler does but outlives it: the environment an Alert.alert's dialog is compiled against.
172
+ *
173
+ * @param environment The environment a handler, effect or event is about to be lowered in.
174
+ *
175
+ * @returns The environment, remembering itself as it is before the body adds its locals.
176
+ */
177
+ export const withAlertBase = (environment) => environment.alertBase
178
+ ? environment
179
+ : { ...environment, alertBase: environment };
180
+ /**
181
+ * Every name an expression or a function's body reads.
182
+ *
183
+ * @param node The expression.
184
+ *
185
+ * @returns The identifiers, object keys and property names left out.
186
+ */
187
+ function namesReadBy(node) {
188
+ const found = [];
189
+ const visit = (value, key, parent) => {
190
+ if (!value || typeof value !== 'object')
191
+ return;
192
+ if (Array.isArray(value)) {
193
+ value.forEach(element => visit(element, key, parent));
194
+ return;
195
+ }
196
+ const child = value;
197
+ if (typeof child.type !== 'string')
198
+ return;
199
+ if (child.type === 'Identifier') {
200
+ const isName = (parent?.type === 'MemberExpression' &&
201
+ key === 'property' &&
202
+ !parent.computed) ||
203
+ (parent?.type === 'ObjectProperty' &&
204
+ key === 'key' &&
205
+ !parent.computed);
206
+ if (!isName)
207
+ found.push(child);
208
+ return;
209
+ }
210
+ for (const [childKey, grandchild] of Object.entries(child)) {
211
+ if (childKey !== 'loc')
212
+ visit(grandchild, childKey, child);
213
+ }
214
+ };
215
+ visit(node);
216
+ return found;
217
+ }
218
+ /**
219
+ * Refuses a dialog that reads what its handler works out: it is built after the handler returns, so it can read
220
+ * only what the component itself can.
221
+ *
222
+ * @param parts The call's title, message, labels and handlers.
223
+ * @param environment The environment the call is lowered in.
224
+ * @param base The environment the handler was lowered from.
225
+ */
226
+ function refuseHandlerReads(parts, environment, base) {
227
+ for (const name of parts.flatMap(namesReadBy)) {
228
+ if (name.name === environment.event ||
229
+ name.name === environment.gesture ||
230
+ environment.locals.get(name.name) !== base.locals.get(name.name) ||
231
+ environment.constants[name.name] !== base.constants[name.name]) {
232
+ throw located(aotError(`AOT: an alert can't read "${name.name}", which the handler works out`, 'the dialog is drawn after the handler returns, so it reads state, props and constants only. ' +
233
+ 'Keep the value in state and read that.'), name);
234
+ }
235
+ }
236
+ }
237
+ /**
238
+ * Whether an argument is left out: missing, `undefined` or `null`.
239
+ *
240
+ * @param node The argument.
241
+ *
242
+ * @returns Whether there is nothing there.
243
+ */
244
+ const isAbsent = (node) => !node ||
245
+ node.type === 'NullLiteral' ||
246
+ (node.type === 'Identifier' && node.name === 'undefined');
247
+ /**
248
+ * Whether a title or a message draws nothing, as Alert.mts's hasText has it: left out, or known to be empty.
249
+ *
250
+ * @param node The argument.
251
+ * @param scope Compile-time constants in reach.
252
+ *
253
+ * @returns Whether its Text is left out.
254
+ */
255
+ function isEmpty(node, scope) {
256
+ if (isAbsent(node))
257
+ return true;
258
+ try {
259
+ const value = evalStatic(node, scope);
260
+ return value === '' || value === null || value === undefined;
261
+ }
262
+ catch (error) {
263
+ rethrowFoldLimit(error);
264
+ return false; // worked out at run time
265
+ }
266
+ }
267
+ /**
268
+ * The fields of an object argument, by name.
269
+ *
270
+ * @param node The argument.
271
+ * @param what What it is, for the error.
272
+ *
273
+ * @returns Its fields.
274
+ */
275
+ function fieldsOf(node, what) {
276
+ const fields = new Map();
277
+ const refuse = (at) => located(aotError(`AOT: ${what} must be written out as an object: {…}`, 'list each field by name, with no spread, method or computed key.'), at);
278
+ if (node.type !== 'ObjectExpression')
279
+ throw refuse(node);
280
+ for (const property of node.properties) {
281
+ if (property.type !== 'ObjectProperty' ||
282
+ property.computed ||
283
+ (property.key.type !== 'Identifier' &&
284
+ property.key.type !== 'StringLiteral')) {
285
+ throw refuse(property);
286
+ }
287
+ fields.set(property.key.type === 'Identifier'
288
+ ? property.key.name
289
+ : property.key.value, property.value);
290
+ }
291
+ return fields;
292
+ }
293
+ /**
294
+ * A field that has to be known at build time.
295
+ *
296
+ * @param node The field's value.
297
+ * @param scope Compile-time constants in reach.
298
+ * @param what The field, for the error.
299
+ *
300
+ * @returns Its value.
301
+ */
302
+ function constantOf(node, scope, what) {
303
+ try {
304
+ return evalStatic(node, scope);
305
+ }
306
+ catch (error) {
307
+ rethrowFoldLimit(error);
308
+ throw located(aotError(`AOT: ${what} must be a compile-time constant`, 'it picks what the dialog is built from.'), node);
309
+ }
310
+ }
311
+ /**
312
+ * The buttons of a call: the ones it lists, or a single OK.
313
+ *
314
+ * @param node The buttons argument.
315
+ * @param scope Compile-time constants in reach.
316
+ *
317
+ * @returns The buttons.
318
+ */
319
+ function buttonsOf(node, scope) {
320
+ const ok = {
321
+ text: { type: 'StringLiteral', value: 'OK' },
322
+ onPress: null,
323
+ destructive: false,
324
+ };
325
+ if (isAbsent(node))
326
+ return [ok];
327
+ if (node.type !== 'ArrayExpression') {
328
+ throw located(aotError("AOT: an alert's buttons must be written out as an array: [{text: 'OK'}]", 'each button is compiled where the alert is raised, so the list cannot come from a variable.'), node);
329
+ }
330
+ const buttons = node.elements.map(element => {
331
+ if (!element || element.type === 'SpreadElement') {
332
+ throw located(aotError("AOT: an alert's buttons must each be an object: {text: 'OK'}"), element ?? node);
333
+ }
334
+ const fields = fieldsOf(element, "an alert's button");
335
+ const style = fields.get('style');
336
+ const styleValue = style
337
+ ? constantOf(style, scope, "an alert button's style")
338
+ : undefined;
339
+ if (styleValue != null && !BUTTON_STYLES.includes(styleValue)) {
340
+ throw located(aotError(`AOT: an alert button's style is 'default', 'cancel' or 'destructive'`), style);
341
+ }
342
+ return {
343
+ text: fields.get('text') ?? null,
344
+ onPress: fields.get('onPress') ?? null,
345
+ destructive: styleValue === 'destructive',
346
+ };
347
+ });
348
+ return buttons.length ? buttons : [ok];
349
+ }
350
+ /**
351
+ * A handler that closes the alert, then does what the app's own would.
352
+ *
353
+ * @param handler The button's onPress or the alert's onDismiss, if it has one.
354
+ *
355
+ * @returns The handler.
356
+ */
357
+ function closingHandler(handler) {
358
+ const close = {
359
+ type: 'ExpressionStatement',
360
+ expression: {
361
+ type: 'CallExpression',
362
+ callee: identifier(SET_ACTIVE),
363
+ arguments: [numberOf(0)],
364
+ },
365
+ };
366
+ let rest = [];
367
+ if (handler && isFn(handler)) {
368
+ if (handler.params.length) {
369
+ throw located(aotError("AOT: an alert's handler takes no arguments", 'React Native calls it with nothing: () => { … }.'), handler.params[0]);
370
+ }
371
+ rest =
372
+ handler.body.type === 'BlockStatement'
373
+ ? handler.body.body
374
+ : [{ type: 'ExpressionStatement', expression: handler.body }];
375
+ }
376
+ else if (handler && !isAbsent(handler)) {
377
+ // A helper or a useCallback by name.
378
+ rest = [
379
+ {
380
+ type: 'ExpressionStatement',
381
+ expression: {
382
+ type: 'CallExpression',
383
+ callee: handler,
384
+ arguments: [],
385
+ loc: handler.loc,
386
+ },
387
+ loc: handler.loc,
388
+ },
389
+ ];
390
+ }
391
+ return {
392
+ type: 'ArrowFunctionExpression',
393
+ params: [],
394
+ body: { type: 'BlockStatement', body: [close, ...rest], directives: [] },
395
+ async: false,
396
+ expression: false,
397
+ loc: handler?.loc,
398
+ };
399
+ }
400
+ /**
401
+ * The dialog Alert.mts draws for a call, shown while the hidden state holds the call's number.
402
+ *
403
+ * @param call The call.
404
+ * @param number The call's number.
405
+ * @param scope Compile-time constants in reach.
406
+ *
407
+ * @returns `active === number && <Modal>…</Modal>`, and every part of it the app wrote.
408
+ */
409
+ function dialogOf(call, number, scope) {
410
+ const [title, message, buttonList, optionList] = call.arguments;
411
+ for (const argument of call.arguments) {
412
+ if (argument.type === 'SpreadElement') {
413
+ throw located(aotError("AOT: Alert.alert's arguments must be listed, not spread"), argument);
414
+ }
415
+ }
416
+ const from = { loc: call.loc };
417
+ const style = (fields) => attributeOf('style', literalObject(fields));
418
+ const text = (fields, value) => elementOf('Text', [style(fields)], [expressionChild(value)], from);
419
+ const buttons = buttonsOf(buttonList, scope);
420
+ const options = isAbsent(optionList)
421
+ ? new Map()
422
+ : fieldsOf(optionList, "an alert's options");
423
+ const cancelableField = options.get('cancelable');
424
+ const cancelable = cancelableField
425
+ ? Boolean(constantOf(cancelableField, scope, "an alert's cancelable"))
426
+ : false;
427
+ const onDismiss = options.get('onDismiss') ?? null;
428
+ const sheetStyle = literalObject(SHEET_STYLE);
429
+ sheetStyle.properties.push({
430
+ type: 'ObjectProperty',
431
+ key: identifier('width'),
432
+ // Math.min(280, screen.width - 32), folded per panel.
433
+ value: {
434
+ type: 'CallExpression',
435
+ callee: {
436
+ type: 'MemberExpression',
437
+ object: identifier('Math'),
438
+ property: identifier('min'),
439
+ computed: false,
440
+ },
441
+ arguments: [
442
+ numberOf(SHEET_MAX_WIDTH),
443
+ {
444
+ type: 'BinaryExpression',
445
+ operator: '-',
446
+ left: {
447
+ type: 'MemberExpression',
448
+ object: identifier('screen'),
449
+ property: identifier('width'),
450
+ computed: false,
451
+ },
452
+ right: numberOf(SHEET_MARGIN * 2),
453
+ },
454
+ ],
455
+ },
456
+ computed: false,
457
+ shorthand: false,
458
+ });
459
+ const shown = [title, message].map(part => isEmpty(part, scope) ? null : part);
460
+ const sheetChildren = [];
461
+ if (shown[0])
462
+ sheetChildren.push(text(TITLE_STYLE, shown[0]));
463
+ if (shown[1])
464
+ sheetChildren.push(text(MESSAGE_STYLE, shown[1]));
465
+ sheetChildren.push(elementOf('View', [style(BUTTONS_STYLE)], buttons.map(button => elementOf('Pressable', [attributeOf('onPress', closingHandler(button.onPress))], [
466
+ text(button.destructive
467
+ ? { ...BUTTON_TEXT_STYLE, color: DESTRUCTIVE_COLOR }
468
+ : BUTTON_TEXT_STYLE, button.text ?? { type: 'StringLiteral', value: '' }),
469
+ ], from)), from));
470
+ // A cancelable alert's sheet is a press target of its own, so a tap on it is not a tap outside.
471
+ const sheet = elementOf(cancelable ? 'Pressable' : 'View', [
472
+ attributeOf('style', sheetStyle),
473
+ ...(cancelable
474
+ ? [
475
+ attributeOf('onPress', {
476
+ type: 'ArrowFunctionExpression',
477
+ params: [],
478
+ body: { type: 'BlockStatement', body: [], directives: [] },
479
+ async: false,
480
+ expression: false,
481
+ }),
482
+ ]
483
+ : []),
484
+ ], sheetChildren, from);
485
+ const modal = elementOf('Modal', [attributeOf('visible', { type: 'BooleanLiteral', value: true })], [
486
+ cancelable
487
+ ? elementOf('Pressable', [
488
+ style(FILL_STYLE),
489
+ attributeOf('onPress', closingHandler(onDismiss)),
490
+ ], [sheet], from)
491
+ : sheet,
492
+ ], from);
493
+ return {
494
+ dialog: {
495
+ type: 'LogicalExpression',
496
+ operator: '&&',
497
+ left: {
498
+ type: 'BinaryExpression',
499
+ operator: '===',
500
+ left: identifier(ACTIVE),
501
+ right: numberOf(number),
502
+ },
503
+ right: modal,
504
+ loc: call.loc,
505
+ },
506
+ parts: [
507
+ ...shown,
508
+ ...buttons.flatMap(button => [button.text, button.onPress]),
509
+ onDismiss,
510
+ ].filter((part) => part != null),
511
+ };
512
+ }
513
+ /**
514
+ * Lowers an Alert.alert call: its dialog is queued to be built under the root, and the call itself becomes a set
515
+ * of the hidden state.
516
+ *
517
+ * @param call The call, already rewritten to ALERT_CALL.
518
+ * @param environment The expression environment.
519
+ * @param state The component's state table.
520
+ * @param output Everything emitted so far; the dialog is added to it.
521
+ *
522
+ * @returns The setter call that raises the alert, and what to lower it against.
523
+ */
524
+ export function raiseAlert(call, environment, state, output) {
525
+ const base = environment.alertBase;
526
+ const active = output.alertState;
527
+ if (!base || !active) {
528
+ throw aotError("AOT: Alert.alert can't be called here", HERE_HINT);
529
+ }
530
+ // The App's hidden state, in reach of whichever component raises the alert.
531
+ const reaching = (table) => ({
532
+ byName: new Map([...table.byName, [ACTIVE, active]]),
533
+ bySetter: new Map([...table.bySetter, [SET_ACTIVE, active]]),
534
+ });
535
+ const number = output.alertSites.length + 1;
536
+ const { dialog, parts } = dialogOf(call, number, environment.constants);
537
+ refuseHandlerReads(parts, environment, base);
538
+ output.alertSites.push({
539
+ call,
540
+ dialog,
541
+ environment: {
542
+ ...base,
543
+ state: new Map([...base.state, [ACTIVE, active]]),
544
+ // Under the root, whatever the handler's own node is under.
545
+ inListRow: false,
546
+ mounted: null,
547
+ event: null,
548
+ layout: false,
549
+ scroll: false,
550
+ },
551
+ state: reaching(state),
552
+ });
553
+ return {
554
+ setter: {
555
+ type: 'CallExpression',
556
+ callee: identifier(SET_ACTIVE),
557
+ arguments: [numberOf(number)],
558
+ loc: call.loc,
559
+ },
560
+ environment: {
561
+ ...environment,
562
+ state: new Map([...environment.state, [ACTIVE, active]]),
563
+ },
564
+ state: reaching(state),
565
+ };
566
+ }
567
+ /**
568
+ * Builds every alert's dialog as the root's last children, so each draws over the app. A button's handler can raise
569
+ * an alert of its own, which joins the list as it is lowered.
570
+ *
571
+ * @param root The app's root element.
572
+ * @param output Everything emitted so far.
573
+ */
574
+ export function emitAlertDialogs(root, output) {
575
+ if (!output.alertSites.length)
576
+ return;
577
+ let host = root;
578
+ while (host.kind === 'component')
579
+ host = host.root;
580
+ if (host.node.type !== 'ER_NODE_VIEW') {
581
+ throw located(aotError('AOT: an app that raises an alert needs a <View> as its root', 'the dialog is added as the last child of the root, over everything else.'), output.alertSites[0].call);
582
+ }
583
+ // One chain of conditionals, so the app is counted as building the largest dialog, not all of them.
584
+ let last = null;
585
+ for (let index = 0; index < output.alertSites.length; index++) {
586
+ const site = output.alertSites[index];
587
+ try {
588
+ const [dialog] = emitChildren([expressionChild(site.dialog)], site.environment.constants, output, site.environment, site.state);
589
+ if (dialog?.kind !== 'conditional' || dialog.whenFalse) {
590
+ throw new Error('AOT: an alert did not lower to a conditional');
591
+ }
592
+ if (last) {
593
+ last.whenFalse = dialog;
594
+ }
595
+ else {
596
+ host.node.children.push(dialog);
597
+ }
598
+ last = dialog;
599
+ }
600
+ catch (error) {
601
+ throw located(error, site.call);
602
+ }
603
+ }
604
+ }
@@ -16,7 +16,7 @@
16
16
  /*----------------------------------------------------------------------------------------------------------------------
17
17
  - Imports
18
18
  ---------------------------------------------------------------------------------------------------------------------*/
19
- import { GRAD_MAX_STOPS } from '../../src/embedded-react/svg-ops.js';
19
+ import { GRAD_MAX_STOPS } from '../../src/embedded-react/svg-ops.mjs';
20
20
  import { aotError, currentLoc, locateError } from './diagnostics.mjs';
21
21
  /*----------------------------------------------------------------------------------------------------------------------
22
22
  - Constants
@@ -1094,7 +1094,7 @@ export function collectSvgImports(program) {
1094
1094
  /**
1095
1095
  * Bakes a Flow B app's <Svg source> imports → { assetName: artifact } via the SAME baker Flow A's esbuild
1096
1096
  * .svg loader uses (svgToVector). This is the I/O half (reads .svg files) kept OUT of the pure compilePackage;
1097
- * both CLI entries (aot/compile.mts and the consumer cli.mjs) call it and hand the result to options.svgArtifacts.
1097
+ * both CLI entries (aot/compile.mts and the consumer cli.mts) call it and hand the result to options.svgArtifacts.
1098
1098
  *
1099
1099
  * @param source App.jsx source text.
1100
1100
  * @param baseDirectory Directory the import paths are resolved against (the app's dir).
@@ -24,7 +24,7 @@ import { at } from './props.mjs';
24
24
  /*----------------------------------------------------------------------------------------------------------------------
25
25
  - Constants
26
26
  ---------------------------------------------------------------------------------------------------------------------*/
27
- // Button.js's styles. Flow B has no textAlign, so the Pressable centres the label instead.
27
+ // Button.mts's styles. Flow B has no textAlign, so the Pressable centres the label instead.
28
28
  const BUTTON_STYLE = {
29
29
  backgroundColor: '#2196F3',
30
30
  borderRadius: 2,
@@ -37,7 +37,7 @@ const TEXT_DISABLED_STYLE = { color: '#a1a1a1' };
37
37
  - Implementation
38
38
  ---------------------------------------------------------------------------------------------------------------------*/
39
39
  /**
40
- * <Button title onPress color disabled /> → the tree Button.js renders: a styled <Pressable> around one <Text>.
40
+ * <Button title onPress color disabled /> → the tree Button.mts renders: a styled <Pressable> around one <Text>.
41
41
  *
42
42
  * @param element The <Button>, its props already checked.
43
43
  * @param scope Compile-time constants in reach.
@@ -106,7 +106,7 @@ export function emitButton(element, scope, output, environment, state, options)
106
106
  return emitNode(elementOf('Pressable', pressableAttributes, [text], element), scope, output, environment, state, options);
107
107
  }
108
108
  /**
109
- * A title as Button.js draws it, `title == null ? '' : String(title)`: worked out now when it folds, else
109
+ * A title as Button.mts draws it, `title == null ? '' : String(title)`: worked out now when it folds, else
110
110
  * `String(title)`, so a boolean draws as "true" or "false" rather than nothing. No value worked out at run time
111
111
  * is null, so only a ternary's branches can be, and each is worked out on its own.
112
112
  *
@@ -35,7 +35,7 @@ const ABSOLUTE_FILL = {
35
35
  ---------------------------------------------------------------------------------------------------------------------*/
36
36
  /**
37
37
  * <ImageBackground style imageStyle imageRef …imageProps>children</ImageBackground> → the tree
38
- * ImageBackground.js renders: a <View> holding an <Image> that fills it, then the children. `style` and `ref`
38
+ * ImageBackground.mts renders: a <View> holding an <Image> that fills it, then the children. `style` and `ref`
39
39
  * go to the View, every other prop to the Image.
40
40
  *
41
41
  * @param element The <ImageBackground>, its props already checked.