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.
- package/NOTICE +5 -0
- package/README.md +1 -0
- package/aot/app-file-loader.mjs +116 -0
- package/aot/compile.mjs +16 -46
- package/aot/compiler/animations.mjs +299 -57
- package/aot/compiler/boot.mjs +27 -2
- package/aot/compiler/check.mjs +61 -9
- package/aot/compiler/collect.mjs +516 -136
- package/aot/compiler/contexts.mjs +391 -0
- package/aot/compiler/control-flow.mjs +276 -117
- package/aot/compiler/custom-hooks.mjs +686 -0
- package/aot/compiler/elements/button.mjs +182 -0
- package/aot/compiler/elements/image-background.mjs +76 -0
- package/aot/compiler/elements/image.mjs +2 -1
- package/aot/compiler/elements/props.mjs +38 -3
- package/aot/compiler/elements/rewrite.mjs +118 -0
- package/aot/compiler/elements/section-list.mjs +370 -0
- package/aot/compiler/expressions.mjs +1351 -97
- package/aot/compiler/handlers.mjs +1761 -365
- package/aot/compiler/helper-values.mjs +312 -0
- package/aot/compiler/ir/nodes.mjs +40 -1
- package/aot/compiler/ir/reads.mjs +206 -5
- package/aot/compiler/ir/text.mjs +5 -1
- package/aot/compiler/ir/types.mjs +12 -0
- package/aot/compiler/kept-states.mjs +177 -0
- package/aot/compiler/list-items.mjs +226 -0
- package/aot/compiler/list-order.mjs +586 -0
- package/aot/compiler/list-split.mjs +259 -0
- package/aot/compiler/loops.mjs +582 -0
- package/aot/compiler/lower.mjs +191 -53
- package/aot/compiler/native-files.mjs +439 -0
- package/aot/compiler/native-header.mjs +335 -0
- package/aot/compiler/native-modules.mjs +1173 -0
- package/aot/compiler/nodes.mjs +28 -22
- package/aot/compiler/options.mjs +3 -2
- package/aot/compiler/out.mjs +14 -1
- package/aot/compiler/package/animations.mjs +133 -23
- package/aot/compiler/package/assemble.mjs +2 -4
- package/aot/compiler/package/bytecode.mjs +37 -8
- package/aot/compiler/package/checks.mjs +4 -0
- package/aot/compiler/package/codegen.mjs +870 -31
- package/aot/compiler/package/enums.mjs +10 -0
- package/aot/compiler/package/format.mjs +12 -1
- package/aot/compiler/package/lower.mjs +658 -161
- package/aot/compiler/package/string-caps.mjs +515 -0
- package/aot/compiler/package/tables.mjs +24 -0
- package/aot/compiler/package/writer.mjs +22 -0
- package/aot/compiler/parse.mjs +3 -1
- package/aot/compiler/record-shapes.mjs +246 -0
- package/aot/compiler/records.mjs +654 -0
- package/aot/compiler/reducers.mjs +272 -0
- package/aot/compiler/static-eval.mjs +315 -7
- package/aot/compiler/style-text.mjs +18 -3
- package/aot/compiler/svg.mjs +7 -4
- package/aot/compiler/switch-cases.mjs +198 -0
- package/aot/compiler/tagged-records.mjs +450 -0
- package/aot/compiler/typing.mjs +307 -0
- package/aot/compiler/updaters.mjs +320 -0
- package/aot/compiler/window-dimensions.mjs +94 -0
- package/aot/compiler/worked-values.mjs +319 -0
- package/aot/demo-file.mjs +30 -0
- package/aot/style-map.mjs +50 -2
- package/build.mjs +9 -1
- package/cli.mjs +44 -1
- package/hotreload/pack-split.mjs +29 -40
- package/pack-container.mjs +9 -1
- package/package.json +1 -1
- package/sim/embedded-react.wasm +0 -0
- package/sim-server.mjs +22 -22
- package/src/embedded-react/Dimensions.js +37 -0
- package/src/embedded-react/SectionList.js +1 -3
- package/src/embedded-react/index.d.ts +83 -3
- package/src/embedded-react/index.js +2 -0
- package/src/embedded-react/nativeModule.js +462 -0
|
@@ -0,0 +1,272 @@
|
|
|
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
|
+
* useReducer: each `dispatch(action)` runs the reducer in place, as an updater with a block body. The action is bound
|
|
18
|
+
* where it is dispatched: its constant fields fold, so the branches its type rules out are dropped before the body is
|
|
19
|
+
* lowered, and a `throw` for an action no dispatch sends never runs.
|
|
20
|
+
*/
|
|
21
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
22
|
+
- Imports
|
|
23
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
24
|
+
import { lowerLocal } from './expressions.mjs';
|
|
25
|
+
import { exits, statementsOf } from './helper-values.mjs';
|
|
26
|
+
import { AstLeaf, isObjectTree, objectTreeOf } from './records.mjs';
|
|
27
|
+
import { RuntimeLeaf, evalStatic, foldScope, rethrowFoldLimit, withoutNames, } from './static-eval.mjs';
|
|
28
|
+
import { switchAsIf } from './switch-cases.mjs';
|
|
29
|
+
import { declaredIn } from './updaters.mjs';
|
|
30
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
31
|
+
- Implementation
|
|
32
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
33
|
+
/**
|
|
34
|
+
* What an expression folds to, if it does.
|
|
35
|
+
*
|
|
36
|
+
* @param node The expression.
|
|
37
|
+
* @param scope The constants a fold may read.
|
|
38
|
+
*
|
|
39
|
+
* @returns The value, or undefined when it does not fold.
|
|
40
|
+
*/
|
|
41
|
+
function folded(node, scope) {
|
|
42
|
+
try {
|
|
43
|
+
return { value: evalStatic(node, scope, 'strict') };
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
rethrowFoldLimit(error);
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The local a runtime value is held in, worked out where it is written.
|
|
52
|
+
*
|
|
53
|
+
* @param node The value.
|
|
54
|
+
* @param environment The environment it is written in.
|
|
55
|
+
*
|
|
56
|
+
* @returns The local.
|
|
57
|
+
*/
|
|
58
|
+
function localOf(node, environment) {
|
|
59
|
+
return lowerLocal(node, environment);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* An object's fields as the reducer reads them: a constant stays one, and anything else is worked out at the
|
|
63
|
+
* dispatch, so names the reducer declares can't change what it reads.
|
|
64
|
+
*
|
|
65
|
+
* @param tree The object.
|
|
66
|
+
* @param environment The dispatch's environment.
|
|
67
|
+
* @param scope The constants a fold may read there.
|
|
68
|
+
*
|
|
69
|
+
* @returns The object.
|
|
70
|
+
*/
|
|
71
|
+
function settledTree(tree, environment, scope) {
|
|
72
|
+
return Object.fromEntries(Object.entries(tree).map(([key, value]) => {
|
|
73
|
+
if (isObjectTree(value)) {
|
|
74
|
+
return [key, settledTree(value, environment, scope)];
|
|
75
|
+
}
|
|
76
|
+
if (!(value instanceof AstLeaf))
|
|
77
|
+
return [key, value];
|
|
78
|
+
const constant = value.locals.size
|
|
79
|
+
? undefined
|
|
80
|
+
: folded(value.node, scope);
|
|
81
|
+
if (constant && typeof constant.value !== 'object') {
|
|
82
|
+
return [key, constant.value];
|
|
83
|
+
}
|
|
84
|
+
const fieldEnvironment = value.locals.size
|
|
85
|
+
? {
|
|
86
|
+
...environment,
|
|
87
|
+
locals: new Map([...environment.locals, ...value.locals]),
|
|
88
|
+
}
|
|
89
|
+
: environment;
|
|
90
|
+
return [key, new RuntimeLeaf(localOf(value.node, fieldEnvironment))];
|
|
91
|
+
}));
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* An action that picks between two at run time: a ternary of actions, or an object with a field that is a ternary of
|
|
95
|
+
* two constants, `{type: up ? 'inc' : 'dec'}`. Each side is dispatched on its own, so its constant fields fold.
|
|
96
|
+
*
|
|
97
|
+
* @param action What is dispatched.
|
|
98
|
+
* @param scope The constants a fold may read.
|
|
99
|
+
*
|
|
100
|
+
* @returns The test and the action each way, or null when the action picks nothing.
|
|
101
|
+
*/
|
|
102
|
+
export function actionChoice(action, scope) {
|
|
103
|
+
if (action.type === 'ConditionalExpression' && !folded(action.test, scope)) {
|
|
104
|
+
return {
|
|
105
|
+
test: action.test,
|
|
106
|
+
whenTrue: action.consequent,
|
|
107
|
+
whenFalse: action.alternate,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
if (action.type !== 'ObjectExpression')
|
|
111
|
+
return null;
|
|
112
|
+
for (const [index, property] of action.properties.entries()) {
|
|
113
|
+
if (property.type !== 'ObjectProperty' ||
|
|
114
|
+
property.value.type !== 'ConditionalExpression' ||
|
|
115
|
+
folded(property.value.test, scope)) {
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
const { test, consequent, alternate } = property.value;
|
|
119
|
+
const isConstant = (node) => {
|
|
120
|
+
const value = folded(node, scope);
|
|
121
|
+
return value !== undefined && typeof value.value !== 'object';
|
|
122
|
+
};
|
|
123
|
+
if (!isConstant(consequent) || !isConstant(alternate))
|
|
124
|
+
continue;
|
|
125
|
+
const withValue = (value) => ({
|
|
126
|
+
...action,
|
|
127
|
+
properties: action.properties.map((other, at) => at === index ? { ...property, value } : other),
|
|
128
|
+
});
|
|
129
|
+
return {
|
|
130
|
+
test,
|
|
131
|
+
whenTrue: withValue(consequent),
|
|
132
|
+
whenFalse: withValue(alternate),
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* The environment a reducer's body runs in for one dispatch: the action parameter bound to what is dispatched. A
|
|
139
|
+
* constant folds, an object is its fields, and anything else is a local holding the value.
|
|
140
|
+
*
|
|
141
|
+
* @param parameter The action parameter's name, if the reducer has one.
|
|
142
|
+
* @param action What the dispatch passes.
|
|
143
|
+
* @param environment The dispatch's environment.
|
|
144
|
+
*
|
|
145
|
+
* @returns The body's environment.
|
|
146
|
+
*/
|
|
147
|
+
export function actionEnvironment(parameter, action, environment) {
|
|
148
|
+
if (!parameter)
|
|
149
|
+
return environment;
|
|
150
|
+
const scope = foldScope(environment, environment.constants ?? {});
|
|
151
|
+
const locals = new Map(environment.locals);
|
|
152
|
+
const state = new Map(environment.state);
|
|
153
|
+
const constants = { ...environment.constants };
|
|
154
|
+
locals.delete(parameter);
|
|
155
|
+
state.delete(parameter);
|
|
156
|
+
delete constants[parameter];
|
|
157
|
+
const constant = folded(action, scope);
|
|
158
|
+
const tree = constant ? null : objectTreeOf(action, environment);
|
|
159
|
+
if (constant) {
|
|
160
|
+
constants[parameter] = constant.value;
|
|
161
|
+
}
|
|
162
|
+
else if (tree) {
|
|
163
|
+
constants[parameter] = settledTree(tree, environment, scope);
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
locals.set(parameter, localOf(action, environment));
|
|
167
|
+
}
|
|
168
|
+
return { ...environment, locals, state, constants };
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* A value with each ternary whose test folds made the side it picks.
|
|
172
|
+
*
|
|
173
|
+
* @param node The value.
|
|
174
|
+
* @param scope The constants a fold may read.
|
|
175
|
+
*
|
|
176
|
+
* @returns The value.
|
|
177
|
+
*/
|
|
178
|
+
function prunedValue(node, scope) {
|
|
179
|
+
if (node.type !== 'ConditionalExpression')
|
|
180
|
+
return node;
|
|
181
|
+
const test = folded(node.test, scope);
|
|
182
|
+
if (test) {
|
|
183
|
+
return prunedValue(test.value ? node.consequent : node.alternate, scope);
|
|
184
|
+
}
|
|
185
|
+
return {
|
|
186
|
+
...node,
|
|
187
|
+
consequent: prunedValue(node.consequent, scope),
|
|
188
|
+
alternate: prunedValue(node.alternate, scope),
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Statements with each branch whose test folds made the one it takes, in loops too. A branch taken that always ends
|
|
193
|
+
* drops the statements after it, which never run. Tests on the action's constant fields fold, so a `switch
|
|
194
|
+
* (action.type)` keeps only the case a dispatch takes. A name a block or a loop declares hides what it names outside,
|
|
195
|
+
* so no test folds through it.
|
|
196
|
+
*
|
|
197
|
+
* @param statements The statements.
|
|
198
|
+
* @param scope The constants a fold may read.
|
|
199
|
+
*
|
|
200
|
+
* @returns The statements.
|
|
201
|
+
*/
|
|
202
|
+
export function prunedStatements(statements, scope) {
|
|
203
|
+
const pruned = [];
|
|
204
|
+
scope = withoutNames(scope, declaredIn(statements));
|
|
205
|
+
for (const original of statements) {
|
|
206
|
+
const statement = original.type === 'SwitchStatement' ? switchAsIf(original) : original;
|
|
207
|
+
switch (statement.type) {
|
|
208
|
+
case 'IfStatement': {
|
|
209
|
+
const test = folded(statement.test, scope);
|
|
210
|
+
if (!test) {
|
|
211
|
+
pruned.push({
|
|
212
|
+
...statement,
|
|
213
|
+
consequent: {
|
|
214
|
+
type: 'BlockStatement',
|
|
215
|
+
body: prunedStatements(statementsOf(statement.consequent), scope),
|
|
216
|
+
directives: [],
|
|
217
|
+
},
|
|
218
|
+
alternate: statement.alternate
|
|
219
|
+
? {
|
|
220
|
+
type: 'BlockStatement',
|
|
221
|
+
body: prunedStatements(statementsOf(statement.alternate), scope),
|
|
222
|
+
directives: [],
|
|
223
|
+
}
|
|
224
|
+
: null,
|
|
225
|
+
});
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
// The branch taken runs in place. One that always ends is the last thing that runs.
|
|
229
|
+
const branch = test.value ? statement.consequent : statement.alternate;
|
|
230
|
+
const taken = branch
|
|
231
|
+
? prunedStatements(statementsOf(branch), scope)
|
|
232
|
+
: [];
|
|
233
|
+
if (exits(taken)) {
|
|
234
|
+
pruned.push(...taken);
|
|
235
|
+
return pruned;
|
|
236
|
+
}
|
|
237
|
+
if (taken.length) {
|
|
238
|
+
pruned.push({ type: 'BlockStatement', body: taken, directives: [] });
|
|
239
|
+
}
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
case 'BlockStatement': {
|
|
243
|
+
const body = prunedStatements(statement.body, scope);
|
|
244
|
+
pruned.push({ ...statement, body });
|
|
245
|
+
if (exits(body))
|
|
246
|
+
return pruned;
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
case 'ForStatement':
|
|
250
|
+
case 'ForOfStatement': {
|
|
251
|
+
const head = statement.type === 'ForStatement' ? statement.init : statement.left;
|
|
252
|
+
pruned.push({
|
|
253
|
+
...statement,
|
|
254
|
+
body: {
|
|
255
|
+
type: 'BlockStatement',
|
|
256
|
+
body: prunedStatements(statementsOf(statement.body), withoutNames(scope, declaredIn([head]))),
|
|
257
|
+
directives: [],
|
|
258
|
+
},
|
|
259
|
+
});
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
case 'ReturnStatement':
|
|
263
|
+
pruned.push(statement.argument
|
|
264
|
+
? { ...statement, argument: prunedValue(statement.argument, scope) }
|
|
265
|
+
: statement);
|
|
266
|
+
return pruned;
|
|
267
|
+
default:
|
|
268
|
+
pruned.push(statement);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return pruned;
|
|
272
|
+
}
|
|
@@ -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;
|
|
@@ -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': {
|
|
@@ -347,8 +584,56 @@ function evaluate(node, scope, fold) {
|
|
|
347
584
|
? bootOf(node, result, fold)
|
|
348
585
|
: result;
|
|
349
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;
|
|
626
|
+
}
|
|
350
627
|
throw new Error(`AOT: cannot statically evaluate call expression`);
|
|
351
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
|
+
}
|
|
352
637
|
}
|
|
353
638
|
throw new Error(`AOT: unsupported expression "${node.type}" in static context`);
|
|
354
639
|
}
|
|
@@ -397,7 +682,8 @@ function binary(operator, left, right) {
|
|
|
397
682
|
* gesture params of the handler being compiled. lowerExpr resolves those before environment.constants, so a fold
|
|
398
683
|
* that consulted the raw scope would silently swap a module const in for the value the code actually
|
|
399
684
|
* uses. Every env-driven constant fold goes through this; the JSX-side folds keep the same rule by
|
|
400
|
-
* 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.
|
|
401
687
|
*
|
|
402
688
|
* @param environment The expression environment.
|
|
403
689
|
* @param scope The constants a fold would otherwise see.
|
|
@@ -425,9 +711,14 @@ export function foldScope(environment, scope) {
|
|
|
425
711
|
break;
|
|
426
712
|
}
|
|
427
713
|
}
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
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])));
|
|
431
722
|
}
|
|
432
723
|
/**
|
|
433
724
|
* Whether `scope` binds `name`, through the scopes it reads through to but not Object.prototype.
|
|
@@ -444,6 +735,23 @@ export function hasBinding(scope, name) {
|
|
|
444
735
|
}
|
|
445
736
|
return false;
|
|
446
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
|
+
}
|
|
447
755
|
/**
|
|
448
756
|
* Binds `name` in `scope` as its own property, so `__proto__` is a name rather than Object.prototype's setter.
|
|
449
757
|
*
|