wasm-idle 1.1.2 → 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 (121) hide show
  1. package/README.md +134 -82
  2. package/dist/container/bash-profile.d.ts +80 -0
  3. package/dist/container/bash-profile.js +328 -0
  4. package/dist/{playground/container → container}/binding.d.ts +21 -8
  5. package/dist/{playground/container → container}/binding.js +109 -25
  6. package/dist/{playground/container → container}/clang-runtime.d.ts +11 -7
  7. package/dist/container/clang-runtime.js +561 -0
  8. package/dist/container/client.d.ts +14 -0
  9. package/dist/{playground/container → container}/client.js +2 -6
  10. package/dist/{playground/container → container}/explorer.d.ts +1 -1
  11. package/dist/container/index.d.ts +11 -0
  12. package/dist/container/index.js +11 -0
  13. package/dist/container/owner-worker.d.ts +19 -0
  14. package/dist/container/owner-worker.js +464 -0
  15. package/dist/{playground/container → container}/protocol.d.ts +12 -1
  16. package/dist/{playground/container → container}/runtime-adapter.d.ts +13 -11
  17. package/dist/{playground/container → container}/runtime-adapter.js +104 -41
  18. package/dist/{playground/container → container}/shell-control.d.ts +2 -2
  19. package/dist/{playground/container → container}/shell-control.js +93 -36
  20. package/dist/{playground/container → container}/workspace.d.ts +5 -1
  21. package/dist/index.d.ts +1 -1
  22. package/dist/playground/assetBridge.js +5 -2
  23. package/dist/playground/assets.d.ts +8 -0
  24. package/dist/playground/clang.d.ts +5 -1
  25. package/dist/playground/clang.js +61 -5
  26. package/dist/playground/compiledLldb.d.ts +41 -0
  27. package/dist/playground/compiledLldb.js +167 -0
  28. package/dist/playground/d.d.ts +4 -1
  29. package/dist/playground/d.js +78 -9
  30. package/dist/playground/debugDeadline.d.ts +12 -0
  31. package/dist/playground/debugDeadline.js +34 -0
  32. package/dist/playground/debugMailbox.d.ts +12 -0
  33. package/dist/playground/debugMailbox.js +77 -0
  34. package/dist/playground/go.js +9 -4
  35. package/dist/playground/goDebugInstrumentation.js +24 -44
  36. package/dist/playground/java-debug-FMHDKUN6.js +124 -0
  37. package/dist/playground/java-debug-notices.txt +492 -0
  38. package/dist/playground/java.d.ts +17 -1
  39. package/dist/playground/java.js +159 -3
  40. package/dist/playground/javaDebug.d.ts +62 -0
  41. package/dist/playground/javaDebug.js +305 -0
  42. package/dist/playground/javaDebugBridge.d.ts +6 -0
  43. package/dist/playground/javaDebugBridge.js +250 -0
  44. package/dist/playground/javaDebugSource.d.ts +9 -0
  45. package/dist/playground/javaDebugSource.js +4 -0
  46. package/dist/playground/javaRuntimeErrors.js +34 -1
  47. package/dist/playground/lean4Assets.d.ts +1 -25
  48. package/dist/playground/lean4Assets.js +2 -123
  49. package/dist/playground/lfortranAssets.d.ts +2 -2
  50. package/dist/playground/lldbSession.d.ts +23 -2
  51. package/dist/playground/lldbSession.js +278 -58
  52. package/dist/playground/lldbSourcePath.d.ts +2 -0
  53. package/dist/playground/lldbSourcePath.js +9 -0
  54. package/dist/playground/lua.d.ts +20 -1
  55. package/dist/playground/lua.js +190 -8
  56. package/dist/playground/luaDebug.d.ts +1 -0
  57. package/dist/playground/luaDebug.js +1 -0
  58. package/dist/playground/nim.d.ts +1 -0
  59. package/dist/playground/nim.js +8 -0
  60. package/dist/playground/objectivec.d.ts +7 -6
  61. package/dist/playground/objectivec.js +75 -9
  62. package/dist/playground/options.d.ts +10 -4
  63. package/dist/playground/python.d.ts +8 -1
  64. package/dist/playground/python.js +81 -6
  65. package/dist/playground/ruby.d.ts +10 -1
  66. package/dist/playground/ruby.js +66 -1
  67. package/dist/playground/rubyDebug.d.ts +21 -0
  68. package/dist/playground/rubyDebug.js +128 -0
  69. package/dist/playground/rust.d.ts +5 -1
  70. package/dist/playground/rust.js +128 -46
  71. package/dist/playground/sandbox.d.ts +8 -1
  72. package/dist/playground/staticWorkerRuntime.js +8 -2
  73. package/dist/playground/stdinBuffer.d.ts +1 -1
  74. package/dist/playground/stdinBuffer.js +19 -2
  75. package/dist/playground/tinygo.js +7 -0
  76. package/dist/playground/typescript.js +8 -0
  77. package/dist/playground/v.d.ts +3 -2
  78. package/dist/playground/v.js +66 -12
  79. package/dist/playground/wasmC3Version.d.ts +2 -2
  80. package/dist/playground/wasmC3Version.js +2 -2
  81. package/dist/playground/wasmLean4Version.d.ts +1 -110
  82. package/dist/playground/wasmLean4Version.js +2 -111
  83. package/dist/playground/wasmLfortranVersion.d.ts +2 -2
  84. package/dist/playground/wasmLfortranVersion.js +2 -2
  85. package/dist/playground/wasmLuaVersion.d.ts +1 -1
  86. package/dist/playground/wasmLuaVersion.js +1 -1
  87. package/dist/playground/wasmRustVersion.d.ts +26 -38
  88. package/dist/playground/wasmRustVersion.js +25 -40
  89. package/dist/playground/worker/clang.js +1 -1
  90. package/dist/playground/worker/cobol.js +1 -1
  91. package/dist/playground/worker/d.js +51 -2
  92. package/dist/playground/worker/fortran.js +1 -1
  93. package/dist/playground/worker/java.js +188 -35
  94. package/dist/playground/worker/javaDebugEvaluator.d.ts +16 -0
  95. package/dist/playground/worker/javaDebugEvaluator.js +44 -0
  96. package/dist/playground/worker/javaDebugRuntime.d.ts +24 -0
  97. package/dist/playground/worker/javaDebugRuntime.js +315 -0
  98. package/dist/playground/worker/javaOutput.d.ts +11 -0
  99. package/dist/playground/worker/javaOutput.js +54 -0
  100. package/dist/playground/worker/lua.js +13 -0
  101. package/dist/playground/worker/python.js +57 -22
  102. package/dist/playground/worker/ruby.js +13 -0
  103. package/dist/playground/worker/rubyDebugPreview.d.ts +2 -0
  104. package/dist/playground/worker/rubyDebugPreview.js +80 -0
  105. package/dist/playground/worker/rubyTracepoint.d.ts +16 -0
  106. package/dist/playground/worker/rubyTracepoint.js +162 -0
  107. package/dist/playground/worker/rust.js +4 -1
  108. package/dist/playground/worker/v.js +30 -5
  109. package/dist/playground/zig.js +4 -0
  110. package/package.json +55 -11
  111. package/dist/playground/container/ContainerExplorer.svelte +0 -307
  112. package/dist/playground/container/ContainerExplorer.svelte.d.ts +0 -14
  113. package/dist/playground/container/clang-runtime.js +0 -343
  114. package/dist/playground/container/client.d.ts +0 -15
  115. package/dist/playground/container/owner.worker.d.ts +0 -1
  116. package/dist/playground/container/owner.worker.js +0 -390
  117. package/dist/playground/container/release.d.ts +0 -31
  118. package/dist/playground/container/release.js +0 -612
  119. /package/dist/{playground/container → container}/explorer.js +0 -0
  120. /package/dist/{playground/container → container}/protocol.js +0 -0
  121. /package/dist/{playground/container → container}/workspace.js +0 -0
@@ -1,307 +0,0 @@
1
- <script lang="ts">
2
- import type { ContainerWorkspaceEntry } from './protocol';
3
- import {
4
- buildExplorerModel,
5
- explorerBreadcrumbs,
6
- EXPLORER_PAGE_SIZE,
7
- resolveExplorerDirectory,
8
- visibleExplorerRows
9
- } from './explorer';
10
-
11
- interface Props {
12
- entries: readonly ContainerWorkspaceEntry[];
13
- selectedPath?: string;
14
- initialDirectory?: string;
15
- busy?: boolean;
16
- onOpenFile: (entry: ContainerWorkspaceEntry) => void;
17
- }
18
-
19
- let {
20
- entries,
21
- selectedPath,
22
- initialDirectory = '',
23
- busy = false,
24
- onOpenFile
25
- }: Props = $props();
26
- let requestedDirectory = $state('');
27
- let expanded = $state(new Set<string>());
28
- let offset = $state(0);
29
- const model = $derived(buildExplorerModel(entries));
30
- const directory = $derived(resolveExplorerDirectory(model, requestedDirectory));
31
- const breadcrumbs = $derived(explorerBreadcrumbs(directory));
32
- const visible = $derived(visibleExplorerRows(model, directory, expanded, offset));
33
-
34
- $effect(() => {
35
- requestedDirectory = initialDirectory;
36
- expanded = new Set();
37
- offset = 0;
38
- });
39
-
40
- function enterDirectory(path: string) {
41
- requestedDirectory = resolveExplorerDirectory(model, path);
42
- offset = 0;
43
- }
44
-
45
- function toggleDirectory(path: string) {
46
- const next = new Set(expanded);
47
- if (next.has(path)) next.delete(path);
48
- else next.add(path);
49
- expanded = next;
50
- offset = 0;
51
- }
52
- </script>
53
-
54
- <div class="container-explorer">
55
- <nav aria-label="Workspace folder" class="breadcrumbs">
56
- <button
57
- type="button"
58
- class="parent-button"
59
- aria-label="Go to parent folder"
60
- title="Go to parent folder"
61
- disabled={!directory}
62
- onclick={() => enterDirectory(model.entries.get(directory)?.parent ?? '')}>↑</button
63
- >
64
- <ol>
65
- {#each breadcrumbs as crumb (crumb.path)}
66
- <li>
67
- <button
68
- type="button"
69
- title={crumb.path ? `/workspace/${crumb.path}` : '/workspace'}
70
- aria-current={crumb.path === directory ? 'location' : undefined}
71
- onclick={() => enterDirectory(crumb.path)}>{crumb.name}</button
72
- >
73
- </li>
74
- {/each}
75
- </ol>
76
- </nav>
77
- <div class="files">
78
- {#if visible.rows.length === 0}
79
- <p class="empty">This folder is empty.</p>
80
- {:else}
81
- <ul aria-label="Workspace files">
82
- {#each visible.rows as row (row.entry.path)}
83
- {@const entry = row.entry}
84
- <li style:padding-left={`${Math.min(row.depth, 16) * 12}px`}>
85
- {#if entry.type === 'directory'}
86
- <button
87
- type="button"
88
- class="toggle"
89
- aria-label={`${row.expanded ? 'Collapse' : 'Expand'} /workspace/${entry.path}`}
90
- aria-expanded={row.expanded}
91
- onclick={() => toggleDirectory(entry.path)}
92
- >{row.expanded ? '▾' : '▸'}</button
93
- >
94
- <button
95
- type="button"
96
- class="entry directory"
97
- data-container-directory={entry.path}
98
- aria-label={`Open folder /workspace/${entry.path}`}
99
- title={`/workspace/${entry.path}/`}
100
- onclick={() => enterDirectory(entry.path)}
101
- >
102
- <span class="material-symbols-outlined" aria-hidden="true"
103
- >folder</span
104
- >
105
- <span>{entry.name}</span>
106
- </button>
107
- {:else if entry.type === 'file'}
108
- <button
109
- type="button"
110
- class="entry file"
111
- class:active={entry.path === selectedPath}
112
- data-container-path={entry.path}
113
- aria-current={entry.path === selectedPath ? 'true' : undefined}
114
- title={`/workspace/${entry.path} (${entry.size} bytes)`}
115
- disabled={busy}
116
- onclick={() => onOpenFile(entry)}
117
- >
118
- <span class="material-symbols-outlined" aria-hidden="true"
119
- >draft</span
120
- >
121
- <span>{entry.name}</span>
122
- </button>
123
- {:else}
124
- <div
125
- class="entry symlink"
126
- title={`/workspace/${entry.path} (symbolic link; target is not followed)`}
127
- >
128
- <span class="material-symbols-outlined" aria-hidden="true"
129
- >link</span
130
- >
131
- <span>{entry.name}</span>
132
- <small>link</small>
133
- </div>
134
- {/if}
135
- </li>
136
- {/each}
137
- </ul>
138
- {/if}
139
- </div>
140
- {#if visible.offset > 0 || visible.remaining > 0}
141
- <div class="pagination">
142
- {#if visible.offset > 0}<button
143
- type="button"
144
- onclick={() => (offset = visible.offset - EXPLORER_PAGE_SIZE)}
145
- >Previous files</button
146
- >{/if}
147
- <span aria-live="polite"
148
- >{visible.offset + 1}–{visible.offset + visible.rows.length} of {visible.total}</span
149
- >
150
- {#if visible.remaining > 0}<button
151
- type="button"
152
- onclick={() => (offset = visible.offset + EXPLORER_PAGE_SIZE)}
153
- >Show more ({visible.remaining} remaining)</button
154
- >{/if}
155
- </div>
156
- {/if}
157
- </div>
158
-
159
- <style>
160
- .container-explorer {
161
- display: flex;
162
- flex-direction: column;
163
- min-height: 36px;
164
- overflow: hidden;
165
- }
166
- .breadcrumbs {
167
- display: flex;
168
- align-items: flex-start;
169
- gap: 4px;
170
- padding: 4px 0;
171
- border-bottom: 1px solid var(--line-soft, #e5e7eb);
172
- }
173
- .breadcrumbs ol {
174
- display: flex;
175
- flex-wrap: wrap;
176
- align-items: center;
177
- gap: 2px;
178
- min-width: 0;
179
- margin: 0;
180
- padding: 0;
181
- list-style: none;
182
- }
183
- .breadcrumbs li {
184
- display: flex;
185
- align-items: center;
186
- min-width: 0;
187
- }
188
- .breadcrumbs li + li::before {
189
- content: '/';
190
- color: var(--text-4, #888);
191
- font-size: 10px;
192
- }
193
- button {
194
- border: 0;
195
- border-radius: 6px;
196
- background: transparent;
197
- color: var(--text-2, #535c6d);
198
- cursor: pointer;
199
- font: inherit;
200
- }
201
- button:disabled {
202
- cursor: not-allowed;
203
- opacity: 0.6;
204
- }
205
- button:hover:enabled,
206
- button.active {
207
- background: var(--primary-soft, #eaf2ff);
208
- color: var(--primary-strong, #2563eb);
209
- }
210
- button:focus-visible {
211
- outline: 2px solid var(--primary, #3182f6);
212
- outline-offset: -2px;
213
- }
214
- .breadcrumbs button {
215
- max-width: 200px;
216
- padding: 4px 5px;
217
- overflow-wrap: anywhere;
218
- text-align: left;
219
- font: 10px var(--mono, monospace);
220
- }
221
- .breadcrumbs button[aria-current] {
222
- color: var(--text, #1f2937);
223
- }
224
- .breadcrumbs .parent-button {
225
- flex: 0 0 26px;
226
- text-align: center;
227
- font-size: 15px;
228
- }
229
- .files {
230
- min-height: 36px;
231
- overflow: auto;
232
- }
233
- .files ul {
234
- margin: 0;
235
- padding: 4px 0;
236
- list-style: none;
237
- }
238
- .files li {
239
- display: flex;
240
- align-items: center;
241
- box-sizing: border-box;
242
- min-width: 0;
243
- }
244
- .entry {
245
- display: flex;
246
- align-items: center;
247
- flex: 1 1 auto;
248
- gap: 6px;
249
- min-width: 0;
250
- min-height: 30px;
251
- padding: 5px 8px;
252
- text-align: left;
253
- font: 11px var(--mono, monospace);
254
- color: var(--text-2, #535c6d);
255
- }
256
- .entry > span:last-child {
257
- min-width: 0;
258
- overflow: hidden;
259
- text-overflow: ellipsis;
260
- white-space: nowrap;
261
- }
262
- .entry.file,
263
- .entry.symlink {
264
- margin-left: 24px;
265
- }
266
- .toggle {
267
- flex: 0 0 24px;
268
- width: 24px;
269
- min-height: 30px;
270
- padding: 0;
271
- font-size: 13px;
272
- }
273
- .material-symbols-outlined {
274
- flex: 0 0 auto;
275
- font-size: 16px;
276
- }
277
- .symlink {
278
- color: var(--text-3, #7a8393);
279
- }
280
- .symlink span:nth-child(2) {
281
- overflow: hidden;
282
- text-overflow: ellipsis;
283
- white-space: nowrap;
284
- }
285
- .symlink small {
286
- margin-left: auto;
287
- font-size: 9px;
288
- }
289
- .empty {
290
- margin: 8px;
291
- color: var(--text-3, #7a8393);
292
- font-size: 11px;
293
- }
294
- .pagination {
295
- display: flex;
296
- flex-wrap: wrap;
297
- align-items: center;
298
- gap: 5px;
299
- border-top: 1px solid var(--line-soft, #e5e7eb);
300
- padding: 5px 0;
301
- color: var(--text-3, #7a8393);
302
- font-size: 10px;
303
- }
304
- .pagination button {
305
- padding: 5px;
306
- }
307
- </style>
@@ -1,14 +0,0 @@
1
- import { SvelteComponentTyped } from "svelte";
2
- declare const __propDef: {
3
- props: Record<string, never>;
4
- events: {
5
- [evt: string]: CustomEvent<any>;
6
- };
7
- slots: {};
8
- };
9
- export type ContainerExplorerProps = typeof __propDef.props;
10
- export type ContainerExplorerEvents = typeof __propDef.events;
11
- export type ContainerExplorerSlots = typeof __propDef.slots;
12
- export default class ContainerExplorer extends SvelteComponentTyped<ContainerExplorerProps, ContainerExplorerEvents, ContainerExplorerSlots> {
13
- }
14
- export {};
@@ -1,343 +0,0 @@
1
- import { resolveSandboxExecutionArgs } from '../options';
2
- /** The Run button uses the same real Clang command and artifacts as Bash. Debug uses its qualified backend. */
3
- export class WebsiteClangRuntime {
4
- profile;
5
- files;
6
- emit;
7
- #active = new Map();
8
- #prepared = new Map();
9
- #input = new Map();
10
- #header;
11
- constructor(profile, files, emit) {
12
- this.profile = profile;
13
- this.files = files;
14
- this.emit = emit;
15
- }
16
- queue(language) {
17
- let value = this.#input.get(language);
18
- if (!value)
19
- this.#input.set(language, (value = { chunks: [], size: 0, eof: false }));
20
- return value;
21
- }
22
- key(language, code, args, options) {
23
- return JSON.stringify([
24
- language,
25
- code,
26
- args,
27
- options.cppVersion,
28
- options.cVersion,
29
- options.compileArgs,
30
- options.activePath,
31
- options.env,
32
- options.workspaceFiles
33
- ]);
34
- }
35
- async drain(process, language, requestId, diagnostics = false, quiet = false) {
36
- const output = async (stream, stderr) => {
37
- const reader = stream.getReader(), decoder = new TextDecoder();
38
- let pending = '';
39
- try {
40
- for (;;) {
41
- const { value, done } = await reader.read();
42
- const text = done ? decoder.decode() : decoder.decode(value, { stream: true });
43
- if (text && !quiet)
44
- this.emit({ language, name: 'output', data: text, requestId });
45
- if (stderr && diagnostics) {
46
- pending += text;
47
- const lines = pending.split('\n');
48
- pending = lines.pop() ?? '';
49
- for (const line of lines) {
50
- const match = /^(.*?):(\d+):(\d+): (fatal error|error|warning|note): (.*)$/.exec(line);
51
- if (match)
52
- this.emit({
53
- language,
54
- name: 'diagnostic',
55
- requestId,
56
- data: {
57
- fileName: match[1].replace(`/workspace/${language}/`, '/workspace/'),
58
- lineNumber: Number(match[2]),
59
- columnNumber: Number(match[3]),
60
- severity: match[4].includes('error')
61
- ? 'error'
62
- : match[4] === 'warning'
63
- ? 'warning'
64
- : 'other',
65
- message: match[5]
66
- }
67
- });
68
- }
69
- if (pending.length > 65536)
70
- pending = '';
71
- }
72
- if (done)
73
- break;
74
- }
75
- }
76
- finally {
77
- reader.releaseLock();
78
- }
79
- };
80
- const [result] = await Promise.all([
81
- process.exited,
82
- output(process.stdout, false),
83
- output(process.stderr, true)
84
- ]);
85
- return result;
86
- }
87
- success(result, signal) {
88
- if (signal.aborted)
89
- throw signal.reason ?? new DOMException('Command cancelled', 'AbortError');
90
- if (result.reason !== 'exited')
91
- throw new Error(`Container command ended with ${result.reason}`);
92
- return result.code === 0;
93
- }
94
- headerKey(language, code, compileArgs, options, cwd, standard) {
95
- // A textual prefix and the fixed system headers are the only admitted inputs.
96
- // Custom arguments, environment paths, or workspace headers can alter declarations.
97
- if (language !== 'CPP' ||
98
- compileArgs.length ||
99
- Object.keys(options.env ?? {}).length ||
100
- options.workspaceFiles?.some((file) => file.path !== options.activePath && !/\.(?:c|cc|cpp|cxx|wasm)$/i.test(file.path)))
101
- return;
102
- let rest = code.replace(/^\uFEFF/, '');
103
- for (;;) {
104
- rest = rest.replace(/^\s+/, '');
105
- if (rest.startsWith('//')) {
106
- const end = rest.indexOf('\n');
107
- if (end < 0 || rest.slice(0, end).trimEnd().endsWith('\\'))
108
- return;
109
- rest = rest.slice(end + 1);
110
- }
111
- else if (rest.startsWith('/*')) {
112
- const end = rest.indexOf('*/', 2);
113
- if (end < 0)
114
- return;
115
- rest = rest.slice(end + 2);
116
- }
117
- else
118
- break;
119
- }
120
- if (!/^#[ \t]*include[ \t]*<bits\/stdc\+\+\.h>/.test(rest))
121
- return;
122
- return JSON.stringify([cwd, standard, '-fno-exceptions']);
123
- }
124
- discardHeader() {
125
- if (!this.#header)
126
- return;
127
- try {
128
- this.profile.container.fs.unlink(this.#header.path);
129
- }
130
- catch {
131
- /* Already removed. */
132
- }
133
- this.#header = undefined;
134
- }
135
- async prepare(language, code, args, options, signal, progress, requestId) {
136
- if (this.#active.has(language))
137
- throw new Error(`${language} already has an active command`);
138
- const input = this.files.inputs(language, code, options);
139
- const cwd = this.files.directory(language), artifact = `${cwd}/program.wasm`;
140
- const job = { language, phase: 'prepare', signal };
141
- this.#active.set(language, job);
142
- this.#prepared.delete(language);
143
- try {
144
- progress?.report?.({
145
- kind: 'activity',
146
- phase: 'initializing',
147
- label: 'Preparing the Clang toolchain'
148
- });
149
- await this.profile.prepareCommand({ path: '/usr/local/bin/clang' }, signal);
150
- const { compileArgs } = resolveSandboxExecutionArgs(language, args, options);
151
- const standard = language === 'CPP'
152
- ? (options.cppVersion ?? 'CPP20').replace(/^CPP/, 'c++').toLowerCase()
153
- : options.cVersion?.toLowerCase();
154
- // An opaque response file or -x override can select C++ for a C editor.
155
- // Keep its default source mode C while enabling the C++ driver's link libraries.
156
- const override = compileArgs.some((arg) => arg.startsWith('@') || arg.startsWith('-x'));
157
- const common = [
158
- ...(language === 'CPP' || override ? ['--driver-mode=g++'] : []),
159
- ...(language === 'C' && override ? ['-x', 'c'] : []),
160
- ...(standard ? [`-std=${standard}`] : []),
161
- '-fno-color-diagnostics',
162
- '-fno-exceptions',
163
- ...compileArgs
164
- ];
165
- const compile = async (argv, quiet = false) => {
166
- job.process = await this.profile.container.spawn('clang', argv, {
167
- cwd,
168
- signal,
169
- env: options.env
170
- });
171
- const drained = this.drain(job.process, language, requestId, !quiet, quiet);
172
- await job.process.stdin.getWriter().close();
173
- const result = await drained;
174
- if (job.stopped)
175
- throw new DOMException('Command cancelled', 'AbortError');
176
- return this.success(result, signal);
177
- };
178
- const headerKey = this.headerKey(language, input.code, compileArgs, input.options, cwd, standard);
179
- if (headerKey && this.#header?.key !== headerKey)
180
- this.discardHeader();
181
- let header = headerKey === this.#header?.key ? this.#header : undefined;
182
- if (header) {
183
- // Restore owned bytes instead of trusting a writable temporary file.
184
- try {
185
- try {
186
- this.profile.container.fs.unlink(header.path);
187
- }
188
- catch {
189
- /* Already removed. */
190
- }
191
- this.profile.container.fs.writeFile(header.path, header.bytes);
192
- }
193
- catch {
194
- this.discardHeader();
195
- header = undefined;
196
- }
197
- }
198
- let okay = await compile([
199
- ...common,
200
- ...(header ? ['-include-pch', header.path] : []),
201
- input.options.activePath,
202
- '-lc-printscan-long-double',
203
- '-o',
204
- artifact
205
- ]);
206
- if (header && okay)
207
- progress?.set?.(95, 'Compiled with precompiled <bits/stdc++.h>');
208
- else if (header) {
209
- this.discardHeader();
210
- okay = await compile([
211
- ...common,
212
- input.options.activePath,
213
- '-lc-printscan-long-double',
214
- '-o',
215
- artifact
216
- ]);
217
- }
218
- if (okay && headerKey && !this.#header) {
219
- const path = `/tmp/wasm-idle-stdcpp-${crypto.randomUUID()}.pch`;
220
- try {
221
- if (await compile([
222
- ...common,
223
- '-c',
224
- '-x',
225
- 'c++-header',
226
- '/usr/share/wasm-sysroot/include/bits/stdc++.h',
227
- '-o',
228
- path
229
- ], true)) {
230
- const bytes = this.profile.container.fs.readFile(path);
231
- if (bytes.byteLength > 0 && bytes.byteLength <= 32 * 1024 * 1024)
232
- this.#header = { key: headerKey, path, bytes };
233
- }
234
- }
235
- catch (error) {
236
- if (signal.aborted || job.stopped)
237
- throw error;
238
- }
239
- finally {
240
- if (this.#header?.path !== path) {
241
- try {
242
- this.profile.container.fs.unlink(path);
243
- }
244
- catch {
245
- /* No usable header. */
246
- }
247
- }
248
- }
249
- }
250
- if (okay)
251
- this.#prepared.set(language, this.key(language, input.code, args, options));
252
- return okay;
253
- }
254
- finally {
255
- if (this.#active.get(language) === job)
256
- this.#active.delete(language);
257
- }
258
- }
259
- async run(language, code, args, options, signal, progress, requestId) {
260
- const input = this.files.inputs(language, code, options);
261
- if (this.#prepared.get(language) !== this.key(language, input.code, args, options) &&
262
- !(await this.prepare(language, code, args, options, signal, progress, requestId)))
263
- return false;
264
- const cwd = this.files.directory(language), job = { language, phase: 'run', signal };
265
- if (this.#active.has(language))
266
- throw new Error(`${language} already has an active command`);
267
- this.#active.set(language, job);
268
- try {
269
- const { programArgs } = resolveSandboxExecutionArgs(language, args, options);
270
- job.process = await this.profile.container.spawn('wasm-run', [`${cwd}/program.wasm`, ...programArgs], { cwd, signal, env: options.env });
271
- job.writer = job.process.stdin.getWriter();
272
- progress?.report?.({
273
- kind: 'ready',
274
- state: 'running',
275
- reason: 'started',
276
- label: 'Container program started'
277
- });
278
- const drained = this.drain(job.process, language, requestId);
279
- if (options.stdin !== undefined) {
280
- await job.writer.write(new TextEncoder().encode(options.stdin));
281
- await job.writer.close();
282
- }
283
- else
284
- await this.flush(language);
285
- return this.success(await drained, signal);
286
- }
287
- finally {
288
- job.writer?.releaseLock();
289
- this.#input.delete(language);
290
- if (this.#active.get(language) === job)
291
- this.#active.delete(language);
292
- }
293
- }
294
- async flush(language) {
295
- const writer = this.#active.get(language)?.writer;
296
- if (!writer)
297
- return;
298
- const queue = this.queue(language);
299
- for (const text of queue.chunks.splice(0)) {
300
- const bytes = new TextEncoder().encode(text);
301
- await writer.write(bytes);
302
- queue.size -= bytes.byteLength;
303
- }
304
- if (queue.eof) {
305
- queue.eof = false;
306
- await writer.close();
307
- }
308
- }
309
- async write(language, text) {
310
- const q = this.queue(language);
311
- q.size += new TextEncoder().encode(text).length;
312
- if (q.size > 8 * 1024 * 1024)
313
- throw new Error('Pending stdin exceeds its limit');
314
- q.chunks.push(text);
315
- await this.flush(language);
316
- }
317
- async eof(language) {
318
- this.queue(language).eof = true;
319
- await this.flush(language);
320
- }
321
- async stop(language) {
322
- this.#input.delete(language);
323
- const job = this.#active.get(language);
324
- if (job)
325
- job.stopped = true;
326
- await job?.process?.terminate();
327
- }
328
- clear(language) {
329
- this.#input.delete(language);
330
- }
331
- inspect() {
332
- return [...this.#active.values()].map((job) => ({
333
- language: job.language,
334
- phase: job.phase,
335
- pid: job.process?.pid,
336
- filesystem: 'live'
337
- }));
338
- }
339
- async dispose() {
340
- await Promise.allSettled([...this.#active.keys()].map((language) => this.stop(language)));
341
- this.discardHeader();
342
- }
343
- }
@@ -1,15 +0,0 @@
1
- import { type ContainerWorkerCallOptions, type ContainerWorkerEvent, type ContainerWorkerInitialization } from './protocol';
2
- export interface ContainerWorkerClient {
3
- call<T = unknown>(method: string, args?: unknown[], options?: ContainerWorkerCallOptions): Promise<T>;
4
- onEvent(listener: (event: ContainerWorkerEvent) => void): () => void;
5
- dispose(): Promise<void>;
6
- }
7
- type OwnerWorker = Pick<Worker, 'postMessage' | 'addEventListener' | 'removeEventListener' | 'terminate'>;
8
- interface ClientOptions extends ContainerWorkerInitialization {
9
- /** Injection is used only by deterministic transport tests. */
10
- workerFactory?: () => OwnerWorker;
11
- initializationTimeoutMs?: number;
12
- cleanupDeadlineMs?: number;
13
- }
14
- export declare function createContainerWorkerClient(options: ClientOptions): ContainerWorkerClient;
15
- export {};
@@ -1 +0,0 @@
1
- export {};