embedded-react 0.20.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 (140) hide show
  1. package/NOTICE +5 -0
  2. package/README.md +3 -2
  3. package/aot/app-file-loader.mjs +116 -0
  4. package/aot/compile.mjs +17 -47
  5. package/aot/compiler/alerts.mjs +604 -0
  6. package/aot/compiler/animations.mjs +299 -57
  7. package/aot/compiler/boot.mjs +27 -2
  8. package/aot/compiler/check.mjs +62 -10
  9. package/aot/compiler/collect.mjs +517 -137
  10. package/aot/compiler/contexts.mjs +391 -0
  11. package/aot/compiler/control-flow.mjs +276 -117
  12. package/aot/compiler/custom-hooks.mjs +686 -0
  13. package/aot/compiler/elements/button.mjs +182 -0
  14. package/aot/compiler/elements/image-background.mjs +76 -0
  15. package/aot/compiler/elements/image.mjs +2 -1
  16. package/aot/compiler/elements/props.mjs +47 -8
  17. package/aot/compiler/elements/rewrite.mjs +118 -0
  18. package/aot/compiler/elements/section-list.mjs +370 -0
  19. package/aot/compiler/expressions.mjs +1378 -97
  20. package/aot/compiler/handlers.mjs +1807 -354
  21. package/aot/compiler/helper-values.mjs +312 -0
  22. package/aot/compiler/ir/nodes.mjs +40 -1
  23. package/aot/compiler/ir/reads.mjs +206 -5
  24. package/aot/compiler/ir/text.mjs +5 -1
  25. package/aot/compiler/ir/types.mjs +12 -0
  26. package/aot/compiler/kept-states.mjs +177 -0
  27. package/aot/compiler/keyboard-dismiss.mjs +99 -0
  28. package/aot/compiler/keyboard.mjs +1 -1
  29. package/aot/compiler/list-items.mjs +226 -0
  30. package/aot/compiler/list-order.mjs +586 -0
  31. package/aot/compiler/list-split.mjs +259 -0
  32. package/aot/compiler/loops.mjs +582 -0
  33. package/aot/compiler/lower.mjs +203 -53
  34. package/aot/compiler/native-files.mjs +439 -0
  35. package/aot/compiler/native-header.mjs +335 -0
  36. package/aot/compiler/native-modules.mjs +1173 -0
  37. package/aot/compiler/nodes.mjs +45 -31
  38. package/aot/compiler/options.mjs +3 -2
  39. package/aot/compiler/out.mjs +18 -1
  40. package/aot/compiler/package/animations.mjs +133 -23
  41. package/aot/compiler/package/assemble.mjs +2 -4
  42. package/aot/compiler/package/bytecode.mjs +37 -8
  43. package/aot/compiler/package/checks.mjs +4 -0
  44. package/aot/compiler/package/codegen.mjs +906 -33
  45. package/aot/compiler/package/enums.mjs +10 -0
  46. package/aot/compiler/package/format.mjs +16 -3
  47. package/aot/compiler/package/lower.mjs +707 -176
  48. package/aot/compiler/package/string-caps.mjs +515 -0
  49. package/aot/compiler/package/tables.mjs +24 -0
  50. package/aot/compiler/package/writer.mjs +22 -0
  51. package/aot/compiler/parse.mjs +3 -1
  52. package/aot/compiler/record-shapes.mjs +246 -0
  53. package/aot/compiler/records.mjs +654 -0
  54. package/aot/compiler/reducers.mjs +272 -0
  55. package/aot/compiler/scroll-to.mjs +112 -0
  56. package/aot/compiler/static-eval.mjs +315 -7
  57. package/aot/compiler/style-text.mjs +20 -5
  58. package/aot/compiler/svg.mjs +13 -7
  59. package/aot/compiler/switch-cases.mjs +198 -0
  60. package/aot/compiler/tagged-records.mjs +450 -0
  61. package/aot/compiler/typing.mjs +307 -0
  62. package/aot/compiler/updaters.mjs +321 -0
  63. package/aot/compiler/window-dimensions.mjs +94 -0
  64. package/aot/compiler/worked-values.mjs +319 -0
  65. package/aot/demo-file.mjs +30 -0
  66. package/aot/style-map.mjs +94 -3
  67. package/assets/bake-font.mjs +187 -244
  68. package/assets/bake-image.mjs +47 -71
  69. package/assets/bake-svg.mjs +465 -533
  70. package/assets/build-builtin-font.mjs +14 -27
  71. package/assets/emit-c.mjs +124 -204
  72. package/assets/emit-container.mjs +129 -197
  73. package/assets/emit-pack.mjs +92 -136
  74. package/assets/font-config.mjs +19 -27
  75. package/assets/font-sizes.mjs +432 -507
  76. package/assets/glyph-coverage.mjs +170 -214
  77. package/assets/index.mjs +52 -97
  78. package/assets/rasterize.mjs +173 -194
  79. package/assets/svg-loader.mjs +42 -48
  80. package/build.mjs +85 -94
  81. package/cli.mjs +490 -586
  82. package/compile-bin.mjs +24 -36
  83. package/flash.mjs +297 -401
  84. package/hotreload/dev-device.mjs +229 -270
  85. package/hotreload/pack-app.mjs +118 -159
  86. package/hotreload/pack-split.mjs +146 -213
  87. package/hotreload/package-transfer.mjs +128 -131
  88. package/hotreload/protocol.mjs +118 -118
  89. package/hotreload/uploader.mjs +132 -159
  90. package/hotreload/watch-tree.mjs +56 -65
  91. package/pack-container.mjs +94 -116
  92. package/package.json +19 -16
  93. package/persist-transform.mjs +89 -105
  94. package/qjsc-wasm.mjs +47 -72
  95. package/sim/embedded-react.wasm +0 -0
  96. package/sim/index.html +1 -1
  97. package/sim-server.mjs +260 -354
  98. package/src/embedded-react/Alert.mjs +112 -0
  99. package/src/embedded-react/Animated.mjs +412 -0
  100. package/src/embedded-react/{AppRegistry.js → AppRegistry.mjs} +26 -25
  101. package/src/embedded-react/{Button.js → Button.mjs} +29 -53
  102. package/src/embedded-react/Dimensions.mjs +32 -0
  103. package/src/embedded-react/Easing.mjs +34 -0
  104. package/src/embedded-react/{FlatList.js → FlatList.mjs} +25 -42
  105. package/src/embedded-react/{ImageBackground.js → ImageBackground.mjs} +11 -22
  106. package/src/embedded-react/Keyboard.mjs +82 -0
  107. package/src/embedded-react/{LayoutAnimation.js → LayoutAnimation.mjs} +17 -28
  108. package/src/embedded-react/PanResponder.mjs +256 -0
  109. package/src/embedded-react/{Platform.js → Platform.mjs} +8 -9
  110. package/src/embedded-react/{Pressable.js → Pressable.mjs} +12 -25
  111. package/src/embedded-react/SectionList.mjs +103 -0
  112. package/src/embedded-react/{StyleSheet.js → StyleSheet.mjs} +16 -20
  113. package/src/embedded-react/{TouchableOpacity.js → TouchableOpacity.mjs} +47 -83
  114. package/src/embedded-react/background-image.mjs +321 -0
  115. package/src/embedded-react/{components.js → components.mjs} +1 -3
  116. package/src/embedded-react/{imperative.js → imperative.mjs} +27 -38
  117. package/src/embedded-react/index.d.ts +238 -5
  118. package/src/embedded-react/index.js +26 -17
  119. package/src/embedded-react/layout-anim-config.mjs +85 -0
  120. package/src/embedded-react/{list-child.js → list-child.mjs} +11 -20
  121. package/src/embedded-react/{named-colors.js → named-colors.mjs} +2 -6
  122. package/src/embedded-react/nativeModule.mjs +371 -0
  123. package/src/embedded-react/split-style.mjs +57 -0
  124. package/src/embedded-react/svg-ops.mjs +1075 -0
  125. package/src/embedded-react/{useHostValue.js → useHostValue.mjs} +2 -4
  126. package/src/embedded-react/{usePersistentState.js → usePersistentState.mjs} +34 -38
  127. package/src/embedded-react/{warn-props.js → warn-props.mjs} +32 -39
  128. package/src/host-config.mjs +512 -0
  129. package/src/{native-ui.js → native-ui.mjs} +1 -8
  130. package/src/props.mjs +341 -0
  131. package/src/{renderer.js → renderer.mjs} +30 -44
  132. package/src/embedded-react/Animated.js +0 -440
  133. package/src/embedded-react/Easing.js +0 -39
  134. package/src/embedded-react/PanResponder.js +0 -330
  135. package/src/embedded-react/SectionList.js +0 -148
  136. package/src/embedded-react/layout-anim-config.js +0 -101
  137. package/src/embedded-react/split-style.js +0 -58
  138. package/src/embedded-react/svg-ops.js +0 -1295
  139. package/src/host-config.js +0 -548
  140. package/src/props.js +0 -306
@@ -0,0 +1,370 @@
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
+ - Imports
18
+ ---------------------------------------------------------------------------------------------------------------------*/
19
+ import { aotError } from '../diagnostics.mjs';
20
+ import { componentReturnJSX, isFn } from '../collect.mjs';
21
+ import { emitChildren, rowEnv } from '../control-flow.mjs';
22
+ import { foldRuntime } from '../expressions.mjs';
23
+ import { emitNode } from '../nodes.mjs';
24
+ import { evalStatic, setBinding } from '../static-eval.mjs';
25
+ import { attrExpr } from '../style-text.mjs';
26
+ import { patternNames } from '../updaters.mjs';
27
+ import { at } from './props.mjs';
28
+ import { elementOf, expressionChild, refuseSpreads } from './rewrite.mjs';
29
+ /*----------------------------------------------------------------------------------------------------------------------
30
+ - Constants
31
+ ---------------------------------------------------------------------------------------------------------------------*/
32
+ /** A constant section's rows; `@` keeps it from meeting an app's name. */
33
+ const SECTION_DATA = 'data@section';
34
+ const SECTIONS_HINT = 'write sections={[{title: …, data: …}, …]} in place, where a data may be list state, or use a constant ' +
35
+ 'array. The number of sections is fixed at build time; a list of rows is not.';
36
+ /*----------------------------------------------------------------------------------------------------------------------
37
+ - Implementation
38
+ ---------------------------------------------------------------------------------------------------------------------*/
39
+ /**
40
+ * <SectionList sections renderItem renderSectionHeader renderSectionFooter style /> → a <ScrollView> holding each
41
+ * section's header, rows and footer as flat siblings, as SectionList.mts renders it. The sections are unrolled at
42
+ * build time; each section's rows are a `.map` over its data, so a data that is list state is a pool of rows.
43
+ * keyExtractor is ignored (no reconciler).
44
+ *
45
+ * @param element The <SectionList>, its props already checked.
46
+ * @param scope Compile-time constants in reach.
47
+ * @param output Everything emitted so far.
48
+ * @param environment The expression environment.
49
+ * @param state The component's state table.
50
+ * @param options How the element is placed.
51
+ *
52
+ * @returns What the <ScrollView> lowers to.
53
+ */
54
+ export function emitSectionList(element, scope, output, environment, state, options) {
55
+ refuseSpreads(element, 'SectionList');
56
+ const attributes = new Map();
57
+ for (const attribute of element.openingElement
58
+ .attributes) {
59
+ attributes.set(attribute.name.name, attribute);
60
+ }
61
+ const sectionsAttribute = attributes.get('sections');
62
+ if (!sectionsAttribute) {
63
+ throw aotError('AOT: <SectionList> needs a sections prop', "sections={[{title: 'A', data: items}]}");
64
+ }
65
+ const renderItem = callbackOf(attributes.get('renderItem'), 'renderItem');
66
+ const header = callbackOf(attributes.get('renderSectionHeader'), 'renderSectionHeader');
67
+ const footer = callbackOf(attributes.get('renderSectionFooter'), 'renderSectionFooter');
68
+ const sections = sectionsOf(sectionsAttribute, scope, environment);
69
+ // The ScrollView first, so its node comes before the rows it holds.
70
+ const styleAttribute = attributes.get('style');
71
+ const scrollView = emitNode(elementOf('ScrollView', styleAttribute ? [styleAttribute] : [], [], element), scope, output, environment, state, options);
72
+ // Each callback sees its own name for the section.
73
+ const emitBound = (callback, allowed, section, child) => {
74
+ const names = destructured(callback, allowed);
75
+ const sectionName = names.get('section');
76
+ // A data that is list state has no value to bind, so `section.data` reads it where it is written.
77
+ const bound = sectionName && section.data && !Object.hasOwn(section.value, 'data')
78
+ ? {
79
+ ...callback,
80
+ body: withSectionData(callback.body, sectionName, section.data),
81
+ }
82
+ : callback;
83
+ const sectionScope = { ...scope };
84
+ if (sectionName) {
85
+ setBinding(sectionScope, sectionName, section.value);
86
+ }
87
+ if (section.constantData) {
88
+ setBinding(sectionScope, SECTION_DATA, section.constantData);
89
+ }
90
+ scrollView.node.children.push(...emitChildren([child(bound, names)], sectionScope, output, rowEnv(environment, [sectionName], sectionScope), state));
91
+ };
92
+ for (const section of sections) {
93
+ if (header) {
94
+ emitBound(header, ['section'], section, bound => bodyChild(bound));
95
+ }
96
+ const rowRenderer = section.renderItem ?? renderItem;
97
+ const rows = section.data ??
98
+ (section.constantData
99
+ ? { type: 'Identifier', name: SECTION_DATA }
100
+ : null);
101
+ if (rows) {
102
+ if (!rowRenderer) {
103
+ throw at(aotError('AOT: a <SectionList> section with data needs a renderItem', 'renderItem={({item}) => <Row item={item} />} on the list, or on the section.'), section.node);
104
+ }
105
+ emitBound(rowRenderer, ['item', 'index', 'section'], section, (bound, names) => rowsChild(bound, names, rows));
106
+ }
107
+ if (footer) {
108
+ emitBound(footer, ['section'], section, bound => bodyChild(bound));
109
+ }
110
+ }
111
+ return scrollView;
112
+ }
113
+ /**
114
+ * `node` with each `<sectionName>.data` read replaced by `data`, outside functions that bind the name again.
115
+ * The callback is shared by every section, so what changes is copied.
116
+ *
117
+ * @param node A node of the callback's body.
118
+ * @param sectionName The callback's name for the section.
119
+ * @param data The section's data, as written.
120
+ *
121
+ * @returns The node, or a copy with the reads replaced.
122
+ */
123
+ function withSectionData(node, sectionName, data) {
124
+ if (Array.isArray(node)) {
125
+ const replaced = node.map(entry => withSectionData(entry, sectionName, data));
126
+ return replaced.some((entry, index) => entry !== node[index])
127
+ ? replaced
128
+ : node;
129
+ }
130
+ if (!node || typeof node.type !== 'string')
131
+ return node;
132
+ const astNode = node;
133
+ if ((astNode.type === 'MemberExpression' ||
134
+ astNode.type === 'OptionalMemberExpression') &&
135
+ !astNode.computed &&
136
+ astNode.object.type === 'Identifier' &&
137
+ astNode.object.name === sectionName &&
138
+ astNode.property.type === 'Identifier' &&
139
+ astNode.property.name === 'data') {
140
+ return data;
141
+ }
142
+ if (isFn(astNode) &&
143
+ astNode.params.some(parameter => patternNames(parameter).includes(sectionName))) {
144
+ return node;
145
+ }
146
+ let copy = null;
147
+ for (const [key, value] of Object.entries(astNode)) {
148
+ if (key === 'loc' || key.endsWith('Comments'))
149
+ continue;
150
+ const replaced = withSectionData(value, sectionName, data);
151
+ if (replaced !== value) {
152
+ copy ??= { ...astNode };
153
+ copy[key] = replaced;
154
+ }
155
+ }
156
+ return copy ?? node;
157
+ }
158
+ /**
159
+ * A render callback prop, which must be an inline function.
160
+ *
161
+ * @param attribute The prop, if given.
162
+ * @param name Its name, for the error.
163
+ *
164
+ * @returns The function, or null without the prop.
165
+ */
166
+ function callbackOf(attribute, name) {
167
+ if (!attribute)
168
+ return null;
169
+ const callback = attrExpr(attribute);
170
+ if (!isFn(callback)) {
171
+ throw at(aotError(`AOT: <SectionList ${name}> must be an inline function`, `${name}={({section}) => <Text>{section.title}</Text>}`), attribute);
172
+ }
173
+ return callback;
174
+ }
175
+ /**
176
+ * The names a render callback destructures its one argument into.
177
+ *
178
+ * @param callback The callback.
179
+ * @param allowed The fields it may take.
180
+ *
181
+ * @returns Each field taken, to its local name.
182
+ */
183
+ function destructured(callback, allowed) {
184
+ const names = new Map();
185
+ const parameter = callback.params[0];
186
+ if (!parameter)
187
+ return names;
188
+ const refusal = () => at(aotError(`AOT: a <SectionList> callback may destructure only ${allowed.join(' / ')} (to plain names)`, `write it as ({${allowed.join(', ')}}) => …`), parameter);
189
+ if (parameter.type !== 'ObjectPattern' || callback.params.length > 1) {
190
+ throw refusal();
191
+ }
192
+ for (const property of parameter.properties) {
193
+ if (property.type !== 'ObjectProperty' ||
194
+ property.computed ||
195
+ property.key.type !== 'Identifier' ||
196
+ property.value.type !== 'Identifier' ||
197
+ !allowed.includes(property.key.name)) {
198
+ throw refusal();
199
+ }
200
+ names.set(property.key.name, property.value.name);
201
+ }
202
+ return names;
203
+ }
204
+ /**
205
+ * A header or footer callback's body as a child: the element a block body returns, or an expression body as is.
206
+ *
207
+ * @param callback The callback.
208
+ *
209
+ * @returns The child.
210
+ */
211
+ function bodyChild(callback) {
212
+ if (callback.body.type === 'BlockStatement') {
213
+ return componentReturnJSX(callback);
214
+ }
215
+ return callback.body.type === 'JSXElement'
216
+ ? callback.body
217
+ : expressionChild(callback.body);
218
+ }
219
+ /**
220
+ * A section's rows as a child: `{rows.map((item, index) => body)}`, the renderItem's body as a `.map` callback.
221
+ *
222
+ * @param renderItem The renderItem.
223
+ * @param names The fields it destructures, to their local names.
224
+ * @param rows What the rows map over.
225
+ *
226
+ * @returns The child.
227
+ */
228
+ function rowsChild(renderItem, names, rows) {
229
+ const itemName = names.get('item');
230
+ if (!itemName) {
231
+ throw at(aotError('AOT: a <SectionList> renderItem must destructure item', 'renderItem={({item}) => …}'), renderItem);
232
+ }
233
+ const parameters = [{ type: 'Identifier', name: itemName }];
234
+ const indexName = names.get('index');
235
+ if (indexName) {
236
+ parameters.push({ type: 'Identifier', name: indexName });
237
+ }
238
+ return expressionChild({
239
+ type: 'CallExpression',
240
+ callee: {
241
+ type: 'MemberExpression',
242
+ object: rows,
243
+ property: { type: 'Identifier', name: 'map' },
244
+ computed: false,
245
+ },
246
+ arguments: [
247
+ {
248
+ type: 'ArrowFunctionExpression',
249
+ params: parameters,
250
+ body: renderItem.body,
251
+ async: false,
252
+ expression: renderItem.body.type !== 'BlockStatement',
253
+ },
254
+ ],
255
+ });
256
+ }
257
+ /**
258
+ * The sections, unrolled at build time: written in place, where each field folds and a data may be anything a
259
+ * `.map` takes, or a constant array.
260
+ *
261
+ * @param attribute The sections prop.
262
+ * @param scope Compile-time constants in reach.
263
+ * @param environment The expression environment.
264
+ *
265
+ * @returns The sections.
266
+ */
267
+ function sectionsOf(attribute, scope, environment) {
268
+ const expression = attrExpr(attribute);
269
+ if (expression.type === 'ArrayExpression') {
270
+ return expression.elements.flatMap(entry => {
271
+ if (!entry || entry.type === 'SpreadElement') {
272
+ throw at(aotError('AOT: <SectionList sections> cannot have holes or spreads', 'write each section out.'), entry ?? expression);
273
+ }
274
+ return entry.type === 'ObjectExpression'
275
+ ? [writtenSection(entry, scope, environment)]
276
+ : foldedSection(foldRuntime(entry, environment, scope), entry);
277
+ });
278
+ }
279
+ let folded;
280
+ try {
281
+ folded = evalStatic(expression, scope);
282
+ }
283
+ catch {
284
+ throw at(aotError('AOT: <SectionList sections> must fold to an array at build time', SECTIONS_HINT), attribute);
285
+ }
286
+ if (!Array.isArray(folded)) {
287
+ throw at(aotError('AOT: <SectionList sections> must be an array', SECTIONS_HINT), attribute);
288
+ }
289
+ return folded.flatMap(value => foldedSection(value, attribute));
290
+ }
291
+ /**
292
+ * A section written in place. Its data and renderItem are kept as written; every other field folds.
293
+ *
294
+ * @param object The section.
295
+ * @param scope Compile-time constants in reach.
296
+ * @param environment The expression environment.
297
+ *
298
+ * @returns The section.
299
+ */
300
+ function writtenSection(object, scope, environment) {
301
+ const section = {
302
+ value: {},
303
+ data: null,
304
+ constantData: null,
305
+ renderItem: null,
306
+ node: object,
307
+ };
308
+ for (const property of object.properties) {
309
+ if (property.type !== 'ObjectProperty' ||
310
+ property.computed ||
311
+ (property.key.type !== 'Identifier' &&
312
+ property.key.type !== 'StringLiteral')) {
313
+ throw at(aotError('AOT: a <SectionList> section must be plain fields', "write each field out: {title: 'A', data: items}."), property);
314
+ }
315
+ const key = property.key.type === 'Identifier'
316
+ ? property.key.name
317
+ : property.key.value;
318
+ const value = property.value;
319
+ if (key === 'keyExtractor')
320
+ continue;
321
+ if (key === 'renderItem') {
322
+ if (!isFn(value)) {
323
+ throw at(aotError("AOT: a <SectionList> section's renderItem must be an inline function", 'renderItem: ({item}) => <Row item={item} />'), property);
324
+ }
325
+ section.renderItem = value;
326
+ continue;
327
+ }
328
+ if (key === 'data') {
329
+ section.data = value;
330
+ // A data that folds can be read through `section` too; list state is only mapped over.
331
+ try {
332
+ const rows = foldRuntime(value, environment, scope);
333
+ if (Array.isArray(rows)) {
334
+ section.value.data = rows;
335
+ }
336
+ }
337
+ catch {
338
+ // mapped over only
339
+ }
340
+ continue;
341
+ }
342
+ section.value[key] = foldRuntime(value, environment, scope);
343
+ }
344
+ return section;
345
+ }
346
+ /**
347
+ * A section that folded. One that is not an object is skipped, as Flow A skips it.
348
+ *
349
+ * @param value The folded section.
350
+ * @param node Where it is written, for errors.
351
+ *
352
+ * @returns The section, or nothing.
353
+ */
354
+ function foldedSection(value, node) {
355
+ if (value === null || typeof value !== 'object')
356
+ return [];
357
+ const fields = value;
358
+ if (fields.data != null && !Array.isArray(fields.data)) {
359
+ throw at(aotError("AOT: a <SectionList> section's data must be an array", SECTIONS_HINT), node);
360
+ }
361
+ return [
362
+ {
363
+ value: fields,
364
+ data: null,
365
+ constantData: fields.data ?? null,
366
+ renderItem: null,
367
+ node,
368
+ },
369
+ ];
370
+ }