@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
package/dist/cpp-ir.js
ADDED
|
@@ -0,0 +1,2426 @@
|
|
|
1
|
+
import { generateMountedRendererDeclarations, generateMountedRenderers, generateMountedRendererSections, } from "./cpp-mounted.js";
|
|
2
|
+
import { domStyleAccessorInteropSource } from "./cpp-ir-dom-style.js";
|
|
3
|
+
import { isReactiveComponentIr } from "./cpp-reactive-component.js";
|
|
4
|
+
import { storeDynamicFallbackEnabled, storeHubBindHelperLines, } from "./cpp-store-hub.js";
|
|
5
|
+
import { collectStoreArraySources, collectStoreFields, collectStoreMethods, collectSingletonMethodsFromSourceFiles, enrichConstantInitializerShapes, generateInterfaceItemHelperForwardDeclarations, generateStoreDeclarations, generateStoreStateReaders, generateTypedSelfStoreReaders, generateTypedStoreReaderDefinitions, storeInstanceGlobalName, } from "./cpp-stores.js";
|
|
6
|
+
import { sanitizeCppIdentifier } from "./utils.js";
|
|
7
|
+
import { existsSync, readFileSync, statSync } from "node:fs";
|
|
8
|
+
import { dirname, resolve } from "node:path";
|
|
9
|
+
import ts from "typescript";
|
|
10
|
+
export function generateCppIrSource(ir, irPath, modules, microtasksNamespace = "gea::framework::app::generated") {
|
|
11
|
+
const drainNamespace = cppNamespace(microtasksNamespace);
|
|
12
|
+
const usesCanvas = irUsesCanvas(ir, modules);
|
|
13
|
+
const emitBoxedCanvasContext = usesCanvas && !directCanvasContextEnabled();
|
|
14
|
+
const lines = [
|
|
15
|
+
"// @geastack/geatsc-plugin-gea consumed gea IR",
|
|
16
|
+
`// ir=${irPath ?? "<inline>"}`,
|
|
17
|
+
`// modules=${ir.modules.length} components=${ir.components.length} stores=${ir.stores.length}`,
|
|
18
|
+
`// emitted-modules=${modules.length}`,
|
|
19
|
+
"",
|
|
20
|
+
...(directCanvasContextEnabled()
|
|
21
|
+
? ['#include "direct_canvas_runtime.h"']
|
|
22
|
+
: []),
|
|
23
|
+
'#include "gea/embedded.h"',
|
|
24
|
+
'#include "ui/tree_internal.h"',
|
|
25
|
+
"#include <algorithm>",
|
|
26
|
+
"#include <cmath>",
|
|
27
|
+
"#include <cstdio>",
|
|
28
|
+
"#include <functional>",
|
|
29
|
+
"#include <memory>",
|
|
30
|
+
"#include <optional>",
|
|
31
|
+
"#include <string>",
|
|
32
|
+
"#include <type_traits>",
|
|
33
|
+
"#include <utility>",
|
|
34
|
+
"#include <vector>",
|
|
35
|
+
"",
|
|
36
|
+
...numberFormattingSource(),
|
|
37
|
+
"",
|
|
38
|
+
...(emitBoxedCanvasContext ? canvasContextValueSource() : []),
|
|
39
|
+
"",
|
|
40
|
+
`namespace ${drainNamespace} {`,
|
|
41
|
+
"void drainMicrotasks() {",
|
|
42
|
+
" gea_cpp_drain_microtasks();",
|
|
43
|
+
"}",
|
|
44
|
+
`} // namespace ${drainNamespace}`,
|
|
45
|
+
"",
|
|
46
|
+
];
|
|
47
|
+
return lines.join("\n");
|
|
48
|
+
}
|
|
49
|
+
function directCanvasContextEnabled() {
|
|
50
|
+
const value = String(process.env.GEA_EMBEDDED_DIRECT_CANVAS_CONTEXT ?? "").toLowerCase();
|
|
51
|
+
return value === "1" || value === "true" || value === "yes";
|
|
52
|
+
}
|
|
53
|
+
export function irUsesCanvas(ir, modules) {
|
|
54
|
+
if (ir.hostCapabilities.includes("canvas"))
|
|
55
|
+
return true;
|
|
56
|
+
if (ir.components.some((component) => component.template.html.includes("<canvas")))
|
|
57
|
+
return true;
|
|
58
|
+
// A non-component app (no JSX `<canvas>` template) can still drive the canvas:
|
|
59
|
+
// `const ctx = Display.ctx` or `canvas.getContext('2d')`. The IR carries no
|
|
60
|
+
// signal for that, but geatsc still lowers `ctx.*` to `gea_ir::canvas*` calls,
|
|
61
|
+
// so the matching definitions must be emitted or the program won't link.
|
|
62
|
+
return modulesUseCanvas(modules);
|
|
63
|
+
}
|
|
64
|
+
// Source-level canvas detection for apps the IR doesn't flag — scans the emitted
|
|
65
|
+
// module sources for the ways a 2D context is obtained or typed. A false
|
|
66
|
+
// positive only emits unused inline/template helpers (dead-stripped by the C++
|
|
67
|
+
// compiler); a false negative is a link error, so this errs toward emitting.
|
|
68
|
+
const CANVAS_SOURCE_SIGNALS = [
|
|
69
|
+
"Display.ctx",
|
|
70
|
+
"getContext",
|
|
71
|
+
"CanvasRenderingContext2D",
|
|
72
|
+
];
|
|
73
|
+
function modulesUseCanvas(modules) {
|
|
74
|
+
if (!modules)
|
|
75
|
+
return false;
|
|
76
|
+
return modules.some((module) => {
|
|
77
|
+
// `sourceFile` is a real ts.SourceFile at runtime (carrying `.text`) but the
|
|
78
|
+
// plugin types it narrowly; fall back to reading the file by name.
|
|
79
|
+
const runtimeText = module.sourceFile.text;
|
|
80
|
+
const fileName = module.sourceFile.fileName;
|
|
81
|
+
const text = typeof runtimeText === "string"
|
|
82
|
+
? runtimeText
|
|
83
|
+
: existsSync(fileName)
|
|
84
|
+
? readFileSync(fileName, "utf8")
|
|
85
|
+
: "";
|
|
86
|
+
return CANVAS_SOURCE_SIGNALS.some((signal) => text.includes(signal));
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
function nativeDisposerSource() {
|
|
90
|
+
return [
|
|
91
|
+
"struct NativeDisposer : public std::enable_shared_from_this<NativeDisposer> {",
|
|
92
|
+
" std::vector<std::function<void()>> callbacks;",
|
|
93
|
+
" std::vector<std::shared_ptr<NativeDisposer>> children;",
|
|
94
|
+
"",
|
|
95
|
+
" void add(std::function<void()> callback) {",
|
|
96
|
+
" callbacks.push_back(std::move(callback));",
|
|
97
|
+
" }",
|
|
98
|
+
"",
|
|
99
|
+
" std::shared_ptr<NativeDisposer> child() {",
|
|
100
|
+
" auto next = std::make_shared<NativeDisposer>();",
|
|
101
|
+
" children.push_back(next);",
|
|
102
|
+
" return next;",
|
|
103
|
+
" }",
|
|
104
|
+
"",
|
|
105
|
+
" void dispose() {",
|
|
106
|
+
" for (auto &child_disposer : children) child_disposer->dispose();",
|
|
107
|
+
" children.clear();",
|
|
108
|
+
" for (auto it = callbacks.rbegin(); it != callbacks.rend(); ++it) (*it)();",
|
|
109
|
+
" callbacks.clear();",
|
|
110
|
+
" }",
|
|
111
|
+
"};",
|
|
112
|
+
];
|
|
113
|
+
}
|
|
114
|
+
function numberFormattingSource() {
|
|
115
|
+
return [
|
|
116
|
+
"namespace {",
|
|
117
|
+
"inline std::string geaStoreNumberToFixed(double value, double digits) {",
|
|
118
|
+
' if (std::isnan(value)) return std::string("NaN");',
|
|
119
|
+
' if (std::isinf(value)) return value > 0.0 ? std::string("Infinity") : std::string("-Infinity");',
|
|
120
|
+
" int d = static_cast<int>(digits);",
|
|
121
|
+
" if (d < 0) d = 0;",
|
|
122
|
+
" if (d > 100) d = 100;",
|
|
123
|
+
" const double scale = std::pow(10.0, static_cast<double>(d));",
|
|
124
|
+
" const double rounded = std::round(value * scale) / scale;",
|
|
125
|
+
" char buffer[160];",
|
|
126
|
+
' std::snprintf(buffer, sizeof(buffer), "%.*f", d, rounded);',
|
|
127
|
+
" return std::string(buffer);",
|
|
128
|
+
"}",
|
|
129
|
+
"} // namespace",
|
|
130
|
+
];
|
|
131
|
+
}
|
|
132
|
+
export function generateCppIrNamespaceSource(ir, modules, options = {}) {
|
|
133
|
+
const components = sortedComponents(ir.components);
|
|
134
|
+
const constants = collectIrConstants(ir);
|
|
135
|
+
enrichConstantInitializerShapes(ir.stores, constants, ir.modules.map((module) => module.file));
|
|
136
|
+
const storeArrayFields = collectStoreArraySources(ir.stores);
|
|
137
|
+
const storeFields = collectStoreFields(ir.stores);
|
|
138
|
+
const storeMethods = collectStoreMethods(ir.stores).concat(collectSingletonMethodsFromSourceFiles(ir.modules.map((module) => module.file), ir.stores));
|
|
139
|
+
// Reactive components use a typed `mount_<Name>(shared_ptr<Name>&, …)` renderer
|
|
140
|
+
// (see cpp-reactive-component); its forward declaration names the class, so it
|
|
141
|
+
// can't live in this early namespace block (which precedes `class <Name>;`).
|
|
142
|
+
// It is injected after the class forward declaration in index.ts instead.
|
|
143
|
+
const nonReactiveComponents = components.filter((component) => !isReactiveComponentIr(component));
|
|
144
|
+
const mountedRendererDeclarations = generateMountedRendererDeclarations(nonReactiveComponents, storeFields, storeArrayFields, constants, storeMethods);
|
|
145
|
+
const needsTemplateRuntime = components.length > 0 ||
|
|
146
|
+
ir.stores.length > 0 ||
|
|
147
|
+
mountedRendererDeclarations.length > 0;
|
|
148
|
+
// Host shims can lower library/runtime classes to typed C++ methods that use
|
|
149
|
+
// gea_ir::StoreMethodBatch even when the app IR has no stores/components.
|
|
150
|
+
const needsStoreRuntime = needsTemplateRuntime || modules.length > 0;
|
|
151
|
+
const lines = [
|
|
152
|
+
"// @geastack/geatsc-plugin-gea app namespace helpers",
|
|
153
|
+
// Typed mount params in this block name the typed-carrier store classes;
|
|
154
|
+
// module emission order can place geatsc's own forward declarations AFTER
|
|
155
|
+
// this block, so declare them here at the same (generated-namespace)
|
|
156
|
+
// scope the classes are defined in.
|
|
157
|
+
...typedCarrierStoreForwardDeclarations(ir.stores),
|
|
158
|
+
"namespace gea_ir {",
|
|
159
|
+
"",
|
|
160
|
+
...(needsTemplateRuntime ? nativeDisposerSource() : []),
|
|
161
|
+
"",
|
|
162
|
+
...(options.includeDomInterop
|
|
163
|
+
? canvasInteropSource(irUsesCanvas(ir, modules))
|
|
164
|
+
: []),
|
|
165
|
+
// Forward-declare the interface-reuse typed-item helpers before
|
|
166
|
+
// `storeRuntimeSource` so the template-dependent `set_typed_field(slot, …)`
|
|
167
|
+
// call inside `storeVectorItemPropertySet` resolves by ordinary lookup (ADL
|
|
168
|
+
// can't reach gea_ir when `slot` is a global `::__gea_type_<Name>`).
|
|
169
|
+
...generateInterfaceItemHelperForwardDeclarations(ir.stores),
|
|
170
|
+
// Merge the global gea_cpp_key overload set (value.cpp generic + per-record
|
|
171
|
+
// boxers) into gea_ir. Any gea_cpp_key overload declared inside this
|
|
172
|
+
// namespace (e.g. the NodeHandle boxer apps with DOM templates emit) would
|
|
173
|
+
// otherwise HIDE every global overload from the unqualified calls in
|
|
174
|
+
// storeRuntimeSource — boxing a plain double store field then fails to
|
|
175
|
+
// compile.
|
|
176
|
+
...(needsStoreRuntime
|
|
177
|
+
? ["using ::gea_cpp_key;", ...storeRuntimeSource()]
|
|
178
|
+
: []),
|
|
179
|
+
...generateStoreDeclarations(ir.stores),
|
|
180
|
+
...generateStoreStateReaders(ir.stores),
|
|
181
|
+
...mountedRendererDeclarations,
|
|
182
|
+
"}",
|
|
183
|
+
"",
|
|
184
|
+
];
|
|
185
|
+
return lines.join("\n");
|
|
186
|
+
}
|
|
187
|
+
function typedCarrierStoreForwardDeclarations(stores) {
|
|
188
|
+
const declarations = [
|
|
189
|
+
...new Set(stores
|
|
190
|
+
.filter((store) => !store.selfStore)
|
|
191
|
+
.map((store) => `class ${sanitizeCppIdentifier(store.className)};`)),
|
|
192
|
+
];
|
|
193
|
+
return declarations.length > 0 ? [...declarations, ""] : [];
|
|
194
|
+
}
|
|
195
|
+
export function generateCppIrMountedSource(ir, knownGlobalAccessorTypes = new Map()) {
|
|
196
|
+
const components = sortedComponents(ir.components);
|
|
197
|
+
const constants = collectIrConstants(ir);
|
|
198
|
+
enrichConstantInitializerShapes(ir.stores, constants, ir.modules.map((module) => module.file));
|
|
199
|
+
const storeArrayFields = collectStoreArraySources(ir.stores);
|
|
200
|
+
const storeFields = collectStoreFields(ir.stores);
|
|
201
|
+
const storeMethods = collectStoreMethods(ir.stores).concat(collectSingletonMethodsFromSourceFiles(ir.modules.map((module) => module.file), ir.stores));
|
|
202
|
+
// collectSingletonMethodsFromSourceFiles guesses `globalAccess: 'reference'`
|
|
203
|
+
// for non-store singleton classes, but geatsc's class-storage strategy can
|
|
204
|
+
// emit the global cell as `std::shared_ptr<Class> &__gea_global_x()`. The
|
|
205
|
+
// ACTUAL accessor types scanned from the generated source are authoritative
|
|
206
|
+
// — reconcile the guess so event lowering emits `->method()` behind a null
|
|
207
|
+
// check instead of `.method()` on a shared_ptr (compile error).
|
|
208
|
+
for (const method of storeMethods) {
|
|
209
|
+
if (method.globalAccess !== "reference" ||
|
|
210
|
+
!method.storeGlobalName ||
|
|
211
|
+
method.storeGlobalName === "this")
|
|
212
|
+
continue;
|
|
213
|
+
const accessorType = knownGlobalAccessorTypes.get(`__gea_global_${sanitizeCppIdentifier(method.storeGlobalName)}`);
|
|
214
|
+
if (accessorType?.startsWith("std::shared_ptr<"))
|
|
215
|
+
method.globalAccess = "shared_ptr";
|
|
216
|
+
}
|
|
217
|
+
// ALL components — including self-store ReactiveComponents — flow through the
|
|
218
|
+
// ONE renderer; mountedRendererForComponent picks typed field access for
|
|
219
|
+
// `reactiveState` components. The typed `read_X(shared_ptr<Class>)` reader
|
|
220
|
+
// overloads are emitted first (this block sits after the class definitions, so
|
|
221
|
+
// they can dereference the typed fields).
|
|
222
|
+
const mountedRendererSections = generateMountedRendererSections(components, storeFields, storeArrayFields, constants, storeMethods);
|
|
223
|
+
const mountedRenderers = mountedRendererSections.flatMap((section) => section.lines);
|
|
224
|
+
const typedSelfReaders = generateTypedSelfStoreReaders(ir.stores);
|
|
225
|
+
// Typed fast-path definitions for the scalar reader DECLARATIONS the early
|
|
226
|
+
// block emitted — must be present whenever stores exist, even with no
|
|
227
|
+
// mounted renderers, so every declared reader has its definition.
|
|
228
|
+
const typedStoreReaders = generateTypedStoreReaderDefinitions(ir.stores);
|
|
229
|
+
const hubBindHelper = storeHubBindHelperLines(ir);
|
|
230
|
+
if (mountedRenderers.length === 0 &&
|
|
231
|
+
typedSelfReaders.length === 0 &&
|
|
232
|
+
typedStoreReaders.length === 0)
|
|
233
|
+
return "";
|
|
234
|
+
// Recognized globals carry typed cells — their forward declarations here
|
|
235
|
+
// must match geatsc core's or the two declarations clash on return type.
|
|
236
|
+
const globalAccessorTypes = new Map();
|
|
237
|
+
for (const [name, type] of knownGlobalAccessorTypes)
|
|
238
|
+
globalAccessorTypes.set(name, type);
|
|
239
|
+
for (const store of ir.stores) {
|
|
240
|
+
if (store.selfStore)
|
|
241
|
+
continue;
|
|
242
|
+
const name = storeInstanceGlobalName(store);
|
|
243
|
+
if (name)
|
|
244
|
+
globalAccessorTypes.set(`__gea_global_${name}`, `std::shared_ptr<${sanitizeCppIdentifier(store.className)}>`);
|
|
245
|
+
}
|
|
246
|
+
const globalDeclarations = mountedRendererGlobalAccessorDeclarations(mountedRenderers, globalAccessorTypes);
|
|
247
|
+
return [
|
|
248
|
+
"",
|
|
249
|
+
"// @geastack/geatsc-plugin-gea mounted renderers",
|
|
250
|
+
...globalAccessorClassForwardDeclarations(globalDeclarations),
|
|
251
|
+
...globalDeclarations,
|
|
252
|
+
"namespace gea_ir {",
|
|
253
|
+
"",
|
|
254
|
+
...hubBindHelper,
|
|
255
|
+
...typedStoreReaders,
|
|
256
|
+
...typedSelfReaders,
|
|
257
|
+
...markedMountedRendererLines(mountedRendererSections),
|
|
258
|
+
"}",
|
|
259
|
+
"",
|
|
260
|
+
].join("\n");
|
|
261
|
+
}
|
|
262
|
+
function markedMountedRendererLines(sections) {
|
|
263
|
+
return sections.flatMap((section) => [
|
|
264
|
+
`// @geastack/geatsc-plugin-gea mounted renderer begin ${JSON.stringify({
|
|
265
|
+
id: section.componentId,
|
|
266
|
+
exportName: section.exportName,
|
|
267
|
+
functionName: section.functionName,
|
|
268
|
+
})}`,
|
|
269
|
+
...section.lines,
|
|
270
|
+
"// @geastack/geatsc-plugin-gea mounted renderer end",
|
|
271
|
+
"",
|
|
272
|
+
]);
|
|
273
|
+
}
|
|
274
|
+
// Forward declarations for the `<img src="…">` byte-array symbols, to be
|
|
275
|
+
// inserted at TRUE global scope (before the program's anonymous namespace). The
|
|
276
|
+
// definitions live `extern "C"` at global scope (generate-gea-embedded-assets);
|
|
277
|
+
// `extern "C"` on a *variable* inside an anonymous namespace does NOT escape the
|
|
278
|
+
// internal linkage GCC gives anon-namespace members, so the reference would
|
|
279
|
+
// mangle to `(anonymous namespace)::gea_asset_<path>` and fail to link. Declared
|
|
280
|
+
// at global scope, the unqualified use inside `gea_ir` resolves out to the flat
|
|
281
|
+
// C symbol the definition provides.
|
|
282
|
+
export function generateCppIrAssetForwardDeclarations(ir) {
|
|
283
|
+
const components = sortedComponents(ir.components);
|
|
284
|
+
const constants = collectIrConstants(ir);
|
|
285
|
+
enrichConstantInitializerShapes(ir.stores, constants, ir.modules.map((module) => module.file));
|
|
286
|
+
const storeArrayFields = collectStoreArraySources(ir.stores);
|
|
287
|
+
const storeFields = collectStoreFields(ir.stores);
|
|
288
|
+
const storeMethods = collectStoreMethods(ir.stores).concat(collectSingletonMethodsFromSourceFiles(ir.modules.map((module) => module.file), ir.stores));
|
|
289
|
+
const mountedRenderers = generateMountedRenderers(components, storeFields, storeArrayFields, constants, storeMethods);
|
|
290
|
+
if (mountedRenderers.length === 0)
|
|
291
|
+
return "";
|
|
292
|
+
const declarations = mountedRendererAssetDeclarations(mountedRenderers);
|
|
293
|
+
if (declarations.length === 0)
|
|
294
|
+
return "";
|
|
295
|
+
return [
|
|
296
|
+
"// @geastack/geatsc-plugin-gea embedded-asset symbols",
|
|
297
|
+
...declarations,
|
|
298
|
+
].join("\n");
|
|
299
|
+
}
|
|
300
|
+
function sortedComponents(components) {
|
|
301
|
+
return [...components].sort((left, right) => componentSortKey(left).localeCompare(componentSortKey(right)));
|
|
302
|
+
}
|
|
303
|
+
function componentSortKey(component) {
|
|
304
|
+
return `${component.id}\0${component.exportName}`;
|
|
305
|
+
}
|
|
306
|
+
// Typed global accessors can name app classes (e.g. a controller singleton:
|
|
307
|
+
// `std::shared_ptr<VoiceNotesController> &__gea_global_voiceNotes();`) that are
|
|
308
|
+
// only DEFINED in a later module header — in module-first output this block
|
|
309
|
+
// lands in the shared gea_ir.hpp, which every module header includes FIRST, so
|
|
310
|
+
// the class name is undeclared at this point. A shared_ptr declaration only
|
|
311
|
+
// needs the class declared, so forward-declare every class a typed accessor
|
|
312
|
+
// names. (`__gea_type_*` record structs are complete-type dependencies handled
|
|
313
|
+
// via the record-alias types.hpp includes, not forward declarations.)
|
|
314
|
+
function globalAccessorClassForwardDeclarations(declarations) {
|
|
315
|
+
const forwardDeclarations = new Set();
|
|
316
|
+
const pattern = /^std::shared_ptr<([A-Za-z_][A-Za-z0-9_]*)>\s*&/;
|
|
317
|
+
for (const declaration of declarations) {
|
|
318
|
+
const match = pattern.exec(declaration);
|
|
319
|
+
if (match && !match[1].startsWith("__gea_type_"))
|
|
320
|
+
forwardDeclarations.add(`class ${match[1]};`);
|
|
321
|
+
}
|
|
322
|
+
return forwardDeclarations.size > 0
|
|
323
|
+
? [...forwardDeclarations].sort().concat("")
|
|
324
|
+
: [];
|
|
325
|
+
}
|
|
326
|
+
function mountedRendererGlobalAccessorDeclarations(lines, globalAccessorTypes = new Map()) {
|
|
327
|
+
// Image globals are defined as native `gea::host::GeaEmbeddedImage&`
|
|
328
|
+
// accessors, so their forward declarations must carry that type — a
|
|
329
|
+
// `gea_cpp_value&` declaration would ambiguate against the definition. The
|
|
330
|
+
// `().id` member read (emitted only by the image-id lowering; boxed values
|
|
331
|
+
// never lower to a member access) marks which globals are images.
|
|
332
|
+
const imageGlobals = new Set();
|
|
333
|
+
const imagePattern = /\b(__gea_global_[A-Za-z_][A-Za-z0-9_]*)\s*\(\s*\)\.id\b/g;
|
|
334
|
+
for (const line of lines) {
|
|
335
|
+
let match;
|
|
336
|
+
while ((match = imagePattern.exec(line)))
|
|
337
|
+
imageGlobals.add(match[1]);
|
|
338
|
+
}
|
|
339
|
+
const declarations = new Set();
|
|
340
|
+
const pattern = /\b(__gea_global_[A-Za-z_][A-Za-z0-9_]*)\s*\(\s*\)/g;
|
|
341
|
+
for (const line of lines) {
|
|
342
|
+
let match;
|
|
343
|
+
while ((match = pattern.exec(line))) {
|
|
344
|
+
if (nonValueRuntimeGlobals.has(match[1]))
|
|
345
|
+
continue;
|
|
346
|
+
if (imageGlobals.has(match[1]))
|
|
347
|
+
declarations.add(`gea::host::GeaEmbeddedImage &${match[1]}();`);
|
|
348
|
+
else if (globalAccessorTypes.has(match[1]))
|
|
349
|
+
declarations.add(`${globalAccessorTypes.get(match[1])} &${match[1]}();`);
|
|
350
|
+
else
|
|
351
|
+
declarations.add(`gea_cpp_value &${match[1]}();`);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
return declarations.size > 0 ? [...declarations].sort().concat("") : [];
|
|
355
|
+
}
|
|
356
|
+
// `<img src="…">` lowers to a `gea::host::image.loadBytesPtr(gea_asset_<path>,
|
|
357
|
+
// gea_asset_<path>_len)` call (see imageSrcAssetLines in cpp-mounted-lowering).
|
|
358
|
+
// Those symbols are defined `extern "C"` at global scope by
|
|
359
|
+
// scripts/generate-gea-embedded-assets.mjs. We forward-declare each referenced
|
|
360
|
+
// one here, at namespace scope — `extern "C"` is illegal at block scope, and a
|
|
361
|
+
// plain C++ `extern` would bind to the enclosing (in an isolated multi-program link,
|
|
362
|
+
// anonymous) namespace and fail to link against the global definition.
|
|
363
|
+
function mountedRendererAssetDeclarations(lines) {
|
|
364
|
+
const symbols = new Set();
|
|
365
|
+
const pattern = /loadBytesPtr\(\s*(gea_asset_[A-Za-z0-9_]+)\s*,/g;
|
|
366
|
+
for (const line of lines) {
|
|
367
|
+
let match;
|
|
368
|
+
while ((match = pattern.exec(line))) {
|
|
369
|
+
symbols.add(match[1]);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
if (symbols.size === 0)
|
|
373
|
+
return [];
|
|
374
|
+
const declarations = [];
|
|
375
|
+
for (const sym of [...symbols].sort()) {
|
|
376
|
+
declarations.push(`extern "C" const unsigned char ${sym}[];`);
|
|
377
|
+
declarations.push(`extern "C" const unsigned long ${sym}_len;`);
|
|
378
|
+
}
|
|
379
|
+
return declarations.concat("");
|
|
380
|
+
}
|
|
381
|
+
const nonValueRuntimeGlobals = new Set([
|
|
382
|
+
"__gea_global__isArr",
|
|
383
|
+
"__gea_global__nestedCache",
|
|
384
|
+
"__gea_global__priv",
|
|
385
|
+
"__gea_global_kebab",
|
|
386
|
+
"__gea_global_reactiveAttr",
|
|
387
|
+
"__gea_global_reactiveClass",
|
|
388
|
+
"__gea_global_reactiveClassName",
|
|
389
|
+
"__gea_global_reactiveStyle",
|
|
390
|
+
"__gea_global_reactiveText",
|
|
391
|
+
"__gea_global_reactiveTextValue",
|
|
392
|
+
"__gea_global_reactiveValueRead",
|
|
393
|
+
]);
|
|
394
|
+
export function collectIrConstants(ir) {
|
|
395
|
+
const constants = new Map();
|
|
396
|
+
const pending = [];
|
|
397
|
+
for (const store of ir.stores) {
|
|
398
|
+
for (const constant of store.constants ?? [])
|
|
399
|
+
constants.set(constant.name, constant);
|
|
400
|
+
}
|
|
401
|
+
const pendingFiles = ir.modules.map((module) => module.file);
|
|
402
|
+
const seenFiles = new Set();
|
|
403
|
+
for (let index = 0; index < pendingFiles.length; index += 1) {
|
|
404
|
+
const file = pendingFiles[index];
|
|
405
|
+
if (seenFiles.has(file) || !existsSync(file) || !statSync(file).isFile())
|
|
406
|
+
continue;
|
|
407
|
+
seenFiles.add(file);
|
|
408
|
+
const source = readFileSync(file, "utf8");
|
|
409
|
+
pending.push(...constDeclarationsInSource(file, source));
|
|
410
|
+
for (const imported of localImportFiles(file, source)) {
|
|
411
|
+
if (!seenFiles.has(imported))
|
|
412
|
+
pendingFiles.push(imported);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
let changed = true;
|
|
416
|
+
while (changed) {
|
|
417
|
+
changed = false;
|
|
418
|
+
for (const item of pending) {
|
|
419
|
+
if (constants.has(item.name))
|
|
420
|
+
continue;
|
|
421
|
+
const constant = evaluateConstantExpression(item.name, item.expr, constants);
|
|
422
|
+
if (!constant)
|
|
423
|
+
continue;
|
|
424
|
+
constants.set(item.name, constant);
|
|
425
|
+
changed = true;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
return constants;
|
|
429
|
+
}
|
|
430
|
+
function constDeclarationsInSource(file, source) {
|
|
431
|
+
const out = [];
|
|
432
|
+
const sourceFile = ts.createSourceFile(file, source, ts.ScriptTarget.Latest, true, file.endsWith(".tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS);
|
|
433
|
+
for (const statement of sourceFile.statements) {
|
|
434
|
+
if (!ts.isVariableStatement(statement))
|
|
435
|
+
continue;
|
|
436
|
+
if ((statement.declarationList.flags & ts.NodeFlags.Const) === 0)
|
|
437
|
+
continue;
|
|
438
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
439
|
+
if (!ts.isIdentifier(declaration.name) || !declaration.initializer)
|
|
440
|
+
continue;
|
|
441
|
+
out.push({
|
|
442
|
+
name: declaration.name.text,
|
|
443
|
+
expr: declaration.initializer.getText(sourceFile),
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
return out;
|
|
448
|
+
}
|
|
449
|
+
function localImportFiles(file, source) {
|
|
450
|
+
const out = [];
|
|
451
|
+
const pattern = /\bimport\b[\s\S]*?\bfrom\s+['"](\.{1,2}\/[^'"]+)['"]/g;
|
|
452
|
+
let match;
|
|
453
|
+
while ((match = pattern.exec(source))) {
|
|
454
|
+
const resolved = resolveLocalSource(file, match[1]);
|
|
455
|
+
if (resolved)
|
|
456
|
+
out.push(resolved);
|
|
457
|
+
}
|
|
458
|
+
return out;
|
|
459
|
+
}
|
|
460
|
+
function resolveLocalSource(fromFile, specifier) {
|
|
461
|
+
const base = resolve(dirname(fromFile), specifier);
|
|
462
|
+
const candidates = [
|
|
463
|
+
base,
|
|
464
|
+
`${base}.ts`,
|
|
465
|
+
`${base}.tsx`,
|
|
466
|
+
`${base}.js`,
|
|
467
|
+
`${base}.jsx`,
|
|
468
|
+
resolve(base, "index.ts"),
|
|
469
|
+
resolve(base, "index.tsx"),
|
|
470
|
+
resolve(base, "index.js"),
|
|
471
|
+
resolve(base, "index.jsx"),
|
|
472
|
+
];
|
|
473
|
+
// A directory import matches the bare `base` candidate — only a file is
|
|
474
|
+
// loadable; directories fall through to the index.* candidates.
|
|
475
|
+
return (candidates.find((candidate) => statSync(candidate, { throwIfNoEntry: false })?.isFile()) ?? null);
|
|
476
|
+
}
|
|
477
|
+
function evaluateConstantExpression(name, expr, constants) {
|
|
478
|
+
const text = expr.trim();
|
|
479
|
+
const string = stringLiteralValue(text);
|
|
480
|
+
if (string !== null)
|
|
481
|
+
return { name, value: string, valueType: "string" };
|
|
482
|
+
if (/^-?(?:\d+|\d+\.\d+|\.\d+)$/.test(text))
|
|
483
|
+
return { name, value: text, valueType: "number" };
|
|
484
|
+
if (text === "true" || text === "false")
|
|
485
|
+
return { name, value: text, valueType: "boolean" };
|
|
486
|
+
if (text === "null")
|
|
487
|
+
return { name, value: "null", valueType: "null" };
|
|
488
|
+
const objectArray = evaluateObjectArrayConstant(name, text, constants);
|
|
489
|
+
if (objectArray)
|
|
490
|
+
return objectArray;
|
|
491
|
+
const substituted = text.replace(/\b[A-Za-z_$][A-Za-z0-9_$]*\b/g, (identifier) => {
|
|
492
|
+
const constant = constants.get(identifier);
|
|
493
|
+
return constant?.valueType === "number"
|
|
494
|
+
? `(${constant.value})`
|
|
495
|
+
: identifier;
|
|
496
|
+
});
|
|
497
|
+
if (!/^[0-9+\-*/().\s]+$/.test(substituted))
|
|
498
|
+
return null;
|
|
499
|
+
try {
|
|
500
|
+
const value = Function(`"use strict"; return (${substituted});`)();
|
|
501
|
+
return typeof value === "number" && Number.isFinite(value)
|
|
502
|
+
? { name, value: String(value), valueType: "number" }
|
|
503
|
+
: null;
|
|
504
|
+
}
|
|
505
|
+
catch {
|
|
506
|
+
return null;
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
function evaluateObjectArrayConstant(name, expr, constants) {
|
|
510
|
+
const source = ts.createSourceFile("constant.ts", `(${expr});`, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
|
|
511
|
+
const statement = source.statements[0];
|
|
512
|
+
if (!statement || !ts.isExpressionStatement(statement))
|
|
513
|
+
return null;
|
|
514
|
+
const expression = stripExpressionWrappers(statement.expression);
|
|
515
|
+
if (!ts.isArrayLiteralExpression(expression))
|
|
516
|
+
return null;
|
|
517
|
+
const rows = [];
|
|
518
|
+
for (const element of expression.elements) {
|
|
519
|
+
const object = stripExpressionWrappers(element);
|
|
520
|
+
if (!ts.isObjectLiteralExpression(object))
|
|
521
|
+
return null;
|
|
522
|
+
const fields = [];
|
|
523
|
+
for (const property of object.properties) {
|
|
524
|
+
if (!ts.isPropertyAssignment(property))
|
|
525
|
+
return null;
|
|
526
|
+
const fieldName = objectPropertyName(property.name);
|
|
527
|
+
if (!fieldName)
|
|
528
|
+
return null;
|
|
529
|
+
const value = primitiveConstantFromExpression(property.initializer, constants);
|
|
530
|
+
if (!value)
|
|
531
|
+
return null;
|
|
532
|
+
fields.push({ name: fieldName, ...value });
|
|
533
|
+
}
|
|
534
|
+
rows.push({ fields });
|
|
535
|
+
}
|
|
536
|
+
if (rows.length === 0)
|
|
537
|
+
return { name, value: "", valueType: "object-array", items: [] };
|
|
538
|
+
const fieldOrder = rows[0].fields.map((field) => field.name);
|
|
539
|
+
for (const row of rows) {
|
|
540
|
+
if (row.fields.length !== fieldOrder.length)
|
|
541
|
+
return null;
|
|
542
|
+
const byName = new Map(row.fields.map((field) => [field.name, field]));
|
|
543
|
+
if (fieldOrder.some((fieldName) => !byName.has(fieldName)))
|
|
544
|
+
return null;
|
|
545
|
+
row.fields = fieldOrder.map((fieldName) => byName.get(fieldName));
|
|
546
|
+
}
|
|
547
|
+
return { name, value: "", valueType: "object-array", items: rows };
|
|
548
|
+
}
|
|
549
|
+
function stripExpressionWrappers(node) {
|
|
550
|
+
let current = node;
|
|
551
|
+
while (ts.isParenthesizedExpression(current) ||
|
|
552
|
+
ts.isAsExpression(current) ||
|
|
553
|
+
ts.isTypeAssertionExpression(current) ||
|
|
554
|
+
ts.isNonNullExpression(current) ||
|
|
555
|
+
ts.isSatisfiesExpression(current)) {
|
|
556
|
+
current = current.expression;
|
|
557
|
+
}
|
|
558
|
+
return current;
|
|
559
|
+
}
|
|
560
|
+
function objectPropertyName(name) {
|
|
561
|
+
if (ts.isIdentifier(name) ||
|
|
562
|
+
ts.isStringLiteral(name) ||
|
|
563
|
+
ts.isNumericLiteral(name))
|
|
564
|
+
return name.text;
|
|
565
|
+
return null;
|
|
566
|
+
}
|
|
567
|
+
function primitiveConstantFromExpression(expression, constants) {
|
|
568
|
+
const node = stripExpressionWrappers(expression);
|
|
569
|
+
if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node))
|
|
570
|
+
return { value: node.text, valueType: "string" };
|
|
571
|
+
if (ts.isNumericLiteral(node))
|
|
572
|
+
return { value: node.text, valueType: "number" };
|
|
573
|
+
if (ts.isPrefixUnaryExpression(node) &&
|
|
574
|
+
node.operator === ts.SyntaxKind.MinusToken &&
|
|
575
|
+
ts.isNumericLiteral(node.operand)) {
|
|
576
|
+
return { value: `-${node.operand.text}`, valueType: "number" };
|
|
577
|
+
}
|
|
578
|
+
if (node.kind === ts.SyntaxKind.TrueKeyword)
|
|
579
|
+
return { value: "true", valueType: "boolean" };
|
|
580
|
+
if (node.kind === ts.SyntaxKind.FalseKeyword)
|
|
581
|
+
return { value: "false", valueType: "boolean" };
|
|
582
|
+
if (node.kind === ts.SyntaxKind.NullKeyword)
|
|
583
|
+
return { value: "null", valueType: "null" };
|
|
584
|
+
if (ts.isIdentifier(node)) {
|
|
585
|
+
const constant = constants.get(node.text);
|
|
586
|
+
if (!constant || constant.valueType === "object-array")
|
|
587
|
+
return null;
|
|
588
|
+
return { value: constant.value, valueType: constant.valueType };
|
|
589
|
+
}
|
|
590
|
+
return null;
|
|
591
|
+
}
|
|
592
|
+
function stringLiteralValue(expr) {
|
|
593
|
+
const quote = expr[0];
|
|
594
|
+
if ((quote !== '"' && quote !== "'") || expr[expr.length - 1] !== quote)
|
|
595
|
+
return null;
|
|
596
|
+
return expr
|
|
597
|
+
.slice(1, -1)
|
|
598
|
+
.replace(/\\n/g, "\n")
|
|
599
|
+
.replace(/\\t/g, "\t")
|
|
600
|
+
.replace(/\\'/g, "'")
|
|
601
|
+
.replace(/\\"/g, '"')
|
|
602
|
+
.replace(/\\\\/g, "\\");
|
|
603
|
+
}
|
|
604
|
+
function canvasContextValueSource() {
|
|
605
|
+
return [
|
|
606
|
+
"namespace {",
|
|
607
|
+
"inline double geaCanvasValueNumber(const gea_cpp_value &value) {",
|
|
608
|
+
" return gea::runtime::coerce::to_number(value);",
|
|
609
|
+
"}",
|
|
610
|
+
"",
|
|
611
|
+
"inline int geaCanvasValueInt(const gea_cpp_value &value) {",
|
|
612
|
+
" const double number = geaCanvasValueNumber(value);",
|
|
613
|
+
" if (!std::isfinite(number)) return 0;",
|
|
614
|
+
" return static_cast<int>(number + (number >= 0.0 ? 0.5 : -0.5));",
|
|
615
|
+
"}",
|
|
616
|
+
"",
|
|
617
|
+
"inline gea::framework::graphics::pixel::native_t geaCanvasValueRgb565(const gea_cpp_value &value) {",
|
|
618
|
+
" return gea::framework::graphics::pixel::nativeFromRrggbbaa(static_cast<std::uint32_t>(geaCanvasValueNumber(value)));",
|
|
619
|
+
"}",
|
|
620
|
+
"",
|
|
621
|
+
"inline std::string geaCanvasValueString(const gea_cpp_value &value) {",
|
|
622
|
+
" return value.is_nullish() ? std::string() : gea_cpp_to_string(value);",
|
|
623
|
+
"}",
|
|
624
|
+
"",
|
|
625
|
+
"inline const gea_cpp_value &geaCanvasArg(const std::vector<gea_cpp_value> &args, std::size_t index) {",
|
|
626
|
+
" static const gea_cpp_value missing = gea_cpp_value::missing();",
|
|
627
|
+
" return index < args.size() ? args[index] : missing;",
|
|
628
|
+
"}",
|
|
629
|
+
"",
|
|
630
|
+
"inline std::vector<std::uint16_t> geaCanvasUint16Vector(const gea_cpp_value &value) {",
|
|
631
|
+
" auto items = static_cast<std::vector<gea_cpp_value>>(value);",
|
|
632
|
+
" std::vector<std::uint16_t> out;",
|
|
633
|
+
" out.reserve(items.size());",
|
|
634
|
+
" for (const auto &item : items) out.push_back(static_cast<std::uint16_t>(geaCanvasValueNumber(item)));",
|
|
635
|
+
" return out;",
|
|
636
|
+
"}",
|
|
637
|
+
"",
|
|
638
|
+
"inline std::vector<std::int32_t> geaCanvasInt32Vector(const gea_cpp_value &value) {",
|
|
639
|
+
" auto items = static_cast<std::vector<gea_cpp_value>>(value);",
|
|
640
|
+
" std::vector<std::int32_t> out;",
|
|
641
|
+
" out.reserve(items.size());",
|
|
642
|
+
" for (const auto &item : items) out.push_back(static_cast<std::int32_t>(geaCanvasValueNumber(item)));",
|
|
643
|
+
" return out;",
|
|
644
|
+
"}",
|
|
645
|
+
"",
|
|
646
|
+
"inline std::vector<std::uint32_t> geaCanvasUint32Vector(const gea_cpp_value &value) {",
|
|
647
|
+
" auto items = static_cast<std::vector<gea_cpp_value>>(value);",
|
|
648
|
+
" std::vector<std::uint32_t> out;",
|
|
649
|
+
" out.reserve(items.size());",
|
|
650
|
+
" for (const auto &item : items) out.push_back(static_cast<std::uint32_t>(geaCanvasValueNumber(item)));",
|
|
651
|
+
" return out;",
|
|
652
|
+
"}",
|
|
653
|
+
"",
|
|
654
|
+
"inline std::vector<gea::framework::graphics::pixel::NativeColor> geaCanvasNativeColorVector(const gea_cpp_value &value) {",
|
|
655
|
+
" auto items = static_cast<std::vector<gea_cpp_value>>(value);",
|
|
656
|
+
" std::vector<gea::framework::graphics::pixel::NativeColor> out;",
|
|
657
|
+
" out.reserve(items.size());",
|
|
658
|
+
" for (const auto &item : items) out.emplace_back(static_cast<gea::framework::graphics::pixel::native_t>(geaCanvasValueNumber(item)));",
|
|
659
|
+
" return out;",
|
|
660
|
+
"}",
|
|
661
|
+
"",
|
|
662
|
+
"inline int geaCanvasImageId(const gea_cpp_value &value) {",
|
|
663
|
+
" if (value.kind == gea_cpp_value::kind_t::number) return geaCanvasValueInt(value);",
|
|
664
|
+
' gea_cpp_value id = value.record_get_literal("id");',
|
|
665
|
+
" if (!id.is_nullish()) return geaCanvasValueInt(id);",
|
|
666
|
+
" return -1;",
|
|
667
|
+
"}",
|
|
668
|
+
"",
|
|
669
|
+
"inline void geaCanvasSetFillStyle(gea::embedded::ui::CanvasRenderingContext2D &ctx, const gea_cpp_value &value) {",
|
|
670
|
+
" if (value.kind == gea_cpp_value::kind_t::number) ctx.setFillStyleRgb565(geaCanvasValueRgb565(value));",
|
|
671
|
+
" else ctx.setFillStyle(geaCanvasValueString(value));",
|
|
672
|
+
"}",
|
|
673
|
+
"",
|
|
674
|
+
"inline void geaCanvasSetStrokeStyle(gea::embedded::ui::CanvasRenderingContext2D &ctx, const gea_cpp_value &value) {",
|
|
675
|
+
" if (value.kind == gea_cpp_value::kind_t::number) ctx.setStrokeStyleRgb565(geaCanvasValueRgb565(value));",
|
|
676
|
+
" else ctx.setStrokeStyle(geaCanvasValueString(value));",
|
|
677
|
+
"}",
|
|
678
|
+
"",
|
|
679
|
+
"inline gea_cpp_value geaCanvasContextValue(gea::embedded::ui::CanvasRenderingContext2D ctx) {",
|
|
680
|
+
" auto state = std::make_shared<gea::embedded::ui::CanvasRenderingContext2D>(ctx);",
|
|
681
|
+
" return gea_cpp_value::object(",
|
|
682
|
+
" reinterpret_cast<std::uintptr_t>(state.get()),",
|
|
683
|
+
" [state, __methodCache = std::make_shared<std::vector<std::pair<std::string, gea_cpp_value>>>()](const std::string &key) -> gea_cpp_value {",
|
|
684
|
+
" // Memoize the bound method callables. The original accessor rebuilt a fresh",
|
|
685
|
+
" // gea_cpp_callable_args closure (a heap alloc, PSRAM-routed on esp32) on EVERY",
|
|
686
|
+
" // ctx.method() access — ~40-50 calls/frame for a canvas app turned that into",
|
|
687
|
+
" // milliseconds. Build each callable once and return the cached value thereafter.",
|
|
688
|
+
" for (auto &__e : *__methodCache) if (__e.first == key) return __e.second;",
|
|
689
|
+
" gea_cpp_value __cachedMethod = [&]() -> gea_cpp_value {",
|
|
690
|
+
' if (key == "clear") return gea_cpp_callable_args([state](const std::vector<gea_cpp_value> &) -> gea_cpp_value {',
|
|
691
|
+
" state->clear();",
|
|
692
|
+
" return gea_cpp_value::missing();",
|
|
693
|
+
" });",
|
|
694
|
+
' if (key == "clearRect") return gea_cpp_callable_args([state](const std::vector<gea_cpp_value> &args) -> gea_cpp_value {',
|
|
695
|
+
" state->clearRect(geaCanvasValueInt(geaCanvasArg(args, 0)), geaCanvasValueInt(geaCanvasArg(args, 1)), geaCanvasValueInt(geaCanvasArg(args, 2)), geaCanvasValueInt(geaCanvasArg(args, 3)));",
|
|
696
|
+
" return gea_cpp_value::missing();",
|
|
697
|
+
" });",
|
|
698
|
+
' if (key == "fillRect") return gea_cpp_callable_args([state](const std::vector<gea_cpp_value> &args) -> gea_cpp_value {',
|
|
699
|
+
" state->fillRect(geaCanvasValueInt(geaCanvasArg(args, 0)), geaCanvasValueInt(geaCanvasArg(args, 1)), geaCanvasValueInt(geaCanvasArg(args, 2)), geaCanvasValueInt(geaCanvasArg(args, 3)));",
|
|
700
|
+
" return gea_cpp_value::missing();",
|
|
701
|
+
" });",
|
|
702
|
+
' if (key == "strokeRect") return gea_cpp_callable_args([state](const std::vector<gea_cpp_value> &args) -> gea_cpp_value {',
|
|
703
|
+
" state->strokeRect(geaCanvasValueInt(geaCanvasArg(args, 0)), geaCanvasValueInt(geaCanvasArg(args, 1)), geaCanvasValueInt(geaCanvasArg(args, 2)), geaCanvasValueInt(geaCanvasArg(args, 3)));",
|
|
704
|
+
" return gea_cpp_value::missing();",
|
|
705
|
+
" });",
|
|
706
|
+
' if (key == "fillCircle") return gea_cpp_callable_args([state](const std::vector<gea_cpp_value> &args) -> gea_cpp_value {',
|
|
707
|
+
" if (args.size() > 3 && !args[3].is_nullish()) geaCanvasSetFillStyle(*state, args[3]);",
|
|
708
|
+
" state->fillCircle(geaCanvasValueInt(geaCanvasArg(args, 0)), geaCanvasValueInt(geaCanvasArg(args, 1)), geaCanvasValueInt(geaCanvasArg(args, 2)));",
|
|
709
|
+
" return gea_cpp_value::missing();",
|
|
710
|
+
" });",
|
|
711
|
+
' if (key == "strokeCircle") return gea_cpp_callable_args([state](const std::vector<gea_cpp_value> &args) -> gea_cpp_value {',
|
|
712
|
+
" state->strokeCircle(geaCanvasValueInt(geaCanvasArg(args, 0)), geaCanvasValueInt(geaCanvasArg(args, 1)), geaCanvasValueInt(geaCanvasArg(args, 2)));",
|
|
713
|
+
" return gea_cpp_value::missing();",
|
|
714
|
+
" });",
|
|
715
|
+
' if (key == "fillCircleRgb565") return gea_cpp_callable_args([state](const std::vector<gea_cpp_value> &args) -> gea_cpp_value {',
|
|
716
|
+
" state->fillCircleRgb565(geaCanvasValueInt(geaCanvasArg(args, 0)), geaCanvasValueInt(geaCanvasArg(args, 1)), geaCanvasValueInt(geaCanvasArg(args, 2)), geaCanvasValueRgb565(geaCanvasArg(args, 3)));",
|
|
717
|
+
" return gea_cpp_value::missing();",
|
|
718
|
+
" });",
|
|
719
|
+
' if (key == "fillTriangleRgb565") return gea_cpp_callable_args([state](const std::vector<gea_cpp_value> &args) -> gea_cpp_value {',
|
|
720
|
+
" state->fillTriangleRgb565(geaCanvasValueInt(geaCanvasArg(args, 0)), geaCanvasValueInt(geaCanvasArg(args, 1)), geaCanvasValueInt(geaCanvasArg(args, 2)), geaCanvasValueInt(geaCanvasArg(args, 3)), geaCanvasValueInt(geaCanvasArg(args, 4)), geaCanvasValueInt(geaCanvasArg(args, 5)), geaCanvasValueRgb565(geaCanvasArg(args, 6)));",
|
|
721
|
+
" return gea_cpp_value::missing();",
|
|
722
|
+
" });",
|
|
723
|
+
' if (key == "fillCirclesRgb565") return gea_cpp_callable_args([state](const std::vector<gea_cpp_value> &args) -> gea_cpp_value {',
|
|
724
|
+
" auto xs = geaCanvasUint16Vector(geaCanvasArg(args, 0));",
|
|
725
|
+
" auto ys = geaCanvasUint16Vector(geaCanvasArg(args, 1));",
|
|
726
|
+
" auto colors = geaCanvasNativeColorVector(geaCanvasArg(args, 3));",
|
|
727
|
+
" const int count = args.size() > 4 && !args[4].is_nullish() ? geaCanvasValueInt(args[4]) : -1;",
|
|
728
|
+
" state->fillCirclesRgb565(xs, ys, geaCanvasValueInt(geaCanvasArg(args, 2)), colors, count);",
|
|
729
|
+
" return gea_cpp_value::missing();",
|
|
730
|
+
" });",
|
|
731
|
+
' if (key == "fillCirclesRgb565Uniform") return gea_cpp_callable_args([state](const std::vector<gea_cpp_value> &args) -> gea_cpp_value {',
|
|
732
|
+
" auto xs = geaCanvasUint16Vector(geaCanvasArg(args, 0));",
|
|
733
|
+
" auto ys = geaCanvasUint16Vector(geaCanvasArg(args, 1));",
|
|
734
|
+
" state->fillCirclesRgb565(xs, ys, geaCanvasValueInt(geaCanvasArg(args, 2)), static_cast<gea::framework::graphics::pixel::native_t>(geaCanvasValueNumber(geaCanvasArg(args, 3))));",
|
|
735
|
+
" return gea_cpp_value::missing();",
|
|
736
|
+
" });",
|
|
737
|
+
' if (key == "fillTrianglesRgb565Sorted") return gea_cpp_callable_args([state](const std::vector<gea_cpp_value> &args) -> gea_cpp_value {',
|
|
738
|
+
" auto x0s = geaCanvasInt32Vector(geaCanvasArg(args, 0));",
|
|
739
|
+
" auto y0s = geaCanvasInt32Vector(geaCanvasArg(args, 1));",
|
|
740
|
+
" auto x1s = geaCanvasInt32Vector(geaCanvasArg(args, 2));",
|
|
741
|
+
" auto y1s = geaCanvasInt32Vector(geaCanvasArg(args, 3));",
|
|
742
|
+
" auto x2s = geaCanvasInt32Vector(geaCanvasArg(args, 4));",
|
|
743
|
+
" auto y2s = geaCanvasInt32Vector(geaCanvasArg(args, 5));",
|
|
744
|
+
" auto colors = geaCanvasUint32Vector(geaCanvasArg(args, 6));",
|
|
745
|
+
" auto order = geaCanvasInt32Vector(geaCanvasArg(args, 7));",
|
|
746
|
+
" const int count = args.size() > 8 && !args[8].is_nullish() ? geaCanvasValueInt(args[8]) : -1;",
|
|
747
|
+
" state->fillTrianglesRgb565Sorted(x0s, y0s, x1s, y1s, x2s, y2s, colors, order, count);",
|
|
748
|
+
" return gea_cpp_value::missing();",
|
|
749
|
+
" });",
|
|
750
|
+
' if (key == "beginPath") return gea_cpp_callable_args([state](const std::vector<gea_cpp_value> &) -> gea_cpp_value {',
|
|
751
|
+
" state->beginPath();",
|
|
752
|
+
" return gea_cpp_value::missing();",
|
|
753
|
+
" });",
|
|
754
|
+
' if (key == "arc") return gea_cpp_callable_args([state](const std::vector<gea_cpp_value> &args) -> gea_cpp_value {',
|
|
755
|
+
" state->arc(geaCanvasValueNumber(geaCanvasArg(args, 0)), geaCanvasValueNumber(geaCanvasArg(args, 1)), geaCanvasValueNumber(geaCanvasArg(args, 2)), geaCanvasValueNumber(geaCanvasArg(args, 3)), geaCanvasValueNumber(geaCanvasArg(args, 4)));",
|
|
756
|
+
" return gea_cpp_value::missing();",
|
|
757
|
+
" });",
|
|
758
|
+
' if (key == "moveTo") return gea_cpp_callable_args([state](const std::vector<gea_cpp_value> &args) -> gea_cpp_value {',
|
|
759
|
+
" state->moveTo(geaCanvasValueNumber(geaCanvasArg(args, 0)), geaCanvasValueNumber(geaCanvasArg(args, 1)));",
|
|
760
|
+
" return gea_cpp_value::missing();",
|
|
761
|
+
" });",
|
|
762
|
+
' if (key == "lineTo") return gea_cpp_callable_args([state](const std::vector<gea_cpp_value> &args) -> gea_cpp_value {',
|
|
763
|
+
" state->lineTo(geaCanvasValueNumber(geaCanvasArg(args, 0)), geaCanvasValueNumber(geaCanvasArg(args, 1)));",
|
|
764
|
+
" return gea_cpp_value::missing();",
|
|
765
|
+
" });",
|
|
766
|
+
' if (key == "closePath") return gea_cpp_callable_args([state](const std::vector<gea_cpp_value> &) -> gea_cpp_value {',
|
|
767
|
+
" state->closePath();",
|
|
768
|
+
" return gea_cpp_value::missing();",
|
|
769
|
+
" });",
|
|
770
|
+
' if (key == "fill") return gea_cpp_callable_args([state](const std::vector<gea_cpp_value> &) -> gea_cpp_value {',
|
|
771
|
+
" state->fill();",
|
|
772
|
+
" return gea_cpp_value::missing();",
|
|
773
|
+
" });",
|
|
774
|
+
' if (key == "stroke") return gea_cpp_callable_args([state](const std::vector<gea_cpp_value> &) -> gea_cpp_value {',
|
|
775
|
+
" state->stroke();",
|
|
776
|
+
" return gea_cpp_value::missing();",
|
|
777
|
+
" });",
|
|
778
|
+
' if (key == "fillText") return gea_cpp_callable_args([state](const std::vector<gea_cpp_value> &args) -> gea_cpp_value {',
|
|
779
|
+
" state->fillText(geaCanvasValueString(geaCanvasArg(args, 0)), geaCanvasValueInt(geaCanvasArg(args, 1)), geaCanvasValueInt(geaCanvasArg(args, 2)));",
|
|
780
|
+
" return gea_cpp_value::missing();",
|
|
781
|
+
" });",
|
|
782
|
+
' if (key == "drawImage") return gea_cpp_callable_args([state](const std::vector<gea_cpp_value> &args) -> gea_cpp_value {',
|
|
783
|
+
" const int imageId = geaCanvasImageId(geaCanvasArg(args, 0));",
|
|
784
|
+
" if (args.size() >= 5) state->drawImage(imageId, geaCanvasValueInt(geaCanvasArg(args, 1)), geaCanvasValueInt(geaCanvasArg(args, 2)), geaCanvasValueInt(geaCanvasArg(args, 3)), geaCanvasValueInt(geaCanvasArg(args, 4)));",
|
|
785
|
+
" else state->drawImage(imageId, geaCanvasValueInt(geaCanvasArg(args, 1)), geaCanvasValueInt(geaCanvasArg(args, 2)));",
|
|
786
|
+
" return gea_cpp_value::missing();",
|
|
787
|
+
" });",
|
|
788
|
+
' if (key == "drawImageRotated90CW") return gea_cpp_callable_args([state](const std::vector<gea_cpp_value> &args) -> gea_cpp_value {',
|
|
789
|
+
" const int imageId = geaCanvasImageId(geaCanvasArg(args, 0));",
|
|
790
|
+
" state->drawImageRotated90CW(imageId, geaCanvasValueInt(geaCanvasArg(args, 1)), geaCanvasValueInt(geaCanvasArg(args, 2)), geaCanvasValueInt(geaCanvasArg(args, 3)), geaCanvasValueInt(geaCanvasArg(args, 4)));",
|
|
791
|
+
" return gea_cpp_value::missing();",
|
|
792
|
+
" });",
|
|
793
|
+
' if (key == "drawImageTiledX") return gea_cpp_callable_args([state](const std::vector<gea_cpp_value> &args) -> gea_cpp_value {',
|
|
794
|
+
" const int imageId = geaCanvasImageId(geaCanvasArg(args, 0));",
|
|
795
|
+
" state->drawImageTiledX(imageId, geaCanvasValueInt(geaCanvasArg(args, 1)), geaCanvasValueInt(geaCanvasArg(args, 2)), geaCanvasValueInt(geaCanvasArg(args, 3)));",
|
|
796
|
+
" return gea_cpp_value::missing();",
|
|
797
|
+
" });",
|
|
798
|
+
' if (key == "flush") return gea_cpp_callable_args([state](const std::vector<gea_cpp_value> &) -> gea_cpp_value {',
|
|
799
|
+
" state->flush();",
|
|
800
|
+
" return gea_cpp_value::missing();",
|
|
801
|
+
" });",
|
|
802
|
+
' if (key == "beginBatch") return gea_cpp_callable_args([state](const std::vector<gea_cpp_value> &) -> gea_cpp_value {',
|
|
803
|
+
" state->beginBatch();",
|
|
804
|
+
" return gea_cpp_value::missing();",
|
|
805
|
+
" });",
|
|
806
|
+
' if (key == "endBatch") return gea_cpp_callable_args([state](const std::vector<gea_cpp_value> &) -> gea_cpp_value {',
|
|
807
|
+
" state->endBatch();",
|
|
808
|
+
" return gea_cpp_value::missing();",
|
|
809
|
+
" });",
|
|
810
|
+
" return gea_cpp_value::missing();",
|
|
811
|
+
" }();",
|
|
812
|
+
" if (!__cachedMethod.is_nullish()) __methodCache->emplace_back(key, __cachedMethod);",
|
|
813
|
+
" return __cachedMethod;",
|
|
814
|
+
" },",
|
|
815
|
+
" [state](const std::string &key, gea_cpp_value value) {",
|
|
816
|
+
' if (key == "fillStyle") geaCanvasSetFillStyle(*state, value);',
|
|
817
|
+
' else if (key == "strokeStyle") geaCanvasSetStrokeStyle(*state, value);',
|
|
818
|
+
' else if (key == "globalAlpha") state->setGlobalAlpha(geaCanvasValueNumber(value));',
|
|
819
|
+
' else if (key == "lineWidth") state->setLineWidth(geaCanvasValueNumber(value));',
|
|
820
|
+
' else if (key == "font") state->setFont(geaCanvasValueString(value));',
|
|
821
|
+
' else if (key == "textBaseline") state->setTextBaseline(geaCanvasValueString(value));',
|
|
822
|
+
' else if (key == "textAlign") state->setTextAlign(geaCanvasValueString(value));',
|
|
823
|
+
" },",
|
|
824
|
+
" [](const std::string &key) -> bool {",
|
|
825
|
+
' return key == "clear" || key == "clearRect" || key == "fillRect" || key == "strokeRect" || key == "fillCircle" || key == "strokeCircle" ||',
|
|
826
|
+
' key == "fillCircleRgb565" || key == "fillTriangleRgb565" || key == "fillCirclesRgb565" || key == "fillCirclesRgb565Uniform" ||',
|
|
827
|
+
' key == "fillTrianglesRgb565Sorted" || key == "beginPath" || key == "arc" || key == "moveTo" ||',
|
|
828
|
+
' key == "lineTo" || key == "closePath" || key == "fill" || key == "stroke" || key == "fillText" || key == "drawImage" || key == "drawImageRotated90CW" || key == "drawImageTiledX" ||',
|
|
829
|
+
' key == "flush" || key == "beginBatch" || key == "endBatch" || key == "fillStyle" || key == "strokeStyle" ||',
|
|
830
|
+
' key == "globalAlpha" || key == "lineWidth" || key == "font" || key == "textBaseline" || key == "textAlign";',
|
|
831
|
+
" });",
|
|
832
|
+
"}",
|
|
833
|
+
"} // namespace",
|
|
834
|
+
"",
|
|
835
|
+
"static gea_cpp_value gea_cpp_key(const gea::embedded::ui::CanvasRenderingContext2D &ctx) {",
|
|
836
|
+
" return geaCanvasContextValue(ctx);",
|
|
837
|
+
"}",
|
|
838
|
+
];
|
|
839
|
+
}
|
|
840
|
+
// The boxed reactive-apply binder. Used by the boxed mount path (e.g. a component
|
|
841
|
+
// whose template reads MULTIPLE global stores, which can't take the single typed
|
|
842
|
+
// carrier). Its companions (gea_cpp_{bind_direct,run,schedule}_reactive_apply) live
|
|
843
|
+
// in the always-linked cpp runtime, so this def is valid in BOTH store-codegen
|
|
844
|
+
// modes — emit it in each so a boxed bindReactiveApply call always has a definition.
|
|
845
|
+
// The keyed-list entry record helper. Keyed children (`map` with a `key`) emit
|
|
846
|
+
// `gea_ir::keyedEntryRecord(...)` in BOTH store-codegen modes (typed/hub and the
|
|
847
|
+
// boxed default), so — like bindReactiveApplyDef — its definition must be present
|
|
848
|
+
// in each branch or a hub-mode keyed list calls an undeclared helper.
|
|
849
|
+
function keyedEntryRecordDef() {
|
|
850
|
+
return [
|
|
851
|
+
"inline gea_cpp_value keyedEntryRecord(",
|
|
852
|
+
" gea_cpp_value key,",
|
|
853
|
+
" gea_cpp_value item,",
|
|
854
|
+
" gea_cpp_value element,",
|
|
855
|
+
" gea_cpp_value disposer,",
|
|
856
|
+
" gea_cpp_value obs) {",
|
|
857
|
+
" gea_cpp_value out = gea_cpp_value::empty_record();",
|
|
858
|
+
' out.record_set_literal("key", std::move(key));',
|
|
859
|
+
' out.record_set_literal("item", std::move(item));',
|
|
860
|
+
' out.record_set_literal("element", std::move(element));',
|
|
861
|
+
' out.record_set_literal("disposer", std::move(disposer));',
|
|
862
|
+
' out.record_set_literal("obs", std::move(obs));',
|
|
863
|
+
" return out;",
|
|
864
|
+
"}",
|
|
865
|
+
"",
|
|
866
|
+
];
|
|
867
|
+
}
|
|
868
|
+
function bindReactiveApplyDef() {
|
|
869
|
+
return [
|
|
870
|
+
"template <typename Apply>",
|
|
871
|
+
"inline void bindReactiveApply(",
|
|
872
|
+
" const gea_cpp_value &store,",
|
|
873
|
+
" const gea_cpp_value &disposer,",
|
|
874
|
+
" std::initializer_list<const char *> deps,",
|
|
875
|
+
" Apply apply) {",
|
|
876
|
+
" auto apply_fn = std::make_shared<std::function<void()>>(std::move(apply));",
|
|
877
|
+
" auto pending = std::make_shared<bool>(false);",
|
|
878
|
+
" gea_cpp_run_reactive_apply(apply_fn);",
|
|
879
|
+
' auto add = gea_cpp_key(disposer.record_get_literal("add"));',
|
|
880
|
+
' auto observe = gea_cpp_key(store.record_get_literal("observe"));',
|
|
881
|
+
" if (!gea::runtime::coerce::to_boolean(observe)) return;",
|
|
882
|
+
" for (const char *dep : deps) {",
|
|
883
|
+
" auto off = gea_cpp_key(observe(std::string(dep), [apply_fn, pending]() mutable -> void {",
|
|
884
|
+
" gea_cpp_schedule_reactive_apply(apply_fn, pending);",
|
|
885
|
+
" }));",
|
|
886
|
+
" if (gea::runtime::coerce::to_boolean(add)) (void)add(off);",
|
|
887
|
+
" }",
|
|
888
|
+
" // Mirror of the hub-mode binder: an apply already queued on the microtask",
|
|
889
|
+
" // queue outlives the unsubscribe, so null the shared fn on dispose too.",
|
|
890
|
+
" if (gea::runtime::coerce::to_boolean(add)) (void)add(gea_cpp_value([apply_fn]() mutable -> gea_cpp_value {",
|
|
891
|
+
" *apply_fn = nullptr;",
|
|
892
|
+
" return gea_cpp_value::missing();",
|
|
893
|
+
" }));",
|
|
894
|
+
"}",
|
|
895
|
+
"",
|
|
896
|
+
];
|
|
897
|
+
}
|
|
898
|
+
function storeRuntimeSource() {
|
|
899
|
+
if (!storeDynamicFallbackEnabled()) {
|
|
900
|
+
return [
|
|
901
|
+
"template <typename Field, typename Value>",
|
|
902
|
+
"inline void storeAssignField(Field &field, const Value &value) {",
|
|
903
|
+
" if constexpr (std::is_same_v<std::decay_t<Field>, std::string>) {",
|
|
904
|
+
" if constexpr (std::is_same_v<std::decay_t<Value>, std::string>) field = value;",
|
|
905
|
+
" else if constexpr (std::is_convertible_v<Value, std::string>) field = std::string(value);",
|
|
906
|
+
" else field = std::to_string(value);",
|
|
907
|
+
" } else {",
|
|
908
|
+
" field = static_cast<Field>(value);",
|
|
909
|
+
" }",
|
|
910
|
+
"}",
|
|
911
|
+
"",
|
|
912
|
+
"inline void storeDirectBatchBegin() {}",
|
|
913
|
+
"inline void storeDirectBatchEnd() {}",
|
|
914
|
+
"",
|
|
915
|
+
"struct StoreMethodBatch {",
|
|
916
|
+
" StoreMethodBatch() = default;",
|
|
917
|
+
" ~StoreMethodBatch() = default;",
|
|
918
|
+
" StoreMethodBatch(const StoreMethodBatch &) = delete;",
|
|
919
|
+
" StoreMethodBatch &operator=(const StoreMethodBatch &) = delete;",
|
|
920
|
+
"};",
|
|
921
|
+
"",
|
|
922
|
+
"template <typename Owner, typename Field, typename Value>",
|
|
923
|
+
"inline Value storeTypedFieldSet(",
|
|
924
|
+
" Owner &owner,",
|
|
925
|
+
" const char *key,",
|
|
926
|
+
" Field &field,",
|
|
927
|
+
" Value value) {",
|
|
928
|
+
" storeAssignField(field, value);",
|
|
929
|
+
" if constexpr (requires { owner.__gea_hub.notify(key); }) { owner.__gea_hub.notify(key); }",
|
|
930
|
+
" return value;",
|
|
931
|
+
"}",
|
|
932
|
+
"",
|
|
933
|
+
"template <typename Owner, typename Field, typename Value>",
|
|
934
|
+
"inline Value storeTypedFieldSet(",
|
|
935
|
+
" const std::shared_ptr<Owner> &owner,",
|
|
936
|
+
" const char *key,",
|
|
937
|
+
" Field &field,",
|
|
938
|
+
" Value value) {",
|
|
939
|
+
" storeAssignField(field, value);",
|
|
940
|
+
" if (owner) owner->__gea_hub.notify(key);",
|
|
941
|
+
" return value;",
|
|
942
|
+
"}",
|
|
943
|
+
"",
|
|
944
|
+
// Legacy reactive components (CompiledReactiveComponent) still reference
|
|
945
|
+
// the non-hub read-tracker even when hub mode is active for the app's
|
|
946
|
+
// typed stores. The helper is benign — provide it so those (dead-in-hub)
|
|
947
|
+
// classes still compile. Only one of the hub / non-hub blocks is ever
|
|
948
|
+
// emitted, so this is not a redefinition of the non-hub copy.
|
|
949
|
+
"template <typename TrackRead>",
|
|
950
|
+
"inline void compiledStoreTrackReadIfActive(bool active, const gea_cpp_value &store_root, const char *property_name, TrackRead track_read) {",
|
|
951
|
+
" if (active) track_read(store_root, property_name);",
|
|
952
|
+
"}",
|
|
953
|
+
"",
|
|
954
|
+
"inline bool compiledStorePlainValue(const gea_cpp_value &value) {",
|
|
955
|
+
" const gea_cpp_value *candidate = &value;",
|
|
956
|
+
" // Protocol-owned proxy targets are immutable snapshots, so this chain cannot cycle.",
|
|
957
|
+
" while (candidate->kind == gea_cpp_value::kind_t::proxy && candidate->rare_r().proxy_target) {",
|
|
958
|
+
" candidate = candidate->rare_r().proxy_target.get();",
|
|
959
|
+
" }",
|
|
960
|
+
" if (candidate->kind == gea_cpp_value::kind_t::array_value) return candidate->rare_r().typed_array_name.empty();",
|
|
961
|
+
" if (candidate->kind != gea_cpp_value::kind_t::record) return false;",
|
|
962
|
+
" if (candidate->rare_r().prototype_names && !candidate->rare_r().prototype_names->empty()) return false;",
|
|
963
|
+
" const auto &prototype = candidate->rare_r().prototype_object;",
|
|
964
|
+
" return !prototype || prototype->kind == gea_cpp_value::kind_t::null_value ||",
|
|
965
|
+
" gea_cpp_strict_equals(*prototype, gea_cpp_object_prototype());",
|
|
966
|
+
"}",
|
|
967
|
+
"",
|
|
968
|
+
"template <typename Owner, typename StoreRoot, typename PrivMap, typename Field, typename Value, typename QueueFn>",
|
|
969
|
+
"inline Value storeFieldSet(",
|
|
970
|
+
" const Owner &owner,",
|
|
971
|
+
" StoreRoot &,",
|
|
972
|
+
" PrivMap &,",
|
|
973
|
+
" const char *key,",
|
|
974
|
+
" Field &field,",
|
|
975
|
+
" Value value,",
|
|
976
|
+
" QueueFn) {",
|
|
977
|
+
" storeAssignField(field, value);",
|
|
978
|
+
" if constexpr (requires { owner.__gea_hub.notify(key); }) { owner.__gea_hub.notify(key); }",
|
|
979
|
+
" return value;",
|
|
980
|
+
"}",
|
|
981
|
+
"",
|
|
982
|
+
"template <typename Owner, typename StoreRoot, typename PrivMap, typename Array, typename Value, typename QueueFn>",
|
|
983
|
+
"inline Value storeVectorItemPropertySet(",
|
|
984
|
+
" const Owner &owner,",
|
|
985
|
+
" StoreRoot &,",
|
|
986
|
+
" PrivMap &,",
|
|
987
|
+
" const char *field_key,",
|
|
988
|
+
" Array &array,",
|
|
989
|
+
" std::size_t index,",
|
|
990
|
+
" const char *property_key,",
|
|
991
|
+
" Value value,",
|
|
992
|
+
" QueueFn) {",
|
|
993
|
+
" if (index >= array.size()) array.resize(index + 1);",
|
|
994
|
+
" auto &slot = array[index];",
|
|
995
|
+
" set_typed_field(slot, property_key, value);",
|
|
996
|
+
" if constexpr (requires { owner.__gea_hub.notify(field_key); }) { owner.__gea_hub.notify(field_key); }",
|
|
997
|
+
" return value;",
|
|
998
|
+
"}",
|
|
999
|
+
"",
|
|
1000
|
+
// Boxed multi-store mount path can emit bindReactiveApply() even in this
|
|
1001
|
+
// (typed/hub) mode, so its definition must be present here too.
|
|
1002
|
+
...bindReactiveApplyDef(),
|
|
1003
|
+
...keyedEntryRecordDef(),
|
|
1004
|
+
...changeRecordHelperSource(),
|
|
1005
|
+
];
|
|
1006
|
+
}
|
|
1007
|
+
return [
|
|
1008
|
+
...bindReactiveApplyDef(),
|
|
1009
|
+
"template <typename TrackRead>",
|
|
1010
|
+
"inline void compiledStoreTrackReadIfActive(bool active, const gea_cpp_value &store_root, const char *property_name, TrackRead track_read) {",
|
|
1011
|
+
" if (active) track_read(store_root, property_name);",
|
|
1012
|
+
"}",
|
|
1013
|
+
"",
|
|
1014
|
+
"inline bool compiledStorePlainValue(const gea_cpp_value &value) {",
|
|
1015
|
+
" const gea_cpp_value *candidate = &value;",
|
|
1016
|
+
" // Protocol-owned proxy targets are immutable snapshots, so this chain cannot cycle.",
|
|
1017
|
+
" while (candidate->kind == gea_cpp_value::kind_t::proxy && candidate->rare_r().proxy_target) {",
|
|
1018
|
+
" candidate = candidate->rare_r().proxy_target.get();",
|
|
1019
|
+
" }",
|
|
1020
|
+
" if (candidate->kind == gea_cpp_value::kind_t::array_value) return candidate->rare_r().typed_array_name.empty();",
|
|
1021
|
+
" if (candidate->kind != gea_cpp_value::kind_t::record) return false;",
|
|
1022
|
+
" if (candidate->rare_r().prototype_names && !candidate->rare_r().prototype_names->empty()) return false;",
|
|
1023
|
+
" const auto &prototype = candidate->rare_r().prototype_object;",
|
|
1024
|
+
" return !prototype || prototype->kind == gea_cpp_value::kind_t::null_value ||",
|
|
1025
|
+
" gea_cpp_strict_equals(*prototype, gea_cpp_object_prototype());",
|
|
1026
|
+
"}",
|
|
1027
|
+
"",
|
|
1028
|
+
"inline const gea_cpp_value &dirtySymbol() {",
|
|
1029
|
+
' static const gea_cpp_value value = gea_cpp_value::symbol(std::string("gea.d.dirty"));',
|
|
1030
|
+
" return value;",
|
|
1031
|
+
"}",
|
|
1032
|
+
"",
|
|
1033
|
+
...changeRecordHelperSource(),
|
|
1034
|
+
...keyedEntryRecordDef(),
|
|
1035
|
+
"inline bool storeValueNonEmpty(const gea_cpp_value &value) {",
|
|
1036
|
+
" if (!gea::runtime::coerce::to_boolean(value)) return false;",
|
|
1037
|
+
" if (value.kind == gea_cpp_value::kind_t::set_value) return value.set_storage && value.set_storage->size() > 0;",
|
|
1038
|
+
" if (value.kind == gea_cpp_value::kind_t::map_value) return value.map_storage && value.map_storage->size() > 0;",
|
|
1039
|
+
" return value.size() > 0;",
|
|
1040
|
+
"}",
|
|
1041
|
+
"",
|
|
1042
|
+
"inline bool storeHasQueuedConsumers(const gea_cpp_value &state, const char *key) {",
|
|
1043
|
+
' auto observers = gea_cpp_key(state.record_get_literal("observers"));',
|
|
1044
|
+
" if (gea::runtime::coerce::to_boolean(observers)) {",
|
|
1045
|
+
" auto bucket = gea_cpp_key(gea_cpp_map_like_get_literal(observers, key));",
|
|
1046
|
+
" if (storeValueNonEmpty(bucket)) return true;",
|
|
1047
|
+
" }",
|
|
1048
|
+
' if (storeValueNonEmpty(gea_cpp_key(state.record_get_literal("rootObservers")))) return true;',
|
|
1049
|
+
' if (storeValueNonEmpty(gea_cpp_key(state.record_get_literal("derived")))) return true;',
|
|
1050
|
+
" return false;",
|
|
1051
|
+
"}",
|
|
1052
|
+
"",
|
|
1053
|
+
"template <typename Field, typename Value>",
|
|
1054
|
+
"inline void storeAssignField(Field &field, const Value &value) {",
|
|
1055
|
+
" if constexpr (std::is_same_v<std::decay_t<Field>, std::string>) {",
|
|
1056
|
+
" if constexpr (std::is_same_v<std::decay_t<Value>, std::string>) {",
|
|
1057
|
+
" field = value;",
|
|
1058
|
+
" } else if constexpr (std::is_convertible_v<Value, std::string>) {",
|
|
1059
|
+
" field = std::string(value);",
|
|
1060
|
+
" } else {",
|
|
1061
|
+
" field = gea_cpp_to_string(gea_cpp_value(value));",
|
|
1062
|
+
" }",
|
|
1063
|
+
" } else if constexpr (std::is_same_v<std::decay_t<Value>, std::vector<gea_cpp_value>> && requires(Field &out, const gea_cpp_value &item) { typename std::decay_t<Field>::value_type; out.clear(); out.reserve(std::size_t{}); out.push_back(typename std::decay_t<Field>::value_type(item)); }) {",
|
|
1064
|
+
" field.clear();",
|
|
1065
|
+
" field.reserve(value.size());",
|
|
1066
|
+
" for (const auto &item : value) field.push_back(typename std::decay_t<Field>::value_type(item));",
|
|
1067
|
+
" } else {",
|
|
1068
|
+
" field = static_cast<Field>(value);",
|
|
1069
|
+
" }",
|
|
1070
|
+
"}",
|
|
1071
|
+
"",
|
|
1072
|
+
// Per-store-method batching of keyed-list direct dispatch. A bulk update
|
|
1073
|
+
// (e.g. re-filling 14 forecast rows = ~42 item-field writes) would otherwise
|
|
1074
|
+
// re-run each affected keyed list\'s `.map` on EVERY write (~36ms each).
|
|
1075
|
+
// While a batch is open (begin/end wrap each store method; nested calls share
|
|
1076
|
+
// the outermost batch) the per-item direct handlers are recorded by
|
|
1077
|
+
// (state,key) and run ONCE at the outermost method exit instead.
|
|
1078
|
+
"inline int &storeDirectBatchDepth() { static int depth = 0; return depth; }",
|
|
1079
|
+
"inline std::vector<std::pair<gea_cpp_value, std::string>> &storeDirectBatchPending() {",
|
|
1080
|
+
" static std::vector<std::pair<gea_cpp_value, std::string>> pending;",
|
|
1081
|
+
" return pending;",
|
|
1082
|
+
"}",
|
|
1083
|
+
"inline const void *storeStateIdentity(const gea_cpp_value &state) {",
|
|
1084
|
+
" return state.entries ? static_cast<const void *>(state.entries.get()) : static_cast<const void *>(nullptr);",
|
|
1085
|
+
"}",
|
|
1086
|
+
"inline void storeDirectBatchBegin() { storeDirectBatchDepth()++; }",
|
|
1087
|
+
"inline void storeRunDirectField(const gea_cpp_value &state, const char *key) {",
|
|
1088
|
+
' auto direct_map = gea_cpp_key(state.record_get_literal("direct"));',
|
|
1089
|
+
" if (!gea::runtime::coerce::to_boolean(direct_map)) return;",
|
|
1090
|
+
" auto direct = gea_cpp_key(gea_cpp_map_like_get_literal(direct_map, key));",
|
|
1091
|
+
" if (!gea::runtime::coerce::to_boolean(direct)) return;",
|
|
1092
|
+
" for (auto &handler : direct) { (void)(handler()); }",
|
|
1093
|
+
"}",
|
|
1094
|
+
"inline void storeDirectBatchRecord(const gea_cpp_value &state, const char *key) {",
|
|
1095
|
+
" const void *id = storeStateIdentity(state);",
|
|
1096
|
+
" auto &pending = storeDirectBatchPending();",
|
|
1097
|
+
" for (const auto &entry : pending) {",
|
|
1098
|
+
" if (entry.second == key && storeStateIdentity(entry.first) == id) return;",
|
|
1099
|
+
" }",
|
|
1100
|
+
" pending.emplace_back(state, std::string(key));",
|
|
1101
|
+
"}",
|
|
1102
|
+
"inline void storeDirectBatchEnd() {",
|
|
1103
|
+
" if (storeDirectBatchDepth() > 0) storeDirectBatchDepth()--;",
|
|
1104
|
+
" if (storeDirectBatchDepth() != 0) return;",
|
|
1105
|
+
" if (storeDirectBatchPending().empty()) return;",
|
|
1106
|
+
" std::vector<std::pair<gea_cpp_value, std::string>> run;",
|
|
1107
|
+
" run.swap(storeDirectBatchPending());",
|
|
1108
|
+
" for (auto &entry : run) { storeRunDirectField(entry.first, entry.second.c_str()); }",
|
|
1109
|
+
"}",
|
|
1110
|
+
"",
|
|
1111
|
+
// RAII batch guard for a whole store method: begin a direct-notification',
|
|
1112
|
+
"// batch on entry, flush it on exit (any path — return, early return, throw).",
|
|
1113
|
+
"// This coalesces all reactive notifications a method makes into a single",
|
|
1114
|
+
"// flush at method exit, matching the batched semantics the lowered-method",
|
|
1115
|
+
"// path produced with storeDirectBatchBegin()/flush/End wrappers.",
|
|
1116
|
+
"struct StoreMethodBatch {",
|
|
1117
|
+
" StoreMethodBatch() { storeDirectBatchBegin(); }",
|
|
1118
|
+
" ~StoreMethodBatch() { storeDirectBatchEnd(); }",
|
|
1119
|
+
" StoreMethodBatch(const StoreMethodBatch &) = delete;",
|
|
1120
|
+
" StoreMethodBatch &operator=(const StoreMethodBatch &) = delete;",
|
|
1121
|
+
"};",
|
|
1122
|
+
"",
|
|
1123
|
+
"template <typename Owner, typename PrivMap, typename Field, typename Value, typename QueueFn>",
|
|
1124
|
+
"inline Value storeFieldSet(",
|
|
1125
|
+
" const Owner &owner,",
|
|
1126
|
+
" gea_cpp_value &self_value,",
|
|
1127
|
+
" PrivMap &priv,",
|
|
1128
|
+
" const char *key,",
|
|
1129
|
+
" Field &field,",
|
|
1130
|
+
" Value value,",
|
|
1131
|
+
" QueueFn queue_fn) {",
|
|
1132
|
+
" auto old_value = gea_cpp_key(field);",
|
|
1133
|
+
" auto stored_value = gea_cpp_key(value);",
|
|
1134
|
+
" if (!gea_cpp_same_value_zero(old_value, stored_value)) {",
|
|
1135
|
+
" storeAssignField(field, value);",
|
|
1136
|
+
// Fallback-lowered store methods (async, try/catch, typed JSON.parse — any
|
|
1137
|
+
// body the re-lowerer cannot take) write through THIS helper, not the
|
|
1138
|
+
// re-lowered dirty-flush path. Typed-hub renderer bindings only listen on
|
|
1139
|
+
// the hub, so without this notify a field written exclusively from a
|
|
1140
|
+
// fallback method (weather\'s applyApiWeather panel fields) never
|
|
1141
|
+
// re-renders. Hub appliers are microtask-scheduled and deduped, so the
|
|
1142
|
+
// extra notify on dual-path writes is harmless.
|
|
1143
|
+
" if constexpr (requires { owner.__gea_hub.notify(key); }) { owner.__gea_hub.notify(key); }",
|
|
1144
|
+
" auto state = gea_cpp_key(priv.get(self_value));",
|
|
1145
|
+
' auto direct_map = gea_cpp_key(state.record_get_literal("direct"));',
|
|
1146
|
+
" if (gea::runtime::coerce::to_boolean(direct_map)) {",
|
|
1147
|
+
" if (storeDirectBatchDepth() > 0) {",
|
|
1148
|
+
" storeDirectBatchRecord(state, key);",
|
|
1149
|
+
" } else {",
|
|
1150
|
+
" auto direct = gea_cpp_key(gea_cpp_map_like_get_literal(direct_map, key));",
|
|
1151
|
+
" if (gea::runtime::coerce::to_boolean(direct)) {",
|
|
1152
|
+
" for (auto &handler : direct) {",
|
|
1153
|
+
" (void)(handler(stored_value));",
|
|
1154
|
+
" }",
|
|
1155
|
+
" }",
|
|
1156
|
+
" }",
|
|
1157
|
+
" }",
|
|
1158
|
+
' auto observers = gea_cpp_key(state.record_get_literal("observers"));',
|
|
1159
|
+
" bool has_observers = gea::runtime::coerce::to_boolean(observers) && storeValueNonEmpty(gea_cpp_key(gea_cpp_map_like_get_literal(observers, key)));",
|
|
1160
|
+
" if (has_observers) {",
|
|
1161
|
+
" (void)(queue_fn(self_value, state, std::string(key), changeRecord(old_value, stored_value)));",
|
|
1162
|
+
" }",
|
|
1163
|
+
" }",
|
|
1164
|
+
" return value;",
|
|
1165
|
+
"}",
|
|
1166
|
+
"",
|
|
1167
|
+
// Generic over the array's element type so typed-storage stores
|
|
1168
|
+
// (`std::vector<X_item>`) and dynamic stores
|
|
1169
|
+
// (`std::vector<gea_cpp_value>`) both link the same fallback path.
|
|
1170
|
+
// For typed slots the inner `if constexpr` branch writes the field
|
|
1171
|
+
// directly via `set_typed_field`; legacy dynamic slots keep the
|
|
1172
|
+
// `record_set_literal` + dirty-symbol path. The previous-value
|
|
1173
|
+
// snapshot still goes through `gea_cpp_key(slot)` which is overloaded
|
|
1174
|
+
// for the typed item struct (see `cpp-stores.ts`).
|
|
1175
|
+
"template <typename Owner, typename PrivMap, typename Array, typename Value, typename QueueFn>",
|
|
1176
|
+
"inline Value storeVectorItemPropertySet(",
|
|
1177
|
+
" const Owner &owner,",
|
|
1178
|
+
" gea_cpp_value &self_value,",
|
|
1179
|
+
" PrivMap &priv,",
|
|
1180
|
+
" const char *field_key,",
|
|
1181
|
+
" Array &array,",
|
|
1182
|
+
" std::size_t index,",
|
|
1183
|
+
" const char *property_key,",
|
|
1184
|
+
" Value value,",
|
|
1185
|
+
" QueueFn queue_fn) {",
|
|
1186
|
+
" using __GeaSlotElement = typename std::decay_t<Array>::value_type;",
|
|
1187
|
+
" if (index >= array.size()) array.resize(index + 1);",
|
|
1188
|
+
" auto &slot = array[index];",
|
|
1189
|
+
" if constexpr (std::is_same_v<__GeaSlotElement, gea_cpp_value>) {",
|
|
1190
|
+
" if (slot.is_nullish()) {",
|
|
1191
|
+
" slot.kind = gea_cpp_value::kind_t::record;",
|
|
1192
|
+
" slot.entries = std::make_shared<std::vector<std::pair<std::string, gea_cpp_value>>>();",
|
|
1193
|
+
" }",
|
|
1194
|
+
" }",
|
|
1195
|
+
" auto state = gea_cpp_key(priv.get(self_value));",
|
|
1196
|
+
" const bool has_queued_consumers = storeHasQueuedConsumers(state, field_key);",
|
|
1197
|
+
" auto old_value = has_queued_consumers ? gea_cpp_snapshot_value(gea_cpp_key(slot)) : gea_cpp_value::missing();",
|
|
1198
|
+
" if constexpr (std::is_same_v<__GeaSlotElement, gea_cpp_value>) {",
|
|
1199
|
+
" slot.record_set_literal(property_key, gea_cpp_key(value));",
|
|
1200
|
+
// Direct gea_cpp_value construction, NOT gea_cpp_key(true): inside
|
|
1201
|
+
// namespace gea_ir the record/NodeHandle gea_cpp_key overloads hide the
|
|
1202
|
+
// runtime's global generic template, and none of them accepts bool.
|
|
1203
|
+
" slot.record_set(dirtySymbol(), gea_cpp_value(true));",
|
|
1204
|
+
" } else {",
|
|
1205
|
+
" set_typed_field(slot, property_key, value);",
|
|
1206
|
+
" }",
|
|
1207
|
+
// Same fallback-method coverage as storeFieldSet: keyed-list bindings for
|
|
1208
|
+
// compiled-base stores subscribe on the typed hub, so item writes from
|
|
1209
|
+
// fallback-lowered methods must notify it or the list never re-renders.
|
|
1210
|
+
" if constexpr (requires { owner.__gea_hub.notify(field_key); }) { owner.__gea_hub.notify(field_key); }",
|
|
1211
|
+
' auto direct_map = gea_cpp_key(state.record_get_literal("direct"));',
|
|
1212
|
+
" if (gea::runtime::coerce::to_boolean(direct_map)) {",
|
|
1213
|
+
" if (storeDirectBatchDepth() > 0) {",
|
|
1214
|
+
" storeDirectBatchRecord(state, field_key);",
|
|
1215
|
+
" } else {",
|
|
1216
|
+
" auto direct = gea_cpp_key(gea_cpp_map_like_get_literal(direct_map, field_key));",
|
|
1217
|
+
" if (gea::runtime::coerce::to_boolean(direct)) {",
|
|
1218
|
+
" for (auto &handler : direct) {",
|
|
1219
|
+
" (void)(handler());",
|
|
1220
|
+
" }",
|
|
1221
|
+
" }",
|
|
1222
|
+
" }",
|
|
1223
|
+
" }",
|
|
1224
|
+
" if (has_queued_consumers) {",
|
|
1225
|
+
" (void)(queue_fn(",
|
|
1226
|
+
" self_value,",
|
|
1227
|
+
" state,",
|
|
1228
|
+
" std::string(field_key),",
|
|
1229
|
+
" arrayItemChange(static_cast<double>(index), old_value, gea_cpp_snapshot_value(gea_cpp_key(slot)), true)));",
|
|
1230
|
+
" }",
|
|
1231
|
+
" return value;",
|
|
1232
|
+
"}",
|
|
1233
|
+
"",
|
|
1234
|
+
];
|
|
1235
|
+
}
|
|
1236
|
+
function changeRecordHelperSource() {
|
|
1237
|
+
return [
|
|
1238
|
+
"inline gea_cpp_value changeRecord(gea_cpp_value previous_value, gea_cpp_value new_value) {",
|
|
1239
|
+
" gea_cpp_value out = gea_cpp_value::empty_record();",
|
|
1240
|
+
' out.record_set_literal("previousValue", std::move(previous_value));',
|
|
1241
|
+
' out.record_set_literal("newValue", std::move(new_value));',
|
|
1242
|
+
" return out;",
|
|
1243
|
+
"}",
|
|
1244
|
+
"",
|
|
1245
|
+
"inline gea_cpp_value listChangeRecord(const char *type, double start = 0.0, double count = 0.0, bool has_range = false) {",
|
|
1246
|
+
" gea_cpp_value out = gea_cpp_value::empty_record();",
|
|
1247
|
+
' out.record_set_literal("type", gea_cpp_value(std::string(type)));',
|
|
1248
|
+
" if (has_range) {",
|
|
1249
|
+
' out.record_set_literal("start", gea_cpp_value(start));',
|
|
1250
|
+
' out.record_set_literal("count", gea_cpp_value(count));',
|
|
1251
|
+
" }",
|
|
1252
|
+
" return out;",
|
|
1253
|
+
"}",
|
|
1254
|
+
"",
|
|
1255
|
+
"inline gea_cpp_value arrayItemChange(double index, gea_cpp_value previous_value, gea_cpp_value new_value, bool item_dirty = false) {",
|
|
1256
|
+
" gea_cpp_value out = gea_cpp_value::empty_record();",
|
|
1257
|
+
' out.record_set_literal("aipu", gea_cpp_value(true));',
|
|
1258
|
+
' out.record_set_literal("arix", gea_cpp_value(index));',
|
|
1259
|
+
' out.record_set_literal("previousValue", std::move(previous_value));',
|
|
1260
|
+
' out.record_set_literal("newValue", std::move(new_value));',
|
|
1261
|
+
' if (item_dirty) out.record_set_literal("itemDirty", gea_cpp_value(true));',
|
|
1262
|
+
" return out;",
|
|
1263
|
+
"}",
|
|
1264
|
+
"",
|
|
1265
|
+
];
|
|
1266
|
+
}
|
|
1267
|
+
export function directCanvasInteropSource(usesCanvas, options = {}) {
|
|
1268
|
+
if (!usesCanvas)
|
|
1269
|
+
return [];
|
|
1270
|
+
const boxed = options.boxedValueModel !== false;
|
|
1271
|
+
return [
|
|
1272
|
+
...(boxed ? [] : ["struct gea_f32;", ""]),
|
|
1273
|
+
// The DOM helpers this namespace's own shim rows name, for the UNBOXED
|
|
1274
|
+
// model only. `domAppendChild` is stated once in the boxed generator
|
|
1275
|
+
// (`generateCppIrNamespaceSource`, alongside four `gea_cpp_value`
|
|
1276
|
+
// overloads), and a unit built on the unboxed model never carries that
|
|
1277
|
+
// block -- so `nativeMemberMethods.appendChild`, which spells
|
|
1278
|
+
// `gea_ir::domAppendChild({receiver}, {arg0})`, named a function nothing in
|
|
1279
|
+
// such a unit defined. Emitted here, guarded, rather than by changing the
|
|
1280
|
+
// row: the row is right, and the boxed unit must not get a second
|
|
1281
|
+
// definition of the same overload.
|
|
1282
|
+
//
|
|
1283
|
+
// Returns the appended child, mirroring DOM `Node.appendChild`, exactly as
|
|
1284
|
+
// the boxed definition does.
|
|
1285
|
+
...(boxed
|
|
1286
|
+
? []
|
|
1287
|
+
: [
|
|
1288
|
+
"inline gea::embedded::ui::NodeHandle domAppendChild(gea::embedded::ui::NodeHandle parent, gea::embedded::ui::NodeHandle child) {",
|
|
1289
|
+
" if (child.valid()) parent.appendChild(child);",
|
|
1290
|
+
" return child;",
|
|
1291
|
+
"}",
|
|
1292
|
+
"",
|
|
1293
|
+
]),
|
|
1294
|
+
"inline gea::embedded::ui::CanvasRenderingContext2D getCanvasContext(gea::embedded::ui::NodeHandle node, const std::string &kind) {",
|
|
1295
|
+
' if (kind != "2d") return gea::embedded::ui::CanvasRenderingContext2D();',
|
|
1296
|
+
" return gea::embedded::ui::CanvasElement(node.id()).getContext2D();",
|
|
1297
|
+
"}",
|
|
1298
|
+
"",
|
|
1299
|
+
...(boxed
|
|
1300
|
+
? [
|
|
1301
|
+
"inline gea::embedded::ui::CanvasRenderingContext2D getCanvasContext(gea::embedded::ui::NodeHandle node, const std::string &kind, const gea_cpp_value &) {",
|
|
1302
|
+
" return getCanvasContext(node, kind);",
|
|
1303
|
+
"}",
|
|
1304
|
+
"",
|
|
1305
|
+
]
|
|
1306
|
+
: []),
|
|
1307
|
+
// geatsc stores a NULLABLE typed handle (`Component<T>`\'s `el: T | null`)
|
|
1308
|
+
// as std::optional<NodeHandle>. Without these unwrapping overloads the
|
|
1309
|
+
// optional binds the generic gea_cpp_value parameter instead, boxing an
|
|
1310
|
+
// OPAQUE object whose record carries no __gea_node_id — the context then
|
|
1311
|
+
// binds node -1 and every draw silently no-ops (maps: black screen).
|
|
1312
|
+
"inline gea::embedded::ui::CanvasRenderingContext2D getCanvasContext(const std::optional<gea::embedded::ui::NodeHandle> &node, const std::string &kind) {",
|
|
1313
|
+
" return getCanvasContext(node.value_or(gea::embedded::ui::NodeHandle()), kind);",
|
|
1314
|
+
"}",
|
|
1315
|
+
"",
|
|
1316
|
+
...(boxed
|
|
1317
|
+
? [
|
|
1318
|
+
"inline gea::embedded::ui::CanvasRenderingContext2D getCanvasContext(const std::optional<gea::embedded::ui::NodeHandle> &node, const std::string &kind, const gea_cpp_value &) {",
|
|
1319
|
+
" return getCanvasContext(node.value_or(gea::embedded::ui::NodeHandle()), kind);",
|
|
1320
|
+
"}",
|
|
1321
|
+
"",
|
|
1322
|
+
]
|
|
1323
|
+
: []),
|
|
1324
|
+
"inline float canvasNumber(float value) { return value; }",
|
|
1325
|
+
"inline float canvasNumber(double value) { return static_cast<float>(value); }",
|
|
1326
|
+
"inline float canvasNumber(int8_t value) { return static_cast<float>(value); }",
|
|
1327
|
+
"inline float canvasNumber(uint8_t value) { return static_cast<float>(value); }",
|
|
1328
|
+
"inline float canvasNumber(int16_t value) { return static_cast<float>(value); }",
|
|
1329
|
+
"inline float canvasNumber(uint16_t value) { return static_cast<float>(value); }",
|
|
1330
|
+
"inline float canvasNumber(int value) { return static_cast<float>(value); }",
|
|
1331
|
+
"inline float canvasNumber(uint32_t value) { return static_cast<float>(value); }",
|
|
1332
|
+
"inline float canvasNumber(long long value) { return static_cast<float>(value); }",
|
|
1333
|
+
...(boxed ? ['inline float canvasNumber(const gea_f32 &value) { return value.v; }'] : []),
|
|
1334
|
+
"template <typename T>",
|
|
1335
|
+
"inline float canvasNumber(const T &value) {",
|
|
1336
|
+
" if constexpr (std::is_same_v<std::decay_t<T>, gea_f32>) return value.v;",
|
|
1337
|
+
" else if constexpr (std::is_arithmetic_v<std::decay_t<T>>) return static_cast<float>(value);",
|
|
1338
|
+
// Not a discarded branch to a consumer without geatsc's runtime: the
|
|
1339
|
+
// names here are NON-dependent, so a compiler resolves them where the
|
|
1340
|
+
// template is written rather than where it is instantiated. The
|
|
1341
|
+
// static_assert IS dependent, so it fires only if the branch is ever
|
|
1342
|
+
// selected -- and then it names the unsupported argument type instead of
|
|
1343
|
+
// reporting a missing namespace at the definition.
|
|
1344
|
+
...(boxed
|
|
1345
|
+
? [
|
|
1346
|
+
" else return gea::runtime::numeric::to_float(gea::runtime::coerce::to_number(gea_cpp_key(value)));",
|
|
1347
|
+
]
|
|
1348
|
+
: [
|
|
1349
|
+
" else {",
|
|
1350
|
+
' static_assert(sizeof(T) == 0, "canvasNumber: no numeric conversion for this argument type");',
|
|
1351
|
+
" return 0.0f;",
|
|
1352
|
+
" }",
|
|
1353
|
+
]),
|
|
1354
|
+
"}",
|
|
1355
|
+
"",
|
|
1356
|
+
"inline int canvasRound(float value) {",
|
|
1357
|
+
" if (!std::isfinite(value)) return 0;",
|
|
1358
|
+
" return static_cast<int>(value + (value >= 0.0f ? 0.5f : -0.5f));",
|
|
1359
|
+
"}",
|
|
1360
|
+
"inline int canvasInt(int8_t value) { return static_cast<int>(value); }",
|
|
1361
|
+
"inline int canvasInt(uint8_t value) { return static_cast<int>(value); }",
|
|
1362
|
+
"inline int canvasInt(int16_t value) { return static_cast<int>(value); }",
|
|
1363
|
+
"inline int canvasInt(uint16_t value) { return static_cast<int>(value); }",
|
|
1364
|
+
"inline int canvasInt(int value) { return value; }",
|
|
1365
|
+
"inline int canvasInt(uint32_t value) { return static_cast<int>(value); }",
|
|
1366
|
+
"inline int canvasInt(long long value) { return static_cast<int>(value); }",
|
|
1367
|
+
"inline int canvasInt(double value) { return canvasRound(static_cast<float>(value)); }",
|
|
1368
|
+
"inline int canvasInt(float value) { return canvasRound(value); }",
|
|
1369
|
+
...(boxed ? ['inline int canvasInt(const gea_f32 &value) { return canvasRound(value.v); }'] : []),
|
|
1370
|
+
"template <typename T>",
|
|
1371
|
+
"inline int canvasInt(const T &value) { return canvasRound(canvasNumber(value)); }",
|
|
1372
|
+
"",
|
|
1373
|
+
"inline std::string canvasString(const std::string &value) { return value; }",
|
|
1374
|
+
"inline std::string canvasString(const char *value) { return value ? std::string(value) : std::string(); }",
|
|
1375
|
+
"template <typename T>",
|
|
1376
|
+
"inline std::string canvasString(const T &value) {",
|
|
1377
|
+
" if constexpr (std::is_convertible_v<T, std::string>) return std::string(value);",
|
|
1378
|
+
" else if constexpr (std::is_arithmetic_v<std::decay_t<T>>) return std::to_string(value);",
|
|
1379
|
+
" else if constexpr (std::is_same_v<std::decay_t<T>, gea_f32>) return std::to_string(value.v);",
|
|
1380
|
+
" else return std::string();",
|
|
1381
|
+
"}",
|
|
1382
|
+
"",
|
|
1383
|
+
"inline gea::framework::graphics::pixel::native_t canvasRgb565(gea::framework::graphics::pixel::NativeColor value) { return value.value; }",
|
|
1384
|
+
"template <typename T>",
|
|
1385
|
+
"inline gea::framework::graphics::pixel::native_t canvasRgb565(const T &value) { return gea::framework::graphics::pixel::nativeFromRrggbbaa(static_cast<std::uint32_t>(canvasNumber(value))); }",
|
|
1386
|
+
"template <typename T>",
|
|
1387
|
+
"inline constexpr bool canvasColorValue = std::is_arithmetic_v<std::decay_t<T>> || std::is_same_v<std::decay_t<T>, gea::framework::graphics::pixel::NativeColor> || std::is_same_v<std::decay_t<T>, gea_f32>;",
|
|
1388
|
+
"",
|
|
1389
|
+
"template <typename T>",
|
|
1390
|
+
"inline int canvasImageId(const T &value) {",
|
|
1391
|
+
" if constexpr (requires { value.id; }) return canvasInt(value.id);",
|
|
1392
|
+
" else if constexpr (std::is_arithmetic_v<std::decay_t<T>> || std::is_same_v<std::decay_t<T>, gea_f32>) return canvasInt(value);",
|
|
1393
|
+
" else return -1;",
|
|
1394
|
+
"}",
|
|
1395
|
+
"",
|
|
1396
|
+
"inline void canvasClear(gea::embedded::ui::CanvasRenderingContext2D &ctx) {",
|
|
1397
|
+
" ctx.clear();",
|
|
1398
|
+
"}",
|
|
1399
|
+
"",
|
|
1400
|
+
"template <typename X, typename Y, typename W, typename H>",
|
|
1401
|
+
"inline void canvasClearRect(gea::embedded::ui::CanvasRenderingContext2D &ctx, const X &x, const Y &y, const W &w, const H &h) {",
|
|
1402
|
+
" ctx.clearRect(canvasInt(x), canvasInt(y), canvasInt(w), canvasInt(h));",
|
|
1403
|
+
"}",
|
|
1404
|
+
"",
|
|
1405
|
+
"template <typename X, typename Y, typename W, typename H>",
|
|
1406
|
+
"inline void canvasFillRect(gea::embedded::ui::CanvasRenderingContext2D &ctx, const X &x, const Y &y, const W &w, const H &h) {",
|
|
1407
|
+
" ctx.fillRect(canvasInt(x), canvasInt(y), canvasInt(w), canvasInt(h));",
|
|
1408
|
+
"}",
|
|
1409
|
+
"",
|
|
1410
|
+
"template <typename X, typename Y, typename W, typename H>",
|
|
1411
|
+
"inline void canvasStrokeRect(gea::embedded::ui::CanvasRenderingContext2D &ctx, const X &x, const Y &y, const W &w, const H &h) {",
|
|
1412
|
+
" ctx.strokeRect(canvasInt(x), canvasInt(y), canvasInt(w), canvasInt(h));",
|
|
1413
|
+
"}",
|
|
1414
|
+
"",
|
|
1415
|
+
"template <typename X, typename Y, typename R, typename Fill>",
|
|
1416
|
+
"inline void canvasFillCircle(gea::embedded::ui::CanvasRenderingContext2D &ctx, const X &x, const Y &y, const R &r, const Fill &fill) {",
|
|
1417
|
+
" if constexpr (canvasColorValue<Fill>) {",
|
|
1418
|
+
" ctx.fillCircleRgb565(canvasInt(x), canvasInt(y), canvasInt(r), canvasRgb565(fill));",
|
|
1419
|
+
" } else {",
|
|
1420
|
+
" ctx.setFillStyle(canvasString(fill));",
|
|
1421
|
+
" ctx.fillCircle(canvasInt(x), canvasInt(y), canvasInt(r));",
|
|
1422
|
+
" }",
|
|
1423
|
+
"}",
|
|
1424
|
+
"",
|
|
1425
|
+
"template <typename X, typename Y, typename R>",
|
|
1426
|
+
"inline void canvasStrokeCircle(gea::embedded::ui::CanvasRenderingContext2D &ctx, const X &x, const Y &y, const R &r) {",
|
|
1427
|
+
" ctx.strokeCircle(canvasInt(x), canvasInt(y), canvasInt(r));",
|
|
1428
|
+
"}",
|
|
1429
|
+
"",
|
|
1430
|
+
"template <typename X, typename Y, typename R, typename Fill>",
|
|
1431
|
+
"inline void canvasFillCircleRgb565(gea::embedded::ui::CanvasRenderingContext2D &ctx, const X &x, const Y &y, const R &r, const Fill &fill) {",
|
|
1432
|
+
" ctx.fillCircleRgb565(canvasInt(x), canvasInt(y), canvasInt(r), canvasRgb565(fill));",
|
|
1433
|
+
"}",
|
|
1434
|
+
"",
|
|
1435
|
+
"template <typename X0, typename Y0, typename X1, typename Y1, typename X2, typename Y2, typename Fill>",
|
|
1436
|
+
"inline void canvasFillTriangleRgb565(gea::embedded::ui::CanvasRenderingContext2D &ctx, const X0 &x0, const Y0 &y0, const X1 &x1, const Y1 &y1, const X2 &x2, const Y2 &y2, const Fill &fill) {",
|
|
1437
|
+
" ctx.fillTriangleRgb565(canvasInt(x0), canvasInt(y0), canvasInt(x1), canvasInt(y1), canvasInt(x2), canvasInt(y2), canvasRgb565(fill));",
|
|
1438
|
+
"}",
|
|
1439
|
+
"",
|
|
1440
|
+
"template <typename XS, typename YS, typename R, typename Colors>",
|
|
1441
|
+
"inline void canvasFillCirclesRgb565(gea::embedded::ui::CanvasRenderingContext2D &ctx, const XS &xs, const YS &ys, const R &r, const Colors &colors, int count = -1) {",
|
|
1442
|
+
" using __gea_color_t = std::decay_t<typename Colors::value_type>;",
|
|
1443
|
+
" if constexpr (std::is_same_v<__gea_color_t, gea::framework::graphics::pixel::native_t> || std::is_same_v<__gea_color_t, gea::framework::graphics::pixel::NativeColor>) {",
|
|
1444
|
+
" if constexpr (requires { xs.data(); ys.data(); colors.data(); } && std::is_same_v<std::decay_t<decltype(*xs.data())>, std::uint16_t> && std::is_same_v<std::decay_t<decltype(*ys.data())>, std::uint16_t>) {",
|
|
1445
|
+
" // Pointer path. Passing the containers themselves binds a typed array",
|
|
1446
|
+
" // through its implicit operator std::vector<Element>(), which heap-",
|
|
1447
|
+
" // allocates and copies the FULL array on every call while ignoring",
|
|
1448
|
+
" // `count` -- ~150us per call on esp32-s3 regardless of how many",
|
|
1449
|
+
" // circles it carried.",
|
|
1450
|
+
" std::size_t __gea_n = xs.size() < ys.size() ? xs.size() : ys.size();",
|
|
1451
|
+
" if (colors.size() < __gea_n) __gea_n = colors.size();",
|
|
1452
|
+
" if (count >= 0 && static_cast<std::size_t>(count) < __gea_n) __gea_n = static_cast<std::size_t>(count);",
|
|
1453
|
+
" ctx.fillCirclesRgb565(xs.data(), ys.data(), canvasInt(r), colors.data(), static_cast<int>(__gea_n));",
|
|
1454
|
+
" } else {",
|
|
1455
|
+
" ctx.fillCirclesRgb565(xs, ys, canvasInt(r), colors, count);",
|
|
1456
|
+
" }",
|
|
1457
|
+
" } else {",
|
|
1458
|
+
" std::size_t __gea_n = colors.size();",
|
|
1459
|
+
" if (count >= 0 && static_cast<std::size_t>(count) < __gea_n) __gea_n = static_cast<std::size_t>(count);",
|
|
1460
|
+
" std::vector<gea::framework::graphics::pixel::native_t> __gea_native_colors;",
|
|
1461
|
+
" __gea_native_colors.reserve(__gea_n);",
|
|
1462
|
+
" for (std::size_t __gea_i = 0; __gea_i < __gea_n; ++__gea_i) __gea_native_colors.push_back(canvasRgb565(colors[__gea_i]));",
|
|
1463
|
+
" ctx.fillCirclesRgb565(xs, ys, canvasInt(r), __gea_native_colors, count);",
|
|
1464
|
+
" }",
|
|
1465
|
+
"}",
|
|
1466
|
+
"",
|
|
1467
|
+
"template <typename XS, typename YS, typename R, typename Fill>",
|
|
1468
|
+
"inline void canvasFillCirclesRgb565Uniform(gea::embedded::ui::CanvasRenderingContext2D &ctx, const XS &xs, const YS &ys, const R &r, const Fill &fill) {",
|
|
1469
|
+
" ctx.fillCirclesRgb565(xs, ys, canvasInt(r), canvasRgb565(fill));",
|
|
1470
|
+
"}",
|
|
1471
|
+
"",
|
|
1472
|
+
"template <typename X0s, typename Y0s, typename X1s, typename Y1s, typename X2s, typename Y2s, typename Colors, typename Order>",
|
|
1473
|
+
"inline void canvasFillTrianglesRgb565Sorted(gea::embedded::ui::CanvasRenderingContext2D &ctx, const X0s &x0s, const Y0s &y0s, const X1s &x1s, const Y1s &y1s, const X2s &x2s, const Y2s &y2s, const Colors &colors, const Order &order, int count = -1) {",
|
|
1474
|
+
" // Colours are authored 0xRRGGBBAA (plain numbers, e.g. a Uint32Array);",
|
|
1475
|
+
" // the canvas layer converts during packing. `order` gives the paint",
|
|
1476
|
+
" // (depth) order into the coordinate/colour arrays; `count` (>=0) limits",
|
|
1477
|
+
" // to the first N entries so callers can reuse fixed-size scratch arrays.",
|
|
1478
|
+
" ctx.fillTrianglesRgb565Sorted(x0s, y0s, x1s, y1s, x2s, y2s, colors, order, count);",
|
|
1479
|
+
"}",
|
|
1480
|
+
"",
|
|
1481
|
+
"inline void canvasBeginPath(gea::embedded::ui::CanvasRenderingContext2D &ctx) {",
|
|
1482
|
+
" ctx.beginPath();",
|
|
1483
|
+
"}",
|
|
1484
|
+
"",
|
|
1485
|
+
"template <typename X, typename Y, typename R, typename Start, typename End>",
|
|
1486
|
+
"inline void canvasArc(gea::embedded::ui::CanvasRenderingContext2D &ctx, const X &x, const Y &y, const R &r, const Start &start, const End &end) {",
|
|
1487
|
+
" ctx.arc(canvasNumber(x), canvasNumber(y), canvasNumber(r), canvasNumber(start), canvasNumber(end));",
|
|
1488
|
+
"}",
|
|
1489
|
+
"",
|
|
1490
|
+
"template <typename X, typename Y>",
|
|
1491
|
+
"inline void canvasMoveTo(gea::embedded::ui::CanvasRenderingContext2D &ctx, const X &x, const Y &y) {",
|
|
1492
|
+
" ctx.moveTo(canvasNumber(x), canvasNumber(y));",
|
|
1493
|
+
"}",
|
|
1494
|
+
"",
|
|
1495
|
+
"template <typename X, typename Y>",
|
|
1496
|
+
"inline void canvasLineTo(gea::embedded::ui::CanvasRenderingContext2D &ctx, const X &x, const Y &y) {",
|
|
1497
|
+
" ctx.lineTo(canvasNumber(x), canvasNumber(y));",
|
|
1498
|
+
"}",
|
|
1499
|
+
"",
|
|
1500
|
+
"inline void canvasClosePath(gea::embedded::ui::CanvasRenderingContext2D &ctx) {",
|
|
1501
|
+
" ctx.closePath();",
|
|
1502
|
+
"}",
|
|
1503
|
+
"",
|
|
1504
|
+
"inline void canvasFill(gea::embedded::ui::CanvasRenderingContext2D &ctx) {",
|
|
1505
|
+
" ctx.fill();",
|
|
1506
|
+
"}",
|
|
1507
|
+
"",
|
|
1508
|
+
"inline void canvasStroke(gea::embedded::ui::CanvasRenderingContext2D &ctx) {",
|
|
1509
|
+
" ctx.stroke();",
|
|
1510
|
+
"}",
|
|
1511
|
+
"",
|
|
1512
|
+
"template <typename Text, typename X, typename Y>",
|
|
1513
|
+
"inline void canvasFillText(gea::embedded::ui::CanvasRenderingContext2D &ctx, const Text &text, const X &x, const Y &y) {",
|
|
1514
|
+
" ctx.fillText(canvasString(text), canvasInt(x), canvasInt(y));",
|
|
1515
|
+
"}",
|
|
1516
|
+
"",
|
|
1517
|
+
"template <typename Image, typename X, typename Y>",
|
|
1518
|
+
"inline void canvasDrawImage(gea::embedded::ui::CanvasRenderingContext2D &ctx, const Image &image, const X &x, const Y &y) {",
|
|
1519
|
+
" ctx.drawImage(canvasImageId(image), canvasInt(x), canvasInt(y));",
|
|
1520
|
+
"}",
|
|
1521
|
+
"",
|
|
1522
|
+
"template <typename Image, typename X, typename Y, typename W, typename H>",
|
|
1523
|
+
"inline void canvasDrawImage(gea::embedded::ui::CanvasRenderingContext2D &ctx, const Image &image, const X &x, const Y &y, const W &w, const H &h) {",
|
|
1524
|
+
" ctx.drawImage(canvasImageId(image), canvasInt(x), canvasInt(y), canvasInt(w), canvasInt(h));",
|
|
1525
|
+
"}",
|
|
1526
|
+
"",
|
|
1527
|
+
"template <typename Text>",
|
|
1528
|
+
"inline double canvasMeasureTextInkCenter(gea::embedded::ui::CanvasRenderingContext2D &ctx, const Text &text) {",
|
|
1529
|
+
" return ctx.measureTextInkCenter(canvasString(text));",
|
|
1530
|
+
"}",
|
|
1531
|
+
"",
|
|
1532
|
+
"template <typename Text>",
|
|
1533
|
+
"inline double canvasMeasureText(gea::embedded::ui::CanvasRenderingContext2D &ctx, const Text &text) {",
|
|
1534
|
+
" return ctx.measureText(canvasString(text));",
|
|
1535
|
+
"}",
|
|
1536
|
+
"",
|
|
1537
|
+
"template <typename Image, typename X, typename Y, typename W, typename H>",
|
|
1538
|
+
"inline void canvasDrawImageCircle(gea::embedded::ui::CanvasRenderingContext2D &ctx, const Image &image, const X &x, const Y &y, const W &w, const H &h) {",
|
|
1539
|
+
" ctx.drawImageCircle(canvasImageId(image), canvasInt(x), canvasInt(y), canvasInt(w), canvasInt(h));",
|
|
1540
|
+
"}",
|
|
1541
|
+
"",
|
|
1542
|
+
"template <typename Image, typename X, typename Y, typename W, typename H>",
|
|
1543
|
+
"inline void canvasDrawImageRotated90CW(gea::embedded::ui::CanvasRenderingContext2D &ctx, const Image &image, const X &x, const Y &y, const W &w, const H &h) {",
|
|
1544
|
+
" ctx.drawImageRotated90CW(canvasImageId(image), canvasInt(x), canvasInt(y), canvasInt(w), canvasInt(h));",
|
|
1545
|
+
"}",
|
|
1546
|
+
"",
|
|
1547
|
+
"template <typename Image, typename X, typename Y, typename W>",
|
|
1548
|
+
"inline void canvasDrawImageTiledX(gea::embedded::ui::CanvasRenderingContext2D &ctx, const Image &image, const X &x, const Y &y, const W &w) {",
|
|
1549
|
+
" ctx.drawImageTiledX(canvasImageId(image), canvasInt(x), canvasInt(y), canvasInt(w));",
|
|
1550
|
+
"}",
|
|
1551
|
+
"",
|
|
1552
|
+
"inline void canvasFlush(gea::embedded::ui::CanvasRenderingContext2D &ctx) {",
|
|
1553
|
+
" ctx.flush();",
|
|
1554
|
+
"}",
|
|
1555
|
+
"",
|
|
1556
|
+
"inline void canvasBeginBatch(gea::embedded::ui::CanvasRenderingContext2D &ctx) {",
|
|
1557
|
+
" ctx.beginBatch();",
|
|
1558
|
+
"}",
|
|
1559
|
+
"",
|
|
1560
|
+
"inline void canvasEndBatch(gea::embedded::ui::CanvasRenderingContext2D &ctx) {",
|
|
1561
|
+
" ctx.endBatch();",
|
|
1562
|
+
"}",
|
|
1563
|
+
"",
|
|
1564
|
+
"template <typename Value>",
|
|
1565
|
+
"inline const Value &canvasSetFillStyle(gea::embedded::ui::CanvasRenderingContext2D &ctx, const Value &value) {",
|
|
1566
|
+
" if constexpr (canvasColorValue<Value>) ctx.setFillStyleRgb565(canvasRgb565(value));",
|
|
1567
|
+
" else ctx.setFillStyle(canvasString(value));",
|
|
1568
|
+
" return value;",
|
|
1569
|
+
"}",
|
|
1570
|
+
"",
|
|
1571
|
+
"inline gea::framework::graphics::pixel::native_t canvasSetFillStyleRgb565(gea::embedded::ui::CanvasRenderingContext2D &ctx, gea::framework::graphics::pixel::native_t value) {",
|
|
1572
|
+
" ctx.setFillStyleRgb565(value);",
|
|
1573
|
+
" return value;",
|
|
1574
|
+
"}",
|
|
1575
|
+
"",
|
|
1576
|
+
"template <typename Value>",
|
|
1577
|
+
"inline const Value &canvasSetStrokeStyle(gea::embedded::ui::CanvasRenderingContext2D &ctx, const Value &value) {",
|
|
1578
|
+
" if constexpr (canvasColorValue<Value>) ctx.setStrokeStyleRgb565(canvasRgb565(value));",
|
|
1579
|
+
" else ctx.setStrokeStyle(canvasString(value));",
|
|
1580
|
+
" return value;",
|
|
1581
|
+
"}",
|
|
1582
|
+
"",
|
|
1583
|
+
"template <typename Value>",
|
|
1584
|
+
"inline const Value &canvasSetGlobalAlpha(gea::embedded::ui::CanvasRenderingContext2D &ctx, const Value &value) {",
|
|
1585
|
+
" ctx.setGlobalAlpha(canvasNumber(value));",
|
|
1586
|
+
" return value;",
|
|
1587
|
+
"}",
|
|
1588
|
+
"",
|
|
1589
|
+
"template <typename Value>",
|
|
1590
|
+
"inline const Value &canvasSetLineWidth(gea::embedded::ui::CanvasRenderingContext2D &ctx, const Value &value) {",
|
|
1591
|
+
" ctx.setLineWidth(canvasNumber(value));",
|
|
1592
|
+
" return value;",
|
|
1593
|
+
"}",
|
|
1594
|
+
"",
|
|
1595
|
+
"template <typename Value>",
|
|
1596
|
+
"inline const Value &canvasSetFont(gea::embedded::ui::CanvasRenderingContext2D &ctx, const Value &value) {",
|
|
1597
|
+
" ctx.setFont(canvasString(value));",
|
|
1598
|
+
" return value;",
|
|
1599
|
+
"}",
|
|
1600
|
+
"",
|
|
1601
|
+
"template <typename Value>",
|
|
1602
|
+
"inline const Value &canvasSetTextBaseline(gea::embedded::ui::CanvasRenderingContext2D &ctx, const Value &value) {",
|
|
1603
|
+
" ctx.setTextBaseline(canvasString(value));",
|
|
1604
|
+
" return value;",
|
|
1605
|
+
"}",
|
|
1606
|
+
"",
|
|
1607
|
+
"template <typename Value>",
|
|
1608
|
+
"inline const Value &canvasSetTextAlign(gea::embedded::ui::CanvasRenderingContext2D &ctx, const Value &value) {",
|
|
1609
|
+
" ctx.setTextAlign(canvasString(value));",
|
|
1610
|
+
" return value;",
|
|
1611
|
+
"}",
|
|
1612
|
+
"",
|
|
1613
|
+
];
|
|
1614
|
+
}
|
|
1615
|
+
function canvasInteropSource(usesCanvas) {
|
|
1616
|
+
if (directCanvasContextEnabled())
|
|
1617
|
+
return [
|
|
1618
|
+
...domStyleInteropSource(),
|
|
1619
|
+
...directCanvasInteropSource(usesCanvas),
|
|
1620
|
+
];
|
|
1621
|
+
return [
|
|
1622
|
+
...domStyleInteropSource(),
|
|
1623
|
+
"",
|
|
1624
|
+
"inline int nodeIdFromValue(const gea_cpp_value &value) {",
|
|
1625
|
+
' gea_cpp_value id = value.record_get_literal("__gea_node_id");',
|
|
1626
|
+
" if (id.is_nullish()) return -1;",
|
|
1627
|
+
" const double raw = gea::runtime::coerce::to_number(id);",
|
|
1628
|
+
" if (!(raw == raw)) return -1;",
|
|
1629
|
+
" return static_cast<int>(raw);",
|
|
1630
|
+
"}",
|
|
1631
|
+
"",
|
|
1632
|
+
"// Native event-listener registration for imperative `el.addEventListener(...)`.",
|
|
1633
|
+
"// The generated handler body is already polymorphic over the event arg, so the",
|
|
1634
|
+
"// emitter types the listener param as `PointerEvent &` and routes here — the",
|
|
1635
|
+
"// node receives the native event directly, never marshalled through a",
|
|
1636
|
+
"// gea_cpp_value record (which would re-box the whole typed PointerEvent on",
|
|
1637
|
+
"// every dispatch).",
|
|
1638
|
+
"inline void addNativeEventListener(gea::embedded::ui::NodeHandle node, const std::string &type, std::function<void(gea::framework::events::PointerEvent &)> listener) {",
|
|
1639
|
+
" node.addEventListener(type.c_str(), std::move(listener));",
|
|
1640
|
+
"}",
|
|
1641
|
+
"",
|
|
1642
|
+
"inline void addNativeEventListener(const gea_cpp_value &node, const std::string &type, std::function<void(gea::framework::events::PointerEvent &)> listener) {",
|
|
1643
|
+
" const int id = nodeIdFromValue(node);",
|
|
1644
|
+
" if (id >= 0) gea::embedded::ui::NodeHandle(id).addEventListener(type.c_str(), std::move(listener));",
|
|
1645
|
+
"}",
|
|
1646
|
+
"",
|
|
1647
|
+
"inline std::string nodeStringArg(const gea_cpp_value &value) {",
|
|
1648
|
+
" return value.is_nullish() ? std::string() : gea_cpp_to_string(value);",
|
|
1649
|
+
"}",
|
|
1650
|
+
"",
|
|
1651
|
+
"inline gea_cpp_value nodeClassListValue(gea::embedded::ui::NodeHandle node) {",
|
|
1652
|
+
" gea_cpp_value value = gea_cpp_value::empty_record();",
|
|
1653
|
+
' value.record_set_literal("add", gea_cpp_callable_args([node](const std::vector<gea_cpp_value> &tokens) -> gea_cpp_value {',
|
|
1654
|
+
" for (const auto &token : tokens) node.classList().add(nodeStringArg(token));",
|
|
1655
|
+
" return gea_cpp_value::missing();",
|
|
1656
|
+
" }));",
|
|
1657
|
+
' value.record_set_literal("remove", gea_cpp_callable_args([node](const std::vector<gea_cpp_value> &tokens) -> gea_cpp_value {',
|
|
1658
|
+
" for (const auto &token : tokens) node.classList().remove(nodeStringArg(token));",
|
|
1659
|
+
" return gea_cpp_value::missing();",
|
|
1660
|
+
" }));",
|
|
1661
|
+
' value.record_set_literal("contains", gea_cpp_value([node](gea_cpp_value token) -> gea_cpp_value {',
|
|
1662
|
+
" return gea_cpp_value(node.classList().contains(nodeStringArg(token)));",
|
|
1663
|
+
" }));",
|
|
1664
|
+
' value.record_set_literal("toggle", gea_cpp_callable_args([node](const std::vector<gea_cpp_value> &args) -> gea_cpp_value {',
|
|
1665
|
+
" if (args.empty()) return gea_cpp_value(false);",
|
|
1666
|
+
" const std::string token = nodeStringArg(args[0]);",
|
|
1667
|
+
" if (args.size() > 1 && !args[1].is_nullish()) return gea_cpp_value(node.classList().toggle(token, gea::runtime::coerce::to_boolean(args[1])));",
|
|
1668
|
+
" return gea_cpp_value(node.classList().toggle(token));",
|
|
1669
|
+
" }));",
|
|
1670
|
+
' value.record_set_literal("set", gea_cpp_value([node](gea_cpp_value class_name) -> gea_cpp_value {',
|
|
1671
|
+
" node.classList().set(nodeStringArg(class_name));",
|
|
1672
|
+
" return gea_cpp_value::missing();",
|
|
1673
|
+
" }));",
|
|
1674
|
+
' value.record_set_literal("value", gea_cpp_value(node.classList().value()));',
|
|
1675
|
+
" return value;",
|
|
1676
|
+
"}",
|
|
1677
|
+
"",
|
|
1678
|
+
"inline void nodeApplyStyleProperty(gea::embedded::ui::NodeHandle node, const std::string &property, const gea_cpp_value &value) {",
|
|
1679
|
+
" if (value.kind == gea_cpp_value::kind_t::number) {",
|
|
1680
|
+
" (void)gea::embedded::ui::StyleSheet::instance().applyNumberProperty(node, property.c_str(), value.number);",
|
|
1681
|
+
" return;",
|
|
1682
|
+
" }",
|
|
1683
|
+
" gea::embedded::ui::StyleSheet::instance().applyProperty(node, property, nodeStringArg(value));",
|
|
1684
|
+
"}",
|
|
1685
|
+
"",
|
|
1686
|
+
"inline gea_cpp_value nodeStyleValue(gea::embedded::ui::NodeHandle node) {",
|
|
1687
|
+
" return gea_cpp_value::object(",
|
|
1688
|
+
" (static_cast<std::uintptr_t>(node.id()) << 8u) | 0x53u,",
|
|
1689
|
+
" [node](const std::string &key) -> gea_cpp_value {",
|
|
1690
|
+
' if (key == "setProperty") return gea_cpp_value([node](gea_cpp_value name_value, gea_cpp_value property_value) -> gea_cpp_value {',
|
|
1691
|
+
" nodeApplyStyleProperty(node, nodeStringArg(name_value), property_value);",
|
|
1692
|
+
" return gea_cpp_value::missing();",
|
|
1693
|
+
" });",
|
|
1694
|
+
' if (key == "removeProperty") return gea_cpp_value([node](gea_cpp_value name_value) -> gea_cpp_value {',
|
|
1695
|
+
" (void)gea::embedded::ui::StyleSheet::instance().removeProperty(node, nodeStringArg(name_value));",
|
|
1696
|
+
" return gea_cpp_value::missing();",
|
|
1697
|
+
" });",
|
|
1698
|
+
" return gea_cpp_value::missing();",
|
|
1699
|
+
" },",
|
|
1700
|
+
" [node](const std::string &key, gea_cpp_value assigned_value) {",
|
|
1701
|
+
" nodeApplyStyleProperty(node, key, assigned_value);",
|
|
1702
|
+
" },",
|
|
1703
|
+
" [](const std::string &key) -> bool {",
|
|
1704
|
+
' return key == "setProperty" || key == "removeProperty";',
|
|
1705
|
+
" });",
|
|
1706
|
+
"}",
|
|
1707
|
+
"",
|
|
1708
|
+
"inline gea_cpp_value pointerEventValue(gea::framework::events::PointerEvent &event);",
|
|
1709
|
+
"",
|
|
1710
|
+
...(usesCanvas
|
|
1711
|
+
? [
|
|
1712
|
+
"// Forward declaration: nodeValue's boxed getContext dispatch needs it; the",
|
|
1713
|
+
"// definition follows later in the prelude.",
|
|
1714
|
+
"inline gea::embedded::ui::CanvasRenderingContext2D getCanvasContext(const gea_cpp_value &node, const std::string &kind);",
|
|
1715
|
+
"inline gea::embedded::ui::CanvasRenderingContext2D getCanvasContext(const gea_cpp_value &node, const std::string &kind, const gea_cpp_value &attributes);",
|
|
1716
|
+
"",
|
|
1717
|
+
]
|
|
1718
|
+
: []),
|
|
1719
|
+
"inline gea_cpp_value nodeValue(gea::embedded::ui::NodeHandle node) {",
|
|
1720
|
+
" return gea_cpp_value::object(",
|
|
1721
|
+
" static_cast<std::uintptr_t>(node.id() + 1),",
|
|
1722
|
+
" [node](const std::string &key) -> gea_cpp_value {",
|
|
1723
|
+
' if (key == "__gea_node_id") return gea_cpp_value(static_cast<double>(node.id()));',
|
|
1724
|
+
' if (key == "nodeType") return gea_cpp_value(1.0);',
|
|
1725
|
+
' if (key == "classList") return nodeClassListValue(node);',
|
|
1726
|
+
' if (key == "className") return gea_cpp_value(node.classList().value());',
|
|
1727
|
+
' if (key == "style") return nodeStyleValue(node);',
|
|
1728
|
+
' if (key == "setAttribute") return gea_cpp_value([node](gea_cpp_value name_value, gea_cpp_value attr_value) -> gea_cpp_value {',
|
|
1729
|
+
" const std::string name = nodeStringArg(name_value);",
|
|
1730
|
+
" const std::string value = nodeStringArg(attr_value);",
|
|
1731
|
+
" node.setAttribute(name.c_str(), value.c_str());",
|
|
1732
|
+
" return gea_cpp_value::missing();",
|
|
1733
|
+
" });",
|
|
1734
|
+
' if (key == "getAttribute") return gea_cpp_value([node](gea_cpp_value name_value) -> gea_cpp_value {',
|
|
1735
|
+
" const std::string name = nodeStringArg(name_value);",
|
|
1736
|
+
" return gea_cpp_value(std::string(node.getAttribute(name.c_str())));",
|
|
1737
|
+
" });",
|
|
1738
|
+
' if (key == "hasAttribute") return gea_cpp_value([node](gea_cpp_value name_value) -> gea_cpp_value {',
|
|
1739
|
+
" const std::string name = nodeStringArg(name_value);",
|
|
1740
|
+
" return gea_cpp_value(node.hasAttribute(name.c_str()));",
|
|
1741
|
+
" });",
|
|
1742
|
+
' if (key == "addEventListener") return gea_cpp_value([node](gea_cpp_value type_value, gea_cpp_value listener_value) -> gea_cpp_value {',
|
|
1743
|
+
" const std::string type = nodeStringArg(type_value);",
|
|
1744
|
+
" auto listener = gea_cpp_key(listener_value);",
|
|
1745
|
+
" if (!gea::runtime::coerce::to_boolean(listener)) return gea_cpp_value::missing();",
|
|
1746
|
+
" node.addEventListener(type.c_str(), [listener](gea::framework::events::PointerEvent &event) mutable {",
|
|
1747
|
+
" (void)listener(pointerEventValue(event));",
|
|
1748
|
+
" });",
|
|
1749
|
+
" return gea_cpp_value::missing();",
|
|
1750
|
+
" });",
|
|
1751
|
+
...(usesCanvas
|
|
1752
|
+
? [
|
|
1753
|
+
' if (key == "getContext") return gea_cpp_value([node](gea_cpp_value kind_value) -> gea_cpp_value {',
|
|
1754
|
+
" return geaCanvasContextValue(getCanvasContext(nodeValue(node), nodeStringArg(kind_value)));",
|
|
1755
|
+
" });",
|
|
1756
|
+
]
|
|
1757
|
+
: []),
|
|
1758
|
+
' if (key == "play") return gea_cpp_value([node]() -> gea_cpp_value {',
|
|
1759
|
+
" return gea_cpp_value(node.play());",
|
|
1760
|
+
" });",
|
|
1761
|
+
' if (key == "pause") return gea_cpp_value([node]() -> gea_cpp_value {',
|
|
1762
|
+
" node.pause();",
|
|
1763
|
+
" return gea_cpp_value::missing();",
|
|
1764
|
+
" });",
|
|
1765
|
+
' if (key == "firstChild") {',
|
|
1766
|
+
" auto child = node.firstChildHandle();",
|
|
1767
|
+
" return child.valid() ? nodeValue(child) : gea_cpp_value::null_v();",
|
|
1768
|
+
" }",
|
|
1769
|
+
' if (key == "childNodes") {',
|
|
1770
|
+
" std::vector<gea_cpp_value> kids;",
|
|
1771
|
+
" for (int i = 0;; i++) {",
|
|
1772
|
+
" auto c = node.childAt(i);",
|
|
1773
|
+
" if (!c.valid()) break;",
|
|
1774
|
+
" kids.push_back(nodeValue(c));",
|
|
1775
|
+
" }",
|
|
1776
|
+
" return gea_cpp_value(kids);",
|
|
1777
|
+
" }",
|
|
1778
|
+
' if (key == "children") {',
|
|
1779
|
+
" std::vector<gea_cpp_value> kids;",
|
|
1780
|
+
" const auto &__gea_tree = gea::embedded::ui::Tree::instance();",
|
|
1781
|
+
" for (int i = 0;; i++) {",
|
|
1782
|
+
" auto c = node.childAt(i);",
|
|
1783
|
+
" if (!c.valid()) break;",
|
|
1784
|
+
" const auto &__gea_child_node = __gea_tree.node(c.id());",
|
|
1785
|
+
" const std::string __gea_child_tag = gea::embedded::ui::tagFromId(__gea_child_node.tag_id);",
|
|
1786
|
+
' if (__gea_child_node.type == gea::embedded::ui::NodeType::Text || __gea_child_tag == "#comment" || __gea_child_tag == "#fragment" || __gea_child_tag == "#document-frag" || __gea_child_tag == "#document-fragment") continue;',
|
|
1787
|
+
" kids.push_back(nodeValue(c));",
|
|
1788
|
+
" }",
|
|
1789
|
+
" return gea_cpp_value(kids);",
|
|
1790
|
+
" }",
|
|
1791
|
+
' if (key == "cloneNode") return gea_cpp_value([node](gea_cpp_value deep_value) -> gea_cpp_value {',
|
|
1792
|
+
" const bool deep = deep_value.is_nullish() ? true : gea::runtime::coerce::to_boolean(gea_cpp_key(deep_value));",
|
|
1793
|
+
" return nodeValue(node.cloneNode(deep));",
|
|
1794
|
+
" });",
|
|
1795
|
+
' if (key == "appendChild") return gea_cpp_value([node](gea_cpp_value child_value) -> gea_cpp_value {',
|
|
1796
|
+
" const int child_id = nodeIdFromValue(child_value);",
|
|
1797
|
+
" if (child_id >= 0) node.appendChild(gea::embedded::ui::NodeHandle(child_id));",
|
|
1798
|
+
" return gea_cpp_value::missing();",
|
|
1799
|
+
" });",
|
|
1800
|
+
' if (key == "remove") return gea_cpp_value([node]() -> gea_cpp_value {',
|
|
1801
|
+
" node.remove();",
|
|
1802
|
+
" return gea_cpp_value::missing();",
|
|
1803
|
+
" });",
|
|
1804
|
+
" if (node.hasAttribute(key.c_str())) return gea_cpp_value(std::string(node.getAttribute(key.c_str())));",
|
|
1805
|
+
" return gea_cpp_value::missing();",
|
|
1806
|
+
" },",
|
|
1807
|
+
" [node](const std::string &key, gea_cpp_value assigned_value) {",
|
|
1808
|
+
" const std::string value = nodeStringArg(assigned_value);",
|
|
1809
|
+
' if (key == "className") {',
|
|
1810
|
+
" node.classList().set(value);",
|
|
1811
|
+
" return;",
|
|
1812
|
+
" }",
|
|
1813
|
+
' if (key == "textContent" || key == "innerText") {',
|
|
1814
|
+
" node.setText(value.c_str());",
|
|
1815
|
+
" return;",
|
|
1816
|
+
" }",
|
|
1817
|
+
" node.setAttribute(key.c_str(), value.c_str());",
|
|
1818
|
+
" },",
|
|
1819
|
+
" [node](const std::string &key) -> bool {",
|
|
1820
|
+
' return key == "__gea_node_id" || key == "nodeType" || key == "classList" || key == "className" || key == "style" ||',
|
|
1821
|
+
' key == "setAttribute" || key == "getAttribute" || key == "hasAttribute" || key == "addEventListener" ||',
|
|
1822
|
+
' key == "appendChild" || key == "remove" || key == "cloneNode" || key == "play" || key == "pause" || key == "firstChild" ||',
|
|
1823
|
+
' key == "childNodes" || key == "children" || key == "getContext" || node.hasAttribute(key.c_str());',
|
|
1824
|
+
" });",
|
|
1825
|
+
"}",
|
|
1826
|
+
"",
|
|
1827
|
+
"// DOM Node accessor properties on a concrete NodeHandle receiver. geatsc lowers",
|
|
1828
|
+
"// `node.nodeType` / `node.parentNode` to these (registered as the gea host's",
|
|
1829
|
+
"// nativeMemberPropertyGetters) instead of a struct member, since NodeHandle is a",
|
|
1830
|
+
"// lightweight tree id with no such fields. Mirrors the gea_cpp_value record",
|
|
1831
|
+
"// protocol (nodeValue's record_get) so both the typed-handle and value-bridged",
|
|
1832
|
+
"// node representations resolve these properties identically.",
|
|
1833
|
+
"inline double domNodeType(gea::embedded::ui::NodeHandle node) {",
|
|
1834
|
+
" if (!node) return 0.0;",
|
|
1835
|
+
" const auto &__gea_tree = gea::embedded::ui::Tree::instance();",
|
|
1836
|
+
" const auto &__gea_n = __gea_tree.node(node.id());",
|
|
1837
|
+
' if (std::string(gea::embedded::ui::tagFromId(__gea_n.tag_id)) == "#comment") return 8.0;',
|
|
1838
|
+
" if (__gea_n.type == gea::embedded::ui::NodeType::Text) return 3.0;",
|
|
1839
|
+
" const std::string __gea_tag = gea::embedded::ui::tagFromId(__gea_n.tag_id);",
|
|
1840
|
+
' if (__gea_tag == "#fragment" || __gea_tag == "#document-frag" || __gea_tag == "#document-fragment") return 11.0;',
|
|
1841
|
+
" return 1.0;",
|
|
1842
|
+
"}",
|
|
1843
|
+
"",
|
|
1844
|
+
"inline gea_cpp_value domParentNode(gea::embedded::ui::NodeHandle node) {",
|
|
1845
|
+
" if (!node) return gea_cpp_value::null_v();",
|
|
1846
|
+
" const auto &__gea_tree = gea::embedded::ui::Tree::instance();",
|
|
1847
|
+
" if (node.id() < 0 || node.id() >= __gea_tree.nodeCount()) return gea_cpp_value::null_v();",
|
|
1848
|
+
" const int __gea_parent = __gea_tree.node(node.id()).parent;",
|
|
1849
|
+
" gea::embedded::ui::NodeHandle parent = __gea_parent >= 0 ? gea::embedded::ui::NodeHandle(__gea_parent) : gea::embedded::ui::NodeHandle();",
|
|
1850
|
+
" return parent.valid() ? nodeValue(parent) : gea_cpp_value::null_v();",
|
|
1851
|
+
"}",
|
|
1852
|
+
"",
|
|
1853
|
+
"// Typed DOM property setters used by generated JSX template factories.",
|
|
1854
|
+
"inline void domSetInnerHtml(gea::embedded::ui::NodeHandle node, const std::string &value) {",
|
|
1855
|
+
" gea::runtime::host::domSetInnerHtml(node, value);",
|
|
1856
|
+
"}",
|
|
1857
|
+
"",
|
|
1858
|
+
"inline void domSetTextContent(gea::embedded::ui::NodeHandle node, const std::string &value) {",
|
|
1859
|
+
" if (node) node.setText(value.c_str());",
|
|
1860
|
+
"}",
|
|
1861
|
+
"",
|
|
1862
|
+
"// Same accessors for a value-bridged node. The binding matches DOM interface",
|
|
1863
|
+
"// names too, so a node typed `Element`/`Node` but stored as gea_cpp_value (a",
|
|
1864
|
+
"// value-bridged handle) routes here; recover the tree id and forward.",
|
|
1865
|
+
"inline double domNodeType(const gea_cpp_value &node) {",
|
|
1866
|
+
" return domNodeType(gea::embedded::ui::NodeHandle(nodeIdFromValue(node)));",
|
|
1867
|
+
"}",
|
|
1868
|
+
"",
|
|
1869
|
+
"// DOM `Node.appendChild` RETURNS the appended child. Mirror that so the call",
|
|
1870
|
+
"// can be used as a value (e.g. a JSX child-append boxed in a conditional —",
|
|
1871
|
+
"// `cond ? gea_cpp_key(domAppendChild(...)) : …`); returning void there would",
|
|
1872
|
+
"// box an incomplete `void` type. In statement context the returned handle is",
|
|
1873
|
+
"// simply ignored.",
|
|
1874
|
+
"inline gea_cpp_value domAppendChild(gea::embedded::ui::NodeHandle parent, const gea_cpp_value &child) {",
|
|
1875
|
+
" const int child_id = nodeIdFromValue(child);",
|
|
1876
|
+
" if (child_id >= 0) parent.appendChild(gea::embedded::ui::NodeHandle(child_id));",
|
|
1877
|
+
" return child;",
|
|
1878
|
+
"}",
|
|
1879
|
+
"",
|
|
1880
|
+
"inline gea::embedded::ui::NodeHandle domAppendChild(gea::embedded::ui::NodeHandle parent, gea::embedded::ui::NodeHandle child) {",
|
|
1881
|
+
" if (child.valid()) parent.appendChild(child);",
|
|
1882
|
+
" return child;",
|
|
1883
|
+
"}",
|
|
1884
|
+
"",
|
|
1885
|
+
"// Value-bridged parent (a `Node`/`DocumentFragment` stored as gea_cpp_value —",
|
|
1886
|
+
"// e.g. a keyed-list fragment): recover the tree id and forward, mirroring the",
|
|
1887
|
+
"// `domNodeType(const gea_cpp_value&)` delegation. Without these overloads a",
|
|
1888
|
+
"// `domAppendChild(frag, child)` where `frag` is a gea_cpp_value finds no viable",
|
|
1889
|
+
"// candidate (neither NodeHandle-parent overload accepts a gea_cpp_value).",
|
|
1890
|
+
"inline gea_cpp_value domAppendChild(const gea_cpp_value &parent, const gea_cpp_value &child) {",
|
|
1891
|
+
" return domAppendChild(gea::embedded::ui::NodeHandle(nodeIdFromValue(parent)), child);",
|
|
1892
|
+
"}",
|
|
1893
|
+
"",
|
|
1894
|
+
"inline gea_cpp_value domAppendChild(const gea_cpp_value &parent, gea::embedded::ui::NodeHandle child) {",
|
|
1895
|
+
" domAppendChild(gea::embedded::ui::NodeHandle(nodeIdFromValue(parent)), child);",
|
|
1896
|
+
" return nodeValue(child);",
|
|
1897
|
+
"}",
|
|
1898
|
+
"",
|
|
1899
|
+
"inline gea_cpp_value domParentNode(const gea_cpp_value &node) {",
|
|
1900
|
+
" return domParentNode(gea::embedded::ui::NodeHandle(nodeIdFromValue(node)));",
|
|
1901
|
+
"}",
|
|
1902
|
+
"",
|
|
1903
|
+
"// `node.ownerDocument`: one embedded document, boxed so a capability probe",
|
|
1904
|
+
"// (record_get('createDocumentFragment')) on the result resolves.",
|
|
1905
|
+
"inline gea_cpp_value domOwnerDocument(gea::embedded::ui::NodeHandle) {",
|
|
1906
|
+
" return gea_cpp_key(gea::runtime::host::document());",
|
|
1907
|
+
"}",
|
|
1908
|
+
"",
|
|
1909
|
+
"inline gea_cpp_value domOwnerDocument(const gea_cpp_value &) {",
|
|
1910
|
+
" return gea_cpp_key(gea::runtime::host::document());",
|
|
1911
|
+
"}",
|
|
1912
|
+
"",
|
|
1913
|
+
"inline gea_cpp_value domFirstChild(gea::embedded::ui::NodeHandle node) {",
|
|
1914
|
+
" gea::embedded::ui::NodeHandle child = node.firstChildHandle();",
|
|
1915
|
+
" return child.valid() ? nodeValue(child) : gea_cpp_value::null_v();",
|
|
1916
|
+
"}",
|
|
1917
|
+
"",
|
|
1918
|
+
"inline gea_cpp_value domFirstChild(const gea_cpp_value &node) {",
|
|
1919
|
+
" return domFirstChild(gea::embedded::ui::NodeHandle(nodeIdFromValue(node)));",
|
|
1920
|
+
"}",
|
|
1921
|
+
"",
|
|
1922
|
+
"// `node.nextSibling`: typed NodeHandle (not boxed) so the compiled-component",
|
|
1923
|
+
"// keyed-list reconciler's `NodeHandle ref = entry.element.nextSibling` copy-init",
|
|
1924
|
+
"// and the following `container.insertBefore(ref, ...)` both stay native. An",
|
|
1925
|
+
"// invalid handle (no next sibling) is the DOM `null` reference insertBefore wants.",
|
|
1926
|
+
"inline gea::embedded::ui::NodeHandle domNextSibling(gea::embedded::ui::NodeHandle node) {",
|
|
1927
|
+
" return node.nextSiblingHandle();",
|
|
1928
|
+
"}",
|
|
1929
|
+
"",
|
|
1930
|
+
"inline gea::embedded::ui::NodeHandle domNextSibling(const gea_cpp_value &node) {",
|
|
1931
|
+
" return domNextSibling(gea::embedded::ui::NodeHandle(nodeIdFromValue(node)));",
|
|
1932
|
+
"}",
|
|
1933
|
+
"",
|
|
1934
|
+
...domStyleAccessorInteropSource(),
|
|
1935
|
+
"inline gea_cpp_value domChildNodes(gea::embedded::ui::NodeHandle node) {",
|
|
1936
|
+
" std::vector<gea_cpp_value> kids;",
|
|
1937
|
+
" for (int i = 0;; i++) {",
|
|
1938
|
+
" auto c = node.childAt(i);",
|
|
1939
|
+
" if (!c.valid()) break;",
|
|
1940
|
+
" kids.push_back(nodeValue(c));",
|
|
1941
|
+
" }",
|
|
1942
|
+
" return gea_cpp_value(kids);",
|
|
1943
|
+
"}",
|
|
1944
|
+
"",
|
|
1945
|
+
"inline gea_cpp_value domChildNodes(const gea_cpp_value &node) {",
|
|
1946
|
+
" return domChildNodes(gea::embedded::ui::NodeHandle(nodeIdFromValue(node)));",
|
|
1947
|
+
"}",
|
|
1948
|
+
"",
|
|
1949
|
+
"inline std::vector<gea::embedded::ui::NodeHandle> domChildren(gea::embedded::ui::NodeHandle node) {",
|
|
1950
|
+
" std::vector<gea::embedded::ui::NodeHandle> kids;",
|
|
1951
|
+
" const auto &__gea_tree = gea::embedded::ui::Tree::instance();",
|
|
1952
|
+
" for (int i = 0;; i++) {",
|
|
1953
|
+
" auto c = node.childAt(i);",
|
|
1954
|
+
" if (!c.valid()) break;",
|
|
1955
|
+
" const auto &__gea_child_node = __gea_tree.node(c.id());",
|
|
1956
|
+
" const std::string __gea_child_tag = gea::embedded::ui::tagFromId(__gea_child_node.tag_id);",
|
|
1957
|
+
' if (__gea_child_node.type == gea::embedded::ui::NodeType::Text || __gea_child_tag == "#comment" || __gea_child_tag == "#fragment" || __gea_child_tag == "#document-frag" || __gea_child_tag == "#document-fragment") continue;',
|
|
1958
|
+
" kids.push_back(c);",
|
|
1959
|
+
" }",
|
|
1960
|
+
" return kids;",
|
|
1961
|
+
"}",
|
|
1962
|
+
"",
|
|
1963
|
+
"inline gea_cpp_value eventTargetValue(gea::framework::events::EventTarget target) {",
|
|
1964
|
+
" return nodeValue(gea::embedded::ui::NodeHandle(target.id()));",
|
|
1965
|
+
"}",
|
|
1966
|
+
"",
|
|
1967
|
+
"inline gea_cpp_value touchPointValue(const gea::framework::events::TouchPoint &point) {",
|
|
1968
|
+
" gea_cpp_value out = gea_cpp_value::empty_record();",
|
|
1969
|
+
' out.record_set_literal("identifier", gea_cpp_value(static_cast<double>(point.identifier)));',
|
|
1970
|
+
' out.record_set_literal("target", eventTargetValue(point.target));',
|
|
1971
|
+
' out.record_set_literal("screenX", gea_cpp_value(static_cast<double>(point.screenX)));',
|
|
1972
|
+
' out.record_set_literal("screenY", gea_cpp_value(static_cast<double>(point.screenY)));',
|
|
1973
|
+
' out.record_set_literal("clientX", gea_cpp_value(static_cast<double>(point.clientX)));',
|
|
1974
|
+
' out.record_set_literal("clientY", gea_cpp_value(static_cast<double>(point.clientY)));',
|
|
1975
|
+
' out.record_set_literal("pageX", gea_cpp_value(static_cast<double>(point.pageX)));',
|
|
1976
|
+
' out.record_set_literal("pageY", gea_cpp_value(static_cast<double>(point.pageY)));',
|
|
1977
|
+
" return out;",
|
|
1978
|
+
"}",
|
|
1979
|
+
"",
|
|
1980
|
+
"inline gea_cpp_value touchPointArrayValue(const gea::framework::events::TouchPoint *points, int count) {",
|
|
1981
|
+
" std::vector<gea_cpp_value> items;",
|
|
1982
|
+
" if (count > 0) items.reserve(static_cast<std::size_t>(count));",
|
|
1983
|
+
" for (int index = 0; index < count; ++index) items.push_back(touchPointValue(points[index]));",
|
|
1984
|
+
" return gea_cpp_value::array(std::move(items));",
|
|
1985
|
+
"}",
|
|
1986
|
+
"",
|
|
1987
|
+
"inline gea_cpp_value pointerEventValue(gea::framework::events::PointerEvent &event) {",
|
|
1988
|
+
" // LAZY event object: build only the field a handler actually reads. The old",
|
|
1989
|
+
" // eager build allocated ~18 record fields PLUS the three touch arrays (each a",
|
|
1990
|
+
" // record per point + a node-target) on EVERY dispatch, once per move",
|
|
1991
|
+
" // (TouchMove) -- ~40 PSRAM allocations per move, measured at",
|
|
1992
|
+
" // ~4ms, the dominant finger-down frame-rate cost -- when typical handlers read",
|
|
1993
|
+
" // just clientX/clientY. The listener runs synchronously during dispatch, so",
|
|
1994
|
+
" // capturing &event by reference is valid for the object lifetime; reads route",
|
|
1995
|
+
" // through record_get -> object_get (value.cpp).",
|
|
1996
|
+
" return gea_cpp_value::object(",
|
|
1997
|
+
" reinterpret_cast<std::uintptr_t>(&event),",
|
|
1998
|
+
" [&event](const std::string &key) -> gea_cpp_value {",
|
|
1999
|
+
' if (key == "clientX") return gea_cpp_value(static_cast<double>(event.clientX));',
|
|
2000
|
+
' if (key == "clientY") return gea_cpp_value(static_cast<double>(event.clientY));',
|
|
2001
|
+
' if (key == "pointerId") return gea_cpp_value(static_cast<double>(event.pointerId));',
|
|
2002
|
+
' if (key == "x") return gea_cpp_value(static_cast<double>(event.x));',
|
|
2003
|
+
' if (key == "y") return gea_cpp_value(static_cast<double>(event.y));',
|
|
2004
|
+
' if (key == "pageX") return gea_cpp_value(static_cast<double>(event.pageX));',
|
|
2005
|
+
' if (key == "pageY") return gea_cpp_value(static_cast<double>(event.pageY));',
|
|
2006
|
+
' if (key == "screenX") return gea_cpp_value(static_cast<double>(event.screenX));',
|
|
2007
|
+
' if (key == "screenY") return gea_cpp_value(static_cast<double>(event.screenY));',
|
|
2008
|
+
' if (key == "pressId") return gea_cpp_value(static_cast<double>(event.pressId));',
|
|
2009
|
+
' if (key == "pressValue") return gea_cpp_value(static_cast<double>(event.pressValue));',
|
|
2010
|
+
' if (key == "delta") return gea_cpp_value(static_cast<double>(event.delta));',
|
|
2011
|
+
' if (key == "type") return gea_cpp_value(std::string(event.typeName()));',
|
|
2012
|
+
' if (key == "target") return eventTargetValue(event.target);',
|
|
2013
|
+
' if (key == "currentTarget") return eventTargetValue(event.currentTarget);',
|
|
2014
|
+
' if (key == "touches") return touchPointArrayValue(event.touches, event.touchesLength);',
|
|
2015
|
+
' if (key == "targetTouches") return touchPointArrayValue(event.targetTouches, event.targetTouchesLength);',
|
|
2016
|
+
' if (key == "changedTouches") return touchPointArrayValue(event.changedTouches, event.changedTouchesLength);',
|
|
2017
|
+
' if (key == "preventDefault") return gea_cpp_value([&event]() -> gea_cpp_value { event.preventDefault(); return gea_cpp_value::missing(); });',
|
|
2018
|
+
' if (key == "stopPropagation") return gea_cpp_value([&event]() -> gea_cpp_value { event.stopPropagation(); return gea_cpp_value::missing(); });',
|
|
2019
|
+
" return gea_cpp_value::missing();",
|
|
2020
|
+
" });",
|
|
2021
|
+
"}",
|
|
2022
|
+
"",
|
|
2023
|
+
...(usesCanvas
|
|
2024
|
+
? [
|
|
2025
|
+
"inline gea::embedded::ui::CanvasRenderingContext2D getCanvasContext(const gea_cpp_value &node, const std::string &kind) {",
|
|
2026
|
+
' if (kind != "2d") return gea::embedded::ui::CanvasRenderingContext2D();',
|
|
2027
|
+
" return gea::embedded::ui::CanvasElement(nodeIdFromValue(node)).getContext2D();",
|
|
2028
|
+
"}",
|
|
2029
|
+
"",
|
|
2030
|
+
"inline gea::embedded::ui::CanvasRenderingContext2D getCanvasContext(const gea_cpp_value &node, const std::string &kind, const gea_cpp_value &) {",
|
|
2031
|
+
" return getCanvasContext(node, kind);",
|
|
2032
|
+
"}",
|
|
2033
|
+
"",
|
|
2034
|
+
"inline gea::embedded::ui::CanvasRenderingContext2D getCanvasContext(gea::embedded::ui::NodeHandle node, const std::string &kind) {",
|
|
2035
|
+
' if (kind != "2d") return gea::embedded::ui::CanvasRenderingContext2D();',
|
|
2036
|
+
" return gea::embedded::ui::CanvasElement(node.id()).getContext2D();",
|
|
2037
|
+
"}",
|
|
2038
|
+
"",
|
|
2039
|
+
"inline gea::embedded::ui::CanvasRenderingContext2D getCanvasContext(gea::embedded::ui::NodeHandle node, const std::string &kind, const gea_cpp_value &) {",
|
|
2040
|
+
" return getCanvasContext(node, kind);",
|
|
2041
|
+
"}",
|
|
2042
|
+
"",
|
|
2043
|
+
// geatsc stores a NULLABLE typed handle (`Component<T>`\'s `el: T | null`)
|
|
2044
|
+
// as std::optional<NodeHandle>. Without these unwrapping overloads the
|
|
2045
|
+
// optional binds the generic gea_cpp_value parameter instead, boxing an
|
|
2046
|
+
// OPAQUE object whose record carries no __gea_node_id — the context then
|
|
2047
|
+
// binds node -1 and every draw silently no-ops (maps: black screen).
|
|
2048
|
+
"inline gea::embedded::ui::CanvasRenderingContext2D getCanvasContext(const std::optional<gea::embedded::ui::NodeHandle> &node, const std::string &kind) {",
|
|
2049
|
+
" return getCanvasContext(node.value_or(gea::embedded::ui::NodeHandle()), kind);",
|
|
2050
|
+
"}",
|
|
2051
|
+
"",
|
|
2052
|
+
"inline gea::embedded::ui::CanvasRenderingContext2D getCanvasContext(const std::optional<gea::embedded::ui::NodeHandle> &node, const std::string &kind, const gea_cpp_value &) {",
|
|
2053
|
+
" return getCanvasContext(node.value_or(gea::embedded::ui::NodeHandle()), kind);",
|
|
2054
|
+
"}",
|
|
2055
|
+
"",
|
|
2056
|
+
"inline double canvasNumber(double value) { return value; }",
|
|
2057
|
+
"inline double canvasNumber(float value) { return static_cast<double>(value); }",
|
|
2058
|
+
"inline double canvasNumber(int8_t value) { return static_cast<double>(value); }",
|
|
2059
|
+
"inline double canvasNumber(uint8_t value) { return static_cast<double>(value); }",
|
|
2060
|
+
"inline double canvasNumber(int16_t value) { return static_cast<double>(value); }",
|
|
2061
|
+
"inline double canvasNumber(uint16_t value) { return static_cast<double>(value); }",
|
|
2062
|
+
"inline double canvasNumber(int value) { return static_cast<double>(value); }",
|
|
2063
|
+
"inline double canvasNumber(uint32_t value) { return static_cast<double>(value); }",
|
|
2064
|
+
"inline double canvasNumber(long long value) { return static_cast<double>(value); }",
|
|
2065
|
+
"inline double canvasNumber(const gea_f32 &value) { return static_cast<double>(value.v); }",
|
|
2066
|
+
"inline double canvasNumber(const gea_cpp_value &value) { return gea::runtime::coerce::to_number(value); }",
|
|
2067
|
+
// int32_t/uint32_t are long/unsigned long on some toolchains (xtensa newlib), so plain
|
|
2068
|
+
// `long` never matches the named overloads there — the arithmetic branch keeps every
|
|
2069
|
+
// scalar width native instead of falling through to a boxed gea_cpp_value round-trip.
|
|
2070
|
+
"template <typename T>",
|
|
2071
|
+
"inline double canvasNumber(const T &value) {",
|
|
2072
|
+
" if constexpr (std::is_arithmetic_v<std::decay_t<T>>) return static_cast<double>(value);",
|
|
2073
|
+
" else return gea::runtime::coerce::to_number(gea_cpp_key(value));",
|
|
2074
|
+
"}",
|
|
2075
|
+
"",
|
|
2076
|
+
"inline int canvasRound(double value) {",
|
|
2077
|
+
" if (!std::isfinite(value)) return 0;",
|
|
2078
|
+
" return static_cast<int>(value + (value >= 0 ? 0.5 : -0.5));",
|
|
2079
|
+
"}",
|
|
2080
|
+
"inline int canvasInt(int8_t value) { return static_cast<int>(value); }",
|
|
2081
|
+
"inline int canvasInt(uint8_t value) { return static_cast<int>(value); }",
|
|
2082
|
+
"inline int canvasInt(int16_t value) { return static_cast<int>(value); }",
|
|
2083
|
+
"inline int canvasInt(uint16_t value) { return static_cast<int>(value); }",
|
|
2084
|
+
"inline int canvasInt(int value) { return value; }",
|
|
2085
|
+
"inline int canvasInt(uint32_t value) { return static_cast<int>(value); }",
|
|
2086
|
+
"inline int canvasInt(long long value) { return static_cast<int>(value); }",
|
|
2087
|
+
"inline int canvasInt(double value) { return canvasRound(value); }",
|
|
2088
|
+
"inline int canvasInt(float value) { return canvasRound(static_cast<double>(value)); }",
|
|
2089
|
+
"inline int canvasInt(const gea_f32 &value) { return canvasRound(static_cast<double>(value.v)); }",
|
|
2090
|
+
"inline int canvasInt(const gea_cpp_value &value) { return canvasRound(gea::runtime::coerce::to_number(value)); }",
|
|
2091
|
+
"template <typename T>",
|
|
2092
|
+
"inline int canvasInt(const T &value) {",
|
|
2093
|
+
" if constexpr (std::is_floating_point_v<std::decay_t<T>>) return canvasRound(static_cast<double>(value));",
|
|
2094
|
+
" else if constexpr (std::is_arithmetic_v<std::decay_t<T>>) return static_cast<int>(value);",
|
|
2095
|
+
" else return canvasRound(gea::runtime::coerce::to_number(gea_cpp_key(value)));",
|
|
2096
|
+
"}",
|
|
2097
|
+
"",
|
|
2098
|
+
"inline std::string canvasString(const std::string &value) { return value; }",
|
|
2099
|
+
"inline std::string canvasString(const char *value) { return value ? std::string(value) : std::string(); }",
|
|
2100
|
+
"inline std::string canvasString(const gea_cpp_value &value) { return gea_cpp_to_string(value); }",
|
|
2101
|
+
"template <typename T>",
|
|
2102
|
+
"inline std::string canvasString(const T &value) { return gea_cpp_to_string(gea_cpp_key(value)); }",
|
|
2103
|
+
"",
|
|
2104
|
+
"inline gea::framework::graphics::pixel::native_t canvasRgb565(gea::framework::graphics::pixel::NativeColor value) { return value.value; }",
|
|
2105
|
+
"inline gea::framework::graphics::pixel::native_t canvasRgb565(const gea_cpp_value &value) { return gea::framework::graphics::pixel::nativeFromRrggbbaa(static_cast<std::uint32_t>(gea::runtime::coerce::to_number(value))); }",
|
|
2106
|
+
"template <typename T>",
|
|
2107
|
+
"inline gea::framework::graphics::pixel::native_t canvasRgb565(const T &value) { return gea::framework::graphics::pixel::nativeFromRrggbbaa(static_cast<std::uint32_t>(canvasNumber(value))); }",
|
|
2108
|
+
"template <typename T>",
|
|
2109
|
+
"inline constexpr bool canvasColorValue = std::is_arithmetic_v<std::decay_t<T>> || std::is_same_v<std::decay_t<T>, gea::framework::graphics::pixel::NativeColor>;",
|
|
2110
|
+
"inline gea_cpp_value canvasAssignedValue(gea::framework::graphics::pixel::NativeColor value) { return gea_cpp_key(static_cast<double>(value.value)); }",
|
|
2111
|
+
"template <typename T>",
|
|
2112
|
+
"inline gea_cpp_value canvasAssignedValue(const T &value) { return gea_cpp_key(value); }",
|
|
2113
|
+
"",
|
|
2114
|
+
"inline int canvasImageId(const gea_cpp_value &value) {",
|
|
2115
|
+
" if (value.kind == gea_cpp_value::kind_t::number) return canvasInt(value);",
|
|
2116
|
+
' gea_cpp_value id = value.record_get_literal("id");',
|
|
2117
|
+
" if (!id.is_nullish()) return canvasInt(id);",
|
|
2118
|
+
" return -1;",
|
|
2119
|
+
"}",
|
|
2120
|
+
"template <typename T>",
|
|
2121
|
+
"inline int canvasImageId(const T &value) {",
|
|
2122
|
+
" if constexpr (requires { value.id; }) return canvasInt(value.id);",
|
|
2123
|
+
" else return canvasImageId(gea_cpp_key(value));",
|
|
2124
|
+
"}",
|
|
2125
|
+
"",
|
|
2126
|
+
"inline void canvasClear(gea::embedded::ui::CanvasRenderingContext2D &ctx) {",
|
|
2127
|
+
" ctx.clear();",
|
|
2128
|
+
"}",
|
|
2129
|
+
"",
|
|
2130
|
+
"template <typename X, typename Y, typename W, typename H>",
|
|
2131
|
+
"inline void canvasClearRect(gea::embedded::ui::CanvasRenderingContext2D &ctx, const X &x, const Y &y, const W &w, const H &h) {",
|
|
2132
|
+
" ctx.clearRect(canvasInt(x), canvasInt(y), canvasInt(w), canvasInt(h));",
|
|
2133
|
+
"}",
|
|
2134
|
+
"",
|
|
2135
|
+
"template <typename X, typename Y, typename W, typename H>",
|
|
2136
|
+
"inline void canvasFillRect(gea::embedded::ui::CanvasRenderingContext2D &ctx, const X &x, const Y &y, const W &w, const H &h) {",
|
|
2137
|
+
" ctx.fillRect(canvasInt(x), canvasInt(y), canvasInt(w), canvasInt(h));",
|
|
2138
|
+
"}",
|
|
2139
|
+
"",
|
|
2140
|
+
"template <typename X, typename Y, typename W, typename H>",
|
|
2141
|
+
"inline void canvasStrokeRect(gea::embedded::ui::CanvasRenderingContext2D &ctx, const X &x, const Y &y, const W &w, const H &h) {",
|
|
2142
|
+
" ctx.strokeRect(canvasInt(x), canvasInt(y), canvasInt(w), canvasInt(h));",
|
|
2143
|
+
"}",
|
|
2144
|
+
"",
|
|
2145
|
+
"template <typename X, typename Y, typename R, typename Fill>",
|
|
2146
|
+
"inline void canvasFillCircle(gea::embedded::ui::CanvasRenderingContext2D &ctx, const X &x, const Y &y, const R &r, const Fill &fill) {",
|
|
2147
|
+
" if constexpr (canvasColorValue<Fill>) {",
|
|
2148
|
+
" ctx.fillCircleRgb565(canvasInt(x), canvasInt(y), canvasInt(r), canvasRgb565(fill));",
|
|
2149
|
+
" } else {",
|
|
2150
|
+
" ctx.setFillStyle(canvasString(fill));",
|
|
2151
|
+
" ctx.fillCircle(canvasInt(x), canvasInt(y), canvasInt(r));",
|
|
2152
|
+
" }",
|
|
2153
|
+
"}",
|
|
2154
|
+
"",
|
|
2155
|
+
"template <typename X, typename Y, typename R>",
|
|
2156
|
+
"inline void canvasFillCircle(gea::embedded::ui::CanvasRenderingContext2D &ctx, const X &x, const Y &y, const R &r, const gea_cpp_value &fill) {",
|
|
2157
|
+
" if (fill.kind == gea_cpp_value::kind_t::number) {",
|
|
2158
|
+
" ctx.fillCircleRgb565(canvasInt(x), canvasInt(y), canvasInt(r), canvasRgb565(fill.number));",
|
|
2159
|
+
" } else {",
|
|
2160
|
+
" ctx.setFillStyle(canvasString(fill));",
|
|
2161
|
+
" ctx.fillCircle(canvasInt(x), canvasInt(y), canvasInt(r));",
|
|
2162
|
+
" }",
|
|
2163
|
+
"}",
|
|
2164
|
+
"",
|
|
2165
|
+
"template <typename X, typename Y, typename R>",
|
|
2166
|
+
"inline void canvasStrokeCircle(gea::embedded::ui::CanvasRenderingContext2D &ctx, const X &x, const Y &y, const R &r) {",
|
|
2167
|
+
" ctx.strokeCircle(canvasInt(x), canvasInt(y), canvasInt(r));",
|
|
2168
|
+
"}",
|
|
2169
|
+
"",
|
|
2170
|
+
"template <typename X, typename Y, typename R, typename Fill>",
|
|
2171
|
+
"inline void canvasFillCircleRgb565(gea::embedded::ui::CanvasRenderingContext2D &ctx, const X &x, const Y &y, const R &r, const Fill &fill) {",
|
|
2172
|
+
" ctx.fillCircleRgb565(canvasInt(x), canvasInt(y), canvasInt(r), canvasRgb565(fill));",
|
|
2173
|
+
"}",
|
|
2174
|
+
"",
|
|
2175
|
+
"template <typename X0, typename Y0, typename X1, typename Y1, typename X2, typename Y2, typename Fill>",
|
|
2176
|
+
"inline void canvasFillTriangleRgb565(gea::embedded::ui::CanvasRenderingContext2D &ctx, const X0 &x0, const Y0 &y0, const X1 &x1, const Y1 &y1, const X2 &x2, const Y2 &y2, const Fill &fill) {",
|
|
2177
|
+
" ctx.fillTriangleRgb565(canvasInt(x0), canvasInt(y0), canvasInt(x1), canvasInt(y1), canvasInt(x2), canvasInt(y2), canvasRgb565(fill));",
|
|
2178
|
+
"}",
|
|
2179
|
+
"",
|
|
2180
|
+
"template <typename XS, typename YS, typename R, typename Colors>",
|
|
2181
|
+
"inline void canvasFillCirclesRgb565(gea::embedded::ui::CanvasRenderingContext2D &ctx, const XS &xs, const YS &ys, const R &r, const Colors &colors, int count = -1) {",
|
|
2182
|
+
" // Convert each colour the same way the scalar path does (canvasRgb565):",
|
|
2183
|
+
" // a plain number (e.g. from rgb() / Uint32Array) is authored 0xRRGGBBAA,",
|
|
2184
|
+
" // while rgb565() values carry the NativeColor marker and pass through",
|
|
2185
|
+
" // unchanged. `count` (>=0) limits to the first N triples so callers can",
|
|
2186
|
+
" // pass a reused fixed-size scratch array without slicing; <0 uses all.",
|
|
2187
|
+
" using __gea_color_t = std::decay_t<typename Colors::value_type>;",
|
|
2188
|
+
" if constexpr (std::is_same_v<__gea_color_t, gea::framework::graphics::pixel::native_t> || std::is_same_v<__gea_color_t, gea::framework::graphics::pixel::NativeColor>) {",
|
|
2189
|
+
" if constexpr (requires { xs.data(); ys.data(); colors.data(); } && std::is_same_v<std::decay_t<decltype(*xs.data())>, std::uint16_t> && std::is_same_v<std::decay_t<decltype(*ys.data())>, std::uint16_t>) {",
|
|
2190
|
+
" // Pointer path. Passing the containers themselves binds a typed array",
|
|
2191
|
+
" // through its implicit operator std::vector<Element>(), which heap-",
|
|
2192
|
+
" // allocates and copies the FULL array on every call while ignoring",
|
|
2193
|
+
" // `count` -- ~150us per call on esp32-s3 regardless of how many",
|
|
2194
|
+
" // circles it carried.",
|
|
2195
|
+
" std::size_t __gea_n = xs.size() < ys.size() ? xs.size() : ys.size();",
|
|
2196
|
+
" if (colors.size() < __gea_n) __gea_n = colors.size();",
|
|
2197
|
+
" if (count >= 0 && static_cast<std::size_t>(count) < __gea_n) __gea_n = static_cast<std::size_t>(count);",
|
|
2198
|
+
" ctx.fillCirclesRgb565(xs.data(), ys.data(), canvasInt(r), colors.data(), static_cast<int>(__gea_n));",
|
|
2199
|
+
" } else {",
|
|
2200
|
+
" ctx.fillCirclesRgb565(xs, ys, canvasInt(r), colors, count);",
|
|
2201
|
+
" }",
|
|
2202
|
+
" } else {",
|
|
2203
|
+
" std::size_t __gea_n = colors.size();",
|
|
2204
|
+
" if (count >= 0 && static_cast<std::size_t>(count) < __gea_n) __gea_n = static_cast<std::size_t>(count);",
|
|
2205
|
+
" std::vector<gea::framework::graphics::pixel::native_t> __gea_native_colors;",
|
|
2206
|
+
" __gea_native_colors.reserve(__gea_n);",
|
|
2207
|
+
" for (std::size_t __gea_i = 0; __gea_i < __gea_n; ++__gea_i) __gea_native_colors.push_back(canvasRgb565(colors[__gea_i]));",
|
|
2208
|
+
" ctx.fillCirclesRgb565(xs, ys, canvasInt(r), __gea_native_colors, count);",
|
|
2209
|
+
" }",
|
|
2210
|
+
"}",
|
|
2211
|
+
"",
|
|
2212
|
+
"template <typename XS, typename YS, typename R, typename Fill>",
|
|
2213
|
+
"inline void canvasFillCirclesRgb565Uniform(gea::embedded::ui::CanvasRenderingContext2D &ctx, const XS &xs, const YS &ys, const R &r, const Fill &fill) {",
|
|
2214
|
+
" ctx.fillCirclesRgb565(xs, ys, canvasInt(r), canvasRgb565(fill));",
|
|
2215
|
+
"}",
|
|
2216
|
+
"",
|
|
2217
|
+
"template <typename X0s, typename Y0s, typename X1s, typename Y1s, typename X2s, typename Y2s, typename Colors, typename Order>",
|
|
2218
|
+
"inline void canvasFillTrianglesRgb565Sorted(gea::embedded::ui::CanvasRenderingContext2D &ctx, const X0s &x0s, const Y0s &y0s, const X1s &x1s, const Y1s &y1s, const X2s &x2s, const Y2s &y2s, const Colors &colors, const Order &order, int count = -1) {",
|
|
2219
|
+
" // Colours are authored 0xRRGGBBAA (plain numbers, e.g. a Uint32Array);",
|
|
2220
|
+
" // the canvas layer converts during packing. `order` gives the paint",
|
|
2221
|
+
" // (depth) order into the coordinate/colour arrays; `count` (>=0) limits",
|
|
2222
|
+
" // to the first N entries so callers can reuse fixed-size scratch arrays.",
|
|
2223
|
+
" ctx.fillTrianglesRgb565Sorted(x0s, y0s, x1s, y1s, x2s, y2s, colors, order, count);",
|
|
2224
|
+
"}",
|
|
2225
|
+
"",
|
|
2226
|
+
"inline void canvasBeginPath(gea::embedded::ui::CanvasRenderingContext2D &ctx) {",
|
|
2227
|
+
" ctx.beginPath();",
|
|
2228
|
+
"}",
|
|
2229
|
+
"",
|
|
2230
|
+
"template <typename X, typename Y, typename R, typename Start, typename End>",
|
|
2231
|
+
"inline void canvasArc(gea::embedded::ui::CanvasRenderingContext2D &ctx, const X &x, const Y &y, const R &r, const Start &start, const End &end) {",
|
|
2232
|
+
" ctx.arc(canvasNumber(x), canvasNumber(y), canvasNumber(r), canvasNumber(start), canvasNumber(end));",
|
|
2233
|
+
"}",
|
|
2234
|
+
"",
|
|
2235
|
+
"template <typename X, typename Y>",
|
|
2236
|
+
"inline void canvasMoveTo(gea::embedded::ui::CanvasRenderingContext2D &ctx, const X &x, const Y &y) {",
|
|
2237
|
+
" ctx.moveTo(canvasNumber(x), canvasNumber(y));",
|
|
2238
|
+
"}",
|
|
2239
|
+
"",
|
|
2240
|
+
"template <typename X, typename Y>",
|
|
2241
|
+
"inline void canvasLineTo(gea::embedded::ui::CanvasRenderingContext2D &ctx, const X &x, const Y &y) {",
|
|
2242
|
+
" ctx.lineTo(canvasNumber(x), canvasNumber(y));",
|
|
2243
|
+
"}",
|
|
2244
|
+
"",
|
|
2245
|
+
"inline void canvasClosePath(gea::embedded::ui::CanvasRenderingContext2D &ctx) {",
|
|
2246
|
+
" ctx.closePath();",
|
|
2247
|
+
"}",
|
|
2248
|
+
"",
|
|
2249
|
+
"inline void canvasFill(gea::embedded::ui::CanvasRenderingContext2D &ctx) {",
|
|
2250
|
+
" ctx.fill();",
|
|
2251
|
+
"}",
|
|
2252
|
+
"",
|
|
2253
|
+
"inline void canvasStroke(gea::embedded::ui::CanvasRenderingContext2D &ctx) {",
|
|
2254
|
+
" ctx.stroke();",
|
|
2255
|
+
"}",
|
|
2256
|
+
"",
|
|
2257
|
+
"template <typename Text, typename X, typename Y>",
|
|
2258
|
+
"inline void canvasFillText(gea::embedded::ui::CanvasRenderingContext2D &ctx, const Text &text, const X &x, const Y &y) {",
|
|
2259
|
+
" ctx.fillText(canvasString(text), canvasInt(x), canvasInt(y));",
|
|
2260
|
+
"}",
|
|
2261
|
+
"",
|
|
2262
|
+
"template <typename Image, typename X, typename Y>",
|
|
2263
|
+
"inline void canvasDrawImage(gea::embedded::ui::CanvasRenderingContext2D &ctx, const Image &image, const X &x, const Y &y) {",
|
|
2264
|
+
" ctx.drawImage(canvasImageId(image), canvasInt(x), canvasInt(y));",
|
|
2265
|
+
"}",
|
|
2266
|
+
"",
|
|
2267
|
+
"template <typename Image, typename X, typename Y, typename W, typename H>",
|
|
2268
|
+
"inline void canvasDrawImage(gea::embedded::ui::CanvasRenderingContext2D &ctx, const Image &image, const X &x, const Y &y, const W &w, const H &h) {",
|
|
2269
|
+
" ctx.drawImage(canvasImageId(image), canvasInt(x), canvasInt(y), canvasInt(w), canvasInt(h));",
|
|
2270
|
+
"}",
|
|
2271
|
+
"",
|
|
2272
|
+
"template <typename Text>",
|
|
2273
|
+
"inline double canvasMeasureTextInkCenter(gea::embedded::ui::CanvasRenderingContext2D &ctx, const Text &text) {",
|
|
2274
|
+
" return ctx.measureTextInkCenter(canvasString(text));",
|
|
2275
|
+
"}",
|
|
2276
|
+
"",
|
|
2277
|
+
"template <typename Text>",
|
|
2278
|
+
"inline double canvasMeasureText(gea::embedded::ui::CanvasRenderingContext2D &ctx, const Text &text) {",
|
|
2279
|
+
" return ctx.measureText(canvasString(text));",
|
|
2280
|
+
"}",
|
|
2281
|
+
"",
|
|
2282
|
+
"template <typename Image, typename X, typename Y, typename W, typename H>",
|
|
2283
|
+
"inline void canvasDrawImageCircle(gea::embedded::ui::CanvasRenderingContext2D &ctx, const Image &image, const X &x, const Y &y, const W &w, const H &h) {",
|
|
2284
|
+
" ctx.drawImageCircle(canvasImageId(image), canvasInt(x), canvasInt(y), canvasInt(w), canvasInt(h));",
|
|
2285
|
+
"}",
|
|
2286
|
+
"",
|
|
2287
|
+
"template <typename Image, typename X, typename Y, typename W, typename H>",
|
|
2288
|
+
"inline void canvasDrawImageRotated90CW(gea::embedded::ui::CanvasRenderingContext2D &ctx, const Image &image, const X &x, const Y &y, const W &w, const H &h) {",
|
|
2289
|
+
" ctx.drawImageRotated90CW(canvasImageId(image), canvasInt(x), canvasInt(y), canvasInt(w), canvasInt(h));",
|
|
2290
|
+
"}",
|
|
2291
|
+
"",
|
|
2292
|
+
"template <typename Image, typename X, typename Y, typename W>",
|
|
2293
|
+
"inline void canvasDrawImageTiledX(gea::embedded::ui::CanvasRenderingContext2D &ctx, const Image &image, const X &x, const Y &y, const W &w) {",
|
|
2294
|
+
" ctx.drawImageTiledX(canvasImageId(image), canvasInt(x), canvasInt(y), canvasInt(w));",
|
|
2295
|
+
"}",
|
|
2296
|
+
"",
|
|
2297
|
+
"inline void canvasFlush(gea::embedded::ui::CanvasRenderingContext2D &ctx) {",
|
|
2298
|
+
" ctx.flush();",
|
|
2299
|
+
"}",
|
|
2300
|
+
"",
|
|
2301
|
+
"inline void canvasBeginBatch(gea::embedded::ui::CanvasRenderingContext2D &ctx) {",
|
|
2302
|
+
" ctx.beginBatch();",
|
|
2303
|
+
"}",
|
|
2304
|
+
"",
|
|
2305
|
+
"inline void canvasEndBatch(gea::embedded::ui::CanvasRenderingContext2D &ctx) {",
|
|
2306
|
+
" ctx.endBatch();",
|
|
2307
|
+
"}",
|
|
2308
|
+
"",
|
|
2309
|
+
"template <typename Value>",
|
|
2310
|
+
"inline gea_cpp_value canvasSetFillStyle(gea::embedded::ui::CanvasRenderingContext2D &ctx, const Value &value) {",
|
|
2311
|
+
" if constexpr (canvasColorValue<Value>) ctx.setFillStyleRgb565(canvasRgb565(value));",
|
|
2312
|
+
" else ctx.setFillStyle(canvasString(value));",
|
|
2313
|
+
" return canvasAssignedValue(value);",
|
|
2314
|
+
"}",
|
|
2315
|
+
"",
|
|
2316
|
+
"inline gea_cpp_value canvasSetFillStyle(gea::embedded::ui::CanvasRenderingContext2D &ctx, const gea_cpp_value &value) {",
|
|
2317
|
+
" if (value.kind == gea_cpp_value::kind_t::number) ctx.setFillStyleRgb565(canvasRgb565(value.number));",
|
|
2318
|
+
" else ctx.setFillStyle(canvasString(value));",
|
|
2319
|
+
" return value;",
|
|
2320
|
+
"}",
|
|
2321
|
+
"",
|
|
2322
|
+
"inline gea_cpp_value canvasSetFillStyleRgb565(gea::embedded::ui::CanvasRenderingContext2D &ctx, gea::framework::graphics::pixel::native_t value) {",
|
|
2323
|
+
" ctx.setFillStyleRgb565(value);",
|
|
2324
|
+
" return gea_cpp_value(static_cast<double>(value));",
|
|
2325
|
+
"}",
|
|
2326
|
+
"",
|
|
2327
|
+
"template <typename Value>",
|
|
2328
|
+
"inline gea_cpp_value canvasSetStrokeStyle(gea::embedded::ui::CanvasRenderingContext2D &ctx, const Value &value) {",
|
|
2329
|
+
" if constexpr (canvasColorValue<Value>) ctx.setStrokeStyleRgb565(canvasRgb565(value));",
|
|
2330
|
+
" else ctx.setStrokeStyle(canvasString(value));",
|
|
2331
|
+
" return canvasAssignedValue(value);",
|
|
2332
|
+
"}",
|
|
2333
|
+
"",
|
|
2334
|
+
"inline gea_cpp_value canvasSetStrokeStyle(gea::embedded::ui::CanvasRenderingContext2D &ctx, const gea_cpp_value &value) {",
|
|
2335
|
+
" if (value.kind == gea_cpp_value::kind_t::number) ctx.setStrokeStyleRgb565(canvasRgb565(value.number));",
|
|
2336
|
+
" else ctx.setStrokeStyle(canvasString(value));",
|
|
2337
|
+
" return value;",
|
|
2338
|
+
"}",
|
|
2339
|
+
"",
|
|
2340
|
+
"template <typename Value>",
|
|
2341
|
+
"inline gea_cpp_value canvasSetGlobalAlpha(gea::embedded::ui::CanvasRenderingContext2D &ctx, const Value &value) {",
|
|
2342
|
+
" ctx.setGlobalAlpha(canvasNumber(value));",
|
|
2343
|
+
" return gea_cpp_key(value);",
|
|
2344
|
+
"}",
|
|
2345
|
+
"",
|
|
2346
|
+
"template <typename Value>",
|
|
2347
|
+
"inline gea_cpp_value canvasSetLineWidth(gea::embedded::ui::CanvasRenderingContext2D &ctx, const Value &value) {",
|
|
2348
|
+
" ctx.setLineWidth(canvasNumber(value));",
|
|
2349
|
+
" return gea_cpp_key(value);",
|
|
2350
|
+
"}",
|
|
2351
|
+
"",
|
|
2352
|
+
"template <typename Value>",
|
|
2353
|
+
"inline gea_cpp_value canvasSetFont(gea::embedded::ui::CanvasRenderingContext2D &ctx, const Value &value) {",
|
|
2354
|
+
" ctx.setFont(canvasString(value));",
|
|
2355
|
+
" return gea_cpp_key(value);",
|
|
2356
|
+
"}",
|
|
2357
|
+
"",
|
|
2358
|
+
"template <typename Value>",
|
|
2359
|
+
"inline gea_cpp_value canvasSetTextBaseline(gea::embedded::ui::CanvasRenderingContext2D &ctx, const Value &value) {",
|
|
2360
|
+
" ctx.setTextBaseline(canvasString(value));",
|
|
2361
|
+
" return gea_cpp_key(value);",
|
|
2362
|
+
"}",
|
|
2363
|
+
"",
|
|
2364
|
+
"template <typename Value>",
|
|
2365
|
+
"inline gea_cpp_value canvasSetTextAlign(gea::embedded::ui::CanvasRenderingContext2D &ctx, const Value &value) {",
|
|
2366
|
+
" ctx.setTextAlign(canvasString(value));",
|
|
2367
|
+
" return gea_cpp_key(value);",
|
|
2368
|
+
"}",
|
|
2369
|
+
"",
|
|
2370
|
+
]
|
|
2371
|
+
: []),
|
|
2372
|
+
"// (global scope) Boxing a NodeHandle must produce the FULL node value with",
|
|
2373
|
+
"// the DOM method table (cloneNode/firstChild/childNodes/appendChild/...).",
|
|
2374
|
+
"// Compiled templates pass typed handles across boxed boundaries (appendChild",
|
|
2375
|
+
"// on the mount root, the component GEA_ELEMENT slot); the generic templated",
|
|
2376
|
+
"// gea_cpp_key boxes them as opaque objects that answer NO node protocol, so",
|
|
2377
|
+
"// the tree never mounts and the screen stays black. Declared OUTSIDE",
|
|
2378
|
+
"// namespace gea_ir: an in-namespace overload would hide the global",
|
|
2379
|
+
"// gea_cpp_key overload set from every later in-namespace helper.",
|
|
2380
|
+
"} // namespace gea_ir (reopened below)",
|
|
2381
|
+
"static gea_cpp_value gea_cpp_key(const gea::embedded::ui::NodeHandle &node) {",
|
|
2382
|
+
" return gea_ir::nodeValue(node);",
|
|
2383
|
+
"}",
|
|
2384
|
+
"// Boxing bridge for native pointer events: lets geatsc's duck-binding",
|
|
2385
|
+
"// adapt a `std::function<void(gea_cpp_value)>` record slot to a host",
|
|
2386
|
+
"// `EventListener` (std::function<void(PointerEvent&)>) by wrapping the",
|
|
2387
|
+
"// callback in a generic lambda that boxes the event (`document as",
|
|
2388
|
+
"// EventSourceLike` in canvas-runtime apps).",
|
|
2389
|
+
"static gea_cpp_value gea_cpp_key(gea::framework::events::PointerEvent &event) {",
|
|
2390
|
+
" return gea_ir::pointerEventValue(event);",
|
|
2391
|
+
"}",
|
|
2392
|
+
"namespace gea_ir {",
|
|
2393
|
+
"",
|
|
2394
|
+
];
|
|
2395
|
+
}
|
|
2396
|
+
function domStyleInteropSource() {
|
|
2397
|
+
return [
|
|
2398
|
+
"inline int styleDisplayValue(const std::string &value) {",
|
|
2399
|
+
' if (value == "none") return gea::embedded::ui::kDisplayNone;',
|
|
2400
|
+
' if (value == "grid") return gea::embedded::ui::kDisplayGrid;',
|
|
2401
|
+
' if (value == "flex") return gea::embedded::ui::kDisplayFlex;',
|
|
2402
|
+
" return gea::embedded::ui::kDisplayBlock;",
|
|
2403
|
+
"}",
|
|
2404
|
+
"",
|
|
2405
|
+
"inline int styleDisplayValue(const char *value) {",
|
|
2406
|
+
" return styleDisplayValue(value ? std::string(value) : std::string());",
|
|
2407
|
+
"}",
|
|
2408
|
+
"",
|
|
2409
|
+
"inline int styleDisplayValue(const gea_cpp_value &value) {",
|
|
2410
|
+
" return styleDisplayValue(value.is_nullish() ? std::string() : gea_cpp_to_string(value));",
|
|
2411
|
+
"}",
|
|
2412
|
+
"",
|
|
2413
|
+
"template <typename T>",
|
|
2414
|
+
"inline int styleDisplayValue(const T &value) {",
|
|
2415
|
+
" if constexpr (std::is_arithmetic_v<std::decay_t<T>>) return static_cast<int>(value);",
|
|
2416
|
+
" else return styleDisplayValue(gea_cpp_to_string(gea_cpp_key(value)));",
|
|
2417
|
+
"}",
|
|
2418
|
+
];
|
|
2419
|
+
}
|
|
2420
|
+
function cppNamespace(value) {
|
|
2421
|
+
const parts = value
|
|
2422
|
+
.split("::")
|
|
2423
|
+
.map((part) => sanitizeCppIdentifier(part.trim()))
|
|
2424
|
+
.filter(Boolean);
|
|
2425
|
+
return parts.length > 0 ? parts.join("::") : "gea::framework::app::generated";
|
|
2426
|
+
}
|