@typesol/cli 0.49.0 → 0.51.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.
Files changed (79) hide show
  1. package/CHANGELOG.md +541 -0
  2. package/NOTICE +9 -0
  3. package/README.md +34 -15
  4. package/SECURITY.md +6 -3
  5. package/dist/cli.js +36 -5
  6. package/dist/compiler/analyze.d.ts +10 -1
  7. package/dist/compiler/analyze.js +234 -20
  8. package/dist/compiler/diagnostics.d.ts +4 -0
  9. package/dist/compiler/diagnostics.js +5 -1
  10. package/dist/compiler/emit/base.d.ts +35 -4
  11. package/dist/compiler/emit/base.js +46 -4
  12. package/dist/compiler/emit/borsh.js +8 -8
  13. package/dist/compiler/emit/calls.d.ts +6 -0
  14. package/dist/compiler/emit/calls.js +98 -18
  15. package/dist/compiler/emit/cpi-core.d.ts +3 -0
  16. package/dist/compiler/emit/cpi-core.js +11 -5
  17. package/dist/compiler/emit/cpi.js +6 -4
  18. package/dist/compiler/emit/create.js +9 -9
  19. package/dist/compiler/emit/expressions.d.ts +9 -1
  20. package/dist/compiler/emit/expressions.js +77 -0
  21. package/dist/compiler/emit/function.d.ts +2 -0
  22. package/dist/compiler/emit/function.js +71 -15
  23. package/dist/compiler/emit/outline.d.ts +35 -0
  24. package/dist/compiler/emit/outline.js +547 -0
  25. package/dist/compiler/emit/program.js +19 -4
  26. package/dist/compiler/emit/rbtree.js +1 -1
  27. package/dist/compiler/emit/shared.d.ts +69 -4
  28. package/dist/compiler/emit/shared.js +44 -9
  29. package/dist/compiler/emit/statements.d.ts +36 -2
  30. package/dist/compiler/emit/statements.js +332 -29
  31. package/dist/compiler/emit/storage.d.ts +16 -0
  32. package/dist/compiler/emit/storage.js +134 -13
  33. package/dist/compiler/emit/views.js +1 -1
  34. package/dist/compiler/hints.d.ts +1 -1
  35. package/dist/compiler/hints.js +9 -1
  36. package/dist/compiler/idl.js +1 -1
  37. package/dist/compiler/index.d.ts +30 -2
  38. package/dist/compiler/index.js +94 -16
  39. package/dist/compiler/model.d.ts +13 -1
  40. package/dist/compiler/stack.d.ts +71 -0
  41. package/dist/compiler/stack.js +309 -0
  42. package/dist/compiler/toolchain.d.ts +87 -5
  43. package/dist/compiler/toolchain.js +384 -19
  44. package/dist/keys.d.ts +12 -1
  45. package/dist/keys.js +57 -7
  46. package/dist/workspace.d.ts +9 -0
  47. package/dist/workspace.js +91 -16
  48. package/docker/deps/0.50.0.lock.json +1206 -0
  49. package/docker/deps/0.51.0.lock.json +1206 -0
  50. package/docs/architecture.md +1 -1
  51. package/docs/audit.md +16 -13
  52. package/docs/case-study-escrow.md +1 -1
  53. package/docs/case-study-jito-mev.md +14 -14
  54. package/docs/case-study-marginfi.md +127 -127
  55. package/docs/case-study-multisig.md +16 -4
  56. package/docs/case-study-raydium-clmm.md +44 -28
  57. package/docs/case-study-raydium-cp-swap.md +18 -18
  58. package/docs/cli.md +5 -4
  59. package/docs/glossary.md +2 -1
  60. package/docs/language-accounts.md +16 -10
  61. package/docs/language-basics.md +29 -2
  62. package/docs/language-builtins.md +4 -2
  63. package/docs/language-cpi.md +34 -0
  64. package/docs/language-native.md +50 -4
  65. package/docs/language-pdas.md +6 -2
  66. package/docs/language-statements.md +103 -12
  67. package/docs/language-types.md +93 -8
  68. package/docs/runtime.md +20 -3
  69. package/docs/security-model.md +6 -3
  70. package/docs/stability.md +10 -5
  71. package/docs/testing.md +6 -2
  72. package/docs/troubleshooting.md +45 -1
  73. package/lib/typesol.d.ts +25 -3
  74. package/llms-full.txt +403 -54
  75. package/package.json +1 -1
  76. package/releases/0.49.0.json +857 -0
  77. package/releases/0.50.0.json +874 -0
  78. package/runtime/typesol_builtins.c +47 -2
  79. package/runtime/typesol_rt.h +254 -54
package/dist/workspace.js CHANGED
@@ -13,7 +13,7 @@
13
13
  */
14
14
  import { agentsMd, CLAUDE_MD, cursorRule, MCP_JSON } from "./ai.js";
15
15
  import { createHash } from "node:crypto";
16
- import { closeSync, existsSync, mkdirSync, openSync, readFileSync, statSync, unlinkSync, writeFileSync, writeSync } from "node:fs";
16
+ import { closeSync, existsSync, lstatSync, mkdirSync, openSync, readFileSync, realpathSync, renameSync, statSync, unlinkSync, writeSync } from "node:fs";
17
17
  import os from "node:os";
18
18
  import path from "node:path";
19
19
  import ts from "typescript";
@@ -97,13 +97,45 @@ export function programAddress(ws, name, opts = {}) {
97
97
  if (recorded && recorded !== address)
98
98
  throw new Error(`${name}: ${path.relative(ws.root, paths.keypair)} is ${address}, but ${CONFIG} records ${recorded}. Restore the right keypair.`);
99
99
  if (!recorded) {
100
- const raw = JSON.parse(readFileSync(ws.file, "utf8"));
101
- raw.programs[name] = { path: ws.programs[name].path, address };
102
- writeFileSync(ws.file, JSON.stringify(raw, null, 2) + "\n");
100
+ // Another process (a first build of another program, under its own lock) may be recording its address now.
101
+ updateConfig(ws, (raw) => {
102
+ const entry = raw.programs[name];
103
+ const current = typeof entry === "object" ? entry : { path: entry ?? ws.programs[name].path };
104
+ if (current.address && current.address !== address)
105
+ throw new Error(`${name}: ${CONFIG} now records ${current.address}, but ${path.relative(ws.root, paths.keypair)} is ${address}. Restore the right keypair.`);
106
+ raw.programs[name] = { ...current, address };
107
+ });
103
108
  ws.programs[name].address = address;
104
109
  }
105
110
  return { address, created };
106
111
  }
112
+ /**
113
+ * Change typesol.json: read it, `edit` it and write it back, one process at a time (`<file>.lock`, as a build's
114
+ * lock), the new text written to a temporary file and renamed over it, so that concurrent first builds each keep
115
+ * their address and no reader sees a partly written file.
116
+ */
117
+ export function updateConfig(ws, edit) {
118
+ withLockFile(`${ws.file}.lock`, () => {
119
+ const raw = JSON.parse(readFileSync(ws.file, "utf8"));
120
+ edit(raw);
121
+ // A linked typesol.json stays a link: the file it points to is the one replaced.
122
+ const target = realpathSync(ws.file);
123
+ const tmp = `${target}.${process.pid}.${Math.random().toString(36).slice(2)}.tmp`;
124
+ writeNewFile(tmp, JSON.stringify(raw, null, 2) + "\n");
125
+ try {
126
+ renameSync(tmp, target);
127
+ }
128
+ catch (e) {
129
+ try {
130
+ unlinkSync(tmp);
131
+ }
132
+ catch {
133
+ // Already gone.
134
+ }
135
+ throw e;
136
+ }
137
+ });
138
+ }
107
139
  /** Build every program (or the ones named). */
108
140
  export function buildWorkspace(ws, names = Object.keys(ws.programs), opts = {}) {
109
141
  return names.map((name) => {
@@ -112,7 +144,7 @@ export function buildWorkspace(ws, names = Object.keys(ws.programs), opts = {})
112
144
  // another process building it at the same time (vitest runs test files in parallel workers) would overwrite them.
113
145
  return withBuildLock(paths.outDir, () => {
114
146
  const { address, created, keyless } = programAddress(ws, name, { needKeypair: opts.needKeypair });
115
- const result = build(paths.entry, paths.outDir, address, { sbpf: opts.sbpf ?? ws.sbpf, profile: opts.profile });
147
+ const result = build(paths.entry, paths.outDir, address, { sbpf: opts.sbpf ?? ws.sbpf, profile: opts.profile, stackAuto: opts.stackAuto });
116
148
  // The TypeScript client (docs/client.md) is written with the build, from the same IDL.
117
149
  const clientPath = writeClient(result.idl, path.join(paths.outDir, `${result.model.name}.client.ts`)).path;
118
150
  return { paths, address, created, keyless, result, clientPath };
@@ -217,7 +249,10 @@ const LOCK_POLL_MS = 100;
217
249
  */
218
250
  export function withBuildLock(dir, fn) {
219
251
  mkdirSync(dir, { recursive: true });
220
- const lock = path.join(dir, ".typesol-build.lock");
252
+ return withLockFile(path.join(dir, ".typesol-build.lock"), fn);
253
+ }
254
+ /** Run `fn` holding `lock` (see withBuildLock): created exclusively, taken over when its holder is gone or it is stale. */
255
+ function withLockFile(lock, fn) {
221
256
  const me = `${os.hostname()} ${process.pid} ${Date.now()}`;
222
257
  const deadline = Date.now() + LOCK_STALE_MS + 60_000;
223
258
  for (;;) {
@@ -401,11 +436,52 @@ export function scaffold(dir, name, template = "counter", opts = {}) {
401
436
  const agent = existing.some((f) => AGENT_FILES.includes(f));
402
437
  if (existing.length)
403
438
  throw new Error(`refusing to overwrite: ${existing.join(", ")}${agent ? " (--no-agent-files leaves out the files for AI coding agents)" : ""}`);
439
+ writeProjectFiles(dir, files);
440
+ return Object.keys(files);
441
+ }
442
+ /**
443
+ * Write new files under `root` (paths relative to it). Refuses, before writing anything, a path through a symbolic link
444
+ * below `root` (a linked directory or a link where a file goes, which would put the file outside the project), then
445
+ * creates each directory and each file exclusively, so an existing file (or one created meanwhile) is never overwritten.
446
+ * `root` itself may be a link: the project is where it points.
447
+ */
448
+ function writeProjectFiles(root, files) {
449
+ for (const file of Object.keys(files)) {
450
+ const parts = file.split("/");
451
+ for (let i = 1; i <= parts.length; i++) {
452
+ const rel = parts.slice(0, i).join("/");
453
+ let link = false;
454
+ try {
455
+ link = lstatSync(path.join(root, rel)).isSymbolicLink();
456
+ }
457
+ catch {
458
+ break; // Does not exist: neither does anything below it.
459
+ }
460
+ if (link)
461
+ throw new Error(`refusing to write ${file}: ${rel} is a symbolic link (it may point outside the project); replace it with a directory or file of the project`);
462
+ }
463
+ }
404
464
  for (const [file, text] of Object.entries(files)) {
405
- mkdirSync(path.dirname(path.join(dir, file)), { recursive: true });
406
- writeFileSync(path.join(dir, file), text);
465
+ mkdirSync(path.dirname(path.join(root, file)), { recursive: true });
466
+ try {
467
+ writeNewFile(path.join(root, file), text);
468
+ }
469
+ catch (e) {
470
+ if (e.code === "EEXIST")
471
+ throw new Error(`refusing to overwrite: ${file}`);
472
+ throw e;
473
+ }
474
+ }
475
+ }
476
+ /** Create `file` with `text`; fails with EEXIST if anything (a file, a link, even a dangling one) is already there. */
477
+ function writeNewFile(file, text) {
478
+ const fd = openSync(file, "wx");
479
+ try {
480
+ writeSync(fd, text);
481
+ }
482
+ finally {
483
+ closeSync(fd);
407
484
  }
408
- return Object.keys(files);
409
485
  }
410
486
  /**
411
487
  * Add a program to an existing project: programs/<name>.ts and tests/<name>.test.ts from a template, and its entry in
@@ -421,13 +497,12 @@ export function addProgram(ws, name, template = "counter") {
421
497
  const existing = Object.keys(files).filter((f) => existsSync(path.join(ws.root, f)));
422
498
  if (existing.length)
423
499
  throw new Error(`refusing to overwrite: ${existing.join(", ")}`);
424
- for (const [file, text] of Object.entries(files)) {
425
- mkdirSync(path.dirname(path.join(ws.root, file)), { recursive: true });
426
- writeFileSync(path.join(ws.root, file), text);
427
- }
428
- const raw = JSON.parse(readFileSync(ws.file, "utf8"));
429
- raw.programs[name] = { path: `programs/${name}.ts` };
430
- writeFileSync(ws.file, JSON.stringify(raw, null, 2) + "\n");
500
+ writeProjectFiles(ws.root, files);
501
+ updateConfig(ws, (raw) => {
502
+ if (raw.programs[name])
503
+ throw new Error(`${CONFIG} already has a program "${name}"`);
504
+ raw.programs[name] = { path: `programs/${name}.ts` };
505
+ });
431
506
  ws.programs[name] = { path: `programs/${name}.ts` };
432
507
  const pkgFile = path.join(ws.root, "package.json");
433
508
  const pkg = existsSync(pkgFile)