@nudojs/core 0.3.0 → 0.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.
Files changed (123) hide show
  1. package/dist/chunk-N7E2A7IK.js +7216 -0
  2. package/dist/exec.d.ts +2 -0
  3. package/dist/exec.js +174 -0
  4. package/dist/index-DgyudAkw.d.ts +911 -0
  5. package/dist/index.d.ts +64 -710
  6. package/dist/index.js +3269 -8426
  7. package/package.json +18 -5
  8. package/CHANGELOG.md +0 -34
  9. package/src/__tests__/environment.test.ts +0 -48
  10. package/src/__tests__/fn-sig-impl.test.ts +0 -57
  11. package/src/__tests__/ops-refined.test.ts +0 -196
  12. package/src/__tests__/ops.test.ts +0 -84
  13. package/src/__tests__/type-value-phase2.test.ts +0 -117
  14. package/src/__tests__/type-value-phase3.test.ts +0 -94
  15. package/src/__tests__/type-value-refined.test.ts +0 -210
  16. package/src/__tests__/type-value.test.ts +0 -319
  17. package/src/algebra/__tests__/abs-native.test.ts +0 -176
  18. package/src/algebra/__tests__/arithmetic.test.ts +0 -170
  19. package/src/algebra/__tests__/ast-eval.test.ts +0 -154
  20. package/src/algebra/__tests__/bridge.test.ts +0 -160
  21. package/src/algebra/__tests__/call-budget.test.ts +0 -117
  22. package/src/algebra/__tests__/check-case-consistency.test.ts +0 -69
  23. package/src/algebra/__tests__/check-gold.test.ts +0 -290
  24. package/src/algebra/__tests__/check-json.test.ts +0 -57
  25. package/src/algebra/__tests__/check-mini-repo.test.ts +0 -49
  26. package/src/algebra/__tests__/check-real-commander.test.ts +0 -43
  27. package/src/algebra/__tests__/check-real-packages.test.ts +0 -165
  28. package/src/algebra/__tests__/check-recall-gold.test.ts +0 -1024
  29. package/src/algebra/__tests__/check-require-dep-memo.test.ts +0 -45
  30. package/src/algebra/__tests__/check-return-contract.test.ts +0 -90
  31. package/src/algebra/__tests__/check-shape-gold.test.ts +0 -303
  32. package/src/algebra/__tests__/check-source-memo.test.ts +0 -204
  33. package/src/algebra/__tests__/check.test.ts +0 -69
  34. package/src/algebra/__tests__/denote.test.ts +0 -77
  35. package/src/algebra/__tests__/diagnostics.test.ts +0 -64
  36. package/src/algebra/__tests__/differential.test.ts +0 -124
  37. package/src/algebra/__tests__/div-mod.test.ts +0 -38
  38. package/src/algebra/__tests__/example0.test.ts +0 -103
  39. package/src/algebra/__tests__/exec-analyze-throws.test.ts +0 -60
  40. package/src/algebra/__tests__/exec-async.test.ts +0 -46
  41. package/src/algebra/__tests__/exec-bpath.test.ts +0 -193
  42. package/src/algebra/__tests__/exec-class.test.ts +0 -56
  43. package/src/algebra/__tests__/exec-e2e.test.ts +0 -97
  44. package/src/algebra/__tests__/exec-generator.test.ts +0 -53
  45. package/src/algebra/__tests__/exec-inherit-default.test.ts +0 -111
  46. package/src/algebra/__tests__/exec-optional-nested.test.ts +0 -87
  47. package/src/algebra/__tests__/exec-rest.test.ts +0 -55
  48. package/src/algebra/__tests__/exec-spread-forof.test.ts +0 -85
  49. package/src/algebra/__tests__/exec-static-computed.test.ts +0 -61
  50. package/src/algebra/__tests__/exec-switch.test.ts +0 -74
  51. package/src/algebra/__tests__/exec-trycatch.test.ts +0 -71
  52. package/src/algebra/__tests__/fn-fp-memo.test.ts +0 -166
  53. package/src/algebra/__tests__/fn-fp-multidecl.test.ts +0 -49
  54. package/src/algebra/__tests__/format.test.ts +0 -34
  55. package/src/algebra/__tests__/generalize-memo.test.ts +0 -285
  56. package/src/algebra/__tests__/generalize.test.ts +0 -113
  57. package/src/algebra/__tests__/hash-source.test.ts +0 -19
  58. package/src/algebra/__tests__/hof.test.ts +0 -80
  59. package/src/algebra/__tests__/inlay.test.ts +0 -30
  60. package/src/algebra/__tests__/language.test.ts +0 -77
  61. package/src/algebra/__tests__/leq.test.ts +0 -109
  62. package/src/algebra/__tests__/load-deps-fp-memo.test.ts +0 -132
  63. package/src/algebra/__tests__/methods.test.ts +0 -37
  64. package/src/algebra/__tests__/node-types.test.ts +0 -26
  65. package/src/algebra/__tests__/nudo-constraints.ts +0 -46
  66. package/src/algebra/__tests__/objects.test.ts +0 -117
  67. package/src/algebra/__tests__/parse-source-cache.test.ts +0 -42
  68. package/src/algebra/__tests__/refine-import.test.ts +0 -109
  69. package/src/algebra/__tests__/stable-source-key-block.test.ts +0 -23
  70. package/src/algebra/__tests__/stable-source-key.test.ts +0 -31
  71. package/src/algebra/__tests__/surface.test.ts +0 -41
  72. package/src/algebra/__tests__/this-inherit.test.ts +0 -130
  73. package/src/algebra/abs-fn.ts +0 -49
  74. package/src/algebra/abs-modules.ts +0 -139
  75. package/src/algebra/abs.ts +0 -206
  76. package/src/algebra/arithmetic.ts +0 -691
  77. package/src/algebra/ast-eval.ts +0 -1763
  78. package/src/algebra/bridge.ts +0 -350
  79. package/src/algebra/builtins.ts +0 -373
  80. package/src/algebra/check-report.ts +0 -150
  81. package/src/algebra/check.ts +0 -1867
  82. package/src/algebra/constraint.ts +0 -304
  83. package/src/algebra/denote.ts +0 -166
  84. package/src/algebra/diagnostics.ts +0 -210
  85. package/src/algebra/exec/call.ts +0 -29
  86. package/src/algebra/exec/calls.ts +0 -96
  87. package/src/algebra/exec/class-registry.ts +0 -29
  88. package/src/algebra/exec/class.ts +0 -329
  89. package/src/algebra/exec/index.ts +0 -6
  90. package/src/algebra/exec/member-diag.ts +0 -141
  91. package/src/algebra/exec/run.ts +0 -341
  92. package/src/algebra/exec/runtime.ts +0 -583
  93. package/src/algebra/exec/transpile.ts +0 -993
  94. package/src/algebra/fn-fp.ts +0 -294
  95. package/src/algebra/format.ts +0 -91
  96. package/src/algebra/generalize.ts +0 -776
  97. package/src/algebra/hash-source.ts +0 -27
  98. package/src/algebra/index.ts +0 -34
  99. package/src/algebra/inlay.ts +0 -198
  100. package/src/algebra/language.ts +0 -289
  101. package/src/algebra/leak.ts +0 -68
  102. package/src/algebra/leq.ts +0 -301
  103. package/src/algebra/load-deps-fp.ts +0 -97
  104. package/src/algebra/methods.ts +0 -128
  105. package/src/algebra/objects.ts +0 -251
  106. package/src/algebra/parse-source.ts +0 -64
  107. package/src/algebra/phi.ts +0 -42
  108. package/src/algebra/pred.ts +0 -395
  109. package/src/algebra/refine.ts +0 -227
  110. package/src/algebra/stable-source-key.ts +0 -81
  111. package/src/algebra/surface.ts +0 -206
  112. package/src/algebra/template.ts +0 -143
  113. package/src/algebra/term.ts +0 -119
  114. package/src/environment.ts +0 -74
  115. package/src/index.ts +0 -73
  116. package/src/mock-helpers.ts +0 -166
  117. package/src/ops.ts +0 -191
  118. package/src/refinements/range.ts +0 -86
  119. package/src/refinements/template-predicates.ts +0 -11
  120. package/src/refinements/template.ts +0 -176
  121. package/src/strip-types.ts +0 -236
  122. package/src/type-value.ts +0 -513
  123. package/tsconfig.json +0 -8
@@ -1,341 +0,0 @@
1
- /**
2
- * 进程内 B 路径执行:transpile 源码 → new Function 跑在 runtime 上。
3
- * 不写临时文件;相对 import 用注入的 AbsModuleExports / JS 导出绑定。
4
- *
5
- * mode:
6
- * - "exec"(默认):执行全部顶层(含副作用)
7
- * - "analyze":只保留函数声明与纯字面量 const;跳过顶层表达式/循环/if
8
- * ——分析入口安全,不触发 fetch 等顶层副作用
9
- */
10
-
11
- import * as runtime from "./runtime.ts";
12
- import * as classRt from "./class.ts";
13
- import * as callsRt from "./calls.ts";
14
- import type { Abs } from "../abs.ts";
15
- import { never, unknown } from "../abs.ts";
16
- import type { AbsModuleExports } from "../abs-modules.ts";
17
- import { transpile } from "./transpile.ts";
18
- import { $call } from "./call.ts";
19
- import { isNudoThrow } from "./runtime.ts";
20
-
21
- const rtAll = { ...runtime, ...classRt, ...callsRt } as Record<string, unknown>;
22
-
23
- export type RunTranspiledOptions = {
24
- /** 说明符 → 依赖导出(host 模块图或 runTranspiled 产物) */
25
- modules?: Record<string, AbsModuleExports | Record<string, unknown>>;
26
- maxLoopIters?: number;
27
- /** analyze = 跳过效应性顶层语句 */
28
- mode?: "exec" | "analyze";
29
- /** @nudo:replace 注入值:varName → Abs */
30
- replacements?: Record<string, Abs>;
31
- /** @nudo:replace 匹配表:传给 transpile */
32
- replacementTargets?: Array<{
33
- target: string;
34
- varName: string;
35
- stmtStart?: number;
36
- stmtEnd?: number;
37
- }>;
38
- /** @nudo:as 注入值:varName → Abs */
39
- asOverrides?: Record<string, Abs>;
40
- /** @nudo:as 语句范围表 */
41
- asOverrideTargets?: Array<{ varName: string; stmtStart: number; stmtEnd: number }>;
42
- /** @nudo:env 全局 Abs(JSON/Math/console…)→ 作用域绑定 */
43
- envGlobals?: Record<string, Abs>;
44
- };
45
-
46
- const RUNTIME_IMPORT_RE = /^import\s*\{[^}]+\}\s*from\s*"[^"]+";\s*$/m;
47
-
48
- /** 相对 import → 从注入 modules 取绑定(Abs fn 包成 JS 可调用) */
49
- function rewriteUserImports(js: string): string {
50
- // namespace:transpile 会写成 `import { * as ns } from "…"`
51
- js = js.replace(
52
- /^import\s*\{\s*\*\s+as\s+([A-Za-z_$][\w$]*)\s*\}\s*from\s*["']([^"']+)["'];\s*$/gm,
53
- (_all, local: string, spec: string) =>
54
- `const ${local} = __nudoBindNamespace(${JSON.stringify(spec)});`,
55
- );
56
- // 无绑定副作用 import → 仅触发模块求值
57
- js = js.replace(
58
- /^import\s*["']([^"']+)["'];\s*$/gm,
59
- (_all, spec: string) => `void __nudoBindNamespace(${JSON.stringify(spec)});`,
60
- );
61
- return js.replace(
62
- /^import\s*\{([^}]+)\}\s*from\s*["']([^"']+)["'];\s*$/gm,
63
- (_all, names: string, spec: string) => {
64
- const parts = names
65
- .split(",")
66
- .map((s) => s.trim())
67
- .filter(Boolean);
68
- return parts
69
- .map((p) => {
70
- const [imported, local] = p.split(/\s+as\s+/).map((x) => x.trim());
71
- const bind = local ?? imported;
72
- return `const ${bind} = __nudoBindImport(${JSON.stringify(spec)}, ${JSON.stringify(imported)});`;
73
- })
74
- .join("\n");
75
- },
76
- );
77
- }
78
-
79
- /** 分析模式:strip 顶层危险副作用与未知全局调用;保留本地函数调用(诊断依赖) */
80
- function stripEffectfulTopLevel(js: string): string {
81
- // 本文件内可解析的绑定名(函数/类/const/let/var/import)
82
- const declared = new Set<string>();
83
- for (const m of js.matchAll(/^(?:export\s+)?(?:async\s+)?function\s*\*?\s*([A-Za-z_$][\w$]*)/gm)) {
84
- declared.add(m[1]!);
85
- }
86
- for (const m of js.matchAll(/^(?:export\s+)?(?:const|let|var)\s+([A-Za-z_$][\w$]*)/gm)) {
87
- declared.add(m[1]!);
88
- }
89
- for (const m of js.matchAll(/^import\s*\{([^}]+)\}\s*from/gm)) {
90
- for (const part of m[1]!.split(",")) {
91
- const local = part.split(/\s+as\s+/).pop()?.trim();
92
- if (local) declared.add(local);
93
- }
94
- }
95
- for (const m of js.matchAll(/^import\s+\*\s+as\s+([A-Za-z_$][\w$]*)/gm)) {
96
- declared.add(m[1]!);
97
- }
98
- for (const m of js.matchAll(/^import\s+([A-Za-z_$][\w$]*)\s*,/gm)) {
99
- declared.add(m[1]!);
100
- }
101
-
102
- const lines = js.split("\n");
103
- const out: string[] = [];
104
- let i = 0;
105
- while (i < lines.length) {
106
- const line = lines[i]!;
107
- const isTop = /^\S/.test(line) && line.trim().length > 0;
108
- const t = line.trim();
109
- if (isTop) {
110
- // 顶层危险全局:fetch / 定时器 / console
111
- if (/^(console\.|fetch\s*\(|setTimeout\s*\(|setInterval\s*\()/.test(t)) {
112
- while (i < lines.length && !lines[i]!.trim().endsWith(";") && !lines[i]!.trim().endsWith("}")) i++;
113
- i++;
114
- continue;
115
- }
116
- // 顶层调用:$callNamed("localFn", …) 仅当 localFn 已声明时保留
117
- //(诊断需要执行本地顶层调用);未知全局 strip
118
- const namedCall = t.match(/^\$callNamed\(\s*"([^"]+)"/);
119
- if (namedCall && !declared.has(namedCall[1]!)) {
120
- while (i < lines.length && !lines[i]!.trim().endsWith(";") && !lines[i]!.trim().endsWith("}")) i++;
121
- i++;
122
- continue;
123
- }
124
- // 其它未知全局调用(未走 $callNamed 的)
125
- const callMatch = t.match(/^([A-Za-z_$][\w$]*)\s*\(/);
126
- if (
127
- callMatch &&
128
- !callMatch[1]!.startsWith("$") &&
129
- !declared.has(callMatch[1]!) &&
130
- !/^(const|let|var|function|export|import|return|if|for|while|switch|try|throw|class)\b/.test(t)
131
- ) {
132
- while (i < lines.length && !lines[i]!.trim().endsWith(";") && !lines[i]!.trim().endsWith("}")) i++;
133
- i++;
134
- continue;
135
- }
136
- // 顶层控制流
137
- if (/^(if\s*\(|for\s*\(|while\s*\(|\$fork\s*\(|\$for\s*\(|\$while)/.test(t)) {
138
- i++;
139
- let depth = t.includes("{") ? 1 : 0;
140
- while (i < lines.length && depth > 0) {
141
- const l = lines[i]!;
142
- depth += (l.match(/\{/g) || []).length;
143
- depth -= (l.match(/\}/g) || []).length;
144
- i++;
145
- }
146
- continue;
147
- }
148
- }
149
- out.push(line);
150
- i++;
151
- }
152
- return out.join("\n");
153
- }
154
-
155
- function runtimeArgNames(): string[] {
156
- return Object.keys(rtAll).filter((k) => k.startsWith("$"));
157
- }
158
-
159
- function bindImport(
160
- modules: RunTranspiledOptions["modules"],
161
- spec: string,
162
- name: string,
163
- ): unknown {
164
- const mod = modules?.[spec] as AbsModuleExports | undefined;
165
- if (!mod) return undefined;
166
- const absCallable = (v: Abs): unknown => {
167
- // fn Abs → JS 可调用;class/其它 Abs 原样(供 $new / $get)
168
- if (v && typeof v === "object" && "shape" in v) {
169
- if ((v as Abs).shape.k === "fn") return (...args: Abs[]) => $call(v, args);
170
- return v;
171
- }
172
- return v;
173
- };
174
- if (name === "default") {
175
- const d = (mod as AbsModuleExports).default;
176
- if (d === undefined) return undefined;
177
- if (typeof d === "function") return d;
178
- return absCallable(d as Abs);
179
- }
180
- const named = (mod as AbsModuleExports).named;
181
- const v = named?.[name];
182
- if (v === undefined) {
183
- const rec = (mod as Record<string, unknown>)[name];
184
- if (typeof rec === "function") return rec;
185
- return undefined;
186
- }
187
- if (typeof v === "function") return v;
188
- return absCallable(v as Abs);
189
- }
190
-
191
- /** `import * as ns`:整命名空间(named + default 槽)→ Abs 对象 */
192
- function bindNamespace(modules: RunTranspiledOptions["modules"], spec: string): Abs {
193
- const mod = modules?.[spec] as AbsModuleExports | undefined;
194
- if (!mod) return unknown;
195
- if (mod.named) {
196
- const { $obj } = rtAll as { $obj: (s: Record<string, Abs>) => Abs };
197
- const slots: Record<string, Abs> = {};
198
- for (const [k, v] of Object.entries(mod.named)) slots[k] = v;
199
- if (mod.default) slots["default"] = mod.default;
200
- return $obj(slots);
201
- }
202
- return unknown;
203
- }
204
-
205
- /** CJS require:modules[spec] → 可 $get 的 namespace Abs */
206
- function requireFromModules(
207
- modules: RunTranspiledOptions["modules"],
208
- spec: string,
209
- ): unknown {
210
- const mod = modules?.[spec] as AbsModuleExports | undefined;
211
- if (!mod) return unknown;
212
- if (mod.named) {
213
- const { $obj } = rtAll as { $obj: (s: Record<string, Abs>) => Abs };
214
- const slots: Record<string, Abs> = {};
215
- for (const [k, v] of Object.entries(mod.named)) slots[k] = v;
216
- if (mod.default) slots["default"] = mod.default;
217
- return $obj(slots);
218
- }
219
- return mod;
220
- }
221
-
222
- /**
223
- * 执行一段 B 路径程序,返回顶层 `export function` / `export const`。
224
- */
225
- export function runTranspiled(
226
- source: string,
227
- opts: RunTranspiledOptions = {},
228
- ): Record<string, unknown> {
229
- const modules = opts.modules ?? {};
230
- let js = transpile(source, {
231
- runtimeImport: "@nudojs/core/exec",
232
- maxLoopIters: opts.maxLoopIters,
233
- source,
234
- replacements: opts.replacementTargets,
235
- asOverrides: opts.asOverrideTargets,
236
- });
237
- js = js.replace(RUNTIME_IMPORT_RE, "");
238
- js = rewriteUserImports(js);
239
- if (opts.mode === "analyze") {
240
- js = stripEffectfulTopLevel(js);
241
- }
242
-
243
- // @nudo:replace / @nudo:as 绑定
244
- const reps = opts.replacements ?? {};
245
- const asVals = opts.asOverrides ?? {};
246
- const allInject = { ...reps, ...asVals };
247
- const injectNames = Object.keys(allInject);
248
- if (injectNames.length > 0) {
249
- const binds = injectNames
250
- .map((n) => `const ${n} = __nudoReplaces[${JSON.stringify(n)}];`)
251
- .join("\n");
252
- js = `${binds}\n${js}`;
253
- }
254
-
255
- const exportFns = [...js.matchAll(/^export function (\w+)/gm)].map((m) => m[1]!);
256
- js = js.replace(/^export function /gm, "function ");
257
- const exportConsts = [...js.matchAll(/^export const (\w+)/gm)].map((m) => m[1]!);
258
- js = js.replace(/^export const /gm, "const ");
259
-
260
- const names = [...new Set([...exportFns, ...exportConsts])];
261
- const argNames = [
262
- ...runtimeArgNames(),
263
- "__nudoModules",
264
- "__nudoBindImport",
265
- "__nudoBindNamespace",
266
- "__nudoReplaces",
267
- "__nudoRequire",
268
- "__nudoEnv",
269
- ];
270
- const args = argNames.map((n) => {
271
- if (n === "__nudoModules") return modules;
272
- if (n === "__nudoBindImport") {
273
- return (spec: string, name: string) => bindImport(modules, spec, name);
274
- }
275
- if (n === "__nudoBindNamespace") {
276
- return (spec: string) => bindNamespace(modules, spec);
277
- }
278
- if (n === "__nudoReplaces") return allInject;
279
- if (n === "__nudoRequire") {
280
- return (spec: string) => requireFromModules(modules, spec);
281
- }
282
- if (n === "__nudoEnv") return opts.envGlobals ?? {};
283
- return rtAll[n];
284
- });
285
-
286
- // @nudo:env 全局绑定
287
- if (opts.envGlobals && Object.keys(opts.envGlobals).length > 0) {
288
- const envBinds = Object.keys(opts.envGlobals)
289
- .map((k) => `const ${k} = __nudoEnv[${JSON.stringify(k)}];`)
290
- .join("\n");
291
- js = `${envBinds}\n${js}`;
292
- }
293
-
294
- const ret = names.length > 0 ? `return { ${names.join(", ")} };` : "return {};";
295
- const fn = new Function(...argNames, `${js}\n${ret}`);
296
- return fn(...args) as Record<string, unknown>;
297
- }
298
-
299
- export type TranspiledCallResult = {
300
- result: Abs;
301
- /** 非 never 表示函数可能 throw 该类型 */
302
- throws: Abs;
303
- };
304
-
305
- function isAbsVal(v: unknown): v is Abs {
306
- return !!v && typeof v === "object" && "shape" in (v as object) && "conf" in (v as object);
307
- }
308
-
309
- /** 调用 runTranspiled 导出(捕获 $throw) */
310
- export function callTranspiledExportFull(
311
- exports: Record<string, unknown>,
312
- name: string,
313
- args: Abs[],
314
- ): TranspiledCallResult {
315
- const fn = exports[name];
316
- if (typeof fn === "function") {
317
- try {
318
- const r = (fn as (...a: Abs[]) => unknown)(...args);
319
- if (!isAbsVal(r)) return { result: unknown, throws: never };
320
- return { result: r, throws: never };
321
- } catch (e) {
322
- if (isNudoThrow(e)) {
323
- return { result: never, throws: e.absValue };
324
- }
325
- return { result: unknown, throws: never };
326
- }
327
- }
328
- if (isAbsVal(fn)) {
329
- return { result: fn, throws: never };
330
- }
331
- return { result: unknown, throws: never };
332
- }
333
-
334
- /** 调用 runTranspiled 导出(仅结果) */
335
- export function callTranspiledExport(
336
- exports: Record<string, unknown>,
337
- name: string,
338
- args: Abs[],
339
- ): Abs {
340
- return callTranspiledExportFull(exports, name, args).result;
341
- }