@nudojs/core 2.1.0 → 3.0.0-beta.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.
@@ -0,0 +1,583 @@
1
+ import {
2
+ canSkipLiteralCallScan,
3
+ checkInjectedDomainEvidence,
4
+ extractAllLoadSpecs,
5
+ fnFingerprints,
6
+ generalizeFromAst,
7
+ generalizeSourceKeyPart,
8
+ getFnNameAndBodies,
9
+ hashSource,
10
+ interfaceTierOf,
11
+ listTopFunctions,
12
+ loadModuleDepsFingerprint,
13
+ normPath,
14
+ resetFnFpCache,
15
+ resetHashSourceCache,
16
+ resolveDepPath,
17
+ sidecarSpecsOf,
18
+ stableAnalyzeKeySource
19
+ } from "./chunk-TVLM42WD.js";
20
+ import {
21
+ $tryDigestSoft,
22
+ $tryMarkSoft,
23
+ $tryReleaseSoft,
24
+ ERROR_FAMILY,
25
+ FORK_TRUNCATION_LABEL,
26
+ MAX_B_TOTAL_FORKS,
27
+ MAX_CALL_DEPTH,
28
+ MAX_TOTAL_CALLS,
29
+ OBJECT_PROTO_NAMES,
30
+ abortDerivationSession,
31
+ absTemplateViews,
32
+ allFixedTextOfViews,
33
+ anyMemberResult,
34
+ awaitAbs,
35
+ beginDerivationSession,
36
+ bumpBForkBudget,
37
+ callBudgetKey,
38
+ classChainNames,
39
+ classFromMethods,
40
+ coerceAsyncReturn,
41
+ concatString,
42
+ createTemplateAbs,
43
+ decideEndsWith,
44
+ decideIncludes,
45
+ decideStartsWith,
46
+ defaultLeakBudget,
47
+ defineClass,
48
+ definitelyUncallableMember,
49
+ derivationChain,
50
+ endDerivationSession,
51
+ enterCall,
52
+ errorTypeAbs,
53
+ exceedsBudget,
54
+ exitCall,
55
+ filterDeclaredThrows,
56
+ filterGateThrows,
57
+ filterIgnoredThrows,
58
+ fixedLengthOfViews,
59
+ flushMayThrowEffects,
60
+ formatShape,
61
+ formatTemplateNameViews,
62
+ formatThrowsAbs,
63
+ getAbsCallBudgetStats,
64
+ getAbsOrigin,
65
+ getBForkBudgetLimit,
66
+ getBForkCount,
67
+ getClass,
68
+ getClassChain,
69
+ getDerivation,
70
+ getMayThrowCollector,
71
+ hasDerivationSession,
72
+ instanceOf,
73
+ instantiateClass,
74
+ isEvalMissingSlotEnabled,
75
+ isNullishAbs,
76
+ isTemplateLike,
77
+ isThrowsIgnored,
78
+ knownPrefixOfViews,
79
+ knownSuffixOfViews,
80
+ leakIfNeeded,
81
+ litValue,
82
+ lookupMethod,
83
+ lookupMethodWithOwner,
84
+ lookupSuperMethod,
85
+ mayThrowEffectsToAbs,
86
+ maybeLeak,
87
+ mergeAdjacentFixedViews,
88
+ noteAbsTruncation,
89
+ noteAnyMemberMayThrow,
90
+ noteBForkTruncation,
91
+ noteDerivationAdd,
92
+ noteDerivationJoin,
93
+ noteMemberDispatchMiss,
94
+ noteNullishMemberThrows,
95
+ noteObjSlotMissing,
96
+ notePrimMemberMissing,
97
+ noteUnknownMemberMissing,
98
+ orphanMayThrowEffects,
99
+ parseSource,
100
+ popCallLoc,
101
+ popMayThrowFrame,
102
+ predToString,
103
+ projectBrand,
104
+ projectDerivationDsl,
105
+ pushCallLoc,
106
+ pushMayThrowFrame,
107
+ recordMayThrow,
108
+ recordMemberDiag,
109
+ resetAbsCallBudget,
110
+ resetBForkBudget,
111
+ resetLeakCounter,
112
+ runWithEvalMissingSlot,
113
+ runWithMayThrowSession,
114
+ setAbsTruncationCollector,
115
+ setBForkBudgetLimit,
116
+ setDerivation,
117
+ setDerivationCollector,
118
+ setEvalMissingSlotEnabled,
119
+ setMayThrowCollector,
120
+ setMemberDiagCollector,
121
+ stableCallId,
122
+ superNameOf,
123
+ tagAbsOrigin,
124
+ tagDerivationRoot,
125
+ templateMatchesValue,
126
+ templatePartsOf,
127
+ termDepth,
128
+ termKey,
129
+ termNodes,
130
+ termToString,
131
+ throwAbsToKinds,
132
+ throwsKindCovered,
133
+ truncatedAbs,
134
+ viewTemplateParts,
135
+ wrapPromise
136
+ } from "./chunk-LDBNOXEM.js";
137
+
138
+ // src/algebra/inlay.ts
139
+ function listFunctions(source) {
140
+ const file = parseSource(source);
141
+ const out = [];
142
+ const visitDecl = (decl) => {
143
+ if (decl.type === "FunctionDeclaration" && decl.id) {
144
+ const fd = decl;
145
+ out.push({ name: fd.id.name, node: fd });
146
+ }
147
+ if (decl.type === "VariableDeclaration") {
148
+ const dcls = decl.declarations ?? [];
149
+ for (const d of dcls) {
150
+ const id = d.id;
151
+ const init = d.init;
152
+ if (id?.type === "Identifier" && id.name && init && (init.type === "ArrowFunctionExpression" || init.type === "FunctionExpression")) {
153
+ out.push({ name: id.name, node: init });
154
+ }
155
+ }
156
+ }
157
+ };
158
+ for (const stmt of file.program.body) {
159
+ if (stmt.type === "ExportNamedDeclaration" && stmt.declaration) {
160
+ visitDecl(stmt.declaration);
161
+ } else if (stmt.type === "ExportDefaultDeclaration" && stmt.declaration) {
162
+ visitDecl(stmt.declaration);
163
+ } else {
164
+ visitDecl(stmt);
165
+ }
166
+ }
167
+ return out;
168
+ }
169
+ function renameTypeVars(s, varMap) {
170
+ let out = s;
171
+ const ids = [...varMap.keys()].sort((a, b) => b.length - a.length);
172
+ for (const id of ids) {
173
+ out = out.replaceAll(id, varMap.get(id));
174
+ }
175
+ return out;
176
+ }
177
+ function formatReturnDisplay(g) {
178
+ const varMap = /* @__PURE__ */ new Map();
179
+ for (let i = 0; i < (g.typeParams?.length ?? 0); i++) {
180
+ const id = g.typeParams[i].id;
181
+ const pname = g.params[i];
182
+ if (pname) varMap.set(id, pname);
183
+ }
184
+ const stripParens = (t) => t.startsWith("(") && t.endsWith(")") ? t.slice(1, -1) : t;
185
+ const memberPath = (m) => {
186
+ if (m.term?.op === "var") {
187
+ const name = renameTypeVars(m.term.id, varMap);
188
+ return { key: `var:${name}`, text: name };
189
+ }
190
+ if (m.term?.op === "app") {
191
+ const t = stripParens(renameTypeVars(termToString(m.term), varMap));
192
+ return { key: `app:${t}`, text: t };
193
+ }
194
+ const lv = litValue(m);
195
+ if (typeof lv === "number" && !Number.isFinite(lv)) {
196
+ return { key: `lit:${String(lv)}`, text: String(lv) };
197
+ }
198
+ if (lv !== void 0) {
199
+ return { key: `lit:${String(lv)}`, text: JSON.stringify(lv) };
200
+ }
201
+ return { key: `shape:${formatShape(m)}`, text: formatShape(m) };
202
+ };
203
+ const abs = g.symbolic;
204
+ if (abs.term && abs.term.op !== "lit") {
205
+ return stripParens(renameTypeVars(termToString(abs.term), varMap));
206
+ }
207
+ if (abs.shape.k === "sum") {
208
+ const parts = [];
209
+ const seen = /* @__PURE__ */ new Set();
210
+ for (const m of abs.shape.members) {
211
+ const { key, text } = memberPath(m);
212
+ if (seen.has(key)) continue;
213
+ seen.add(key);
214
+ parts.push(text);
215
+ }
216
+ return parts.join(" | ");
217
+ }
218
+ return memberPath(abs).text;
219
+ }
220
+ function collectAbsInlays(source, opts) {
221
+ const inlays = [];
222
+ const refineOpts = opts?.loadModule || opts?.fromFile ? {
223
+ ...opts.loadModule ? { loadModule: opts.loadModule } : {},
224
+ ...opts.fromFile ? { fromFile: opts.fromFile } : {}
225
+ } : void 0;
226
+ for (const { name, node } of listFunctions(source)) {
227
+ let g;
228
+ try {
229
+ g = generalizeFromAst(name, source, refineOpts ? { refine: refineOpts } : {});
230
+ } catch {
231
+ continue;
232
+ }
233
+ if (!g) continue;
234
+ const tier = opts?.fromFile !== void 0 ? interfaceTierOf(source, name, opts.fromFile, {
235
+ ...opts.loadModule ? { loadModule: opts.loadModule } : {},
236
+ ...opts.autoBind !== void 0 ? { autoBind: opts.autoBind } : {}
237
+ }) : void 0;
238
+ const tierSrc = tier?.source;
239
+ const isImplicitExport = tierSrc === "implicit";
240
+ const params = g.params;
241
+ const predsByName = /* @__PURE__ */ new Map();
242
+ if (g.entryReqs) {
243
+ for (const r of g.entryReqs) {
244
+ predsByName.set(r.param, [r.pred]);
245
+ }
246
+ }
247
+ const fnNode = node;
248
+ const paramList = fnNode.params ?? [];
249
+ for (let i = 0; i < paramList.length; i++) {
250
+ const p = paramList[i];
251
+ const pname = params[i] ?? p?.name;
252
+ if (!p?.loc || !pname) continue;
253
+ const preds = predsByName.get(pname);
254
+ if (preds && preds.length > 0) {
255
+ const text = preds.map(predToString).join(" \u2227 ");
256
+ inlays.push({
257
+ line: p.loc.end.line,
258
+ character: p.loc.end.column,
259
+ label: ` where ${text}`,
260
+ kind: "parameter",
261
+ ...tierSrc ? { interfaceSource: tierSrc } : {}
262
+ });
263
+ }
264
+ }
265
+ const bodyLoc = fnNode.body?.loc;
266
+ if (bodyLoc?.start) {
267
+ let label;
268
+ try {
269
+ label = `: ${formatReturnDisplay(g)}`;
270
+ } catch {
271
+ label = `: ${formatShape(g.symbolic)}`;
272
+ }
273
+ if (isImplicitExport) label += " \xB7 derived";
274
+ inlays.push({
275
+ line: bodyLoc.start.line,
276
+ character: Math.max(0, bodyLoc.start.column),
277
+ label: `${label} `,
278
+ kind: "type",
279
+ ...tierSrc ? { interfaceSource: tierSrc } : {},
280
+ ...isImplicitExport ? { derived: true } : {}
281
+ });
282
+ }
283
+ }
284
+ return inlays;
285
+ }
286
+
287
+ // src/algebra/denote.ts
288
+ function denoteGuard(a, v) {
289
+ const lv = litValue(a);
290
+ if (lv !== void 0 && (!a.pred || a.pred.op === "true")) {
291
+ return eqGuard(v, lv);
292
+ }
293
+ const shape = denoteShape(a.shape, v);
294
+ const pred = denotePred(a, v);
295
+ if (shape === "true") return pred;
296
+ if (pred === "true") return shape;
297
+ return `(${shape}) && (${pred})`;
298
+ }
299
+ function denoteShape(s, v) {
300
+ switch (s.k) {
301
+ case "never":
302
+ return "false";
303
+ case "unknown":
304
+ case "any":
305
+ return "true";
306
+ case "prim":
307
+ switch (s.type) {
308
+ case "number":
309
+ return `typeof ${v} === "number"`;
310
+ case "string":
311
+ return `typeof ${v} === "string"`;
312
+ case "boolean":
313
+ return `typeof ${v} === "boolean"`;
314
+ case "bigint":
315
+ return `typeof ${v} === "bigint"`;
316
+ case "symbol":
317
+ return `typeof ${v} === "symbol"`;
318
+ default:
319
+ return "true";
320
+ }
321
+ case "obj": {
322
+ const checks = [`typeof ${v} === "object"`, `${v} !== null`];
323
+ for (const [key, slot] of Object.entries(s.slots)) {
324
+ const access = `${v}.${key}`;
325
+ const inner = denoteGuard(slot.value, access);
326
+ if (slot.optional) {
327
+ if (inner !== "true") checks.push(`(${access} === undefined || ${inner})`);
328
+ } else {
329
+ if (inner !== "true") checks.push(inner);
330
+ }
331
+ }
332
+ return checks.join(" && ");
333
+ }
334
+ case "arr":
335
+ return `Array.isArray(${v}) && ${v}.every((item) => ${denoteGuard(s.element, "item")})`;
336
+ case "tuple": {
337
+ const checks = [`Array.isArray(${v})`];
338
+ const minLen = s.elements.length;
339
+ checks.push(s.rest ? `${v}.length >= ${minLen}` : `${v}.length === ${minLen}`);
340
+ s.elements.forEach((el, i) => {
341
+ const inner = denoteGuard(el, `${v}[${i}]`);
342
+ if (inner !== "true") checks.push(inner);
343
+ });
344
+ if (s.rest) {
345
+ const rest = denoteGuard(s.rest, "item");
346
+ if (rest !== "true") {
347
+ checks.push(`${v}.slice(${minLen}).every((item) => ${rest})`);
348
+ }
349
+ }
350
+ return checks.join(" && ");
351
+ }
352
+ case "fn":
353
+ return `typeof ${v} === "function"`;
354
+ case "eff":
355
+ if (s.eff === "promise") return `${v} instanceof Promise`;
356
+ return "true";
357
+ case "brand":
358
+ return denoteShape(s.shape.shape, v);
359
+ case "sum": {
360
+ if (s.members.length === 0) return "false";
361
+ const parts = s.members.map((m) => denoteGuard(m, v));
362
+ return `(${parts.join(" || ")})`;
363
+ }
364
+ }
365
+ }
366
+ function denotePred(a, v) {
367
+ const p = a.pred;
368
+ if (!p || p.op === "true") {
369
+ const lv = litValue(a);
370
+ if (lv !== void 0) return eqGuard(v, lv);
371
+ return "true";
372
+ }
373
+ return predAsJs(p, v, a);
374
+ }
375
+ function predAsJs(p, v, a) {
376
+ switch (p.op) {
377
+ case "true":
378
+ return "true";
379
+ case "false":
380
+ return "false";
381
+ case "and":
382
+ return p.args.map((x) => predAsJs(x, v, a)).join(" && ");
383
+ case "or":
384
+ return `(${p.args.map((x) => predAsJs(x, v, a)).join(" || ")})`;
385
+ case "not": {
386
+ const inner = predAsJs(p.arg, v, a);
387
+ return inner === "true" ? "false" : `!(${inner})`;
388
+ }
389
+ case "gt":
390
+ case "ge":
391
+ case "lt":
392
+ case "le":
393
+ case "eq":
394
+ case "ne": {
395
+ const op = { gt: ">", ge: ">=", lt: "<", le: "<=", eq: "===", ne: "!==" }[p.op];
396
+ const leftIsValue = termIsValue(p.a, a);
397
+ const rightLit = litOfTerm(p.b);
398
+ if (leftIsValue && rightLit !== void 0) {
399
+ return cmpOp(v, op, rightLit);
400
+ }
401
+ const leftLit = litOfTerm(p.a);
402
+ const rightIsValue = termIsValue(p.b, a);
403
+ if (rightIsValue && leftLit !== void 0) {
404
+ return cmpLitValue(leftLit, op, v);
405
+ }
406
+ if (leftLit !== void 0 && rightLit !== void 0) {
407
+ return `${jsLit(leftLit)} ${op} ${jsLit(rightLit)}`;
408
+ }
409
+ return "true";
410
+ }
411
+ default:
412
+ return "true";
413
+ }
414
+ }
415
+ function litOfTerm(t) {
416
+ return t.op === "lit" ? t.value : void 0;
417
+ }
418
+ function jsLit(v) {
419
+ if (v === void 0) return "undefined";
420
+ if (v === null) return "null";
421
+ if (typeof v === "number") {
422
+ if (Number.isNaN(v)) return "NaN";
423
+ if (v === Infinity) return "Infinity";
424
+ if (v === -Infinity) return "-Infinity";
425
+ return String(v);
426
+ }
427
+ return JSON.stringify(v);
428
+ }
429
+ function eqGuard(v, lv) {
430
+ if (typeof lv === "number" && Number.isNaN(lv)) return `Number.isNaN(${v})`;
431
+ return `${v} === ${jsLit(lv)}`;
432
+ }
433
+ function cmpOp(v, op, lit) {
434
+ if (typeof lit === "number" && Number.isNaN(lit)) {
435
+ if (op === "===") return `Number.isNaN(${v})`;
436
+ if (op === "!==") return `!Number.isNaN(${v})`;
437
+ }
438
+ return `${v} ${op} ${jsLit(lit)}`;
439
+ }
440
+ function cmpLitValue(lit, op, v) {
441
+ if (typeof lit === "number" && Number.isNaN(lit)) {
442
+ if (op === "===") return `Number.isNaN(${v})`;
443
+ if (op === "!==") return `!Number.isNaN(${v})`;
444
+ }
445
+ return `${jsLit(lit)} ${op} ${v}`;
446
+ }
447
+ function termIsValue(t, a) {
448
+ if (t.op === "var") return true;
449
+ if (t.op === "lit") {
450
+ const lv = litValue(a);
451
+ return lv !== void 0 && Object.is(lv, t.value);
452
+ }
453
+ return false;
454
+ }
455
+ export {
456
+ $tryDigestSoft,
457
+ $tryMarkSoft,
458
+ $tryReleaseSoft,
459
+ ERROR_FAMILY,
460
+ FORK_TRUNCATION_LABEL,
461
+ MAX_B_TOTAL_FORKS,
462
+ MAX_CALL_DEPTH,
463
+ MAX_TOTAL_CALLS,
464
+ OBJECT_PROTO_NAMES,
465
+ abortDerivationSession,
466
+ absTemplateViews,
467
+ allFixedTextOfViews,
468
+ anyMemberResult,
469
+ awaitAbs,
470
+ beginDerivationSession,
471
+ bumpBForkBudget,
472
+ callBudgetKey,
473
+ canSkipLiteralCallScan,
474
+ checkInjectedDomainEvidence,
475
+ classChainNames,
476
+ classFromMethods,
477
+ coerceAsyncReturn,
478
+ collectAbsInlays,
479
+ concatString,
480
+ createTemplateAbs,
481
+ decideEndsWith,
482
+ decideIncludes,
483
+ decideStartsWith,
484
+ defaultLeakBudget,
485
+ defineClass,
486
+ definitelyUncallableMember,
487
+ denoteGuard,
488
+ derivationChain,
489
+ endDerivationSession,
490
+ enterCall,
491
+ errorTypeAbs,
492
+ exceedsBudget,
493
+ exitCall,
494
+ extractAllLoadSpecs,
495
+ filterDeclaredThrows,
496
+ filterGateThrows,
497
+ filterIgnoredThrows,
498
+ fixedLengthOfViews,
499
+ flushMayThrowEffects,
500
+ fnFingerprints,
501
+ formatTemplateNameViews,
502
+ formatThrowsAbs,
503
+ generalizeSourceKeyPart,
504
+ getAbsCallBudgetStats,
505
+ getAbsOrigin,
506
+ getBForkBudgetLimit,
507
+ getBForkCount,
508
+ getClass,
509
+ getClassChain,
510
+ getDerivation,
511
+ getFnNameAndBodies,
512
+ getMayThrowCollector,
513
+ hasDerivationSession,
514
+ hashSource,
515
+ instanceOf,
516
+ instantiateClass,
517
+ isEvalMissingSlotEnabled,
518
+ isNullishAbs,
519
+ isTemplateLike,
520
+ isThrowsIgnored,
521
+ knownPrefixOfViews,
522
+ knownSuffixOfViews,
523
+ leakIfNeeded,
524
+ listTopFunctions,
525
+ loadModuleDepsFingerprint,
526
+ lookupMethod,
527
+ lookupMethodWithOwner,
528
+ lookupSuperMethod,
529
+ mayThrowEffectsToAbs,
530
+ maybeLeak,
531
+ mergeAdjacentFixedViews,
532
+ normPath,
533
+ noteAbsTruncation,
534
+ noteAnyMemberMayThrow,
535
+ noteBForkTruncation,
536
+ noteDerivationAdd,
537
+ noteDerivationJoin,
538
+ noteMemberDispatchMiss,
539
+ noteNullishMemberThrows,
540
+ noteObjSlotMissing,
541
+ notePrimMemberMissing,
542
+ noteUnknownMemberMissing,
543
+ orphanMayThrowEffects,
544
+ popCallLoc,
545
+ popMayThrowFrame,
546
+ projectBrand,
547
+ projectDerivationDsl,
548
+ pushCallLoc,
549
+ pushMayThrowFrame,
550
+ recordMayThrow,
551
+ recordMemberDiag,
552
+ resetAbsCallBudget,
553
+ resetBForkBudget,
554
+ resetFnFpCache,
555
+ resetHashSourceCache,
556
+ resetLeakCounter,
557
+ resolveDepPath,
558
+ runWithEvalMissingSlot,
559
+ runWithMayThrowSession,
560
+ setAbsTruncationCollector,
561
+ setBForkBudgetLimit,
562
+ setDerivation,
563
+ setDerivationCollector,
564
+ setEvalMissingSlotEnabled,
565
+ setMayThrowCollector,
566
+ setMemberDiagCollector,
567
+ sidecarSpecsOf,
568
+ stableAnalyzeKeySource,
569
+ stableCallId,
570
+ superNameOf,
571
+ tagAbsOrigin,
572
+ tagDerivationRoot,
573
+ templateMatchesValue,
574
+ templatePartsOf,
575
+ termDepth,
576
+ termKey,
577
+ termNodes,
578
+ throwAbsToKinds,
579
+ throwsKindCovered,
580
+ truncatedAbs,
581
+ viewTemplateParts,
582
+ wrapPromise
583
+ };
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@nudojs/core",
3
- "version": "2.1.0",
3
+ "version": "3.0.0-beta.0",
4
4
  "engines": {
5
5
  "node": ">=20"
6
6
  },
7
- "description": "Nudo type system: type-as-computation (Abs) with TypeValue evaluation IR",
7
+ "description": "Nudo type system: type-as-computation (Abs = shape × term × pred × conf)",
8
8
  "type": "module",
9
9
  "license": "MIT",
10
10
  "keywords": [
@@ -24,6 +24,10 @@
24
24
  "./exec": {
25
25
  "types": "./dist/exec.d.ts",
26
26
  "default": "./dist/exec.js"
27
+ },
28
+ "./internal": {
29
+ "types": "./dist/internal.d.ts",
30
+ "default": "./dist/internal.js"
27
31
  }
28
32
  },
29
33
  "files": [
@@ -42,19 +46,19 @@
42
46
  "registry": "https://registry.npmjs.org"
43
47
  },
44
48
  "dependencies": {
45
- "@babel/parser": "^7.29.0",
46
- "@babel/types": "^7.29.0"
49
+ "@babel/parser": "^8.0.6",
50
+ "@babel/types": "^8.0.6"
47
51
  },
48
52
  "devDependencies": {
49
- "commander": "^13.0.0",
53
+ "commander": "^15.0.0",
50
54
  "debug": "^4.4.3",
51
55
  "escape-string-regexp": "^5.0.0",
52
56
  "eventemitter3": "^5.0.4",
53
57
  "is-plain-obj": "^4.1.0",
54
58
  "kleur": "^4.1.5",
55
- "lodash": "^4.17.21",
59
+ "lodash": "^4.18.1",
56
60
  "ms": "^2.1.3",
57
- "p-limit": "^7.3.2",
61
+ "p-limit": "^7.3.3",
58
62
  "yocto-queue": "^1.2.2"
59
63
  },
60
64
  "scripts": {