@native-sdk/cli 0.11.0 → 0.11.1
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/build/app.zig +73 -37
- package/build.zig +239 -5
- package/native-sdk.d.ts +5 -1
- package/package.json +13 -12
- package/packages/core/compile-surface/core.ts +30 -0
- package/packages/core/package-lock.json +6 -6
- package/packages/core/package.json +3 -3
- package/packages/core/scripts/scriptc-toolchain.json +44 -44
- package/packages/core/scripts/stage_external_core.mjs +27 -16
- package/packages/core/sdk/core.d.ts +24 -1
- package/packages/core/sdk/core.ts +51 -1
- package/packages/core/sdk/events.d.ts +5 -0
- package/packages/core/sdk/events.ts +10 -0
- package/packages/core/src/bridge_descriptors.ts +3 -3
- package/packages/core/src/checker.ts +20 -0
- package/packages/core/src/cli.ts +2 -11
- package/packages/core/src/context_menus.ts +76 -0
- package/packages/core/src/diagnostics.ts +7 -0
- package/packages/core/src/extent_program.ts +53 -0
- package/packages/core/src/frontend.ts +12 -10
- package/packages/core/src/markup_canonical.ts +140 -0
- package/packages/core/src/markup_construction.ts +189 -0
- package/packages/core/src/markup_display.ts +43 -0
- package/packages/core/src/markup_document.ts +226 -0
- package/packages/core/src/markup_elements.ts +225 -0
- package/packages/core/src/markup_expressions.ts +79 -0
- package/packages/core/src/markup_imports.ts +146 -0
- package/packages/core/src/markup_parser.ts +228 -0
- package/packages/core/src/markup_runtime.ts +93 -0
- package/packages/core/src/markup_scalars.ts +143 -0
- package/packages/core/src/markup_shapes.ts +243 -0
- package/packages/core/src/markup_specialized.ts +242 -0
- package/packages/core/src/markup_text.ts +86 -0
- package/packages/core/src/markup_values.ts +336 -0
- package/packages/core/src/portable_sources.json +24 -0
- package/packages/core/src/registered_font.ts +2 -3
- package/packages/core/src/runtime_policy.ts +24 -0
- package/packages/core/src/scalar_text.ts +1 -3
- package/packages/core/src/scroll_mutation.ts +86 -0
- package/packages/core/src/scroll_tree.ts +86 -0
- package/packages/core/src/service_contract.ts +53 -2
- package/packages/core/src/sqlite_cli.ts +3 -46
- package/packages/core/src/sqlite_codegen.ts +84 -0
- package/packages/core/src/staged_rebuild.ts +60 -0
- package/packages/core/src/view_bridge.ts +161 -0
- package/packages/core/src/view_check.ts +145 -0
- package/packages/core/src/view_evaluator.ts +823 -0
- package/packages/core/src/view_frontend.ts +340 -218
- package/packages/core/src/view_program.ts +221 -0
- package/packages/core/src/view_reload.ts +166 -0
- package/packages/core/src/view_reload_watch.ts +98 -0
- package/packages/core/src/view_sources.ts +34 -0
- package/packages/core/src/virtual_windows.ts +88 -0
- package/packages/core/src/web_panes.ts +67 -0
- package/packages/core/src/widget_continuity.ts +37 -0
- package/packages/core/src/widget_reconcile.ts +63 -0
- package/packages/core/src/widget_retention.ts +62 -0
- package/skill-data/native-ui/SKILL.md +2 -2
- package/skill-data/ts-core/SKILL.md +5 -3
- package/skill-data/ts-services/references/service-surface.md +29 -5
- package/src/app_runner/root.zig +14 -0
- package/src/app_runner/ts_compiled_view.zig +88 -17
- package/src/app_runner/ts_core_main.zig +43 -5
- package/src/app_runner/ts_core_mobile.zig +1 -1
- package/src/automation/server.zig +56 -3
- package/src/embed/host.zig +2 -0
- package/src/embed/tests.zig +2 -0
- package/src/embed/ui_host.zig +2 -0
- package/src/platform/keyboard_input.zig +32 -0
- package/src/platform/linux/root.zig +2 -0
- package/src/platform/macos/appkit_host.h +7 -2
- package/src/platform/macos/appkit_host.m +31 -9
- package/src/platform/macos/cef_host.mm +15 -1
- package/src/platform/macos/global_shortcuts.zig +138 -0
- package/src/platform/macos/key_codes.zig +102 -0
- package/src/platform/macos/keyboard_input.zig +81 -0
- package/src/platform/macos/root.zig +25 -3
- package/src/platform/macos/webview_inspection_test.m +106 -0
- package/src/platform/null_platform.zig +104 -0
- package/src/platform/root.zig +14 -0
- package/src/platform/types.zig +102 -0
- package/src/platform/windows/global_shortcuts.zig +157 -0
- package/src/platform/windows/key_codes.zig +63 -0
- package/src/platform/windows/keyboard_input.zig +136 -0
- package/src/platform/windows/root.zig +15 -0
- package/src/primitives/canvas/decision_reuse.zig +133 -0
- package/src/primitives/canvas/font_ttf.zig +27 -3
- package/src/primitives/canvas/fonts/Geist-Bold.ttf +0 -0
- package/src/primitives/canvas/fonts/Geist-Medium.ttf +0 -0
- package/src/primitives/canvas/fonts/OFL.txt +93 -0
- package/src/primitives/canvas/fonts/README.md +15 -0
- package/src/primitives/canvas/intrinsic_measure_policy.zig +4 -1
- package/src/primitives/canvas/markdown_tests.zig +3 -3
- package/src/primitives/canvas/reference.zig +3 -5
- package/src/primitives/canvas/reference_tests.zig +58 -0
- package/src/primitives/canvas/registered_font_policy.zig +1 -1
- package/src/primitives/canvas/root.zig +7 -4
- package/src/primitives/canvas/text_metrics.zig +32 -39
- package/src/primitives/canvas/text_metrics_tests.zig +52 -7
- package/src/primitives/canvas/text_query_policy.zig +142 -3
- package/src/primitives/canvas/text_scalar_policy.zig +80 -2
- package/src/primitives/canvas/tokens.zig +9 -0
- package/src/primitives/canvas/ui.zig +1 -0
- package/src/primitives/canvas/ui_markup.zig +69 -14
- package/src/primitives/canvas/ui_markup_canonical.zig +237 -0
- package/src/primitives/canvas/ui_markup_compiled.zig +1407 -23
- package/src/primitives/canvas/ui_markup_compiled_values.zig +76 -0
- package/src/primitives/canvas/ui_markup_construction.zig +146 -0
- package/src/primitives/canvas/ui_markup_display.zig +82 -0
- package/src/primitives/canvas/ui_markup_document.zig +196 -0
- package/src/primitives/canvas/ui_markup_elements.zig +163 -0
- package/src/primitives/canvas/ui_markup_expr.zig +192 -2
- package/src/primitives/canvas/ui_markup_imports.zig +262 -0
- package/src/primitives/canvas/ui_markup_parser.zig +117 -0
- package/src/primitives/canvas/ui_markup_runtime.zig +76 -0
- package/src/primitives/canvas/ui_markup_scalars.zig +115 -0
- package/src/primitives/canvas/ui_markup_shapes.zig +224 -0
- package/src/primitives/canvas/ui_markup_specialized.zig +106 -0
- package/src/primitives/canvas/ui_markup_text.zig +128 -0
- package/src/primitives/canvas/ui_markup_values.zig +368 -0
- package/src/primitives/canvas/ui_markup_view.zig +2817 -74
- package/src/primitives/canvas/virtual_extent_policy.zig +58 -0
- package/src/primitives/canvas/virtual_extents.zig +47 -33
- package/src/primitives/canvas/widget_metric_policy.zig +4 -1
- package/src/primitives/canvas/widget_runtime.zig +2 -1
- package/src/root.zig +5 -0
- package/src/runtime/api.zig +1 -0
- package/src/runtime/bridge_descriptor_policy.zig +1 -1
- package/src/runtime/bridge_payload.zig +2 -0
- package/src/runtime/canvas_widget_context_menu.zig +179 -0
- package/src/runtime/canvas_widget_context_menu_test_cases.zig +1147 -0
- package/src/runtime/canvas_widget_context_menu_tests.zig +23 -1098
- package/src/runtime/canvas_widget_runtime.zig +306 -92
- package/src/runtime/context_menu_policy.zig +112 -0
- package/src/runtime/core.zig +14 -0
- package/src/runtime/effects.zig +172 -4
- package/src/runtime/effects_host_tests.zig +25 -0
- package/src/runtime/flow.zig +1 -0
- package/src/runtime/flow_reference.zig +1 -0
- package/src/runtime/global_shortcuts.zig +75 -0
- package/src/runtime/keyboard_input.zig +86 -0
- package/src/runtime/root.zig +3 -0
- package/src/runtime/scroll_mutation_policy.zig +130 -0
- package/src/runtime/scroll_mutation_runtime.zig +158 -0
- package/src/runtime/scroll_tree_policy.zig +99 -0
- package/src/runtime/session_record.zig +7 -0
- package/src/runtime/staged_rebuild_policy.zig +83 -0
- package/src/runtime/system_services.zig +19 -0
- package/src/runtime/tests.zig +1 -0
- package/src/runtime/ts_core_host.zig +126 -0
- package/src/runtime/ts_ui_app.zig +24 -0
- package/src/runtime/ui_app.zig +274 -31
- package/src/runtime/ui_app_tests.zig +48 -179
- package/src/runtime/ui_app_window_tests.zig +133 -0
- package/src/runtime/view_reload.zig +226 -0
- package/src/runtime/view_widget_scroll.zig +30 -21
- package/src/runtime/view_widget_tree.zig +43 -13
- package/src/runtime/virtual_window_policy.zig +128 -0
- package/src/runtime/web_pane_policy.zig +111 -0
- package/src/runtime/web_pane_test_cases.zig +204 -0
- package/src/runtime/widget_continuity_policy.zig +73 -0
- package/src/runtime/widget_reconcile_policy.zig +81 -0
- package/src/runtime/widget_retention_policy.zig +75 -0
- package/src/runtime/window_views.zig +19 -4
- package/src/security/root.zig +1 -0
- package/src/tooling/templates.zig +8 -0
- package/src/tooling/ts_core.zig +84 -1
- package/src/tooling/verbs.zig +79 -15
- package/tools/corewire/core.test.ts +25 -3
- package/tools/corewire/core_abi.zig +4 -0
- package/tools/corewire/core_contract.ts +1 -1
- package/tools/corewire/core_goldens.json +75 -75
- package/tools/corewire/core_projection.ts +3 -2
- package/tools/corewire/emit_mirror.ts +19 -9
- package/tools/corewire/facade_templates.ts +1 -1
- package/tools/corewire/intake.test.ts +4 -0
- package/tools/corewire/intake_goldens.json +50 -50
- package/tools/corewire/invocation.test.ts +4 -0
- package/tools/corewire/invocation_goldens.json +3 -3
- package/tools/corewire/mirror_templates.ts +4 -0
- package/tools/corewire/profile_emission.ts +2 -0
- package/tools/corewire/sidecar.zig +2 -0
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
* constructs fail at build time; no bindings are evaluated by the native host.
|
|
3
3
|
*/
|
|
4
4
|
import { portableSourceText } from "./portable_sources.ts";
|
|
5
|
+
import { wyhash } from "./wyhash.ts";
|
|
6
|
+
import { printStatements, s, v, type ViewExpr, type ViewProgram, type ViewProp, type ViewStmt } from "./view_program.ts";
|
|
7
|
+
export type { ViewProgram } from "./view_program.ts";
|
|
5
8
|
|
|
6
9
|
interface Ref { kind: string; name?: string; elem?: Ref; inner?: Ref; class?: string; type?: Ref }
|
|
7
10
|
interface Arm { name: string; member?: string; payload: Ref }
|
|
@@ -12,13 +15,27 @@ export interface ViewContract {
|
|
|
12
15
|
msg: { name?: string; arms: Arm[] };
|
|
13
16
|
}
|
|
14
17
|
interface Element { name: string; attrs: Map<string, string>; children: Element[]; content: (Element | { text: string; at: number; end: number })[]; text: string; at: number; end: number; file: string }
|
|
15
|
-
interface Expr { code:
|
|
18
|
+
interface Expr { code: ViewExpr; type: Ref; virtualIndex?: string }
|
|
16
19
|
type Scope = ReadonlyMap<string, Expr>;
|
|
17
|
-
interface Slot { children: Element[]; scope: Scope; slot?: Slot }
|
|
20
|
+
interface Slot { children: Element[]; scope: Scope; slot?: Slot; chain: readonly ViewSourceSite[] }
|
|
18
21
|
export interface ViewSources { entry?: string; sources?: ReadonlyMap<string, string> }
|
|
19
22
|
|
|
20
23
|
export interface WindowViewSource extends ViewSources { label: string; source: string }
|
|
21
24
|
|
|
25
|
+
/** Where a view node was authored: a markup file (relative to the app's
|
|
26
|
+
* `src/`, empty for the root view file), the element's UTF-8 byte span,
|
|
27
|
+
* and its 1-based line and byte column.
|
|
28
|
+
*/
|
|
29
|
+
export interface ViewSourceSite { readonly file: string; readonly start: number; readonly end: number; readonly line: number; readonly column: number }
|
|
30
|
+
/** A node's definition site and the `<use>` sites that placed it, outermost first. */
|
|
31
|
+
export interface ViewSource extends ViewSourceSite { readonly chain: readonly ViewSourceSite[] }
|
|
32
|
+
|
|
33
|
+
/** The model names and Msg kinds the compiled views read or dispatch,
|
|
34
|
+
* collected while compiling so `native check` can report model state and
|
|
35
|
+
* messages no view uses.
|
|
36
|
+
*/
|
|
37
|
+
export interface ViewUsage { readonly model: Set<string>; readonly msg: Set<string> }
|
|
38
|
+
|
|
22
39
|
const colorTokenNames = ["background", "surface", "surface_subtle", "surface_pressed", "text", "text_muted", "syntax_plain", "syntax_comment", "syntax_keyword", "syntax_literal", "syntax_function", "syntax_property", "syntax_constant", "border", "accent", "accent_text", "destructive", "destructive_text", "success", "success_text", "warning", "warning_text", "info", "info_text", "focus_ring", "shadow", "scrim", "disabled"];
|
|
23
40
|
|
|
24
41
|
const nonHitTargetNames = ["row", "column", "stack", "list", "grid", "split", "tree", "breadcrumb", "button-group", "pagination", "radio-group", "tabs", "toggle-group", "badge", "avatar", "tooltip", "separator", "spacer", "table", "data-grid", "table-row", "skeleton", "spinner", "icon"];
|
|
@@ -31,10 +48,30 @@ export function compileView(source: string, contract: ViewContract, options: Vie
|
|
|
31
48
|
* Window source paths are relative to src/windows, preserving its import boundary.
|
|
32
49
|
*/
|
|
33
50
|
export function compileViewBundle(source: string, contract: ViewContract, options: ViewSources,
|
|
34
|
-
windows: readonly WindowViewSource[]): string {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
51
|
+
windows: readonly WindowViewSource[], usage?: ViewUsage): string {
|
|
52
|
+
return compileViewProgram(source, contract, options, windows, usage).text;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The release TypeScript and the program it prints, from one compilation.
|
|
56
|
+
* With `development`, every view export serves the development evaluator's
|
|
57
|
+
* active program when one is prepared (view_evaluator.ts must be staged
|
|
58
|
+
* beside the result); release builds never pass it.
|
|
59
|
+
*/
|
|
60
|
+
export function compileViewProgram(source: string, contract: ViewContract, options: ViewSources,
|
|
61
|
+
windows: readonly WindowViewSource[], usage?: ViewUsage, development = false): { text: string; program: ViewProgram; virtual: boolean } {
|
|
62
|
+
// Source paths are stamped relative to the app's src/; the root view file
|
|
63
|
+
// is stamped empty, as the native markup engine stamps it.
|
|
64
|
+
const entry = options.entry ?? "app.native";
|
|
65
|
+
const main = compileViewFunction(source, contract, options, "nscvMainView", false, usage, development ? file => file === entry ? "" : file : undefined);
|
|
66
|
+
const program: ViewProgram & { windows: ViewProgram["windows"][number][] } = { main: main.body, mainAttribution: main.attribution, windows: [] };
|
|
67
|
+
// `compiled` reads its video state from `nscvVideo` and is the export's
|
|
68
|
+
// whole result when no development program is active.
|
|
69
|
+
const exported = (signature: string, label: string, video: string, virtual: string, compiled: string): string => development
|
|
70
|
+
? `export function ${signature}: Uint8Array { const nscvVideo = ${video}; if (nscvdActive()) return nscvdView(${label}, nscvVideo, ${virtual}); return ${compiled}; }\n`
|
|
71
|
+
: `export function ${signature}: Uint8Array { return ${compiled.replace("nscvVideo", video)}; }\n`;
|
|
72
|
+
let generated = viewPrelude + main.text +
|
|
73
|
+
exported("native_view()", "new Uint8Array(0)", "nscvEmptyVideo()", "undefined", "nscvMainView(nscvVideo)") +
|
|
74
|
+
exported("native_media_view(request: Uint8Array)", "new Uint8Array(0)", "nscvVideoState(request)", "undefined", "nscvMainView(nscvVideo)");
|
|
38
75
|
const labels = new Set<string>();
|
|
39
76
|
const branches: string[] = [];
|
|
40
77
|
for (const [index, window] of windows.entries()) {
|
|
@@ -43,23 +80,43 @@ export function compileViewBundle(source: string, contract: ViewContract, option
|
|
|
43
80
|
throw new Error("compiled TypeScript view: invalid or duplicate window label");
|
|
44
81
|
}
|
|
45
82
|
labels.add(window.label);
|
|
46
|
-
|
|
83
|
+
const compiled = compileViewFunction(window.source, contract, window, `nscvWindow${index}`, false, usage, development ? file => `windows/${file}` : undefined);
|
|
84
|
+
generated += compiled.text; program.windows.push({ label: window.label, body: compiled.body, attribution: compiled.attribution });
|
|
47
85
|
branches.push(`if (nscvWindowLabel(label, ${JSON.stringify(window.label)})) return nscvWindow${index}(nscvVideo);`);
|
|
48
86
|
}
|
|
49
87
|
const virtual = [source, ...options.sources?.values() ?? [], ...windows.flatMap(window => [window.source, ...window.sources?.values() ?? []])].some(value => /<virtual-window\b/.test(value));
|
|
50
|
-
if (windows.length === 0 && !virtual) return generated;
|
|
88
|
+
if (windows.length === 0 && !virtual) return { text: generated, program, virtual };
|
|
51
89
|
generated += `function nscvWindowLabel(label: Uint8Array, expected: string): boolean { const bytes = new TextEncoder().encode(expected); if (label.length !== bytes.length) return false; for (let i = 0; i < bytes.length; i++) if (label[i] !== bytes[i]) return false; return true; }\n`;
|
|
52
90
|
if (windows.length) generated +=
|
|
53
91
|
`function nscvWindowView(label: Uint8Array, nscvVideo: NscVideoPlaybackState): Uint8Array {\n${branches.join("\n")}\nthrow new Error("unknown compiled window view label");\n}\n` +
|
|
54
|
-
|
|
55
|
-
|
|
92
|
+
exported("native_window_view(label: Uint8Array)", "label", "nscvEmptyVideo()", "undefined", "nscvWindowView(label, nscvVideo)") +
|
|
93
|
+
exported("native_media_window_view(label: Uint8Array, request: Uint8Array)", "label", "nscvVideoState(request)", "undefined", "nscvWindowView(label, nscvVideo)");
|
|
56
94
|
if (virtual) generated += `function nscvVirtualView(label: Uint8Array, nscvVideo: NscVideoPlaybackState, nscvVirtual: NscVirtualBuild): Uint8Array {\nif (label.length === 0) return nscvMainView(nscvVideo, nscvVirtual);\n${windows.map((window, index) => `if (nscvWindowLabel(label, ${JSON.stringify(window.label)})) return nscvWindow${index}(nscvVideo, nscvVirtual);`).join("\n")}\nthrow new Error("unknown compiled virtual window label");\n}\n` +
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
95
|
+
(development
|
|
96
|
+
? `export function native_virtual_requests(label: Uint8Array, video: Uint8Array): Uint8Array { const nscvVideo = nscvVideoState(video); const nscvVirtual: NscVirtualBuild = { requestsOnly: true, requests: [], ranges: [], consumed: [] }; if (nscvdActive()) return nscvdView(label, nscvVideo, nscvVirtual); return nscvVirtualView(label, nscvVideo, nscvVirtual); }\n` +
|
|
97
|
+
`export function native_virtual_view(label: Uint8Array, context: Uint8Array, video: Uint8Array): Uint8Array { const nscvVideo = nscvVideoState(video); const nscvVirtual = nscvVirtualContext(context); if (nscvdActive()) return nscvdView(label, nscvVideo, nscvVirtual); return nscvVirtualView(label, nscvVideo, nscvVirtual); }\n`
|
|
98
|
+
: `export function native_virtual_requests(label: Uint8Array, video: Uint8Array): Uint8Array { return nscvVirtualView(label, nscvVideoState(video), { requestsOnly: true, requests: [], ranges: [], consumed: [] }); }\n` +
|
|
99
|
+
`export function native_virtual_view(label: Uint8Array, context: Uint8Array, video: Uint8Array): Uint8Array { return nscvVirtualView(label, nscvVideoState(video), nscvVirtualContext(context)); }\n`);
|
|
100
|
+
return { text: generated, program, virtual };
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** The static half of a development view's records, as one JSON fragment:
|
|
104
|
+
* the source table and every markup file it names, root view file first,
|
|
105
|
+
* with the Wyhash (seed 0, hex) of the bytes the view was compiled from.
|
|
106
|
+
*/
|
|
107
|
+
function sourceAttribution(table: readonly ViewSource[], sources: ReadonlyMap<string, string>, entry: string, stamp: (file: string) => string): string {
|
|
108
|
+
const files = [entry];
|
|
109
|
+
const byStamp = new Map([...sources.keys()].map(file => [stamp(file), file]));
|
|
110
|
+
for (const source of table) for (const site of [source, ...source.chain]) {
|
|
111
|
+
const file = byStamp.get(site.file)!;
|
|
112
|
+
if (!files.includes(file)) files.push(file);
|
|
113
|
+
}
|
|
114
|
+
const listed = files.map(file => ({ path: stamp(file), hash: wyhash(0n, new TextEncoder().encode(sources.get(file)!)).toString(16).padStart(16, "0") }));
|
|
115
|
+
return `"sourceTable":${JSON.stringify(table)},"sourceFiles":${JSON.stringify(listed)}`;
|
|
60
116
|
}
|
|
61
117
|
|
|
62
|
-
function compileViewFunction(source: string, contract: ViewContract, options: ViewSources, functionName: string, exported: boolean
|
|
118
|
+
function compileViewFunction(source: string, contract: ViewContract, options: ViewSources, functionName: string, exported: boolean, usage?: ViewUsage,
|
|
119
|
+
stamp?: (file: string) => string): { text: string; body: readonly ViewStmt[]; attribution: string } {
|
|
63
120
|
const entry = options.entry ?? "app.native";
|
|
64
121
|
const sources = new Map(options.sources); sources.set(entry, source);
|
|
65
122
|
const fail: (node: Pick<Element, "file" | "at">, message: string) => never = (node, message) => {
|
|
@@ -176,13 +233,14 @@ function compileViewFunction(source: string, contract: ViewContract, options: Vi
|
|
|
176
233
|
const [name, ...members] = raw.split(".");
|
|
177
234
|
const field = fields!.find(item => item.name === name);
|
|
178
235
|
const helper = contract.model_helpers.find(item => item.name === name && item.params.length === 0);
|
|
179
|
-
let value = scope.get(name!) ?? (field ? { code:
|
|
236
|
+
let value = scope.get(name!) ?? (field ? { code: v.field(v.id("nscfCommitted"), name!, true), type: field.type } : helper ? { code: v.call(name!, v.id("nscfCommitted")), type: helper.returns } : null);
|
|
180
237
|
if (!value) fail(node, `unknown binding ${name}`);
|
|
238
|
+
if (!scope.has(name!)) usage?.model.add(name!);
|
|
181
239
|
for (const member of members) {
|
|
182
240
|
const record: ViewContract["types"]["structs"][number] | undefined = ["node", "value"].includes(value.type.kind) ? contract.types.structs.find(item => item.name === value!.type.name) : undefined;
|
|
183
241
|
const next: { name: string; type: Ref } | undefined = record?.fields.find(item => item.name === member);
|
|
184
242
|
if (!next) fail(node, `unsupported field ${member} on ${raw}`);
|
|
185
|
-
value = { code:
|
|
243
|
+
value = { code: v.field(value.code, member, true), type: next.type };
|
|
186
244
|
}
|
|
187
245
|
return value;
|
|
188
246
|
};
|
|
@@ -202,12 +260,12 @@ function compileViewFunction(source: string, contract: ViewContract, options: Vi
|
|
|
202
260
|
else if (token === "!" || token === "-") {
|
|
203
261
|
const inner = parseExpr(7), expected = token === "!" ? "boolean" : "number";
|
|
204
262
|
if (category(inner.type) !== expected) fail(node, `${token} requires ${expected}`);
|
|
205
|
-
left = { code:
|
|
206
|
-
} else if (token === "true" || token === "false") left = { code: token, type: { kind: "bool" } };
|
|
207
|
-
else if (token?.startsWith("'")) left = { code:
|
|
263
|
+
left = { code: v.unary(token, inner.code), type: { kind: token === "!" ? "bool" : "f64" } };
|
|
264
|
+
} else if (token === "true" || token === "false") left = { code: v.bool(token === "true"), type: { kind: "bool" } };
|
|
265
|
+
else if (token?.startsWith("'")) left = { code: v.str(token.slice(1, -1)), type: { kind: "string", name: token.slice(1, -1) } };
|
|
208
266
|
else if (token && /^\d/.test(token)) {
|
|
209
267
|
if (!Number.isFinite(Number(token))) fail(node, "non-finite literal");
|
|
210
|
-
left = { code: token, type: { kind: token.includes(".") ? "f64" : "i64" } };
|
|
268
|
+
left = { code: v.num(token), type: { kind: token.includes(".") ? "f64" : "i64" } };
|
|
211
269
|
} else left = path(token ?? "", node, scope);
|
|
212
270
|
for (;;) {
|
|
213
271
|
const op = tokens[cursor], precedence = op && Object.hasOwn(precedences, op) ? precedences[op] : undefined;
|
|
@@ -223,7 +281,7 @@ function compileViewFunction(source: string, contract: ViewContract, options: Vi
|
|
|
223
281
|
valid = literal.type.kind === "string" && !!contract.types.enums?.find(item => item.name === enumRef.type.name)?.members.includes(literal.type.name!);
|
|
224
282
|
}
|
|
225
283
|
if (!valid) fail(node, `invalid operands for ${op}`);
|
|
226
|
-
left = { code:
|
|
284
|
+
left = { code: v.binary(op === "==" ? "===" : op === "!=" ? "!==" : op!, left.code, right.code), type: { kind: logical || equality || comparison ? "bool" : "f64" } };
|
|
227
285
|
}
|
|
228
286
|
return left;
|
|
229
287
|
};
|
|
@@ -232,59 +290,87 @@ function compileViewFunction(source: string, contract: ViewContract, options: Vi
|
|
|
232
290
|
return result;
|
|
233
291
|
};
|
|
234
292
|
const binding = (raw: string, node: Element, scope: Scope): Expr => expression(/^\{([^{}]*)\}$/.exec(raw)?.[1] ?? raw, node, scope);
|
|
235
|
-
const bound = (raw: string, type: string, node: Element, scope: Scope):
|
|
293
|
+
const bound = (raw: string, type: string, node: Element, scope: Scope): ViewExpr => {
|
|
236
294
|
const expr = binding(raw, node, scope);
|
|
237
295
|
if (category(expr.type) !== type) fail(node, `expected ${type} binding`);
|
|
238
296
|
return expr.code;
|
|
239
297
|
};
|
|
240
|
-
const condition = (raw: string, node: Element, scope: Scope):
|
|
298
|
+
const condition = (raw: string, node: Element, scope: Scope): ViewExpr => {
|
|
241
299
|
const expr = binding(raw, node, scope);
|
|
242
300
|
if (expr.type.kind === "bool") return expr.code;
|
|
243
|
-
if (category(expr.type) === "number") return
|
|
244
|
-
if (["bytes", "string", "enum"].includes(expr.type.kind)) return
|
|
301
|
+
if (category(expr.type) === "number") return v.binary("!==", expr.code, v.num("0"));
|
|
302
|
+
if (["bytes", "string", "enum"].includes(expr.type.kind)) return v.binary(">", v.length(expr.code), v.num("0"));
|
|
245
303
|
return fail(node, "condition requires a scalar or text binding");
|
|
246
304
|
};
|
|
247
|
-
const textValue = (expr: Expr, node: Element):
|
|
248
|
-
if (expr.type.kind === "bytes") return
|
|
249
|
-
if (["number", "boolean", "enum", "string"].includes(category(expr.type))) return
|
|
305
|
+
const textValue = (expr: Expr, node: Element): ViewExpr => {
|
|
306
|
+
if (expr.type.kind === "bytes") return v.decode(expr.code);
|
|
307
|
+
if (["number", "boolean", "enum", "string"].includes(category(expr.type))) return v.string(expr.code);
|
|
250
308
|
return fail(node, "text requires bytes, an enum or a scalar binding");
|
|
251
309
|
};
|
|
252
|
-
const text = (raw: string, node: Element, scope: Scope):
|
|
253
|
-
const parts:
|
|
310
|
+
const text = (raw: string, node: Element, scope: Scope): ViewExpr => {
|
|
311
|
+
const parts: ViewExpr[] = []; let last = 0;
|
|
254
312
|
for (const match of raw.matchAll(/\{([^{}]*)\}/g)) {
|
|
255
|
-
parts.push(
|
|
313
|
+
parts.push(v.str(raw.slice(last, match.index)), textValue(expression(match[1]!, node, scope), node)); last = match.index + match[0].length;
|
|
256
314
|
}
|
|
257
|
-
parts.push(
|
|
315
|
+
parts.push(v.str(raw.slice(last)));
|
|
258
316
|
if (raw.replace(/\{[^{}]*\}/g, "").match(/[{}]/)) fail(node, "unbalanced text binding");
|
|
259
|
-
return parts.
|
|
317
|
+
return parts.reduce((left, right) => v.binary("+", left, right, false));
|
|
260
318
|
};
|
|
261
|
-
const byteText = (raw: string, node: Element, scope: Scope):
|
|
262
|
-
const parts:
|
|
319
|
+
const byteText = (raw: string, node: Element, scope: Scope): ViewExpr | null => {
|
|
320
|
+
const parts: ViewExpr[] = []; let last = 0, hasBytes = false;
|
|
263
321
|
for (const match of raw.matchAll(/\{([^{}]*)\}/g)) {
|
|
264
|
-
if (match.index > last) parts.push(
|
|
322
|
+
if (match.index > last) parts.push(v.encode(v.str(raw.slice(last, match.index))));
|
|
265
323
|
const expr = expression(match[1]!, node, scope);
|
|
266
324
|
if (expr.type.kind === "bytes") { hasBytes = true; parts.push(expr.code); }
|
|
267
|
-
else parts.push(
|
|
325
|
+
else parts.push(v.encode(textValue(expr, node)));
|
|
268
326
|
last = match.index + match[0].length;
|
|
269
327
|
}
|
|
270
328
|
if (!hasBytes) return null;
|
|
271
|
-
if (last < raw.length) parts.push(
|
|
272
|
-
return
|
|
329
|
+
if (last < raw.length) parts.push(v.encode(v.str(raw.slice(last))));
|
|
330
|
+
return v.call("nscvTextBytes", v.array(parts));
|
|
273
331
|
};
|
|
274
332
|
const key = (raw: string, node: Element, scope: Scope): Expr => {
|
|
275
333
|
if (/^-?\d+$/.test(raw)) {
|
|
276
334
|
if (!Number.isSafeInteger(Number(raw))) fail(node, "key is not an exact integer");
|
|
277
|
-
return { code:
|
|
335
|
+
return { code: v.call("nscvInteger", v.num(raw)), type: { kind: "i64" } };
|
|
278
336
|
}
|
|
279
337
|
if (!raw.startsWith("{")) {
|
|
280
338
|
if (/^(?:[-+]?\d|[.]\d|true$|false$)/.test(raw)) fail(node, "keys must be integers or strings");
|
|
281
|
-
return { code:
|
|
339
|
+
return { code: v.str(raw), type: { kind: "string" } };
|
|
282
340
|
}
|
|
283
341
|
const expr = binding(raw, node, scope);
|
|
284
342
|
if (expr.type.kind !== "i64" && expr.type.kind !== "bytes" && expr.type.kind !== "string") fail(node, "keys must be integers or strings");
|
|
285
|
-
return expr.type.kind === "i64" ? { code:
|
|
343
|
+
return expr.type.kind === "i64" ? { code: v.call("nscvInteger", expr.code), type: expr.type } : expr.type.kind === "bytes" ? expr : { code: textValue(expr, node), type: { kind: "string" } };
|
|
344
|
+
};
|
|
345
|
+
let output: ViewStmt[] = []; let next = 0;
|
|
346
|
+
// Development compiles (`stamp` set) record where each node was authored:
|
|
347
|
+
// a source table, and a mark after every element that attributes the
|
|
348
|
+
// records it pushed (and that its children did not claim) to its source.
|
|
349
|
+
const sourceTable: ViewSource[] = [], sourceIds = new Map<string, number>();
|
|
350
|
+
let chain: readonly ViewSourceSite[] = [], marks = 0;
|
|
351
|
+
const byteOffsets = new Map<string, Int32Array>();
|
|
352
|
+
const site = (node: Pick<Element, "file" | "at" | "end">): ViewSourceSite => {
|
|
353
|
+
const text = sources.get(node.file)!;
|
|
354
|
+
let offsets = byteOffsets.get(node.file);
|
|
355
|
+
if (!offsets) {
|
|
356
|
+
offsets = new Int32Array(text.length + 1);
|
|
357
|
+
for (let i = 0, bytes = 0; i < text.length; i++) {
|
|
358
|
+
offsets[i] = bytes;
|
|
359
|
+
const unit = text.charCodeAt(i);
|
|
360
|
+
bytes += unit < 0x80 ? 1 : unit < 0x800 ? 2 : unit >= 0xd800 && unit < 0xdc00 ? 4 : unit >= 0xdc00 && unit < 0xe000 ? 0 : 3;
|
|
361
|
+
offsets[i + 1] = bytes;
|
|
362
|
+
}
|
|
363
|
+
byteOffsets.set(node.file, offsets);
|
|
364
|
+
}
|
|
365
|
+
const lineStart = text.lastIndexOf("\n", node.at - 1) + 1;
|
|
366
|
+
let line = 1;
|
|
367
|
+
for (let i = text.indexOf("\n"); i >= 0 && i < node.at; i = text.indexOf("\n", i + 1)) line++;
|
|
368
|
+
return { file: stamp!(node.file), start: offsets[node.at]!, end: offsets[node.end]!, line, column: offsets[node.at]! - offsets[lineStart]! + 1 };
|
|
369
|
+
};
|
|
370
|
+
const block = (build: () => void): ViewStmt[] => {
|
|
371
|
+
const outer = output; output = [];
|
|
372
|
+
try { build(); return output; } finally { output = outer; }
|
|
286
373
|
};
|
|
287
|
-
const output: string[] = []; let next = 0;
|
|
288
374
|
const textInputUnion = (ref: Ref): boolean => {
|
|
289
375
|
const arms = ref.kind === "union" ? contract.types.unions?.find(item => item.name === ref.name)?.arms : undefined;
|
|
290
376
|
const verbs = ["delete_backward", "delete_forward", "delete_word_backward", "delete_word_forward", "delete_to_start", "delete_to_line_start", "clear", "commit_composition", "cancel_composition"];
|
|
@@ -317,61 +403,72 @@ function compileViewFunction(source: string, contract: ViewContract, options: Vi
|
|
|
317
403
|
if (alternate?.name === "else") {
|
|
318
404
|
if (alternate.attrs.size || alternate.text.trim()) fail(alternate, "else accepts only child elements");
|
|
319
405
|
const items = path(node.attrs.get("each")!, node, scope);
|
|
320
|
-
output.push(
|
|
321
|
-
emitChildren(alternate.children, scope, slot, stack, depth + 1); output.push("}"); i++;
|
|
406
|
+
output.push(s.if(v.binary("===", v.length(items.code), v.num("0"), false), block(() => emitChildren(alternate.children, scope, slot, stack, depth + 1)))); i++;
|
|
322
407
|
}
|
|
323
408
|
continue;
|
|
324
409
|
}
|
|
325
410
|
if (node.attrs.size !== 1 || !node.attrs.has("test") || node.text.trim()) fail(node, "if requires only test and child elements");
|
|
326
|
-
|
|
327
|
-
emitChildren(node.children, scope, slot, stack, depth + 1)
|
|
411
|
+
const test = condition(node.attrs.get("test")!, node, scope);
|
|
412
|
+
const then = block(() => emitChildren(node.children, scope, slot, stack, depth + 1));
|
|
328
413
|
const alternate = nodes[i + 1];
|
|
329
414
|
if (alternate?.name === "else") {
|
|
330
415
|
if (alternate.attrs.size || alternate.text.trim()) fail(alternate, "else accepts only child elements");
|
|
331
|
-
output.push(
|
|
332
|
-
}
|
|
416
|
+
output.push(s.if(test, then, block(() => emitChildren(alternate.children, scope, slot, stack, depth + 1)))); i++;
|
|
417
|
+
} else output.push(s.if(test, then));
|
|
333
418
|
}
|
|
334
419
|
};
|
|
335
|
-
const event = (raw: string, channel: string, node: Element, scope: Scope):
|
|
420
|
+
const event = (raw: string, channel: string, node: Element, scope: Scope): ViewExpr => {
|
|
336
421
|
const match = /^([A-Za-z_][A-Za-z0-9_]*)(?::\{([^{}]+)\})?$/.exec(raw);
|
|
337
422
|
const arm = contract.msg.arms.find(item => item.name === match?.[1]);
|
|
338
423
|
if (!arm || !match) fail(node, `unknown or unsupported event ${raw}`);
|
|
424
|
+
usage?.msg.add(arm.name);
|
|
339
425
|
const payload = match[2] ? path(match[2], node, scope) : null;
|
|
340
426
|
if (channel === "value" || channel === "resize") {
|
|
341
427
|
if (payload || arm.payload.kind !== "number" || (arm.payload.class ?? "f64") !== "f64") fail(node, channel === "resize" ? "split on-resize requires a bare one-number float Msg arm" : "slider on-change requires a bare one-number float or void Msg arm");
|
|
342
|
-
return String(contract.msg.arms.indexOf(arm));
|
|
428
|
+
return v.num(String(contract.msg.arms.indexOf(arm)));
|
|
343
429
|
}
|
|
344
430
|
if (channel === "input") {
|
|
345
431
|
if (payload || !textInputUnion(arm.payload)) fail(node, "on-input requires a bare TextInputEvent Msg arm");
|
|
346
|
-
return String(contract.msg.arms.indexOf(arm));
|
|
432
|
+
return v.num(String(contract.msg.arms.indexOf(arm)));
|
|
347
433
|
}
|
|
348
434
|
if (channel === "terminal") {
|
|
349
435
|
const record = arm.payload.kind === "record" ? contract.types.structs.find(item => item.name === arm.payload.name) : null;
|
|
350
436
|
const names = ["scrollback", "history", "cols", "rows"];
|
|
351
437
|
if (payload || !record || record.fields.length !== 4 || !names.every(name => record.fields.some(field => field.name === name && category(field.type) === "number"))) fail(node, "on-terminal requires a bare TerminalState Msg arm");
|
|
352
|
-
return String(contract.msg.arms.indexOf(arm));
|
|
438
|
+
return v.num(String(contract.msg.arms.indexOf(arm)));
|
|
353
439
|
}
|
|
354
440
|
if (channel === "scroll") {
|
|
355
441
|
const record = arm.payload.kind === "record" ? contract.types.structs.find(item => item.name === arm.payload.name) : null;
|
|
356
442
|
const names = ["offsetX", "offsetY", "velocityX", "velocityY", "viewportExtentX", "viewportExtentY", "contentExtentX", "contentExtentY"];
|
|
357
443
|
if (payload || !record || record.fields.length !== 8 || !names.every(name => record.fields.some(field => field.name === name && category(field.type) === "number"))) fail(node, "on-scroll requires a bare ScrollState Msg arm");
|
|
358
|
-
return String(contract.msg.arms.indexOf(arm));
|
|
444
|
+
return v.num(String(contract.msg.arms.indexOf(arm)));
|
|
359
445
|
}
|
|
360
|
-
|
|
446
|
+
const props: ViewProp[] = [v.prop("kind", v.str(arm.name))];
|
|
361
447
|
if (channel === "drag") {
|
|
362
448
|
const record = arm.payload.kind === "record" ? contract.types.structs.find(item => item.name === arm.payload.name) : null;
|
|
363
449
|
const names = ["sourceId", "phase", "x", "y", "viewWidth", "viewHeight"];
|
|
364
450
|
if (!payload || category(payload.type) !== "number" || !record || record.fields.length !== 6 || !names.every(name => record.fields.some(field => field.name === name && (["x", "y", "viewWidth", "viewHeight"].includes(name) ? field.type.kind === "f64" : category(field.type) === "number")))) fail(node, "on-drag requires a sourceId binding and the six-field drag Msg record");
|
|
365
|
-
props
|
|
451
|
+
props.push(v.prop("sourceId", payload.code), ...["phase", "x", "y", "viewWidth", "viewHeight"].map(name => v.prop(name, v.num("0"))));
|
|
366
452
|
} else if (arm.payload.kind === "void") { if (payload) fail(node, "void Msg arm cannot take a payload"); }
|
|
367
453
|
else {
|
|
368
454
|
const type = arm.payload.kind === "number" ? { kind: arm.payload.class ?? "f64" } : arm.payload.kind === "scalar" ? arm.payload.type! : arm.payload;
|
|
369
455
|
if (!payload || !arm.member || category(type) !== category(payload.type) || !["number", "boolean", "bytes", "enum"].includes(category(type)) || (type.kind === "enum" && type.name !== payload.type.name)) fail(node, "event requires a matching scalar Msg payload binding");
|
|
370
|
-
props
|
|
456
|
+
props.push(v.prop(arm.member, payload.code, true));
|
|
371
457
|
}
|
|
372
|
-
return
|
|
458
|
+
return v.pack(v.object("msg", props, arm.name));
|
|
373
459
|
};
|
|
374
460
|
const emit = (node: Element, scope: Scope, slot: Slot | undefined, stack: string[], depth: number, inputActionsAllowed = false): void => {
|
|
461
|
+
if (!stamp || node.name === "use" || node.name === "slot") return emitElement(node, scope, slot, stack, depth, inputActionsAllowed);
|
|
462
|
+
const source: ViewSource = { ...site(node), chain };
|
|
463
|
+
const key = JSON.stringify(source);
|
|
464
|
+
let id = sourceIds.get(key);
|
|
465
|
+
if (id === undefined) { id = sourceTable.length; sourceTable.push(source); sourceIds.set(key, id); }
|
|
466
|
+
const start = `nscvSourceStart${marks++}`;
|
|
467
|
+
output.push(s.const(start, v.length(v.id("nscvNodes"))));
|
|
468
|
+
emitElement(node, scope, slot, stack, depth, inputActionsAllowed);
|
|
469
|
+
output.push(s.expr(v.call("nscvSourceMark", v.id("nscvSources"), v.id(start), v.length(v.id("nscvNodes")), v.num(String(id)))));
|
|
470
|
+
};
|
|
471
|
+
const emitElement = (node: Element, scope: Scope, slot: Slot | undefined, stack: string[], depth: number, inputActionsAllowed = false): void => {
|
|
375
472
|
if (depth > 64 || next > 4096) fail(node, "expanded view exceeds 64 levels or 4096 elements");
|
|
376
473
|
if (node.name === "virtual-window") {
|
|
377
474
|
if ([...scope.values()].some(value => value.virtualIndex !== undefined)) fail(node, "virtual-window declarations cannot nest");
|
|
@@ -382,11 +479,12 @@ function compileViewFunction(source: string, contract: ViewContract, options: Vi
|
|
|
382
479
|
const estimateName = node.attrs.get("extent-estimate");
|
|
383
480
|
const estimator = contract.model_helpers.find(helper => helper.name === estimateName && helper.params.length === 1 && ["i64", "f64"].includes(helper.params[0]!.kind) && ["i64", "f64"].includes(helper.returns.kind));
|
|
384
481
|
if (estimateName !== undefined && !estimator) fail(node, "extent-estimate requires a Model-first numeric index helper returning a number");
|
|
482
|
+
if (estimator) usage?.model.add(estimator.name);
|
|
385
483
|
const range = contract.model_helpers.flatMap(helper => helper.params).find(rangeType);
|
|
386
484
|
if (!range) fail(node, "virtual-window requires a row helper taking the complete VirtualListRange");
|
|
387
|
-
const numeric = (attr: string, fallback: string, integer = false):
|
|
485
|
+
const numeric = (attr: string, fallback: string, integer = false): ViewExpr => {
|
|
388
486
|
const raw = node.attrs.get(attr);
|
|
389
|
-
if (raw === undefined) return fallback;
|
|
487
|
+
if (raw === undefined) return v.num(fallback);
|
|
390
488
|
const value = raw.startsWith("{") ? binding(raw, node, scope) : expression(raw, node, scope);
|
|
391
489
|
if (!["i64", "f64"].includes(value.type.kind) || integer && value.type.kind !== "i64") fail(node, `${attr} requires ${integer ? "an integer" : "a number"}`);
|
|
392
490
|
return value.code;
|
|
@@ -394,12 +492,16 @@ function compileViewFunction(source: string, contract: ViewContract, options: Vi
|
|
|
394
492
|
const anchor = node.attrs.get("anchor") ?? "leading";
|
|
395
493
|
if (anchor !== "leading" && anchor !== "trailing") fail(node, "virtual-window anchor requires leading or trailing");
|
|
396
494
|
const id = next++, variable = `nscvRange${id}`, index = `nscvWindowIndex${id}`;
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
495
|
+
const request = v.object("virtual_request", [v.prop("id", v.str(identity)), v.prop("itemCount", numeric("item-count", "0", true)), v.prop("itemExtent", numeric("item-extent", "0")), v.prop("gap", numeric("gap", "0")),
|
|
496
|
+
v.prop("overscan", numeric("overscan", "4", true)), v.prop("viewportFallback", numeric("viewport-fallback", "0")), v.prop("indexBase", numeric("index-base", "0", true)),
|
|
497
|
+
v.prop("trailing", v.bool(anchor === "trailing")), v.prop("estimateHelper", v.num(String(estimator ? contract.model_helpers.indexOf(estimator) : -1)))]);
|
|
498
|
+
output.push(s.throwIf(v.binary("===", v.id("nscvVirtual"), v.undef(), false), "virtual view requires retained window context"),
|
|
499
|
+
s.const(index, v.call("nscvVirtualRequest", v.field(v.id("nscvVirtual"), "requests"), request)));
|
|
500
|
+
const inner = new Map(scope); inner.set(name, { code: v.id(variable), type: range, virtualIndex: index });
|
|
501
|
+
output.push(s.if(v.unary("!", v.field(v.id("nscvVirtual"), "requestsOnly"), false), block(() => {
|
|
502
|
+
output.push(s.const(variable, v.index(v.field(v.id("nscvVirtual"), "ranges"), v.id(index))), s.throwIf(v.binary("===", v.id(variable), v.undef(), false), "missing virtual window range"));
|
|
503
|
+
emitChildren(node.children, inner, slot, stack, depth + 1);
|
|
504
|
+
}))); return;
|
|
403
505
|
}
|
|
404
506
|
if (node.name === "virtual-list") {
|
|
405
507
|
const windowName = node.attrs.get("window"), window = windowName ? scope.get(windowName) : undefined;
|
|
@@ -407,17 +509,22 @@ function compileViewFunction(source: string, contract: ViewContract, options: Vi
|
|
|
407
509
|
const name = node.attrs.get("as");
|
|
408
510
|
if (!window?.virtualIndex || !helper || helper.params.length !== 1 || !rangeType(helper.params[0]!) || helper.returns.kind !== "slice" || !helper.returns.elem || !name || !/^[A-Za-z_][A-Za-z0-9_]*$/.test(name) || node.children.length !== 1 || node.text.trim()) fail(node, "virtual-list requires an enclosing window, a Model-first range query, as and one keyed root per row");
|
|
409
511
|
if (!node.children[0]!.attrs.has("key") && !node.children[0]!.attrs.has("global-key")) fail(node, "virtual-list requires one keyed root per row");
|
|
512
|
+
usage?.model.add(helper.name);
|
|
410
513
|
const allowed = ["window", "each", "as", "grow", "width", "height", "min-width", "padding", "label", "background", "foreground", "border-color", "focus-ring", "radius", "overscroll", "on-scroll", "on-reach-end", "on-reach-start"];
|
|
411
514
|
if ([...node.attrs.keys()].some(attr => !allowed.includes(attr))) fail(node, "unsupported virtual-list attribute");
|
|
412
515
|
const attrs = new Map(node.attrs); for (const attr of ["window", "each", "as", "on-reach-end", "on-reach-start"]) attrs.delete(attr);
|
|
413
516
|
const id = next++, start = `nscvListStart${id}`, item = `nscvVirtualItem${id}`;
|
|
414
|
-
|
|
517
|
+
const nodes = v.id("nscvNodes"), started = v.nonnull(v.index(nodes, v.id(start)));
|
|
518
|
+
output.push(s.expr(v.call("nscvVirtualConsume", v.nonnull(v.id("nscvVirtual")), v.id(window.virtualIndex))), s.const(start, v.length(nodes)));
|
|
415
519
|
emit({ ...node, name: "scroll", attrs, children: [], content: [] }, scope, slot, stack, depth + 1);
|
|
416
|
-
for (const [attr, property] of [["on-reach-end", "reachEnd"], ["on-reach-start", "reachStart"]]) if (node.attrs.has(attr!)) output.push(
|
|
417
|
-
output.push(
|
|
418
|
-
const inner = new Map(scope); inner.set(name, { code: item, type: helper.returns.elem });
|
|
419
|
-
|
|
420
|
-
|
|
520
|
+
for (const [attr, property] of [["on-reach-end", "reachEnd"], ["on-reach-start", "reachStart"]]) if (node.attrs.has(attr!)) output.push(s.assign(v.field(started, property!), event(node.attrs.get(attr!)!, attr!.slice(3), node, scope)));
|
|
521
|
+
output.push(s.assign(v.field(started, "virtualWindow"), v.id(window.virtualIndex)));
|
|
522
|
+
const inner = new Map(scope); inner.set(name, { code: v.id(item), type: helper.returns.elem });
|
|
523
|
+
output.push(s.for(item, v.call(helper.name, v.id("nscfCommitted"), window.code), block(() => {
|
|
524
|
+
output.push(s.const(`nscvRowStart${id}`, v.length(nodes)));
|
|
525
|
+
emitChildren(node.children, inner, slot, stack, depth + 1);
|
|
526
|
+
output.push(s.expr(v.call("nscvVirtualRow", nodes, v.id(`nscvRowStart${id}`))));
|
|
527
|
+
})), s.assign(v.field(started, "end"), v.length(nodes)), s.expr(v.call("nscvVirtualList", nodes, v.id(start), window.code)));
|
|
421
528
|
return;
|
|
422
529
|
}
|
|
423
530
|
if (node.name === "use") {
|
|
@@ -430,49 +537,55 @@ function compileViewFunction(source: string, contract: ViewContract, options: Vi
|
|
|
430
537
|
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(arg) || inner.has(arg)) fail(template, "invalid or duplicate template argument");
|
|
431
538
|
const raw = node.attrs.get(arg);
|
|
432
539
|
if (raw === undefined) fail(node, `missing template argument ${arg}`);
|
|
433
|
-
const value = raw.startsWith("{") ? binding(raw, node, scope) : { code:
|
|
434
|
-
const variable = `nscvArg${next++}`; output.push(
|
|
540
|
+
const value = raw.startsWith("{") ? binding(raw, node, scope) : { code: v.str(raw), type: { kind: "string" } };
|
|
541
|
+
const variable = `nscvArg${next++}`; output.push(s.const(variable, value.code)); inner.set(arg, { ...value, code: v.id(variable) });
|
|
435
542
|
}
|
|
436
543
|
if ([...node.attrs.keys()].some(arg => arg !== "template" && !args.includes(arg))) fail(node, "unknown template argument");
|
|
437
|
-
|
|
544
|
+
const consumer = chain;
|
|
545
|
+
if (stamp) chain = [...chain, site(node)];
|
|
546
|
+
try { emitChildren(template.children, inner, { children: node.children, scope, slot, chain: consumer }, [...stack, name!], depth + 1); } finally { chain = consumer; }
|
|
547
|
+
return;
|
|
438
548
|
}
|
|
439
549
|
if (node.name === "slot") {
|
|
440
550
|
if (!slot || node.attrs.size || node.children.length || node.text.trim()) fail(node, "slot requires a template use and no attributes or children");
|
|
441
|
-
|
|
551
|
+
const inside = chain; chain = slot.chain;
|
|
552
|
+
try { emitChildren(slot.children, slot.scope, slot.slot, stack, depth + 1); } finally { chain = inside; }
|
|
553
|
+
return;
|
|
442
554
|
}
|
|
443
555
|
if (node.name === "for") {
|
|
444
556
|
const each = node.attrs.get("each"), as = node.attrs.get("as"), field = node.attrs.get("key");
|
|
445
557
|
if (!each || !as || !/^[A-Za-z_][A-Za-z0-9_]*$/.test(as) || node.attrs.size !== (field === undefined ? 2 : 3) || field === "" || node.text.trim()) fail(node, "for requires each, as, an optional key and child elements");
|
|
446
558
|
const items = path(each, node, scope);
|
|
447
559
|
if (items.type.kind !== "slice" || !items.type.elem) fail(node, "each requires a list binding");
|
|
448
|
-
const id = next++, variable = `nscvItem${id}`, inner = new Map(scope); inner.set(as, { code: variable, type: items.type.elem });
|
|
560
|
+
const id = next++, variable = `nscvItem${id}`, inner = new Map(scope); inner.set(as, { code: v.id(variable), type: items.type.elem });
|
|
449
561
|
const base = field === undefined ? null : key(`{${as}.${field}}`, node, inner);
|
|
450
|
-
output.push(
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
562
|
+
output.push(s.for(variable, items.code, block(() => {
|
|
563
|
+
output.push(s.const(`nscvFirst${id}`, v.length(v.id("nscvNodes"))));
|
|
564
|
+
emitChildren(node.children, inner, slot, stack, depth + 1);
|
|
565
|
+
if (base !== null) output.push(s.expr(v.call(base.type.kind === "bytes" ? "nscvLoopByteKeys" : "nscvLoopKeys", v.id("nscvNodes"), v.id(`nscvFirst${id}`), base.code)));
|
|
566
|
+
}))); return;
|
|
454
567
|
}
|
|
455
|
-
const stringAttr = (name: string, fallback = ""):
|
|
568
|
+
const stringAttr = (name: string, fallback = ""): ViewExpr => {
|
|
456
569
|
const raw = node.attrs.get(name);
|
|
457
|
-
if (raw === undefined) return
|
|
458
|
-
const value = raw.startsWith("{") ? binding(raw, node, scope) : { code:
|
|
570
|
+
if (raw === undefined) return v.str(fallback);
|
|
571
|
+
const value = raw.startsWith("{") ? binding(raw, node, scope) : { code: v.str(raw), type: { kind: "string" } };
|
|
459
572
|
if (!["bytes", "string", "enum"].includes(value.type.kind)) fail(node, `${name} requires text`);
|
|
460
573
|
return textValue(value, node);
|
|
461
574
|
};
|
|
462
|
-
const
|
|
575
|
+
const keyProp = (name: string, expr: Expr): ViewProp => v.prop(`${name === "key" ? "key" : "globalKey"}${expr.type.kind === "i64" ? "Int" : expr.type.kind === "bytes" ? "Bytes" : ""}`,
|
|
576
|
+
expr.type.kind === "bytes" ? v.call("nscvTextBytes", v.array([expr.code])) : expr.code);
|
|
577
|
+
const componentRoot = (allowed: string[]): ViewProp[] => {
|
|
463
578
|
if (node.text.trim()) fail(node, "component accepts only child elements");
|
|
464
579
|
for (const name of node.attrs.keys()) if (!allowed.includes(name)) fail(node, `unsupported ${node.name} attribute ${name}`);
|
|
465
|
-
const props = [
|
|
580
|
+
const props = [v.prop("end", v.num("0")), v.prop("kind", v.str("row")), v.prop("text", v.str(""))];
|
|
466
581
|
for (const name of ["key", "global-key"]) {
|
|
467
582
|
const raw = node.attrs.get(name);
|
|
468
|
-
if (raw !== undefined)
|
|
469
|
-
const expr = key(raw, node, scope);
|
|
470
|
-
props.push(`${name === "key" ? "key" : "globalKey"}${expr.type.kind === "i64" ? "Int" : expr.type.kind === "bytes" ? "Bytes" : ""}: ${expr.type.kind === "bytes" ? `nscvTextBytes([${expr.code}])` : expr.code}`);
|
|
471
|
-
}
|
|
583
|
+
if (raw !== undefined) props.push(keyProp(name, key(raw, node, scope)));
|
|
472
584
|
}
|
|
473
585
|
if (node.attrs.has("label")) {
|
|
474
586
|
const bytes = byteText(node.attrs.get("label")!, node, scope);
|
|
475
|
-
|
|
587
|
+
if (bytes === null) props.push(v.prop("label", stringAttr("label")));
|
|
588
|
+
else props.push(v.prop("label", v.str("")), v.prop("labelBytes", bytes));
|
|
476
589
|
}
|
|
477
590
|
return props;
|
|
478
591
|
};
|
|
@@ -480,15 +593,15 @@ function compileViewFunction(source: string, contract: ViewContract, options: Vi
|
|
|
480
593
|
const actions = node.name === "input-group-actions";
|
|
481
594
|
if (actions && !inputActionsAllowed) fail(node, "input-group-actions requires an input-group parent");
|
|
482
595
|
const props = componentRoot(actions ? ["gap", "key", "global-key"] : ["label", "width", "height", "min-width", "grow", "key", "global-key"]);
|
|
483
|
-
props[1] =
|
|
596
|
+
props[1] = v.prop("kind", v.str(actions ? "input_group_actions" : "input_group"));
|
|
484
597
|
for (const name of actions ? ["gap"] : ["width", "height", "min-width", "grow"]) {
|
|
485
598
|
const raw = node.attrs.get(name);
|
|
486
|
-
if (raw !== undefined) props.push(
|
|
599
|
+
if (raw !== undefined) props.push(v.prop(name === "min-width" ? "minWidth" : name, bound(raw, "number", node, scope)));
|
|
487
600
|
}
|
|
488
|
-
const id = next
|
|
489
|
-
output.push(`
|
|
601
|
+
const id = next++, group = v.id(`nscvGroup${id}`);
|
|
602
|
+
output.push(s.const(`nscvGroup${id}`, v.object("node", props), "NscViewNode"), s.expr(v.call("nscvPush", v.id("nscvNodes"), group)));
|
|
490
603
|
if (actions) {
|
|
491
|
-
if (!node.attrs.has("gap")) output.push(
|
|
604
|
+
if (!node.attrs.has("gap")) output.push(s.assign(v.field(group, "gap"), v.num("6")));
|
|
492
605
|
if (node.children.some(child => child.name === "#text")) fail(node, "input-group-actions requires element children");
|
|
493
606
|
emitChildren(node.children, scope, slot, stack, depth + 1);
|
|
494
607
|
} else {
|
|
@@ -497,7 +610,7 @@ function compileViewFunction(source: string, contract: ViewContract, options: Vi
|
|
|
497
610
|
emit(node.children[0]!, scope, slot, stack, depth + 1);
|
|
498
611
|
if (node.children[1]) emit(node.children[1], scope, slot, stack, depth + 1, true);
|
|
499
612
|
}
|
|
500
|
-
output.push(
|
|
613
|
+
output.push(s.assign(v.field(group, "end"), v.length(v.id("nscvNodes"))));
|
|
501
614
|
return;
|
|
502
615
|
}
|
|
503
616
|
if (node.name === "chart") {
|
|
@@ -507,11 +620,11 @@ function compileViewFunction(source: string, contract: ViewContract, options: Vi
|
|
|
507
620
|
if (raw === undefined) continue;
|
|
508
621
|
const value = bound(raw, "number", node, scope);
|
|
509
622
|
const field = { "y-min": "chartYMin", "y-max": "chartYMax", "stroke-width": "chartStrokeWidth", "grid-lines": "chartGridLines" }[name];
|
|
510
|
-
props.push(
|
|
623
|
+
props.push(v.prop(field ?? name, name === "grid-lines" ? v.call("nscvChartGrid", value) : field ? v.call("nscvChartWord", value) : value));
|
|
511
624
|
}
|
|
512
625
|
for (const [name, field] of [["baseline", "chartBaseline"], ["y-labels", "chartYLabels"], ["hover-details", "chartHoverDetails"]]) {
|
|
513
626
|
const raw = node.attrs.get(name!);
|
|
514
|
-
if (raw !== undefined) props.push(
|
|
627
|
+
if (raw !== undefined) props.push(v.prop(field!, bound(raw, "boolean", node, scope)));
|
|
515
628
|
}
|
|
516
629
|
const series = node.children.map(child => {
|
|
517
630
|
if (child.name !== "series" || child.text.trim() || child.children.length || [...child.attrs.keys()].some(name => !["kind", "values", "color", "label"].includes(name))) fail(child, "chart requires series leaves");
|
|
@@ -523,17 +636,17 @@ function compileViewFunction(source: string, contract: ViewContract, options: Vi
|
|
|
523
636
|
if (!["line", "area", "bar"].includes(kind)) fail(child, "series kind requires line, area or bar");
|
|
524
637
|
if (!["background", "surface", "surface_subtle", "surface_pressed", "text", "text_muted", "syntax_plain", "syntax_comment", "syntax_keyword", "syntax_literal", "syntax_function", "syntax_property", "syntax_constant", "border", "accent", "accent_text", "destructive", "destructive_text", "success", "success_text", "warning", "warning_text", "info", "info_text", "focus_ring", "shadow", "scrim", "disabled"].includes(color)) fail(child, "series color requires a literal color token");
|
|
525
638
|
const label = child.attrs.get("label");
|
|
526
|
-
const bytes = label === undefined ?
|
|
527
|
-
return
|
|
639
|
+
const bytes = label === undefined ? v.array([]) : byteText(label, child, scope) ?? v.call("nscvTextBytes", v.array([v.encode(label.startsWith("{") ? textValue(binding(label, child, scope), child) : v.str(label))]));
|
|
640
|
+
return v.object("chart_series", [v.prop("kind", v.str(kind === "bar" ? "bar" : "line")), v.prop("fill", v.bool(kind === "area")), v.prop("color", v.str(color)), v.prop("label", bytes), v.prop("values", v.call("nscvChartWords", values.code))]);
|
|
528
641
|
});
|
|
529
642
|
if (series.length === 0) fail(node, "chart requires at least one series");
|
|
530
|
-
const rawLabels = node.attrs.get("x-labels"); let labels =
|
|
643
|
+
const rawLabels = node.attrs.get("x-labels"); let labels: ViewExpr = v.array([]);
|
|
531
644
|
if (rawLabels !== undefined) {
|
|
532
645
|
const values = binding(rawLabels, node, scope);
|
|
533
646
|
if (values.type.kind !== "slice" || !values.type.elem || !["bytes", "string"].includes(values.type.elem.kind)) fail(node, "chart x-labels requires a text array binding");
|
|
534
|
-
labels =
|
|
647
|
+
labels = v.labelBytes(values.code, values.type.elem.kind === "bytes");
|
|
535
648
|
}
|
|
536
|
-
output.push(
|
|
649
|
+
output.push(s.expr(v.call("nscvChart", v.id("nscvNodes"), v.object("node", props), v.array(series), labels)));
|
|
537
650
|
return;
|
|
538
651
|
}
|
|
539
652
|
if (node.name === "stepper") {
|
|
@@ -550,43 +663,44 @@ function compileViewFunction(source: string, contract: ViewContract, options: Vi
|
|
|
550
663
|
const labels = steps.map(step => {
|
|
551
664
|
if (step.name !== "step" || step.attrs.size || step.children.length) fail(step, "stepper takes only step text leaves");
|
|
552
665
|
const raw = step.text.trim(), bytes = byteText(raw, step, stepScope);
|
|
553
|
-
return bytes === null ?
|
|
666
|
+
return bytes === null ? v.object("component_text", [v.prop("text", text(raw, step, stepScope))]) : v.object("component_text", [v.prop("text", v.str("")), v.prop("bytes", bytes)]);
|
|
554
667
|
});
|
|
555
|
-
output.push(
|
|
668
|
+
output.push(s.expr(v.call("nscvStepper", v.id("nscvNodes"), v.object("node", props), active.code, v.array(labels))));
|
|
556
669
|
return;
|
|
557
670
|
}
|
|
558
671
|
if (node.name === "timeline") {
|
|
559
672
|
const props = componentRoot(["gap", "grow", "key", "global-key", "label"]);
|
|
560
|
-
props[1] =
|
|
561
|
-
for (const name of ["gap", "grow"]) if (node.attrs.has(name)) props.push(
|
|
562
|
-
const id = next
|
|
563
|
-
output.push(`
|
|
564
|
-
`
|
|
565
|
-
|
|
673
|
+
props[1] = v.prop("kind", v.str("column")); props.push(v.prop("role", v.str("list")));
|
|
674
|
+
for (const name of ["gap", "grow"]) if (node.attrs.has(name)) props.push(v.prop(name, bound(node.attrs.get(name)!, "number", node, scope)));
|
|
675
|
+
const id = next++, root = v.id(`nscvNode${id}`);
|
|
676
|
+
output.push(s.const(`nscvNode${id}`, v.object("node", props), "NscViewNode"),
|
|
677
|
+
s.const(`nscvRecipe${id}`, v.call("nscvCompositionRequest", ...["7", "0", "0", "0", "0", "0", "0"].map(v.num), v.nullish(v.field(root, "gap"), v.num("0")))),
|
|
678
|
+
s.assign(v.field(root, "gap"), v.float32(v.id(`nscvRecipe${id}`), 32)), s.expr(v.call("nscvPush", v.id("nscvNodes"), root)));
|
|
566
679
|
emitChildren(node.children, scope, slot, stack, depth + 1);
|
|
567
|
-
output.push(
|
|
680
|
+
output.push(s.assign(v.field(root, "end"), v.length(v.id("nscvNodes"))));
|
|
568
681
|
return;
|
|
569
682
|
}
|
|
570
683
|
if (node.name === "timeline-item") {
|
|
571
684
|
const props = componentRoot(["title", "description", "meta", "indicator", "icon", "variant", "connector", "selected", "on-press", "key", "global-key"]);
|
|
572
685
|
if (!node.attrs.has("title") || node.children.length) fail(node, "timeline-item requires title and no children");
|
|
573
|
-
if (node.attrs.has("on-press")) props.push(
|
|
574
|
-
if (node.attrs.has("selected")) props.push(
|
|
686
|
+
if (node.attrs.has("on-press")) props.push(v.prop("press", event(node.attrs.get("on-press")!, "press", node, scope)));
|
|
687
|
+
if (node.attrs.has("selected")) props.push(v.prop("selected", bound(node.attrs.get("selected")!, "boolean", node, scope)));
|
|
575
688
|
const variant = stringAttr("variant", "outline");
|
|
576
|
-
const connector = node.attrs.has("connector") ? bound(node.attrs.get("connector")!, "boolean", node, scope) :
|
|
577
|
-
const itemText = (name: string):
|
|
689
|
+
const connector = node.attrs.has("connector") ? bound(node.attrs.get("connector")!, "boolean", node, scope) : v.bool(true);
|
|
690
|
+
const itemText = (name: string): ViewExpr => {
|
|
578
691
|
const raw = node.attrs.get(name), bytes = raw === undefined ? null : byteText(raw, node, scope);
|
|
579
|
-
return bytes === null ?
|
|
692
|
+
return bytes === null ? v.object("component_text", [v.prop("text", stringAttr(name))]) : v.object("component_text", [v.prop("text", v.str("")), v.prop("bytes", bytes)]);
|
|
580
693
|
};
|
|
581
|
-
output.push(
|
|
694
|
+
output.push(s.expr(v.call("nscvTimelineItem", v.id("nscvNodes"), v.object("timeline_item", [v.prop("root", v.object("node", props)), v.prop("title", itemText("title")), v.prop("description", itemText("description")),
|
|
695
|
+
v.prop("meta", itemText("meta")), v.prop("indicator", itemText("indicator")), v.prop("icon", stringAttr("icon")), v.prop("variant", v.call("nscvVariant", variant)), v.prop("connector", connector)]))));
|
|
582
696
|
return;
|
|
583
697
|
}
|
|
584
698
|
if (node.name === "video") {
|
|
585
699
|
const props = componentRoot(["src", "controls", "autoplay", "loop", "muted", "key", "global-key", "width", "height", "grow", "label"]);
|
|
586
700
|
if (node.children.length || node.text.trim()) fail(node, "video is a leaf");
|
|
587
|
-
for (const name of ["width", "height", "grow"]) if (node.attrs.has(name)) props.push(
|
|
588
|
-
const flag = (name: string, fallback:
|
|
589
|
-
output.push(
|
|
701
|
+
for (const name of ["width", "height", "grow"]) if (node.attrs.has(name)) props.push(v.prop(name, bound(node.attrs.get(name)!, "number", node, scope)));
|
|
702
|
+
const flag = (name: string, fallback: boolean) => node.attrs.has(name) ? bound(node.attrs.get(name)!, "boolean", node, scope) : v.bool(fallback);
|
|
703
|
+
output.push(s.expr(v.call("nscvVideoElement", v.id("nscvNodes"), v.object("node", props), v.id("nscvVideo"), stringAttr("src"), flag("controls", false), flag("autoplay", true), flag("loop", false), flag("muted", false))));
|
|
590
704
|
return;
|
|
591
705
|
}
|
|
592
706
|
if (node.name === "markdown") {
|
|
@@ -594,7 +708,7 @@ function compileViewFunction(source: string, contract: ViewContract, options: Vi
|
|
|
594
708
|
if (node.children.length || node.text.trim() || !node.attrs.has("source") || [...node.attrs.keys()].some(name => !allowed.includes(name))) fail(node, "markdown requires source, no children, and its closed presentation attributes");
|
|
595
709
|
const source = binding(node.attrs.get("source")!, node, scope);
|
|
596
710
|
if (!/^\{[^{}]+\}$/.test(node.attrs.get("source")!) || source.type.kind !== "bytes") fail(node, "markdown source requires one byte binding");
|
|
597
|
-
let expanded =
|
|
711
|
+
let expanded: ViewExpr = v.array([]), issue: ViewExpr = v.nul(), images: ViewExpr = v.array([]), wordImages = false;
|
|
598
712
|
if (node.attrs.has("details-expanded")) {
|
|
599
713
|
const raw = node.attrs.get("details-expanded")!, value = binding(raw, node, scope);
|
|
600
714
|
if (!/^\{[^{}]+\}$/.test(raw) || value.type.kind !== "slice" || value.type.elem?.kind !== "bool") fail(node, "details-expanded requires one boolean-array binding");
|
|
@@ -611,16 +725,18 @@ function compileViewFunction(source: string, contract: ViewContract, options: Vi
|
|
|
611
725
|
if (node.attrs.has("issue-link-base")) {
|
|
612
726
|
const raw = node.attrs.get("issue-link-base")!;
|
|
613
727
|
if (raw.startsWith("{")) { const value = binding(raw, node, scope); if (!/^\{[^{}]+\}$/.test(raw) || value.type.kind !== "bytes") fail(node, "issue-link-base requires literal text or one byte binding"); issue = value.code; }
|
|
614
|
-
else { if (/[{}]/.test(raw)) fail(node, "invalid issue-link-base literal"); issue =
|
|
728
|
+
else { if (/[{}]/.test(raw)) fail(node, "invalid issue-link-base literal"); issue = v.encode(v.str(raw)); }
|
|
615
729
|
}
|
|
616
|
-
const channels:
|
|
730
|
+
const channels: ViewProp[] = [];
|
|
617
731
|
for (const [attribute, property, kind] of [["on-link", "markdownLink", "bytes"], ["on-details", "markdownDetails", "number"]]) {
|
|
618
732
|
const raw = node.attrs.get(attribute!); if (raw === undefined) continue;
|
|
619
733
|
const arm = contract.msg.arms.find(item => item.name === raw);
|
|
620
734
|
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(raw) || !arm || arm.payload.kind !== kind) fail(node, `${attribute} requires a bare ${kind} Msg arm`);
|
|
621
|
-
|
|
735
|
+
usage?.msg.add(arm.name);
|
|
736
|
+
channels.push(v.prop(property!, v.num(String(contract.msg.arms.indexOf(arm)))));
|
|
622
737
|
}
|
|
623
|
-
output.push(
|
|
738
|
+
output.push(s.push(v.id("nscvNodes"), v.object("node", [v.prop("end", v.binary("+", v.length(v.id("nscvNodes")), v.num("1"), false)), v.prop("kind", v.str("markdown")), v.prop("text", v.str("")),
|
|
739
|
+
v.prop("markdownRecipe", v.call(wordImages ? "nscvMarkdownRecipeWords" : "nscvMarkdownRecipe", source.code, expanded, issue, images)), ...channels])));
|
|
624
740
|
return;
|
|
625
741
|
}
|
|
626
742
|
const kinds: Record<string, string> = { image: "image", terminal: "terminal", icon: "icon", combobox: "combobox", bubble: "bubble", table: "table", "data-grid": "data_grid", popover: "popover", "menu-surface": "menu_surface", "table-row": "data_row", "table-cell": "data_cell", progress: "progress", skeleton: "skeleton", spinner: "spinner", column: "column", row: "row", stack: "stack", grid: "grid", card: "card", alert: "alert", dialog: "dialog", drawer: "drawer", sheet: "sheet", tooltip: "tooltip", panel: "panel", badge: "badge", input: "input", "text-field": "text_field", textarea: "textarea", code: "textarea", "search-field": "search_field", select: "select", text: "text", button: "button", checkbox: "checkbox", switch: "switch_control", toggle: "toggle", slider: "slider", "status-bar": "status_bar", spacer: "spacer", scroll: "scroll", avatar: "avatar", radio: "radio", "radio-group": "radio_group", "button-group": "button_group", breadcrumb: "breadcrumb", pagination: "pagination", "toggle-group": "toggle_group", "toggle-button": "toggle_button", accordion: "accordion", tabs: "tabs", "segmented-control": "segmented_control", tree: "tree", list: "list", "list-item": "list_item", "dropdown-menu": "dropdown_menu", "menu-item": "menu_item", separator: "separator", split: "split", resizable: "resizable", "media-surface": "media_surface" };
|
|
@@ -642,11 +758,11 @@ function compileViewFunction(source: string, contract: ViewContract, options: Vi
|
|
|
642
758
|
if (!paragraph && (["list-item", "card", "alert"].includes(node.name) ? node.text.trim() !== "" && contentChildren.length !== 0 : container ? node.text.trim() !== "" : contentChildren.length !== 0)) fail(node, "mixed content is unsupported");
|
|
643
759
|
const textSource = node.name === "icon" ? node.attrs.get("name")! : node.text.trim();
|
|
644
760
|
const rawText = byteText(textSource, node, scope);
|
|
645
|
-
const props:
|
|
761
|
+
const props: ViewProp[] = [v.prop("kind", v.str(kinds[node.name]!)), v.prop("text", rawText === null ? text(textSource, node, scope) : v.str(""))];
|
|
646
762
|
if (node.name === "icon" && (textSource.startsWith("{") || textSource.startsWith("app:"))) {
|
|
647
|
-
props[1] =
|
|
648
|
-
props.push(rawText === null ?
|
|
649
|
-
} else if (rawText !== null) props.push(
|
|
763
|
+
props[1] = v.prop("text", v.str(""));
|
|
764
|
+
props.push(rawText === null ? v.prop("icon", text(textSource, node, scope)) : v.prop("iconBytes", rawText));
|
|
765
|
+
} else if (rawText !== null) props.push(v.prop("textBytes", rawText));
|
|
650
766
|
if (reactions.length) {
|
|
651
767
|
const pill = reactions[0]!;
|
|
652
768
|
if (pill.children.length || !pill.text.trim()) fail(pill, "reactions requires a nonempty text run");
|
|
@@ -654,16 +770,16 @@ function compileViewFunction(source: string, contract: ViewContract, options: Vi
|
|
|
654
770
|
const alignment = pill.attrs.get("text-alignment") ?? "end";
|
|
655
771
|
if (!["start", "center", "end"].includes(alignment)) fail(pill, "unsupported reactions text-alignment");
|
|
656
772
|
const raw = byteText(pill.text.trim(), pill, scope);
|
|
657
|
-
props[1] =
|
|
658
|
-
if (raw !== null) props.push(
|
|
659
|
-
props.push(
|
|
773
|
+
props[1] = v.prop("text", raw === null ? text(pill.text.trim(), pill, scope) : v.str(""));
|
|
774
|
+
if (raw !== null) props.push(v.prop("textBytes", raw));
|
|
775
|
+
props.push(v.prop("textAlignment", v.str(alignment)));
|
|
660
776
|
}
|
|
661
777
|
if (menus.length) {
|
|
662
778
|
const menu = menus[0]!;
|
|
663
779
|
if (nonHitTargetNames.includes(node.name) && !["on-press", "on-double-press", "on-toggle", "on-hold", "on-drag"].some(name => node.attrs.has(name))) fail(menu, "context-menu requires an interactive host");
|
|
664
780
|
if (menu.attrs.size || menu.text.trim()) fail(menu, "context-menu accepts only item declarations");
|
|
665
781
|
const menuId = next++, menuName = `nscvContext${menuId}`;
|
|
666
|
-
output.push(
|
|
782
|
+
output.push(s.const(menuName, v.array([]), "NscContextMenuItem[]"));
|
|
667
783
|
let leaves = 0, items = 0;
|
|
668
784
|
const emitMenu = (nodes: Element[], menuScope: Scope, menuDepth: number): void => {
|
|
669
785
|
if (menuDepth > 64) fail(menu, "context-menu nesting exceeds 64");
|
|
@@ -671,41 +787,42 @@ function compileViewFunction(source: string, contract: ViewContract, options: Vi
|
|
|
671
787
|
const item = nodes[i]!;
|
|
672
788
|
if (item.name === "if") {
|
|
673
789
|
if (item.attrs.size !== 1 || !item.attrs.has("test") || item.text.trim()) fail(item, "if requires only test and child elements");
|
|
674
|
-
|
|
675
|
-
emitMenu(item.children, menuScope, menuDepth + 1)
|
|
790
|
+
const test = condition(item.attrs.get("test")!, item, menuScope);
|
|
791
|
+
const then = block(() => emitMenu(item.children, menuScope, menuDepth + 1));
|
|
676
792
|
const alternate = nodes[i + 1];
|
|
677
793
|
if (alternate?.name === "else") {
|
|
678
794
|
if (alternate.attrs.size || alternate.text.trim()) fail(alternate, "else accepts only child elements");
|
|
679
|
-
output.push(
|
|
680
|
-
}
|
|
795
|
+
output.push(s.if(test, then, block(() => emitMenu(alternate.children, menuScope, menuDepth + 1)))); i++;
|
|
796
|
+
} else output.push(s.if(test, then));
|
|
681
797
|
continue;
|
|
682
798
|
}
|
|
683
799
|
if (item.name === "else") fail(item, "else must immediately follow if");
|
|
684
800
|
leaves++;
|
|
685
801
|
if (leaves > 32) fail(menu, "context-menu takes at most 32 declared leaves");
|
|
686
|
-
let value:
|
|
802
|
+
let value: ViewExpr;
|
|
687
803
|
if (item.name === "separator") {
|
|
688
804
|
if (item.attrs.size || item.children.length || item.text.trim()) fail(item, "context-menu separator must be empty");
|
|
689
|
-
value =
|
|
805
|
+
value = v.object("context_item", [v.prop("label", v.str("")), v.prop("enabled", v.bool(true)), v.prop("separator", v.bool(true))]);
|
|
690
806
|
} else {
|
|
691
807
|
if (item.name !== "menu-item" || item.children.length || !item.text.trim()) fail(item, "context-menu takes labeled menu-item leaves or conditional declarations");
|
|
692
808
|
for (const name of item.attrs.keys()) if (!["on-press", "disabled"].includes(name)) fail(item, `unsupported context-menu item attribute ${name}`);
|
|
693
809
|
const press = item.attrs.get("on-press"), disabled = item.attrs.get("disabled");
|
|
694
810
|
if (press === undefined) fail(item, "context-menu menu-item requires on-press");
|
|
695
811
|
const rawLabel = byteText(item.text.trim(), item, menuScope);
|
|
696
|
-
value =
|
|
812
|
+
value = v.object("context_item", [v.prop("label", rawLabel === null ? text(item.text.trim(), item, menuScope) : v.str("")), ...(rawLabel === null ? [] : [v.prop("labelBytes", rawLabel)]),
|
|
813
|
+
v.prop("enabled", disabled === undefined ? v.bool(true) : v.unary("!", v.group(bound(disabled, "boolean", item, menuScope)), false)), v.prop("separator", v.bool(false)), v.prop("press", event(press, "press", item, menuScope))]);
|
|
697
814
|
items++;
|
|
698
815
|
}
|
|
699
|
-
output.push(
|
|
816
|
+
output.push(s.push(v.id(menuName), value));
|
|
700
817
|
}
|
|
701
818
|
};
|
|
702
819
|
emitMenu(menu.children, scope, 0);
|
|
703
820
|
if (items === 0) fail(menu, "context-menu requires at least one menu-item");
|
|
704
|
-
props.push(
|
|
821
|
+
props.push(v.prop("contextMenu", v.id(menuName)));
|
|
705
822
|
}
|
|
706
823
|
if (paragraph) {
|
|
707
824
|
if (node.attrs.has("wrap") || node.attrs.has("overflow")) fail(node, "span paragraphs always word-wrap; drop wrap and overflow");
|
|
708
|
-
const runs:
|
|
825
|
+
const runs: ViewExpr[] = [];
|
|
709
826
|
let separator = false;
|
|
710
827
|
for (const part of node.content) {
|
|
711
828
|
if ("name" in part && part.name === "context-menu") continue;
|
|
@@ -715,42 +832,42 @@ function compileViewFunction(source: string, contract: ViewContract, options: Vi
|
|
|
715
832
|
const visible = raw.replace(/^[ \t\r\n]+|[ \t\r\n]+$/g, "");
|
|
716
833
|
if (!isSpan && /^[ \t\r\n]/.test(raw)) separator = true;
|
|
717
834
|
if (!visible) { if (/[ \t\r\n]/.test(raw)) separator = true; continue; }
|
|
718
|
-
if (runs.length && separator) runs.push(
|
|
835
|
+
if (runs.length && separator) runs.push(v.object("span", [v.prop("text", v.str(" "))]));
|
|
719
836
|
const rawSpan = byteText(visible, node, scope);
|
|
720
|
-
const fields = [
|
|
721
|
-
if (rawSpan !== null) fields.push(
|
|
837
|
+
const fields = [v.prop("text", rawSpan === null ? text(visible, node, scope) : v.str(""))];
|
|
838
|
+
if (rawSpan !== null) fields.push(v.prop("textBytes", rawSpan));
|
|
722
839
|
if (isSpan) for (const [name, value] of part.attrs) {
|
|
723
|
-
if (["mono", "italic", "underline"].includes(name)) fields.push(
|
|
840
|
+
if (["mono", "italic", "underline"].includes(name)) fields.push(v.prop(name === "mono" ? "monospace" : name, bound(value, "boolean", part, scope)));
|
|
724
841
|
else if (name === "scale") {
|
|
725
842
|
if (!value.startsWith("{") && (!Number.isFinite(Number(value)) || Number(value) <= 0)) fail(part, "span scale requires a positive finite number");
|
|
726
|
-
fields.push(
|
|
843
|
+
fields.push(v.prop("scale", v.call("nscvSpanScale", bound(value, "number", part, scope))));
|
|
727
844
|
} else if (name === "weight") {
|
|
728
845
|
if (value.startsWith("{")) {
|
|
729
846
|
const expr = binding(value, part, scope);
|
|
730
847
|
const members = contract.types.enums?.find(item => item.name === expr.type.name)?.members;
|
|
731
848
|
if (expr.type.kind !== "enum" || !members?.every(member => ["regular", "medium", "bold"].includes(member))) fail(part, "span weight requires regular, medium or bold");
|
|
732
|
-
fields.push(
|
|
849
|
+
fields.push(v.prop("weight", expr.code));
|
|
733
850
|
} else {
|
|
734
851
|
if (!["regular", "medium", "bold"].includes(value)) fail(part, "unsupported span weight");
|
|
735
|
-
fields.push(
|
|
852
|
+
fields.push(v.prop("weight", v.str(value)));
|
|
736
853
|
}
|
|
737
854
|
} else if (name === "foreground") {
|
|
738
855
|
if (value.startsWith("{")) {
|
|
739
856
|
const expr = binding(value, part, scope);
|
|
740
857
|
const members = contract.types.enums?.find(item => item.name === expr.type.name)?.members;
|
|
741
858
|
if (expr.type.kind !== "enum" || !members?.length || !members.every(member => colorTokenNames.includes(member))) fail(part, "span foreground requires its closed token vocabulary");
|
|
742
|
-
fields.push(
|
|
859
|
+
fields.push(v.prop("color", expr.code));
|
|
743
860
|
} else {
|
|
744
861
|
if (!colorTokenNames.includes(value)) fail(part, "unsupported span foreground token");
|
|
745
|
-
fields.push(
|
|
862
|
+
fields.push(v.prop("color", v.str(value)));
|
|
746
863
|
}
|
|
747
864
|
} else fail(part, `unsupported span attribute ${name}; layout, identity and events belong on text`);
|
|
748
865
|
}
|
|
749
|
-
runs.push(
|
|
866
|
+
runs.push(v.object("span", fields));
|
|
750
867
|
separator = !isSpan && /[ \t\r\n]$/.test(raw);
|
|
751
868
|
}
|
|
752
|
-
props[1] =
|
|
753
|
-
props.push(
|
|
869
|
+
props[1] = v.prop("text", v.str(""));
|
|
870
|
+
props.push(v.prop("spans", v.array(runs)));
|
|
754
871
|
}
|
|
755
872
|
if (node.name === "code") {
|
|
756
873
|
const editable = node.attrs.get("editable") ?? "false", wrap = node.attrs.get("wrap") ?? "true";
|
|
@@ -758,22 +875,22 @@ function compileViewFunction(source: string, contract: ViewContract, options: Vi
|
|
|
758
875
|
if (wrap !== "true" && wrap !== "false") fail(node, "code wrap requires a literal boolean");
|
|
759
876
|
if (editable === "false" && node.attrs.has("on-input")) fail(node, "read-only code cannot declare on-input");
|
|
760
877
|
if (editable !== "true" || wrap !== "false") {
|
|
761
|
-
props[0] =
|
|
762
|
-
props.push(
|
|
878
|
+
props[0] = v.prop("kind", v.str("code"));
|
|
879
|
+
props.push(v.prop("codeEditable", editableCode), v.prop("wrap", v.bool(wrap === "true")));
|
|
763
880
|
}
|
|
764
881
|
if (!node.attrs.has("source") || node.text.trim()) fail(node, "code requires a source binding and no element text");
|
|
765
882
|
const expr = binding(node.attrs.get("source")!, node, scope);
|
|
766
883
|
if (expr.type.kind !== "bytes") fail(node, "code source requires UTF-8 bytes");
|
|
767
|
-
props[1] =
|
|
768
|
-
props.push(
|
|
884
|
+
props[1] = v.prop("text", v.str(""));
|
|
885
|
+
props.push(v.prop("textBytes", v.call("nscvTextBytes", v.array([expr.code]))));
|
|
769
886
|
const language = (node.attrs.get("language") ?? "plain").replace(/^[ \t\r\n]+|[ \t\r\n]+$/g, "").toLowerCase();
|
|
770
887
|
const aliases: Record<string, string> = { text: "plain", js: "javascript", mjs: "javascript", ts: "typescript", jsonc: "json", yml: "yaml", sh: "shell", bash: "shell", zsh: "shell", py: "python", rs: "rust", c: "c_like", h: "c_like", cc: "c_like", cpp: "c_like", "c++": "c_like", cs: "c_like", csharp: "c_like", java: "c_like", kotlin: "c_like", swift: "c_like", golang: "go", xml: "html", svg: "html", scss: "css", less: "css", md: "markdown" };
|
|
771
888
|
const canonical = aliases[language] ?? language;
|
|
772
889
|
if (language === "c_like" || !["plain", "zig", "javascript", "typescript", "json", "yaml", "shell", "python", "rust", "c_like", "go", "html", "css", "sql", "jsx", "tsx", "markdown"].includes(canonical)) fail(node, "code language requires a supported literal name");
|
|
773
|
-
props.push(
|
|
890
|
+
props.push(v.prop("codeLanguage", v.str(canonical)));
|
|
774
891
|
const attrs = ["source", "language", "editable", "line-numbers", "added-lines", "removed-lines", "wrap", "width", "height", "min-width", "grow", "label", "key", "global-key", "on-input"];
|
|
775
892
|
for (const name of node.attrs.keys()) if (!attrs.includes(name)) fail(node, `unsupported compiled code attribute ${name}`);
|
|
776
|
-
if (node.attrs.has("on-input")) props.push(
|
|
893
|
+
if (node.attrs.has("on-input")) props.push(v.prop("input", v.cond(editableCode, event(node.attrs.get("on-input")!, "input", node, scope), v.undef())));
|
|
777
894
|
}
|
|
778
895
|
for (const [name, value] of node.attrs) {
|
|
779
896
|
if (node.name === "code" && ["source", "language", "editable", "wrap", "line-numbers", "added-lines", "removed-lines", "on-input"].includes(name)) continue;
|
|
@@ -783,32 +900,32 @@ function compileViewFunction(source: string, contract: ViewContract, options: Vi
|
|
|
783
900
|
const identity = binding(value, node, scope);
|
|
784
901
|
if (identity.type.kind === "bytes") {
|
|
785
902
|
const local = `nscvImage${next++}`;
|
|
786
|
-
output.push(
|
|
787
|
-
props.push(
|
|
788
|
-
} else props.push(
|
|
903
|
+
output.push(s.const(local, v.call("nscvImageIdentity", identity.code)));
|
|
904
|
+
props.push(v.prop("imageLower", v.field(v.id(local), "imageLower")), v.prop("imageUpper", v.field(v.id(local), "imageUpper")));
|
|
905
|
+
} else props.push(v.prop("image", bound(value, "number", node, scope)));
|
|
789
906
|
}
|
|
790
907
|
else if (name === "fit") {
|
|
791
908
|
if (node.name !== "image" || !["contain", "cover", "stretch"].includes(value)) fail(node, "fit requires contain, cover or stretch on image");
|
|
792
|
-
props.push(
|
|
909
|
+
props.push(v.prop("imageFit", v.str(value)));
|
|
793
910
|
}
|
|
794
|
-
else if (["gap", "padding", "grow", "width", "height", "value"].includes(name)) props.push(
|
|
911
|
+
else if (["gap", "padding", "grow", "width", "height", "value"].includes(name)) props.push(v.prop(name, bound(value, "number", node, scope)));
|
|
795
912
|
else if (["padding-top", "padding-bottom", "padding-left", "padding-right"].includes(name)) {
|
|
796
913
|
const property = name.replace(/-([a-z])/g, (_, letter: string) => letter.toUpperCase());
|
|
797
|
-
props.push(
|
|
914
|
+
props.push(v.prop(property, bound(value, "number", node, scope)));
|
|
798
915
|
}
|
|
799
916
|
else if (name === "surface") {
|
|
800
917
|
if (node.name !== "media-surface") fail(node, "surface requires media-surface");
|
|
801
|
-
props.push(
|
|
918
|
+
props.push(v.prop("image", bound(value, "number", node, scope)));
|
|
802
919
|
}
|
|
803
920
|
else if (name === "columns" || name === "virtual-item-extent" || name === "virtualized") {
|
|
804
921
|
if (node.name !== "grid") fail(node, `${name} requires grid`);
|
|
805
922
|
const prop = name === "virtual-item-extent" ? "virtualItemExtent" : name;
|
|
806
923
|
const expr = bound(value, name === "virtualized" ? "boolean" : "number", node, scope);
|
|
807
|
-
props.push(
|
|
924
|
+
props.push(v.prop(prop, name === "columns" ? v.call("nscvGridCount", expr) : expr));
|
|
808
925
|
}
|
|
809
926
|
else if (name === "value-x") {
|
|
810
927
|
if (node.name !== "scroll" || !node.attrs.has("axis") || node.attrs.get("axis") === "vertical") fail(node, "value-x requires scroll with axis=horizontal or both");
|
|
811
|
-
props.push(
|
|
928
|
+
props.push(v.prop("valueX", bound(value, "number", node, scope)));
|
|
812
929
|
} else if (name === "axis" || name === "overscroll") {
|
|
813
930
|
if (node.name !== "scroll") fail(node, `${name} requires scroll`);
|
|
814
931
|
const vocabulary = name === "axis" ? ["vertical", "horizontal", "both"] : ["default", "none", "rubber_band"];
|
|
@@ -816,10 +933,10 @@ function compileViewFunction(source: string, contract: ViewContract, options: Vi
|
|
|
816
933
|
const expr = binding(value, node, scope);
|
|
817
934
|
const members = contract.types.enums?.find(item => item.name === expr.type.name)?.members;
|
|
818
935
|
if (expr.type.kind !== "enum" || !members?.every(member => vocabulary.includes(member))) fail(node, `${name} requires its closed enum vocabulary`);
|
|
819
|
-
props.push(
|
|
936
|
+
props.push(v.prop(name, expr.code));
|
|
820
937
|
} else {
|
|
821
938
|
if (!vocabulary.includes(value)) fail(node, `unsupported ${name}=${value}`);
|
|
822
|
-
props.push(
|
|
939
|
+
props.push(v.prop(name, v.str(value)));
|
|
823
940
|
}
|
|
824
941
|
}
|
|
825
942
|
else if (name === "backdrop-blur") {
|
|
@@ -827,46 +944,46 @@ function compileViewFunction(source: string, contract: ViewContract, options: Vi
|
|
|
827
944
|
const expr = binding(value, node, scope);
|
|
828
945
|
const members = contract.types.enums?.find(item => item.name === expr.type.name)?.members;
|
|
829
946
|
if (expr.type.kind !== "enum" || !members?.every(member => ["none", "sm", "md"].includes(member))) fail(node, "backdrop-blur requires its closed token vocabulary");
|
|
830
|
-
props.push(
|
|
947
|
+
props.push(v.prop("backdropBlur", expr.code));
|
|
831
948
|
} else {
|
|
832
949
|
if (!["none", "sm", "md"].includes(value)) fail(node, "unsupported backdrop-blur token");
|
|
833
|
-
props.push(
|
|
950
|
+
props.push(v.prop("backdropBlur", v.str(value)));
|
|
834
951
|
}
|
|
835
952
|
}
|
|
836
953
|
else if (name === "overflow") {
|
|
837
954
|
if (!["clip", "ellipsis"].includes(value)) fail(node, "unsupported overflow");
|
|
838
|
-
props.push(
|
|
955
|
+
props.push(v.prop("overflow", v.str(value)));
|
|
839
956
|
}
|
|
840
957
|
else if (name === "text-alignment") {
|
|
841
958
|
if (!["start", "center", "end"].includes(value)) fail(node, "unsupported text-alignment");
|
|
842
|
-
props.push(
|
|
959
|
+
props.push(v.prop("textAlignment", v.str(value)));
|
|
843
960
|
}
|
|
844
|
-
else if (name === "min-width" || name === "max-width") props.push(
|
|
961
|
+
else if (name === "min-width" || name === "max-width") props.push(v.prop(name === "min-width" ? "minWidth" : "maxWidth", bound(value, "number", node, scope)));
|
|
845
962
|
else if (["resize-duration", "resize-easing", "resize-origin"].includes(name)) {
|
|
846
963
|
if (node.name !== "split") fail(node, `${name} requires split`);
|
|
847
964
|
if (name !== "resize-duration" && (!node.attrs.has("resize-duration") || node.attrs.get("resize-duration") === "0")) fail(node, `${name} requires nonzero resize-duration`);
|
|
848
|
-
if (name === "resize-duration") props.push(
|
|
965
|
+
if (name === "resize-duration") props.push(v.prop("resizeDuration", v.call("nscvResizeDuration", bound(value, "number", node, scope))));
|
|
849
966
|
else if (name === "resize-origin") {
|
|
850
967
|
if (!Number.isFinite(Number(value)) || Number(value) < 0 || Number(value) > 1) fail(node, "resize-origin requires a literal fraction from 0 to 1");
|
|
851
|
-
props.push(
|
|
968
|
+
props.push(v.prop("resizeOrigin", v.num(String(Number(value)))));
|
|
852
969
|
} else {
|
|
853
970
|
if (!["linear", "standard", "emphasized", "spring"].includes(value)) fail(node, "unsupported resize-easing");
|
|
854
|
-
props.push(
|
|
971
|
+
props.push(v.prop("resizeEasing", v.str(value)));
|
|
855
972
|
}
|
|
856
973
|
}
|
|
857
974
|
else if (name === "pty") {
|
|
858
975
|
if (node.name !== "terminal" || !value.startsWith("{")) fail(node, "pty requires a terminal binding");
|
|
859
976
|
const expr = binding(value, node, scope);
|
|
860
|
-
if (expr.type.kind === "bytes") props.push(
|
|
861
|
-
else if (expr.type.kind === "i64") props.push(
|
|
977
|
+
if (expr.type.kind === "bytes") props.push(v.prop("ptyBytes", v.from(expr.code)));
|
|
978
|
+
else if (expr.type.kind === "i64") props.push(v.prop("pty", v.call("nscvPtyKey", expr.code)));
|
|
862
979
|
else fail(node, "pty requires a byte name or integer key");
|
|
863
980
|
} else if (name === "scrollback") {
|
|
864
981
|
if (node.name !== "terminal") fail(node, "scrollback requires terminal");
|
|
865
|
-
props.push(
|
|
982
|
+
props.push(v.prop("scrollback", v.call("nscvScrollback", bound(value, "number", node, scope))));
|
|
866
983
|
}
|
|
867
984
|
else if (name === "quiet-hover") {
|
|
868
985
|
if (nonHitTargetNames.includes(node.name)) fail(node, "quiet-hover requires a hit target element");
|
|
869
|
-
props.push(
|
|
986
|
+
props.push(v.prop("quietHover", bound(value, "boolean", node, scope)));
|
|
870
987
|
}
|
|
871
988
|
else if (["checked", "disabled", "selected", "window-drag", "wrap", "expanded", "submit-on-enter", "autofocus", "focusable"].includes(name)) {
|
|
872
989
|
if (name === "autofocus" && !["input", "text-field", "search-field", "textarea", "combobox", "terminal"].includes(node.name)) fail(node, "autofocus requires a text-entry widget");
|
|
@@ -874,24 +991,23 @@ function compileViewFunction(source: string, contract: ViewContract, options: Vi
|
|
|
874
991
|
if (name === "submit-on-enter" && node.name !== "textarea") fail(node, "submit-on-enter requires textarea");
|
|
875
992
|
if (name === "wrap" && node.name !== "text") fail(node, "wrap requires text");
|
|
876
993
|
if (name === "expanded" && node.attrs.get("role") !== "treeitem") fail(node, "expanded requires role=treeitem");
|
|
877
|
-
props.push(
|
|
994
|
+
props.push(v.prop(name === "window-drag" ? "windowDrag" : name === "submit-on-enter" ? "submitOnEnter" : name, bound(value, "boolean", node, scope)));
|
|
878
995
|
} else if (name === "tree-level") {
|
|
879
996
|
if (node.attrs.get("role") !== "treeitem") fail(node, "tree-level requires role=treeitem");
|
|
880
|
-
props.push(
|
|
997
|
+
props.push(v.prop("treeLevel", v.call("nscvTreeLevel", bound(value, "number", node, scope))));
|
|
881
998
|
} else if (name === "key" || name === "global-key") {
|
|
882
|
-
|
|
883
|
-
props.push(`${prop}${expr.type.kind === "i64" ? "Int" : expr.type.kind === "bytes" ? "Bytes" : ""}: ${expr.type.kind === "bytes" ? `nscvTextBytes([${expr.code}])` : expr.code}`);
|
|
999
|
+
props.push(keyProp(name, key(value, node, scope)));
|
|
884
1000
|
} else if (["label", "text", "placeholder", "command"].includes(name)) {
|
|
885
1001
|
if (name === "text" && !["input", "text-field", "search-field", "textarea", "combobox", "select", "accordion", "checkbox", "radio", "switch", "toggle", "button", "toggle-button", "table-cell", "status-bar", "badge", "menu-item", "list-item", "avatar", "tooltip", "text", "card", "alert", "dialog", "drawer", "sheet", "data-grid"].includes(node.name)) fail(node, "text requires a text-entry widget, select, accordion header, checkbox or captioned surface");
|
|
886
1002
|
if (name === "placeholder" && !["input", "text-field", "search-field", "textarea", "combobox", "select"].includes(node.name)) fail(node, "placeholder requires a text-entry widget or select");
|
|
887
1003
|
if (name === "text" && node.text.trim()) fail(node, "text attribute cannot be combined with element text");
|
|
888
|
-
const expr = value.startsWith("{") ? binding(value, node, scope) : { code:
|
|
1004
|
+
const expr = value.startsWith("{") ? binding(value, node, scope) : { code: v.str(value), type: { kind: "string" } };
|
|
889
1005
|
if (!["bytes", "string", "enum"].includes(expr.type.kind)) fail(node, `${name} requires text`);
|
|
890
1006
|
const rawBytes = name === "command" ? null : byteText(value, node, scope);
|
|
891
|
-
const display = rawBytes === null ? textValue(expr, node) :
|
|
892
|
-
if (name === "text") props[1] =
|
|
893
|
-
else props.push(
|
|
894
|
-
if (rawBytes !== null) props.push(`${name}Bytes
|
|
1007
|
+
const display = rawBytes === null ? textValue(expr, node) : v.str("");
|
|
1008
|
+
if (name === "text") props[1] = v.prop("text", display);
|
|
1009
|
+
else props.push(v.prop(name, display));
|
|
1010
|
+
if (rawBytes !== null) props.push(v.prop(`${name}Bytes`, rawBytes));
|
|
895
1011
|
}
|
|
896
1012
|
else if (name === "name" && node.name === "icon") {
|
|
897
1013
|
if (!value || /[{}]/.test(value) && !/^\{[^{}]+\}$/.test(value)) fail(node, "icon name requires a literal name or one text binding");
|
|
@@ -901,33 +1017,33 @@ function compileViewFunction(source: string, contract: ViewContract, options: Vi
|
|
|
901
1017
|
if (value.startsWith("{")) {
|
|
902
1018
|
const expr = binding(value, node, scope);
|
|
903
1019
|
if (!["bytes", "string", "enum"].includes(expr.type.kind)) fail(node, "icon requires text");
|
|
904
|
-
if (expr.type.kind === "bytes") props.push(
|
|
905
|
-
else props.push(
|
|
906
|
-
} else { if (/[{}]/.test(value)) fail(node, "invalid icon name"); props.push(
|
|
1020
|
+
if (expr.type.kind === "bytes") props.push(v.prop("iconBytes", v.call("nscvTextBytes", v.array([expr.code]))));
|
|
1021
|
+
else props.push(v.prop("icon", expr.code));
|
|
1022
|
+
} else { if (/[{}]/.test(value)) fail(node, "invalid icon name"); props.push(v.prop("icon", v.str(value))); }
|
|
907
1023
|
}
|
|
908
1024
|
else if (name === "icon-placement") {
|
|
909
1025
|
if (!["leading", "trailing"].includes(value)) fail(node, "unsupported icon-placement");
|
|
910
|
-
props.push(
|
|
1026
|
+
props.push(v.prop("iconPlacement", v.str(value)));
|
|
911
1027
|
}
|
|
912
1028
|
else if (name === "tooltip-delay") {
|
|
913
1029
|
if (node.name !== "tooltip" || !node.attrs.has("anchor")) fail(node, "tooltip-delay requires anchored tooltip");
|
|
914
|
-
const delay = value.startsWith("{") ? binding(value, node, scope) : { code: String(Number(value)), type: { kind: "i64" } };
|
|
1030
|
+
const delay = value.startsWith("{") ? binding(value, node, scope) : { code: v.num(String(Number(value))), type: { kind: "i64" } };
|
|
915
1031
|
if (delay.type.kind !== "i64" || !value.startsWith("{") &&
|
|
916
1032
|
(!/^[0-9]+$/.test(value) || !Number.isInteger(Number(value)) || Number(value) > 2147483647))
|
|
917
1033
|
fail(node, "tooltip-delay requires a non-negative i32 integer");
|
|
918
|
-
props.push(
|
|
1034
|
+
props.push(v.prop("tooltipDelay", v.call("nscvTooltipDelay", delay.code)));
|
|
919
1035
|
}
|
|
920
1036
|
else if (["anchor", "anchor-alignment", "anchor-offset"].includes(name)) {
|
|
921
1037
|
if (node.name !== "dropdown-menu" && node.name !== "tooltip") fail(node, `${name} requires dropdown-menu or tooltip`);
|
|
922
1038
|
if (name !== "anchor" && !node.attrs.has("anchor")) fail(node, `${name} requires anchor`);
|
|
923
1039
|
if (name === "anchor-offset") {
|
|
924
1040
|
if (value.trim() === "" || !Number.isFinite(Number(value))) fail(node, "anchor-offset requires a finite literal number");
|
|
925
|
-
props.push(
|
|
1041
|
+
props.push(v.prop("anchorOffset", v.num(String(Number(value)))));
|
|
926
1042
|
}
|
|
927
1043
|
else {
|
|
928
1044
|
const vocabulary = name === "anchor" ? ["below", "above"] : ["start", "end", "stretch"];
|
|
929
1045
|
if (!vocabulary.includes(value)) fail(node, `unsupported ${name}=${value}`);
|
|
930
|
-
props.push(
|
|
1046
|
+
props.push(v.prop(name === "anchor" ? "anchor" : "anchorAlignment", v.str(value)));
|
|
931
1047
|
}
|
|
932
1048
|
}
|
|
933
1049
|
else if (["main", "cross", "size", "variant", "role", "background", "foreground", "border-color", "focus-ring", "accent", "accent-foreground", "radius"].includes(name)) {
|
|
@@ -937,12 +1053,12 @@ function compileViewFunction(source: string, contract: ViewContract, options: Vi
|
|
|
937
1053
|
const members = contract.types.enums?.find(item => item.name === expr.type.name)?.members;
|
|
938
1054
|
if (expr.type.kind !== "enum" || !members?.length || !members.every(member => vocab[name]!.includes(member))) fail(node, `${name} requires its closed token vocabulary`);
|
|
939
1055
|
const property = name === "border-color" ? "borderColor" : name === "focus-ring" ? "focusRing" : name === "accent-foreground" ? "accentForeground" : name;
|
|
940
|
-
props.push(
|
|
1056
|
+
props.push(v.prop(property, expr.code));
|
|
941
1057
|
continue;
|
|
942
1058
|
}
|
|
943
1059
|
if (!vocab[name]!.includes(value)) fail(node, `unsupported ${name}=${value}`);
|
|
944
1060
|
if (name === "size" && (value === "heading" || value === "display") && node.name !== "text") fail(node, `${value} size requires text`);
|
|
945
|
-
props.push(
|
|
1061
|
+
props.push(v.prop(name === "border-color" ? "borderColor" : name === "focus-ring" ? "focusRing" : name === "accent-foreground" ? "accentForeground" : name, v.str(value)));
|
|
946
1062
|
} else if (["on-press", "on-double-press", "on-hold", "on-toggle", "on-change", "on-drag", "on-scroll", "on-terminal", "on-input", "on-submit", "on-dismiss", "on-resize", "on-hover-enter", "on-hover-leave"].includes(name)) {
|
|
947
1063
|
const channel = name.slice(3);
|
|
948
1064
|
const treeRow = container && node.attrs.get("role") === "treeitem";
|
|
@@ -951,30 +1067,36 @@ function compileViewFunction(source: string, contract: ViewContract, options: Vi
|
|
|
951
1067
|
if (channel === "terminal" && node.name !== "terminal") fail(node, "on-terminal requires terminal");
|
|
952
1068
|
if (channel === "resize" && node.name !== "split") fail(node, "on-resize requires split");
|
|
953
1069
|
if (channel === "change" && node.name === "slider" && contract.msg.arms.find(arm => arm.name === value)?.payload.kind !== "void") {
|
|
954
|
-
props.push(
|
|
955
|
-
} else props.push(
|
|
1070
|
+
props.push(v.prop("valueChange", event(value, "value", node, scope)));
|
|
1071
|
+
} else props.push(v.prop(channel === "double-press" ? "doublePress" : channel === "hover-enter" ? "hoverEnter" : channel === "hover-leave" ? "hoverLeave" : channel, event(value, channel, node, scope)));
|
|
956
1072
|
} else fail(node, `unsupported attribute ${name}`);
|
|
957
1073
|
}
|
|
958
|
-
const id = next
|
|
959
|
-
if (node.name === "tabs" || node.name === "split") output.push(`
|
|
960
|
-
output.push(`
|
|
961
|
-
if (!paragraph) emitChildren(contentChildren, scope, slot, stack, depth + 1); output.push(
|
|
1074
|
+
const id = next++, nodes = v.id("nscvNodes"), built = v.id(`nscvNode${id}`);
|
|
1075
|
+
if (node.name === "tabs" || node.name === "split") output.push(s.const(`nscvStart${id}`, v.length(nodes)));
|
|
1076
|
+
output.push(s.const(`nscvNode${id}`, v.object("node", [v.prop("end", v.num("0")), ...props]), "NscViewNode"), s.push(nodes, built), s.throwIf(v.binary(">", v.length(nodes), v.num("1024"), false), "compiled view exceeds 1024 nodes"));
|
|
1077
|
+
if (!paragraph) emitChildren(contentChildren, scope, slot, stack, depth + 1); output.push(s.assign(v.field(built, "end"), v.length(nodes)));
|
|
962
1078
|
if (node.name === "code") {
|
|
963
|
-
const spec = (name: string):
|
|
1079
|
+
const spec = (name: string): ViewExpr => {
|
|
964
1080
|
const raw = node.attrs.get(name);
|
|
965
|
-
if (raw === undefined) return
|
|
966
|
-
if (!raw.startsWith("{")) return
|
|
1081
|
+
if (raw === undefined) return v.emptyBytes();
|
|
1082
|
+
if (!raw.startsWith("{")) return v.encode(v.str(raw));
|
|
967
1083
|
const value = binding(raw, node, scope);
|
|
968
1084
|
if (value.type.kind !== "bytes") fail(node, `${name} requires one text binding or a literal line list`);
|
|
969
1085
|
return value.code;
|
|
970
1086
|
};
|
|
971
|
-
output.push(
|
|
1087
|
+
output.push(s.expr(v.call("nscvCodeEditor", built, node.attrs.has("line-numbers") ? bound(node.attrs.get("line-numbers")!, "boolean", node, scope) : v.bool(false), spec("added-lines"), spec("removed-lines"))));
|
|
972
1088
|
}
|
|
973
|
-
if (node.name === "tabs") output.push(
|
|
974
|
-
if (node.name === "split") output.push(
|
|
1089
|
+
if (node.name === "tabs") output.push(s.expr(v.call("nscvTabs", nodes, v.id(`nscvStart${id}`))));
|
|
1090
|
+
if (node.name === "split") output.push(s.expr(v.call("nscvSplit", nodes, v.id(`nscvStart${id}`))));
|
|
975
1091
|
};
|
|
976
1092
|
emit(roots[0]!, new Map(), undefined, [], 0);
|
|
977
|
-
|
|
1093
|
+
const prologue = stamp ? "const nscvNodes: NscViewNode[] = [];\nconst nscvSources: number[] = [];\n" : "const nscvNodes: NscViewNode[] = [];\n";
|
|
1094
|
+
// Development records carry the node -> source attribution and the table.
|
|
1095
|
+
const attribution = stamp ? sourceAttribution(sourceTable, sources, entry, stamp) : "";
|
|
1096
|
+
const records = stamp
|
|
1097
|
+
? `'{"format":2,"nodes":' + JSON.stringify(nscvNodes) + ',"sources":' + JSON.stringify(nscvSources) + ',' + ${JSON.stringify(attribution)} + '}'`
|
|
1098
|
+
: "JSON.stringify({ format: 2, nodes: nscvNodes })";
|
|
1099
|
+
return { body: output, attribution, text: `${exported ? "export " : ""}function ${functionName}(nscvVideo: NscVideoPlaybackState, nscvVirtual?: NscVirtualBuild): Uint8Array {\n${prologue}${printStatements(output).join("\n")}\nif (nscvVirtual?.requestsOnly) return new TextEncoder().encode(JSON.stringify({ format: 1, requests: nscvVirtual.requests }));\nif (nscvVirtual !== undefined && (nscvVirtual.requests.length !== nscvVirtual.ranges.length || nscvVirtual.consumed.length !== nscvVirtual.requests.length)) throw new Error("virtual window context count mismatch");\nif (nscvNodes.length === 0 || nscvNodes[0]!.end !== nscvNodes.length) throw new Error("compiled view requires one rendered root");\nreturn new TextEncoder().encode(${records});\n}\n` };
|
|
978
1100
|
}
|
|
979
1101
|
|
|
980
1102
|
const viewPrelude = "\n// Portable Native components compiled beside the committed model.\n" + portableSourceText;
|