@loopdive/js2 0.57.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 (233) hide show
  1. package/CHANGELOG.md +1425 -0
  2. package/LICENSE +189 -0
  3. package/README.md +451 -0
  4. package/dist/checker/index.d.ts +117 -0
  5. package/dist/checker/language-service.d.ts +39 -0
  6. package/dist/checker/node-capability-map.d.ts +63 -0
  7. package/dist/checker/type-mapper.d.ts +84 -0
  8. package/dist/cjs-rewrite.d.ts +19 -0
  9. package/dist/cli.d.ts +2 -0
  10. package/dist/cli.js +363 -0
  11. package/dist/codegen/accessor-driver.d.ts +97 -0
  12. package/dist/codegen/any-helpers.d.ts +72 -0
  13. package/dist/codegen/array-element-typing.d.ts +46 -0
  14. package/dist/codegen/array-holes.d.ts +69 -0
  15. package/dist/codegen/array-methods.d.ts +68 -0
  16. package/dist/codegen/array-object-proto.d.ts +64 -0
  17. package/dist/codegen/array-reduce-fusion.d.ts +31 -0
  18. package/dist/codegen/array-to-primitive.d.ts +28 -0
  19. package/dist/codegen/async-cps.d.ts +239 -0
  20. package/dist/codegen/async-scheduler.d.ts +349 -0
  21. package/dist/codegen/binary-ops.d.ts +78 -0
  22. package/dist/codegen/binding-info.d.ts +31 -0
  23. package/dist/codegen/builtin-scaffold.d.ts +98 -0
  24. package/dist/codegen/builtin-static-globals.d.ts +8 -0
  25. package/dist/codegen/builtin-tags.d.ts +189 -0
  26. package/dist/codegen/case-convert-native.d.ts +12 -0
  27. package/dist/codegen/case-tables.d.ts +4 -0
  28. package/dist/codegen/class-bodies.d.ts +41 -0
  29. package/dist/codegen/class-member-keys.d.ts +33 -0
  30. package/dist/codegen/class-to-primitive.d.ts +39 -0
  31. package/dist/codegen/closed-method-dispatch.d.ts +42 -0
  32. package/dist/codegen/closures.d.ts +285 -0
  33. package/dist/codegen/coercion-engine.d.ts +154 -0
  34. package/dist/codegen/coercion-plan.d.ts +29 -0
  35. package/dist/codegen/context/bodies.d.ts +4 -0
  36. package/dist/codegen/context/create-context.d.ts +4 -0
  37. package/dist/codegen/context/errors.d.ts +39 -0
  38. package/dist/codegen/context/locals.d.ts +69 -0
  39. package/dist/codegen/context/source-pos.d.ts +5 -0
  40. package/dist/codegen/context/speculative.d.ts +95 -0
  41. package/dist/codegen/context/types.d.ts +1936 -0
  42. package/dist/codegen/custom-iterable.d.ts +34 -0
  43. package/dist/codegen/dataview-native.d.ts +51 -0
  44. package/dist/codegen/date-parse-native.d.ts +13 -0
  45. package/dist/codegen/dead-elimination.d.ts +26 -0
  46. package/dist/codegen/declarations.d.ts +147 -0
  47. package/dist/codegen/deno-api.d.ts +11 -0
  48. package/dist/codegen/destructuring-params.d.ts +102 -0
  49. package/dist/codegen/dyn-read.d.ts +26 -0
  50. package/dist/codegen/eval-tiering.d.ts +19 -0
  51. package/dist/codegen/expressions/assignment.d.ts +61 -0
  52. package/dist/codegen/expressions/builtins.d.ts +26 -0
  53. package/dist/codegen/expressions/calls-closures.d.ts +54 -0
  54. package/dist/codegen/expressions/calls-guards.d.ts +49 -0
  55. package/dist/codegen/expressions/calls-optional.d.ts +4 -0
  56. package/dist/codegen/expressions/calls.d.ts +83 -0
  57. package/dist/codegen/expressions/eval-inline.d.ts +24 -0
  58. package/dist/codegen/expressions/extern.d.ts +67 -0
  59. package/dist/codegen/expressions/fnctor-prototype.d.ts +52 -0
  60. package/dist/codegen/expressions/helpers.d.ts +212 -0
  61. package/dist/codegen/expressions/identifiers.d.ts +57 -0
  62. package/dist/codegen/expressions/late-imports.d.ts +81 -0
  63. package/dist/codegen/expressions/logical-ops.d.ts +18 -0
  64. package/dist/codegen/expressions/misc.d.ts +27 -0
  65. package/dist/codegen/expressions/new-super.d.ts +25 -0
  66. package/dist/codegen/expressions/promise-subclass.d.ts +38 -0
  67. package/dist/codegen/expressions/proto-override.d.ts +63 -0
  68. package/dist/codegen/expressions/unary-updates.d.ts +21 -0
  69. package/dist/codegen/expressions/unary.d.ts +6 -0
  70. package/dist/codegen/expressions.d.ts +31 -0
  71. package/dist/codegen/fallback-telemetry.d.ts +53 -0
  72. package/dist/codegen/fixups.d.ts +80 -0
  73. package/dist/codegen/fmod.d.ts +10 -0
  74. package/dist/codegen/fnctor-escape-gate.d.ts +92 -0
  75. package/dist/codegen/function-body.d.ts +52 -0
  76. package/dist/codegen/generators-native.d.ts +92 -0
  77. package/dist/codegen/helpers/body-references-own-this.d.ts +22 -0
  78. package/dist/codegen/helpers/body-uses-arguments.d.ts +12 -0
  79. package/dist/codegen/helpers/is-strict-function.d.ts +52 -0
  80. package/dist/codegen/host-import-allowlist.d.ts +140 -0
  81. package/dist/codegen/index.d.ts +500 -0
  82. package/dist/codegen/ir-tail-call.d.ts +8 -0
  83. package/dist/codegen/iterator-native.d.ts +44 -0
  84. package/dist/codegen/json-codec-native.d.ts +78 -0
  85. package/dist/codegen/json-runtime.d.ts +35 -0
  86. package/dist/codegen/json-standalone.d.ts +25 -0
  87. package/dist/codegen/linear-uint8-analysis.d.ts +46 -0
  88. package/dist/codegen/linear-uint8-arena.d.ts +7 -0
  89. package/dist/codegen/linear-uint8-codegen.d.ts +103 -0
  90. package/dist/codegen/linear-uint8-signatures.d.ts +26 -0
  91. package/dist/codegen/literals.d.ts +115 -0
  92. package/dist/codegen/map-runtime.d.ts +142 -0
  93. package/dist/codegen/math-helpers.d.ts +7 -0
  94. package/dist/codegen/member-get-dispatch.d.ts +42 -0
  95. package/dist/codegen/member-set-dispatch.d.ts +28 -0
  96. package/dist/codegen/native-proto.d.ts +98 -0
  97. package/dist/codegen/native-regex.d.ts +158 -0
  98. package/dist/codegen/native-strings.d.ts +146 -0
  99. package/dist/codegen/new-target.d.ts +30 -0
  100. package/dist/codegen/node-fs-api.d.ts +47 -0
  101. package/dist/codegen/number-format-native.d.ts +9 -0
  102. package/dist/codegen/number-ryu.d.ts +27 -0
  103. package/dist/codegen/object-ops.d.ts +94 -0
  104. package/dist/codegen/object-runtime.d.ts +171 -0
  105. package/dist/codegen/parse-number-native.d.ts +10 -0
  106. package/dist/codegen/peephole.d.ts +6 -0
  107. package/dist/codegen/property-access.d.ts +294 -0
  108. package/dist/codegen/raw-wasi-api.d.ts +13 -0
  109. package/dist/codegen/regex/bytecode.d.ts +140 -0
  110. package/dist/codegen/regex/casefold.d.ts +41 -0
  111. package/dist/codegen/regex/compile.d.ts +51 -0
  112. package/dist/codegen/regex/parse.d.ts +76 -0
  113. package/dist/codegen/regex/unicode.d.ts +42 -0
  114. package/dist/codegen/regex/vm.d.ts +24 -0
  115. package/dist/codegen/regexp-standalone.d.ts +350 -0
  116. package/dist/codegen/registry/error-types.d.ts +38 -0
  117. package/dist/codegen/registry/imports.d.ts +46 -0
  118. package/dist/codegen/registry/types.d.ts +59 -0
  119. package/dist/codegen/set-algebra.d.ts +17 -0
  120. package/dist/codegen/set-runtime.d.ts +74 -0
  121. package/dist/codegen/shared.d.ts +111 -0
  122. package/dist/codegen/stack-balance.d.ts +43 -0
  123. package/dist/codegen/statements/control-flow.d.ts +25 -0
  124. package/dist/codegen/statements/destructuring.d.ts +177 -0
  125. package/dist/codegen/statements/exceptions.d.ts +11 -0
  126. package/dist/codegen/statements/functions.d.ts +1 -0
  127. package/dist/codegen/statements/index.d.ts +1 -0
  128. package/dist/codegen/statements/loops.d.ts +7 -0
  129. package/dist/codegen/statements/nested-declarations.d.ts +78 -0
  130. package/dist/codegen/statements/shared.d.ts +38 -0
  131. package/dist/codegen/statements/tdz.d.ts +43 -0
  132. package/dist/codegen/statements/variables.d.ts +3 -0
  133. package/dist/codegen/statements.d.ts +9 -0
  134. package/dist/codegen/string-builder.d.ts +131 -0
  135. package/dist/codegen/string-ops.d.ts +87 -0
  136. package/dist/codegen/struct-accessor-closure.d.ts +36 -0
  137. package/dist/codegen/symbol-native.d.ts +55 -0
  138. package/dist/codegen/temporal-native.d.ts +8 -0
  139. package/dist/codegen/timsort.d.ts +2 -0
  140. package/dist/codegen/type-coercion.d.ts +123 -0
  141. package/dist/codegen/typeof-delete.d.ts +38 -0
  142. package/dist/codegen/uri-encoding-native.d.ts +33 -0
  143. package/dist/codegen/value-tags.d.ts +74 -0
  144. package/dist/codegen/walk-instructions.d.ts +20 -0
  145. package/dist/codegen/weak-collections-runtime.d.ts +16 -0
  146. package/dist/codegen/with-scope.d.ts +106 -0
  147. package/dist/codegen-linear/c-abi.d.ts +74 -0
  148. package/dist/codegen-linear/context.d.ts +86 -0
  149. package/dist/codegen-linear/index.d.ts +28 -0
  150. package/dist/codegen-linear/layout.d.ts +39 -0
  151. package/dist/codegen-linear/runtime.d.ts +161 -0
  152. package/dist/codegen-linear/simd.d.ts +7 -0
  153. package/dist/compiler/define-substitution.d.ts +27 -0
  154. package/dist/compiler/early-errors/assignment.d.ts +26 -0
  155. package/dist/compiler/early-errors/context.d.ts +17 -0
  156. package/dist/compiler/early-errors/duplicates.d.ts +20 -0
  157. package/dist/compiler/early-errors/index.d.ts +11 -0
  158. package/dist/compiler/early-errors/labels.d.ts +13 -0
  159. package/dist/compiler/early-errors/module-rules.d.ts +36 -0
  160. package/dist/compiler/early-errors/node-checks.d.ts +7 -0
  161. package/dist/compiler/early-errors/predicates.d.ts +140 -0
  162. package/dist/compiler/early-errors/tdz.d.ts +17 -0
  163. package/dist/compiler/import-manifest.d.ts +18 -0
  164. package/dist/compiler/output.d.ts +46 -0
  165. package/dist/compiler/validation.d.ts +45 -0
  166. package/dist/compiler.d.ts +48 -0
  167. package/dist/define-substitution-BcUeKC2A.js +109 -0
  168. package/dist/emit/binary.d.ts +50 -0
  169. package/dist/emit/c-header.d.ts +23 -0
  170. package/dist/emit/canonical-recgroup.d.ts +86 -0
  171. package/dist/emit/encoder.d.ts +28 -0
  172. package/dist/emit/object.d.ts +14 -0
  173. package/dist/emit/opcodes.d.ts +464 -0
  174. package/dist/emit/sourcemap.d.ts +33 -0
  175. package/dist/emit/wat.d.ts +6 -0
  176. package/dist/env.d.ts +46 -0
  177. package/dist/import-resolver.d.ts +68 -0
  178. package/dist/index.d.ts +486 -0
  179. package/dist/index.js +755 -0
  180. package/dist/ir/alloc-registry.d.ts +75 -0
  181. package/dist/ir/analysis/encoding.d.ts +38 -0
  182. package/dist/ir/analysis/escape.d.ts +32 -0
  183. package/dist/ir/analysis/lattice.d.ts +72 -0
  184. package/dist/ir/analysis/ownership.d.ts +31 -0
  185. package/dist/ir/analysis/stack-alloc.d.ts +20 -0
  186. package/dist/ir/backend/bytecode-emitter.d.ts +237 -0
  187. package/dist/ir/backend/bytecode-vm.d.ts +74 -0
  188. package/dist/ir/backend/emitter.d.ts +121 -0
  189. package/dist/ir/backend/handles.d.ts +133 -0
  190. package/dist/ir/backend/legality.d.ts +9 -0
  191. package/dist/ir/backend/linear-emitter.d.ts +41 -0
  192. package/dist/ir/backend/wasmgc-emitter.d.ts +43 -0
  193. package/dist/ir/builder.d.ts +401 -0
  194. package/dist/ir/from-ast.d.ts +192 -0
  195. package/dist/ir/index.d.ts +16 -0
  196. package/dist/ir/integration.d.ts +27 -0
  197. package/dist/ir/lower.d.ts +203 -0
  198. package/dist/ir/nodes.d.ts +1452 -0
  199. package/dist/ir/passes/alloc-discipline.d.ts +19 -0
  200. package/dist/ir/passes/constant-fold.d.ts +7 -0
  201. package/dist/ir/passes/dead-code.d.ts +18 -0
  202. package/dist/ir/passes/inline-small.d.ts +7 -0
  203. package/dist/ir/passes/monomorphize.d.ts +21 -0
  204. package/dist/ir/passes/simplify-cfg.d.ts +19 -0
  205. package/dist/ir/passes/tagged-union-types.d.ts +45 -0
  206. package/dist/ir/passes/tagged-unions.d.ts +22 -0
  207. package/dist/ir/propagate.d.ts +135 -0
  208. package/dist/ir/select.d.ts +81 -0
  209. package/dist/ir/types.d.ts +832 -0
  210. package/dist/ir/verify-alloc.d.ts +18 -0
  211. package/dist/ir/verify.d.ts +7 -0
  212. package/dist/link/index.d.ts +11 -0
  213. package/dist/link/isolation.d.ts +24 -0
  214. package/dist/link/linker.d.ts +37 -0
  215. package/dist/link/reader.d.ts +158 -0
  216. package/dist/link/resolver.d.ts +19 -0
  217. package/dist/optimize.d.ts +54 -0
  218. package/dist/optimize.js +262 -0
  219. package/dist/position-map.d.ts +64 -0
  220. package/dist/process-stdin-prelude.d.ts +16 -0
  221. package/dist/resolve.d.ts +82 -0
  222. package/dist/runtime/builtins.d.ts +1 -0
  223. package/dist/runtime-C-4q_KwU.js +164438 -0
  224. package/dist/runtime-containment.d.ts +6 -0
  225. package/dist/runtime-eval.d.ts +132 -0
  226. package/dist/runtime-instantiate.d.ts +16 -0
  227. package/dist/runtime.d.ts +128 -0
  228. package/dist/runtime.js +12 -0
  229. package/dist/shape-inference.d.ts +20 -0
  230. package/dist/treeshake.d.ts +17 -0
  231. package/dist/ts-api.d.ts +30 -0
  232. package/dist/wit-generator.d.ts +18 -0
  233. package/package.json +187 -0
package/dist/index.js ADDED
@@ -0,0 +1,755 @@
1
+ import * as path from "path";
2
+ import { i as isKnownLibName, g as getLibSourceFile, a as getDefaultEnvironment, r as rewriteCjsRequire, f as forEachChild, c as compileSource, b as buildImports, d as compileFilesSource, e as compileMultiSource, h as compileToObjectSource } from "./runtime-C-4q_KwU.js";
3
+ import { j, k, l, m, n, o, p, q, s } from "./runtime-C-4q_KwU.js";
4
+ import ts from "typescript";
5
+ class IncrementalLanguageService {
6
+ currentSource = "";
7
+ currentSourceFile;
8
+ fileName;
9
+ compilerOptions;
10
+ host;
11
+ // #2528/#2736 — the default-lib composite name for the current `analyze` call.
12
+ // `lib.d.ts` (DOM) by default; `lib.no-dom.d.ts` under `--target node`/`deno`.
13
+ // Read by the host's `getDefaultLibFileName` closure at `createProgram` time.
14
+ defaultLibName = "lib.d.ts";
15
+ constructor(fileName = "input.ts") {
16
+ this.fileName = fileName;
17
+ this.compilerOptions = {
18
+ target: ts.ScriptTarget.ES2022,
19
+ module: ts.ModuleKind.ESNext,
20
+ strict: true,
21
+ noImplicitAny: false,
22
+ noEmit: true
23
+ };
24
+ this.host = {
25
+ getSourceFile: (name, languageVersion) => {
26
+ if (name === this.fileName) return this.currentSourceFile;
27
+ return getLibSourceFile(name, languageVersion);
28
+ },
29
+ getDefaultLibFileName: () => this.defaultLibName,
30
+ writeFile: () => {
31
+ },
32
+ getCurrentDirectory: () => "/",
33
+ getCanonicalFileName: (f) => f,
34
+ useCaseSensitiveFileNames: () => true,
35
+ getNewLine: () => "\n",
36
+ fileExists: (name) => name === this.fileName || isKnownLibName(name),
37
+ readFile: (name) => {
38
+ if (name === this.fileName) return this.currentSource;
39
+ return void 0;
40
+ },
41
+ getDirectories: () => [],
42
+ directoryExists: () => true
43
+ };
44
+ }
45
+ /**
46
+ * Update the source for the next compilation.
47
+ *
48
+ * #2752 — `forceTsGrammar` forces `ScriptKind.TS` for the parsed SourceFile
49
+ * even when `fileName` ends in `.js` (otherwise `ts.createSourceFile` infers
50
+ * `ScriptKind.JS` from the extension and the loose-JS grammar checker rejects
51
+ * an injected TS prelude with TS8009/8010/8017). Mirrors the
52
+ * `forceTsGrammar` override in the non-incremental `analyzeSource`.
53
+ */
54
+ updateSource(source, fileName, forceTsGrammar) {
55
+ this.currentSource = source;
56
+ if (fileName) this.fileName = fileName;
57
+ this.currentSourceFile = ts.createSourceFile(
58
+ this.fileName,
59
+ this.currentSource,
60
+ this.compilerOptions.target ?? ts.ScriptTarget.ES2022,
61
+ true,
62
+ forceTsGrammar ? ts.ScriptKind.TS : void 0
63
+ );
64
+ }
65
+ /**
66
+ * Analyze — fresh Program + fresh checker, cached lib SourceFiles.
67
+ */
68
+ analyze(analyzeOptions) {
69
+ const options = { ...this.compilerOptions };
70
+ if (analyzeOptions?.allowJs) {
71
+ options.allowJs = true;
72
+ options.checkJs = true;
73
+ }
74
+ this.defaultLibName = analyzeOptions?.platform === "node" || analyzeOptions?.platform === "deno" ? "lib.no-dom.d.ts" : "lib.d.ts";
75
+ const program = ts.createProgram([this.fileName], options, this.host);
76
+ const checker = program.getTypeChecker();
77
+ const syntacticDiagnostics = program.getSyntacticDiagnostics();
78
+ const semanticDiagnostics = analyzeOptions?.skipSemanticDiagnostics ? [] : program.getSemanticDiagnostics();
79
+ const diagnostics = [...syntacticDiagnostics, ...semanticDiagnostics];
80
+ return {
81
+ sourceFile: program.getSourceFile(this.fileName),
82
+ checker,
83
+ program,
84
+ diagnostics,
85
+ syntacticDiagnostics
86
+ };
87
+ }
88
+ dispose() {
89
+ }
90
+ }
91
+ function getFs() {
92
+ return getDefaultEnvironment().fs;
93
+ }
94
+ class ModuleResolver {
95
+ constructor(rootDir, options) {
96
+ this.rootDir = rootDir;
97
+ this.externals = new Set(options?.externals ?? []);
98
+ this.extensions = options?.resolve?.extensions ?? [".ts", ".tsx", ".d.ts"];
99
+ const moduleDirs = options?.resolve?.modules ?? ["node_modules"];
100
+ this.compilerOptions = {
101
+ target: ts.ScriptTarget.ES2022,
102
+ module: ts.ModuleKind.ESNext,
103
+ moduleResolution: ts.ModuleResolutionKind.Node10,
104
+ baseUrl: rootDir,
105
+ rootDir,
106
+ // Allow TS to find types in the specified module directories
107
+ typeRoots: moduleDirs.map((d) => path.isAbsolute(d) ? d : path.resolve(rootDir, d)),
108
+ // Try to load tsconfig.json paths if available
109
+ ...this.loadTsconfigPaths()
110
+ };
111
+ this.host = {
112
+ fileExists: (fileName) => {
113
+ try {
114
+ return getFs().statSync(fileName).isFile();
115
+ } catch {
116
+ return false;
117
+ }
118
+ },
119
+ readFile: (fileName) => {
120
+ try {
121
+ return getFs().readFileSync(fileName, "utf-8");
122
+ } catch {
123
+ return void 0;
124
+ }
125
+ },
126
+ directoryExists: (dirName) => {
127
+ try {
128
+ return getFs().statSync(dirName).isDirectory();
129
+ } catch {
130
+ return false;
131
+ }
132
+ },
133
+ getCurrentDirectory: () => rootDir,
134
+ getDirectories: (dirPath) => {
135
+ try {
136
+ return getFs().readdirSync(dirPath, { withFileTypes: true }).filter((d) => d.isDirectory()).map((d) => d.name);
137
+ } catch {
138
+ return [];
139
+ }
140
+ },
141
+ realpath: (p2) => {
142
+ try {
143
+ return getFs().realpathSync(p2);
144
+ } catch {
145
+ return p2;
146
+ }
147
+ }
148
+ };
149
+ }
150
+ compilerOptions;
151
+ host;
152
+ externals;
153
+ extensions;
154
+ resolveCache = /* @__PURE__ */ new Map();
155
+ /**
156
+ * Try to load tsconfig.json paths configuration from the root directory.
157
+ */
158
+ loadTsconfigPaths() {
159
+ const tsconfigPath = path.join(this.rootDir, "tsconfig.json");
160
+ try {
161
+ if (!getFs().statSync(tsconfigPath).isFile()) return {};
162
+ } catch {
163
+ return {};
164
+ }
165
+ const configFile = ts.readConfigFile(tsconfigPath, (p2) => getFs().readFileSync(p2, "utf-8"));
166
+ if (configFile.error || !configFile.config) return {};
167
+ const parsed = ts.parseJsonConfigFileContent(configFile.config, ts.sys, this.rootDir);
168
+ const result = {};
169
+ if (parsed.options.paths) {
170
+ result.paths = parsed.options.paths;
171
+ }
172
+ if (parsed.options.baseUrl) {
173
+ result.baseUrl = parsed.options.baseUrl;
174
+ }
175
+ return result;
176
+ }
177
+ /**
178
+ * Resolve a module specifier to a file path.
179
+ *
180
+ * @param specifier - The import specifier (e.g., "lodash", "./utils")
181
+ * @param containingFile - The file that contains the import statement
182
+ * @returns The resolved file path, or null if the module is external or not found
183
+ */
184
+ resolve(specifier, containingFile) {
185
+ const pkgName = getBarePackageName(specifier);
186
+ if (pkgName && this.externals.has(pkgName)) {
187
+ return null;
188
+ }
189
+ const cacheKey = `${containingFile}::${specifier}`;
190
+ if (this.resolveCache.has(cacheKey)) {
191
+ return this.resolveCache.get(cacheKey);
192
+ }
193
+ const result = ts.resolveModuleName(specifier, containingFile, this.compilerOptions, this.host);
194
+ let resolved = null;
195
+ if (result.resolvedModule) {
196
+ resolved = result.resolvedModule.resolvedFileName;
197
+ resolved = path.resolve(resolved);
198
+ if (pkgName && (/[/\\]@types[/\\]/.test(resolved) || resolved.endsWith(".d.ts"))) {
199
+ const implPath = this.findImplementationBody(pkgName, specifier, containingFile);
200
+ if (implPath) {
201
+ resolved = implPath;
202
+ }
203
+ }
204
+ }
205
+ this.resolveCache.set(cacheKey, resolved);
206
+ return resolved;
207
+ }
208
+ /**
209
+ * When `ts.resolveModuleName` returned a file under `@types/<pkg>/`,
210
+ * attempt to find the matching real implementation body in a sibling
211
+ * `node_modules/<pkg>/` directory and return its absolute path, or null
212
+ * if no implementation file can be located.
213
+ *
214
+ * Handles both standard npm layouts (`node_modules/<pkg>/...`) and pnpm
215
+ * layouts (where `@types/<pkg>` lives under `.pnpm/` but the real package
216
+ * is still hoisted to the top-level `node_modules/<pkg>`). The search
217
+ * walks up from `containingFile` through parent directories looking for
218
+ * each candidate — this matches Node's own module resolution walk.
219
+ */
220
+ findImplementationBody(pkgName, specifier, containingFile) {
221
+ const fs = getFs();
222
+ if (!fs) return null;
223
+ const afterPkg = specifier.slice(pkgName.length).replace(/^\//, "");
224
+ const probeExtensions = ["", ".js", ".mjs", ".cjs", ".ts"];
225
+ let dir = path.dirname(containingFile);
226
+ const root = path.parse(dir).root;
227
+ const seenDirs = /* @__PURE__ */ new Set();
228
+ while (!seenDirs.has(dir)) {
229
+ seenDirs.add(dir);
230
+ const pkgRoot = path.join(dir, "node_modules", pkgName);
231
+ if (this.tryStatDir(pkgRoot)) {
232
+ const found = this.probeImplementationPath(pkgRoot, afterPkg, probeExtensions);
233
+ if (found) return found;
234
+ }
235
+ if (dir === root) break;
236
+ const parent = path.dirname(dir);
237
+ if (parent === dir) break;
238
+ dir = parent;
239
+ }
240
+ const rootPkg = path.join(this.rootDir, "node_modules", pkgName);
241
+ if (this.tryStatDir(rootPkg)) {
242
+ const found = this.probeImplementationPath(rootPkg, afterPkg, probeExtensions);
243
+ if (found) return found;
244
+ }
245
+ return null;
246
+ }
247
+ tryStatDir(p2) {
248
+ const fs = getFs();
249
+ if (!fs) return false;
250
+ try {
251
+ return fs.statSync(p2).isDirectory();
252
+ } catch {
253
+ return false;
254
+ }
255
+ }
256
+ tryStatFile(p2) {
257
+ const fs = getFs();
258
+ if (!fs) return false;
259
+ try {
260
+ return fs.statSync(p2).isFile();
261
+ } catch {
262
+ return false;
263
+ }
264
+ }
265
+ /**
266
+ * Given a package root (e.g. `.../node_modules/lodash-es`) and a subpath
267
+ * from a specifier (e.g. `identity.js` or `identity` or ``), attempt to
268
+ * locate the implementation file on disk using the probe extensions.
269
+ */
270
+ probeImplementationPath(pkgRoot, afterPkg, exts) {
271
+ if (afterPkg === "") {
272
+ const pkgJsonPath = path.join(pkgRoot, "package.json");
273
+ if (this.tryStatFile(pkgJsonPath)) {
274
+ try {
275
+ const pkg = JSON.parse(getFs().readFileSync(pkgJsonPath, "utf-8"));
276
+ const mainField = pkg.module ?? pkg.main;
277
+ if (typeof mainField === "string" && mainField.length > 0) {
278
+ const mainPath = path.resolve(pkgRoot, mainField);
279
+ if (this.tryStatFile(mainPath)) return mainPath;
280
+ for (const ext of exts) {
281
+ if (ext === "") continue;
282
+ const withExt = mainPath + ext;
283
+ if (this.tryStatFile(withExt)) return withExt;
284
+ }
285
+ }
286
+ } catch {
287
+ }
288
+ }
289
+ for (const ext of exts) {
290
+ if (ext === "") continue;
291
+ const indexPath = path.join(pkgRoot, "index" + ext);
292
+ if (this.tryStatFile(indexPath)) return indexPath;
293
+ }
294
+ return null;
295
+ }
296
+ const direct = path.join(pkgRoot, afterPkg);
297
+ if (this.tryStatFile(direct)) return direct;
298
+ if (afterPkg.endsWith(".js")) {
299
+ const asTs = path.join(pkgRoot, afterPkg.slice(0, -3) + ".ts");
300
+ if (this.tryStatFile(asTs)) return asTs;
301
+ const asMjs = path.join(pkgRoot, afterPkg.slice(0, -3) + ".mjs");
302
+ if (this.tryStatFile(asMjs)) return asMjs;
303
+ }
304
+ if (!/\.[a-zA-Z0-9]+$/.test(afterPkg)) {
305
+ for (const ext of exts) {
306
+ if (ext === "") continue;
307
+ const withExt = path.join(pkgRoot, afterPkg + ext);
308
+ if (this.tryStatFile(withExt)) return withExt;
309
+ }
310
+ }
311
+ return null;
312
+ }
313
+ /**
314
+ * Check if a specifier refers to an external package.
315
+ */
316
+ isExternal(specifier) {
317
+ const pkgName = getBarePackageName(specifier);
318
+ return pkgName !== null && this.externals.has(pkgName);
319
+ }
320
+ }
321
+ function getBarePackageName(specifier) {
322
+ if (specifier.startsWith(".") || specifier.startsWith("/")) {
323
+ return null;
324
+ }
325
+ if (specifier.startsWith("@")) {
326
+ const parts = specifier.split("/");
327
+ if (parts.length >= 2) {
328
+ return `${parts[0]}/${parts[1]}`;
329
+ }
330
+ return specifier;
331
+ }
332
+ const slashIdx = specifier.indexOf("/");
333
+ if (slashIdx === -1) return specifier;
334
+ return specifier.slice(0, slashIdx);
335
+ }
336
+ function resolveAllImports(entryFile, resolver) {
337
+ const resolved = /* @__PURE__ */ new Map();
338
+ const visited = /* @__PURE__ */ new Set();
339
+ const onStack = /* @__PURE__ */ new Set();
340
+ function visit(filePath) {
341
+ if (visited.has(filePath) || onStack.has(filePath)) return;
342
+ onStack.add(filePath);
343
+ let content;
344
+ try {
345
+ content = getFs().readFileSync(filePath, "utf-8");
346
+ } catch {
347
+ onStack.delete(filePath);
348
+ return;
349
+ }
350
+ content = rewriteCjsRequire(content);
351
+ const sf = ts.createSourceFile(
352
+ filePath,
353
+ content,
354
+ ts.ScriptTarget.Latest,
355
+ true,
356
+ filePath.endsWith(".tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS
357
+ );
358
+ for (const stmt of sf.statements) {
359
+ if (ts.isImportDeclaration(stmt) && ts.isStringLiteral(stmt.moduleSpecifier)) {
360
+ const specifier = stmt.moduleSpecifier.text;
361
+ const resolvedPath = resolver.resolve(specifier, filePath);
362
+ if (resolvedPath) visit(resolvedPath);
363
+ }
364
+ if (ts.isExportDeclaration(stmt) && stmt.moduleSpecifier && ts.isStringLiteral(stmt.moduleSpecifier)) {
365
+ const specifier = stmt.moduleSpecifier.text;
366
+ const resolvedPath = resolver.resolve(specifier, filePath);
367
+ if (resolvedPath) visit(resolvedPath);
368
+ }
369
+ }
370
+ visited.add(filePath);
371
+ onStack.delete(filePath);
372
+ resolved.set(filePath, content);
373
+ }
374
+ visit(path.resolve(entryFile));
375
+ return resolved;
376
+ }
377
+ function treeshake(entryExports, sourceFiles, checker) {
378
+ const reachable = /* @__PURE__ */ new Set();
379
+ const visitedSymbols = /* @__PURE__ */ new Set();
380
+ const symbolToDecl = /* @__PURE__ */ new Map();
381
+ for (const sf of sourceFiles) {
382
+ for (const stmt of sf.statements) {
383
+ const symbols = getTopLevelSymbols(stmt, checker);
384
+ for (const sym of symbols) {
385
+ symbolToDecl.set(sym, stmt);
386
+ }
387
+ }
388
+ }
389
+ const entryFile = sourceFiles[sourceFiles.length - 1];
390
+ if (!entryFile) return reachable;
391
+ const entryExportSet = new Set(entryExports);
392
+ for (const stmt of entryFile.statements) {
393
+ if (!hasExportModifier(stmt)) continue;
394
+ const names = getDeclaredNames(stmt);
395
+ for (const name of names) {
396
+ if (entryExportSet.size === 0 || entryExportSet.has(name)) {
397
+ const symbols = getTopLevelSymbols(stmt, checker);
398
+ for (const sym of symbols) {
399
+ markReachable(sym, symbolToDecl, reachable, visitedSymbols, checker);
400
+ }
401
+ }
402
+ }
403
+ }
404
+ for (const sf of sourceFiles) {
405
+ for (const stmt of sf.statements) {
406
+ if (isSideEffectStatement(stmt)) {
407
+ reachable.add(stmt);
408
+ const symbols = getTopLevelSymbols(stmt, checker);
409
+ for (const sym of symbols) {
410
+ markReachable(sym, symbolToDecl, reachable, visitedSymbols, checker);
411
+ }
412
+ }
413
+ }
414
+ }
415
+ return reachable;
416
+ }
417
+ function markReachable(sym, symbolToDecl, reachable, visitedSymbols, checker) {
418
+ if (visitedSymbols.has(sym)) return;
419
+ visitedSymbols.add(sym);
420
+ const aliased = getAliasedSymbol(sym, checker);
421
+ if (aliased && aliased !== sym) {
422
+ markReachable(aliased, symbolToDecl, reachable, visitedSymbols, checker);
423
+ }
424
+ const declNode = symbolToDecl.get(sym);
425
+ if (declNode) {
426
+ reachable.add(declNode);
427
+ }
428
+ if (aliased && aliased !== sym) {
429
+ const aliasedDecl = symbolToDecl.get(aliased);
430
+ if (aliasedDecl) {
431
+ reachable.add(aliasedDecl);
432
+ }
433
+ }
434
+ const declarations = sym.getDeclarations() ?? [];
435
+ for (const decl of declarations) {
436
+ walkReferences(decl, symbolToDecl, reachable, visitedSymbols, checker);
437
+ }
438
+ }
439
+ function walkReferences(node, symbolToDecl, reachable, visitedSymbols, checker) {
440
+ function visit(n2) {
441
+ if (ts.isIdentifier(n2)) {
442
+ try {
443
+ const sym = checker.getSymbolAtLocation(n2);
444
+ if (sym) {
445
+ markReachable(sym, symbolToDecl, reachable, visitedSymbols, checker);
446
+ }
447
+ } catch {
448
+ }
449
+ }
450
+ if (ts.isTypeReferenceNode(n2)) {
451
+ try {
452
+ const sym = checker.getSymbolAtLocation(n2.typeName);
453
+ if (sym) {
454
+ markReachable(sym, symbolToDecl, reachable, visitedSymbols, checker);
455
+ }
456
+ } catch {
457
+ }
458
+ }
459
+ forEachChild(n2, visit);
460
+ }
461
+ visit(node);
462
+ }
463
+ function getAliasedSymbol(sym, checker) {
464
+ try {
465
+ if (sym.flags & ts.SymbolFlags.Alias) {
466
+ return checker.getAliasedSymbol(sym);
467
+ }
468
+ } catch {
469
+ }
470
+ return void 0;
471
+ }
472
+ function getTopLevelSymbols(stmt, checker) {
473
+ const symbols = [];
474
+ if (ts.isFunctionDeclaration(stmt) && stmt.name) {
475
+ const sym = checker.getSymbolAtLocation(stmt.name);
476
+ if (sym) symbols.push(sym);
477
+ } else if (ts.isClassDeclaration(stmt) && stmt.name) {
478
+ const sym = checker.getSymbolAtLocation(stmt.name);
479
+ if (sym) symbols.push(sym);
480
+ } else if (ts.isVariableStatement(stmt)) {
481
+ for (const decl of stmt.declarationList.declarations) {
482
+ if (ts.isIdentifier(decl.name)) {
483
+ const sym = checker.getSymbolAtLocation(decl.name);
484
+ if (sym) symbols.push(sym);
485
+ }
486
+ }
487
+ } else if (ts.isEnumDeclaration(stmt)) {
488
+ const sym = checker.getSymbolAtLocation(stmt.name);
489
+ if (sym) symbols.push(sym);
490
+ } else if (ts.isInterfaceDeclaration(stmt)) {
491
+ const sym = checker.getSymbolAtLocation(stmt.name);
492
+ if (sym) symbols.push(sym);
493
+ } else if (ts.isTypeAliasDeclaration(stmt)) {
494
+ const sym = checker.getSymbolAtLocation(stmt.name);
495
+ if (sym) symbols.push(sym);
496
+ }
497
+ return symbols;
498
+ }
499
+ function getDeclaredNames(stmt) {
500
+ const names = [];
501
+ if (ts.isFunctionDeclaration(stmt) && stmt.name) {
502
+ names.push(stmt.name.text);
503
+ } else if (ts.isClassDeclaration(stmt) && stmt.name) {
504
+ names.push(stmt.name.text);
505
+ } else if (ts.isVariableStatement(stmt)) {
506
+ for (const decl of stmt.declarationList.declarations) {
507
+ if (ts.isIdentifier(decl.name)) {
508
+ names.push(decl.name.text);
509
+ }
510
+ }
511
+ } else if (ts.isEnumDeclaration(stmt)) {
512
+ names.push(stmt.name.text);
513
+ } else if (ts.isInterfaceDeclaration(stmt)) {
514
+ names.push(stmt.name.text);
515
+ } else if (ts.isTypeAliasDeclaration(stmt)) {
516
+ names.push(stmt.name.text);
517
+ }
518
+ return names;
519
+ }
520
+ function hasExportModifier(node) {
521
+ const modifiers = ts.canHaveModifiers(node) ? ts.getModifiers(node) : void 0;
522
+ return modifiers?.some((m2) => m2.kind === ts.SyntaxKind.ExportKeyword) ?? false;
523
+ }
524
+ function isSideEffectStatement(stmt) {
525
+ if (ts.isImportDeclaration(stmt)) return true;
526
+ if (ts.isExportDeclaration(stmt) && stmt.moduleSpecifier) return true;
527
+ if (ts.isExpressionStatement(stmt)) return true;
528
+ return false;
529
+ }
530
+ function getEntryExportNames(entryFile) {
531
+ const names = [];
532
+ for (const stmt of entryFile.statements) {
533
+ if (hasExportModifier(stmt)) {
534
+ names.push(...getDeclaredNames(stmt));
535
+ }
536
+ }
537
+ return names;
538
+ }
539
+ const RUNTIME_RECGROUP_TYPE_NAMES = [
540
+ // String family (i16-array native-string backend).
541
+ "AnyString",
542
+ "NativeString",
543
+ "ConsString",
544
+ "__str_data",
545
+ // Vec / array family (name-stable members only).
546
+ "__vec_base",
547
+ "__vec_f64",
548
+ "__arr_f64"
549
+ ];
550
+ const RUNTIME_RECGROUP_ABI_VERSION = 1;
551
+ const RUNTIME_NAME_SET = new Set(RUNTIME_RECGROUP_TYPE_NAMES);
552
+ function typeDefName(t) {
553
+ switch (t.kind) {
554
+ case "func":
555
+ case "struct":
556
+ case "array":
557
+ return t.name;
558
+ case "sub":
559
+ return t.name;
560
+ case "rec":
561
+ return void 0;
562
+ }
563
+ }
564
+ function valTypeToken(t, localOf) {
565
+ switch (t.kind) {
566
+ case "ref":
567
+ case "ref_null": {
568
+ const local = localOf(t.typeIdx);
569
+ const nul = t.kind === "ref_null" ? "n" : "";
570
+ if (local !== void 0) return `${nul}r${local}`;
571
+ return `${nul}x`;
572
+ }
573
+ case "i32":
574
+ return t.boolean ? "i32b" : "i32";
575
+ case "i64":
576
+ return t.bigint ? "i64big" : "i64";
577
+ default:
578
+ return t.kind;
579
+ }
580
+ }
581
+ function fieldToken(f, localOf) {
582
+ return `${f.mutable ? "m" : ""}${valTypeToken(f.type, localOf)}`;
583
+ }
584
+ function structuralToken(t, localOf) {
585
+ switch (t.kind) {
586
+ case "func": {
587
+ const p2 = t.params.map((v) => valTypeToken(v, localOf)).join(",");
588
+ const r = t.results.map((v) => valTypeToken(v, localOf)).join(",");
589
+ return `func(${p2})->(${r})`;
590
+ }
591
+ case "struct": {
592
+ const sup = t.superTypeIdx !== void 0 && t.superTypeIdx >= 0 ? (() => {
593
+ const local = localOf(t.superTypeIdx);
594
+ return local !== void 0 ? `sub r${local}${t.final ? "!" : ""} ` : `sub x${t.final ? "!" : ""} `;
595
+ })() : "";
596
+ const fields = t.fields.map((f) => fieldToken(f, localOf)).join(";");
597
+ return `${sup}struct{${fields}}`;
598
+ }
599
+ case "array":
600
+ return `array<${t.mutable ? "m" : ""}${valTypeToken(t.element, localOf)}>`;
601
+ case "sub": {
602
+ const sup = t.superType !== null ? (() => {
603
+ const local = localOf(t.superType);
604
+ return local !== void 0 ? `sub r${local}${t.final ? "!" : ""} ` : `sub x${t.final ? "!" : ""} `;
605
+ })() : t.final ? "final " : "";
606
+ return `${sup}${structuralToken(t.type, localOf)}`;
607
+ }
608
+ }
609
+ }
610
+ function fnv1a64(s2) {
611
+ let h = 0xcbf29ce484222325n;
612
+ const prime = 0x100000001b3n;
613
+ const mask = 0xffffffffffffffffn;
614
+ for (let i = 0; i < s2.length; i++) {
615
+ h ^= BigInt(s2.charCodeAt(i));
616
+ h = h * prime & mask;
617
+ }
618
+ return h.toString(16).padStart(16, "0");
619
+ }
620
+ function canonicalHashOfTypeGroup(group, absIndices) {
621
+ const indexToLocal = /* @__PURE__ */ new Map();
622
+ for (let i = 0; i < absIndices.length; i++) indexToLocal.set(absIndices[i], i);
623
+ const localOf = (absIdx) => indexToLocal.get(absIdx);
624
+ const parts = [`v${RUNTIME_RECGROUP_ABI_VERSION}`, `n${group.length}`];
625
+ for (let i = 0; i < group.length; i++) {
626
+ parts.push(`${i}:${structuralToken(group[i], localOf)}`);
627
+ }
628
+ return fnv1a64(parts.join("|"));
629
+ }
630
+ function extractRuntimeGroup(mod) {
631
+ const out = [];
632
+ const types = mod.types;
633
+ for (let i = 0; i < types.length; i++) {
634
+ const t = types[i];
635
+ if (t.kind === "rec") continue;
636
+ const name = typeDefName(t);
637
+ if (name !== void 0 && RUNTIME_NAME_SET.has(name)) {
638
+ out.push({ name, absIndex: i, def: t });
639
+ }
640
+ }
641
+ return out;
642
+ }
643
+ function fingerprintRuntimeGroup(mod) {
644
+ const members = extractRuntimeGroup(mod);
645
+ return {
646
+ abiVersion: RUNTIME_RECGROUP_ABI_VERSION,
647
+ hash: canonicalHashOfTypeGroup(
648
+ members.map((m2) => m2.def),
649
+ members.map((m2) => m2.absIndex)
650
+ ),
651
+ members: members.map((m2) => m2.name),
652
+ count: members.length
653
+ };
654
+ }
655
+ async function compile(source, options) {
656
+ return withImportObject(await compileSource(source, options));
657
+ }
658
+ function withImportObject(result) {
659
+ let cached;
660
+ Object.defineProperty(result, "importObject", {
661
+ enumerable: true,
662
+ configurable: true,
663
+ get() {
664
+ if (cached) return cached;
665
+ if (!result.success || result.imports.length === 0) {
666
+ cached = {};
667
+ return cached;
668
+ }
669
+ const built = buildImports(result.imports, void 0, result.stringPool);
670
+ cached = {
671
+ env: built.env,
672
+ "wasm:js-string": built["wasm:js-string"],
673
+ string_constants: built.string_constants
674
+ };
675
+ if (built.setExports) {
676
+ Object.defineProperty(cached, "__setExports", {
677
+ value: built.setExports,
678
+ enumerable: false,
679
+ configurable: true
680
+ });
681
+ }
682
+ return cached;
683
+ }
684
+ });
685
+ return result;
686
+ }
687
+ async function compileMulti(files, entryFile, options) {
688
+ return withImportObject(await compileMultiSource(files, entryFile, options));
689
+ }
690
+ async function compileFiles(entryPath, options) {
691
+ return withImportObject(await compileFilesSource(entryPath, options));
692
+ }
693
+ async function compileToWat(source) {
694
+ const result = await compileSource(source, { emitWat: true });
695
+ return result.wat;
696
+ }
697
+ function compileToObject(source, options) {
698
+ return compileToObjectSource(source, options);
699
+ }
700
+ async function compileProject(entryFile, options) {
701
+ const resolvedEntry = path.resolve(entryFile);
702
+ const rootDir = path.dirname(resolvedEntry);
703
+ const isJs = /\.[cm]?js$/.test(resolvedEntry);
704
+ const effectiveOptions = isJs && !options?.allowJs ? { ...options, allowJs: true } : options;
705
+ const resolver = new ModuleResolver(rootDir, effectiveOptions);
706
+ const allFiles = resolveAllImports(resolvedEntry, resolver);
707
+ const files = {};
708
+ for (const [filePath, content] of allFiles) {
709
+ const relPath = path.relative(rootDir, filePath);
710
+ const key = relPath.startsWith(".") ? relPath : `./${relPath}`;
711
+ files[key] = content;
712
+ }
713
+ const entryKey = `./${path.relative(rootDir, resolvedEntry)}`;
714
+ return withImportObject(await compileMultiSource(files, entryKey, effectiveOptions));
715
+ }
716
+ function createIncrementalCompiler(defaultOptions) {
717
+ const service = new IncrementalLanguageService();
718
+ return {
719
+ compile(source, options) {
720
+ return compileSource(source, { ...defaultOptions, ...options }, service);
721
+ },
722
+ dispose() {
723
+ service.dispose();
724
+ }
725
+ };
726
+ }
727
+ export {
728
+ ModuleResolver,
729
+ RUNTIME_RECGROUP_ABI_VERSION,
730
+ RUNTIME_RECGROUP_TYPE_NAMES,
731
+ buildImports,
732
+ j as buildStringConstants,
733
+ k as buildWasiPolyfill,
734
+ canonicalHashOfTypeGroup,
735
+ l as checkPolicy,
736
+ compile,
737
+ m as compileAndInstantiate,
738
+ compileFiles,
739
+ compileMulti,
740
+ compileProject,
741
+ compileToObject,
742
+ compileToWat,
743
+ createIncrementalCompiler,
744
+ extractRuntimeGroup,
745
+ fingerprintRuntimeGroup,
746
+ n as generateWit,
747
+ getBarePackageName,
748
+ getEntryExportNames,
749
+ o as instantiateWasm,
750
+ p as instantiateWasmStreaming,
751
+ q as jsString,
752
+ s as preloadLibFiles,
753
+ resolveAllImports,
754
+ treeshake
755
+ };