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,315 @@
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
+ * Builds one function's bytecode: typed registers, instructions in the forms erb_format.h gives each opcode,
18
+ * and forward branches to labels placed later.
19
+ */
20
+ /*----------------------------------------------------------------------------------------------------------------------
21
+ - Imports
22
+ ---------------------------------------------------------------------------------------------------------------------*/
23
+ import { aotError } from '../diagnostics.mjs';
24
+ import { OPCODES, ValueType } from './format.mjs';
25
+ /*----------------------------------------------------------------------------------------------------------------------
26
+ - Constants
27
+ ---------------------------------------------------------------------------------------------------------------------*/
28
+ /** Each opcode by name, with its number and length. */
29
+ export const OP = Object.fromEntries(OPCODES.map(([name, , words], index) => [name, { code: index + 1, words }]));
30
+ /** Register cells a function may have: the function record counts them in a byte. */
31
+ const MAX_REGISTERS = 255;
32
+ /** The longest branch forward, in words: a branch keeps its offset in a signed 16 bits. */
33
+ const MAX_BRANCH_OFFSET = 0x7fff;
34
+ /** The most loops in one function: the verifier's ERB_MAX_LOOPS. */
35
+ const MAX_LOOPS = 32;
36
+ /** The register types that take two cells. */
37
+ const WIDE = new Set(['int64', 'double', 'node']);
38
+ /*----------------------------------------------------------------------------------------------------------------------
39
+ - Classes
40
+ ---------------------------------------------------------------------------------------------------------------------*/
41
+ /**
42
+ * One function as it is built. Temporaries go back to a pool by type when released, so a long function
43
+ * doesn't run out of registers.
44
+ *
45
+ * @class FunctionBuilder
46
+ */
47
+ export class FunctionBuilder {
48
+ code = [];
49
+ /** Each cell's type, as the types table holds it. */
50
+ cellTypes = [];
51
+ pool = new Map();
52
+ /** Released runs of consecutive registers, by their types, at their first cell. */
53
+ runs = new Map();
54
+ /** The types of each run, by its first cell. */
55
+ runTypes = new Map();
56
+ /** Each run's registers, by its first. */
57
+ runIndices = new Map();
58
+ /** Registers in use, in the order they were taken, for checkpoints. */
59
+ taken = [];
60
+ /** Where each run of the code starts in the source, for the debug table. */
61
+ lines = [];
62
+ /** How many loops the function has so far. */
63
+ loops = 0;
64
+ kind;
65
+ what;
66
+ /**
67
+ * @param kind The function's kind (a FunctionKind or an EREventType).
68
+ * @param what What the function is, for an error.
69
+ */
70
+ constructor(kind, what) {
71
+ this.kind = kind;
72
+ this.what = what;
73
+ }
74
+ /** The next instruction's word index. */
75
+ get position() {
76
+ return this.code.length;
77
+ }
78
+ /**
79
+ * Marks the code from here on as lowered from a place in the source.
80
+ *
81
+ * @param location The place, if it has one.
82
+ */
83
+ at(location) {
84
+ if (!location) {
85
+ return;
86
+ }
87
+ // A mark nothing was written after is replaced.
88
+ if (this.lines.at(-1)?.offset === this.position) {
89
+ this.lines.pop();
90
+ }
91
+ const last = this.lines.at(-1);
92
+ if (last?.line !== location.line || last.column !== location.column) {
93
+ this.lines.push({
94
+ offset: this.position,
95
+ line: location.line,
96
+ column: location.column,
97
+ });
98
+ }
99
+ }
100
+ /**
101
+ * A register of a type: a released one when there is one, else a new one.
102
+ *
103
+ * @param type Its type.
104
+ *
105
+ * @returns The register.
106
+ */
107
+ register(type) {
108
+ const reused = this.pool.get(type)?.pop();
109
+ const register = { index: reused ?? this.newRegister(type), type };
110
+ this.taken.push(register);
111
+ return register;
112
+ }
113
+ /**
114
+ * A register of a type that is never released, for the rest of the function. Registers start at zero, so
115
+ * one nothing writes keeps that value.
116
+ *
117
+ * @param type Its type.
118
+ *
119
+ * @returns The register.
120
+ */
121
+ reserved(type) {
122
+ return { index: this.newRegister(type), type };
123
+ }
124
+ /**
125
+ * Registers in consecutive cells, as APPEND reads a row and CALLN its arguments: a released run of the same types
126
+ * when there is one, else new ones. A two-cell register starts on an even cell, so a gap may come before it. A run
127
+ * is released whole, and only ever reused as a run.
128
+ *
129
+ * @param types Their types.
130
+ *
131
+ * @returns The registers.
132
+ */
133
+ consecutive(types) {
134
+ const key = types.join(' ');
135
+ const reused = this.runs.get(key)?.pop();
136
+ let indices = reused === undefined ? undefined : this.runIndices.get(reused);
137
+ if (!indices) {
138
+ indices = types.map(type => this.newRegister(type));
139
+ this.runTypes.set(indices[0], key);
140
+ this.runIndices.set(indices[0], indices);
141
+ }
142
+ const registers = types.map((type, index) => ({
143
+ index: indices[index],
144
+ type,
145
+ }));
146
+ this.taken.push(registers[0]);
147
+ return registers;
148
+ }
149
+ newRegister(type) {
150
+ // A two-cell register starts on an even cell.
151
+ if (WIDE.has(type) && this.cellTypes.length % 2) {
152
+ this.cellTypes.push(ValueType.int);
153
+ }
154
+ const index = this.cellTypes.length;
155
+ this.cellTypes.push(ValueType[type]);
156
+ if (WIDE.has(type)) {
157
+ this.cellTypes.push(ValueType.pair);
158
+ }
159
+ if (this.cellTypes.length > MAX_REGISTERS) {
160
+ throw aotError(`AOT: ${this.what} needs more than ${MAX_REGISTERS} registers in a package`, 'split it into smaller pieces.');
161
+ }
162
+ return index;
163
+ }
164
+ /** Where the registers taken so far end, for release. */
165
+ checkpoint() {
166
+ return this.taken.length;
167
+ }
168
+ /**
169
+ * Releases every register taken since a checkpoint, except those kept.
170
+ *
171
+ * @param checkpoint The checkpoint.
172
+ * @param keep Registers still in use.
173
+ */
174
+ release(checkpoint, ...keep) {
175
+ const kept = this.taken.splice(checkpoint).filter(register => {
176
+ if (keep.includes(register)) {
177
+ return true;
178
+ }
179
+ const run = this.runTypes.get(register.index);
180
+ if (run !== undefined) {
181
+ this.runs.set(run, [...(this.runs.get(run) ?? []), register.index]);
182
+ return false;
183
+ }
184
+ const free = this.pool.get(register.type) ?? [];
185
+ free.push(register.index);
186
+ this.pool.set(register.type, free);
187
+ return false;
188
+ });
189
+ this.taken.push(...kept);
190
+ }
191
+ /**
192
+ * An instruction with A, B and C.
193
+ *
194
+ * @param name The opcode.
195
+ * @param operandA A.
196
+ * @param operandB B.
197
+ * @param operandC C.
198
+ * @param extension The extension word, for an opcode that has one.
199
+ */
200
+ abc(name, operandA = 0, operandB = 0, operandC = 0, extension) {
201
+ this.emit(name, (operandA & 0xff) | ((operandB & 0xff) << 8) | ((operandC & 0xff) << 16), extension);
202
+ }
203
+ /**
204
+ * An instruction with A and a 16-bit Bx.
205
+ *
206
+ * @param name The opcode.
207
+ * @param operandA A.
208
+ * @param operandBx Bx, an index or a signed immediate.
209
+ * @param extension The extension word, for an opcode that has one.
210
+ */
211
+ abx(name, operandA, operandBx, extension) {
212
+ this.emit(name, (operandA & 0xff) | ((operandBx & 0xffff) << 8), extension);
213
+ }
214
+ emit(name, operands, extension) {
215
+ const opcode = OP[name];
216
+ this.code.push((opcode.code | (operands << 8)) >>> 0);
217
+ if (opcode.words === 2) {
218
+ this.code.push((extension ?? 0) >>> 0);
219
+ }
220
+ }
221
+ /** A label to branch to, placed later. */
222
+ label() {
223
+ return { at: null, branches: [] };
224
+ }
225
+ /**
226
+ * Places a label here, and points the branches to it here.
227
+ *
228
+ * @param label The label.
229
+ */
230
+ place(label) {
231
+ label.at = this.position;
232
+ for (const at of label.branches) {
233
+ const offset = label.at - (at + wordsOf(this.code[at]));
234
+ if (offset > MAX_BRANCH_OFFSET) {
235
+ throw aotError(`AOT: ${this.what} has a branch too long for a package`, 'split it into smaller pieces.');
236
+ }
237
+ this.code[at] = ((this.code[at] & 0xffff) | (offset << 16)) >>> 0;
238
+ }
239
+ }
240
+ /**
241
+ * A forward branch to a label: JMP, or JT / JF on a bool register.
242
+ *
243
+ * @param name JMP, JT or JF.
244
+ * @param label Where it goes.
245
+ * @param test The bool register JT and JF test.
246
+ */
247
+ branch(name, label, test) {
248
+ label.branches.push(this.position);
249
+ this.abx(name, test?.index ?? 0, 0);
250
+ }
251
+ /**
252
+ * A counted loop: the body runs `bound` times, the counter going from `bound` down to 1. Only LOOPSET and LOOP
253
+ * may write the counter, so it is a register no release hands out.
254
+ *
255
+ * @param counter The counter.
256
+ * @param bound How many times.
257
+ * @param body Lowers the body.
258
+ */
259
+ loop(counter, bound, body) {
260
+ if (++this.loops > MAX_LOOPS) {
261
+ throw aotError(`AOT: ${this.what} has more than ${MAX_LOOPS} loops`, 'move some of them into another handler or effect, or combine loops over the same list.');
262
+ }
263
+ this.abx('LOOPSET', counter.index, bound);
264
+ const start = this.position;
265
+ body();
266
+ const offset = start - (this.position + 1);
267
+ if (-offset > MAX_BRANCH_OFFSET + 1) {
268
+ throw aotError(`AOT: ${this.what} has a loop too long for a package`, 'split it into smaller pieces.');
269
+ }
270
+ this.abx('LOOP', counter.index, offset);
271
+ }
272
+ /**
273
+ * A branch to a label when any of a dirty word's bits is set in this update pass.
274
+ *
275
+ * @param word The dirty word.
276
+ * @param mask Its bits.
277
+ * @param label Where it goes.
278
+ */
279
+ branchIfDirty(word, mask, label) {
280
+ label.branches.push(this.position);
281
+ this.abx('JDIRTY', word, 0, mask);
282
+ }
283
+ /**
284
+ * The finished function, ending in RET.
285
+ *
286
+ * @param parameters Parameter cells.
287
+ * @param result What it returns, for a function that returns a value.
288
+ *
289
+ * @returns The function.
290
+ */
291
+ finish(parameters = 0, result) {
292
+ this.abc('RET', result?.index ?? 0);
293
+ return {
294
+ kind: this.kind,
295
+ parameters,
296
+ result: result ? ValueType[result.type] : 0,
297
+ types: [...this.cellTypes],
298
+ code: [...this.code],
299
+ debug: { name: this.what, lines: [...this.lines] },
300
+ };
301
+ }
302
+ }
303
+ /*----------------------------------------------------------------------------------------------------------------------
304
+ - Implementation
305
+ ---------------------------------------------------------------------------------------------------------------------*/
306
+ /**
307
+ * An instruction's length, from its first word.
308
+ *
309
+ * @param word The word.
310
+ *
311
+ * @returns Its length in words.
312
+ */
313
+ function wordsOf(word) {
314
+ return OPCODES[(word & 0xff) - 1][2];
315
+ }