trebol 0.1.0 → 0.1.2

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.
Files changed (160) hide show
  1. package/README.md +18 -3
  2. package/dist/cli.js +13790 -11417
  3. package/dist/fonts/OFL.txt +93 -0
  4. package/dist/fonts/inter-cyrillic-ext.woff2 +0 -0
  5. package/dist/fonts/inter-cyrillic.woff2 +0 -0
  6. package/dist/fonts/inter-greek-ext.woff2 +0 -0
  7. package/dist/fonts/inter-greek.woff2 +0 -0
  8. package/dist/fonts/inter-latin-ext.woff2 +0 -0
  9. package/dist/fonts/inter-latin.woff2 +0 -0
  10. package/dist/fonts/inter-vietnamese.woff2 +0 -0
  11. package/dist/lib/core/access.d.ts +10 -0
  12. package/dist/lib/core/access.js +210 -0
  13. package/dist/lib/core/base.d.ts +4 -0
  14. package/dist/lib/core/base.js +50 -0
  15. package/dist/lib/core/bitset.d.ts +9 -0
  16. package/dist/lib/core/bitset.js +75 -0
  17. package/dist/lib/core/boot.d.ts +18 -0
  18. package/dist/lib/core/boot.js +73 -0
  19. package/dist/lib/core/compare.d.ts +9 -0
  20. package/dist/lib/core/compare.js +156 -0
  21. package/dist/lib/core/ctx.d.ts +36 -0
  22. package/dist/lib/core/ctx.js +251 -0
  23. package/dist/lib/core/dec.d.ts +28 -0
  24. package/dist/lib/core/dec.js +158 -0
  25. package/dist/lib/core/errors.d.ts +15 -0
  26. package/dist/lib/core/errors.js +44 -0
  27. package/dist/lib/core/escapes.d.ts +14 -0
  28. package/dist/lib/core/escapes.js +80 -0
  29. package/dist/lib/core/eval.d.ts +220 -0
  30. package/dist/lib/core/eval.js +666 -0
  31. package/dist/lib/core/func.d.ts +68 -0
  32. package/dist/lib/core/func.js +129 -0
  33. package/dist/lib/core/hash.d.ts +5 -0
  34. package/dist/lib/core/hash.js +155 -0
  35. package/dist/lib/core/index.d.ts +18 -0
  36. package/dist/lib/core/index.js +20 -0
  37. package/dist/lib/core/int64.d.ts +22 -0
  38. package/dist/lib/core/int64.js +113 -0
  39. package/dist/lib/core/kinds.d.ts +67 -0
  40. package/dist/lib/core/kinds.js +134 -0
  41. package/dist/lib/core/lexer.d.ts +13 -0
  42. package/dist/lib/core/lexer.js +702 -0
  43. package/dist/lib/core/mezz.d.ts +1 -0
  44. package/dist/lib/core/mezz.js +360 -0
  45. package/dist/lib/core/mold.d.ts +20 -0
  46. package/dist/lib/core/mold.js +434 -0
  47. package/dist/lib/core/natives/control.d.ts +1 -0
  48. package/dist/lib/core/natives/control.js +374 -0
  49. package/dist/lib/core/natives/csv.d.ts +15 -0
  50. package/dist/lib/core/natives/csv.js +315 -0
  51. package/dist/lib/core/natives/data.d.ts +11 -0
  52. package/dist/lib/core/natives/data.js +367 -0
  53. package/dist/lib/core/natives/define.d.ts +17 -0
  54. package/dist/lib/core/natives/define.js +47 -0
  55. package/dist/lib/core/natives/encode.d.ts +3 -0
  56. package/dist/lib/core/natives/encode.js +187 -0
  57. package/dist/lib/core/natives/json.d.ts +5 -0
  58. package/dist/lib/core/natives/json.js +273 -0
  59. package/dist/lib/core/natives/math.d.ts +3 -0
  60. package/dist/lib/core/natives/math.js +500 -0
  61. package/dist/lib/core/natives/parse.d.ts +1 -0
  62. package/dist/lib/core/natives/parse.js +600 -0
  63. package/dist/lib/core/natives/series.d.ts +4 -0
  64. package/dist/lib/core/natives/series.js +519 -0
  65. package/dist/lib/core/natives/text.d.ts +1 -0
  66. package/dist/lib/core/natives/text.js +487 -0
  67. package/dist/lib/core/natives/xml.d.ts +9 -0
  68. package/dist/lib/core/natives/xml.js +242 -0
  69. package/dist/lib/core/series.d.ts +26 -0
  70. package/dist/lib/core/series.js +73 -0
  71. package/dist/lib/core/symbols.d.ts +9 -0
  72. package/dist/lib/core/symbols.js +44 -0
  73. package/dist/lib/core/values.d.ts +206 -0
  74. package/dist/lib/core/values.js +223 -0
  75. package/dist/lib/host/browser.d.ts +4 -0
  76. package/dist/lib/host/browser.js +4974 -0
  77. package/dist/lib/host/chart.d.ts +26 -0
  78. package/dist/lib/host/chart.js +158 -0
  79. package/dist/lib/host/compress.d.ts +1 -0
  80. package/dist/lib/host/compress.js +37 -0
  81. package/dist/lib/host/devlog.d.ts +11 -0
  82. package/dist/lib/host/devlog.js +13 -0
  83. package/dist/lib/host/devtools.d.ts +23 -0
  84. package/dist/lib/host/devtools.js +626 -0
  85. package/dist/lib/host/draw.d.ts +31 -0
  86. package/dist/lib/host/draw.js +348 -0
  87. package/dist/lib/host/fonts/OFL.txt +93 -0
  88. package/dist/lib/host/fonts/inter-cyrillic-ext.woff2 +0 -0
  89. package/dist/lib/host/fonts/inter-cyrillic.woff2 +0 -0
  90. package/dist/lib/host/fonts/inter-greek-ext.woff2 +0 -0
  91. package/dist/lib/host/fonts/inter-greek.woff2 +0 -0
  92. package/dist/lib/host/fonts/inter-latin-ext.woff2 +0 -0
  93. package/dist/lib/host/fonts/inter-latin.woff2 +0 -0
  94. package/dist/lib/host/fonts/inter-vietnamese.woff2 +0 -0
  95. package/dist/lib/host/fonts.d.ts +11 -0
  96. package/dist/lib/host/fonts.js +29 -0
  97. package/dist/lib/host/icons.d.ts +3 -0
  98. package/dist/lib/host/icons.js +97 -0
  99. package/dist/lib/host/index.d.ts +40 -0
  100. package/dist/lib/host/index.js +192 -0
  101. package/dist/lib/host/js.d.ts +5 -0
  102. package/dist/lib/host/js.js +130 -0
  103. package/dist/lib/host/locale.d.ts +40 -0
  104. package/dist/lib/host/locale.js +241 -0
  105. package/dist/lib/host/markdown.d.ts +30 -0
  106. package/dist/lib/host/markdown.js +161 -0
  107. package/dist/lib/host/modules.d.ts +14 -0
  108. package/dist/lib/host/modules.js +300 -0
  109. package/dist/lib/host/natives.d.ts +6 -0
  110. package/dist/lib/host/natives.js +98 -0
  111. package/dist/lib/host/net.d.ts +12 -0
  112. package/dist/lib/host/net.js +549 -0
  113. package/dist/lib/host/orbit.d.ts +121 -0
  114. package/dist/lib/host/orbit.js +374 -0
  115. package/dist/lib/host/page.d.ts +9 -0
  116. package/dist/lib/host/page.js +55 -0
  117. package/dist/lib/host/rows.d.ts +29 -0
  118. package/dist/lib/host/rows.js +109 -0
  119. package/dist/lib/host/scheduler.d.ts +51 -0
  120. package/dist/lib/host/scheduler.js +197 -0
  121. package/dist/lib/host/theme.d.ts +58 -0
  122. package/dist/lib/host/theme.js +1667 -0
  123. package/dist/lib/ui/i18n.trb +117 -0
  124. package/dist/lib/ui/test.trb +565 -0
  125. package/dist/lib/ui/ui.trb +1678 -0
  126. package/dist/trebol.js +1427 -138
  127. package/editors/vscode/README.md +15 -0
  128. package/editors/vscode/language-configuration.json +28 -0
  129. package/editors/vscode/package.json +42 -0
  130. package/editors/vscode/syntaxes/trebol.tmLanguage.json +237 -0
  131. package/package.json +23 -7
  132. package/src/core/boot.ts +1 -1
  133. package/src/core/func.ts +4 -0
  134. package/src/core/natives/control.ts +1 -1
  135. package/src/core/natives/series.ts +9 -1
  136. package/src/core/natives/text.ts +2 -0
  137. package/src/host/browser.ts +1235 -83
  138. package/src/host/devlog.ts +16 -0
  139. package/src/host/devtools.ts +641 -0
  140. package/src/host/fonts/OFL.txt +93 -0
  141. package/src/host/fonts/inter-cyrillic-ext.woff2 +0 -0
  142. package/src/host/fonts/inter-cyrillic.woff2 +0 -0
  143. package/src/host/fonts/inter-greek-ext.woff2 +0 -0
  144. package/src/host/fonts/inter-greek.woff2 +0 -0
  145. package/src/host/fonts/inter-latin-ext.woff2 +0 -0
  146. package/src/host/fonts/inter-latin.woff2 +0 -0
  147. package/src/host/fonts/inter-vietnamese.woff2 +0 -0
  148. package/src/host/fonts.ts +39 -0
  149. package/src/host/index.ts +5 -3
  150. package/src/host/js.ts +127 -0
  151. package/src/host/markdown.ts +9 -0
  152. package/src/host/modules.ts +41 -2
  153. package/src/host/net.ts +9 -1
  154. package/src/host/orbit.ts +432 -0
  155. package/src/host/rows.ts +30 -0
  156. package/src/host/scheduler.ts +62 -36
  157. package/src/host/theme.ts +270 -6
  158. package/src/ui/i18n.trb +10 -0
  159. package/src/ui/test.trb +565 -0
  160. package/src/ui/ui.trb +564 -90
@@ -0,0 +1,197 @@
1
+ // The task scheduler: runs Trebol tasks cooperatively on the JS event loop.
2
+ //
3
+ // Each task runs for a slice of steps, then the scheduler gives control back
4
+ // to the host (browser rendering, network callbacks) before continuing. A
5
+ // task that returns `await` is parked until its promise settles. Event
6
+ // handlers, `spawn` and timers all create new tasks here.
7
+ //
8
+ // Host resources (tasks, sockets, timers) are exposed to Trebol as handle!
9
+ // cells holding only a kind and a numeric id; the objects live in a table
10
+ // here, so no JS object is stored inside a Trebol value.
11
+ import { K, UNSET, intern, newTask, run, resume, resumeError, makeError, bindSeries, copyDeep, context, } from "../core/index.js";
12
+ /** Steps a task runs before the scheduler yields to the host. */
13
+ export const SLICE = 10_000;
14
+ let nextId = 1;
15
+ const resources = new Map();
16
+ /** Stores a host object and returns a handle! cell for it. */
17
+ export function makeHandle(kind, obj) {
18
+ const id = nextId++;
19
+ resources.set(id, obj);
20
+ return { t: K.Handle, p: { kind, id } };
21
+ }
22
+ export function handleOf(c, kind) {
23
+ if (c.t !== K.Handle)
24
+ return null;
25
+ const h = c.p;
26
+ return h.kind === kind ? resources.get(h.id) ?? null : null;
27
+ }
28
+ export function dropHandle(c) {
29
+ if (c.t === K.Handle)
30
+ resources.delete(c.p.id);
31
+ }
32
+ const schedulers = new WeakMap();
33
+ export function makeScheduler(rt, onError) {
34
+ const sch = { rt, ready: [], byTask: new WeakMap(), onError, pumping: false, live: 0, afterPump: null };
35
+ schedulers.set(rt, sch);
36
+ return sch;
37
+ }
38
+ /** The scheduler running a task's runtime. */
39
+ export function schedulerOf(t) {
40
+ const sch = schedulers.get(t.rt);
41
+ // Host natives only run inside a scheduler; anything else is a host bug.
42
+ if (sch === undefined)
43
+ throw new Error('no scheduler for this runtime');
44
+ return sch;
45
+ }
46
+ /** Starts a task for `code` (a block bound to the user context or deeper). */
47
+ export function spawn(sch, s, i = 0) {
48
+ const task = newTask(sch.rt, s, i);
49
+ const ht = { id: nextId++, task, state: 'ready', result: UNSET, error: null, abort: null, watchers: [] };
50
+ sch.byTask.set(task, ht);
51
+ sch.live++;
52
+ sch.ready.push(ht);
53
+ schedule(sch);
54
+ return ht;
55
+ }
56
+ /**
57
+ * Runs a handler block as a new task with some words bound to values, e.g.
58
+ * `value` for a message. The block is copied, so handlers never share state.
59
+ */
60
+ export function spawnHandler(sch, body, words) {
61
+ const names = Object.keys(words);
62
+ const s = copyDeep(body.s, body.i);
63
+ if (names.length > 0) {
64
+ const ctx = context(names.map(n => intern(n)), names.map(n => words[n]));
65
+ bindSeries(s, ctx);
66
+ }
67
+ return spawn(sch, s, 0);
68
+ }
69
+ /**
70
+ * Runs `fn` after the host had a chance to do its own work (render, I/O).
71
+ * setImmediate where it exists (Node); a MessageChannel in browsers, which
72
+ * unlike setTimeout(0) is not clamped to 4 ms.
73
+ */
74
+ const defer = (() => {
75
+ const g = globalThis;
76
+ if (typeof g.setImmediate === 'function')
77
+ return (fn) => void g.setImmediate(fn);
78
+ if (typeof MessageChannel === 'undefined')
79
+ return (fn) => void setTimeout(fn, 0);
80
+ const ch = new MessageChannel();
81
+ const queue = [];
82
+ ch.port1.onmessage = () => queue.shift()?.();
83
+ return (fn) => {
84
+ queue.push(fn);
85
+ ch.port2.postMessage(null);
86
+ };
87
+ })();
88
+ function schedule(sch) {
89
+ if (sch.pumping)
90
+ return;
91
+ sch.pumping = true;
92
+ defer(() => pump(sch));
93
+ }
94
+ function finish(sch, ht, state) {
95
+ ht.state = state;
96
+ ht.abort = null;
97
+ sch.live--;
98
+ if (state === 'failed' && ht.watchers.length === 0)
99
+ sch.onError(ht.error, ht);
100
+ for (const w of ht.watchers.splice(0))
101
+ w(ht);
102
+ }
103
+ /** Runs one slice of a ready task; true if it yielded and has more to do. */
104
+ function slice(sch, ht) {
105
+ const r = run(ht.task, SLICE);
106
+ switch (r.k) {
107
+ case 'done':
108
+ ht.result = r.v;
109
+ finish(sch, ht, 'done');
110
+ break;
111
+ case 'error':
112
+ ht.error = r.e;
113
+ finish(sch, ht, 'failed');
114
+ break;
115
+ case 'yield':
116
+ return true;
117
+ case 'await':
118
+ ht.state = 'waiting';
119
+ r.p.then(v => {
120
+ if (ht.state !== 'waiting')
121
+ return;
122
+ ht.abort = null;
123
+ resume(ht.task, v);
124
+ ht.state = 'ready';
125
+ sch.ready.push(ht);
126
+ schedule(sch);
127
+ }, (e) => {
128
+ if (ht.state !== 'waiting')
129
+ return;
130
+ ht.abort = null;
131
+ resumeError(ht.task, asError(e));
132
+ ht.state = 'ready';
133
+ sch.ready.push(ht);
134
+ schedule(sch);
135
+ });
136
+ break;
137
+ }
138
+ return false;
139
+ }
140
+ /** Runs ready tasks, one slice each, until none is ready; then yields. */
141
+ function pump(sch) {
142
+ sch.pumping = false;
143
+ const batch = sch.ready.splice(0);
144
+ for (const ht of batch) {
145
+ if (ht.state === 'ready' && slice(sch, ht))
146
+ sch.ready.push(ht);
147
+ }
148
+ sch.afterPump?.();
149
+ if (sch.ready.length > 0)
150
+ schedule(sch);
151
+ }
152
+ /** Slices a task started with `runNow` may take before it waits its turn like the others. */
153
+ const NOW_SLICES = 10;
154
+ /**
155
+ * Starts a task and runs it at once, before any other, until it ends or waits;
156
+ * a long one goes on in turn with the rest. The browser host runs reactions
157
+ * this way, between a round and the repaint that follows it.
158
+ */
159
+ export function runNow(sch, s, i = 0) {
160
+ const task = newTask(sch.rt, s, i);
161
+ const ht = { id: nextId++, task, state: 'ready', result: UNSET, error: null, abort: null, watchers: [] };
162
+ sch.byTask.set(task, ht);
163
+ sch.live++;
164
+ let more = true;
165
+ for (let n = 0; more && n < NOW_SLICES; n++)
166
+ more = slice(sch, ht);
167
+ if (more) {
168
+ sch.ready.push(ht);
169
+ schedule(sch);
170
+ }
171
+ return ht;
172
+ }
173
+ /** A rejected promise's reason as a Trebol error!. */
174
+ export function asError(e) {
175
+ if (typeof e === 'object' && e !== null && e.t === K.Error)
176
+ return e;
177
+ const msg = e instanceof Error ? e.message : String(e);
178
+ return makeError('access', 'host', msg);
179
+ }
180
+ /** Cancels a task: aborts what it waits on; watchers see a `cancelled` error. */
181
+ export function cancelTask(sch, ht) {
182
+ if (ht.state === 'done' || ht.state === 'failed' || ht.state === 'cancelled')
183
+ return;
184
+ ht.abort?.();
185
+ ht.error = makeError('access', 'cancelled', 'the task was cancelled');
186
+ finish(sch, ht, 'cancelled');
187
+ }
188
+ /** A promise for the task's result; rejects with its error. */
189
+ export function taskResult(ht) {
190
+ return new Promise((resolve, reject) => {
191
+ const settle = (t) => (t.state === 'done' ? resolve(t.result) : reject(t.error ?? makeError('access', 'cancelled', 'the task was cancelled')));
192
+ if (ht.state === 'done' || ht.state === 'failed' || ht.state === 'cancelled')
193
+ settle(ht);
194
+ else
195
+ ht.watchers.push(settle);
196
+ });
197
+ }
@@ -0,0 +1,58 @@
1
+ /** modern: today's flat controls; classic recalls Rebol/View, cupertino the Mac, fusion Qt, fluent Windows 11. */
2
+ export type Skin = 'modern' | 'classic' | 'cupertino' | 'fusion' | 'fluent';
3
+ export interface ThemeParams {
4
+ /** Hue of the primary color, 0-360. */
5
+ base: number;
6
+ /** Hue of the accent color, 0-360. */
7
+ accent: number;
8
+ neutral: 'cool' | 'neutral' | 'warm';
9
+ density: 'compact' | 'normal' | 'comfortable';
10
+ radius: 'sharp' | 'soft' | 'round';
11
+ font: string;
12
+ /** Font of titles and headings; the same as `font` if it is not given. */
13
+ headings: string | null;
14
+ mode: 'light' | 'dark' | 'auto';
15
+ /** How much color the primary and accent have, 0-1; 0 is black and white. */
16
+ color: number;
17
+ /** Thickness of borders, in px. */
18
+ line: 1 | 2;
19
+ /** Whether panels and buttons cast a shadow. */
20
+ depth: 'flat' | 'soft' | 'lifted';
21
+ /** How the same icons are drawn. */
22
+ icons: 'fine' | 'round' | 'sharp' | 'bold';
23
+ /** How the controls are built: see SKINS. */
24
+ skin: Skin;
25
+ }
26
+ export declare const DEFAULT_THEME: ThemeParams;
27
+ /** The theme parameters that take a word, and the words each takes. */
28
+ export declare const THEME_WORDS: Record<string, readonly string[]>;
29
+ /** The looks: complete sets of parameters with a name. `theme [look: civic base: 200]` starts from one. */
30
+ export declare const LOOKS: Record<string, Partial<ThemeParams>>;
31
+ export interface Oklch {
32
+ l: number;
33
+ c: number;
34
+ h: number;
35
+ }
36
+ /** Roles every theme defines, for both modes. */
37
+ export declare const ROLES: readonly ["bg", "surface", "raised", "border", "border-strong", "text", "muted", "primary", "primary-hover", "on-primary", "accent", "on-accent", "danger", "on-danger", "success", "warning", "focus"];
38
+ export type Role = (typeof ROLES)[number];
39
+ /** Text/background pairs that must reach WCAG AA (4.5:1). */
40
+ export declare const CONTRAST_PAIRS: ReadonlyArray<[Role, Role]>;
41
+ /** The palette for one mode. Lightness values are fixed; hues come from the theme. */
42
+ export declare function palette(t: ThemeParams, dark: boolean): Record<Role, Oklch>;
43
+ /** Linear sRGB of an OKLCH color, clamped to the gamut. */
44
+ export declare function toLinearRgb({ l, c, h }: Oklch): [number, number, number];
45
+ export declare function luminance(c: Oklch): number;
46
+ /** WCAG contrast ratio between two colors. */
47
+ export declare function contrast(x: Oklch, y: Oklch): number;
48
+ /** Luminance of `c` at `alpha` over a pixel of gray `px` (0 black, 1 white), blended as browsers do. */
49
+ export declare function luminanceOver(c: Oklch, alpha: number, px: number): number;
50
+ /** The worst contrast of `fg` on the background at `alpha` over any image: a black or a white one. */
51
+ export declare function contrastOverImage(fg: Oklch, bg: Oklch, alpha: number): number;
52
+ /**
53
+ * The alpha of the veil over a backdrop: the least that keeps text AA on any image. Muted text would
54
+ * need a veil almost opaque, so on a backdrop it takes the color of the text.
55
+ */
56
+ export declare function scrimAlpha(p: Record<Role, Oklch>): number;
57
+ /** The full stylesheet for a theme: tokens, then the fixed component rules. */
58
+ export declare function themeCss(t: ThemeParams): string;