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
@@ -20,14 +20,19 @@ import { aotError } from './diagnostics.mjs';
20
20
  import { foldBoot, isBoot } from './boot.mjs';
21
21
  import { mergeBranchElements } from './branch-merge.mjs';
22
22
  import { evalStatic, setBinding, withUndefined } from './static-eval.mjs';
23
- import { lowerExpr, lowerSourcedWide } from './expressions.mjs';
24
- import { isFn, declaredNames, collectState, componentReturnJSX, collectHelpers, collectCallbacks, collectMemos, collectEffects, usesState, bindConst, settleBootInitials, } from './collect.mjs';
23
+ import { holdSame, shapeOf } from './typing.mjs';
24
+ import { bindBuiltValues, foldRuntime, listItemOf, lowerExpr, lowerLocal, lowerSourcedWide, } from './expressions.mjs';
25
+ import { BOOL, INT, STRUCT } from './ir/types.mjs';
26
+ import { isFn, bindFoldedPattern, declaredNames, collectState, componentReturnJSX, collectHelpers, collectCallbacks, collectMemos, collectEffects, collectNativeEvents, usesState, bindConst, settleBootInitials, } from './collect.mjs';
25
27
  import { collectAnims, collectRefs } from './animations.mjs';
26
28
  import { attrExpr } from './style-text.mjs';
27
29
  import { inlineFragments } from './elements/props.mjs';
28
- import { lowerEffect } from './handlers.mjs';
30
+ import { lowerEffect, lowerNativeEvent } from './handlers.mjs';
31
+ import { bindDerivedList, derivedListOf, renderBindings, rowItemOf, } from './list-order.mjs';
29
32
  import { collectPanResponders, panSpreadTarget } from './pan-responder.mjs';
30
33
  import { emitNode } from './nodes.mjs';
34
+ import { bindContextUses, providedContextOf, providedEnvironment, } from './contexts.mjs';
35
+ import { narrowOn, narrowed, narrowedScope } from './tagged-records.mjs';
31
36
  /*----------------------------------------------------------------------------------------------------------------------
32
37
  - Implementation
33
38
  ---------------------------------------------------------------------------------------------------------------------*/
@@ -73,47 +78,64 @@ function extractProps(openingElement, scope, environment) {
73
78
  attribute.name.name === 'key') {
74
79
  continue;
75
80
  }
76
- // A callback prop (inline arrow, caller's useCallback or forwarded prop) is an ` fn `, resolved at its handler.
77
- const node = attrExpr(attribute);
78
- if (isFn(node)) {
79
- props[attribute.name.name] = {
80
- isCallback: true,
81
- node,
82
- };
83
- continue;
84
- }
85
- if (node.type === 'Identifier' && environment.callbacks?.has(node.name)) {
86
- props[attribute.name.name] = {
87
- isCallback: true,
88
- node: environment.callbacks.get(node.name),
89
- };
90
- continue;
91
- }
92
- if (node.type === 'Identifier' &&
93
- environment.functionProps?.has(node.name)) {
94
- props[attribute.name.name] = {
95
- isCallback: true,
96
- ...environment.functionProps.get(node.name),
97
- }; // forward original {node, env, state}
98
- continue;
99
- }
100
- // Any other prop folds to a constant when it can, else becomes a runtime expression.
101
- try {
102
- const propName = attribute.name.name;
103
- props[propName] = {
104
- static: true,
105
- value: bindConst(propName, evalStatic(node, scope, 'boot')),
106
- };
107
- }
108
- catch {
109
- props[attribute.name.name] = {
110
- static: false,
111
- ...lowerSourcedWide(node, environment),
112
- };
113
- }
81
+ props[attribute.name.name] = propDescriptorOf(attribute.name.name, attrExpr(attribute), scope, environment);
114
82
  }
115
83
  return props;
116
84
  }
85
+ /**
86
+ * A prop's descriptor (see extractProps): a callback, a list, a constant, or a runtime expression.
87
+ *
88
+ * @param propName The prop's name.
89
+ * @param node Its value.
90
+ * @param scope Compile-time constants in reach where the value is written.
91
+ * @param environment The expression environment there.
92
+ *
93
+ * @returns The descriptor.
94
+ */
95
+ export function propDescriptorOf(propName, node, scope, environment) {
96
+ // A callback prop (inline arrow, caller's useCallback or forwarded prop) is an ` fn `, resolved at its handler.
97
+ if (isFn(node)) {
98
+ return { isCallback: true, node };
99
+ }
100
+ if (node.type === 'Identifier' && environment.callbacks?.has(node.name)) {
101
+ return { isCallback: true, node: environment.callbacks.get(node.name) };
102
+ }
103
+ // A forwarded callback prop keeps its original {node, env, state}.
104
+ if (node.type === 'Identifier' && environment.functionProps?.has(node.name)) {
105
+ return { isCallback: true, ...environment.functionProps.get(node.name) };
106
+ }
107
+ // A list state passes as it is, and so does a list filtered or sorted in render.
108
+ const listState = node.type === 'Identifier'
109
+ ? environment.state.get(node.name)
110
+ : derivedListOf(node, environment);
111
+ if (listState?.kind === 'list') {
112
+ return { static: 'list', list: listState };
113
+ }
114
+ // Any other prop folds to a constant when it can, else becomes a runtime expression. A list's item by index
115
+ // passes as it is, and an array or object built from runtime values folds with those values kept as runtime
116
+ // reads.
117
+ try {
118
+ return {
119
+ static: true,
120
+ value: bindConst(propName, evalStatic(node, scope, 'boot')),
121
+ };
122
+ }
123
+ catch {
124
+ const item = listItemOf(node, environment);
125
+ return item
126
+ ? {
127
+ static: false,
128
+ ...item,
129
+ source: { kind: 'local', local: item, location: node.loc?.start },
130
+ expression: { kind: 'local', local: item, type: STRUCT },
131
+ }
132
+ : node.type === 'ArrayExpression' || node.type === 'ObjectExpression'
133
+ ? { static: true, value: foldRuntime(node, environment, scope) }
134
+ : Object.assign(lowerSourcedWide(node, environment), {
135
+ static: false,
136
+ });
137
+ }
138
+ }
117
139
  /**
118
140
  * Maps a component's parameter to its prop descriptors (handles destructure rename and defaults).
119
141
  *
@@ -132,7 +154,7 @@ function bindParams(functionNode, props, scope) {
132
154
  if (parameter.type === 'Identifier') {
133
155
  const propsObject = {};
134
156
  for (const [propName, descriptor] of Object.entries(props)) {
135
- if (!descriptor.static) {
157
+ if (descriptor.static !== true) {
136
158
  throw new Error('AOT: dynamic props require a destructured component parameter (e.g. `function C({ x })`)');
137
159
  }
138
160
  propsObject[propName] = descriptor.value;
@@ -168,7 +190,7 @@ function bindParams(functionNode, props, scope) {
168
190
  };
169
191
  }
170
192
  descriptor ??= { static: true, value: undefined };
171
- if (descriptor.static)
193
+ if (descriptor.static === true)
172
194
  setBinding(defaultScope, bindName, descriptor.value);
173
195
  bindings.set(bindName, descriptor);
174
196
  }
@@ -178,6 +200,47 @@ function bindParams(functionNode, props, scope) {
178
200
  }
179
201
  return bindings;
180
202
  }
203
+ /**
204
+ * Binds a prop's descriptor to a name in a component's tables: a callback to its callback props, a list state to its
205
+ * lists, a constant to its scope, and a runtime value to its locals.
206
+ *
207
+ * @param name The name.
208
+ * @param descriptor The prop.
209
+ * @param tables The component's tables.
210
+ * @param environment Where a callback without one of its own is lowered.
211
+ * @param state The state table it is lowered with.
212
+ */
213
+ export function bindProp(name, descriptor, tables, environment, state) {
214
+ if (descriptor.isCallback) {
215
+ tables.functionProps.set(name, {
216
+ node: descriptor.node,
217
+ environment: descriptor.environment ?? environment,
218
+ state: descriptor.state ?? state,
219
+ });
220
+ }
221
+ else if (descriptor.static === 'list') {
222
+ tables.lists.set(name, descriptor.list);
223
+ delete tables.scope[name];
224
+ }
225
+ else if (descriptor.static) {
226
+ setBinding(tables.scope, name, descriptor.value);
227
+ }
228
+ else {
229
+ // What it holds crosses the prop boundary, a boolean as a boolean (extractProps supplies it).
230
+ tables.locals.set(name, holdSame({
231
+ name: descriptor.name,
232
+ struct: descriptor.struct,
233
+ list: descriptor.list,
234
+ row: descriptor.row,
235
+ index: descriptor.index,
236
+ guarded: descriptor.guarded,
237
+ source: descriptor.source,
238
+ expression: descriptor.expression,
239
+ }, descriptor));
240
+ // A dynamic prop is a runtime binding; drop any same-named module const so no fold emits it instead.
241
+ delete tables.scope[name];
242
+ }
243
+ }
181
244
  /**
182
245
  * True if `expression` is how a component body refers to its children (destructured {children} or props.children).
183
246
  *
@@ -242,35 +305,20 @@ export function emitComponent(element, scope, output, environment, state, option
242
305
  const childLocals = new Map();
243
306
  // Callback props resolve to the caller's function, env and state; inherit the caller's own so they forward.
244
307
  const functionProps = new Map(environment.functionProps);
245
- // Bind each prop: a callback to fnProps, a constant to the child's scope, a runtime value to its locals.
308
+ // Bind each prop: a callback to fnProps, a constant to the child's scope, a runtime value to its locals, and a
309
+ // list state to its state.
310
+ const listProps = new Map();
311
+ const tables = {
312
+ scope: childScope,
313
+ locals: childLocals,
314
+ functionProps,
315
+ lists: listProps,
316
+ };
246
317
  for (const [name, descriptor] of bindParams(functionNode, extractProps(element.openingElement, scope, environment), moduleScope)) {
247
318
  // The children binding comes from the slot, not from a value prop.
248
319
  if (childrenRef?.kind === 'local' && name === childrenRef?.name)
249
320
  continue;
250
- if (descriptor.isCallback) {
251
- functionProps.set(name, {
252
- node: descriptor.node,
253
- environment: descriptor.environment ?? environment,
254
- state: descriptor.state ?? state,
255
- });
256
- }
257
- else if (descriptor.static) {
258
- setBinding(childScope, name, descriptor.value);
259
- }
260
- else {
261
- childLocals.set(name, {
262
- name: descriptor.name,
263
- slotKind: descriptor.slotKind,
264
- struct: descriptor.struct,
265
- isBoolean: descriptor.isBoolean, // keep boolean-ness across the prop boundary (extractProps supplies it)
266
- isLiteral: descriptor.isLiteral,
267
- wide: descriptor.wide,
268
- source: descriptor.source,
269
- expression: descriptor.expression,
270
- });
271
- // A dynamic prop is a runtime binding; drop any same-named module const so no fold emits it instead.
272
- delete childScope[name];
273
- }
321
+ bindProp(name, descriptor, tables, environment, state);
274
322
  }
275
323
  // Capture the call-site children with the caller's scope and env, for wherever the body renders them.
276
324
  const children = {
@@ -282,21 +330,35 @@ export function emitComponent(element, scope, output, environment, state, option
282
330
  // Each inlined instance namespaces its own hooks under a unique `c<N>_` prefix so instances stay independent.
283
331
  const prefix = `c${output.instanceCount++}_`;
284
332
  // Fold the child body's static consts as lowerApp does for App; they shadow same-named module consts.
333
+ let contextNames = new Set();
285
334
  if (functionNode.body.type === 'BlockStatement') {
286
335
  // As in App: the body's own names, hook bindings included, shadow module ones before anything folds.
287
336
  for (const name of declaredNames(functionNode.body.body)) {
288
337
  delete childScope[name];
289
338
  }
339
+ // A value read from a context binds as a prop does, from the nearest provider above the instance.
340
+ contextNames = bindContextUses(functionNode.body, environment, tables);
290
341
  for (const statement of functionNode.body.body) {
291
342
  if (statement.type !== 'VariableDeclaration' ||
292
343
  statement.kind !== 'const') {
293
344
  continue;
294
345
  }
295
346
  for (const declarator of statement.declarations) {
347
+ if (declarator.id.type === 'ObjectPattern' && declarator.init) {
348
+ try {
349
+ bindFoldedPattern(childScope, declarator.id, declarator.init);
350
+ }
351
+ catch {
352
+ // Not a fold, so bindBuiltValues binds it.
353
+ }
354
+ continue;
355
+ }
296
356
  if (declarator.id.type !== 'Identifier' || !declarator.init)
297
357
  continue;
298
358
  if (childLocals.has(declarator.id.name))
299
359
  continue; // a dynamic prop of that name is already bound
360
+ if (contextNames.has(declarator.id.name))
361
+ continue;
300
362
  try {
301
363
  setBinding(childScope, declarator.id.name, bindConst(declarator.id.name, evalStatic(declarator.init, withUndefined(childScope), 'boot')));
302
364
  }
@@ -309,13 +371,13 @@ export function emitComponent(element, scope, output, environment, state, option
309
371
  }
310
372
  // Collect the child's own hooks under its prefix; its state slots are runtime bindings, so they leave the scope.
311
373
  const childAnimations = collectAnims(functionNode.body, childScope, prefix);
312
- const childRefs = collectRefs(functionNode.body, childScope, prefix, environment.wide);
374
+ const childRefs = collectRefs(functionNode.body, childScope, prefix);
313
375
  const childPans = collectPanResponders(functionNode.body, prefix);
314
376
  const childCallbacks = collectCallbacks(functionNode.body);
315
377
  const childMemos = collectMemos(functionNode.body);
316
378
  let childState = state;
317
379
  if (usesState(functionNode)) {
318
- childState = collectState(functionNode.body, childScope, environment.caps, prefix, environment.wide);
380
+ childState = collectState(functionNode.body, childScope, environment.caps, prefix, output.program);
319
381
  settleBootInitials(childState.byName.values());
320
382
  output.childStateRecords.push(...childState.byName.values());
321
383
  for (const name of childState.byName.keys()) {
@@ -335,7 +397,11 @@ export function emitComponent(element, scope, output, environment, state, option
335
397
  locals: childLocals,
336
398
  children,
337
399
  functionProps,
338
- state: childState.byName,
400
+ // A child without state of its own reads none of its caller's by name.
401
+ state: new Map([
402
+ ...(childState === state ? [] : childState.byName),
403
+ ...listProps,
404
+ ]),
339
405
  animations: childAnimations,
340
406
  refs: childRefs,
341
407
  pans: childPans,
@@ -344,30 +410,76 @@ export function emitComponent(element, scope, output, environment, state, option
344
410
  callbackPrefix: prefix,
345
411
  };
346
412
  // Resolve memos in order (fold, else a local expression), then compile its effects, all in the child's env/state.
347
- for (const [name, expression] of childMemos) {
413
+ for (const { name, expression, isMemo } of renderBindings(functionNode.body, childMemos)) {
414
+ if (!isMemo) {
415
+ bindDerivedList(name, expression, childEnvironment);
416
+ continue;
417
+ }
348
418
  try {
349
419
  setBinding(childScope, name, bindConst(name, evalStatic(expression, childScope, 'boot')));
350
420
  }
351
421
  catch {
352
- const memo = lowerSourcedWide(expression, childEnvironment);
353
- childLocals.set(name, {
354
- slotKind: memo.slotKind,
355
- isBoolean: memo.isBoolean,
356
- isLiteral: memo.isLiteral,
357
- wide: memo.wide,
358
- source: memo.source,
359
- expression: memo.expression,
360
- });
422
+ if (bindDerivedList(name, expression, childEnvironment)) {
423
+ continue;
424
+ }
425
+ if (expression.type === 'ArrayExpression' ||
426
+ expression.type === 'ObjectExpression') {
427
+ setBinding(childScope, name, foldRuntime(expression, childEnvironment));
428
+ continue;
429
+ }
430
+ childLocals.set(name, lowerLocal(expression, childEnvironment));
361
431
  delete childScope[name]; // a runtime binding beats a module const of its name in every fold
362
432
  }
363
433
  }
434
+ if (functionNode.body.type === 'BlockStatement') {
435
+ bindBuiltValues(functionNode.body.body, childEnvironment);
436
+ }
364
437
  for (const effect of collectEffects(functionNode.body)) {
365
438
  lowerEffect(effect, childEnvironment, childState, output);
366
439
  }
440
+ for (const call of collectNativeEvents(functionNode.body)) {
441
+ lowerNativeEvent(call, childEnvironment, childState, output);
442
+ }
367
443
  // Emit the JSX the component returns, in the child's own scope.
368
444
  const root = emitNode(componentReturnJSX(functionNode, childScope), childScope, output, childEnvironment, childState, options);
369
445
  return { kind: 'component', id: root.id, name: componentName, prefix, root };
370
446
  }
447
+ /**
448
+ * A context's provider where one element is wanted, as a component's root or a branch: the one element it wraps,
449
+ * emitted reading its value. It may wrap `{children}` that are one element.
450
+ *
451
+ * @param element The provider.
452
+ * @param context The context it provides.
453
+ * @param scope Compile-time constants in reach.
454
+ * @param output Everything emitted so far.
455
+ * @param environment The expression environment.
456
+ * @param state The component's state table.
457
+ * @param options How the element is placed.
458
+ *
459
+ * @returns The element.
460
+ */
461
+ export function emitProvider(element, context, scope, output, environment, state, options) {
462
+ const provided = providedEnvironment(element, context, scope, environment, state);
463
+ const rendered = (nodes) => inlineFragments(nodes).filter(child => !(child.type === 'JSXText' && !child.value.trim()) &&
464
+ !(child.type === 'JSXExpressionContainer' &&
465
+ child.expression.type === 'JSXEmptyExpression'));
466
+ const children = rendered(element.children);
467
+ if (children.length === 1 && children[0].type === 'JSXElement') {
468
+ return emitNode(children[0], scope, output, provided, state, options);
469
+ }
470
+ // `{children}`, when they are one element: emitted in the caller's scope, reading this provider.
471
+ if (children.length === 1 &&
472
+ children[0].type === 'JSXExpressionContainer' &&
473
+ isChildrenRef(children[0].expression, environment)) {
474
+ const slot = environment.children;
475
+ const nodes = rendered(slot.nodes);
476
+ if (nodes.length === 1 && nodes[0].type === 'JSXElement') {
477
+ return emitNode(nodes[0], slot.scope, output, { ...slot.environment, providers: provided.providers }, state, options);
478
+ }
479
+ }
480
+ throw aotError(`AOT: <${context}.Provider> must wrap one element here`, "a provider adds no node of its own, so where one element is wanted (a component's root, a branch) it must " +
481
+ 'wrap exactly one: put its children in a <View>.');
482
+ }
371
483
  /**
372
484
  * Emits an element / component child. options.show toggles its display.
373
485
  *
@@ -398,13 +510,19 @@ function emitElementInto(node, scope, output, environment, state, options) {
398
510
  * @returns The rows.
399
511
  */
400
512
  function emitMap(call, scope, output, environment, state) {
401
- // The array must fold at compile time and the callback must be inline.
513
+ // The array must fold at compile time, its values worked out at run time aside, and the callback must be inline.
514
+ const target = call.callee.object;
402
515
  let items;
403
- try {
404
- items = evalStatic(call.callee.object, scope);
516
+ if (target.type === 'ArrayExpression') {
517
+ items = foldRuntime(target, environment, scope);
405
518
  }
406
- catch {
407
- throw new Error('AOT: .map target must be a compile-time-constant array (dynamic lists not yet supported)');
519
+ else {
520
+ try {
521
+ items = evalStatic(target, scope);
522
+ }
523
+ catch {
524
+ throw new Error('AOT: .map target must be a list state, a constant array, or an array literal built in render');
525
+ }
408
526
  }
409
527
  if (!Array.isArray(items)) {
410
528
  throw new Error('AOT: .map target did not resolve to an array');
@@ -441,7 +559,7 @@ function emitMap(call, scope, output, environment, state) {
441
559
  *
442
560
  * @returns The row's environment.
443
561
  */
444
- function rowEnv(environment, parameters, constants, ownLocals = null) {
562
+ export function rowEnv(environment, parameters, constants, ownLocals = null) {
445
563
  // Copy a table without the row's names; one that binds none of them is shared as is.
446
564
  const names = parameters.filter(Boolean);
447
565
  const drop = (bindings) => {
@@ -511,19 +629,39 @@ function emitDynamicMap(call, listState, scope, output, environment, state) {
511
629
  }
512
630
  // A row's item has no slot of its own: only its fields are read, through `struct`.
513
631
  if (itemName) {
514
- rowLocals.set(itemName, {
515
- name: itemName,
516
- struct: listState.struct,
517
- list: listState,
518
- row: rowIndex,
519
- });
520
- }
521
- rows.push(emitElementInto(rowJSX, rowScope, output, rowEnv(environment, [itemName, indexName], rowScope, rowLocals), state, { show: { kind: 'rowInUse', row: rowIndex, list: listState } }));
632
+ rowLocals.set(itemName, rowItemOf(listState, { name: itemName, row: rowIndex }));
633
+ }
634
+ rows.push(emitElementInto(rowJSX, rowScope, output, {
635
+ ...rowEnv(environment, [itemName, indexName], rowScope, rowLocals),
636
+ inListRow: true,
637
+ mounted: mountedWhile(environment, {
638
+ kind: 'compare',
639
+ operator: '<',
640
+ left: { kind: 'number', value: rowIndex, type: INT },
641
+ right: { kind: 'listLength', list: listState, type: INT },
642
+ type: BOOL,
643
+ }),
644
+ }, state, { show: { kind: 'rowInUse', row: rowIndex, list: listState } }));
522
645
  }
523
646
  return { kind: 'pool', list: listState, rows };
524
647
  }
525
- /** The value types whose falsy value, 0, React renders as text. */
526
- const NUMERIC_KINDS = new Set(['int', 'float', 'int64']);
648
+ /**
649
+ * Whether something under a test is mounted: the test, and whatever its surroundings are mounted under.
650
+ *
651
+ * @param environment The environment around the test.
652
+ * @param test What it is built under.
653
+ *
654
+ * @returns The condition.
655
+ */
656
+ const mountedWhile = (environment, test) => environment.mounted
657
+ ? {
658
+ kind: 'logical',
659
+ operator: '&&',
660
+ left: environment.mounted,
661
+ right: test,
662
+ type: BOOL,
663
+ }
664
+ : test;
527
665
  /**
528
666
  * Whether a folded child value renders nothing, as React treats it: false, null, undefined and the empty string.
529
667
  *
@@ -554,8 +692,8 @@ function unsupportedChild(expression, what) {
554
692
  }
555
693
  /**
556
694
  * Lowers one branch of a conditional child: an element, a nested `&&` or ternary, or something that renders
557
- * nothing. A condition that folds picks its branch now; a state-driven one builds every branch and shows each
558
- * only while it is chosen, `within` the branch it sits in.
695
+ * nothing. A condition that folds picks its branch now. A state-driven one builds the branch it picks; in a list
696
+ * row it builds every branch with the row and shows each only while it is chosen, `within` the branch it sits in.
559
697
  *
560
698
  * @param expression The branch.
561
699
  * @param scope Compile-time constants in reach.
@@ -608,27 +746,37 @@ function lowerBranch(expression, scope, output, environment, state, within) {
608
746
  ? lowerBranch(chosen, scope, output, environment, state, within)
609
747
  : null;
610
748
  }
611
- // A state-driven test builds every branch; each shows while the test picks it.
749
+ // A state-driven test builds the branch it picks. A list row builds every branch, each shown while picked.
612
750
  const test = lowerExpr(testNode, environment);
613
- if (isAnd && NUMERIC_KINDS.has(test.type.kind)) {
751
+ if (isAnd && shapeOf(test) === 'number') {
614
752
  const error = aotError('AOT: a number on the left of `&&` renders 0 as text when it is 0', 'React draws the 0 itself, and text needs a <Text>. Test it as a condition: `count > 0 && <El/>`.');
615
753
  if (testNode.loc) {
616
754
  error.aotLoc = testNode.loc.start;
617
755
  }
618
756
  throw error;
619
757
  }
620
- const shown = (negate) => ({
621
- kind: 'condition',
622
- test,
623
- negate,
624
- within,
625
- });
758
+ const toggle = !!environment.inListRow;
759
+ const shown = (negate) => toggle ? { kind: 'condition', test, negate, within } : null;
760
+ // A test on a tagged object's tag narrows the fields each branch can read.
761
+ const narrowing = narrowOn(testNode, environment);
626
762
  return {
627
763
  kind: 'conditional',
628
764
  test,
629
- whenTrue: lowerBranch(whenTrue, scope, output, environment, state, shown(false)),
765
+ toggle,
766
+ whenTrue: lowerBranch(whenTrue, narrowedScope(scope, narrowing.whenTrue), output, {
767
+ ...narrowed(environment, narrowing.whenTrue),
768
+ mounted: mountedWhile(environment, test),
769
+ }, state, shown(false)),
630
770
  whenFalse: whenFalse
631
- ? lowerBranch(whenFalse, scope, output, environment, state, shown(true))
771
+ ? lowerBranch(whenFalse, narrowedScope(scope, narrowing.whenFalse), output, {
772
+ ...narrowed(environment, narrowing.whenFalse),
773
+ mounted: mountedWhile(environment, {
774
+ kind: 'unary',
775
+ operator: '!',
776
+ operand: test,
777
+ type: BOOL,
778
+ }),
779
+ }, state, shown(true))
632
780
  : null,
633
781
  };
634
782
  }
@@ -646,11 +794,10 @@ function lowerBranch(expression, scope, output, environment, state, within) {
646
794
  return null;
647
795
  }
648
796
  /**
649
- * Emits the children of a container node. Everything unrolls at compile time into a fixed set of nodes:
650
- * fragments are inlined, a state-driven condition builds its elements and toggles their display, and a
651
- * `.map` over list state builds a fixed pool of rows. Every child is either emitted or refused with an
652
- * "AOT: …" error: one that would change the node count at runtime, text outside a <Text>, or anything else
653
- * the engine has no node for.
797
+ * Emits the children of a container node. Everything unrolls at compile time: fragments are inlined, a
798
+ * state-driven condition lowers each of its branches, and a `.map` over list state builds a fixed pool of rows.
799
+ * Every child is either emitted or refused with an "AOT: …" error: text outside a <Text>, or anything else the
800
+ * engine has no node for.
654
801
  *
655
802
  * @param children The container's children.
656
803
  * @param scope Compile-time constants in reach.
@@ -664,6 +811,14 @@ export function emitChildren(children, scope, output, environment, state) {
664
811
  const lowered = [];
665
812
  const element = (built) => lowered.push({ kind: 'element', element: built });
666
813
  for (const child of inlineFragments(children)) {
814
+ // A context's provider is no node of its own: its children are emitted in its place, reading its value.
815
+ if (child.type === 'JSXElement') {
816
+ const context = providedContextOf(child, environment);
817
+ if (context) {
818
+ lowered.push(...emitChildren(child.children, scope, output, providedEnvironment(child, context, scope, environment, state), state));
819
+ continue;
820
+ }
821
+ }
667
822
  // An element child is emitted as it is; an {expression} child is lowered by its shape below.
668
823
  if (child.type === 'JSXElement') {
669
824
  element(emitElementInto(child, scope, output, environment, state));
@@ -687,8 +842,12 @@ export function emitChildren(children, scope, output, environment, state) {
687
842
  if (expression.type === 'JSXEmptyExpression')
688
843
  continue;
689
844
  if (isChildrenRef(expression, environment)) {
690
- // {children} / {props.children}: emit the captured call-site children, in the caller's scope/env.
691
- lowered.push(...emitChildren(environment.children.nodes, environment.children.scope, output, environment.children.environment, state));
845
+ // {children} / {props.children}: emit the captured call-site children, in the caller's scope/env. They read
846
+ // a context from the providers where they are rendered, as in React.
847
+ lowered.push(...emitChildren(environment.children.nodes, environment.children.scope, output, {
848
+ ...environment.children.environment,
849
+ providers: environment.providers,
850
+ }, state));
692
851
  continue;
693
852
  }
694
853
  if (expression.type === 'CallExpression' &&
@@ -698,7 +857,7 @@ export function emitChildren(children, scope, output, environment, state) {
698
857
  const mapTarget = expression.callee.object;
699
858
  const stateRecord = mapTarget.type === 'Identifier'
700
859
  ? environment.state.get(mapTarget.name)
701
- : null;
860
+ : derivedListOf(mapTarget, environment);
702
861
  if (stateRecord?.kind === 'list') {
703
862
  lowered.push(emitDynamicMap(expression, stateRecord, scope, output, environment, state));
704
863
  }