wasm-idle 1.1.0 → 1.2.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 (197) hide show
  1. package/README.md +267 -112
  2. package/dist/container/bash-profile.d.ts +80 -0
  3. package/dist/container/bash-profile.js +328 -0
  4. package/dist/container/binding.d.ts +39 -0
  5. package/dist/container/binding.js +331 -0
  6. package/dist/container/clang-runtime.d.ts +42 -0
  7. package/dist/container/clang-runtime.js +561 -0
  8. package/dist/container/client.d.ts +14 -0
  9. package/dist/container/client.js +185 -0
  10. package/dist/container/explorer.d.ts +31 -0
  11. package/dist/container/explorer.js +112 -0
  12. package/dist/container/index.d.ts +11 -0
  13. package/dist/container/index.js +11 -0
  14. package/dist/container/owner-worker.d.ts +19 -0
  15. package/dist/container/owner-worker.js +464 -0
  16. package/dist/container/protocol.d.ts +84 -0
  17. package/dist/container/protocol.js +28 -0
  18. package/dist/container/runtime-adapter.d.ts +75 -0
  19. package/dist/container/runtime-adapter.js +537 -0
  20. package/dist/container/shell-control.d.ts +49 -0
  21. package/dist/container/shell-control.js +360 -0
  22. package/dist/container/workspace.d.ts +12 -0
  23. package/dist/container/workspace.js +29 -0
  24. package/dist/index.d.ts +1 -1
  25. package/dist/playground/assetBridge.js +111 -33
  26. package/dist/playground/assets.d.ts +10 -2
  27. package/dist/playground/c3.js +22 -0
  28. package/dist/playground/c3Memory.d.ts +19 -0
  29. package/dist/playground/c3Memory.js +117 -0
  30. package/dist/playground/cachedClang.js +143 -34
  31. package/dist/playground/clang.d.ts +5 -1
  32. package/dist/playground/clang.js +61 -5
  33. package/dist/playground/clangAssetIntegrity.js +6 -6
  34. package/dist/playground/clangWorkerProtocol.d.ts +12 -1
  35. package/dist/playground/cobol.d.ts +3 -0
  36. package/dist/playground/cobol.js +10 -1
  37. package/dist/playground/commonlispAssets.d.ts +2 -2
  38. package/dist/playground/compiledArtifactCache.d.ts +3 -1
  39. package/dist/playground/compiledArtifactCache.js +31 -5
  40. package/dist/playground/compiledLldb.d.ts +41 -0
  41. package/dist/playground/compiledLldb.js +167 -0
  42. package/dist/playground/d.d.ts +10 -2
  43. package/dist/playground/d.js +161 -14
  44. package/dist/playground/debugDeadline.d.ts +12 -0
  45. package/dist/playground/debugDeadline.js +34 -0
  46. package/dist/playground/debugMailbox.d.ts +12 -0
  47. package/dist/playground/debugMailbox.js +77 -0
  48. package/dist/playground/dotnet.js +6 -8
  49. package/dist/playground/fortran.d.ts +3 -0
  50. package/dist/playground/fortran.js +10 -1
  51. package/dist/playground/go.js +23 -7
  52. package/dist/playground/goDebugInstrumentation.js +207 -97
  53. package/dist/playground/java-debug-FMHDKUN6.js +124 -0
  54. package/dist/playground/java-debug-notices.txt +492 -0
  55. package/dist/playground/java.d.ts +17 -1
  56. package/dist/playground/java.js +162 -5
  57. package/dist/playground/javaDebug.d.ts +62 -0
  58. package/dist/playground/javaDebug.js +305 -0
  59. package/dist/playground/javaDebugBridge.d.ts +6 -0
  60. package/dist/playground/javaDebugBridge.js +250 -0
  61. package/dist/playground/javaDebugSource.d.ts +9 -0
  62. package/dist/playground/javaDebugSource.js +4 -0
  63. package/dist/playground/javaRuntimeErrors.d.ts +9 -0
  64. package/dist/playground/javaRuntimeErrors.js +93 -0
  65. package/dist/playground/lean4Assets.d.ts +1 -25
  66. package/dist/playground/lean4Assets.js +2 -123
  67. package/dist/playground/lfortran.js +10 -0
  68. package/dist/playground/lfortranAssets.d.ts +2 -2
  69. package/dist/playground/lldbSession.d.ts +23 -2
  70. package/dist/playground/lldbSession.js +278 -58
  71. package/dist/playground/lldbSourcePath.d.ts +2 -0
  72. package/dist/playground/lldbSourcePath.js +9 -0
  73. package/dist/playground/lua.d.ts +20 -1
  74. package/dist/playground/lua.js +190 -8
  75. package/dist/playground/luaDebug.d.ts +1 -0
  76. package/dist/playground/luaDebug.js +1 -0
  77. package/dist/playground/nim.d.ts +1 -0
  78. package/dist/playground/nim.js +30 -0
  79. package/dist/playground/objectivec.d.ts +10 -6
  80. package/dist/playground/objectivec.js +85 -10
  81. package/dist/playground/options.d.ts +12 -4
  82. package/dist/playground/python.d.ts +8 -1
  83. package/dist/playground/python.js +89 -6
  84. package/dist/playground/ruby.d.ts +10 -1
  85. package/dist/playground/ruby.js +66 -1
  86. package/dist/playground/rubyDebug.d.ts +21 -0
  87. package/dist/playground/rubyDebug.js +128 -0
  88. package/dist/playground/runtimeAssetCache.d.ts +1 -32
  89. package/dist/playground/runtimeAssetCache.js +1 -193
  90. package/dist/playground/runtimeSession.d.ts +2 -2
  91. package/dist/playground/runtimeSession.js +11 -11
  92. package/dist/playground/rust.d.ts +10 -0
  93. package/dist/playground/rust.js +230 -51
  94. package/dist/playground/sandbox.d.ts +8 -1
  95. package/dist/playground/staticStdinRing.d.ts +2 -1
  96. package/dist/playground/staticStdinRing.js +21 -10
  97. package/dist/playground/staticWorkerRuntime.d.ts +15 -1
  98. package/dist/playground/staticWorkerRuntime.js +127 -19
  99. package/dist/playground/stdinBuffer.d.ts +1 -1
  100. package/dist/playground/stdinBuffer.js +19 -2
  101. package/dist/playground/tinygo.d.ts +3 -0
  102. package/dist/playground/tinygo.js +88 -6
  103. package/dist/playground/typescript.js +8 -0
  104. package/dist/playground/v.d.ts +6 -2
  105. package/dist/playground/v.js +76 -13
  106. package/dist/playground/wasmAwkVersion.d.ts +13 -13
  107. package/dist/playground/wasmAwkVersion.js +4 -4
  108. package/dist/playground/wasmBqnVersion.d.ts +2 -2
  109. package/dist/playground/wasmBqnVersion.js +2 -2
  110. package/dist/playground/wasmC3Version.d.ts +2 -2
  111. package/dist/playground/wasmC3Version.js +2 -2
  112. package/dist/playground/wasmClojureScriptVersion.d.ts +2 -2
  113. package/dist/playground/wasmClojureScriptVersion.js +2 -2
  114. package/dist/playground/wasmCommonLispVersion.d.ts +2 -2
  115. package/dist/playground/wasmCommonLispVersion.js +2 -2
  116. package/dist/playground/wasmDIntegrity.d.ts +5 -5
  117. package/dist/playground/wasmDIntegrity.js +5 -5
  118. package/dist/playground/wasmDVersion.d.ts +16 -1
  119. package/dist/playground/wasmDVersion.js +16 -1
  120. package/dist/playground/wasmDebugVersion.d.ts +1 -1
  121. package/dist/playground/wasmDebugVersion.js +1 -1
  122. package/dist/playground/wasmForthVersion.d.ts +2 -2
  123. package/dist/playground/wasmForthVersion.js +2 -2
  124. package/dist/playground/wasmGleamVersion.d.ts +2 -2
  125. package/dist/playground/wasmGleamVersion.js +2 -2
  126. package/dist/playground/wasmGrainVersion.d.ts +2 -2
  127. package/dist/playground/wasmGrainVersion.js +2 -2
  128. package/dist/playground/wasmJVersion.d.ts +2 -2
  129. package/dist/playground/wasmJVersion.js +2 -2
  130. package/dist/playground/wasmJanetVersion.js +2 -2
  131. package/dist/playground/wasmJuliaVersion.js +2 -2
  132. package/dist/playground/wasmLean4Version.d.ts +1 -110
  133. package/dist/playground/wasmLean4Version.js +2 -111
  134. package/dist/playground/wasmLfortranVersion.d.ts +2 -2
  135. package/dist/playground/wasmLfortranVersion.js +2 -2
  136. package/dist/playground/wasmLuaVersion.d.ts +1 -1
  137. package/dist/playground/wasmLuaVersion.js +1 -1
  138. package/dist/playground/wasmNimVersion.d.ts +9 -9
  139. package/dist/playground/wasmNimVersion.js +6 -6
  140. package/dist/playground/wasmOcamlVersion.d.ts +2 -2
  141. package/dist/playground/wasmOcamlVersion.js +1 -1
  142. package/dist/playground/wasmPascalVersion.js +2 -2
  143. package/dist/playground/wasmPerlVersion.js +2 -2
  144. package/dist/playground/wasmPrologVersion.d.ts +2 -2
  145. package/dist/playground/wasmPrologVersion.js +2 -2
  146. package/dist/playground/wasmReScriptVersion.d.ts +2 -2
  147. package/dist/playground/wasmReScriptVersion.js +2 -2
  148. package/dist/playground/wasmRustVersion.d.ts +26 -38
  149. package/dist/playground/wasmRustVersion.js +25 -40
  150. package/dist/playground/wasmTclVersion.d.ts +4 -4
  151. package/dist/playground/wasmTclVersion.js +2 -2
  152. package/dist/playground/wasmTinyGoVersion.d.ts +14 -14
  153. package/dist/playground/wasmTinyGoVersion.js +14 -14
  154. package/dist/playground/wasmTypeScriptVersion.d.ts +5 -5
  155. package/dist/playground/wasmTypeScriptVersion.js +5 -5
  156. package/dist/playground/worker/cachedClangCompile.js +52 -4
  157. package/dist/playground/worker/cachedClangExecute.js +6 -5
  158. package/dist/playground/worker/clang.js +33 -11
  159. package/dist/playground/worker/cobol.js +3 -3
  160. package/dist/playground/worker/d.js +63 -7
  161. package/dist/playground/worker/fortran.js +3 -3
  162. package/dist/playground/worker/go.js +16 -5
  163. package/dist/playground/worker/haskell.js +10 -7
  164. package/dist/playground/worker/java.js +263 -45
  165. package/dist/playground/worker/javaDebugEvaluator.d.ts +16 -0
  166. package/dist/playground/worker/javaDebugEvaluator.js +44 -0
  167. package/dist/playground/worker/javaDebugRuntime.d.ts +24 -0
  168. package/dist/playground/worker/javaDebugRuntime.js +315 -0
  169. package/dist/playground/worker/javaOutput.d.ts +11 -0
  170. package/dist/playground/worker/javaOutput.js +54 -0
  171. package/dist/playground/worker/lua.js +15 -3
  172. package/dist/playground/worker/php.js +15 -9
  173. package/dist/playground/worker/python.js +102 -30
  174. package/dist/playground/worker/pythonExecution.js +17 -1
  175. package/dist/playground/worker/pythonWorkspaceFiles.d.ts +6 -0
  176. package/dist/playground/worker/pythonWorkspaceFiles.js +26 -0
  177. package/dist/playground/worker/pythonWorkspaceImports.d.ts +4 -0
  178. package/dist/playground/worker/pythonWorkspaceImports.js +129 -0
  179. package/dist/playground/worker/r.js +13 -6
  180. package/dist/playground/worker/ruby.js +13 -0
  181. package/dist/playground/worker/rubyDebugPreview.d.ts +2 -0
  182. package/dist/playground/worker/rubyDebugPreview.js +80 -0
  183. package/dist/playground/worker/rubyTracepoint.d.ts +16 -0
  184. package/dist/playground/worker/rubyTracepoint.js +162 -0
  185. package/dist/playground/worker/rust.js +38 -3
  186. package/dist/playground/worker/sqlite.js +4 -3
  187. package/dist/playground/worker/tinygo.js +11 -3
  188. package/dist/playground/worker/typescript.js +2 -3
  189. package/dist/playground/worker/v.js +32 -7
  190. package/dist/playground/worker/wat.js +2 -3
  191. package/dist/playground/worker/zig.js +20 -6
  192. package/dist/playground/workerSession.d.ts +1 -28
  193. package/dist/playground/workerSession.js +1 -164
  194. package/dist/playground/zig.js +4 -0
  195. package/package.json +66 -15
  196. package/dist/playground/worker/python.runtime.cases.d.ts +0 -1
  197. package/dist/playground/worker/python.runtime.cases.js +0 -374
@@ -1,15 +1,19 @@
1
1
  import { resolveDRuntimeAssetConfig } from './assets';
2
- import { BusyError, CancelledError, createRuntimeAssetsKey, DEFAULT_WORKSPACE_LIMITS, DiagnosticLimitError, OutputLimitError, RuntimeConfigurationError, TimeoutError, resolveExecutionLimits, validateExecutionWorkspace } from '@wasm-idle/core';
2
+ import { CompiledLldbSandbox } from './compiledLldb';
3
+ import { BusyError, CancelledError, createRuntimeAssetsKey, createRuntimeWasmModuleService, DEFAULT_WORKSPACE_LIMITS, DiagnosticLimitError, OutputLimitError, RuntimeConfigurationError, RuntimeAssetCache, TimeoutError, resolveExecutionLimits, validateExecutionWorkspace } from '@wasm-idle/core';
3
4
  import { resolveSandboxExecutionArgs } from './options';
4
5
  import { flushBufferedEof, flushQueuedStdin, resetBufferedStdin } from './stdinBuffer';
5
- import { createWasmIdleSharedBuffer } from './sharedBuffer';
6
+ import { createWasmIdleSharedBuffer, requireSharedArrayBuffer } from './sharedBuffer';
6
7
  import { WorkerSession } from './workerSession';
7
8
  import { reportWorkerInputReady, reportWorkerProgress } from './workerProgress';
9
+ import { WASM_D_COMPILER_PROFILE } from './wasmDVersion';
10
+ import { WASM_D_OUTER_ASSET_RECEIPTS } from './wasmDIntegrity';
8
11
  const OUTPUT_ENCODER = new TextEncoder();
9
12
  const abortReason = (signal, phase) => signal.reason !== undefined
10
13
  ? signal.reason
11
14
  : new DOMException(phase === 'startup' ? 'D runtime startup aborted' : 'D execution aborted', 'AbortError');
12
- class D {
15
+ class D extends CompiledLldbSandbox {
16
+ lldbEntryFunction = '_Dmain';
13
17
  output = null;
14
18
  worker = null;
15
19
  buffer = createWasmIdleSharedBuffer(1024);
@@ -26,7 +30,10 @@ class D {
26
30
  waitingForInput = false;
27
31
  pendingEof = false;
28
32
  activeOperation = null;
33
+ lldbAssets = {};
29
34
  disposed = false;
35
+ runtimeAssetCache = new RuntimeAssetCache();
36
+ ownsRuntimeAssetCache = true;
30
37
  disposePromise = null;
31
38
  disposeCancellation = new CancelledError('D sandbox disposed', {
32
39
  phase: 'dispose',
@@ -183,10 +190,11 @@ class D {
183
190
  if (!this.isOperationActive(operation))
184
191
  return;
185
192
  let timeout;
186
- operation.cleanups.push(() => {
193
+ const cancelTimeout = () => {
187
194
  if (timeout !== undefined)
188
195
  clearTimeout(timeout);
189
- });
196
+ };
197
+ operation.cleanups.push(cancelTimeout);
190
198
  try {
191
199
  timeout = setTimeout(() => {
192
200
  if (!this.isOperationActive(operation))
@@ -204,6 +212,7 @@ class D {
204
212
  catch (error) {
205
213
  this.cancelOperation(operation, error);
206
214
  }
215
+ return cancelTimeout;
207
216
  }
208
217
  resetExplicitStdinState() {
209
218
  this.pendingInput = [];
@@ -234,6 +243,14 @@ class D {
234
243
  let limits;
235
244
  let unbindPreSessionAbort = () => undefined;
236
245
  try {
246
+ // Ordinary D execution only needs its compiler bundle. Keep debugger settings
247
+ // across worker retirement, but resolve their URLs only for an LLDB run.
248
+ const assets = typeof runtimeAssets === 'string' ? { rootUrl: runtimeAssets } : runtimeAssets;
249
+ this.lldbAssets = {
250
+ rootUrl: assets.rootUrl,
251
+ debug: assets.debug ? { ...assets.debug } : undefined,
252
+ persistentCache: options.persistentCache ?? assets.persistentCache
253
+ };
237
254
  signal = options.signal;
238
255
  unbindPreSessionAbort = this.bindPreSessionAbort(activeOperation, signal);
239
256
  if (!this.isOperationActive(activeOperation) || signal?.aborted) {
@@ -357,6 +374,10 @@ class D {
357
374
  });
358
375
  }
359
376
  write(input) {
377
+ if (this.lldbSession) {
378
+ void this.lldbSession.write(input);
379
+ return;
380
+ }
360
381
  if (this.disposed)
361
382
  return;
362
383
  this.pendingInput.push(input);
@@ -364,6 +385,10 @@ class D {
364
385
  this.flushPendingInput();
365
386
  }
366
387
  eof() {
388
+ if (this.lldbSession) {
389
+ void this.lldbSession.eof();
390
+ return;
391
+ }
367
392
  if (this.disposed)
368
393
  return;
369
394
  this.pendingEof = true;
@@ -382,7 +407,20 @@ class D {
382
407
  this.waitingForInput = false;
383
408
  }
384
409
  }
385
- run(code, prepare, _log = true, _prog, args = [], options = {}) {
410
+ async run(code, prepare, log = true, progress, args = [], options = {}) {
411
+ if (!this.worker && this.moduleUrl && this.outerIntegrity && !this.disposed) {
412
+ await this.load({
413
+ ...this.lldbAssets,
414
+ d: {
415
+ moduleUrl: this.moduleUrl,
416
+ manifestUrl: this.manifestUrl,
417
+ integrity: this.outerIntegrity
418
+ }
419
+ }, '', log, [], options, progress);
420
+ }
421
+ return this.runLoaded(code, prepare, log, progress, args, options);
422
+ }
423
+ runLoaded(code, prepare, _log = true, _prog, args = [], options = {}) {
386
424
  let activeOperation;
387
425
  try {
388
426
  activeOperation = this.beginOperation('execute');
@@ -396,17 +434,30 @@ class D {
396
434
  }
397
435
  let signal;
398
436
  let unbindPreSessionAbort = () => undefined;
437
+ let compileArgs;
399
438
  let programArgs;
400
439
  let limits;
401
440
  let workspace;
402
441
  let stdin;
442
+ const debugMode = options.debugMode || (options.debug ? 'trace' : 'none');
403
443
  try {
444
+ if (debugMode !== 'none' && debugMode !== 'lldb') {
445
+ throw new Error('D debugging requires debugMode: lldb.');
446
+ }
447
+ if (debugMode === 'lldb') {
448
+ requireSharedArrayBuffer('D debugging');
449
+ if (options.workspaceFiles?.some((file) => file.path !== options.activePath)) {
450
+ throw new Error('The packaged D compiler supports one source file per LLDB session.');
451
+ }
452
+ this.configureLldb(this.lldbAssets, options);
453
+ this.prepareLldb(options, 'main.d');
454
+ }
404
455
  signal = options.signal;
405
456
  unbindPreSessionAbort = this.bindPreSessionAbort(activeOperation, signal);
406
457
  if (!this.isOperationActive(activeOperation)) {
407
458
  return Promise.reject(this.releaseBeforeSession(activeOperation, 'D execution cancelled'));
408
459
  }
409
- programArgs = resolveSandboxExecutionArgs('D', args, options).programArgs;
460
+ ({ compileArgs, programArgs } = resolveSandboxExecutionArgs('D', args, options));
410
461
  limits = resolveExecutionLimits(options.limits);
411
462
  const workspaceFiles = options.workspaceFiles ?? [];
412
463
  const activePath = options.activePath ?? 'main.d';
@@ -440,18 +491,19 @@ class D {
440
491
  let outputBytes = 0;
441
492
  const operation = this.workerSession.beginRun(worker, reject);
442
493
  const timeoutMs = Math.min(2_147_483_647, limits.compileTimeoutMs + limits.runTimeoutMs);
443
- this.bindOperationTimeout(activeOperation, timeoutMs);
494
+ const cancelCompileTimeout = this.bindOperationTimeout(activeOperation, timeoutMs);
444
495
  this.bindAbortSignal(activeOperation, signal);
445
496
  if (!this.isOperationActive(activeOperation))
446
497
  return;
447
498
  let handler;
499
+ let handedOff = false;
448
500
  const ownsRun = () => this.isOperationActive(activeOperation) &&
449
- this.worker === worker &&
450
- worker.onmessage === handler &&
501
+ (handedOff || (this.worker === worker && worker.onmessage === handler)) &&
451
502
  _uid === this.uid;
452
503
  const claimRun = () => {
453
504
  if (!ownsRun())
454
505
  return false;
506
+ this.lldbPending = false;
455
507
  this.finishExplicitStdin(activeOperation);
456
508
  this.elapse = Date.now() - this.begin;
457
509
  this.exit = true;
@@ -467,6 +519,12 @@ class D {
467
519
  catch {
468
520
  // Handler cleanup must not replace the operation result.
469
521
  }
522
+ // Only immutable compiler modules survive an execution. Retire the worker, its
523
+ // Emscripten heaps, filesystem and compiled guest before the next run. A
524
+ // prepare-only call has executed no guest; keep its compiled artifact for
525
+ // the following execution instead of compiling the same source twice.
526
+ if (!prepare)
527
+ this.workerSession.reset();
470
528
  return true;
471
529
  };
472
530
  const failRun = (error, disposeWorker = false) => {
@@ -484,7 +542,34 @@ class D {
484
542
  if (!ownsRun())
485
543
  return;
486
544
  try {
487
- const { output, results, error, buffer, diagnostic, progress } = event.data;
545
+ const { output, results, error, buffer, diagnostic, progress, lldbArtifact } = event.data;
546
+ if (lldbArtifact) {
547
+ cancelCompileTimeout?.();
548
+ handedOff = true;
549
+ const completion = this.startLldb(lldbArtifact, options, programArgs, this.pendingInput, this.pendingEof, (output) => {
550
+ if (!ownsRun())
551
+ return;
552
+ outputBytes += OUTPUT_ENCODER.encode(output).byteLength;
553
+ if (outputBytes > limits.maxOutputBytes) {
554
+ failRun(new OutputLimitError(`D output exceeded ${limits.maxOutputBytes} bytes`, {
555
+ actual: outputBytes,
556
+ limit: limits.maxOutputBytes,
557
+ phase: 'execute',
558
+ runtimeId: 'D'
559
+ }), true);
560
+ return;
561
+ }
562
+ this.output?.(output);
563
+ });
564
+ this.pendingEof = false;
565
+ if (this.workerSession.release(worker))
566
+ delete this.worker;
567
+ void completion.then((result) => {
568
+ if (claimRun())
569
+ resolve(result);
570
+ }, (error) => failRun(error));
571
+ return;
572
+ }
488
573
  if (buffer && !hasExplicitStdin) {
489
574
  this.waitingForInput = true;
490
575
  if (!prepare) {
@@ -549,15 +634,60 @@ class D {
549
634
  return;
550
635
  this.begin = Date.now();
551
636
  try {
552
- worker.postMessage({
637
+ const modulePorts = {};
638
+ const profile = WASM_D_COMPILER_PROFILE;
639
+ // Custom runtime bundles keep their independent compiler path. A worker
640
+ // cannot choose the host's receipt, loader profile or authorization key.
641
+ if (typeof MessageChannel === 'function' &&
642
+ this.outerIntegrity?.['runtime/runtime-manifest.v1.json'].sha256 ===
643
+ profile.manifestSha256 &&
644
+ this.outerIntegrity?.['index.js'].sha256 ===
645
+ WASM_D_OUTER_ASSET_RECEIPTS['index.js'].sha256) {
646
+ for (const kind of ['ldc2', 'lld']) {
647
+ const service = createRuntimeWasmModuleService({
648
+ cache: this.runtimeAssetCache,
649
+ receipt: profile[kind],
650
+ identity: JSON.stringify([
651
+ 'wasm-d-verified-compiler-v1',
652
+ this.activeDAssetsKey,
653
+ profile.version,
654
+ profile.manifestSha256,
655
+ kind,
656
+ ...(kind === 'lld'
657
+ ? [
658
+ profile.lldProfile,
659
+ profile.lldLoaderSha256,
660
+ profile.lldDataSha256
661
+ ]
662
+ : [])
663
+ ]),
664
+ compileKey: kind === 'lld'
665
+ ? 'emscripten-lld16-instantiate-v1'
666
+ : 'ldc2-wasi-preview1-v1',
667
+ maxAssetBytes: limits.maxAssetBytes,
668
+ signal
669
+ });
670
+ activeOperation.cleanups.push(() => service.close());
671
+ modulePorts[kind] = service.port;
672
+ }
673
+ }
674
+ const message = {
553
675
  code,
554
676
  prepare,
555
677
  buffer: this.buffer,
678
+ compileArgs,
556
679
  args: programArgs,
557
680
  stdin,
558
681
  fileName: workspace.activePath,
559
- log: _log
560
- });
682
+ log: _log,
683
+ debugMode,
684
+ ...(modulePorts.ldc2 ? { compilerModulePorts: modulePorts } : {})
685
+ };
686
+ const transfers = Object.values(modulePorts);
687
+ if (transfers.length)
688
+ worker.postMessage(message, transfers);
689
+ else
690
+ worker.postMessage(message);
561
691
  }
562
692
  catch (error) {
563
693
  failRun(error, true);
@@ -574,6 +704,7 @@ class D {
574
704
  cancelOperation(operation, reason) {
575
705
  if (!this.isOperationActive(operation))
576
706
  return;
707
+ void this.stopLldb();
577
708
  operation.cancelled = true;
578
709
  operation.cancellationReason = reason;
579
710
  this.activeOperation = null;
@@ -585,10 +716,22 @@ class D {
585
716
  this.workerSession.terminate(reason);
586
717
  this.cleanupOperation(operation);
587
718
  }
719
+ setRuntimeAssetCache(cache) {
720
+ if (cache === this.runtimeAssetCache)
721
+ return;
722
+ if (this.worker || this.activeOperation || this.disposed) {
723
+ throw new Error('Cannot replace the D runtime asset cache while its worker is active or disposed');
724
+ }
725
+ if (this.ownsRuntimeAssetCache)
726
+ this.runtimeAssetCache.dispose();
727
+ this.runtimeAssetCache = cache;
728
+ this.ownsRuntimeAssetCache = false;
729
+ }
588
730
  kill() {
589
731
  this.terminate();
590
732
  }
591
733
  terminate(reason = 'Process terminated') {
734
+ void this.stopLldb();
592
735
  if (this.disposed)
593
736
  return;
594
737
  const activeOperation = this.activeOperation;
@@ -619,7 +762,10 @@ class D {
619
762
  dispose() {
620
763
  if (this.disposePromise)
621
764
  return this.disposePromise;
765
+ void this.stopLldb();
622
766
  this.disposed = true;
767
+ if (this.ownsRuntimeAssetCache)
768
+ this.runtimeAssetCache.dispose();
623
769
  this.disposePromise = Promise.resolve();
624
770
  const activeOperation = this.activeOperation;
625
771
  delete this.worker;
@@ -627,6 +773,7 @@ class D {
627
773
  this.manifestUrl = '';
628
774
  this.outerIntegrity = undefined;
629
775
  this.activeDAssetsKey = '';
776
+ this.lldbAssets = {};
630
777
  this.output = null;
631
778
  this.oncompilerdiagnostic = undefined;
632
779
  this.resetExplicitStdinState();
@@ -0,0 +1,12 @@
1
+ /** Charges execution time while allowing a person to inspect a paused VM. */
2
+ export declare class DebugDeadline {
3
+ private readonly expire;
4
+ private remaining;
5
+ private started;
6
+ private timer?;
7
+ private closed;
8
+ constructor(timeoutMs: number, expire: () => void);
9
+ pause(): void;
10
+ resume(): void;
11
+ dispose(): void;
12
+ }
@@ -0,0 +1,34 @@
1
+ /** Charges execution time while allowing a person to inspect a paused VM. */
2
+ export class DebugDeadline {
3
+ expire;
4
+ remaining;
5
+ started = 0;
6
+ timer;
7
+ closed = false;
8
+ constructor(timeoutMs, expire) {
9
+ this.expire = expire;
10
+ this.remaining = timeoutMs;
11
+ this.resume();
12
+ }
13
+ pause() {
14
+ if (this.timer === undefined)
15
+ return;
16
+ clearTimeout(this.timer);
17
+ this.timer = undefined;
18
+ this.remaining = Math.max(0, this.remaining - (Date.now() - this.started));
19
+ }
20
+ resume() {
21
+ if (this.closed || this.timer !== undefined)
22
+ return;
23
+ this.started = Date.now();
24
+ this.timer = setTimeout(() => {
25
+ this.timer = undefined;
26
+ this.closed = true;
27
+ this.expire();
28
+ }, this.remaining);
29
+ }
30
+ dispose() {
31
+ this.pause();
32
+ this.closed = true;
33
+ }
34
+ }
@@ -0,0 +1,12 @@
1
+ /** One published command plus a bounded UI queue; consumption wakes the next producer write. */
2
+ export declare class DebugCommandMailbox {
3
+ private readonly label;
4
+ readonly buffer: import("./sharedBuffer").WasmIdleSharedBuffer;
5
+ private queued;
6
+ constructor(label?: string);
7
+ reset(): void;
8
+ send(command: string): void;
9
+ flush(): void;
10
+ close(): void;
11
+ }
12
+ export declare function readDebugCommand(buffer: SharedArrayBuffer, blocking: boolean, onConsumed: () => void, label?: string): string | null;
@@ -0,0 +1,77 @@
1
+ import { createWasmIdleSharedBuffer, isSharedBufferBackedView } from './sharedBuffer';
2
+ const HEADER_BYTES = 16;
3
+ const COMMAND_BYTES = 65_536;
4
+ const MAX_QUEUED_COMMANDS = 64;
5
+ const encoder = new TextEncoder();
6
+ const decoder = new TextDecoder('utf-8', { fatal: true });
7
+ /** One published command plus a bounded UI queue; consumption wakes the next producer write. */
8
+ export class DebugCommandMailbox {
9
+ label;
10
+ buffer = createWasmIdleSharedBuffer(HEADER_BYTES + COMMAND_BYTES);
11
+ queued = [];
12
+ constructor(label = 'Lua') {
13
+ this.label = label;
14
+ }
15
+ reset() {
16
+ this.queued = [];
17
+ new Int32Array(this.buffer, 0, 4).fill(0);
18
+ }
19
+ send(command) {
20
+ const header = new Int32Array(this.buffer, 0, 4);
21
+ if (!isSharedBufferBackedView(header))
22
+ throw new Error(`${this.label} debugging requires SharedArrayBuffer.`);
23
+ if (Atomics.load(header, 2))
24
+ throw new Error(`${this.label} debugger is closed.`);
25
+ const bytes = encoder.encode(command);
26
+ if (!bytes.length || bytes.length > COMMAND_BYTES) {
27
+ throw new RangeError(`${this.label} debugger commands must contain 1–${COMMAND_BYTES} UTF-8 bytes.`);
28
+ }
29
+ if (this.queued.length >= MAX_QUEUED_COMMANDS)
30
+ throw new Error(`${this.label} debugger command queue is full.`);
31
+ this.queued.push(bytes);
32
+ this.flush();
33
+ }
34
+ flush() {
35
+ const header = new Int32Array(this.buffer, 0, 4);
36
+ if (!this.queued.length || Atomics.load(header, 1) !== 0 || Atomics.load(header, 2))
37
+ return;
38
+ const bytes = this.queued.shift();
39
+ new Uint8Array(this.buffer, HEADER_BYTES, bytes.length).set(bytes);
40
+ Atomics.store(header, 1, bytes.length);
41
+ Atomics.add(header, 0, 1);
42
+ Atomics.notify(header, 0);
43
+ }
44
+ close() {
45
+ this.queued = [];
46
+ const header = new Int32Array(this.buffer, 0, 4);
47
+ if (!isSharedBufferBackedView(header))
48
+ return;
49
+ Atomics.store(header, 2, 1);
50
+ Atomics.add(header, 0, 1);
51
+ Atomics.notify(header, 0);
52
+ }
53
+ }
54
+ export function readDebugCommand(buffer, blocking, onConsumed, label = 'Lua') {
55
+ const header = new Int32Array(buffer, 0, 4);
56
+ if (!isSharedBufferBackedView(header) || buffer.byteLength !== HEADER_BYTES + COMMAND_BYTES) {
57
+ throw new Error(`Invalid ${label} debugger command buffer.`);
58
+ }
59
+ for (;;) {
60
+ const sequence = Atomics.load(header, 0);
61
+ if (Atomics.load(header, 2))
62
+ return null;
63
+ const size = Atomics.load(header, 1);
64
+ if (size < 0 || size > COMMAND_BYTES)
65
+ throw new Error(`Invalid ${label} debugger command length.`);
66
+ if (size) {
67
+ // Browser TextDecoder rejects shared views; copy while this slot is owned by the reader.
68
+ const command = decoder.decode(new Uint8Array(buffer, HEADER_BYTES, size).slice());
69
+ Atomics.store(header, 1, 0);
70
+ onConsumed();
71
+ return command;
72
+ }
73
+ if (!blocking)
74
+ return '';
75
+ Atomics.wait(header, 0, sequence);
76
+ }
77
+ }
@@ -495,15 +495,13 @@ class Dotnet {
495
495
  const hasExplicitStdin = !prepare && explicitStdin !== undefined;
496
496
  if (runUid !== this.uid)
497
497
  return '';
498
- if (!prepare &&
499
- !hasExplicitStdin &&
500
- this.pendingInput.length === 0 &&
501
- !this.pendingEof &&
502
- readsConsoleStdin(code)) {
498
+ if (!prepare && !hasExplicitStdin && !this.pendingEof && readsConsoleStdin(code)) {
503
499
  reportWorkerInputReady(progress, `${this.languageLabel} runtime ready for input`);
504
- if (runUid !== this.uid)
505
- return '';
506
- await new Promise((resolve) => this.stdinWaiters.push(resolve));
500
+ // The program receives stdin as one string, so wait for EOF instead of
501
+ // starting with only the input written before the first chunk arrived.
502
+ while (runUid === this.uid && !this.pendingEof) {
503
+ await new Promise((resolve) => this.stdinWaiters.push(resolve));
504
+ }
507
505
  }
508
506
  if (runUid !== this.uid)
509
507
  return '';
@@ -1,4 +1,5 @@
1
1
  import { WorkerAssetBridge } from './assetBridge';
2
+ import type { RuntimeAssetCache } from './runtimeAssetCache';
2
3
  import { type PlaygroundRuntimeAssets } from './assets';
3
4
  import type { SandboxExecutionOptions } from './options';
4
5
  import type { Sandbox, SandboxProgress } from './sandbox';
@@ -18,6 +19,7 @@ declare class Fortran implements Sandbox {
18
19
  assetBridge: WorkerAssetBridge | null;
19
20
  activeFortranAssetsKey: string;
20
21
  private activeOperation;
22
+ private runtimeAssetCache?;
21
23
  private preserveOperationOnWorkerDispose;
22
24
  private disposed;
23
25
  private disposePromise;
@@ -25,6 +27,7 @@ declare class Fortran implements Sandbox {
25
27
  private readonly workerSession;
26
28
  private disposedConfigurationError;
27
29
  private requireOperationIdle;
30
+ setRuntimeAssetCache(cache: RuntimeAssetCache): void;
28
31
  private beginOperation;
29
32
  private isOperationActive;
30
33
  private releaseOperation;
@@ -37,6 +37,7 @@ class Fortran {
37
37
  assetBridge = null;
38
38
  activeFortranAssetsKey = '';
39
39
  activeOperation = null;
40
+ runtimeAssetCache;
40
41
  preserveOperationOnWorkerDispose = false;
41
42
  disposed = false;
42
43
  disposePromise = null;
@@ -88,6 +89,14 @@ class Fortran {
88
89
  });
89
90
  }
90
91
  }
92
+ setRuntimeAssetCache(cache) {
93
+ if (this.runtimeAssetCache === cache)
94
+ return;
95
+ if (this.disposed || this.worker || this.activeOperation) {
96
+ throw new RuntimeConfigurationError('Cannot replace an active Fortran compiler cache');
97
+ }
98
+ this.runtimeAssetCache = cache;
99
+ }
91
100
  beginOperation(phase) {
92
101
  this.requireOperationIdle();
93
102
  const operation = {
@@ -287,7 +296,7 @@ class Fortran {
287
296
  }
288
297
  let assetBridge;
289
298
  try {
290
- assetBridge = new WorkerAssetBridge(worker, 'clang', clangAssets, progress, limits.maxAssetBytes);
299
+ assetBridge = new WorkerAssetBridge(worker, 'clang', clangAssets, progress, limits.maxAssetBytes, false, this.runtimeAssetCache);
291
300
  assetBridge.setExecutionPersistentCache(options.persistentCache);
292
301
  }
293
302
  catch (error) {
@@ -224,7 +224,12 @@ class Go {
224
224
  resetBufferedStdin(this.buffer);
225
225
  }
226
226
  run(code, prepare, _log = true, _prog, args = [], options = {}) {
227
- if (options.debug)
227
+ const debugMode = options.debugMode ?? (options.debug ? 'trace' : 'none');
228
+ if (debugMode !== 'none' && debugMode !== 'trace') {
229
+ return Promise.reject(new Error('Go native debugging is unavailable: the current Wasm linker does not emit DWARF. Use trace mode.'));
230
+ }
231
+ const debugEnabled = debugMode === 'trace';
232
+ if (debugEnabled)
228
233
  requireSharedArrayBuffer('Go debugging');
229
234
  if (!this.exit) {
230
235
  const error = new BusyError('Go runtime already has an active execution', {
@@ -253,8 +258,8 @@ class Go {
253
258
  const { programArgs } = resolveSandboxExecutionArgs('GO', args, options);
254
259
  const target = options.goTarget || 'wasip1/wasm';
255
260
  const _uid = ++this.uid;
256
- this.setBreakpoints(options.debug ? [...(options.breakpoints || [])] : []);
257
- const hasExplicitStdin = !options.debug && options.stdin !== undefined;
261
+ this.setBreakpoints(debugEnabled ? [...(options.breakpoints || [])] : []);
262
+ const hasExplicitStdin = !debugEnabled && options.stdin !== undefined;
258
263
  this.activeExplicitStdinCleanup?.();
259
264
  let explicitStdinCleaned = false;
260
265
  const cleanupExplicitStdin = () => {
@@ -328,7 +333,7 @@ class Go {
328
333
  worker.onmessage = null;
329
334
  return;
330
335
  }
331
- const { output, results, error, buffer, diagnostic, progress, debugEvent, phase } = event.data;
336
+ const { output, results, error, fatal, buffer, diagnostic, progress, debugEvent, phase } = event.data;
332
337
  if (phase === 'compile' || phase === 'execute') {
333
338
  if (phase !== activePhase)
334
339
  armTimeout(phase);
@@ -342,8 +347,12 @@ class Go {
342
347
  reportWorkerProgress(_prog, progress);
343
348
  if (output)
344
349
  this.output(output);
350
+ // The worker compiles only the active source, which the compiler names main.go.
345
351
  if (diagnostic)
346
- this.oncompilerdiagnostic?.(diagnostic);
352
+ this.oncompilerdiagnostic?.(options.activePath &&
353
+ /(?:^|[\\/])main\.go$/u.test(diagnostic.fileName ?? '')
354
+ ? { ...diagnostic, fileName: options.activePath }
355
+ : diagnostic);
347
356
  if (debugEvent)
348
357
  this.ondebug?.(debugEvent);
349
358
  if (results) {
@@ -367,7 +376,14 @@ class Go {
367
376
  this.waitingForInput = false;
368
377
  this.pendingEof = false;
369
378
  this.workerSession.complete(operation);
370
- this.ondebug?.({ type: 'stop' });
379
+ if (fatal === true && this.worker === worker) {
380
+ // The worker abandoned a program that may still be running; terminating
381
+ // it stops that program, and disposal reports the debug stop.
382
+ this.workerSession.reset();
383
+ }
384
+ else {
385
+ this.ondebug?.({ type: 'stop' });
386
+ }
371
387
  reject(error);
372
388
  }
373
389
  };
@@ -404,7 +420,7 @@ class Go {
404
420
  args: programArgs,
405
421
  target,
406
422
  log: _log,
407
- debug: !!options.debug,
423
+ debug: debugEnabled,
408
424
  breakpoints: [...(options.breakpoints || [])],
409
425
  pauseOnEntry: !!options.pauseOnEntry,
410
426
  runtimeLimits: limits