embedded-react 0.19.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 (148) hide show
  1. package/NOTICE +5 -0
  2. package/README.md +8 -3
  3. package/aot/app-file-loader.mjs +116 -0
  4. package/aot/compile.mjs +180 -90
  5. package/aot/compiler/animations.mjs +568 -317
  6. package/aot/compiler/boot.mjs +97 -35
  7. package/aot/compiler/branch-merge.mjs +4 -3
  8. package/aot/compiler/check.mjs +70 -18
  9. package/aot/compiler/collect.mjs +787 -288
  10. package/aot/compiler/contexts.mjs +391 -0
  11. package/aot/compiler/control-flow.mjs +439 -268
  12. package/aot/compiler/custom-hooks.mjs +686 -0
  13. package/aot/compiler/diagnostics.mjs +22 -25
  14. package/aot/compiler/elements/activity-indicator.mjs +25 -24
  15. package/aot/compiler/elements/button.mjs +182 -0
  16. package/aot/compiler/elements/dial.mjs +91 -77
  17. package/aot/compiler/elements/flat-list.mjs +24 -22
  18. package/aot/compiler/elements/image-background.mjs +76 -0
  19. package/aot/compiler/elements/image.mjs +52 -50
  20. package/aot/compiler/elements/modal.mjs +31 -30
  21. package/aot/compiler/elements/props.mjs +61 -26
  22. package/aot/compiler/elements/rewrite.mjs +118 -0
  23. package/aot/compiler/elements/section-list.mjs +370 -0
  24. package/aot/compiler/elements/switch.mjs +42 -34
  25. package/aot/compiler/elements/text-input.mjs +34 -32
  26. package/aot/compiler/expressions.mjs +1685 -392
  27. package/aot/compiler/handlers.mjs +2029 -547
  28. package/aot/compiler/helper-values.mjs +312 -0
  29. package/aot/compiler/ir/functions.mjs +11 -1
  30. package/aot/compiler/ir/nodes.mjs +43 -4
  31. package/aot/compiler/ir/reads.mjs +223 -22
  32. package/aot/compiler/ir/text.mjs +5 -1
  33. package/aot/compiler/ir/types.mjs +26 -14
  34. package/aot/compiler/ir/vectors.mjs +5 -1
  35. package/aot/compiler/kept-states.mjs +177 -0
  36. package/aot/compiler/keyboard.mjs +17 -7
  37. package/aot/compiler/list-items.mjs +226 -0
  38. package/aot/compiler/list-order.mjs +586 -0
  39. package/aot/compiler/list-split.mjs +259 -0
  40. package/aot/compiler/loops.mjs +582 -0
  41. package/aot/compiler/lower.mjs +246 -111
  42. package/aot/compiler/native-files.mjs +439 -0
  43. package/aot/compiler/native-header.mjs +335 -0
  44. package/aot/compiler/native-modules.mjs +1173 -0
  45. package/aot/compiler/nodes.mjs +195 -172
  46. package/aot/compiler/options.mjs +37 -31
  47. package/aot/compiler/out.mjs +32 -23
  48. package/aot/compiler/package/animations.mjs +779 -0
  49. package/aot/compiler/package/arena.mjs +252 -0
  50. package/aot/compiler/package/assemble.mjs +175 -0
  51. package/aot/compiler/package/bytecode.mjs +315 -0
  52. package/aot/compiler/package/checks.mjs +1026 -0
  53. package/aot/compiler/package/codegen.mjs +2168 -0
  54. package/aot/compiler/package/enums.mjs +148 -0
  55. package/aot/compiler/package/format.mjs +771 -0
  56. package/aot/compiler/package/layouts.mjs +136 -0
  57. package/aot/compiler/package/lower.mjs +2103 -0
  58. package/aot/compiler/package/props.mjs +118 -0
  59. package/aot/compiler/package/string-caps.mjs +515 -0
  60. package/aot/compiler/package/tables.mjs +127 -0
  61. package/aot/compiler/package/vectors.mjs +502 -0
  62. package/aot/compiler/package/writer.mjs +686 -0
  63. package/aot/compiler/pan-responder.mjs +69 -61
  64. package/aot/compiler/parse.mjs +23 -21
  65. package/aot/compiler/record-shapes.mjs +246 -0
  66. package/aot/compiler/records.mjs +654 -0
  67. package/aot/compiler/reducers.mjs +272 -0
  68. package/aot/compiler/static-eval.mjs +340 -30
  69. package/aot/compiler/style-text.mjs +178 -174
  70. package/aot/compiler/svg.mjs +480 -251
  71. package/aot/compiler/switch-cases.mjs +198 -0
  72. package/aot/compiler/tagged-records.mjs +450 -0
  73. package/aot/compiler/typing.mjs +307 -0
  74. package/aot/compiler/updaters.mjs +320 -0
  75. package/aot/compiler/window-dimensions.mjs +94 -0
  76. package/aot/compiler/worked-values.mjs +319 -0
  77. package/aot/demo-file.mjs +30 -0
  78. package/aot/screenshot-smoke.mjs +54 -40
  79. package/aot/style-map.mjs +152 -92
  80. package/assets/bake-font.mjs +123 -77
  81. package/assets/bake-image.mjs +39 -22
  82. package/assets/bake-svg.mjs +361 -262
  83. package/assets/build-builtin-font.mjs +11 -7
  84. package/assets/emit-c.mjs +77 -58
  85. package/assets/emit-container.mjs +123 -37
  86. package/assets/emit-pack.mjs +77 -67
  87. package/assets/font-config.mjs +9 -6
  88. package/assets/font-sizes.mjs +163 -123
  89. package/assets/glyph-coverage.mjs +74 -54
  90. package/assets/index.mjs +31 -9
  91. package/assets/rasterize.mjs +126 -99
  92. package/assets/svg-loader.mjs +8 -8
  93. package/build.mjs +23 -13
  94. package/cli.mjs +402 -172
  95. package/compile-bin.mjs +3 -3
  96. package/flash.mjs +537 -0
  97. package/hotreload/dev-device.mjs +90 -33
  98. package/hotreload/pack-app.mjs +55 -43
  99. package/hotreload/pack-split.mjs +62 -64
  100. package/hotreload/package-transfer.mjs +175 -0
  101. package/hotreload/protocol.mjs +50 -37
  102. package/hotreload/uploader.mjs +38 -27
  103. package/hotreload/watch-tree.mjs +19 -19
  104. package/pack-container.mjs +37 -21
  105. package/package.json +7 -8
  106. package/persist-transform.mjs +34 -28
  107. package/qjsc-wasm.mjs +25 -14
  108. package/sim/embedded-react.cjs +1 -1
  109. package/sim/embedded-react.js +1 -1
  110. package/sim/embedded-react.wasm +0 -0
  111. package/sim/index.html +94 -85
  112. package/sim-server.mjs +113 -94
  113. package/src/embedded-react/Animated.js +24 -24
  114. package/src/embedded-react/Dimensions.js +37 -0
  115. package/src/embedded-react/Easing.js +1 -1
  116. package/src/embedded-react/FlatList.js +3 -3
  117. package/src/embedded-react/LayoutAnimation.js +8 -5
  118. package/src/embedded-react/PanResponder.js +75 -71
  119. package/src/embedded-react/Pressable.js +2 -8
  120. package/src/embedded-react/SectionList.js +7 -9
  121. package/src/embedded-react/StyleSheet.js +4 -3
  122. package/src/embedded-react/TouchableOpacity.js +13 -8
  123. package/src/embedded-react/index.d.ts +88 -8
  124. package/src/embedded-react/index.js +2 -0
  125. package/src/embedded-react/layout-anim-config.js +10 -4
  126. package/src/embedded-react/list-child.js +5 -5
  127. package/src/embedded-react/named-colors.js +1 -1
  128. package/src/embedded-react/nativeModule.js +462 -0
  129. package/src/embedded-react/split-style.js +11 -11
  130. package/src/embedded-react/svg-ops.js +760 -593
  131. package/src/embedded-react/useHostValue.js +3 -3
  132. package/src/embedded-react/usePersistentState.js +6 -6
  133. package/src/embedded-react/warn-props.js +1 -1
  134. package/src/host-config.js +60 -53
  135. package/src/props.js +49 -41
  136. package/src/renderer.js +7 -4
  137. package/aot/compiler/assemble.mjs +0 -944
  138. package/aot/compiler/c/animations.mjs +0 -436
  139. package/aot/compiler/c/boot.mjs +0 -127
  140. package/aot/compiler/c/expressions.mjs +0 -270
  141. package/aot/compiler/c/functions.mjs +0 -268
  142. package/aot/compiler/c/keyboard.mjs +0 -96
  143. package/aot/compiler/c/nodes.mjs +0 -303
  144. package/aot/compiler/c/pan-responder.mjs +0 -136
  145. package/aot/compiler/c/statements.mjs +0 -319
  146. package/aot/compiler/c/text.mjs +0 -83
  147. package/aot/compiler/c/vectors.mjs +0 -381
  148. package/aot/compiler/c-syntax.mjs +0 -127
@@ -17,7 +17,7 @@
17
17
  - Imports
18
18
  ---------------------------------------------------------------------------------------------------------------------*/
19
19
  import { BootReadError, BootValue, buildValueOf, containsBoot, foldBoot, isBoot, isContainer, } from './boot.mjs';
20
- import { aotError } from './diagnostics.mjs';
20
+ import { ENGINE_STRING_MAX, aotError } from './diagnostics.mjs';
21
21
  /*----------------------------------------------------------------------------------------------------------------------
22
22
  - Constants
23
23
  ---------------------------------------------------------------------------------------------------------------------*/
@@ -31,9 +31,237 @@ const STATIC_MATH = {
31
31
  trunc: Math.trunc,
32
32
  abs: Math.abs,
33
33
  };
34
+ /** What a fold sees for a global the code shadows with a runtime value: bound, so not the global, and not constant. */
35
+ const RUNTIME_BINDING = Symbol('runtime binding');
36
+ /**
37
+ * A value inside an array or object built in render that is worked out at run time. The array folds, so its length
38
+ * and constant fields do; a read of this value doesn't, and lowers to the local it holds.
39
+ *
40
+ * @class RuntimeLeaf
41
+ */
42
+ export class RuntimeLeaf {
43
+ local;
44
+ /**
45
+ * @param local What a read of the value lowers to.
46
+ */
47
+ constructor(local) {
48
+ this.local = local;
49
+ }
50
+ }
51
+ /**
52
+ * A field of a tagged object that only some of its shapes have, where the shape isn't known: a test on the tag
53
+ * reveals it.
54
+ *
55
+ * @class HiddenField
56
+ */
57
+ export class HiddenField {
58
+ /** The field's slot. */
59
+ field;
60
+ message;
61
+ hint;
62
+ /**
63
+ * @param field The field's slot.
64
+ * @param message Why a read of it is refused.
65
+ * @param hint How to read it.
66
+ */
67
+ constructor(field, message, hint) {
68
+ this.field = field;
69
+ this.message = message;
70
+ this.hint = hint;
71
+ }
72
+ /**
73
+ * @returns The error a read of it raises.
74
+ */
75
+ error() {
76
+ return aotError(this.message, this.hint);
77
+ }
78
+ }
79
+ /** Globals a fold calls as JS does, unless the code shadows them. */
80
+ const FOLDED_GLOBALS = ['String'];
81
+ /** The most decimals toFixed prints in a package, as ERB_FIXED_DIGITS_MAX allows. */
82
+ export const MAX_FIXED_DIGITS = 20;
83
+ /** The hint for a helper's number that isn't a whole number. */
84
+ const WHOLE_NUMBER_HINT = 'the package bakes it into the instruction, so write a literal or a module constant.';
85
+ /**
86
+ * The string helpers a fold works out, as JS does, and the type each one is called on. A call past what the package
87
+ * could do at run time is refused, so a constant and a runtime value accept the same code.
88
+ */
89
+ const STATIC_METHODS = {
90
+ toFixed: {
91
+ receiver: 'number',
92
+ problem: (_value, digits = 0) => fixedDigitsProblem(digits),
93
+ apply: (value, digits) => value.toFixed(digits),
94
+ },
95
+ padStart: {
96
+ receiver: 'string',
97
+ problem: (_value, width = 0, fill = ' ') => padWidthProblem(width) ?? padFillProblem(fill),
98
+ apply: (value, width, fill) => value.padStart(width, fill),
99
+ },
100
+ slice: {
101
+ receiver: 'string',
102
+ problem: (_value, ...positions) => slicePositionProblem(positions),
103
+ apply: (value, start, end) => value.slice(start, end),
104
+ },
105
+ toUpperCase: {
106
+ receiver: 'string',
107
+ problem: () => null,
108
+ apply: (value) => value.toUpperCase(),
109
+ },
110
+ includes: {
111
+ receiver: 'string',
112
+ problem: (_value, needle, ...rest) => includesProblem(needle, rest.length > 0),
113
+ apply: (value, needle) => value.includes(needle),
114
+ },
115
+ split: {
116
+ receiver: 'string',
117
+ problem: (_value, separator, limit, ...rest) => splitProblem(separator, limit, rest.length > 0),
118
+ apply: (value, separator, limit) => value.split(separator, limit),
119
+ },
120
+ };
34
121
  /*----------------------------------------------------------------------------------------------------------------------
35
122
  - Implementation
36
123
  ---------------------------------------------------------------------------------------------------------------------*/
124
+ /**
125
+ * Why toFixed can't take these digits in a package, or null when it can.
126
+ *
127
+ * @param digits The digits.
128
+ *
129
+ * @returns The error's message and hint.
130
+ */
131
+ export function fixedDigitsProblem(digits) {
132
+ if (!Number.isInteger(digits)) {
133
+ return [
134
+ "AOT: toFixed's digits must be a compile-time whole number",
135
+ WHOLE_NUMBER_HINT,
136
+ ];
137
+ }
138
+ if (digits < 0 || digits > MAX_FIXED_DIGITS) {
139
+ return [
140
+ `AOT: toFixed(${digits}) is out of range`,
141
+ `a package prints 0 to ${MAX_FIXED_DIGITS} decimals.`,
142
+ ];
143
+ }
144
+ return null;
145
+ }
146
+ /**
147
+ * Why padStart can't take this length in a package, or null when it can.
148
+ *
149
+ * @param width The length.
150
+ *
151
+ * @returns The error's message and hint.
152
+ */
153
+ export function padWidthProblem(width) {
154
+ if (!Number.isInteger(width)) {
155
+ return [
156
+ "AOT: padStart's length must be a compile-time whole number",
157
+ WHOLE_NUMBER_HINT,
158
+ ];
159
+ }
160
+ if (width > ENGINE_STRING_MAX.ER_TEXT_MAX) {
161
+ return [
162
+ `AOT: padStart(${width}) is longer than any text`,
163
+ `a text holds at most ${ENGINE_STRING_MAX.ER_TEXT_MAX} bytes.`,
164
+ ];
165
+ }
166
+ return null;
167
+ }
168
+ /**
169
+ * Why padStart can't take this fill in a package, or null when it can: one printable ASCII character, or none.
170
+ *
171
+ * @param fill The fill.
172
+ *
173
+ * @returns The error's message and hint.
174
+ */
175
+ export function padFillProblem(fill) {
176
+ if (fill === '' || (typeof fill === 'string' && /^[ -~]$/.test(fill))) {
177
+ return null;
178
+ }
179
+ return [
180
+ "AOT: padStart's fill must be one printable ASCII character",
181
+ 'write a constant such as "0" or " "; a longer fill is not supported.',
182
+ ];
183
+ }
184
+ /**
185
+ * Why slice can't take these positions in a package, or null when it can: numbers, or left out.
186
+ *
187
+ * @param positions The start and end, as given.
188
+ *
189
+ * @returns The error's message and hint.
190
+ */
191
+ export function slicePositionProblem(positions) {
192
+ if (positions.every(position => typeof position === 'number')) {
193
+ return null;
194
+ }
195
+ return [
196
+ "AOT: slice's start and end must be numbers",
197
+ 'pass whole numbers, or leave the end out to cut to the end.',
198
+ ];
199
+ }
200
+ /**
201
+ * Why includes can't take this search in a package, or null when it can: a string, and no position.
202
+ *
203
+ * @param needle What it searches for.
204
+ * @param hasPosition Whether the call gives a position to search from.
205
+ *
206
+ * @returns The error's message and hint.
207
+ */
208
+ export function includesProblem(needle, hasPosition) {
209
+ if (hasPosition) {
210
+ return [
211
+ "AOT: includes' position is not supported",
212
+ 'slice the string into a local first: `const rest = name.slice(2);` then `rest.includes(x)`.',
213
+ ];
214
+ }
215
+ if (typeof needle !== 'string') {
216
+ return [
217
+ 'AOT: includes only searches for a string',
218
+ 'JS converts anything else to a string first; convert it yourself: name.includes(String(n)).',
219
+ ];
220
+ }
221
+ return null;
222
+ }
223
+ /**
224
+ * Why split can't take these arguments in a package, or null when it can: a string or no separator, and a whole
225
+ * number or no limit.
226
+ *
227
+ * @param separator What it splits at.
228
+ * @param limit The most pieces it keeps.
229
+ * @param hasMore Whether the call gives more arguments.
230
+ *
231
+ * @returns The error's message and hint.
232
+ */
233
+ export function splitProblem(separator, limit, hasMore) {
234
+ if (hasMore) {
235
+ return [
236
+ 'AOT: split takes a separator and a limit',
237
+ 'pass at most two arguments: text.split(",", 3).',
238
+ ];
239
+ }
240
+ if (separator !== undefined && typeof separator !== 'string') {
241
+ return [
242
+ 'AOT: split only splits at a string',
243
+ 'a regular expression is not supported; split at a string such as ",".',
244
+ ];
245
+ }
246
+ if (limit !== undefined && !Number.isInteger(limit)) {
247
+ return [
248
+ "AOT: split's limit must be a whole number",
249
+ 'pass a whole number, or leave it out to keep every piece.',
250
+ ];
251
+ }
252
+ return null;
253
+ }
254
+ /**
255
+ * Rethrows an error that refuses a fold because a string helper is past what a package can run, so a caller that
256
+ * treats a fold that fails as a runtime value doesn't swallow it.
257
+ *
258
+ * @param error What a fold threw.
259
+ */
260
+ export function rethrowFoldLimit(error) {
261
+ if (error?.isFoldLimit) {
262
+ throw error;
263
+ }
264
+ }
37
265
  /**
38
266
  * evalStatic at a boundary that REQUIRES a compile-time constant. On a fold failure it rethrows as a
39
267
  * LOCATED aotError with a clear message (+ optional hint), instead of letting evalStatic's bare
@@ -50,7 +278,8 @@ export function evalStaticOrThrow(node, scope, message, hint) {
50
278
  try {
51
279
  return evalStatic(node, scope);
52
280
  }
53
- catch {
281
+ catch (foldError) {
282
+ rethrowFoldLimit(foldError);
54
283
  const error = aotError(message, hint);
55
284
  if (node && node.loc) {
56
285
  error.aotLoc = node.loc.start;
@@ -61,7 +290,7 @@ export function evalStaticOrThrow(node, scope, message, hint) {
61
290
  /**
62
291
  * Folds an expression to the value JS would give it when every name it reads is a compile-time constant.
63
292
  * Styles and state initial values fold through here. A caller that can also handle a runtime value catches
64
- * the error thrown on anything dynamic (a state reference, say) and emits C instead.
293
+ * the error thrown on anything dynamic (a state reference, say) and lowers it instead.
65
294
  *
66
295
  * A value derived from the panel size (see BootValue) folds by `mode`: `fold` takes what it is on the build's
67
296
  * panel and records that as an assumption the panel at boot must meet; `boot` keeps it as a boot value, which
@@ -276,8 +505,12 @@ function evaluate(node, scope, fold) {
276
505
  : evaluate(node.alternate, scope, fold);
277
506
  }
278
507
  case 'Identifier':
279
- if (hasBinding(scope, node.name))
508
+ if (hasBinding(scope, node.name) &&
509
+ scope[node.name] !== RUNTIME_BINDING &&
510
+ !(scope[node.name] instanceof RuntimeLeaf) &&
511
+ !(scope[node.name] instanceof HiddenField)) {
280
512
  return scope[node.name];
513
+ }
281
514
  throw new Error(`AOT: cannot statically resolve identifier "${node.name}"`);
282
515
  case 'MemberExpression': {
283
516
  const object = evaluate(node.object, scope, fold);
@@ -298,6 +531,10 @@ function evaluate(node, scope, fold) {
298
531
  if (object == null) {
299
532
  throw new Error(`AOT: member access on null/undefined ("${key}")`);
300
533
  }
534
+ if (object[key] instanceof RuntimeLeaf ||
535
+ object[key] instanceof HiddenField) {
536
+ throw new Error(`AOT: "${key}" is worked out at run time`);
537
+ }
301
538
  return object[key];
302
539
  }
303
540
  case 'ObjectExpression': {
@@ -328,25 +565,75 @@ function evaluate(node, scope, fold) {
328
565
  return evaluate(node.arguments[0], scope, fold);
329
566
  }
330
567
  // Math.min / max / floor / ceil / round / trunc / abs fold as JS works them out; others are dynamic.
331
- const mathFn = callee.type === 'MemberExpression' &&
568
+ const mathFunction = callee.type === 'MemberExpression' &&
332
569
  !callee.computed &&
333
570
  callee.object.type === 'Identifier' &&
334
571
  callee.object.name === 'Math' &&
335
572
  !hasBinding(scope, 'Math')
336
573
  ? callee.property.name
337
574
  : null;
338
- if (mathFn && Object.hasOwn(STATIC_MATH, mathFn)) {
339
- const args = node.arguments.map(arg => {
340
- if (arg.type === 'SpreadElement') {
575
+ if (mathFunction && Object.hasOwn(STATIC_MATH, mathFunction)) {
576
+ const argumentValues = node.arguments.map(argument => {
577
+ if (argument.type === 'SpreadElement') {
341
578
  throw new Error('AOT: cannot statically evaluate a spread');
342
579
  }
343
- return evaluate(arg, scope, fold);
580
+ return evaluate(argument, scope, fold);
344
581
  });
345
- const result = STATIC_MATH[mathFn](...args.map(buildValueOf));
346
- return args.some(isBoot) ? bootOf(node, result, fold) : result;
582
+ const result = STATIC_MATH[mathFunction](...argumentValues.map(buildValueOf));
583
+ return argumentValues.some(isBoot)
584
+ ? bootOf(node, result, fold)
585
+ : result;
586
+ }
587
+ // String(x), and the string helpers on a constant.
588
+ const isString = callee.type === 'Identifier' &&
589
+ callee.name === 'String' &&
590
+ !hasBinding(scope, 'String');
591
+ const methodName = callee.type === 'MemberExpression' && !callee.computed
592
+ ? callee.property.name
593
+ : '';
594
+ const method = Object.hasOwn(STATIC_METHODS, methodName)
595
+ ? STATIC_METHODS[methodName]
596
+ : undefined;
597
+ if (isString || method) {
598
+ const values = [
599
+ ...(isString ? [] : [callee.object]),
600
+ ...node.arguments,
601
+ ].map(argument => {
602
+ if (argument.type === 'SpreadElement') {
603
+ throw new Error('AOT: cannot statically evaluate a spread');
604
+ }
605
+ return evaluate(argument, scope, fold);
606
+ });
607
+ const built = values.map(buildValueOf);
608
+ if (method && typeof built[0] !== method.receiver) {
609
+ throw new Error('AOT: a string helper on the wrong type');
610
+ }
611
+ const problem = method?.problem(...built);
612
+ if (problem) {
613
+ const error = aotError(...problem);
614
+ error.isFoldLimit = true;
615
+ if (node.loc) {
616
+ error.aotLoc = node.loc.start;
617
+ }
618
+ throw error;
619
+ }
620
+ const result = isString
621
+ ? built.length === 0
622
+ ? ''
623
+ : String(built[0])
624
+ : method.apply(...built);
625
+ return values.some(isBoot) ? bootOf(node, result, fold) : result;
347
626
  }
348
627
  throw new Error(`AOT: cannot statically evaluate call expression`);
349
628
  }
629
+ case 'TemplateLiteral': {
630
+ const values = node.expressions.map(expression => evaluate(expression, scope, fold));
631
+ const result = node.quasis
632
+ .map((quasi, index) => quasi.value.cooked +
633
+ (index < values.length ? String(buildValueOf(values[index])) : ''))
634
+ .join('');
635
+ return values.some(isBoot) ? bootOf(node, result, fold) : result;
636
+ }
350
637
  }
351
638
  throw new Error(`AOT: unsupported expression "${node.type}" in static context`);
352
639
  }
@@ -391,29 +678,30 @@ function binary(operator, left, right) {
391
678
  throw new Error('AOT: unsupported expression "BinaryExpression" in static context');
392
679
  }
393
680
  /**
394
- * `scope` minus every name `env` binds at RUNTIME — locals, state, refs, animated values, and the event /
395
- * gesture params of the handler being compiled. emitExpr resolves those before env.consts, so a fold
681
+ * `scope` minus every name `environment` binds at RUNTIME — locals, state, refs, animated values, and the event /
682
+ * gesture params of the handler being compiled. lowerExpr resolves those before environment.constants, so a fold
396
683
  * that consulted the raw scope would silently swap a module const in for the value the code actually
397
684
  * uses. Every env-driven constant fold goes through this; the JSX-side folds keep the same rule by
398
- * deleting a name from their scope copy when they bind it (see emitComponent / emitDynamicMap).
685
+ * deleting a name from their scope copy when they bind it (see emitComponent / emitDynamicMap). A runtime name that
686
+ * hides a global the fold calls, such as `String`, is kept as bound but not constant, so the global isn't called.
399
687
  *
400
- * @param env The expression environment.
688
+ * @param environment The expression environment.
401
689
  * @param scope The constants a fold would otherwise see.
402
690
  *
403
691
  * @returns `scope` itself when nothing shadows it, else a copy without the shadowed names.
404
692
  */
405
- export function foldScope(env, scope) {
693
+ export function foldScope(environment, scope) {
406
694
  const runtimeNames = new Set([
407
- ...(env.locals?.keys() ?? []),
408
- ...(env.state?.keys() ?? []),
409
- ...(env.refs?.keys() ?? []),
410
- ...(env.animations?.keys() ?? []),
695
+ ...(environment.locals?.keys() ?? []),
696
+ ...(environment.state?.keys() ?? []),
697
+ ...(environment.refs?.keys() ?? []),
698
+ ...(environment.animations?.keys() ?? []),
411
699
  ]);
412
- if (env.event) {
413
- runtimeNames.add(env.event);
700
+ if (environment.event) {
701
+ runtimeNames.add(environment.event);
414
702
  }
415
- if (env.gesture) {
416
- runtimeNames.add(env.gesture);
703
+ if (environment.gesture) {
704
+ runtimeNames.add(environment.gesture);
417
705
  }
418
706
  // Copy the scope only when a runtime name actually shadows one of its constants.
419
707
  let hasShadowedName = false;
@@ -423,9 +711,14 @@ export function foldScope(env, scope) {
423
711
  break;
424
712
  }
425
713
  }
426
- if (!hasShadowedName)
427
- return scope;
428
- return Object.fromEntries(Object.entries(scope).filter(([name]) => !runtimeNames.has(name)));
714
+ const kept = hasShadowedName
715
+ ? Object.fromEntries(Object.entries(scope).filter(([name]) => !runtimeNames.has(name)))
716
+ : scope;
717
+ // A runtime value named like a global the fold calls hides that global.
718
+ const shadowedGlobals = FOLDED_GLOBALS.filter(name => runtimeNames.has(name));
719
+ if (shadowedGlobals.length === 0)
720
+ return kept;
721
+ return Object.assign(Object.create(kept), Object.fromEntries(shadowedGlobals.map(name => [name, RUNTIME_BINDING])));
429
722
  }
430
723
  /**
431
724
  * Whether `scope` binds `name`, through the scopes it reads through to but not Object.prototype.
@@ -442,6 +735,23 @@ export function hasBinding(scope, name) {
442
735
  }
443
736
  return false;
444
737
  }
738
+ /**
739
+ * `scope` with names hidden, as a block that declares them hides what they name outside it, so a fold can't read a
740
+ * constant they shadow.
741
+ *
742
+ * @param scope Compile-time constants in reach.
743
+ * @param names The names.
744
+ *
745
+ * @returns The scope.
746
+ */
747
+ export function withoutNames(scope, names) {
748
+ if (!names.length)
749
+ return scope;
750
+ const hidden = Object.create(scope);
751
+ for (const name of names)
752
+ setBinding(hidden, name, RUNTIME_BINDING);
753
+ return hidden;
754
+ }
445
755
  /**
446
756
  * Binds `name` in `scope` as its own property, so `__proto__` is a name rather than Object.prototype's setter.
447
757
  *
@@ -469,16 +779,16 @@ export const withUndefined = (scope) => Object.assign(Object.create(scope), { un
469
779
  * evalStatic with a fallback default when the node is absent or not foldable.
470
780
  *
471
781
  * @param node The expression, if there is one.
472
- * @param env The expression environment; its constants are what the fold sees.
782
+ * @param environment The expression environment; its constants are what the fold sees.
473
783
  * @param defaultValue The value to use when the node is absent or does not fold.
474
784
  *
475
785
  * @returns The folded value, or `defaultValue`.
476
786
  */
477
- export function evalStaticOr(node, env, defaultValue) {
787
+ export function evalStaticOr(node, environment, defaultValue) {
478
788
  if (!node)
479
789
  return defaultValue;
480
790
  try {
481
- return evalStatic(node, foldScope(env, env.consts ?? {}));
791
+ return evalStatic(node, foldScope(environment, environment.constants ?? {}));
482
792
  }
483
793
  catch {
484
794
  return defaultValue;