@fossil-lang/wasm 0.3.0-alpha.2 → 0.3.0-alpha.5

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,178 @@ 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
40
+ *
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.
19
63
  *
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.
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 — the `.shex` the
78
+ * playground had to open to give the compiler a document, a `.csv`, a
79
+ * `.parquet` — is an INPUT and is not parsed as fossil.
80
+ * [`fossil_ide::diagnostics()`] is where that is said and measured, for both
81
+ * hosts.
33
82
  *
34
83
  * `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).
84
+ * rust-analyzer model). `severity` is the LSP integer constant
85
+ * (1 = error, 2 = warning, 3 = info). `message` carries any
86
+ * `suggestion_source` as a `\nhelp: ...` suffix. All three come from
87
+ * [`fossil_ide::lsp_diagnostic`], the rendering an editor is shown.
39
88
  *
40
89
  * # Errors
41
90
  *
42
- * Returns a JS error only if the result fails to serialize to `JsValue`.
91
+ * Returns a JS error if the workspace is busy, or if the result fails to
92
+ * serialize to `JsValue`.
43
93
  */
44
94
  check(): any;
45
95
  /**
46
- * Return the stdlib classification manifest as a JS array of
47
- * `{ name, wasm_class }` objects (STDL-07).
96
+ * Close a file in the workspace. Idempotent in spirit but strict in
97
+ * signal: closing an unknown / already-closed handle is an error so
98
+ * JS-side bugs surface loudly (mirrors `ty_wasm`).
99
+ *
100
+ * # Errors
101
+ *
102
+ * Returns a JS error if `handle` was never opened or was already closed,
103
+ * or if the workspace is busy.
104
+ */
105
+ close_file(handle: FileHandle): void;
106
+ /**
107
+ * The completion candidates at a position: `{ label, kind, detail }` rows,
108
+ * already narrowed by the receiver — `str.` offers string members and no
109
+ * reader, a property key position offers the target shape's predicates and
110
+ * no catalogue row.
111
+ *
112
+ * `kind` is the LSP `CompletionItemKind` **by name** (`"function"`,
113
+ * `"field"`). See [`ide::CompletionRow`] for why a number does not cross
114
+ * this boundary.
48
115
  *
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.
116
+ * # Errors
55
117
  *
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.
118
+ * Returns a JS error if the workspace is busy, or if the result fails to
119
+ * serialize to `JsValue`. An unknown handle is an empty array.
120
+ */
121
+ completions(handle: FileHandle, line: number, character: number): any;
122
+ /**
123
+ * Per-file diagnostic drain: `check()` returns the workspace-wide flat
124
+ * array, this returns one file's rows, so a host can refresh one buffer's
125
+ * panel without partitioning the workspace array on the JS side. It is NOT
126
+ * what the LSP Worker publishes — that is
127
+ * [`fossil_ide::lsp_diagnostics`], through `lsp_worker::publish_diagnostics`.
59
128
  *
60
129
  * # Errors
61
130
  *
62
- * Returns a JS error only if the manifest fails to serialize to `JsValue`.
131
+ * Returns a JS error if `handle` is unknown, if the workspace is busy, or
132
+ * if serialization fails.
63
133
  */
64
- classification(): any;
134
+ diagnostics_for(handle: FileHandle): any;
65
135
  /**
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`).
136
+ * Where the name under the cursor is defined: `{ uri, range }` rows, empty
137
+ * when nothing there has a definition.
138
+ *
139
+ * `uri` is the key the host opened the buffer under, verbatim — and two of
140
+ * the three positions goto-def recognises resolve into the shape document,
141
+ * so a host with one pane has to read it before moving a cursor.
69
142
  *
70
143
  * # Errors
71
144
  *
72
- * Returns a JS error if `handle` was never opened or was already closed.
145
+ * Returns a JS error if the workspace is busy, or if the result fails to
146
+ * serialize to `JsValue`.
73
147
  */
74
- close_file(handle: FileHandle): void;
148
+ gotoDefinition(handle: FileHandle, line: number, character: number): any;
75
149
  /**
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.
150
+ * What is under the cursor: `{ markdown, range }`, or `null` when the
151
+ * cursor is on whitespace, on an expression the checker inferred no type
152
+ * for, or outside any mapping.
153
+ *
154
+ * `line` / `character` are LSP — zero-based, and `character` in UTF-16
155
+ * code units, which is what a JS host counts in anyway. The `range` comes
156
+ * back in the same units.
82
157
  *
83
158
  * # Errors
84
159
  *
85
- * Returns a JS error if `handle` is unknown, or if serialization fails.
160
+ * Returns a JS error if the workspace is busy, or if the result fails to
161
+ * serialize to `JsValue`. An unknown handle is `null`, not an error —
162
+ * see [`FossilPlayground::hover_row`].
86
163
  */
87
- diagnostics_for(handle: FileHandle): any;
164
+ hover(handle: FileHandle, line: number, character: number): any;
88
165
  /**
89
166
  * Construct a new playground.
90
167
  *
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).
168
+ * Installs `console_error_panic_hook` (idempotent) so any panic inside
169
+ * compiler-core surfaces as a `console.error` stack trace in the host
170
+ * (browser `DevTools` or Node).
94
171
  */
95
172
  constructor();
96
173
  /**
97
174
  * 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.
175
+ * keys subsequent `update_file` / `close_file` calls on. `path` is the
176
+ * URI / virtual path the diagnostics carry back to the LSP client.
101
177
  *
102
178
  * Mirrors `ty_wasm::Workspace::open_file` (Astral). Interns a fresh
103
179
  * [`fossil_base::SourceFile`] under the current Salsa revision.
104
180
  *
105
181
  * # Errors
106
182
  *
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).
183
+ * Returns a JS error if the workspace is already inside another call —
184
+ * see the type-level note on [`WasmPlayground`].
110
185
  */
111
186
  open_file(path: string, contents: string): FileHandle;
112
187
  /**
113
188
  * 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).
189
+ * source binding name BEFORE invoking `check`. The Rust compiler reads
190
+ * from this registration during forward type propagation — the browser has
191
+ * no filesystem to introspect a CSV from, so the column types must arrive
192
+ * from the host.
118
193
  *
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.
194
+ * Registering the same URI twice REPLACES the previous descriptor
195
+ * — intentional, since the host re-introspects when the source changes.
142
196
  *
143
197
  * # Errors
144
198
  *
145
199
  * - Malformed JSON / missing required fields → JS `Error` with the
146
200
  * underlying `serde_json` message.
147
- *
148
- * Implementation: thin shim over the pure-Rust
149
- * [`Self::register_inferred_descriptor_native`] helper.
201
+ * - The workspace is busy.
150
202
  */
151
203
  registerInferredDescriptor(descriptor_json: string): void;
152
- /**
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.
165
- *
166
- * # Errors
167
- *
168
- * Returns a JS error if the text is not a parseable `ShEx` schema.
169
- */
170
- set_target_shex(text: string): void;
171
204
  /**
172
205
  * Apply an edit to an open file. Mutates the SAME `SourceFile` via the
173
206
  * Salsa [`salsa::Setter`] (`set_text`) — this BUMPS THE REVISION, the
174
- * real cancellation trigger (ADR-0022): any in-flight analysis from the
207
+ * real cancellation trigger: any in-flight analysis from the
175
208
  * previous keystroke observes the new revision at its next cooperative
176
209
  * checkpoint. NO new `SourceFile` is interned — the Salsa input
177
210
  * identity stays stable across the file's lifetime, so memoised
@@ -180,21 +213,46 @@ export class FossilPlayground {
180
213
  *
181
214
  * # Errors
182
215
  *
183
- * Returns a JS error if `handle` was never opened or was already closed.
216
+ * Returns a JS error if `handle` was never opened or was already closed,
217
+ * or if the workspace is already inside another call — see the type-level
218
+ * note on [`WasmPlayground`]. **Neither leaves the workspace unusable**,
219
+ * which is the whole reason this method takes `&self`.
184
220
  */
185
221
  update_file(handle: FileHandle, contents: string): void;
186
222
  }
187
223
 
188
224
  /**
189
- * `JS`-facing re-export of the Phase-6 06-07 semantic-token legend.
225
+ * The providers this host installs (`io.csv`, `io.rdf`, `io.shex`, …) as a JS
226
+ * array of `{ name, extensions, kind }`. Pure projection of the provider
227
+ * registry — no parsing, no db. `kind` is read off each row's capabilities, so
228
+ * the two shape-document rows come back as `Schema`.
229
+ *
230
+ * # Errors
231
+ * Returns a JS error only if the result fails to serialize to `JsValue`.
232
+ */
233
+ export function providers(): any;
234
+
235
+ /**
236
+ * Parse `program` and return its external references — every data URI +
237
+ * `schema =` argument, each tagged with its `@conn` alias (`null` for a direct
238
+ * path) and role (`Data` / `Schema`) — as a JS array of
239
+ * `{ connection, path, role }`. Parse-only; the host resolves `@conn` →
240
+ * `{base}/path` itself (its data-plane job, kept out of fossil's semantics).
241
+ *
242
+ * # Errors
243
+ * Returns a JS error only if the result fails to serialize to `JsValue`.
244
+ */
245
+ export function refs(program: string): any;
246
+
247
+ /**
248
+ * `JS`-facing re-export of the semantic-token legend.
190
249
  *
191
250
  * 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.
251
+ * LSP `SemanticTokensLegend` shape, which is what turns the indices in a
252
+ * `semanticTokens/full` response into names a host can style.
195
253
  *
196
- * Delegates to [`fossil_ide::semantic_legend`] (the 06-07 authoritative
197
- * definition) — NO duplicate legend definition lives here.
254
+ * Delegates to [`fossil_ide::semantic_legend`], the one definition — a second
255
+ * legend here would desynchronise the indices the native LSP already emits.
198
256
  *
199
257
  * # Errors
200
258
  *
@@ -217,10 +275,31 @@ export function semantic_legend(): any;
217
275
  */
218
276
  export function start_lsp_worker(): void;
219
277
 
278
+ /**
279
+ * The name of every [`TokenRow::kind`], indexed BY that kind.
280
+ *
281
+ * `token_kinds()[row.kind]` is the variant name — `"Comment"`, `"KwFrom"`,
282
+ * `"String"`. A host maps names to highlight categories and never writes a
283
+ * discriminant down, which is the only version of this contract that survives
284
+ * a reorder of the lexer.
285
+ *
286
+ * This is the same move [`semantic_legend`] makes for LSP semantic tokens: the
287
+ * indices are meaningless without the legend, so ship the legend.
288
+ *
289
+ * A kind past the end of the array is a variant appended by a newer compiler
290
+ * than the host was built against. That is BACKWARDS-COMPATIBLE by
291
+ * construction: the host sees `undefined` and styles the span as plain text.
292
+ *
293
+ * # Errors
294
+ *
295
+ * Returns a JS error only if `serde_wasm_bindgen` fails to serialise a
296
+ * `Vec<&str>` (impossible in practice).
297
+ */
298
+ export function tokenKinds(): any;
299
+
220
300
  /**
221
301
  * `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).
302
+ * serialised via `serde_wasm_bindgen`.
224
303
  *
225
304
  * # Errors
226
305
  *
@@ -238,18 +317,22 @@ export interface InitOutput {
238
317
  readonly start_lsp_worker: () => [number, number];
239
318
  readonly tokenize: (a: number, b: number) => [number, number, number];
240
319
  readonly semantic_legend: () => [number, number, number];
320
+ readonly tokenKinds: () => [number, number, number];
241
321
  readonly __wbg_filehandle_free: (a: number, b: number) => void;
242
322
  readonly __wbg_fossilplayground_free: (a: number, b: number) => void;
243
323
  readonly fossilplayground_new: () => number;
244
- readonly fossilplayground_classification: (a: number) => [number, number, number];
245
324
  readonly fossilplayground_open_file: (a: number, b: number, c: number, d: number, e: number) => [number, number, number];
246
325
  readonly fossilplayground_update_file: (a: number, b: number, c: number, d: number) => [number, number];
247
326
  readonly fossilplayground_close_file: (a: number, b: number) => [number, number];
248
327
  readonly fossilplayground_check: (a: number) => [number, number, number];
249
328
  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];
329
+ readonly fossilplayground_hover: (a: number, b: number, c: number, d: number) => [number, number, number];
330
+ readonly fossilplayground_completions: (a: number, b: number, c: number, d: number) => [number, number, number];
331
+ readonly fossilplayground_gotoDefinition: (a: number, b: number, c: number, d: number) => [number, number, number];
251
332
  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;
333
+ readonly providers: () => [number, number, number];
334
+ readonly refs: (a: number, b: number) => [number, number, number];
335
+ readonly wasm_bindgen__convert__closures_____invoke__h900f85fb942a113b: (a: number, b: number, c: any) => void;
253
336
  readonly __wbindgen_malloc_command_export: (a: number, b: number) => number;
254
337
  readonly __wbindgen_realloc_command_export: (a: number, b: number, c: number, d: number) => number;
255
338
  readonly __wbindgen_exn_store_command_export: (a: number) => void;