@geastack/geatsc-plugin-gea 0.1.6
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/LICENSE +202 -0
- package/README.md +37 -0
- package/dist/analyze.d.ts +4 -0
- package/dist/analyze.js +195 -0
- package/dist/cpp-canvas-colors.d.ts +4 -0
- package/dist/cpp-canvas-colors.js +226 -0
- package/dist/cpp-field-access.d.ts +49 -0
- package/dist/cpp-field-access.js +144 -0
- package/dist/cpp-ir-dom-style.d.ts +1 -0
- package/dist/cpp-ir-dom-style.js +49 -0
- package/dist/cpp-ir.d.ts +30 -0
- package/dist/cpp-ir.js +2426 -0
- package/dist/cpp-keyed-component-diagnostics.d.ts +8 -0
- package/dist/cpp-keyed-component-diagnostics.js +74 -0
- package/dist/cpp-keyed-list-diagnostics.d.ts +17 -0
- package/dist/cpp-keyed-list-diagnostics.js +84 -0
- package/dist/cpp-mounted-lowering.d.ts +81 -0
- package/dist/cpp-mounted-lowering.js +2235 -0
- package/dist/cpp-mounted.d.ts +25 -0
- package/dist/cpp-mounted.js +1376 -0
- package/dist/cpp-reactive-component-diagnostics.d.ts +2 -0
- package/dist/cpp-reactive-component-diagnostics.js +76 -0
- package/dist/cpp-reactive-component.d.ts +14 -0
- package/dist/cpp-reactive-component.js +349 -0
- package/dist/cpp-replacements.d.ts +14 -0
- package/dist/cpp-replacements.js +787 -0
- package/dist/cpp-source-replacements.d.ts +11 -0
- package/dist/cpp-source-replacements.js +257 -0
- package/dist/cpp-store-hub.d.ts +7 -0
- package/dist/cpp-store-hub.js +160 -0
- package/dist/cpp-store-method-body-parser.d.ts +2 -0
- package/dist/cpp-store-method-body-parser.js +392 -0
- package/dist/cpp-store-method-context.d.ts +48 -0
- package/dist/cpp-store-method-context.js +114 -0
- package/dist/cpp-store-method-expressions.d.ts +12 -0
- package/dist/cpp-store-method-expressions.js +1263 -0
- package/dist/cpp-store-method-replacements.d.ts +5 -0
- package/dist/cpp-store-method-replacements.js +1521 -0
- package/dist/cpp-store-method-targets.d.ts +19 -0
- package/dist/cpp-store-method-targets.js +296 -0
- package/dist/cpp-store-typed-arrays.d.ts +2 -0
- package/dist/cpp-store-typed-arrays.js +655 -0
- package/dist/cpp-stores.d.ts +73 -0
- package/dist/cpp-stores.js +1739 -0
- package/dist/cpp-template-renderer.d.ts +23 -0
- package/dist/cpp-template-renderer.js +3530 -0
- package/dist/css-style-declaration-shims.d.ts +3 -0
- package/dist/css-style-declaration-shims.js +47 -0
- package/dist/host-declarations.d.ts +11 -0
- package/dist/host-declarations.js +320 -0
- package/dist/host-shims.d.ts +3 -0
- package/dist/host-shims.js +3217 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +1239 -0
- package/dist/load-ir.d.ts +7 -0
- package/dist/load-ir.js +40 -0
- package/dist/types.d.ts +604 -0
- package/dist/types.js +1 -0
- package/dist/utils.d.ts +26 -0
- package/dist/utils.js +723 -0
- package/dist/validate.d.ts +3 -0
- package/dist/validate.js +226 -0
- package/package.json +57 -0
|
@@ -0,0 +1,3530 @@
|
|
|
1
|
+
import { attributeLines, attributeSlotLines, childForMount, constantExpression, eventSetupLines, fieldNameForSlot, imageSrcAssetIdExpression, lowerRowSlots, isRootSetupSlot, mountSlotStyleSlot, mountSlotTag, objectArrayConstant, rootSetupLines, slotAttrName, storeFieldLocalName, } from './cpp-mounted-lowering.js';
|
|
2
|
+
import { cppString, isPxLengthStylePropertyName, isRuntimeNumericConstantIdentifier, nativeStyleColorExpression, opaqueCssHexColor, sanitizeCppIdentifier, styleDeclarationEnumForPropertyName, styleKeywordPropertiesForPropertyName, styleKeywordPropertyValuesForPropertyName, styleOpaqueColorTargetForPropertyName, stylePercentPropertyEnumForPropertyName, styleRawNumberPropertyEnumsForPropertyName, } from './utils.js';
|
|
3
|
+
import { BoxedStoreFieldAccess } from './cpp-field-access.js';
|
|
4
|
+
import { arrayRevFieldName, reactiveArrayFieldInfo } from './cpp-reactive-component.js';
|
|
5
|
+
import { storeHubEmissionEnabled } from './cpp-store-hub.js';
|
|
6
|
+
import { geaHostShims } from './host-shims.js';
|
|
7
|
+
// Receivers that are host facades / JS globals, NOT app store singletons. A
|
|
8
|
+
// bare `receiver.field` read on one of these must NOT be mistaken for a global
|
|
9
|
+
// store field (see `dynamicGlobalStoreFieldRef`): `display.width`,
|
|
10
|
+
// `Math.PI`, `window.x`, etc. route through their own host/intrinsic paths.
|
|
11
|
+
const NON_STORE_RECEIVERS = new Set([
|
|
12
|
+
...Object.keys(geaHostShims.hostNamespaces ?? {}),
|
|
13
|
+
...Object.keys(geaHostShims.hostGlobalObjects ?? {}),
|
|
14
|
+
'Math',
|
|
15
|
+
'JSON',
|
|
16
|
+
'Date',
|
|
17
|
+
'Object',
|
|
18
|
+
'Array',
|
|
19
|
+
'Number',
|
|
20
|
+
'String',
|
|
21
|
+
'Boolean',
|
|
22
|
+
'console',
|
|
23
|
+
'document',
|
|
24
|
+
'window',
|
|
25
|
+
'props',
|
|
26
|
+
'this',
|
|
27
|
+
]);
|
|
28
|
+
// Default code-emit strategy: today's boxed `gea_cpp_value` store reads. The
|
|
29
|
+
// renderer threads a `FieldAccess` (this or a TypedSelf) so a self-store
|
|
30
|
+
// ReactiveComponent emits typed `self->field` reads through the SAME renderer.
|
|
31
|
+
// Existing (non-reactive) callers pass nothing → boxed → byte-identical output.
|
|
32
|
+
const BOXED_FIELD_ACCESS = new BoxedStoreFieldAccess();
|
|
33
|
+
function activeDisposerVar(args) {
|
|
34
|
+
return args.disposerVar ?? 'disposer';
|
|
35
|
+
}
|
|
36
|
+
export function templateMountedRenderer(component, components, storeFields, constants, canMount, storeMethods = [], fieldAccess = BOXED_FIELD_ACCESS, storeArrayFields = []) {
|
|
37
|
+
const root = parseTemplateRoot(component.template.html);
|
|
38
|
+
const transparentImage = root ? transparentDisplayContentsImageMount(component, root) : null;
|
|
39
|
+
if (transparentImage) {
|
|
40
|
+
const lines = [
|
|
41
|
+
`inline gea::embedded::ui::NodeHandle mount_${sanitizeCppIdentifier(component.exportName)}(${fieldAccess.paramDecl}, const std::shared_ptr<NativeDisposer> &disposer) {`,
|
|
42
|
+
' auto root = gea::embedded::ui::Document::instance().createImage();',
|
|
43
|
+
...attributeLines('root', { tag: 'img' }),
|
|
44
|
+
];
|
|
45
|
+
if (!emitIntrinsicImageMount(lines, 'root', transparentImage, storeFields, constants, new Map(), 'disposer'))
|
|
46
|
+
return null;
|
|
47
|
+
lines.push(' return root;');
|
|
48
|
+
lines.push('}');
|
|
49
|
+
lines.push('');
|
|
50
|
+
return lines;
|
|
51
|
+
}
|
|
52
|
+
// Drill through any transparent wrapper chain. If the wrapped element has no
|
|
53
|
+
// props we can keep the cheap delegation; otherwise we emit the wrapped
|
|
54
|
+
// element's template inline with the composed bindings so `<View class="x">…
|
|
55
|
+
// </View>` compiles to the same code as `<div class="x">…</div>`.
|
|
56
|
+
const resolved = root ? resolveTransparentWrappers(component, components, new Map(), canMount) : null;
|
|
57
|
+
if (resolved && resolved.component !== component) {
|
|
58
|
+
if (resolved.propBindings.size === 0) {
|
|
59
|
+
const delegateStoreArg = fieldAccess.childMountStoreArg(resolved.component);
|
|
60
|
+
if (delegateStoreArg === null)
|
|
61
|
+
return null;
|
|
62
|
+
return [
|
|
63
|
+
`inline gea::embedded::ui::NodeHandle mount_${sanitizeCppIdentifier(component.exportName)}(${fieldAccess.paramDecl}, const std::shared_ptr<NativeDisposer> &disposer) {`,
|
|
64
|
+
` return mount_${sanitizeCppIdentifier(resolved.component.exportName)}(${delegateStoreArg}, disposer);`,
|
|
65
|
+
'}',
|
|
66
|
+
'',
|
|
67
|
+
];
|
|
68
|
+
}
|
|
69
|
+
const resolvedRoot = parseTemplateRoot(resolved.component.template.html);
|
|
70
|
+
const createResolvedRoot = resolvedRoot ? uiCreateNodeExpression(resolvedRoot) : null;
|
|
71
|
+
if (!resolvedRoot || !createResolvedRoot)
|
|
72
|
+
return null;
|
|
73
|
+
const lines = [
|
|
74
|
+
`inline gea::embedded::ui::NodeHandle mount_${sanitizeCppIdentifier(component.exportName)}(${fieldAccess.paramDecl}, const std::shared_ptr<NativeDisposer> &disposer) {`,
|
|
75
|
+
` auto root = ${createResolvedRoot};`,
|
|
76
|
+
];
|
|
77
|
+
let nextNode = 0;
|
|
78
|
+
const nextVar = () => `__gea_node_${nextNode++}`;
|
|
79
|
+
emitInputButtonValueText(lines, 'root', resolvedRoot, nextVar);
|
|
80
|
+
if (!emitTemplateElement({
|
|
81
|
+
component: resolved.component,
|
|
82
|
+
node: resolvedRoot,
|
|
83
|
+
varName: 'root',
|
|
84
|
+
components,
|
|
85
|
+
storeFields,
|
|
86
|
+
lines,
|
|
87
|
+
nextVar,
|
|
88
|
+
canMount,
|
|
89
|
+
constants,
|
|
90
|
+
propBindings: resolved.propBindings,
|
|
91
|
+
storeMethods,
|
|
92
|
+
fieldAccess,
|
|
93
|
+
storeArrayFields,
|
|
94
|
+
})) {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
lines.push(' return root;');
|
|
98
|
+
lines.push('}');
|
|
99
|
+
lines.push('');
|
|
100
|
+
return lines;
|
|
101
|
+
}
|
|
102
|
+
const createRoot = root ? uiCreateNodeExpression(root) : null;
|
|
103
|
+
if (!root || !createRoot)
|
|
104
|
+
return null;
|
|
105
|
+
const lines = [
|
|
106
|
+
`inline gea::embedded::ui::NodeHandle mount_${sanitizeCppIdentifier(component.exportName)}(${fieldAccess.paramDecl}, const std::shared_ptr<NativeDisposer> &disposer) {`,
|
|
107
|
+
` auto root = ${createRoot};`,
|
|
108
|
+
];
|
|
109
|
+
let nextNode = 0;
|
|
110
|
+
const nextVar = () => `__gea_node_${nextNode++}`;
|
|
111
|
+
if (!emitTemplateElement({
|
|
112
|
+
component,
|
|
113
|
+
node: root,
|
|
114
|
+
varName: 'root',
|
|
115
|
+
components,
|
|
116
|
+
storeFields,
|
|
117
|
+
lines,
|
|
118
|
+
nextVar,
|
|
119
|
+
canMount,
|
|
120
|
+
constants,
|
|
121
|
+
propBindings: new Map(),
|
|
122
|
+
storeMethods,
|
|
123
|
+
fieldAccess,
|
|
124
|
+
storeArrayFields,
|
|
125
|
+
})) {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
lines.push(' return root;');
|
|
129
|
+
lines.push('}');
|
|
130
|
+
lines.push('');
|
|
131
|
+
return lines;
|
|
132
|
+
}
|
|
133
|
+
// When a function component's JSX root is *another* component (e.g. `function
|
|
134
|
+
// SettingsRow() { return <View class="settings-row">...</View> }`), the IR
|
|
135
|
+
// emits `<span style="display:contents"><!--0--></span>` and a single mount
|
|
136
|
+
// slot for the wrapped component. Without resolving that, the compiler creates
|
|
137
|
+
// a real <span> at runtime, the wrapper props (class, style, onClick, …) and
|
|
138
|
+
// JSX children never bind on the wrapped element, and `<View class="x">y</View>`
|
|
139
|
+
// stops being equivalent to `<div class="x">y</div>`.
|
|
140
|
+
//
|
|
141
|
+
// This helper walks down such transparent layers, composing each mount slot's
|
|
142
|
+
// prop bindings via `mountPropBindings(slot, currentProps)` — that builds a
|
|
143
|
+
// `PropBinding` whose `parentProps` chain back to the original use site, so
|
|
144
|
+
// references like `<Text>{label}</Text>` inside `SettingsRow` still resolve
|
|
145
|
+
// `label` to whatever the caller passed (`<SettingsRow label="Bluetooth name"
|
|
146
|
+
// …/>`). The returned component is the first non-wrapper element, and the
|
|
147
|
+
// returned bindings are the merged props the emitter should apply to it.
|
|
148
|
+
function resolveTransparentWrappers(component, components, propBindings, canMount) {
|
|
149
|
+
let current = component;
|
|
150
|
+
let currentProps = propBindings;
|
|
151
|
+
// Bounded to keep degenerate IR (cycles) from spinning forever; real apps
|
|
152
|
+
// never nest wrapper components anywhere close to this depth.
|
|
153
|
+
for (let depth = 0; depth < 16; depth += 1) {
|
|
154
|
+
const root = parseTemplateRoot(current.template.html);
|
|
155
|
+
if (!root || !isTransparentDisplayContentsRoot(root))
|
|
156
|
+
break;
|
|
157
|
+
if (root.children.length !== 1 || current.template.slots.length !== 1)
|
|
158
|
+
break;
|
|
159
|
+
const child = root.children[0];
|
|
160
|
+
if (child.kind !== 'slot')
|
|
161
|
+
break;
|
|
162
|
+
const slot = current.template.slots[0];
|
|
163
|
+
if (slot.index !== child.index || slot.kind !== 'mount')
|
|
164
|
+
break;
|
|
165
|
+
// <Image> uses a dedicated intrinsic path; leave it alone.
|
|
166
|
+
if (mountSlotTag(slot) === 'Image')
|
|
167
|
+
break;
|
|
168
|
+
const mountedChild = childForMount(slot, components);
|
|
169
|
+
if (!mountedChild || mountedChild === current || !canMount(mountedChild))
|
|
170
|
+
break;
|
|
171
|
+
// Never peel INTO a ReactiveComponent: its template renders against its
|
|
172
|
+
// own typed instance, not inline against the wrapper caller's bindings.
|
|
173
|
+
if (mountedChild.reactiveState)
|
|
174
|
+
break;
|
|
175
|
+
currentProps = mountPropBindings(slot, currentProps);
|
|
176
|
+
current = mountedChild;
|
|
177
|
+
}
|
|
178
|
+
return { component: current, propBindings: currentProps };
|
|
179
|
+
}
|
|
180
|
+
function transparentDisplayContentsImageMount(component, root) {
|
|
181
|
+
if (!isTransparentDisplayContentsRoot(root))
|
|
182
|
+
return null;
|
|
183
|
+
if (root.children.length !== 1 || component.template.slots.length !== 1)
|
|
184
|
+
return null;
|
|
185
|
+
const child = root.children[0];
|
|
186
|
+
if (child.kind !== 'slot')
|
|
187
|
+
return null;
|
|
188
|
+
const slot = component.template.slots[0];
|
|
189
|
+
if (slot.index !== child.index || slot.kind !== 'mount' || mountSlotTag(slot) !== 'Image')
|
|
190
|
+
return null;
|
|
191
|
+
return slot;
|
|
192
|
+
}
|
|
193
|
+
function isTransparentDisplayContentsRoot(root) {
|
|
194
|
+
const attrs = root.attrs ?? {};
|
|
195
|
+
const keys = Object.keys(attrs);
|
|
196
|
+
return keys.length === 1 && keys[0] === 'style' && /\bdisplay\s*:\s*contents\b/i.test(attrs.style);
|
|
197
|
+
}
|
|
198
|
+
export function canTemplateMountComponent(component, components, storeFields, constants, canMount, storeMethods = [], storeArrayFields = []) {
|
|
199
|
+
const root = parseTemplateRoot(component.template.html);
|
|
200
|
+
if (!root || (!uiCreateNodeExpression(root) && !transparentDisplayContentsImageMount(component, root)))
|
|
201
|
+
return false;
|
|
202
|
+
return canEmitTemplateElement(component, components, storeFields, constants, canMount, new Map(), storeMethods, storeArrayFields);
|
|
203
|
+
}
|
|
204
|
+
function emitTemplateElement(args) {
|
|
205
|
+
const { component, node, varName, storeFields, lines } = args;
|
|
206
|
+
// Mirror the filter in `canEmitTemplateElement`: drop root setup slots whose
|
|
207
|
+
// expressions reference props the parent never passed. Without this, calling
|
|
208
|
+
// `rootSetupLines` would fail on unresolvable `props.X` expressions even
|
|
209
|
+
// though the can-emit pass had already filtered them out.
|
|
210
|
+
const setupSlots = resolveRootSetupPropSlots(component.template.slots
|
|
211
|
+
.filter((slot) => isRootSetupSlot(slot, args.constants))
|
|
212
|
+
.filter((slot) => !slotReferencesUnboundProp(slot, args.propBindings)), args.propBindings, args.constants);
|
|
213
|
+
const setup = rootSetupLines(varName, setupSlots, storeFields, args.storeMethods, args.fieldAccess, node.tag, args.constants, activeDisposerVar(args));
|
|
214
|
+
if (!setup)
|
|
215
|
+
return false;
|
|
216
|
+
const slotsByIndex = new Map(component.template.slots.map((slot) => [slot.index, slot]));
|
|
217
|
+
const pathVars = new Map();
|
|
218
|
+
const keyedListAnchors = new Map();
|
|
219
|
+
pathVars.set('', varName);
|
|
220
|
+
lines.push(...attributeLines(varName, { tag: node.tag, className: node.className, attrs: node.attrs }, component.template.slots));
|
|
221
|
+
lines.push(...intrinsicStyleLines(varName, node));
|
|
222
|
+
lines.push(...setup);
|
|
223
|
+
emitInputButtonValueText(lines, varName, node, args.nextVar);
|
|
224
|
+
if (!emitTemplateChildren({ ...args, slotsByIndex, pathVars, keyedListAnchors, path: [], disposerVar: activeDisposerVar(args) })) {
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
return emitReactiveSlots({
|
|
228
|
+
component,
|
|
229
|
+
components: args.components,
|
|
230
|
+
storeFields,
|
|
231
|
+
storeArrayFields: args.storeArrayFields,
|
|
232
|
+
pathVars,
|
|
233
|
+
lines,
|
|
234
|
+
constants: args.constants,
|
|
235
|
+
propBindings: args.propBindings,
|
|
236
|
+
storeMethods: args.storeMethods,
|
|
237
|
+
fieldAccess: args.fieldAccess,
|
|
238
|
+
keyedListAnchors,
|
|
239
|
+
disposerVar: activeDisposerVar(args),
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
function canEmitTemplateElement(component, components, storeFields, constants, canMount, propBindings, storeMethods, storeArrayFields) {
|
|
243
|
+
// When this component is inlined by a parent that doesn't pass every prop
|
|
244
|
+
// (typical for the gea-embedded `<View>`/`<Text>` wrappers: parents pass
|
|
245
|
+
// `class` but not `style`, `onClick`, etc.), a slot whose expression is
|
|
246
|
+
// just `props.X` for an unbound X is effectively undefined at runtime —
|
|
247
|
+
// there's nothing to bind, nothing to emit. The previous implementation
|
|
248
|
+
// failed the whole template here because `lowerTemplateExpression` can't
|
|
249
|
+
// produce a value for `props.style` when `style` isn't in `propBindings`.
|
|
250
|
+
// Filter those slots out instead, both for the root setup pass below and
|
|
251
|
+
// for the per-slot loop. `emitTemplateElement` / `emitReactiveSlots` skip
|
|
252
|
+
// the same set so emit-time stays in sync with the can-emit decision.
|
|
253
|
+
const setupSlots = resolveRootSetupPropSlots(component.template.slots
|
|
254
|
+
.filter((slot) => isRootSetupSlot(slot, constants))
|
|
255
|
+
.filter((slot) => !slotReferencesUnboundProp(slot, propBindings)), propBindings, constants);
|
|
256
|
+
if (!rootSetupLines('root', setupSlots, storeFields, storeMethods, undefined, undefined, constants))
|
|
257
|
+
return false;
|
|
258
|
+
for (const slot of component.template.slots) {
|
|
259
|
+
if (isRootSetupSlot(slot, constants))
|
|
260
|
+
continue;
|
|
261
|
+
if (slotReferencesUnboundProp(slot, propBindings))
|
|
262
|
+
continue;
|
|
263
|
+
if (slot.kind === 'mount') {
|
|
264
|
+
if (mountSlotTag(slot) === 'Image') {
|
|
265
|
+
if (!canEmitIntrinsicImageMount(slot, storeFields, constants, propBindings))
|
|
266
|
+
return false;
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
const child = childForMount(slot, components);
|
|
270
|
+
const childProps = mountPropBindings(slot, propBindings);
|
|
271
|
+
if (!child || (child === component && childProps.size === 0))
|
|
272
|
+
return false;
|
|
273
|
+
if (childProps.size > 0) {
|
|
274
|
+
// Props mean inlining, which a reactive child can't do (its fields
|
|
275
|
+
// live on its own typed instance) — mirror of the emission guard.
|
|
276
|
+
if (child.reactiveState)
|
|
277
|
+
return false;
|
|
278
|
+
const childRoot = parseTemplateRoot(child.template.html);
|
|
279
|
+
if (!childRoot || !uiCreateNodeExpression(childRoot))
|
|
280
|
+
return false;
|
|
281
|
+
if (!canEmitTemplateElement(child, components, storeFields, constants, canMount, childProps, storeMethods, storeArrayFields))
|
|
282
|
+
return false;
|
|
283
|
+
}
|
|
284
|
+
else if (!canMount(child) && (child.reactiveState || !canGenericChildMount(child, childProps))) {
|
|
285
|
+
return false;
|
|
286
|
+
}
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
if (slot.kind === 'keyed-list') {
|
|
290
|
+
if (!canEmitStaticKeyedList({ component, slot, components, storeFields, storeArrayFields, constants, canMount, propBindings, storeMethods }) &&
|
|
291
|
+
!canEmitDynamicKeyedListSlot({
|
|
292
|
+
component,
|
|
293
|
+
slot,
|
|
294
|
+
components,
|
|
295
|
+
storeFields,
|
|
296
|
+
storeArrayFields,
|
|
297
|
+
constants,
|
|
298
|
+
propBindings,
|
|
299
|
+
storeMethods,
|
|
300
|
+
}))
|
|
301
|
+
return false;
|
|
302
|
+
continue;
|
|
303
|
+
}
|
|
304
|
+
if (slot.kind === 'text') {
|
|
305
|
+
const childrenBinding = childrenBindingForPropSlot(slot, propBindings);
|
|
306
|
+
if (childrenBinding?.childrenTemplate) {
|
|
307
|
+
if (!canEmitTemplateFragment(component, childrenBinding.childrenTemplate, components, storeFields, constants, canMount, childrenBinding.parentProps, storeMethods, storeArrayFields))
|
|
308
|
+
return false;
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
const lowered = lowerTemplateExpression(slot.expr, storeFields, constants, propBindings);
|
|
312
|
+
if (!lowered || lowered.fields.some((field) => !field.readerName))
|
|
313
|
+
return false;
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
316
|
+
if (slot.kind === 'class') {
|
|
317
|
+
if (!canLowerTemplateClassSlot(slot, storeFields, constants, propBindings))
|
|
318
|
+
return false;
|
|
319
|
+
continue;
|
|
320
|
+
}
|
|
321
|
+
if (slot.kind === 'style') {
|
|
322
|
+
if (!canLowerTemplateStyleSlot(slot, storeFields, constants, propBindings))
|
|
323
|
+
return false;
|
|
324
|
+
continue;
|
|
325
|
+
}
|
|
326
|
+
if (slot.kind === 'conditional') {
|
|
327
|
+
if (!canEmitConditionalSlot(component, slot, components, storeFields, constants, canMount, propBindings, storeMethods, storeArrayFields))
|
|
328
|
+
return false;
|
|
329
|
+
continue;
|
|
330
|
+
}
|
|
331
|
+
if (slot.kind === 'attr') {
|
|
332
|
+
if (!canEmitTemplateAttrSlot(slot, storeFields, constants, propBindings))
|
|
333
|
+
return false;
|
|
334
|
+
continue;
|
|
335
|
+
}
|
|
336
|
+
if (slot.kind === 'event') {
|
|
337
|
+
if (!eventSetupLines('root', slot, storeFields, storeMethods, undefined, undefined, constants))
|
|
338
|
+
return false;
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
// `kind === 'value'` / `kind === 'bool'` are the IR's reactive-attribute
|
|
342
|
+
// bindings used when a JSX attribute is wired to a store expression:
|
|
343
|
+
// `value={todo.draft}` on `<input type="range">` (value) or
|
|
344
|
+
// `checked={store.on}` on `<input type="checkbox">` (bool). Both carry the
|
|
345
|
+
// same `expr` / `payload.attrName` shape a `text` slot does — lower the
|
|
346
|
+
// expression through the standard store-field reader pipeline and emit a
|
|
347
|
+
// reactiveApply that writes back via `setAttribute` (see
|
|
348
|
+
// `emitReactiveSlots`). The macOS renderer's applyInputProps /
|
|
349
|
+
// applySwitchProps read that attribute back on every tree sync.
|
|
350
|
+
if (slot.kind === 'value' || slot.kind === 'bool') {
|
|
351
|
+
if (!slotAttrName(slot))
|
|
352
|
+
return false;
|
|
353
|
+
const lowered = lowerTemplateExpression(slot.expr, storeFields, constants, propBindings);
|
|
354
|
+
if (!lowered || lowered.fields.some((field) => !field.readerName))
|
|
355
|
+
return false;
|
|
356
|
+
continue;
|
|
357
|
+
}
|
|
358
|
+
return false;
|
|
359
|
+
}
|
|
360
|
+
return true;
|
|
361
|
+
}
|
|
362
|
+
function emitTemplateChildren(args) {
|
|
363
|
+
const { node, varName, path, components, slotsByIndex, pathVars, lines, nextVar, canMount, propBindings } = args;
|
|
364
|
+
const disposerVar = activeDisposerVar(args);
|
|
365
|
+
for (let index = 0; index < node.children.length; index += 1) {
|
|
366
|
+
const child = node.children[index];
|
|
367
|
+
const childPath = [...path, index];
|
|
368
|
+
if (child.kind === 'element') {
|
|
369
|
+
const childVar = nextVar();
|
|
370
|
+
const createChild = uiCreateNodeExpression(child);
|
|
371
|
+
if (!createChild)
|
|
372
|
+
return false;
|
|
373
|
+
lines.push(` auto ${childVar} = ${createChild};`);
|
|
374
|
+
pathVars.set(pathKey(childPath), childVar);
|
|
375
|
+
lines.push(...attributeLines(childVar, { tag: child.tag, className: child.className, attrs: child.attrs }));
|
|
376
|
+
lines.push(...intrinsicStyleLines(childVar, child));
|
|
377
|
+
lines.push(` ${varName}.appendChild(${childVar});`);
|
|
378
|
+
emitInputButtonValueText(lines, childVar, child, nextVar);
|
|
379
|
+
if (!emitTemplateChildren({ ...args, node: child, varName: childVar, path: childPath }))
|
|
380
|
+
return false;
|
|
381
|
+
continue;
|
|
382
|
+
}
|
|
383
|
+
if (child.kind === 'text') {
|
|
384
|
+
const text = renderableTemplateText(child.text);
|
|
385
|
+
if (!text)
|
|
386
|
+
continue;
|
|
387
|
+
// The IR uses a literal `0` character as the text anchor for `directText`
|
|
388
|
+
// slots (see walk.ts: `html += '0' // placeholder, overwritten by ...`).
|
|
389
|
+
// It is meant to be replaced by the reactive text emit. If we treat it
|
|
390
|
+
// as literal content the user sees a bare "0" on screen wherever a
|
|
391
|
+
// reactive text slot lives — and *especially* wherever such a slot
|
|
392
|
+
// ends up unbound (e.g. `<View>{props.children}</View>` inlined into a
|
|
393
|
+
// parent that didn't pass children).
|
|
394
|
+
const placeholderSlot = text === '0'
|
|
395
|
+
? Array.from(slotsByIndex.values()).find((s) => s.directText === true && s.kind === 'text' && pathKey(s.walk) === pathKey(childPath)) ?? null
|
|
396
|
+
: null;
|
|
397
|
+
const childrenBinding = placeholderSlot ? childrenBindingForPropSlot(placeholderSlot, propBindings) : null;
|
|
398
|
+
const childrenTemplate = childrenBinding?.childrenTemplate ?? null;
|
|
399
|
+
if (childrenTemplate && childrenBinding) {
|
|
400
|
+
const textTargetVar = canUseTextNodeForElement(node) ? varName : undefined;
|
|
401
|
+
if (!emitTemplateFragment({
|
|
402
|
+
...args,
|
|
403
|
+
template: childrenTemplate,
|
|
404
|
+
parentVar: varName,
|
|
405
|
+
textTargetVar,
|
|
406
|
+
// Emit the children in the scope where the JSX was authored — the
|
|
407
|
+
// wrapper's caller — not in the inlined element's scope.
|
|
408
|
+
propBindings: childrenBinding.parentProps,
|
|
409
|
+
})) {
|
|
410
|
+
return false;
|
|
411
|
+
}
|
|
412
|
+
continue;
|
|
413
|
+
}
|
|
414
|
+
if (args.textTargetVar && node.tag === '#fragment') {
|
|
415
|
+
if (!placeholderSlot)
|
|
416
|
+
lines.push(` ${args.textTargetVar}.setText(${cppString(text)});`);
|
|
417
|
+
pathVars.set(pathKey(childPath), args.textTargetVar);
|
|
418
|
+
continue;
|
|
419
|
+
}
|
|
420
|
+
// A directText placeholder ("0") carries a reactive expr. Seed its current
|
|
421
|
+
// value here (before append / alongside the parent) so a parent whose only
|
|
422
|
+
// content is this value measures a non-zero size at first layout; otherwise
|
|
423
|
+
// it stays 0-sized and invisible. emitReactiveSlots still binds it for updates.
|
|
424
|
+
const seedPlaceholder = (target) => {
|
|
425
|
+
if (!placeholderSlot)
|
|
426
|
+
return;
|
|
427
|
+
const ph = lowerTemplateExpression(placeholderSlot.expr, args.storeFields, args.constants, propBindings);
|
|
428
|
+
if (ph && !ph.fields.some((field) => !field.readerName)) {
|
|
429
|
+
emitSyncSeed(lines, ph, [`${target}.setText((${loweredAsString(ph)}).c_str());`]);
|
|
430
|
+
}
|
|
431
|
+
};
|
|
432
|
+
if (canUseTextNodeForElement(node)) {
|
|
433
|
+
if (!placeholderSlot)
|
|
434
|
+
lines.push(` ${varName}.setText(${cppString(text)});`);
|
|
435
|
+
else
|
|
436
|
+
seedPlaceholder(varName);
|
|
437
|
+
pathVars.set(pathKey(childPath), varName);
|
|
438
|
+
}
|
|
439
|
+
else {
|
|
440
|
+
const textVar = nextVar();
|
|
441
|
+
lines.push(` auto ${textVar} = gea::embedded::ui::Document::instance().createText();`);
|
|
442
|
+
if (!placeholderSlot)
|
|
443
|
+
lines.push(` ${textVar}.setText(${cppString(text)});`);
|
|
444
|
+
else
|
|
445
|
+
seedPlaceholder(textVar);
|
|
446
|
+
lines.push(` ${varName}.appendChild(${textVar});`);
|
|
447
|
+
pathVars.set(pathKey(childPath), textVar);
|
|
448
|
+
}
|
|
449
|
+
continue;
|
|
450
|
+
}
|
|
451
|
+
const slot = slotsByIndex.get(child.index);
|
|
452
|
+
if (!slot)
|
|
453
|
+
return false;
|
|
454
|
+
// If the slot references a prop the parent didn't pass, treat its slot
|
|
455
|
+
// position as empty. Without this we'd emit an unused text/anchor child
|
|
456
|
+
// that the matching `emitReactiveSlots` then skips, producing a dead DOM
|
|
457
|
+
// node. Common for the gea-embedded `<View>` template's `props.children`
|
|
458
|
+
// when the parent supplies no children.
|
|
459
|
+
if (slotReferencesUnboundProp(slot, propBindings))
|
|
460
|
+
continue;
|
|
461
|
+
if (slot.kind === 'text') {
|
|
462
|
+
if (args.textTargetVar && node.tag === '#fragment') {
|
|
463
|
+
pathVars.set(pathKey(childPath), args.textTargetVar);
|
|
464
|
+
continue;
|
|
465
|
+
}
|
|
466
|
+
const textVar = nextVar();
|
|
467
|
+
lines.push(` auto ${textVar} = gea::embedded::ui::Document::instance().createText();`);
|
|
468
|
+
// Seed the current value BEFORE appendChild so a parent whose only child is
|
|
469
|
+
// this reactive text measures a non-zero size at first layout (matching how
|
|
470
|
+
// static text is emitted). emitReactiveSlots still binds it for updates.
|
|
471
|
+
const seedLowered = lowerTemplateExpression(slot.expr, args.storeFields, args.constants, propBindings);
|
|
472
|
+
if (seedLowered && !seedLowered.fields.some((field) => !field.readerName)) {
|
|
473
|
+
emitSyncSeed(lines, seedLowered, [`${textVar}.setText((${loweredAsString(seedLowered)}).c_str());`]);
|
|
474
|
+
}
|
|
475
|
+
lines.push(` ${varName}.appendChild(${textVar});`);
|
|
476
|
+
pathVars.set(pathKey(childPath), textVar);
|
|
477
|
+
continue;
|
|
478
|
+
}
|
|
479
|
+
if (slot.kind === 'keyed-list') {
|
|
480
|
+
if (emitStaticKeyedList({
|
|
481
|
+
component: args.component,
|
|
482
|
+
components: args.components,
|
|
483
|
+
storeFields: args.storeFields,
|
|
484
|
+
storeMethods: args.storeMethods,
|
|
485
|
+
storeArrayFields: args.storeArrayFields,
|
|
486
|
+
constants: args.constants,
|
|
487
|
+
propBindings,
|
|
488
|
+
fieldAccess: args.fieldAccess,
|
|
489
|
+
lines,
|
|
490
|
+
parentVar: varName,
|
|
491
|
+
slot,
|
|
492
|
+
nextVar,
|
|
493
|
+
canMount: args.canMount,
|
|
494
|
+
disposerVar,
|
|
495
|
+
})) {
|
|
496
|
+
continue;
|
|
497
|
+
}
|
|
498
|
+
if (!canEmitDynamicKeyedListSlot({
|
|
499
|
+
component: args.component,
|
|
500
|
+
slot,
|
|
501
|
+
components: args.components,
|
|
502
|
+
storeFields: args.storeFields,
|
|
503
|
+
storeArrayFields: args.storeArrayFields,
|
|
504
|
+
constants: args.constants,
|
|
505
|
+
propBindings,
|
|
506
|
+
storeMethods: args.storeMethods,
|
|
507
|
+
}))
|
|
508
|
+
return false;
|
|
509
|
+
const anchorVar = nextVar();
|
|
510
|
+
lines.push(` auto ${anchorVar} = gea::embedded::ui::Document::instance().createView();`);
|
|
511
|
+
lines.push(` ${anchorVar}.setTagName("#comment");`);
|
|
512
|
+
lines.push(` ${anchorVar}.style().display(gea::embedded::ui::kDisplayNone);`);
|
|
513
|
+
lines.push(` ${varName}.appendChild(${anchorVar});`);
|
|
514
|
+
args.keyedListAnchors?.set(pathKey(childPath), { parentVar: varName, anchorVar });
|
|
515
|
+
continue;
|
|
516
|
+
}
|
|
517
|
+
if (slot.kind === 'conditional') {
|
|
518
|
+
if (!emitConditionalSlot({ ...args, slot, parentVar: varName }))
|
|
519
|
+
return false;
|
|
520
|
+
continue;
|
|
521
|
+
}
|
|
522
|
+
if (slot.kind !== 'mount')
|
|
523
|
+
return false;
|
|
524
|
+
if (mountSlotTag(slot) === 'Image') {
|
|
525
|
+
const imageVar = nextVar();
|
|
526
|
+
lines.push(` auto ${imageVar} = gea::embedded::ui::Document::instance().createImage();`);
|
|
527
|
+
lines.push(...attributeLines(imageVar, { tag: 'img' }));
|
|
528
|
+
if (!emitIntrinsicImageMount(lines, imageVar, slot, args.storeFields, args.constants, propBindings, disposerVar))
|
|
529
|
+
return false;
|
|
530
|
+
lines.push(` ${varName}.appendChild(${imageVar});`);
|
|
531
|
+
pathVars.set(pathKey(childPath), imageVar);
|
|
532
|
+
continue;
|
|
533
|
+
}
|
|
534
|
+
const mountedChild = childForMount(slot, components);
|
|
535
|
+
if (!mountedChild)
|
|
536
|
+
return false;
|
|
537
|
+
const childProps = mountPropBindings(slot, propBindings);
|
|
538
|
+
if (childProps.size === 0) {
|
|
539
|
+
const fieldAccess = args.fieldAccess ?? BOXED_FIELD_ACCESS;
|
|
540
|
+
if (canMount(mountedChild)) {
|
|
541
|
+
const childStoreArg = fieldAccess.childMountStoreArg(mountedChild);
|
|
542
|
+
if (childStoreArg === null)
|
|
543
|
+
return false;
|
|
544
|
+
lines.push(` ${varName}.appendChild(mount_${sanitizeCppIdentifier(mountedChild.exportName)}(${childStoreArg}, ${disposerVar}));`);
|
|
545
|
+
continue;
|
|
546
|
+
}
|
|
547
|
+
// The generic fallback instantiates the child's compiled C++ class. In
|
|
548
|
+
// typed self-store mode the child may exist only in the IR (its module
|
|
549
|
+
// tree-shaken from the bundle once the lean parent's template moved to
|
|
550
|
+
// the IR), so there is no class to construct — fail the renderer so the
|
|
551
|
+
// component surfaces a diagnostic instead of emitting uncompilable code.
|
|
552
|
+
// A reactive child must never go through it either: its compiled class
|
|
553
|
+
// has a neutralized __gea_to_value(), so fn_mount renders NOTHING.
|
|
554
|
+
if (mountedChild.reactiveState || fieldAccess.typed || !canGenericChildMount(mountedChild, childProps))
|
|
555
|
+
return false;
|
|
556
|
+
emitGenericChildMount(lines, varName, mountedChild, nextVar, disposerVar);
|
|
557
|
+
continue;
|
|
558
|
+
}
|
|
559
|
+
// Inlining renders the child's template against the PARENT's store access —
|
|
560
|
+
// impossible for a ReactiveComponent child, whose fields live on its own
|
|
561
|
+
// typed instance.
|
|
562
|
+
if (mountedChild.reactiveState)
|
|
563
|
+
return false;
|
|
564
|
+
// Resolve any transparent wrapper chain so the inlined child renders on
|
|
565
|
+
// the wrapped element directly. Without this, mounting a wrapper component
|
|
566
|
+
// like `<SettingsRow label=… value=…/>` would create an extra
|
|
567
|
+
// display:contents span around the actual <div>, and the wrapper's prop
|
|
568
|
+
// bindings would never reach the inner element.
|
|
569
|
+
const resolvedChild = resolveTransparentWrappers(mountedChild, components, childProps, canMount);
|
|
570
|
+
const effectiveChild = resolvedChild.component;
|
|
571
|
+
const effectiveProps = resolvedChild.propBindings;
|
|
572
|
+
const mountedRoot = parseTemplateRoot(effectiveChild.template.html);
|
|
573
|
+
const createMountedRoot = mountedRoot ? uiCreateNodeExpression(mountedRoot) : null;
|
|
574
|
+
if (!mountedRoot || !createMountedRoot)
|
|
575
|
+
return false;
|
|
576
|
+
const childVar = nextVar();
|
|
577
|
+
lines.push(` auto ${childVar} = ${createMountedRoot};`);
|
|
578
|
+
lines.push(` ${varName}.appendChild(${childVar});`);
|
|
579
|
+
emitInputButtonValueText(lines, childVar, mountedRoot, nextVar);
|
|
580
|
+
if (!emitTemplateElement({
|
|
581
|
+
component: effectiveChild,
|
|
582
|
+
node: mountedRoot,
|
|
583
|
+
varName: childVar,
|
|
584
|
+
components,
|
|
585
|
+
storeFields: args.storeFields,
|
|
586
|
+
lines,
|
|
587
|
+
nextVar,
|
|
588
|
+
canMount,
|
|
589
|
+
constants: args.constants,
|
|
590
|
+
propBindings: effectiveProps,
|
|
591
|
+
storeMethods: args.storeMethods,
|
|
592
|
+
fieldAccess: args.fieldAccess,
|
|
593
|
+
storeArrayFields: args.storeArrayFields,
|
|
594
|
+
disposerVar,
|
|
595
|
+
})) {
|
|
596
|
+
return false;
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
return true;
|
|
600
|
+
}
|
|
601
|
+
function canGenericChildMount(component, propBindings) {
|
|
602
|
+
return propBindings.size === 0 && parseTemplateRoot(component.template.html) !== null;
|
|
603
|
+
}
|
|
604
|
+
function emitGenericChildMount(lines, parentVar, component, nextVar, disposerVar) {
|
|
605
|
+
const componentVar = `${nextVar()}_component`;
|
|
606
|
+
const addVar = `${componentVar}_dispose`;
|
|
607
|
+
lines.push(` auto ${componentVar} = std::make_shared<${sanitizeCppIdentifier(component.exportName)}>();`);
|
|
608
|
+
lines.push(` ${componentVar}->render(gea_ir::hostDocumentNodeValue(${parentVar}));`);
|
|
609
|
+
lines.push(` auto ${addVar} = ${disposerVar};`);
|
|
610
|
+
lines.push(` if (${addVar}) {`);
|
|
611
|
+
lines.push(` ${addVar}->add([${componentVar}]() mutable -> void { if (${componentVar}) ${componentVar}->dispose(); });`);
|
|
612
|
+
lines.push(' }');
|
|
613
|
+
}
|
|
614
|
+
function emitGenericChildAsNode(lines, varName, component, nextVar, disposerVar) {
|
|
615
|
+
const componentVar = `${nextVar()}_component`;
|
|
616
|
+
const addVar = `${componentVar}_dispose`;
|
|
617
|
+
lines.push(` auto ${varName} = gea::embedded::ui::Document::instance().createView();`);
|
|
618
|
+
lines.push(` ${varName}.setTagName("span");`);
|
|
619
|
+
lines.push(` auto ${componentVar} = std::make_shared<${sanitizeCppIdentifier(component.exportName)}>();`);
|
|
620
|
+
lines.push(` ${componentVar}->render(gea_ir::hostDocumentNodeValue(${varName}));`);
|
|
621
|
+
lines.push(` auto ${addVar} = ${disposerVar};`);
|
|
622
|
+
lines.push(` if (${addVar}) {`);
|
|
623
|
+
lines.push(` ${addVar}->add([${componentVar}]() mutable -> void { if (${componentVar}) ${componentVar}->dispose(); });`);
|
|
624
|
+
lines.push(' }');
|
|
625
|
+
}
|
|
626
|
+
function emitReactiveSlots(args) {
|
|
627
|
+
const { component, storeFields, pathVars, lines, constants, propBindings } = args;
|
|
628
|
+
const fieldAccess = args.fieldAccess ?? BOXED_FIELD_ACCESS;
|
|
629
|
+
const disposerVar = activeDisposerVar(args);
|
|
630
|
+
for (const slot of args.slots ?? component.template.slots) {
|
|
631
|
+
if (slot.kind === 'keyed-list' && isStaticKeyedListSource(slot, constants))
|
|
632
|
+
continue;
|
|
633
|
+
// Anchorless keyed-list slots (the map is the element's sole content) are
|
|
634
|
+
// attached to the element itself. The typed self-store path renders them
|
|
635
|
+
// inline (rebuild-on-rev keyed rows); the boxed path keeps its existing
|
|
636
|
+
// behavior (dedicated keyed renderers handle list-rooted components).
|
|
637
|
+
if (slot.kind === 'keyed-list' && fieldAccess.typed) {
|
|
638
|
+
const target = pathVars.get(pathKey(slot.walk));
|
|
639
|
+
if (!target)
|
|
640
|
+
return false;
|
|
641
|
+
if (!emitTypedKeyedListSlot(lines, target, slot, component, constants, disposerVar))
|
|
642
|
+
return false;
|
|
643
|
+
continue;
|
|
644
|
+
}
|
|
645
|
+
if (slot.kind === 'keyed-list') {
|
|
646
|
+
const anchor = args.keyedListAnchors?.get(pathKey(slot.walk));
|
|
647
|
+
const target = anchor?.parentVar ?? pathVars.get(pathKey(slot.walk));
|
|
648
|
+
if (!target)
|
|
649
|
+
return false;
|
|
650
|
+
if (!emitDynamicKeyedListSlot({
|
|
651
|
+
component,
|
|
652
|
+
slot,
|
|
653
|
+
target,
|
|
654
|
+
insertBefore: anchor?.anchorVar,
|
|
655
|
+
components: args.components,
|
|
656
|
+
storeFields,
|
|
657
|
+
storeArrayFields: args.storeArrayFields,
|
|
658
|
+
constants,
|
|
659
|
+
propBindings,
|
|
660
|
+
storeMethods: args.storeMethods,
|
|
661
|
+
lines,
|
|
662
|
+
disposerVar,
|
|
663
|
+
}))
|
|
664
|
+
return false;
|
|
665
|
+
continue;
|
|
666
|
+
}
|
|
667
|
+
if (isRootSetupSlot(slot, constants) || slot.kind === 'mount' || slot.kind === 'keyed-list' || slot.kind === 'conditional')
|
|
668
|
+
continue;
|
|
669
|
+
if (slotReferencesUnboundProp(slot, propBindings))
|
|
670
|
+
continue;
|
|
671
|
+
if (childrenTemplateForPropSlot(slot, propBindings))
|
|
672
|
+
continue;
|
|
673
|
+
const target = pathVars.get(pathKey(slot.walk));
|
|
674
|
+
if (!target)
|
|
675
|
+
return false;
|
|
676
|
+
if (slot.kind === 'text') {
|
|
677
|
+
const lowered = lowerTemplateExpression(slot.expr, storeFields, constants, propBindings);
|
|
678
|
+
if (!lowered || lowered.fields.some((field) => !field.readerName))
|
|
679
|
+
return false;
|
|
680
|
+
emitReactiveApply(lines, target, lowered, `${target}.setText((${loweredAsString(lowered)}).c_str());`, fieldAccess, [], disposerVar);
|
|
681
|
+
continue;
|
|
682
|
+
}
|
|
683
|
+
if (slot.kind === 'class') {
|
|
684
|
+
if (!emitTemplateClassSlot(lines, target, slot, storeFields, constants, propBindings, fieldAccess, disposerVar))
|
|
685
|
+
return false;
|
|
686
|
+
continue;
|
|
687
|
+
}
|
|
688
|
+
if (slot.kind === 'style') {
|
|
689
|
+
if (!emitTemplateStyleSlot(lines, target, slot, storeFields, constants, propBindings, fieldAccess, disposerVar))
|
|
690
|
+
return false;
|
|
691
|
+
continue;
|
|
692
|
+
}
|
|
693
|
+
if (slot.kind === 'attr') {
|
|
694
|
+
const attrLines = attributeSlotLines(target, slot);
|
|
695
|
+
if (attrLines) {
|
|
696
|
+
lines.push(...attrLines);
|
|
697
|
+
}
|
|
698
|
+
else if (!emitTemplateAttrSlot(lines, target, slot, storeFields, constants, propBindings, fieldAccess, disposerVar)) {
|
|
699
|
+
return false;
|
|
700
|
+
}
|
|
701
|
+
continue;
|
|
702
|
+
}
|
|
703
|
+
if (slot.kind === 'event') {
|
|
704
|
+
const eventLines = eventSetupLines(target, slot, storeFields, args.storeMethods, fieldAccess, undefined, constants, disposerVar);
|
|
705
|
+
if (!eventLines)
|
|
706
|
+
return false;
|
|
707
|
+
lines.push(...eventLines);
|
|
708
|
+
continue;
|
|
709
|
+
}
|
|
710
|
+
// Reactive `value=` (and other reactive-attr-shaped) slots: track the
|
|
711
|
+
// store field via emitReactiveApply and push the latest stringified
|
|
712
|
+
// value into the DOM attribute. The macOS renderer's applyInputProps
|
|
713
|
+
// reads that attribute back into NSTextField's stringValue on every
|
|
714
|
+
// tree sync (except when the field is first responder, to avoid
|
|
715
|
+
// clobbering an in-flight edit).
|
|
716
|
+
if (slot.kind === 'value' || slot.kind === 'bool') {
|
|
717
|
+
const attrName = slotAttrName(slot);
|
|
718
|
+
if (!attrName)
|
|
719
|
+
return false;
|
|
720
|
+
const lowered = lowerTemplateExpression(slot.expr, storeFields, constants, propBindings);
|
|
721
|
+
if (!lowered || lowered.fields.some((field) => !field.readerName))
|
|
722
|
+
return false;
|
|
723
|
+
emitReactiveApply(lines, target, lowered, `${target}.setAttribute(${cppString(attrName)}, (${loweredAsString(lowered)}).c_str());`, fieldAccess, [], disposerVar);
|
|
724
|
+
continue;
|
|
725
|
+
}
|
|
726
|
+
return false;
|
|
727
|
+
}
|
|
728
|
+
return true;
|
|
729
|
+
}
|
|
730
|
+
function canEmitTemplateFragment(component, template, components, storeFields, constants, canMount, propBindings, storeMethods, storeArrayFields) {
|
|
731
|
+
const children = parseTemplateFragmentChildren(template.html);
|
|
732
|
+
if (!children || !canEmitTemplateChildStructure(children))
|
|
733
|
+
return false;
|
|
734
|
+
for (const slot of template.slots) {
|
|
735
|
+
if (slotReferencesUnboundProp(slot, propBindings))
|
|
736
|
+
continue;
|
|
737
|
+
if (slot.kind === 'mount') {
|
|
738
|
+
if (mountSlotTag(slot) === 'Image') {
|
|
739
|
+
if (!canEmitIntrinsicImageMount(slot, storeFields, constants, propBindings))
|
|
740
|
+
return false;
|
|
741
|
+
continue;
|
|
742
|
+
}
|
|
743
|
+
const child = childForMount(slot, components);
|
|
744
|
+
const childProps = mountPropBindings(slot, propBindings);
|
|
745
|
+
if (!child || (child === component && childProps.size === 0))
|
|
746
|
+
return false;
|
|
747
|
+
if (childProps.size > 0) {
|
|
748
|
+
// Mirror of canEmitTemplateElement: props mean inlining, which a
|
|
749
|
+
// reactive child can't do.
|
|
750
|
+
if (child.reactiveState)
|
|
751
|
+
return false;
|
|
752
|
+
const childRoot = parseTemplateRoot(child.template.html);
|
|
753
|
+
if (!childRoot || !uiCreateNodeExpression(childRoot))
|
|
754
|
+
return false;
|
|
755
|
+
if (!canEmitTemplateElement(child, components, storeFields, constants, canMount, childProps, storeMethods, storeArrayFields))
|
|
756
|
+
return false;
|
|
757
|
+
}
|
|
758
|
+
else if (!canMount(child)) {
|
|
759
|
+
return false;
|
|
760
|
+
}
|
|
761
|
+
continue;
|
|
762
|
+
}
|
|
763
|
+
if (slot.kind === 'keyed-list') {
|
|
764
|
+
if (!canEmitStaticKeyedList({ component, slot, components, storeFields, storeArrayFields, constants, canMount, propBindings, storeMethods }) &&
|
|
765
|
+
!canEmitDynamicKeyedListSlot({
|
|
766
|
+
component,
|
|
767
|
+
slot,
|
|
768
|
+
components,
|
|
769
|
+
storeFields,
|
|
770
|
+
storeArrayFields,
|
|
771
|
+
constants,
|
|
772
|
+
propBindings,
|
|
773
|
+
storeMethods,
|
|
774
|
+
}))
|
|
775
|
+
return false;
|
|
776
|
+
continue;
|
|
777
|
+
}
|
|
778
|
+
if (slot.kind === 'conditional') {
|
|
779
|
+
if (!canEmitConditionalSlot(component, slot, components, storeFields, constants, canMount, propBindings, storeMethods, storeArrayFields))
|
|
780
|
+
return false;
|
|
781
|
+
continue;
|
|
782
|
+
}
|
|
783
|
+
if (slot.kind === 'text') {
|
|
784
|
+
const childrenBinding = childrenBindingForPropSlot(slot, propBindings);
|
|
785
|
+
if (childrenBinding?.childrenTemplate) {
|
|
786
|
+
if (!canEmitTemplateFragment(component, childrenBinding.childrenTemplate, components, storeFields, constants, canMount, childrenBinding.parentProps, storeMethods, storeArrayFields))
|
|
787
|
+
return false;
|
|
788
|
+
continue;
|
|
789
|
+
}
|
|
790
|
+
const lowered = lowerTemplateExpression(slot.expr, storeFields, constants, propBindings);
|
|
791
|
+
if (!lowered || lowered.fields.some((field) => !field.readerName))
|
|
792
|
+
return false;
|
|
793
|
+
continue;
|
|
794
|
+
}
|
|
795
|
+
if (slot.kind === 'class') {
|
|
796
|
+
if (!canLowerTemplateClassSlot(slot, storeFields, constants, propBindings))
|
|
797
|
+
return false;
|
|
798
|
+
continue;
|
|
799
|
+
}
|
|
800
|
+
if (slot.kind === 'style') {
|
|
801
|
+
if (!canLowerTemplateStyleSlot(slot, storeFields, constants, propBindings))
|
|
802
|
+
return false;
|
|
803
|
+
continue;
|
|
804
|
+
}
|
|
805
|
+
if (slot.kind === 'attr') {
|
|
806
|
+
if (!canEmitTemplateAttrSlot(slot, storeFields, constants, propBindings))
|
|
807
|
+
return false;
|
|
808
|
+
continue;
|
|
809
|
+
}
|
|
810
|
+
if (slot.kind === 'event') {
|
|
811
|
+
if (!eventSetupLines('root', slot, storeFields, storeMethods, undefined, undefined, constants))
|
|
812
|
+
return false;
|
|
813
|
+
continue;
|
|
814
|
+
}
|
|
815
|
+
if (slot.kind === 'value' || slot.kind === 'bool') {
|
|
816
|
+
if (!slotAttrName(slot))
|
|
817
|
+
return false;
|
|
818
|
+
const lowered = lowerTemplateExpression(slot.expr, storeFields, constants, propBindings);
|
|
819
|
+
if (!lowered || lowered.fields.some((field) => !field.readerName))
|
|
820
|
+
return false;
|
|
821
|
+
continue;
|
|
822
|
+
}
|
|
823
|
+
return false;
|
|
824
|
+
}
|
|
825
|
+
return true;
|
|
826
|
+
}
|
|
827
|
+
function canEmitTemplateChildStructure(children) {
|
|
828
|
+
for (const child of children) {
|
|
829
|
+
if (child.kind !== 'element')
|
|
830
|
+
continue;
|
|
831
|
+
if (!uiCreateNodeExpression(child))
|
|
832
|
+
return false;
|
|
833
|
+
if (!canEmitTemplateChildStructure(child.children))
|
|
834
|
+
return false;
|
|
835
|
+
}
|
|
836
|
+
return true;
|
|
837
|
+
}
|
|
838
|
+
function canEmitConditionalSlot(component, slot, components, storeFields, constants, canMount, propBindings, storeMethods, storeArrayFields) {
|
|
839
|
+
const condition = lowerTemplateExpression(slot.expr, storeFields, constants, propBindings);
|
|
840
|
+
if (!condition || condition.fields.some((field) => !field.readerName))
|
|
841
|
+
return false;
|
|
842
|
+
const templates = conditionalBranchTemplates(slot);
|
|
843
|
+
if (!templates || !templates.consequent)
|
|
844
|
+
return false;
|
|
845
|
+
if (!canEmitTemplateFragment(component, templates.consequent, components, storeFields, constants, canMount, propBindings, storeMethods, storeArrayFields))
|
|
846
|
+
return false;
|
|
847
|
+
return !templates.alternate || canEmitTemplateFragment(component, templates.alternate, components, storeFields, constants, canMount, propBindings, storeMethods, storeArrayFields);
|
|
848
|
+
}
|
|
849
|
+
function emitTemplateFragment(args) {
|
|
850
|
+
const children = parseTemplateFragmentChildren(args.template.html);
|
|
851
|
+
if (!children)
|
|
852
|
+
return false;
|
|
853
|
+
const slotsByIndex = new Map(args.template.slots.map((slot) => [slot.index, slot]));
|
|
854
|
+
const pathVars = new Map();
|
|
855
|
+
const keyedListAnchors = new Map();
|
|
856
|
+
const fragment = { kind: 'element', tag: '#fragment', children };
|
|
857
|
+
if (!emitTemplateChildren({
|
|
858
|
+
component: args.component,
|
|
859
|
+
node: fragment,
|
|
860
|
+
varName: args.parentVar,
|
|
861
|
+
components: args.components,
|
|
862
|
+
storeFields: args.storeFields,
|
|
863
|
+
storeMethods: args.storeMethods,
|
|
864
|
+
lines: args.lines,
|
|
865
|
+
nextVar: args.nextVar,
|
|
866
|
+
canMount: args.canMount,
|
|
867
|
+
constants: args.constants,
|
|
868
|
+
propBindings: args.propBindings,
|
|
869
|
+
slotsByIndex,
|
|
870
|
+
pathVars,
|
|
871
|
+
keyedListAnchors,
|
|
872
|
+
path: [],
|
|
873
|
+
textTargetVar: args.textTargetVar,
|
|
874
|
+
fieldAccess: args.fieldAccess,
|
|
875
|
+
storeArrayFields: args.storeArrayFields,
|
|
876
|
+
disposerVar: activeDisposerVar(args),
|
|
877
|
+
})) {
|
|
878
|
+
return false;
|
|
879
|
+
}
|
|
880
|
+
return emitReactiveSlots({
|
|
881
|
+
component: args.component,
|
|
882
|
+
components: args.components,
|
|
883
|
+
slots: args.template.slots,
|
|
884
|
+
storeFields: args.storeFields,
|
|
885
|
+
storeArrayFields: args.storeArrayFields,
|
|
886
|
+
storeMethods: args.storeMethods,
|
|
887
|
+
pathVars,
|
|
888
|
+
lines: args.lines,
|
|
889
|
+
constants: args.constants,
|
|
890
|
+
propBindings: args.propBindings,
|
|
891
|
+
fieldAccess: args.fieldAccess,
|
|
892
|
+
keyedListAnchors,
|
|
893
|
+
disposerVar: activeDisposerVar(args),
|
|
894
|
+
});
|
|
895
|
+
}
|
|
896
|
+
function emitConditionalSlot(args) {
|
|
897
|
+
const condition = lowerTemplateExpression(args.slot.expr, args.storeFields, args.constants, args.propBindings);
|
|
898
|
+
const templates = conditionalBranchTemplates(args.slot);
|
|
899
|
+
if (!condition || condition.fields.some((field) => !field.readerName) || !templates?.consequent)
|
|
900
|
+
return false;
|
|
901
|
+
const disposerVar = activeDisposerVar(args);
|
|
902
|
+
const suffix = args.nextVar().replace(/^__gea_node_/, '');
|
|
903
|
+
const branchRef = `__gea_branch_${suffix}`;
|
|
904
|
+
const stateRef = `__gea_branch_state_${suffix}`;
|
|
905
|
+
const branchDisposerRef = `__gea_branch_disposer_${suffix}`;
|
|
906
|
+
const anchorRef = `__gea_branch_anchor_${suffix}`;
|
|
907
|
+
const makeTrue = `__gea_make_true_${suffix}`;
|
|
908
|
+
const makeFalse = `__gea_make_false_${suffix}`;
|
|
909
|
+
const hasFalseBranch = templates.alternate ? 'true' : 'false';
|
|
910
|
+
args.lines.push(` auto ${branchRef} = std::make_shared<gea::embedded::ui::NodeHandle>();`);
|
|
911
|
+
args.lines.push(` auto ${stateRef} = std::make_shared<int>(-1);`);
|
|
912
|
+
args.lines.push(` auto ${branchDisposerRef} = std::make_shared<std::shared_ptr<NativeDisposer>>();`);
|
|
913
|
+
args.lines.push(` auto ${anchorRef} = gea::embedded::ui::Document::instance().createView();`);
|
|
914
|
+
args.lines.push(` ${anchorRef}.setTagName("#comment");`);
|
|
915
|
+
args.lines.push(` ${anchorRef}.style().display(gea::embedded::ui::kDisplayNone);`);
|
|
916
|
+
args.lines.push(` ${args.parentVar}.appendChild(${anchorRef});`);
|
|
917
|
+
if (!emitTemplateFactory(args, makeTrue, templates.consequent, branchDisposerRef))
|
|
918
|
+
return false;
|
|
919
|
+
if (!emitTemplateFactory(args, makeFalse, templates.alternate ?? null, branchDisposerRef))
|
|
920
|
+
return false;
|
|
921
|
+
emitReactiveApply(args.lines, args.parentVar, condition, [
|
|
922
|
+
`const bool __gea_next_cond = ${loweredAsBoolean(condition)};`,
|
|
923
|
+
'const int __gea_next_state = __gea_next_cond ? 1 : 0;',
|
|
924
|
+
`const bool __gea_next_has_branch = __gea_next_cond || ${hasFalseBranch};`,
|
|
925
|
+
`const bool __gea_branch_attached = ${branchRef}->id() >= 0 && gea::embedded::ui::Tree::instance().containsNode(${args.parentVar}.id(), ${branchRef}->id());`,
|
|
926
|
+
`const bool __gea_branch_has_disposer = static_cast<bool>(*${branchDisposerRef});`,
|
|
927
|
+
`if (*${stateRef} == __gea_next_state && (!__gea_next_has_branch || (__gea_branch_attached && __gea_branch_has_disposer))) return;`,
|
|
928
|
+
`if (*${branchDisposerRef}) { (*${branchDisposerRef})->dispose(); ${branchDisposerRef}->reset(); }`,
|
|
929
|
+
`if (${branchRef}->id() >= 0) { const bool __gea_branch_attached_now = gea::embedded::ui::Tree::instance().containsNode(${args.parentVar}.id(), ${branchRef}->id()); if (__gea_branch_attached_now) ${branchRef}->remove(); *${branchRef} = gea::embedded::ui::NodeHandle(); }`,
|
|
930
|
+
`*${stateRef} = __gea_next_state;`,
|
|
931
|
+
`*${branchRef} = __gea_next_cond ? ${makeTrue}() : ${makeFalse}();`,
|
|
932
|
+
`if (${branchRef}->id() >= 0) ${args.parentVar}.insertBefore(*${branchRef}, ${anchorRef});`,
|
|
933
|
+
], args.fieldAccess, [branchRef, stateRef, branchDisposerRef, anchorRef, makeTrue, makeFalse], disposerVar);
|
|
934
|
+
return true;
|
|
935
|
+
}
|
|
936
|
+
function emitTemplateFactory(args, name, template, scopedDisposerRef) {
|
|
937
|
+
const parentDisposerVar = activeDisposerVar(args);
|
|
938
|
+
const branchDisposerVar = scopedDisposerRef ? `__gea_${name}_disposer` : parentDisposerVar;
|
|
939
|
+
const captures = ['store', parentDisposerVar];
|
|
940
|
+
if (scopedDisposerRef)
|
|
941
|
+
captures.push(scopedDisposerRef);
|
|
942
|
+
args.lines.push(` auto ${name} = [${[...new Set(captures)].join(', ')}]() mutable -> gea::embedded::ui::NodeHandle {`);
|
|
943
|
+
if (!template) {
|
|
944
|
+
args.lines.push(' return gea::embedded::ui::NodeHandle();');
|
|
945
|
+
args.lines.push(' };');
|
|
946
|
+
return true;
|
|
947
|
+
}
|
|
948
|
+
if (scopedDisposerRef) {
|
|
949
|
+
args.lines.push(` auto __gea_parent_disposer = ${parentDisposerVar};`);
|
|
950
|
+
args.lines.push(` auto __gea_branch_disposer = __gea_parent_disposer ? __gea_parent_disposer->child() : std::make_shared<NativeDisposer>();`);
|
|
951
|
+
args.lines.push(` *${scopedDisposerRef} = __gea_branch_disposer;`);
|
|
952
|
+
args.lines.push(` auto ${branchDisposerVar} = __gea_branch_disposer;`);
|
|
953
|
+
}
|
|
954
|
+
const rootVar = `__gea_${name}_root`;
|
|
955
|
+
if (!emitTemplateAsNode({ ...args, template, rootVar, disposerVar: branchDisposerVar }))
|
|
956
|
+
return false;
|
|
957
|
+
args.lines.push(` return ${rootVar};`);
|
|
958
|
+
args.lines.push(' };');
|
|
959
|
+
return true;
|
|
960
|
+
}
|
|
961
|
+
function emitTemplateAsNode(args) {
|
|
962
|
+
const children = parseTemplateFragmentChildren(args.template.html);
|
|
963
|
+
if (!children)
|
|
964
|
+
return false;
|
|
965
|
+
if (children.length === 1) {
|
|
966
|
+
const child = children[0];
|
|
967
|
+
if (child.kind === 'element') {
|
|
968
|
+
const createRoot = uiCreateNodeExpression(child);
|
|
969
|
+
if (!createRoot)
|
|
970
|
+
return false;
|
|
971
|
+
args.lines.push(` auto ${args.rootVar} = ${createRoot};`);
|
|
972
|
+
// A conditional / mount branch template carries its OWN slots. Resolve slot
|
|
973
|
+
// indices against THIS template, not the parent component's — emitTemplateElement
|
|
974
|
+
// otherwise rebuilds slotsByIndex from `component.template.slots`, so a
|
|
975
|
+
// single-root-element branch reuses the PARENT's slots and an internal slot
|
|
976
|
+
// index can resolve back to the conditional slot itself: infinite recursion in
|
|
977
|
+
// emitTemplateChildren. (The multi-child path, emitTemplateFragment, already
|
|
978
|
+
// scopes slots to args.template.slots; this mirrors it for the 1-root case.)
|
|
979
|
+
return emitTemplateElement({
|
|
980
|
+
component: { ...args.component, template: args.template },
|
|
981
|
+
node: child,
|
|
982
|
+
varName: args.rootVar,
|
|
983
|
+
components: args.components,
|
|
984
|
+
storeFields: args.storeFields,
|
|
985
|
+
storeMethods: args.storeMethods,
|
|
986
|
+
lines: args.lines,
|
|
987
|
+
nextVar: args.nextVar,
|
|
988
|
+
canMount: args.canMount,
|
|
989
|
+
constants: args.constants,
|
|
990
|
+
propBindings: args.propBindings,
|
|
991
|
+
fieldAccess: args.fieldAccess,
|
|
992
|
+
storeArrayFields: args.storeArrayFields,
|
|
993
|
+
disposerVar: activeDisposerVar(args),
|
|
994
|
+
});
|
|
995
|
+
}
|
|
996
|
+
if (child.kind === 'slot') {
|
|
997
|
+
const slot = args.template.slots.find((candidate) => candidate.index === child.index);
|
|
998
|
+
if (slot?.kind === 'mount')
|
|
999
|
+
return emitMountSlotAsNode(args, slot, args.rootVar);
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
args.lines.push(` auto ${args.rootVar} = gea::embedded::ui::Document::instance().createView();`);
|
|
1003
|
+
args.lines.push(` ${args.rootVar}.setTagName("span");`);
|
|
1004
|
+
return emitTemplateFragment({ ...args, parentVar: args.rootVar });
|
|
1005
|
+
}
|
|
1006
|
+
function emitMountSlotAsNode(args, slot, varName) {
|
|
1007
|
+
const disposerVar = activeDisposerVar(args);
|
|
1008
|
+
if (mountSlotTag(slot) === 'Image') {
|
|
1009
|
+
args.lines.push(` auto ${varName} = gea::embedded::ui::Document::instance().createImage();`);
|
|
1010
|
+
args.lines.push(...attributeLines(varName, { tag: 'img' }));
|
|
1011
|
+
return emitIntrinsicImageMount(args.lines, varName, slot, args.storeFields, args.constants, args.propBindings, disposerVar);
|
|
1012
|
+
}
|
|
1013
|
+
const mountedChild = childForMount(slot, args.components);
|
|
1014
|
+
if (!mountedChild)
|
|
1015
|
+
return false;
|
|
1016
|
+
const childProps = mountPropBindings(slot, args.propBindings);
|
|
1017
|
+
if (childProps.size === 0) {
|
|
1018
|
+
if (!args.canMount(mountedChild)) {
|
|
1019
|
+
if (args.fieldAccess?.typed || mountedChild.reactiveState || !canGenericChildMount(mountedChild, childProps))
|
|
1020
|
+
return false;
|
|
1021
|
+
emitGenericChildAsNode(args.lines, varName, mountedChild, args.nextVar, disposerVar);
|
|
1022
|
+
return true;
|
|
1023
|
+
}
|
|
1024
|
+
const fieldAccess = args.fieldAccess ?? BOXED_FIELD_ACCESS;
|
|
1025
|
+
const childStoreArg = fieldAccess.childMountStoreArg(mountedChild);
|
|
1026
|
+
if (childStoreArg === null)
|
|
1027
|
+
return false;
|
|
1028
|
+
args.lines.push(` auto ${varName} = mount_${sanitizeCppIdentifier(mountedChild.exportName)}(${childStoreArg}, ${disposerVar});`);
|
|
1029
|
+
return true;
|
|
1030
|
+
}
|
|
1031
|
+
// Same rule as the main mount path: a reactive child cannot be inlined.
|
|
1032
|
+
if (mountedChild.reactiveState)
|
|
1033
|
+
return false;
|
|
1034
|
+
// Same wrapper-resolution as the main mount-inlining path: a wrapper
|
|
1035
|
+
// component used here (e.g. inside a conditional branch) must be peeled so
|
|
1036
|
+
// the wrapped element receives the bindings directly, not a stray
|
|
1037
|
+
// display:contents span.
|
|
1038
|
+
const resolvedChild = resolveTransparentWrappers(mountedChild, args.components, childProps, args.canMount);
|
|
1039
|
+
const effectiveChild = resolvedChild.component;
|
|
1040
|
+
const effectiveProps = resolvedChild.propBindings;
|
|
1041
|
+
const mountedRoot = parseTemplateRoot(effectiveChild.template.html);
|
|
1042
|
+
const createMountedRoot = mountedRoot ? uiCreateNodeExpression(mountedRoot) : null;
|
|
1043
|
+
if (!mountedRoot || !createMountedRoot)
|
|
1044
|
+
return false;
|
|
1045
|
+
args.lines.push(` auto ${varName} = ${createMountedRoot};`);
|
|
1046
|
+
emitInputButtonValueText(args.lines, varName, mountedRoot, args.nextVar);
|
|
1047
|
+
return emitTemplateElement({
|
|
1048
|
+
component: effectiveChild,
|
|
1049
|
+
node: mountedRoot,
|
|
1050
|
+
varName,
|
|
1051
|
+
components: args.components,
|
|
1052
|
+
storeFields: args.storeFields,
|
|
1053
|
+
storeMethods: args.storeMethods,
|
|
1054
|
+
lines: args.lines,
|
|
1055
|
+
nextVar: args.nextVar,
|
|
1056
|
+
canMount: args.canMount,
|
|
1057
|
+
constants: args.constants,
|
|
1058
|
+
propBindings: effectiveProps,
|
|
1059
|
+
fieldAccess: args.fieldAccess,
|
|
1060
|
+
storeArrayFields: args.storeArrayFields,
|
|
1061
|
+
disposerVar,
|
|
1062
|
+
});
|
|
1063
|
+
}
|
|
1064
|
+
function emitStaticKeyedList(args) {
|
|
1065
|
+
const { lines, parentVar, slot, nextVar } = args;
|
|
1066
|
+
const payload = keyedListPayload(slot);
|
|
1067
|
+
const values = staticStringArrayValues(slot.expr);
|
|
1068
|
+
const itemParam = payload?.itemParam;
|
|
1069
|
+
const rowRoot = payload?.rowTemplate ? parseTemplateRoot(payload.rowTemplate.html) : null;
|
|
1070
|
+
const createRow = rowRoot ? uiCreateNodeExpression(rowRoot) : null;
|
|
1071
|
+
if (!payload?.rowTemplate || !itemParam || !rowRoot || !createRow)
|
|
1072
|
+
return false;
|
|
1073
|
+
const objectValues = objectArrayConstant(slot.expr, args.constants);
|
|
1074
|
+
if (objectValues)
|
|
1075
|
+
return emitStaticObjectKeyedList(args, payload.rowTemplate, itemParam, objectValues);
|
|
1076
|
+
if (!values)
|
|
1077
|
+
return false;
|
|
1078
|
+
for (const value of values) {
|
|
1079
|
+
const rowVar = nextVar();
|
|
1080
|
+
lines.push(` auto ${rowVar} = ${createRow};`);
|
|
1081
|
+
lines.push(...attributeLines(rowVar, { tag: rowRoot.tag, className: rowRoot.className }));
|
|
1082
|
+
lines.push(...intrinsicStyleLines(rowVar, rowRoot));
|
|
1083
|
+
for (const rowSlot of payload.rowTemplate.slots) {
|
|
1084
|
+
if (!emitStaticKeyedListRowSlot(lines, rowVar, rowSlot, itemParam, value))
|
|
1085
|
+
return false;
|
|
1086
|
+
}
|
|
1087
|
+
lines.push(` ${parentVar}.appendChild(${rowVar});`);
|
|
1088
|
+
}
|
|
1089
|
+
return true;
|
|
1090
|
+
}
|
|
1091
|
+
function emitStaticObjectKeyedList(args, rowTemplate, itemParam, items) {
|
|
1092
|
+
for (let index = 0; index < items.length; index += 1) {
|
|
1093
|
+
const rewritten = rewriteStaticObjectRowTemplate(rowTemplate, itemParam, items[index]);
|
|
1094
|
+
const rowRoot = parseTemplateRoot(rewritten.html);
|
|
1095
|
+
const createRow = rowRoot ? uiCreateNodeExpression(rowRoot) : null;
|
|
1096
|
+
if (!rowRoot || !createRow)
|
|
1097
|
+
return false;
|
|
1098
|
+
const rowVar = args.nextVar();
|
|
1099
|
+
const rowComponent = {
|
|
1100
|
+
...args.component,
|
|
1101
|
+
id: `${args.component.id}#static-list-${args.slot.index}-${index}`,
|
|
1102
|
+
exportName: `${args.component.exportName}_StaticListRow_${args.slot.index}_${index}`,
|
|
1103
|
+
template: rewritten,
|
|
1104
|
+
};
|
|
1105
|
+
args.lines.push(` auto ${rowVar} = ${createRow};`);
|
|
1106
|
+
args.lines.push(` ${args.parentVar}.appendChild(${rowVar});`);
|
|
1107
|
+
if (!emitTemplateElement({
|
|
1108
|
+
component: rowComponent,
|
|
1109
|
+
node: rowRoot,
|
|
1110
|
+
varName: rowVar,
|
|
1111
|
+
components: args.components,
|
|
1112
|
+
storeFields: args.storeFields,
|
|
1113
|
+
lines: args.lines,
|
|
1114
|
+
nextVar: args.nextVar,
|
|
1115
|
+
canMount: args.canMount,
|
|
1116
|
+
constants: args.constants,
|
|
1117
|
+
propBindings: args.propBindings,
|
|
1118
|
+
storeMethods: args.storeMethods,
|
|
1119
|
+
fieldAccess: args.fieldAccess,
|
|
1120
|
+
storeArrayFields: args.storeArrayFields,
|
|
1121
|
+
disposerVar: activeDisposerVar(args),
|
|
1122
|
+
}))
|
|
1123
|
+
return false;
|
|
1124
|
+
}
|
|
1125
|
+
return true;
|
|
1126
|
+
}
|
|
1127
|
+
function emitStaticKeyedListRowSlot(lines, rowVar, rowSlot, itemParam, value) {
|
|
1128
|
+
if (!isStaticKeyedListRowSlot(rowSlot, itemParam))
|
|
1129
|
+
return false;
|
|
1130
|
+
if (rowSlot.kind === 'class') {
|
|
1131
|
+
lines.push(` ${rowVar}.classList().set(${cppString(value)});`);
|
|
1132
|
+
return true;
|
|
1133
|
+
}
|
|
1134
|
+
if (rowSlot.kind === 'text') {
|
|
1135
|
+
lines.push(` ${rowVar}.setText(${cppString(value)});`);
|
|
1136
|
+
return true;
|
|
1137
|
+
}
|
|
1138
|
+
return false;
|
|
1139
|
+
}
|
|
1140
|
+
function canEmitStaticKeyedList(args) {
|
|
1141
|
+
const { slot } = args;
|
|
1142
|
+
const payload = keyedListPayload(slot);
|
|
1143
|
+
const values = staticStringArrayValues(slot.expr);
|
|
1144
|
+
const itemParam = payload?.itemParam;
|
|
1145
|
+
const rowRoot = payload?.rowTemplate ? parseTemplateRoot(payload.rowTemplate.html) : null;
|
|
1146
|
+
if (!payload?.rowTemplate || !itemParam)
|
|
1147
|
+
return false;
|
|
1148
|
+
const objectValues = objectArrayConstant(slot.expr, args.constants);
|
|
1149
|
+
if (objectValues) {
|
|
1150
|
+
return objectValues.every((item, index) => {
|
|
1151
|
+
const rewritten = rewriteStaticObjectRowTemplate(payload.rowTemplate, itemParam, item);
|
|
1152
|
+
const rowComponent = {
|
|
1153
|
+
...args.component,
|
|
1154
|
+
id: `${args.component.id}#static-list-${slot.index}-${index}`,
|
|
1155
|
+
exportName: `${args.component.exportName}_StaticListRow_${slot.index}_${index}`,
|
|
1156
|
+
template: rewritten,
|
|
1157
|
+
};
|
|
1158
|
+
return canEmitTemplateElement(rowComponent, args.components, args.storeFields, args.constants, args.canMount, args.propBindings, args.storeMethods, args.storeArrayFields);
|
|
1159
|
+
});
|
|
1160
|
+
}
|
|
1161
|
+
if (!values || !rowRoot || !uiCreateNodeExpression(rowRoot))
|
|
1162
|
+
return false;
|
|
1163
|
+
return payload.rowTemplate.slots.every((rowSlot) => isStaticKeyedListRowSlot(rowSlot, itemParam));
|
|
1164
|
+
}
|
|
1165
|
+
function isStaticKeyedListSource(slot, constants) {
|
|
1166
|
+
const payload = keyedListPayload(slot);
|
|
1167
|
+
if (!payload?.rowTemplate || !payload.itemParam)
|
|
1168
|
+
return false;
|
|
1169
|
+
return staticStringArrayValues(slot.expr) !== null || objectArrayConstant(slot.expr, constants) !== null;
|
|
1170
|
+
}
|
|
1171
|
+
function canEmitDynamicKeyedListSlot(args) {
|
|
1172
|
+
return dynamicKeyedListPlan(args) !== null;
|
|
1173
|
+
}
|
|
1174
|
+
function emitDynamicKeyedListSlot(args) {
|
|
1175
|
+
const plan = dynamicKeyedListPlan(args);
|
|
1176
|
+
if (!plan)
|
|
1177
|
+
return false;
|
|
1178
|
+
const disposerVar = activeDisposerVar(args);
|
|
1179
|
+
const suffix = args.slot.index;
|
|
1180
|
+
const storeVar = `__gea_kl_store_${suffix}`;
|
|
1181
|
+
const rowsVar = `__gea_kl_rows_${suffix}`;
|
|
1182
|
+
const rebuildVar = `__gea_kl_rebuild_${suffix}`;
|
|
1183
|
+
const itemName = plan.itemName;
|
|
1184
|
+
const itemSource = plan.field.isGetter ? `${storeVar}->${plan.field.fieldName}()` : `${storeVar}->${plan.field.fieldName}`;
|
|
1185
|
+
const rebuildCaptures = [args.target, storeVar, rowsVar];
|
|
1186
|
+
if (args.insertBefore)
|
|
1187
|
+
rebuildCaptures.push(args.insertBefore);
|
|
1188
|
+
args.lines.push(` auto ${storeVar} = ${plan.storeExpr};`);
|
|
1189
|
+
args.lines.push(` auto ${rowsVar} = std::make_shared<std::vector<gea::embedded::ui::NodeHandle>>();`);
|
|
1190
|
+
args.lines.push(` auto ${rebuildVar} = [${rebuildCaptures.join(', ')}]() mutable -> void {`);
|
|
1191
|
+
args.lines.push(` for (auto &__gea_kl_old : *${rowsVar}) __gea_kl_old.remove();`);
|
|
1192
|
+
args.lines.push(` ${rowsVar}->clear();`);
|
|
1193
|
+
args.lines.push(` if (!${storeVar}) return;`);
|
|
1194
|
+
args.lines.push(` const auto &typed_store = ${storeVar};`);
|
|
1195
|
+
// Also alias `store` so row-scalar readers emitted as `read_X(store)`
|
|
1196
|
+
// (lowerRowExpressionNode) resolve here — this lambda captures the keyed-list
|
|
1197
|
+
// store as ${storeVar}, not `store`. Other rebuild paths capture `store`
|
|
1198
|
+
// directly, so the reader codegen stays `(store)` and works in both contexts.
|
|
1199
|
+
args.lines.push(` const auto &store = ${storeVar};`);
|
|
1200
|
+
args.lines.push(` const auto &__gea_items = ${itemSource};`);
|
|
1201
|
+
args.lines.push(' for (std::size_t __gea_index = 0; __gea_index < __gea_items.size(); ++__gea_index) {');
|
|
1202
|
+
args.lines.push(` const auto &${itemName} = __gea_items[__gea_index];`);
|
|
1203
|
+
args.lines.push(` auto __gea_row_root = ${plan.createRow};`);
|
|
1204
|
+
for (const line of plan.rowAttributeLines)
|
|
1205
|
+
args.lines.push(` ${line}`);
|
|
1206
|
+
for (const line of plan.rowSetupLines)
|
|
1207
|
+
args.lines.push(` ${line.trimStart()}`);
|
|
1208
|
+
for (const line of plan.rowTreeLines)
|
|
1209
|
+
args.lines.push(` ${line}`);
|
|
1210
|
+
for (const line of plan.rowSlotLines)
|
|
1211
|
+
args.lines.push(` ${line}`);
|
|
1212
|
+
for (const line of plan.rowEventLines)
|
|
1213
|
+
args.lines.push(` ${line}`);
|
|
1214
|
+
if (args.insertBefore)
|
|
1215
|
+
args.lines.push(` ${args.target}.insertBefore(__gea_row_root, ${args.insertBefore});`);
|
|
1216
|
+
else
|
|
1217
|
+
args.lines.push(` ${args.target}.appendChild(__gea_row_root);`);
|
|
1218
|
+
args.lines.push(` ${rowsVar}->push_back(__gea_row_root);`);
|
|
1219
|
+
args.lines.push(' }');
|
|
1220
|
+
args.lines.push(' };');
|
|
1221
|
+
const deps = plan.deps.map((dep) => cppString(dep)).join(', ');
|
|
1222
|
+
const apply = `[${rebuildVar}]() mutable -> void { ${rebuildVar}(); }`;
|
|
1223
|
+
if (storeHubEmissionEnabled() && plan.field.storeRuntimeBase === 'compiled' && !plan.field.storeIsSelfStore) {
|
|
1224
|
+
args.lines.push(` gea_rc_bind_hub_apply(${storeVar}, ${disposerVar}, {${deps}}, std::function<void()>(${apply}));`);
|
|
1225
|
+
}
|
|
1226
|
+
else {
|
|
1227
|
+
args.lines.push(` bindReactiveApply(gea_cpp_key(${storeVar}), ${disposerVar}, {${deps}}, ${apply});`);
|
|
1228
|
+
}
|
|
1229
|
+
return true;
|
|
1230
|
+
}
|
|
1231
|
+
function dynamicKeyedListPlan(args) {
|
|
1232
|
+
const payload = keyedListPayload(args.slot);
|
|
1233
|
+
if (!payload?.rowTemplate || !payload.itemParam)
|
|
1234
|
+
return null;
|
|
1235
|
+
const field = storeArrayFieldForSlot(args.slot, args.storeArrayFields);
|
|
1236
|
+
const storeGlobalName = keyedListReceiver(args.slot) ?? field?.storeGlobalName ?? null;
|
|
1237
|
+
if (!field || !storeGlobalName)
|
|
1238
|
+
return null;
|
|
1239
|
+
const itemName = sanitizeCppIdentifier(payload.itemParam);
|
|
1240
|
+
const rowTemplate = dynamicKeyedListRowTemplate(payload.rowTemplate, args.components, itemName);
|
|
1241
|
+
const rowRoot = rowTemplate ? parseTemplateRoot(rowTemplate.html) : null;
|
|
1242
|
+
const createRow = rowRoot ? uiCreateNodeExpression(rowRoot) : null;
|
|
1243
|
+
if (!rowTemplate || !rowRoot || !createRow)
|
|
1244
|
+
return null;
|
|
1245
|
+
const rowSetupSlots = rowTemplate.slots.filter((s) => s.kind !== 'event' &&
|
|
1246
|
+
s.kind !== 'class' &&
|
|
1247
|
+
s.kind !== 'text' &&
|
|
1248
|
+
s.kind !== 'style' &&
|
|
1249
|
+
// `key` belongs to keyed reconciliation, never to the rendered node.
|
|
1250
|
+
!(s.kind === 'attr' && slotAttrName(s) === 'key') &&
|
|
1251
|
+
!(s.kind === 'attr' && slotAttrName(s) === 'src' && !!s.expr && stringLiteralValue(s.expr) === null));
|
|
1252
|
+
const rowSetupLines = rootSetupLines('__gea_row_root', rowSetupSlots, [], [], undefined, undefined, args.constants);
|
|
1253
|
+
if (!rowSetupLines)
|
|
1254
|
+
return null;
|
|
1255
|
+
const rowTree = emitDynamicKeyedRowTree(rowRoot, '__gea_row_root', rowTemplate.slots);
|
|
1256
|
+
if (!rowTree.ok)
|
|
1257
|
+
return null;
|
|
1258
|
+
const rowSlots = lowerRowSlots(rowTemplate.slots, itemName, field, args.constants, rowTree.pathVars, args.storeFields, args.storeMethods);
|
|
1259
|
+
if (rowSlots.lines.length === 0 && rowTree.lines.length === 0 && rowSlots.eventLines.length === 0)
|
|
1260
|
+
return null;
|
|
1261
|
+
const deps = uniqueStrings([...(field.isGetter ? field.reactiveDeps ?? [] : [field.fieldName]), ...rowSlots.storeFields.map((storeField) => storeField.fieldName)]);
|
|
1262
|
+
return {
|
|
1263
|
+
field,
|
|
1264
|
+
itemName,
|
|
1265
|
+
storeExpr: `__gea_global_${sanitizeCppIdentifier(storeGlobalName)}()`,
|
|
1266
|
+
deps,
|
|
1267
|
+
createRow,
|
|
1268
|
+
rowAttributeLines: attributeLines('__gea_row_root', { tag: rowRoot.tag, className: rowRoot.className, attrs: rowRoot.attrs }, rowTemplate.slots),
|
|
1269
|
+
rowSetupLines,
|
|
1270
|
+
rowTreeLines: rowTree.lines,
|
|
1271
|
+
rowSlotLines: rowSlots.lines,
|
|
1272
|
+
rowEventLines: rowSlots.eventLines,
|
|
1273
|
+
};
|
|
1274
|
+
}
|
|
1275
|
+
function storeArrayFieldForSlot(slot, fields) {
|
|
1276
|
+
const fieldName = fieldNameForSlot(slot);
|
|
1277
|
+
if (!fieldName)
|
|
1278
|
+
return null;
|
|
1279
|
+
const receiver = keyedListReceiver(slot);
|
|
1280
|
+
const matches = fields.filter((field) => field.fieldName === fieldName);
|
|
1281
|
+
if (receiver) {
|
|
1282
|
+
const byReceiver = matches.filter((field) => field.storeGlobalName === receiver);
|
|
1283
|
+
if (byReceiver.length === 1)
|
|
1284
|
+
return byReceiver[0];
|
|
1285
|
+
}
|
|
1286
|
+
return matches.length === 1 ? matches[0] : null;
|
|
1287
|
+
}
|
|
1288
|
+
function keyedListReceiver(slot) {
|
|
1289
|
+
if (slot.exprPath && slot.exprPath.length > 1)
|
|
1290
|
+
return sanitizeCppIdentifier(slot.exprPath[0]);
|
|
1291
|
+
const match = slot.expr?.trim().match(/^([A-Za-z_$][A-Za-z0-9_$]*)\./);
|
|
1292
|
+
return match ? sanitizeCppIdentifier(match[1]) : null;
|
|
1293
|
+
}
|
|
1294
|
+
function dynamicKeyedListRowTemplate(rowTemplate, components, itemName) {
|
|
1295
|
+
if (parseTemplateRoot(rowTemplate.html))
|
|
1296
|
+
return rowTemplate;
|
|
1297
|
+
const mountSlots = rowTemplate.slots.filter((slot) => slot.kind === 'mount');
|
|
1298
|
+
if (mountSlots.length !== 1)
|
|
1299
|
+
return null;
|
|
1300
|
+
const childTag = mountSlotTag(mountSlots[0]);
|
|
1301
|
+
const child = childTag ? components.find((component) => component.exportName === childTag) : null;
|
|
1302
|
+
if (!child || !parseTemplateRoot(child.template.html))
|
|
1303
|
+
return null;
|
|
1304
|
+
const bindings = rowComponentPropBindings(mountSlots[0], itemName);
|
|
1305
|
+
if (!bindings)
|
|
1306
|
+
return null;
|
|
1307
|
+
return {
|
|
1308
|
+
html: child.template.html,
|
|
1309
|
+
slots: child.template.slots.map((slot) => rewriteRowComponentSlot(slot, bindings)),
|
|
1310
|
+
};
|
|
1311
|
+
}
|
|
1312
|
+
function rowComponentPropBindings(slot, itemName) {
|
|
1313
|
+
const attrs = mountSlotAttrs(slot);
|
|
1314
|
+
if (attrs.length === 0)
|
|
1315
|
+
return null;
|
|
1316
|
+
const bindings = new Map();
|
|
1317
|
+
let bindsItem = false;
|
|
1318
|
+
for (const attr of attrs) {
|
|
1319
|
+
const parsed = parseMountAttr(attr);
|
|
1320
|
+
if (!parsed)
|
|
1321
|
+
return null;
|
|
1322
|
+
// `key` belongs to the list reconciler, not to the child component.
|
|
1323
|
+
if (parsed.name === 'key')
|
|
1324
|
+
continue;
|
|
1325
|
+
bindings.set(parsed.name, parsed.expr);
|
|
1326
|
+
if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(parsed.expr) && sanitizeCppIdentifier(parsed.expr) === itemName) {
|
|
1327
|
+
bindsItem = true;
|
|
1328
|
+
}
|
|
1329
|
+
}
|
|
1330
|
+
return bindsItem ? bindings : null;
|
|
1331
|
+
}
|
|
1332
|
+
function rewriteRowComponentSlot(slot, bindings) {
|
|
1333
|
+
return {
|
|
1334
|
+
...slot,
|
|
1335
|
+
...(slot.expr ? { expr: rewriteRowComponentExpression(slot.expr, bindings) } : {}),
|
|
1336
|
+
...(slot.exprObjectFields
|
|
1337
|
+
? {
|
|
1338
|
+
exprObjectFields: slot.exprObjectFields.map((field) => ({
|
|
1339
|
+
...field,
|
|
1340
|
+
expr: rewriteRowComponentExpression(field.expr, bindings),
|
|
1341
|
+
})),
|
|
1342
|
+
}
|
|
1343
|
+
: {}),
|
|
1344
|
+
};
|
|
1345
|
+
}
|
|
1346
|
+
function rewriteRowComponentExpression(expr, bindings) {
|
|
1347
|
+
let rewritten = expr;
|
|
1348
|
+
// Longest names first keeps `props.bookTitle` independent from `props.book`.
|
|
1349
|
+
const entries = [...bindings.entries()].sort(([a], [b]) => b.length - a.length);
|
|
1350
|
+
for (const [propName, value] of entries) {
|
|
1351
|
+
const prop = escapeRegExp(propName);
|
|
1352
|
+
const replacement = /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(value) ? value : `(${value})`;
|
|
1353
|
+
rewritten = rewritten
|
|
1354
|
+
.replace(new RegExp(`\\bthis\\.props\\.${prop}\\b`, 'g'), replacement)
|
|
1355
|
+
.replace(new RegExp(`\\bprops\\.${prop}\\b`, 'g'), replacement);
|
|
1356
|
+
}
|
|
1357
|
+
return rewritten;
|
|
1358
|
+
}
|
|
1359
|
+
function emitDynamicKeyedRowTree(root, rowVarName, slots) {
|
|
1360
|
+
const lines = [];
|
|
1361
|
+
const pathVars = new Map([['', rowVarName]]);
|
|
1362
|
+
const dynamicTextPaths = new Set(slots.filter((slot) => slot.kind === 'text').map((slot) => slot.walk.join(',')));
|
|
1363
|
+
let nextId = 0;
|
|
1364
|
+
const nextVar = () => `__gea_kl_row_${nextId++}`;
|
|
1365
|
+
const visit = (parent, parentVar, parentPath) => {
|
|
1366
|
+
for (let index = 0; index < parent.children.length; index += 1) {
|
|
1367
|
+
const child = parent.children[index];
|
|
1368
|
+
const childPath = [...parentPath, index];
|
|
1369
|
+
if (child.kind === 'element') {
|
|
1370
|
+
const createChild = uiCreateNodeExpression(child);
|
|
1371
|
+
if (!createChild)
|
|
1372
|
+
return false;
|
|
1373
|
+
const childVar = nextVar();
|
|
1374
|
+
lines.push(`auto ${childVar} = ${createChild};`);
|
|
1375
|
+
lines.push(`${childVar}.setTagName(${cppString(child.tag.toLowerCase())});`);
|
|
1376
|
+
if (child.className)
|
|
1377
|
+
lines.push(`${childVar}.classList().set(${cppString(child.className)});`);
|
|
1378
|
+
for (const [name, value] of Object.entries(child.attrs ?? {})) {
|
|
1379
|
+
const lower = name.toLowerCase();
|
|
1380
|
+
if (lower === 'class' || lower === 'style')
|
|
1381
|
+
continue;
|
|
1382
|
+
lines.push(`${childVar}.setAttribute(${cppString(name)}, ${cppString(value)});`);
|
|
1383
|
+
}
|
|
1384
|
+
lines.push(`${parentVar}.appendChild(${childVar});`);
|
|
1385
|
+
pathVars.set(childPath.join(','), childVar);
|
|
1386
|
+
if (!visit(child, childVar, childPath))
|
|
1387
|
+
return false;
|
|
1388
|
+
continue;
|
|
1389
|
+
}
|
|
1390
|
+
if (child.kind === 'text') {
|
|
1391
|
+
const text = renderableTemplateText(child.text);
|
|
1392
|
+
if (!text)
|
|
1393
|
+
continue;
|
|
1394
|
+
// The IR uses a literal `0` text node as the DOM anchor for a dynamic
|
|
1395
|
+
// JSX expression. A real authored zero has the same HTML spelling, so
|
|
1396
|
+
// only treat it as an anchor when a text slot targets this exact path.
|
|
1397
|
+
// Previously every literal zero in a keyed row was discarded.
|
|
1398
|
+
if (text === '0' && dynamicTextPaths.has(childPath.join(','))) {
|
|
1399
|
+
pathVars.set(childPath.join(','), parentVar);
|
|
1400
|
+
continue;
|
|
1401
|
+
}
|
|
1402
|
+
if (canUseTextNodeForElement(parent)) {
|
|
1403
|
+
lines.push(`${parentVar}.setText(${cppString(text)});`);
|
|
1404
|
+
pathVars.set(childPath.join(','), parentVar);
|
|
1405
|
+
}
|
|
1406
|
+
else {
|
|
1407
|
+
const textVar = nextVar();
|
|
1408
|
+
lines.push(`auto ${textVar} = gea::embedded::ui::Document::instance().createText();`);
|
|
1409
|
+
lines.push(`${textVar}.setText(${cppString(text)});`);
|
|
1410
|
+
lines.push(`${parentVar}.appendChild(${textVar});`);
|
|
1411
|
+
pathVars.set(childPath.join(','), textVar);
|
|
1412
|
+
}
|
|
1413
|
+
continue;
|
|
1414
|
+
}
|
|
1415
|
+
if (dynamicTextPaths.has(childPath.join(',')) && !canUseTextNodeForElement(parent)) {
|
|
1416
|
+
// A mixed inline run is encoded as `<!--slot-->suffix`. The parent must
|
|
1417
|
+
// remain a View so its runs lay out side by side; give the dynamic slot
|
|
1418
|
+
// its own text node instead of calling setText on that View (which made
|
|
1419
|
+
// numeric 0, and every other prefix, disappear before `% READ`).
|
|
1420
|
+
const textVar = nextVar();
|
|
1421
|
+
lines.push(`auto ${textVar} = gea::embedded::ui::Document::instance().createText();`);
|
|
1422
|
+
lines.push(`${parentVar}.appendChild(${textVar});`);
|
|
1423
|
+
pathVars.set(childPath.join(','), textVar);
|
|
1424
|
+
continue;
|
|
1425
|
+
}
|
|
1426
|
+
pathVars.set(childPath.join(','), parentVar);
|
|
1427
|
+
}
|
|
1428
|
+
return true;
|
|
1429
|
+
};
|
|
1430
|
+
return visit(root, rowVarName, []) ? { ok: true, lines, pathVars } : { ok: false, lines: [], pathVars };
|
|
1431
|
+
}
|
|
1432
|
+
function rewriteStaticObjectRowTemplate(template, itemParam, item) {
|
|
1433
|
+
return {
|
|
1434
|
+
html: template.html,
|
|
1435
|
+
slots: template.slots.map((slot) => rewriteStaticObjectRowSlot(slot, itemParam, item)),
|
|
1436
|
+
};
|
|
1437
|
+
}
|
|
1438
|
+
function rewriteStaticObjectRowSlot(slot, itemParam, item) {
|
|
1439
|
+
return {
|
|
1440
|
+
...slot,
|
|
1441
|
+
...(slot.expr ? { expr: rewriteStaticObjectRowExpression(slot.expr, itemParam, item) } : {}),
|
|
1442
|
+
...(slot.exprObjectFields
|
|
1443
|
+
? {
|
|
1444
|
+
exprObjectFields: slot.exprObjectFields.map((field) => ({
|
|
1445
|
+
...field,
|
|
1446
|
+
expr: rewriteStaticObjectRowExpression(field.expr, itemParam, item),
|
|
1447
|
+
})),
|
|
1448
|
+
}
|
|
1449
|
+
: {}),
|
|
1450
|
+
...(slot.payload ? { payload: rewriteStaticObjectRowPayload(slot.payload, itemParam, item) } : {}),
|
|
1451
|
+
};
|
|
1452
|
+
}
|
|
1453
|
+
function rewriteStaticObjectRowPayload(payload, itemParam, item) {
|
|
1454
|
+
if (!payload || typeof payload !== 'object')
|
|
1455
|
+
return payload;
|
|
1456
|
+
if (!('attrs' in payload) || !Array.isArray(payload.attrs))
|
|
1457
|
+
return payload;
|
|
1458
|
+
return {
|
|
1459
|
+
...payload,
|
|
1460
|
+
attrs: payload.attrs.map((attr) => {
|
|
1461
|
+
if (!attr || typeof attr !== 'object' || typeof attr.code !== 'string')
|
|
1462
|
+
return attr;
|
|
1463
|
+
return {
|
|
1464
|
+
...attr,
|
|
1465
|
+
code: rewriteStaticObjectRowExpression(attr.code, itemParam, item),
|
|
1466
|
+
};
|
|
1467
|
+
}),
|
|
1468
|
+
};
|
|
1469
|
+
}
|
|
1470
|
+
function rewriteStaticObjectRowExpression(expr, itemParam, item) {
|
|
1471
|
+
let rewritten = expr;
|
|
1472
|
+
for (const field of item.fields) {
|
|
1473
|
+
const escapedItem = escapeRegExp(itemParam);
|
|
1474
|
+
const escapedField = escapeRegExp(field.name);
|
|
1475
|
+
rewritten = rewritten.replace(new RegExp(`\\b${escapedItem}\\.${escapedField}\\b`, 'g'), staticObjectFieldLiteral(field));
|
|
1476
|
+
}
|
|
1477
|
+
return rewritten;
|
|
1478
|
+
}
|
|
1479
|
+
function staticObjectFieldLiteral(field) {
|
|
1480
|
+
if (field.valueType === 'string')
|
|
1481
|
+
return JSON.stringify(field.value);
|
|
1482
|
+
if (field.valueType === 'number' || field.valueType === 'boolean')
|
|
1483
|
+
return field.value;
|
|
1484
|
+
return 'null';
|
|
1485
|
+
}
|
|
1486
|
+
function isStaticKeyedListRowSlot(slot, itemParam) {
|
|
1487
|
+
if (slot.walk.length !== 0)
|
|
1488
|
+
return false;
|
|
1489
|
+
if (slot.kind !== 'class' && slot.kind !== 'text')
|
|
1490
|
+
return false;
|
|
1491
|
+
if (slot.exprPath && slot.exprPath.length === 1 && slot.exprPath[0] === itemParam)
|
|
1492
|
+
return true;
|
|
1493
|
+
return slot.expr?.trim() === itemParam;
|
|
1494
|
+
}
|
|
1495
|
+
function keyedListPayload(slot) {
|
|
1496
|
+
const payload = slot.payload;
|
|
1497
|
+
if (!payload || typeof payload !== 'object')
|
|
1498
|
+
return null;
|
|
1499
|
+
const record = payload;
|
|
1500
|
+
const itemParam = typeof record.itemParam === 'string' ? record.itemParam : undefined;
|
|
1501
|
+
const rowTemplate = record.rowTemplate && typeof record.rowTemplate === 'object'
|
|
1502
|
+
? record.rowTemplate
|
|
1503
|
+
: null;
|
|
1504
|
+
if (!rowTemplate || typeof rowTemplate.html !== 'string' || !Array.isArray(rowTemplate.slots))
|
|
1505
|
+
return { itemParam };
|
|
1506
|
+
return { itemParam, rowTemplate: { html: rowTemplate.html, slots: rowTemplate.slots } };
|
|
1507
|
+
}
|
|
1508
|
+
function rowExprAsString(expr) {
|
|
1509
|
+
return expr.kind === 'string' ? expr.cpp : `gea_cpp_to_string(${expr.cpp})`;
|
|
1510
|
+
}
|
|
1511
|
+
function rowExprAsBoolean(expr) {
|
|
1512
|
+
if (expr.kind === 'boolean')
|
|
1513
|
+
return expr.cpp;
|
|
1514
|
+
if (expr.kind === 'number')
|
|
1515
|
+
return `(${expr.cpp} != static_cast<double>(0))`;
|
|
1516
|
+
return `(!${expr.cpp}.empty())`;
|
|
1517
|
+
}
|
|
1518
|
+
// Lower a row-template expression over the map callback's item/index params and
|
|
1519
|
+
// literals. Returns null on anything richer — the component then falls off the
|
|
1520
|
+
// typed fast path loudly (no renderer) instead of emitting broken code.
|
|
1521
|
+
function lowerRowExpr(expr, itemParam, indexParam, itemKind, constants = new Map()) {
|
|
1522
|
+
if (!expr)
|
|
1523
|
+
return null;
|
|
1524
|
+
const text = stripOuterParens(expr.trim());
|
|
1525
|
+
const ternary = splitTopLevelTernary(text);
|
|
1526
|
+
if (ternary) {
|
|
1527
|
+
const test = lowerRowExpr(ternary.test, itemParam, indexParam, itemKind, constants);
|
|
1528
|
+
const consequent = lowerRowExpr(ternary.consequent, itemParam, indexParam, itemKind, constants);
|
|
1529
|
+
const alternate = lowerRowExpr(ternary.alternate, itemParam, indexParam, itemKind, constants);
|
|
1530
|
+
if (!test || !consequent || !alternate)
|
|
1531
|
+
return null;
|
|
1532
|
+
const kind = consequent.kind === 'string' || alternate.kind === 'string' ? 'string' : consequent.kind;
|
|
1533
|
+
const branch = (value) => (kind === 'string' ? rowExprAsString(value) : value.cpp);
|
|
1534
|
+
return { cpp: `(${test.cpp} ? ${branch(consequent)} : ${branch(alternate)})`, kind };
|
|
1535
|
+
}
|
|
1536
|
+
const binary = splitTopLevelBinary(text);
|
|
1537
|
+
if (binary) {
|
|
1538
|
+
const left = lowerRowExpr(binary.left, itemParam, indexParam, itemKind, constants);
|
|
1539
|
+
const right = lowerRowExpr(binary.right, itemParam, indexParam, itemKind, constants);
|
|
1540
|
+
if (!left || !right)
|
|
1541
|
+
return null;
|
|
1542
|
+
return { cpp: `(${left.cpp} ${cppBinaryOperator(binary.op)} ${right.cpp})`, kind: 'boolean' };
|
|
1543
|
+
}
|
|
1544
|
+
const plusParts = splitTopLevelOperator(text, '+');
|
|
1545
|
+
if (plusParts.length > 1) {
|
|
1546
|
+
const parts = plusParts.map((part) => lowerRowExpr(part, itemParam, indexParam, itemKind, constants));
|
|
1547
|
+
if (parts.some((part) => !part))
|
|
1548
|
+
return null;
|
|
1549
|
+
const lowered = parts;
|
|
1550
|
+
if (lowered.every((part) => part.kind === 'number')) {
|
|
1551
|
+
return { cpp: `(${lowered.map((part) => part.cpp).join(' + ')})`, kind: 'number' };
|
|
1552
|
+
}
|
|
1553
|
+
return { cpp: `(${lowered.map(rowExprAsString).join(' + ')})`, kind: 'string' };
|
|
1554
|
+
}
|
|
1555
|
+
const string = stringLiteralValue(text);
|
|
1556
|
+
if (string !== null)
|
|
1557
|
+
return { cpp: `std::string(${cppString(string)})`, kind: 'string' };
|
|
1558
|
+
const constant = constants.get(text);
|
|
1559
|
+
if (constant?.valueType === 'string')
|
|
1560
|
+
return { cpp: `std::string(${cppString(constant.value)})`, kind: 'string' };
|
|
1561
|
+
if (constant?.valueType === 'number')
|
|
1562
|
+
return { cpp: `static_cast<double>(${constant.value})`, kind: 'number' };
|
|
1563
|
+
if (constant?.valueType === 'boolean')
|
|
1564
|
+
return { cpp: constant.value, kind: 'boolean' };
|
|
1565
|
+
if (numericLiteralValue(text) !== null)
|
|
1566
|
+
return { cpp: `static_cast<double>(${text})`, kind: 'number' };
|
|
1567
|
+
if (text === 'true' || text === 'false')
|
|
1568
|
+
return { cpp: text, kind: 'boolean' };
|
|
1569
|
+
if (text === itemParam)
|
|
1570
|
+
return { cpp: '__gea_item', kind: itemKind };
|
|
1571
|
+
if (indexParam && text === indexParam)
|
|
1572
|
+
return { cpp: 'static_cast<double>(__gea_index)', kind: 'number' };
|
|
1573
|
+
return null;
|
|
1574
|
+
}
|
|
1575
|
+
function lowerRowPxLengthStyleExpr(expr, propertyName, itemParam, indexParam, itemKind, constants = new Map()) {
|
|
1576
|
+
if (!expr || !isPxLengthStylePropertyName(propertyName))
|
|
1577
|
+
return null;
|
|
1578
|
+
const parts = parseTemplateLiteralParts(stripOuterParens(expr.trim()));
|
|
1579
|
+
if (!parts || parts.length !== 2)
|
|
1580
|
+
return null;
|
|
1581
|
+
if (parts[0].kind !== 'expr' || parts[1].kind !== 'text' || parts[1].value !== 'px')
|
|
1582
|
+
return null;
|
|
1583
|
+
const lowered = lowerRowExpr(parts[0].value, itemParam, indexParam, itemKind, constants);
|
|
1584
|
+
return lowered ? { cpp: rowExprAsNumber(lowered), kind: 'number' } : null;
|
|
1585
|
+
}
|
|
1586
|
+
function lowerRowPercentLengthStyleExpr(expr, propertyName, itemParam, indexParam, itemKind, constants = new Map()) {
|
|
1587
|
+
if (!expr || !stylePercentPropertyEnumForPropertyName(propertyName))
|
|
1588
|
+
return null;
|
|
1589
|
+
const parts = parseTemplateLiteralParts(stripOuterParens(expr.trim()));
|
|
1590
|
+
if (!parts || parts.length !== 2)
|
|
1591
|
+
return null;
|
|
1592
|
+
if (parts[0].kind !== 'expr' || parts[1].kind !== 'text' || parts[1].value !== '%')
|
|
1593
|
+
return null;
|
|
1594
|
+
const lowered = lowerRowExpr(parts[0].value, itemParam, indexParam, itemKind, constants);
|
|
1595
|
+
return lowered ? { cpp: rowExprAsNumber(lowered), kind: 'number', styleUnit: 'percent' } : null;
|
|
1596
|
+
}
|
|
1597
|
+
function lowerRowRotateStyleExpr(expr, propertyName, itemParam, indexParam, itemKind, constants = new Map()) {
|
|
1598
|
+
if (!expr || !isRotateTransformPropertyName(propertyName))
|
|
1599
|
+
return null;
|
|
1600
|
+
const parts = parseTemplateLiteralParts(stripOuterParens(expr.trim()));
|
|
1601
|
+
const degrees = rotateDegreesTemplateExpression(parts, propertyName);
|
|
1602
|
+
if (!degrees)
|
|
1603
|
+
return null;
|
|
1604
|
+
const lowered = lowerRowExpr(degrees, itemParam, indexParam, itemKind, constants);
|
|
1605
|
+
return lowered ? { cpp: rowExprAsNumber(lowered), kind: 'number' } : null;
|
|
1606
|
+
}
|
|
1607
|
+
function lowerRowOpaqueColorStyleExpr(expr, propertyName, itemParam, indexParam, itemKind, constants = new Map()) {
|
|
1608
|
+
if (!expr || !styleOpaqueColorTargetForPropertyName(propertyName))
|
|
1609
|
+
return null;
|
|
1610
|
+
const text = stripOuterParens(expr.trim());
|
|
1611
|
+
const literal = opaqueHexColorExpressionForStyleValue(text, constants, new Map());
|
|
1612
|
+
if (literal)
|
|
1613
|
+
return { cpp: literal, kind: 'number', styleUnit: 'color' };
|
|
1614
|
+
const ternary = splitTopLevelTernary(text);
|
|
1615
|
+
if (!ternary)
|
|
1616
|
+
return null;
|
|
1617
|
+
const test = lowerRowExpr(ternary.test, itemParam, indexParam, itemKind, constants);
|
|
1618
|
+
const consequent = opaqueHexColorExpressionForStyleValue(ternary.consequent, constants, new Map());
|
|
1619
|
+
const alternate = opaqueHexColorExpressionForStyleValue(ternary.alternate, constants, new Map());
|
|
1620
|
+
if (!test || !consequent || !alternate)
|
|
1621
|
+
return null;
|
|
1622
|
+
return {
|
|
1623
|
+
cpp: `(${rowExprAsBoolean(test)} ? ${consequent} : ${alternate})`,
|
|
1624
|
+
kind: 'number',
|
|
1625
|
+
styleUnit: 'color',
|
|
1626
|
+
};
|
|
1627
|
+
}
|
|
1628
|
+
function lowerRowKeywordStyleExpr(expr, propertyName, itemParam, indexParam, itemKind, constants = new Map()) {
|
|
1629
|
+
if (!expr || !styleKeywordPropertiesForPropertyName(propertyName))
|
|
1630
|
+
return null;
|
|
1631
|
+
const text = stripOuterParens(expr.trim());
|
|
1632
|
+
const literal = keywordCommonValue(keywordEntriesForExpr(propertyName, text, constants, new Map()));
|
|
1633
|
+
if (literal !== null)
|
|
1634
|
+
return { cpp: String(literal), kind: 'number', styleUnit: 'keyword' };
|
|
1635
|
+
const ternary = splitTopLevelTernary(text);
|
|
1636
|
+
if (!ternary)
|
|
1637
|
+
return null;
|
|
1638
|
+
const test = lowerRowExpr(ternary.test, itemParam, indexParam, itemKind, constants);
|
|
1639
|
+
const consequent = keywordEntriesForExpr(propertyName, ternary.consequent, constants, new Map());
|
|
1640
|
+
const alternate = keywordEntriesForExpr(propertyName, ternary.alternate, constants, new Map());
|
|
1641
|
+
const consequentValue = keywordCommonValue(consequent);
|
|
1642
|
+
const alternateValue = keywordCommonValue(alternate);
|
|
1643
|
+
if (!test || consequentValue === null || alternateValue === null || !keywordEntriesCompatible(consequent, alternate))
|
|
1644
|
+
return null;
|
|
1645
|
+
return {
|
|
1646
|
+
cpp: `(${rowExprAsBoolean(test)} ? ${consequentValue} : ${alternateValue})`,
|
|
1647
|
+
kind: 'number',
|
|
1648
|
+
styleUnit: 'keyword',
|
|
1649
|
+
};
|
|
1650
|
+
}
|
|
1651
|
+
function rowExprAsNumber(expr) {
|
|
1652
|
+
return expr.kind === 'number' ? expr.cpp : `gea::runtime::coerce::to_number(${expr.cpp})`;
|
|
1653
|
+
}
|
|
1654
|
+
// The element a row slot targets, as a dotted path of element-child indices
|
|
1655
|
+
// (mirrors how the main tree keys pathVars). A trailing {child:N} (text slots)
|
|
1656
|
+
// belongs to the containing element.
|
|
1657
|
+
function rowSlotElemPath(slot) {
|
|
1658
|
+
const kinds = slot.walkKinds;
|
|
1659
|
+
if (kinds)
|
|
1660
|
+
return kinds.filter((kind) => 'elem' in kind).map((kind) => kind.elem).join('.');
|
|
1661
|
+
const walk = slot.kind === 'text' ? slot.walk.slice(0, -1) : slot.walk;
|
|
1662
|
+
return walk.join('.');
|
|
1663
|
+
}
|
|
1664
|
+
function typedRowEventType(slot) {
|
|
1665
|
+
const attrName = slotAttrName(slot);
|
|
1666
|
+
if (!attrName)
|
|
1667
|
+
return null;
|
|
1668
|
+
const map = { onclick: 'click' };
|
|
1669
|
+
return map[attrName.toLowerCase()] ?? null;
|
|
1670
|
+
}
|
|
1671
|
+
// Lower `() => this.<method>(<item/index/literal args>)` to a typed call.
|
|
1672
|
+
function lowerRowEventCall(expr, itemParam, indexParam, itemKind) {
|
|
1673
|
+
if (!expr)
|
|
1674
|
+
return null;
|
|
1675
|
+
const arrow = expr.trim().match(/^\(\s*\)\s*=>\s*([\s\S]+)$/);
|
|
1676
|
+
if (!arrow)
|
|
1677
|
+
return null;
|
|
1678
|
+
let body = arrow[1].trim();
|
|
1679
|
+
if (body.startsWith('{') && body.endsWith('}'))
|
|
1680
|
+
body = body.slice(1, -1).trim();
|
|
1681
|
+
body = body.replace(/;$/, '').trim();
|
|
1682
|
+
const call = body.match(/^this\.([A-Za-z_$][A-Za-z0-9_$]*)\(([\s\S]*)\)$/);
|
|
1683
|
+
if (!call || body.includes(';'))
|
|
1684
|
+
return null;
|
|
1685
|
+
const args = splitTopLevelOperator(call[2], ',').map((arg) => arg.trim()).filter((arg) => arg.length > 0);
|
|
1686
|
+
const lowered = [];
|
|
1687
|
+
for (const arg of args) {
|
|
1688
|
+
const value = lowerRowExpr(arg, itemParam, indexParam, itemKind);
|
|
1689
|
+
if (!value)
|
|
1690
|
+
return null;
|
|
1691
|
+
lowered.push(value.cpp);
|
|
1692
|
+
}
|
|
1693
|
+
return `(void)store->${sanitizeCppIdentifier(call[1])}(${lowered.join(', ')});`;
|
|
1694
|
+
}
|
|
1695
|
+
function emitTypedKeyedListSlot(lines, target, slot, component, constants, disposerVar) {
|
|
1696
|
+
const member = (slot.expr ?? '').trim().match(/^this\.([A-Za-z_$][A-Za-z0-9_$]*)$/);
|
|
1697
|
+
if (!member)
|
|
1698
|
+
return false;
|
|
1699
|
+
const arrayField = reactiveArrayFieldInfo(component).find((field) => field.name === member[1]);
|
|
1700
|
+
if (!arrayField)
|
|
1701
|
+
return false;
|
|
1702
|
+
const payload = slot.payload;
|
|
1703
|
+
const itemParam = payload?.itemParam;
|
|
1704
|
+
const indexParam = payload?.indexParam;
|
|
1705
|
+
const rowTemplate = payload?.rowTemplate;
|
|
1706
|
+
if (!itemParam || !rowTemplate)
|
|
1707
|
+
return false;
|
|
1708
|
+
const rowRoot = parseTemplateRoot(rowTemplate.html);
|
|
1709
|
+
if (!rowRoot)
|
|
1710
|
+
return false;
|
|
1711
|
+
const itemKind = arrayField.elementValueType;
|
|
1712
|
+
const fieldName = sanitizeCppIdentifier(arrayField.name);
|
|
1713
|
+
const rev = arrayRevFieldName(arrayField.name);
|
|
1714
|
+
// Group row slots by the element they target; collect root events separately.
|
|
1715
|
+
const slotsByPath = new Map();
|
|
1716
|
+
const rootEvents = [];
|
|
1717
|
+
for (const rowSlot of rowTemplate.slots) {
|
|
1718
|
+
if (rowSlot.kind === 'event') {
|
|
1719
|
+
if (rowSlotElemPath(rowSlot) !== '')
|
|
1720
|
+
return false; // events on row root only (v1)
|
|
1721
|
+
const type = typedRowEventType(rowSlot);
|
|
1722
|
+
const call = lowerRowEventCall(rowSlot.expr, itemParam, indexParam, itemKind);
|
|
1723
|
+
if (!type || !call)
|
|
1724
|
+
return false;
|
|
1725
|
+
rootEvents.push({ type, call });
|
|
1726
|
+
continue;
|
|
1727
|
+
}
|
|
1728
|
+
if (rowSlot.kind !== 'style' && rowSlot.kind !== 'text' && rowSlot.kind !== 'class')
|
|
1729
|
+
return false;
|
|
1730
|
+
const key = rowSlotElemPath(rowSlot);
|
|
1731
|
+
const bucket = slotsByPath.get(key) ?? [];
|
|
1732
|
+
bucket.push(rowSlot);
|
|
1733
|
+
slotsByPath.set(key, bucket);
|
|
1734
|
+
}
|
|
1735
|
+
// Build the row subtree (recursive over elements; bounded row shapes).
|
|
1736
|
+
const rowLines = [];
|
|
1737
|
+
let rowVarCount = 0;
|
|
1738
|
+
const nextRowVar = () => `__gea_kl_n${rowVarCount++}`;
|
|
1739
|
+
const emitRowElement = (node, varName, elemPath) => {
|
|
1740
|
+
rowLines.push(`auto ${varName} = ${uiCreateNodeExpression(node) ?? 'gea::embedded::ui::Document::instance().createView()'};`);
|
|
1741
|
+
rowLines.push(`${varName}.setTagName(${cppString(node.tag.toLowerCase())});`);
|
|
1742
|
+
if (node.className)
|
|
1743
|
+
rowLines.push(`${varName}.classList().set(${cppString(node.className)});`);
|
|
1744
|
+
const elementSlots = slotsByPath.get(elemPath) ?? [];
|
|
1745
|
+
const hasDynamicTextSlot = elementSlots.some((rowSlot) => rowSlot.kind === 'text');
|
|
1746
|
+
for (const rowSlot of elementSlots) {
|
|
1747
|
+
if (rowSlot.kind === 'style') {
|
|
1748
|
+
for (const styleField of rowSlot.exprObjectFields ?? []) {
|
|
1749
|
+
const property = cssPropertyName(styleField.name);
|
|
1750
|
+
const lowered = lowerRowRotateStyleExpr(styleField.expr, property, itemParam, indexParam, itemKind, constants) ??
|
|
1751
|
+
lowerRowPxLengthStyleExpr(styleField.expr, property, itemParam, indexParam, itemKind, constants) ??
|
|
1752
|
+
lowerRowPercentLengthStyleExpr(styleField.expr, property, itemParam, indexParam, itemKind, constants) ??
|
|
1753
|
+
lowerRowOpaqueColorStyleExpr(styleField.expr, property, itemParam, indexParam, itemKind, constants) ??
|
|
1754
|
+
lowerRowKeywordStyleExpr(styleField.expr, property, itemParam, indexParam, itemKind, constants) ??
|
|
1755
|
+
lowerRowExpr(styleField.expr, itemParam, indexParam, itemKind, constants);
|
|
1756
|
+
if (!lowered)
|
|
1757
|
+
return false;
|
|
1758
|
+
const propertyArg = stylePropertyArgument(property);
|
|
1759
|
+
const literalString = stringLiteralValue(styleField.expr);
|
|
1760
|
+
const directStringLines = literalString !== null ? directStringStyleApplyLines(varName, property, literalString) : null;
|
|
1761
|
+
const numberLine = lowered.kind === 'number'
|
|
1762
|
+
? styleNumberApplyLine(varName, property, lowered.cpp, lowered.styleUnit) ??
|
|
1763
|
+
`gea::embedded::ui::StyleSheet::instance().applyNumberProperty(${varName}, ${propertyArg}, static_cast<double>(${lowered.cpp}));`
|
|
1764
|
+
: null;
|
|
1765
|
+
if (directStringLines)
|
|
1766
|
+
rowLines.push(...directStringLines);
|
|
1767
|
+
else
|
|
1768
|
+
rowLines.push(lowered.kind === 'number'
|
|
1769
|
+
? numberLine
|
|
1770
|
+
: `gea::embedded::ui::StyleSheet::instance().applyProperty(${varName}, ${propertyArg}, ${rowExprAsString(lowered)});`);
|
|
1771
|
+
}
|
|
1772
|
+
continue;
|
|
1773
|
+
}
|
|
1774
|
+
if (rowSlot.kind === 'class') {
|
|
1775
|
+
const lowered = lowerRowExpr(rowSlot.expr, itemParam, indexParam, itemKind, constants);
|
|
1776
|
+
if (!lowered)
|
|
1777
|
+
return false;
|
|
1778
|
+
rowLines.push(`${varName}.classList().set(${rowExprAsString(lowered)});`);
|
|
1779
|
+
continue;
|
|
1780
|
+
}
|
|
1781
|
+
// text
|
|
1782
|
+
const lowered = lowerRowExpr(rowSlot.expr, itemParam, indexParam, itemKind, constants);
|
|
1783
|
+
if (!lowered)
|
|
1784
|
+
return false;
|
|
1785
|
+
rowLines.push(`${varName}.setText((${rowExprAsString(lowered)}).c_str());`);
|
|
1786
|
+
}
|
|
1787
|
+
let elemChildIndex = 0;
|
|
1788
|
+
for (const child of node.children) {
|
|
1789
|
+
if (child.kind === 'element') {
|
|
1790
|
+
const childVar = nextRowVar();
|
|
1791
|
+
if (!emitRowElement(child, childVar, elemPath === '' ? String(elemChildIndex) : `${elemPath}.${elemChildIndex}`))
|
|
1792
|
+
return false;
|
|
1793
|
+
rowLines.push(`${varName}.appendChild(${childVar});`);
|
|
1794
|
+
elemChildIndex += 1;
|
|
1795
|
+
continue;
|
|
1796
|
+
}
|
|
1797
|
+
if (child.kind === 'text') {
|
|
1798
|
+
// A zero is an IR placeholder only when this element actually owns a
|
|
1799
|
+
// dynamic text slot. Otherwise it is authored content and must render.
|
|
1800
|
+
const text = renderableTemplateText(child.text);
|
|
1801
|
+
if (text && (text !== '0' || !hasDynamicTextSlot))
|
|
1802
|
+
rowLines.push(`${varName}.setText(${cppString(text)});`);
|
|
1803
|
+
}
|
|
1804
|
+
}
|
|
1805
|
+
return true;
|
|
1806
|
+
};
|
|
1807
|
+
if (!emitRowElement(rowRoot, '__gea_kl_row', ''))
|
|
1808
|
+
return false;
|
|
1809
|
+
const rows = `__gea_kl_rows_${slot.index}`;
|
|
1810
|
+
const rebuild = `__gea_kl_rebuild_${slot.index}`;
|
|
1811
|
+
lines.push(` auto ${rows} = std::make_shared<std::vector<gea::embedded::ui::NodeHandle>>();`);
|
|
1812
|
+
lines.push(` auto ${rebuild} = [${target}, store, ${rows}]() mutable -> void {`);
|
|
1813
|
+
lines.push(` for (auto &__gea_kl_old : *${rows}) __gea_kl_old.remove();`);
|
|
1814
|
+
lines.push(` ${rows}->clear();`);
|
|
1815
|
+
lines.push(` const auto &__gea_items = store->${fieldName};`);
|
|
1816
|
+
lines.push(` for (std::size_t __gea_index = 0; __gea_index < __gea_items.size(); ++__gea_index) {`);
|
|
1817
|
+
lines.push(` const auto &__gea_item = __gea_items[__gea_index];`);
|
|
1818
|
+
lines.push(` (void)__gea_item;`);
|
|
1819
|
+
for (const line of rowLines)
|
|
1820
|
+
lines.push(` ${line}`);
|
|
1821
|
+
lines.push(` ${target}.appendChild(__gea_kl_row);`);
|
|
1822
|
+
lines.push(` ${rows}->push_back(__gea_kl_row);`);
|
|
1823
|
+
lines.push(` }`);
|
|
1824
|
+
lines.push(` };`);
|
|
1825
|
+
lines.push(` ${rebuild}();`);
|
|
1826
|
+
lines.push(` store->${rev}.subscribe(${rebuild});`);
|
|
1827
|
+
for (const [eventIndex, event] of rootEvents.entries()) {
|
|
1828
|
+
const listenerToken = `__gea_kl_event_listener_${slot.index}_${eventIndex}`;
|
|
1829
|
+
lines.push(` auto ${listenerToken} = gea::embedded::ui::Document::instance().body().addEventListener(${cppString(event.type)}, [store, ${rows}](gea::framework::events::PointerEvent &event) mutable {`);
|
|
1830
|
+
lines.push(` for (std::size_t __gea_index = 0; __gea_index < ${rows}->size(); ++__gea_index) {`);
|
|
1831
|
+
lines.push(` if (!gea::embedded::ui::Tree::instance().containsNode((*${rows})[__gea_index].id(), event.targetId)) continue;`);
|
|
1832
|
+
lines.push(` const auto &__gea_item = store->${fieldName}[__gea_index];`);
|
|
1833
|
+
lines.push(` (void)__gea_item;`);
|
|
1834
|
+
lines.push(` ${event.call}`);
|
|
1835
|
+
lines.push(` return;`);
|
|
1836
|
+
lines.push(` }`);
|
|
1837
|
+
lines.push(` });`);
|
|
1838
|
+
lines.push(` if (${disposerVar}) {`);
|
|
1839
|
+
lines.push(` ${disposerVar}->add([${listenerToken}]() -> void {`);
|
|
1840
|
+
lines.push(` gea::embedded::ui::Document::instance().body().removeEventListener(${cppString(event.type)}, ${listenerToken});`);
|
|
1841
|
+
lines.push(` });`);
|
|
1842
|
+
lines.push(` }`);
|
|
1843
|
+
}
|
|
1844
|
+
return true;
|
|
1845
|
+
}
|
|
1846
|
+
function staticStringArrayValues(expr) {
|
|
1847
|
+
if (!expr)
|
|
1848
|
+
return null;
|
|
1849
|
+
const text = stripOuterParens(expr.trim());
|
|
1850
|
+
if (!text.startsWith('[') || !text.endsWith(']'))
|
|
1851
|
+
return null;
|
|
1852
|
+
const inner = text.slice(1, -1).trim();
|
|
1853
|
+
if (!inner)
|
|
1854
|
+
return [];
|
|
1855
|
+
const values = [];
|
|
1856
|
+
for (const part of splitTopLevelOperator(inner, ',')) {
|
|
1857
|
+
const value = stringLiteralValue(part);
|
|
1858
|
+
if (value === null)
|
|
1859
|
+
return null;
|
|
1860
|
+
values.push(value);
|
|
1861
|
+
}
|
|
1862
|
+
return values;
|
|
1863
|
+
}
|
|
1864
|
+
function resolveRootSetupPropSlots(slots, propBindings, constants) {
|
|
1865
|
+
if (propBindings.size === 0)
|
|
1866
|
+
return slots;
|
|
1867
|
+
let changed = false;
|
|
1868
|
+
const next = slots.map((slot) => {
|
|
1869
|
+
if (!isRootSetupSlot(slot, constants))
|
|
1870
|
+
return slot;
|
|
1871
|
+
let expr = slot.expr;
|
|
1872
|
+
let exprObjectFields = slot.exprObjectFields;
|
|
1873
|
+
if (expr) {
|
|
1874
|
+
const resolved = resolveLiteralProps(expr, propBindings);
|
|
1875
|
+
if (resolved !== expr) {
|
|
1876
|
+
expr = resolved;
|
|
1877
|
+
changed = true;
|
|
1878
|
+
}
|
|
1879
|
+
}
|
|
1880
|
+
if (exprObjectFields) {
|
|
1881
|
+
const fields = exprObjectFields.map((field) => {
|
|
1882
|
+
const resolved = resolveLiteralProps(field.expr, propBindings);
|
|
1883
|
+
if (resolved === field.expr)
|
|
1884
|
+
return field;
|
|
1885
|
+
changed = true;
|
|
1886
|
+
return { ...field, expr: resolved };
|
|
1887
|
+
});
|
|
1888
|
+
exprObjectFields = fields;
|
|
1889
|
+
}
|
|
1890
|
+
return { ...slot, ...(expr !== slot.expr ? { expr } : {}), ...(exprObjectFields !== slot.exprObjectFields ? { exprObjectFields } : {}) };
|
|
1891
|
+
});
|
|
1892
|
+
return changed ? next : slots;
|
|
1893
|
+
}
|
|
1894
|
+
function resolveLiteralProps(expr, propBindings) {
|
|
1895
|
+
let out = expr;
|
|
1896
|
+
for (const name of propBindings.keys()) {
|
|
1897
|
+
const value = literalPropExpression(name, propBindings);
|
|
1898
|
+
if (value === null)
|
|
1899
|
+
continue;
|
|
1900
|
+
const escaped = escapeRegExp(name);
|
|
1901
|
+
const pattern = new RegExp(`\\bthis\\.props\\.${escaped}\\b|\\bprops\\.${escaped}\\b|(?<!\\.)\\b${escaped}\\b`, 'g');
|
|
1902
|
+
out = out.replace(pattern, value);
|
|
1903
|
+
}
|
|
1904
|
+
return out;
|
|
1905
|
+
}
|
|
1906
|
+
function literalPropExpression(name, propBindings, seenProps = new Set()) {
|
|
1907
|
+
const binding = propBindings.get(name);
|
|
1908
|
+
if (!binding || seenProps.has(name))
|
|
1909
|
+
return null;
|
|
1910
|
+
const expr = stripOuterParens(binding.expr.trim());
|
|
1911
|
+
if (stringLiteralValue(expr) !== null || numericLiteralValue(expr) !== null || expr === 'true' || expr === 'false')
|
|
1912
|
+
return expr;
|
|
1913
|
+
// Arrow function passed as a prop (e.g. `onClick={() => Settings.foo()}`).
|
|
1914
|
+
// Wrapper components like `<View>` forward those via `props.onClick`, and
|
|
1915
|
+
// `lowerRootEvent` needs an actual arrow expression to parse — substitute
|
|
1916
|
+
// the literal directly so the event lowering pipeline sees the same shape
|
|
1917
|
+
// it would have if the user wrote the handler inline on the wrapper's
|
|
1918
|
+
// root element.
|
|
1919
|
+
if (isArrowFunctionExpression(expr))
|
|
1920
|
+
return expr;
|
|
1921
|
+
const prop = propNameForExpression(expr);
|
|
1922
|
+
if (!prop)
|
|
1923
|
+
return null;
|
|
1924
|
+
const nextSeen = new Set(seenProps);
|
|
1925
|
+
nextSeen.add(name);
|
|
1926
|
+
return literalPropExpression(prop, binding.parentProps, nextSeen);
|
|
1927
|
+
}
|
|
1928
|
+
function isArrowFunctionExpression(expr) {
|
|
1929
|
+
// Cheap detection: arrow params either as `()`, `(x)`, `(x, y, …)`, or a
|
|
1930
|
+
// single bare identifier — followed by `=>`. Anything more exotic
|
|
1931
|
+
// (destructured params, default values) falls through to the next branch
|
|
1932
|
+
// and is left to the caller's existing handling.
|
|
1933
|
+
return /^(?:\([^)]*\)|[A-Za-z_$][A-Za-z0-9_$]*)\s*=>/.test(expr);
|
|
1934
|
+
}
|
|
1935
|
+
function canLowerTemplateClassSlot(slot, storeFields, constants, propBindings) {
|
|
1936
|
+
const classObject = lowerTemplateClassObject(slot.expr, storeFields, constants, propBindings);
|
|
1937
|
+
if (classObject)
|
|
1938
|
+
return !classObject.lowered.fields.some((field) => !field.readerName);
|
|
1939
|
+
const lowered = lowerTemplateExpression(slot.expr, storeFields, constants, propBindings);
|
|
1940
|
+
return !!lowered && !lowered.fields.some((field) => !field.readerName);
|
|
1941
|
+
}
|
|
1942
|
+
function emitTemplateClassSlot(lines, target, slot, storeFields, constants, propBindings, fieldAccess = BOXED_FIELD_ACCESS, disposerVar = 'disposer') {
|
|
1943
|
+
const classObject = lowerTemplateClassObject(slot.expr, storeFields, constants, propBindings);
|
|
1944
|
+
if (classObject) {
|
|
1945
|
+
if (classObject.lowered.fields.some((field) => !field.readerName))
|
|
1946
|
+
return false;
|
|
1947
|
+
emitReactiveApply(lines, target, classObject.lowered, [
|
|
1948
|
+
'{',
|
|
1949
|
+
'std::string __gea_class_name;',
|
|
1950
|
+
...classObject.entries.map((entry) => `if (${loweredAsBoolean(entry.condition)}) { if (!__gea_class_name.empty()) __gea_class_name.push_back(' '); __gea_class_name.append(${cppString(entry.className)}); }`),
|
|
1951
|
+
`${target}.classList().set(__gea_class_name);`,
|
|
1952
|
+
'}',
|
|
1953
|
+
], fieldAccess, [], disposerVar);
|
|
1954
|
+
return true;
|
|
1955
|
+
}
|
|
1956
|
+
const lowered = lowerTemplateExpression(slot.expr, storeFields, constants, propBindings);
|
|
1957
|
+
if (!lowered || lowered.fields.some((field) => !field.readerName))
|
|
1958
|
+
return false;
|
|
1959
|
+
emitReactiveApply(lines, target, lowered, `${target}.classList().set((${loweredAsString(lowered)}));`, fieldAccess, [], disposerVar);
|
|
1960
|
+
return true;
|
|
1961
|
+
}
|
|
1962
|
+
function lowerTemplateClassObject(expr, storeFields, constants, propBindings) {
|
|
1963
|
+
if (!expr)
|
|
1964
|
+
return null;
|
|
1965
|
+
const text = stripOuterParens(expr.trim());
|
|
1966
|
+
if (!text.startsWith('{') || !text.endsWith('}'))
|
|
1967
|
+
return null;
|
|
1968
|
+
const inner = text.slice(1, -1).trim();
|
|
1969
|
+
if (!inner) {
|
|
1970
|
+
return {
|
|
1971
|
+
lowered: { kind: 'boolean', expr: 'true', deps: [], fields: [] },
|
|
1972
|
+
entries: [],
|
|
1973
|
+
};
|
|
1974
|
+
}
|
|
1975
|
+
const entries = [];
|
|
1976
|
+
for (const part of splitTopLevelOperator(inner, ',')) {
|
|
1977
|
+
if (part.startsWith('...'))
|
|
1978
|
+
return null;
|
|
1979
|
+
const colon = findTopLevelOperator(part, ':');
|
|
1980
|
+
if (colon < 0)
|
|
1981
|
+
return null;
|
|
1982
|
+
const key = classObjectKeyName(part.slice(0, colon).trim());
|
|
1983
|
+
if (!key)
|
|
1984
|
+
return null;
|
|
1985
|
+
const condition = lowerTemplateExpression(part.slice(colon + 1).trim(), storeFields, constants, propBindings);
|
|
1986
|
+
if (!condition)
|
|
1987
|
+
return null;
|
|
1988
|
+
entries.push({ className: key, condition });
|
|
1989
|
+
}
|
|
1990
|
+
return {
|
|
1991
|
+
lowered: mergeLowered('boolean', 'true', entries.map((entry) => entry.condition)),
|
|
1992
|
+
entries,
|
|
1993
|
+
};
|
|
1994
|
+
}
|
|
1995
|
+
function classObjectKeyName(expr) {
|
|
1996
|
+
const string = stringLiteralValue(expr);
|
|
1997
|
+
if (string !== null)
|
|
1998
|
+
return string;
|
|
1999
|
+
return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(expr) ? expr : null;
|
|
2000
|
+
}
|
|
2001
|
+
function canLowerTemplateStyleSlot(slot, storeFields, constants, propBindings) {
|
|
2002
|
+
const fields = styleObjectFieldsForSlot(slot, propBindings);
|
|
2003
|
+
if (!fields || fields.length === 0)
|
|
2004
|
+
return false;
|
|
2005
|
+
for (const field of fields) {
|
|
2006
|
+
const propertyName = cssPropertyName(field.name);
|
|
2007
|
+
const lowered = lowerPxLengthStyleValue(field.expr, propertyName, storeFields, constants, propBindings) ??
|
|
2008
|
+
lowerPercentLengthStyleValue(field.expr, propertyName, storeFields, constants, propBindings) ??
|
|
2009
|
+
lowerRotateStyleDegrees(field.expr, propertyName, storeFields, constants, propBindings) ??
|
|
2010
|
+
lowerOpaqueColorStyleValue(field.expr, propertyName, storeFields, constants, propBindings) ??
|
|
2011
|
+
lowerKeywordStyleValue(field.expr, propertyName, storeFields, constants, propBindings) ??
|
|
2012
|
+
lowerTemplateExpression(field.expr, storeFields, constants, propBindings);
|
|
2013
|
+
if (!lowered || lowered.fields.some((storeField) => !storeField.readerName))
|
|
2014
|
+
return false;
|
|
2015
|
+
}
|
|
2016
|
+
return true;
|
|
2017
|
+
}
|
|
2018
|
+
function emitTemplateStyleSlot(lines, target, slot, storeFields, constants, propBindings, fieldAccess = BOXED_FIELD_ACCESS, disposerVar = 'disposer') {
|
|
2019
|
+
const fields = styleObjectFieldsForSlot(slot, propBindings);
|
|
2020
|
+
if (!fields || fields.length === 0)
|
|
2021
|
+
return false;
|
|
2022
|
+
const loweredFields = [];
|
|
2023
|
+
for (const field of fields) {
|
|
2024
|
+
const propertyName = cssPropertyName(field.name);
|
|
2025
|
+
const lowered = lowerPxLengthStyleValue(field.expr, propertyName, storeFields, constants, propBindings) ??
|
|
2026
|
+
lowerPercentLengthStyleValue(field.expr, propertyName, storeFields, constants, propBindings) ??
|
|
2027
|
+
lowerRotateStyleDegrees(field.expr, propertyName, storeFields, constants, propBindings) ??
|
|
2028
|
+
lowerOpaqueColorStyleValue(field.expr, propertyName, storeFields, constants, propBindings) ??
|
|
2029
|
+
lowerKeywordStyleValue(field.expr, propertyName, storeFields, constants, propBindings) ??
|
|
2030
|
+
lowerTemplateExpression(field.expr, storeFields, constants, propBindings);
|
|
2031
|
+
if (!lowered || lowered.fields.some((storeField) => !storeField.readerName))
|
|
2032
|
+
return false;
|
|
2033
|
+
const property = stylePropertyArgument(propertyName);
|
|
2034
|
+
const literalString = staticStringStyleValue(field.expr, constants, propBindings);
|
|
2035
|
+
const directStringLines = literalString !== null ? directStringStyleApplyLines(target, propertyName, literalString) : null;
|
|
2036
|
+
const bodyLine = directStringLines ??
|
|
2037
|
+
(lowered.kind === 'number'
|
|
2038
|
+
? styleNumberApplyLine(target, propertyName, loweredAsKind(lowered, 'number'), lowered.styleUnit) ??
|
|
2039
|
+
`gea::embedded::ui::StyleSheet::instance().applyNumberProperty(${target}, ${property}, static_cast<double>(${loweredAsKind(lowered, 'number')}));`
|
|
2040
|
+
: `gea::embedded::ui::StyleSheet::instance().applyProperty(${target}, ${property}, (${loweredAsString(lowered)}));`);
|
|
2041
|
+
loweredFields.push({ lowered, bodyLines: Array.isArray(bodyLine) ? bodyLine : [bodyLine] });
|
|
2042
|
+
}
|
|
2043
|
+
const staticFields = loweredFields.filter((item) => item.lowered.deps.length === 0 && item.lowered.fields.length === 0);
|
|
2044
|
+
const dynamicFields = loweredFields.filter((item) => item.lowered.deps.length > 0 || item.lowered.fields.length > 0);
|
|
2045
|
+
for (const item of staticFields) {
|
|
2046
|
+
for (const line of item.bodyLines)
|
|
2047
|
+
lines.push(` ${line}`);
|
|
2048
|
+
}
|
|
2049
|
+
if (canCombineStyleLowerings(dynamicFields)) {
|
|
2050
|
+
const combined = mergeLowered('boolean', 'true', dynamicFields.map((item) => item.lowered));
|
|
2051
|
+
emitReactiveApply(lines, target, combined, dynamicFields.flatMap((item) => item.bodyLines), fieldAccess, [], disposerVar);
|
|
2052
|
+
return true;
|
|
2053
|
+
}
|
|
2054
|
+
for (const item of dynamicFields) {
|
|
2055
|
+
emitReactiveApply(lines, target, item.lowered, item.bodyLines, fieldAccess, [], disposerVar);
|
|
2056
|
+
}
|
|
2057
|
+
return true;
|
|
2058
|
+
}
|
|
2059
|
+
function canCombineStyleLowerings(items) {
|
|
2060
|
+
const fields = uniqueStoreFields(items.flatMap((item) => item.lowered.fields));
|
|
2061
|
+
const locals = new Set();
|
|
2062
|
+
for (const field of fields) {
|
|
2063
|
+
const local = storeFieldLocalName(field.fieldName);
|
|
2064
|
+
if (locals.has(local))
|
|
2065
|
+
return false;
|
|
2066
|
+
locals.add(local);
|
|
2067
|
+
}
|
|
2068
|
+
return true;
|
|
2069
|
+
}
|
|
2070
|
+
function styleObjectFieldsForSlot(slot, propBindings) {
|
|
2071
|
+
if (slot.exprObjectFields && slot.exprObjectFields.length > 0)
|
|
2072
|
+
return slot.exprObjectFields;
|
|
2073
|
+
const prop = propNameForExpression(stripOuterParens(slot.expr?.trim() ?? ''));
|
|
2074
|
+
const binding = prop ? propBindings.get(prop) : null;
|
|
2075
|
+
if (!binding)
|
|
2076
|
+
return null;
|
|
2077
|
+
return parseExpressionObjectFields(resolveLiteralProps(binding.expr, binding.parentProps));
|
|
2078
|
+
}
|
|
2079
|
+
function parseExpressionObjectFields(expr) {
|
|
2080
|
+
const text = stripOuterParens(expr.trim());
|
|
2081
|
+
if (!text.startsWith('{') || !text.endsWith('}'))
|
|
2082
|
+
return null;
|
|
2083
|
+
const inner = text.slice(1, -1).trim();
|
|
2084
|
+
if (!inner)
|
|
2085
|
+
return [];
|
|
2086
|
+
const fields = [];
|
|
2087
|
+
for (const part of splitTopLevelOperator(inner, ',')) {
|
|
2088
|
+
const colon = findTopLevelOperator(part, ':');
|
|
2089
|
+
if (colon < 0)
|
|
2090
|
+
return null;
|
|
2091
|
+
const name = classObjectKeyName(part.slice(0, colon).trim());
|
|
2092
|
+
if (!name)
|
|
2093
|
+
return null;
|
|
2094
|
+
fields.push({ name, expr: part.slice(colon + 1).trim() });
|
|
2095
|
+
}
|
|
2096
|
+
return fields;
|
|
2097
|
+
}
|
|
2098
|
+
function canEmitTemplateAttrSlot(slot, storeFields, constants, propBindings) {
|
|
2099
|
+
if (attributeSlotLines('root', slot))
|
|
2100
|
+
return true;
|
|
2101
|
+
const attrName = slotAttrName(slot);
|
|
2102
|
+
if (!attrName)
|
|
2103
|
+
return false;
|
|
2104
|
+
const lowered = lowerTemplateExpression(slot.expr, storeFields, constants, propBindings);
|
|
2105
|
+
return !!lowered && !lowered.fields.some((field) => !field.readerName);
|
|
2106
|
+
}
|
|
2107
|
+
function emitTemplateAttrSlot(lines, target, slot, storeFields, constants, propBindings, fieldAccess = BOXED_FIELD_ACCESS, disposerVar = 'disposer') {
|
|
2108
|
+
const attrName = slotAttrName(slot);
|
|
2109
|
+
if (!attrName)
|
|
2110
|
+
return false;
|
|
2111
|
+
const lowered = lowerTemplateExpression(slot.expr, storeFields, constants, propBindings);
|
|
2112
|
+
if (!lowered || lowered.fields.some((field) => !field.readerName))
|
|
2113
|
+
return false;
|
|
2114
|
+
emitReactiveApply(lines, target, lowered, `${target}.setAttribute(${cppString(attrName)}, (${loweredAsString(lowered)}).c_str());`, fieldAccess, [], disposerVar);
|
|
2115
|
+
return true;
|
|
2116
|
+
}
|
|
2117
|
+
function lowerPxLengthStyleValue(expr, propertyName, storeFields, constants, propBindings) {
|
|
2118
|
+
if (!expr || !isPxLengthStylePropertyName(propertyName))
|
|
2119
|
+
return null;
|
|
2120
|
+
const parts = parseTemplateLiteralParts(stripOuterParens(expr.trim()));
|
|
2121
|
+
if (!parts || parts.length !== 2)
|
|
2122
|
+
return null;
|
|
2123
|
+
if (parts[0].kind !== 'expr' || parts[1].kind !== 'text' || parts[1].value !== 'px')
|
|
2124
|
+
return null;
|
|
2125
|
+
const value = lowerTemplateExpression(parts[0].value, storeFields, constants, propBindings);
|
|
2126
|
+
if (!value)
|
|
2127
|
+
return null;
|
|
2128
|
+
return mergeLowered('number', loweredAsKind(value, 'number'), [value]);
|
|
2129
|
+
}
|
|
2130
|
+
function lowerPercentLengthStyleValue(expr, propertyName, storeFields, constants, propBindings) {
|
|
2131
|
+
if (!expr || !stylePercentPropertyEnumForPropertyName(propertyName))
|
|
2132
|
+
return null;
|
|
2133
|
+
const parts = parseTemplateLiteralParts(stripOuterParens(expr.trim()));
|
|
2134
|
+
if (!parts || parts.length !== 2)
|
|
2135
|
+
return null;
|
|
2136
|
+
if (parts[0].kind !== 'expr' || parts[1].kind !== 'text' || parts[1].value !== '%')
|
|
2137
|
+
return null;
|
|
2138
|
+
const value = lowerTemplateExpression(parts[0].value, storeFields, constants, propBindings);
|
|
2139
|
+
if (!value)
|
|
2140
|
+
return null;
|
|
2141
|
+
return { ...mergeLowered('number', loweredAsKind(value, 'number'), [value]), styleUnit: 'percent' };
|
|
2142
|
+
}
|
|
2143
|
+
function lowerRotateStyleDegrees(expr, propertyName, storeFields, constants, propBindings) {
|
|
2144
|
+
if (propertyName !== 'transform' && propertyName !== 'rotate')
|
|
2145
|
+
return null;
|
|
2146
|
+
if (!expr)
|
|
2147
|
+
return null;
|
|
2148
|
+
const parts = parseTemplateLiteralParts(stripOuterParens(expr.trim()));
|
|
2149
|
+
const degreesExpr = rotateDegreesTemplateExpression(parts, propertyName);
|
|
2150
|
+
if (!degreesExpr)
|
|
2151
|
+
return null;
|
|
2152
|
+
const degrees = lowerTemplateExpression(degreesExpr, storeFields, constants, propBindings);
|
|
2153
|
+
if (!degrees)
|
|
2154
|
+
return null;
|
|
2155
|
+
return mergeLowered('number', loweredAsKind(degrees, 'number'), [degrees]);
|
|
2156
|
+
}
|
|
2157
|
+
function lowerOpaqueColorStyleValue(expr, propertyName, storeFields, constants, propBindings) {
|
|
2158
|
+
if (!expr || !styleOpaqueColorTargetForPropertyName(propertyName))
|
|
2159
|
+
return null;
|
|
2160
|
+
const text = stripOuterParens(expr.trim());
|
|
2161
|
+
const literal = opaqueHexColorExpressionForStyleValue(text, constants, propBindings);
|
|
2162
|
+
if (literal)
|
|
2163
|
+
return { kind: 'number', expr: literal, deps: [], fields: [], styleUnit: 'color' };
|
|
2164
|
+
const ternary = splitTopLevelTernary(text);
|
|
2165
|
+
if (!ternary)
|
|
2166
|
+
return null;
|
|
2167
|
+
const test = lowerTemplateExpression(ternary.test, storeFields, constants, propBindings);
|
|
2168
|
+
const consequent = opaqueHexColorExpressionForStyleValue(ternary.consequent, constants, propBindings);
|
|
2169
|
+
const alternate = opaqueHexColorExpressionForStyleValue(ternary.alternate, constants, propBindings);
|
|
2170
|
+
if (!test || !consequent || !alternate)
|
|
2171
|
+
return null;
|
|
2172
|
+
return {
|
|
2173
|
+
...mergeLowered('number', `(${loweredAsBoolean(test)} ? ${consequent} : ${alternate})`, [test]),
|
|
2174
|
+
styleUnit: 'color',
|
|
2175
|
+
};
|
|
2176
|
+
}
|
|
2177
|
+
function lowerKeywordStyleValue(expr, propertyName, storeFields, constants, propBindings) {
|
|
2178
|
+
if (!expr || !styleKeywordPropertiesForPropertyName(propertyName))
|
|
2179
|
+
return null;
|
|
2180
|
+
const text = stripOuterParens(expr.trim());
|
|
2181
|
+
const literal = keywordCommonValue(keywordEntriesForExpr(propertyName, text, constants, propBindings));
|
|
2182
|
+
if (literal !== null)
|
|
2183
|
+
return { kind: 'number', expr: String(literal), deps: [], fields: [], styleUnit: 'keyword' };
|
|
2184
|
+
const ternary = splitTopLevelTernary(text);
|
|
2185
|
+
if (!ternary)
|
|
2186
|
+
return null;
|
|
2187
|
+
const test = lowerTemplateExpression(ternary.test, storeFields, constants, propBindings);
|
|
2188
|
+
const consequent = keywordEntriesForExpr(propertyName, ternary.consequent, constants, propBindings);
|
|
2189
|
+
const alternate = keywordEntriesForExpr(propertyName, ternary.alternate, constants, propBindings);
|
|
2190
|
+
const consequentValue = keywordCommonValue(consequent);
|
|
2191
|
+
const alternateValue = keywordCommonValue(alternate);
|
|
2192
|
+
if (!test || consequentValue === null || alternateValue === null || !keywordEntriesCompatible(consequent, alternate))
|
|
2193
|
+
return null;
|
|
2194
|
+
return {
|
|
2195
|
+
...mergeLowered('number', `(${loweredAsBoolean(test)} ? ${consequentValue} : ${alternateValue})`, [test]),
|
|
2196
|
+
styleUnit: 'keyword',
|
|
2197
|
+
};
|
|
2198
|
+
}
|
|
2199
|
+
function isRotateTransformPropertyName(propertyName) {
|
|
2200
|
+
return propertyName === 'transform' || propertyName === 'rotate';
|
|
2201
|
+
}
|
|
2202
|
+
function rotateDegreesTemplateExpression(parts, propertyName) {
|
|
2203
|
+
if (!parts)
|
|
2204
|
+
return null;
|
|
2205
|
+
if (propertyName === 'rotate') {
|
|
2206
|
+
if (parts.length !== 2)
|
|
2207
|
+
return null;
|
|
2208
|
+
if (parts[0].kind !== 'expr' || parts[1].kind !== 'text' || parts[1].value.trim() !== 'deg')
|
|
2209
|
+
return null;
|
|
2210
|
+
return parts[0].value;
|
|
2211
|
+
}
|
|
2212
|
+
if (propertyName === 'transform') {
|
|
2213
|
+
if (parts.length !== 3)
|
|
2214
|
+
return null;
|
|
2215
|
+
if (parts[0].kind !== 'text' || parts[1].kind !== 'expr' || parts[2].kind !== 'text')
|
|
2216
|
+
return null;
|
|
2217
|
+
if (parts[0].value.trim() !== 'rotate(' || parts[2].value.trim() !== 'deg)')
|
|
2218
|
+
return null;
|
|
2219
|
+
return parts[1].value;
|
|
2220
|
+
}
|
|
2221
|
+
return null;
|
|
2222
|
+
}
|
|
2223
|
+
function directNumberStyleApplyLine(node, propertyName, value) {
|
|
2224
|
+
const rawNumber = rawNumberExpression(value);
|
|
2225
|
+
const properties = styleRawNumberPropertyEnumsForPropertyName(propertyName);
|
|
2226
|
+
if (properties) {
|
|
2227
|
+
return properties.map((property) => `${node}.style().set(gea::embedded::ui::Property::${property}, ${rawNumber});`).join(' ');
|
|
2228
|
+
}
|
|
2229
|
+
if (propertyName === 'opacity')
|
|
2230
|
+
return `${node}.style().opacity(${opacityNumberExpression(value)});`;
|
|
2231
|
+
if (propertyName === 'flex') {
|
|
2232
|
+
return `${node}.style().set(gea::embedded::ui::Property::Flex, ${rawNumber}); ${node}.style().set(gea::embedded::ui::Property::FlexBasis, gea::embedded::ui::kUnset);`;
|
|
2233
|
+
}
|
|
2234
|
+
if (propertyName === 'translate-x')
|
|
2235
|
+
return `${node}.style().translateX(${rawNumber});`;
|
|
2236
|
+
if (propertyName === 'translate-y')
|
|
2237
|
+
return `${node}.style().translateY(${rawNumber});`;
|
|
2238
|
+
if (propertyName === 'scale')
|
|
2239
|
+
return `${node}.style().scale(static_cast<double>(${value}));`;
|
|
2240
|
+
if (propertyName === 'transform' || propertyName === 'rotate')
|
|
2241
|
+
return `${node}.style().rotateDegrees(static_cast<double>(${value}));`;
|
|
2242
|
+
return null;
|
|
2243
|
+
}
|
|
2244
|
+
function rawNumberExpression(value) {
|
|
2245
|
+
return `([](double __v) -> int { return std::isfinite(__v) ? static_cast<int>(std::round(__v)) : 0; })(static_cast<double>(${value}))`;
|
|
2246
|
+
}
|
|
2247
|
+
function opacityNumberExpression(value) {
|
|
2248
|
+
return `([](double __v) -> int { if (!std::isfinite(__v)) return 0; if (__v <= 1.0) __v *= 255.0; if (__v < 0.0) __v = 0.0; if (__v > 255.0) __v = 255.0; return static_cast<int>(__v + 0.5); })(static_cast<double>(${value}))`;
|
|
2249
|
+
}
|
|
2250
|
+
function styleNumberApplyLine(node, propertyName, value, styleUnit) {
|
|
2251
|
+
if (styleUnit === 'percent')
|
|
2252
|
+
return directPercentStyleApplyLine(node, propertyName, value);
|
|
2253
|
+
if (styleUnit === 'color')
|
|
2254
|
+
return directColorStyleApplyLine(node, propertyName, value);
|
|
2255
|
+
if (styleUnit === 'keyword')
|
|
2256
|
+
return directKeywordStyleApplyLine(node, propertyName, value);
|
|
2257
|
+
return directNumberStyleApplyLine(node, propertyName, value);
|
|
2258
|
+
}
|
|
2259
|
+
function directPercentStyleApplyLine(node, propertyName, value) {
|
|
2260
|
+
const property = stylePercentPropertyEnumForPropertyName(propertyName);
|
|
2261
|
+
return property
|
|
2262
|
+
? `${node}.style().set(gea::embedded::ui::Property::${property}, static_cast<int>(std::round(static_cast<double>(${value}) * 10.0)));`
|
|
2263
|
+
: null;
|
|
2264
|
+
}
|
|
2265
|
+
function directStringStyleApplyLines(node, propertyName, rawValue) {
|
|
2266
|
+
const color = opaqueCssHexColor(rawValue);
|
|
2267
|
+
const colorLine = color ? directColorStyleApplyLine(node, propertyName, nativeStyleColorExpression(color)) : null;
|
|
2268
|
+
if (colorLine)
|
|
2269
|
+
return [colorLine];
|
|
2270
|
+
const keyword = styleKeywordPropertyValuesForPropertyName(propertyName, rawValue);
|
|
2271
|
+
return keyword
|
|
2272
|
+
? keyword.map(({ property, value }) => `${node}.style().set(gea::embedded::ui::Property::${property}, ${value});`)
|
|
2273
|
+
: null;
|
|
2274
|
+
}
|
|
2275
|
+
function directKeywordStyleApplyLine(node, propertyName, value) {
|
|
2276
|
+
const properties = styleKeywordPropertiesForPropertyName(propertyName);
|
|
2277
|
+
return properties
|
|
2278
|
+
? properties.map((property) => `${node}.style().set(gea::embedded::ui::Property::${property}, static_cast<int>(${value}));`).join(' ')
|
|
2279
|
+
: null;
|
|
2280
|
+
}
|
|
2281
|
+
function keywordEntriesForExpr(propertyName, expr, constants = new Map(), propBindings = new Map()) {
|
|
2282
|
+
const literal = staticStringStyleValue(expr, constants, propBindings);
|
|
2283
|
+
return literal === null ? null : styleKeywordPropertyValuesForPropertyName(propertyName, literal);
|
|
2284
|
+
}
|
|
2285
|
+
function keywordEntriesCompatible(a, b) {
|
|
2286
|
+
if (!a || !b || a.length !== b.length)
|
|
2287
|
+
return false;
|
|
2288
|
+
for (let i = 0; i < a.length; i += 1) {
|
|
2289
|
+
if (a[i].property !== b[i].property)
|
|
2290
|
+
return false;
|
|
2291
|
+
}
|
|
2292
|
+
return true;
|
|
2293
|
+
}
|
|
2294
|
+
function keywordCommonValue(entries) {
|
|
2295
|
+
if (!entries || entries.length === 0)
|
|
2296
|
+
return null;
|
|
2297
|
+
const first = entries[0].value;
|
|
2298
|
+
return entries.every((entry) => entry.value === first) ? first : null;
|
|
2299
|
+
}
|
|
2300
|
+
function directColorStyleApplyLine(node, propertyName, value) {
|
|
2301
|
+
const target = styleOpaqueColorTargetForPropertyName(propertyName);
|
|
2302
|
+
if (target === 'background')
|
|
2303
|
+
return `${node}.style().backgroundColor(static_cast<int>(${value}));`;
|
|
2304
|
+
if (target === 'color')
|
|
2305
|
+
return `${node}.style().color(static_cast<int>(${value}));`;
|
|
2306
|
+
if (target === 'active-background') {
|
|
2307
|
+
return `${node}.style().set(gea::embedded::ui::Property::ActiveBackgroundColor, static_cast<int>(${value})); ${node}.style().set(gea::embedded::ui::Property::HasActiveBackground, 1);`;
|
|
2308
|
+
}
|
|
2309
|
+
const borderProperty = opaqueColorBorderProperty(target);
|
|
2310
|
+
if (borderProperty)
|
|
2311
|
+
return `${node}.style().set(gea::embedded::ui::Property::${borderProperty}, static_cast<int>(${value}));`;
|
|
2312
|
+
return null;
|
|
2313
|
+
}
|
|
2314
|
+
function opaqueColorBorderProperty(target) {
|
|
2315
|
+
if (target === 'border-color')
|
|
2316
|
+
return 'BorderColor';
|
|
2317
|
+
if (target === 'border-top-color')
|
|
2318
|
+
return 'BorderTopColor';
|
|
2319
|
+
if (target === 'border-right-color')
|
|
2320
|
+
return 'BorderRightColor';
|
|
2321
|
+
if (target === 'border-bottom-color')
|
|
2322
|
+
return 'BorderBottomColor';
|
|
2323
|
+
if (target === 'border-left-color')
|
|
2324
|
+
return 'BorderLeftColor';
|
|
2325
|
+
return null;
|
|
2326
|
+
}
|
|
2327
|
+
function stylePropertyArgument(propertyName) {
|
|
2328
|
+
const declaration = styleDeclarationEnumForPropertyName(propertyName);
|
|
2329
|
+
return declaration ? `gea::embedded::ui::StyleDeclaration::${declaration}` : cppString(propertyName);
|
|
2330
|
+
}
|
|
2331
|
+
function emitInputButtonValueText(lines, varName, node, nextVar) {
|
|
2332
|
+
if (node.tag.toLowerCase() !== 'input' || node.attrs?.type !== 'button')
|
|
2333
|
+
return;
|
|
2334
|
+
const value = node.attrs.value;
|
|
2335
|
+
if (!value)
|
|
2336
|
+
return;
|
|
2337
|
+
const textVar = nextVar();
|
|
2338
|
+
lines.push(` auto ${textVar} = gea::embedded::ui::Document::instance().createText();`);
|
|
2339
|
+
lines.push(` ${textVar}.setText(${cppString(value)});`);
|
|
2340
|
+
lines.push(` ${varName}.appendChild(${textVar});`);
|
|
2341
|
+
}
|
|
2342
|
+
function emitReactiveApply(lines, target, lowered, bodyLine, fieldAccess = BOXED_FIELD_ACCESS, extraCaptures = [], disposerVar = 'disposer') {
|
|
2343
|
+
const bodyLines = Array.isArray(bodyLine) ? bodyLine : [bodyLine];
|
|
2344
|
+
if (lowered.deps.length === 0 && lowered.fields.length === 0) {
|
|
2345
|
+
for (const line of bodyLines)
|
|
2346
|
+
lines.push(` ${line}`);
|
|
2347
|
+
return;
|
|
2348
|
+
}
|
|
2349
|
+
const captures = [target, ...extraCaptures];
|
|
2350
|
+
const fields = uniqueStoreFields(lowered.fields);
|
|
2351
|
+
// A field read against a DIFFERENT global store (e.g. `weather.temp` inside a
|
|
2352
|
+
// component) resolves through that store's global.
|
|
2353
|
+
const globalReceiver = singleGlobalReceiver(fields);
|
|
2354
|
+
if (globalReceiver) {
|
|
2355
|
+
const storeExpr = globalStoreExpression(globalReceiver);
|
|
2356
|
+
const deps = [...new Set(fields.flatMap(storeFieldReactiveDeps))].map((dep) => cppString(dep)).join(', ');
|
|
2357
|
+
const applyLines = [
|
|
2358
|
+
`std::function<void()>([${captures.join(', ')}]() mutable -> void {`,
|
|
2359
|
+
...fields.map((field) => ` const auto ${storeFieldLocalName(field.fieldName)} = ${field.readerName}(${storeExpr});`),
|
|
2360
|
+
...bodyLines.map((line) => ` ${line}`),
|
|
2361
|
+
' })',
|
|
2362
|
+
];
|
|
2363
|
+
// Compiled-base stores notify a typed SignalHub from every write path, so
|
|
2364
|
+
// static-dep binds register typed against the shared_ptr global directly —
|
|
2365
|
+
// no downcast, no boxed fallback. Lean/self stores keep the boxed
|
|
2366
|
+
// registration; gea_cpp_key boxes a typed cell at that dynamic boundary
|
|
2367
|
+
// (identity for already-boxed globals).
|
|
2368
|
+
const hubCapable = storeHubEmissionEnabled() && fields.every((field) => field.storeRuntimeBase === 'compiled' && !field.storeIsSelfStore);
|
|
2369
|
+
if (hubCapable) {
|
|
2370
|
+
lines.push(` gea_rc_bind_hub_apply(${storeExpr}, ${disposerVar}, {${deps}}, ${applyLines[0]}`);
|
|
2371
|
+
for (const line of applyLines.slice(1, -1))
|
|
2372
|
+
lines.push(` ${line}`);
|
|
2373
|
+
lines.push(` ${applyLines[applyLines.length - 1]});`);
|
|
2374
|
+
return;
|
|
2375
|
+
}
|
|
2376
|
+
lines.push(` bindReactiveApply(gea_cpp_key(${storeExpr}), ${disposerVar}, {${deps}}, [${captures.join(', ')}]() mutable -> void {`);
|
|
2377
|
+
for (const field of fields) {
|
|
2378
|
+
lines.push(` const auto ${storeFieldLocalName(field.fieldName)} = ${field.readerName}(${storeExpr});`);
|
|
2379
|
+
}
|
|
2380
|
+
for (const line of bodyLines)
|
|
2381
|
+
lines.push(` ${line}`);
|
|
2382
|
+
lines.push(' });');
|
|
2383
|
+
return;
|
|
2384
|
+
}
|
|
2385
|
+
// Several DISTINCT global stores read by one renderer (e.g. `breakout.score` and
|
|
2386
|
+
// `fps.fpsText` in the same span). Read each field through its own store's global
|
|
2387
|
+
// accessor, and register the SAME apply once per store so a write to ANY of them
|
|
2388
|
+
// re-runs it. (singleGlobalReceiver returned null because >1 store is involved.)
|
|
2389
|
+
const globalGroups = groupGlobalStoreFields(fields);
|
|
2390
|
+
if (globalGroups) {
|
|
2391
|
+
const readLines = fields.map((field) => ` const auto ${storeFieldLocalName(field.fieldName)} = ${field.readerName}(${fieldStoreArg(field)});`);
|
|
2392
|
+
const hubCapable = storeHubEmissionEnabled() && fields.every((field) => field.storeRuntimeBase === 'compiled' && !field.storeIsSelfStore);
|
|
2393
|
+
for (const [receiver, groupFields] of globalGroups) {
|
|
2394
|
+
const storeExpr = globalStoreExpression(receiver);
|
|
2395
|
+
const deps = [...new Set(groupFields.flatMap(storeFieldReactiveDeps))].map((dep) => cppString(dep)).join(', ');
|
|
2396
|
+
if (hubCapable) {
|
|
2397
|
+
lines.push(` gea_rc_bind_hub_apply(${storeExpr}, ${disposerVar}, {${deps}}, std::function<void()>([${captures.join(', ')}]() mutable -> void {`);
|
|
2398
|
+
for (const line of readLines)
|
|
2399
|
+
lines.push(` ${line}`);
|
|
2400
|
+
for (const line of bodyLines)
|
|
2401
|
+
lines.push(` ${line}`);
|
|
2402
|
+
lines.push(' }));');
|
|
2403
|
+
}
|
|
2404
|
+
else {
|
|
2405
|
+
lines.push(` bindReactiveApply(gea_cpp_key(${storeExpr}), ${disposerVar}, {${deps}}, [${captures.join(', ')}]() mutable -> void {`);
|
|
2406
|
+
for (const line of readLines)
|
|
2407
|
+
lines.push(` ${line}`);
|
|
2408
|
+
for (const line of bodyLines)
|
|
2409
|
+
lines.push(` ${line}`);
|
|
2410
|
+
lines.push(' });');
|
|
2411
|
+
}
|
|
2412
|
+
}
|
|
2413
|
+
return;
|
|
2414
|
+
}
|
|
2415
|
+
// The component's own store: boxed `bindReactiveApply(store, …)` or typed
|
|
2416
|
+
// per-field `self->dep.subscribe(…)`, chosen by the FieldAccess strategy.
|
|
2417
|
+
lines.push(...fieldAccess.bindReactiveOwn({ deps: lowered.deps, fields, captures, body: bodyLines, disposerVar }));
|
|
2418
|
+
}
|
|
2419
|
+
// Emit a synchronous "seed" of a reactive expression's CURRENT value, in a scoped
|
|
2420
|
+
// block, declaring the same store-field locals the reactive binding uses. Emitted
|
|
2421
|
+
// at node-creation time (before appendChild) so a parent whose only child is this
|
|
2422
|
+
// value measures a non-zero size at first layout; without it, the reactive
|
|
2423
|
+
// binding's (possibly deferred) initial apply runs after layout and a
|
|
2424
|
+
// never-changing value (e.g. a store default) stays 0-sized and invisible.
|
|
2425
|
+
function emitSyncSeed(lines, lowered, bodyLines) {
|
|
2426
|
+
if (lowered.deps.length === 0 && lowered.fields.length === 0) {
|
|
2427
|
+
for (const line of bodyLines)
|
|
2428
|
+
lines.push(` ${line}`);
|
|
2429
|
+
return;
|
|
2430
|
+
}
|
|
2431
|
+
const fields = uniqueStoreFields(lowered.fields);
|
|
2432
|
+
// Read each field through its OWN store's accessor (a renderer may read several
|
|
2433
|
+
// global stores), falling back to the boxed `store` param for own-store fields.
|
|
2434
|
+
lines.push(' {');
|
|
2435
|
+
for (const field of fields)
|
|
2436
|
+
lines.push(` const auto ${storeFieldLocalName(field.fieldName)} = ${field.readerName}(${fieldStoreArg(field)});`);
|
|
2437
|
+
for (const line of bodyLines)
|
|
2438
|
+
lines.push(` ${line}`);
|
|
2439
|
+
lines.push(' }');
|
|
2440
|
+
}
|
|
2441
|
+
// The receiver expression for reading one field: its global accessor
|
|
2442
|
+
// (`__gea_global_<store>()`) for a global store, else the boxed `store` param.
|
|
2443
|
+
function fieldStoreArg(field) {
|
|
2444
|
+
if (field.receiverName === 'this')
|
|
2445
|
+
return 'store';
|
|
2446
|
+
const isGlobal = !!field.receiverName &&
|
|
2447
|
+
(field.receiverName === field.storeGlobalName || /^[A-Z]/.test(field.receiverName) || !field.storeGlobalName);
|
|
2448
|
+
return isGlobal ? globalStoreExpression(field.receiverName) : 'store';
|
|
2449
|
+
}
|
|
2450
|
+
// Group fields by their global store when a renderer reads MORE THAN ONE distinct
|
|
2451
|
+
// global store (so each can be bound on its own typed hub). Returns null if any
|
|
2452
|
+
// field isn't a global (own-store/`this`/local) or only one store is involved —
|
|
2453
|
+
// those cases are handled by singleGlobalReceiver / the own-store path.
|
|
2454
|
+
function groupGlobalStoreFields(fields) {
|
|
2455
|
+
const groups = new Map();
|
|
2456
|
+
for (const field of fields) {
|
|
2457
|
+
if (field.receiverName === 'this')
|
|
2458
|
+
return null;
|
|
2459
|
+
const isGlobal = !!field.receiverName &&
|
|
2460
|
+
(field.receiverName === field.storeGlobalName || /^[A-Z]/.test(field.receiverName) || !field.storeGlobalName);
|
|
2461
|
+
if (!isGlobal)
|
|
2462
|
+
return null;
|
|
2463
|
+
const list = groups.get(field.receiverName);
|
|
2464
|
+
if (list)
|
|
2465
|
+
list.push(field);
|
|
2466
|
+
else
|
|
2467
|
+
groups.set(field.receiverName, [field]);
|
|
2468
|
+
}
|
|
2469
|
+
return groups.size >= 2 ? groups : null;
|
|
2470
|
+
}
|
|
2471
|
+
function singleGlobalReceiver(fields) {
|
|
2472
|
+
let receiver = null;
|
|
2473
|
+
for (const field of fields) {
|
|
2474
|
+
// `this.field` is the component's OWN store (a self-store registers `this`
|
|
2475
|
+
// as its instance name purely for collision disambiguation) — never a
|
|
2476
|
+
// global; route through the own-store branch (boxed `store` / typed self).
|
|
2477
|
+
if (field.receiverName === 'this')
|
|
2478
|
+
return null;
|
|
2479
|
+
// A receiver is a global store if it's the store's exported singleton name
|
|
2480
|
+
// (e.g. the lowercase `companion` in `export const companion = new ...`) or a
|
|
2481
|
+
// PascalCase store identifier. Without the storeGlobalName check, a lowercase
|
|
2482
|
+
// singleton falls back to `store` — the component's (often empty) store
|
|
2483
|
+
// context — so reactive reads in the template return empty.
|
|
2484
|
+
const isGlobal = !!field.receiverName &&
|
|
2485
|
+
(field.receiverName === field.storeGlobalName || /^[A-Z]/.test(field.receiverName) || !field.storeGlobalName);
|
|
2486
|
+
const next = isGlobal ? field.receiverName : null;
|
|
2487
|
+
if (!next)
|
|
2488
|
+
return null;
|
|
2489
|
+
if (receiver && receiver !== next)
|
|
2490
|
+
return null;
|
|
2491
|
+
receiver = next;
|
|
2492
|
+
}
|
|
2493
|
+
return receiver;
|
|
2494
|
+
}
|
|
2495
|
+
function canEmitIntrinsicImageMount(slot, storeFields, constants, propBindings) {
|
|
2496
|
+
if (mountSlotTag(slot) !== 'Image')
|
|
2497
|
+
return false;
|
|
2498
|
+
const source = lowerImageSourceExpression(slot, storeFields, constants, propBindings);
|
|
2499
|
+
if (!source || source.fields.some((field) => !field.readerName))
|
|
2500
|
+
return false;
|
|
2501
|
+
const styleSlot = mountSlotStyleSlot(slot);
|
|
2502
|
+
return !styleSlot || canLowerTemplateStyleSlot(styleSlot, storeFields, constants, propBindings);
|
|
2503
|
+
}
|
|
2504
|
+
function emitIntrinsicImageMount(lines, varName, slot, storeFields, constants, propBindings, disposerVar = 'disposer') {
|
|
2505
|
+
const source = lowerImageSourceExpression(slot, storeFields, constants, propBindings);
|
|
2506
|
+
if (!source || source.fields.some((field) => !field.readerName))
|
|
2507
|
+
return false;
|
|
2508
|
+
emitReactiveApply(lines, varName, source, `gea::embedded::ui::ImageElement(${varName}.id()).imageId(static_cast<int>(${loweredAsKind(source, 'number')}));`, BOXED_FIELD_ACCESS, [], disposerVar);
|
|
2509
|
+
const styleSlot = mountSlotStyleSlot(slot);
|
|
2510
|
+
return !styleSlot || emitTemplateStyleSlot(lines, varName, styleSlot, storeFields, constants, propBindings, BOXED_FIELD_ACCESS, disposerVar);
|
|
2511
|
+
}
|
|
2512
|
+
function lowerImageSourceExpression(slot, storeFields, constants, propBindings) {
|
|
2513
|
+
for (const attr of mountSlotAttrs(slot)) {
|
|
2514
|
+
const parsed = parseMountAttr(attr);
|
|
2515
|
+
if (!parsed || parsed.name !== 'src')
|
|
2516
|
+
continue;
|
|
2517
|
+
return lowerImageExpression(parsed.expr, storeFields, constants, propBindings);
|
|
2518
|
+
}
|
|
2519
|
+
return { kind: 'number', expr: '-1', deps: [], fields: [] };
|
|
2520
|
+
}
|
|
2521
|
+
function lowerImageExpression(expr, storeFields, constants, propBindings) {
|
|
2522
|
+
const text = stripOuterParens(expr.trim());
|
|
2523
|
+
const ternary = splitTopLevelTernary(text);
|
|
2524
|
+
if (ternary) {
|
|
2525
|
+
const test = lowerTemplateExpression(ternary.test, storeFields, constants, propBindings);
|
|
2526
|
+
const consequent = lowerImageExpression(ternary.consequent, storeFields, constants, propBindings);
|
|
2527
|
+
const alternate = lowerImageExpression(ternary.alternate, storeFields, constants, propBindings);
|
|
2528
|
+
if (!test || !consequent || !alternate)
|
|
2529
|
+
return null;
|
|
2530
|
+
return mergeLowered('number', `(${loweredAsBoolean(test)} ? ${loweredAsKind(consequent, 'number')} : ${loweredAsKind(alternate, 'number')})`, [test, consequent, alternate]);
|
|
2531
|
+
}
|
|
2532
|
+
const global = simpleIdentifierValue(text);
|
|
2533
|
+
if (global)
|
|
2534
|
+
return { kind: 'number', expr: imageIdExpression(global), deps: [], fields: [] };
|
|
2535
|
+
const literalPath = stringLiteralValue(text);
|
|
2536
|
+
if (literalPath !== null) {
|
|
2537
|
+
return { kind: 'number', expr: `static_cast<double>(${imageSrcAssetIdExpression(literalPath)})`, deps: [], fields: [] };
|
|
2538
|
+
}
|
|
2539
|
+
return null;
|
|
2540
|
+
}
|
|
2541
|
+
// Native `.id` field read off the `gea::host::GeaEmbeddedImage&` global —
|
|
2542
|
+
// keep the shape in sync with imageIdApplyLine in cpp-mounted-lowering.ts.
|
|
2543
|
+
function imageIdExpression(imageGlobal) {
|
|
2544
|
+
return `static_cast<double>(__gea_global_${sanitizeCppIdentifier(imageGlobal)}().id)`;
|
|
2545
|
+
}
|
|
2546
|
+
function mountPropBindings(slot, parentProps) {
|
|
2547
|
+
const out = new Map();
|
|
2548
|
+
const attrs = mountSlotAttrs(slot);
|
|
2549
|
+
for (const attr of attrs) {
|
|
2550
|
+
const parsed = parseMountAttr(attr);
|
|
2551
|
+
if (parsed)
|
|
2552
|
+
out.set(parsed.name, { expr: parsed.expr, parentProps });
|
|
2553
|
+
}
|
|
2554
|
+
const childrenTemplate = mountSlotChildrenTemplate(slot);
|
|
2555
|
+
if (childrenTemplate && !out.has('children')) {
|
|
2556
|
+
out.set('children', { expr: 'props.children', parentProps, childrenTemplate });
|
|
2557
|
+
}
|
|
2558
|
+
return out;
|
|
2559
|
+
}
|
|
2560
|
+
function mountSlotChildrenTemplate(slot) {
|
|
2561
|
+
const payload = slot.payload;
|
|
2562
|
+
if (!payload || typeof payload !== 'object')
|
|
2563
|
+
return null;
|
|
2564
|
+
const childrenTemplate = payload.childrenTemplate;
|
|
2565
|
+
if (isGeaIrTemplate(childrenTemplate))
|
|
2566
|
+
return childrenTemplate;
|
|
2567
|
+
// The upstream IR only emits `childrenTemplate` when at least one child is
|
|
2568
|
+
// dynamic (an expression or another element). For pure static text — e.g.
|
|
2569
|
+
// `<Text class="settings-title">Settings</Text>` — the literal lives only in
|
|
2570
|
+
// `payload.children` as `JSXText` nodes and never reaches the bindings.
|
|
2571
|
+
// Without this recovery, every wrapper component with hard-coded text would
|
|
2572
|
+
// mount with an empty child node. Synthesize a no-slot template from the raw
|
|
2573
|
+
// JSX text so the static content survives inlining intact.
|
|
2574
|
+
const children = payload.children;
|
|
2575
|
+
if (!Array.isArray(children) || children.length === 0)
|
|
2576
|
+
return null;
|
|
2577
|
+
let text = '';
|
|
2578
|
+
for (const node of children) {
|
|
2579
|
+
if (!node || typeof node !== 'object')
|
|
2580
|
+
return null;
|
|
2581
|
+
if (node.nodeType !== 'JSXText')
|
|
2582
|
+
return null;
|
|
2583
|
+
const code = node.code;
|
|
2584
|
+
if (typeof code !== 'string')
|
|
2585
|
+
return null;
|
|
2586
|
+
text += code;
|
|
2587
|
+
}
|
|
2588
|
+
const collapsed = text.replace(/\s+/g, ' ').trim();
|
|
2589
|
+
if (!collapsed)
|
|
2590
|
+
return null;
|
|
2591
|
+
return { html: collapsed, slots: [] };
|
|
2592
|
+
}
|
|
2593
|
+
function renderableTemplateText(text) {
|
|
2594
|
+
const collapsed = text.replace(/\s+/g, ' ');
|
|
2595
|
+
return collapsed.trim() ? collapsed : '';
|
|
2596
|
+
}
|
|
2597
|
+
function isGeaIrTemplate(value) {
|
|
2598
|
+
return !!value && typeof value === 'object' && typeof value.html === 'string' && Array.isArray(value.slots);
|
|
2599
|
+
}
|
|
2600
|
+
function childrenTemplateForPropSlot(slot, propBindings) {
|
|
2601
|
+
return childrenBindingForPropSlot(slot, propBindings)?.childrenTemplate ?? null;
|
|
2602
|
+
}
|
|
2603
|
+
// A `props.children` placeholder inside a wrapped element (e.g. View's `{children}`
|
|
2604
|
+
// position inside `<View class="settings-row">{Texts}</View>`) is expanded with
|
|
2605
|
+
// the JSX subtree captured in the wrapper's mount slot. That subtree was *authored*
|
|
2606
|
+
// in the caller's scope (SettingsRow's body, where `label`/`value` are bound), not
|
|
2607
|
+
// in View's. Without using `binding.parentProps` when emitting it, references like
|
|
2608
|
+
// `<Text>{label}</Text>` resolve against View's props — finding no `label`, and
|
|
2609
|
+
// the text renders empty. Returning the full binding lets callers thread the
|
|
2610
|
+
// caller-scope propBindings through.
|
|
2611
|
+
function childrenBindingForPropSlot(slot, propBindings) {
|
|
2612
|
+
if (slot.kind !== 'text')
|
|
2613
|
+
return null;
|
|
2614
|
+
const prop = propNameForExpression(stripOuterParens(slot.expr?.trim() ?? ''));
|
|
2615
|
+
if (!prop)
|
|
2616
|
+
return null;
|
|
2617
|
+
const binding = propBindings.get(prop);
|
|
2618
|
+
return binding?.childrenTemplate ? binding : null;
|
|
2619
|
+
}
|
|
2620
|
+
function conditionalBranchTemplates(slot) {
|
|
2621
|
+
const payload = slot.payload;
|
|
2622
|
+
if (!payload || typeof payload !== 'object')
|
|
2623
|
+
return null;
|
|
2624
|
+
const record = payload;
|
|
2625
|
+
const consequent = isGeaIrTemplate(record.consequentTemplate) ? record.consequentTemplate : null;
|
|
2626
|
+
const alternate = isGeaIrTemplate(record.alternateTemplate) ? record.alternateTemplate : null;
|
|
2627
|
+
return { consequent, alternate };
|
|
2628
|
+
}
|
|
2629
|
+
function globalStoreExpression(receiver) {
|
|
2630
|
+
return `__gea_global_${sanitizeCppIdentifier(receiver)}()`;
|
|
2631
|
+
}
|
|
2632
|
+
function mountSlotAttrs(slot) {
|
|
2633
|
+
const payload = slot.payload;
|
|
2634
|
+
if (!payload || typeof payload !== 'object' || !('attrs' in payload))
|
|
2635
|
+
return [];
|
|
2636
|
+
const attrs = payload.attrs;
|
|
2637
|
+
if (!Array.isArray(attrs))
|
|
2638
|
+
return [];
|
|
2639
|
+
return attrs
|
|
2640
|
+
.map((attr) => {
|
|
2641
|
+
if (!attr || typeof attr !== 'object' || !('code' in attr))
|
|
2642
|
+
return null;
|
|
2643
|
+
const code = attr.code;
|
|
2644
|
+
return typeof code === 'string' ? code : null;
|
|
2645
|
+
})
|
|
2646
|
+
.filter((code) => !!code);
|
|
2647
|
+
}
|
|
2648
|
+
function parseMountAttr(code) {
|
|
2649
|
+
const trimmed = code.trim();
|
|
2650
|
+
const stringAttr = trimmed.match(/^([A-Za-z_$][A-Za-z0-9_$]*)=(["'])(.*)\2$/s);
|
|
2651
|
+
if (stringAttr)
|
|
2652
|
+
return { name: stringAttr[1], expr: `${stringAttr[2]}${stringAttr[3]}${stringAttr[2]}` };
|
|
2653
|
+
const expressionAttr = trimmed.match(/^([A-Za-z_$][A-Za-z0-9_$]*)=\{([\s\S]*)\}$/);
|
|
2654
|
+
if (expressionAttr)
|
|
2655
|
+
return { name: expressionAttr[1], expr: expressionAttr[2].trim() };
|
|
2656
|
+
const bareAttr = trimmed.match(/^([A-Za-z_$][A-Za-z0-9_$]*)$/);
|
|
2657
|
+
return bareAttr ? { name: bareAttr[1], expr: 'true' } : null;
|
|
2658
|
+
}
|
|
2659
|
+
function lowerTemplateExpression(expr, storeFields, constants, propBindings, seenProps = new Set()) {
|
|
2660
|
+
if (!expr)
|
|
2661
|
+
return null;
|
|
2662
|
+
const text = stripOuterParens(expr.trim());
|
|
2663
|
+
if (text === 'true' || text === 'false')
|
|
2664
|
+
return { kind: 'boolean', expr: text, deps: [], fields: [] };
|
|
2665
|
+
const prop = propNameForExpression(text);
|
|
2666
|
+
if (prop) {
|
|
2667
|
+
const binding = propBindings.get(prop);
|
|
2668
|
+
if (binding) {
|
|
2669
|
+
if (seenProps.has(prop))
|
|
2670
|
+
return null;
|
|
2671
|
+
const nextSeen = new Set(seenProps);
|
|
2672
|
+
nextSeen.add(prop);
|
|
2673
|
+
return lowerTemplateExpression(binding.expr, storeFields, constants, binding.parentProps, nextSeen);
|
|
2674
|
+
}
|
|
2675
|
+
const constant = constantExpression(prop, constants);
|
|
2676
|
+
if (constant)
|
|
2677
|
+
return { kind: kindForShape(constant.shape), expr: constant.expr, deps: [], fields: [] };
|
|
2678
|
+
if (isRuntimeNumericConstantIdentifier(prop)) {
|
|
2679
|
+
return { kind: 'number', expr: `static_cast<double>(${prop})`, deps: [], fields: [] };
|
|
2680
|
+
}
|
|
2681
|
+
}
|
|
2682
|
+
const ternary = splitTopLevelTernary(text);
|
|
2683
|
+
if (ternary) {
|
|
2684
|
+
const test = lowerTemplateExpression(ternary.test, storeFields, constants, propBindings, seenProps);
|
|
2685
|
+
const consequent = lowerTemplateExpression(ternary.consequent, storeFields, constants, propBindings, seenProps);
|
|
2686
|
+
const alternate = lowerTemplateExpression(ternary.alternate, storeFields, constants, propBindings, seenProps);
|
|
2687
|
+
if (!test || !consequent || !alternate)
|
|
2688
|
+
return null;
|
|
2689
|
+
return mergeLowered(consequent.kind === 'string' || alternate.kind === 'string' ? 'string' : consequent.kind, `(${loweredAsBoolean(test)} ? ${loweredAsKind(consequent, consequent.kind === 'string' || alternate.kind === 'string' ? 'string' : consequent.kind)} : ${loweredAsKind(alternate, consequent.kind === 'string' || alternate.kind === 'string' ? 'string' : consequent.kind)})`, [test, consequent, alternate]);
|
|
2690
|
+
}
|
|
2691
|
+
const templateLiteral = lowerTemplateLiteral(text, storeFields, constants, propBindings, seenProps);
|
|
2692
|
+
if (templateLiteral)
|
|
2693
|
+
return templateLiteral;
|
|
2694
|
+
const logicalOr = splitTopLevelOperator(text, '||');
|
|
2695
|
+
if (logicalOr.length > 1) {
|
|
2696
|
+
const lowered = logicalOr.map((part) => lowerTemplateExpression(part, storeFields, constants, propBindings, seenProps));
|
|
2697
|
+
if (lowered.some((part) => !part))
|
|
2698
|
+
return null;
|
|
2699
|
+
return mergeLowered('boolean', lowered.map((part) => loweredAsBoolean(part)).join(' || '), lowered);
|
|
2700
|
+
}
|
|
2701
|
+
const logicalAnd = splitTopLevelOperator(text, '&&');
|
|
2702
|
+
if (logicalAnd.length > 1) {
|
|
2703
|
+
const lowered = logicalAnd.map((part) => lowerTemplateExpression(part, storeFields, constants, propBindings, seenProps));
|
|
2704
|
+
if (lowered.some((part) => !part))
|
|
2705
|
+
return null;
|
|
2706
|
+
return mergeLowered('boolean', lowered.map((part) => loweredAsBoolean(part)).join(' && '), lowered);
|
|
2707
|
+
}
|
|
2708
|
+
const plusParts = splitTopLevelOperator(text, '+');
|
|
2709
|
+
if (plusParts.length > 1) {
|
|
2710
|
+
const lowered = plusParts.map((part) => lowerTemplateExpression(part, storeFields, constants, propBindings, seenProps));
|
|
2711
|
+
if (lowered.some((part) => !part))
|
|
2712
|
+
return null;
|
|
2713
|
+
if (lowered.every((part) => part.kind === 'number')) {
|
|
2714
|
+
return mergeLowered('number', lowered.map((part) => part.expr).join(' + '), lowered);
|
|
2715
|
+
}
|
|
2716
|
+
return mergeLowered('string', lowered.map((part) => loweredAsString(part)).join(' + '), lowered);
|
|
2717
|
+
}
|
|
2718
|
+
if (text.startsWith('-') && numericLiteralValue(text) === null) {
|
|
2719
|
+
const operand = lowerTemplateExpression(text.slice(1), storeFields, constants, propBindings, seenProps);
|
|
2720
|
+
if (!operand)
|
|
2721
|
+
return null;
|
|
2722
|
+
return mergeLowered('number', `(-${loweredAsNumberOperand(operand)})`, [operand]);
|
|
2723
|
+
}
|
|
2724
|
+
const unaryMathMatch = text.match(/^Math\.(abs|floor|ceil|round)\(([\s\S]*)\)$/);
|
|
2725
|
+
if (unaryMathMatch) {
|
|
2726
|
+
const operand = lowerTemplateExpression(unaryMathMatch[2], storeFields, constants, propBindings, seenProps);
|
|
2727
|
+
if (!operand)
|
|
2728
|
+
return null;
|
|
2729
|
+
const fn = unaryMathMatch[1] === 'abs'
|
|
2730
|
+
? 'std::abs'
|
|
2731
|
+
: unaryMathMatch[1] === 'floor'
|
|
2732
|
+
? 'std::floor'
|
|
2733
|
+
: unaryMathMatch[1] === 'ceil'
|
|
2734
|
+
? 'std::ceil'
|
|
2735
|
+
: 'std::round';
|
|
2736
|
+
return mergeLowered('number', `${fn}(${loweredAsNumberOperand(operand)})`, [operand]);
|
|
2737
|
+
}
|
|
2738
|
+
const arithmetic = splitTopLevelArithmetic(text);
|
|
2739
|
+
if (arithmetic) {
|
|
2740
|
+
const left = lowerTemplateExpression(arithmetic.left, storeFields, constants, propBindings, seenProps);
|
|
2741
|
+
const right = lowerTemplateExpression(arithmetic.right, storeFields, constants, propBindings, seenProps);
|
|
2742
|
+
if (!left || !right)
|
|
2743
|
+
return null;
|
|
2744
|
+
return mergeLowered('number', `(${loweredAsNumberOperand(left)} ${arithmetic.op} ${loweredAsNumberOperand(right)})`, [left, right]);
|
|
2745
|
+
}
|
|
2746
|
+
const binary = splitTopLevelBinary(text);
|
|
2747
|
+
if (binary) {
|
|
2748
|
+
const left = lowerTemplateExpression(binary.left, storeFields, constants, propBindings, seenProps);
|
|
2749
|
+
const right = lowerTemplateExpression(binary.right, storeFields, constants, propBindings, seenProps);
|
|
2750
|
+
if (!left || !right)
|
|
2751
|
+
return null;
|
|
2752
|
+
const equality = loweredEqualityExpression(left, right, binary.op);
|
|
2753
|
+
if (equality)
|
|
2754
|
+
return mergeLowered('boolean', equality, [left, right]);
|
|
2755
|
+
return mergeLowered('boolean', `(${loweredAsComparable(left)} ${cppBinaryOperator(binary.op)} ${loweredAsComparable(right)})`, [left, right]);
|
|
2756
|
+
}
|
|
2757
|
+
if (text.startsWith('!')) {
|
|
2758
|
+
const operand = lowerTemplateExpression(text.slice(1), storeFields, constants, propBindings, seenProps);
|
|
2759
|
+
if (!operand)
|
|
2760
|
+
return null;
|
|
2761
|
+
return mergeLowered('boolean', `!(${loweredAsBoolean(operand)})`, [operand]);
|
|
2762
|
+
}
|
|
2763
|
+
const string = stringLiteralValue(text);
|
|
2764
|
+
if (string !== null)
|
|
2765
|
+
return { kind: 'string', expr: `std::string(${cppString(string)})`, deps: [], fields: [] };
|
|
2766
|
+
if (numericLiteralValue(text) !== null)
|
|
2767
|
+
return { kind: 'number', expr: `static_cast<double>(${text})`, deps: [], fields: [] };
|
|
2768
|
+
const memberLength = text.match(/^([A-Za-z_$][A-Za-z0-9_$]*)\.([A-Za-z_$][A-Za-z0-9_$]*)\.length$/);
|
|
2769
|
+
if (memberLength) {
|
|
2770
|
+
const field = storeFieldByName(memberLength[2], storeFields, memberLength[1]);
|
|
2771
|
+
// `.length` lowers to native `.size()` for arrays AND strings — a string
|
|
2772
|
+
// store field is a `std::string` member, so `{store.toast.length}` reads
|
|
2773
|
+
// the same scalar without boxing (JS string .length === size() for the
|
|
2774
|
+
// template-expression subset, which never emits non-ASCII field text).
|
|
2775
|
+
const lengthCarrier = field?.shape?.kind === 'array' || (field?.shape?.kind === 'literal' && field.shape.valueType === 'string');
|
|
2776
|
+
if (!field || !lengthCarrier)
|
|
2777
|
+
return null;
|
|
2778
|
+
if (!field.readerName) {
|
|
2779
|
+
// Named arrays whose element records contain nested data may not have a
|
|
2780
|
+
// value reader, but their concrete store member still has a native
|
|
2781
|
+
// `.size()`. Read only that scalar so `{store.books.length}` stays typed
|
|
2782
|
+
// without materializing or boxing the array.
|
|
2783
|
+
if (field.storeGlobalName !== sanitizeCppIdentifier(memberLength[1]) || field.storeIsSelfStore)
|
|
2784
|
+
return null;
|
|
2785
|
+
const fieldRef = {
|
|
2786
|
+
...field,
|
|
2787
|
+
fieldType: 'double',
|
|
2788
|
+
readerName: `[](const auto &__gea_store) -> double { return __gea_store ? static_cast<double>(__gea_store->${field.fieldName}.size()) : 0.0; }`,
|
|
2789
|
+
shape: { kind: 'literal', valueType: 'number' },
|
|
2790
|
+
receiverName: memberLength[1],
|
|
2791
|
+
};
|
|
2792
|
+
return {
|
|
2793
|
+
kind: 'number',
|
|
2794
|
+
expr: storeFieldLocalName(field.fieldName),
|
|
2795
|
+
deps: [field.fieldName],
|
|
2796
|
+
fields: [fieldRef],
|
|
2797
|
+
};
|
|
2798
|
+
}
|
|
2799
|
+
const fieldRef = { ...field, receiverName: memberLength[1] };
|
|
2800
|
+
return {
|
|
2801
|
+
kind: 'number',
|
|
2802
|
+
expr: `static_cast<double>(${storeFieldLocalName(field.fieldName)}.size())`,
|
|
2803
|
+
deps: [field.fieldName],
|
|
2804
|
+
fields: [fieldRef],
|
|
2805
|
+
};
|
|
2806
|
+
}
|
|
2807
|
+
const member = text.match(/^([A-Za-z_$][A-Za-z0-9_$]*)\.([A-Za-z_$][A-Za-z0-9_$]*)$/);
|
|
2808
|
+
if (member) {
|
|
2809
|
+
const field = storeFieldByName(member[2], storeFields, member[1]);
|
|
2810
|
+
if (field?.readerName) {
|
|
2811
|
+
const fieldRef = { ...field, receiverName: member[1] };
|
|
2812
|
+
return {
|
|
2813
|
+
kind: kindForStoreField(field),
|
|
2814
|
+
expr: storeFieldLocalName(field.fieldName),
|
|
2815
|
+
deps: storeFieldReactiveDeps(field),
|
|
2816
|
+
fields: [fieldRef],
|
|
2817
|
+
};
|
|
2818
|
+
}
|
|
2819
|
+
// A member read against a global store singleton that the per-component IR
|
|
2820
|
+
// doesn't enumerate (e.g. `voiceNotesLibrary.noteListBackSelected` when the
|
|
2821
|
+
// store lives in another module) still has a runtime home: the global store
|
|
2822
|
+
// hub. Lower it to a dynamic boxed read through `__gea_global_<receiver>()`
|
|
2823
|
+
// — a reactive binding keyed on the field name — instead of failing the
|
|
2824
|
+
// whole renderer back to the slow runtime-template path.
|
|
2825
|
+
const dynamicField = dynamicGlobalStoreFieldRef(member[1], member[2], constants, propBindings);
|
|
2826
|
+
if (dynamicField) {
|
|
2827
|
+
return {
|
|
2828
|
+
kind: 'value',
|
|
2829
|
+
expr: storeFieldLocalName(dynamicField.fieldName),
|
|
2830
|
+
deps: [dynamicField.fieldName],
|
|
2831
|
+
fields: [dynamicField],
|
|
2832
|
+
};
|
|
2833
|
+
}
|
|
2834
|
+
return null;
|
|
2835
|
+
}
|
|
2836
|
+
return null;
|
|
2837
|
+
}
|
|
2838
|
+
// Synthesize a reactive field reference for a member read whose receiver is a
|
|
2839
|
+
// global store singleton not modeled in `storeFields`. The read lowers through
|
|
2840
|
+
// the global store hub via an inline-lambda reader so it slots into every
|
|
2841
|
+
// `${field.readerName}(${storeExpr})` site unchanged: the lambda boxes the hub
|
|
2842
|
+
// accessor (typed `shared_ptr<Store>` or `gea_cpp_value`) and reads the field
|
|
2843
|
+
// dynamically. Returns null for host facades / JS globals / locally-bound names.
|
|
2844
|
+
function dynamicGlobalStoreFieldRef(receiver, property, constants, propBindings) {
|
|
2845
|
+
if (NON_STORE_RECEIVERS.has(receiver))
|
|
2846
|
+
return null;
|
|
2847
|
+
if (constants.has(receiver) || propBindings.has(receiver))
|
|
2848
|
+
return null;
|
|
2849
|
+
const reader = `[](auto&& __gea_store) { return gea_cpp_key(__gea_store).record_get_literal(${cppString(property)}); }`;
|
|
2850
|
+
return {
|
|
2851
|
+
storeClass: '',
|
|
2852
|
+
stateType: '',
|
|
2853
|
+
fieldName: property,
|
|
2854
|
+
fieldType: 'gea_cpp_value',
|
|
2855
|
+
readerName: reader,
|
|
2856
|
+
field: { name: property },
|
|
2857
|
+
shape: null,
|
|
2858
|
+
reader: null,
|
|
2859
|
+
storeGlobalName: receiver,
|
|
2860
|
+
receiverName: receiver,
|
|
2861
|
+
};
|
|
2862
|
+
}
|
|
2863
|
+
function lowerTemplateLiteral(expr, storeFields, constants, propBindings, seenProps) {
|
|
2864
|
+
if (!expr.startsWith('`') || !expr.endsWith('`'))
|
|
2865
|
+
return null;
|
|
2866
|
+
const parts = parseTemplateLiteralParts(expr);
|
|
2867
|
+
if (!parts)
|
|
2868
|
+
return null;
|
|
2869
|
+
const loweredParts = [];
|
|
2870
|
+
const exprParts = [];
|
|
2871
|
+
for (const part of parts) {
|
|
2872
|
+
if (part.kind === 'text') {
|
|
2873
|
+
if (part.value.length === 0)
|
|
2874
|
+
continue;
|
|
2875
|
+
exprParts.push(`std::string(${cppString(part.value)})`);
|
|
2876
|
+
continue;
|
|
2877
|
+
}
|
|
2878
|
+
const lowered = lowerTemplateExpression(part.value, storeFields, constants, propBindings, seenProps);
|
|
2879
|
+
if (!lowered)
|
|
2880
|
+
return null;
|
|
2881
|
+
loweredParts.push(lowered);
|
|
2882
|
+
exprParts.push(loweredAsString(lowered));
|
|
2883
|
+
}
|
|
2884
|
+
return mergeLowered('string', exprParts.length > 0 ? exprParts.join(' + ') : 'std::string("")', loweredParts);
|
|
2885
|
+
}
|
|
2886
|
+
function parseTemplateLiteralParts(expr) {
|
|
2887
|
+
const body = expr.slice(1, -1);
|
|
2888
|
+
const parts = [];
|
|
2889
|
+
let text = '';
|
|
2890
|
+
for (let index = 0; index < body.length; index += 1) {
|
|
2891
|
+
const char = body[index];
|
|
2892
|
+
if (char === '\\') {
|
|
2893
|
+
const next = body[index + 1];
|
|
2894
|
+
if (next === undefined)
|
|
2895
|
+
return null;
|
|
2896
|
+
text += next === 'n' ? '\n' : next === 't' ? '\t' : next;
|
|
2897
|
+
index += 1;
|
|
2898
|
+
continue;
|
|
2899
|
+
}
|
|
2900
|
+
if (char !== '$' || body[index + 1] !== '{') {
|
|
2901
|
+
text += char;
|
|
2902
|
+
continue;
|
|
2903
|
+
}
|
|
2904
|
+
if (text) {
|
|
2905
|
+
parts.push({ kind: 'text', value: text });
|
|
2906
|
+
text = '';
|
|
2907
|
+
}
|
|
2908
|
+
const end = findTemplateExpressionEnd(body, index + 2);
|
|
2909
|
+
if (end < 0)
|
|
2910
|
+
return null;
|
|
2911
|
+
parts.push({ kind: 'expr', value: body.slice(index + 2, end).trim() });
|
|
2912
|
+
index = end;
|
|
2913
|
+
}
|
|
2914
|
+
if (text)
|
|
2915
|
+
parts.push({ kind: 'text', value: text });
|
|
2916
|
+
return parts;
|
|
2917
|
+
}
|
|
2918
|
+
function findTemplateExpressionEnd(expr, start) {
|
|
2919
|
+
let depth = 1;
|
|
2920
|
+
let quote = null;
|
|
2921
|
+
let escaped = false;
|
|
2922
|
+
for (let index = start; index < expr.length; index += 1) {
|
|
2923
|
+
const char = expr[index];
|
|
2924
|
+
if (quote) {
|
|
2925
|
+
if (escaped)
|
|
2926
|
+
escaped = false;
|
|
2927
|
+
else if (char === '\\')
|
|
2928
|
+
escaped = true;
|
|
2929
|
+
else if (char === quote)
|
|
2930
|
+
quote = null;
|
|
2931
|
+
continue;
|
|
2932
|
+
}
|
|
2933
|
+
if (char === '"' || char === "'" || char === '`') {
|
|
2934
|
+
quote = char;
|
|
2935
|
+
continue;
|
|
2936
|
+
}
|
|
2937
|
+
if (char === '{')
|
|
2938
|
+
depth += 1;
|
|
2939
|
+
else if (char === '}') {
|
|
2940
|
+
depth -= 1;
|
|
2941
|
+
if (depth === 0)
|
|
2942
|
+
return index;
|
|
2943
|
+
}
|
|
2944
|
+
}
|
|
2945
|
+
return -1;
|
|
2946
|
+
}
|
|
2947
|
+
function propNameForExpression(expr) {
|
|
2948
|
+
const direct = expr.match(/^[A-Za-z_$][A-Za-z0-9_$]*$/);
|
|
2949
|
+
if (direct)
|
|
2950
|
+
return direct[0];
|
|
2951
|
+
// Match both `this.props.X` (class-component prop access, emitted by the
|
|
2952
|
+
// class-component transform) and `props.X` (functional-component prop
|
|
2953
|
+
// access, emitted by `rewriteFnComponent`). Without the `props.X` branch,
|
|
2954
|
+
// lowering fails for every wrapper-style functional component (View, Text,
|
|
2955
|
+
// Button, …) because their slot expressions read `props.class`, etc., and
|
|
2956
|
+
// we need to substitute those into the parent's bound prop value.
|
|
2957
|
+
const propsAccess = expr.match(/^(?:this\.)?props\.([A-Za-z_$][A-Za-z0-9_$]*)$/);
|
|
2958
|
+
return propsAccess ? propsAccess[1] : null;
|
|
2959
|
+
}
|
|
2960
|
+
function simpleIdentifierValue(expr) {
|
|
2961
|
+
const text = expr.trim();
|
|
2962
|
+
return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(text) ? text : null;
|
|
2963
|
+
}
|
|
2964
|
+
function cssPropertyName(name) {
|
|
2965
|
+
return name.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);
|
|
2966
|
+
}
|
|
2967
|
+
function storeFieldByName(name, storeFields, receiver) {
|
|
2968
|
+
const sanitized = sanitizeCppIdentifier(name);
|
|
2969
|
+
const matches = storeFields.filter((field) => field.fieldName === sanitized);
|
|
2970
|
+
if (matches.length === 1)
|
|
2971
|
+
return matches[0];
|
|
2972
|
+
// The field name collides across several bundled stores (e.g. an app store
|
|
2973
|
+
// and the built-in SettingsStore both declare `status`/`screen`). Use the
|
|
2974
|
+
// receiver to pick the store whose exported instance matches — `store.status`
|
|
2975
|
+
// resolves against the store exported as `store`. Without this the slot fails
|
|
2976
|
+
// to lower and the component falls back to a path that renders the raw `0`
|
|
2977
|
+
// text placeholder instead of the bound value.
|
|
2978
|
+
if (matches.length > 1 && receiver) {
|
|
2979
|
+
const recv = sanitizeCppIdentifier(receiver);
|
|
2980
|
+
const byReceiver = matches.filter((field) => field.storeGlobalName === recv);
|
|
2981
|
+
if (byReceiver.length === 1)
|
|
2982
|
+
return byReceiver[0];
|
|
2983
|
+
}
|
|
2984
|
+
return null;
|
|
2985
|
+
}
|
|
2986
|
+
function kindForShape(shape) {
|
|
2987
|
+
if (shape?.kind !== 'literal')
|
|
2988
|
+
return 'value';
|
|
2989
|
+
if (shape.valueType === 'string' || shape.valueType === 'number' || shape.valueType === 'boolean')
|
|
2990
|
+
return shape.valueType;
|
|
2991
|
+
return 'value';
|
|
2992
|
+
}
|
|
2993
|
+
function kindForStoreField(field) {
|
|
2994
|
+
const shaped = kindForShape(field.shape);
|
|
2995
|
+
if (shaped !== 'value')
|
|
2996
|
+
return shaped;
|
|
2997
|
+
const fieldType = field.fieldType.trim();
|
|
2998
|
+
if (fieldType === 'double' || fieldType === 'gea_number')
|
|
2999
|
+
return 'number';
|
|
3000
|
+
if (fieldType === 'std::string')
|
|
3001
|
+
return 'string';
|
|
3002
|
+
if (fieldType === 'bool')
|
|
3003
|
+
return 'boolean';
|
|
3004
|
+
return 'value';
|
|
3005
|
+
}
|
|
3006
|
+
function storeFieldReactiveDeps(field) {
|
|
3007
|
+
return field.isGetter ? (field.reactiveDeps ?? []) : [field.fieldName];
|
|
3008
|
+
}
|
|
3009
|
+
function mergeLowered(kind, expr, parts) {
|
|
3010
|
+
return {
|
|
3011
|
+
kind,
|
|
3012
|
+
expr,
|
|
3013
|
+
deps: parts.flatMap((part) => part.deps),
|
|
3014
|
+
fields: uniqueStoreFields(parts.flatMap((part) => part.fields)),
|
|
3015
|
+
};
|
|
3016
|
+
}
|
|
3017
|
+
function loweredAsKind(lowered, kind) {
|
|
3018
|
+
if (kind === 'string')
|
|
3019
|
+
return loweredAsString(lowered);
|
|
3020
|
+
if (kind === 'boolean')
|
|
3021
|
+
return loweredAsBoolean(lowered);
|
|
3022
|
+
if (kind === 'number')
|
|
3023
|
+
return lowered.kind === 'number' ? lowered.expr : `gea::runtime::coerce::to_number(${lowered.expr})`;
|
|
3024
|
+
return lowered.expr;
|
|
3025
|
+
}
|
|
3026
|
+
function loweredAsNumberOperand(lowered) {
|
|
3027
|
+
return `(${loweredAsKind(lowered, 'number')})`;
|
|
3028
|
+
}
|
|
3029
|
+
function loweredAsString(lowered) {
|
|
3030
|
+
return lowered.kind === 'string' ? lowered.expr : `gea_cpp_to_string(${lowered.expr})`;
|
|
3031
|
+
}
|
|
3032
|
+
function loweredAsBoolean(lowered) {
|
|
3033
|
+
if (lowered.kind === 'boolean')
|
|
3034
|
+
return lowered.expr;
|
|
3035
|
+
return `gea::runtime::coerce::to_boolean(${lowered.expr})`;
|
|
3036
|
+
}
|
|
3037
|
+
function loweredAsComparable(lowered) {
|
|
3038
|
+
if (lowered.kind === 'string')
|
|
3039
|
+
return lowered.expr;
|
|
3040
|
+
if (lowered.kind === 'number' || lowered.kind === 'boolean')
|
|
3041
|
+
return lowered.expr;
|
|
3042
|
+
return `gea_cpp_key(${lowered.expr})`;
|
|
3043
|
+
}
|
|
3044
|
+
function loweredEqualityExpression(left, right, op) {
|
|
3045
|
+
// When both operands are the SAME comparable primitive (string/number/boolean),
|
|
3046
|
+
// emit a NATIVE C++ comparison — never re-box a typed std::string/double/bool
|
|
3047
|
+
// through gea_cpp_key just to run `==`. For matching static types JS `==` and
|
|
3048
|
+
// `===` are equivalent (no coercion happens), so both collapse to native `==`,
|
|
3049
|
+
// and `!=`/`!==` to native `!=`. Only genuinely dynamic or mixed-type operands
|
|
3050
|
+
// fall through to the boxed gea_cpp_value path below. Mirrors loweredAsComparable.
|
|
3051
|
+
const nativeComparable = left.kind === right.kind && (left.kind === 'string' || left.kind === 'number' || left.kind === 'boolean');
|
|
3052
|
+
if (nativeComparable) {
|
|
3053
|
+
if (op === '===' || op === '==')
|
|
3054
|
+
return `(${left.expr} == ${right.expr})`;
|
|
3055
|
+
if (op === '!==' || op === '!=')
|
|
3056
|
+
return `(${left.expr} != ${right.expr})`;
|
|
3057
|
+
}
|
|
3058
|
+
if (op === '===')
|
|
3059
|
+
return `gea_cpp_strict_equals(gea_cpp_key(${left.expr}), gea_cpp_key(${right.expr}))`;
|
|
3060
|
+
if (op === '==')
|
|
3061
|
+
return `gea_cpp_loose_equals(gea_cpp_key(${left.expr}), gea_cpp_key(${right.expr}))`;
|
|
3062
|
+
if (op === '!==')
|
|
3063
|
+
return `(!gea_cpp_strict_equals(gea_cpp_key(${left.expr}), gea_cpp_key(${right.expr})))`;
|
|
3064
|
+
if (op === '!=')
|
|
3065
|
+
return `(!gea_cpp_loose_equals(gea_cpp_key(${left.expr}), gea_cpp_key(${right.expr})))`;
|
|
3066
|
+
return null;
|
|
3067
|
+
}
|
|
3068
|
+
function cppBinaryOperator(op) {
|
|
3069
|
+
if (op === '===' || op === '==')
|
|
3070
|
+
return '==';
|
|
3071
|
+
if (op === '!==' || op === '!=')
|
|
3072
|
+
return '!=';
|
|
3073
|
+
return op;
|
|
3074
|
+
}
|
|
3075
|
+
function stripOuterParens(expr) {
|
|
3076
|
+
let text = expr.trim();
|
|
3077
|
+
while (text.startsWith('(') && text.endsWith(')') && matchingOuterParens(text)) {
|
|
3078
|
+
text = text.slice(1, -1).trim();
|
|
3079
|
+
}
|
|
3080
|
+
return text;
|
|
3081
|
+
}
|
|
3082
|
+
function matchingOuterParens(expr) {
|
|
3083
|
+
let depth = 0;
|
|
3084
|
+
let quote = null;
|
|
3085
|
+
let escaped = false;
|
|
3086
|
+
for (let index = 0; index < expr.length; index += 1) {
|
|
3087
|
+
const char = expr[index];
|
|
3088
|
+
if (quote) {
|
|
3089
|
+
if (escaped)
|
|
3090
|
+
escaped = false;
|
|
3091
|
+
else if (char === '\\')
|
|
3092
|
+
escaped = true;
|
|
3093
|
+
else if (char === quote)
|
|
3094
|
+
quote = null;
|
|
3095
|
+
continue;
|
|
3096
|
+
}
|
|
3097
|
+
if (char === '"' || char === "'") {
|
|
3098
|
+
quote = char;
|
|
3099
|
+
continue;
|
|
3100
|
+
}
|
|
3101
|
+
if (char === '(')
|
|
3102
|
+
depth += 1;
|
|
3103
|
+
else if (char === ')') {
|
|
3104
|
+
depth -= 1;
|
|
3105
|
+
if (depth === 0 && index < expr.length - 1)
|
|
3106
|
+
return false;
|
|
3107
|
+
}
|
|
3108
|
+
}
|
|
3109
|
+
return depth === 0;
|
|
3110
|
+
}
|
|
3111
|
+
function splitTopLevelTernary(expr) {
|
|
3112
|
+
let quote = null;
|
|
3113
|
+
let escaped = false;
|
|
3114
|
+
let depth = 0;
|
|
3115
|
+
let question = -1;
|
|
3116
|
+
let ternaryDepth = 0;
|
|
3117
|
+
for (let index = 0; index < expr.length; index += 1) {
|
|
3118
|
+
const char = expr[index];
|
|
3119
|
+
if (quote) {
|
|
3120
|
+
if (escaped)
|
|
3121
|
+
escaped = false;
|
|
3122
|
+
else if (char === '\\')
|
|
3123
|
+
escaped = true;
|
|
3124
|
+
else if (char === quote)
|
|
3125
|
+
quote = null;
|
|
3126
|
+
continue;
|
|
3127
|
+
}
|
|
3128
|
+
if (char === '"' || char === "'") {
|
|
3129
|
+
quote = char;
|
|
3130
|
+
continue;
|
|
3131
|
+
}
|
|
3132
|
+
if (char === '(' || char === '[' || char === '{')
|
|
3133
|
+
depth += 1;
|
|
3134
|
+
else if (char === ')' || char === ']' || char === '}')
|
|
3135
|
+
depth -= 1;
|
|
3136
|
+
else if (char === '?' && depth === 0) {
|
|
3137
|
+
if (question < 0)
|
|
3138
|
+
question = index;
|
|
3139
|
+
else
|
|
3140
|
+
ternaryDepth += 1;
|
|
3141
|
+
}
|
|
3142
|
+
else if (char === ':' && depth === 0 && question >= 0) {
|
|
3143
|
+
if (ternaryDepth > 0) {
|
|
3144
|
+
ternaryDepth -= 1;
|
|
3145
|
+
continue;
|
|
3146
|
+
}
|
|
3147
|
+
return {
|
|
3148
|
+
test: expr.slice(0, question).trim(),
|
|
3149
|
+
consequent: expr.slice(question + 1, index).trim(),
|
|
3150
|
+
alternate: expr.slice(index + 1).trim(),
|
|
3151
|
+
};
|
|
3152
|
+
}
|
|
3153
|
+
}
|
|
3154
|
+
return null;
|
|
3155
|
+
}
|
|
3156
|
+
function splitTopLevelBinary(expr) {
|
|
3157
|
+
for (const op of ['===', '!==', '>=', '<=', '==', '!=', '>', '<']) {
|
|
3158
|
+
const index = findTopLevelOperator(expr, op);
|
|
3159
|
+
if (index < 0)
|
|
3160
|
+
continue;
|
|
3161
|
+
return {
|
|
3162
|
+
left: expr.slice(0, index).trim(),
|
|
3163
|
+
op,
|
|
3164
|
+
right: expr.slice(index + op.length).trim(),
|
|
3165
|
+
};
|
|
3166
|
+
}
|
|
3167
|
+
return null;
|
|
3168
|
+
}
|
|
3169
|
+
function splitTopLevelArithmetic(expr) {
|
|
3170
|
+
return splitLastTopLevelOperator(expr, ['-']) ?? splitLastTopLevelOperator(expr, ['*', '/']);
|
|
3171
|
+
}
|
|
3172
|
+
function splitLastTopLevelOperator(expr, operators) {
|
|
3173
|
+
const match = findLastTopLevelOperator(expr, operators);
|
|
3174
|
+
if (!match || match.index <= 0)
|
|
3175
|
+
return null;
|
|
3176
|
+
const left = expr.slice(0, match.index).trim();
|
|
3177
|
+
const right = expr.slice(match.index + match.op.length).trim();
|
|
3178
|
+
return left && right ? { left, op: match.op, right } : null;
|
|
3179
|
+
}
|
|
3180
|
+
function findLastTopLevelOperator(expr, operators) {
|
|
3181
|
+
let quote = null;
|
|
3182
|
+
let escaped = false;
|
|
3183
|
+
let depth = 0;
|
|
3184
|
+
let last = null;
|
|
3185
|
+
for (let index = 0; index < expr.length; index += 1) {
|
|
3186
|
+
const char = expr[index];
|
|
3187
|
+
if (quote) {
|
|
3188
|
+
if (escaped)
|
|
3189
|
+
escaped = false;
|
|
3190
|
+
else if (char === '\\')
|
|
3191
|
+
escaped = true;
|
|
3192
|
+
else if (char === quote)
|
|
3193
|
+
quote = null;
|
|
3194
|
+
continue;
|
|
3195
|
+
}
|
|
3196
|
+
if (char === '"' || char === "'") {
|
|
3197
|
+
quote = char;
|
|
3198
|
+
continue;
|
|
3199
|
+
}
|
|
3200
|
+
if (char === '(' || char === '[' || char === '{')
|
|
3201
|
+
depth += 1;
|
|
3202
|
+
else if (char === ')' || char === ']' || char === '}')
|
|
3203
|
+
depth -= 1;
|
|
3204
|
+
if (depth !== 0)
|
|
3205
|
+
continue;
|
|
3206
|
+
for (const op of operators) {
|
|
3207
|
+
if (index === 0 || !expr.startsWith(op, index))
|
|
3208
|
+
continue;
|
|
3209
|
+
last = { index, op };
|
|
3210
|
+
}
|
|
3211
|
+
}
|
|
3212
|
+
return last;
|
|
3213
|
+
}
|
|
3214
|
+
function splitTopLevelOperator(expr, operator) {
|
|
3215
|
+
const parts = [];
|
|
3216
|
+
let start = 0;
|
|
3217
|
+
let quote = null;
|
|
3218
|
+
let escaped = false;
|
|
3219
|
+
let depth = 0;
|
|
3220
|
+
for (let index = 0; index < expr.length; index += 1) {
|
|
3221
|
+
const char = expr[index];
|
|
3222
|
+
if (quote) {
|
|
3223
|
+
if (escaped)
|
|
3224
|
+
escaped = false;
|
|
3225
|
+
else if (char === '\\')
|
|
3226
|
+
escaped = true;
|
|
3227
|
+
else if (char === quote)
|
|
3228
|
+
quote = null;
|
|
3229
|
+
continue;
|
|
3230
|
+
}
|
|
3231
|
+
if (char === '"' || char === "'") {
|
|
3232
|
+
quote = char;
|
|
3233
|
+
continue;
|
|
3234
|
+
}
|
|
3235
|
+
if (char === '(' || char === '[' || char === '{')
|
|
3236
|
+
depth += 1;
|
|
3237
|
+
else if (char === ')' || char === ']' || char === '}')
|
|
3238
|
+
depth -= 1;
|
|
3239
|
+
else if (depth === 0 && expr.startsWith(operator, index)) {
|
|
3240
|
+
parts.push(expr.slice(start, index).trim());
|
|
3241
|
+
start = index + operator.length;
|
|
3242
|
+
index += operator.length - 1;
|
|
3243
|
+
}
|
|
3244
|
+
}
|
|
3245
|
+
parts.push(expr.slice(start).trim());
|
|
3246
|
+
return parts.filter(Boolean);
|
|
3247
|
+
}
|
|
3248
|
+
function findTopLevelOperator(expr, operator) {
|
|
3249
|
+
let quote = null;
|
|
3250
|
+
let escaped = false;
|
|
3251
|
+
let depth = 0;
|
|
3252
|
+
for (let index = 0; index < expr.length; index += 1) {
|
|
3253
|
+
const char = expr[index];
|
|
3254
|
+
if (quote) {
|
|
3255
|
+
if (escaped)
|
|
3256
|
+
escaped = false;
|
|
3257
|
+
else if (char === '\\')
|
|
3258
|
+
escaped = true;
|
|
3259
|
+
else if (char === quote)
|
|
3260
|
+
quote = null;
|
|
3261
|
+
continue;
|
|
3262
|
+
}
|
|
3263
|
+
if (char === '"' || char === "'") {
|
|
3264
|
+
quote = char;
|
|
3265
|
+
continue;
|
|
3266
|
+
}
|
|
3267
|
+
if (char === '(' || char === '[' || char === '{')
|
|
3268
|
+
depth += 1;
|
|
3269
|
+
else if (char === ')' || char === ']' || char === '}')
|
|
3270
|
+
depth -= 1;
|
|
3271
|
+
else if (depth === 0 && expr.startsWith(operator, index))
|
|
3272
|
+
return index;
|
|
3273
|
+
}
|
|
3274
|
+
return -1;
|
|
3275
|
+
}
|
|
3276
|
+
function numericLiteralValue(expr) {
|
|
3277
|
+
const text = expr.trim();
|
|
3278
|
+
return /^-?(?:\d+|\d+\.\d+|\.\d+)$/.test(text) ? text : null;
|
|
3279
|
+
}
|
|
3280
|
+
function opaqueHexColorExpressionForStyleValue(expr, constants, propBindings) {
|
|
3281
|
+
const literal = staticStringStyleValue(expr, constants, propBindings);
|
|
3282
|
+
const color = literal === null ? null : opaqueCssHexColor(literal);
|
|
3283
|
+
return color ? nativeStyleColorExpression(color) : null;
|
|
3284
|
+
}
|
|
3285
|
+
function staticStringStyleValue(expr, constants, propBindings, seenProps = new Set()) {
|
|
3286
|
+
if (!expr)
|
|
3287
|
+
return null;
|
|
3288
|
+
const text = stripOuterParens(expr.trim());
|
|
3289
|
+
const literal = stringLiteralValue(text);
|
|
3290
|
+
if (literal !== null)
|
|
3291
|
+
return literal;
|
|
3292
|
+
const prop = propNameForExpression(text);
|
|
3293
|
+
if (!prop)
|
|
3294
|
+
return null;
|
|
3295
|
+
const binding = propBindings.get(prop);
|
|
3296
|
+
if (binding) {
|
|
3297
|
+
if (seenProps.has(prop))
|
|
3298
|
+
return null;
|
|
3299
|
+
const nextSeen = new Set(seenProps);
|
|
3300
|
+
nextSeen.add(prop);
|
|
3301
|
+
return staticStringStyleValue(binding.expr, constants, binding.parentProps, nextSeen);
|
|
3302
|
+
}
|
|
3303
|
+
const constant = constants.get(prop);
|
|
3304
|
+
return constant?.valueType === 'string' ? constant.value : null;
|
|
3305
|
+
}
|
|
3306
|
+
function stringLiteralValue(expr) {
|
|
3307
|
+
const quote = expr[0];
|
|
3308
|
+
if ((quote !== '"' && quote !== "'") || expr[expr.length - 1] !== quote)
|
|
3309
|
+
return null;
|
|
3310
|
+
return expr
|
|
3311
|
+
.slice(1, -1)
|
|
3312
|
+
.replace(/\\n/g, '\n')
|
|
3313
|
+
.replace(/\\t/g, '\t')
|
|
3314
|
+
.replace(/\\'/g, "'")
|
|
3315
|
+
.replace(/\\"/g, '"')
|
|
3316
|
+
.replace(/\\\\/g, '\\');
|
|
3317
|
+
}
|
|
3318
|
+
function uniqueStoreFields(fields) {
|
|
3319
|
+
const seen = new Set();
|
|
3320
|
+
const out = [];
|
|
3321
|
+
for (const field of fields) {
|
|
3322
|
+
const key = `${field.receiverName ?? ''}.${field.storeClass}.${field.fieldName}`;
|
|
3323
|
+
if (seen.has(key))
|
|
3324
|
+
continue;
|
|
3325
|
+
seen.add(key);
|
|
3326
|
+
out.push(field);
|
|
3327
|
+
}
|
|
3328
|
+
return out;
|
|
3329
|
+
}
|
|
3330
|
+
function uniqueStrings(values) {
|
|
3331
|
+
return [...new Set(values)];
|
|
3332
|
+
}
|
|
3333
|
+
export function parseTemplateRoot(html) {
|
|
3334
|
+
const children = parseTemplateFragmentChildren(html);
|
|
3335
|
+
return children?.find((child) => child.kind === 'element') ?? null;
|
|
3336
|
+
}
|
|
3337
|
+
function parseTemplateFragmentChildren(html) {
|
|
3338
|
+
const fragment = { kind: 'element', tag: '#fragment', children: [] };
|
|
3339
|
+
const stack = [fragment];
|
|
3340
|
+
let index = 0;
|
|
3341
|
+
while (index < html.length) {
|
|
3342
|
+
if (html.startsWith('<!--', index)) {
|
|
3343
|
+
const end = html.indexOf('-->', index + 4);
|
|
3344
|
+
if (end < 0)
|
|
3345
|
+
return null;
|
|
3346
|
+
const value = Number.parseInt(html.slice(index + 4, end).trim(), 10);
|
|
3347
|
+
if (Number.isFinite(value))
|
|
3348
|
+
stack[stack.length - 1].children.push({ kind: 'slot', index: value });
|
|
3349
|
+
index = end + 3;
|
|
3350
|
+
continue;
|
|
3351
|
+
}
|
|
3352
|
+
if (html.startsWith('</', index)) {
|
|
3353
|
+
const end = html.indexOf('>', index + 2);
|
|
3354
|
+
if (end < 0)
|
|
3355
|
+
break;
|
|
3356
|
+
if (stack.length > 1)
|
|
3357
|
+
stack.pop();
|
|
3358
|
+
index = end + 1;
|
|
3359
|
+
continue;
|
|
3360
|
+
}
|
|
3361
|
+
if (html[index] === '<') {
|
|
3362
|
+
const end = html.indexOf('>', index + 1);
|
|
3363
|
+
if (end < 0)
|
|
3364
|
+
break;
|
|
3365
|
+
const raw = html.slice(index + 1, end).trim();
|
|
3366
|
+
const selfClosing = raw.endsWith('/');
|
|
3367
|
+
const tag = raw.match(/^([A-Za-z][A-Za-z0-9:-]*)/)?.[1];
|
|
3368
|
+
if (!tag)
|
|
3369
|
+
return null;
|
|
3370
|
+
const attrs = parseStaticAttrs(raw);
|
|
3371
|
+
const child = { kind: 'element', tag, children: [], ...(attrs.class ? { className: attrs.class } : {}), attrs };
|
|
3372
|
+
stack[stack.length - 1].children.push(child);
|
|
3373
|
+
if (!selfClosing && !isVoidTag(tag))
|
|
3374
|
+
stack.push(child);
|
|
3375
|
+
index = end + 1;
|
|
3376
|
+
continue;
|
|
3377
|
+
}
|
|
3378
|
+
const next = html.indexOf('<', index);
|
|
3379
|
+
const end = next < 0 ? html.length : next;
|
|
3380
|
+
const text = html.slice(index, end);
|
|
3381
|
+
if (text.trim())
|
|
3382
|
+
stack[stack.length - 1].children.push({ kind: 'text', text });
|
|
3383
|
+
index = end;
|
|
3384
|
+
}
|
|
3385
|
+
return fragment.children;
|
|
3386
|
+
}
|
|
3387
|
+
function pathKey(path) {
|
|
3388
|
+
return path.join('.');
|
|
3389
|
+
}
|
|
3390
|
+
function escapeRegExp(value) {
|
|
3391
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
3392
|
+
}
|
|
3393
|
+
function parseStaticAttrs(rawTag) {
|
|
3394
|
+
const attrs = {};
|
|
3395
|
+
const attrPattern = /\s([A-Za-z_:][A-Za-z0-9_:.-]*)(?:=(?:"([^"]*)"|'([^']*)'|([^ >]+)))?/g;
|
|
3396
|
+
let match;
|
|
3397
|
+
while ((match = attrPattern.exec(rawTag))) {
|
|
3398
|
+
attrs[match[1]] = match[2] ?? match[3] ?? match[4] ?? 'true';
|
|
3399
|
+
}
|
|
3400
|
+
return attrs;
|
|
3401
|
+
}
|
|
3402
|
+
function isTextNodeTag(tag) {
|
|
3403
|
+
const normalized = tag.toLowerCase();
|
|
3404
|
+
return normalized === 'span' || normalized === 'p' || /^h[1-6]$/.test(normalized);
|
|
3405
|
+
}
|
|
3406
|
+
function canUseTextNodeForElement(node) {
|
|
3407
|
+
if (!isTextNodeTag(node.tag))
|
|
3408
|
+
return false;
|
|
3409
|
+
if (node.children.some((child) => child.kind === 'element'))
|
|
3410
|
+
return false;
|
|
3411
|
+
// A Text node holds exactly ONE run of text. Collapse the element into a text
|
|
3412
|
+
// node only when it has a single text-producing child (one static text, or one
|
|
3413
|
+
// reactive placeholder). An element with multiple runs — e.g.
|
|
3414
|
+
// `<span>{a} / {b}</span>` (children: slot, " / ", slot) — must stay a VIEW
|
|
3415
|
+
// container so each run becomes its own child text node laid out side by side.
|
|
3416
|
+
// Baking one run into the parent via `setText` while appending the others as
|
|
3417
|
+
// children stacks them all at the parent's origin (the device-only overlap
|
|
3418
|
+
// where "0 / 2" renders as one garbled glyph).
|
|
3419
|
+
let runs = 0;
|
|
3420
|
+
for (const child of node.children) {
|
|
3421
|
+
if (child.kind === 'text') {
|
|
3422
|
+
if (renderableTemplateText(child.text))
|
|
3423
|
+
runs += 1;
|
|
3424
|
+
}
|
|
3425
|
+
else {
|
|
3426
|
+
// A slot placeholder (reactive text / inlined children) is a text run.
|
|
3427
|
+
runs += 1;
|
|
3428
|
+
}
|
|
3429
|
+
if (runs > 1)
|
|
3430
|
+
return false;
|
|
3431
|
+
}
|
|
3432
|
+
return true;
|
|
3433
|
+
}
|
|
3434
|
+
function intrinsicStyleLines(varName, node) {
|
|
3435
|
+
const normalized = node.tag.toLowerCase();
|
|
3436
|
+
const lines = [];
|
|
3437
|
+
// A paragraph carrying inline children (<p>copy <em>x</em> more</p>) used to be
|
|
3438
|
+
// lowered as `display: flex; flex-direction: row; flex-wrap: wrap`, because the
|
|
3439
|
+
// engine had no inline formatting and a plain block would have stacked the runs.
|
|
3440
|
+
// It does now: a block whose in-flow children are all inline-level synthesizes an
|
|
3441
|
+
// inline formatting context (LayoutNodePass::resolveInlineFormattingRow), which
|
|
3442
|
+
// flows real line boxes — baseline-aligned, always wrapping, never flex-shrunk,
|
|
3443
|
+
// and splitting a text run ACROSS lines so the box after it resumes on the run's
|
|
3444
|
+
// last line. Forcing display:flex here defeated all of that: setDefaultStyle goes
|
|
3445
|
+
// through the class-rule path, which marks `display` explicit, so the engine could
|
|
3446
|
+
// not tell the lowering apart from an authored `display: flex` and every run stayed
|
|
3447
|
+
// an atomic flex item.
|
|
3448
|
+
const headingSize = headingDefaultFontSize(normalized);
|
|
3449
|
+
if (headingSize !== null)
|
|
3450
|
+
lines.push(defaultStyleLine(varName, 'FontSize', headingSize));
|
|
3451
|
+
return lines;
|
|
3452
|
+
}
|
|
3453
|
+
function headingDefaultFontSize(tag) {
|
|
3454
|
+
if (tag === 'h1')
|
|
3455
|
+
return 34;
|
|
3456
|
+
if (tag === 'h2')
|
|
3457
|
+
return 28;
|
|
3458
|
+
if (tag === 'h3')
|
|
3459
|
+
return 24;
|
|
3460
|
+
if (tag === 'h4')
|
|
3461
|
+
return 20;
|
|
3462
|
+
if (tag === 'h5')
|
|
3463
|
+
return 18;
|
|
3464
|
+
if (tag === 'h6')
|
|
3465
|
+
return 16;
|
|
3466
|
+
return null;
|
|
3467
|
+
}
|
|
3468
|
+
function defaultStyleLine(varName, property, value) {
|
|
3469
|
+
return ` gea::embedded::ui::Tree::instance().setDefaultStyle(${varName}.id(), gea::embedded::ui::Property::${property}, ${value});`;
|
|
3470
|
+
}
|
|
3471
|
+
function isVoidTag(tag) {
|
|
3472
|
+
return ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr'].includes(tag.toLowerCase());
|
|
3473
|
+
}
|
|
3474
|
+
function uiCreateNodeExpression(node) {
|
|
3475
|
+
const normalized = node.tag.toLowerCase();
|
|
3476
|
+
if (normalized === 'canvas')
|
|
3477
|
+
return 'gea::embedded::ui::Document::instance().createCanvas()';
|
|
3478
|
+
if (normalized === 'camera')
|
|
3479
|
+
return 'gea::embedded::ui::Document::instance().createCamera()';
|
|
3480
|
+
if (normalized === 'audio')
|
|
3481
|
+
return 'gea::embedded::ui::Document::instance().createAudio()';
|
|
3482
|
+
if (normalized === 'img')
|
|
3483
|
+
return 'gea::embedded::ui::Document::instance().createImage()';
|
|
3484
|
+
if (normalized === 'virtual-list')
|
|
3485
|
+
return 'gea::embedded::ui::Document::instance().createVirtualList()';
|
|
3486
|
+
if (normalized === 'input' && node.attrs?.type === 'button')
|
|
3487
|
+
return 'gea::embedded::ui::Document::instance().createButton()';
|
|
3488
|
+
if (normalized === 'button')
|
|
3489
|
+
return 'gea::embedded::ui::Document::instance().createButton()';
|
|
3490
|
+
// The IR wraps fragment / component-rooted templates in
|
|
3491
|
+
// `<span style="display:contents">…</span>`. That wrapper is a container,
|
|
3492
|
+
// not a text run — treat it as a view so its mount/keyed-list/conditional
|
|
3493
|
+
// children get proper child nodes appended. Without this, the wrapper
|
|
3494
|
+
// collapses to a text node and `canMountComponent` fails for any
|
|
3495
|
+
// functional component whose root JSX is itself a component (SettingsPanel,
|
|
3496
|
+
// SettingsContent, all the wrapper-style library components).
|
|
3497
|
+
if (isTransparentFragmentWrapper(node))
|
|
3498
|
+
return 'gea::embedded::ui::Document::instance().createView()';
|
|
3499
|
+
if (canUseTextNodeForElement(node))
|
|
3500
|
+
return 'gea::embedded::ui::Document::instance().createText()';
|
|
3501
|
+
return 'gea::embedded::ui::Document::instance().createView()';
|
|
3502
|
+
}
|
|
3503
|
+
function isTransparentFragmentWrapper(node) {
|
|
3504
|
+
if (node.tag.toLowerCase() !== 'span')
|
|
3505
|
+
return false;
|
|
3506
|
+
const attrs = node.attrs ?? {};
|
|
3507
|
+
const keys = Object.keys(attrs);
|
|
3508
|
+
if (keys.length !== 1 || keys[0] !== 'style')
|
|
3509
|
+
return false;
|
|
3510
|
+
return /\bdisplay\s*:\s*contents\b/i.test(attrs.style);
|
|
3511
|
+
}
|
|
3512
|
+
// True when the slot's expression is a bare `props.X` reference and the
|
|
3513
|
+
// parent didn't pass `X`. Used so wrapper components like `<View>` — which
|
|
3514
|
+
// expose lots of optional props (`style`, `pressId`, `onClick`, `children`,
|
|
3515
|
+
// etc.) — can be inlined as direct-mount children even when the parent only
|
|
3516
|
+
// binds a subset. Conservative: anything beyond a bare `props.X` (composite
|
|
3517
|
+
// expressions, ternaries that read multiple props, etc.) is left to the
|
|
3518
|
+
// normal lowering pipeline, which still emits the right code when the props
|
|
3519
|
+
// are bound and still fails loudly when they're not.
|
|
3520
|
+
function slotReferencesUnboundProp(slot, propBindings) {
|
|
3521
|
+
if (slot.exprPath && slot.exprPath.length === 2 && slot.exprPath[0] === 'props') {
|
|
3522
|
+
return !propBindings.has(slot.exprPath[1]);
|
|
3523
|
+
}
|
|
3524
|
+
if (slot.expr) {
|
|
3525
|
+
const match = slot.expr.match(/^props\.([A-Za-z_$][A-Za-z0-9_$]*)$/);
|
|
3526
|
+
if (match)
|
|
3527
|
+
return !propBindings.has(match[1]);
|
|
3528
|
+
}
|
|
3529
|
+
return false;
|
|
3530
|
+
}
|