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.
Files changed (74) hide show
  1. package/NOTICE +5 -0
  2. package/README.md +1 -0
  3. package/aot/app-file-loader.mjs +116 -0
  4. package/aot/compile.mjs +16 -46
  5. package/aot/compiler/animations.mjs +299 -57
  6. package/aot/compiler/boot.mjs +27 -2
  7. package/aot/compiler/check.mjs +61 -9
  8. package/aot/compiler/collect.mjs +516 -136
  9. package/aot/compiler/contexts.mjs +391 -0
  10. package/aot/compiler/control-flow.mjs +276 -117
  11. package/aot/compiler/custom-hooks.mjs +686 -0
  12. package/aot/compiler/elements/button.mjs +182 -0
  13. package/aot/compiler/elements/image-background.mjs +76 -0
  14. package/aot/compiler/elements/image.mjs +2 -1
  15. package/aot/compiler/elements/props.mjs +38 -3
  16. package/aot/compiler/elements/rewrite.mjs +118 -0
  17. package/aot/compiler/elements/section-list.mjs +370 -0
  18. package/aot/compiler/expressions.mjs +1351 -97
  19. package/aot/compiler/handlers.mjs +1761 -365
  20. package/aot/compiler/helper-values.mjs +312 -0
  21. package/aot/compiler/ir/nodes.mjs +40 -1
  22. package/aot/compiler/ir/reads.mjs +206 -5
  23. package/aot/compiler/ir/text.mjs +5 -1
  24. package/aot/compiler/ir/types.mjs +12 -0
  25. package/aot/compiler/kept-states.mjs +177 -0
  26. package/aot/compiler/list-items.mjs +226 -0
  27. package/aot/compiler/list-order.mjs +586 -0
  28. package/aot/compiler/list-split.mjs +259 -0
  29. package/aot/compiler/loops.mjs +582 -0
  30. package/aot/compiler/lower.mjs +191 -53
  31. package/aot/compiler/native-files.mjs +439 -0
  32. package/aot/compiler/native-header.mjs +335 -0
  33. package/aot/compiler/native-modules.mjs +1173 -0
  34. package/aot/compiler/nodes.mjs +28 -22
  35. package/aot/compiler/options.mjs +3 -2
  36. package/aot/compiler/out.mjs +14 -1
  37. package/aot/compiler/package/animations.mjs +133 -23
  38. package/aot/compiler/package/assemble.mjs +2 -4
  39. package/aot/compiler/package/bytecode.mjs +37 -8
  40. package/aot/compiler/package/checks.mjs +4 -0
  41. package/aot/compiler/package/codegen.mjs +870 -31
  42. package/aot/compiler/package/enums.mjs +10 -0
  43. package/aot/compiler/package/format.mjs +12 -1
  44. package/aot/compiler/package/lower.mjs +658 -161
  45. package/aot/compiler/package/string-caps.mjs +515 -0
  46. package/aot/compiler/package/tables.mjs +24 -0
  47. package/aot/compiler/package/writer.mjs +22 -0
  48. package/aot/compiler/parse.mjs +3 -1
  49. package/aot/compiler/record-shapes.mjs +246 -0
  50. package/aot/compiler/records.mjs +654 -0
  51. package/aot/compiler/reducers.mjs +272 -0
  52. package/aot/compiler/static-eval.mjs +315 -7
  53. package/aot/compiler/style-text.mjs +18 -3
  54. package/aot/compiler/svg.mjs +7 -4
  55. package/aot/compiler/switch-cases.mjs +198 -0
  56. package/aot/compiler/tagged-records.mjs +450 -0
  57. package/aot/compiler/typing.mjs +307 -0
  58. package/aot/compiler/updaters.mjs +320 -0
  59. package/aot/compiler/window-dimensions.mjs +94 -0
  60. package/aot/compiler/worked-values.mjs +319 -0
  61. package/aot/demo-file.mjs +30 -0
  62. package/aot/style-map.mjs +50 -2
  63. package/build.mjs +9 -1
  64. package/cli.mjs +44 -1
  65. package/hotreload/pack-split.mjs +29 -40
  66. package/pack-container.mjs +9 -1
  67. package/package.json +1 -1
  68. package/sim/embedded-react.wasm +0 -0
  69. package/sim-server.mjs +22 -22
  70. package/src/embedded-react/Dimensions.js +37 -0
  71. package/src/embedded-react/SectionList.js +1 -3
  72. package/src/embedded-react/index.d.ts +83 -3
  73. package/src/embedded-react/index.js +2 -0
  74. package/src/embedded-react/nativeModule.js +462 -0
@@ -0,0 +1,586 @@
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-order.mts — `filter` and `sort` on a list. In a setter they change its rows in place; in render they make a
17
+ // list of the source's row numbers, which the update rebuilds.
18
+ /*----------------------------------------------------------------------------------------------------------------------
19
+ - Imports
20
+ ---------------------------------------------------------------------------------------------------------------------*/
21
+ import { shapeOf, walkedEntries, walkedKind } from './typing.mjs';
22
+ import { aotError } from './diagnostics.mjs';
23
+ import { isFn } from './collect.mjs';
24
+ import { constantListOf, fieldType, lowerExprWide } from './expressions.mjs';
25
+ import { splitListOf } from './list-split.mjs';
26
+ import { evalStatic, foldScope, rethrowFoldLimit } from './static-eval.mjs';
27
+ import { INT } from './ir/types.mjs';
28
+ import { nextRebuild } from './worked-values.mjs';
29
+ /*----------------------------------------------------------------------------------------------------------------------
30
+ - Constants
31
+ ---------------------------------------------------------------------------------------------------------------------*/
32
+ /** One int field: a row number. */
33
+ const ROW_NUMBERS = {
34
+ fields: [{ key: '', path: [], kind: 'int' }],
35
+ plain: true,
36
+ };
37
+ /*----------------------------------------------------------------------------------------------------------------------
38
+ - Implementation
39
+ ---------------------------------------------------------------------------------------------------------------------*/
40
+ let visits = 0;
41
+ /**
42
+ * A local a filter or sort reads a row's index through. Each has a name no JS name can clash with.
43
+ *
44
+ * @returns The local.
45
+ */
46
+ const visitLocal = () => ({
47
+ slotKind: 'int',
48
+ name: `#visit${visits++}`,
49
+ visit: true,
50
+ });
51
+ /**
52
+ * The inline function a list method is given.
53
+ *
54
+ * @param node The argument.
55
+ * @param what The call, for errors: `items.map`.
56
+ *
57
+ * @returns Its parameters and what it returns.
58
+ */
59
+ export function itemCallback(node, what) {
60
+ if (!isFn(node)) {
61
+ throw aotError(`AOT: ${what} needs an inline function`, `write the function in the call: ${what}((item, i) => …).`);
62
+ }
63
+ const names = node.params.map(parameter => {
64
+ if (parameter.type !== 'Identifier') {
65
+ throw aotError(`AOT: ${what}'s function must name its parameters`, 'name the item and its index, e.g. (item, i) => …, and read fields as item.done.');
66
+ }
67
+ return parameter.name;
68
+ });
69
+ let { body } = node;
70
+ if (body.type === 'BlockStatement') {
71
+ const [statement] = body.body;
72
+ if (body.body.length !== 1 ||
73
+ statement.type !== 'ReturnStatement' ||
74
+ !statement.argument) {
75
+ throw aotError(`AOT: ${what}'s function must return a single expression`, 'return one value, e.g. (item, i) => (i === n ? {...item, done: true} : item).');
76
+ }
77
+ body = statement.argument;
78
+ }
79
+ return { itemName: names[0], indexName: names[1], body };
80
+ }
81
+ /**
82
+ * The method a call makes on an object: `x.filter(…)` gives `filter`.
83
+ *
84
+ * @param node The expression.
85
+ *
86
+ * @returns The method and what it is called on, or null when it is no such call.
87
+ */
88
+ function methodCall(node) {
89
+ if (node.type !== 'CallExpression' ||
90
+ node.callee.type !== 'MemberExpression' ||
91
+ node.callee.computed ||
92
+ node.callee.property.type !== 'Identifier') {
93
+ return null;
94
+ }
95
+ return {
96
+ method: node.callee.property.name,
97
+ object: node.callee.object,
98
+ call: node,
99
+ };
100
+ }
101
+ /**
102
+ * The list a `sort` sorts a copy of: `[...items]`, `items.slice()`, or the result of a filter or sort, which is a
103
+ * copy already. `items.sort(…)` would sort the list itself.
104
+ *
105
+ * @param node What `sort` is called on.
106
+ *
107
+ * @returns The list it copies, the node itself for a result that is a copy, or null when it is no copy.
108
+ */
109
+ function copied(node) {
110
+ if (node.type === 'ArrayExpression' &&
111
+ node.elements.length === 1 &&
112
+ node.elements[0]?.type === 'SpreadElement') {
113
+ return node.elements[0].argument;
114
+ }
115
+ const call = methodCall(node);
116
+ if (call?.method === 'slice' &&
117
+ (call.call.arguments.length === 0 ||
118
+ (call.call.arguments.length === 1 &&
119
+ call.call.arguments[0].type === 'NumericLiteral' &&
120
+ call.call.arguments[0].value === 0))) {
121
+ return call.object;
122
+ }
123
+ if (call &&
124
+ (call.method === 'filter' ||
125
+ call.method === 'toSorted' ||
126
+ call.method === 'sort')) {
127
+ return node;
128
+ }
129
+ return null;
130
+ }
131
+ /**
132
+ * Reads a chain of filters and sorts: `items.filter(…)`, `items.toSorted(…)`, `[...items].sort(…)` and those
133
+ * one after another.
134
+ *
135
+ * @param node The expression.
136
+ *
137
+ * @returns The chain, or null when the expression is none.
138
+ */
139
+ export function listChainOf(node) {
140
+ const steps = [];
141
+ let current = node;
142
+ for (;;) {
143
+ const call = methodCall(current);
144
+ if (!call)
145
+ break;
146
+ const [callback] = call.call.arguments;
147
+ if (call.call.arguments.length > 1 || callback?.type === 'SpreadElement') {
148
+ break;
149
+ }
150
+ if (call.method === 'filter' || call.method === 'toSorted') {
151
+ steps.unshift({
152
+ kind: call.method === 'filter' ? 'filter' : 'sort',
153
+ callback,
154
+ what: `.${call.method}`,
155
+ });
156
+ current = call.object;
157
+ continue;
158
+ }
159
+ if (call.method !== 'sort')
160
+ break;
161
+ const list = copied(call.object);
162
+ if (!list) {
163
+ throw aotError('AOT: sort() sorts the list itself, which would change state in place', 'sort a copy: [...items].sort((a, b) => a.n - b.n), or items.toSorted((a, b) => a.n - b.n).');
164
+ }
165
+ steps.unshift({ kind: 'sort', callback, what: '.sort' });
166
+ current = list;
167
+ }
168
+ if (!steps.length)
169
+ return null;
170
+ const name = current.type === 'Identifier' ? current.name : steps[0].what.slice(1);
171
+ steps.forEach(step => {
172
+ step.what = `${name}${step.what}`;
173
+ });
174
+ return { base: current, steps };
175
+ }
176
+ /**
177
+ * An environment where a function's parameters name what a step binds them to.
178
+ *
179
+ * @param environment The environment around the call.
180
+ * @param bindings Each parameter's name, if it has one, and its local.
181
+ *
182
+ * @returns The environment.
183
+ */
184
+ function boundEnvironment(environment, bindings) {
185
+ const locals = new Map(environment.locals);
186
+ let { state } = environment;
187
+ for (const [name, local] of bindings) {
188
+ if (!name)
189
+ continue;
190
+ locals.set(name, local);
191
+ if (state.has(name)) {
192
+ state = new Map(state);
193
+ state.delete(name);
194
+ }
195
+ }
196
+ return { ...environment, locals, state };
197
+ }
198
+ /**
199
+ * What a filter's function keeps a row for: `item => !item.done`, `(item, i) => i < 3`.
200
+ *
201
+ * @param step The filter.
202
+ * @param rows The rows it visits.
203
+ * @param position The row's index.
204
+ *
205
+ * @returns The test.
206
+ */
207
+ function filterTest(step, rows, position) {
208
+ const { itemName, indexName, body } = itemCallback(step.callback, step.what);
209
+ return lowerExprWide(body, boundEnvironment(step.environment, [
210
+ [itemName, rows.itemAt(position)],
211
+ [indexName, position],
212
+ ]));
213
+ }
214
+ /**
215
+ * A compare function's result with each `x || y` written `x ? x : y`, which gives the number JS does: a tie broken
216
+ * by another field, `a.done - b.done || a.n - b.n`.
217
+ *
218
+ * @param node The result.
219
+ *
220
+ * @returns It, rewritten.
221
+ */
222
+ function keepsNumber(node) {
223
+ if (node.type !== 'LogicalExpression' || node.operator !== '||') {
224
+ return node;
225
+ }
226
+ const left = keepsNumber(node.left);
227
+ return {
228
+ type: 'ConditionalExpression',
229
+ test: left,
230
+ consequent: left,
231
+ alternate: keepsNumber(node.right),
232
+ loc: node.loc,
233
+ };
234
+ }
235
+ /**
236
+ * What a sort's compare function gives two rows: above zero when the first goes after the second. With no function,
237
+ * a list of strings sorts by UTF-16 code unit, as JS does; JS sorts any other list by its text, which a package doesn't.
238
+ *
239
+ * @param step The sort.
240
+ * @param rows The rows it visits.
241
+ * @param first The first row's index.
242
+ * @param second The second row's index.
243
+ *
244
+ * @returns The order.
245
+ */
246
+ function sortOrder(step, rows, first, second) {
247
+ const { struct } = rows.source;
248
+ if (!step.callback) {
249
+ const [field] = struct.fields;
250
+ if (!struct.plain || field.kind !== 'string') {
251
+ throw aotError(`AOT: ${step.what}() needs a compare function`, 'without one JS sorts by text, which a package does only for a list of strings. Pass one: ' +
252
+ `${step.what}((a, b) => a.n - b.n).`);
253
+ }
254
+ const read = (position) => ({
255
+ kind: 'itemField',
256
+ item: rows.itemAt(position),
257
+ field,
258
+ type: fieldType(field),
259
+ });
260
+ return {
261
+ kind: 'stringOrder',
262
+ left: read(first),
263
+ right: read(second),
264
+ type: INT,
265
+ };
266
+ }
267
+ const callback = step.callback;
268
+ if (!isFn(callback) || callback.params.length !== 2) {
269
+ throw aotError(`AOT: ${step.what} needs an inline compare function of two items`, `write it in the call: ${step.what}((a, b) => a.n - b.n).`);
270
+ }
271
+ const { itemName, indexName, body } = itemCallback(callback, step.what);
272
+ const order = lowerExprWide(keepsNumber(body), boundEnvironment(step.environment, [
273
+ [itemName, rows.itemAt(first)],
274
+ [indexName, rows.itemAt(second)],
275
+ ]));
276
+ if (shapeOf(order) !== 'number' && shapeOf(order) !== 'bool') {
277
+ throw aotError(`AOT: ${step.what}'s compare function must give a number`, 'give a negative number when a goes first, a positive one when b does, and 0 to keep their order: ' +
278
+ '(a, b) => a.n - b.n, or a.name.localeCompare(b.name).');
279
+ }
280
+ return order;
281
+ }
282
+ /**
283
+ * The statements that filter and sort a list's rows in place, one step after another.
284
+ *
285
+ * @param list The list whose rows they change.
286
+ * @param steps The steps.
287
+ * @param rows The rows they visit.
288
+ *
289
+ * @returns The statements.
290
+ */
291
+ export function orderStatements(list, steps, rows) {
292
+ return steps.map(step => {
293
+ if (step.kind === 'filter') {
294
+ const position = visitLocal();
295
+ return {
296
+ kind: 'filterItems',
297
+ list,
298
+ position,
299
+ keep: filterTest(step, rows, position),
300
+ };
301
+ }
302
+ const first = visitLocal();
303
+ const second = visitLocal();
304
+ return {
305
+ kind: 'sortItems',
306
+ list,
307
+ first,
308
+ second,
309
+ order: sortOrder(step, rows, first, second),
310
+ };
311
+ });
312
+ }
313
+ /**
314
+ * The tests and compare functions of a rebuild.
315
+ *
316
+ * @param statements The rebuild.
317
+ *
318
+ * @returns What they work out.
319
+ */
320
+ const testsOf = (statements) => statements.flatMap(statement => statement.kind === 'fillOrder'
321
+ ? statement.keep
322
+ ? [statement.keep]
323
+ : []
324
+ : statement.kind === 'filterItems'
325
+ ? [statement.keep]
326
+ : statement.kind === 'sortItems'
327
+ ? [statement.order]
328
+ : []);
329
+ /** What the walk of a test skips: where a value came from, and the lists an item reads, checked when they were made. */
330
+ const UNWALKED_KEYS = new Set([
331
+ 'source',
332
+ 'location',
333
+ 'list',
334
+ 'through',
335
+ 'struct',
336
+ ]);
337
+ /** What a handler reads that the update, which rebuilds the list later, can't read as the handler did. */
338
+ const HANDLER_READS = {
339
+ event: 'its event',
340
+ gesture: 'its gesture',
341
+ layout: 'its layout',
342
+ ref: 'a ref',
343
+ clock: 'the clock',
344
+ };
345
+ /**
346
+ * Refuses a test that reads what the update can't work out as the code that made the list sees it: a handler's
347
+ * locals, and in a handler, effect or timer its event, a ref or the clock too.
348
+ *
349
+ * @param tests The tests.
350
+ * @param what The call, for errors.
351
+ * @param environment Where the list is made.
352
+ */
353
+ function checkReads(tests, what, environment) {
354
+ const refuse = (read) => {
355
+ throw aotError(`AOT: ${what} in a handler, effect or timer can't read ${read}`, 'a filtered or sorted list is worked out with the render, from state and props. Filter in the setter ' +
356
+ 'instead, or read the value into state first.');
357
+ };
358
+ const seen = new Set();
359
+ const walk = (value) => {
360
+ if (!value || typeof value !== 'object' || seen.has(value))
361
+ return;
362
+ seen.add(value);
363
+ if (Array.isArray(value)) {
364
+ value.forEach(walk);
365
+ return;
366
+ }
367
+ const node = value;
368
+ const kind = walkedKind(value);
369
+ if (kind === 'local' && node.local) {
370
+ const { local } = node;
371
+ if (!local.list && !local.visit && !local.expression) {
372
+ refuse("the handler's locals");
373
+ }
374
+ }
375
+ if (environment.outsideRender && kind && kind in HANDLER_READS) {
376
+ refuse(HANDLER_READS[kind]);
377
+ }
378
+ for (const [key, child] of walkedEntries(value)) {
379
+ if (!UNWALKED_KEYS.has(key))
380
+ walk(child);
381
+ }
382
+ };
383
+ tests.forEach(walk);
384
+ }
385
+ /**
386
+ * The list a chain starts from: a list state or one worked out in render that a name holds, a split, or a constant
387
+ * array.
388
+ * A chain whose functions fold with a constant array folds whole, so it is no list here.
389
+ *
390
+ * @param chain The chain.
391
+ * @param node The whole chain.
392
+ * @param environment The expression environment.
393
+ *
394
+ * @returns The list, or null.
395
+ */
396
+ function baseListOf(chain, node, environment) {
397
+ const { base } = chain;
398
+ if (base.type === 'Identifier' && environment.state.has(base.name)) {
399
+ const record = environment.state.get(base.name);
400
+ return record.kind === 'list' ? record : null;
401
+ }
402
+ const split = splitListOf(base, environment);
403
+ if (split)
404
+ return split;
405
+ const scope = foldScope(environment, environment.constants ?? {});
406
+ const fold = (part) => {
407
+ try {
408
+ return { value: evalStatic(part, scope, 'boot') };
409
+ }
410
+ catch (error) {
411
+ rethrowFoldLimit(error);
412
+ return null;
413
+ }
414
+ };
415
+ const array = fold(base)?.value;
416
+ if (!Array.isArray(array) || fold(node))
417
+ return null;
418
+ return constantListOf(array, base, environment);
419
+ }
420
+ /**
421
+ * The list a filter or sort chain makes in render: the row numbers of its source that the filters keep, in the
422
+ * order the sorts leave them, rebuilt by the update when what it reads changes. A chain on a list that is one
423
+ * goes on from its steps. A split makes a list of its pieces.
424
+ *
425
+ * @param node The chain.
426
+ * @param environment The expression environment.
427
+ *
428
+ * @returns The list, or null when the expression is no such chain.
429
+ */
430
+ export function derivedListOf(node, environment) {
431
+ const split = splitListOf(node, environment);
432
+ if (split)
433
+ return split;
434
+ const chain = listChainOf(node);
435
+ if (!chain)
436
+ return null;
437
+ const base = baseListOf(chain, node, environment);
438
+ if (!base)
439
+ return null;
440
+ if (base.handlerSplit) {
441
+ throw aotError(`AOT: ${chain.steps[0].what} can't filter or sort a handler's split`, 'a filtered or sorted list is worked out with the render, after the handler. Split and filter it in ' +
442
+ 'render instead: const kept = line.split(",").filter(…).');
443
+ }
444
+ if (environment.inListRow) {
445
+ throw aotError(`AOT: ${chain.steps[0].what} inside a list row isn't supported`, 'filter or sort the list once, outside the rows: const visible = items.filter(…).');
446
+ }
447
+ const lists = environment.derivedLists;
448
+ if (!lists) {
449
+ throw new Error('AOT internal: no table for filtered and sorted lists');
450
+ }
451
+ // Each instance of a component reads its own props, so it gets a list of its own.
452
+ const key = `${environment.callbackPrefix ?? ''}${base.slotName}`;
453
+ const known = lists.get(node)?.get(key);
454
+ if (known)
455
+ return known;
456
+ // A list filtered or sorted already holds rows of its source; a split holds its own.
457
+ const source = base.derived?.source ?? base;
458
+ const steps = [
459
+ ...(base.derived?.steps ?? []),
460
+ ...chain.steps.map(step => ({ ...step, environment })),
461
+ ];
462
+ const count = [...lists.values()].reduce((sum, map) => sum + map.size, 0);
463
+ const list = {
464
+ kind: 'list',
465
+ name: steps.at(-1).what,
466
+ slotName: `o${count}_${source.name}`,
467
+ setter: undefined,
468
+ struct: ROW_NUMBERS,
469
+ items: [],
470
+ capacity: source.capacity,
471
+ initialAt: node.loc?.start,
472
+ };
473
+ // The first filter fills the list with the rows it keeps; the steps after it change those in place.
474
+ const listName = source.name;
475
+ const fill = visitLocal();
476
+ const [first, ...rest] = steps;
477
+ const sourceRows = {
478
+ itemAt: position => ({
479
+ name: `${listName}[]`,
480
+ struct: source.struct,
481
+ list: source,
482
+ index: { kind: 'local', local: position, type: INT },
483
+ }),
484
+ listName,
485
+ source,
486
+ };
487
+ const orderRows = {
488
+ ...sourceRows,
489
+ itemAt: position => ({
490
+ ...sourceRows.itemAt(position),
491
+ through: list,
492
+ }),
493
+ };
494
+ const filtersFirst = first.kind === 'filter';
495
+ const rebuild = [
496
+ {
497
+ kind: 'fillOrder',
498
+ list,
499
+ row: fill,
500
+ keep: filtersFirst ? filterTest(first, sourceRows, fill) : null,
501
+ },
502
+ ...orderStatements(list, filtersFirst ? rest : steps, orderRows),
503
+ ];
504
+ const tests = testsOf(rebuild);
505
+ checkReads(tests, chain.steps[0].what, environment);
506
+ list.derived = { source, steps, rebuild, tests, order: nextRebuild() };
507
+ if (!lists.has(node))
508
+ lists.set(node, new Map());
509
+ lists.get(node).set(key, list);
510
+ return list;
511
+ }
512
+ /**
513
+ * The item a row of a list names: a row of its own, or for a filtered or sorted list, the source row it holds.
514
+ *
515
+ * @param list The list.
516
+ * @param item Where the row is: `row` or `index`, `guarded`, and its name.
517
+ *
518
+ * @returns The item.
519
+ */
520
+ export function rowItemOf(list, item) {
521
+ return list.derived?.source
522
+ ? {
523
+ ...item,
524
+ struct: list.derived.source.struct,
525
+ list: list.derived.source,
526
+ through: list,
527
+ }
528
+ : { ...item, struct: list.struct, list };
529
+ }
530
+ /**
531
+ * A component's memos and the lists its body filters, sorts or splits by name, in the order the body has them, so each
532
+ * can read the ones before it.
533
+ *
534
+ * @param body The component's body.
535
+ * @param memos Its useMemo bindings.
536
+ *
537
+ * @returns Each binding's name, its expression, and whether it is a memo.
538
+ */
539
+ export function renderBindings(body, memos) {
540
+ if (body.type !== 'BlockStatement') {
541
+ return [...memos].map(([name, expression]) => ({
542
+ name,
543
+ expression,
544
+ isMemo: true,
545
+ }));
546
+ }
547
+ const bindings = [];
548
+ for (const statement of body.body) {
549
+ if (statement.type !== 'VariableDeclaration')
550
+ continue;
551
+ for (const { id, init } of statement.declarations) {
552
+ if (id.type !== 'Identifier' || !init)
553
+ continue;
554
+ const memo = memos.get(id.name);
555
+ const method = methodCall(init)?.method;
556
+ if (memo) {
557
+ bindings.push({ name: id.name, expression: memo, isMemo: true });
558
+ }
559
+ else if (statement.kind === 'const' &&
560
+ (method === 'filter' ||
561
+ method === 'toSorted' ||
562
+ method === 'sort' ||
563
+ method === 'split')) {
564
+ bindings.push({ name: id.name, expression: init, isMemo: false });
565
+ }
566
+ }
567
+ }
568
+ return bindings;
569
+ }
570
+ /**
571
+ * Binds a name to the list it filters or sorts in render, which reads then find as a list.
572
+ *
573
+ * @param name The name.
574
+ * @param expression What it is bound to.
575
+ * @param environment The component's environment, whose state table takes it.
576
+ *
577
+ * @returns Whether it named such a list.
578
+ */
579
+ export function bindDerivedList(name, expression, environment) {
580
+ const list = derivedListOf(expression, environment);
581
+ if (!list)
582
+ return false;
583
+ environment.state = new Map(environment.state).set(name, list);
584
+ delete environment.constants?.[name];
585
+ return true;
586
+ }