embedded-react 0.16.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.
- package/aot/compiler/assemble.mjs +143 -27
- package/aot/compiler/boot.mjs +441 -0
- package/aot/compiler/branch-merge.mjs +319 -0
- package/aot/compiler/c/boot.mjs +127 -0
- package/aot/compiler/c/expressions.mjs +63 -8
- package/aot/compiler/c/nodes.mjs +15 -3
- package/aot/compiler/c/statements.mjs +34 -5
- package/aot/compiler/collect.mjs +50 -8
- package/aot/compiler/control-flow.mjs +19 -6
- package/aot/compiler/elements/dial.mjs +7 -4
- package/aot/compiler/expressions.mjs +104 -4
- 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/lower.mjs +39 -10
- package/aot/compiler/nodes.mjs +6 -1
- package/aot/compiler/static-eval.mjs +280 -54
- package/aot/compiler/style-text.mjs +166 -7
- package/package.json +1 -1
|
@@ -0,0 +1,319 @@
|
|
|
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
|
+
- Constants
|
|
18
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
19
|
+
/** Node fields that say where a node is or how it was written, not what it is. */
|
|
20
|
+
const POSITION_KEYS = new Set([
|
|
21
|
+
'loc',
|
|
22
|
+
'start',
|
|
23
|
+
'end',
|
|
24
|
+
'range',
|
|
25
|
+
'extra',
|
|
26
|
+
'leadingComments',
|
|
27
|
+
'trailingComments',
|
|
28
|
+
'innerComments',
|
|
29
|
+
]);
|
|
30
|
+
/** Node types a merged value may not contain: their meaning is not a value boot can pick. */
|
|
31
|
+
const UNMERGEABLE_TYPES = new Set([
|
|
32
|
+
'JSXElement',
|
|
33
|
+
'JSXFragment',
|
|
34
|
+
'ArrowFunctionExpression',
|
|
35
|
+
'FunctionExpression',
|
|
36
|
+
'CallExpression',
|
|
37
|
+
'NewExpression',
|
|
38
|
+
'AssignmentExpression',
|
|
39
|
+
'UpdateExpression',
|
|
40
|
+
]);
|
|
41
|
+
/** The Math functions the compiler lowers, and how many arguments each takes. */
|
|
42
|
+
const MATH_ARITY = {
|
|
43
|
+
sin: 1,
|
|
44
|
+
cos: 1,
|
|
45
|
+
tan: 1,
|
|
46
|
+
sqrt: 1,
|
|
47
|
+
abs: 1,
|
|
48
|
+
round: 1,
|
|
49
|
+
floor: 1,
|
|
50
|
+
ceil: 1,
|
|
51
|
+
trunc: 1,
|
|
52
|
+
min: 2,
|
|
53
|
+
max: 2,
|
|
54
|
+
atan2: 2,
|
|
55
|
+
pow: 2,
|
|
56
|
+
};
|
|
57
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
58
|
+
- Implementation
|
|
59
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
60
|
+
/**
|
|
61
|
+
* Whether two nodes are the same code, wherever each was written.
|
|
62
|
+
*
|
|
63
|
+
* @param first One node.
|
|
64
|
+
* @param second The other.
|
|
65
|
+
*
|
|
66
|
+
* @returns True when they are.
|
|
67
|
+
*/
|
|
68
|
+
export function sameCode(first, second) {
|
|
69
|
+
if (first === second)
|
|
70
|
+
return true;
|
|
71
|
+
if (typeof first !== 'object' ||
|
|
72
|
+
typeof second !== 'object' ||
|
|
73
|
+
first === null ||
|
|
74
|
+
second === null) {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
if (Array.isArray(first) !== Array.isArray(second))
|
|
78
|
+
return false;
|
|
79
|
+
const keysOf = (node) => Object.keys(node).filter(key => !POSITION_KEYS.has(key) &&
|
|
80
|
+
node[key] !== undefined);
|
|
81
|
+
const firstKeys = keysOf(first);
|
|
82
|
+
const secondKeys = keysOf(second);
|
|
83
|
+
return (firstKeys.length === secondKeys.length &&
|
|
84
|
+
firstKeys.every(key => sameCode(first[key], second[key])));
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Whether a node holds code a merged value cannot pick between: an element, a function, or a call (other than
|
|
88
|
+
* Math's, which boot works out).
|
|
89
|
+
*
|
|
90
|
+
* @param node The node.
|
|
91
|
+
*
|
|
92
|
+
* @returns True when it does.
|
|
93
|
+
*/
|
|
94
|
+
function holdsUnmergeable(node) {
|
|
95
|
+
if (typeof node !== 'object' || node === null)
|
|
96
|
+
return false;
|
|
97
|
+
if (Array.isArray(node))
|
|
98
|
+
return node.some(holdsUnmergeable);
|
|
99
|
+
const typed = node;
|
|
100
|
+
if (typed.type === 'CallExpression' && isLowerableMath(typed)) {
|
|
101
|
+
return typed.arguments.some(holdsUnmergeable);
|
|
102
|
+
}
|
|
103
|
+
if (typed.type && UNMERGEABLE_TYPES.has(typed.type))
|
|
104
|
+
return true;
|
|
105
|
+
return Object.keys(typed).some(key => !POSITION_KEYS.has(key) && holdsUnmergeable(typed[key]));
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Whether a node is a `Math.<fn>(…)` call the compiler lowers, with the number of arguments it takes.
|
|
109
|
+
*
|
|
110
|
+
* @param node The node.
|
|
111
|
+
*
|
|
112
|
+
* @returns True when it is.
|
|
113
|
+
*/
|
|
114
|
+
function isLowerableMath(node) {
|
|
115
|
+
if (node.type !== 'CallExpression' ||
|
|
116
|
+
node.callee.type !== 'MemberExpression' ||
|
|
117
|
+
node.callee.computed ||
|
|
118
|
+
node.callee.object.type !== 'Identifier' ||
|
|
119
|
+
node.callee.object.name !== 'Math' ||
|
|
120
|
+
node.callee.property.type !== 'Identifier' ||
|
|
121
|
+
node.arguments.some(arg => arg.type === 'SpreadElement')) {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
const fn = node.callee.property.name;
|
|
125
|
+
return (Object.hasOwn(MATH_ARITY, fn) && MATH_ARITY[fn] === node.arguments.length);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* `test ? whenTrue : whenFalse`, entry by entry where both are object or array literals of the same shape, so
|
|
129
|
+
* an entry that reads state stays an expression of its own.
|
|
130
|
+
*
|
|
131
|
+
* @param test The test.
|
|
132
|
+
* @param whenTrue The value when it holds.
|
|
133
|
+
* @param whenFalse The value when it does not.
|
|
134
|
+
*
|
|
135
|
+
* @returns The merged value.
|
|
136
|
+
*/
|
|
137
|
+
function mergeValues(test, whenTrue, whenFalse) {
|
|
138
|
+
if (sameCode(whenTrue, whenFalse))
|
|
139
|
+
return whenTrue;
|
|
140
|
+
// Two objects with the same keys in the same order merge entry by entry.
|
|
141
|
+
if (whenTrue.type === 'ObjectExpression' &&
|
|
142
|
+
whenFalse.type === 'ObjectExpression' &&
|
|
143
|
+
whenTrue.properties.length === whenFalse.properties.length &&
|
|
144
|
+
whenTrue.properties.every((property, index) => {
|
|
145
|
+
const other = whenFalse.properties[index];
|
|
146
|
+
return (property.type === 'ObjectProperty' &&
|
|
147
|
+
other.type === 'ObjectProperty' &&
|
|
148
|
+
!property.computed &&
|
|
149
|
+
!other.computed &&
|
|
150
|
+
sameCode(property.key, other.key));
|
|
151
|
+
})) {
|
|
152
|
+
return {
|
|
153
|
+
...whenTrue,
|
|
154
|
+
properties: whenTrue.properties.map((property, index) => ({
|
|
155
|
+
...property,
|
|
156
|
+
value: mergeValues(test, property.value, whenFalse.properties[index]
|
|
157
|
+
.value),
|
|
158
|
+
})),
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
// Two arrays of the same length merge element by element.
|
|
162
|
+
if (whenTrue.type === 'ArrayExpression' &&
|
|
163
|
+
whenFalse.type === 'ArrayExpression' &&
|
|
164
|
+
whenTrue.elements.length === whenFalse.elements.length &&
|
|
165
|
+
[...whenTrue.elements, ...whenFalse.elements].every(element => element && element.type !== 'SpreadElement')) {
|
|
166
|
+
return {
|
|
167
|
+
...whenTrue,
|
|
168
|
+
elements: whenTrue.elements.map((element, index) => mergeValues(test, element, whenFalse.elements[index])),
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
return {
|
|
172
|
+
type: 'ConditionalExpression',
|
|
173
|
+
test,
|
|
174
|
+
consequent: whenTrue,
|
|
175
|
+
alternate: whenFalse,
|
|
176
|
+
loc: whenTrue.loc,
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* An attribute's value as an expression, or null for a bare attribute (`<Switch disabled />`).
|
|
181
|
+
*
|
|
182
|
+
* @param attribute The attribute.
|
|
183
|
+
*
|
|
184
|
+
* @returns The value.
|
|
185
|
+
*/
|
|
186
|
+
function attributeValue(attribute) {
|
|
187
|
+
const { value } = attribute;
|
|
188
|
+
if (!value)
|
|
189
|
+
return null;
|
|
190
|
+
if (value.type === 'JSXExpressionContainer') {
|
|
191
|
+
return value.expression.type === 'JSXEmptyExpression'
|
|
192
|
+
? null
|
|
193
|
+
: value.expression;
|
|
194
|
+
}
|
|
195
|
+
return value;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Two attributes merged, or null when they differ in a way boot cannot pick: their names, or a value that is a
|
|
199
|
+
* handler, a ref, a key, an element or a call.
|
|
200
|
+
*
|
|
201
|
+
* @param test The test.
|
|
202
|
+
* @param whenTrue The attribute when it holds.
|
|
203
|
+
* @param whenFalse The attribute when it does not.
|
|
204
|
+
*
|
|
205
|
+
* @returns The merged attribute, or null.
|
|
206
|
+
*/
|
|
207
|
+
function mergeAttributes(test, whenTrue, whenFalse) {
|
|
208
|
+
if (sameCode(whenTrue, whenFalse))
|
|
209
|
+
return whenTrue;
|
|
210
|
+
if (whenTrue.type !== 'JSXAttribute' ||
|
|
211
|
+
whenFalse.type !== 'JSXAttribute' ||
|
|
212
|
+
!sameCode(whenTrue.name, whenFalse.name)) {
|
|
213
|
+
return null;
|
|
214
|
+
}
|
|
215
|
+
const name = whenTrue.name.type === 'JSXIdentifier' ? whenTrue.name.name : '';
|
|
216
|
+
const trueValue = attributeValue(whenTrue);
|
|
217
|
+
const falseValue = attributeValue(whenFalse);
|
|
218
|
+
if (name === 'key' ||
|
|
219
|
+
name === 'ref' ||
|
|
220
|
+
/^on[A-Z]/.test(name) ||
|
|
221
|
+
!trueValue ||
|
|
222
|
+
!falseValue ||
|
|
223
|
+
holdsUnmergeable(trueValue) ||
|
|
224
|
+
holdsUnmergeable(falseValue)) {
|
|
225
|
+
return null;
|
|
226
|
+
}
|
|
227
|
+
return {
|
|
228
|
+
...whenTrue,
|
|
229
|
+
value: {
|
|
230
|
+
type: 'JSXExpressionContainer',
|
|
231
|
+
expression: mergeValues(test, trueValue, falseValue),
|
|
232
|
+
loc: whenTrue.value?.loc,
|
|
233
|
+
},
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* An element's children, less the empty `{}` a JSX comment leaves.
|
|
238
|
+
*
|
|
239
|
+
* @param element The element.
|
|
240
|
+
*
|
|
241
|
+
* @returns The children.
|
|
242
|
+
*/
|
|
243
|
+
const childrenOf = (element) => element.children.filter(child => !(child.type === 'JSXExpressionContainer' &&
|
|
244
|
+
child.expression.type === 'JSXEmptyExpression'));
|
|
245
|
+
/**
|
|
246
|
+
* JSX text as it renders: text spanning lines collapses its whitespace, as Babel does.
|
|
247
|
+
*
|
|
248
|
+
* @param text The text.
|
|
249
|
+
*
|
|
250
|
+
* @returns The rendered text.
|
|
251
|
+
*/
|
|
252
|
+
const renderedText = (text) => /\n/.test(text.value) ? text.value.replace(/\s+/g, ' ').trim() : text.value;
|
|
253
|
+
/**
|
|
254
|
+
* Two branches of a test that depends on the panel, as one element whose differing values each pick at boot,
|
|
255
|
+
* or null when the branches differ in shape: their elements, attributes, children, handlers or text. A branch
|
|
256
|
+
* that changes only values then needs no per-panel variant.
|
|
257
|
+
*
|
|
258
|
+
* @param test The test.
|
|
259
|
+
* @param whenTrue The element when it holds.
|
|
260
|
+
* @param whenFalse The element when it does not.
|
|
261
|
+
*
|
|
262
|
+
* @returns The merged element, or null.
|
|
263
|
+
*/
|
|
264
|
+
export function mergeBranchElements(test, whenTrue, whenFalse) {
|
|
265
|
+
if (sameCode(whenTrue, whenFalse))
|
|
266
|
+
return whenTrue;
|
|
267
|
+
const trueOpening = whenTrue.openingElement;
|
|
268
|
+
const falseOpening = whenFalse.openingElement;
|
|
269
|
+
if (!sameCode(trueOpening.name, falseOpening.name) ||
|
|
270
|
+
trueOpening.attributes.length !== falseOpening.attributes.length) {
|
|
271
|
+
return null;
|
|
272
|
+
}
|
|
273
|
+
// Attributes pair up in order, since a later style or spread wins over an earlier one.
|
|
274
|
+
const attributes = [];
|
|
275
|
+
for (const [index, attribute] of trueOpening.attributes.entries()) {
|
|
276
|
+
const merged = mergeAttributes(test, attribute, falseOpening.attributes[index]);
|
|
277
|
+
if (!merged)
|
|
278
|
+
return null;
|
|
279
|
+
attributes.push(merged);
|
|
280
|
+
}
|
|
281
|
+
// Children pair up too: elements merge in turn, and a differing {value} picks at boot.
|
|
282
|
+
const trueChildren = childrenOf(whenTrue);
|
|
283
|
+
const falseChildren = childrenOf(whenFalse);
|
|
284
|
+
if (trueChildren.length !== falseChildren.length)
|
|
285
|
+
return null;
|
|
286
|
+
const children = [];
|
|
287
|
+
for (const [index, child] of trueChildren.entries()) {
|
|
288
|
+
const other = falseChildren[index];
|
|
289
|
+
if (sameCode(child, other) ||
|
|
290
|
+
(child.type === 'JSXText' &&
|
|
291
|
+
other.type === 'JSXText' &&
|
|
292
|
+
renderedText(child) === renderedText(other))) {
|
|
293
|
+
children.push(child);
|
|
294
|
+
}
|
|
295
|
+
else if (child.type === 'JSXElement' && other.type === 'JSXElement') {
|
|
296
|
+
const merged = mergeBranchElements(test, child, other);
|
|
297
|
+
if (!merged)
|
|
298
|
+
return null;
|
|
299
|
+
children.push(merged);
|
|
300
|
+
}
|
|
301
|
+
else if (child.type === 'JSXExpressionContainer' &&
|
|
302
|
+
other.type === 'JSXExpressionContainer' &&
|
|
303
|
+
!holdsUnmergeable(child.expression) &&
|
|
304
|
+
!holdsUnmergeable(other.expression)) {
|
|
305
|
+
children.push({
|
|
306
|
+
...child,
|
|
307
|
+
expression: mergeValues(test, child.expression, other.expression),
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
else {
|
|
311
|
+
return null;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
return {
|
|
315
|
+
...whenTrue,
|
|
316
|
+
openingElement: { ...trueOpening, attributes },
|
|
317
|
+
children,
|
|
318
|
+
};
|
|
319
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -37,6 +37,22 @@ const FLOAT_MATH_C = {
|
|
|
37
37
|
pow: 'powf',
|
|
38
38
|
mod: 'fmodf',
|
|
39
39
|
};
|
|
40
|
+
/** The C function each float Math function prints as when a boot value is worked out in double precision. */
|
|
41
|
+
const DOUBLE_MATH_C = {
|
|
42
|
+
sin: 'sin',
|
|
43
|
+
cos: 'cos',
|
|
44
|
+
tan: 'tan',
|
|
45
|
+
sqrt: 'sqrt',
|
|
46
|
+
abs: 'fabs',
|
|
47
|
+
round: 'app_round',
|
|
48
|
+
floor: 'floor',
|
|
49
|
+
ceil: 'ceil',
|
|
50
|
+
min: 'fmin',
|
|
51
|
+
max: 'fmax',
|
|
52
|
+
atan2: 'atan2',
|
|
53
|
+
pow: 'pow',
|
|
54
|
+
mod: 'fmod',
|
|
55
|
+
};
|
|
40
56
|
/** The field of the engine's ERRect each onLayout field reads. */
|
|
41
57
|
const LAYOUT_RECT_C = {
|
|
42
58
|
x: 'x',
|
|
@@ -46,6 +62,8 @@ const LAYOUT_RECT_C = {
|
|
|
46
62
|
};
|
|
47
63
|
/** The smallest C int. */
|
|
48
64
|
const INT_MIN = -(2 ** 31);
|
|
65
|
+
/** Whether a fraction prints as a double, as JS computes it, rather than a float (see printInDoubles). */
|
|
66
|
+
let inDoubles = false;
|
|
49
67
|
/*----------------------------------------------------------------------------------------------------------------------
|
|
50
68
|
- Implementation
|
|
51
69
|
---------------------------------------------------------------------------------------------------------------------*/
|
|
@@ -96,7 +114,7 @@ function printGestureField(expr) {
|
|
|
96
114
|
*/
|
|
97
115
|
function printNumber(expr) {
|
|
98
116
|
if (expr.type.kind === 'float') {
|
|
99
|
-
return floatLit(expr.value);
|
|
117
|
+
return inDoubles ? doubleLit(expr.value) : floatLit(expr.value);
|
|
100
118
|
}
|
|
101
119
|
if (expr.type.kind === 'int64') {
|
|
102
120
|
return i64Lit(expr.value);
|
|
@@ -104,6 +122,32 @@ function printNumber(expr) {
|
|
|
104
122
|
// `-2147483648` is `-` applied to 2147483648, which does not fit an int.
|
|
105
123
|
return expr.value === INT_MIN ? '(-2147483647 - 1)' : String(expr.value);
|
|
106
124
|
}
|
|
125
|
+
/**
|
|
126
|
+
* A number as a C double constant.
|
|
127
|
+
*
|
|
128
|
+
* @param value The number.
|
|
129
|
+
*
|
|
130
|
+
* @returns Its C constant.
|
|
131
|
+
*/
|
|
132
|
+
export const doubleLit = (value) => floatLit(value).replace(/f$/, '');
|
|
133
|
+
/**
|
|
134
|
+
* Prints with every fraction worked out in double precision, as JS works it out: a value set once at boot then
|
|
135
|
+
* rounds exactly as the compiler's own fold would have.
|
|
136
|
+
*
|
|
137
|
+
* @param print The printing.
|
|
138
|
+
*
|
|
139
|
+
* @returns What it prints.
|
|
140
|
+
*/
|
|
141
|
+
export function printInDoubles(print) {
|
|
142
|
+
const outer = inDoubles;
|
|
143
|
+
inDoubles = true;
|
|
144
|
+
try {
|
|
145
|
+
return print();
|
|
146
|
+
}
|
|
147
|
+
finally {
|
|
148
|
+
inDoubles = outer;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
107
151
|
/**
|
|
108
152
|
* An expression as a C condition (see asCond).
|
|
109
153
|
*
|
|
@@ -112,6 +156,12 @@ function printNumber(expr) {
|
|
|
112
156
|
* @returns C that is true exactly when JS would find it truthy.
|
|
113
157
|
*/
|
|
114
158
|
export const printCondition = (expr) => asCond({ code: printExpr(expr), cType: slotOf(expr.type) });
|
|
159
|
+
/**
|
|
160
|
+
* The C type a fraction is worked out in.
|
|
161
|
+
*
|
|
162
|
+
* @returns `double` while printing in doubles, else `float`.
|
|
163
|
+
*/
|
|
164
|
+
const fraction = () => (inDoubles ? 'double' : 'float');
|
|
115
165
|
/**
|
|
116
166
|
* Prints an expression as C.
|
|
117
167
|
*
|
|
@@ -129,6 +179,8 @@ export function printExpr(expr) {
|
|
|
129
179
|
return expr.value ? '1' : '0';
|
|
130
180
|
case 'state':
|
|
131
181
|
return expr.state.cMember;
|
|
182
|
+
case 'boot':
|
|
183
|
+
return `s_boot.${expr.slot.field}`;
|
|
132
184
|
case 'local':
|
|
133
185
|
return expr.local.code;
|
|
134
186
|
case 'ref':
|
|
@@ -144,11 +196,11 @@ export function printExpr(expr) {
|
|
|
144
196
|
case 'clock':
|
|
145
197
|
return expr.clock === 'Date' ? 'app_date_now()' : 'app_perf_now()';
|
|
146
198
|
case 'pi':
|
|
147
|
-
return '(float)M_PI';
|
|
199
|
+
return inDoubles ? 'M_PI' : '(float)M_PI';
|
|
148
200
|
case 'gesture':
|
|
149
201
|
return printGestureField(expr);
|
|
150
202
|
case 'toFloat':
|
|
151
|
-
return `((
|
|
203
|
+
return `((${fraction()})${printExpr(expr.operand)})`;
|
|
152
204
|
case 'unary': {
|
|
153
205
|
// The operand is parenthesized so `-` on a negative operand prints `(-(-x))`, not `(--x)` (a decrement).
|
|
154
206
|
const operand = expr.op === '!'
|
|
@@ -161,19 +213,22 @@ export function printExpr(expr) {
|
|
|
161
213
|
case 'arithmetic':
|
|
162
214
|
return `(${printExpr(expr.left)} ${expr.op} ${printExpr(expr.right)})`;
|
|
163
215
|
case 'floatDivide':
|
|
164
|
-
return `((
|
|
216
|
+
return `((${fraction()})(${printExpr(expr.left)}) / (${fraction()})(${printExpr(expr.right)}))`;
|
|
165
217
|
case 'narrowRemainder':
|
|
166
218
|
return `((int)(${printExpr(expr.left)} % ${printExpr(expr.right)}))`;
|
|
167
219
|
case 'floatMath': {
|
|
168
|
-
const args = expr.args.map(arg => `(
|
|
169
|
-
|
|
220
|
+
const args = expr.args.map(arg => `(${fraction()})(${printExpr(arg)})`);
|
|
221
|
+
const fn = (inDoubles ? DOUBLE_MATH_C : FLOAT_MATH_C)[expr.fn];
|
|
222
|
+
return `${fn}(${args.join(', ')})`;
|
|
170
223
|
}
|
|
171
224
|
case 'wholeRound':
|
|
172
225
|
return printExpr(expr.operand);
|
|
173
226
|
case 'trunc':
|
|
174
|
-
return
|
|
227
|
+
return inDoubles
|
|
228
|
+
? `app_d2i((double)(${printExpr(expr.operand)}))`
|
|
229
|
+
: `app_f2i((float)(${printExpr(expr.operand)}))`;
|
|
175
230
|
case 'toInt':
|
|
176
|
-
return
|
|
231
|
+
return `${inDoubles ? 'app_d2i' : 'app_f2i'}(${printExpr(expr.operand)})`;
|
|
177
232
|
case 'compare':
|
|
178
233
|
return `(${printExpr(expr.left)} ${expr.op} ${printExpr(expr.right)})`;
|
|
179
234
|
case 'stringEquals':
|
package/aot/compiler/c/nodes.mjs
CHANGED
|
@@ -21,7 +21,7 @@ import { printCondition, printExpr } from './expressions.mjs';
|
|
|
21
21
|
import { panEventName, panQueryName } from './pan-responder.mjs';
|
|
22
22
|
import { printText } from './text.mjs';
|
|
23
23
|
import { printStateDrivenVectorWiring, printVectorOps } from './vectors.mjs';
|
|
24
|
-
import { branchElements, isStateDriven } from '../ir/nodes.mjs';
|
|
24
|
+
import { branchElements, hasRuntimeProps, isStateDriven } from '../ir/nodes.mjs';
|
|
25
25
|
/*----------------------------------------------------------------------------------------------------------------------
|
|
26
26
|
- Implementation
|
|
27
27
|
---------------------------------------------------------------------------------------------------------------------*/
|
|
@@ -158,6 +158,17 @@ export function printNode(node) {
|
|
|
158
158
|
placeholder: node.placeholder,
|
|
159
159
|
};
|
|
160
160
|
}
|
|
161
|
+
else if (hasRuntimeProps(node)) {
|
|
162
|
+
// Props that read-only values worked out at boot are set once, here.
|
|
163
|
+
build.push(...printNodeUpdate({
|
|
164
|
+
nodeId: id,
|
|
165
|
+
styleAssigns: node.constantProps,
|
|
166
|
+
text,
|
|
167
|
+
dynAssigns: node.stateProps,
|
|
168
|
+
imageName: node.image?.kind === 'static' ? cstr(node.image.name) : imageName,
|
|
169
|
+
placeholder: node.placeholder,
|
|
170
|
+
}, id));
|
|
171
|
+
}
|
|
161
172
|
else {
|
|
162
173
|
build.push(` er_props_default(&p);`);
|
|
163
174
|
for (const prop of node.constantProps) {
|
|
@@ -252,10 +263,11 @@ export function printElementTree(element) {
|
|
|
252
263
|
* A state-driven node's app_update() entry: its props built again, then set.
|
|
253
264
|
*
|
|
254
265
|
* @param update The node's entry.
|
|
266
|
+
* @param target The node's variable: its file-scope handle, unless er_app_build() sets it.
|
|
255
267
|
*
|
|
256
268
|
* @returns The C lines.
|
|
257
269
|
*/
|
|
258
|
-
export function printNodeUpdate(update) {
|
|
270
|
+
export function printNodeUpdate(update, target = `s_${update.nodeId}`) {
|
|
259
271
|
const lines = [` er_props_default(&p);`];
|
|
260
272
|
for (const assign of update.styleAssigns) {
|
|
261
273
|
lines.push(` p.${assign.field} = ${assign.expr};`);
|
|
@@ -277,6 +289,6 @@ export function printNodeUpdate(update) {
|
|
|
277
289
|
lines.push(` snprintf(p.text, sizeof(p.text), "%s", ${cstr(update.text.format.replace(/%%/g, '%'))});`);
|
|
278
290
|
}
|
|
279
291
|
}
|
|
280
|
-
lines.push(` er_node_set_props(
|
|
292
|
+
lines.push(` er_node_set_props(${target}, &p);`);
|
|
281
293
|
return lines;
|
|
282
294
|
}
|