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
@@ -8,7 +8,7 @@ import { resolveRustNonDebugResourceLimits } from './rustWorkerLimits';
8
8
  import { WASM_RUST_EXECUTABLE_GRAPH_PROFILE, WASM_RUST_RUNTIME_PROFILE } from './wasmRustVersion';
9
9
  import { WorkerSession } from './workerSession';
10
10
  import { reportWorkerInputReady, reportWorkerProgress } from './workerProgress';
11
- import { resolveExecutionLimits, resolveRuntimeAssetCacheOptions, TimeoutError } from '@wasm-idle/core';
11
+ import { createRuntimeAssetsKey, assertDebugBreakpointSupport, mergeDebugBreakpointLines, normalizeDebugSourceBreakpoints, RuntimeAssetCache, resolveExecutionLimits, resolveRuntimeAssetCacheOptions, serveRuntimeWasmModule, TimeoutError } from '@wasm-idle/core';
12
12
  const debugBreakpointBufferInts = 1028;
13
13
  const rustLldbSourcePath = '/workspace/main.rs';
14
14
  const outputEncoder = new TextEncoder();
@@ -54,6 +54,9 @@ class Rust {
54
54
  debugManifestUrl = '';
55
55
  debugManifestReceipt;
56
56
  executableGraphFingerprint = '';
57
+ runtimeAssetsKey = '';
58
+ runtimeAssetCache = new RuntimeAssetCache();
59
+ ownsRuntimeAssetCache = true;
57
60
  persistentCacheBaseline;
58
61
  oncompilerdiagnostic;
59
62
  waitingForInput = false;
@@ -84,6 +87,7 @@ class Rust {
84
87
  debugMode = 'none';
85
88
  activeDebugPhaseTimeout;
86
89
  lldbBreakpoints = new Map();
90
+ lldbBreakpointSpecifications = new Map();
87
91
  lldbEditorSourcePath = rustLldbSourcePath;
88
92
  disposeWorkerExecutableGraph(worker) {
89
93
  const executableGraph = this.workerExecutableGraphs.get(worker);
@@ -122,9 +126,10 @@ class Rust {
122
126
  let candidateLoadReject = null;
123
127
  let clearCandidateWait = () => { };
124
128
  try {
125
- const currentUrl = typeof window !== 'undefined' ? window.location.href : '';
129
+ const currentUrl = globalThis.location?.href ?? '';
126
130
  const persistentCache = resolveRuntimeAssetCacheOptions(typeof runtimeAssets === 'object' ? runtimeAssets.persistentCache : undefined, _options.persistentCache);
127
131
  const nextCompilerUrl = resolveRustCompilerUrl(runtimeAssets, currentUrl);
132
+ const nextRuntimeAssetsKey = createRuntimeAssetsKey(runtimeAssets) ?? '';
128
133
  const nextDebugModuleUrl = resolveRustDebugModuleUrl(runtimeAssets, currentUrl);
129
134
  const debugRuntime = resolveDebugRuntimeUrls(runtimeAssets, currentUrl);
130
135
  const nextDebugRuntimeBaseUrl = debugRuntime.baseUrl;
@@ -133,9 +138,8 @@ class Rust {
133
138
  const nextAssetPath = typeof runtimeAssets === 'string'
134
139
  ? runtimeAssets
135
140
  : runtimeAssets?.rootUrl ||
136
- (typeof window !== 'undefined'
137
- ? window.location.pathname.replace(/\/$/, '')
138
- : '');
141
+ globalThis.location?.pathname.replace(/\/$/, '') ||
142
+ '';
139
143
  if (!nextCompilerUrl) {
140
144
  throw new Error('Rust runtime is not configured. Set runtimeAssets.rust.compilerUrl.');
141
145
  }
@@ -174,6 +178,7 @@ class Rust {
174
178
  this.compilerUrl !== nextCompilerUrl ||
175
179
  this.debugModuleUrl !== nextDebugModuleUrl ||
176
180
  this.assetPath !== nextAssetPath ||
181
+ this.runtimeAssetsKey !== nextRuntimeAssetsKey ||
177
182
  this.executableGraphFingerprint !==
178
183
  WASM_RUST_EXECUTABLE_GRAPH_PROFILE.fingerprint;
179
184
  if (!needsWorkerReset) {
@@ -279,6 +284,7 @@ class Rust {
279
284
  this.waitingForInput = false;
280
285
  this.pendingEof = false;
281
286
  this.compilerUrl = nextCompilerUrl;
287
+ this.runtimeAssetsKey = nextRuntimeAssetsKey;
282
288
  this.persistentCacheBaseline = persistentCache;
283
289
  this.debugModuleUrl = nextDebugModuleUrl;
284
290
  this.assetPath = nextAssetPath;
@@ -353,6 +359,9 @@ class Rust {
353
359
  }
354
360
  run(code, prepare, _log = true, _prog, args = [], options = {}) {
355
361
  const debugMode = options.debugMode || (options.debug ? 'trace' : 'none');
362
+ if (debugMode !== 'none' && debugMode !== 'trace' && debugMode !== 'lldb') {
363
+ return Promise.reject(new Error(`Rust does not support ${debugMode} debugging.`));
364
+ }
356
365
  this.debugMode = debugMode;
357
366
  if (debugMode !== 'none')
358
367
  requireSharedArrayBuffer('Rust debugging');
@@ -367,11 +376,31 @@ class Rust {
367
376
  let limits;
368
377
  let nonDebugResourceLimits;
369
378
  let persistentCache;
379
+ let editorSourcePath;
380
+ let initialBreakpointSpecifications;
381
+ let lldbBreakpointLines = [];
370
382
  try {
371
383
  persistentCache = resolveRuntimeAssetCacheOptions(this.persistentCacheBaseline, options.persistentCache);
372
384
  limits = resolveExecutionLimits(options.limits);
373
385
  nonDebugResourceLimits =
374
386
  debugMode === 'none' ? resolveRustNonDebugResourceLimits(limits) : undefined;
387
+ editorSourcePath = normalizeRustLldbSourcePath(options.debugPath || options.activePath);
388
+ if (debugMode === 'lldb') {
389
+ const requestedLines = [...(options.breakpoints || [])];
390
+ for (const sourceBreakpoints of options.sourceBreakpoints || []) {
391
+ if (normalizeRustLldbSourcePath(sourceBreakpoints.sourcePath) ===
392
+ editorSourcePath) {
393
+ requestedLines.push(...sourceBreakpoints.lines);
394
+ if (sourceBreakpoints.breakpoints)
395
+ initialBreakpointSpecifications = normalizeDebugSourceBreakpoints(sourceBreakpoints.breakpoints);
396
+ }
397
+ }
398
+ lldbBreakpointLines = initialBreakpointSpecifications
399
+ ? initialBreakpointSpecifications
400
+ .filter((value) => value.enabled !== false)
401
+ .map((value) => value.line)
402
+ : normalizeRustBreakpointLines(requestedLines);
403
+ }
375
404
  }
376
405
  catch (error) {
377
406
  return reject(error);
@@ -379,10 +408,25 @@ class Rust {
379
408
  this.runActive = true;
380
409
  this.exit = false;
381
410
  let settled = false;
411
+ let terminating = false;
382
412
  let phaseTimeout;
383
413
  let phaseTimeoutState;
384
414
  let outputBytes = 0;
385
415
  let diagnosticCount = 0;
416
+ let moduleBroker;
417
+ const moduleServices = new Set();
418
+ const closeCompilerModuleBroker = () => {
419
+ if (moduleBroker) {
420
+ moduleBroker.port1.onmessage = null;
421
+ moduleBroker.port1.onmessageerror = null;
422
+ moduleBroker.port1.close();
423
+ moduleBroker.port2.close();
424
+ moduleBroker = undefined;
425
+ }
426
+ for (const service of moduleServices)
427
+ service.close();
428
+ moduleServices.clear();
429
+ };
386
430
  const clearScheduledPhaseTimeout = () => {
387
431
  if (phaseTimeout !== undefined)
388
432
  clearTimeout(phaseTimeout);
@@ -408,31 +452,7 @@ class Rust {
408
452
  runtimeId: 'RUST',
409
453
  timeoutMs: state.timeoutMs
410
454
  });
411
- const lldbSession = this.lldbSession;
412
- if (!lldbSession) {
413
- this.workerSession.terminate(timeoutError);
414
- return;
415
- }
416
- if (!this.workerSession.complete(operation))
417
- return;
418
- let disconnecting;
419
- try {
420
- disconnecting = Promise.resolve(lldbSession.disconnect());
421
- }
422
- catch {
423
- disconnecting = Promise.resolve();
424
- }
425
- void disconnecting
426
- .catch(() => undefined)
427
- .then(() => {
428
- if (this.lldbSession === lldbSession)
429
- this.lldbSession = undefined;
430
- this.lldbEditorSourcePath = rustLldbSourcePath;
431
- this.exit = true;
432
- this.waitingForInput = false;
433
- this.pendingEof = false;
434
- finishReject(timeoutError);
435
- });
455
+ terminateRun(timeoutError);
436
456
  }, state.remainingMs);
437
457
  };
438
458
  const debugPhaseTimeout = {
@@ -459,6 +479,7 @@ class Rust {
459
479
  return;
460
480
  settled = true;
461
481
  this.runActive = false;
482
+ closeCompilerModuleBroker();
462
483
  clearPhaseTimeout();
463
484
  if (this.activeDebugPhaseTimeout === debugPhaseTimeout) {
464
485
  this.activeDebugPhaseTimeout = undefined;
@@ -471,6 +492,7 @@ class Rust {
471
492
  return;
472
493
  settled = true;
473
494
  this.runActive = false;
495
+ closeCompilerModuleBroker();
474
496
  clearPhaseTimeout();
475
497
  if (this.activeDebugPhaseTimeout === debugPhaseTimeout) {
476
498
  this.activeDebugPhaseTimeout = undefined;
@@ -478,6 +500,48 @@ class Rust {
478
500
  options.signal?.removeEventListener('abort', abortRun);
479
501
  reject(reason);
480
502
  };
503
+ const terminateRun = (error) => {
504
+ if (settled || terminating)
505
+ return;
506
+ terminating = true;
507
+ clearPhaseTimeout();
508
+ const lldbSession = this.lldbSession;
509
+ if (!lldbSession) {
510
+ this.workerSession.terminate(error);
511
+ return;
512
+ }
513
+ if (!this.workerSession.complete(operation))
514
+ return;
515
+ let disconnecting;
516
+ try {
517
+ disconnecting = Promise.resolve(lldbSession.disconnect());
518
+ }
519
+ catch {
520
+ disconnecting = Promise.resolve();
521
+ }
522
+ void disconnecting
523
+ .catch(() => undefined)
524
+ .then(() => {
525
+ if (this.lldbSession === lldbSession)
526
+ this.lldbSession = undefined;
527
+ this.lldbEditorSourcePath = rustLldbSourcePath;
528
+ this.exit = true;
529
+ this.waitingForInput = false;
530
+ this.pendingEof = false;
531
+ finishReject(error);
532
+ });
533
+ };
534
+ const forwardOutput = (output) => {
535
+ if (settled || terminating)
536
+ return false;
537
+ outputBytes += outputEncoder.encode(output).byteLength;
538
+ if (outputBytes > limits.maxOutputBytes) {
539
+ terminateRun(new Error(`Rust output exceeded ${limits.maxOutputBytes} bytes`));
540
+ return false;
541
+ }
542
+ this.output(output);
543
+ return true;
544
+ };
481
545
  const abortRun = () => {
482
546
  try {
483
547
  const disconnecting = this.lldbSession?.disconnect();
@@ -518,19 +582,12 @@ class Rust {
518
582
  abortRun();
519
583
  return;
520
584
  }
521
- const editorSourcePath = normalizeRustLldbSourcePath(options.debugPath || options.activePath);
522
585
  this.lldbEditorSourcePath = editorSourcePath;
523
586
  this.lldbBreakpoints.clear();
524
- let lldbBreakpointLines = [];
587
+ this.lldbBreakpointSpecifications.clear();
525
588
  if (debugMode === 'lldb') {
526
- const requestedLines = [...(options.breakpoints || [])];
527
- for (const sourceBreakpoints of options.sourceBreakpoints || []) {
528
- if (normalizeRustLldbSourcePath(sourceBreakpoints.sourcePath) ===
529
- editorSourcePath) {
530
- requestedLines.push(...sourceBreakpoints.lines);
531
- }
532
- }
533
- lldbBreakpointLines = normalizeRustBreakpointLines(requestedLines);
589
+ if (initialBreakpointSpecifications)
590
+ this.lldbBreakpointSpecifications.set(rustLldbSourcePath, initialBreakpointSpecifications);
534
591
  this.lldbBreakpoints.set(rustLldbSourcePath, lldbBreakpointLines);
535
592
  }
536
593
  else {
@@ -553,14 +610,8 @@ class Rust {
553
610
  this.flushPendingInput();
554
611
  }
555
612
  reportWorkerProgress(_prog, progress);
556
- if (output) {
557
- outputBytes += outputEncoder.encode(String(output)).byteLength;
558
- if (outputBytes > limits.maxOutputBytes) {
559
- this.workerSession.terminate(new Error(`Rust output exceeded ${limits.maxOutputBytes} bytes`));
560
- return;
561
- }
562
- this.output(output);
563
- }
613
+ if (output && !forwardOutput(String(output)))
614
+ return;
564
615
  if (diagnostic) {
565
616
  diagnosticCount += 1;
566
617
  if (diagnosticCount > limits.maxDiagnostics) {
@@ -572,6 +623,7 @@ class Rust {
572
623
  if (debugEvent)
573
624
  forwardDebugEvent(debugEvent);
574
625
  if (lldbArtifact) {
626
+ closeCompilerModuleBroker();
575
627
  startPhaseTimeout('run', limits.runTimeoutMs);
576
628
  const compilerWorker = this.worker;
577
629
  const lldbSession = new LldbSandboxSession({
@@ -584,9 +636,17 @@ class Rust {
584
636
  breakpoints: lldbBreakpointLines,
585
637
  sourceBreakpoints: Array.from(this.lldbBreakpoints, ([sourcePath, lines]) => ({
586
638
  sourcePath,
587
- lines: [...lines]
639
+ lines: [...lines],
640
+ ...(this.lldbBreakpointSpecifications.has(sourcePath)
641
+ ? {
642
+ breakpoints: normalizeDebugSourceBreakpoints(this.lldbBreakpointSpecifications.get(sourcePath))
643
+ }
644
+ : {})
588
645
  })),
646
+ debugSteppingOptions: options.debugSteppingOptions,
589
647
  pauseOnEntry: !!options.pauseOnEntry,
648
+ pauseAtMain: true,
649
+ entryFunctionName: 'main::main',
590
650
  programArgs,
591
651
  stdin: options.stdin,
592
652
  onDebugEvent: (debugEvent) => {
@@ -612,9 +672,20 @@ class Rust {
612
672
  });
613
673
  return;
614
674
  }
675
+ if (editorSourcePath !== rustLldbSourcePath &&
676
+ debugEvent.type === 'pauseDetails' &&
677
+ debugEvent.callStack) {
678
+ forwardDebugEvent({
679
+ ...debugEvent,
680
+ callStack: debugEvent.callStack.map((frame) => frame.sourcePath === rustLldbSourcePath
681
+ ? { ...frame, sourcePath: editorSourcePath }
682
+ : frame)
683
+ });
684
+ return;
685
+ }
615
686
  forwardDebugEvent(debugEvent);
616
687
  },
617
- onOutput: (debugOutput) => this.output(debugOutput)
688
+ onOutput: forwardOutput
618
689
  });
619
690
  this.lldbSession = lldbSession;
620
691
  for (const input of this.pendingInput.splice(0))
@@ -679,8 +750,63 @@ class Rust {
679
750
  this.worker.onmessage = handler;
680
751
  this.begin = Date.now();
681
752
  try {
753
+ if (typeof MessageChannel === 'function' && typeof MessagePort === 'function') {
754
+ moduleBroker = new MessageChannel();
755
+ const isActiveModuleBroker = () => !settled && _uid === this.uid && this.workerSession.isActive(operation);
756
+ moduleBroker.port1.onmessageerror = () => {
757
+ if (isActiveModuleBroker()) {
758
+ this.workerSession.terminate(new Error('Rust module broker message failed'));
759
+ }
760
+ };
761
+ moduleBroker.port1.onmessage = (event) => {
762
+ const message = event.data;
763
+ if (!isActiveModuleBroker()) {
764
+ if (message?.port instanceof MessagePort) {
765
+ try {
766
+ message.port.close();
767
+ }
768
+ catch {
769
+ // A retired endpoint cannot affect the current worker operation.
770
+ }
771
+ }
772
+ return;
773
+ }
774
+ if (!(message?.port instanceof MessagePort)) {
775
+ this.workerSession.terminate(new Error('Invalid Rust module service request'));
776
+ return;
777
+ }
778
+ if (message.type !== 'rustc-module-service') {
779
+ message.port.close();
780
+ this.workerSession.terminate(new Error('Invalid Rust module service request'));
781
+ return;
782
+ }
783
+ try {
784
+ const receipt = WASM_RUST_RUNTIME_PROFILE.assetReceipts['wasm-rust/runtime/rustc/rustc.wasm.gz'];
785
+ moduleServices.add(serveRuntimeWasmModule(message.port, {
786
+ cache: this.runtimeAssetCache,
787
+ receipt: {
788
+ bytes: receipt.uncompressedBytes,
789
+ sha256: receipt.uncompressedSha256
790
+ },
791
+ identity: JSON.stringify([
792
+ this.runtimeAssetsKey,
793
+ this.compilerUrl,
794
+ WASM_RUST_RUNTIME_PROFILE.profileId,
795
+ this.executableGraphFingerprint
796
+ ]),
797
+ compileKey: 'rustc-native-default-v1',
798
+ maxAssetBytes: limits.maxAssetBytes
799
+ }));
800
+ }
801
+ catch (error) {
802
+ message.port.close();
803
+ this.workerSession.terminate(error);
804
+ }
805
+ };
806
+ }
682
807
  this.worker.postMessage({
683
808
  persistentCache,
809
+ ...(moduleBroker ? { rustcModuleBroker: moduleBroker.port2 } : {}),
684
810
  code,
685
811
  prepare,
686
812
  buffer: this.buffer,
@@ -688,6 +814,9 @@ class Rust {
688
814
  stdin: options.stdin,
689
815
  args: programArgs,
690
816
  targetTriple,
817
+ optimizationProfile: debugMode === 'none'
818
+ ? options.rustOptimizationProfile || 'interactive'
819
+ : 'interactive',
691
820
  log: _log,
692
821
  debugMode,
693
822
  debug: debugMode === 'trace',
@@ -704,7 +833,7 @@ class Rust {
704
833
  }
705
834
  : {})
706
835
  }
707
- });
836
+ }, moduleBroker ? [moduleBroker.port2] : []);
708
837
  }
709
838
  catch (error) {
710
839
  this.workerSession.terminate(error);
@@ -734,6 +863,8 @@ class Rust {
734
863
  if (this.debugMode === 'lldb') {
735
864
  if (resolvedSourcePath !== this.lldbEditorSourcePath)
736
865
  return;
866
+ if (this.lldbBreakpointSpecifications.has(rustLldbSourcePath))
867
+ return this.setSourceBreakpoints(mergeDebugBreakpointLines(this.lldbBreakpointSpecifications.get(rustLldbSourcePath), normalizeRustBreakpointLines(lines)), resolvedSourcePath);
737
868
  this.lldbBreakpoints.set(rustLldbSourcePath, normalizeRustBreakpointLines(lines));
738
869
  return;
739
870
  }
@@ -745,6 +876,34 @@ class Rust {
745
876
  Atomics.store(control, 3, next.length);
746
877
  Atomics.add(control, 2, 1);
747
878
  }
879
+ async setSourceBreakpoints(breakpoints, sourcePath) {
880
+ const values = normalizeDebugSourceBreakpoints(breakpoints);
881
+ const path = normalizeRustLldbSourcePath(sourcePath || this.lldbEditorSourcePath);
882
+ if (this.lldbSession) {
883
+ if (path !== this.lldbEditorSourcePath)
884
+ return;
885
+ return this.lldbSession.setSourceBreakpoints(values, rustLldbSourcePath);
886
+ }
887
+ if (this.debugMode === 'lldb') {
888
+ if (path !== this.lldbEditorSourcePath)
889
+ return;
890
+ this.lldbBreakpointSpecifications.set(rustLldbSourcePath, values);
891
+ this.lldbBreakpoints.set(rustLldbSourcePath, values.filter((value) => value.enabled !== false).map((value) => value.line));
892
+ return;
893
+ }
894
+ assertDebugBreakpointSupport(values, {});
895
+ this.setBreakpoints(values.filter((value) => value.enabled !== false).map((value) => value.line), sourcePath);
896
+ }
897
+ async debugSetSteppingOptions(options) {
898
+ if (!this.lldbSession)
899
+ throw new Error('Stepping options require an LLDB session.');
900
+ await this.lldbSession.setSteppingOptions(options);
901
+ }
902
+ async debugEvaluateValue(expression, options) {
903
+ if (this.lldbSession)
904
+ return this.lldbSession.evaluateValue(expression, options);
905
+ return { result: '?', variablesReference: 0 };
906
+ }
748
907
  debugEvaluate(expression) {
749
908
  return this.lldbSession?.evaluate(expression) ?? Promise.resolve('?');
750
909
  }
@@ -776,6 +935,26 @@ class Rust {
776
935
  kill() {
777
936
  return this.terminate();
778
937
  }
938
+ setRuntimeAssetCache(cache) {
939
+ if (cache === this.runtimeAssetCache)
940
+ return;
941
+ if (this.worker || this.loadController || this.runActive) {
942
+ throw new Error('Cannot replace the Rust runtime asset cache while its worker is active');
943
+ }
944
+ if (this.ownsRuntimeAssetCache)
945
+ this.runtimeAssetCache.dispose();
946
+ this.runtimeAssetCache = cache;
947
+ this.ownsRuntimeAssetCache = false;
948
+ }
949
+ async dispose() {
950
+ try {
951
+ await this.terminate();
952
+ }
953
+ finally {
954
+ if (this.ownsRuntimeAssetCache)
955
+ this.runtimeAssetCache.dispose();
956
+ }
957
+ }
779
958
  async terminate() {
780
959
  this.loadGeneration += 1;
781
960
  this.loadController?.abort(new Error('Rust runtime terminated'));
@@ -1,4 +1,4 @@
1
- import type { CompilerDiagnostic, DebugCommand, DebugDataBreakpoint, DebugDataBreakpointInfo, DebugDataBreakpointInfoArguments, DebugMemory, DebugResolvedDataBreakpoint, DebugSessionEvent, DebugWriteMemoryResult, SandboxExecutionOptions } from './options';
1
+ import type { CompilerDiagnostic, DebugCommand, DebugEvaluateOptions, DebugEvaluateResult, DebugSourceBreakpoint, DebugSteppingOptions, DebugVariable, DebugScope, DebugDataBreakpoint, DebugDataBreakpointInfo, DebugDataBreakpointInfoArguments, DebugMemory, DebugResolvedDataBreakpoint, DebugSessionEvent, DebugWriteMemoryResult, SandboxExecutionOptions } from './options';
2
2
  import type { PlaygroundRuntimeAssets } from './assets';
3
3
  import type { ExecutionLimits, SandboxProgress as CoreSandboxProgress } from '@wasm-idle/core';
4
4
  export type SandboxRuntimeAssets = string | PlaygroundRuntimeAssets;
@@ -21,6 +21,13 @@ export interface Sandbox {
21
21
  debugPause?: () => void | Promise<void>;
22
22
  setBreakpoints?: (lines: number[], sourcePath?: string) => void | Promise<void>;
23
23
  debugEvaluate?: (expression: string) => Promise<string>;
24
+ debugEvaluateValue?: (expression: string, options?: DebugEvaluateOptions) => Promise<DebugEvaluateResult>;
25
+ setSourceBreakpoints?: (breakpoints: DebugSourceBreakpoint[], sourcePath?: string) => Promise<void>;
26
+ debugSetSteppingOptions?: (options: DebugSteppingOptions) => Promise<void>;
27
+ debugVariables?: (variablesReference: number, start?: number, count?: number) => Promise<DebugVariable[]>;
28
+ debugScopes?: (frameId: number) => Promise<DebugScope[]>;
29
+ debugFrameScopes?: (frameId: number) => Promise<DebugScope[]>;
30
+ debugFrameName?: (frameId: number) => Promise<string | null>;
24
31
  debugReadMemory?: (memoryReference: string, offset: number, count: number) => Promise<DebugMemory | null>;
25
32
  debugWriteMemory?: (memoryReference: string, offset: number, data: Uint8Array, allowPartial?: boolean) => Promise<DebugWriteMemoryResult | null>;
26
33
  debugDataBreakpointInfo?: (arguments_: DebugDataBreakpointInfoArguments) => Promise<DebugDataBreakpointInfo | null>;
@@ -1,5 +1,5 @@
1
1
  export declare const STATIC_STDIN_RING_PROTOCOL: "wasm-idle-static-stdin-ring";
2
- export declare const STATIC_STDIN_RING_PROTOCOL_VERSION: 1;
2
+ export declare const STATIC_STDIN_RING_PROTOCOL_VERSION: 2;
3
3
  export declare const STATIC_STDIN_RING_CONTROL_SLOTS: 4;
4
4
  export declare const STATIC_STDIN_RING_CONTROL_BYTES: number;
5
5
  export declare const STATIC_STDIN_RING_WRITE_INDEX: 0;
@@ -27,6 +27,7 @@ export declare class StaticStdinRingHost {
27
27
  readonly maxBufferedBytes: number;
28
28
  private readonly control;
29
29
  private readonly data;
30
+ private readonly counterRange;
30
31
  private readonly pending;
31
32
  private pendingBytes;
32
33
  private pendingOffset;
@@ -1,5 +1,5 @@
1
1
  export const STATIC_STDIN_RING_PROTOCOL = 'wasm-idle-static-stdin-ring';
2
- export const STATIC_STDIN_RING_PROTOCOL_VERSION = 1;
2
+ export const STATIC_STDIN_RING_PROTOCOL_VERSION = 2;
3
3
  export const STATIC_STDIN_RING_CONTROL_SLOTS = 4;
4
4
  export const STATIC_STDIN_RING_CONTROL_BYTES = STATIC_STDIN_RING_CONTROL_SLOTS * Int32Array.BYTES_PER_ELEMENT;
5
5
  export const STATIC_STDIN_RING_WRITE_INDEX = 0;
@@ -24,6 +24,7 @@ export class StaticStdinRingHost {
24
24
  maxBufferedBytes;
25
25
  control;
26
26
  data;
27
+ counterRange;
27
28
  pending = [];
28
29
  pendingBytes = 0;
29
30
  pendingOffset = 0;
@@ -43,12 +44,18 @@ export class StaticStdinRingHost {
43
44
  if (maxBufferedBytes < capacity) {
44
45
  throw new TypeError('Static stdin maximum buffered bytes must cover the ring capacity');
45
46
  }
47
+ if (capacity > 2 ** 31) {
48
+ throw new TypeError('Static stdin ring capacity must not exceed 2^31 bytes');
49
+ }
46
50
  if (typeof SharedArrayBuffer !== 'function') {
47
51
  throw new TypeError('SharedArrayBuffer is required for streaming static stdin');
48
52
  }
49
53
  const buffer = new SharedArrayBuffer(STATIC_STDIN_RING_CONTROL_BYTES + capacity);
50
54
  this.control = new Int32Array(buffer, 0, STATIC_STDIN_RING_CONTROL_SLOTS);
51
55
  this.data = new Uint8Array(buffer, STATIC_STDIN_RING_CONTROL_BYTES, capacity);
56
+ // A capacity-aligned modulus preserves physical offsets at rollover for
57
+ // non-power-of-two rings too. At least two full rings fit in this range.
58
+ this.counterRange = Math.floor(2 ** 32 / capacity) * capacity;
52
59
  this.maxBufferedBytes = maxBufferedBytes;
53
60
  this.descriptor = Object.freeze({
54
61
  protocol: STATIC_STDIN_RING_PROTOCOL,
@@ -107,10 +114,12 @@ export class StaticStdinRingHost {
107
114
  this.flush();
108
115
  }
109
116
  ringBytes() {
110
- const write = Atomics.load(this.control, STATIC_STDIN_RING_WRITE_INDEX);
111
- const read = Atomics.load(this.control, STATIC_STDIN_RING_READ_INDEX);
112
- const used = write - read;
113
- if (used < 0 || used > this.data.byteLength) {
117
+ const write = Atomics.load(this.control, STATIC_STDIN_RING_WRITE_INDEX) >>> 0;
118
+ const read = Atomics.load(this.control, STATIC_STDIN_RING_READ_INDEX) >>> 0;
119
+ const used = (write - read + this.counterRange) % this.counterRange;
120
+ if (write >= this.counterRange ||
121
+ read >= this.counterRange ||
122
+ used > this.data.byteLength) {
114
123
  throw new Error('Static stdin ring counters are invalid');
115
124
  }
116
125
  return used;
@@ -118,10 +127,12 @@ export class StaticStdinRingHost {
118
127
  flush() {
119
128
  if (this.cancelled)
120
129
  return;
121
- let write = Atomics.load(this.control, STATIC_STDIN_RING_WRITE_INDEX);
122
- const read = Atomics.load(this.control, STATIC_STDIN_RING_READ_INDEX);
123
- const used = write - read;
124
- if (used < 0 || used > this.data.byteLength) {
130
+ let write = Atomics.load(this.control, STATIC_STDIN_RING_WRITE_INDEX) >>> 0;
131
+ const read = Atomics.load(this.control, STATIC_STDIN_RING_READ_INDEX) >>> 0;
132
+ const used = (write - read + this.counterRange) % this.counterRange;
133
+ if (write >= this.counterRange ||
134
+ read >= this.counterRange ||
135
+ used > this.data.byteLength) {
125
136
  throw new Error('Static stdin ring counters are invalid');
126
137
  }
127
138
  let available = this.data.byteLength - used;
@@ -136,7 +147,7 @@ export class StaticStdinRingHost {
136
147
  if (first < count) {
137
148
  this.data.set(chunk.subarray(this.pendingOffset + first, this.pendingOffset + count), 0);
138
149
  }
139
- write += count;
150
+ write = (write + count) % this.counterRange;
140
151
  available -= count;
141
152
  this.pendingOffset += count;
142
153
  this.pendingBytes -= count;
@@ -1,5 +1,5 @@
1
1
  import type { PlaygroundRuntimeAssets } from './assets';
2
- import { type ExecutionLimits, type RuntimeAssetIntegrityEntry, type RuntimeAssetCacheOptions, type RuntimeStdinMode, type RuntimeWorkerLifetimePolicy } from '@wasm-idle/core';
2
+ import { type ExecutionLimits, type RuntimeAssetIntegrityEntry, type RuntimeAssetCacheOptions, type RuntimeAssetCache, type RuntimeWasmModuleReceipt, type RuntimeStdinMode, type RuntimeWorkerLifetimePolicy } from '@wasm-idle/core';
3
3
  import { type CompilerDiagnostic, type SandboxExecutionOptions } from './options';
4
4
  import type { Sandbox, SandboxProgress } from './sandbox';
5
5
  type StaticWorkerReceipt = Readonly<Required<Pick<RuntimeAssetIntegrityEntry, 'bytes' | 'sha256'>>>;
@@ -55,8 +55,16 @@ export interface StaticWorkerRuntimeConfig {
55
55
  workerLifetime?: RuntimeWorkerLifetimePolicy;
56
56
  resolveRuntimeAssets: (runtimeAssets: string | PlaygroundRuntimeAssets, currentUrl: string) => StaticWorkerRuntimeUrls;
57
57
  preflightRuntimeAssets?: (urls: StaticWorkerRuntimeUrls, context: StaticWorkerRuntimePreflightContext) => unknown | Promise<unknown>;
58
+ /** Only the host selects verified logical receipts and deterministic compilation settings. */
59
+ compilerModules?: (urls: StaticWorkerRuntimeUrls, limits: ExecutionLimits) => Readonly<Record<string, StaticWorkerCompilerModule>>;
58
60
  runtimePreflightDelivery?: 'structured-clone' | 'transfer-owned' | 'transfer-owned-worker-cache';
59
61
  }
62
+ export interface StaticWorkerCompilerModule {
63
+ readonly receipt: RuntimeWasmModuleReceipt;
64
+ /** Include the transform implementation version, options and any enforced memory cap. */
65
+ readonly compileKey: string;
66
+ readonly compile?: (ownedBytes: Uint8Array<ArrayBuffer>) => Promise<WebAssembly.Module>;
67
+ }
60
68
  type StdinWaiter = {
61
69
  reject: (reason: unknown) => void;
62
70
  resolve: () => void;
@@ -101,7 +109,13 @@ export declare class StaticWorkerRuntimeSandbox implements Sandbox {
101
109
  private readonly workerRuntimePreflightMaxAssetBytes;
102
110
  private readonly workersWithCachedRuntimePreflight;
103
111
  private workerStartMaxAssetBytes;
112
+ private runtimeAssetCache?;
113
+ private readonly workerCompilerModuleServices;
104
114
  constructor(config: StaticWorkerRuntimeConfig);
115
+ /** The application owns this cache; worker or sandbox disposal never disposes it. */
116
+ setRuntimeAssetCache(cache: RuntimeAssetCache): void;
117
+ private createCompilerModulePorts;
118
+ private closeCompilerModuleServices;
105
119
  private usesOwnedRuntimePreflight;
106
120
  private usesWorkerCachedRuntimePreflight;
107
121
  private resetWorkerForLowerAssetLimit;