embedded-react 0.20.0 → 0.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/NOTICE +5 -0
- package/README.md +1 -0
- package/aot/app-file-loader.mjs +116 -0
- package/aot/compile.mjs +16 -46
- package/aot/compiler/animations.mjs +299 -57
- package/aot/compiler/boot.mjs +27 -2
- package/aot/compiler/check.mjs +61 -9
- package/aot/compiler/collect.mjs +516 -136
- package/aot/compiler/contexts.mjs +391 -0
- package/aot/compiler/control-flow.mjs +276 -117
- package/aot/compiler/custom-hooks.mjs +686 -0
- package/aot/compiler/elements/button.mjs +182 -0
- package/aot/compiler/elements/image-background.mjs +76 -0
- package/aot/compiler/elements/image.mjs +2 -1
- package/aot/compiler/elements/props.mjs +38 -3
- package/aot/compiler/elements/rewrite.mjs +118 -0
- package/aot/compiler/elements/section-list.mjs +370 -0
- package/aot/compiler/expressions.mjs +1351 -97
- package/aot/compiler/handlers.mjs +1761 -365
- package/aot/compiler/helper-values.mjs +312 -0
- package/aot/compiler/ir/nodes.mjs +40 -1
- package/aot/compiler/ir/reads.mjs +206 -5
- package/aot/compiler/ir/text.mjs +5 -1
- package/aot/compiler/ir/types.mjs +12 -0
- package/aot/compiler/kept-states.mjs +177 -0
- package/aot/compiler/list-items.mjs +226 -0
- package/aot/compiler/list-order.mjs +586 -0
- package/aot/compiler/list-split.mjs +259 -0
- package/aot/compiler/loops.mjs +582 -0
- package/aot/compiler/lower.mjs +191 -53
- package/aot/compiler/native-files.mjs +439 -0
- package/aot/compiler/native-header.mjs +335 -0
- package/aot/compiler/native-modules.mjs +1173 -0
- package/aot/compiler/nodes.mjs +28 -22
- package/aot/compiler/options.mjs +3 -2
- package/aot/compiler/out.mjs +14 -1
- package/aot/compiler/package/animations.mjs +133 -23
- package/aot/compiler/package/assemble.mjs +2 -4
- package/aot/compiler/package/bytecode.mjs +37 -8
- package/aot/compiler/package/checks.mjs +4 -0
- package/aot/compiler/package/codegen.mjs +870 -31
- package/aot/compiler/package/enums.mjs +10 -0
- package/aot/compiler/package/format.mjs +12 -1
- package/aot/compiler/package/lower.mjs +658 -161
- package/aot/compiler/package/string-caps.mjs +515 -0
- package/aot/compiler/package/tables.mjs +24 -0
- package/aot/compiler/package/writer.mjs +22 -0
- package/aot/compiler/parse.mjs +3 -1
- package/aot/compiler/record-shapes.mjs +246 -0
- package/aot/compiler/records.mjs +654 -0
- package/aot/compiler/reducers.mjs +272 -0
- package/aot/compiler/static-eval.mjs +315 -7
- package/aot/compiler/style-text.mjs +18 -3
- package/aot/compiler/svg.mjs +7 -4
- package/aot/compiler/switch-cases.mjs +198 -0
- package/aot/compiler/tagged-records.mjs +450 -0
- package/aot/compiler/typing.mjs +307 -0
- package/aot/compiler/updaters.mjs +320 -0
- package/aot/compiler/window-dimensions.mjs +94 -0
- package/aot/compiler/worked-values.mjs +319 -0
- package/aot/demo-file.mjs +30 -0
- package/aot/style-map.mjs +50 -2
- package/build.mjs +9 -1
- package/cli.mjs +44 -1
- package/hotreload/pack-split.mjs +29 -40
- package/pack-container.mjs +9 -1
- package/package.json +1 -1
- package/sim/embedded-react.wasm +0 -0
- package/sim-server.mjs +22 -22
- package/src/embedded-react/Dimensions.js +37 -0
- package/src/embedded-react/SectionList.js +1 -3
- package/src/embedded-react/index.d.ts +83 -3
- package/src/embedded-react/index.js +2 -0
- package/src/embedded-react/nativeModule.js +462 -0
|
@@ -0,0 +1,94 @@
|
|
|
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
|
+
* @file compiler/window-dimensions.mts
|
|
18
|
+
* @brief Turns `useWindowDimensions()` and `Dimensions.get('window')` into reads of the panel's size.
|
|
19
|
+
*/
|
|
20
|
+
import { traverse } from '@babel/core';
|
|
21
|
+
import { PANEL_KEY } from './boot.mjs';
|
|
22
|
+
import { aotError } from './diagnostics.mjs';
|
|
23
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
24
|
+
- Constants
|
|
25
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
26
|
+
/** The package the two are imported from. */
|
|
27
|
+
const PACKAGE = 'embedded-react';
|
|
28
|
+
/** The sizes `Dimensions.get` names, which are the same on a panel. */
|
|
29
|
+
const SIZES = new Set(['window', 'screen']);
|
|
30
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
31
|
+
- Implementation
|
|
32
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
33
|
+
/**
|
|
34
|
+
* The package export a name stands for where it is read, or null when it is anything else: a local, a prop or a
|
|
35
|
+
* state of that name hides the import.
|
|
36
|
+
*
|
|
37
|
+
* @param path Where the name is read.
|
|
38
|
+
* @param node The name.
|
|
39
|
+
*
|
|
40
|
+
* @returns The export's name, or null.
|
|
41
|
+
*/
|
|
42
|
+
function packageExportOf(path, node) {
|
|
43
|
+
if (node.type !== 'Identifier')
|
|
44
|
+
return null;
|
|
45
|
+
const binding = path.scope.getBinding(node.name);
|
|
46
|
+
const specifier = binding?.path.node;
|
|
47
|
+
const declaration = binding?.path.parent;
|
|
48
|
+
if (specifier?.type !== 'ImportSpecifier' ||
|
|
49
|
+
declaration?.type !== 'ImportDeclaration' ||
|
|
50
|
+
declaration.source.value !== PACKAGE) {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
const { imported } = specifier;
|
|
54
|
+
return imported.type === 'Identifier' ? imported.name : imported.value;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Rewrites each `useWindowDimensions()` and `Dimensions.get('window' | 'screen')` to a read of the panel's size,
|
|
58
|
+
* which folds as `screen` does. Only the package's own exports are rewritten, by whatever name they are imported.
|
|
59
|
+
*
|
|
60
|
+
* @param ast The parsed module, rewritten in place.
|
|
61
|
+
*
|
|
62
|
+
* @throws An AotError for any other use of `Dimensions`.
|
|
63
|
+
*/
|
|
64
|
+
export function rewriteWindowDimensions(ast) {
|
|
65
|
+
const panel = (node) => ({ type: 'Identifier', name: PANEL_KEY, loc: node.loc });
|
|
66
|
+
traverse.cache.clear();
|
|
67
|
+
traverse(ast, {
|
|
68
|
+
CallExpression(path) {
|
|
69
|
+
const { callee, arguments: callArguments } = path.node;
|
|
70
|
+
if (packageExportOf(path, callee) === 'useWindowDimensions') {
|
|
71
|
+
path.replaceWith(panel(path.node));
|
|
72
|
+
}
|
|
73
|
+
else if (callee.type === 'MemberExpression' &&
|
|
74
|
+
packageExportOf(path, callee.object) === 'Dimensions') {
|
|
75
|
+
const [size] = callArguments;
|
|
76
|
+
const isGet = !callee.computed &&
|
|
77
|
+
callee.property.type === 'Identifier' &&
|
|
78
|
+
callee.property.name === 'get';
|
|
79
|
+
if (!isGet ||
|
|
80
|
+
callArguments.length !== 1 ||
|
|
81
|
+
size.type !== 'StringLiteral' ||
|
|
82
|
+
!SIZES.has(size.value)) {
|
|
83
|
+
const error = aotError("AOT: Dimensions has only get('window') and get('screen')", 'the panel size is fixed while the app runs, so there is nothing else to read or listen to.');
|
|
84
|
+
if (path.node.loc) {
|
|
85
|
+
error.aotLoc = path.node.loc.start;
|
|
86
|
+
}
|
|
87
|
+
throw error;
|
|
88
|
+
}
|
|
89
|
+
path.replaceWith(panel(path.node));
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
traverse.cache.clear();
|
|
94
|
+
}
|
|
@@ -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
|
+
* Values worked out with a loop: a block-bodied `useMemo`, or a helper used as a value, whose body has `let` locals
|
|
18
|
+
* or loops. Such a body can't be one expression, so it runs as statements, each `return` setting a local and leaving
|
|
19
|
+
* the body. In a handler, effect or timer it runs just before the statement that reads it. In render it has a state
|
|
20
|
+
* of its own, which the update works out again, before anything that reads it, when what it reads changes.
|
|
21
|
+
*/
|
|
22
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
23
|
+
- Imports
|
|
24
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
25
|
+
import { holdLater, setLater, shapeOf, shapeOfType, typedLater, } from './typing.mjs';
|
|
26
|
+
import { aotError } from './diagnostics.mjs';
|
|
27
|
+
import { assumeType, assumedTypeOf, buildsText, lowerSource, lowerText, typeSource, } from './expressions.mjs';
|
|
28
|
+
import { lowerStmts } from './handlers.mjs';
|
|
29
|
+
import { helperEnvironment } from './helper-values.mjs';
|
|
30
|
+
import { BOOL, FLOAT, INT, INT64_NUMBER, STRING, TIME } from './ir/types.mjs';
|
|
31
|
+
import { Out } from './out.mjs';
|
|
32
|
+
import { checkValueBody } from './updaters.mjs';
|
|
33
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
34
|
+
- Variables
|
|
35
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
36
|
+
/** Numbers each value and each list worked out in render as it is made, so the update works out each in turn. */
|
|
37
|
+
let rebuilds = 0;
|
|
38
|
+
/** Numbers the locals a value is left in. */
|
|
39
|
+
let results = 0;
|
|
40
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
41
|
+
- Implementation
|
|
42
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
43
|
+
/**
|
|
44
|
+
* The next number in the order the update works out what render makes: a value worked out with a loop, or a list
|
|
45
|
+
* filtered, sorted or split.
|
|
46
|
+
*
|
|
47
|
+
* @returns The number.
|
|
48
|
+
*/
|
|
49
|
+
export const nextRebuild = () => rebuilds++;
|
|
50
|
+
/**
|
|
51
|
+
* Whether statements need to run as statements: they declare a `let` or loop, not counting a function inside them.
|
|
52
|
+
*
|
|
53
|
+
* @param statements The statements.
|
|
54
|
+
*
|
|
55
|
+
* @returns Whether they do.
|
|
56
|
+
*/
|
|
57
|
+
function loopsIn(statements) {
|
|
58
|
+
return statements.some(statement => {
|
|
59
|
+
switch (statement?.type) {
|
|
60
|
+
case 'VariableDeclaration':
|
|
61
|
+
return statement.kind !== 'const';
|
|
62
|
+
case 'ForStatement':
|
|
63
|
+
case 'ForOfStatement':
|
|
64
|
+
case 'ForInStatement':
|
|
65
|
+
case 'WhileStatement':
|
|
66
|
+
case 'DoWhileStatement':
|
|
67
|
+
return true;
|
|
68
|
+
case 'BlockStatement':
|
|
69
|
+
return loopsIn(statement.body);
|
|
70
|
+
case 'IfStatement':
|
|
71
|
+
return loopsIn([statement.consequent, statement.alternate]);
|
|
72
|
+
case 'SwitchStatement':
|
|
73
|
+
return statement.cases.some(({ consequent }) => loopsIn(consequent));
|
|
74
|
+
case 'LabeledStatement':
|
|
75
|
+
return loopsIn([statement.body]);
|
|
76
|
+
default:
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Whether a helper's body loops or declares a `let`, so it runs as statements rather than as one expression.
|
|
83
|
+
*
|
|
84
|
+
* @param helper The helper.
|
|
85
|
+
*
|
|
86
|
+
* @returns Whether it does.
|
|
87
|
+
*/
|
|
88
|
+
export const runsAsStatements = (helper) => helper.body.type === 'BlockStatement' && loopsIn(helper.body.body);
|
|
89
|
+
/**
|
|
90
|
+
* What a value-giving body is, for errors: `the useMemo for "total"` or `helper "sum"`.
|
|
91
|
+
*
|
|
92
|
+
* @param name The helper's name.
|
|
93
|
+
*
|
|
94
|
+
* @returns The phrase, and the memo's name for a memo.
|
|
95
|
+
*/
|
|
96
|
+
function describe(name) {
|
|
97
|
+
const memo = /^useMemo\((.*)\)$/.exec(name)?.[1] ?? null;
|
|
98
|
+
return {
|
|
99
|
+
what: memo === null ? `helper "${name}"` : `the useMemo for "${memo}"`,
|
|
100
|
+
memo,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* The type a local holding every value a body returns has: a string, a boolean, or a number. A number is a float
|
|
105
|
+
* when any is, and 64 bits when any is, a time unless every 64-bit one is a plain number.
|
|
106
|
+
*
|
|
107
|
+
* @param types The returned values' types.
|
|
108
|
+
* @param what What returns them, for errors.
|
|
109
|
+
*
|
|
110
|
+
* @returns The type.
|
|
111
|
+
*/
|
|
112
|
+
function joinedType(types, what) {
|
|
113
|
+
const kinds = new Set(types.map(type => type.kind));
|
|
114
|
+
joinedShape(types.map(shapeOfType), what);
|
|
115
|
+
if (kinds.has('string'))
|
|
116
|
+
return STRING;
|
|
117
|
+
if (kinds.has('bool'))
|
|
118
|
+
return BOOL;
|
|
119
|
+
if (kinds.has('int64')) {
|
|
120
|
+
if (kinds.has('float')) {
|
|
121
|
+
throw aotError(`AOT: ${what} loops, so it can't return a 64-bit whole number on one path and a fraction on another`, 'return whole numbers on every path, e.g. Math.round(x), or fractions on every path.');
|
|
122
|
+
}
|
|
123
|
+
return types.every(type => type.kind !== 'int64' || type.source === 'number')
|
|
124
|
+
? INT64_NUMBER
|
|
125
|
+
: TIME;
|
|
126
|
+
}
|
|
127
|
+
return kinds.has('float') ? FLOAT : INT;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Whether a local holding every value a body returns holds a string, a boolean or a number: each must be one of
|
|
131
|
+
* those, and all the same.
|
|
132
|
+
*
|
|
133
|
+
* @param shapes The returned values' shapes.
|
|
134
|
+
* @param what What returns them, for errors.
|
|
135
|
+
*
|
|
136
|
+
* @returns The shape.
|
|
137
|
+
*/
|
|
138
|
+
function joinedShape(shapes, what) {
|
|
139
|
+
const sorts = new Set(shapes);
|
|
140
|
+
if (sorts.has('struct') || sorts.has('node')) {
|
|
141
|
+
throw aotError(`AOT: ${what} loops, so it must return a number, a boolean or a string`, 'return the value itself, e.g. a field of an item rather than the item.');
|
|
142
|
+
}
|
|
143
|
+
if (sorts.size > 1) {
|
|
144
|
+
throw aotError(`AOT: ${what} loops, so each \`return\` must give the same kind of value`, `it returns a ${[...sorts].join(' on one path and a ')} on another. Return one kind on every path.`);
|
|
145
|
+
}
|
|
146
|
+
return shapes[0] ?? 'number';
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* How a local or a state holding a type is kept.
|
|
150
|
+
*
|
|
151
|
+
* @param type The type.
|
|
152
|
+
*
|
|
153
|
+
* @returns Its slot kind, and whether it is a boolean or a 64-bit plain number.
|
|
154
|
+
*/
|
|
155
|
+
const heldAs = (type) => type.kind === 'string'
|
|
156
|
+
? { slotKind: 'string' }
|
|
157
|
+
: type.kind === 'float'
|
|
158
|
+
? { slotKind: 'float' }
|
|
159
|
+
: type.kind === 'int64'
|
|
160
|
+
? {
|
|
161
|
+
slotKind: 'i64',
|
|
162
|
+
...(type.source === 'number' ? { isLiteral: true } : {}),
|
|
163
|
+
}
|
|
164
|
+
: { slotKind: 'int', ...(type.kind === 'bool' ? { isBoolean: true } : {}) };
|
|
165
|
+
/**
|
|
166
|
+
* A value of a type that a local starts at before a `return` sets it.
|
|
167
|
+
*
|
|
168
|
+
* @param type The type.
|
|
169
|
+
*
|
|
170
|
+
* @returns The value.
|
|
171
|
+
*/
|
|
172
|
+
const zeroOf = (type) => type.kind === 'string'
|
|
173
|
+
? { kind: 'string', value: '', type }
|
|
174
|
+
: type.kind === 'bool'
|
|
175
|
+
? { kind: 'boolean', value: false, type }
|
|
176
|
+
: { kind: 'number', value: 0, type };
|
|
177
|
+
/**
|
|
178
|
+
* Lowers a looping body to statements, each `return` leaving its value in a local of its own and the body.
|
|
179
|
+
*
|
|
180
|
+
* @param call The call.
|
|
181
|
+
* @param name The helper's name.
|
|
182
|
+
* @param helper The helper.
|
|
183
|
+
* @param environment The caller's environment.
|
|
184
|
+
* @param state The component's state table.
|
|
185
|
+
* @param output Everything emitted so far.
|
|
186
|
+
*
|
|
187
|
+
* @returns The statements and the local.
|
|
188
|
+
*/
|
|
189
|
+
function lowerValueStatements(call, name, helper, environment, state, output) {
|
|
190
|
+
const { what, memo } = describe(name);
|
|
191
|
+
checkValueBody(helper.body, what, memo !== null);
|
|
192
|
+
const resultName = `value@${name}#${results++}`;
|
|
193
|
+
const returned = [];
|
|
194
|
+
const context = {
|
|
195
|
+
stateChanged: false,
|
|
196
|
+
output,
|
|
197
|
+
helperList: helper.body.body,
|
|
198
|
+
returnsValue: (argument, bodyEnvironment) => {
|
|
199
|
+
if (buildsText(argument, bodyEnvironment)) {
|
|
200
|
+
const text = lowerText(argument, bodyEnvironment);
|
|
201
|
+
const value = { kind: 'string', value: '', type: STRING };
|
|
202
|
+
returned.push({ value, text });
|
|
203
|
+
return [{ kind: 'assignLocal', name: resultName, value, text }];
|
|
204
|
+
}
|
|
205
|
+
const { source, expression: value } = lowerSource(argument, bodyEnvironment);
|
|
206
|
+
returned.push({ value, source });
|
|
207
|
+
return [{ kind: 'assignLocal', name: resultName, value }];
|
|
208
|
+
},
|
|
209
|
+
};
|
|
210
|
+
const body = lowerStmts(helper.body.body, helperEnvironment(name, helper, call.arguments, environment), state, context);
|
|
211
|
+
// Whether it is a string, a boolean or a number is known now; a number's width once each value is typed.
|
|
212
|
+
const shape = joinedShape(returned.map(({ value, text }) => (text ? 'string' : shapeOf(value))), what);
|
|
213
|
+
const type = () => joinedType(returned.map(({ value, text }) => (text ? STRING : value.type)), what);
|
|
214
|
+
const result = holdLater({ name: resultName }, shape, () => heldAs(type()));
|
|
215
|
+
assumeType(result, () => joinedType(returned.map(({ source }) => source ? typeSource(source, false).type : STRING), what));
|
|
216
|
+
return {
|
|
217
|
+
statements: [
|
|
218
|
+
{
|
|
219
|
+
kind: 'declareLocal',
|
|
220
|
+
name: resultName,
|
|
221
|
+
initializer: typedLater(shape, () => zeroOf(type())),
|
|
222
|
+
isHoisted: false,
|
|
223
|
+
},
|
|
224
|
+
...(context.usedReturn ? [{ kind: 'block', body }] : body),
|
|
225
|
+
],
|
|
226
|
+
result,
|
|
227
|
+
shape,
|
|
228
|
+
type,
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* A call to a helper that loops, used as a value. In a handler, effect or timer, the body runs before the statement
|
|
233
|
+
* that reads it, into a local. In render it is a state of its own, which the update works out again before anything
|
|
234
|
+
* that reads it.
|
|
235
|
+
*
|
|
236
|
+
* @param call The call.
|
|
237
|
+
* @param name The helper's name.
|
|
238
|
+
* @param helper The helper.
|
|
239
|
+
* @param environment The caller's environment.
|
|
240
|
+
*
|
|
241
|
+
* @returns The value.
|
|
242
|
+
*/
|
|
243
|
+
export function workedValueOf(call, name, helper, environment) {
|
|
244
|
+
const location = call.loc?.start;
|
|
245
|
+
const { what, memo } = describe(name);
|
|
246
|
+
// In a handler, effect or timer: worked out just before the statement.
|
|
247
|
+
const sink = environment.before;
|
|
248
|
+
if (sink) {
|
|
249
|
+
const { statements, result } = lowerValueStatements(call, name, helper, environment, sink.state, sink.output);
|
|
250
|
+
sink.statements.push(...statements);
|
|
251
|
+
return { kind: 'local', local: result, location };
|
|
252
|
+
}
|
|
253
|
+
if (environment.outsideRender) {
|
|
254
|
+
throw aotError(`AOT: ${what} loops, so it can't be used as a value here`, 'call it from a statement of a handler, an effect or a timer, or from render.');
|
|
255
|
+
}
|
|
256
|
+
if (environment.inListRow) {
|
|
257
|
+
throw aotError(`AOT: ${what} loops, so it can't be used inside a list row`, 'a value worked out with a loop is worked out once, outside the rows. Work it out for the whole list in a ' +
|
|
258
|
+
'useMemo, or keep it in each item.');
|
|
259
|
+
}
|
|
260
|
+
const values = environment.workedValues;
|
|
261
|
+
if (!values) {
|
|
262
|
+
throw new Error('AOT internal: no table for values worked out with a loop');
|
|
263
|
+
}
|
|
264
|
+
// In render: a state of its own, one per call and component instance. A call inside a helper being inlined may be
|
|
265
|
+
// given other arguments each time, so it gets one each time.
|
|
266
|
+
const prefix = environment.callbackPrefix ?? '';
|
|
267
|
+
const shared = !environment.inlining?.size;
|
|
268
|
+
const known = shared ? values.get(call)?.get(prefix) : undefined;
|
|
269
|
+
if (known)
|
|
270
|
+
return { kind: 'state', state: known, location };
|
|
271
|
+
const { statements, result, shape, type } = lowerValueStatements(call, name, helper, { ...environment, workedInRender: true }, { byName: new Map(environment.state), bySetter: new Map() }, new Out(new Map(), null));
|
|
272
|
+
const count = [...values.values()].reduce((sum, map) => sum + map.size, 0);
|
|
273
|
+
const label = memo ?? name;
|
|
274
|
+
const worked = {
|
|
275
|
+
kind: 'scalar',
|
|
276
|
+
name: label,
|
|
277
|
+
slotName: `${prefix}w${count}_${label}`,
|
|
278
|
+
setter: undefined,
|
|
279
|
+
slotKind: shape === 'string' ? 'string' : 'int',
|
|
280
|
+
isBoolean: shape === 'bool',
|
|
281
|
+
initial: shape === 'string' ? '' : 0,
|
|
282
|
+
host: false,
|
|
283
|
+
initialAt: location,
|
|
284
|
+
};
|
|
285
|
+
if (shape === 'number') {
|
|
286
|
+
setLater(worked, ['slotKind', 'widened'], () => {
|
|
287
|
+
const held = type();
|
|
288
|
+
const widened = held.kind !== 'int64'
|
|
289
|
+
? undefined
|
|
290
|
+
: held.source === 'number'
|
|
291
|
+
? 'number'
|
|
292
|
+
: 'time';
|
|
293
|
+
return { slotKind: heldAs(held).slotKind, widened };
|
|
294
|
+
});
|
|
295
|
+
assumeType(worked, () => assumedTypeOf(result));
|
|
296
|
+
}
|
|
297
|
+
const read = typedLater(shape, () => ({
|
|
298
|
+
kind: 'local',
|
|
299
|
+
local: result,
|
|
300
|
+
type: type(),
|
|
301
|
+
}));
|
|
302
|
+
worked.worked = {
|
|
303
|
+
rebuild: [
|
|
304
|
+
...statements,
|
|
305
|
+
shape === 'string'
|
|
306
|
+
? {
|
|
307
|
+
kind: 'setStateText',
|
|
308
|
+
state: worked,
|
|
309
|
+
text: { parts: [{ kind: 'value', value: read }] },
|
|
310
|
+
}
|
|
311
|
+
: { kind: 'setState', state: worked, value: read },
|
|
312
|
+
],
|
|
313
|
+
order: nextRebuild(),
|
|
314
|
+
};
|
|
315
|
+
if (!values.has(call))
|
|
316
|
+
values.set(call, new Map());
|
|
317
|
+
values.get(call).set(shared ? prefix : `${prefix}#${count}`, worked);
|
|
318
|
+
return { kind: 'state', state: worked, location };
|
|
319
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
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
|
+
import { existsSync } from 'node:fs';
|
|
17
|
+
import { resolve } from 'node:path';
|
|
18
|
+
/**
|
|
19
|
+
* A demo's file, in TypeScript or JavaScript: `demoFile(directory, 'App')` is App.tsx when there is one, else
|
|
20
|
+
* App.jsx.
|
|
21
|
+
*
|
|
22
|
+
* @param directory The demo's folder.
|
|
23
|
+
* @param name The file's name, without its extension.
|
|
24
|
+
*
|
|
25
|
+
* @returns The path, which is the .jsx one when neither is there.
|
|
26
|
+
*/
|
|
27
|
+
export function demoFile(directory, name) {
|
|
28
|
+
const typed = resolve(directory, `${name}.tsx`);
|
|
29
|
+
return existsSync(typed) ? typed : resolve(directory, `${name}.jsx`);
|
|
30
|
+
}
|
package/aot/style-map.mjs
CHANGED
|
@@ -94,6 +94,7 @@ const NUMBER_FIELDS = {
|
|
|
94
94
|
fontSize: 'font_size',
|
|
95
95
|
lineHeight: 'line_height',
|
|
96
96
|
letterSpacing: 'letter_spacing',
|
|
97
|
+
shadowOpacity: 'shadow_opacity',
|
|
97
98
|
};
|
|
98
99
|
/**
|
|
99
100
|
* Enum style keys that can be state-driven: the value (a string literal or a ternary of them) lowers to the
|
|
@@ -109,8 +110,8 @@ const ENUM_FIELDS = {
|
|
|
109
110
|
};
|
|
110
111
|
/**
|
|
111
112
|
* Every style key whose value can be state-driven, with its ERProps field and how an update lowers it (see
|
|
112
|
-
* DynamicStyleField). Keys absent here (such as the `flex` shorthand, `flexBasis`, the four insets
|
|
113
|
-
* `fontWeight`) can only be static.
|
|
113
|
+
* DynamicStyleField). Keys absent here (such as the `flex` shorthand, `flexBasis`, the four insets,
|
|
114
|
+
* `fontWeight` and `shadowOffset`) can only be static.
|
|
114
115
|
*/
|
|
115
116
|
export const DYN_FIELDS = {
|
|
116
117
|
...Object.fromEntries(Object.entries(NUMBER_FIELDS).map(([styleKey, field]) => [
|
|
@@ -124,12 +125,17 @@ export const DYN_FIELDS = {
|
|
|
124
125
|
backgroundColor: { field: 'background_color', kind: 'color' },
|
|
125
126
|
color: { field: 'color', kind: 'color' },
|
|
126
127
|
borderColor: { field: 'border_color', kind: 'color' },
|
|
128
|
+
shadowColor: { field: 'shadow_color', kind: 'color' },
|
|
129
|
+
shadowRadius: { field: 'shadow_radius', kind: 'byte' },
|
|
130
|
+
elevation: { field: 'elevation', kind: 'byte' },
|
|
127
131
|
opacity: { field: 'opacity', kind: 'opacity' },
|
|
128
132
|
};
|
|
129
133
|
/** The engine's opacity for fully opaque, which a 0–1 opacity scales to. */
|
|
130
134
|
export const OPACITY_OPAQUE = 255;
|
|
131
135
|
/** The alpha of a color written without one. */
|
|
132
136
|
const ALPHA_OPAQUE = 0xff;
|
|
137
|
+
/** The largest value a byte field holds. */
|
|
138
|
+
const BYTE_MAX = 255;
|
|
133
139
|
/** The lightest numeric fontWeight drawn bold. */
|
|
134
140
|
const BOLD_WEIGHT = 600;
|
|
135
141
|
/*----------------------------------------------------------------------------------------------------------------------
|
|
@@ -226,6 +232,28 @@ const dimension = (value) => {
|
|
|
226
232
|
// Adding 0 turns -0 into 0, whose bits a float field keeps.
|
|
227
233
|
return Math.round(value) + 0;
|
|
228
234
|
};
|
|
235
|
+
/**
|
|
236
|
+
* A whole number for a byte field.
|
|
237
|
+
*
|
|
238
|
+
* @param value The style value; must be a finite number.
|
|
239
|
+
*
|
|
240
|
+
* @returns It with its fraction dropped, clamped to 0–255, as Flow A reads one.
|
|
241
|
+
*/
|
|
242
|
+
const byteValue = (value) => {
|
|
243
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) {
|
|
244
|
+
throw new Error(`expected a number, got ${JSON.stringify(value)}`);
|
|
245
|
+
}
|
|
246
|
+
// Adding 0 turns -0 into 0.
|
|
247
|
+
return Math.min(BYTE_MAX, Math.max(0, Math.trunc(value))) + 0;
|
|
248
|
+
};
|
|
249
|
+
/**
|
|
250
|
+
* One side of a `shadowOffset`.
|
|
251
|
+
*
|
|
252
|
+
* @param offset `width` or `height`; left out, it is 0.
|
|
253
|
+
*
|
|
254
|
+
* @returns The offset in pixels.
|
|
255
|
+
*/
|
|
256
|
+
const shadowOffsetSide = (offset) => offset === undefined || offset === null ? 0 : floatConstant(offset);
|
|
229
257
|
/**
|
|
230
258
|
* A dimension that may be a percentage. `'50%'` → the engine's float `*_percent` field (% of the parent);
|
|
231
259
|
* a number → the pixel field. Used for width / height / flexBasis and the four insets, all of which the
|
|
@@ -372,6 +400,26 @@ const KEYS = {
|
|
|
372
400
|
},
|
|
373
401
|
],
|
|
374
402
|
zIndex: zIndex => [{ field: 'z_index', value: dimension(zIndex) }],
|
|
403
|
+
// Shadow, drawn only by a firmware built with ERUI_SHADOWS (same as Flow A).
|
|
404
|
+
shadowColor: color => [
|
|
405
|
+
{ field: 'shadow_color', value: parseColorValue(color) },
|
|
406
|
+
],
|
|
407
|
+
// Both sides are written, so a later shadowOffset replaces an earlier one whole, as in RN.
|
|
408
|
+
shadowOffset: offset => {
|
|
409
|
+
if (typeof offset !== 'object' || Array.isArray(offset)) {
|
|
410
|
+
throw new Error(`expected {width, height}, got ${JSON.stringify(offset)}`);
|
|
411
|
+
}
|
|
412
|
+
const { width, height } = offset;
|
|
413
|
+
return [
|
|
414
|
+
{ field: 'shadow_offset_x', value: shadowOffsetSide(width) },
|
|
415
|
+
{ field: 'shadow_offset_y', value: shadowOffsetSide(height) },
|
|
416
|
+
];
|
|
417
|
+
},
|
|
418
|
+
shadowOpacity: opacity => [
|
|
419
|
+
{ field: 'shadow_opacity', value: floatConstant(opacity) },
|
|
420
|
+
],
|
|
421
|
+
shadowRadius: radius => [{ field: 'shadow_radius', value: byteValue(radius) }],
|
|
422
|
+
elevation: elevation => [{ field: 'elevation', value: byteValue(elevation) }],
|
|
375
423
|
// Text
|
|
376
424
|
color: color => [{ field: 'color', value: parseColorValue(color) }],
|
|
377
425
|
fontSize: size => [{ field: 'font_size', value: dimension(size) }],
|
package/build.mjs
CHANGED
|
@@ -34,6 +34,8 @@ import {bakeAssets} from './assets/index.mjs';
|
|
|
34
34
|
import {resolveFontJobs} from './assets/font-config.mjs';
|
|
35
35
|
import {analyzeFontSizes} from './assets/font-sizes.mjs';
|
|
36
36
|
import {registerSvgVectorLoader} from './assets/svg-loader.mjs';
|
|
37
|
+
import {loadAppFile, loadDependencyFile} from './aot/app-file-loader.mjs';
|
|
38
|
+
import {demoFile} from './aot/demo-file.mjs';
|
|
37
39
|
|
|
38
40
|
const here = dirname(fileURLToPath(import.meta.url)); // packages/embedded-react
|
|
39
41
|
const repoRoot = resolve(here, '../..');
|
|
@@ -45,7 +47,7 @@ const distDir = resolve(here, 'dist');
|
|
|
45
47
|
const DEFAULT_DEMO = 'thermostat';
|
|
46
48
|
const demo = process.argv[2] || process.env.DEMO || DEFAULT_DEMO;
|
|
47
49
|
const demoDir = resolve(demosDir, demo);
|
|
48
|
-
const entry =
|
|
50
|
+
const entry = demoFile(demoDir, 'index');
|
|
49
51
|
|
|
50
52
|
if (!existsSync(entry)) {
|
|
51
53
|
const available = existsSync(demosDir)
|
|
@@ -67,6 +69,12 @@ const fonts = new Map(); // family -> path
|
|
|
67
69
|
const assetPlugin = {
|
|
68
70
|
name: 'embedded-react-assets',
|
|
69
71
|
setup(pluginBuild) {
|
|
72
|
+
// The demo's own files give its native modules their signatures, and so do a package's.
|
|
73
|
+
pluginBuild.onLoad({filter: /\.(jsx?|tsx?)$/}, args =>
|
|
74
|
+
args.path.startsWith(demoDir)
|
|
75
|
+
? loadAppFile(args.path)
|
|
76
|
+
: loadDependencyFile(args.path),
|
|
77
|
+
);
|
|
70
78
|
pluginBuild.onLoad({filter: /\.(png|jpe?g|webp|gif|bmp)$/i}, args => {
|
|
71
79
|
const name = basename(args.path).replace(/\.[^.]+$/, '');
|
|
72
80
|
images.set(name, args.path);
|
package/cli.mjs
CHANGED
|
@@ -84,6 +84,10 @@ Usage:
|
|
|
84
84
|
embedded-react pack [package] --board <name> [--slot a|b] [--sequence <n>] [--out <dir>]
|
|
85
85
|
Write a slot's image to a file, for a factory line or a
|
|
86
86
|
drag-and-drop UF2, without a board attached.
|
|
87
|
+
embedded-react native-header <package or file> [--out <dir>]
|
|
88
|
+
Write a C header for each native module a device package
|
|
89
|
+
or a .ts file declares: its records, where each argument
|
|
90
|
+
starts, and an entry for the firmware's table.
|
|
87
91
|
|
|
88
92
|
board esp32-2432s028r, esp32-s3 or rp2040-touch-lcd-1.69
|
|
89
93
|
package defaults to dist/app.erpkg
|
|
@@ -423,7 +427,7 @@ async function compileAppPackage(
|
|
|
423
427
|
appPath,
|
|
424
428
|
{screens = null, release = false} = {},
|
|
425
429
|
) {
|
|
426
|
-
const {compilePackage, bakeSvgArtifacts, packPackage} =
|
|
430
|
+
const {compilePackage, bakeSvgArtifacts, packPackage, readNativeFiles} =
|
|
427
431
|
await import('./aot/compile.mjs');
|
|
428
432
|
const {bakeAssetPackBytes} = await import('./assets/index.mjs');
|
|
429
433
|
const {warnMissingGlyphs} = await import('./assets/glyph-coverage.mjs');
|
|
@@ -438,6 +442,8 @@ async function compileAppPackage(
|
|
|
438
442
|
const result = compilePackage(source, basename(dirname(appPath)), {
|
|
439
443
|
filename: appPath,
|
|
440
444
|
svgArtifacts,
|
|
445
|
+
// Native modules and their interfaces in other files and packages, read here so the compile stays pure.
|
|
446
|
+
nativeFiles: readNativeFiles(source, appDir),
|
|
441
447
|
screen: screens?.[0] ?? null, // null without --screen
|
|
442
448
|
screens,
|
|
443
449
|
release,
|
|
@@ -715,6 +721,41 @@ function flash(args) {
|
|
|
715
721
|
}
|
|
716
722
|
}
|
|
717
723
|
|
|
724
|
+
/** `embedded-react native-header <package or file> [--out dir]` */
|
|
725
|
+
async function nativeHeader(args) {
|
|
726
|
+
const cwd = process.cwd();
|
|
727
|
+
const outDir = resolve(cwd, optValue(args, '--out')?.value ?? '.');
|
|
728
|
+
const [target] = bareArgs(args, ['--out']);
|
|
729
|
+
if (!target) {
|
|
730
|
+
console.error(
|
|
731
|
+
'Pass the package or the .ts file that declares the modules.\n',
|
|
732
|
+
);
|
|
733
|
+
usage();
|
|
734
|
+
process.exit(1);
|
|
735
|
+
}
|
|
736
|
+
const {nativeHeaders, readModuleFile} = await import('./aot/compile.mjs');
|
|
737
|
+
let shownFile = target;
|
|
738
|
+
try {
|
|
739
|
+
const {path, source, files} = readModuleFile(target, cwd);
|
|
740
|
+
shownFile = relative(cwd, path);
|
|
741
|
+
const headers = nativeHeaders(source, files);
|
|
742
|
+
mkdirSync(outDir, {recursive: true});
|
|
743
|
+
for (const header of headers) {
|
|
744
|
+
writeFileSync(resolve(outDir, header.file), header.text);
|
|
745
|
+
console.log(
|
|
746
|
+
`✓ ${header.module} → ${relative(cwd, resolve(outDir, header.file))}`,
|
|
747
|
+
);
|
|
748
|
+
}
|
|
749
|
+
} catch (error) {
|
|
750
|
+
const where = error?.aotLoc
|
|
751
|
+
? `${shownFile}:${error.aotLoc.line}:${error.aotLoc.column + 1}: `
|
|
752
|
+
: '';
|
|
753
|
+
console.error(where + (error?.message || String(error)));
|
|
754
|
+
if (error?.aotHint) console.error(` ${error.aotHint}`);
|
|
755
|
+
process.exit(1);
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
|
|
718
759
|
const [command, ...commandArgs] = process.argv.slice(2);
|
|
719
760
|
if (command === 'dev') {
|
|
720
761
|
await dev(commandArgs);
|
|
@@ -726,6 +767,8 @@ if (command === 'dev') {
|
|
|
726
767
|
pack(commandArgs);
|
|
727
768
|
} else if (command === 'flash') {
|
|
728
769
|
flash(commandArgs);
|
|
770
|
+
} else if (command === 'native-header') {
|
|
771
|
+
await nativeHeader(commandArgs);
|
|
729
772
|
} else if (
|
|
730
773
|
!command ||
|
|
731
774
|
command === '--help' ||
|