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
@@ -0,0 +1,272 @@
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
+ * useReducer: each `dispatch(action)` runs the reducer in place, as an updater with a block body. The action is bound
18
+ * where it is dispatched: its constant fields fold, so the branches its type rules out are dropped before the body is
19
+ * lowered, and a `throw` for an action no dispatch sends never runs.
20
+ */
21
+ /*----------------------------------------------------------------------------------------------------------------------
22
+ - Imports
23
+ ---------------------------------------------------------------------------------------------------------------------*/
24
+ import { lowerLocal } from './expressions.mjs';
25
+ import { exits, statementsOf } from './helper-values.mjs';
26
+ import { AstLeaf, isObjectTree, objectTreeOf } from './records.mjs';
27
+ import { RuntimeLeaf, evalStatic, foldScope, rethrowFoldLimit, withoutNames, } from './static-eval.mjs';
28
+ import { switchAsIf } from './switch-cases.mjs';
29
+ import { declaredIn } from './updaters.mjs';
30
+ /*----------------------------------------------------------------------------------------------------------------------
31
+ - Implementation
32
+ ---------------------------------------------------------------------------------------------------------------------*/
33
+ /**
34
+ * What an expression folds to, if it does.
35
+ *
36
+ * @param node The expression.
37
+ * @param scope The constants a fold may read.
38
+ *
39
+ * @returns The value, or undefined when it does not fold.
40
+ */
41
+ function folded(node, scope) {
42
+ try {
43
+ return { value: evalStatic(node, scope, 'strict') };
44
+ }
45
+ catch (error) {
46
+ rethrowFoldLimit(error);
47
+ return undefined;
48
+ }
49
+ }
50
+ /**
51
+ * The local a runtime value is held in, worked out where it is written.
52
+ *
53
+ * @param node The value.
54
+ * @param environment The environment it is written in.
55
+ *
56
+ * @returns The local.
57
+ */
58
+ function localOf(node, environment) {
59
+ return lowerLocal(node, environment);
60
+ }
61
+ /**
62
+ * An object's fields as the reducer reads them: a constant stays one, and anything else is worked out at the
63
+ * dispatch, so names the reducer declares can't change what it reads.
64
+ *
65
+ * @param tree The object.
66
+ * @param environment The dispatch's environment.
67
+ * @param scope The constants a fold may read there.
68
+ *
69
+ * @returns The object.
70
+ */
71
+ function settledTree(tree, environment, scope) {
72
+ return Object.fromEntries(Object.entries(tree).map(([key, value]) => {
73
+ if (isObjectTree(value)) {
74
+ return [key, settledTree(value, environment, scope)];
75
+ }
76
+ if (!(value instanceof AstLeaf))
77
+ return [key, value];
78
+ const constant = value.locals.size
79
+ ? undefined
80
+ : folded(value.node, scope);
81
+ if (constant && typeof constant.value !== 'object') {
82
+ return [key, constant.value];
83
+ }
84
+ const fieldEnvironment = value.locals.size
85
+ ? {
86
+ ...environment,
87
+ locals: new Map([...environment.locals, ...value.locals]),
88
+ }
89
+ : environment;
90
+ return [key, new RuntimeLeaf(localOf(value.node, fieldEnvironment))];
91
+ }));
92
+ }
93
+ /**
94
+ * An action that picks between two at run time: a ternary of actions, or an object with a field that is a ternary of
95
+ * two constants, `{type: up ? 'inc' : 'dec'}`. Each side is dispatched on its own, so its constant fields fold.
96
+ *
97
+ * @param action What is dispatched.
98
+ * @param scope The constants a fold may read.
99
+ *
100
+ * @returns The test and the action each way, or null when the action picks nothing.
101
+ */
102
+ export function actionChoice(action, scope) {
103
+ if (action.type === 'ConditionalExpression' && !folded(action.test, scope)) {
104
+ return {
105
+ test: action.test,
106
+ whenTrue: action.consequent,
107
+ whenFalse: action.alternate,
108
+ };
109
+ }
110
+ if (action.type !== 'ObjectExpression')
111
+ return null;
112
+ for (const [index, property] of action.properties.entries()) {
113
+ if (property.type !== 'ObjectProperty' ||
114
+ property.value.type !== 'ConditionalExpression' ||
115
+ folded(property.value.test, scope)) {
116
+ continue;
117
+ }
118
+ const { test, consequent, alternate } = property.value;
119
+ const isConstant = (node) => {
120
+ const value = folded(node, scope);
121
+ return value !== undefined && typeof value.value !== 'object';
122
+ };
123
+ if (!isConstant(consequent) || !isConstant(alternate))
124
+ continue;
125
+ const withValue = (value) => ({
126
+ ...action,
127
+ properties: action.properties.map((other, at) => at === index ? { ...property, value } : other),
128
+ });
129
+ return {
130
+ test,
131
+ whenTrue: withValue(consequent),
132
+ whenFalse: withValue(alternate),
133
+ };
134
+ }
135
+ return null;
136
+ }
137
+ /**
138
+ * The environment a reducer's body runs in for one dispatch: the action parameter bound to what is dispatched. A
139
+ * constant folds, an object is its fields, and anything else is a local holding the value.
140
+ *
141
+ * @param parameter The action parameter's name, if the reducer has one.
142
+ * @param action What the dispatch passes.
143
+ * @param environment The dispatch's environment.
144
+ *
145
+ * @returns The body's environment.
146
+ */
147
+ export function actionEnvironment(parameter, action, environment) {
148
+ if (!parameter)
149
+ return environment;
150
+ const scope = foldScope(environment, environment.constants ?? {});
151
+ const locals = new Map(environment.locals);
152
+ const state = new Map(environment.state);
153
+ const constants = { ...environment.constants };
154
+ locals.delete(parameter);
155
+ state.delete(parameter);
156
+ delete constants[parameter];
157
+ const constant = folded(action, scope);
158
+ const tree = constant ? null : objectTreeOf(action, environment);
159
+ if (constant) {
160
+ constants[parameter] = constant.value;
161
+ }
162
+ else if (tree) {
163
+ constants[parameter] = settledTree(tree, environment, scope);
164
+ }
165
+ else {
166
+ locals.set(parameter, localOf(action, environment));
167
+ }
168
+ return { ...environment, locals, state, constants };
169
+ }
170
+ /**
171
+ * A value with each ternary whose test folds made the side it picks.
172
+ *
173
+ * @param node The value.
174
+ * @param scope The constants a fold may read.
175
+ *
176
+ * @returns The value.
177
+ */
178
+ function prunedValue(node, scope) {
179
+ if (node.type !== 'ConditionalExpression')
180
+ return node;
181
+ const test = folded(node.test, scope);
182
+ if (test) {
183
+ return prunedValue(test.value ? node.consequent : node.alternate, scope);
184
+ }
185
+ return {
186
+ ...node,
187
+ consequent: prunedValue(node.consequent, scope),
188
+ alternate: prunedValue(node.alternate, scope),
189
+ };
190
+ }
191
+ /**
192
+ * Statements with each branch whose test folds made the one it takes, in loops too. A branch taken that always ends
193
+ * drops the statements after it, which never run. Tests on the action's constant fields fold, so a `switch
194
+ * (action.type)` keeps only the case a dispatch takes. A name a block or a loop declares hides what it names outside,
195
+ * so no test folds through it.
196
+ *
197
+ * @param statements The statements.
198
+ * @param scope The constants a fold may read.
199
+ *
200
+ * @returns The statements.
201
+ */
202
+ export function prunedStatements(statements, scope) {
203
+ const pruned = [];
204
+ scope = withoutNames(scope, declaredIn(statements));
205
+ for (const original of statements) {
206
+ const statement = original.type === 'SwitchStatement' ? switchAsIf(original) : original;
207
+ switch (statement.type) {
208
+ case 'IfStatement': {
209
+ const test = folded(statement.test, scope);
210
+ if (!test) {
211
+ pruned.push({
212
+ ...statement,
213
+ consequent: {
214
+ type: 'BlockStatement',
215
+ body: prunedStatements(statementsOf(statement.consequent), scope),
216
+ directives: [],
217
+ },
218
+ alternate: statement.alternate
219
+ ? {
220
+ type: 'BlockStatement',
221
+ body: prunedStatements(statementsOf(statement.alternate), scope),
222
+ directives: [],
223
+ }
224
+ : null,
225
+ });
226
+ continue;
227
+ }
228
+ // The branch taken runs in place. One that always ends is the last thing that runs.
229
+ const branch = test.value ? statement.consequent : statement.alternate;
230
+ const taken = branch
231
+ ? prunedStatements(statementsOf(branch), scope)
232
+ : [];
233
+ if (exits(taken)) {
234
+ pruned.push(...taken);
235
+ return pruned;
236
+ }
237
+ if (taken.length) {
238
+ pruned.push({ type: 'BlockStatement', body: taken, directives: [] });
239
+ }
240
+ continue;
241
+ }
242
+ case 'BlockStatement': {
243
+ const body = prunedStatements(statement.body, scope);
244
+ pruned.push({ ...statement, body });
245
+ if (exits(body))
246
+ return pruned;
247
+ continue;
248
+ }
249
+ case 'ForStatement':
250
+ case 'ForOfStatement': {
251
+ const head = statement.type === 'ForStatement' ? statement.init : statement.left;
252
+ pruned.push({
253
+ ...statement,
254
+ body: {
255
+ type: 'BlockStatement',
256
+ body: prunedStatements(statementsOf(statement.body), withoutNames(scope, declaredIn([head]))),
257
+ directives: [],
258
+ },
259
+ });
260
+ continue;
261
+ }
262
+ case 'ReturnStatement':
263
+ pruned.push(statement.argument
264
+ ? { ...statement, argument: prunedValue(statement.argument, scope) }
265
+ : statement);
266
+ return pruned;
267
+ default:
268
+ pruned.push(statement);
269
+ }
270
+ }
271
+ return pruned;
272
+ }