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,259 @@
|
|
|
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
|
+
// list-split.mts — `split` of a string into a list of strings. A setter fills its list with the pieces; in render
|
|
17
|
+
// the pieces make a list the update rebuilds; a handler's `const` fills a list of its own.
|
|
18
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
19
|
+
- Imports
|
|
20
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
21
|
+
import { whenTyped } from './check.mjs';
|
|
22
|
+
import { shapeOf } from './typing.mjs';
|
|
23
|
+
import { aotError } from './diagnostics.mjs';
|
|
24
|
+
import { buildsText, lowerExpr, lowerExprWide } from './expressions.mjs';
|
|
25
|
+
import { expressionReads } from './ir/reads.mjs';
|
|
26
|
+
import { evalStatic, foldScope, rethrowFoldLimit, splitProblem, } from './static-eval.mjs';
|
|
27
|
+
import { nextRebuild } from './worked-values.mjs';
|
|
28
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
29
|
+
- Implementation
|
|
30
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
31
|
+
/**
|
|
32
|
+
* One string field: a piece.
|
|
33
|
+
*
|
|
34
|
+
* @returns The struct.
|
|
35
|
+
*/
|
|
36
|
+
const pieces = () => ({
|
|
37
|
+
fields: [{ key: '', path: [], kind: 'string' }],
|
|
38
|
+
plain: true,
|
|
39
|
+
});
|
|
40
|
+
/**
|
|
41
|
+
* Reads `text.split(…)`.
|
|
42
|
+
*
|
|
43
|
+
* @param node The expression.
|
|
44
|
+
*
|
|
45
|
+
* @returns The call, or null when the expression is none.
|
|
46
|
+
*/
|
|
47
|
+
export function splitCallOf(node) {
|
|
48
|
+
if (node?.type !== 'CallExpression' ||
|
|
49
|
+
node.callee.type !== 'MemberExpression' ||
|
|
50
|
+
node.callee.computed ||
|
|
51
|
+
node.callee.property.type !== 'Identifier' ||
|
|
52
|
+
node.callee.property.name !== 'split') {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
return { text: node.callee.object, call: node };
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* A string a split reads, kept somewhere: a string built in place lives in the format buffer, not in a register.
|
|
59
|
+
*
|
|
60
|
+
* @param node The string.
|
|
61
|
+
* @param environment The expression environment.
|
|
62
|
+
* @param what What it is to the split, for errors.
|
|
63
|
+
*
|
|
64
|
+
* @returns The string.
|
|
65
|
+
*/
|
|
66
|
+
function keptString(node, environment, what) {
|
|
67
|
+
if (what === 'the separator' &&
|
|
68
|
+
(node.type === 'RegExpLiteral' ||
|
|
69
|
+
(node.type === 'NewExpression' &&
|
|
70
|
+
node.callee.type === 'Identifier' &&
|
|
71
|
+
node.callee.name === 'RegExp'))) {
|
|
72
|
+
throw aotError(...splitProblem(/,/, undefined, false));
|
|
73
|
+
}
|
|
74
|
+
if (buildsText(node, environment)) {
|
|
75
|
+
throw aotError(`AOT: split needs ${what} kept somewhere, not built in place`, 'build it into a local or state first: `const line = `${a},${b}`;` then line.split(",").');
|
|
76
|
+
}
|
|
77
|
+
const value = lowerExprWide(node, environment);
|
|
78
|
+
if (shapeOf(value) !== 'string') {
|
|
79
|
+
throw what === 'the separator'
|
|
80
|
+
? aotError(...splitProblem(0, undefined, false))
|
|
81
|
+
: aotError('AOT: split is only supported on a string', 'JS has split on strings only; convert first: String(n).split("").');
|
|
82
|
+
}
|
|
83
|
+
return value;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* The statement that fills a list of strings with a string's pieces. `split()` with no separator keeps the whole
|
|
87
|
+
* string as one piece.
|
|
88
|
+
*
|
|
89
|
+
* @param list The list.
|
|
90
|
+
* @param split The split.
|
|
91
|
+
* @param environment The expression environment.
|
|
92
|
+
*
|
|
93
|
+
* @returns The statement.
|
|
94
|
+
*/
|
|
95
|
+
function splitStatement(list, split, environment) {
|
|
96
|
+
const [separatorNode, limitNode, ...rest] = split.call.arguments;
|
|
97
|
+
if ([separatorNode, limitNode].some(node => node?.type === 'SpreadElement')) {
|
|
98
|
+
throw aotError('AOT: split cannot take a spread', 'pass the separator and the limit: text.split(",", 3).');
|
|
99
|
+
}
|
|
100
|
+
const problem = splitProblem(separatorNode ? '' : undefined, limitNode ? 0 : undefined, rest.length > 0);
|
|
101
|
+
if (problem) {
|
|
102
|
+
throw aotError(...problem);
|
|
103
|
+
}
|
|
104
|
+
const text = keptString(split.text, environment, 'the string it splits');
|
|
105
|
+
if (!separatorNode) {
|
|
106
|
+
return { kind: 'splitItems', list, text, separator: null, limit: null };
|
|
107
|
+
}
|
|
108
|
+
const separator = keptString(separatorNode, environment, 'the separator');
|
|
109
|
+
let limit = null;
|
|
110
|
+
if (limitNode) {
|
|
111
|
+
const lowered = lowerExpr(limitNode, environment);
|
|
112
|
+
limit = lowered;
|
|
113
|
+
whenTyped(() => {
|
|
114
|
+
if (lowered.type.kind !== 'int') {
|
|
115
|
+
throw aotError("AOT: split's limit must be a whole number", 'pass a whole number, or leave it out to keep every piece.');
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
return { kind: 'splitItems', list, text, separator, limit };
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* What a split reads: its string, its separator and its limit.
|
|
123
|
+
*
|
|
124
|
+
* @param statement The split.
|
|
125
|
+
*
|
|
126
|
+
* @returns The values.
|
|
127
|
+
*/
|
|
128
|
+
const splitValuesOf = (statement) => [
|
|
129
|
+
statement.text,
|
|
130
|
+
...(statement.separator ? [statement.separator] : []),
|
|
131
|
+
...(statement.limit ? [statement.limit] : []),
|
|
132
|
+
];
|
|
133
|
+
/**
|
|
134
|
+
* Lowers a split as a list state's next value, `setWords(text.split(','))`: its pieces replace the rows.
|
|
135
|
+
*
|
|
136
|
+
* @param listState The list.
|
|
137
|
+
* @param nextList The setter's argument.
|
|
138
|
+
* @param environment The expression environment.
|
|
139
|
+
* @param listName What the setter's argument calls the list.
|
|
140
|
+
*
|
|
141
|
+
* @returns The statements, or null when the argument is no split.
|
|
142
|
+
*/
|
|
143
|
+
export function lowerSplitInto(listState, nextList, environment, listName) {
|
|
144
|
+
const split = splitCallOf(nextList);
|
|
145
|
+
if (!split)
|
|
146
|
+
return null;
|
|
147
|
+
const { struct } = listState;
|
|
148
|
+
if (!struct.plain || struct.fields[0].kind !== 'string') {
|
|
149
|
+
throw aotError(`AOT: only a list of strings takes a split, and "${listName}" holds ${struct.plain ? `${struct.fields[0].kind}s` : 'objects'}`, 'keep the pieces in a list of strings: const [words, setWords] = useState([""]).');
|
|
150
|
+
}
|
|
151
|
+
const statement = splitStatement(listState, split, environment);
|
|
152
|
+
const { separator } = statement;
|
|
153
|
+
whenTyped(() => {
|
|
154
|
+
if (separator && expressionReads(separator)?.has(listState)) {
|
|
155
|
+
throw aotError(`AOT: a split into "${listName}" can't split at one of its own items`, `the pieces replace its rows as they are found. Copy the separator into a const first.`);
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
return [statement];
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* The list a split makes in render: the pieces of its string, rebuilt by the update when what it reads changes.
|
|
162
|
+
* A split of a constant folds to an array, so it is no list here.
|
|
163
|
+
*
|
|
164
|
+
* @param node The expression.
|
|
165
|
+
* @param environment The expression environment.
|
|
166
|
+
*
|
|
167
|
+
* @returns The list, or null when the expression is no such split.
|
|
168
|
+
*/
|
|
169
|
+
export function splitListOf(node, environment) {
|
|
170
|
+
const split = splitCallOf(node);
|
|
171
|
+
if (!split)
|
|
172
|
+
return null;
|
|
173
|
+
try {
|
|
174
|
+
evalStatic(node, foldScope(environment, environment.constants ?? {}), 'boot');
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
catch (error) {
|
|
178
|
+
rethrowFoldLimit(error);
|
|
179
|
+
}
|
|
180
|
+
if (environment.outsideRender) {
|
|
181
|
+
throw aotError('AOT: a split in a handler, effect or timer must be a const', 'split into a const first: const parts = text.split(","); then read parts[0] or parts.length.');
|
|
182
|
+
}
|
|
183
|
+
if (environment.inListRow) {
|
|
184
|
+
throw aotError("AOT: split inside a list row isn't supported", 'split once, outside the rows: const parts = text.split(",").');
|
|
185
|
+
}
|
|
186
|
+
const lists = environment.derivedLists;
|
|
187
|
+
if (!lists) {
|
|
188
|
+
throw new Error('AOT internal: no table for lists worked out in render');
|
|
189
|
+
}
|
|
190
|
+
// Each instance of a component reads its own props, so it gets a list of its own.
|
|
191
|
+
const key = environment.callbackPrefix ?? '';
|
|
192
|
+
const known = lists.get(node)?.get(key);
|
|
193
|
+
if (known)
|
|
194
|
+
return known;
|
|
195
|
+
const count = [...lists.values()].reduce((sum, map) => sum + map.size, 0);
|
|
196
|
+
const list = {
|
|
197
|
+
kind: 'list',
|
|
198
|
+
name: 'split',
|
|
199
|
+
slotName: `o${count}_split`,
|
|
200
|
+
setter: undefined,
|
|
201
|
+
struct: pieces(),
|
|
202
|
+
items: [],
|
|
203
|
+
capacity: environment.caps.listCap,
|
|
204
|
+
initialAt: node.loc?.start,
|
|
205
|
+
};
|
|
206
|
+
const statement = splitStatement(list, split, environment);
|
|
207
|
+
list.derived = {
|
|
208
|
+
steps: [],
|
|
209
|
+
rebuild: [statement],
|
|
210
|
+
tests: splitValuesOf(statement),
|
|
211
|
+
order: nextRebuild(),
|
|
212
|
+
};
|
|
213
|
+
if (!lists.has(node))
|
|
214
|
+
lists.set(node, new Map());
|
|
215
|
+
lists.get(node).set(key, list);
|
|
216
|
+
return list;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* A handler's `const parts = text.split(',')`: a list of its own, filled where the const is.
|
|
220
|
+
*
|
|
221
|
+
* @param name The const's name.
|
|
222
|
+
* @param init What it is set to.
|
|
223
|
+
* @param kind `const`, `let` or `var`.
|
|
224
|
+
* @param environment The expression environment.
|
|
225
|
+
*
|
|
226
|
+
* @returns The list and the statements that fill it, or null when the const is no split.
|
|
227
|
+
*/
|
|
228
|
+
export function handlerSplitList(name, init, kind, environment) {
|
|
229
|
+
const split = splitCallOf(init);
|
|
230
|
+
if (!split)
|
|
231
|
+
return null;
|
|
232
|
+
if (kind !== 'const') {
|
|
233
|
+
throw aotError('AOT: a split in a handler, effect or timer must be a const', `its list can't be set again, so declare it with const: const ${name} = text.split(",").`);
|
|
234
|
+
}
|
|
235
|
+
const lists = environment.splitLists;
|
|
236
|
+
if (!lists) {
|
|
237
|
+
throw new Error('AOT internal: no table for split lists');
|
|
238
|
+
}
|
|
239
|
+
const key = environment.callbackPrefix ?? '';
|
|
240
|
+
let list = lists.get(init)?.get(key);
|
|
241
|
+
if (!list) {
|
|
242
|
+
const count = [...lists.values()].reduce((sum, map) => sum + map.size, 0);
|
|
243
|
+
list = {
|
|
244
|
+
kind: 'list',
|
|
245
|
+
name,
|
|
246
|
+
slotName: `h${count}_${name}`,
|
|
247
|
+
setter: undefined,
|
|
248
|
+
struct: pieces(),
|
|
249
|
+
items: [],
|
|
250
|
+
capacity: environment.caps.listCap,
|
|
251
|
+
initialAt: init.loc?.start,
|
|
252
|
+
handlerSplit: true,
|
|
253
|
+
};
|
|
254
|
+
if (!lists.has(init))
|
|
255
|
+
lists.set(init, new Map());
|
|
256
|
+
lists.get(init).set(key, list);
|
|
257
|
+
}
|
|
258
|
+
return { list, statements: [splitStatement(list, split, environment)] };
|
|
259
|
+
}
|