embedded-react 0.19.0 → 0.21.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 (148) hide show
  1. package/NOTICE +5 -0
  2. package/README.md +8 -3
  3. package/aot/app-file-loader.mjs +116 -0
  4. package/aot/compile.mjs +180 -90
  5. package/aot/compiler/animations.mjs +568 -317
  6. package/aot/compiler/boot.mjs +97 -35
  7. package/aot/compiler/branch-merge.mjs +4 -3
  8. package/aot/compiler/check.mjs +70 -18
  9. package/aot/compiler/collect.mjs +787 -288
  10. package/aot/compiler/contexts.mjs +391 -0
  11. package/aot/compiler/control-flow.mjs +439 -268
  12. package/aot/compiler/custom-hooks.mjs +686 -0
  13. package/aot/compiler/diagnostics.mjs +22 -25
  14. package/aot/compiler/elements/activity-indicator.mjs +25 -24
  15. package/aot/compiler/elements/button.mjs +182 -0
  16. package/aot/compiler/elements/dial.mjs +91 -77
  17. package/aot/compiler/elements/flat-list.mjs +24 -22
  18. package/aot/compiler/elements/image-background.mjs +76 -0
  19. package/aot/compiler/elements/image.mjs +52 -50
  20. package/aot/compiler/elements/modal.mjs +31 -30
  21. package/aot/compiler/elements/props.mjs +61 -26
  22. package/aot/compiler/elements/rewrite.mjs +118 -0
  23. package/aot/compiler/elements/section-list.mjs +370 -0
  24. package/aot/compiler/elements/switch.mjs +42 -34
  25. package/aot/compiler/elements/text-input.mjs +34 -32
  26. package/aot/compiler/expressions.mjs +1685 -392
  27. package/aot/compiler/handlers.mjs +2029 -547
  28. package/aot/compiler/helper-values.mjs +312 -0
  29. package/aot/compiler/ir/functions.mjs +11 -1
  30. package/aot/compiler/ir/nodes.mjs +43 -4
  31. package/aot/compiler/ir/reads.mjs +223 -22
  32. package/aot/compiler/ir/text.mjs +5 -1
  33. package/aot/compiler/ir/types.mjs +26 -14
  34. package/aot/compiler/ir/vectors.mjs +5 -1
  35. package/aot/compiler/kept-states.mjs +177 -0
  36. package/aot/compiler/keyboard.mjs +17 -7
  37. package/aot/compiler/list-items.mjs +226 -0
  38. package/aot/compiler/list-order.mjs +586 -0
  39. package/aot/compiler/list-split.mjs +259 -0
  40. package/aot/compiler/loops.mjs +582 -0
  41. package/aot/compiler/lower.mjs +246 -111
  42. package/aot/compiler/native-files.mjs +439 -0
  43. package/aot/compiler/native-header.mjs +335 -0
  44. package/aot/compiler/native-modules.mjs +1173 -0
  45. package/aot/compiler/nodes.mjs +195 -172
  46. package/aot/compiler/options.mjs +37 -31
  47. package/aot/compiler/out.mjs +32 -23
  48. package/aot/compiler/package/animations.mjs +779 -0
  49. package/aot/compiler/package/arena.mjs +252 -0
  50. package/aot/compiler/package/assemble.mjs +175 -0
  51. package/aot/compiler/package/bytecode.mjs +315 -0
  52. package/aot/compiler/package/checks.mjs +1026 -0
  53. package/aot/compiler/package/codegen.mjs +2168 -0
  54. package/aot/compiler/package/enums.mjs +148 -0
  55. package/aot/compiler/package/format.mjs +771 -0
  56. package/aot/compiler/package/layouts.mjs +136 -0
  57. package/aot/compiler/package/lower.mjs +2103 -0
  58. package/aot/compiler/package/props.mjs +118 -0
  59. package/aot/compiler/package/string-caps.mjs +515 -0
  60. package/aot/compiler/package/tables.mjs +127 -0
  61. package/aot/compiler/package/vectors.mjs +502 -0
  62. package/aot/compiler/package/writer.mjs +686 -0
  63. package/aot/compiler/pan-responder.mjs +69 -61
  64. package/aot/compiler/parse.mjs +23 -21
  65. package/aot/compiler/record-shapes.mjs +246 -0
  66. package/aot/compiler/records.mjs +654 -0
  67. package/aot/compiler/reducers.mjs +272 -0
  68. package/aot/compiler/static-eval.mjs +340 -30
  69. package/aot/compiler/style-text.mjs +178 -174
  70. package/aot/compiler/svg.mjs +480 -251
  71. package/aot/compiler/switch-cases.mjs +198 -0
  72. package/aot/compiler/tagged-records.mjs +450 -0
  73. package/aot/compiler/typing.mjs +307 -0
  74. package/aot/compiler/updaters.mjs +320 -0
  75. package/aot/compiler/window-dimensions.mjs +94 -0
  76. package/aot/compiler/worked-values.mjs +319 -0
  77. package/aot/demo-file.mjs +30 -0
  78. package/aot/screenshot-smoke.mjs +54 -40
  79. package/aot/style-map.mjs +152 -92
  80. package/assets/bake-font.mjs +123 -77
  81. package/assets/bake-image.mjs +39 -22
  82. package/assets/bake-svg.mjs +361 -262
  83. package/assets/build-builtin-font.mjs +11 -7
  84. package/assets/emit-c.mjs +77 -58
  85. package/assets/emit-container.mjs +123 -37
  86. package/assets/emit-pack.mjs +77 -67
  87. package/assets/font-config.mjs +9 -6
  88. package/assets/font-sizes.mjs +163 -123
  89. package/assets/glyph-coverage.mjs +74 -54
  90. package/assets/index.mjs +31 -9
  91. package/assets/rasterize.mjs +126 -99
  92. package/assets/svg-loader.mjs +8 -8
  93. package/build.mjs +23 -13
  94. package/cli.mjs +402 -172
  95. package/compile-bin.mjs +3 -3
  96. package/flash.mjs +537 -0
  97. package/hotreload/dev-device.mjs +90 -33
  98. package/hotreload/pack-app.mjs +55 -43
  99. package/hotreload/pack-split.mjs +62 -64
  100. package/hotreload/package-transfer.mjs +175 -0
  101. package/hotreload/protocol.mjs +50 -37
  102. package/hotreload/uploader.mjs +38 -27
  103. package/hotreload/watch-tree.mjs +19 -19
  104. package/pack-container.mjs +37 -21
  105. package/package.json +7 -8
  106. package/persist-transform.mjs +34 -28
  107. package/qjsc-wasm.mjs +25 -14
  108. package/sim/embedded-react.cjs +1 -1
  109. package/sim/embedded-react.js +1 -1
  110. package/sim/embedded-react.wasm +0 -0
  111. package/sim/index.html +94 -85
  112. package/sim-server.mjs +113 -94
  113. package/src/embedded-react/Animated.js +24 -24
  114. package/src/embedded-react/Dimensions.js +37 -0
  115. package/src/embedded-react/Easing.js +1 -1
  116. package/src/embedded-react/FlatList.js +3 -3
  117. package/src/embedded-react/LayoutAnimation.js +8 -5
  118. package/src/embedded-react/PanResponder.js +75 -71
  119. package/src/embedded-react/Pressable.js +2 -8
  120. package/src/embedded-react/SectionList.js +7 -9
  121. package/src/embedded-react/StyleSheet.js +4 -3
  122. package/src/embedded-react/TouchableOpacity.js +13 -8
  123. package/src/embedded-react/index.d.ts +88 -8
  124. package/src/embedded-react/index.js +2 -0
  125. package/src/embedded-react/layout-anim-config.js +10 -4
  126. package/src/embedded-react/list-child.js +5 -5
  127. package/src/embedded-react/named-colors.js +1 -1
  128. package/src/embedded-react/nativeModule.js +462 -0
  129. package/src/embedded-react/split-style.js +11 -11
  130. package/src/embedded-react/svg-ops.js +760 -593
  131. package/src/embedded-react/useHostValue.js +3 -3
  132. package/src/embedded-react/usePersistentState.js +6 -6
  133. package/src/embedded-react/warn-props.js +1 -1
  134. package/src/host-config.js +60 -53
  135. package/src/props.js +49 -41
  136. package/src/renderer.js +7 -4
  137. package/aot/compiler/assemble.mjs +0 -944
  138. package/aot/compiler/c/animations.mjs +0 -436
  139. package/aot/compiler/c/boot.mjs +0 -127
  140. package/aot/compiler/c/expressions.mjs +0 -270
  141. package/aot/compiler/c/functions.mjs +0 -268
  142. package/aot/compiler/c/keyboard.mjs +0 -96
  143. package/aot/compiler/c/nodes.mjs +0 -303
  144. package/aot/compiler/c/pan-responder.mjs +0 -136
  145. package/aot/compiler/c/statements.mjs +0 -319
  146. package/aot/compiler/c/text.mjs +0 -83
  147. package/aot/compiler/c/vectors.mjs +0 -381
  148. package/aot/compiler/c-syntax.mjs +0 -127
@@ -95,11 +95,11 @@ function lowerKey(key, layerIndex) {
95
95
  *
96
96
  * @returns The config.
97
97
  */
98
- const lowerConfig = withLoc((configArg) => {
98
+ const lowerConfig = withLoc((configArgument) => {
99
99
  // Fold the config object at compile time.
100
100
  let config;
101
101
  try {
102
- config = evalStatic(configArg, {});
102
+ config = evalStatic(configArgument, {});
103
103
  }
104
104
  catch {
105
105
  throw aotError('AOT: setKeyboardConfig(...) needs a statically-foldable config object', 'pass an object literal of colors/sizes (+ an optional `layers` array) — no state or runtime values.');
@@ -107,7 +107,7 @@ const lowerConfig = withLoc((configArg) => {
107
107
  if (config == null || typeof config !== 'object') {
108
108
  throw aotError('AOT: setKeyboardConfig(...) needs a config object');
109
109
  }
110
- // Lower the layout when the app gives one, layer by layer and row by row; C has no empty table.
110
+ // Lower the layout when the app gives one, layer by layer and row by row; an empty table holds nothing.
111
111
  let layers = null;
112
112
  if (Array.isArray(config.layers)) {
113
113
  if (!config.layers.length) {
@@ -127,10 +127,20 @@ const lowerConfig = withLoc((configArg) => {
127
127
  return row.map(key => lowerKey(key, layerIndex));
128
128
  });
129
129
  });
130
+ // A layer key switching to a layer that isn't there would leave the keyboard with no keys.
131
+ const layerCount = layers.length;
132
+ for (const key of layers.flat(2)) {
133
+ if (key.kind === 'layer' &&
134
+ !(Number.isInteger(key.layer) &&
135
+ key.layer >= 0 &&
136
+ key.layer < layerCount)) {
137
+ throw aotError(`AOT: a setKeyboardConfig layer key switches to layer ${key.layer}, but there are only ${layerCount}`, `use a layer from 0 to ${layerCount - 1}.`);
138
+ }
139
+ }
130
140
  }
131
141
  return {
132
142
  layers,
133
- gridCols: kbdSize(config.gridCols),
143
+ gridColumns: kbdSize(config.gridColumns),
134
144
  rowHeight: kbdSize(config.rowHeight),
135
145
  keyGap: kbdSize(config.keyGap),
136
146
  keyRadius: kbdSize(config.keyRadius),
@@ -152,15 +162,15 @@ const lowerConfig = withLoc((configArg) => {
152
162
  */
153
163
  export function lowerKeyboardConfig(program) {
154
164
  // Find the first top-level setKeyboardConfig(...) call; without one, the built-in keyboard stays as is.
155
- let configArg = null;
165
+ let configArgument = null;
156
166
  for (const statement of program.body) {
157
167
  if (statement.type === 'ExpressionStatement' &&
158
168
  statement.expression.type === 'CallExpression' &&
159
169
  statement.expression.callee.type === 'Identifier' &&
160
170
  statement.expression.callee.name === 'setKeyboardConfig') {
161
- configArg = statement.expression.arguments[0];
171
+ configArgument = statement.expression.arguments[0];
162
172
  break;
163
173
  }
164
174
  }
165
- return configArg ? lowerConfig(configArg) : null;
175
+ return configArgument ? lowerConfig(configArgument) : null;
166
176
  }
@@ -0,0 +1,226 @@
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
+ // list-items.mts — the fields of a list's items: worked out from its initial items, and laid out in a row.
17
+ /*----------------------------------------------------------------------------------------------------------------------
18
+ - Imports
19
+ ---------------------------------------------------------------------------------------------------------------------*/
20
+ import { readLocalOf, settledOf } from './typing.mjs';
21
+ import { aotError } from './diagnostics.mjs';
22
+ /*----------------------------------------------------------------------------------------------------------------------
23
+ - Implementation
24
+ ---------------------------------------------------------------------------------------------------------------------*/
25
+ /**
26
+ * Whether a value is a plain object, as an item or a nested field is.
27
+ *
28
+ * @param value The value.
29
+ *
30
+ * @returns Whether it is one.
31
+ */
32
+ export const isPlainObject = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
33
+ /**
34
+ * The kind of field a value is stored in, or null when it is none.
35
+ *
36
+ * @param value The value.
37
+ *
38
+ * @returns The kind.
39
+ */
40
+ function kindOf(value) {
41
+ if (typeof value === 'string')
42
+ return 'string';
43
+ if (typeof value === 'boolean')
44
+ return 'bool';
45
+ if (typeof value === 'number') {
46
+ return Number.isInteger(value) ? 'int' : 'float';
47
+ }
48
+ return null;
49
+ }
50
+ /**
51
+ * Whether two kinds can share a field: a whole number and a fraction can, as a fraction.
52
+ *
53
+ * @param first The kind the field has.
54
+ * @param other Another item's kind.
55
+ *
56
+ * @returns Whether they agree.
57
+ */
58
+ const kindsAgree = (first, other) => first === other ||
59
+ ((first === 'int' || first === 'float') &&
60
+ (other === 'int' || other === 'float'));
61
+ /**
62
+ * Works out the fields of a list's items from its initial items. Objects take the union of their fields, nested
63
+ * objects flattened into dotted paths; a field some item leaves out, or sets to null, is optional. Plain values
64
+ * (numbers, strings or booleans) are one field. A number field is a fraction when any item's value is one.
65
+ *
66
+ * @param items The initial items.
67
+ * @param what The list, for errors: `list state "items"`.
68
+ *
69
+ * @returns The struct.
70
+ */
71
+ export function inferItemStruct(items, what) {
72
+ const shapeHint = 'a list holds objects with string, number or boolean fields (nested objects too), or plain numbers, strings ' +
73
+ 'or booleans, e.g. useState([{title: "A", n: 1}, {title: "B", n: 2}]).';
74
+ if (!Array.isArray(items) || !items.length) {
75
+ throw aotError(`AOT: ${what} needs ≥1 initial element to infer its item shape`, shapeHint);
76
+ }
77
+ // Plain values: one field, of the values' kind.
78
+ if (!items.some(isPlainObject)) {
79
+ const kind = kindOf(items[0]);
80
+ if (!kind ||
81
+ items.some(item => !kindOf(item) || !kindsAgree(kind, kindOf(item)))) {
82
+ throw aotError(`AOT: ${what} must hold objects, or values of one kind (numbers, strings or booleans)`, shapeHint);
83
+ }
84
+ const widened = items.some(item => kindOf(item) === 'float')
85
+ ? 'float'
86
+ : kind;
87
+ return { fields: [{ key: '', path: [], kind: widened }], plain: true };
88
+ }
89
+ if (!items.every(isPlainObject)) {
90
+ throw aotError(`AOT: ${what} elements must all be objects`, shapeHint);
91
+ }
92
+ // Each field in the order it first appears, typed by the items' values.
93
+ const fields = new Map();
94
+ const objects = new Set();
95
+ const nulls = new Set();
96
+ const visit = (object, path) => {
97
+ const keys = Object.keys(object);
98
+ if (path.length && !keys.length) {
99
+ throw aotError(`AOT: field "${path.join('.')}" of ${what} is an empty object`, 'give it a field, or leave it out.');
100
+ }
101
+ for (const key of keys) {
102
+ const fieldPath = [...path, key];
103
+ const name = fieldPath.join('.');
104
+ if (key.includes('.') || key === '') {
105
+ throw aotError(`AOT: ${what} has a field named "${key}"`, 'name each field with letters, digits or `_`.');
106
+ }
107
+ const value = object[key];
108
+ if (isPlainObject(value)) {
109
+ if (fields.has(name)) {
110
+ throw aotError(`AOT: field "${name}" of ${what} is a value in one item and an object in another`, shapeHint);
111
+ }
112
+ objects.add(name);
113
+ visit(value, fieldPath);
114
+ continue;
115
+ }
116
+ if (value == null) {
117
+ nulls.add(name);
118
+ continue;
119
+ }
120
+ const kind = kindOf(value);
121
+ if (!kind || objects.has(name)) {
122
+ throw aotError(`AOT: field "${name}" of ${what} must be a string, a number, a boolean or an object of those`, shapeHint);
123
+ }
124
+ const field = fields.get(name);
125
+ if (!field) {
126
+ fields.set(name, { key: name, path: fieldPath, kind });
127
+ }
128
+ else if (!kindsAgree(field.kind, kind)) {
129
+ throw aotError(`AOT: field "${name}" of ${what} is a ${field.kind === 'bool' ? 'boolean' : field.kind === 'string' ? 'string' : 'number'} in one item and not in another`, shapeHint);
130
+ }
131
+ else if (kind === 'float') {
132
+ field.kind = 'float';
133
+ }
134
+ }
135
+ };
136
+ items.forEach(item => visit(item, []));
137
+ // A field that is null in every item has no kind.
138
+ for (const name of nulls) {
139
+ if (!fields.has(name) && !objects.has(name)) {
140
+ throw aotError(`AOT: field "${name}" of ${what} is null in every item`, 'give one item a value for it, so the field has a kind.');
141
+ }
142
+ }
143
+ const struct = { fields: [...fields.values()] };
144
+ for (const field of struct.fields) {
145
+ if (items.some(item => !itemValueAt(item, field).present)) {
146
+ field.optional = true;
147
+ }
148
+ }
149
+ return struct;
150
+ }
151
+ /**
152
+ * A field's value in an item: missing when the item, or an object on the way, leaves it out or holds null.
153
+ *
154
+ * @param item The item.
155
+ * @param field The field.
156
+ *
157
+ * @returns The value, and whether it is there.
158
+ */
159
+ export function itemValueAt(item, field) {
160
+ let value = item;
161
+ for (const key of field.path) {
162
+ if (!isPlainObject(value) || !Object.hasOwn(value, key)) {
163
+ return { present: false, value: undefined };
164
+ }
165
+ value = value[key];
166
+ }
167
+ return value == null
168
+ ? { present: false, value: undefined }
169
+ : { present: true, value };
170
+ }
171
+ /**
172
+ * The fields of a row in order: every value, then a flag for each optional one.
173
+ *
174
+ * @param struct The items' fields.
175
+ *
176
+ * @returns The row's fields.
177
+ */
178
+ export const rowEntries = (struct) => [
179
+ ...struct.fields.map(field => ({ field, presence: false })),
180
+ ...struct.fields
181
+ .filter(field => field.optional)
182
+ .map(field => ({ field, presence: true })),
183
+ ];
184
+ /**
185
+ * Where a field, or its flag, is in a row.
186
+ *
187
+ * @param struct The items' fields.
188
+ * @param field The field.
189
+ * @param presence The flag rather than the value.
190
+ *
191
+ * @returns Its index.
192
+ */
193
+ export function rowIndexOf(struct, field, presence = false) {
194
+ const index = rowEntries(struct).findIndex(entry => entry.field === field && entry.presence === presence);
195
+ if (index < 0) {
196
+ throw new Error(`AOT internal: no row field for "${field.key}"`);
197
+ }
198
+ return index;
199
+ }
200
+ /**
201
+ * The fields at or under a path: the field itself, or every field of the object there.
202
+ *
203
+ * @param struct The items' fields.
204
+ * @param path The path.
205
+ *
206
+ * @returns The fields, none when nothing is there.
207
+ */
208
+ export function fieldsUnder(struct, path) {
209
+ return struct.fields.filter(field => field.path.length >= path.length &&
210
+ path.every((key, index) => field.path[index] === key));
211
+ }
212
+ /**
213
+ * The list item field an expression reads, directly or through a local that stands for it (a prop given one).
214
+ *
215
+ * @param expression The expression.
216
+ *
217
+ * @returns The read, or null.
218
+ */
219
+ export function itemReadOf(expression) {
220
+ const settled = settledOf(expression);
221
+ if (settled?.kind === 'itemField') {
222
+ return settled;
223
+ }
224
+ const local = readLocalOf(expression);
225
+ return local?.expression ? itemReadOf(local.expression) : null;
226
+ }