@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,1263 @@
|
|
|
1
|
+
import { arrayElementAliasFieldTarget, arrayItemAliasKey, arrayItemFieldAliasKey, arrayItemFieldHint, fieldHint, localArrayCppType, localArrayTypeRef, nextStoreMethodTemp, typedArrayItemFieldAccess } from './cpp-store-method-context.js';
|
|
2
|
+
import { exprPath, thisArrayItemFieldTarget, thisArrayLengthTarget, thisArrayPopTarget, thisArrayPushTarget, thisArrayShiftTarget, thisArraySpliceTarget, thisArrayUnshiftTarget, thisFieldName, } from './cpp-store-method-targets.js';
|
|
3
|
+
import { sanitizeCppIdentifier } from './utils.js';
|
|
4
|
+
export function lowerExpr(context, expr, hint) {
|
|
5
|
+
if (expr.kind === 'number')
|
|
6
|
+
return constantExpr(expr.value, hint);
|
|
7
|
+
if (expr.kind === 'string')
|
|
8
|
+
return hint === 'string' || hint === 'any' ? `std::string(${JSON.stringify(expr.value)})` : JSON.stringify(expr.value);
|
|
9
|
+
if (expr.kind === 'boolean')
|
|
10
|
+
return constantExpr(expr.value, hint);
|
|
11
|
+
if (expr.kind === 'null')
|
|
12
|
+
return constantExpr(null, hint);
|
|
13
|
+
if (expr.kind === 'identifier')
|
|
14
|
+
return lowerIdentifier(context, expr.name, hint);
|
|
15
|
+
if (expr.kind === 'this')
|
|
16
|
+
return '(*this)';
|
|
17
|
+
if (expr.kind === 'member')
|
|
18
|
+
return lowerMember(context, expr, hint);
|
|
19
|
+
if (expr.kind === 'index')
|
|
20
|
+
return lowerIndex(context, expr, hint);
|
|
21
|
+
if (expr.kind === 'call')
|
|
22
|
+
return lowerCall(context, expr, hint);
|
|
23
|
+
if (expr.kind === 'object')
|
|
24
|
+
return lowerObject(context, expr);
|
|
25
|
+
if (expr.kind === 'array')
|
|
26
|
+
return lowerArray(context, expr);
|
|
27
|
+
if (expr.kind === 'unary') {
|
|
28
|
+
const arg = lowerExpr(context, expr.arg, expr.op === '!' ? 'boolean' : 'number');
|
|
29
|
+
return arg ? `${expr.op}${parenthesize(arg)}` : null;
|
|
30
|
+
}
|
|
31
|
+
if (expr.kind === 'binary' || expr.kind === 'logical')
|
|
32
|
+
return lowerBinary(context, expr, hint);
|
|
33
|
+
if (expr.kind === 'conditional')
|
|
34
|
+
return lowerConditional(context, expr, hint);
|
|
35
|
+
if (expr.kind === 'update')
|
|
36
|
+
return lowerForUpdate(context, expr);
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
export function lowerForUpdate(context, expr) {
|
|
40
|
+
if (expr.kind === 'update' && expr.arg.kind === 'identifier')
|
|
41
|
+
return `${expr.op === '++' ? '++' : '--'}${sanitizeCppIdentifier(expr.arg.name)}`;
|
|
42
|
+
return lowerExpr(context, expr, 'any');
|
|
43
|
+
}
|
|
44
|
+
export function assignmentHint(target) {
|
|
45
|
+
const path = exprPath(target);
|
|
46
|
+
if (path?.endsWith('.frequency.value'))
|
|
47
|
+
return 'number';
|
|
48
|
+
if (path?.endsWith('.type'))
|
|
49
|
+
return 'string';
|
|
50
|
+
return 'any';
|
|
51
|
+
}
|
|
52
|
+
// The JS URI globals lower to the gea::uri runtime helpers (same mapping as the
|
|
53
|
+
// geatsc core emitter's uriBuiltinRuntimeName). The store-method lowering path
|
|
54
|
+
// would otherwise emit a bare `fn_encodeURIComponent` that has no declaration.
|
|
55
|
+
const URI_BUILTIN_RUNTIME = {
|
|
56
|
+
decodeURI: 'gea::uri::decode',
|
|
57
|
+
decodeURIComponent: 'gea::uri::decode_component',
|
|
58
|
+
encodeURI: 'gea::uri::encode',
|
|
59
|
+
encodeURIComponent: 'gea::uri::encode_component',
|
|
60
|
+
escape: 'gea::uri::escape',
|
|
61
|
+
unescape: 'gea::uri::unescape'
|
|
62
|
+
};
|
|
63
|
+
export function globalStoreFieldTarget(context, expr) {
|
|
64
|
+
if (expr.kind !== 'member' || expr.object.kind !== 'identifier')
|
|
65
|
+
return null;
|
|
66
|
+
const field = globalStoreFieldPlan(context, expr.object.name, expr.property);
|
|
67
|
+
if (!field)
|
|
68
|
+
return null;
|
|
69
|
+
const storeAccess = globalStoreAccess(expr.object.name);
|
|
70
|
+
return {
|
|
71
|
+
access: `${storeAccess}->${field.fieldName}`,
|
|
72
|
+
storeAccess,
|
|
73
|
+
fieldName: field.fieldName,
|
|
74
|
+
hint: storeFieldHint(field.shape),
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
function globalStoreFieldPlan(context, receiver, property) {
|
|
78
|
+
if (!unshadowedGlobalStoreReceiver(context, receiver))
|
|
79
|
+
return null;
|
|
80
|
+
const receiverName = sanitizeCppIdentifier(receiver);
|
|
81
|
+
const fieldName = sanitizeCppIdentifier(property);
|
|
82
|
+
return context.storeFields.find((field) => field.storeGlobalName === receiverName && field.fieldName === fieldName) ?? null;
|
|
83
|
+
}
|
|
84
|
+
function globalStoreMethodPlan(context, receiver, method) {
|
|
85
|
+
if (!unshadowedGlobalStoreReceiver(context, receiver))
|
|
86
|
+
return null;
|
|
87
|
+
const receiverName = sanitizeCppIdentifier(receiver);
|
|
88
|
+
const methodName = sanitizeCppIdentifier(method);
|
|
89
|
+
return context.storeMethods.find((candidate) => candidate.storeGlobalName === receiverName && candidate.methodName === methodName) ?? null;
|
|
90
|
+
}
|
|
91
|
+
function unshadowedGlobalStoreReceiver(context, receiver) {
|
|
92
|
+
if (context.locals.has(receiver) || context.params.has(receiver) || context.constants.has(receiver))
|
|
93
|
+
return false;
|
|
94
|
+
const receiverName = sanitizeCppIdentifier(receiver);
|
|
95
|
+
return context.storeFields.some((field) => field.storeGlobalName === receiverName) || context.storeMethods.some((method) => method.storeGlobalName === receiverName);
|
|
96
|
+
}
|
|
97
|
+
function globalStoreAccess(receiver) {
|
|
98
|
+
return `__gea_global_${sanitizeCppIdentifier(receiver)}()`;
|
|
99
|
+
}
|
|
100
|
+
function storeFieldHint(shape) {
|
|
101
|
+
if (shape?.kind === 'literal') {
|
|
102
|
+
if (shape.valueType === 'boolean')
|
|
103
|
+
return 'boolean';
|
|
104
|
+
if (shape.valueType === 'number')
|
|
105
|
+
return 'number';
|
|
106
|
+
if (shape.valueType === 'string')
|
|
107
|
+
return 'string';
|
|
108
|
+
}
|
|
109
|
+
return 'any';
|
|
110
|
+
}
|
|
111
|
+
function lowerIdentifier(context, name, hint) {
|
|
112
|
+
const id = sanitizeCppIdentifier(name);
|
|
113
|
+
if (context.constants.has(name))
|
|
114
|
+
return constantExpr(context.constants.get(name), hint);
|
|
115
|
+
if (name === 'initBleServers')
|
|
116
|
+
return context.topLevelFunctionNames.get(name) ?? 'fn_initBleServers';
|
|
117
|
+
if (context.params.has(name)) {
|
|
118
|
+
const paramHint = context.paramHints.get(name);
|
|
119
|
+
if (hint === 'any' || paramHint === hint)
|
|
120
|
+
return id;
|
|
121
|
+
if (hint === 'number')
|
|
122
|
+
return `gea::runtime::coerce::to_number(${id})`;
|
|
123
|
+
if (hint === 'string')
|
|
124
|
+
return `gea_cpp_to_string(${id})`;
|
|
125
|
+
if (hint === 'boolean')
|
|
126
|
+
return `gea::runtime::coerce::to_boolean(${id})`;
|
|
127
|
+
}
|
|
128
|
+
if (!context.locals.has(name)) {
|
|
129
|
+
const topLevelValue = context.topLevelValueNames.get(name) ?? context.topLevelValueNames.get(id);
|
|
130
|
+
if (topLevelValue)
|
|
131
|
+
return coerceTopLevelValueForHint(topLevelValue, hint);
|
|
132
|
+
}
|
|
133
|
+
if (unshadowedGlobalStoreReceiver(context, name))
|
|
134
|
+
return globalStoreAccess(name);
|
|
135
|
+
if (hint === 'string')
|
|
136
|
+
return `gea_cpp_to_string(${id})`;
|
|
137
|
+
if (hint === 'boolean')
|
|
138
|
+
return `gea::runtime::coerce::to_boolean(${id})`;
|
|
139
|
+
return id;
|
|
140
|
+
}
|
|
141
|
+
function lowerMember(context, expr, hint) {
|
|
142
|
+
const path = exprPath(expr);
|
|
143
|
+
if (path === 'Date.now')
|
|
144
|
+
return 'gea_cpp_now_ms';
|
|
145
|
+
if (path === 'audioContext.currentTime')
|
|
146
|
+
return 'gea::host::audioContext.currentTime';
|
|
147
|
+
if (path === '__gea_audioContext.currentTime')
|
|
148
|
+
return 'gea::host::audioContext.currentTime';
|
|
149
|
+
if (path === 'audioContext.destination')
|
|
150
|
+
return 'gea::host::audioContext.destination';
|
|
151
|
+
if (path === '__gea_audioContext.destination')
|
|
152
|
+
return 'gea::host::audioContext.destination';
|
|
153
|
+
if (path === 'Accelerometer.tiltX')
|
|
154
|
+
return 'gea::host::Accelerometer.tiltX';
|
|
155
|
+
if (path === 'Accelerometer.tiltY')
|
|
156
|
+
return 'gea::host::Accelerometer.tiltY';
|
|
157
|
+
if (path === 'Accelerometer.accelerationX')
|
|
158
|
+
return 'gea::host::Accelerometer.accelerationX';
|
|
159
|
+
if (path === 'Accelerometer.accelerationY')
|
|
160
|
+
return 'gea::host::Accelerometer.accelerationY';
|
|
161
|
+
if (path === 'Accelerometer.accelerationZ')
|
|
162
|
+
return 'gea::host::Accelerometer.accelerationZ';
|
|
163
|
+
if (path === 'Accelerometer.gyroscopeX')
|
|
164
|
+
return 'gea::host::Accelerometer.gyroscopeX';
|
|
165
|
+
if (path === 'Accelerometer.gyroscopeY')
|
|
166
|
+
return 'gea::host::Accelerometer.gyroscopeY';
|
|
167
|
+
if (path === 'Accelerometer.gyroscopeZ')
|
|
168
|
+
return 'gea::host::Accelerometer.gyroscopeZ';
|
|
169
|
+
if (path === 'Camera.width')
|
|
170
|
+
return 'gea::host::Camera.width';
|
|
171
|
+
if (path === 'Camera.height')
|
|
172
|
+
return 'gea::host::Camera.height';
|
|
173
|
+
if (path === 'Camera.orientation')
|
|
174
|
+
return 'gea::host::Camera.orientation';
|
|
175
|
+
if (path === 'Camera.facing')
|
|
176
|
+
return 'gea::host::Camera.facing';
|
|
177
|
+
if (path === 'Camera.deviceCount')
|
|
178
|
+
return 'gea::host::Camera.deviceCount';
|
|
179
|
+
// Viewport dimensions — browser-style `window.innerWidth`/`innerHeight`
|
|
180
|
+
// and the framework's `Display.width`/`height`. Route through the host
|
|
181
|
+
// Display facade so orientation changes and viewport metrics share one source.
|
|
182
|
+
if (path === 'window.innerWidth' || path === 'Display.width' || path === 'display.width' || path === '__gea_Display.width') {
|
|
183
|
+
return 'gea::host::Display.width()';
|
|
184
|
+
}
|
|
185
|
+
if (path === 'window.innerHeight' || path === 'Display.height' || path === 'display.height' || path === '__gea_Display.height') {
|
|
186
|
+
return 'gea::host::Display.height()';
|
|
187
|
+
}
|
|
188
|
+
if (path === 'Display.nativeWidth' || path === 'display.nativeWidth' || path === '__gea_Display.nativeWidth') {
|
|
189
|
+
return 'gea::host::Display.nativeWidth()';
|
|
190
|
+
}
|
|
191
|
+
if (path === 'Display.nativeHeight' || path === 'display.nativeHeight' || path === '__gea_Display.nativeHeight') {
|
|
192
|
+
return 'gea::host::Display.nativeHeight()';
|
|
193
|
+
}
|
|
194
|
+
if (path === 'Display.orientation' || path === 'display.orientation' || path === '__gea_Display.orientation') {
|
|
195
|
+
return 'gea::host::Display.orientation()';
|
|
196
|
+
}
|
|
197
|
+
if (path === 'Display.supportedOrientations' || path === 'display.supportedOrientations' || path === '__gea_Display.supportedOrientations') {
|
|
198
|
+
return 'gea::host::Display.supportedOrientations()';
|
|
199
|
+
}
|
|
200
|
+
if (path === 'Display.autoRotate' || path === 'display.autoRotate' || path === '__gea_Display.autoRotate') {
|
|
201
|
+
return 'gea::host::Display.autoRotate()';
|
|
202
|
+
}
|
|
203
|
+
if (path === 'Display.pixelFormat' || path === 'display.pixelFormat' || path === '__gea_Display.pixelFormat') {
|
|
204
|
+
return 'gea::host::Display.pixelFormat()';
|
|
205
|
+
}
|
|
206
|
+
if (path === 'Display.panelPixelFormat' || path === 'display.panelPixelFormat' || path === '__gea_Display.panelPixelFormat') {
|
|
207
|
+
return 'gea::host::Display.panelPixelFormat()';
|
|
208
|
+
}
|
|
209
|
+
if (path === 'Display.supportedPixelFormats' || path === 'display.supportedPixelFormats' || path === '__gea_Display.supportedPixelFormats') {
|
|
210
|
+
return 'gea::host::Display.supportedPixelFormats()';
|
|
211
|
+
}
|
|
212
|
+
// Property access on `Display` — the framework's DisplayController
|
|
213
|
+
// imported from `gea-embedded`. The TS interface has getters (`ctx`)
|
|
214
|
+
// and methods, but on the C++ side `gea::host::DisplayFacade` exposes
|
|
215
|
+
// *everything* as a method (including `width()`/`height()`/`ctx()`).
|
|
216
|
+
// Emit with parens, matching what the top-level emitter does for
|
|
217
|
+
// `hostNamespaceProperty` access (see expression-core.ts ~line 792).
|
|
218
|
+
// Method calls like `Display.setFrameRate(...)` are handled separately
|
|
219
|
+
// in `lowerHostFacadeCall` below, so this only fires for bare reads.
|
|
220
|
+
// TODO: long-term, wire this emit path through the shared `hostNamespace*`
|
|
221
|
+
// registry in `geatsc/src/host-shims.ts` so plugins don't need to enumerate
|
|
222
|
+
// host facades twice (once for top-level, once for class methods).
|
|
223
|
+
if (expr.object.kind === 'identifier' && (expr.object.name === 'Display' || expr.object.name === 'display' || expr.object.name === '__gea_Display')) {
|
|
224
|
+
return `gea::host::Display.${sanitizeCppIdentifier(expr.property)}()`;
|
|
225
|
+
}
|
|
226
|
+
if (expr.property === 'length' && expr.object.kind === 'identifier' && localArrayCppType(context, expr.object.name)) {
|
|
227
|
+
return `static_cast<double>(${sanitizeCppIdentifier(expr.object.name)}.size())`;
|
|
228
|
+
}
|
|
229
|
+
const lengthField = thisArrayLengthTarget(expr);
|
|
230
|
+
if (lengthField)
|
|
231
|
+
return `static_cast<double>((*this).${sanitizeCppIdentifier(lengthField)}.size())`;
|
|
232
|
+
const field = thisFieldName(expr);
|
|
233
|
+
if (field) {
|
|
234
|
+
const value = `(*this).${sanitizeCppIdentifier(field)}`;
|
|
235
|
+
return coerceForHint(value, hint);
|
|
236
|
+
}
|
|
237
|
+
const globalField = globalStoreFieldTarget(context, expr);
|
|
238
|
+
if (globalField) {
|
|
239
|
+
return hint === 'any' || globalField.hint === 'any' || hint === globalField.hint
|
|
240
|
+
? globalField.access
|
|
241
|
+
: coerceForHint(globalField.access, hint);
|
|
242
|
+
}
|
|
243
|
+
// Read-only numeric DOM/element properties the host exposes as record
|
|
244
|
+
// literals (e.g. a <virtual-list>'s scrollTop and measured rowHeight). Lower
|
|
245
|
+
// them to a dynamic record read coerced to number, rather than a C++ member
|
|
246
|
+
// access on the gea_cpp_value.
|
|
247
|
+
if (expr.property === 'scrollTop' || expr.property === 'rowHeight') {
|
|
248
|
+
const object = lowerExpr(context, expr.object, 'any');
|
|
249
|
+
return object ? `gea::runtime::coerce::to_number(gea_cpp_key(${object}).record_get_literal("${expr.property}"))` : null;
|
|
250
|
+
}
|
|
251
|
+
const item = thisArrayItemFieldTarget(expr);
|
|
252
|
+
if (item) {
|
|
253
|
+
const alias = item.index.kind === 'identifier' ? context.arrayItemFieldAliases.get(arrayItemFieldAliasKey(item.arrayField, item.index.name, item.itemField)) : undefined;
|
|
254
|
+
if (alias)
|
|
255
|
+
return alias;
|
|
256
|
+
const index = lowerExpr(context, item.index, 'number');
|
|
257
|
+
if (!index)
|
|
258
|
+
return null;
|
|
259
|
+
const array = `(*this).${sanitizeCppIdentifier(item.arrayField)}`;
|
|
260
|
+
const field = JSON.stringify(item.itemField);
|
|
261
|
+
const indexTemp = nextStoreMethodTemp(context, 'index');
|
|
262
|
+
const directAccess = typedArrayItemFieldAccess(context.store, item.arrayField, item.itemField, `${array}[${indexTemp}]`);
|
|
263
|
+
if (directAccess) {
|
|
264
|
+
// Pick the lambda's return type from the FIELD's declared type, not the
|
|
265
|
+
// caller's hint: a string field read as a bare method argument or in a
|
|
266
|
+
// concatenation arrives with hint 'any'/'number', and a `-> double` lambda
|
|
267
|
+
// returning the std::string member fails to compile. Coerce to the caller's
|
|
268
|
+
// hint afterwards (e.g. number field in a string concat -> gea_cpp_to_string).
|
|
269
|
+
const itemHint = arrayItemFieldHint(context.store, item.arrayField, item.itemField);
|
|
270
|
+
let lambda;
|
|
271
|
+
if (itemHint === 'any') {
|
|
272
|
+
const member = sanitizeCppIdentifier(item.itemField);
|
|
273
|
+
const fieldType = `std::decay_t<decltype((${array})[0].${member})>`;
|
|
274
|
+
lambda = `([&]() -> ${fieldType} { using __GeaField = ${fieldType}; auto ${indexTemp} = static_cast<std::size_t>(${index}); if (${indexTemp} >= ${array}.size()) return __GeaField{}; return ${directAccess}; })()`;
|
|
275
|
+
}
|
|
276
|
+
else if (itemHint === 'string') {
|
|
277
|
+
lambda = `([&]() -> std::string { auto ${indexTemp} = static_cast<std::size_t>(${index}); if (${indexTemp} >= ${array}.size()) return std::string(); return ${directAccess}; })()`;
|
|
278
|
+
}
|
|
279
|
+
else if (itemHint === 'boolean') {
|
|
280
|
+
lambda = `([&]() -> bool { auto ${indexTemp} = static_cast<std::size_t>(${index}); if (${indexTemp} >= ${array}.size()) return false; return gea::runtime::coerce::to_boolean(${directAccess}); })()`;
|
|
281
|
+
}
|
|
282
|
+
else {
|
|
283
|
+
lambda = `([&]() -> double { auto ${indexTemp} = static_cast<std::size_t>(${index}); if (${indexTemp} >= ${array}.size()) return 0.0; return ${directAccess}; })()`;
|
|
284
|
+
}
|
|
285
|
+
return hint === 'any' || hint === itemHint ? lambda : coerceForHint(lambda, hint);
|
|
286
|
+
}
|
|
287
|
+
if (hint === 'string') {
|
|
288
|
+
return `([&]() -> std::string { auto ${indexTemp} = static_cast<std::size_t>(${index}); if (${indexTemp} >= ${array}.size()) return gea_ir::read_string_field(gea_cpp_value::missing(), ${field}); return gea_ir::read_string_field(${array}[${indexTemp}], ${field}); })()`;
|
|
289
|
+
}
|
|
290
|
+
if (hint === 'boolean') {
|
|
291
|
+
return `([&]() -> bool { auto ${indexTemp} = static_cast<std::size_t>(${index}); if (${indexTemp} >= ${array}.size()) return false; return gea::runtime::coerce::to_boolean(${array}[${indexTemp}].record_get_literal(${field})); })()`;
|
|
292
|
+
}
|
|
293
|
+
return `([&]() -> double { auto ${indexTemp} = static_cast<std::size_t>(${index}); if (${indexTemp} >= ${array}.size()) return gea_ir::read_number_field(gea_cpp_value::missing(), ${field}); return gea_ir::read_number_field(${array}[${indexTemp}], ${field}); })()`;
|
|
294
|
+
}
|
|
295
|
+
const aliasField = arrayElementAliasFieldTarget(context, expr);
|
|
296
|
+
if (aliasField) {
|
|
297
|
+
const { alias, itemField } = aliasField;
|
|
298
|
+
const itemHint = arrayItemFieldHint(context.store, alias.arrayField, itemField);
|
|
299
|
+
const directAccess = typedArrayItemFieldAccess(context.store, alias.arrayField, itemField, alias.itemExpression);
|
|
300
|
+
if (directAccess) {
|
|
301
|
+
return hint === 'any' || hint === itemHint ? directAccess : coerceForHint(directAccess, hint);
|
|
302
|
+
}
|
|
303
|
+
const field = JSON.stringify(itemField);
|
|
304
|
+
if (hint === 'string')
|
|
305
|
+
return `gea_ir::read_string_field(${alias.itemExpression}, ${field})`;
|
|
306
|
+
if (hint === 'boolean')
|
|
307
|
+
return `gea::runtime::coerce::to_boolean(${alias.itemExpression}.record_get_literal(${field}))`;
|
|
308
|
+
return `gea_ir::read_number_field(${alias.itemExpression}, ${field})`;
|
|
309
|
+
}
|
|
310
|
+
// `localStorage.length` — the receiver is the host global, not a C++ value, so
|
|
311
|
+
// it can't go through the generic length helper below (which would reference an
|
|
312
|
+
// undeclared `localStorage`). Read the facade's count member directly.
|
|
313
|
+
if (expr.property === 'length' &&
|
|
314
|
+
expr.object.kind === 'identifier' &&
|
|
315
|
+
expr.object.name === 'localStorage' &&
|
|
316
|
+
!context.locals.has('localStorage') &&
|
|
317
|
+
!context.params.has('localStorage') &&
|
|
318
|
+
!context.constants.has('localStorage')) {
|
|
319
|
+
return coerceForHint('gea::host::Storage.length', hint);
|
|
320
|
+
}
|
|
321
|
+
if (expr.property === 'length') {
|
|
322
|
+
const object = lowerExpr(context, expr.object, 'any');
|
|
323
|
+
if (!object)
|
|
324
|
+
return null;
|
|
325
|
+
return coerceForHint(`([&](const auto &__gea_v) -> double { using __GeaLenValue = std::decay_t<decltype(__gea_v)>; if constexpr (std::is_same_v<__GeaLenValue, std::string>) { return static_cast<double>(gea_cpp_string_utf16_length(__gea_v)); } else if constexpr (requires { __gea_v.size(); }) { return static_cast<double>(__gea_v.size()); } else { return static_cast<double>(gea_cpp_string_utf16_length(gea_cpp_to_string(__gea_v))); } })(${object})`, hint);
|
|
326
|
+
}
|
|
327
|
+
// A method param without a primitive hint arrives in the generated signature
|
|
328
|
+
// as boxed gea_cpp_value — raw `.member` access on it is a miscompile (the
|
|
329
|
+
// C++ value type has no such member). Bail the method to the generic
|
|
330
|
+
// fallback rather than emit invalid code.
|
|
331
|
+
if (expr.object.kind === 'identifier' && context.params.has(expr.object.name) && !context.paramHints.has(expr.object.name)) {
|
|
332
|
+
return null;
|
|
333
|
+
}
|
|
334
|
+
const object = lowerExpr(context, expr.object, 'any');
|
|
335
|
+
return object ? coerceForHint(adaptiveMemberRead(object, sanitizeCppIdentifier(expr.property)), hint) : null;
|
|
336
|
+
}
|
|
337
|
+
// Generic member access must survive both value receivers and heap-promoted
|
|
338
|
+
// handles: a module-global class cell can be refreshed to std::shared_ptr /
|
|
339
|
+
// gea_gc_ptr after classification, and a hard-coded `.member` on that handle
|
|
340
|
+
// either fails to compile or (worse) reads a stale value copy. Dispatch on
|
|
341
|
+
// operator-> structurally.
|
|
342
|
+
function adaptiveMemberRead(object, property) {
|
|
343
|
+
return `([&](auto &&__gea_mr_obj) -> decltype(auto) { if constexpr (requires { __gea_mr_obj->${property}; }) { return (__gea_mr_obj->${property}); } else { return (__gea_mr_obj.${property}); } })(${object})`;
|
|
344
|
+
}
|
|
345
|
+
function lowerIndex(context, expr, hint) {
|
|
346
|
+
const thisArray = thisFieldName(expr.object);
|
|
347
|
+
const index = lowerExpr(context, expr.index, 'number');
|
|
348
|
+
if (!index)
|
|
349
|
+
return null;
|
|
350
|
+
if (thisArray) {
|
|
351
|
+
const alias = expr.index.kind === 'identifier' ? context.arrayItemAliases.get(arrayItemAliasKey(thisArray, expr.index.name)) : undefined;
|
|
352
|
+
if (alias)
|
|
353
|
+
return alias;
|
|
354
|
+
const array = `(*this).${sanitizeCppIdentifier(thisArray)}`;
|
|
355
|
+
if (hint === 'boolean') {
|
|
356
|
+
// JS truthiness of `this.arr[k]`: an out-of-bounds index reads `undefined`
|
|
357
|
+
// (falsy), and a present element follows ToBoolean. A typed record-struct
|
|
358
|
+
// element is a JS object → always truthy; decide that at compile time via
|
|
359
|
+
// the `__gea_from_value` discriminator (record-alias structs declare it;
|
|
360
|
+
// primitives, std::string, and gea_cpp_value do not) so the hot path needs
|
|
361
|
+
// no gea_cpp_value allocation. Without this, the whole-element read below
|
|
362
|
+
// returns a default-constructed struct for an OOB index, which `operator
|
|
363
|
+
// bool` reports as truthy — so `if (!this.arr[k])` never fired for missing
|
|
364
|
+
// indices, diverging from JS (where `arr[k]` is `undefined`).
|
|
365
|
+
const indexTemp = nextStoreMethodTemp(context, 'index');
|
|
366
|
+
return `([&]() -> bool { using __GeaItem = std::decay_t<decltype((${array})[0])>; auto ${indexTemp} = static_cast<std::size_t>(${index}); if (${indexTemp} >= ${array}.size()) return false; if constexpr (requires(const gea_cpp_value &__gea_v) { __GeaItem::__gea_from_value(__gea_v); }) return true; else return gea::runtime::coerce::to_boolean(${array}[${indexTemp}]); })()`;
|
|
367
|
+
}
|
|
368
|
+
// Return BY VALUE in the array's ACTUAL element type — NOT a forced
|
|
369
|
+
// `gea_cpp_value`. For a typed store array (`CityState[]`) the element is the
|
|
370
|
+
// typed item struct, and erasing a whole-element read to `gea_cpp_value`
|
|
371
|
+
// throws away the static type (so `cityCoordsMatch(this.cities[i])` no longer
|
|
372
|
+
// sees a `CityState`). Deduce the element type via decltype so this works for
|
|
373
|
+
// both typed and `gea_cpp_value` arrays; the out-of-bounds fallback is a
|
|
374
|
+
// default-constructed element of that same type. By value (not const&) so a
|
|
375
|
+
// bound reference can't dangle on the temporary the element materializes into.
|
|
376
|
+
const indexTemp = nextStoreMethodTemp(context, 'index');
|
|
377
|
+
// Coerce the by-value element read to the requested hint: a numeric element
|
|
378
|
+
// used in a string concat (`'CC' + this.slotCc[i]`) must stringify via
|
|
379
|
+
// gea_cpp_to_string, exactly like any other numeric operand in that context.
|
|
380
|
+
// 'any'/'number' are no-ops, so typed-struct whole-element reads are
|
|
381
|
+
// unaffected; the 'boolean' hint already returned above.
|
|
382
|
+
return coerceForHint(`([&]() -> std::decay_t<decltype((${array})[0])> { using __GeaItem = std::decay_t<decltype((${array})[0])>; auto ${indexTemp} = static_cast<std::size_t>(${index}); if (${indexTemp} >= ${array}.size()) return __GeaItem{}; return ${array}[${indexTemp}]; })()`, hint);
|
|
383
|
+
}
|
|
384
|
+
const object = lowerExpr(context, expr.object, 'any');
|
|
385
|
+
return object ? `${object}[static_cast<std::size_t>(${index})]` : null;
|
|
386
|
+
}
|
|
387
|
+
function lowerCall(context, expr, hint) {
|
|
388
|
+
const path = exprPath(expr.callee);
|
|
389
|
+
const member = expr.callee.kind === 'member' ? expr.callee : null;
|
|
390
|
+
if (member?.property === 'startsWith') {
|
|
391
|
+
const object = lowerExpr(context, member.object, 'string');
|
|
392
|
+
const needle = expr.args[0] ? lowerExpr(context, expr.args[0], 'string') : 'std::string("undefined")';
|
|
393
|
+
const position = expr.args[1] ? lowerExpr(context, expr.args[1], 'number') : '0.0';
|
|
394
|
+
return object && needle && position
|
|
395
|
+
? coerceForHint(`([&](const std::string &__s, const std::string &__needle) -> bool { double __pos_d = ${position}; if (!std::isfinite(__pos_d) || __pos_d < 0.0) __pos_d = 0.0; if (__pos_d > static_cast<double>(__s.size())) __pos_d = static_cast<double>(__s.size()); auto __pos = static_cast<std::size_t>(__pos_d); if (__pos + __needle.size() > __s.size()) return false; return __s.compare(__pos, __needle.size(), __needle) == 0; })(${object}, ${needle})`, hint)
|
|
396
|
+
: null;
|
|
397
|
+
}
|
|
398
|
+
if (member?.property === 'endsWith') {
|
|
399
|
+
const object = lowerExpr(context, member.object, 'string');
|
|
400
|
+
const needle = expr.args[0] ? lowerExpr(context, expr.args[0], 'string') : 'std::string("undefined")';
|
|
401
|
+
const endPosition = expr.args[1] ? lowerExpr(context, expr.args[1], 'number') : 'static_cast<double>(__s.size())';
|
|
402
|
+
return object && needle && endPosition
|
|
403
|
+
? coerceForHint(`([&](const std::string &__s, const std::string &__needle) -> bool { double __end_d = ${endPosition}; if (!std::isfinite(__end_d) || __end_d > static_cast<double>(__s.size())) __end_d = static_cast<double>(__s.size()); if (__end_d < 0.0) __end_d = 0.0; auto __end = static_cast<std::size_t>(__end_d); if (__needle.size() > __end) return false; return __s.compare(__end - __needle.size(), __needle.size(), __needle) == 0; })(${object}, ${needle})`, hint)
|
|
404
|
+
: null;
|
|
405
|
+
}
|
|
406
|
+
if (member?.property === 'indexOf') {
|
|
407
|
+
const object = lowerExpr(context, member.object, 'string');
|
|
408
|
+
const needle = expr.args[0] ? lowerExpr(context, expr.args[0], 'string') : null;
|
|
409
|
+
const start = expr.args[1] ? lowerExpr(context, expr.args[1], 'number') : '0';
|
|
410
|
+
return object && needle && start
|
|
411
|
+
? `([&](const std::string &__s) -> double { auto __start = static_cast<std::size_t>(std::max(0.0, ${start})); auto __pos = __s.find(${needle}, __start); return __pos == std::string::npos ? -1.0 : static_cast<double>(__pos); })(${object})`
|
|
412
|
+
: null;
|
|
413
|
+
}
|
|
414
|
+
if (member?.property === 'substring') {
|
|
415
|
+
const object = lowerExpr(context, member.object, 'string');
|
|
416
|
+
const start = expr.args[0] ? lowerExpr(context, expr.args[0], 'number') : '0';
|
|
417
|
+
const end = expr.args[1] ? lowerExpr(context, expr.args[1], 'number') : null;
|
|
418
|
+
if (!object || !start)
|
|
419
|
+
return null;
|
|
420
|
+
const endInit = end ? `std::min(__size, static_cast<std::size_t>(std::max(0.0, ${end})))` : '__size';
|
|
421
|
+
return `([&](const std::string &__s) -> std::string { auto __size = __s.size(); auto __start = std::min(__size, static_cast<std::size_t>(std::max(0.0, ${start}))); auto __end = ${endInit}; if (__end < __start) std::swap(__start, __end); return __s.substr(__start, __end - __start); })(${object})`;
|
|
422
|
+
}
|
|
423
|
+
if (path === 'JSON.parse' || path === 'JSON.stringify')
|
|
424
|
+
return null;
|
|
425
|
+
if (path === 'Date.now')
|
|
426
|
+
return coerceForHint('gea_cpp_now_ms()', hint);
|
|
427
|
+
if (path === 'Math.random')
|
|
428
|
+
return coerceForHint('gea::runtime::math::random()', hint);
|
|
429
|
+
if (path === 'Math.floor') {
|
|
430
|
+
const arg = expr.args[0] ? lowerExpr(context, expr.args[0], 'number') : null;
|
|
431
|
+
return arg ? coerceForHint(`std::floor(${arg})`, hint) : null;
|
|
432
|
+
}
|
|
433
|
+
if (path === 'Math.round') {
|
|
434
|
+
const arg = expr.args[0] ? lowerExpr(context, expr.args[0], 'number') : null;
|
|
435
|
+
return arg ? coerceForHint(`std::floor((${arg}) + 0.5)`, hint) : null;
|
|
436
|
+
}
|
|
437
|
+
// The rest of the common Math intrinsics. Anything missing from this table
|
|
438
|
+
// falls through to an emission that references a bare `Math` symbol the C++
|
|
439
|
+
// has no definition for ('Math' was not declared in this scope) — hit first
|
|
440
|
+
// by Math.ceil in the e-reader's Contents pager math.
|
|
441
|
+
if (path === 'Math.ceil' || path === 'Math.trunc' || path === 'Math.sqrt' ||
|
|
442
|
+
path === 'Math.sin' || path === 'Math.cos' || path === 'Math.tan' ||
|
|
443
|
+
path === 'Math.log' || path === 'Math.exp') {
|
|
444
|
+
const fn = path.slice('Math.'.length);
|
|
445
|
+
const cppFn = fn === 'log' ? 'std::log' : `std::${fn}`;
|
|
446
|
+
const arg = expr.args[0] ? lowerExpr(context, expr.args[0], 'number') : null;
|
|
447
|
+
return arg ? coerceForHint(`${cppFn}(${arg})`, hint) : null;
|
|
448
|
+
}
|
|
449
|
+
if (path === 'Math.abs') {
|
|
450
|
+
const arg = expr.args[0] ? lowerExpr(context, expr.args[0], 'number') : null;
|
|
451
|
+
return arg ? coerceForHint(`std::fabs(${arg})`, hint) : null;
|
|
452
|
+
}
|
|
453
|
+
if (path === 'Math.pow' || path === 'Math.atan2') {
|
|
454
|
+
const a = expr.args[0] ? lowerExpr(context, expr.args[0], 'number') : null;
|
|
455
|
+
const b = expr.args[1] ? lowerExpr(context, expr.args[1], 'number') : null;
|
|
456
|
+
const cppFn = path === 'Math.pow' ? 'std::pow' : 'std::atan2';
|
|
457
|
+
return a && b ? coerceForHint(`${cppFn}(${a}, ${b})`, hint) : null;
|
|
458
|
+
}
|
|
459
|
+
if (path === 'Math.min' || path === 'Math.max') {
|
|
460
|
+
if (expr.args.length === 0)
|
|
461
|
+
return null;
|
|
462
|
+
const args = [];
|
|
463
|
+
for (const argExpr of expr.args) {
|
|
464
|
+
const lowered = lowerExpr(context, argExpr, 'number');
|
|
465
|
+
if (!lowered)
|
|
466
|
+
return null;
|
|
467
|
+
args.push(lowered);
|
|
468
|
+
}
|
|
469
|
+
if (args.length === 1)
|
|
470
|
+
return coerceForHint(`(${args[0]})`, hint);
|
|
471
|
+
const cppFn = path === 'Math.min' ? 'std::min<double>' : 'std::max<double>';
|
|
472
|
+
return coerceForHint(`${cppFn}({${args.join(', ')}})`, hint);
|
|
473
|
+
}
|
|
474
|
+
if (path === 'Math.sign') {
|
|
475
|
+
const arg = expr.args[0] ? lowerExpr(context, expr.args[0], 'number') : null;
|
|
476
|
+
return arg
|
|
477
|
+
? coerceForHint(`([&](double __gea_value) -> double { return __gea_value > 0.0 ? 1.0 : (__gea_value < 0.0 ? -1.0 : __gea_value); })(${arg})`, hint)
|
|
478
|
+
: null;
|
|
479
|
+
}
|
|
480
|
+
if (path === 'Number.isInteger') {
|
|
481
|
+
const arg = expr.args[0] ? lowerExpr(context, expr.args[0], 'number') : null;
|
|
482
|
+
return arg
|
|
483
|
+
? coerceForHint(`([&](double __gea_value) -> bool { return std::isfinite(__gea_value) && std::floor(__gea_value) == __gea_value; })(${arg})`, hint)
|
|
484
|
+
: null;
|
|
485
|
+
}
|
|
486
|
+
// The primitive coercion globals — `Number(x)` / `String(x)` / `Boolean(x)` —
|
|
487
|
+
// turn one value into a number/string/boolean (e.g. parsing a localStorage
|
|
488
|
+
// string back to a number). They work in free functions and templates, so wire
|
|
489
|
+
// them up for store methods too (they are deliberately excluded from the
|
|
490
|
+
// top-level-function fallback in shouldLowerAsTopLevelFunction).
|
|
491
|
+
if (expr.callee.kind === 'identifier' &&
|
|
492
|
+
expr.args.length === 1 &&
|
|
493
|
+
(expr.callee.name === 'Number' || expr.callee.name === 'String' || expr.callee.name === 'Boolean') &&
|
|
494
|
+
!context.locals.has(expr.callee.name) &&
|
|
495
|
+
!context.params.has(expr.callee.name) &&
|
|
496
|
+
!context.constants.has(expr.callee.name)) {
|
|
497
|
+
const inner = lowerExpr(context, expr.args[0], 'any');
|
|
498
|
+
if (!inner)
|
|
499
|
+
return null;
|
|
500
|
+
if (expr.callee.name === 'Number')
|
|
501
|
+
return coerceForHint(`gea::runtime::coerce::to_number(${inner})`, hint);
|
|
502
|
+
if (expr.callee.name === 'String')
|
|
503
|
+
return coerceForHint(`gea_cpp_to_string(${inner})`, hint);
|
|
504
|
+
return coerceForHint(`gea::runtime::coerce::to_boolean(${inner})`, hint);
|
|
505
|
+
}
|
|
506
|
+
if (member?.property === 'toFixed') {
|
|
507
|
+
const object = lowerExpr(context, member.object, 'number');
|
|
508
|
+
const digits = expr.args[0] ? lowerExpr(context, expr.args[0], 'number') : '0.0';
|
|
509
|
+
if (!object || !digits)
|
|
510
|
+
return null;
|
|
511
|
+
const call = `geaStoreNumberToFixed(${object}, ${digits})`;
|
|
512
|
+
if (hint === 'boolean')
|
|
513
|
+
return `gea::runtime::coerce::to_boolean(${call})`;
|
|
514
|
+
if (hint === 'number')
|
|
515
|
+
return `gea::runtime::coerce::to_number(${call})`;
|
|
516
|
+
return call;
|
|
517
|
+
}
|
|
518
|
+
if (path === 'audioContext.createOscillator')
|
|
519
|
+
return coerceForHint('gea::host::audioContext.createOscillator()', hint);
|
|
520
|
+
if (path === '__gea_audioContext.createOscillator')
|
|
521
|
+
return coerceForHint('gea::host::audioContext.createOscillator()', hint);
|
|
522
|
+
if (path === 'Accelerometer.start')
|
|
523
|
+
return coerceForHint('gea::host::Accelerometer.start()', hint);
|
|
524
|
+
if (path === 'touch.read' || path === 'touch.__gea_read') {
|
|
525
|
+
const sample = 'gea::host::touch.read()';
|
|
526
|
+
return hint === 'any' ? sample : coerceForHint(sample, hint);
|
|
527
|
+
}
|
|
528
|
+
if (path === 'document.getElementById') {
|
|
529
|
+
const id = expr.args[0] ? lowerExpr(context, expr.args[0], 'string') : null;
|
|
530
|
+
return id ? `gea::runtime::host::document().getElementById(${id})` : null;
|
|
531
|
+
}
|
|
532
|
+
// querySelector / querySelectorAll: emit the TYPED embedded-Document form (the
|
|
533
|
+
// same one the main emitter uses) so the result is a NodeHandle /
|
|
534
|
+
// std::vector<NodeHandle> with real methods (scrollIntoView, size, indexing).
|
|
535
|
+
// The store-method lowerer's generic member-call path would otherwise wrap this
|
|
536
|
+
// in a pointer-guard IIFE that leaves the `document` global unresolved.
|
|
537
|
+
if (path === 'document.querySelector') {
|
|
538
|
+
const sel = expr.args[0] ? lowerExpr(context, expr.args[0], 'string') : null;
|
|
539
|
+
return sel ? `gea::embedded::ui::Document::instance().querySelector((${sel}).c_str())` : null;
|
|
540
|
+
}
|
|
541
|
+
if (path === 'document.querySelectorAll') {
|
|
542
|
+
const sel = expr.args[0] ? lowerExpr(context, expr.args[0], 'string') : null;
|
|
543
|
+
return sel ? `gea::embedded::ui::Document::instance().querySelectorAll((${sel}).c_str())` : null;
|
|
544
|
+
}
|
|
545
|
+
const hostCall = lowerHostFacadeCall(context, path, expr.args);
|
|
546
|
+
if (hostCall)
|
|
547
|
+
return coerceForHint(hostCall, hint);
|
|
548
|
+
if (path === 'console.log') {
|
|
549
|
+
const parts = expr.args.map((arg) => lowerExpr(context, arg, 'string'));
|
|
550
|
+
return parts.every(Boolean) ? `gea::runtime::console::log(${parts.join(' + ')})` : null;
|
|
551
|
+
}
|
|
552
|
+
const pushField = thisArrayPushTarget(expr.callee);
|
|
553
|
+
if (pushField) {
|
|
554
|
+
context.needsArrayPush = true;
|
|
555
|
+
const args = expr.args.map((arg) => lowerTypedElementLiteral(context, pushField, arg) ?? lowerExpr(context, arg, 'any'));
|
|
556
|
+
return args.every(Boolean) ? `push_array_items(${JSON.stringify(pushField)}, (*this).${sanitizeCppIdentifier(pushField)}${args.length > 0 ? `, ${args.join(', ')}` : ''})` : null;
|
|
557
|
+
}
|
|
558
|
+
const unshiftField = thisArrayUnshiftTarget(expr.callee);
|
|
559
|
+
if (unshiftField) {
|
|
560
|
+
context.needsArrayUnshift = true;
|
|
561
|
+
const args = expr.args.map((arg) => lowerTypedElementLiteral(context, unshiftField, arg) ?? lowerExpr(context, arg, 'any'));
|
|
562
|
+
return args.every(Boolean) ? `unshift_array_items(${JSON.stringify(unshiftField)}, (*this).${sanitizeCppIdentifier(unshiftField)}${args.length > 0 ? `, ${args.join(', ')}` : ''})` : null;
|
|
563
|
+
}
|
|
564
|
+
const spliceField = thisArraySpliceTarget(expr.callee);
|
|
565
|
+
if (spliceField) {
|
|
566
|
+
context.needsArraySplice = true;
|
|
567
|
+
if (expr.args.length < 1)
|
|
568
|
+
return null;
|
|
569
|
+
const start = lowerExpr(context, expr.args[0], 'number');
|
|
570
|
+
if (!start)
|
|
571
|
+
return null;
|
|
572
|
+
const deleteCount = expr.args.length > 1 ? lowerExpr(context, expr.args[1], 'number') : '0';
|
|
573
|
+
if (!deleteCount)
|
|
574
|
+
return null;
|
|
575
|
+
const inserts = expr.args.slice(2).map((arg) => lowerTypedElementLiteral(context, spliceField, arg) ?? lowerExpr(context, arg, 'any'));
|
|
576
|
+
if (!inserts.every(Boolean))
|
|
577
|
+
return null;
|
|
578
|
+
const insertList = inserts.length > 0 ? `, ${inserts.join(', ')}` : '';
|
|
579
|
+
return `splice_array_items(${JSON.stringify(spliceField)}, (*this).${sanitizeCppIdentifier(spliceField)}, ${start}, ${deleteCount}${insertList})`;
|
|
580
|
+
}
|
|
581
|
+
const popField = thisArrayPopTarget(expr.callee);
|
|
582
|
+
if (popField) {
|
|
583
|
+
context.needsArrayPop = true;
|
|
584
|
+
if (expr.args.length !== 0)
|
|
585
|
+
return null;
|
|
586
|
+
return `pop_array_item(${JSON.stringify(popField)}, (*this).${sanitizeCppIdentifier(popField)})`;
|
|
587
|
+
}
|
|
588
|
+
const shiftField = thisArrayShiftTarget(expr.callee);
|
|
589
|
+
if (shiftField) {
|
|
590
|
+
context.needsArrayShift = true;
|
|
591
|
+
if (expr.args.length !== 0)
|
|
592
|
+
return null;
|
|
593
|
+
return `shift_array_item(${JSON.stringify(shiftField)}, (*this).${sanitizeCppIdentifier(shiftField)})`;
|
|
594
|
+
}
|
|
595
|
+
if (member?.property === 'push' && member.object.kind === 'identifier') {
|
|
596
|
+
const localName = member.object.name;
|
|
597
|
+
const elementType = localArrayTypeRef(context, localName);
|
|
598
|
+
if (elementType) {
|
|
599
|
+
const vectorName = sanitizeCppIdentifier(localName);
|
|
600
|
+
// Lower the pushed value with the element's own hint so a primitive
|
|
601
|
+
// vector (double/std::string/bool) gets a concretely-typed expression
|
|
602
|
+
// instead of a boxed `any`.
|
|
603
|
+
const elementHint = elementType === 'double' ? 'number' : elementType === 'std::string' ? 'string' : elementType === 'bool' ? 'boolean' : 'any';
|
|
604
|
+
const args = expr.args.map((arg) => lowerElementLiteralForType(context, elementType, arg) ?? lowerExpr(context, arg, elementHint));
|
|
605
|
+
if (!args.every(Boolean))
|
|
606
|
+
return null;
|
|
607
|
+
const pushes = args.map((arg) => `${vectorName}.push_back(${arg});`).join(' ');
|
|
608
|
+
return `([&]() -> double { ${pushes} return static_cast<double>(${vectorName}.size()); })()`;
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
if (expr.callee.kind === 'identifier' && URI_BUILTIN_RUNTIME[expr.callee.name] && expr.args.length === 1) {
|
|
612
|
+
const arg = lowerExpr(context, expr.args[0], 'string');
|
|
613
|
+
return arg ? coerceForHint(`${URI_BUILTIN_RUNTIME[expr.callee.name]}(${arg})`, hint) : null;
|
|
614
|
+
}
|
|
615
|
+
if (expr.callee.kind === 'identifier' && !context.locals.has(expr.callee.name) && !context.params.has(expr.callee.name)) {
|
|
616
|
+
const calleeName = sanitizeCppIdentifier(expr.callee.name);
|
|
617
|
+
const topLevelValue = context.topLevelValueNames.get(expr.callee.name) ?? context.topLevelValueNames.get(calleeName);
|
|
618
|
+
if (topLevelValue) {
|
|
619
|
+
const args = expr.args.map((arg) => lowerExpr(context, arg, 'any'));
|
|
620
|
+
return args.every(Boolean) ? coerceForHint(`${topLevelValue}(${args.join(', ')})`, hint) : null;
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
if (expr.callee.kind === 'identifier' && shouldLowerAsTopLevelFunction(context, expr.callee.name)) {
|
|
624
|
+
const args = expr.args.map((arg) => lowerExpr(context, arg, 'any'));
|
|
625
|
+
const fnName = context.topLevelFunctionNames.get(expr.callee.name) ?? `fn_${sanitizeCppIdentifier(expr.callee.name)}`;
|
|
626
|
+
return args.every(Boolean) ? coerceTopLevelFunctionCallForHint(`${fnName}(${args.join(', ')})`, hint) : null;
|
|
627
|
+
}
|
|
628
|
+
if (member?.object.kind === 'identifier') {
|
|
629
|
+
const method = globalStoreMethodPlan(context, member.object.name, member.property);
|
|
630
|
+
if (method && method.params.length === expr.args.length) {
|
|
631
|
+
const args = expr.args.map((arg, index) => lowerExpr(context, arg, method.params[index]?.valueType ?? 'any'));
|
|
632
|
+
return args.every(Boolean)
|
|
633
|
+
? coerceForHint(`${globalStoreAccess(member.object.name)}->${method.methodName}(${args.join(', ')})`, hint)
|
|
634
|
+
: null;
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
if (member?.object.kind === 'this') {
|
|
638
|
+
const args = expr.args.map((arg) => lowerExpr(context, arg, 'any'));
|
|
639
|
+
return args.every(Boolean) ? `this->${sanitizeCppIdentifier(member.property)}(${args.join(', ')})` : null;
|
|
640
|
+
}
|
|
641
|
+
if (member) {
|
|
642
|
+
// Same pointer-agnostic dispatch as adaptiveMemberRead, but for CALLS: the
|
|
643
|
+
// member-function name can't be returned from a lambda, so the whole call
|
|
644
|
+
// expression sits inside the constexpr branch.
|
|
645
|
+
const object = lowerExpr(context, member.object, 'any');
|
|
646
|
+
const args = expr.args.map((arg) => lowerExpr(context, arg, 'any'));
|
|
647
|
+
if (object && args.every(Boolean)) {
|
|
648
|
+
const prop = sanitizeCppIdentifier(member.property);
|
|
649
|
+
const argList = args.join(', ');
|
|
650
|
+
return coerceForHint(`([&](auto &&__gea_mc_obj) -> decltype(auto) { if constexpr (requires { __gea_mc_obj->${prop}(${argList}); }) { return __gea_mc_obj->${prop}(${argList}); } else { return __gea_mc_obj.${prop}(${argList}); } })(${object})`, hint);
|
|
651
|
+
}
|
|
652
|
+
return null;
|
|
653
|
+
}
|
|
654
|
+
const callee = lowerExpr(context, expr.callee, 'any');
|
|
655
|
+
const args = expr.args.map((arg) => lowerExpr(context, arg, hint));
|
|
656
|
+
return callee && args.every(Boolean) ? coerceForHint(`${callee}(${args.join(', ')})`, hint) : null;
|
|
657
|
+
}
|
|
658
|
+
function shouldLowerAsTopLevelFunction(context, name) {
|
|
659
|
+
if (context.locals.has(name) || context.params.has(name) || context.constants.has(name))
|
|
660
|
+
return false;
|
|
661
|
+
return !new Set([
|
|
662
|
+
'Number',
|
|
663
|
+
'String',
|
|
664
|
+
'Boolean',
|
|
665
|
+
'fetch',
|
|
666
|
+
'setTimeout',
|
|
667
|
+
'clearTimeout',
|
|
668
|
+
'setInterval',
|
|
669
|
+
'clearInterval',
|
|
670
|
+
'requestAnimationFrame',
|
|
671
|
+
]).has(name);
|
|
672
|
+
}
|
|
673
|
+
function coerceTopLevelFunctionCallForHint(call, hint) {
|
|
674
|
+
if (hint === 'number')
|
|
675
|
+
return `gea::runtime::coerce::to_number(${call})`;
|
|
676
|
+
return coerceForHint(call, hint);
|
|
677
|
+
}
|
|
678
|
+
function coerceTopLevelValueForHint(value, hint) {
|
|
679
|
+
if (hint === 'number')
|
|
680
|
+
return `gea::runtime::coerce::to_number(${value})`;
|
|
681
|
+
return coerceForHint(value, hint);
|
|
682
|
+
}
|
|
683
|
+
function lowerHostFacadeCall(context, path, args) {
|
|
684
|
+
if (!path)
|
|
685
|
+
return null;
|
|
686
|
+
const calls = {
|
|
687
|
+
'Apps.launch': { target: 'gea::host::apps.launch', hints: ['string'] },
|
|
688
|
+
'apps.launch': { target: 'gea::host::apps.launch', hints: ['string'] },
|
|
689
|
+
'Geolocation.hasFix': { target: 'gea::host::navigator.geolocation.hasFix' },
|
|
690
|
+
'geolocation.hasFix': { target: 'gea::host::navigator.geolocation.hasFix' },
|
|
691
|
+
'Geolocation.currentPosition': { target: 'gea::host::navigator.geolocation.currentPosition' },
|
|
692
|
+
'geolocation.currentPosition': { target: 'gea::host::navigator.geolocation.currentPosition' },
|
|
693
|
+
'Geolocation.coords': { target: 'gea::host::navigator.geolocation.coords' },
|
|
694
|
+
'geolocation.coords': { target: 'gea::host::navigator.geolocation.coords' },
|
|
695
|
+
'Geolocation.latitude': { target: 'gea::host::navigator.geolocation.latitude' },
|
|
696
|
+
'geolocation.latitude': { target: 'gea::host::navigator.geolocation.latitude' },
|
|
697
|
+
'Geolocation.longitude': { target: 'gea::host::navigator.geolocation.longitude' },
|
|
698
|
+
'geolocation.longitude': { target: 'gea::host::navigator.geolocation.longitude' },
|
|
699
|
+
'Geolocation.accuracy': { target: 'gea::host::navigator.geolocation.accuracy' },
|
|
700
|
+
'geolocation.accuracy': { target: 'gea::host::navigator.geolocation.accuracy' },
|
|
701
|
+
'Audio.getVolume': { target: 'gea::host::Audio.getVolume' },
|
|
702
|
+
'__gea_Audio.getVolume': { target: 'gea::host::Audio.getVolume' },
|
|
703
|
+
'Audio.setVolume': { target: 'gea::host::Audio.setVolume', hints: ['number'] },
|
|
704
|
+
'__gea_Audio.setVolume': { target: 'gea::host::Audio.setVolume', hints: ['number'] },
|
|
705
|
+
'WiFi.enabled': { target: 'gea::host::navigator.wifi.enabled' },
|
|
706
|
+
'wifi.enabled': { target: 'gea::host::navigator.wifi.enabled' },
|
|
707
|
+
'WiFi.setEnabled': { target: 'gea::host::navigator.wifi.setEnabled', hints: ['boolean'] },
|
|
708
|
+
'wifi.setEnabled': { target: 'gea::host::navigator.wifi.setEnabled', hints: ['boolean'] },
|
|
709
|
+
'WiFi.connected': { target: 'gea::host::navigator.wifi.connected' },
|
|
710
|
+
'wifi.connected': { target: 'gea::host::navigator.wifi.connected' },
|
|
711
|
+
'WiFi.isConnected': { target: 'gea::host::navigator.wifi.connected' },
|
|
712
|
+
'wifi.isConnected': { target: 'gea::host::navigator.wifi.connected' },
|
|
713
|
+
'WiFi.ssid': { target: 'gea::host::navigator.wifi.ssid' },
|
|
714
|
+
'wifi.ssid': { target: 'gea::host::navigator.wifi.ssid' },
|
|
715
|
+
'WiFi.getSSID': { target: 'gea::host::navigator.wifi.ssid' },
|
|
716
|
+
'wifi.getSSID': { target: 'gea::host::navigator.wifi.ssid' },
|
|
717
|
+
'WiFi.ip': { target: 'gea::host::navigator.wifi.ip' },
|
|
718
|
+
'wifi.ip': { target: 'gea::host::navigator.wifi.ip' },
|
|
719
|
+
'WiFi.getIP': { target: 'gea::host::navigator.wifi.ip' },
|
|
720
|
+
'wifi.getIP': { target: 'gea::host::navigator.wifi.ip' },
|
|
721
|
+
'WiFi.rssi': { target: 'gea::host::navigator.wifi.rssi' },
|
|
722
|
+
'wifi.rssi': { target: 'gea::host::navigator.wifi.rssi' },
|
|
723
|
+
'WiFi.getRSSI': { target: 'gea::host::navigator.wifi.rssi' },
|
|
724
|
+
'wifi.getRSSI': { target: 'gea::host::navigator.wifi.rssi' },
|
|
725
|
+
'WiFi.mac': { target: 'gea::host::navigator.wifi.mac' },
|
|
726
|
+
'wifi.mac': { target: 'gea::host::navigator.wifi.mac' },
|
|
727
|
+
'WiFi.configure': { target: 'gea::host::navigator.wifi.configure', hints: ['string', 'string'] },
|
|
728
|
+
'wifi.configure': { target: 'gea::host::navigator.wifi.configure', hints: ['string', 'string'] },
|
|
729
|
+
'WiFi.waitForConnection': { target: 'gea::host::navigator.wifi.waitForConnection', hints: ['number'] },
|
|
730
|
+
'wifi.waitForConnection': { target: 'gea::host::navigator.wifi.waitForConnection', hints: ['number'] },
|
|
731
|
+
'WiFi.startScan': { target: 'gea::host::navigator.wifi.startScan' },
|
|
732
|
+
'wifi.startScan': { target: 'gea::host::navigator.wifi.startScan' },
|
|
733
|
+
'WiFi.scanning': { target: 'gea::host::navigator.wifi.scanning' },
|
|
734
|
+
'wifi.scanning': { target: 'gea::host::navigator.wifi.scanning' },
|
|
735
|
+
'WiFi.scanCount': { target: 'gea::host::navigator.wifi.scanCount' },
|
|
736
|
+
'wifi.scanCount': { target: 'gea::host::navigator.wifi.scanCount' },
|
|
737
|
+
'WiFi.scanSsidAt': { target: 'gea::host::navigator.wifi.scanSsidAt', hints: ['number'] },
|
|
738
|
+
'wifi.scanSsidAt': { target: 'gea::host::navigator.wifi.scanSsidAt', hints: ['number'] },
|
|
739
|
+
'WiFi.scanRssiAt': { target: 'gea::host::navigator.wifi.scanRssiAt', hints: ['number'] },
|
|
740
|
+
'wifi.scanRssiAt': { target: 'gea::host::navigator.wifi.scanRssiAt', hints: ['number'] },
|
|
741
|
+
'WiFi.scanSecuredAt': { target: 'gea::host::navigator.wifi.scanSecuredAt', hints: ['number'] },
|
|
742
|
+
'wifi.scanSecuredAt': { target: 'gea::host::navigator.wifi.scanSecuredAt', hints: ['number'] },
|
|
743
|
+
'BLE.init': { target: 'gea::host::navigator.bluetooth.init', hints: ['string', 'number', 'string'] },
|
|
744
|
+
'bluetooth.init': { target: 'gea::host::navigator.bluetooth.init', hints: ['string', 'number', 'string'] },
|
|
745
|
+
'BLE.enabled': { target: 'gea::host::navigator.bluetooth.enabled' },
|
|
746
|
+
'bluetooth.enabled': { target: 'gea::host::navigator.bluetooth.enabled' },
|
|
747
|
+
'BLE.setEnabled': { target: 'gea::host::navigator.bluetooth.setEnabled', hints: ['boolean'] },
|
|
748
|
+
'bluetooth.setEnabled': { target: 'gea::host::navigator.bluetooth.setEnabled', hints: ['boolean'] },
|
|
749
|
+
'BLE.connected': { target: 'gea::host::navigator.bluetooth.connected' },
|
|
750
|
+
'bluetooth.connected': { target: 'gea::host::navigator.bluetooth.connected' },
|
|
751
|
+
'BLE.bound': { target: 'gea::host::navigator.bluetooth.bound' },
|
|
752
|
+
'bluetooth.bound': { target: 'gea::host::navigator.bluetooth.bound' },
|
|
753
|
+
'BLE.batteryLevel': { target: 'gea::host::navigator.bluetooth.batteryLevel' },
|
|
754
|
+
'bluetooth.batteryLevel': { target: 'gea::host::navigator.bluetooth.batteryLevel' },
|
|
755
|
+
'BLE.mac': { target: 'gea::host::navigator.bluetooth.mac' },
|
|
756
|
+
'bluetooth.mac': { target: 'gea::host::navigator.bluetooth.mac' },
|
|
757
|
+
'BLE.deviceName': { target: 'gea::host::navigator.bluetooth.deviceName' },
|
|
758
|
+
'bluetooth.deviceName': { target: 'gea::host::navigator.bluetooth.deviceName' },
|
|
759
|
+
'BLE.startAdvertising': { target: 'gea::host::navigator.bluetooth.startAdvertising' },
|
|
760
|
+
'bluetooth.startAdvertising': { target: 'gea::host::navigator.bluetooth.startAdvertising' },
|
|
761
|
+
'BLE.stopAdvertising': { target: 'gea::host::navigator.bluetooth.stopAdvertising' },
|
|
762
|
+
'bluetooth.stopAdvertising': { target: 'gea::host::navigator.bluetooth.stopAdvertising' },
|
|
763
|
+
'BLE.keyboard.tap': { target: 'gea::host::navigator.bluetooth.keyboard.tap', hints: ['number'] },
|
|
764
|
+
'bluetooth.keyboard.tap': { target: 'gea::host::navigator.bluetooth.keyboard.tap', hints: ['number'] },
|
|
765
|
+
'BLE.keyboard.down': { target: 'gea::host::navigator.bluetooth.keyboard.down', hints: ['number', 'number'] },
|
|
766
|
+
'bluetooth.keyboard.down': { target: 'gea::host::navigator.bluetooth.keyboard.down', hints: ['number', 'number'] },
|
|
767
|
+
'BLE.keyboard.up': { target: 'gea::host::navigator.bluetooth.keyboard.up' },
|
|
768
|
+
'bluetooth.keyboard.up': { target: 'gea::host::navigator.bluetooth.keyboard.up' },
|
|
769
|
+
'BLE.mouse.move': { target: 'gea::host::navigator.bluetooth.mouse.move', hints: ['number', 'number', 'number', 'number'] },
|
|
770
|
+
'bluetooth.mouse.move': { target: 'gea::host::navigator.bluetooth.mouse.move', hints: ['number', 'number', 'number', 'number'] },
|
|
771
|
+
'BLE.mouse.click': { target: 'gea::host::navigator.bluetooth.mouse.click', hints: ['number'] },
|
|
772
|
+
'bluetooth.mouse.click': { target: 'gea::host::navigator.bluetooth.mouse.click', hints: ['number'] },
|
|
773
|
+
// BLE-MIDI (MIDI over GATT) — mirrors the `BLE.midi` entries in
|
|
774
|
+
// host-shims.ts so class-method bodies resolve them the same way
|
|
775
|
+
// top-level code does.
|
|
776
|
+
'BLE.midi.enable': { target: 'gea::host::navigator.bluetooth.midi.enable' },
|
|
777
|
+
'bluetooth.midi.enable': { target: 'gea::host::navigator.bluetooth.midi.enable' },
|
|
778
|
+
'BLE.midi.bound': { target: 'gea::host::navigator.bluetooth.midi.bound' },
|
|
779
|
+
'bluetooth.midi.bound': { target: 'gea::host::navigator.bluetooth.midi.bound' },
|
|
780
|
+
'BLE.midi.send': { target: 'gea::host::navigator.bluetooth.midi.send' },
|
|
781
|
+
'bluetooth.midi.send': { target: 'gea::host::navigator.bluetooth.midi.send' },
|
|
782
|
+
'BLE.midi.startScan': { target: 'gea::host::navigator.bluetooth.midi.startScan' },
|
|
783
|
+
'bluetooth.midi.startScan': { target: 'gea::host::navigator.bluetooth.midi.startScan' },
|
|
784
|
+
'BLE.midi.stopScan': { target: 'gea::host::navigator.bluetooth.midi.stopScan' },
|
|
785
|
+
'bluetooth.midi.stopScan': { target: 'gea::host::navigator.bluetooth.midi.stopScan' },
|
|
786
|
+
'BLE.midi.scanning': { target: 'gea::host::navigator.bluetooth.midi.scanning' },
|
|
787
|
+
'bluetooth.midi.scanning': { target: 'gea::host::navigator.bluetooth.midi.scanning' },
|
|
788
|
+
'BLE.midi.scanCount': { target: 'gea::host::navigator.bluetooth.midi.scanCount' },
|
|
789
|
+
'bluetooth.midi.scanCount': { target: 'gea::host::navigator.bluetooth.midi.scanCount' },
|
|
790
|
+
'BLE.midi.scanNameAt': { target: 'gea::host::navigator.bluetooth.midi.scanNameAt', hints: ['number'] },
|
|
791
|
+
'bluetooth.midi.scanNameAt': { target: 'gea::host::navigator.bluetooth.midi.scanNameAt', hints: ['number'] },
|
|
792
|
+
'BLE.midi.connect': { target: 'gea::host::navigator.bluetooth.midi.connect', hints: ['number'] },
|
|
793
|
+
'bluetooth.midi.connect': { target: 'gea::host::navigator.bluetooth.midi.connect', hints: ['number'] },
|
|
794
|
+
'BLE.midi.disconnect': { target: 'gea::host::navigator.bluetooth.midi.disconnect' },
|
|
795
|
+
'bluetooth.midi.disconnect': { target: 'gea::host::navigator.bluetooth.midi.disconnect' },
|
|
796
|
+
// BLE HID-host role (this device is the central; a remote keyboard/macro
|
|
797
|
+
// pad like the XPPen ACK05 is the input source) — mirrors `BLE.hidHost`
|
|
798
|
+
// in host-shims.ts.
|
|
799
|
+
'BLE.hidHost.startScan': { target: 'gea::host::navigator.bluetooth.hidHost.startScan' },
|
|
800
|
+
'bluetooth.hidHost.startScan': { target: 'gea::host::navigator.bluetooth.hidHost.startScan' },
|
|
801
|
+
'BLE.hidHost.stopScan': { target: 'gea::host::navigator.bluetooth.hidHost.stopScan' },
|
|
802
|
+
'bluetooth.hidHost.stopScan': { target: 'gea::host::navigator.bluetooth.hidHost.stopScan' },
|
|
803
|
+
'BLE.hidHost.scanning': { target: 'gea::host::navigator.bluetooth.hidHost.scanning' },
|
|
804
|
+
'bluetooth.hidHost.scanning': { target: 'gea::host::navigator.bluetooth.hidHost.scanning' },
|
|
805
|
+
'BLE.hidHost.scanCount': { target: 'gea::host::navigator.bluetooth.hidHost.scanCount' },
|
|
806
|
+
'bluetooth.hidHost.scanCount': { target: 'gea::host::navigator.bluetooth.hidHost.scanCount' },
|
|
807
|
+
'BLE.hidHost.scanNameAt': { target: 'gea::host::navigator.bluetooth.hidHost.scanNameAt', hints: ['number'] },
|
|
808
|
+
'bluetooth.hidHost.scanNameAt': { target: 'gea::host::navigator.bluetooth.hidHost.scanNameAt', hints: ['number'] },
|
|
809
|
+
'BLE.hidHost.connect': { target: 'gea::host::navigator.bluetooth.hidHost.connect', hints: ['number'] },
|
|
810
|
+
'bluetooth.hidHost.connect': { target: 'gea::host::navigator.bluetooth.hidHost.connect', hints: ['number'] },
|
|
811
|
+
'BLE.hidHost.disconnect': { target: 'gea::host::navigator.bluetooth.hidHost.disconnect' },
|
|
812
|
+
'bluetooth.hidHost.disconnect': { target: 'gea::host::navigator.bluetooth.hidHost.disconnect' },
|
|
813
|
+
'BLE.hidHost.bound': { target: 'gea::host::navigator.bluetooth.hidHost.bound' },
|
|
814
|
+
'bluetooth.hidHost.bound': { target: 'gea::host::navigator.bluetooth.hidHost.bound' },
|
|
815
|
+
'BLE.hidHost.reportCount': { target: 'gea::host::navigator.bluetooth.hidHost.reportCount' },
|
|
816
|
+
'bluetooth.hidHost.reportCount': { target: 'gea::host::navigator.bluetooth.hidHost.reportCount' },
|
|
817
|
+
'BLE.hidHost.reportIdAt': { target: 'gea::host::navigator.bluetooth.hidHost.reportIdAt', hints: ['number'] },
|
|
818
|
+
'bluetooth.hidHost.reportIdAt': { target: 'gea::host::navigator.bluetooth.hidHost.reportIdAt', hints: ['number'] },
|
|
819
|
+
'BLE.hidHost.reportLenAt': { target: 'gea::host::navigator.bluetooth.hidHost.reportLenAt', hints: ['number'] },
|
|
820
|
+
'bluetooth.hidHost.reportLenAt': { target: 'gea::host::navigator.bluetooth.hidHost.reportLenAt', hints: ['number'] },
|
|
821
|
+
'BLE.hidHost.reportByteAt': { target: 'gea::host::navigator.bluetooth.hidHost.reportByteAt', hints: ['number', 'number'] },
|
|
822
|
+
'bluetooth.hidHost.reportByteAt': { target: 'gea::host::navigator.bluetooth.hidHost.reportByteAt', hints: ['number', 'number'] },
|
|
823
|
+
'BLE.hidHost.clearReports': { target: 'gea::host::navigator.bluetooth.hidHost.clearReports' },
|
|
824
|
+
'bluetooth.hidHost.clearReports': { target: 'gea::host::navigator.bluetooth.hidHost.clearReports' },
|
|
825
|
+
// BLE connection registry — mirrors `BLE.connections` in host-shims.ts.
|
|
826
|
+
'BLE.connections.count': { target: 'gea::host::navigator.bluetooth.connections.count' },
|
|
827
|
+
'bluetooth.connections.count': { target: 'gea::host::navigator.bluetooth.connections.count' },
|
|
828
|
+
'BLE.connections.kindAt': { target: 'gea::host::navigator.bluetooth.connections.kindAt', hints: ['number'] },
|
|
829
|
+
'bluetooth.connections.kindAt': { target: 'gea::host::navigator.bluetooth.connections.kindAt', hints: ['number'] },
|
|
830
|
+
'BLE.connections.nameAt': { target: 'gea::host::navigator.bluetooth.connections.nameAt', hints: ['number'] },
|
|
831
|
+
'bluetooth.connections.nameAt': { target: 'gea::host::navigator.bluetooth.connections.nameAt', hints: ['number'] },
|
|
832
|
+
// Config service (custom GATT) — a Web Bluetooth portal live-programs the
|
|
833
|
+
// device. Mirrors `BLE.config` in host-shims.ts. setDocument takes a
|
|
834
|
+
// number[] lowered to native std::vector<double> like BLE.midi.send (no
|
|
835
|
+
// hints); the *At getters take a numeric index.
|
|
836
|
+
'BLE.config.setDocument': { target: 'gea::host::navigator.bluetooth.config.setDocument' },
|
|
837
|
+
'bluetooth.config.setDocument': { target: 'gea::host::navigator.bluetooth.config.setDocument' },
|
|
838
|
+
'BLE.config.pendingLength': { target: 'gea::host::navigator.bluetooth.config.pendingLength' },
|
|
839
|
+
'bluetooth.config.pendingLength': { target: 'gea::host::navigator.bluetooth.config.pendingLength' },
|
|
840
|
+
'BLE.config.pendingByteAt': { target: 'gea::host::navigator.bluetooth.config.pendingByteAt', hints: ['number'] },
|
|
841
|
+
'bluetooth.config.pendingByteAt': { target: 'gea::host::navigator.bluetooth.config.pendingByteAt', hints: ['number'] },
|
|
842
|
+
'BLE.config.consumePending': { target: 'gea::host::navigator.bluetooth.config.consumePending' },
|
|
843
|
+
'bluetooth.config.consumePending': { target: 'gea::host::navigator.bluetooth.config.consumePending' },
|
|
844
|
+
'BLE.config.pairing': { target: 'gea::host::navigator.bluetooth.config.pairing' },
|
|
845
|
+
'bluetooth.config.pairing': { target: 'gea::host::navigator.bluetooth.config.pairing' },
|
|
846
|
+
'BLE.config.pairCode': { target: 'gea::host::navigator.bluetooth.config.pairCode' },
|
|
847
|
+
'bluetooth.config.pairCode': { target: 'gea::host::navigator.bluetooth.config.pairCode' },
|
|
848
|
+
'BLE.config.dismissPairing': { target: 'gea::host::navigator.bluetooth.config.dismissPairing' },
|
|
849
|
+
'bluetooth.config.dismissPairing': { target: 'gea::host::navigator.bluetooth.config.dismissPairing' },
|
|
850
|
+
// Device -> portal activity push. Takes a numeric control index.
|
|
851
|
+
'BLE.config.pushActivity': { target: 'gea::host::navigator.bluetooth.config.pushActivity', hints: ['number'] },
|
|
852
|
+
'bluetooth.config.pushActivity': { target: 'gea::host::navigator.bluetooth.config.pushActivity', hints: ['number'] },
|
|
853
|
+
// DisplayController — the framework's `Display` import from `gea-embedded`.
|
|
854
|
+
// Mirrors the `__gea_Display` entries in host-shims.ts so class-method
|
|
855
|
+
// bodies resolve `Display.X` the same way top-level code does.
|
|
856
|
+
'Display.getBrightness': { target: 'gea::host::Display.getBrightness' },
|
|
857
|
+
'Display.setBrightness': { target: 'gea::host::Display.setBrightness', hints: ['number'] },
|
|
858
|
+
'Display.getDevicePixelRatio': { target: 'gea::host::Display.getDevicePixelRatio' },
|
|
859
|
+
'Display.setDevicePixelRatio': { target: 'gea::host::Display.setDevicePixelRatio', hints: ['number'] },
|
|
860
|
+
'Display.getFrameIntervalMs': { target: 'gea::host::Display.getFrameIntervalMs' },
|
|
861
|
+
'Display.setFrameIntervalMs': { target: 'gea::host::Display.setFrameIntervalMs', hints: ['number'] },
|
|
862
|
+
'Display.getFrameRate': { target: 'gea::host::Display.getFrameRate' },
|
|
863
|
+
'Display.setFrameRate': { target: 'gea::host::Display.setFrameRate', hints: ['number'] },
|
|
864
|
+
'Display.getOrientation': { target: 'gea::host::Display.getOrientation' },
|
|
865
|
+
'Display.setOrientation': { target: 'gea::host::Display.setOrientation', hints: ['string'] },
|
|
866
|
+
'Display.getSupportedOrientations': { target: 'gea::host::Display.getSupportedOrientations' },
|
|
867
|
+
'Display.setSupportedOrientations': { target: 'gea::host::Display.setSupportedOrientations' },
|
|
868
|
+
'Display.getAutoRotate': { target: 'gea::host::Display.getAutoRotate' },
|
|
869
|
+
'Display.setAutoRotate': { target: 'gea::host::Display.setAutoRotate', hints: ['boolean'] },
|
|
870
|
+
'Display.setVSync': { target: 'gea::host::Display.setVSync', hints: ['boolean'] },
|
|
871
|
+
'Display.setTextRasterCache': { target: 'gea::host::Display.setTextRasterCache', hints: ['boolean'] },
|
|
872
|
+
'Display.setTextSolidBackdrop': { target: 'gea::host::Display.setTextSolidBackdrop', hints: ['number'] },
|
|
873
|
+
'Display.invalidate': { target: 'gea::host::Display.invalidate' },
|
|
874
|
+
'Display.getPixelFormat': { target: 'gea::host::Display.getPixelFormat' },
|
|
875
|
+
'Display.setPixelFormat': { target: 'gea::host::Display.setPixelFormat', hints: ['string'] },
|
|
876
|
+
'Display.getPanelPixelFormat': { target: 'gea::host::Display.getPanelPixelFormat' },
|
|
877
|
+
'Display.getSupportedPixelFormats': { target: 'gea::host::Display.getSupportedPixelFormats' },
|
|
878
|
+
'Display.setAA': { target: 'gea::host::Display.setAA', hints: ['number'] },
|
|
879
|
+
'Display.setFlushConfig': { target: 'gea::host::Display.setFlushConfig' },
|
|
880
|
+
'Display.epaperFullRefresh': { target: 'gea::host::Display.epaperFullRefresh' },
|
|
881
|
+
'display.getBrightness': { target: 'gea::host::Display.getBrightness' },
|
|
882
|
+
'display.setBrightness': { target: 'gea::host::Display.setBrightness', hints: ['number'] },
|
|
883
|
+
'display.getDevicePixelRatio': { target: 'gea::host::Display.getDevicePixelRatio' },
|
|
884
|
+
'display.setDevicePixelRatio': { target: 'gea::host::Display.setDevicePixelRatio', hints: ['number'] },
|
|
885
|
+
'display.getFrameIntervalMs': { target: 'gea::host::Display.getFrameIntervalMs' },
|
|
886
|
+
'display.setFrameIntervalMs': { target: 'gea::host::Display.setFrameIntervalMs', hints: ['number'] },
|
|
887
|
+
'display.getFrameRate': { target: 'gea::host::Display.getFrameRate' },
|
|
888
|
+
'display.setFrameRate': { target: 'gea::host::Display.setFrameRate', hints: ['number'] },
|
|
889
|
+
'display.getOrientation': { target: 'gea::host::Display.getOrientation' },
|
|
890
|
+
'display.setOrientation': { target: 'gea::host::Display.setOrientation', hints: ['string'] },
|
|
891
|
+
'display.getSupportedOrientations': { target: 'gea::host::Display.getSupportedOrientations' },
|
|
892
|
+
'display.setSupportedOrientations': { target: 'gea::host::Display.setSupportedOrientations' },
|
|
893
|
+
'display.getAutoRotate': { target: 'gea::host::Display.getAutoRotate' },
|
|
894
|
+
'display.setAutoRotate': { target: 'gea::host::Display.setAutoRotate', hints: ['boolean'] },
|
|
895
|
+
'display.setVSync': { target: 'gea::host::Display.setVSync', hints: ['boolean'] },
|
|
896
|
+
'display.setTextRasterCache': { target: 'gea::host::Display.setTextRasterCache', hints: ['boolean'] },
|
|
897
|
+
'display.setTextSolidBackdrop': { target: 'gea::host::Display.setTextSolidBackdrop', hints: ['number'] },
|
|
898
|
+
'display.invalidate': { target: 'gea::host::Display.invalidate' },
|
|
899
|
+
'display.getPixelFormat': { target: 'gea::host::Display.getPixelFormat' },
|
|
900
|
+
'display.setPixelFormat': { target: 'gea::host::Display.setPixelFormat', hints: ['string'] },
|
|
901
|
+
'display.getPanelPixelFormat': { target: 'gea::host::Display.getPanelPixelFormat' },
|
|
902
|
+
'display.getSupportedPixelFormats': { target: 'gea::host::Display.getSupportedPixelFormats' },
|
|
903
|
+
'display.setAA': { target: 'gea::host::Display.setAA', hints: ['number'] },
|
|
904
|
+
'display.setFlushConfig': { target: 'gea::host::Display.setFlushConfig' },
|
|
905
|
+
'display.epaperFullRefresh': { target: 'gea::host::Display.epaperFullRefresh' },
|
|
906
|
+
'__gea_Display.getBrightness': { target: 'gea::host::Display.getBrightness' },
|
|
907
|
+
'__gea_Display.setBrightness': { target: 'gea::host::Display.setBrightness', hints: ['number'] },
|
|
908
|
+
'__gea_Display.getDevicePixelRatio': { target: 'gea::host::Display.getDevicePixelRatio' },
|
|
909
|
+
'__gea_Display.setDevicePixelRatio': { target: 'gea::host::Display.setDevicePixelRatio', hints: ['number'] },
|
|
910
|
+
'__gea_Display.getFrameIntervalMs': { target: 'gea::host::Display.getFrameIntervalMs' },
|
|
911
|
+
'__gea_Display.setFrameIntervalMs': { target: 'gea::host::Display.setFrameIntervalMs', hints: ['number'] },
|
|
912
|
+
'__gea_Display.getFrameRate': { target: 'gea::host::Display.getFrameRate' },
|
|
913
|
+
'__gea_Display.setFrameRate': { target: 'gea::host::Display.setFrameRate', hints: ['number'] },
|
|
914
|
+
'__gea_Display.getOrientation': { target: 'gea::host::Display.getOrientation' },
|
|
915
|
+
'__gea_Display.setOrientation': { target: 'gea::host::Display.setOrientation', hints: ['string'] },
|
|
916
|
+
'__gea_Display.getSupportedOrientations': { target: 'gea::host::Display.getSupportedOrientations' },
|
|
917
|
+
'__gea_Display.setSupportedOrientations': { target: 'gea::host::Display.setSupportedOrientations' },
|
|
918
|
+
'__gea_Display.getAutoRotate': { target: 'gea::host::Display.getAutoRotate' },
|
|
919
|
+
'__gea_Display.setAutoRotate': { target: 'gea::host::Display.setAutoRotate', hints: ['boolean'] },
|
|
920
|
+
'__gea_Display.setVSync': { target: 'gea::host::Display.setVSync', hints: ['boolean'] },
|
|
921
|
+
'__gea_Display.setTextRasterCache': { target: 'gea::host::Display.setTextRasterCache', hints: ['boolean'] },
|
|
922
|
+
'__gea_Display.setTextSolidBackdrop': { target: 'gea::host::Display.setTextSolidBackdrop', hints: ['number'] },
|
|
923
|
+
'__gea_Display.invalidate': { target: 'gea::host::Display.invalidate' },
|
|
924
|
+
'__gea_Display.getPixelFormat': { target: 'gea::host::Display.getPixelFormat' },
|
|
925
|
+
'__gea_Display.setPixelFormat': { target: 'gea::host::Display.setPixelFormat', hints: ['string'] },
|
|
926
|
+
'__gea_Display.getPanelPixelFormat': { target: 'gea::host::Display.getPanelPixelFormat' },
|
|
927
|
+
'__gea_Display.getSupportedPixelFormats': { target: 'gea::host::Display.getSupportedPixelFormats' },
|
|
928
|
+
'__gea_Display.setAA': { target: 'gea::host::Display.setAA', hints: ['number'] },
|
|
929
|
+
'__gea_Display.setFlushConfig': { target: 'gea::host::Display.setFlushConfig' },
|
|
930
|
+
'__gea_Display.epaperFullRefresh': { target: 'gea::host::Display.epaperFullRefresh' },
|
|
931
|
+
// ClockController — the framework's `Clock` import from `gea-embedded`.
|
|
932
|
+
// `epochMs()` reads gettimeofday (wall clock; reflects GEADEV SETTIME),
|
|
933
|
+
// distinct from Date.now() which is monotonic on ESP32.
|
|
934
|
+
'Clock.epochMs': { target: 'gea::host::Clock.epochMs' },
|
|
935
|
+
'__gea_Clock.epochMs': { target: 'gea::host::Clock.epochMs' },
|
|
936
|
+
// ProfilerController — monotonic microsecond timing for app instrumentation.
|
|
937
|
+
'Profiler.nowUs': { target: 'gea::host::Profiler.nowUs' },
|
|
938
|
+
'__gea_Profiler.nowUs': { target: 'gea::host::Profiler.nowUs' },
|
|
939
|
+
'Profiler.nowCycles': { target: 'gea::host::Profiler.nowCycles' },
|
|
940
|
+
'__gea_Profiler.nowCycles': { target: 'gea::host::Profiler.nowCycles' },
|
|
941
|
+
// Browser-compatible `localStorage` — persistent NVS-backed key/value for
|
|
942
|
+
// TSX apps. A global (no import); mirrors the DOM Storage API.
|
|
943
|
+
'localStorage.getItem': { target: 'gea::host::Storage.getItem', hints: ['string'] },
|
|
944
|
+
'localStorage.setItem': { target: 'gea::host::Storage.setItem', hints: ['string', 'string'] },
|
|
945
|
+
'localStorage.removeItem': { target: 'gea::host::Storage.removeItem', hints: ['string'] },
|
|
946
|
+
'localStorage.clear': { target: 'gea::host::Storage.clear' },
|
|
947
|
+
'localStorage.key': { target: 'gea::host::Storage.key', hints: ['number'] },
|
|
948
|
+
// BatteryController — charge percentage from the platform PMU.
|
|
949
|
+
'Battery.level': { target: 'gea::host::Battery.level' },
|
|
950
|
+
'__gea_Battery.level': { target: 'gea::host::Battery.level' },
|
|
951
|
+
// NotifyController — transient host->app notification channel.
|
|
952
|
+
'Notify.text': { target: 'gea::host::Notify.text' },
|
|
953
|
+
'__gea_Notify.text': { target: 'gea::host::Notify.text' },
|
|
954
|
+
'Notify.seq': { target: 'gea::host::Notify.seq' },
|
|
955
|
+
'__gea_Notify.seq': { target: 'gea::host::Notify.seq' },
|
|
956
|
+
// DeviceControlController — host shell exec (macOS companion).
|
|
957
|
+
'DeviceControl.exec': { target: 'gea::host::DeviceControl.exec', hints: ['string'] },
|
|
958
|
+
'__gea_DeviceControl.exec': { target: 'gea::host::DeviceControl.exec', hints: ['string'] },
|
|
959
|
+
'Camera.isAvailable': { target: 'gea::host::Camera.isAvailable' },
|
|
960
|
+
'Camera.hasPermission': { target: 'gea::host::Camera.hasPermission' },
|
|
961
|
+
'Camera.requestPermission': { target: 'gea::host::Camera.requestPermission' },
|
|
962
|
+
'Camera.open': { target: 'gea::host::Camera.open', optionsRecord: true },
|
|
963
|
+
'Camera.close': { target: 'gea::host::Camera.close' },
|
|
964
|
+
'Camera.isOpen': { target: 'gea::host::Camera.isOpen' },
|
|
965
|
+
'Camera.draw': { target: 'gea::host::Camera.draw', hints: ['number', 'number', 'number', 'number'] },
|
|
966
|
+
'Camera.capture': { target: 'gea::host::Camera.capture', optionsRecord: true },
|
|
967
|
+
'Camera.captureMirrored': { target: 'gea::host::Camera.captureMirrored' },
|
|
968
|
+
'Camera.setFlash': { target: 'gea::host::Camera.setFlash', hints: ['string'] },
|
|
969
|
+
'Camera.setZoom': { target: 'gea::host::Camera.setZoom', hints: ['number'] },
|
|
970
|
+
'Camera.setMirror': { target: 'gea::host::Camera.setMirror', hints: ['boolean'] },
|
|
971
|
+
// Options-record controls (mirror Camera.open): an object-literal argument
|
|
972
|
+
// lowers to a TYPED anonymous struct (lowerAnonymousOptionsStruct) whose
|
|
973
|
+
// fields the CameraFacade template overloads read directly via `requires`
|
|
974
|
+
// member probes — no gea_cpp_value boxing. Non-literal args keep the boxed
|
|
975
|
+
// record path, which the host readers still accept via record_get_literal.
|
|
976
|
+
'Camera.setExposure': { target: 'gea::host::Camera.setExposure', optionsRecord: true },
|
|
977
|
+
'Camera.setWhiteBalance': { target: 'gea::host::Camera.setWhiteBalance', optionsRecord: true },
|
|
978
|
+
'Camera.setFocus': { target: 'gea::host::Camera.setFocus', optionsRecord: true },
|
|
979
|
+
'Camera.deviceIdAt': { target: 'gea::host::Camera.deviceIdAt', hints: ['number'] },
|
|
980
|
+
'Camera.deviceFacingAt': { target: 'gea::host::Camera.deviceFacingAt', hints: ['number'] },
|
|
981
|
+
};
|
|
982
|
+
const match = calls[path];
|
|
983
|
+
if (!match)
|
|
984
|
+
return null;
|
|
985
|
+
const lowered = args.map((arg, index) => {
|
|
986
|
+
if (match.optionsRecord && arg.kind === 'object') {
|
|
987
|
+
const typed = lowerAnonymousOptionsStruct(context, arg);
|
|
988
|
+
if (typed)
|
|
989
|
+
return typed;
|
|
990
|
+
}
|
|
991
|
+
return lowerExpr(context, arg, match.hints?.[index] ?? 'any');
|
|
992
|
+
});
|
|
993
|
+
return lowered.every(Boolean) ? `${match.target}(${lowered.join(', ')})` : null;
|
|
994
|
+
}
|
|
995
|
+
// An options-record literal for a host template overload (`Camera.setExposure(
|
|
996
|
+
// { mode, bias })`) constructs a TYPED anonymous struct carrying exactly the
|
|
997
|
+
// literal's fields, each typed by its lowered expression. The host reads the
|
|
998
|
+
// fields through `requires` member probes, so absent fields simply don't
|
|
999
|
+
// exist and the reader's fallback applies — no gea_cpp_value boxing, no
|
|
1000
|
+
// dependency on a named `__gea_type_*` struct being emitted. Nested object
|
|
1001
|
+
// fields (setFocus's `point: { x, y }`) recurse into nested anonymous
|
|
1002
|
+
// structs. Bails (boxed fallback) when a field name isn't a clean C++
|
|
1003
|
+
// identifier, since the host probes by member name.
|
|
1004
|
+
function lowerAnonymousOptionsStruct(context, arg) {
|
|
1005
|
+
if (arg.kind !== 'object' || arg.fields.length === 0)
|
|
1006
|
+
return null;
|
|
1007
|
+
const locals = [];
|
|
1008
|
+
const members = [];
|
|
1009
|
+
const inits = [];
|
|
1010
|
+
for (let index = 0; index < arg.fields.length; index += 1) {
|
|
1011
|
+
const field = arg.fields[index];
|
|
1012
|
+
if (sanitizeCppIdentifier(field.name) !== field.name)
|
|
1013
|
+
return null;
|
|
1014
|
+
const value = field.value.kind === 'object'
|
|
1015
|
+
? lowerAnonymousOptionsStruct(context, field.value)
|
|
1016
|
+
: lowerExpr(context, field.value, 'any');
|
|
1017
|
+
if (!value)
|
|
1018
|
+
return null;
|
|
1019
|
+
locals.push(`auto __gea_opt_${index} = ${value};`);
|
|
1020
|
+
members.push(`decltype(__gea_opt_${index}) ${field.name};`);
|
|
1021
|
+
inits.push(`__gea_opt_${index}`);
|
|
1022
|
+
}
|
|
1023
|
+
return `([&]() { ${locals.join(' ')} struct { ${members.join(' ')} } __gea_options{${inits.join(', ')}}; return __gea_options; })()`;
|
|
1024
|
+
}
|
|
1025
|
+
// An object literal pushed/unshifted/spliced into a TYPED-STORAGE store array
|
|
1026
|
+
// constructs the element struct DIRECTLY. Without this, the literal lowers to
|
|
1027
|
+
// a boxed gea_cpp_value record (shared entries vector allocation) that
|
|
1028
|
+
// push_array_items immediately decodes back through
|
|
1029
|
+
// `Element::__gea_from_value` — a build-then-unbuild round trip on every push.
|
|
1030
|
+
//
|
|
1031
|
+
// The storage may have been typed by EITHER side (the plugin's typedStorage
|
|
1032
|
+
// metadata, or geatsc's checker via a restored JSDoc class-field hint that the
|
|
1033
|
+
// plugin's shape analysis can't see — car-dashboard's `speedTicks:
|
|
1034
|
+
// GaugeTick[] = []` has an empty-initializer shape). So the gate is COMPILE
|
|
1035
|
+
// TIME: resolve the element type from the member itself and take the typed
|
|
1036
|
+
// branch only when it's a real struct exposing `record_set_literal` (both
|
|
1037
|
+
// geatsc's `__gea_type_X` interface structs — which also maintain their
|
|
1038
|
+
// `__gea_has_*` presence flags through it — and the plugin's synthesized
|
|
1039
|
+
// `<Store>_<field>_item` structs do). Untyped `vector<gea_cpp_value>` storage
|
|
1040
|
+
// keeps the boxed record shape, bit-for-bit the same as lowerObject's.
|
|
1041
|
+
function lowerTypedElementLiteral(context, fieldName, arg) {
|
|
1042
|
+
if (arg.kind !== 'object' || arg.fields.length === 0)
|
|
1043
|
+
return null;
|
|
1044
|
+
const member = `(*this).${sanitizeCppIdentifier(fieldName)}`;
|
|
1045
|
+
const valueLocals = [];
|
|
1046
|
+
const typedWrites = [];
|
|
1047
|
+
const boxedWrites = [];
|
|
1048
|
+
for (let index = 0; index < arg.fields.length; index += 1) {
|
|
1049
|
+
const literalField = arg.fields[index];
|
|
1050
|
+
const value = lowerExpr(context, literalField.value, 'any');
|
|
1051
|
+
if (!value)
|
|
1052
|
+
return null;
|
|
1053
|
+
valueLocals.push(`auto __gea_value_${index} = ${value};`);
|
|
1054
|
+
// Typed element: write each field through the per-item-type `set_typed_field`
|
|
1055
|
+
// helper (gea_ir::set_typed_field), emitted for every typed store-array item
|
|
1056
|
+
// — synthesized AND interface-reuse, in BOTH lean and dynamic-fallback modes
|
|
1057
|
+
// (see generateTypedItemHelpers). It assigns the C++ field directly with no
|
|
1058
|
+
// gea_cpp_value box/unbox. The lean typed struct no longer carries
|
|
1059
|
+
// `record_set_literal` (generateTypedSlotMemberMethods strips it when the
|
|
1060
|
+
// dynamic fallback is off), so set_typed_field is the construction op that
|
|
1061
|
+
// works across modes.
|
|
1062
|
+
typedWrites.push(`gea_ir::set_typed_field(__gea_item, ${JSON.stringify(literalField.name)}, __gea_value_${index});`);
|
|
1063
|
+
boxedWrites.push(`__gea_record.record_set_literal(${JSON.stringify(literalField.name)}, gea_cpp_key(__gea_value_${index}));`);
|
|
1064
|
+
}
|
|
1065
|
+
// A C++20 templated lambda parameterised on the element type. `if constexpr`
|
|
1066
|
+
// only discards its untaken branch inside a template; in the non-template
|
|
1067
|
+
// store-method body a plain lambda would semantically check BOTH branches and
|
|
1068
|
+
// deduce `auto` from both — so a typed element (returns __GeaElem) and the
|
|
1069
|
+
// gea_cpp_value fallback (returns gea_cpp_value) would clash with "return type
|
|
1070
|
+
// must match previous return type". Templating on `__GeaElem` makes the
|
|
1071
|
+
// condition dependent, so exactly one branch is instantiated and type-deduced.
|
|
1072
|
+
return (`([&]<typename __GeaElem>() { ${valueLocals.join(' ')} ` +
|
|
1073
|
+
`if constexpr (std::is_same_v<__GeaElem, gea_cpp_value>) { ` +
|
|
1074
|
+
`gea_cpp_value __gea_record; __gea_record.kind = gea_cpp_value::kind_t::record; __gea_record.entries = std::make_shared<std::vector<std::pair<std::string, gea_cpp_value>>>(); ${boxedWrites.join(' ')} return __gea_record; ` +
|
|
1075
|
+
`} else { __GeaElem __gea_item{}; ${typedWrites.join(' ')} return __gea_item; } ` +
|
|
1076
|
+
`}.template operator()<typename std::decay_t<decltype(${member})>::value_type>())`);
|
|
1077
|
+
}
|
|
1078
|
+
function lowerElementLiteralForType(context, elementType, arg) {
|
|
1079
|
+
if (arg.kind !== 'object' || arg.fields.length === 0)
|
|
1080
|
+
return null;
|
|
1081
|
+
const writes = [];
|
|
1082
|
+
for (const field of arg.fields) {
|
|
1083
|
+
const value = lowerExpr(context, field.value, 'any');
|
|
1084
|
+
if (!value)
|
|
1085
|
+
return null;
|
|
1086
|
+
writes.push(`__gea_item.record_set_literal(${JSON.stringify(field.name)}, gea_cpp_key(${value}));`);
|
|
1087
|
+
}
|
|
1088
|
+
return `([&]() { ${elementType} __gea_item{}; ${writes.join(' ')} return __gea_item; })()`;
|
|
1089
|
+
}
|
|
1090
|
+
function lowerObject(context, expr) {
|
|
1091
|
+
const writes = expr.fields.map((field) => {
|
|
1092
|
+
const value = lowerExpr(context, field.value, 'any');
|
|
1093
|
+
return value ? `__gea_record.record_set_literal(${JSON.stringify(field.name)}, gea_cpp_key(${value}));` : null;
|
|
1094
|
+
});
|
|
1095
|
+
if (!writes.every(Boolean))
|
|
1096
|
+
return null;
|
|
1097
|
+
return `([&]() { gea_cpp_value __gea_record; __gea_record.kind = gea_cpp_value::kind_t::record; __gea_record.entries = std::make_shared<std::vector<std::pair<std::string, gea_cpp_value>>>(); ${writes.join(' ')} return __gea_record; })()`;
|
|
1098
|
+
}
|
|
1099
|
+
function lowerArray(context, expr) {
|
|
1100
|
+
// An empty array literal must NOT be treated as vector<string> (every() is
|
|
1101
|
+
// vacuously true on []). It's typically `const items = []` accumulated via
|
|
1102
|
+
// push(record) then assigned to a record-typed store field, so the generic
|
|
1103
|
+
// gea_cpp_value vector is the only safe element type.
|
|
1104
|
+
if (expr.elements.length === 0)
|
|
1105
|
+
return 'std::vector<gea_cpp_value>{}';
|
|
1106
|
+
if (expr.elements.every((element) => element.kind === 'string')) {
|
|
1107
|
+
const elements = expr.elements.map((element) => lowerExpr(context, element, 'string'));
|
|
1108
|
+
return elements.every(Boolean) ? `std::vector<std::string>{${elements.join(', ')}}` : null;
|
|
1109
|
+
}
|
|
1110
|
+
if (expr.elements.every((element) => element.kind === 'number')) {
|
|
1111
|
+
const elements = expr.elements.map((element) => lowerExpr(context, element, 'number'));
|
|
1112
|
+
return elements.every(Boolean) ? `std::vector<double>{${elements.join(', ')}}` : null;
|
|
1113
|
+
}
|
|
1114
|
+
const elements = expr.elements.map((element) => {
|
|
1115
|
+
const lowered = lowerExpr(context, element, 'any');
|
|
1116
|
+
return lowered ? `gea_cpp_key(${lowered})` : null;
|
|
1117
|
+
});
|
|
1118
|
+
return elements.every(Boolean) ? `std::vector<gea_cpp_value>{${elements.join(', ')}}` : null;
|
|
1119
|
+
}
|
|
1120
|
+
function lowerBinary(context, expr, hint) {
|
|
1121
|
+
const op = normalizedBinaryOp(expr.op);
|
|
1122
|
+
// `+` is string concatenation ONLY when an operand is actually a string.
|
|
1123
|
+
// A purely numeric `+` whose RESULT is needed as a string (e.g. `${idx + 1}`
|
|
1124
|
+
// in a template literal, hint='string') must be evaluated as a numeric add
|
|
1125
|
+
// and THEN stringified — not split into `str(idx) + "1"`. Triggering concat
|
|
1126
|
+
// on `hint === 'string'` alone wrongly forced the numeric add down the
|
|
1127
|
+
// concat path (so `#${idx + 1}` rendered "#01","#11","#21"…). The numeric
|
|
1128
|
+
// path below + coerceForHint('string') already produces the correct
|
|
1129
|
+
// `gea_cpp_to_string((idx + 1.0))`, and `${5 + 3}` -> "8".
|
|
1130
|
+
if (op === '+' && (containsString(context, expr.left) || containsString(context, expr.right))) {
|
|
1131
|
+
const left = lowerExpr(context, expr.left, 'string');
|
|
1132
|
+
const right = lowerExpr(context, expr.right, 'string');
|
|
1133
|
+
return left && right ? `${left} + ${right}` : null;
|
|
1134
|
+
}
|
|
1135
|
+
if (op === '|') {
|
|
1136
|
+
const left = lowerExpr(context, expr.left, 'number');
|
|
1137
|
+
const right = lowerExpr(context, expr.right, 'number');
|
|
1138
|
+
return left && right ? coerceForHint(`static_cast<double>(gea::runtime::numeric::to_int32(${left}) | gea::runtime::numeric::to_int32(${right}))`, hint) : null;
|
|
1139
|
+
}
|
|
1140
|
+
if (op === '%') {
|
|
1141
|
+
const left = lowerExpr(context, expr.left, 'number');
|
|
1142
|
+
const right = lowerExpr(context, expr.right, 'number');
|
|
1143
|
+
// JS `%` is the floating-point remainder: the result takes the sign of the
|
|
1144
|
+
// dividend (`-7 % 16 === -7`). The previous lowering cast both operands to
|
|
1145
|
+
// std::size_t, but converting a negative double to an unsigned integer is
|
|
1146
|
+
// undefined behavior (yields 0 on 32-bit ARM), so any `%` with a negative
|
|
1147
|
+
// left operand silently produced 0 — e.g. a virtual-list's circular slot
|
|
1148
|
+
// recycling `(k - start) % SLOT_COUNT` collapsed every wrapped slot to the
|
|
1149
|
+
// same item. std::fmod matches JS semantics for all inputs.
|
|
1150
|
+
return left && right ? coerceForHint(`std::fmod(${left}, ${right})`, hint) : null;
|
|
1151
|
+
}
|
|
1152
|
+
if (op === '<<') {
|
|
1153
|
+
const left = lowerExpr(context, expr.left, 'number');
|
|
1154
|
+
const right = lowerExpr(context, expr.right, 'number');
|
|
1155
|
+
return left && right ? coerceForHint(`static_cast<double>(gea::runtime::numeric::to_int32(${left}) << (gea::runtime::numeric::to_uint32(${right}) & 31u))`, hint) : null;
|
|
1156
|
+
}
|
|
1157
|
+
if (op === '>>') {
|
|
1158
|
+
const left = lowerExpr(context, expr.left, 'number');
|
|
1159
|
+
const right = lowerExpr(context, expr.right, 'number');
|
|
1160
|
+
return left && right ? coerceForHint(`static_cast<double>(gea::runtime::numeric::to_int32(${left}) >> (gea::runtime::numeric::to_uint32(${right}) & 31u))`, hint) : null;
|
|
1161
|
+
}
|
|
1162
|
+
if (op === '>>>') {
|
|
1163
|
+
const left = lowerExpr(context, expr.left, 'number');
|
|
1164
|
+
const right = lowerExpr(context, expr.right, 'number');
|
|
1165
|
+
return left && right ? coerceForHint(`static_cast<double>(gea::runtime::numeric::to_uint32(${left}) >> (gea::runtime::numeric::to_uint32(${right}) & 31u))`, hint) : null;
|
|
1166
|
+
}
|
|
1167
|
+
const boolOp = op === '&&' || op === '||';
|
|
1168
|
+
// For equality / inequality / ordering, pick the operand hint from the
|
|
1169
|
+
// actual types — coercing a string param or string field to number would
|
|
1170
|
+
// emit `to_number(noteId) == "literal"` which fails to compile against a
|
|
1171
|
+
// std::string field. The previous logic hardcoded 'number', so equality
|
|
1172
|
+
// comparisons involving any string operand were silently miscompiled.
|
|
1173
|
+
const comparisonOp = op === '==' || op === '!=' || op === '<' || op === '>' || op === '<=' || op === '>=';
|
|
1174
|
+
const stringComparison = comparisonOp && (containsString(context, expr.left) || containsString(context, expr.right));
|
|
1175
|
+
const operandHint = boolOp ? 'boolean' : stringComparison ? 'string' : 'number';
|
|
1176
|
+
const left = lowerExpr(context, expr.left, operandHint);
|
|
1177
|
+
const right = lowerExpr(context, expr.right, operandHint);
|
|
1178
|
+
// A numeric/comparison result used in a string context (e.g. `${a * b}` in a
|
|
1179
|
+
// template literal) must be stringified; coerceForHint is a no-op for the
|
|
1180
|
+
// number/any hints so existing call sites are unaffected.
|
|
1181
|
+
if (!left || !right)
|
|
1182
|
+
return null;
|
|
1183
|
+
const combined = `(${left} ${op} ${right})`;
|
|
1184
|
+
// Comparison (`==`,`<`,…) and logical (`&&`,`||`) operators already produce a
|
|
1185
|
+
// C++ bool, so wrapping them in to_boolean() for a boolean hint is redundant
|
|
1186
|
+
// noise (it emitted `to_boolean((y >= 0.0))` in for-conditions and `&&`
|
|
1187
|
+
// operands). Coerce only when the boolean result feeds a non-boolean hint.
|
|
1188
|
+
if ((boolOp || comparisonOp) && hint === 'boolean')
|
|
1189
|
+
return combined;
|
|
1190
|
+
return coerceForHint(combined, hint);
|
|
1191
|
+
}
|
|
1192
|
+
function lowerConditional(context, expr, hint) {
|
|
1193
|
+
const test = lowerExpr(context, expr.test, 'boolean');
|
|
1194
|
+
const consequent = lowerExpr(context, expr.consequent, hint);
|
|
1195
|
+
const alternate = lowerExpr(context, expr.alternate, hint);
|
|
1196
|
+
return test && consequent && alternate ? `(${test} ? ${consequent} : ${alternate})` : null;
|
|
1197
|
+
}
|
|
1198
|
+
function containsString(context, expr) {
|
|
1199
|
+
if (expr.kind === 'string')
|
|
1200
|
+
return true;
|
|
1201
|
+
if (expr.kind === 'identifier') {
|
|
1202
|
+
if (typeof context.constants.get(expr.name) === 'string')
|
|
1203
|
+
return true;
|
|
1204
|
+
if (context.paramHints.get(expr.name) === 'string')
|
|
1205
|
+
return true;
|
|
1206
|
+
return false;
|
|
1207
|
+
}
|
|
1208
|
+
if (expr.kind === 'conditional')
|
|
1209
|
+
return containsString(context, expr.consequent) || containsString(context, expr.alternate);
|
|
1210
|
+
if (expr.kind === 'member') {
|
|
1211
|
+
const field = thisFieldName(expr);
|
|
1212
|
+
if (field && fieldHint(context.store, field) === 'string')
|
|
1213
|
+
return true;
|
|
1214
|
+
const item = thisArrayItemFieldTarget(expr);
|
|
1215
|
+
if (item && arrayItemFieldHint(context.store, item.arrayField, item.itemField) === 'string')
|
|
1216
|
+
return true;
|
|
1217
|
+
const aliasField = arrayElementAliasFieldTarget(context, expr);
|
|
1218
|
+
if (aliasField && arrayItemFieldHint(context.store, aliasField.alias.arrayField, aliasField.itemField) === 'string')
|
|
1219
|
+
return true;
|
|
1220
|
+
return false;
|
|
1221
|
+
}
|
|
1222
|
+
if (expr.kind === 'binary' && (expr.op === '+' || expr.op === '+=')) {
|
|
1223
|
+
return containsString(context, expr.left) || containsString(context, expr.right);
|
|
1224
|
+
}
|
|
1225
|
+
if (expr.kind === 'logical')
|
|
1226
|
+
return containsString(context, expr.left) || containsString(context, expr.right);
|
|
1227
|
+
return false;
|
|
1228
|
+
}
|
|
1229
|
+
function normalizedBinaryOp(op) {
|
|
1230
|
+
if (op === '===')
|
|
1231
|
+
return '==';
|
|
1232
|
+
if (op === '!==')
|
|
1233
|
+
return '!=';
|
|
1234
|
+
return op;
|
|
1235
|
+
}
|
|
1236
|
+
function constantExpr(value, hint) {
|
|
1237
|
+
// In a string context (e.g. string concatenation / template literals) every
|
|
1238
|
+
// constant must be stringified the way JS would — `5000 + ''` -> "5000",
|
|
1239
|
+
// `true + ''` -> "true", `null + ''` -> "null". Emitting the raw number
|
|
1240
|
+
// literal `5000.0` produces invalid `double + std::string` C++.
|
|
1241
|
+
if (hint === 'string')
|
|
1242
|
+
return `std::string(${JSON.stringify(String(value))})`;
|
|
1243
|
+
if (typeof value === 'number')
|
|
1244
|
+
return cppNumber(value);
|
|
1245
|
+
if (typeof value === 'boolean')
|
|
1246
|
+
return value ? 'true' : 'false';
|
|
1247
|
+
if (value === null)
|
|
1248
|
+
return 'gea_cpp_value::null_v()';
|
|
1249
|
+
return hint === 'any' ? `std::string(${JSON.stringify(value)})` : JSON.stringify(value);
|
|
1250
|
+
}
|
|
1251
|
+
function coerceForHint(value, hint) {
|
|
1252
|
+
if (hint === 'string')
|
|
1253
|
+
return `gea_cpp_to_string(${value})`;
|
|
1254
|
+
if (hint === 'boolean')
|
|
1255
|
+
return `gea::runtime::coerce::to_boolean(${value})`;
|
|
1256
|
+
return value;
|
|
1257
|
+
}
|
|
1258
|
+
function cppNumber(value) {
|
|
1259
|
+
return Number.isInteger(value) ? `${value}.0` : String(value);
|
|
1260
|
+
}
|
|
1261
|
+
function parenthesize(value) {
|
|
1262
|
+
return /^[A-Za-z0-9_:().]+$/.test(value) ? value : `(${value})`;
|
|
1263
|
+
}
|