embedded-react 0.20.0 → 0.22.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 (140) hide show
  1. package/NOTICE +5 -0
  2. package/README.md +3 -2
  3. package/aot/app-file-loader.mjs +116 -0
  4. package/aot/compile.mjs +17 -47
  5. package/aot/compiler/alerts.mjs +604 -0
  6. package/aot/compiler/animations.mjs +299 -57
  7. package/aot/compiler/boot.mjs +27 -2
  8. package/aot/compiler/check.mjs +62 -10
  9. package/aot/compiler/collect.mjs +517 -137
  10. package/aot/compiler/contexts.mjs +391 -0
  11. package/aot/compiler/control-flow.mjs +276 -117
  12. package/aot/compiler/custom-hooks.mjs +686 -0
  13. package/aot/compiler/elements/button.mjs +182 -0
  14. package/aot/compiler/elements/image-background.mjs +76 -0
  15. package/aot/compiler/elements/image.mjs +2 -1
  16. package/aot/compiler/elements/props.mjs +47 -8
  17. package/aot/compiler/elements/rewrite.mjs +118 -0
  18. package/aot/compiler/elements/section-list.mjs +370 -0
  19. package/aot/compiler/expressions.mjs +1378 -97
  20. package/aot/compiler/handlers.mjs +1807 -354
  21. package/aot/compiler/helper-values.mjs +312 -0
  22. package/aot/compiler/ir/nodes.mjs +40 -1
  23. package/aot/compiler/ir/reads.mjs +206 -5
  24. package/aot/compiler/ir/text.mjs +5 -1
  25. package/aot/compiler/ir/types.mjs +12 -0
  26. package/aot/compiler/kept-states.mjs +177 -0
  27. package/aot/compiler/keyboard-dismiss.mjs +99 -0
  28. package/aot/compiler/keyboard.mjs +1 -1
  29. package/aot/compiler/list-items.mjs +226 -0
  30. package/aot/compiler/list-order.mjs +586 -0
  31. package/aot/compiler/list-split.mjs +259 -0
  32. package/aot/compiler/loops.mjs +582 -0
  33. package/aot/compiler/lower.mjs +203 -53
  34. package/aot/compiler/native-files.mjs +439 -0
  35. package/aot/compiler/native-header.mjs +335 -0
  36. package/aot/compiler/native-modules.mjs +1173 -0
  37. package/aot/compiler/nodes.mjs +45 -31
  38. package/aot/compiler/options.mjs +3 -2
  39. package/aot/compiler/out.mjs +18 -1
  40. package/aot/compiler/package/animations.mjs +133 -23
  41. package/aot/compiler/package/assemble.mjs +2 -4
  42. package/aot/compiler/package/bytecode.mjs +37 -8
  43. package/aot/compiler/package/checks.mjs +4 -0
  44. package/aot/compiler/package/codegen.mjs +906 -33
  45. package/aot/compiler/package/enums.mjs +10 -0
  46. package/aot/compiler/package/format.mjs +16 -3
  47. package/aot/compiler/package/lower.mjs +707 -176
  48. package/aot/compiler/package/string-caps.mjs +515 -0
  49. package/aot/compiler/package/tables.mjs +24 -0
  50. package/aot/compiler/package/writer.mjs +22 -0
  51. package/aot/compiler/parse.mjs +3 -1
  52. package/aot/compiler/record-shapes.mjs +246 -0
  53. package/aot/compiler/records.mjs +654 -0
  54. package/aot/compiler/reducers.mjs +272 -0
  55. package/aot/compiler/scroll-to.mjs +112 -0
  56. package/aot/compiler/static-eval.mjs +315 -7
  57. package/aot/compiler/style-text.mjs +20 -5
  58. package/aot/compiler/svg.mjs +13 -7
  59. package/aot/compiler/switch-cases.mjs +198 -0
  60. package/aot/compiler/tagged-records.mjs +450 -0
  61. package/aot/compiler/typing.mjs +307 -0
  62. package/aot/compiler/updaters.mjs +321 -0
  63. package/aot/compiler/window-dimensions.mjs +94 -0
  64. package/aot/compiler/worked-values.mjs +319 -0
  65. package/aot/demo-file.mjs +30 -0
  66. package/aot/style-map.mjs +94 -3
  67. package/assets/bake-font.mjs +187 -244
  68. package/assets/bake-image.mjs +47 -71
  69. package/assets/bake-svg.mjs +465 -533
  70. package/assets/build-builtin-font.mjs +14 -27
  71. package/assets/emit-c.mjs +124 -204
  72. package/assets/emit-container.mjs +129 -197
  73. package/assets/emit-pack.mjs +92 -136
  74. package/assets/font-config.mjs +19 -27
  75. package/assets/font-sizes.mjs +432 -507
  76. package/assets/glyph-coverage.mjs +170 -214
  77. package/assets/index.mjs +52 -97
  78. package/assets/rasterize.mjs +173 -194
  79. package/assets/svg-loader.mjs +42 -48
  80. package/build.mjs +85 -94
  81. package/cli.mjs +490 -586
  82. package/compile-bin.mjs +24 -36
  83. package/flash.mjs +297 -401
  84. package/hotreload/dev-device.mjs +229 -270
  85. package/hotreload/pack-app.mjs +118 -159
  86. package/hotreload/pack-split.mjs +146 -213
  87. package/hotreload/package-transfer.mjs +128 -131
  88. package/hotreload/protocol.mjs +118 -118
  89. package/hotreload/uploader.mjs +132 -159
  90. package/hotreload/watch-tree.mjs +56 -65
  91. package/pack-container.mjs +94 -116
  92. package/package.json +19 -16
  93. package/persist-transform.mjs +89 -105
  94. package/qjsc-wasm.mjs +47 -72
  95. package/sim/embedded-react.wasm +0 -0
  96. package/sim/index.html +1 -1
  97. package/sim-server.mjs +260 -354
  98. package/src/embedded-react/Alert.mjs +112 -0
  99. package/src/embedded-react/Animated.mjs +412 -0
  100. package/src/embedded-react/{AppRegistry.js → AppRegistry.mjs} +26 -25
  101. package/src/embedded-react/{Button.js → Button.mjs} +29 -53
  102. package/src/embedded-react/Dimensions.mjs +32 -0
  103. package/src/embedded-react/Easing.mjs +34 -0
  104. package/src/embedded-react/{FlatList.js → FlatList.mjs} +25 -42
  105. package/src/embedded-react/{ImageBackground.js → ImageBackground.mjs} +11 -22
  106. package/src/embedded-react/Keyboard.mjs +82 -0
  107. package/src/embedded-react/{LayoutAnimation.js → LayoutAnimation.mjs} +17 -28
  108. package/src/embedded-react/PanResponder.mjs +256 -0
  109. package/src/embedded-react/{Platform.js → Platform.mjs} +8 -9
  110. package/src/embedded-react/{Pressable.js → Pressable.mjs} +12 -25
  111. package/src/embedded-react/SectionList.mjs +103 -0
  112. package/src/embedded-react/{StyleSheet.js → StyleSheet.mjs} +16 -20
  113. package/src/embedded-react/{TouchableOpacity.js → TouchableOpacity.mjs} +47 -83
  114. package/src/embedded-react/background-image.mjs +321 -0
  115. package/src/embedded-react/{components.js → components.mjs} +1 -3
  116. package/src/embedded-react/{imperative.js → imperative.mjs} +27 -38
  117. package/src/embedded-react/index.d.ts +238 -5
  118. package/src/embedded-react/index.js +26 -17
  119. package/src/embedded-react/layout-anim-config.mjs +85 -0
  120. package/src/embedded-react/{list-child.js → list-child.mjs} +11 -20
  121. package/src/embedded-react/{named-colors.js → named-colors.mjs} +2 -6
  122. package/src/embedded-react/nativeModule.mjs +371 -0
  123. package/src/embedded-react/split-style.mjs +57 -0
  124. package/src/embedded-react/svg-ops.mjs +1075 -0
  125. package/src/embedded-react/{useHostValue.js → useHostValue.mjs} +2 -4
  126. package/src/embedded-react/{usePersistentState.js → usePersistentState.mjs} +34 -38
  127. package/src/embedded-react/{warn-props.js → warn-props.mjs} +32 -39
  128. package/src/host-config.mjs +512 -0
  129. package/src/{native-ui.js → native-ui.mjs} +1 -8
  130. package/src/props.mjs +341 -0
  131. package/src/{renderer.js → renderer.mjs} +30 -44
  132. package/src/embedded-react/Animated.js +0 -440
  133. package/src/embedded-react/Easing.js +0 -39
  134. package/src/embedded-react/PanResponder.js +0 -330
  135. package/src/embedded-react/SectionList.js +0 -148
  136. package/src/embedded-react/layout-anim-config.js +0 -101
  137. package/src/embedded-react/split-style.js +0 -58
  138. package/src/embedded-react/svg-ops.js +0 -1295
  139. package/src/host-config.js +0 -548
  140. package/src/props.js +0 -306
@@ -0,0 +1,391 @@
1
+ /*
2
+ * Copyright 2026 Cory Lamming
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ /*
17
+ * Contexts, worked out at compile time: `const Theme = createContext(…)`, `<Theme.Provider value={…}>` and
18
+ * `useContext(Theme)`. Each component instance is inlined where it is rendered, so the nearest provider above it is
19
+ * known, and a value read from the context binds as a prop given that provider's value would. With no provider above,
20
+ * it reads the context's default.
21
+ */
22
+ /*----------------------------------------------------------------------------------------------------------------------
23
+ - Imports
24
+ ---------------------------------------------------------------------------------------------------------------------*/
25
+ import { traverse } from '@babel/core';
26
+ import { aotError } from './diagnostics.mjs';
27
+ import { evalStatic } from './static-eval.mjs';
28
+ import { attrExpr } from './style-text.mjs';
29
+ import { bindProp, propDescriptorOf } from './control-flow.mjs';
30
+ /*----------------------------------------------------------------------------------------------------------------------
31
+ - Implementation
32
+ ---------------------------------------------------------------------------------------------------------------------*/
33
+ /**
34
+ * An error located at a node.
35
+ *
36
+ * @param message The message.
37
+ * @param hint How to fix it.
38
+ * @param node Where it is.
39
+ *
40
+ * @returns The error.
41
+ */
42
+ function refusal(message, hint, node) {
43
+ const error = aotError(message, hint);
44
+ if (node?.loc) {
45
+ error.aotLoc = node.loc.start;
46
+ }
47
+ return error;
48
+ }
49
+ /**
50
+ * The name a call is made to, `React.` stripped: `useContext(…)` and `React.useContext(…)` are both `useContext`.
51
+ *
52
+ * @param node Any node.
53
+ *
54
+ * @returns The name, or null when it isn't a call to a name.
55
+ */
56
+ function calleeName(node) {
57
+ if (node?.type !== 'CallExpression')
58
+ return null;
59
+ const callee = node.callee;
60
+ if (callee.type === 'Identifier')
61
+ return callee.name;
62
+ if (callee.type === 'MemberExpression' &&
63
+ !callee.computed &&
64
+ callee.object.type === 'Identifier' &&
65
+ callee.object.name === 'React' &&
66
+ callee.property.type === 'Identifier') {
67
+ return callee.property.name;
68
+ }
69
+ return null;
70
+ }
71
+ /**
72
+ * Whether a node is a `useContext(…)` call, whose value bindContextUses binds.
73
+ *
74
+ * @param node Any node.
75
+ *
76
+ * @returns Whether it is.
77
+ */
78
+ export const isContextRead = (node) => calleeName(node) === 'useContext';
79
+ /**
80
+ * The module's contexts: each module-level `const C = createContext(default)`.
81
+ *
82
+ * @param program The module.
83
+ *
84
+ * @returns The contexts, by name.
85
+ */
86
+ export function contextsOf(program) {
87
+ const contexts = new Map();
88
+ for (const statement of program.body) {
89
+ const declaration = statement.type === 'ExportNamedDeclaration'
90
+ ? statement.declaration
91
+ : statement;
92
+ if (declaration?.type !== 'VariableDeclaration')
93
+ continue;
94
+ for (const declarator of declaration.declarations) {
95
+ if (calleeName(declarator.init) !== 'createContext')
96
+ continue;
97
+ if (declarator.id.type !== 'Identifier') {
98
+ throw refusal('AOT: a context must be bound to a name', 'const Theme = createContext(…);', declarator);
99
+ }
100
+ const argument = declarator.init.arguments[0];
101
+ contexts.set(declarator.id.name, {
102
+ name: declarator.id.name,
103
+ value: argument ?? null,
104
+ });
105
+ }
106
+ }
107
+ return contexts;
108
+ }
109
+ /**
110
+ * Refuses a context's name where a binding of that name hides it, in `useContext(C)` or `<C.Provider>`, since contexts
111
+ * are found by name.
112
+ *
113
+ * @param ast The parsed module.
114
+ */
115
+ export function refuseHiddenContexts(ast) {
116
+ const names = new Set(contextsOf(ast.program).keys());
117
+ if (!names.size)
118
+ return;
119
+ const check = (name, scope, node) => {
120
+ if (!names.has(name))
121
+ return;
122
+ if (scope.getBinding(name)?.scope.block.type === 'Program')
123
+ return;
124
+ throw refusal(`AOT: "${name}" here is not the context: a binding of that name hides it`, `rename the binding, so ${name} names the context made with createContext.`, node);
125
+ };
126
+ traverse.cache.clear();
127
+ traverse(ast, {
128
+ JSXOpeningElement(path) {
129
+ const name = path.node.name;
130
+ if (name.type === 'JSXMemberExpression' &&
131
+ name.object.type === 'JSXIdentifier') {
132
+ check(name.object.name, path.scope, path.node);
133
+ }
134
+ },
135
+ CallExpression(path) {
136
+ const argument = path.node.arguments[0];
137
+ if (isContextRead(path.node) && argument?.type === 'Identifier') {
138
+ check(argument.name, path.scope, argument);
139
+ }
140
+ },
141
+ });
142
+ traverse.cache.clear();
143
+ }
144
+ /**
145
+ * The context a `<C.Provider>` provides. `<C.Consumer>` is refused.
146
+ *
147
+ * @param element Any element.
148
+ * @param environment The expression environment.
149
+ *
150
+ * @returns The context's name, or null when the element isn't a provider.
151
+ */
152
+ export function providedContextOf(element, environment) {
153
+ const name = element.openingElement.name;
154
+ if (name.type !== 'JSXMemberExpression' ||
155
+ name.object.type !== 'JSXIdentifier' ||
156
+ !environment.contexts?.records.has(name.object.name)) {
157
+ return null;
158
+ }
159
+ if (name.property.name === 'Provider')
160
+ return name.object.name;
161
+ throw refusal(`AOT: <${name.object.name}.${name.property.name}> is not supported`, `read the context with useContext(${name.object.name}) at the top of the component instead.`, element);
162
+ }
163
+ /**
164
+ * The environment inside a `<C.Provider value={…}>`: what is lowered there reads C from it.
165
+ *
166
+ * @param element The provider.
167
+ * @param context The context it provides.
168
+ * @param scope Compile-time constants in reach.
169
+ * @param environment The expression environment around it.
170
+ * @param state The component's state table.
171
+ *
172
+ * @returns The environment.
173
+ */
174
+ export function providedEnvironment(element, context, scope, environment, state) {
175
+ let value = null;
176
+ for (const attribute of element.openingElement.attributes) {
177
+ if (attribute.type === 'JSXAttribute' &&
178
+ attribute.name.type === 'JSXIdentifier' &&
179
+ attribute.name.name === 'key') {
180
+ continue;
181
+ }
182
+ if (attribute.type !== 'JSXAttribute' ||
183
+ attribute.name.type !== 'JSXIdentifier' ||
184
+ attribute.name.name !== 'value') {
185
+ throw refusal(`AOT: <${context}.Provider> takes only a value`, `<${context}.Provider value={…}>.`, attribute);
186
+ }
187
+ value = attrExpr(attribute);
188
+ }
189
+ const providers = new Map(environment.providers);
190
+ providers.set(context, { value, scope, environment, state });
191
+ return { ...environment, providers };
192
+ }
193
+ /**
194
+ * The provider of a context in reach, or the context's default when there is none.
195
+ *
196
+ * @param context The context.
197
+ * @param environment The expression environment.
198
+ *
199
+ * @returns The provider.
200
+ */
201
+ function providerOf(context, environment) {
202
+ const provider = environment.providers?.get(context.name);
203
+ if (provider)
204
+ return provider;
205
+ const defaults = environment.contexts.defaults;
206
+ return {
207
+ value: context.value,
208
+ scope: defaults.constants,
209
+ environment: defaults,
210
+ state: { byName: new Map(), bySetter: new Map() },
211
+ };
212
+ }
213
+ /**
214
+ * The descriptor of a value a provider gives, worked out where the provider is written.
215
+ *
216
+ * @param name The name it is bound to.
217
+ * @param node The value, or null for none.
218
+ * @param provider The provider.
219
+ *
220
+ * @returns The descriptor.
221
+ */
222
+ function describe(name, node, provider) {
223
+ if (!node)
224
+ return { static: true, value: undefined };
225
+ if (node.type === 'Identifier' && provider.state.bySetter.has(node.name)) {
226
+ throw refusal(`AOT: a context's value can't hold the setter "${node.name}"`, `give it a function that calls the setter instead: {toggle: () => ${node.name}(…)}.`, node);
227
+ }
228
+ return propDescriptorOf(name, node, provider.scope, provider.environment);
229
+ }
230
+ /**
231
+ * The value an object a provider gives has under a key.
232
+ *
233
+ * @param object The object.
234
+ * @param key The key.
235
+ * @param context The context's name, for the error.
236
+ *
237
+ * @returns The value, or null when it has none.
238
+ */
239
+ function valueOfKey(object, key, context) {
240
+ for (const property of object.properties) {
241
+ if (property.type === 'SpreadElement') {
242
+ throw refusal(`AOT: the value of context "${context}" is an object with a spread`, 'list each value: value={{theme, toggle}}.', property);
243
+ }
244
+ const name = property.computed
245
+ ? property.key.type === 'StringLiteral'
246
+ ? property.key.value
247
+ : null
248
+ : property.key.type === 'Identifier'
249
+ ? property.key.name
250
+ : property.key.type === 'StringLiteral'
251
+ ? property.key.value
252
+ : null;
253
+ if (name !== key)
254
+ continue;
255
+ if (property.type === 'ObjectMethod') {
256
+ return {
257
+ type: 'FunctionExpression',
258
+ params: property.params,
259
+ body: property.body,
260
+ generator: property.generator,
261
+ async: property.async,
262
+ loc: property.loc,
263
+ };
264
+ }
265
+ return property.value;
266
+ }
267
+ return null;
268
+ }
269
+ /**
270
+ * Binds the values a component body reads with `const x = useContext(C)` or `const {a, b} = useContext(C)`, each as
271
+ * a prop of that value from the nearest provider would be. A `useContext` call anywhere else is refused.
272
+ *
273
+ * @param body The component's body.
274
+ * @param environment The expression environment the component is rendered in, whose providers are in reach.
275
+ * @param tables The component's tables, which the values are bound into.
276
+ *
277
+ * @returns The names bound.
278
+ */
279
+ export function bindContextUses(body, environment, tables) {
280
+ const names = new Set();
281
+ const allowed = new WeakSet();
282
+ for (const statement of body.body) {
283
+ if (statement.type !== 'VariableDeclaration')
284
+ continue;
285
+ for (const declarator of statement.declarations) {
286
+ if (calleeName(declarator.init) !== 'useContext')
287
+ continue;
288
+ const call = declarator.init;
289
+ allowed.add(call);
290
+ // The argument is a context the module makes.
291
+ const argument = call.arguments[0];
292
+ const context = argument?.type === 'Identifier'
293
+ ? environment.contexts?.records.get(argument.name)
294
+ : undefined;
295
+ if (!context || call.arguments.length !== 1) {
296
+ throw refusal('AOT: useContext takes a context made in this file', 'make it at module level and pass it: const Theme = createContext(…); … useContext(Theme).', call);
297
+ }
298
+ const provider = providerOf(context, environment);
299
+ const bind = (name, descriptor) => {
300
+ bindProp(name, descriptor, tables, provider.environment, provider.state);
301
+ names.add(name);
302
+ };
303
+ // `const value = useContext(C)`: the whole value.
304
+ const pattern = declarator.id;
305
+ if (pattern.type === 'Identifier') {
306
+ bind(pattern.name, describe(pattern.name, provider.value, provider));
307
+ continue;
308
+ }
309
+ if (pattern.type !== 'ObjectPattern') {
310
+ throw refusal(`AOT: the value of context "${context.name}" can only be destructured as an object`, `const {a, b} = useContext(${context.name}), or const value = useContext(${context.name}).`, pattern);
311
+ }
312
+ // `const {a, b: c = 1} = useContext(C)`: each key, from an object written out in the provider, or from a
313
+ // constant one.
314
+ const value = provider.value;
315
+ const whole = value && value.type !== 'ObjectExpression'
316
+ ? describe('value', value, provider)
317
+ : null;
318
+ for (const property of pattern.properties) {
319
+ if (property.type === 'RestElement' ||
320
+ (property.computed && property.key.type !== 'StringLiteral')) {
321
+ throw refusal(`AOT: the value of context "${context.name}" can only be destructured by key`, `name each key: const {a, b} = useContext(${context.name}).`, property);
322
+ }
323
+ const key = property.key.type === 'Identifier'
324
+ ? property.key.name
325
+ : property.key.value;
326
+ const target = property.value;
327
+ const name = target.type === 'Identifier'
328
+ ? target.name
329
+ : target.type === 'AssignmentPattern' &&
330
+ target.left.type === 'Identifier'
331
+ ? target.left.name
332
+ : null;
333
+ if (!name) {
334
+ throw refusal(`AOT: the value of context "${context.name}" can only be destructured one level deep`, 'bind each key to a name, then destructure that name on its own.', target);
335
+ }
336
+ let descriptor;
337
+ if (whole) {
338
+ if (whole.static !== true ||
339
+ whole.value === null ||
340
+ typeof whole.value !== 'object') {
341
+ throw refusal(`AOT: the value of context "${context.name}" must be an object to destructure`, 'give the provider an object written out in place: value={{theme, toggle}}.', value);
342
+ }
343
+ descriptor = {
344
+ static: true,
345
+ value: whole.value[key],
346
+ };
347
+ }
348
+ else {
349
+ descriptor = describe(name, value
350
+ ? valueOfKey(value, key, context.name)
351
+ : null, provider);
352
+ }
353
+ // A default stands in for a key the value has no value for.
354
+ if (descriptor.static === true &&
355
+ descriptor.value === undefined &&
356
+ target.type === 'AssignmentPattern') {
357
+ descriptor = {
358
+ static: true,
359
+ value: evalStatic(target.right, tables.scope, 'boot'),
360
+ };
361
+ }
362
+ bind(name, descriptor);
363
+ }
364
+ }
365
+ }
366
+ // Anywhere else, useContext would read a provider this pass doesn't know.
367
+ const walk = (node) => {
368
+ if (!node || typeof node !== 'object')
369
+ return;
370
+ if (Array.isArray(node)) {
371
+ node.forEach(walk);
372
+ return;
373
+ }
374
+ const astNode = node;
375
+ if (typeof astNode.type !== 'string')
376
+ return;
377
+ if (calleeName(astNode) === 'useContext' && !allowed.has(astNode)) {
378
+ throw refusal('AOT: useContext must be called at the top of a component', 'bind its value on a line of its own at the top of the component: const theme = useContext(Theme).', astNode);
379
+ }
380
+ for (const key of Object.keys(astNode)) {
381
+ if (key === 'loc' ||
382
+ key === 'leadingComments' ||
383
+ key === 'trailingComments') {
384
+ continue;
385
+ }
386
+ walk(astNode[key]);
387
+ }
388
+ };
389
+ walk(body);
390
+ return names;
391
+ }