embedded-react 0.19.0 → 0.20.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 (116) hide show
  1. package/README.md +7 -3
  2. package/aot/compile.mjs +178 -58
  3. package/aot/compiler/animations.mjs +290 -281
  4. package/aot/compiler/boot.mjs +70 -33
  5. package/aot/compiler/branch-merge.mjs +4 -3
  6. package/aot/compiler/check.mjs +9 -9
  7. package/aot/compiler/collect.mjs +324 -205
  8. package/aot/compiler/control-flow.mjs +209 -197
  9. package/aot/compiler/diagnostics.mjs +22 -25
  10. package/aot/compiler/elements/activity-indicator.mjs +25 -24
  11. package/aot/compiler/elements/dial.mjs +91 -77
  12. package/aot/compiler/elements/flat-list.mjs +24 -22
  13. package/aot/compiler/elements/image.mjs +51 -50
  14. package/aot/compiler/elements/modal.mjs +31 -30
  15. package/aot/compiler/elements/props.mjs +23 -23
  16. package/aot/compiler/elements/switch.mjs +42 -34
  17. package/aot/compiler/elements/text-input.mjs +34 -32
  18. package/aot/compiler/expressions.mjs +372 -333
  19. package/aot/compiler/handlers.mjs +473 -387
  20. package/aot/compiler/ir/functions.mjs +11 -1
  21. package/aot/compiler/ir/nodes.mjs +4 -4
  22. package/aot/compiler/ir/reads.mjs +19 -19
  23. package/aot/compiler/ir/types.mjs +14 -14
  24. package/aot/compiler/ir/vectors.mjs +5 -1
  25. package/aot/compiler/keyboard.mjs +17 -7
  26. package/aot/compiler/lower.mjs +77 -80
  27. package/aot/compiler/nodes.mjs +169 -152
  28. package/aot/compiler/options.mjs +35 -30
  29. package/aot/compiler/out.mjs +18 -22
  30. package/aot/compiler/package/animations.mjs +669 -0
  31. package/aot/compiler/package/arena.mjs +252 -0
  32. package/aot/compiler/package/assemble.mjs +177 -0
  33. package/aot/compiler/package/bytecode.mjs +286 -0
  34. package/aot/compiler/package/checks.mjs +1022 -0
  35. package/aot/compiler/package/codegen.mjs +1329 -0
  36. package/aot/compiler/package/enums.mjs +138 -0
  37. package/aot/compiler/package/format.mjs +760 -0
  38. package/aot/compiler/package/layouts.mjs +136 -0
  39. package/aot/compiler/package/lower.mjs +1606 -0
  40. package/aot/compiler/package/props.mjs +118 -0
  41. package/aot/compiler/package/tables.mjs +103 -0
  42. package/aot/compiler/package/vectors.mjs +502 -0
  43. package/aot/compiler/package/writer.mjs +664 -0
  44. package/aot/compiler/pan-responder.mjs +69 -61
  45. package/aot/compiler/parse.mjs +21 -21
  46. package/aot/compiler/static-eval.mjs +25 -23
  47. package/aot/compiler/style-text.mjs +161 -172
  48. package/aot/compiler/svg.mjs +477 -251
  49. package/aot/screenshot-smoke.mjs +54 -40
  50. package/aot/style-map.mjs +102 -90
  51. package/assets/bake-font.mjs +123 -77
  52. package/assets/bake-image.mjs +39 -22
  53. package/assets/bake-svg.mjs +361 -262
  54. package/assets/build-builtin-font.mjs +11 -7
  55. package/assets/emit-c.mjs +77 -58
  56. package/assets/emit-container.mjs +123 -37
  57. package/assets/emit-pack.mjs +77 -67
  58. package/assets/font-config.mjs +9 -6
  59. package/assets/font-sizes.mjs +163 -123
  60. package/assets/glyph-coverage.mjs +74 -54
  61. package/assets/index.mjs +31 -9
  62. package/assets/rasterize.mjs +126 -99
  63. package/assets/svg-loader.mjs +8 -8
  64. package/build.mjs +14 -12
  65. package/cli.mjs +359 -172
  66. package/compile-bin.mjs +3 -3
  67. package/flash.mjs +537 -0
  68. package/hotreload/dev-device.mjs +90 -33
  69. package/hotreload/pack-app.mjs +55 -43
  70. package/hotreload/pack-split.mjs +46 -37
  71. package/hotreload/package-transfer.mjs +175 -0
  72. package/hotreload/protocol.mjs +50 -37
  73. package/hotreload/uploader.mjs +38 -27
  74. package/hotreload/watch-tree.mjs +19 -19
  75. package/pack-container.mjs +28 -20
  76. package/package.json +7 -8
  77. package/persist-transform.mjs +34 -28
  78. package/qjsc-wasm.mjs +25 -14
  79. package/sim/embedded-react.cjs +1 -1
  80. package/sim/embedded-react.js +1 -1
  81. package/sim/embedded-react.wasm +0 -0
  82. package/sim/index.html +94 -85
  83. package/sim-server.mjs +101 -82
  84. package/src/embedded-react/Animated.js +24 -24
  85. package/src/embedded-react/Easing.js +1 -1
  86. package/src/embedded-react/FlatList.js +3 -3
  87. package/src/embedded-react/LayoutAnimation.js +8 -5
  88. package/src/embedded-react/PanResponder.js +75 -71
  89. package/src/embedded-react/Pressable.js +2 -8
  90. package/src/embedded-react/SectionList.js +6 -6
  91. package/src/embedded-react/StyleSheet.js +4 -3
  92. package/src/embedded-react/TouchableOpacity.js +13 -8
  93. package/src/embedded-react/index.d.ts +5 -5
  94. package/src/embedded-react/layout-anim-config.js +10 -4
  95. package/src/embedded-react/list-child.js +5 -5
  96. package/src/embedded-react/named-colors.js +1 -1
  97. package/src/embedded-react/split-style.js +11 -11
  98. package/src/embedded-react/svg-ops.js +760 -593
  99. package/src/embedded-react/useHostValue.js +3 -3
  100. package/src/embedded-react/usePersistentState.js +6 -6
  101. package/src/embedded-react/warn-props.js +1 -1
  102. package/src/host-config.js +60 -53
  103. package/src/props.js +49 -41
  104. package/src/renderer.js +7 -4
  105. package/aot/compiler/assemble.mjs +0 -944
  106. package/aot/compiler/c/animations.mjs +0 -436
  107. package/aot/compiler/c/boot.mjs +0 -127
  108. package/aot/compiler/c/expressions.mjs +0 -270
  109. package/aot/compiler/c/functions.mjs +0 -268
  110. package/aot/compiler/c/keyboard.mjs +0 -96
  111. package/aot/compiler/c/nodes.mjs +0 -303
  112. package/aot/compiler/c/pan-responder.mjs +0 -136
  113. package/aot/compiler/c/statements.mjs +0 -319
  114. package/aot/compiler/c/text.mjs +0 -83
  115. package/aot/compiler/c/vectors.mjs +0 -381
  116. package/aot/compiler/c-syntax.mjs +0 -127
@@ -0,0 +1,252 @@
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
+ * The arena's layout: every slot the program reads or writes, the dirty bits, and the regions that hold them.
18
+ * Slots are planned first and laid out once, before any code is written, since code names them by index and
19
+ * the slot table lists them in arena order.
20
+ */
21
+ /*----------------------------------------------------------------------------------------------------------------------
22
+ - Imports
23
+ ---------------------------------------------------------------------------------------------------------------------*/
24
+ import { ANIMATION_ENTRY_SIZE, CELL_SIZE, EVENT_SIZE, FRAME_CELLS, HANDLE_SIZE, LIST_HEADER_SIZE, TAPE_FLOAT_SIZE, Region, SlotFlag, TIMER_SIZE, ValueType, } from './format.mjs';
25
+ /*----------------------------------------------------------------------------------------------------------------------
26
+ - Constants
27
+ ---------------------------------------------------------------------------------------------------------------------*/
28
+ /** The init of a 64-bit or string slot that starts at zero. */
29
+ const NO_INITIAL = 0xffffffff;
30
+ /*----------------------------------------------------------------------------------------------------------------------
31
+ - Classes
32
+ ---------------------------------------------------------------------------------------------------------------------*/
33
+ /**
34
+ * The arena as it is planned.
35
+ *
36
+ * @class Arena
37
+ */
38
+ export class Arena {
39
+ values = [];
40
+ strings = [];
41
+ lists = [];
42
+ handles = [];
43
+ dirtyBits = 0;
44
+ timers = 0;
45
+ animations = 0;
46
+ events = 0;
47
+ tapeFloats = 0;
48
+ laidOut = null;
49
+ /**
50
+ * Plans a slot.
51
+ *
52
+ * @param type Its ValueType.
53
+ * @param initialValue Its initial value: an immediate, or a constant or string index; none starts at zero.
54
+ * @param capacity A string's most bytes, or a handle array's handles.
55
+ *
56
+ * @returns The slot.
57
+ */
58
+ slot(type, initialValue, capacity = 0) {
59
+ if (this.laidOut) {
60
+ throw new Error('AOT internal: a slot planned after the arena was laid out');
61
+ }
62
+ const wide = type === ValueType.int64 || type === ValueType.double;
63
+ const slot = {
64
+ type,
65
+ flags: 0,
66
+ dirtyBit: 0,
67
+ init: initialValue ?? (wide || type === ValueType.string ? NO_INITIAL : 0),
68
+ cap: capacity,
69
+ aux: 0,
70
+ rowBytes: 0,
71
+ index: -1,
72
+ offset: -1,
73
+ };
74
+ (type === ValueType.string
75
+ ? this.strings
76
+ : type === ValueType.list
77
+ ? this.lists
78
+ : type === ValueType.node
79
+ ? this.handles
80
+ : this.values).push(slot);
81
+ return slot;
82
+ }
83
+ /**
84
+ * Plans a list's pool: its row count, then `capacity` rows, empty at load.
85
+ *
86
+ * @param layout Its row layout.
87
+ * @param rowBytes A row's size.
88
+ * @param capacity Rows it holds.
89
+ *
90
+ * @returns The slot.
91
+ */
92
+ list(layout, rowBytes, capacity) {
93
+ return Object.assign(this.slot(ValueType.list, 0, capacity), {
94
+ aux: layout,
95
+ rowBytes,
96
+ });
97
+ }
98
+ /**
99
+ * Gives a slot a dirty bit, set when its value changes.
100
+ *
101
+ * @param slot The slot.
102
+ *
103
+ * @returns The slot.
104
+ */
105
+ track(slot) {
106
+ if (this.laidOut) {
107
+ throw new Error('AOT internal: a dirty bit given after the arena was laid out');
108
+ }
109
+ slot.flags |= SlotFlag.tracked;
110
+ slot.dirtyBit = this.dirtyBits++;
111
+ return slot;
112
+ }
113
+ /**
114
+ * Gives the arena a timer table.
115
+ *
116
+ * @param count Timers that run at once.
117
+ */
118
+ timerTable(count) {
119
+ this.timers = count;
120
+ }
121
+ /**
122
+ * Gives the arena a running-animation table, which the trees ASTART starts are laid out in.
123
+ *
124
+ * @param count Entries it holds.
125
+ */
126
+ animTable(count) {
127
+ this.animations = count;
128
+ }
129
+ /**
130
+ * Gives the arena's event queue room for more events: the effects an update pass queues, or the animation
131
+ * completions that arrive while a function runs.
132
+ *
133
+ * @param count Events it holds besides.
134
+ */
135
+ eventQueue(count) {
136
+ this.events += count;
137
+ }
138
+ /**
139
+ * Gives the arena a tape scratch, as large as the largest tape code builds. An empty tape still needs one.
140
+ *
141
+ * @param floats The tape's floats.
142
+ */
143
+ tapeScratch(floats) {
144
+ this.tapeFloats = Math.max(this.tapeFloats, floats, 1);
145
+ }
146
+ /**
147
+ * Lays the slots out: values, the dirty bits, strings, list pools, node handles, then the timer and animation
148
+ * tables. Code may name slots from here on.
149
+ */
150
+ layOut() {
151
+ const regions = [];
152
+ let offset = 0;
153
+ const region = (kind, slots, bytesOf) => {
154
+ const start = offset;
155
+ for (const slot of slots) {
156
+ slot.offset = offset;
157
+ offset += bytesOf(slot);
158
+ }
159
+ if (offset > start) {
160
+ regions.push({ kind, count: 0, offset: start, bytes: offset - start });
161
+ }
162
+ };
163
+ region(Region.slots, this.values, slot => slot.type === ValueType.int64 || slot.type === ValueType.double ? 8 : 4);
164
+ if (this.dirtyBits) {
165
+ // Kept twice: live, and the update pass's copy.
166
+ const bytes = 2 * Math.ceil(this.dirtyBits / 32) * 4;
167
+ regions.push({ kind: Region.dirty, count: this.dirtyBits, offset, bytes });
168
+ offset += bytes;
169
+ }
170
+ region(Region.strings, this.strings, slot => (slot.cap + 1 + 3) & ~3);
171
+ region(Region.lists, this.lists, slot => LIST_HEADER_SIZE + slot.cap * slot.rowBytes);
172
+ region(Region.handles, this.handles, slot => HANDLE_SIZE * Math.max(slot.cap, 1));
173
+ if (this.timers) {
174
+ const bytes = this.timers * TIMER_SIZE;
175
+ regions.push({ kind: Region.timers, count: this.timers, offset, bytes });
176
+ offset += bytes;
177
+ }
178
+ if (this.animations) {
179
+ const bytes = this.animations * ANIMATION_ENTRY_SIZE;
180
+ regions.push({
181
+ kind: Region.animations,
182
+ count: this.animations,
183
+ offset,
184
+ bytes,
185
+ });
186
+ offset += bytes;
187
+ }
188
+ [...this.values, ...this.strings, ...this.lists, ...this.handles].forEach((slot, index) => {
189
+ slot.index = index;
190
+ });
191
+ this.laidOut = regions;
192
+ }
193
+ /** The slot table, in arena order. */
194
+ get slots() {
195
+ return [
196
+ ...this.values,
197
+ ...this.strings,
198
+ ...this.lists,
199
+ ...this.handles,
200
+ ].map(slot => ({
201
+ type: slot.type,
202
+ flags: slot.flags,
203
+ dirtyBit: slot.dirtyBit,
204
+ offset: slot.offset,
205
+ init: slot.init,
206
+ cap: slot.cap,
207
+ aux: slot.aux,
208
+ }));
209
+ }
210
+ /** Whether any slot has a dirty bit. */
211
+ get hasDirtyBits() {
212
+ return this.dirtyBits > 0;
213
+ }
214
+ /**
215
+ * The arena's regions, with the register stack after the slots, then the tape scratch, and the event queue.
216
+ *
217
+ * @param stackCells Cells the deepest function needs, frame included.
218
+ *
219
+ * @returns The regions and the arena's size.
220
+ */
221
+ regions(stackCells) {
222
+ if (!this.laidOut) {
223
+ throw new Error('AOT internal: the arena was not laid out');
224
+ }
225
+ const end = this.laidOut.reduce((last, region) => Math.max(last, region.offset + region.bytes), 0);
226
+ const cells = Math.max(stackCells, FRAME_CELLS);
227
+ const regions = [
228
+ ...this.laidOut,
229
+ { kind: Region.stack, count: cells, offset: end, bytes: cells * CELL_SIZE },
230
+ ];
231
+ let bytes = end + cells * CELL_SIZE;
232
+ if (this.tapeFloats) {
233
+ regions.push({
234
+ kind: Region.tape,
235
+ count: this.tapeFloats,
236
+ offset: bytes,
237
+ bytes: this.tapeFloats * TAPE_FLOAT_SIZE,
238
+ });
239
+ bytes += this.tapeFloats * TAPE_FLOAT_SIZE;
240
+ }
241
+ if (this.events) {
242
+ regions.push({
243
+ kind: Region.events,
244
+ count: this.events,
245
+ offset: bytes,
246
+ bytes: this.events * EVENT_SIZE,
247
+ });
248
+ bytes += this.events * EVENT_SIZE;
249
+ }
250
+ return { regions, bytes };
251
+ }
252
+ }
@@ -0,0 +1,177 @@
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
+ * A package's file assembly: the program section from the IR, the container around it, and the
18
+ * resource report the build prints.
19
+ */
20
+ /*----------------------------------------------------------------------------------------------------------------------
21
+ - Imports
22
+ ---------------------------------------------------------------------------------------------------------------------*/
23
+ import { SECTION_ASSET_PACK, SECTION_DEBUG, SECTION_PROGRAM, emitPackageContainer, } from '../../../assets/emit-container.mjs';
24
+ import { lowerApp } from '../lower.mjs';
25
+ import { FLAG_DEBUG, Resource, Table } from './format.mjs';
26
+ import { lowerPackage } from './lower.mjs';
27
+ import { writeDebug, writeProgram } from './writer.mjs';
28
+ /*----------------------------------------------------------------------------------------------------------------------
29
+ - Constants
30
+ ---------------------------------------------------------------------------------------------------------------------*/
31
+ const SECTION_NAMES = {
32
+ [SECTION_ASSET_PACK]: 'assets',
33
+ [SECTION_PROGRAM]: 'program',
34
+ [SECTION_DEBUG]: 'debug',
35
+ };
36
+ const TABLE_NAMES = Object.fromEntries(Object.entries(Table).map(([name, id]) => [id, name]));
37
+ /** The words app.erpkg.c puts on each line. */
38
+ const WORDS_PER_LINE = 8;
39
+ /*----------------------------------------------------------------------------------------------------------------------
40
+ - Implementation
41
+ ---------------------------------------------------------------------------------------------------------------------*/
42
+ /**
43
+ * Compiles a Flow B app's source to one panel's program. Pure; compilePackage
44
+ * (compile.mts) is the public entry around it.
45
+ *
46
+ * @param source The App.jsx/App.tsx source text.
47
+ * @param options compilePackage's options, resolved (see resolveOptions), with the panel as the screen.
48
+ * @param wide State/ref slots to declare 64-bit.
49
+ * @param stores Collects every value stored into an int slot.
50
+ *
51
+ * @returns The panel's program, and its images.
52
+ */
53
+ export function compilePanel(source, options, wide, stores) {
54
+ const app = lowerApp(source, options, wide, stores);
55
+ return {
56
+ program: lowerPackage(app, options),
57
+ images: app.images,
58
+ assumptions: app.boot.assumptions,
59
+ };
60
+ }
61
+ /**
62
+ * Writes the program section for every panel, and its debug section. The asset pack holds each image any
63
+ * panel shows, so each panel needs room to register all of them.
64
+ *
65
+ * @param panels Each panel's program, in the order the package lists them.
66
+ * @param file The source file, as the debug table names it.
67
+ * @param release Build for release: the debug flag clear.
68
+ *
69
+ * @returns The program section, and what the package holds.
70
+ */
71
+ export function assemblePackage(panels, file, release = false) {
72
+ const images = [
73
+ ...new Map(panels.flatMap(panel => panel.images).map(image => [image.name, image])).values(),
74
+ ];
75
+ const programs = panels.map(({ program }) => ({
76
+ ...program,
77
+ flags: release ? program.flags & ~FLAG_DEBUG : program.flags,
78
+ needs: program.needs.map(need => need.resource === Resource.images
79
+ ? { ...need, amount: images.length }
80
+ : need),
81
+ }));
82
+ const written = writeProgram(programs);
83
+ return {
84
+ program: written.bytes,
85
+ buildId: written.buildId,
86
+ debug: writeDebug(programs, written.buildId, file),
87
+ release,
88
+ panels: programs.map(program => program.panels[0]),
89
+ tables: written.tables,
90
+ images,
91
+ };
92
+ }
93
+ /**
94
+ * Puts a program section, its asset pack and, unless it is a release build, its debug section in a container.
95
+ *
96
+ * @param result The compiled program.
97
+ * @param assetPack The baked asset pack, or nothing when the app has no assets.
98
+ *
99
+ * @returns The package.
100
+ */
101
+ export function packPackage(result, assetPack) {
102
+ const { bytes, sections } = emitPackageContainer({
103
+ program: result.program,
104
+ assetPack,
105
+ debug: result.release ? undefined : result.debug,
106
+ });
107
+ return {
108
+ bytes,
109
+ sections: sections.map(section => ({
110
+ type: section.type,
111
+ bytes: section.bytes,
112
+ })),
113
+ };
114
+ }
115
+ /**
116
+ * The resource report: what each layout needs of the firmware's RAM and nodes, named by a size that picks it, and
117
+ * the flash each section takes.
118
+ *
119
+ * @param result The compiled program.
120
+ * @param file The finished package.
121
+ *
122
+ * @returns The report's lines.
123
+ */
124
+ export function resourceReport(result, file) {
125
+ const panels = result.panels.map(panel => ` layout ${panel.width}x${panel.height}: arena ${panel.arenaBytes} B, ${panel.nodeCount} nodes`);
126
+ const sections = file.sections.map(section => `${SECTION_NAMES[section.type] ?? `section ${section.type}`} ${section.bytes} B`);
127
+ // A table stored once per panel lists each copy's size.
128
+ const tables = result.tables
129
+ .filter(table => table.bytes.some(bytes => bytes > 0))
130
+ .map(table => `${TABLE_NAMES[table.id]} ${table.bytes.join('+')}`);
131
+ return [
132
+ ...panels,
133
+ ` flash: ${file.bytes.length} B (${sections.join(', ')})`,
134
+ ` program tables (B): ${tables.join(', ')}`,
135
+ ];
136
+ }
137
+ /**
138
+ * A package as a C array, for a board with no spare partition to link into its firmware: a const array of words, so it
139
+ * is 4-aligned with any compiler, and its length in bytes.
140
+ *
141
+ * @param bytes The package.
142
+ *
143
+ * @returns app.erpkg.c's text.
144
+ */
145
+ export function bakePackage(bytes) {
146
+ const padded = new Uint8Array(Math.ceil(bytes.length / 4) * 4);
147
+ padded.set(bytes);
148
+ const view = new DataView(padded.buffer);
149
+ const words = Array.from({ length: padded.length / 4 }, (_, index) => `0x${view
150
+ .getUint32(index * 4, true)
151
+ .toString(16)
152
+ .padStart(8, '0')}u`);
153
+ const lines = [];
154
+ for (let index = 0; index < words.length; index += WORDS_PER_LINE) {
155
+ lines.push(` ${words.slice(index, index + WORDS_PER_LINE).join(', ')},`);
156
+ }
157
+ return [
158
+ '/* Generated by `embedded-react build --aot --bake`. Do not edit. */',
159
+ '',
160
+ '/*',
161
+ ' * A Flow B package, linked into the firmware. Load it with',
162
+ ' * extern const uint32_t app_erpkg[];',
163
+ ' * extern const size_t app_erpkg_bytes;',
164
+ ' * erb_load(app_erpkg, app_erpkg_bytes, &config, &error);',
165
+ ' */',
166
+ '',
167
+ '#include <stddef.h>',
168
+ '#include <stdint.h>',
169
+ '',
170
+ `const uint32_t app_erpkg[${words.length}] = {`,
171
+ ...lines,
172
+ '};',
173
+ '',
174
+ `const size_t app_erpkg_bytes = ${bytes.length}u;`,
175
+ '',
176
+ ].join('\n');
177
+ }