@fossil-lang/wasm 0.3.0-alpha.1 → 0.3.0-alpha.10

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.
@@ -7,6 +7,24 @@
7
7
  *
8
8
  * `Copy + Clone + Eq + Hash` so it can be a `HashMap` key on the Rust
9
9
  * side; `#[wasm_bindgen]` so JS can pass it back across the boundary.
10
+ *
11
+ * # Every exported method takes `&FileHandle`, and it has to
12
+ *
13
+ * wasm-bindgen CONSUMES an exported struct passed by value: the generated glue
14
+ * calls `__destroy_into_raw()` on the JS wrapper, which nulls its pointer. A
15
+ * handle passed by value is therefore good for exactly one call, and the second
16
+ * throws *"null pointer passed to rust"* — from inside a method that has nothing
17
+ * wrong with it, naming no handle and no file.
18
+ *
19
+ * `Copy` does not save it. The derive is a Rust-side property; nothing about it
20
+ * reaches the JS boundary, where this is a class holding a pointer like any
21
+ * other. The three methods a host calls repeatedly with one handle
22
+ * (`update_file`, `close_file`, `diagnostics_for`) take `&FileHandle` so
23
+ * wasm-bindgen borrows instead, and the handle stays live across the whole
24
+ * lifetime `open_file`'s doc comment promises.
25
+ *
26
+ * This was reachable from the FIRST loop an editor runs: `updateFile(h, text)` on
27
+ * every check, with the handle `openFile` returned once.
10
28
  */
11
29
  export class FileHandle {
12
30
  private constructor();
@@ -15,163 +33,216 @@ export class FileHandle {
15
33
  }
16
34
 
17
35
  /**
18
- * JS-facing handle for the Fossil compiler running inside a WASM module.
36
+ * The JS-facing workspace — `FossilPlayground` on the JS side, and a
37
+ * `RefCell` around the Rust one.
38
+ *
39
+ * # Why the interior `RefCell`, and it is not a style choice
19
40
  *
20
- * One instance owns one [`WasmDb`] (Salsa store + injected [`WasmSystem`]
21
- * + `Arc<OutputDescriptorKind>`). Hosts construct a single playground per
22
- * browser tab / Node process and reuse it across all method calls to
23
- * amortise the Salsa interning + memoisation overhead.
41
+ * `wasm-bindgen` wraps every exported struct in its own `WasmRefCell`. A
42
+ * method taking `&mut self` borrows that cell EXCLUSIVELY for the call, and a
43
+ * failed borrow does not return — it panics, with
44
+ * *"recursive use of an object detected which would lead to unsafe aliasing in
45
+ * rust"*. On `wasm32-unknown-unknown` a panic is an abort: the trap unwinds
46
+ * nothing, so the borrow flag it was holding is never cleared, and **every
47
+ * later call on that object fails the same way for the life of the module.**
48
+ * One bad call poisons the workspace permanently.
49
+ *
50
+ * That is a real defect and it was reachable from correct host code:
51
+ * `apps/playground` reproduced it by typing sixteen characters faster than its
52
+ * debounce, and the app carried a `busy` flag and a 120 ms coalesce to stay
53
+ * out of it. A mitigation a host has to remember is not a fix — and an editor
54
+ * is precisely the workload that forgets.
55
+ *
56
+ * So no exported method takes `&mut self`. Every one takes `&self`, which
57
+ * wasm-bindgen borrows SHARED — shared borrows nest, so re-entry cannot fail
58
+ * there — and the mutation goes through this `RefCell` with
59
+ * `try_borrow_mut`. Genuine re-entry (a JS callback that calls back in while a
60
+ * call is live) now returns a catchable `Error` naming what happened, and
61
+ * **the workspace is still usable afterwards**, because a returned `Err` drops
62
+ * its guard where a panic did not.
63
+ *
64
+ * The Rust-side [`FossilPlayground`] keeps its `&mut self` signatures
65
+ * untouched: `lsp_worker` already owns it inside an `Rc<RefCell<…>>`, and the
66
+ * native tests drive it directly. Only the JS boundary changed.
24
67
  */
25
68
  export class FossilPlayground {
26
69
  free(): void;
27
70
  [Symbol.dispose](): void;
28
71
  /**
29
- * Run `parse → def_map → typecheck_mapping` across every open file and
30
- * return a flat JS array of `{ uri, range, severity, message }` rows
31
- * keyed by file URI. The LSP Worker (07-03) republishes these grouped
32
- * by URI as `textDocument/publishDiagnostics` notifications.
72
+ * Every open **program**'s diagnostics as a flat JS array of
73
+ * `{ uri, range, severity, message }` rows keyed by file URI — the
74
+ * playground's panel view. It is NOT what the LSP Worker publishes; see
75
+ * [`CheckRow`], and `lsp_worker::publish_diagnostics` for the wire.
76
+ *
77
+ * A buffer the installed provider catalogue claims — a `.shex` being
78
+ * edited, a `.csv`, a `.parquet` — is an INPUT and is not parsed as fossil.
79
+ * [`fossil_ide::diagnostics()`] is where that is said and measured, for both
80
+ * hosts.
33
81
  *
34
82
  * `range` is the UTF-16 LSP range (via `fossil_ide::LineIndex` — the
35
- * rust-analyzer model from 06-05). `severity` is the LSP integer
36
- * constant (1 = error, 2 = warning, 3 = info). `message` carries any
37
- * `suggestion_source` as a `\nhelp: ...` suffix (mirroring `fossil-lsp`
38
- * `to_lsp_diagnostic` in 06-09).
83
+ * rust-analyzer model). `severity` is the LSP integer constant
84
+ * (1 = error, 2 = warning, 3 = info). `message` carries any
85
+ * `suggestion_source` as a `\nhelp: ...` suffix. All three come from
86
+ * [`fossil_ide::lsp_diagnostic`], the rendering an editor is shown.
39
87
  *
40
88
  * # Errors
41
89
  *
42
- * Returns a JS error only if the result fails to serialize to `JsValue`.
90
+ * Returns a JS error if the workspace is busy, or if the result fails to
91
+ * serialize to `JsValue`.
43
92
  */
44
93
  check(): any;
45
94
  /**
46
- * Return the stdlib classification manifest as a JS array of
47
- * `{ name, wasm_class }` objects (STDL-07).
95
+ * Close a file in the workspace. Idempotent in spirit but strict in
96
+ * signal: closing an unknown / already-closed handle is an error so
97
+ * JS-side bugs surface loudly (mirrors `ty_wasm`).
98
+ *
99
+ * # Errors
48
100
  *
49
- * `wasm_class` is the string `"pure_sql"` or `"native_udf_only"`. The
50
- * playground reads this once at startup to render `native_udf_only`
51
- * functions as disabled with a "native-only — unavailable in the browser"
52
- * tooltip (SC#1 playground half). `DuckDB`-WASM cannot register the Rust
53
- * UDFs those functions need (Pitfall 3), so the classification is the
54
- * authority on what is runnable in-browser.
101
+ * Returns a JS error if `handle` was never opened or was already closed,
102
+ * or if the workspace is busy.
103
+ */
104
+ close_file(handle: FileHandle): void;
105
+ /**
106
+ * The completion candidates at a position: `{ label, kind, detail }` rows,
107
+ * already narrowed by the receiver — `str.` offers string members and no
108
+ * reader, a property key position offers the target shape's predicates and
109
+ * no catalogue row.
55
110
  *
56
- * This is pure read-only data projected from the `&'static`-ready
57
- * `fossil_registry::FunctionRegistry` — no `DuckDB`, no native UDF code,
58
- * WASM-clean.
111
+ * `kind` is the LSP `CompletionItemKind` **by name** (`"function"`,
112
+ * `"field"`). See [`ide::CompletionRow`] for why a number does not cross
113
+ * this boundary.
59
114
  *
60
115
  * # Errors
61
116
  *
62
- * Returns a JS error only if the manifest fails to serialize to `JsValue`.
117
+ * Returns a JS error if the workspace is busy, or if the result fails to
118
+ * serialize to `JsValue`. An unknown handle is an empty array.
63
119
  */
64
- classification(): any;
120
+ completions(handle: FileHandle, line: number, character: number): any;
65
121
  /**
66
- * Close a file in the workspace. Idempotent in spirit but strict in
67
- * signal: closing an unknown / already-closed handle is an error so
68
- * JS-side bugs surface loudly (mirrors `ty_wasm`).
122
+ * Per-file diagnostic drain: `check()` returns the workspace-wide flat
123
+ * array, this returns one file's rows, so a host can refresh one buffer's
124
+ * panel without partitioning the workspace array on the JS side. It is NOT
125
+ * what the LSP Worker publishes — that is
126
+ * [`fossil_ide::lsp_diagnostics`], through `lsp_worker::publish_diagnostics`.
69
127
  *
70
128
  * # Errors
71
129
  *
72
- * Returns a JS error if `handle` was never opened or was already closed.
130
+ * Returns a JS error if `handle` is unknown, if the workspace is busy, or
131
+ * if serialization fails.
73
132
  */
74
- close_file(handle: FileHandle): void;
133
+ diagnostics_for(handle: FileHandle): any;
75
134
  /**
76
- * Per-file diagnostic drain — the B3 follow-up accessor the LSP Worker
77
- * (07-03) consumes for its per-file `publishDiagnostics` notifications.
78
- * `check()` returns the workspace-wide flat array; `diagnostics_for`
79
- * returns just one file's rows so 07-03 can dispatch one notification
80
- * per affected URI without partitioning the workspace array on the JS
81
- * side.
135
+ * Where the name under the cursor is defined: `{ uri, range }` rows, empty
136
+ * when nothing there has a definition.
137
+ *
138
+ * `uri` is the key the host opened the buffer under, verbatim — and two of
139
+ * the three positions goto-def recognises resolve into the shape document,
140
+ * so a host with one pane has to read it before moving a cursor.
82
141
  *
83
142
  * # Errors
84
143
  *
85
- * Returns a JS error if `handle` is unknown, or if serialization fails.
144
+ * Returns a JS error if the workspace is busy, or if the result fails to
145
+ * serialize to `JsValue`.
86
146
  */
87
- diagnostics_for(handle: FileHandle): any;
147
+ gotoDefinition(handle: FileHandle, line: number, character: number): any;
148
+ /**
149
+ * What is under the cursor: `{ markdown, range }`, or `null` when the
150
+ * cursor is on whitespace, on an expression the checker inferred no type
151
+ * for, or outside any mapping.
152
+ *
153
+ * `line` / `character` are LSP — zero-based, and `character` in UTF-16
154
+ * code units, which is what a JS host counts in anyway. The `range` comes
155
+ * back in the same units.
156
+ *
157
+ * # Errors
158
+ *
159
+ * Returns a JS error if the workspace is busy, or if the result fails to
160
+ * serialize to `JsValue`. An unknown handle is `null`, not an error —
161
+ * see [`FossilPlayground::hover_row`].
162
+ */
163
+ hover(handle: FileHandle, line: number, character: number): any;
164
+ /**
165
+ * The documents the file at `handle` names that nothing has registered:
166
+ * `{ key, locator }` rows, the locator expanded through the connection map.
167
+ *
168
+ * # Errors
169
+ *
170
+ * Returns a JS error if `handle` is unknown, if the workspace is busy, or
171
+ * if serialization fails.
172
+ */
173
+ missingDocuments(handle: FileHandle): any;
88
174
  /**
89
175
  * Construct a new playground.
90
176
  *
91
- * Installs `console_error_panic_hook` (idempotent — RESEARCH.md
92
- * Don't-Hand-Roll #8) so any panic inside compiler-core surfaces as a
93
- * `console.error` stack trace in the host (browser `DevTools` or Node).
177
+ * Installs `console_error_panic_hook` (idempotent) so any panic inside
178
+ * compiler-core surfaces as a `console.error` stack trace in the host
179
+ * (browser `DevTools` or Node).
94
180
  */
95
181
  constructor();
96
182
  /**
97
183
  * Open a file in the workspace. Returns a [`FileHandle`] the JS side
98
- * keys subsequent `update_file` / `close_file` / `compile_file` calls
99
- * on. `path` is the URI / virtual path the diagnostics carry back to
100
- * the LSP client.
184
+ * keys subsequent `update_file` / `close_file` calls on. `path` is the
185
+ * URI / virtual path the diagnostics carry back to the LSP client.
101
186
  *
102
187
  * Mirrors `ty_wasm::Workspace::open_file` (Astral). Interns a fresh
103
188
  * [`fossil_base::SourceFile`] under the current Salsa revision.
104
189
  *
105
190
  * # Errors
106
191
  *
107
- * Returns a JS error only if the internal counter is exhausted
108
- * (`u32::MAX` files — would require a runaway loop in JS, not a normal
109
- * failure mode).
192
+ * Returns a JS error if the workspace is already inside another call —
193
+ * see the type-level note on [`WasmPlayground`].
110
194
  */
111
195
  open_file(path: string, contents: string): FileHandle;
196
+ /**
197
+ * Register a fetched document's `text` under the `key` `missingDocuments`
198
+ * reported.
199
+ *
200
+ * # Errors
201
+ *
202
+ * Returns a JS error if the workspace is busy.
203
+ */
204
+ registerDocument(key: string, text: string): void;
112
205
  /**
113
206
  * Register an [`fossil_descriptors_input::InferredDescriptor`] for a
114
- * source binding name BEFORE invoking [`Self::compile`] /
115
- * [`Self::compile_file`]. The Rust compiler reads from this registration
116
- * during forward type propagation (Phase 3 CORE-05 rewired in plan
117
- * 13-02).
118
- *
119
- * `descriptor_json` is the JSON serialisation of `InferredDescriptor`;
120
- * the canonical shape is exposed in `packages/wasm/src/index.ts` as
121
- * `InferredDescriptorJson`:
122
- *
123
- * ```json
124
- * {
125
- * "source_name": "users",
126
- * "columns": [
127
- * { "name": "id", "primitive": "Integer" },
128
- * { "name": "name", "primitive": "String" }
129
- * ],
130
- * "content_hash": ""
131
- * }
132
- * ```
133
- *
134
- * Called by the browser-side playground orchestration AFTER running
135
- * DuckDB-WASM `DESCRIBE read_csv_auto('<resolved-url>')` and BEFORE
136
- * invoking `compile()` / `compile_file()`. Keyed by source-binding
137
- * name (`"users"` for `users := io.csv("...")`), NOT by URL.
138
- *
139
- * Idempotent: re-registering with the same `source_name` OVERWRITES the
140
- * previous entry — intentional, since the host may re-introspect when
141
- * file content changes.
207
+ * source binding name BEFORE invoking `check`. The Rust compiler reads
208
+ * from this registration during forward type propagation — the browser has
209
+ * no filesystem to introspect a CSV from, so the column types must arrive
210
+ * from the host.
211
+ *
212
+ * Registering the same URI twice REPLACES the previous descriptor
213
+ * — intentional, since the host re-introspects when the source changes.
142
214
  *
143
215
  * # Errors
144
216
  *
145
217
  * - Malformed JSON / missing required fields → JS `Error` with the
146
218
  * underlying `serde_json` message.
147
- *
148
- * Implementation: thin shim over the pure-Rust
149
- * [`Self::register_inferred_descriptor_native`] helper.
219
+ * - The workspace is busy.
150
220
  */
151
221
  registerInferredDescriptor(descriptor_json: string): void;
152
222
  /**
153
- * Install a user-supplied `ShEx` schema as the active output descriptor.
154
- * On parse failure the previously-installed descriptor is RETAINED (no
155
- * half-applied state — a broken schema must never wedge the editor;
156
- * same contract as `fossil-lsp`'s `load_sibling_shex` in 06-09).
157
- *
158
- * The schema is parsed via
159
- * [`fossil_descriptors_output::ShExDescriptor::from_reader`] (no
160
- * network access — Pitfall 1) and wrapped in
161
- * [`OutputDescriptorKind::ShEx`]. The resulting `Arc` is swapped into
162
- * the [`WasmDb`]'s descriptor slot atomically; future `fossil-ide`
163
- * feature calls (hover, completion) reading
164
- * [`HirDb::output_descriptor_kind`] see the new schema.
223
+ * Replace the connection map `@name/…` expands against, as
224
+ * `SourceHost.connections()` answers it.
165
225
  *
166
226
  * # Errors
167
227
  *
168
- * Returns a JS error if the text is not a parseable `ShEx` schema.
228
+ * Returns a JS error if `connections` is not a string-to-string record, or
229
+ * if the workspace is busy.
169
230
  */
170
- set_target_shex(text: string): void;
231
+ setConnections(connections: any): void;
232
+ /**
233
+ * The data sources the file at `handle` reads — see
234
+ * [`fossil_lineage::program_sources`].
235
+ *
236
+ * # Errors
237
+ *
238
+ * Returns a JS error if `handle` is unknown, if the workspace is busy, or
239
+ * if serialization fails.
240
+ */
241
+ sources(handle: FileHandle): any;
171
242
  /**
172
243
  * Apply an edit to an open file. Mutates the SAME `SourceFile` via the
173
244
  * Salsa [`salsa::Setter`] (`set_text`) — this BUMPS THE REVISION, the
174
- * real cancellation trigger (ADR-0022): any in-flight analysis from the
245
+ * real cancellation trigger: any in-flight analysis from the
175
246
  * previous keystroke observes the new revision at its next cooperative
176
247
  * checkpoint. NO new `SourceFile` is interned — the Salsa input
177
248
  * identity stays stable across the file's lifetime, so memoised
@@ -180,21 +251,46 @@ export class FossilPlayground {
180
251
  *
181
252
  * # Errors
182
253
  *
183
- * Returns a JS error if `handle` was never opened or was already closed.
254
+ * Returns a JS error if `handle` was never opened or was already closed,
255
+ * or if the workspace is already inside another call — see the type-level
256
+ * note on [`WasmPlayground`]. **Neither leaves the workspace unusable**,
257
+ * which is the whole reason this method takes `&self`.
184
258
  */
185
259
  update_file(handle: FileHandle, contents: string): void;
186
260
  }
187
261
 
188
262
  /**
189
- * `JS`-facing re-export of the Phase-6 06-07 semantic-token legend.
263
+ * The providers this host installs (`io.csv`, `io.rdf`, `io.shex`, …) as a JS
264
+ * array of `{ name, extensions, kind }`. Pure projection of the provider
265
+ * registry — no parsing, no db. `kind` is read off each row's capabilities, so
266
+ * the two shape-document rows come back as `Schema`.
267
+ *
268
+ * # Errors
269
+ * Returns a JS error only if the result fails to serialize to `JsValue`.
270
+ */
271
+ export function providers(): any;
272
+
273
+ /**
274
+ * Parse `program` and return its external references — every data URI +
275
+ * `schema =` argument, each tagged with its `@conn` alias (`null` for a direct
276
+ * path) and role (`Data` / `Schema`) — as a JS array of
277
+ * `{ connection, path, role }`. Parse-only; the host resolves `@conn` →
278
+ * `{base}/path` itself (its data-plane job, kept out of fossil's semantics).
279
+ *
280
+ * # Errors
281
+ * Returns a JS error only if the result fails to serialize to `JsValue`.
282
+ */
283
+ export function refs(program: string): any;
284
+
285
+ /**
286
+ * `JS`-facing re-export of the semantic-token legend.
190
287
  *
191
288
  * Returns `{ tokenTypes: string[], tokenModifiers: string[] }` — the exact
192
- * LSP `SemanticTokensLegend` shape. `CodeMirror` (plan 08-08 +
193
- * `packages/codemirror-fossil/src/tags.ts`) uses this to translate
194
- * LSP `semanticTokens/full` response indices into highlight tag names.
289
+ * LSP `SemanticTokensLegend` shape, which is what turns the indices in a
290
+ * `semanticTokens/full` response into names a host can style.
195
291
  *
196
- * Delegates to [`fossil_ide::semantic_legend`] (the 06-07 authoritative
197
- * definition) — NO duplicate legend definition lives here.
292
+ * Delegates to [`fossil_ide::semantic_legend`], the one definition — a second
293
+ * legend here would desynchronise the indices the native LSP already emits.
198
294
  *
199
295
  * # Errors
200
296
  *
@@ -217,10 +313,31 @@ export function semantic_legend(): any;
217
313
  */
218
314
  export function start_lsp_worker(): void;
219
315
 
316
+ /**
317
+ * The name of every [`TokenRow::kind`], indexed BY that kind.
318
+ *
319
+ * `token_kinds()[row.kind]` is the variant name — `"Comment"`, `"KwFrom"`,
320
+ * `"String"`. A host maps names to highlight categories and never writes a
321
+ * discriminant down, which is the only version of this contract that survives
322
+ * a reorder of the lexer.
323
+ *
324
+ * This is the same move [`semantic_legend`] makes for LSP semantic tokens: the
325
+ * indices are meaningless without the legend, so ship the legend.
326
+ *
327
+ * A kind past the end of the array is a variant appended by a newer compiler
328
+ * than the host was built against. That is BACKWARDS-COMPATIBLE by
329
+ * construction: the host sees `undefined` and styles the span as plain text.
330
+ *
331
+ * # Errors
332
+ *
333
+ * Returns a JS error only if `serde_wasm_bindgen` fails to serialise a
334
+ * `Vec<&str>` (impossible in practice).
335
+ */
336
+ export function tokenKinds(): any;
337
+
220
338
  /**
221
339
  * `JS`-facing tokenize. Returns the same row stream as [`tokenize_native`],
222
- * serialised via `serde_wasm_bindgen`. Consumed by
223
- * `@fossil-lang/codemirror-fossil`'s `StreamParser` (plan 08-08).
340
+ * serialised via `serde_wasm_bindgen`.
224
341
  *
225
342
  * # Errors
226
343
  *
@@ -238,18 +355,26 @@ export interface InitOutput {
238
355
  readonly start_lsp_worker: () => [number, number];
239
356
  readonly tokenize: (a: number, b: number) => [number, number, number];
240
357
  readonly semantic_legend: () => [number, number, number];
358
+ readonly tokenKinds: () => [number, number, number];
241
359
  readonly __wbg_filehandle_free: (a: number, b: number) => void;
242
360
  readonly __wbg_fossilplayground_free: (a: number, b: number) => void;
243
361
  readonly fossilplayground_new: () => number;
244
- readonly fossilplayground_classification: (a: number) => [number, number, number];
245
362
  readonly fossilplayground_open_file: (a: number, b: number, c: number, d: number, e: number) => [number, number, number];
246
363
  readonly fossilplayground_update_file: (a: number, b: number, c: number, d: number) => [number, number];
247
364
  readonly fossilplayground_close_file: (a: number, b: number) => [number, number];
248
365
  readonly fossilplayground_check: (a: number) => [number, number, number];
249
366
  readonly fossilplayground_diagnostics_for: (a: number, b: number) => [number, number, number];
250
- readonly fossilplayground_set_target_shex: (a: number, b: number, c: number) => [number, number];
367
+ readonly fossilplayground_setConnections: (a: number, b: any) => [number, number];
368
+ readonly fossilplayground_missingDocuments: (a: number, b: number) => [number, number, number];
369
+ readonly fossilplayground_registerDocument: (a: number, b: number, c: number, d: number, e: number) => [number, number];
370
+ readonly fossilplayground_sources: (a: number, b: number) => [number, number, number];
371
+ readonly fossilplayground_hover: (a: number, b: number, c: number, d: number) => [number, number, number];
372
+ readonly fossilplayground_completions: (a: number, b: number, c: number, d: number) => [number, number, number];
373
+ readonly fossilplayground_gotoDefinition: (a: number, b: number, c: number, d: number) => [number, number, number];
251
374
  readonly fossilplayground_registerInferredDescriptor: (a: number, b: number, c: number) => [number, number];
252
- readonly wasm_bindgen__convert__closures_____invoke__h4393dedb0066ad40: (a: number, b: number, c: any) => void;
375
+ readonly providers: () => [number, number, number];
376
+ readonly refs: (a: number, b: number) => [number, number, number];
377
+ readonly wasm_bindgen__convert__closures_____invoke__h6c9357f9f01d3cd7: (a: number, b: number, c: any) => void;
253
378
  readonly __wbindgen_malloc_command_export: (a: number, b: number) => number;
254
379
  readonly __wbindgen_realloc_command_export: (a: number, b: number, c: number, d: number) => number;
255
380
  readonly __wbindgen_exn_store_command_export: (a: number) => void;