functionalscript 0.32.3 → 0.33.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 (41) hide show
  1. package/fs/bnf/data/proof.f.d.ts +3 -0
  2. package/fs/bnf/data/proof.f.js +7 -0
  3. package/fs/cas/mcp/module.f.d.ts +4 -4
  4. package/fs/cas/mcp/module.f.js +21 -17
  5. package/fs/cas/mcp/proof.f.d.ts +1 -0
  6. package/fs/cas/mcp/proof.f.js +62 -42
  7. package/fs/cas/module.f.d.ts +13 -21
  8. package/fs/cas/module.f.js +100 -54
  9. package/fs/cas/proof.f.js +15 -24
  10. package/fs/ci/config/module.f.d.ts +6 -6
  11. package/fs/ci/config/module.f.js +6 -6
  12. package/fs/ci/proof.f.js +8 -6
  13. package/fs/cli/proof.f.js +2 -12
  14. package/fs/djs/parser/module.f.d.ts +0 -5
  15. package/fs/djs/proof.f.js +5 -6
  16. package/fs/djs/serializer/module.f.d.ts +1 -3
  17. package/fs/djs/tokenizer-new/module.f.js +2 -1
  18. package/fs/djs/tokenizer-new/proof.f.d.ts +1 -0
  19. package/fs/djs/tokenizer-new/proof.f.js +101 -67
  20. package/fs/djs/transpiler/proof.f.js +10 -10
  21. package/fs/effects/node/module.f.d.ts +8 -2
  22. package/fs/effects/node/module.f.js +3 -0
  23. package/fs/effects/node/module.js +21 -1
  24. package/fs/effects/node/proof.f.d.ts +15 -0
  25. package/fs/effects/node/proof.f.js +137 -26
  26. package/fs/effects/node/virtual/module.f.d.ts +18 -2
  27. package/fs/effects/node/virtual/module.f.js +185 -10
  28. package/fs/effects/node/virtual/proof.f.d.ts +14 -0
  29. package/fs/effects/node/virtual/proof.f.js +98 -5
  30. package/fs/emergent_testing/proof.f.js +3 -11
  31. package/fs/fjs/module.f.js +3 -3
  32. package/fs/fjs/proof.f.js +2 -11
  33. package/fs/js/tokenizer/module.f.d.ts +5 -0
  34. package/fs/js/tokenizer/module.f.js +13 -0
  35. package/fs/json/serializer/module.f.js +0 -1
  36. package/fs/text/sgr/proof.f.js +2 -11
  37. package/fs/types/bigint/module.f.d.ts +8 -15
  38. package/fs/types/bigint/module.f.js +9 -22
  39. package/fs/types/bigint/proof.f.d.ts +0 -2
  40. package/fs/types/bigint/proof.f.js +6 -16
  41. package/package.json +2 -2
@@ -22,6 +22,7 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
22
22
  */
23
23
  import http from 'node:http';
24
24
  import childProcess from 'node:child_process';
25
+ import crypto from 'node:crypto';
25
26
  import fs from 'node:fs';
26
27
  import os from 'node:os';
27
28
  import process from 'node:process';
@@ -58,7 +59,7 @@ const collect = async (v) => {
58
59
  }
59
60
  return result;
60
61
  };
61
- const { mkdir, readFile, readdir, writeFile, rm, access, stat } = fs.promises;
62
+ const { mkdir, open, readFile, readdir, rename, writeFile, rm, access, stat } = fs.promises;
62
63
  const { exec } = childProcess;
63
64
  const maxFileSizeBytes = Number(maxLengthBytes);
64
65
  const prefix = 'file:///';
@@ -179,6 +180,25 @@ const runNodeEffect = asyncRun({
179
180
  }))),
180
181
  writeFile: (path, data) => tc(() => writeFile(path, fromVec(data))),
181
182
  rm: path => tc(() => rm(path)),
183
+ rename: (src, dst) => tc(() => rename(src, dst)),
184
+ readBytes: (path, offset, size) => tc(async () => {
185
+ if (offset < 0) {
186
+ throw new Error(`Offset ${offset} is negative`);
187
+ }
188
+ if (size > maxFileSizeBytes) {
189
+ throw new Error(`Chunk size ${size} exceeds maximum allowed size of ${maxFileSizeBytes} bytes`);
190
+ }
191
+ const fh = await open(path, 'r');
192
+ try {
193
+ const buffer = Buffer.alloc(size);
194
+ const { bytesRead } = await fh.read(buffer, 0, size, offset);
195
+ return toVec(buffer.subarray(0, bytesRead));
196
+ }
197
+ finally {
198
+ await fh.close();
199
+ }
200
+ }),
201
+ randomInt: async () => crypto.randomInt(2 ** 32),
182
202
  access: path => tc(() => access(path)),
183
203
  import: path => tc(() => asyncImport(path)),
184
204
  exec: (command, stdin) => new Promise(resolve => {
@@ -46,4 +46,19 @@ export declare const proof: {
46
46
  createAndRead: () => void;
47
47
  createAndWrite: () => void;
48
48
  };
49
+ rename: {
50
+ fileOverFile: () => void;
51
+ nestedRename: () => void;
52
+ dirOverFile: () => void;
53
+ missingSource: () => void;
54
+ };
55
+ readBytes: {
56
+ simple: () => void;
57
+ withOffset: () => void;
58
+ oversizeChunk: () => void;
59
+ missingFile: () => void;
60
+ };
61
+ randomInt: {
62
+ increments: () => void;
63
+ };
49
64
  };
@@ -1,7 +1,7 @@
1
1
  import { empty, isVec, uint, vec8 } from "../../types/bit_vec/module.f.js";
2
2
  import { utf8, utf8ToString } from "../../text/module.f.js";
3
3
  import { decode, pure } from "../module.f.js";
4
- import { both, fetch, mkdir, now, readdir, readFile, readUtf8File, rm, sandbox, writeFile, writeUtf8File } from "./module.f.js";
4
+ import { both, fetch, mkdir, now, readdir, readFile, readUtf8File, rm, sandbox, writeFile, writeUtf8File, rename, readBytes, randomInt } from "./module.f.js";
5
5
  import { create as memCreate, read as memRead, write as memWrite } from "../memory/module.f.js";
6
6
  import { emptyState, virtual } from "./virtual/module.f.js";
7
7
  export const proof = {
@@ -51,7 +51,7 @@ export const proof = {
51
51
  throw result;
52
52
  }
53
53
  const a = state.root.a;
54
- if (a === undefined || isVec(a)) {
54
+ if (a === undefined || Array.isArray(a)) {
55
55
  throw a;
56
56
  }
57
57
  },
@@ -61,11 +61,11 @@ export const proof = {
61
61
  throw result;
62
62
  }
63
63
  const tmp = state.root.tmp;
64
- if (typeof tmp !== 'object') {
64
+ if (typeof tmp !== 'object' || Array.isArray(tmp)) {
65
65
  throw state.root;
66
66
  }
67
67
  const cache = tmp.cache;
68
- if (typeof cache !== 'object') {
68
+ if (typeof cache !== 'object' || Array.isArray(cache)) {
69
69
  throw tmp;
70
70
  }
71
71
  },
@@ -84,7 +84,7 @@ export const proof = {
84
84
  const initial = {
85
85
  ...emptyState,
86
86
  root: {
87
- hello: vec8(0x2an),
87
+ hello: [vec8(0x2an)],
88
88
  },
89
89
  };
90
90
  const [state, [t, result]] = virtual(initial)(readFile('hello'));
@@ -104,7 +104,7 @@ export const proof = {
104
104
  nested: () => {
105
105
  const [_, [tag, result]] = virtual({
106
106
  ...emptyState,
107
- root: { tmp: { cache: vec8(0x15n) } }
107
+ root: { tmp: { cache: [vec8(0x15n)] } }
108
108
  })(readFile('tmp/cache'));
109
109
  if (tag === 'error') {
110
110
  throw result;
@@ -133,7 +133,7 @@ export const proof = {
133
133
  const initial = {
134
134
  ...emptyState,
135
135
  root: {
136
- smallFile: vec8(0x2an),
136
+ smallFile: [vec8(0x2an)],
137
137
  },
138
138
  };
139
139
  const [_, [t, result]] = virtual(initial)(readFile('smallFile'));
@@ -149,7 +149,7 @@ export const proof = {
149
149
  ok: () => {
150
150
  const [_, [t, result]] = virtual({
151
151
  ...emptyState,
152
- root: { hello: utf8('Hello, world!') },
152
+ root: { hello: [utf8('Hello, world!')] },
153
153
  })(readUtf8File('hello'));
154
154
  if (t !== 'ok') {
155
155
  throw result;
@@ -170,9 +170,9 @@ export const proof = {
170
170
  const [_, [t, result]] = virtual({
171
171
  ...emptyState,
172
172
  root: {
173
- file: vec8(0x2an),
173
+ file: [vec8(0x2an)],
174
174
  dir: {
175
- a: empty
175
+ a: [empty]
176
176
  },
177
177
  },
178
178
  })(readdir('', { recursive: true }));
@@ -192,9 +192,9 @@ export const proof = {
192
192
  const [_, [t, result]] = virtual({
193
193
  ...emptyState,
194
194
  root: {
195
- file: vec8(0x2an),
195
+ file: [vec8(0x2an)],
196
196
  dir: {
197
- a: empty
197
+ a: [empty]
198
198
  },
199
199
  },
200
200
  })(readdir('', {}));
@@ -216,7 +216,7 @@ export const proof = {
216
216
  nested: () => {
217
217
  const [_, [t, result]] = virtual({
218
218
  ...emptyState,
219
- root: { tmp: { cache: vec8(0x15n) } }
219
+ root: { tmp: { cache: [vec8(0x15n)] } }
220
220
  })(readdir('tmp', { recursive: true }));
221
221
  if (t !== 'ok') {
222
222
  throw result;
@@ -249,10 +249,10 @@ export const proof = {
249
249
  throw result;
250
250
  }
251
251
  const file = state.root.hello;
252
- if (!isVec(file)) {
252
+ if (!Array.isArray(file)) {
253
253
  throw file;
254
254
  }
255
- if (uint(file) !== 0x2an) {
255
+ if (uint(file[0]) !== 0x2an) {
256
256
  throw file;
257
257
  }
258
258
  },
@@ -260,17 +260,17 @@ export const proof = {
260
260
  const [state, [t, result]] = virtual({
261
261
  ...emptyState,
262
262
  root: {
263
- hello: vec8(0x15n),
263
+ hello: [vec8(0x15n)],
264
264
  },
265
265
  })(writeFile('hello', vec8(0x2an)));
266
266
  if (t !== 'ok') {
267
267
  throw result;
268
268
  }
269
269
  const file = state.root.hello;
270
- if (!isVec(file)) {
270
+ if (!Array.isArray(file)) {
271
271
  throw file;
272
272
  }
273
- if (uint(file) !== 0x2an) {
273
+ if (uint(file[0]) !== 0x2an) {
274
274
  throw file;
275
275
  }
276
276
  },
@@ -300,7 +300,7 @@ export const proof = {
300
300
  throw result;
301
301
  }
302
302
  const tmp = state.root.tmp;
303
- if (tmp === undefined || isVec(tmp)) {
303
+ if (tmp === undefined || Array.isArray(tmp)) {
304
304
  throw tmp;
305
305
  }
306
306
  },
@@ -311,10 +311,10 @@ export const proof = {
311
311
  throw result;
312
312
  }
313
313
  const file = state.root.hello;
314
- if (!isVec(file)) {
314
+ if (!Array.isArray(file)) {
315
315
  throw file;
316
316
  }
317
- if (utf8ToString(file) !== 'Hello, world!') {
317
+ if (utf8ToString(file[0]) !== 'Hello, world!') {
318
318
  throw file;
319
319
  }
320
320
  },
@@ -322,7 +322,7 @@ export const proof = {
322
322
  one: () => {
323
323
  const [state, [t, result]] = virtual({
324
324
  ...emptyState,
325
- root: { hello: vec8(0x2an) },
325
+ root: { hello: [vec8(0x2an)] },
326
326
  })(rm('hello'));
327
327
  if (t !== 'ok') {
328
328
  throw result;
@@ -334,13 +334,13 @@ export const proof = {
334
334
  nested: () => {
335
335
  const [state, [t, result]] = virtual({
336
336
  ...emptyState,
337
- root: { tmp: { cache: vec8(0x15n) } },
337
+ root: { tmp: { cache: [vec8(0x15n)] } },
338
338
  })(rm('tmp/cache'));
339
339
  if (t !== 'ok') {
340
340
  throw result;
341
341
  }
342
342
  const tmp = state.root.tmp;
343
- if (typeof tmp !== 'object') {
343
+ if (typeof tmp !== 'object' || Array.isArray(tmp)) {
344
344
  throw state.root;
345
345
  }
346
346
  if (tmp.cache !== undefined) {
@@ -376,8 +376,8 @@ export const proof = {
376
376
  const [_, results] = virtual({
377
377
  ...emptyState,
378
378
  root: {
379
- a: vec8(0x2an),
380
- b: vec8(0x15n),
379
+ a: [vec8(0x2an)],
380
+ b: [vec8(0x15n)],
381
381
  },
382
382
  })(both(readFile('a'))(readFile('b')));
383
383
  if (results[0][0] !== 'ok') {
@@ -442,4 +442,115 @@ export const proof = {
442
442
  }
443
443
  },
444
444
  },
445
+ rename: {
446
+ fileOverFile: () => {
447
+ const [state, [t, result]] = virtual({
448
+ ...emptyState,
449
+ root: { src: [vec8(0x2an)], dst: [vec8(0x15n)] },
450
+ })(rename('src', 'dst'));
451
+ if (t !== 'ok') {
452
+ throw result;
453
+ }
454
+ if (state.root.src !== undefined) {
455
+ throw state.root;
456
+ }
457
+ if (!Array.isArray(state.root.dst)) {
458
+ throw state.root;
459
+ }
460
+ if (uint(state.root.dst[0]) !== 0x2an) {
461
+ throw state.root;
462
+ }
463
+ },
464
+ nestedRename: () => {
465
+ const [state, [t, result]] = virtual({
466
+ ...emptyState,
467
+ root: { tmp: { src: [vec8(0x2an)] } },
468
+ })(rename('tmp/src', 'tmp/dst'));
469
+ if (t !== 'ok') {
470
+ throw result;
471
+ }
472
+ const tmp = state.root.tmp;
473
+ if (typeof tmp !== 'object' || Array.isArray(tmp)) {
474
+ throw state.root;
475
+ }
476
+ if (tmp.src !== undefined) {
477
+ throw tmp;
478
+ }
479
+ },
480
+ dirOverFile: () => {
481
+ const [state, [t, result]] = virtual({
482
+ ...emptyState,
483
+ root: { src: {}, dst: [vec8(0x15n)] },
484
+ })(rename('src', 'dst'));
485
+ if (t !== 'error') {
486
+ throw result;
487
+ }
488
+ if (!Array.isArray(state.root.dst)) {
489
+ throw state.root;
490
+ }
491
+ },
492
+ missingSource: () => {
493
+ const [_, [t, result]] = virtual(emptyState)(rename('missing', 'dst'));
494
+ if (t !== 'error') {
495
+ throw result;
496
+ }
497
+ },
498
+ },
499
+ readBytes: {
500
+ simple: () => {
501
+ const [_, [t, result]] = virtual({
502
+ ...emptyState,
503
+ root: { file: [vec8(0xabn)] },
504
+ })(readBytes('file', 0, 1));
505
+ if (t !== 'ok') {
506
+ throw result;
507
+ }
508
+ if (!isVec(result)) {
509
+ throw result;
510
+ }
511
+ },
512
+ withOffset: () => {
513
+ const [_, [t, result]] = virtual({
514
+ ...emptyState,
515
+ root: { file: [vec8(0xabn), vec8(0xcdn)] },
516
+ })(readBytes('file', 1, 1));
517
+ if (t !== 'ok') {
518
+ throw result;
519
+ }
520
+ if (!isVec(result)) {
521
+ throw result;
522
+ }
523
+ },
524
+ oversizeChunk: () => {
525
+ const [_, [t, result]] = virtual({
526
+ ...emptyState,
527
+ root: { file: [vec8(0x2an)] },
528
+ })(readBytes('file', 0, Number(2n ** 32n)));
529
+ if (t !== 'error') {
530
+ throw result;
531
+ }
532
+ },
533
+ missingFile: () => {
534
+ const [_, [t, result]] = virtual(emptyState)(readBytes('missing', 0, 4));
535
+ if (t !== 'error') {
536
+ throw result;
537
+ }
538
+ },
539
+ },
540
+ randomInt: {
541
+ increments: () => {
542
+ const [state1, r1] = virtual(emptyState)(randomInt());
543
+ if (r1 !== 0) {
544
+ throw r1;
545
+ }
546
+ const [state2, r2] = virtual(state1)(randomInt());
547
+ if (r2 !== 1) {
548
+ throw r2;
549
+ }
550
+ const [_, r3] = virtual(state2)(randomInt());
551
+ if (r3 !== 2) {
552
+ throw r3;
553
+ }
554
+ },
555
+ },
445
556
  };
@@ -1,6 +1,6 @@
1
1
  import { type Vec } from '../../../types/bit_vec/module.f.ts';
2
2
  import { type RunInstance } from '../../mock/module.f.ts';
3
- import type { Module, NodeOp } from '../module.f.ts';
3
+ import type { Module, NodeOp, NodeProgramOptions } from '../module.f.ts';
4
4
  /**
5
5
  * In-memory JS module entry. When `import_` is called on the path, the
6
6
  * function is invoked and its return value is the module value (with a
@@ -10,7 +10,7 @@ import type { Module, NodeOp } from '../module.f.ts';
10
10
  * each import for closures/state.
11
11
  */
12
12
  export type JsModule = () => Module;
13
- export type Entity = Vec | Dir | JsModule;
13
+ export type Entity = readonly Vec[] | Dir | JsModule;
14
14
  export type Dir = {
15
15
  readonly [name in string]?: Entity;
16
16
  };
@@ -28,6 +28,22 @@ export type State = {
28
28
  memoryValues: {
29
29
  readonly [key: string]: unknown;
30
30
  };
31
+ /** Monotonically increasing counter returned by `randomInt`; starts at 0. */
32
+ randomNext: number;
31
33
  };
32
34
  export declare const emptyState: State;
33
35
  export declare const virtual: RunInstance<NodeOp, State>;
36
+ /**
37
+ * Safe, inert defaults for every {@link NodeProgramOptions} field, intended for
38
+ * proof files that need to call a program without owning the full literal.
39
+ *
40
+ * Proofs spread-override only what their test cares about:
41
+ *
42
+ * ```ts
43
+ * const opts: NodeProgramOptions = { ...defaultNodeProgramOptions, args }
44
+ * ```
45
+ *
46
+ * Future additions to `NodeProgramOptions` only need a default added here,
47
+ * keeping unrelated proof files from churning.
48
+ */
49
+ export declare const defaultNodeProgramOptions: NodeProgramOptions;
@@ -6,7 +6,7 @@
6
6
  import { todo } from "../../../asserts/module.f.js";
7
7
  import { join, parse } from "../../../path/module.f.js";
8
8
  import { utf8ToString } from "../../../text/module.f.js";
9
- import { isVec } from "../../../types/bit_vec/module.f.js";
9
+ import { empty, length, maxLengthBytes, msb, vec } from "../../../types/bit_vec/module.f.js";
10
10
  import { error, ok } from "../../../types/result/module.f.js";
11
11
  import { run } from "../../mock/module.f.js";
12
12
  import { asBase, asNominal } from "../../memory/module.f.js";
@@ -19,6 +19,7 @@ export const emptyState = {
19
19
  epochNs: 0,
20
20
  memoryNext: 0,
21
21
  memoryValues: {},
22
+ randomNext: 0,
22
23
  };
23
24
  const operation = (op) => {
24
25
  const f = (dir, path) => {
@@ -27,7 +28,7 @@ const operation = (op) => {
27
28
  }
28
29
  const [first, ...rest] = path;
29
30
  const subDir = dir[first];
30
- if (typeof subDir !== 'object') {
31
+ if (typeof subDir !== 'object' || Array.isArray(subDir)) {
31
32
  return op(dir, path);
32
33
  }
33
34
  const [newSubDir, r] = f(subDir, rest);
@@ -66,11 +67,23 @@ const readFile = readOperation((dir, path) => {
66
67
  if (file === undefined) {
67
68
  return enoent;
68
69
  }
69
- // exists but is a directory — a real error, not a missing path
70
- if (!isVec(file)) {
70
+ if (!Array.isArray(file)) {
71
71
  return error(`'${path[0]}' is not a file`);
72
72
  }
73
- return ok(file);
73
+ const chunks = file;
74
+ const capBits = maxLengthBytes * 8n;
75
+ let result = empty;
76
+ for (const chunk of chunks) {
77
+ const chunkLen = length(chunk);
78
+ if (chunkLen === 0n) {
79
+ continue;
80
+ }
81
+ if (length(result) + chunkLen > capBits) {
82
+ return error(`File size exceeds maximum allowed size of ${maxLengthBytes} bytes`);
83
+ }
84
+ result = msb.concat(result)(chunk);
85
+ }
86
+ return ok(result);
74
87
  });
75
88
  const import_ = readOperation((dir, path) => {
76
89
  if (path.length !== 1) {
@@ -89,11 +102,10 @@ const writeFile = (payload) => operation((dir, path) => {
89
102
  }
90
103
  const [name] = path;
91
104
  const file = dir[name];
92
- // fail if the file is a directory
93
- if (file !== undefined && !isVec(file)) {
105
+ if (file !== undefined && !Array.isArray(file)) {
94
106
  return [dir, writeFileError];
95
107
  }
96
- dir = { ...dir, [name]: payload };
108
+ dir = { ...dir, [name]: [payload] };
97
109
  return [dir, okVoid];
98
110
  });
99
111
  const invalidPath = error('invalid path');
@@ -108,7 +120,7 @@ const readdir = (base, recursive) => readOperation((dir, path) => {
108
120
  if (content === undefined) {
109
121
  continue;
110
122
  }
111
- const isFile = typeof content !== 'object';
123
+ const isFile = Array.isArray(content) || typeof content !== 'object';
112
124
  result = [...result, { name, parentPath, isFile }];
113
125
  if (!isFile && recursive) {
114
126
  result = [...result, ...f(join(parentPath, name), content)];
@@ -136,12 +148,148 @@ const rm = operation((dir, path) => {
136
148
  if (entry === undefined) {
137
149
  return [dir, error('no such file')];
138
150
  }
139
- if (typeof entry === 'object') {
151
+ if (!Array.isArray(entry) && typeof entry === 'object') {
140
152
  return [dir, error('is a directory')];
141
153
  }
142
154
  const { [name]: _, ...rest } = dir;
143
155
  return [rest, okVoid];
144
156
  });
157
+ const extractEntity = (dir, path) => {
158
+ if (path.length === 0) {
159
+ return [dir, error('cannot extract root')];
160
+ }
161
+ if (path.length === 1) {
162
+ const [name] = path;
163
+ const entry = dir[name];
164
+ if (entry === undefined) {
165
+ return [dir, enoent];
166
+ }
167
+ const { [name]: _, ...rest } = dir;
168
+ return [rest, ok(entry)];
169
+ }
170
+ const [first, ...rest] = path;
171
+ const sub = dir[first];
172
+ if (sub === undefined || Array.isArray(sub) || typeof sub === 'function') {
173
+ return [dir, enoent];
174
+ }
175
+ const [newSub, result] = extractEntity(sub, rest);
176
+ if (result[0] === 'error') {
177
+ return [dir, result];
178
+ }
179
+ return [{ ...dir, [first]: newSub }, result];
180
+ };
181
+ const insertEntityAt = (dir, path, entity) => {
182
+ if (path.length === 0) {
183
+ return [dir, error('cannot insert at root')];
184
+ }
185
+ if (path.length === 1) {
186
+ const [name] = path;
187
+ const existing = dir[name];
188
+ if (existing !== undefined) {
189
+ const entityIsDir = !Array.isArray(entity) && typeof entity === 'object';
190
+ const existingIsDir = !Array.isArray(existing) && typeof existing === 'object';
191
+ if (entityIsDir && !existingIsDir) {
192
+ return [dir, error(`cannot overwrite file '${name}' with a directory`)];
193
+ }
194
+ if (!entityIsDir && existingIsDir) {
195
+ return [dir, error(`'${name}' is a directory`)];
196
+ }
197
+ if (entityIsDir && existingIsDir) {
198
+ const existingDir = existing;
199
+ const hasContent = Object.values(existingDir).some(v => v !== undefined);
200
+ if (hasContent) {
201
+ return [dir, error(`cannot overwrite non-empty directory '${name}'`)];
202
+ }
203
+ }
204
+ }
205
+ return [{ ...dir, [name]: entity }, okVoid];
206
+ }
207
+ const [first, ...rest] = path;
208
+ const sub = dir[first];
209
+ if (sub === undefined) {
210
+ return [dir, enoent];
211
+ }
212
+ if (Array.isArray(sub) || typeof sub === 'function') {
213
+ return [dir, error('not a directory')];
214
+ }
215
+ const [newSub, result] = insertEntityAt(sub, rest, entity);
216
+ if (result[0] === 'error') {
217
+ return [dir, result];
218
+ }
219
+ return [{ ...dir, [first]: newSub }, result];
220
+ };
221
+ const isProperPrefix = (prefix, path) => prefix.length < path.length && prefix.every((seg, i) => seg === path[i]);
222
+ const rename = (src, dst) => (state) => {
223
+ const srcParsed = parse(src);
224
+ const dstParsed = parse(dst);
225
+ // extract source first to report ENOENT if it's missing, before checking subtree guards
226
+ const [srcRoot, srcResult] = extractEntity(state.root, srcParsed);
227
+ if (srcResult[0] === 'error') {
228
+ return [state, srcResult];
229
+ }
230
+ // now that source exists, reject if dst is strictly inside src's subtree (rename into own descendant)
231
+ // or if src is strictly inside dst's subtree (rename onto own ancestor)
232
+ if (isProperPrefix(srcParsed, dstParsed) || isProperPrefix(dstParsed, srcParsed)) {
233
+ return [state, error('cannot rename a directory into its own subtree or onto an ancestor')];
234
+ }
235
+ const [dstRoot, dstResult] = insertEntityAt(srcRoot, dstParsed, srcResult[1]);
236
+ if (dstResult[0] === 'error') {
237
+ return [state, dstResult];
238
+ }
239
+ return [{ ...state, root: dstRoot }, okVoid];
240
+ };
241
+ const readBytesOp = (path, offset, size) => readOperation((dir, p) => {
242
+ if (p.length !== 1) {
243
+ return enoent;
244
+ }
245
+ const file = dir[p[0]];
246
+ if (typeof file === 'function') {
247
+ throw new Error(`'${p[0]}' is a JsModule; readBytes not supported`);
248
+ }
249
+ if (file === undefined) {
250
+ return enoent;
251
+ }
252
+ if (!Array.isArray(file)) {
253
+ return error(`'${p[0]}' is not a file`);
254
+ }
255
+ if (!Number.isInteger(offset)) {
256
+ return error(`Offset ${offset} is not an integer`);
257
+ }
258
+ if (!Number.isInteger(size)) {
259
+ return error(`Chunk size ${size} is not an integer`);
260
+ }
261
+ if (offset < 0) {
262
+ return error(`Offset ${offset} is negative`);
263
+ }
264
+ if (size < 0) {
265
+ return error(`Chunk size ${size} is negative`);
266
+ }
267
+ if (BigInt(size) > maxLengthBytes) {
268
+ return error(`Chunk size ${size} exceeds maximum allowed size of ${maxLengthBytes} bytes`);
269
+ }
270
+ const chunks = file;
271
+ let toSkip = BigInt(offset) * 8n;
272
+ let toRead = BigInt(size) * 8n;
273
+ let result = empty;
274
+ for (const chunk of chunks) {
275
+ if (toRead === 0n) {
276
+ break;
277
+ }
278
+ const chunkBits = length(chunk);
279
+ if (toSkip >= chunkBits) {
280
+ toSkip -= chunkBits;
281
+ continue;
282
+ }
283
+ const skipInChunk = toSkip;
284
+ const availableBits = chunkBits - skipInChunk;
285
+ const takeBits = availableBits <= toRead ? availableBits : toRead;
286
+ const taken = vec(takeBits)(msb.front(takeBits)(msb.removeFront(skipInChunk)(chunk)));
287
+ result = msb.concat(result)(taken);
288
+ toSkip = 0n;
289
+ toRead -= takeBits;
290
+ }
291
+ return ok(result);
292
+ })(path);
145
293
  const map = {
146
294
  all: (...a) => state => {
147
295
  let e = [];
@@ -180,6 +328,9 @@ const map = {
180
328
  access,
181
329
  import: import_,
182
330
  rm,
331
+ rename,
332
+ readBytes: readBytesOp,
333
+ randomInt: () => state => [{ ...state, randomNext: state.randomNext + 1 }, state.randomNext],
183
334
  exec: todo,
184
335
  createServer: todo,
185
336
  listen: todo,
@@ -207,3 +358,27 @@ const map = {
207
358
  },
208
359
  };
209
360
  export const virtual = run(map);
361
+ const testContext = { test: todo };
362
+ /**
363
+ * Safe, inert defaults for every {@link NodeProgramOptions} field, intended for
364
+ * proof files that need to call a program without owning the full literal.
365
+ *
366
+ * Proofs spread-override only what their test cares about:
367
+ *
368
+ * ```ts
369
+ * const opts: NodeProgramOptions = { ...defaultNodeProgramOptions, args }
370
+ * ```
371
+ *
372
+ * Future additions to `NodeProgramOptions` only need a default added here,
373
+ * keeping unrelated proof files from churning.
374
+ */
375
+ export const defaultNodeProgramOptions = {
376
+ args: [],
377
+ env: {},
378
+ home: '.',
379
+ std: { stdout: { isTTY: false }, stderr: { isTTY: false } },
380
+ testContext,
381
+ bunTestContext: testContext,
382
+ playwrightTestContext: testContext,
383
+ engine: 'node',
384
+ };
@@ -14,4 +14,18 @@ export declare const proof: {
14
14
  throw: {
15
15
  readFileOnJsModule: () => void;
16
16
  };
17
+ renameSamePath: () => void;
18
+ renameIntoOwnSubtree: () => void;
19
+ renameOntoOwnAncestor: () => void;
20
+ renameNonEmptyDirOverEmptyDir: () => void;
21
+ renameEmptyDirOverNonEmptyDir: () => void;
22
+ renameFileOntoDirectory: () => void;
23
+ readFileTooLarge: () => void;
24
+ readBytesNegativeSize: () => void;
25
+ readBytesZeroSize: () => void;
26
+ readBytesNegativeOffset: () => void;
27
+ readBytesFractionalSize: () => void;
28
+ readBytesFractionalOffset: () => void;
29
+ readBytesAcrossChunkBoundary: () => void;
30
+ largeFileReadBytes: () => void;
17
31
  };