trebol 0.1.1 → 0.2.0
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/README.md +12 -1
- package/dist/cli.js +17097 -12238
- package/dist/fonts/OFL.txt +93 -0
- package/dist/fonts/inter-cyrillic-ext.woff2 +0 -0
- package/dist/fonts/inter-cyrillic.woff2 +0 -0
- package/dist/fonts/inter-greek-ext.woff2 +0 -0
- package/dist/fonts/inter-greek.woff2 +0 -0
- package/dist/fonts/inter-latin-ext.woff2 +0 -0
- package/dist/fonts/inter-latin.woff2 +0 -0
- package/dist/fonts/inter-vietnamese.woff2 +0 -0
- package/dist/lib/core/boot.js +12 -8
- package/dist/lib/core/eval.js +2 -2
- package/dist/lib/core/func.d.ts +4 -0
- package/dist/lib/core/machine/checks.d.ts +1 -0
- package/dist/lib/core/machine/checks.js +250 -0
- package/dist/lib/core/machine/events.d.ts +1 -0
- package/dist/lib/core/machine/events.js +365 -0
- package/dist/lib/core/machine/running.d.ts +1 -0
- package/dist/lib/core/machine/running.js +180 -0
- package/dist/lib/core/machine/shared.d.ts +1 -0
- package/dist/lib/core/machine/shared.js +123 -0
- package/dist/lib/core/machine/snapshot.d.ts +1 -0
- package/dist/lib/core/machine/snapshot.js +244 -0
- package/dist/lib/core/machine/spec.d.ts +1 -0
- package/dist/lib/core/machine/spec.js +274 -0
- package/dist/lib/core/machine/words.d.ts +1 -0
- package/dist/lib/core/machine/words.js +98 -0
- package/dist/lib/core/machine.d.ts +1 -0
- package/dist/lib/core/machine.js +17 -0
- package/dist/lib/core/mezz.js +31 -0
- package/dist/lib/core/natives/control.js +2 -2
- package/dist/lib/core/natives/data.js +6 -1
- package/dist/lib/core/natives/series.js +59 -9
- package/dist/lib/core/natives/text.js +3 -1
- package/dist/lib/host/browser/calendar.d.ts +34 -0
- package/dist/lib/host/browser/calendar.js +268 -0
- package/dist/lib/host/browser/count.d.ts +15 -0
- package/dist/lib/host/browser/count.js +48 -0
- package/dist/lib/host/browser/dialogs.d.ts +39 -0
- package/dist/lib/host/browser/dialogs.js +443 -0
- package/dist/lib/host/browser/drag.d.ts +70 -0
- package/dist/lib/host/browser/drag.js +268 -0
- package/dist/lib/host/browser/drawings.d.ts +30 -0
- package/dist/lib/host/browser/drawings.js +176 -0
- package/dist/lib/host/browser/events.d.ts +16 -0
- package/dist/lib/host/browser/events.js +125 -0
- package/dist/lib/host/browser/faces.d.ts +35 -0
- package/dist/lib/host/browser/faces.js +131 -0
- package/dist/lib/host/browser/fields.d.ts +16 -0
- package/dist/lib/host/browser/fields.js +322 -0
- package/dist/lib/host/browser/keys.d.ts +29 -0
- package/dist/lib/host/browser/keys.js +151 -0
- package/dist/lib/host/browser/lookup.d.ts +25 -0
- package/dist/lib/host/browser/lookup.js +275 -0
- package/dist/lib/host/browser/menus.d.ts +20 -0
- package/dist/lib/host/browser/menus.js +149 -0
- package/dist/lib/host/browser/mount.d.ts +92 -0
- package/dist/lib/host/browser/mount.js +448 -0
- package/dist/lib/host/browser/orbits.d.ts +90 -0
- package/dist/lib/host/browser/orbits.js +379 -0
- package/dist/lib/host/browser/page.d.ts +13 -0
- package/dist/lib/host/browser/page.js +35 -0
- package/dist/lib/host/browser/richtext.d.ts +19 -0
- package/dist/lib/host/browser/richtext.js +379 -0
- package/dist/lib/host/browser/row-faces.d.ts +5 -0
- package/dist/lib/host/browser/row-faces.js +74 -0
- package/dist/lib/host/browser/screens.d.ts +37 -0
- package/dist/lib/host/browser/screens.js +140 -0
- package/dist/lib/host/browser/swipe.d.ts +31 -0
- package/dist/lib/host/browser/swipe.js +178 -0
- package/dist/lib/host/browser/sync.d.ts +24 -0
- package/dist/lib/host/browser/sync.js +450 -0
- package/dist/lib/host/browser/tables.d.ts +65 -0
- package/dist/lib/host/browser/tables.js +505 -0
- package/dist/lib/host/browser/times.d.ts +14 -0
- package/dist/lib/host/browser/times.js +114 -0
- package/dist/lib/host/browser/toolbars.d.ts +17 -0
- package/dist/lib/host/browser/toolbars.js +129 -0
- package/dist/lib/host/browser/trees.d.ts +20 -0
- package/dist/lib/host/browser/trees.js +235 -0
- package/dist/lib/host/browser/uploads.d.ts +29 -0
- package/dist/lib/host/browser/uploads.js +370 -0
- package/dist/lib/host/browser/widget.d.ts +13 -0
- package/dist/lib/host/browser/widget.js +105 -0
- package/dist/lib/host/browser.d.ts +25 -0
- package/dist/lib/host/browser.js +129 -3964
- package/dist/lib/host/devlog.d.ts +11 -0
- package/dist/lib/host/devlog.js +13 -0
- package/dist/lib/host/devtools/console.d.ts +9 -0
- package/dist/lib/host/devtools/console.js +104 -0
- package/dist/lib/host/devtools/faces.d.ts +25 -0
- package/dist/lib/host/devtools/faces.js +169 -0
- package/dist/lib/host/devtools/log.d.ts +3 -0
- package/dist/lib/host/devtools/log.js +32 -0
- package/dist/lib/host/devtools/machines.d.ts +34 -0
- package/dist/lib/host/devtools/machines.js +212 -0
- package/dist/lib/host/devtools/read.d.ts +13 -0
- package/dist/lib/host/devtools/read.js +52 -0
- package/dist/lib/host/devtools/state.d.ts +90 -0
- package/dist/lib/host/devtools/state.js +123 -0
- package/dist/lib/host/devtools.d.ts +19 -0
- package/dist/lib/host/devtools.js +238 -0
- package/dist/lib/host/fonts/OFL.txt +93 -0
- package/dist/lib/host/fonts/inter-cyrillic-ext.woff2 +0 -0
- package/dist/lib/host/fonts/inter-cyrillic.woff2 +0 -0
- package/dist/lib/host/fonts/inter-greek-ext.woff2 +0 -0
- package/dist/lib/host/fonts/inter-greek.woff2 +0 -0
- package/dist/lib/host/fonts/inter-latin-ext.woff2 +0 -0
- package/dist/lib/host/fonts/inter-latin.woff2 +0 -0
- package/dist/lib/host/fonts/inter-vietnamese.woff2 +0 -0
- package/dist/lib/host/fonts.d.ts +11 -0
- package/dist/lib/host/fonts.js +30 -0
- package/dist/lib/host/icons.js +4 -0
- package/dist/lib/host/index.d.ts +5 -1
- package/dist/lib/host/index.js +110 -5
- package/dist/lib/host/js.d.ts +5 -0
- package/dist/lib/host/js.js +130 -0
- package/dist/lib/host/locale.d.ts +18 -0
- package/dist/lib/host/locale.js +75 -5
- package/dist/lib/host/markdown.d.ts +6 -0
- package/dist/lib/host/markdown.js +18 -0
- package/dist/lib/host/modules.d.ts +4 -0
- package/dist/lib/host/modules.js +157 -20
- package/dist/lib/host/motion.d.ts +32 -0
- package/dist/lib/host/motion.js +190 -0
- package/dist/lib/host/net.js +14 -3
- package/dist/lib/host/orbit.d.ts +121 -0
- package/dist/lib/host/orbit.js +374 -0
- package/dist/lib/host/palette.d.ts +17 -0
- package/dist/lib/host/palette.js +131 -0
- package/dist/lib/host/rows.d.ts +14 -0
- package/dist/lib/host/rows.js +29 -0
- package/dist/lib/host/scheduler.d.ts +8 -0
- package/dist/lib/host/scheduler.js +64 -36
- package/dist/lib/host/share.d.ts +31 -0
- package/dist/lib/host/share.js +134 -0
- package/dist/lib/host/theme/classic.d.ts +1 -0
- package/dist/lib/host/theme/classic.js +120 -0
- package/dist/lib/host/theme/cupertino.d.ts +1 -0
- package/dist/lib/host/theme/cupertino.js +65 -0
- package/dist/lib/host/theme/data.d.ts +1 -0
- package/dist/lib/host/theme/data.js +109 -0
- package/dist/lib/host/theme/devtools.d.ts +1 -0
- package/dist/lib/host/theme/devtools.js +166 -0
- package/dist/lib/host/theme/dialogs.d.ts +1 -0
- package/dist/lib/host/theme/dialogs.js +63 -0
- package/dist/lib/host/theme/fluent.d.ts +1 -0
- package/dist/lib/host/theme/fluent.js +64 -0
- package/dist/lib/host/theme/forms.d.ts +1 -0
- package/dist/lib/host/theme/forms.js +239 -0
- package/dist/lib/host/theme/fusion.d.ts +1 -0
- package/dist/lib/host/theme/fusion.js +66 -0
- package/dist/lib/host/theme/layout.d.ts +1 -0
- package/dist/lib/host/theme/layout.js +166 -0
- package/dist/lib/host/theme/media.d.ts +1 -0
- package/dist/lib/host/theme/media.js +106 -0
- package/dist/lib/host/theme/navigation.d.ts +1 -0
- package/dist/lib/host/theme/navigation.js +36 -0
- package/dist/lib/host/theme/panes.d.ts +1 -0
- package/dist/lib/host/theme/panes.js +99 -0
- package/dist/lib/host/theme/popups.d.ts +1 -0
- package/dist/lib/host/theme/popups.js +149 -0
- package/dist/lib/host/theme/responsive.d.ts +1 -0
- package/dist/lib/host/theme/responsive.js +103 -0
- package/dist/lib/host/theme/toasts.d.ts +1 -0
- package/dist/lib/host/theme/toasts.js +62 -0
- package/dist/lib/host/theme.js +19 -1179
- package/dist/lib/ui/dialect/events.trb +276 -0
- package/dist/lib/ui/dialect/faces.trb +135 -0
- package/dist/lib/ui/dialect/layout.trb +283 -0
- package/dist/lib/ui/dialect/palette.trb +87 -0
- package/dist/lib/ui/dialect/problems.trb +174 -0
- package/dist/lib/ui/dialect/reactions.trb +198 -0
- package/dist/lib/ui/dialect/rows.trb +208 -0
- package/dist/lib/ui/dialect/screens.trb +293 -0
- package/dist/lib/ui/dialect/steps.trb +203 -0
- package/dist/lib/ui/dialect/validation.trb +137 -0
- package/dist/lib/ui/dialect/view.trb +110 -0
- package/dist/lib/ui/dialect/walking.trb +98 -0
- package/dist/lib/ui/dialect/words.trb +70 -0
- package/dist/lib/ui/i18n.trb +23 -0
- package/dist/lib/ui/test.trb +12 -0
- package/dist/lib/ui/testing/actions.trb +421 -0
- package/dist/lib/ui/testing/faces.trb +148 -0
- package/dist/lib/ui/testing/server.trb +73 -0
- package/dist/lib/ui/ui.trb +25 -1234
- package/dist/trebol.js +4882 -1088
- package/editors/vscode/README.md +15 -0
- package/editors/vscode/language-configuration.json +28 -0
- package/editors/vscode/package.json +42 -0
- package/editors/vscode/syntaxes/trebol.tmLanguage.json +237 -0
- package/package.json +7 -3
- package/src/core/boot.ts +10 -7
- package/src/core/eval.ts +2 -2
- package/src/core/func.ts +4 -0
- package/src/core/machine/checks.ts +251 -0
- package/src/core/machine/events.ts +366 -0
- package/src/core/machine/running.ts +181 -0
- package/src/core/machine/shared.ts +124 -0
- package/src/core/machine/snapshot.ts +245 -0
- package/src/core/machine/spec.ts +275 -0
- package/src/core/machine/words.ts +99 -0
- package/src/core/machine.ts +20 -0
- package/src/core/mezz.ts +31 -0
- package/src/core/natives/control.ts +2 -2
- package/src/core/natives/data.ts +6 -1
- package/src/core/natives/parse.ts +1 -1
- package/src/core/natives/series.ts +55 -11
- package/src/core/natives/text.ts +2 -1
- package/src/host/browser/calendar.ts +264 -0
- package/src/host/browser/count.ts +48 -0
- package/src/host/browser/dialogs.ts +439 -0
- package/src/host/browser/drag.ts +278 -0
- package/src/host/browser/drawings.ts +178 -0
- package/src/host/browser/events.ts +117 -0
- package/src/host/browser/faces.ts +143 -0
- package/src/host/browser/fields.ts +315 -0
- package/src/host/browser/keys.ts +145 -0
- package/src/host/browser/lookup.ts +262 -0
- package/src/host/browser/menus.ts +146 -0
- package/src/host/browser/mount.ts +420 -0
- package/src/host/browser/orbits.ts +418 -0
- package/src/host/browser/page.ts +43 -0
- package/src/host/browser/richtext.ts +346 -0
- package/src/host/browser/row-faces.ts +75 -0
- package/src/host/browser/screens.ts +142 -0
- package/src/host/browser/swipe.ts +168 -0
- package/src/host/browser/sync.ts +354 -0
- package/src/host/browser/tables.ts +461 -0
- package/src/host/browser/times.ts +115 -0
- package/src/host/browser/toolbars.ts +126 -0
- package/src/host/browser/trees.ts +205 -0
- package/src/host/browser/uploads.ts +367 -0
- package/src/host/browser/widget.ts +103 -0
- package/src/host/browser.ts +128 -3747
- package/src/host/devlog.ts +16 -0
- package/src/host/devtools/console.ts +102 -0
- package/src/host/devtools/faces.ts +160 -0
- package/src/host/devtools/log.ts +34 -0
- package/src/host/devtools/machines.ts +208 -0
- package/src/host/devtools/read.ts +58 -0
- package/src/host/devtools/state.ts +148 -0
- package/src/host/devtools.ts +238 -0
- package/src/host/fonts/OFL.txt +93 -0
- package/src/host/fonts/inter-cyrillic-ext.woff2 +0 -0
- package/src/host/fonts/inter-cyrillic.woff2 +0 -0
- package/src/host/fonts/inter-greek-ext.woff2 +0 -0
- package/src/host/fonts/inter-greek.woff2 +0 -0
- package/src/host/fonts/inter-latin-ext.woff2 +0 -0
- package/src/host/fonts/inter-latin.woff2 +0 -0
- package/src/host/fonts/inter-vietnamese.woff2 +0 -0
- package/src/host/fonts.ts +40 -0
- package/src/host/icons.ts +4 -0
- package/src/host/index.ts +110 -5
- package/src/host/js.ts +127 -0
- package/src/host/locale.ts +71 -5
- package/src/host/markdown.ts +18 -0
- package/src/host/modules.ts +150 -22
- package/src/host/motion.ts +204 -0
- package/src/host/net.ts +13 -3
- package/src/host/orbit.ts +432 -0
- package/src/host/palette.ts +139 -0
- package/src/host/rows.ts +30 -0
- package/src/host/scheduler.ts +67 -36
- package/src/host/share.ts +168 -0
- package/src/host/theme/classic.ts +121 -0
- package/src/host/theme/cupertino.ts +66 -0
- package/src/host/theme/data.ts +110 -0
- package/src/host/theme/devtools.ts +167 -0
- package/src/host/theme/dialogs.ts +64 -0
- package/src/host/theme/fluent.ts +65 -0
- package/src/host/theme/forms.ts +240 -0
- package/src/host/theme/fusion.ts +67 -0
- package/src/host/theme/layout.ts +167 -0
- package/src/host/theme/media.ts +107 -0
- package/src/host/theme/navigation.ts +37 -0
- package/src/host/theme/panes.ts +100 -0
- package/src/host/theme/popups.ts +150 -0
- package/src/host/theme/responsive.ts +104 -0
- package/src/host/theme/toasts.ts +63 -0
- package/src/host/theme.ts +19 -1179
- package/src/ui/dialect/events.trb +276 -0
- package/src/ui/dialect/faces.trb +135 -0
- package/src/ui/dialect/layout.trb +283 -0
- package/src/ui/dialect/palette.trb +87 -0
- package/src/ui/dialect/problems.trb +174 -0
- package/src/ui/dialect/reactions.trb +198 -0
- package/src/ui/dialect/rows.trb +208 -0
- package/src/ui/dialect/screens.trb +293 -0
- package/src/ui/dialect/steps.trb +203 -0
- package/src/ui/dialect/validation.trb +137 -0
- package/src/ui/dialect/view.trb +110 -0
- package/src/ui/dialect/walking.trb +98 -0
- package/src/ui/dialect/words.trb +70 -0
- package/src/ui/i18n.trb +23 -0
- package/src/ui/test.trb +12 -0
- package/src/ui/testing/actions.trb +421 -0
- package/src/ui/testing/faces.trb +148 -0
- package/src/ui/testing/server.trb +73 -0
- package/src/ui/ui.trb +25 -1234
package/src/host/index.ts
CHANGED
|
@@ -5,18 +5,20 @@ import './natives.ts';
|
|
|
5
5
|
import './net.ts';
|
|
6
6
|
import './page.ts';
|
|
7
7
|
import './compress.ts';
|
|
8
|
+
import './share.ts';
|
|
8
9
|
import './locale.ts';
|
|
9
10
|
import { setLoader, defaultLoader, type Loader } from './modules.ts';
|
|
10
11
|
import {
|
|
11
|
-
|
|
12
|
+
makeRuntime, load, bindSeries, runSync, scriptStart, errorMessage, form,
|
|
12
13
|
LIBRARY_FILES, type Cell, type ObjectCell, type Runtime,
|
|
13
14
|
} from '../core/index.ts';
|
|
14
15
|
import { makeScheduler, spawn, taskResult, type Scheduler } from './scheduler.ts';
|
|
15
16
|
|
|
16
17
|
export { output } from './natives.ts';
|
|
17
18
|
export { fileHandle } from './net.ts';
|
|
19
|
+
export { setShareEnv, type ShareEnv, type ChannelLike, type LocksLike, type Envelope } from './share.ts';
|
|
18
20
|
export { page } from './page.ts';
|
|
19
|
-
export { mapAt, formatLocal, activeLanguage, shown, translate, uiText, editLocal, readLocal, readDate, weekStart, fileSize, LOCAL_KINDS } from './locale.ts';
|
|
21
|
+
export { mapAt, formatLocal, activeLanguage, shown, translate, uiText, editLocal, readLocal, readDate, readRange, readTime, weekStart, fileSize, LOCAL_KINDS, amountOf, countStep } from './locale.ts';
|
|
20
22
|
export type { Loader } from './modules.ts';
|
|
21
23
|
export * from './scheduler.ts';
|
|
22
24
|
|
|
@@ -31,8 +33,106 @@ fetch: func [
|
|
|
31
33
|
url [url! string!]
|
|
32
34
|
/with "method, headers, body, form, multipart, timeout, full"
|
|
33
35
|
options [block! object!]
|
|
36
|
+
/local answer
|
|
34
37
|
][
|
|
35
|
-
|
|
38
|
+
if all [with block? options] [options: make object! options]
|
|
39
|
+
answer: http-request url either with [options] [none]
|
|
40
|
+
; what was kept of the same path is old now
|
|
41
|
+
if all [with find [post put patch delete] attempt [to word! select options 'method]] [ui-cache-touched url]
|
|
42
|
+
answer
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
; --- answers kept: cached and refresh ---------------------------------------------
|
|
46
|
+
;
|
|
47
|
+
; Each url read with cached has an entry; a reaction that reads cached %/x has
|
|
48
|
+
; the entry's version among its sources, so it runs again when an answer comes.
|
|
49
|
+
; ui-cache-news counts every answer, for reactions whose url is not written out.
|
|
50
|
+
|
|
51
|
+
ui-cache-entries: copy []
|
|
52
|
+
ui-cache-news: make object! [version: 0]
|
|
53
|
+
|
|
54
|
+
ui-cache-entry: func ["The entry of what cached keeps for a url, made the first time." url /local key e] [
|
|
55
|
+
key: form url
|
|
56
|
+
unless e: select/case ui-cache-entries key [
|
|
57
|
+
e: make object! [url: none answer: none version: 0 stale: true asking: false again: false task: none]
|
|
58
|
+
e/url: url
|
|
59
|
+
append ui-cache-entries reduce [key e]
|
|
60
|
+
]
|
|
61
|
+
e
|
|
62
|
+
]
|
|
63
|
+
|
|
64
|
+
; The path of a url, without the server, the query or the fragment.
|
|
65
|
+
ui-cache-path: func ["The path of a url, without the server, the query or the fragment." url /local s i] [
|
|
66
|
+
s: form url
|
|
67
|
+
if i: find s "#" [s: copy/part s i]
|
|
68
|
+
if i: find s "?" [s: copy/part s i]
|
|
69
|
+
if i: find s "://" [s: any [find skip i 3 "/" copy "/"]]
|
|
70
|
+
s
|
|
71
|
+
]
|
|
72
|
+
|
|
73
|
+
; Asks again behind; one request at a time per url, and one more if asked meanwhile.
|
|
74
|
+
ui-cache-ask: func ["Asks again behind for the answer of an entry of cached." e] [
|
|
75
|
+
e/stale: false
|
|
76
|
+
either e/asking [e/again: true] [
|
|
77
|
+
e/asking: true
|
|
78
|
+
e/task: spawn compose [ui-cache-answer (e)]
|
|
79
|
+
]
|
|
80
|
+
]
|
|
81
|
+
|
|
82
|
+
ui-cache-answer: func ["Asks for the answer of an entry of cached, and keeps it." e /local r] [
|
|
83
|
+
forever [
|
|
84
|
+
e/again: false
|
|
85
|
+
r: try [fetch e/url]
|
|
86
|
+
either error? :r [
|
|
87
|
+
; the last answer stays; the next read asks again
|
|
88
|
+
e/stale: true
|
|
89
|
+
print ["cached:" form e/url "--" r/message]
|
|
90
|
+
] [
|
|
91
|
+
e/answer: :r
|
|
92
|
+
e/version: e/version + 1
|
|
93
|
+
ui-cache-news/version: ui-cache-news/version + 1
|
|
94
|
+
]
|
|
95
|
+
unless e/again [break]
|
|
96
|
+
]
|
|
97
|
+
e/asking: false
|
|
98
|
+
]
|
|
99
|
+
|
|
100
|
+
; Every url kept is asked again: the page came back into view.
|
|
101
|
+
ui-caches-again: func ["Asks again for every answer cached keeps." /local key e] [
|
|
102
|
+
foreach [key e] ui-cache-entries [ui-cache-ask e]
|
|
103
|
+
]
|
|
104
|
+
|
|
105
|
+
; Every url kept is old: it is asked again the next time it is read (a screen is entered).
|
|
106
|
+
ui-caches-old: func ["Marks every answer cached keeps as old: the next read asks again." /local key e] [
|
|
107
|
+
foreach [key e] ui-cache-entries [e/stale: true]
|
|
108
|
+
]
|
|
109
|
+
|
|
110
|
+
; A post, put, patch or delete to url: what was kept of its path, or of a path
|
|
111
|
+
; above or under it (/api/clients for /api/clients/7), is asked again.
|
|
112
|
+
ui-cache-touched: func ["Asks again for what cached keeps of the path of url, or above or under it." url /local key e a b] [
|
|
113
|
+
a: ui-cache-path url
|
|
114
|
+
foreach [key e] ui-cache-entries [
|
|
115
|
+
b: ui-cache-path e/url
|
|
116
|
+
if any [a = b find/match a join b "/" find/match b join a "/"] [ui-cache-ask e]
|
|
117
|
+
]
|
|
118
|
+
]
|
|
119
|
+
|
|
120
|
+
cached: func [
|
|
121
|
+
"The last answer to a GET of url, at once (none the first time), and asks again behind: a reaction (is, react) that reads it runs again when the answer comes."
|
|
122
|
+
url [url! string!]
|
|
123
|
+
/local e
|
|
124
|
+
][
|
|
125
|
+
e: ui-cache-entry url
|
|
126
|
+
if e/stale [ui-cache-ask e]
|
|
127
|
+
e/answer
|
|
128
|
+
]
|
|
129
|
+
|
|
130
|
+
refresh: func [
|
|
131
|
+
"Asks again for an answer kept by cached, now; reactions that read it run again when it comes."
|
|
132
|
+
url [url! string!]
|
|
133
|
+
][
|
|
134
|
+
ui-cache-ask ui-cache-entry url
|
|
135
|
+
exit
|
|
36
136
|
]
|
|
37
137
|
|
|
38
138
|
show-progress: func [
|
|
@@ -183,7 +283,10 @@ export function startHost(opts: HostOptions = {}): Host {
|
|
|
183
283
|
const onError = opts.onError ?? (e => console.error(report(e)));
|
|
184
284
|
const rt = makeRuntime();
|
|
185
285
|
setLoader(rt, opts.loader ?? defaultLoader());
|
|
186
|
-
|
|
286
|
+
// in lib, as the core's mezzanines: a word a program sets never takes the place of one of them
|
|
287
|
+
const code = load(HOST_MEZZANINES, { file: 'host' });
|
|
288
|
+
bindSeries(code.s, rt.lib, true);
|
|
289
|
+
const r = runSync(rt, code);
|
|
187
290
|
// Host mezzanines failing to load is a bug in Trebol itself.
|
|
188
291
|
if (r.k === 'error') throw new Error(`host mezzanines failed: ${errorMessage(r.e)}`);
|
|
189
292
|
const sch = makeScheduler(rt, e => onError(e));
|
|
@@ -194,8 +297,10 @@ export function startHost(opts: HostOptions = {}): Host {
|
|
|
194
297
|
export const UI_SOURCE = new URL('../ui/ui.trb', import.meta.url).href;
|
|
195
298
|
/** URL of the languages module (i18n, tr, translated). */
|
|
196
299
|
export const I18N_SOURCE = new URL('../ui/i18n.trb', import.meta.url).href;
|
|
300
|
+
/** URL of the words of screen tests (click, type, see...), which `trebol test` loads. */
|
|
301
|
+
export const TEST_SOURCE = new URL('../ui/test.trb', import.meta.url).href;
|
|
197
302
|
// Trebol's own code: its errors point at the program's call (see LIBRARY_FILES).
|
|
198
|
-
LIBRARY_FILES.add('host').add(UI_SOURCE).add(I18N_SOURCE);
|
|
303
|
+
LIBRARY_FILES.add('host').add(UI_SOURCE).add(I18N_SOURCE).add(TEST_SOURCE);
|
|
199
304
|
|
|
200
305
|
/** Loads languages and the UI dialect (view, layout, stylize, theme...) into the user context. */
|
|
201
306
|
export function startUi(host: Host): Promise<Cell> {
|
package/src/host/js.ts
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
// JavaScript modules as Trebol modules.
|
|
2
|
+
//
|
|
3
|
+
// `import %pdf-tools.js` loads a JavaScript module and gives an object with a
|
|
4
|
+
// function for each function it exports, named in Trebol's way (countPages is
|
|
5
|
+
// count-pages), and the other exports as values:
|
|
6
|
+
//
|
|
7
|
+
// pdf: import %pdf-tools.js
|
|
8
|
+
// pages: pdf/count-pages %contract.pdf
|
|
9
|
+
//
|
|
10
|
+
// Arguments and results cross as JSON, so text, numbers, blocks, maps, logic
|
|
11
|
+
// and none keep their shape; a promise is awaited while other tasks run, and
|
|
12
|
+
// a JavaScript exception is a Trebol error. A function's spec comes from its
|
|
13
|
+
// parameters, or from a `spec` property written as Trebol source:
|
|
14
|
+
//
|
|
15
|
+
// export function countPages(file) { ... }
|
|
16
|
+
// countPages.spec = '["The pages of a PDF." file [file! url!]]';
|
|
17
|
+
//
|
|
18
|
+
// No Trebol value holds the JavaScript function: each is a native built on one
|
|
19
|
+
// hidden native, carrying the function's index in a table here as its data.
|
|
20
|
+
|
|
21
|
+
import {
|
|
22
|
+
K, NONE, registerNative, load, parseSpec, parseJson, toJson, context, ctxAdd, intern, makeError, value, throwError,
|
|
23
|
+
type Cell, type BlockCell, type ObjectCell, type NativeDef, type Step,
|
|
24
|
+
} from '../core/index.ts';
|
|
25
|
+
|
|
26
|
+
type JsFunction = (...args: unknown[]) => unknown;
|
|
27
|
+
|
|
28
|
+
const functions: JsFunction[] = [];
|
|
29
|
+
|
|
30
|
+
const call = registerNative({
|
|
31
|
+
name: 'js-function', hidden: true, spec: parseSpec(load('[]').s.data[0] as BlockCell) as NativeDef['spec'],
|
|
32
|
+
specBlock: load('[]').s.data[0] as BlockCell,
|
|
33
|
+
}, (_t, f, v): Step => {
|
|
34
|
+
if (f.phase === 1) return value(v!);
|
|
35
|
+
const fn = functions[f.def.data!]!;
|
|
36
|
+
const name = f.def.name;
|
|
37
|
+
let result: unknown;
|
|
38
|
+
try {
|
|
39
|
+
result = fn(...f.args.map(a => JSON.parse(toJson(a)) as unknown));
|
|
40
|
+
} catch (e) {
|
|
41
|
+
return throwError('script', 'js-error', `${name}: ${(e as Error)?.message ?? String(e)}`);
|
|
42
|
+
}
|
|
43
|
+
if (!(result instanceof Promise)) return toTrebol(result, name);
|
|
44
|
+
f.phase = 1;
|
|
45
|
+
return {
|
|
46
|
+
k: 'await',
|
|
47
|
+
p: result.then(r => {
|
|
48
|
+
const step = toTrebol(r, name);
|
|
49
|
+
if (step.k === 'value') return step.v;
|
|
50
|
+
throw (step as Extract<Step, { k: 'throw' }>).e.v;
|
|
51
|
+
}, (e: unknown) => {
|
|
52
|
+
throw makeError('script', 'js-error', `${name}: ${(e as Error)?.message ?? String(e)}`);
|
|
53
|
+
}),
|
|
54
|
+
};
|
|
55
|
+
}).p as NativeDef;
|
|
56
|
+
|
|
57
|
+
function toTrebol(r: unknown, name: string): Step {
|
|
58
|
+
if (r === undefined) return value(NONE);
|
|
59
|
+
let json: string | undefined;
|
|
60
|
+
try {
|
|
61
|
+
json = JSON.stringify(r);
|
|
62
|
+
} catch {
|
|
63
|
+
json = undefined;
|
|
64
|
+
}
|
|
65
|
+
if (json === undefined) return throwError('script', 'js-error', `${name} gave a value that is not JSON`);
|
|
66
|
+
const v = parseJson(json);
|
|
67
|
+
return typeof v === 'string' ? throwError('script', 'js-error', `${name}: ${v}`) : value(v);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** countPages is count-pages, as Trebol names words. */
|
|
71
|
+
export function trebolName(js: string): string {
|
|
72
|
+
return js.replace(/([a-z0-9])([A-Z])/g, '$1-$2').replace(/_/g, '-').toLowerCase();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** The parameter names of a plain function or an arrow, or none when they cannot be read. */
|
|
76
|
+
function params(fn: JsFunction): string[] | null {
|
|
77
|
+
const src = fn.toString();
|
|
78
|
+
const m = /^\s*(?:async\s+)?([\w$]+)\s*=>/.exec(src) ?? /^[^(=]*\(([^)]*)\)/.exec(src);
|
|
79
|
+
if (m === null) return null;
|
|
80
|
+
const names = m[1]!.split(',').map(p => p.trim()).filter(p => p !== '');
|
|
81
|
+
return names.every(p => /^[A-Za-z_$][\w$]*$/.test(p)) ? names.map(trebolName) : null;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function specOf(fn: JsFunction, name: string): { spec: NativeDef['spec']; specBlock: BlockCell } | string {
|
|
85
|
+
const own = (fn as { spec?: unknown }).spec;
|
|
86
|
+
const src = typeof own === 'string' ? own
|
|
87
|
+
: `[${(params(fn) ?? Array.from({ length: fn.length }, (_, i) => `arg${i + 1}`)).join(' ')}]`;
|
|
88
|
+
let specBlock: BlockCell;
|
|
89
|
+
try {
|
|
90
|
+
specBlock = load(src).s.data[0] as BlockCell;
|
|
91
|
+
} catch (e) {
|
|
92
|
+
return `${name}: bad spec: ${(e as Error).message}`;
|
|
93
|
+
}
|
|
94
|
+
if (specBlock?.t !== K.Block) return `${name}: its spec is not a block`;
|
|
95
|
+
const spec = parseSpec(specBlock);
|
|
96
|
+
return typeof spec === 'string' ? `${name}: ${spec}` : { spec, specBlock };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** The object for a loaded JavaScript module, or an error message. */
|
|
100
|
+
export function jsModule(ns: Record<string, unknown>): ObjectCell | string {
|
|
101
|
+
const ctx = context();
|
|
102
|
+
ctx.self = true;
|
|
103
|
+
for (const [key, x] of Object.entries(ns)) {
|
|
104
|
+
if (key === 'default') continue;
|
|
105
|
+
const name = trebolName(key);
|
|
106
|
+
let v: Cell;
|
|
107
|
+
if (typeof x === 'function') {
|
|
108
|
+
const spec = specOf(x as JsFunction, name);
|
|
109
|
+
if (typeof spec === 'string') return spec;
|
|
110
|
+
functions.push(x as JsFunction);
|
|
111
|
+
v = { t: K.Native, p: { ...call, ...spec, name, data: functions.length - 1, hidden: true } satisfies NativeDef };
|
|
112
|
+
} else {
|
|
113
|
+
let json: string | undefined;
|
|
114
|
+
try {
|
|
115
|
+
json = JSON.stringify(x);
|
|
116
|
+
} catch {
|
|
117
|
+
json = undefined;
|
|
118
|
+
}
|
|
119
|
+
if (json === undefined) continue;
|
|
120
|
+
const parsed = parseJson(json);
|
|
121
|
+
if (typeof parsed === 'string') continue;
|
|
122
|
+
v = parsed;
|
|
123
|
+
}
|
|
124
|
+
ctxAdd(ctx, intern(name), v);
|
|
125
|
+
}
|
|
126
|
+
return { t: K.Object, ctx };
|
|
127
|
+
}
|
package/src/host/locale.ts
CHANGED
|
@@ -3,10 +3,44 @@
|
|
|
3
3
|
// core keeps its own language-neutral form/mold.
|
|
4
4
|
|
|
5
5
|
import {
|
|
6
|
-
K, native, value, string, form, text, formatDec, spell, ctxFind, intern,
|
|
6
|
+
K, native, value, string, form, text, formatDec, spell, ctxFind, intern, integer, decimal, percent, money, dec,
|
|
7
|
+
decToNumber, I64,
|
|
7
8
|
type Cell, type ObjectCell, type StringCell, type WordCell, type MapCell, type Runtime,
|
|
8
9
|
} from '../core/index.ts';
|
|
9
10
|
|
|
11
|
+
/** How much a number, a percent or an amount is, as a JavaScript number; null for anything else. */
|
|
12
|
+
export function amountOf(c: Cell): number | null {
|
|
13
|
+
switch (c.t) {
|
|
14
|
+
case K.Integer: return I64.toNumber(c.v);
|
|
15
|
+
case K.Decimal: case K.Percent: return c.v;
|
|
16
|
+
case K.Money: return decToNumber(c.v);
|
|
17
|
+
default: return null;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** The digits after the point a decimal number is written with. */
|
|
22
|
+
function fractionDigits(n: number): number {
|
|
23
|
+
const m = /\.(\d+)|e-(\d+)/.exec(String(n));
|
|
24
|
+
return m === null ? 0 : m[1] !== undefined ? m[1].length : Number(m[2]);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* What a number that counts shows at x on its way to `to` (a text with `count`): a value of the kind of `to`,
|
|
29
|
+
* with no more digits than it has, so the run reads as the number it ends on.
|
|
30
|
+
*/
|
|
31
|
+
export function countStep(to: Cell, x: number): Cell {
|
|
32
|
+
switch (to.t) {
|
|
33
|
+
case K.Integer: return integer(Math.round(x));
|
|
34
|
+
case K.Money: return money(dec(BigInt(Math.round(x * 10 ** to.v.e)), to.v.e), to.cur);
|
|
35
|
+
case K.Decimal: case K.Percent: {
|
|
36
|
+
const k = 10 ** fractionDigits(to.v);
|
|
37
|
+
const v = Math.round(x * k) / k;
|
|
38
|
+
return to.t === K.Decimal ? decimal(v) : percent(v);
|
|
39
|
+
}
|
|
40
|
+
default: return to;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
10
44
|
/** The kinds `localize` formats; anything else shows as `form` gives it. */
|
|
11
45
|
export const LOCAL_KINDS: ReadonlySet<number> = new Set([K.Date, K.Time, K.Integer, K.Decimal, K.Percent, K.Money]);
|
|
12
46
|
|
|
@@ -121,6 +155,38 @@ export function readDate(s: string, lang: string): string | null {
|
|
|
121
155
|
return `${y}-${String(m).padStart(2, '0')}-${String(d).padStart(2, '0')}`;
|
|
122
156
|
}
|
|
123
157
|
|
|
158
|
+
/**
|
|
159
|
+
* Two dates typed for a range, as `2026-11-03 2026-11-10` with the start first, or null. They are
|
|
160
|
+
* written apart by a dash with spaces around it, as `3/11/2026 – 10/11/2026` or `3/11/2026 - 10/11/2026`,
|
|
161
|
+
* or by a space alone; a range typed end first is turned round.
|
|
162
|
+
*/
|
|
163
|
+
export function readRange(s: string, lang: string): string | null {
|
|
164
|
+
let parts = s.trim().split(/\s*[\u2013\u2014]\s*|\s+-\s+/);
|
|
165
|
+
if (parts.length === 1) parts = s.trim().split(/\s+/);
|
|
166
|
+
if (parts.length !== 2) return null;
|
|
167
|
+
const [a, b] = parts.map(p => readDate(p, lang));
|
|
168
|
+
if (a == null || b == null) return null;
|
|
169
|
+
return a <= b ? `${a} ${b}` : `${b} ${a}`;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* A time typed in any language, as `09:30`, or null: `9:30`, `9.30`, `9h30`, `930`, `21`, and with am or pm,
|
|
174
|
+
* `9:30 pm` or `9pm`. Minutes go from 00 to 59 and hours from 0 to 23, or 1 to 12 with am or pm.
|
|
175
|
+
*/
|
|
176
|
+
export function readTime(s: string): string | null {
|
|
177
|
+
const t = /^(\d{1,2})(?:\s*[:.h]\s*(\d{2})|(\d{2}))?\s*h?\s*(?:([ap])\.?\s*m\.?)?$/i.exec(s.trim());
|
|
178
|
+
if (t === null) return null;
|
|
179
|
+
let h = Number(t[1]);
|
|
180
|
+
const m = Number(t[2] ?? t[3] ?? 0);
|
|
181
|
+
const half = t[4]?.toLowerCase();
|
|
182
|
+
if (m > 59) return null;
|
|
183
|
+
if (half !== undefined) {
|
|
184
|
+
if (h < 1 || h > 12) return null;
|
|
185
|
+
h = (h % 12) + (half === 'p' ? 12 : 0);
|
|
186
|
+
} else if (h > 23) return null;
|
|
187
|
+
return `${String(h).padStart(2, '0')}:${String(m).padStart(2, '0')}`;
|
|
188
|
+
}
|
|
189
|
+
|
|
124
190
|
/** The first day of the week in a language, 1 for Monday to 7 for Sunday. */
|
|
125
191
|
export function weekStart(lang: string): number {
|
|
126
192
|
try {
|
|
@@ -136,8 +202,8 @@ export function weekStart(lang: string): number {
|
|
|
136
202
|
|
|
137
203
|
/** The active language: `i18n/language` of the user context, `en` if there is none. */
|
|
138
204
|
export function activeLanguage(rt: Runtime): string {
|
|
139
|
-
const i = ctxFind(rt.
|
|
140
|
-
const obj = i < 0 ? null : rt.
|
|
205
|
+
const i = ctxFind(rt.lib, intern('i18n'));
|
|
206
|
+
const obj = i < 0 ? null : rt.lib.vals[i]!;
|
|
141
207
|
if (obj === null || obj.t !== K.Object) return 'en';
|
|
142
208
|
const o = obj as ObjectCell;
|
|
143
209
|
const j = ctxFind(o.ctx, intern('language'));
|
|
@@ -149,8 +215,8 @@ export function activeLanguage(rt: Runtime): string {
|
|
|
149
215
|
|
|
150
216
|
/** A field of the user's `i18n` object, or null. */
|
|
151
217
|
function i18nField(rt: Runtime, name: string): Cell | null {
|
|
152
|
-
const i = ctxFind(rt.
|
|
153
|
-
const obj = i < 0 ? null : rt.
|
|
218
|
+
const i = ctxFind(rt.lib, intern('i18n'));
|
|
219
|
+
const obj = i < 0 ? null : rt.lib.vals[i]!;
|
|
154
220
|
if (obj === null || obj.t !== K.Object) return null;
|
|
155
221
|
const j = ctxFind((obj as ObjectCell).ctx, intern(name));
|
|
156
222
|
return j < 0 ? null : (obj as ObjectCell).ctx.vals[j]!;
|
package/src/host/markdown.ts
CHANGED
|
@@ -144,7 +144,25 @@ export function printMd(blocks: Block[]): string {
|
|
|
144
144
|
}).join('\n\n');
|
|
145
145
|
}
|
|
146
146
|
|
|
147
|
+
/** Markdown with plain text added at its end, in its last paragraph or a new one after a heading or list. */
|
|
148
|
+
export function appendText(src: string, text: string): string {
|
|
149
|
+
const blocks = parseMd(src);
|
|
150
|
+
const last = blocks[blocks.length - 1];
|
|
151
|
+
if (last?.k === 'p') last.kids.push({ k: 'text', text });
|
|
152
|
+
else blocks.push({ k: 'p', kids: [{ k: 'text', text }] });
|
|
153
|
+
return printMd(blocks);
|
|
154
|
+
}
|
|
155
|
+
|
|
147
156
|
/** Only web and mail links open; anything else (javascript:...) is shown as plain text. */
|
|
148
157
|
export function safeHref(href: string): boolean {
|
|
149
158
|
return /^(https?:|mailto:)/i.test(href.trim());
|
|
150
159
|
}
|
|
160
|
+
|
|
161
|
+
/** What a link face may open: web, mail and phone addresses and addresses with no scheme (a path,
|
|
162
|
+
* a #screen); anything else (javascript:, data:...) is null. Spaces before it and tabs or line
|
|
163
|
+
* breaks inside it are dropped first, as the browser does, so "java\tscript:" is caught too. */
|
|
164
|
+
export function linkHref(href: string): string | null {
|
|
165
|
+
const plain = href.replace(/[\t\n\r]/g, '').replace(/^[\x00-\x20]+/, '');
|
|
166
|
+
const scheme = /^([a-z][a-z0-9+.-]*):/i.exec(plain);
|
|
167
|
+
return scheme === null || /^(https?|mailto|tel)$/i.test(scheme[1]!) ? href : null;
|
|
168
|
+
}
|
package/src/host/modules.ts
CHANGED
|
@@ -9,6 +9,15 @@
|
|
|
9
9
|
// Trebol [exports: [load-clients]]
|
|
10
10
|
// export save-client: func [...] [...]
|
|
11
11
|
//
|
|
12
|
+
// A `.js` file is a JavaScript module instead (js.ts).
|
|
13
|
+
//
|
|
14
|
+
// `import 'trebol-es` imports a package by its name: the first node_modules/trebol-es/
|
|
15
|
+
// found going up from the importing file (from the page or the working directory when
|
|
16
|
+
// no file imports it), as Node finds packages. Its package.json says which file is the
|
|
17
|
+
// module, `"trebol": "es.trb"`. The same loader reads it, so this works on a disk and
|
|
18
|
+
// over http alike; `trebol build` puts the package's files inside the page where the
|
|
19
|
+
// page finds them, under node_modules/.
|
|
20
|
+
//
|
|
12
21
|
// `do %file` runs a script in the user context each time; `read` gives a
|
|
13
22
|
// file's text and `load %file` its values.
|
|
14
23
|
//
|
|
@@ -16,15 +25,18 @@
|
|
|
16
25
|
|
|
17
26
|
import {
|
|
18
27
|
K, NONE, UNSET, native, value, text, load, mold, string, context, collectSetWords, bindSeries, bindSelf,
|
|
19
|
-
copyDeep, setWord, getWord, splice, currentFile, throwError, makeError, scriptStart, LoadError, T, canon, intern,
|
|
20
|
-
type Cell, type BlockCell, type StringCell, type ObjectCell, type Runtime, type WordCell, type Series, type Task,
|
|
28
|
+
copyDeep, setWord, getWord, splice, currentFile, throwError, makeError, scriptStart, LoadError, T, canon, intern, spell, LIBRARY_FILES, blockOf,
|
|
29
|
+
type Cell, type BlockCell, type StringCell, type ObjectCell, type Runtime, type WordCell, type Series, type Task, type Step, type NativeFrame,
|
|
21
30
|
} from '../core/index.ts';
|
|
31
|
+
import { jsModule } from './js.ts';
|
|
22
32
|
|
|
23
33
|
export interface Loader {
|
|
24
34
|
/** Base for resolving the first file: the page URL, or the working directory as a file: URL. */
|
|
25
35
|
base: string;
|
|
26
36
|
/** Reads a module's source by its resolved URL. */
|
|
27
37
|
read: (href: string) => Promise<string>;
|
|
38
|
+
/** Loads a JavaScript module by its resolved URL; the platform's import when not given. */
|
|
39
|
+
importJs?: (href: string) => Promise<Record<string, unknown>>;
|
|
28
40
|
}
|
|
29
41
|
|
|
30
42
|
interface ModuleEntry {
|
|
@@ -69,6 +81,7 @@ const EXPORTS = canon(intern('exports'));
|
|
|
69
81
|
const EXPORT = canon(intern('export'));
|
|
70
82
|
const TYPE = canon(intern('type'));
|
|
71
83
|
const MODULE = canon(intern('module'));
|
|
84
|
+
const PARTS = canon(intern('parts'));
|
|
72
85
|
|
|
73
86
|
/** The value after `name:` in a script header, or null. */
|
|
74
87
|
function headerField(header: BlockCell | null, name: number): Cell | null {
|
|
@@ -113,6 +126,11 @@ export function addBuiltIn(href: string, src: string): void {
|
|
|
113
126
|
builtIn.set(href, src);
|
|
114
127
|
}
|
|
115
128
|
|
|
129
|
+
/** The source built in under a URL, or undefined. */
|
|
130
|
+
export function builtInText(href: string): string | undefined {
|
|
131
|
+
return builtIn.get(href);
|
|
132
|
+
}
|
|
133
|
+
|
|
116
134
|
function readText(loader: Loader, href: string, path: Cell): Promise<Cell> {
|
|
117
135
|
const src = builtIn.get(href);
|
|
118
136
|
if (src !== undefined) return Promise.resolve(string(src));
|
|
@@ -160,15 +178,74 @@ native('do-file', '["Runs the script in a file, as do does; a module is imported
|
|
|
160
178
|
return { k: 'do', s: code.s, i: start };
|
|
161
179
|
});
|
|
162
180
|
|
|
163
|
-
interface ImportState {
|
|
181
|
+
interface ImportState {
|
|
182
|
+
href: string; entry: ModuleEntry; ctx: ReturnType<typeof context> | null; exports: WordCell[]; lib?: boolean;
|
|
183
|
+
/** The module's code, kept while its parts are read. */
|
|
184
|
+
code?: BlockCell; start?: number; parts?: string[];
|
|
185
|
+
/** What runs, each part and then the module's own body, and which of them runs now. */
|
|
186
|
+
bodies?: Series<Cell>[]; at?: number;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** Package names as npm takes them, without a scope (a scope does not fit in a word). */
|
|
190
|
+
const PACKAGE_NAME = /^[a-z0-9][a-z0-9._-]*$/;
|
|
164
191
|
|
|
165
|
-
|
|
192
|
+
/** The href of a package's module: the "trebol" file of the first node_modules/<name>/ going up from `from`. */
|
|
193
|
+
async function packageModule(loader: Loader, from: string, name: string): Promise<Cell> {
|
|
194
|
+
let dir = new URL('./', from);
|
|
195
|
+
for (;;) {
|
|
196
|
+
const at = new URL(`node_modules/${name}/`, dir).href;
|
|
197
|
+
const json = builtIn.get(`${at}package.json`) ?? await loader.read(`${at}package.json`).catch(() => null);
|
|
198
|
+
if (json !== null) {
|
|
199
|
+
let main: unknown;
|
|
200
|
+
try {
|
|
201
|
+
main = (JSON.parse(json) as { trebol?: unknown }).trebol;
|
|
202
|
+
} catch {
|
|
203
|
+
throw makeError('access', 'cannot-open', `the package.json of ${name} is not JSON`);
|
|
204
|
+
}
|
|
205
|
+
if (typeof main !== 'string') throw makeError('access', 'cannot-open', `${name} is not a Trebol package: its package.json has no "trebol" file`);
|
|
206
|
+
return string(new URL(main, at).href);
|
|
207
|
+
}
|
|
208
|
+
if (dir.pathname === '/' || dir.protocol !== 'file:' && dir.protocol !== 'http:' && dir.protocol !== 'https:') break;
|
|
209
|
+
dir = new URL('../', dir);
|
|
210
|
+
}
|
|
211
|
+
throw makeError('access', 'cannot-open', `cannot find the package ${name}: no node_modules/${name}/package.json`);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** Binds the bodies of a module (its parts, then its own code) to one context and runs the first. */
|
|
215
|
+
function startModule(rt: Runtime, f: NativeFrame, st: ImportState, bodies: Series<Cell>[]): Step {
|
|
216
|
+
for (const b of bodies) st.exports.push(...takeExports(b));
|
|
217
|
+
const ctx = context();
|
|
218
|
+
ctx.self = true;
|
|
219
|
+
for (const b of bodies) collectSetWords(b, 0, ctx);
|
|
220
|
+
// Trebol's own modules (the UI dialect) see lib, as R3's modules do, so a word a program sets,
|
|
221
|
+
// form: or can?, never takes the place of one they use; their exports go to lib too
|
|
222
|
+
for (const b of bodies) {
|
|
223
|
+
bindSeries(b, st.lib ? rt.lib : rt.user, true);
|
|
224
|
+
bindSeries(b, ctx);
|
|
225
|
+
bindSelf(b, ctx);
|
|
226
|
+
}
|
|
227
|
+
st.ctx = ctx;
|
|
228
|
+
st.bodies = bodies;
|
|
229
|
+
st.at = 0;
|
|
230
|
+
f.phase = 2;
|
|
231
|
+
f.trap = 1 << T.Error;
|
|
232
|
+
return { k: 'do', s: bodies[0]!, i: 0 };
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
native('import', '["Runs a module and gives its exports; a word is a package by its name, from node_modules." source [file! url! string! word!]]', (t, f, v) => {
|
|
166
236
|
const rt = t.rt;
|
|
167
237
|
const loader = loaders.get(rt);
|
|
168
238
|
const mods = modules.get(rt);
|
|
169
239
|
if (loader === undefined || mods === undefined) return throwError('access', 'no-loader', 'this host cannot load modules');
|
|
170
|
-
|
|
171
|
-
|
|
240
|
+
// a package: where its module is comes first, then it is imported as a file is
|
|
241
|
+
if (f.phase === 0 && f.args[0]!.t === K.Word) {
|
|
242
|
+
const name = spell((f.args[0] as WordCell).sym);
|
|
243
|
+
if (!PACKAGE_NAME.test(name)) return throwError('script', 'bad-module', `${name} is not the name of a package`);
|
|
244
|
+
f.phase = 5;
|
|
245
|
+
return { k: 'await', p: packageModule(loader, resolve(loader.base, currentFile(t), './'), name) };
|
|
246
|
+
}
|
|
247
|
+
if (f.phase === 0 || f.phase === 5) {
|
|
248
|
+
const href = f.phase === 5 ? text(v as StringCell) : hrefOf(t, loader, f.args[0]!);
|
|
172
249
|
const known = mods.get(href);
|
|
173
250
|
if (known !== undefined) {
|
|
174
251
|
if (known.obj !== null) return value(known.obj);
|
|
@@ -183,6 +260,28 @@ native('import', '["Runs a module and gives its exports." source [file! url! str
|
|
|
183
260
|
const entry: ModuleEntry = { obj: null, done, settle };
|
|
184
261
|
mods.set(href, entry);
|
|
185
262
|
f.data = { href, entry, ctx: null, exports: [] } satisfies ImportState;
|
|
263
|
+
if (/\.m?js$/i.test(new URL(href).pathname)) {
|
|
264
|
+
f.phase = 4;
|
|
265
|
+
const load = loader.importJs ?? ((h: string) => import(/* @vite-ignore */ h) as Promise<Record<string, unknown>>);
|
|
266
|
+
return {
|
|
267
|
+
k: 'await',
|
|
268
|
+
p: load(href).then(ns => {
|
|
269
|
+
const obj = jsModule(ns);
|
|
270
|
+
if (typeof obj === 'string') throw makeError('script', 'bad-module', obj);
|
|
271
|
+
return obj;
|
|
272
|
+
}, (e: unknown) => {
|
|
273
|
+
throw makeError('access', 'cannot-open', `cannot open ${mold(f.args[0]!)}: ${(e as Error)?.message ?? String(e)}`);
|
|
274
|
+
}).then(obj => {
|
|
275
|
+
entry.obj = obj;
|
|
276
|
+
entry.settle(true, obj);
|
|
277
|
+
return obj;
|
|
278
|
+
}, (err: unknown) => {
|
|
279
|
+
mods.delete(href);
|
|
280
|
+
entry.settle(false, err as Cell);
|
|
281
|
+
throw err;
|
|
282
|
+
}),
|
|
283
|
+
};
|
|
284
|
+
}
|
|
186
285
|
f.phase = 1;
|
|
187
286
|
return {
|
|
188
287
|
k: 'await',
|
|
@@ -193,16 +292,15 @@ native('import', '["Runs a module and gives its exports." source [file! url! str
|
|
|
193
292
|
}),
|
|
194
293
|
};
|
|
195
294
|
}
|
|
196
|
-
if (f.phase === 3) return value(v!);
|
|
295
|
+
if (f.phase === 3 || f.phase === 4) return value(v!);
|
|
197
296
|
const st = f.data as ImportState;
|
|
198
297
|
const failed = (e: Cell) => {
|
|
199
298
|
mods.delete(st.href);
|
|
200
299
|
st.entry.settle(false, e);
|
|
201
300
|
};
|
|
202
|
-
|
|
203
|
-
let code: BlockCell;
|
|
301
|
+
const loadIt = (src: Cell, file: string): BlockCell | Step => {
|
|
204
302
|
try {
|
|
205
|
-
|
|
303
|
+
return load(text(src as StringCell), { file });
|
|
206
304
|
} catch (e) {
|
|
207
305
|
// LoadError is the one exception on the normal path; it becomes a value here.
|
|
208
306
|
if (!(e instanceof LoadError)) throw e;
|
|
@@ -210,21 +308,50 @@ native('import', '["Runs a module and gives its exports." source [file! url! str
|
|
|
210
308
|
if (step.k === 'throw') failed(step.e.v);
|
|
211
309
|
return step;
|
|
212
310
|
}
|
|
311
|
+
};
|
|
312
|
+
if (f.phase === 1) {
|
|
313
|
+
const code = loadIt(v!, st.href);
|
|
314
|
+
if ('k' in code) return code;
|
|
213
315
|
const { header, start } = scriptStart(code);
|
|
214
316
|
const listed = headerField(header, EXPORTS);
|
|
215
317
|
if (listed !== null && listed.t === K.Block) st.exports = listed.s.data.filter(w => w.t === K.Word) as WordCell[];
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
const
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
318
|
+
st.lib = LIBRARY_FILES.has(st.href);
|
|
319
|
+
// parts: [%a.trb %b.trb] are files of the same module, read before it runs
|
|
320
|
+
const parts = headerField(header, PARTS);
|
|
321
|
+
if (parts !== null) {
|
|
322
|
+
const files = parts.t === K.Block ? parts.s.data.slice(parts.i) : [];
|
|
323
|
+
if (files.length === 0 || files.some(x => x.t !== K.File)) {
|
|
324
|
+
const step = throwError('script', 'bad-module', 'parts: takes a block of files');
|
|
325
|
+
if (step.k === 'throw') failed(step.e.v);
|
|
326
|
+
return step;
|
|
327
|
+
}
|
|
328
|
+
st.code = code;
|
|
329
|
+
st.start = start;
|
|
330
|
+
st.parts = files.map(x => new URL(text(x as StringCell), st.href).href);
|
|
331
|
+
// the parts of Trebol's own modules are Trebol's own code too
|
|
332
|
+
if (st.lib) for (const h of st.parts) LIBRARY_FILES.add(h);
|
|
333
|
+
f.phase = 6;
|
|
334
|
+
const loader = loaders.get(rt)!;
|
|
335
|
+
return { k: 'await', p: Promise.all(st.parts.map((h, i) => readText(loader, h, files[i]!))).then(texts => blockOf(K.Block, texts)) };
|
|
336
|
+
}
|
|
337
|
+
return startModule(rt, f, st, [copyDeep(code.s, start)]);
|
|
338
|
+
}
|
|
339
|
+
if (f.phase === 6) {
|
|
340
|
+
const bodies: Series<Cell>[] = [];
|
|
341
|
+
const texts = (v as BlockCell).s.data;
|
|
342
|
+
for (let i = 0; i < texts.length; i++) {
|
|
343
|
+
const part = loadIt(texts[i]!, st.parts![i]!);
|
|
344
|
+
if ('k' in part) return part;
|
|
345
|
+
bodies.push(copyDeep(part.s, scriptStart(part).start));
|
|
346
|
+
}
|
|
347
|
+
bodies.push(copyDeep(st.code!.s, st.start!));
|
|
348
|
+
st.code = undefined;
|
|
349
|
+
return startModule(rt, f, st, bodies);
|
|
350
|
+
}
|
|
351
|
+
// a part ran: the next one, or the module's own body
|
|
352
|
+
if (f.phase === 2 && f.thrown === null && st.at! + 1 < st.bodies!.length) {
|
|
353
|
+
st.at!++;
|
|
354
|
+
return { k: 'do', s: st.bodies![st.at!]!, i: 0 };
|
|
228
355
|
}
|
|
229
356
|
// phase 2: the module body ran
|
|
230
357
|
if (f.thrown !== null) {
|
|
@@ -241,6 +368,7 @@ native('import', '["Runs a module and gives its exports." source [file! url! str
|
|
|
241
368
|
if (step.k === 'throw') failed(step.e.v);
|
|
242
369
|
return step;
|
|
243
370
|
}
|
|
371
|
+
if (st.lib) setWord({ t: K.Word, sym: w.sym, bind: rt.lib }, st.ctx!.vals[i]!);
|
|
244
372
|
setWord({ t: K.Word, sym: w.sym, bind: rt.user }, st.ctx!.vals[i]!);
|
|
245
373
|
}
|
|
246
374
|
st.entry.obj = obj;
|