zigsm 1.4.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/dist/std.js ADDED
@@ -0,0 +1,1006 @@
1
+ import { cosineSimilarity, embedTexts, getVoyageConfig } from "./voyage.js";
2
+ const CAT_namespace = 0;
3
+ const CAT_container = 1;
4
+ const CAT_global_variable = 2;
5
+ const CAT_function = 3;
6
+ const CAT_primitive = 4;
7
+ const CAT_error_set = 5;
8
+ const CAT_global_const = 6;
9
+ const CAT_alias = 7;
10
+ const CAT_type = 8;
11
+ const CAT_type_type = 9;
12
+ const CAT_type_function = 10;
13
+ const LOG_err = 0;
14
+ const LOG_warn = 1;
15
+ const LOG_info = 2;
16
+ const LOG_debug = 3;
17
+ const domContent = typeof document !== "undefined" ? document.getElementById("content") : null;
18
+ const domSearch = typeof document !== "undefined" ? document.getElementById("search") : null;
19
+ const domErrors = typeof document !== "undefined" ? document.getElementById("errors") : null;
20
+ const domErrorsText = typeof document !== "undefined" ? document.getElementById("errorsText") : null;
21
+ var searchTimer = null;
22
+ const curNav = {
23
+ tag: 0,
24
+ decl: null,
25
+ path: null,
26
+ };
27
+ var curNavSearch = "";
28
+ const moduleList = [];
29
+ var wasm_exports = null;
30
+ const text_decoder = new TextDecoder();
31
+ const text_encoder = new TextEncoder();
32
+ export function startDocsViewer() {
33
+ const wasm_promise = fetch("main.wasm");
34
+ const sources_promise = fetch("sources.tar").then((response) => {
35
+ if (!response.ok)
36
+ throw new Error("unable to download sources");
37
+ return response.arrayBuffer();
38
+ });
39
+ WebAssembly.instantiateStreaming(wasm_promise, {
40
+ js: {
41
+ log: (level, ptr, len) => {
42
+ const msg = decodeString(ptr, len);
43
+ switch (level) {
44
+ case LOG_err:
45
+ console.error(msg);
46
+ if (domErrorsText)
47
+ domErrorsText.textContent += msg + "\n";
48
+ if (domErrors)
49
+ domErrors.classList.remove("hidden");
50
+ break;
51
+ case LOG_warn:
52
+ console.warn(msg);
53
+ break;
54
+ case LOG_info:
55
+ console.info(msg);
56
+ break;
57
+ case LOG_debug:
58
+ console.debug(msg);
59
+ break;
60
+ }
61
+ },
62
+ },
63
+ }).then((obj) => {
64
+ wasm_exports = obj.instance.exports;
65
+ if (typeof window !== "undefined")
66
+ window.wasm = obj; // for debugging
67
+ sources_promise.then((buffer) => {
68
+ const js_array = new Uint8Array(buffer);
69
+ const ptr = wasm_exports.alloc(js_array.length);
70
+ const wasm_array = new Uint8Array(wasm_exports.memory.buffer, ptr, js_array.length);
71
+ wasm_array.set(js_array);
72
+ wasm_exports.unpack(ptr, js_array.length);
73
+ updateModuleList();
74
+ if (typeof window !== "undefined") {
75
+ window.addEventListener("popstate", onPopState, false);
76
+ window.addEventListener("keydown", onWindowKeyDown, false);
77
+ }
78
+ if (domSearch) {
79
+ domSearch.addEventListener("keydown", onSearchKeyDown, false);
80
+ domSearch.addEventListener("input", onSearchChange, false);
81
+ }
82
+ onHashChange(null);
83
+ });
84
+ });
85
+ }
86
+ function renderTitle() {
87
+ if (typeof document === "undefined")
88
+ return;
89
+ const suffix = " - Zig Documentation";
90
+ if (curNavSearch.length > 0) {
91
+ document.title = curNavSearch + " - Search" + suffix;
92
+ }
93
+ else if (curNav.decl != null) {
94
+ document.title = fullyQualifiedName(curNav.decl) + suffix;
95
+ }
96
+ else if (curNav.path != null) {
97
+ document.title = curNav.path + suffix;
98
+ }
99
+ else {
100
+ document.title = moduleList[0] + suffix;
101
+ }
102
+ }
103
+ function render() {
104
+ renderTitle();
105
+ if (domContent)
106
+ domContent.textContent = "";
107
+ if (curNavSearch !== "")
108
+ return renderSearch();
109
+ switch (curNav.tag) {
110
+ case 0:
111
+ return renderHome();
112
+ case 1:
113
+ if (curNav.decl == null) {
114
+ return renderNotFound();
115
+ }
116
+ else {
117
+ return renderDecl(curNav.decl);
118
+ }
119
+ case 2:
120
+ return renderSource(curNav.path);
121
+ default:
122
+ throw new Error("invalid navigation state");
123
+ }
124
+ }
125
+ function renderHome() {
126
+ if (moduleList.length == 0) {
127
+ if (domContent)
128
+ domContent.textContent = "# Error\n\nsources.tar contains no modules";
129
+ return;
130
+ }
131
+ return renderModule(0);
132
+ }
133
+ function renderModule(pkg_index) {
134
+ const root_decl = wasm_exports.find_module_root(pkg_index);
135
+ return renderDecl(root_decl);
136
+ }
137
+ function renderDecl(decl_index) {
138
+ let current = decl_index;
139
+ const seen = new Set();
140
+ while (true) {
141
+ const category = wasm_exports.categorize_decl(current, 0);
142
+ switch (category) {
143
+ case CAT_namespace:
144
+ case CAT_container:
145
+ return renderNamespacePage(current);
146
+ case CAT_global_variable:
147
+ case CAT_primitive:
148
+ case CAT_global_const:
149
+ case CAT_type:
150
+ case CAT_type_type:
151
+ return renderGlobal(current);
152
+ case CAT_function:
153
+ return renderFunction(current);
154
+ case CAT_type_function:
155
+ return renderTypeFunction(current);
156
+ case CAT_error_set:
157
+ return renderErrorSetPage(current);
158
+ case CAT_alias: {
159
+ if (seen.has(current))
160
+ return renderNotFound();
161
+ seen.add(current);
162
+ const aliasee = wasm_exports.get_aliasee();
163
+ if (aliasee === -1)
164
+ return renderNotFound();
165
+ current = aliasee;
166
+ continue;
167
+ }
168
+ default:
169
+ throw new Error("unrecognized category " + category);
170
+ }
171
+ }
172
+ }
173
+ function renderSource(path) {
174
+ const decl_index = findFileRoot(path);
175
+ if (decl_index == null)
176
+ return renderNotFound();
177
+ let markdown = "";
178
+ markdown += "# " + path + "\n\n";
179
+ markdown += unwrapString(wasm_exports.decl_source_html(decl_index));
180
+ if (domContent)
181
+ domContent.textContent = markdown;
182
+ return markdown;
183
+ }
184
+ function renderNamespacePage(decl_index) {
185
+ let markdown = "";
186
+ // Add title
187
+ const name = unwrapString(wasm_exports.decl_category_name(decl_index));
188
+ markdown += "# " + name + "\n\n";
189
+ // Add documentation
190
+ const docs = unwrapString(wasm_exports.decl_docs_html(decl_index, false));
191
+ if (docs.length > 0) {
192
+ markdown += docs + "\n\n";
193
+ }
194
+ // Add namespace content
195
+ const members = namespaceMembers(decl_index, false).slice();
196
+ const fields = declFields(decl_index).slice();
197
+ markdown += renderNamespaceMarkdown(decl_index, members, fields);
198
+ if (domContent)
199
+ domContent.textContent = markdown;
200
+ return markdown;
201
+ }
202
+ function renderFunction(decl_index) {
203
+ let markdown = "";
204
+ // Add title
205
+ const name = unwrapString(wasm_exports.decl_category_name(decl_index));
206
+ markdown += "# " + name + "\n";
207
+ // Add documentation
208
+ const docs = unwrapString(wasm_exports.decl_docs_html(decl_index, false));
209
+ if (docs.length > 0) {
210
+ markdown += "\n" + docs;
211
+ }
212
+ // Add function prototype
213
+ const proto = unwrapString(wasm_exports.decl_fn_proto_html(decl_index, false));
214
+ if (proto.length > 0) {
215
+ markdown += "\n\n## Function Signature\n\n" + proto;
216
+ }
217
+ // Add parameters
218
+ const params = declParams(decl_index).slice();
219
+ if (params.length > 0) {
220
+ markdown += "\n\n## Parameters\n";
221
+ for (let i = 0; i < params.length; i++) {
222
+ const param_html = unwrapString(wasm_exports.decl_param_html(decl_index, params[i]));
223
+ markdown += "\n" + param_html;
224
+ }
225
+ }
226
+ // Add errors
227
+ const errorSetNode = fnErrorSet(decl_index);
228
+ if (errorSetNode != null) {
229
+ const base_decl = wasm_exports.fn_error_set_decl(decl_index, errorSetNode);
230
+ const errorList = errorSetNodeList(decl_index, errorSetNode);
231
+ if (errorList != null && errorList.length > 0) {
232
+ markdown += "\n\n## Errors\n";
233
+ for (let i = 0; i < errorList.length; i++) {
234
+ const error_html = unwrapString(wasm_exports.error_html(base_decl, errorList[i]));
235
+ markdown += "\n" + error_html;
236
+ }
237
+ }
238
+ }
239
+ // Add doctest
240
+ const doctest = unwrapString(wasm_exports.decl_doctest_html(decl_index));
241
+ if (doctest.length > 0) {
242
+ markdown += "\n\n## Example Usage\n\n" + doctest;
243
+ }
244
+ // Add source code
245
+ const source = unwrapString(wasm_exports.decl_source_html(decl_index));
246
+ if (source.length > 0) {
247
+ markdown += "\n\n## Source Code\n\n" + source;
248
+ }
249
+ if (domContent)
250
+ domContent.textContent = markdown;
251
+ return markdown;
252
+ }
253
+ function renderGlobal(decl_index) {
254
+ let markdown = "";
255
+ // Add title
256
+ const name = unwrapString(wasm_exports.decl_category_name(decl_index));
257
+ markdown += "# " + name + "\n\n";
258
+ // Add documentation
259
+ const docs = unwrapString(wasm_exports.decl_docs_html(decl_index, true));
260
+ if (docs.length > 0) {
261
+ markdown += docs + "\n\n";
262
+ }
263
+ // Add source code
264
+ const source = unwrapString(wasm_exports.decl_source_html(decl_index));
265
+ if (source.length > 0) {
266
+ markdown += "## Source Code\n\n" + source + "\n\n";
267
+ }
268
+ if (domContent)
269
+ domContent.textContent = markdown;
270
+ return markdown;
271
+ }
272
+ function renderTypeFunction(decl_index) {
273
+ let markdown = "";
274
+ // Add title
275
+ const name = unwrapString(wasm_exports.decl_category_name(decl_index));
276
+ markdown += "# " + name + "\n\n";
277
+ // Add documentation
278
+ const docs = unwrapString(wasm_exports.decl_docs_html(decl_index, false));
279
+ if (docs.length > 0) {
280
+ markdown += docs + "\n\n";
281
+ }
282
+ // Add parameters
283
+ const params = declParams(decl_index).slice();
284
+ if (params.length > 0) {
285
+ markdown += "## Parameters\n\n";
286
+ for (let i = 0; i < params.length; i++) {
287
+ const param_html = unwrapString(wasm_exports.decl_param_html(decl_index, params[i]));
288
+ markdown += param_html + "\n\n";
289
+ }
290
+ }
291
+ // Add doctest
292
+ const doctest = unwrapString(wasm_exports.decl_doctest_html(decl_index));
293
+ if (doctest.length > 0) {
294
+ markdown += "## Example Usage\n\n" + doctest + "\n\n";
295
+ }
296
+ // Add namespace content or source
297
+ const members = unwrapSlice32(wasm_exports.type_fn_members(decl_index, false)).slice();
298
+ const fields = unwrapSlice32(wasm_exports.type_fn_fields(decl_index)).slice();
299
+ if (members.length !== 0 || fields.length !== 0) {
300
+ markdown += renderNamespaceMarkdown(decl_index, members, fields);
301
+ }
302
+ else {
303
+ const source = unwrapString(wasm_exports.decl_source_html(decl_index));
304
+ if (source.length > 0) {
305
+ markdown += "## Source Code\n\n" + source + "\n\n";
306
+ }
307
+ }
308
+ if (domContent)
309
+ domContent.textContent = markdown;
310
+ return markdown;
311
+ }
312
+ function renderErrorSetPage(decl_index) {
313
+ let markdown = "";
314
+ // Add title
315
+ const name = unwrapString(wasm_exports.decl_category_name(decl_index));
316
+ markdown += "# " + name + "\n\n";
317
+ // Add documentation
318
+ const docs = unwrapString(wasm_exports.decl_docs_html(decl_index, false));
319
+ if (docs.length > 0) {
320
+ markdown += docs + "\n\n";
321
+ }
322
+ // Add errors
323
+ const errorSetList = declErrorSet(decl_index).slice();
324
+ if (errorSetList != null && errorSetList.length > 0) {
325
+ markdown += "## Errors\n\n";
326
+ for (let i = 0; i < errorSetList.length; i++) {
327
+ const error_html = unwrapString(wasm_exports.error_html(decl_index, errorSetList[i]));
328
+ markdown += error_html + "\n\n";
329
+ }
330
+ }
331
+ if (domContent)
332
+ domContent.textContent = markdown;
333
+ return markdown;
334
+ }
335
+ function renderNavMarkdown(decl_index) {
336
+ let markdown = "";
337
+ const list = [];
338
+ // Walk backwards through decl parents
339
+ let decl_it = decl_index;
340
+ while (decl_it != null) {
341
+ list.push(declIndexName(decl_it));
342
+ decl_it = declParent(decl_it);
343
+ }
344
+ // Walk backwards through file path segments
345
+ if (decl_index != null) {
346
+ const file_path = fullyQualifiedName(decl_index);
347
+ const parts = file_path.split(".");
348
+ parts.pop(); // skip last
349
+ for (let i = parts.length - 1; i >= 0; i--) {
350
+ if (parts[i]) {
351
+ list.push(parts[i]);
352
+ }
353
+ }
354
+ }
355
+ list.reverse();
356
+ if (list.length > 0) {
357
+ markdown += "*Navigation: " + list.join(" > ") + "*\n\n";
358
+ }
359
+ return markdown;
360
+ }
361
+ function renderNamespaceMarkdown(base_decl, members, fields) {
362
+ let markdown = "";
363
+ const typesList = [];
364
+ const namespacesList = [];
365
+ const errSetsList = [];
366
+ const fnsList = [];
367
+ const varsList = [];
368
+ const valsList = [];
369
+ // Categorize members
370
+ for (let i = 0; i < members.length; i++) {
371
+ let member = members[i];
372
+ const original = member;
373
+ const seen = new Set();
374
+ while (true) {
375
+ const member_category = wasm_exports.categorize_decl(member, 0);
376
+ switch (member_category) {
377
+ case CAT_namespace:
378
+ namespacesList.push({ original: original, member: member });
379
+ break;
380
+ case CAT_container:
381
+ typesList.push({ original: original, member: member });
382
+ break;
383
+ case CAT_global_variable:
384
+ varsList.push(member);
385
+ break;
386
+ case CAT_function:
387
+ fnsList.push(member);
388
+ break;
389
+ case CAT_type:
390
+ case CAT_type_type:
391
+ case CAT_type_function:
392
+ typesList.push({ original: original, member: member });
393
+ break;
394
+ case CAT_error_set:
395
+ errSetsList.push({ original: original, member: member });
396
+ break;
397
+ case CAT_global_const:
398
+ case CAT_primitive:
399
+ valsList.push({ original: original, member: member });
400
+ break;
401
+ case CAT_alias: {
402
+ if (seen.has(member)) {
403
+ valsList.push({ original: original, member: member });
404
+ break;
405
+ }
406
+ seen.add(member);
407
+ member = wasm_exports.get_aliasee();
408
+ continue;
409
+ }
410
+ default:
411
+ throw new Error("unknown category: " + member_category);
412
+ }
413
+ break;
414
+ }
415
+ }
416
+ // Render each category
417
+ if (typesList.length > 0) {
418
+ markdown += "## Types\n\n";
419
+ for (let i = 0; i < typesList.length; i++) {
420
+ const name = declIndexName(typesList[i].original);
421
+ markdown += "- " + name + "\n";
422
+ }
423
+ markdown += "\n";
424
+ }
425
+ if (namespacesList.length > 0) {
426
+ markdown += "## Namespaces\n\n";
427
+ for (let i = 0; i < namespacesList.length; i++) {
428
+ const name = declIndexName(namespacesList[i].original);
429
+ markdown += "- " + name + "\n";
430
+ }
431
+ markdown += "\n";
432
+ }
433
+ if (errSetsList.length > 0) {
434
+ markdown += "## Error Sets\n\n";
435
+ for (let i = 0; i < errSetsList.length; i++) {
436
+ const name = declIndexName(errSetsList[i].original);
437
+ markdown += "- " + name + "\n";
438
+ }
439
+ markdown += "\n";
440
+ }
441
+ if (fnsList.length > 0) {
442
+ markdown += "## Functions\n\n";
443
+ for (let i = 0; i < fnsList.length; i++) {
444
+ const decl = fnsList[i];
445
+ const name = declIndexName(decl);
446
+ const proto = unwrapString(wasm_exports.decl_fn_proto_html(decl, true));
447
+ const docs = unwrapString(wasm_exports.decl_docs_html(decl, true));
448
+ markdown += "### " + name + "\n\n";
449
+ if (proto.length > 0) {
450
+ markdown += proto + "\n\n";
451
+ }
452
+ if (docs.length > 0) {
453
+ markdown += docs + "\n\n";
454
+ }
455
+ }
456
+ }
457
+ if (fields.length > 0) {
458
+ markdown += "## Fields\n\n";
459
+ for (let i = 0; i < fields.length; i++) {
460
+ const field_html = unwrapString(wasm_exports.decl_field_html(base_decl, fields[i]));
461
+ markdown += field_html + "\n\n";
462
+ }
463
+ }
464
+ if (varsList.length > 0) {
465
+ markdown += "## Global Variables\n\n";
466
+ for (let i = 0; i < varsList.length; i++) {
467
+ const decl = varsList[i];
468
+ const name = declIndexName(decl);
469
+ const type_html = unwrapString(wasm_exports.decl_type_html(decl));
470
+ const docs = unwrapString(wasm_exports.decl_docs_html(decl, true));
471
+ markdown += "### " + name + "\n\n";
472
+ if (type_html.length > 0) {
473
+ markdown += "Type: " + type_html + "\n\n";
474
+ }
475
+ if (docs.length > 0) {
476
+ markdown += docs + "\n\n";
477
+ }
478
+ }
479
+ }
480
+ if (valsList.length > 0) {
481
+ markdown += "## Values\n\n";
482
+ for (let i = 0; i < valsList.length; i++) {
483
+ const original_decl = valsList[i].original;
484
+ const decl = valsList[i].member;
485
+ const name = declIndexName(original_decl);
486
+ const type_html = unwrapString(wasm_exports.decl_type_html(decl));
487
+ const docs = unwrapString(wasm_exports.decl_docs_html(decl, true));
488
+ markdown += "### " + name + "\n\n";
489
+ if (type_html.length > 0) {
490
+ markdown += "Type: " + type_html + "\n\n";
491
+ }
492
+ if (docs.length > 0) {
493
+ markdown += docs + "\n\n";
494
+ }
495
+ }
496
+ }
497
+ return markdown;
498
+ }
499
+ function renderNotFound() {
500
+ const markdown = "# Error\n\nDeclaration not found.";
501
+ if (domContent)
502
+ domContent.textContent = markdown;
503
+ return markdown;
504
+ }
505
+ function renderSearch() {
506
+ const ignoreCase = curNavSearch.toLowerCase() === curNavSearch;
507
+ const results = executeQuery(curNavSearch, ignoreCase);
508
+ let markdown = "# Search Results\n\n";
509
+ markdown += 'Query: "' + curNavSearch + '"\n\n';
510
+ if (results.length > 0) {
511
+ markdown += "Found " + results.length + " results:\n\n";
512
+ for (let i = 0; i < results.length; i++) {
513
+ const match = results[i];
514
+ const full_name = fullyQualifiedName(match);
515
+ markdown += "- " + full_name + "\n";
516
+ }
517
+ }
518
+ else {
519
+ markdown += "No results found.\n\nPress escape to exit search.";
520
+ }
521
+ if (domContent)
522
+ domContent.textContent = markdown;
523
+ return markdown;
524
+ }
525
+ // Event handlers and utility functions (unchanged from original)
526
+ function updateCurNav(location_hash) {
527
+ curNav.tag = 0;
528
+ curNav.decl = null;
529
+ curNav.path = null;
530
+ curNavSearch = "";
531
+ if (location_hash.length > 1 && location_hash[0] === "#") {
532
+ const query = location_hash.substring(1);
533
+ const qpos = query.indexOf("?");
534
+ let nonSearchPart;
535
+ if (qpos === -1) {
536
+ nonSearchPart = query;
537
+ }
538
+ else {
539
+ nonSearchPart = query.substring(0, qpos);
540
+ curNavSearch = decodeURIComponent(query.substring(qpos + 1));
541
+ }
542
+ if (nonSearchPart.length > 0) {
543
+ const source_mode = nonSearchPart.startsWith("src/");
544
+ if (source_mode) {
545
+ curNav.tag = 2;
546
+ curNav.path = nonSearchPart.substring(4);
547
+ }
548
+ else {
549
+ curNav.tag = 1;
550
+ curNav.decl = findDecl(nonSearchPart);
551
+ }
552
+ }
553
+ }
554
+ }
555
+ function onHashChange(state) {
556
+ if (typeof history !== "undefined")
557
+ history.replaceState({}, "");
558
+ if (typeof location !== "undefined")
559
+ navigate(location.hash);
560
+ if (state == null && typeof window !== "undefined")
561
+ window.scrollTo({ top: 0 });
562
+ }
563
+ function onPopState(ev) {
564
+ onHashChange(ev.state);
565
+ }
566
+ function navigate(location_hash) {
567
+ updateCurNav(location_hash);
568
+ if (domSearch && domSearch.value !== curNavSearch) {
569
+ domSearch.value = curNavSearch;
570
+ }
571
+ render();
572
+ }
573
+ function onSearchKeyDown(ev) {
574
+ switch (ev.code) {
575
+ case "Enter":
576
+ if (ev.shiftKey || ev.ctrlKey || ev.altKey)
577
+ return;
578
+ clearAsyncSearch();
579
+ if (typeof location !== "undefined")
580
+ location.hash = computeSearchHash();
581
+ ev.preventDefault();
582
+ ev.stopPropagation();
583
+ return;
584
+ case "Escape":
585
+ if (ev.shiftKey || ev.ctrlKey || ev.altKey)
586
+ return;
587
+ if (domSearch) {
588
+ domSearch.value = "";
589
+ domSearch.blur();
590
+ }
591
+ ev.preventDefault();
592
+ ev.stopPropagation();
593
+ startSearch();
594
+ return;
595
+ default:
596
+ ev.stopPropagation();
597
+ return;
598
+ }
599
+ }
600
+ function onSearchChange(ev) {
601
+ startAsyncSearch();
602
+ }
603
+ function onWindowKeyDown(ev) {
604
+ switch (ev.code) {
605
+ case "KeyS":
606
+ if (ev.shiftKey || ev.ctrlKey || ev.altKey)
607
+ return;
608
+ if (domSearch) {
609
+ domSearch.focus();
610
+ domSearch.select();
611
+ }
612
+ ev.preventDefault();
613
+ ev.stopPropagation();
614
+ startAsyncSearch();
615
+ break;
616
+ }
617
+ }
618
+ function clearAsyncSearch() {
619
+ if (searchTimer != null) {
620
+ clearTimeout(searchTimer);
621
+ searchTimer = null;
622
+ }
623
+ }
624
+ function startAsyncSearch() {
625
+ clearAsyncSearch();
626
+ searchTimer = setTimeout(startSearch, 10);
627
+ }
628
+ function computeSearchHash() {
629
+ if (typeof location === "undefined" || !domSearch)
630
+ return "";
631
+ const oldWatHash = location.hash;
632
+ const oldHash = oldWatHash.startsWith("#") ? oldWatHash : "#" + oldWatHash;
633
+ const parts = oldHash.split("?");
634
+ const newPart2 = domSearch.value === "" ? "" : "?" + domSearch.value;
635
+ return parts[0] + newPart2;
636
+ }
637
+ function startSearch() {
638
+ clearAsyncSearch();
639
+ navigate(computeSearchHash());
640
+ }
641
+ function updateModuleList() {
642
+ moduleList.length = 0;
643
+ for (let i = 0;; i += 1) {
644
+ const name = unwrapString(wasm_exports.module_name(i));
645
+ if (name.length == 0)
646
+ break;
647
+ moduleList.push(name);
648
+ }
649
+ }
650
+ // Utility functions (unchanged from original)
651
+ function decodeString(ptr, len) {
652
+ if (len === 0)
653
+ return "";
654
+ return text_decoder.decode(new Uint8Array(wasm_exports.memory.buffer, ptr, len));
655
+ }
656
+ function unwrapString(bigint) {
657
+ const ptr = Number(bigint & 0xffffffffn);
658
+ const len = Number(bigint >> 32n);
659
+ return decodeString(ptr, len);
660
+ }
661
+ function fullyQualifiedName(decl_index) {
662
+ return unwrapString(wasm_exports.decl_fqn(decl_index));
663
+ }
664
+ function declIndexName(decl_index) {
665
+ return unwrapString(wasm_exports.decl_name(decl_index));
666
+ }
667
+ function setQueryString(s) {
668
+ const jsArray = text_encoder.encode(s);
669
+ const len = jsArray.length;
670
+ const ptr = wasm_exports.query_begin(len);
671
+ const wasmArray = new Uint8Array(wasm_exports.memory.buffer, ptr, len);
672
+ wasmArray.set(jsArray);
673
+ }
674
+ function executeQuery(query_string, ignore_case) {
675
+ setQueryString(query_string);
676
+ const ptr = wasm_exports.query_exec(ignore_case);
677
+ const head = new Uint32Array(wasm_exports.memory.buffer, ptr, 1);
678
+ const len = head[0];
679
+ return new Uint32Array(wasm_exports.memory.buffer, ptr + 4, len);
680
+ }
681
+ function namespaceMembers(decl_index, include_private) {
682
+ return unwrapSlice32(wasm_exports.namespace_members(decl_index, include_private));
683
+ }
684
+ function declFields(decl_index) {
685
+ return unwrapSlice32(wasm_exports.decl_fields(decl_index));
686
+ }
687
+ function declParams(decl_index) {
688
+ return unwrapSlice32(wasm_exports.decl_params(decl_index));
689
+ }
690
+ function declErrorSet(decl_index) {
691
+ return unwrapSlice64(wasm_exports.decl_error_set(decl_index));
692
+ }
693
+ function errorSetNodeList(base_decl, err_set_node) {
694
+ return unwrapSlice64(wasm_exports.error_set_node_list(base_decl, err_set_node));
695
+ }
696
+ function unwrapSlice32(bigint) {
697
+ const ptr = Number(bigint & 0xffffffffn);
698
+ const len = Number(bigint >> 32n);
699
+ if (len === 0)
700
+ return [];
701
+ return new Uint32Array(wasm_exports.memory.buffer, ptr, len);
702
+ }
703
+ function unwrapSlice64(bigint) {
704
+ const ptr = Number(bigint & 0xffffffffn);
705
+ const len = Number(bigint >> 32n);
706
+ if (len === 0)
707
+ return [];
708
+ return new BigUint64Array(wasm_exports.memory.buffer, ptr, len);
709
+ }
710
+ function findDecl(fqn) {
711
+ setInputString(fqn);
712
+ const result = wasm_exports.find_decl();
713
+ if (result === -1)
714
+ return null;
715
+ return result;
716
+ }
717
+ function findFileRoot(path) {
718
+ setInputString(path);
719
+ const result = wasm_exports.find_file_root();
720
+ if (result === -1)
721
+ return null;
722
+ return result;
723
+ }
724
+ function declParent(decl_index) {
725
+ const result = wasm_exports.decl_parent(decl_index);
726
+ if (result === -1)
727
+ return null;
728
+ return result;
729
+ }
730
+ function fnErrorSet(decl_index) {
731
+ const result = wasm_exports.fn_error_set(decl_index);
732
+ if (result === 0)
733
+ return null;
734
+ return result;
735
+ }
736
+ function setInputString(s) {
737
+ const jsArray = text_encoder.encode(s);
738
+ const len = jsArray.length;
739
+ const ptr = wasm_exports.set_input_string(len);
740
+ const wasmArray = new Uint8Array(wasm_exports.memory.buffer, ptr, len);
741
+ wasmArray.set(jsArray);
742
+ }
743
+ let inMemoryEmbeddingCache = null;
744
+ async function initWasmRuntime(wasmPath, stdSources) {
745
+ const fs = await import("node:fs");
746
+ const wasmBytes = typeof wasmPath === "string" ? fs.readFileSync(wasmPath) : wasmPath;
747
+ const wasmModule = await WebAssembly.instantiate(wasmBytes, {
748
+ js: {
749
+ log: (level, ptr, len) => {
750
+ const msg = decodeString(ptr, len);
751
+ if (level === LOG_err) {
752
+ throw new Error(msg);
753
+ }
754
+ },
755
+ },
756
+ });
757
+ const exports = wasmModule.instance.exports;
758
+ wasm_exports = exports;
759
+ const ptr = exports.alloc(stdSources.length);
760
+ const wasmArray = new Uint8Array(exports.memory.buffer, ptr, stdSources.length);
761
+ wasmArray.set(stdSources);
762
+ exports.unpack(ptr, stdSources.length);
763
+ }
764
+ function collectDeclsForEmbeddings() {
765
+ const seen = new Set();
766
+ const result = [];
767
+ function walk(decl) {
768
+ if (decl < 0 || seen.has(decl)) {
769
+ return;
770
+ }
771
+ seen.add(decl);
772
+ result.push(decl);
773
+ const category = wasm_exports.categorize_decl(decl, 0);
774
+ if (category === CAT_alias) {
775
+ const aliasee = wasm_exports.get_aliasee();
776
+ if (aliasee !== -1) {
777
+ walk(aliasee);
778
+ }
779
+ return;
780
+ }
781
+ if (category === CAT_namespace || category === CAT_container) {
782
+ const members = namespaceMembers(decl, false).slice();
783
+ for (let i = 0; i < members.length; i++) {
784
+ walk(members[i]);
785
+ }
786
+ return;
787
+ }
788
+ if (category === CAT_type_function) {
789
+ const members = unwrapSlice32(wasm_exports.type_fn_members(decl, false)).slice();
790
+ for (let i = 0; i < members.length; i++) {
791
+ walk(members[i]);
792
+ }
793
+ }
794
+ }
795
+ for (let i = 0;; i++) {
796
+ const name = unwrapString(wasm_exports.module_name(i));
797
+ if (name.length === 0) {
798
+ break;
799
+ }
800
+ const root = wasm_exports.find_module_root(i);
801
+ if (root !== -1) {
802
+ walk(root);
803
+ }
804
+ }
805
+ return result;
806
+ }
807
+ function stripMarkdown(input) {
808
+ return input
809
+ .replace(/```[\s\S]*?```/g, " ")
810
+ .replace(/`([^`]+)`/g, "$1")
811
+ .replace(/\[(.*?)\]\((.*?)\)/g, "$1")
812
+ .replace(/[#*_>~-]/g, " ")
813
+ .replace(/\s+/g, " ")
814
+ .trim();
815
+ }
816
+ function buildDeclEmbeddingText(decl) {
817
+ const category = wasm_exports.categorize_decl(decl, 0);
818
+ const fqn = fullyQualifiedName(decl);
819
+ const name = declIndexName(decl);
820
+ const docs = unwrapString(wasm_exports.decl_docs_html(decl, true));
821
+ const proto = category === CAT_function || category === CAT_type_function
822
+ ? unwrapString(wasm_exports.decl_fn_proto_html(decl, true))
823
+ : "";
824
+ const typeInfo = category === CAT_global_variable ||
825
+ category === CAT_global_const ||
826
+ category === CAT_primitive ||
827
+ category === CAT_type ||
828
+ category === CAT_type_type
829
+ ? unwrapString(wasm_exports.decl_type_html(decl))
830
+ : "";
831
+ const source = unwrapString(wasm_exports.decl_source_html(decl)).slice(0, 1200);
832
+ const text = stripMarkdown(`${fqn}\n${name}\n${proto}\n${typeInfo}\n${docs}\n${source}`).slice(0, 4000);
833
+ return { decl, fqn, text };
834
+ }
835
+ async function getEmbeddingCachePath(zigVersion, docSource, model) {
836
+ const envPathsMod = await import("env-paths");
837
+ const path = await import("node:path");
838
+ const paths = envPathsMod.default("zigsm", { suffix: "" });
839
+ const safeModel = model.replace(/[^a-zA-Z0-9._-]+/g, "-");
840
+ return path.join(paths.cache, zigVersion, `std-embeddings-${docSource}-${safeModel}.json`);
841
+ }
842
+ async function hashStdSources(stdSources) {
843
+ const crypto = await import("node:crypto");
844
+ return crypto.createHash("sha256").update(stdSources).digest("hex");
845
+ }
846
+ async function loadEmbeddingCache(cachePath) {
847
+ const fs = await import("node:fs");
848
+ if (!fs.existsSync(cachePath)) {
849
+ return null;
850
+ }
851
+ try {
852
+ const raw = fs.readFileSync(cachePath, "utf8");
853
+ return JSON.parse(raw);
854
+ }
855
+ catch {
856
+ return null;
857
+ }
858
+ }
859
+ async function saveEmbeddingCache(cachePath, data) {
860
+ const fs = await import("node:fs");
861
+ const path = await import("node:path");
862
+ const dir = path.dirname(cachePath);
863
+ if (!fs.existsSync(dir)) {
864
+ fs.mkdirSync(dir, { recursive: true });
865
+ }
866
+ fs.writeFileSync(cachePath, JSON.stringify(data));
867
+ }
868
+ async function getOrBuildEmbeddingCache(wasmPath, stdSources, options) {
869
+ const voyage = getVoyageConfig();
870
+ if (!voyage) {
871
+ return null;
872
+ }
873
+ const zigVersion = options.zigVersion || "master";
874
+ const docSource = options.docSource || "local";
875
+ if (inMemoryEmbeddingCache &&
876
+ inMemoryEmbeddingCache.model === voyage.model &&
877
+ inMemoryEmbeddingCache.zigVersion === zigVersion &&
878
+ inMemoryEmbeddingCache.docSource === docSource) {
879
+ return inMemoryEmbeddingCache;
880
+ }
881
+ const sourceHash = await hashStdSources(stdSources);
882
+ const cachePath = await getEmbeddingCachePath(zigVersion, docSource, voyage.model);
883
+ const existing = await loadEmbeddingCache(cachePath);
884
+ if (existing &&
885
+ existing.version === 1 &&
886
+ existing.model === voyage.model &&
887
+ existing.sourceHash === sourceHash &&
888
+ existing.zigVersion === zigVersion &&
889
+ existing.docSource === docSource &&
890
+ existing.docs.length === existing.vectors.length) {
891
+ inMemoryEmbeddingCache = existing;
892
+ return existing;
893
+ }
894
+ await initWasmRuntime(wasmPath, stdSources);
895
+ const decls = collectDeclsForEmbeddings();
896
+ const docs = decls.map((decl) => buildDeclEmbeddingText(decl)).filter((doc) => doc.text.length > 0);
897
+ if (docs.length === 0) {
898
+ return null;
899
+ }
900
+ const vectors = await embedTexts(docs.map((doc) => doc.text), voyage, "document");
901
+ const built = {
902
+ version: 1,
903
+ model: voyage.model,
904
+ sourceHash,
905
+ zigVersion,
906
+ docSource,
907
+ docs,
908
+ vectors,
909
+ };
910
+ await saveEmbeddingCache(cachePath, built);
911
+ inMemoryEmbeddingCache = built;
912
+ return built;
913
+ }
914
+ function buildHybridRanking(lexicalDecls, semanticDecls, maxResults) {
915
+ const scores = new Map();
916
+ for (let i = 0; i < lexicalDecls.length; i++) {
917
+ const decl = lexicalDecls[i];
918
+ const score = 1 / (20 + i);
919
+ scores.set(decl, (scores.get(decl) || 0) + score);
920
+ }
921
+ for (let i = 0; i < semanticDecls.length; i++) {
922
+ const decl = semanticDecls[i];
923
+ const score = 2 / (20 + i);
924
+ scores.set(decl, (scores.get(decl) || 0) + score);
925
+ }
926
+ return Array.from(scores.entries())
927
+ .sort((a, b) => b[1] - a[1])
928
+ .slice(0, maxResults)
929
+ .map(([decl]) => decl);
930
+ }
931
+ export async function searchStdLib(wasmPath, stdSources, query, limit = 20, options = {}) {
932
+ await initWasmRuntime(wasmPath, stdSources);
933
+ const ignoreCase = query.toLowerCase() === query;
934
+ const lexicalResults = Array.from(executeQuery(query, ignoreCase));
935
+ let mergedResults = lexicalResults;
936
+ try {
937
+ const voyage = getVoyageConfig();
938
+ if (voyage) {
939
+ const cache = await getOrBuildEmbeddingCache(wasmPath, stdSources, options);
940
+ if (cache && cache.docs.length > 0) {
941
+ const queryVector = (await embedTexts([query], voyage, "query"))[0];
942
+ const semanticResults = cache.docs
943
+ .map((doc, i) => ({ decl: doc.decl, score: cosineSimilarity(queryVector, cache.vectors[i]) }))
944
+ .sort((a, b) => b.score - a.score)
945
+ .slice(0, Math.max(limit * 8, 120))
946
+ .map((item) => item.decl);
947
+ const lexicalTop = lexicalResults.slice(0, Math.max(limit * 8, 120));
948
+ mergedResults = buildHybridRanking(lexicalTop, semanticResults, Math.max(limit * 8, 120));
949
+ }
950
+ }
951
+ }
952
+ catch {
953
+ mergedResults = lexicalResults;
954
+ }
955
+ let markdown = `# Search Results\n\nQuery: "${query}"\n\n`;
956
+ if (mergedResults.length > 0) {
957
+ const limitedResults = mergedResults.slice(0, limit);
958
+ markdown += `Found ${mergedResults.length} results (showing ${limitedResults.length}):\n\n`;
959
+ for (let i = 0; i < limitedResults.length; i++) {
960
+ const match = limitedResults[i];
961
+ const full_name = fullyQualifiedName(match);
962
+ markdown += `- ${full_name}\n`;
963
+ }
964
+ }
965
+ else {
966
+ markdown += "No results found.";
967
+ }
968
+ return markdown;
969
+ }
970
+ export async function getStdLibItem(wasmPath, stdSources, name, getSourceFile = false) {
971
+ await initWasmRuntime(wasmPath, stdSources);
972
+ const exports = wasm_exports;
973
+ const decl_index = findDecl(name);
974
+ if (decl_index === null) {
975
+ return `# Error\n\nDeclaration "${name}" not found.`;
976
+ }
977
+ if (getSourceFile) {
978
+ // Resolve aliases by decl index
979
+ let cur = decl_index;
980
+ const seen = new Set();
981
+ while (true) {
982
+ const cat = exports.categorize_decl(cur, 0);
983
+ if (cat !== CAT_alias)
984
+ break;
985
+ if (seen.has(cur))
986
+ break; // cycle guard
987
+ seen.add(cur);
988
+ const next = exports.get_aliasee();
989
+ if (next === -1 || next === cur)
990
+ break;
991
+ cur = next;
992
+ }
993
+ const filePath = unwrapString(wasm_exports.decl_file_path(cur));
994
+ if (filePath && filePath.length > 0) {
995
+ const fileDecl = findFileRoot(filePath);
996
+ if (fileDecl !== null) {
997
+ let markdown = "";
998
+ markdown += "# " + filePath + "\n\n";
999
+ markdown += unwrapString(wasm_exports.decl_source_html(fileDecl));
1000
+ return markdown;
1001
+ }
1002
+ }
1003
+ return `# Error\n\nCould not find source file for "${name}".`;
1004
+ }
1005
+ return renderDecl(decl_index);
1006
+ }