@geastack/geatsc-plugin-gea 0.1.11 → 0.1.13

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.
@@ -0,0 +1 @@
1
+ export declare function inferCanvasOnly(sources: ReadonlyMap<string, string>, unknown: boolean): boolean;
@@ -0,0 +1,136 @@
1
+ import ts from 'typescript';
2
+ // This proof is intentionally narrower than "no JSX". The canvas frame/boot
3
+ // path omits document refresh and service callbacks, so every executable import
4
+ // and free global must be understood before that path may be selected.
5
+ const canvasDisplayMembers = new Set(['ctx', 'width', 'height', 'getDevicePixelRatio', 'getFrameIntervalMs', 'setFrameIntervalMs', 'getFrameRate', 'setFrameRate', 'setAA', 'setFlushConfig']);
6
+ const canvasHostExports = new Set(['Display', 'rgb']);
7
+ const pureGlobals = new Set([
8
+ 'undefined', 'NaN', 'Infinity', 'Math', 'Number', 'String', 'Boolean', 'BigInt',
9
+ 'Object', 'Array', 'ArrayBuffer', 'SharedArrayBuffer', 'DataView', 'Map', 'Set',
10
+ 'WeakMap', 'WeakSet', 'Symbol', 'Date', 'RegExp', 'JSON', 'Promise',
11
+ 'Int8Array', 'Uint8Array', 'Uint8ClampedArray', 'Int16Array', 'Uint16Array',
12
+ 'Int32Array', 'Uint32Array', 'Float32Array', 'Float64Array', 'BigInt64Array', 'BigUint64Array',
13
+ 'Error', 'TypeError', 'RangeError', 'ReferenceError', 'SyntaxError', 'URIError', 'EvalError',
14
+ 'parseInt', 'parseFloat', 'isNaN', 'isFinite', 'decodeURI', 'decodeURIComponent',
15
+ 'encodeURI', 'encodeURIComponent', 'console', 'performance',
16
+ 'requestAnimationFrame', 'setTimeout', 'clearTimeout', 'setInterval', 'clearInterval',
17
+ ]);
18
+ const hostModule = /^(?:gea-embedded|@geastack\/(?:core|engine)|@geajs\/core)(?:\/|$)/;
19
+ const canvasModule = /^(?:gea-embedded|@geastack\/core)$/;
20
+ export function inferCanvasOnly(sources, unknown) {
21
+ if (unknown || !sources.size)
22
+ return false;
23
+ const files = new Map();
24
+ for (const [file, text] of sources) {
25
+ if (!/\.[cm]?[jt]sx?$/.test(file))
26
+ return false;
27
+ const source = ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true, /\.[jt]sx$/.test(file) ? ts.ScriptKind.TSX : /\.[cm]?js$/.test(file) ? ts.ScriptKind.JS : ts.ScriptKind.TS);
28
+ if (source.parseDiagnostics?.length)
29
+ return false;
30
+ files.set(file, source);
31
+ }
32
+ // Binding local names avoids both a host-global blacklist (which misses new
33
+ // APIs) and false positives for ordinary app methods named fetch/document.
34
+ // No library or filesystem resolution: discovery already owns the graph.
35
+ const options = { noLib: true, noResolve: true, types: [], allowJs: true, target: ts.ScriptTarget.Latest };
36
+ const host = ts.createCompilerHost(options);
37
+ host.getSourceFile = file => files.get(file);
38
+ host.resolveModuleNames = names => names.map(() => undefined);
39
+ host.directoryExists = () => false;
40
+ host.getDirectories = () => [];
41
+ host.fileExists = file => files.has(file);
42
+ host.readFile = file => sources.get(file);
43
+ const checker = ts.createProgram([...files.keys()], options, host).getTypeChecker();
44
+ let safe = true;
45
+ let display = false;
46
+ const visit = (node) => {
47
+ if (ts.canHaveModifiers(node) && ts.getModifiers(node)?.some(m => m.kind === ts.SyntaxKind.DeclareKeyword))
48
+ return;
49
+ if (!safe || ts.isTypeNode(node) || ts.isInterfaceDeclaration(node) || ts.isTypeAliasDeclaration(node))
50
+ return;
51
+ if (ts.isJsxElement(node) || ts.isJsxSelfClosingElement(node) || ts.isJsxFragment(node)) {
52
+ safe = false;
53
+ return;
54
+ }
55
+ if (ts.isImportDeclaration(node)) {
56
+ if (node.importClause?.isTypeOnly)
57
+ return;
58
+ const module = ts.isStringLiteral(node.moduleSpecifier) ? node.moduleSpecifier.text : '';
59
+ if (hostModule.test(module)) {
60
+ const clause = node.importClause;
61
+ const names = clause?.namedBindings;
62
+ if (!canvasModule.test(module) || !clause || clause.name || !names || !ts.isNamedImports(names)) {
63
+ safe = false;
64
+ return;
65
+ }
66
+ for (const item of names.elements) {
67
+ if (item.isTypeOnly)
68
+ continue;
69
+ const name = (item.propertyName ?? item.name).text;
70
+ if (!canvasHostExports.has(name))
71
+ safe = false;
72
+ if (name === 'Display')
73
+ display = true;
74
+ }
75
+ }
76
+ return;
77
+ }
78
+ if (ts.isExportDeclaration(node)) {
79
+ if (!node.isTypeOnly && node.moduleSpecifier && ts.isStringLiteral(node.moduleSpecifier) && hostModule.test(node.moduleSpecifier.text))
80
+ safe = false;
81
+ return;
82
+ }
83
+ if (ts.isImportEqualsDeclaration(node) ||
84
+ (ts.isCallExpression(node) && (node.expression.kind === ts.SyntaxKind.ImportKeyword ||
85
+ (ts.isIdentifier(node.expression) && ['require', 'eval', 'Function'].includes(node.expression.text))))) {
86
+ safe = false;
87
+ return;
88
+ }
89
+ if (ts.isIdentifier(node) && isValueReference(node)) {
90
+ const symbol = ts.isShorthandPropertyAssignment(node.parent)
91
+ ? checker.getShorthandAssignmentValueSymbol(node.parent) : checker.getSymbolAtLocation(node);
92
+ const importedDisplay = symbol?.declarations?.find(d => ts.isImportSpecifier(d) && (d.propertyName ?? d.name).text === 'Display' && ts.isStringLiteral(d.parent.parent.parent.moduleSpecifier) && canvasModule.test(d.parent.parent.parent.moduleSpecifier.text));
93
+ if (importedDisplay) {
94
+ const parent = node.parent;
95
+ if (!ts.isPropertyAccessExpression(parent) || parent.expression !== node || !canvasDisplayMembers.has(parent.name.text))
96
+ safe = false;
97
+ }
98
+ if (!symbol?.declarations?.length || symbol.declarations.some(isAmbient)) {
99
+ if (node.text === 'window') {
100
+ const parent = node.parent;
101
+ if (!ts.isPropertyAccessExpression(parent) || parent.expression !== node ||
102
+ !['innerWidth', 'innerHeight', 'devicePixelRatio'].includes(parent.name.text))
103
+ safe = false;
104
+ }
105
+ else if (!pureGlobals.has(node.text))
106
+ safe = false;
107
+ }
108
+ }
109
+ ts.forEachChild(node, visit);
110
+ };
111
+ for (const source of files.values())
112
+ visit(source);
113
+ return safe && display;
114
+ }
115
+ function isValueReference(node) {
116
+ const parent = node.parent;
117
+ if (ts.isPropertyAccessExpression(parent) && parent.name === node)
118
+ return false;
119
+ if (ts.isBindingElement(parent) && parent.propertyName === node)
120
+ return false;
121
+ if (ts.isLabeledStatement(parent) || ts.isBreakStatement(parent) || ts.isContinueStatement(parent))
122
+ return false;
123
+ // Declaration/property names are not reads; shorthand properties are.
124
+ if ('name' in parent && parent.name === node && !ts.isShorthandPropertyAssignment(parent))
125
+ return false;
126
+ return true;
127
+ }
128
+ function isAmbient(node) {
129
+ for (let current = node; current; current = current.parent) {
130
+ if (ts.isSourceFile(current) && current.isDeclarationFile)
131
+ return true;
132
+ if (ts.canHaveModifiers(current) && ts.getModifiers(current)?.some(m => m.kind === ts.SyntaxKind.DeclareKeyword))
133
+ return true;
134
+ }
135
+ return false;
136
+ }
package/dist/analyze.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { inferCanvasOnly } from './analyze-runtime.js';
1
2
  import { nodeAuxVersion, addNodeAuxFeatures, addUnknownNodeAux, cssUsesNodeAttributes } from './analyze-node-aux.js';
2
3
  import { sourceLiteralResolver } from './analyze-literals.js';
3
4
  import fs from 'node:fs';
@@ -76,6 +77,9 @@ export function analyzeSourceHostBindings(entry) {
76
77
  features.add(`node-class-capacity-v1-${classCapacity}`);
77
78
  features.add(`node-class-storage-v1-${classCapacity}`);
78
79
  }
80
+ features.add('runtime-analysis-v1');
81
+ if (inferCanvasOnly(literalSources, discovery.unknown || discovery.runtimeUnknown))
82
+ features.add('runtime-canvas-only');
79
83
  variables.finish();
80
84
  css.finish();
81
85
  ranges.finish();
@@ -193,6 +197,7 @@ function discoverSourceFiles(entry) {
193
197
  const visited = new Set();
194
198
  const files = [];
195
199
  let unknown = false;
200
+ let runtimeUnknown = false;
196
201
  function visit(file) {
197
202
  const resolved = path.resolve(file);
198
203
  // An entry that is a directory (a configure run without app metadata)
@@ -200,6 +205,16 @@ function discoverSourceFiles(entry) {
200
205
  if (visited.has(resolved) || !fs.existsSync(resolved) || !fs.statSync(resolved).isFile())
201
206
  return;
202
207
  visited.add(resolved);
208
+ // Imported data is not executable source. Parsing a JPEG or JSON as TS
209
+ // manufactures syntax errors and falsely marks every renderer feature as
210
+ // reachable. Dynamic use of data as CSS is still observed at its call site.
211
+ if (/\.(?:json|png|jpe?g|gif|webp|bmp|ico|ttf|otf|woff2?|wav|mp3|ogg|mp4)$/i.test(resolved)) {
212
+ // Asset imports can generate decoder/host calls outside this source
213
+ // graph. Plain JSON is data; other assets cannot certify a minimal boot.
214
+ if (!/\.json$/i.test(resolved))
215
+ runtimeUnknown = true;
216
+ return;
217
+ }
203
218
  const text = fs.readFileSync(resolved, 'utf8');
204
219
  for (const specifier of /\.css$/i.test(resolved) ? [] : moduleSpecifiers(resolved, text)) {
205
220
  // Framework host imports do not inject renderer instructions. Other
@@ -227,7 +242,7 @@ function discoverSourceFiles(entry) {
227
242
  visit(entry);
228
243
  if (!files.length)
229
244
  unknown = true;
230
- return { files, unknown };
245
+ return { files, unknown, runtimeUnknown };
231
246
  }
232
247
  function moduleSpecifiers(file, text) {
233
248
  const source = ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true, /\.[jt]sx$/i.test(file) ? ts.ScriptKind.TSX : ts.ScriptKind.TS);
package/dist/cpp-ir.d.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  import type { EmitModule, GeaIrBundleV1, GeaIrConstant } from "./types.js";
2
- export declare function generateCppIrSource(ir: GeaIrBundleV1, irPath: string | null, modules: EmitModule[], microtasksNamespace?: string): string;
2
+ export declare function generateCppIrSource(ir: GeaIrBundleV1, irPath: string | null, modules: EmitModule[], microtasksNamespace?: string, directCanvas?: boolean): string;
3
3
  export declare function irUsesCanvas(ir: GeaIrBundleV1, modules?: readonly EmitModule[]): boolean;
4
4
  export declare function generateCppIrNamespaceSource(ir: GeaIrBundleV1, modules: readonly EmitModule[], options?: {
5
5
  includeDomInterop?: boolean;
6
+ directCanvas?: boolean;
6
7
  }): string;
7
8
  export declare function generateCppIrMountedSource(ir: GeaIrBundleV1, knownGlobalAccessorTypes?: ReadonlyMap<string, string>): string;
8
9
  export declare function generateCppIrAssetForwardDeclarations(ir: GeaIrBundleV1): string;
package/dist/cpp-ir.js CHANGED
@@ -7,17 +7,17 @@ import { sanitizeCppIdentifier } from "./utils.js";
7
7
  import { existsSync, readFileSync, statSync } from "node:fs";
8
8
  import { dirname, resolve } from "node:path";
9
9
  import ts from "typescript";
10
- export function generateCppIrSource(ir, irPath, modules, microtasksNamespace = "gea::framework::app::generated") {
10
+ export function generateCppIrSource(ir, irPath, modules, microtasksNamespace = "gea::framework::app::generated", directCanvas = false) {
11
11
  const drainNamespace = cppNamespace(microtasksNamespace);
12
12
  const usesCanvas = irUsesCanvas(ir, modules);
13
- const emitBoxedCanvasContext = usesCanvas && !directCanvasContextEnabled();
13
+ const emitBoxedCanvasContext = usesCanvas && !directCanvas;
14
14
  const lines = [
15
15
  "// @geastack/geatsc-plugin-gea consumed gea IR",
16
16
  `// ir=${irPath ?? "<inline>"}`,
17
17
  `// modules=${ir.modules.length} components=${ir.components.length} stores=${ir.stores.length}`,
18
18
  `// emitted-modules=${modules.length}`,
19
19
  "",
20
- ...(directCanvasContextEnabled()
20
+ ...(directCanvas
21
21
  ? ['#include "direct_canvas_runtime.h"']
22
22
  : []),
23
23
  '#include "gea/embedded.h"',
@@ -46,10 +46,6 @@ export function generateCppIrSource(ir, irPath, modules, microtasksNamespace = "
46
46
  ];
47
47
  return lines.join("\n");
48
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
49
  export function irUsesCanvas(ir, modules) {
54
50
  if (ir.hostCapabilities.includes("canvas"))
55
51
  return true;
@@ -160,7 +156,7 @@ export function generateCppIrNamespaceSource(ir, modules, options = {}) {
160
156
  ...(needsTemplateRuntime ? nativeDisposerSource() : []),
161
157
  "",
162
158
  ...(options.includeDomInterop
163
- ? canvasInteropSource(irUsesCanvas(ir, modules))
159
+ ? canvasInteropSource(irUsesCanvas(ir, modules), options.directCanvas === true)
164
160
  : []),
165
161
  // Forward-declare the interface-reuse typed-item helpers before
166
162
  // `storeRuntimeSource` so the template-dependent `set_typed_field(slot, …)`
@@ -1612,8 +1608,8 @@ export function directCanvasInteropSource(usesCanvas, options = {}) {
1612
1608
  "",
1613
1609
  ];
1614
1610
  }
1615
- function canvasInteropSource(usesCanvas) {
1616
- if (directCanvasContextEnabled())
1611
+ function canvasInteropSource(usesCanvas, directCanvas) {
1612
+ if (directCanvas)
1617
1613
  return [
1618
1614
  ...domStyleInteropSource(),
1619
1615
  ...directCanvasInteropSource(usesCanvas),
package/dist/index.js CHANGED
@@ -278,7 +278,8 @@ function transformModuleFirstGeneratedSource(source, ir, compilerRuntimeHeader,
278
278
  if (nextSource.fileName.endsWith('.types.hpp'))
279
279
  return nextSource;
280
280
  let next = nextSource.source;
281
- next = process.env.GEA_SKIP_STORE_RELOWER ? next : replaceStoreMethodsFromIr(next, ir, collectIrConstants(ir), topLevelValueNames);
281
+ if (options['gea.store-relowering'] !== 'false')
282
+ next = replaceStoreMethodsFromIr(next, ir, collectIrConstants(ir), topLevelValueNames);
282
283
  next = applyTypedArrayStorage(next, ir);
283
284
  next = foldCanvasColorLiteralsInCpp(next, { pixelPanelEndian: pixelPanelEndian(options) });
284
285
  const beforeRuntimeGlobals = next;
@@ -305,10 +306,10 @@ function generateModuleFirstGeaIrHeader(ir, irPath, context, options) {
305
306
  '#include "../generated_support.hpp"',
306
307
  usesReactiveRuntime ? '#include "gea/reactive_runtime.h"' : '',
307
308
  generateCppIrAssetForwardDeclarations(ir),
308
- generateCppIrSource(ir, irPath, context.modules, microtasksNamespace(options.pluginOptions)),
309
+ generateCppIrSource(ir, irPath, context.modules, microtasksNamespace(options.pluginOptions), true),
309
310
  options.includeGeaDocumentRuntime ? generateHostDocumentNodeValueDeclaration() : '',
310
311
  cppPreludeSource(options.pluginOptions),
311
- generateCppIrNamespaceSource(ir, context.modules, { includeDomInterop: options.includeDomInterop }),
312
+ generateCppIrNamespaceSource(ir, context.modules, { includeDomInterop: options.includeDomInterop, directCanvas: true }),
312
313
  ].filter(Boolean).join('\n');
313
314
  }
314
315
  function insertModuleHeaderInclude(source, includeLine) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geastack/geatsc-plugin-gea",
3
- "version": "0.1.11",
3
+ "version": "0.1.13",
4
4
  "description": "Gea IR backend plugin for geatsc.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -53,7 +53,6 @@
53
53
  },
54
54
  "peerDependencies": {
55
55
  "typescript": ">=5.9.3",
56
- "@geastack/compiler": ">=1.0.19"
57
- },
58
- "gitHead": "e375eddd8f725d7cc5de3fd46baca63e35936027"
56
+ "@geastack/compiler": ">=1.0.20"
57
+ }
59
58
  }