embedded-react 0.15.0 → 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/aot/compile.mjs +173 -8272
- package/aot/compiler/animations.mjs +1001 -0
- package/aot/compiler/assemble.mjs +922 -0
- package/aot/compiler/boot.mjs +441 -0
- package/aot/compiler/branch-merge.mjs +319 -0
- package/aot/compiler/c/animations.mjs +362 -0
- package/aot/compiler/c/boot.mjs +127 -0
- package/aot/compiler/c/expressions.mjs +270 -0
- package/aot/compiler/c/functions.mjs +268 -0
- package/aot/compiler/c/keyboard.mjs +96 -0
- package/aot/compiler/c/nodes.mjs +294 -0
- package/aot/compiler/c/pan-responder.mjs +136 -0
- package/aot/compiler/c/statements.mjs +271 -0
- package/aot/compiler/c/text.mjs +83 -0
- package/aot/compiler/c/vectors.mjs +381 -0
- package/aot/compiler/c-syntax.mjs +127 -0
- package/aot/compiler/check.mjs +165 -0
- package/aot/compiler/collect.mjs +785 -0
- package/aot/compiler/control-flow.mjs +696 -0
- package/aot/compiler/diagnostics.mjs +137 -0
- package/aot/compiler/elements/activity-indicator.mjs +160 -0
- package/aot/compiler/elements/dial.mjs +346 -0
- package/aot/compiler/elements/flat-list.mjs +139 -0
- package/aot/compiler/elements/image.mjs +151 -0
- package/aot/compiler/elements/modal.mjs +158 -0
- package/aot/compiler/elements/props.mjs +474 -0
- package/aot/compiler/elements/switch.mjs +183 -0
- package/aot/compiler/elements/text-input.mjs +171 -0
- package/aot/compiler/expressions.mjs +1579 -0
- package/aot/compiler/handlers.mjs +1072 -0
- package/aot/compiler/ir/animations.mjs +16 -0
- package/aot/compiler/ir/app.mjs +16 -0
- package/aot/compiler/ir/boot.mjs +16 -0
- package/aot/compiler/ir/expressions.mjs +16 -0
- package/aot/compiler/ir/functions.mjs +16 -0
- package/aot/compiler/ir/keyboard.mjs +16 -0
- package/aot/compiler/ir/nodes.mjs +107 -0
- package/aot/compiler/ir/pan-responder.mjs +16 -0
- package/aot/compiler/ir/reads.mjs +351 -0
- package/aot/compiler/ir/source.mjs +16 -0
- package/aot/compiler/ir/statements.mjs +16 -0
- package/aot/compiler/ir/text.mjs +16 -0
- package/aot/compiler/ir/types.mjs +185 -0
- package/aot/compiler/ir/vectors.mjs +16 -0
- package/aot/compiler/keyboard.mjs +152 -0
- package/aot/compiler/lower.mjs +311 -0
- package/aot/compiler/nodes.mjs +688 -0
- package/aot/compiler/options.mjs +55 -0
- package/aot/compiler/out.mjs +94 -0
- package/aot/compiler/pan-responder.mjs +354 -0
- package/aot/compiler/parse.mjs +300 -0
- package/aot/compiler/static-eval.mjs +454 -0
- package/aot/compiler/style-text.mjs +807 -0
- package/aot/compiler/svg.mjs +1167 -0
- package/aot/compiler/types.mjs +16 -0
- package/aot/screenshot-smoke.mjs +95 -55
- package/aot/style-map.mjs +366 -304
- package/cli.mjs +5 -8
- package/package.json +14 -6
- package/sim/embedded-react.wasm +0 -0
- package/src/embedded-react/FlatList.js +4 -4
- package/src/props.js +1 -1
|
@@ -0,0 +1,185 @@
|
|
|
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
|
+
- Constants
|
|
18
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
19
|
+
export const BOOL = { kind: 'bool' };
|
|
20
|
+
export const INT = { kind: 'int' };
|
|
21
|
+
export const FLOAT = { kind: 'float' };
|
|
22
|
+
export const STRING = { kind: 'string' };
|
|
23
|
+
export const TIME = { kind: 'int64', source: 'time' };
|
|
24
|
+
export const INT64_NUMBER = { kind: 'int64', source: 'number' };
|
|
25
|
+
export const INT64_MATH = { kind: 'int64', source: 'math' };
|
|
26
|
+
export const STRUCT = { kind: 'struct' };
|
|
27
|
+
export const NODE = { kind: 'node' };
|
|
28
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
29
|
+
- Implementation
|
|
30
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
31
|
+
/**
|
|
32
|
+
* The slot a value of a type is stored in: a boolean in an int, every 64-bit number in an int64. A list row's
|
|
33
|
+
* item has none: it is read through its fields.
|
|
34
|
+
*
|
|
35
|
+
* @param type The type.
|
|
36
|
+
*
|
|
37
|
+
* @returns The slot's kind, or undefined for a list row's item.
|
|
38
|
+
*/
|
|
39
|
+
export function slotOf(type) {
|
|
40
|
+
switch (type.kind) {
|
|
41
|
+
case 'bool':
|
|
42
|
+
return 'int';
|
|
43
|
+
case 'int64':
|
|
44
|
+
return 'i64';
|
|
45
|
+
case 'struct':
|
|
46
|
+
return undefined;
|
|
47
|
+
case 'node':
|
|
48
|
+
return 'ERNode*'; // a handle, which no number or text slot holds
|
|
49
|
+
default:
|
|
50
|
+
return type.kind;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The type of a 64-bit number from a source.
|
|
55
|
+
*
|
|
56
|
+
* @param source Where it comes from.
|
|
57
|
+
*
|
|
58
|
+
* @returns The type.
|
|
59
|
+
*/
|
|
60
|
+
export const int64Of = (source) => source === 'time' ? TIME : source === 'number' ? INT64_NUMBER : INT64_MATH;
|
|
61
|
+
/**
|
|
62
|
+
* The type of a value whose slot is known: a boolean when it holds one, a time when it is a 64-bit slot.
|
|
63
|
+
*
|
|
64
|
+
* @param slot The slot's kind.
|
|
65
|
+
* @param isBool The value is a JS boolean.
|
|
66
|
+
*
|
|
67
|
+
* @returns The type.
|
|
68
|
+
*/
|
|
69
|
+
export function typeOfSlot(slot, isBool = false) {
|
|
70
|
+
if (isBool) {
|
|
71
|
+
if (slot !== 'int') {
|
|
72
|
+
throw new Error(`AOT internal: a boolean in a "${slot}" slot`);
|
|
73
|
+
}
|
|
74
|
+
return BOOL;
|
|
75
|
+
}
|
|
76
|
+
switch (slot) {
|
|
77
|
+
case 'int':
|
|
78
|
+
return INT;
|
|
79
|
+
case 'float':
|
|
80
|
+
return FLOAT;
|
|
81
|
+
case 'string':
|
|
82
|
+
return STRING;
|
|
83
|
+
case 'i64':
|
|
84
|
+
return TIME;
|
|
85
|
+
case undefined:
|
|
86
|
+
return STRUCT;
|
|
87
|
+
}
|
|
88
|
+
// Only a node ref's handle, cast to a slot, reaches here.
|
|
89
|
+
return NODE;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* The type of a value lowered to C: its slot, and what the C form records about it.
|
|
93
|
+
*
|
|
94
|
+
* @param value The lowered value.
|
|
95
|
+
*
|
|
96
|
+
* @returns The type.
|
|
97
|
+
*/
|
|
98
|
+
export function typeOfCExpr(value) {
|
|
99
|
+
if (value.lit || value.wide) {
|
|
100
|
+
if (value.cType !== 'i64' || (value.lit && value.wide)) {
|
|
101
|
+
throw new Error('AOT internal: a 64-bit source on a value that is not one 64-bit number');
|
|
102
|
+
}
|
|
103
|
+
return value.lit ? INT64_NUMBER : INT64_MATH;
|
|
104
|
+
}
|
|
105
|
+
return typeOfSlot(value.cType, value.isBool);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Whether a value is a time: a 64-bit number computed at runtime from a clock.
|
|
109
|
+
*
|
|
110
|
+
* @param type The value's type.
|
|
111
|
+
*
|
|
112
|
+
* @returns True for a time.
|
|
113
|
+
*/
|
|
114
|
+
export const isTime = (type) => type.kind === 'int64' && type.source === 'time';
|
|
115
|
+
/**
|
|
116
|
+
* Whether a value is a JS boolean.
|
|
117
|
+
*
|
|
118
|
+
* @param type The value's type.
|
|
119
|
+
*
|
|
120
|
+
* @returns True for a boolean.
|
|
121
|
+
*/
|
|
122
|
+
export const isBoolean = (type) => type.kind === 'bool';
|
|
123
|
+
/**
|
|
124
|
+
* Whether a value is a 64-bit number that is not a time (a number past the int range, or whole-number math),
|
|
125
|
+
* which converts to float like any other number.
|
|
126
|
+
*
|
|
127
|
+
* @param type The value's type.
|
|
128
|
+
*
|
|
129
|
+
* @returns True for such a number.
|
|
130
|
+
*/
|
|
131
|
+
export const isConvertible64 = (type) => type.kind === 'int64' && type.source !== 'time';
|
|
132
|
+
/**
|
|
133
|
+
* Whether a value is stored in an int: an int, or a boolean.
|
|
134
|
+
*
|
|
135
|
+
* @param type The value's type.
|
|
136
|
+
*
|
|
137
|
+
* @returns True for an int slot.
|
|
138
|
+
*/
|
|
139
|
+
export const isIntSlot = (type) => type.kind === 'int' || type.kind === 'bool';
|
|
140
|
+
/**
|
|
141
|
+
* The type of a 64-bit value picked from several (a ternary's branches, the operands of `&&` / `||`): a time
|
|
142
|
+
* when any of them is one, a number when every 64-bit one is, whole-number math otherwise.
|
|
143
|
+
*
|
|
144
|
+
* @param types The types it is picked from.
|
|
145
|
+
*
|
|
146
|
+
* @returns The picked value's type.
|
|
147
|
+
*/
|
|
148
|
+
export function int64Join(...types) {
|
|
149
|
+
const sources = types.flatMap(type => type.kind === 'int64' ? [type.source] : []);
|
|
150
|
+
if (sources.includes('time'))
|
|
151
|
+
return TIME;
|
|
152
|
+
return sources.every(source => source === 'number')
|
|
153
|
+
? INT64_NUMBER
|
|
154
|
+
: INT64_MATH;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* The type of a 64-bit slot: a time, unless it only ever holds 64-bit numbers that are not times.
|
|
158
|
+
*
|
|
159
|
+
* @param widened What the slot was widened to hold, if it was.
|
|
160
|
+
*
|
|
161
|
+
* @returns The type of a read of the slot.
|
|
162
|
+
*/
|
|
163
|
+
const wideSlotType = (widened) => widened === 'number' ? INT64_NUMBER : TIME;
|
|
164
|
+
/**
|
|
165
|
+
* The type of a scalar state's value.
|
|
166
|
+
*
|
|
167
|
+
* @param state The state.
|
|
168
|
+
*
|
|
169
|
+
* @returns Its type.
|
|
170
|
+
*/
|
|
171
|
+
export const stateType = (state) => state.cType === 'i64'
|
|
172
|
+
? wideSlotType(state.widened)
|
|
173
|
+
: typeOfSlot(state.cType, state.isBool);
|
|
174
|
+
/**
|
|
175
|
+
* The type of a ref's current value.
|
|
176
|
+
*
|
|
177
|
+
* @param ref The ref.
|
|
178
|
+
*
|
|
179
|
+
* @returns A node handle for a node ref, the value's type otherwise.
|
|
180
|
+
*/
|
|
181
|
+
export const refType = (ref) => ref.kind === 'node'
|
|
182
|
+
? NODE
|
|
183
|
+
: ref.cType === 'i64'
|
|
184
|
+
? wideSlotType(ref.widened)
|
|
185
|
+
: typeOfSlot(ref.cType);
|
|
@@ -0,0 +1,16 @@
|
|
|
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
|
+
export {};
|
|
@@ -0,0 +1,152 @@
|
|
|
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
|
+
- Imports
|
|
18
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
19
|
+
import { parseColor } from '../../src/embedded-react/svg-ops.js';
|
|
20
|
+
import { aotError } from './diagnostics.mjs';
|
|
21
|
+
import { evalStatic } from './static-eval.mjs';
|
|
22
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
23
|
+
- Implementation
|
|
24
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
25
|
+
/**
|
|
26
|
+
* A keyboard-config color as ARGB; null when the config leaves it out (the engine's default).
|
|
27
|
+
*
|
|
28
|
+
* @param color The color, if the config gives one.
|
|
29
|
+
*
|
|
30
|
+
* @returns The color, or null.
|
|
31
|
+
*/
|
|
32
|
+
const kbdColor = (color) => color == null ? null : parseColor(String(color)) >>> 0;
|
|
33
|
+
/**
|
|
34
|
+
* A keyboard-config size, rounded; null when the config leaves it out (the engine's default).
|
|
35
|
+
*
|
|
36
|
+
* @param size The size, if the config gives one.
|
|
37
|
+
*
|
|
38
|
+
* @returns The size, or null.
|
|
39
|
+
*/
|
|
40
|
+
const kbdSize = (size) => size == null ? null : Math.round(Number(size));
|
|
41
|
+
/**
|
|
42
|
+
* Lowers one JS keyboard key object. A key marked `highlight` (e.g., shift) is drawn highlighted while its own
|
|
43
|
+
* layer is showing. Shapes: { char } (types it), { char:' ', span } (space), { label, layer } (switch),
|
|
44
|
+
* { label, backspace }, { label, done }; optional span / highlight.
|
|
45
|
+
*
|
|
46
|
+
* @param key The key's config object.
|
|
47
|
+
* @param layerIndex The index of the layer the key is on.
|
|
48
|
+
*
|
|
49
|
+
* @returns The key.
|
|
50
|
+
*/
|
|
51
|
+
function lowerKey(key, layerIndex) {
|
|
52
|
+
if (key == null || typeof key !== 'object') {
|
|
53
|
+
throw aotError('AOT: each setKeyboardConfig key must be an object', 'e.g. { char: "q" } or { label: "shift", layer: 1, highlight: true }');
|
|
54
|
+
}
|
|
55
|
+
// Work out the key's kind from the one action field it carries, with that kind's default label.
|
|
56
|
+
let kind;
|
|
57
|
+
let label;
|
|
58
|
+
let text = null;
|
|
59
|
+
let layer = 0;
|
|
60
|
+
if (key.backspace) {
|
|
61
|
+
kind = 'backspace';
|
|
62
|
+
label = key.label ?? '<';
|
|
63
|
+
}
|
|
64
|
+
else if (key.done) {
|
|
65
|
+
kind = 'done';
|
|
66
|
+
label = key.label ?? 'OK';
|
|
67
|
+
}
|
|
68
|
+
else if (key.layer != null) {
|
|
69
|
+
kind = 'layer';
|
|
70
|
+
label = key.label ?? '';
|
|
71
|
+
layer = Math.round(Number(key.layer));
|
|
72
|
+
}
|
|
73
|
+
else if (key.char != null) {
|
|
74
|
+
kind = 'char';
|
|
75
|
+
text = String(key.char);
|
|
76
|
+
label = key.label ?? (String(key.char) === ' ' ? '' : String(key.char)); // a space bar shows no label
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
throw aotError('AOT: a setKeyboardConfig key needs one of char / layer / backspace / done');
|
|
80
|
+
}
|
|
81
|
+
// Span defaults to one grid column.
|
|
82
|
+
return {
|
|
83
|
+
kind,
|
|
84
|
+
label: label === '' ? null : String(label),
|
|
85
|
+
text,
|
|
86
|
+
layer,
|
|
87
|
+
span: key.span != null ? Math.round(Number(key.span)) : 1,
|
|
88
|
+
highlightLayer: key.highlight ? layerIndex : null,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Lowers a module-level `setKeyboardConfig({...})`, which customizes the on-screen keyboard (colors/sizes, and
|
|
93
|
+
* optionally a full layout) from the app with no engine edit. The config must be statically foldable; omit
|
|
94
|
+
* `layers` to keep the built-in QWERTY.
|
|
95
|
+
*
|
|
96
|
+
* @param program The parsed module.
|
|
97
|
+
*
|
|
98
|
+
* @returns The config, or null when the module has none.
|
|
99
|
+
*/
|
|
100
|
+
export function lowerKeyboardConfig(program) {
|
|
101
|
+
// Find the first top-level setKeyboardConfig(...) call; without one, the built-in keyboard stays as is.
|
|
102
|
+
let configArg = null;
|
|
103
|
+
for (const statement of program.body) {
|
|
104
|
+
if (statement.type === 'ExpressionStatement' &&
|
|
105
|
+
statement.expression.type === 'CallExpression' &&
|
|
106
|
+
statement.expression.callee.type === 'Identifier' &&
|
|
107
|
+
statement.expression.callee.name === 'setKeyboardConfig') {
|
|
108
|
+
configArg = statement.expression.arguments[0];
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (!configArg)
|
|
113
|
+
return null;
|
|
114
|
+
// Fold the config object at compile time.
|
|
115
|
+
let config;
|
|
116
|
+
try {
|
|
117
|
+
config = evalStatic(configArg, {});
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
throw aotError('AOT: setKeyboardConfig(...) needs a statically-foldable config object', 'pass an object literal of colors/sizes (+ an optional `layers` array) — no state or runtime values.');
|
|
121
|
+
}
|
|
122
|
+
if (config == null || typeof config !== 'object') {
|
|
123
|
+
throw aotError('AOT: setKeyboardConfig(...) needs a config object');
|
|
124
|
+
}
|
|
125
|
+
// Lower the layout when the app gives one, layer by layer and row by row.
|
|
126
|
+
let layers = null;
|
|
127
|
+
if (Array.isArray(config.layers)) {
|
|
128
|
+
layers = config.layers.map((layer, layerIndex) => {
|
|
129
|
+
if (!Array.isArray(layer)) {
|
|
130
|
+
throw aotError('AOT: setKeyboardConfig `layers[i]` must be an array of rows');
|
|
131
|
+
}
|
|
132
|
+
return layer.map(row => {
|
|
133
|
+
if (!Array.isArray(row) || !row.length) {
|
|
134
|
+
throw aotError('AOT: each keyboard row must be a non-empty array of keys');
|
|
135
|
+
}
|
|
136
|
+
return row.map(key => lowerKey(key, layerIndex));
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
return {
|
|
141
|
+
layers,
|
|
142
|
+
gridCols: kbdSize(config.gridCols),
|
|
143
|
+
rowHeight: kbdSize(config.rowHeight),
|
|
144
|
+
keyGap: kbdSize(config.keyGap),
|
|
145
|
+
keyRadius: kbdSize(config.keyRadius),
|
|
146
|
+
fontSize: kbdSize(config.fontSize),
|
|
147
|
+
panelColor: kbdColor(config.panelColor),
|
|
148
|
+
keyColor: kbdColor(config.keyColor),
|
|
149
|
+
keyActiveColor: kbdColor(config.keyActiveColor),
|
|
150
|
+
labelColor: kbdColor(config.labelColor),
|
|
151
|
+
};
|
|
152
|
+
}
|
|
@@ -0,0 +1,311 @@
|
|
|
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
|
+
- Imports
|
|
18
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
19
|
+
import { checkNumber, lowerWithChecks, runChecks } from './check.mjs';
|
|
20
|
+
import { aotError } from './diagnostics.mjs';
|
|
21
|
+
import { evalStatic, withUndefined } from './static-eval.mjs';
|
|
22
|
+
import { abandonBoot, beginBoot, endBoot, setBootLowering } from './boot.mjs';
|
|
23
|
+
import { findShadowedClockCalls, emitSourcedWide, lowerExpr, } from './expressions.mjs';
|
|
24
|
+
import { findComponent, declaredNames, moduleScope, collectState, findReturnJSX, collectComponents, collectHelpers, collectImageImports, collectSvgImports, collectCallbacks, collectMemos, collectEffects, bindConst, } from './collect.mjs';
|
|
25
|
+
import { collectAnims, collectRefs } from './animations.mjs';
|
|
26
|
+
import { lowerEffect } from './handlers.mjs';
|
|
27
|
+
import { collectPanResponders } from './pan-responder.mjs';
|
|
28
|
+
import { emitNode } from './nodes.mjs';
|
|
29
|
+
import { lowerKeyboardConfig } from './keyboard.mjs';
|
|
30
|
+
import { normalizeUndefined, parseApp } from './parse.mjs';
|
|
31
|
+
import { Out } from './out.mjs';
|
|
32
|
+
import { isStateDriven, nodesOf, updatedVectorOf } from './ir/nodes.mjs';
|
|
33
|
+
import { nodeReads, vectorReads } from './ir/reads.mjs';
|
|
34
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
35
|
+
- Implementation
|
|
36
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
37
|
+
/**
|
|
38
|
+
* Settles what only the whole app decides. With nothing for app_update() to re-apply there is no app_update(),
|
|
39
|
+
* so no body ends by calling it. A composition that no start waits on has no waits for a stop to end.
|
|
40
|
+
*
|
|
41
|
+
* @param out The lowered app's functions.
|
|
42
|
+
* @param hasUpdate The app has an app_update().
|
|
43
|
+
*/
|
|
44
|
+
function settleFunctions(out, hasUpdate) {
|
|
45
|
+
const idleOwners = new Set([...out.animWaits.values()]
|
|
46
|
+
.filter(record => !record.isStarted)
|
|
47
|
+
.map(record => record.owner));
|
|
48
|
+
const settleStmts = (stmts) => {
|
|
49
|
+
for (const stmt of stmts) {
|
|
50
|
+
if (stmt.kind === 'stopAnimations') {
|
|
51
|
+
if (stmt.waitOwner !== null && idleOwners.has(stmt.waitOwner)) {
|
|
52
|
+
stmt.waitOwner = null;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
else if (stmt.kind === 'if') {
|
|
56
|
+
settleStmts(stmt.consequent);
|
|
57
|
+
settleStmts(stmt.alternate ?? []);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
const settle = (callback) => {
|
|
62
|
+
if (!callback)
|
|
63
|
+
return;
|
|
64
|
+
if (!hasUpdate) {
|
|
65
|
+
callback.updatesState = false;
|
|
66
|
+
}
|
|
67
|
+
settleStmts(callback.body);
|
|
68
|
+
};
|
|
69
|
+
for (const handler of out.handlers) {
|
|
70
|
+
if (handler.kind === 'callback') {
|
|
71
|
+
settle(handler.callback);
|
|
72
|
+
}
|
|
73
|
+
else if (handler.kind === 'panEvent') {
|
|
74
|
+
settle(handler.event.callback);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
for (const timer of out.timerFns) {
|
|
78
|
+
settle(timer.callback);
|
|
79
|
+
}
|
|
80
|
+
for (const animationCallback of out.animCbs) {
|
|
81
|
+
if (animationCallback.kind === 'completion') {
|
|
82
|
+
settle(animationCallback.completion);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
for (const effectFunction of out.effectFns) {
|
|
86
|
+
if (effectFunction.kind === 'mount') {
|
|
87
|
+
settleStmts(effectFunction.body);
|
|
88
|
+
}
|
|
89
|
+
else if (effectFunction.kind === 'cleanup') {
|
|
90
|
+
settleStmts(effectFunction.effect.cleanup);
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
settleStmts(effectFunction.effect.body);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
for (const mountEffect of out.mountEffects) {
|
|
97
|
+
if (mountEffect.kind === 'inline') {
|
|
98
|
+
settle(mountEffect.callback);
|
|
99
|
+
}
|
|
100
|
+
else if (mountEffect.kind === 'call' && !hasUpdate) {
|
|
101
|
+
mountEffect.updatesState = false;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Flags each state a node or drawing reads, so a write to it marks it changed and app_update() sets them again.
|
|
107
|
+
* A node that also reads something untracked is set on every update, so its reads need no flag.
|
|
108
|
+
*
|
|
109
|
+
* @param nodes The app's nodes.
|
|
110
|
+
*/
|
|
111
|
+
function watchState(nodes) {
|
|
112
|
+
for (const node of nodes) {
|
|
113
|
+
const vector = updatedVectorOf(node);
|
|
114
|
+
for (const reads of [nodeReads(node), vector && vectorReads(vector)]) {
|
|
115
|
+
reads?.forEach(record => {
|
|
116
|
+
record.isWatched = true;
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Refuses state C cannot hold: a number in a list item that has no C form, and a host value that would take
|
|
123
|
+
* the name of the setter every app exports for the wall clock. A field an item leaves out starts at 0.
|
|
124
|
+
*
|
|
125
|
+
* @param stateRecords The app's state, then each inlined instance's.
|
|
126
|
+
*/
|
|
127
|
+
function checkStateRecords(stateRecords) {
|
|
128
|
+
for (const record of stateRecords) {
|
|
129
|
+
if (record.kind !== 'list')
|
|
130
|
+
continue;
|
|
131
|
+
for (const item of record.items) {
|
|
132
|
+
for (const field of record.struct.fields) {
|
|
133
|
+
const value = item[field.key];
|
|
134
|
+
if (field.kind === 'string' || value == null)
|
|
135
|
+
continue;
|
|
136
|
+
const number = Number(value);
|
|
137
|
+
if (field.kind === 'float' || !Number.isFinite(number)) {
|
|
138
|
+
checkNumber(number);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (stateRecords.some(record => record.kind === 'scalar' && record.host && record.name === 'wall_clock')) {
|
|
144
|
+
throw aotError('AOT: useHostValue("wall_clock") would collide with er_app_set_wall_clock()', 'rename the host value — every app exports er_app_set_wall_clock() for Date.now().');
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Lowers a Flow B app's JSX (or TSX) source, then runs the check pass over what lowering recorded. Pure (no
|
|
149
|
+
* I/O); writeApp (assemble.mts) writes the result as C.
|
|
150
|
+
*
|
|
151
|
+
* @param src The App.jsx/App.tsx source text.
|
|
152
|
+
* @param opts compileSource's options, resolved (see resolveOptions).
|
|
153
|
+
* @param wide State/ref slots to declare 64-bit (see compileWidened).
|
|
154
|
+
* @param stores Collects every value stored into an int slot.
|
|
155
|
+
*
|
|
156
|
+
* @returns The lowered app.
|
|
157
|
+
*/
|
|
158
|
+
export function lowerApp(src, opts, wide, stores) {
|
|
159
|
+
beginBoot();
|
|
160
|
+
try {
|
|
161
|
+
const { result, checks } = lowerWithChecks(() => lowerProgram(src, opts, wide, stores));
|
|
162
|
+
const app = { ...result, boot: endBoot(), checks };
|
|
163
|
+
runChecks(app.checks);
|
|
164
|
+
return app;
|
|
165
|
+
}
|
|
166
|
+
finally {
|
|
167
|
+
abandonBoot();
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Runs the compiler passes over the parsed app, from its module scope and hooks through the element tree to its
|
|
172
|
+
* effects, recording the values the check pass checks.
|
|
173
|
+
*
|
|
174
|
+
* @param src The App.jsx/App.tsx source text.
|
|
175
|
+
* @param opts compileSource's options, resolved (see resolveOptions).
|
|
176
|
+
* @param wide State/ref slots to declare 64-bit (see compileWidened).
|
|
177
|
+
* @param stores Collects every value stored into an int slot.
|
|
178
|
+
*
|
|
179
|
+
* @returns The lowered app, without its checks.
|
|
180
|
+
*/
|
|
181
|
+
function lowerProgram(src, opts, wide, stores) {
|
|
182
|
+
// Parse the app and prepare the AST for the passes below.
|
|
183
|
+
const ast = parseApp(src, opts);
|
|
184
|
+
normalizeUndefined(ast);
|
|
185
|
+
const shadowedClock = findShadowedClockCalls(ast);
|
|
186
|
+
// Seed the module scope with image imports (each is its baked-name string) so consts that use them can fold.
|
|
187
|
+
const { screen } = opts;
|
|
188
|
+
const imageImports = collectImageImports(ast.program);
|
|
189
|
+
const svgImports = collectSvgImports(ast.program);
|
|
190
|
+
const imageSeed = Object.fromEntries([...imageImports].map(([local, imageImport]) => [local, imageImport.name]));
|
|
191
|
+
const scope = moduleScope(ast.program, screen, imageSeed);
|
|
192
|
+
// Snapshot module bindings before App's consts fold in: child components close over these, not App's shadows.
|
|
193
|
+
const moduleConsts = { ...scope };
|
|
194
|
+
const component = findComponent(ast.program);
|
|
195
|
+
// Hide redeclared module names, then fold the body's static consts so responsive `if`s resolve at compile time.
|
|
196
|
+
for (const name of declaredNames(component.body.body)) {
|
|
197
|
+
delete scope[name];
|
|
198
|
+
}
|
|
199
|
+
for (const statement of component.body.body) {
|
|
200
|
+
if (statement.type !== 'VariableDeclaration' ||
|
|
201
|
+
statement.kind !== 'const') {
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
for (const decl of statement.declarations) {
|
|
205
|
+
if (decl.id.type !== 'Identifier' || !decl.init)
|
|
206
|
+
continue;
|
|
207
|
+
try {
|
|
208
|
+
scope[decl.id.name] = bindConst(decl.id.name, evalStatic(decl.init, withUndefined(scope), 'boot'));
|
|
209
|
+
}
|
|
210
|
+
catch {
|
|
211
|
+
// A dynamic const must hide a same-named module const, or the JSX quietly renders the module value.
|
|
212
|
+
delete scope[decl.id.name];
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
// Collect the component's state; a state name hides any module const of the same name from the folds.
|
|
217
|
+
const state = collectState(component.body, scope, opts.caps, '', wide);
|
|
218
|
+
for (const name of state.byName.keys()) {
|
|
219
|
+
delete scope[name];
|
|
220
|
+
}
|
|
221
|
+
// Collect the component's other hooks, plus the file's helpers and child components.
|
|
222
|
+
const rootJSX = findReturnJSX(component.body, scope);
|
|
223
|
+
const anims = collectAnims(component.body, scope);
|
|
224
|
+
const refs = collectRefs(component.body, scope, '', wide);
|
|
225
|
+
for (const name of [...anims.keys(), ...refs.keys()]) {
|
|
226
|
+
delete scope[name];
|
|
227
|
+
}
|
|
228
|
+
const pans = collectPanResponders(component.body);
|
|
229
|
+
const callbacks = collectCallbacks(component.body);
|
|
230
|
+
const memos = collectMemos(component.body);
|
|
231
|
+
const helpers = collectHelpers(component.body, ast.program);
|
|
232
|
+
const components = collectComponents(ast.program);
|
|
233
|
+
const imageNames = new Map([...imageImports].map(([, imageImport]) => [
|
|
234
|
+
imageImport.name,
|
|
235
|
+
imageImport.importPath,
|
|
236
|
+
])); // asset name → path
|
|
237
|
+
// The expression environment every emitter compiles against.
|
|
238
|
+
const env = {
|
|
239
|
+
state: state.byName,
|
|
240
|
+
locals: new Map(),
|
|
241
|
+
consts: scope,
|
|
242
|
+
moduleConsts, // what an inlined child component sees (see emitComponent)
|
|
243
|
+
animations: anims,
|
|
244
|
+
refs,
|
|
245
|
+
pans,
|
|
246
|
+
callbacks,
|
|
247
|
+
helpers,
|
|
248
|
+
imageNames,
|
|
249
|
+
svgImports,
|
|
250
|
+
svgArtifacts: opts.svgArtifacts || {},
|
|
251
|
+
caps: opts.caps,
|
|
252
|
+
wide,
|
|
253
|
+
stores,
|
|
254
|
+
shadowedClock,
|
|
255
|
+
};
|
|
256
|
+
// A value worked out at boot lowers its slot against the app's environment.
|
|
257
|
+
setBootLowering(env, lowerExpr);
|
|
258
|
+
// Before emit, fold each memo or inline its C expression at each use (no cache needed: dep tracking re-applies).
|
|
259
|
+
for (const [name, expr] of memos) {
|
|
260
|
+
try {
|
|
261
|
+
scope[name] = bindConst(name, evalStatic(expr, scope, 'boot'));
|
|
262
|
+
}
|
|
263
|
+
catch {
|
|
264
|
+
const cExpr = emitSourcedWide(expr, env);
|
|
265
|
+
env.locals.set(name, {
|
|
266
|
+
code: `(${cExpr.code})`,
|
|
267
|
+
cType: cExpr.cType,
|
|
268
|
+
isBool: cExpr.isBool,
|
|
269
|
+
lit: cExpr.lit,
|
|
270
|
+
wide: cExpr.wide,
|
|
271
|
+
source: cExpr.source,
|
|
272
|
+
});
|
|
273
|
+
delete scope[name]; // a runtime binding beats a module const of its name in every fold
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
// Emit the scene graph from the root JSX; `out` collects every generated fragment.
|
|
277
|
+
const out = new Out(components, ast.program);
|
|
278
|
+
const keyboard = lowerKeyboardConfig(ast.program); // a module-level setKeyboardConfig({...})
|
|
279
|
+
const root = emitNode(rootJSX, scope, out, env, state);
|
|
280
|
+
// Emit registered only the images it reached; a dynamic source can't be enumerated, so bake every import.
|
|
281
|
+
if (out.bakeAllImages) {
|
|
282
|
+
for (const [, imageImport] of imageImports) {
|
|
283
|
+
out.images.set(imageImport.name, imageImport.importPath);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
// Lower effects (mount-once or dep-driven) after emit, so out.timerFns/usesTimers include handler timers too.
|
|
287
|
+
for (const effect of collectEffects(component.body)) {
|
|
288
|
+
lowerEffect(effect, env, state, out);
|
|
289
|
+
}
|
|
290
|
+
// app_update() exists only when something has to be re-applied after a state change.
|
|
291
|
+
const hasUpdate = nodesOf(root).some(node => isStateDriven(node) || updatedVectorOf(node) !== null) || out.depEffects.length > 0;
|
|
292
|
+
settleFunctions(out, hasUpdate);
|
|
293
|
+
if (hasUpdate) {
|
|
294
|
+
watchState(nodesOf(root));
|
|
295
|
+
}
|
|
296
|
+
const stateRecords = [...state.byName.values(), ...out.childStateRecords];
|
|
297
|
+
checkStateRecords(stateRecords);
|
|
298
|
+
return {
|
|
299
|
+
state: stateRecords,
|
|
300
|
+
refs: [...refs.values(), ...out.childRefs],
|
|
301
|
+
animations: [...anims.values(), ...out.childAnimations],
|
|
302
|
+
keyboard,
|
|
303
|
+
root,
|
|
304
|
+
hasUpdate,
|
|
305
|
+
images: [...out.images.entries()].map(([name, importPath]) => ({
|
|
306
|
+
name,
|
|
307
|
+
importPath,
|
|
308
|
+
})),
|
|
309
|
+
out,
|
|
310
|
+
};
|
|
311
|
+
}
|