embedded-react 0.16.0 → 0.18.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.
- package/aot/compiler/animations.mjs +102 -34
- package/aot/compiler/assemble.mjs +169 -31
- package/aot/compiler/boot.mjs +441 -0
- package/aot/compiler/branch-merge.mjs +315 -0
- package/aot/compiler/c/animations.mjs +90 -16
- package/aot/compiler/c/boot.mjs +127 -0
- package/aot/compiler/c/expressions.mjs +63 -8
- package/aot/compiler/c/nodes.mjs +34 -13
- package/aot/compiler/c/statements.mjs +92 -15
- package/aot/compiler/check.mjs +32 -3
- package/aot/compiler/collect.mjs +95 -9
- package/aot/compiler/control-flow.mjs +43 -18
- package/aot/compiler/diagnostics.mjs +33 -0
- package/aot/compiler/elements/dial.mjs +8 -7
- package/aot/compiler/elements/props.mjs +19 -9
- package/aot/compiler/elements/text-input.mjs +7 -2
- package/aot/compiler/expressions.mjs +132 -10
- package/aot/compiler/handlers.mjs +62 -24
- package/aot/compiler/ir/boot.mjs +16 -0
- package/aot/compiler/ir/nodes.mjs +28 -4
- package/aot/compiler/ir/reads.mjs +351 -0
- package/aot/compiler/keyboard.mjs +35 -21
- package/aot/compiler/lower.mjs +40 -11
- package/aot/compiler/nodes.mjs +22 -9
- package/aot/compiler/options.mjs +63 -12
- package/aot/compiler/static-eval.mjs +314 -56
- package/aot/compiler/style-text.mjs +201 -19
- package/aot/compiler/svg.mjs +7 -3
- package/aot/style-map.mjs +9 -11
- package/assets/bake-svg.mjs +3 -2
- package/package.json +1 -1
- package/sim/embedded-react.wasm +0 -0
- package/src/embedded-react/Animated.js +41 -0
- package/src/embedded-react/Button.js +3 -4
- package/src/embedded-react/Pressable.js +59 -0
- package/src/embedded-react/TouchableOpacity.js +6 -9
- package/src/embedded-react/index.d.ts +118 -30
- package/src/embedded-react/index.js +1 -1
- package/src/embedded-react/named-colors.js +37 -0
- package/src/embedded-react/svg-ops.js +13 -16
- package/src/host-config.js +50 -8
|
@@ -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
|
+
- Imports
|
|
18
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
19
|
+
import { cleanJsxText } from './style-text.mjs';
|
|
20
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
21
|
+
- Constants
|
|
22
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
23
|
+
/** Node fields that say where a node is or how it was written, not what it is. */
|
|
24
|
+
const POSITION_KEYS = new Set([
|
|
25
|
+
'loc',
|
|
26
|
+
'start',
|
|
27
|
+
'end',
|
|
28
|
+
'range',
|
|
29
|
+
'extra',
|
|
30
|
+
'leadingComments',
|
|
31
|
+
'trailingComments',
|
|
32
|
+
'innerComments',
|
|
33
|
+
]);
|
|
34
|
+
/** Node types a merged value may not contain: their meaning is not a value boot can pick. */
|
|
35
|
+
const UNMERGEABLE_TYPES = new Set([
|
|
36
|
+
'JSXElement',
|
|
37
|
+
'JSXFragment',
|
|
38
|
+
'ArrowFunctionExpression',
|
|
39
|
+
'FunctionExpression',
|
|
40
|
+
'CallExpression',
|
|
41
|
+
'NewExpression',
|
|
42
|
+
'AssignmentExpression',
|
|
43
|
+
'UpdateExpression',
|
|
44
|
+
]);
|
|
45
|
+
/** The Math functions the compiler lowers, and how many arguments each takes. */
|
|
46
|
+
const MATH_ARITY = {
|
|
47
|
+
sin: 1,
|
|
48
|
+
cos: 1,
|
|
49
|
+
tan: 1,
|
|
50
|
+
sqrt: 1,
|
|
51
|
+
abs: 1,
|
|
52
|
+
round: 1,
|
|
53
|
+
floor: 1,
|
|
54
|
+
ceil: 1,
|
|
55
|
+
trunc: 1,
|
|
56
|
+
min: 2,
|
|
57
|
+
max: 2,
|
|
58
|
+
atan2: 2,
|
|
59
|
+
pow: 2,
|
|
60
|
+
};
|
|
61
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
62
|
+
- Implementation
|
|
63
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
64
|
+
/**
|
|
65
|
+
* Whether two nodes are the same code, wherever each was written.
|
|
66
|
+
*
|
|
67
|
+
* @param first One node.
|
|
68
|
+
* @param second The other.
|
|
69
|
+
*
|
|
70
|
+
* @returns True when they are.
|
|
71
|
+
*/
|
|
72
|
+
export function sameCode(first, second) {
|
|
73
|
+
if (first === second)
|
|
74
|
+
return true;
|
|
75
|
+
if (typeof first !== 'object' ||
|
|
76
|
+
typeof second !== 'object' ||
|
|
77
|
+
first === null ||
|
|
78
|
+
second === null) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
if (Array.isArray(first) !== Array.isArray(second))
|
|
82
|
+
return false;
|
|
83
|
+
const keysOf = (node) => Object.keys(node).filter(key => !POSITION_KEYS.has(key) &&
|
|
84
|
+
node[key] !== undefined);
|
|
85
|
+
const firstKeys = keysOf(first);
|
|
86
|
+
const secondKeys = keysOf(second);
|
|
87
|
+
return (firstKeys.length === secondKeys.length &&
|
|
88
|
+
firstKeys.every(key => sameCode(first[key], second[key])));
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Whether a node holds code a merged value cannot pick between: an element, a function, or a call (other than
|
|
92
|
+
* Math's, which boot works out).
|
|
93
|
+
*
|
|
94
|
+
* @param node The node.
|
|
95
|
+
*
|
|
96
|
+
* @returns True when it does.
|
|
97
|
+
*/
|
|
98
|
+
function holdsUnmergeable(node) {
|
|
99
|
+
if (typeof node !== 'object' || node === null)
|
|
100
|
+
return false;
|
|
101
|
+
if (Array.isArray(node))
|
|
102
|
+
return node.some(holdsUnmergeable);
|
|
103
|
+
const typed = node;
|
|
104
|
+
if (typed.type === 'CallExpression' && isLowerableMath(typed)) {
|
|
105
|
+
return typed.arguments.some(holdsUnmergeable);
|
|
106
|
+
}
|
|
107
|
+
if (typed.type && UNMERGEABLE_TYPES.has(typed.type))
|
|
108
|
+
return true;
|
|
109
|
+
return Object.keys(typed).some(key => !POSITION_KEYS.has(key) && holdsUnmergeable(typed[key]));
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Whether a node is a `Math.<fn>(…)` call the compiler lowers, with the number of arguments it takes.
|
|
113
|
+
*
|
|
114
|
+
* @param node The node.
|
|
115
|
+
*
|
|
116
|
+
* @returns True when it is.
|
|
117
|
+
*/
|
|
118
|
+
function isLowerableMath(node) {
|
|
119
|
+
if (node.type !== 'CallExpression' ||
|
|
120
|
+
node.callee.type !== 'MemberExpression' ||
|
|
121
|
+
node.callee.computed ||
|
|
122
|
+
node.callee.object.type !== 'Identifier' ||
|
|
123
|
+
node.callee.object.name !== 'Math' ||
|
|
124
|
+
node.callee.property.type !== 'Identifier' ||
|
|
125
|
+
node.arguments.some(arg => arg.type === 'SpreadElement')) {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
const fn = node.callee.property.name;
|
|
129
|
+
return (Object.hasOwn(MATH_ARITY, fn) && MATH_ARITY[fn] === node.arguments.length);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* `test ? whenTrue : whenFalse`, entry by entry where both are object or array literals of the same shape, so
|
|
133
|
+
* an entry that reads state stays an expression of its own.
|
|
134
|
+
*
|
|
135
|
+
* @param test The test.
|
|
136
|
+
* @param whenTrue The value when it holds.
|
|
137
|
+
* @param whenFalse The value when it does not.
|
|
138
|
+
*
|
|
139
|
+
* @returns The merged value.
|
|
140
|
+
*/
|
|
141
|
+
function mergeValues(test, whenTrue, whenFalse) {
|
|
142
|
+
if (sameCode(whenTrue, whenFalse))
|
|
143
|
+
return whenTrue;
|
|
144
|
+
// Two objects with the same keys in the same order merge entry by entry.
|
|
145
|
+
if (whenTrue.type === 'ObjectExpression' &&
|
|
146
|
+
whenFalse.type === 'ObjectExpression' &&
|
|
147
|
+
whenTrue.properties.length === whenFalse.properties.length &&
|
|
148
|
+
whenTrue.properties.every((property, index) => {
|
|
149
|
+
const other = whenFalse.properties[index];
|
|
150
|
+
return (property.type === 'ObjectProperty' &&
|
|
151
|
+
other.type === 'ObjectProperty' &&
|
|
152
|
+
!property.computed &&
|
|
153
|
+
!other.computed &&
|
|
154
|
+
sameCode(property.key, other.key));
|
|
155
|
+
})) {
|
|
156
|
+
return {
|
|
157
|
+
...whenTrue,
|
|
158
|
+
properties: whenTrue.properties.map((property, index) => ({
|
|
159
|
+
...property,
|
|
160
|
+
value: mergeValues(test, property.value, whenFalse.properties[index]
|
|
161
|
+
.value),
|
|
162
|
+
})),
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
// Two arrays of the same length merge element by element.
|
|
166
|
+
if (whenTrue.type === 'ArrayExpression' &&
|
|
167
|
+
whenFalse.type === 'ArrayExpression' &&
|
|
168
|
+
whenTrue.elements.length === whenFalse.elements.length &&
|
|
169
|
+
[...whenTrue.elements, ...whenFalse.elements].every(element => element && element.type !== 'SpreadElement')) {
|
|
170
|
+
return {
|
|
171
|
+
...whenTrue,
|
|
172
|
+
elements: whenTrue.elements.map((element, index) => mergeValues(test, element, whenFalse.elements[index])),
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
return {
|
|
176
|
+
type: 'ConditionalExpression',
|
|
177
|
+
test,
|
|
178
|
+
consequent: whenTrue,
|
|
179
|
+
alternate: whenFalse,
|
|
180
|
+
loc: whenTrue.loc,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* An attribute's value as an expression, or null for a bare attribute (`<Switch disabled />`).
|
|
185
|
+
*
|
|
186
|
+
* @param attribute The attribute.
|
|
187
|
+
*
|
|
188
|
+
* @returns The value.
|
|
189
|
+
*/
|
|
190
|
+
function attributeValue(attribute) {
|
|
191
|
+
const { value } = attribute;
|
|
192
|
+
if (!value)
|
|
193
|
+
return null;
|
|
194
|
+
if (value.type === 'JSXExpressionContainer') {
|
|
195
|
+
return value.expression.type === 'JSXEmptyExpression'
|
|
196
|
+
? null
|
|
197
|
+
: value.expression;
|
|
198
|
+
}
|
|
199
|
+
return value;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Two attributes merged, or null when they differ in a way boot cannot pick: their names, or a value that is a
|
|
203
|
+
* handler, a ref, a key, an element or a call.
|
|
204
|
+
*
|
|
205
|
+
* @param test The test.
|
|
206
|
+
* @param whenTrue The attribute when it holds.
|
|
207
|
+
* @param whenFalse The attribute when it does not.
|
|
208
|
+
*
|
|
209
|
+
* @returns The merged attribute, or null.
|
|
210
|
+
*/
|
|
211
|
+
function mergeAttributes(test, whenTrue, whenFalse) {
|
|
212
|
+
if (sameCode(whenTrue, whenFalse))
|
|
213
|
+
return whenTrue;
|
|
214
|
+
if (whenTrue.type !== 'JSXAttribute' ||
|
|
215
|
+
whenFalse.type !== 'JSXAttribute' ||
|
|
216
|
+
!sameCode(whenTrue.name, whenFalse.name)) {
|
|
217
|
+
return null;
|
|
218
|
+
}
|
|
219
|
+
const name = whenTrue.name.type === 'JSXIdentifier' ? whenTrue.name.name : '';
|
|
220
|
+
const trueValue = attributeValue(whenTrue);
|
|
221
|
+
const falseValue = attributeValue(whenFalse);
|
|
222
|
+
if (name === 'key' ||
|
|
223
|
+
name === 'ref' ||
|
|
224
|
+
/^on[A-Z]/.test(name) ||
|
|
225
|
+
!trueValue ||
|
|
226
|
+
!falseValue ||
|
|
227
|
+
holdsUnmergeable(trueValue) ||
|
|
228
|
+
holdsUnmergeable(falseValue)) {
|
|
229
|
+
return null;
|
|
230
|
+
}
|
|
231
|
+
return {
|
|
232
|
+
...whenTrue,
|
|
233
|
+
value: {
|
|
234
|
+
type: 'JSXExpressionContainer',
|
|
235
|
+
expression: mergeValues(test, trueValue, falseValue),
|
|
236
|
+
loc: whenTrue.value?.loc,
|
|
237
|
+
},
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* An element's children, less the empty `{}` a JSX comment leaves.
|
|
242
|
+
*
|
|
243
|
+
* @param element The element.
|
|
244
|
+
*
|
|
245
|
+
* @returns The children.
|
|
246
|
+
*/
|
|
247
|
+
const childrenOf = (element) => element.children.filter(child => !(child.type === 'JSXExpressionContainer' &&
|
|
248
|
+
child.expression.type === 'JSXEmptyExpression'));
|
|
249
|
+
/**
|
|
250
|
+
* Two branches of a test that depends on the panel, as one element whose differing values each pick at boot,
|
|
251
|
+
* or null when the branches differ in shape: their elements, attributes, children, handlers or text. A branch
|
|
252
|
+
* that changes only values then needs no per-panel variant.
|
|
253
|
+
*
|
|
254
|
+
* @param test The test.
|
|
255
|
+
* @param whenTrue The element when it holds.
|
|
256
|
+
* @param whenFalse The element when it does not.
|
|
257
|
+
*
|
|
258
|
+
* @returns The merged element, or null.
|
|
259
|
+
*/
|
|
260
|
+
export function mergeBranchElements(test, whenTrue, whenFalse) {
|
|
261
|
+
if (sameCode(whenTrue, whenFalse))
|
|
262
|
+
return whenTrue;
|
|
263
|
+
const trueOpening = whenTrue.openingElement;
|
|
264
|
+
const falseOpening = whenFalse.openingElement;
|
|
265
|
+
if (!sameCode(trueOpening.name, falseOpening.name) ||
|
|
266
|
+
trueOpening.attributes.length !== falseOpening.attributes.length) {
|
|
267
|
+
return null;
|
|
268
|
+
}
|
|
269
|
+
// Attributes pair up in order, since a later style or spread wins over an earlier one.
|
|
270
|
+
const attributes = [];
|
|
271
|
+
for (const [index, attribute] of trueOpening.attributes.entries()) {
|
|
272
|
+
const merged = mergeAttributes(test, attribute, falseOpening.attributes[index]);
|
|
273
|
+
if (!merged)
|
|
274
|
+
return null;
|
|
275
|
+
attributes.push(merged);
|
|
276
|
+
}
|
|
277
|
+
// Children pair up too: elements merge in turn, and a differing {value} picks at boot.
|
|
278
|
+
const trueChildren = childrenOf(whenTrue);
|
|
279
|
+
const falseChildren = childrenOf(whenFalse);
|
|
280
|
+
if (trueChildren.length !== falseChildren.length)
|
|
281
|
+
return null;
|
|
282
|
+
const children = [];
|
|
283
|
+
for (const [index, child] of trueChildren.entries()) {
|
|
284
|
+
const other = falseChildren[index];
|
|
285
|
+
if (sameCode(child, other) ||
|
|
286
|
+
(child.type === 'JSXText' &&
|
|
287
|
+
other.type === 'JSXText' &&
|
|
288
|
+
cleanJsxText(child.value) === cleanJsxText(other.value))) {
|
|
289
|
+
children.push(child);
|
|
290
|
+
}
|
|
291
|
+
else if (child.type === 'JSXElement' && other.type === 'JSXElement') {
|
|
292
|
+
const merged = mergeBranchElements(test, child, other);
|
|
293
|
+
if (!merged)
|
|
294
|
+
return null;
|
|
295
|
+
children.push(merged);
|
|
296
|
+
}
|
|
297
|
+
else if (child.type === 'JSXExpressionContainer' &&
|
|
298
|
+
other.type === 'JSXExpressionContainer' &&
|
|
299
|
+
!holdsUnmergeable(child.expression) &&
|
|
300
|
+
!holdsUnmergeable(other.expression)) {
|
|
301
|
+
children.push({
|
|
302
|
+
...child,
|
|
303
|
+
expression: mergeValues(test, child.expression, other.expression),
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
else {
|
|
307
|
+
return null;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
return {
|
|
311
|
+
...whenTrue,
|
|
312
|
+
openingElement: { ...trueOpening, attributes },
|
|
313
|
+
children,
|
|
314
|
+
};
|
|
315
|
+
}
|
|
@@ -28,10 +28,11 @@ import { printExpr } from './expressions.mjs';
|
|
|
28
28
|
* @param animation The animation.
|
|
29
29
|
* @param configName The config's name, unique within the function.
|
|
30
30
|
* @param onComplete The C function to run when it completes, or null.
|
|
31
|
+
* @param userData What the engine passes that function as user_data, or null.
|
|
31
32
|
*
|
|
32
33
|
* @returns The C block.
|
|
33
34
|
*/
|
|
34
|
-
export function printAnimation(animation, configName, onComplete) {
|
|
35
|
+
export function printAnimation(animation, configName, onComplete, userData = null) {
|
|
35
36
|
const set = (field, value) => ` ${configName}.${field} = ${value};`;
|
|
36
37
|
const lines = [
|
|
37
38
|
' {',
|
|
@@ -63,6 +64,9 @@ export function printAnimation(animation, configName, onComplete) {
|
|
|
63
64
|
if (onComplete) {
|
|
64
65
|
lines.push(set('on_complete', onComplete));
|
|
65
66
|
}
|
|
67
|
+
if (userData) {
|
|
68
|
+
lines.push(set('on_complete_user_data', userData));
|
|
69
|
+
}
|
|
66
70
|
// Decay with no target is given the value it holds now.
|
|
67
71
|
const to = animation.to
|
|
68
72
|
? printExpr(animation.to)
|
|
@@ -104,25 +108,28 @@ export function printWaitDeclarations(callers) {
|
|
|
104
108
|
#endif
|
|
105
109
|
|
|
106
110
|
/* The wait between a run's last animation and its completion: the composition it belongs to (which a stop
|
|
107
|
-
names), the time left and the completion it runs. */
|
|
111
|
+
names), the time left, the animations of a joined run still running, and the completion it runs. */
|
|
108
112
|
typedef struct
|
|
109
113
|
{
|
|
110
114
|
int remaining_ms;
|
|
111
115
|
int owner;
|
|
112
116
|
ERAnimCompleteFn done;
|
|
113
117
|
uint8_t state;
|
|
118
|
+
uint8_t parts;
|
|
114
119
|
} ErAnimWait;
|
|
115
120
|
enum
|
|
116
121
|
{
|
|
117
122
|
APP_WAIT_FREE,
|
|
118
123
|
APP_WAIT_PENDING,
|
|
119
124
|
APP_WAIT_DUE,
|
|
120
|
-
APP_WAIT_STOPPING
|
|
125
|
+
APP_WAIT_STOPPING,
|
|
126
|
+
/* A joined run that has completed, kept until its animations still running have reported. */
|
|
127
|
+
APP_WAIT_DRAINING
|
|
121
128
|
};
|
|
122
129
|
static ErAnimWait s_waits[ER_AOT_MAX_WAITS];
|
|
123
130
|
|
|
124
|
-
/*
|
|
125
|
-
static
|
|
131
|
+
/* Takes a free wait for one run; runs never share one. */
|
|
132
|
+
static int app_wait_claim(int owner, int ms, int parts, ERAnimCompleteFn done)
|
|
126
133
|
{
|
|
127
134
|
for (int i = 0; i < ER_AOT_MAX_WAITS; i++)
|
|
128
135
|
{
|
|
@@ -131,13 +138,15 @@ static void app_wait_start(int owner, int ms, ERAnimCompleteFn done)
|
|
|
131
138
|
s_waits[i].remaining_ms = ms;
|
|
132
139
|
s_waits[i].owner = owner;
|
|
133
140
|
s_waits[i].done = done;
|
|
141
|
+
s_waits[i].parts = (uint8_t)parts;
|
|
134
142
|
s_waits[i].state = APP_WAIT_PENDING;
|
|
135
|
-
return;
|
|
143
|
+
return i;
|
|
136
144
|
}
|
|
137
145
|
}
|
|
138
146
|
|
|
139
|
-
/* The table is full
|
|
140
|
-
|
|
147
|
+
/* The table is full: the run cannot wait, so it ends now, as not finished. A run that completion starts
|
|
148
|
+
while the table is still full is dropped instead of recursing. */
|
|
149
|
+
APP_LIMIT("wait table full, raise ER_AOT_MAX_WAITS");
|
|
141
150
|
static bool s_reporting_full;
|
|
142
151
|
if (!s_reporting_full)
|
|
143
152
|
{
|
|
@@ -145,21 +154,22 @@ static void app_wait_start(int owner, int ms, ERAnimCompleteFn done)
|
|
|
145
154
|
done(false, NULL);
|
|
146
155
|
s_reporting_full = false;
|
|
147
156
|
}
|
|
157
|
+
return -1;
|
|
148
158
|
}
|
|
149
159
|
|
|
150
|
-
/* Counts down every wait, then completes the ones that ran out. A
|
|
151
|
-
full time. */
|
|
160
|
+
/* Counts down every wait, then completes the ones that ran out. A joined run that runs out first holds until
|
|
161
|
+
its animations finish. A wait a completion starts here waits its full time. */
|
|
152
162
|
static void app_wait_tick(int dt_ms)
|
|
153
163
|
{
|
|
154
164
|
for (int i = 0; i < ER_AOT_MAX_WAITS; i++)
|
|
155
165
|
{
|
|
156
|
-
if (s_waits[i].state == APP_WAIT_PENDING)
|
|
166
|
+
if (s_waits[i].state == APP_WAIT_PENDING && s_waits[i].remaining_ms > 0)
|
|
157
167
|
{
|
|
158
168
|
s_waits[i].remaining_ms -= dt_ms;
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
169
|
+
}
|
|
170
|
+
if (s_waits[i].state == APP_WAIT_PENDING && s_waits[i].remaining_ms <= 0 && s_waits[i].parts == 0)
|
|
171
|
+
{
|
|
172
|
+
s_waits[i].state = APP_WAIT_DUE;
|
|
163
173
|
}
|
|
164
174
|
}
|
|
165
175
|
for (int i = 0; i < ER_AOT_MAX_WAITS; i++)
|
|
@@ -172,6 +182,60 @@ static void app_wait_tick(int dt_ms)
|
|
|
172
182
|
}
|
|
173
183
|
}`,
|
|
174
184
|
];
|
|
185
|
+
if (callers.includes('app_wait_start(')) {
|
|
186
|
+
blocks.push(`/* Starts the wait between a run's last animation and its completion. */
|
|
187
|
+
static void app_wait_start(int owner, int ms, ERAnimCompleteFn done)
|
|
188
|
+
{
|
|
189
|
+
(void)app_wait_claim(owner, ms, 0, done);
|
|
190
|
+
}`);
|
|
191
|
+
}
|
|
192
|
+
if (callers.includes('app_wait_join(')) {
|
|
193
|
+
blocks.push(`/* Starts a run that waits for its animations as well as its delay: it completes as finished once ms have passed
|
|
194
|
+
and all of its parts (animations) have finished, and as not finished as soon as one is interrupted. Each
|
|
195
|
+
animation runs app_wait_part when it ends, with what this returns as its user_data (NULL: the table was full,
|
|
196
|
+
so the run has already ended). */
|
|
197
|
+
static void* app_wait_join(int owner, int ms, int parts, ERAnimCompleteFn done)
|
|
198
|
+
{
|
|
199
|
+
const int i = app_wait_claim(owner, ms, parts, done);
|
|
200
|
+
return i < 0 ? NULL : (void*)(intptr_t)(i + 1);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/* An animation of a joined run has ended. */
|
|
204
|
+
static void app_wait_part(bool finished, void* user_data)
|
|
205
|
+
{
|
|
206
|
+
if (!user_data)
|
|
207
|
+
{
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
ErAnimWait* wait = &s_waits[(intptr_t)user_data - 1];
|
|
212
|
+
wait->parts--;
|
|
213
|
+
if (wait->state == APP_WAIT_DRAINING)
|
|
214
|
+
{
|
|
215
|
+
if (wait->parts == 0)
|
|
216
|
+
{
|
|
217
|
+
wait->state = APP_WAIT_FREE;
|
|
218
|
+
}
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/* A stop in progress reports the run itself (see app_wait_report). */
|
|
223
|
+
if (wait->state != APP_WAIT_PENDING)
|
|
224
|
+
{
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
if (!finished)
|
|
228
|
+
{
|
|
229
|
+
wait->state = wait->parts ? APP_WAIT_DRAINING : APP_WAIT_FREE;
|
|
230
|
+
wait->done(false, NULL);
|
|
231
|
+
}
|
|
232
|
+
else if (wait->parts == 0 && wait->remaining_ms <= 0)
|
|
233
|
+
{
|
|
234
|
+
wait->state = APP_WAIT_FREE;
|
|
235
|
+
wait->done(true, NULL);
|
|
236
|
+
}
|
|
237
|
+
}`);
|
|
238
|
+
}
|
|
175
239
|
if (callers.includes('app_wait_detach(')) {
|
|
176
240
|
blocks.push(`/* A stop's two halves: detach the composition's waits, then report them as not finished. Anything a
|
|
177
241
|
completion in between starts is a new run, which the stop leaves alone. */
|
|
@@ -193,7 +257,7 @@ static void app_wait_report(int owner)
|
|
|
193
257
|
{
|
|
194
258
|
if (s_waits[i].owner == owner && s_waits[i].state == APP_WAIT_STOPPING)
|
|
195
259
|
{
|
|
196
|
-
s_waits[i].state = APP_WAIT_FREE;
|
|
260
|
+
s_waits[i].state = s_waits[i].parts ? APP_WAIT_DRAINING : APP_WAIT_FREE;
|
|
197
261
|
s_waits[i].done(false, NULL);
|
|
198
262
|
}
|
|
199
263
|
}
|
|
@@ -335,6 +399,16 @@ export function printAnimationStart(start) {
|
|
|
335
399
|
if (!start.animations.length) {
|
|
336
400
|
return start.completion ? [` (void)${start.completion.name};`] : [];
|
|
337
401
|
}
|
|
402
|
+
// A joined run takes its wait first, and every animation reports to it.
|
|
403
|
+
const join = start.completionJoin;
|
|
404
|
+
if (join && start.completion) {
|
|
405
|
+
return [
|
|
406
|
+
' {',
|
|
407
|
+
` void* join = app_wait_join(${join.owner}, ${join.delayMs}, ${start.animations.length}, ${start.completion.name});`,
|
|
408
|
+
...start.animations.flatMap((animation, index) => printAnimation(animation, `cfg${start.firstConfig + index}`, 'app_wait_part', 'join').map(line => ` ${line}`)),
|
|
409
|
+
' }',
|
|
410
|
+
];
|
|
411
|
+
}
|
|
338
412
|
// The completion (or its wait) goes on the last animation, the only one when there is a completion.
|
|
339
413
|
const last = start.animations.length - 1;
|
|
340
414
|
const onComplete = start.completionWait?.callback ?? start.completion?.name ?? null;
|
|
@@ -0,0 +1,127 @@
|
|
|
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 { doubleLit, printExpr, printInDoubles } from './expressions.mjs';
|
|
21
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
22
|
+
- Constants
|
|
23
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
24
|
+
/**
|
|
25
|
+
* How far a fraction a guard reads may stray from the build's, relative to its size. Boot works in doubles as JS
|
|
26
|
+
* does, so only a libm function's last digit can differ; any other panel's value is far outside this.
|
|
27
|
+
*/
|
|
28
|
+
const FLOAT_TOLERANCE = 1e-9;
|
|
29
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
30
|
+
- Implementation
|
|
31
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
32
|
+
/**
|
|
33
|
+
* Whether an app works anything out at boot or assumes anything of its panel, so it has an app_boot().
|
|
34
|
+
*
|
|
35
|
+
* @param boot The app's boot values.
|
|
36
|
+
*
|
|
37
|
+
* @returns True when it does.
|
|
38
|
+
*/
|
|
39
|
+
export const hasBoot = (boot) => boot.slots.length > 0 || boot.assumptions.length > 0;
|
|
40
|
+
/**
|
|
41
|
+
* A slot's C declaration in the ErAppBoot struct. A fraction is a double, as JS holds it.
|
|
42
|
+
*
|
|
43
|
+
* @param slot The slot.
|
|
44
|
+
*
|
|
45
|
+
* @returns The C line.
|
|
46
|
+
*/
|
|
47
|
+
function printSlotDecl(slot) {
|
|
48
|
+
const cType = slot.type.kind === 'float'
|
|
49
|
+
? 'double'
|
|
50
|
+
: slot.type.kind === 'string'
|
|
51
|
+
? 'const char*'
|
|
52
|
+
: 'int';
|
|
53
|
+
return ` ${cType} ${slot.field};`;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The file-scope struct of the values er_app_build() works out at boot.
|
|
57
|
+
*
|
|
58
|
+
* @param boot The app's boot values.
|
|
59
|
+
*
|
|
60
|
+
* @returns The C, or '' when there are none.
|
|
61
|
+
*/
|
|
62
|
+
export const printBootDecls = (boot) => boot.slots.length
|
|
63
|
+
? `/* The values that depend on the panel size, worked out once by er_app_build(). */\n` +
|
|
64
|
+
`typedef struct\n{\n${boot.slots.map(printSlotDecl).join('\n')}\n} ErAppBoot;\n\n` +
|
|
65
|
+
`static ErAppBoot s_boot;\n`
|
|
66
|
+
: '';
|
|
67
|
+
/**
|
|
68
|
+
* The C test that a panel meets one assumption.
|
|
69
|
+
*
|
|
70
|
+
* @param assumption The assumption.
|
|
71
|
+
* @param screen The panel the build was compiled for.
|
|
72
|
+
*
|
|
73
|
+
* @returns The C condition.
|
|
74
|
+
*/
|
|
75
|
+
function printAssumption(assumption, screen) {
|
|
76
|
+
if (assumption.kind === 'panel') {
|
|
77
|
+
return `(screen_w == ${screen.width} && screen_h == ${screen.height})`;
|
|
78
|
+
}
|
|
79
|
+
const read = `s_boot.${assumption.slot.field}`;
|
|
80
|
+
const { value } = assumption;
|
|
81
|
+
if (typeof value === 'string')
|
|
82
|
+
return `(strcmp(${read}, ${cstr(value)}) == 0)`;
|
|
83
|
+
if (typeof value === 'boolean')
|
|
84
|
+
return value ? read : `!${read}`;
|
|
85
|
+
if (assumption.slot.type.kind !== 'float') {
|
|
86
|
+
return `(${read} == ${value})`;
|
|
87
|
+
}
|
|
88
|
+
// A fraction within a small margin: boot and the compiler need not round its last digit alike.
|
|
89
|
+
const margin = doubleLit(FLOAT_TOLERANCE * (1 + Math.abs(value)));
|
|
90
|
+
const expected = doubleLit(value);
|
|
91
|
+
return `(${read} - ${expected} <= ${margin} && ${expected} - ${read} <= ${margin})`;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* app_boot(): sets each boot value from the panel size, then reports whether the panel meets everything the
|
|
95
|
+
* build assumed of it.
|
|
96
|
+
*
|
|
97
|
+
* @param boot The app's boot values.
|
|
98
|
+
* @param screen The panel the build was compiled for.
|
|
99
|
+
*
|
|
100
|
+
* @returns The C function, or '' when the app has no boot values.
|
|
101
|
+
*/
|
|
102
|
+
export function printBootFunction(boot, screen) {
|
|
103
|
+
if (!hasBoot(boot))
|
|
104
|
+
return '';
|
|
105
|
+
const lines = boot.slots.map(({ field, init }) => {
|
|
106
|
+
const value = init.kind === 'expr'
|
|
107
|
+
? printInDoubles(() => printExpr(init.expr))
|
|
108
|
+
: init.dimension === 'width'
|
|
109
|
+
? 'screen_w'
|
|
110
|
+
: 'screen_h';
|
|
111
|
+
return ` s_boot.${field} = ${value};`;
|
|
112
|
+
});
|
|
113
|
+
const body = lines.join('\n');
|
|
114
|
+
const reads = (name) => new RegExp(`\\b${name}\\b`).test(body);
|
|
115
|
+
const guards = boot.assumptions.map(assumption => printAssumption(assumption, screen));
|
|
116
|
+
const unused = ['screen_w', 'screen_h']
|
|
117
|
+
.filter(name => !reads(name) && !guards.some(guard => guard.includes(name)))
|
|
118
|
+
.map(name => ` (void)${name};`);
|
|
119
|
+
const verdict = guards.length
|
|
120
|
+
? ` return ${guards.join('\n && ')};`
|
|
121
|
+
: ' return true;';
|
|
122
|
+
return ('/* Works out the values that depend on the panel size, and whether this panel takes the layout the app\n' +
|
|
123
|
+
' was compiled for: what the compiler folded at its panel has to come out the same here. */\n' +
|
|
124
|
+
'static bool app_boot(int screen_w, int screen_h)\n{\n' +
|
|
125
|
+
[...unused, ...lines, verdict].join('\n') +
|
|
126
|
+
'\n}');
|
|
127
|
+
}
|