embedded-react 0.15.0 → 0.17.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 (62) hide show
  1. package/aot/compile.mjs +173 -8272
  2. package/aot/compiler/animations.mjs +1001 -0
  3. package/aot/compiler/assemble.mjs +922 -0
  4. package/aot/compiler/boot.mjs +441 -0
  5. package/aot/compiler/branch-merge.mjs +319 -0
  6. package/aot/compiler/c/animations.mjs +362 -0
  7. package/aot/compiler/c/boot.mjs +127 -0
  8. package/aot/compiler/c/expressions.mjs +270 -0
  9. package/aot/compiler/c/functions.mjs +268 -0
  10. package/aot/compiler/c/keyboard.mjs +96 -0
  11. package/aot/compiler/c/nodes.mjs +294 -0
  12. package/aot/compiler/c/pan-responder.mjs +136 -0
  13. package/aot/compiler/c/statements.mjs +271 -0
  14. package/aot/compiler/c/text.mjs +83 -0
  15. package/aot/compiler/c/vectors.mjs +381 -0
  16. package/aot/compiler/c-syntax.mjs +127 -0
  17. package/aot/compiler/check.mjs +165 -0
  18. package/aot/compiler/collect.mjs +785 -0
  19. package/aot/compiler/control-flow.mjs +696 -0
  20. package/aot/compiler/diagnostics.mjs +137 -0
  21. package/aot/compiler/elements/activity-indicator.mjs +160 -0
  22. package/aot/compiler/elements/dial.mjs +346 -0
  23. package/aot/compiler/elements/flat-list.mjs +139 -0
  24. package/aot/compiler/elements/image.mjs +151 -0
  25. package/aot/compiler/elements/modal.mjs +158 -0
  26. package/aot/compiler/elements/props.mjs +474 -0
  27. package/aot/compiler/elements/switch.mjs +183 -0
  28. package/aot/compiler/elements/text-input.mjs +171 -0
  29. package/aot/compiler/expressions.mjs +1579 -0
  30. package/aot/compiler/handlers.mjs +1072 -0
  31. package/aot/compiler/ir/animations.mjs +16 -0
  32. package/aot/compiler/ir/app.mjs +16 -0
  33. package/aot/compiler/ir/boot.mjs +16 -0
  34. package/aot/compiler/ir/expressions.mjs +16 -0
  35. package/aot/compiler/ir/functions.mjs +16 -0
  36. package/aot/compiler/ir/keyboard.mjs +16 -0
  37. package/aot/compiler/ir/nodes.mjs +107 -0
  38. package/aot/compiler/ir/pan-responder.mjs +16 -0
  39. package/aot/compiler/ir/reads.mjs +351 -0
  40. package/aot/compiler/ir/source.mjs +16 -0
  41. package/aot/compiler/ir/statements.mjs +16 -0
  42. package/aot/compiler/ir/text.mjs +16 -0
  43. package/aot/compiler/ir/types.mjs +185 -0
  44. package/aot/compiler/ir/vectors.mjs +16 -0
  45. package/aot/compiler/keyboard.mjs +152 -0
  46. package/aot/compiler/lower.mjs +311 -0
  47. package/aot/compiler/nodes.mjs +688 -0
  48. package/aot/compiler/options.mjs +55 -0
  49. package/aot/compiler/out.mjs +94 -0
  50. package/aot/compiler/pan-responder.mjs +354 -0
  51. package/aot/compiler/parse.mjs +300 -0
  52. package/aot/compiler/static-eval.mjs +454 -0
  53. package/aot/compiler/style-text.mjs +807 -0
  54. package/aot/compiler/svg.mjs +1167 -0
  55. package/aot/compiler/types.mjs +16 -0
  56. package/aot/screenshot-smoke.mjs +95 -55
  57. package/aot/style-map.mjs +366 -304
  58. package/cli.mjs +5 -8
  59. package/package.json +14 -6
  60. package/sim/embedded-react.wasm +0 -0
  61. package/src/embedded-react/FlatList.js +4 -4
  62. package/src/props.js +1 -1
@@ -0,0 +1,271 @@
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 { cScalarType } from '../c-syntax.mjs';
20
+ import { printAnimationStart } from './animations.mjs';
21
+ import { printUpdateVector } from './vectors.mjs';
22
+ import { printCondition, printExpr } from './expressions.mjs';
23
+ import { formatArgs, printText } from './text.mjs';
24
+ import { slotOf } from '../ir/types.mjs';
25
+ /*----------------------------------------------------------------------------------------------------------------------
26
+ - Constants
27
+ ---------------------------------------------------------------------------------------------------------------------*/
28
+ /** One level of indentation. */
29
+ const INDENT = ' ';
30
+ /** The line that re-applies state-dependent props after a body set state. */
31
+ export const APP_UPDATE_CALL = ' app_update();';
32
+ /** The indent list operations print at, whatever block they sit in. */
33
+ const LIST_INDENT = INDENT;
34
+ /** The file-scope flags that record which state was written since app_update() last ran. */
35
+ export const CHANGED_FLAGS = 's_changed';
36
+ /*----------------------------------------------------------------------------------------------------------------------
37
+ - Implementation
38
+ ---------------------------------------------------------------------------------------------------------------------*/
39
+ /**
40
+ * Matches `<member>` as a whole C lvalue, so `s_state.label` does not also match `s_state.label2`.
41
+ *
42
+ * @param member A C lvalue.
43
+ *
44
+ * @returns A pattern that finds it in the C source.
45
+ */
46
+ const readsMember = (member) => new RegExp(`(^|[^\\w.])${member.replace(/\./g, '\\.')}(?![\\w])`);
47
+ /**
48
+ * The line that flags a state as written, so app_update() sets the nodes that read it again; none when no node
49
+ * reads it.
50
+ *
51
+ * @param state The state.
52
+ * @param indent The line's indentation.
53
+ *
54
+ * @returns The C lines.
55
+ */
56
+ export const printChangedFlag = (state, indent) => state.isWatched ? [`${indent}${CHANGED_FLAGS}.${state.cField} = 1;`] : [];
57
+ /**
58
+ * A timer as the C call that starts it and evaluates to its id. A timestamp or float delay goes through
59
+ * ToInt32 and a floor at 0, as Flow A's setTimeout does.
60
+ *
61
+ * @param timer The timer.
62
+ *
63
+ * @returns The C call.
64
+ */
65
+ function printTimerAdd(timer) {
66
+ const delay = timer.delay;
67
+ const delayMs = !delay
68
+ ? '0'
69
+ : delay.type.kind === 'int64'
70
+ ? `app_delay_ms64(${printExpr(delay)})`
71
+ : delay.type.kind === 'float'
72
+ ? `app_delay_msf(${printExpr(delay)})`
73
+ : printExpr(delay);
74
+ return `er_timer_add((int)(${delayMs}), ${timer.isRepeating ? 'true' : 'false'}, ${timer.fn})`;
75
+ }
76
+ /**
77
+ * A string state write. snprintf's source and destination may not overlap, so a value that reads the slot
78
+ * itself (`setLabel(label + '!')`) is built in a temporary first.
79
+ *
80
+ * @param stmt The write.
81
+ * @param indent The statement's indentation.
82
+ *
83
+ * @returns The C statement (several lines, joined, for a value that reads the slot).
84
+ */
85
+ function printSetStateText(stmt, indent) {
86
+ const member = stmt.state.cMember;
87
+ const format = printText(stmt.text);
88
+ if (format.args.some(formatArg => readsMember(member).test(formatArg))) {
89
+ return [
90
+ `${indent}{`,
91
+ `${indent} char next[sizeof(${member})];`,
92
+ `${indent} snprintf(next, sizeof(next), ${formatArgs(format)});`,
93
+ `${indent} memcpy(${member}, next, strlen(next) + 1);`,
94
+ `${indent}}`,
95
+ ].join('\n');
96
+ }
97
+ return `${indent}snprintf(${member}, sizeof(${member}), ${formatArgs(format)});`;
98
+ }
99
+ /**
100
+ * An appended list item: fill the next free slot's fields and count it, unless the list is full.
101
+ *
102
+ * @param stmt The append.
103
+ *
104
+ * @returns The C lines.
105
+ */
106
+ function printAppendItem(stmt) {
107
+ const { arrayName, countMember, cap } = stmt.list;
108
+ const lines = [
109
+ `${LIST_INDENT}if (${countMember} < ${cap})`,
110
+ `${LIST_INDENT}{`,
111
+ ];
112
+ for (const itemValue of stmt.values) {
113
+ const slot = `${arrayName}[${countMember}].${itemValue.field.key}`;
114
+ if ('text' in itemValue) {
115
+ lines.push(`${LIST_INDENT}${INDENT}snprintf(${slot}, sizeof(${slot}), ${formatArgs(printText(itemValue.text))});`);
116
+ }
117
+ else {
118
+ lines.push(`${LIST_INDENT}${INDENT}${slot} = ${printExpr(itemValue.value)};`);
119
+ }
120
+ }
121
+ lines.push(`${LIST_INDENT}${INDENT}${countMember}++;`, `${LIST_INDENT}}`);
122
+ return lines;
123
+ }
124
+ /**
125
+ * Prints a statement as C lines.
126
+ *
127
+ * @param stmt The statement.
128
+ * @param indent Its indentation.
129
+ *
130
+ * @returns Its C lines.
131
+ */
132
+ function printStmt(stmt, indent) {
133
+ switch (stmt.kind) {
134
+ case 'declareLocal': {
135
+ // A string local is a buffer of its own, so a later write to the state it copied cannot change it.
136
+ const init = stmt.init;
137
+ if (init.type.kind === 'string') {
138
+ const declaration = stmt.isHoisted
139
+ ? []
140
+ : [`${indent}char ${stmt.name}[${stmt.stringCap}];`];
141
+ return [
142
+ ...declaration,
143
+ `${indent}snprintf(${stmt.name}, sizeof(${stmt.name}), "%s", ${printExpr(init)});`,
144
+ ];
145
+ }
146
+ const declaredType = stmt.isHoisted
147
+ ? ''
148
+ : cScalarType(slotOf(init.type)) + ' ';
149
+ return [`${indent}${declaredType}${stmt.name} = ${printExpr(init)};`];
150
+ }
151
+ case 'startTimer': {
152
+ const timerAdd = printTimerAdd(stmt.timer);
153
+ if (!stmt.idLocal) {
154
+ return [`${indent}${timerAdd};`];
155
+ }
156
+ // The id is only read by a later clear*(); `(void)` keeps the compiler quiet when nothing does.
157
+ const { name, isHoisted } = stmt.idLocal;
158
+ return [
159
+ `${indent}${isHoisted ? '' : 'int '}${name} = ${timerAdd};`,
160
+ `${indent}(void)${name};`,
161
+ ];
162
+ }
163
+ case 'clearTimer':
164
+ return [`${indent}er_timer_clear(${printExpr(stmt.id)});`];
165
+ case 'setState':
166
+ return [
167
+ `${indent}${stmt.state.cMember} = ${printExpr(stmt.value)};`,
168
+ ...printChangedFlag(stmt.state, indent),
169
+ ];
170
+ case 'setStateText':
171
+ return [
172
+ printSetStateText(stmt, indent),
173
+ ...printChangedFlag(stmt.state, indent),
174
+ ];
175
+ case 'clearList':
176
+ return [
177
+ `${LIST_INDENT}${stmt.list.countMember} = 0;`,
178
+ ...printChangedFlag(stmt.list, LIST_INDENT),
179
+ ];
180
+ case 'appendItem':
181
+ return [
182
+ ...printAppendItem(stmt),
183
+ ...printChangedFlag(stmt.list, LIST_INDENT),
184
+ ];
185
+ case 'dropLastItem': {
186
+ const count = stmt.list.countMember;
187
+ return [
188
+ `${LIST_INDENT}if (${count} > 0) ${count}--;`,
189
+ ...printChangedFlag(stmt.list, LIST_INDENT),
190
+ ];
191
+ }
192
+ case 'keepFirstItems': {
193
+ const count = stmt.list.countMember;
194
+ return [
195
+ `${LIST_INDENT}${count} = (${count} < ${stmt.count}) ? ${count} : ${stmt.count};`,
196
+ ...printChangedFlag(stmt.list, LIST_INDENT),
197
+ ];
198
+ }
199
+ case 'sliceItems': {
200
+ const count = stmt.list.countMember;
201
+ return [
202
+ `${LIST_INDENT}${count} = app_slice_len(${count}, ${printExpr(stmt.end)});`,
203
+ ...printChangedFlag(stmt.list, LIST_INDENT),
204
+ ];
205
+ }
206
+ case 'writeRef':
207
+ return [`${indent}${stmt.ref.cVar} ${stmt.op} ${printExpr(stmt.value)};`];
208
+ case 'stepRef':
209
+ return [`${indent}${stmt.ref.cVar}${stmt.op};`];
210
+ case 'stopAnimations': {
211
+ const lines = stmt.handles.map(handle => `${indent}er_anim_value_set(${handle}, er_anim_value_get(${handle}));`);
212
+ if (stmt.waitOwner === null) {
213
+ return lines;
214
+ }
215
+ // Waits are detached before a frozen value's completion can start a new run, and reported after it.
216
+ return [
217
+ `${indent}app_wait_detach(${stmt.waitOwner});`,
218
+ ...lines,
219
+ `${indent}app_wait_report(${stmt.waitOwner});`,
220
+ ];
221
+ }
222
+ case 'if': {
223
+ // Each branch is a nested C block.
224
+ const lines = [
225
+ `${indent}if (${printCondition(stmt.test)})`,
226
+ `${indent}{`,
227
+ ...printStmts(stmt.consequent, indent + INDENT),
228
+ `${indent}}`,
229
+ ];
230
+ if (stmt.alternate) {
231
+ lines.push(`${indent}else`, `${indent}{`, ...printStmts(stmt.alternate, indent + INDENT), `${indent}}`);
232
+ }
233
+ return lines;
234
+ }
235
+ case 'return':
236
+ return [`${indent}return;`];
237
+ case 'armCleanup':
238
+ return [`${indent}${stmt.flag} = 1;`];
239
+ case 'startAnimations':
240
+ case 'startChain':
241
+ case 'completeAfter':
242
+ return printAnimationStart(stmt);
243
+ case 'updateVector':
244
+ return printUpdateVector(stmt).map(line => indent + line);
245
+ }
246
+ }
247
+ /**
248
+ * Prints statements as C lines.
249
+ *
250
+ * @param stmts The statements.
251
+ * @param indent Their indentation.
252
+ *
253
+ * @returns Their C lines.
254
+ */
255
+ export function printStmts(stmts, indent) {
256
+ return stmts.flatMap(stmt => printStmt(stmt, indent));
257
+ }
258
+ /**
259
+ * A callback's function body: its statements, then app_update() when it set state.
260
+ *
261
+ * @param callback The callback.
262
+ *
263
+ * @returns The C lines.
264
+ */
265
+ export function printCallback(callback) {
266
+ const lines = printStmts(callback.body, INDENT);
267
+ if (callback.updatesState) {
268
+ lines.push(APP_UPDATE_CALL);
269
+ }
270
+ return lines;
271
+ }
@@ -0,0 +1,83 @@
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 { cstr } from '../c-syntax.mjs';
20
+ import { printExpr } from './expressions.mjs';
21
+ /*----------------------------------------------------------------------------------------------------------------------
22
+ - Implementation
23
+ ---------------------------------------------------------------------------------------------------------------------*/
24
+ /**
25
+ * A value as one printf spec and its argument. `%lld` needs a long long, and int64_t is `long` on 64-bit
26
+ * Linux. A float goes through app_ftoa, which prints Infinity, -Infinity, NaN, and 0 where %g prints inf,
27
+ * -inf, nan and -0, and anything else as %g, into a buffer the call site lends it: one per float, so two
28
+ * floats in one snprintf keep their own.
29
+ *
30
+ * @param value The value.
31
+ *
32
+ * @returns Its printf spec and argument.
33
+ */
34
+ function printfPart(value) {
35
+ const code = printExpr(value);
36
+ if (value.type.kind === 'string') {
37
+ return { spec: '%s', code };
38
+ }
39
+ if (value.type.kind === 'float') {
40
+ return { spec: '%s', code: `app_ftoa((char[16]){0}, ${code})` };
41
+ }
42
+ if (value.type.kind === 'int64') {
43
+ return { spec: '%lld', code: `(long long)(${code})` };
44
+ }
45
+ return { spec: '%d', code };
46
+ }
47
+ /**
48
+ * Prints a text as one printf format and its arguments, for a char-buffer destination.
49
+ *
50
+ * @param text The text.
51
+ *
52
+ * @returns The format, with literal `%` escaped as `%%`, and its arguments.
53
+ */
54
+ export function printText(text) {
55
+ let format = '';
56
+ const args = [];
57
+ for (const part of text.parts) {
58
+ // Literal text joins the format; each runtime value adds a spec and its argument.
59
+ if (part.kind === 'literal') {
60
+ format += part.text.replace(/%/g, '%%');
61
+ continue;
62
+ }
63
+ const { spec, code } = part.kind === 'booleanWord'
64
+ ? {
65
+ spec: '%s',
66
+ code: `((${printExpr(part.value)}) ? "true" : "false")`,
67
+ }
68
+ : printfPart(part.value);
69
+ format += spec;
70
+ args.push(code);
71
+ }
72
+ return { format, args };
73
+ }
74
+ /**
75
+ * Renders a printed text as snprintf's trailing arguments (a constant string keeps its `%s` form).
76
+ *
77
+ * @param result The format and its arguments.
78
+ *
79
+ * @returns The arguments after the buffer and its size, as C.
80
+ */
81
+ export const formatArgs = ({ format, args }) => args.length
82
+ ? `${cstr(format)}, ${args.join(', ')}`
83
+ : `"%s", ${cstr(format.replace(/%%/g, '%'))}`;