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.
- package/README.md +267 -112
- package/dist/container/bash-profile.d.ts +80 -0
- package/dist/container/bash-profile.js +328 -0
- package/dist/container/binding.d.ts +39 -0
- package/dist/container/binding.js +331 -0
- package/dist/container/clang-runtime.d.ts +42 -0
- package/dist/container/clang-runtime.js +561 -0
- package/dist/container/client.d.ts +14 -0
- package/dist/container/client.js +185 -0
- package/dist/container/explorer.d.ts +31 -0
- package/dist/container/explorer.js +112 -0
- package/dist/container/index.d.ts +11 -0
- package/dist/container/index.js +11 -0
- package/dist/container/owner-worker.d.ts +19 -0
- package/dist/container/owner-worker.js +464 -0
- package/dist/container/protocol.d.ts +84 -0
- package/dist/container/protocol.js +28 -0
- package/dist/container/runtime-adapter.d.ts +75 -0
- package/dist/container/runtime-adapter.js +537 -0
- package/dist/container/shell-control.d.ts +49 -0
- package/dist/container/shell-control.js +360 -0
- package/dist/container/workspace.d.ts +12 -0
- package/dist/container/workspace.js +29 -0
- package/dist/index.d.ts +1 -1
- package/dist/playground/assetBridge.js +111 -33
- package/dist/playground/assets.d.ts +10 -2
- package/dist/playground/c3.js +22 -0
- package/dist/playground/c3Memory.d.ts +19 -0
- package/dist/playground/c3Memory.js +117 -0
- package/dist/playground/cachedClang.js +143 -34
- package/dist/playground/clang.d.ts +5 -1
- package/dist/playground/clang.js +61 -5
- package/dist/playground/clangAssetIntegrity.js +6 -6
- package/dist/playground/clangWorkerProtocol.d.ts +12 -1
- package/dist/playground/cobol.d.ts +3 -0
- package/dist/playground/cobol.js +10 -1
- package/dist/playground/commonlispAssets.d.ts +2 -2
- package/dist/playground/compiledArtifactCache.d.ts +3 -1
- package/dist/playground/compiledArtifactCache.js +31 -5
- package/dist/playground/compiledLldb.d.ts +41 -0
- package/dist/playground/compiledLldb.js +167 -0
- package/dist/playground/d.d.ts +10 -2
- package/dist/playground/d.js +161 -14
- package/dist/playground/debugDeadline.d.ts +12 -0
- package/dist/playground/debugDeadline.js +34 -0
- package/dist/playground/debugMailbox.d.ts +12 -0
- package/dist/playground/debugMailbox.js +77 -0
- package/dist/playground/dotnet.js +6 -8
- package/dist/playground/fortran.d.ts +3 -0
- package/dist/playground/fortran.js +10 -1
- package/dist/playground/go.js +23 -7
- package/dist/playground/goDebugInstrumentation.js +207 -97
- package/dist/playground/java-debug-FMHDKUN6.js +124 -0
- package/dist/playground/java-debug-notices.txt +492 -0
- package/dist/playground/java.d.ts +17 -1
- package/dist/playground/java.js +162 -5
- package/dist/playground/javaDebug.d.ts +62 -0
- package/dist/playground/javaDebug.js +305 -0
- package/dist/playground/javaDebugBridge.d.ts +6 -0
- package/dist/playground/javaDebugBridge.js +250 -0
- package/dist/playground/javaDebugSource.d.ts +9 -0
- package/dist/playground/javaDebugSource.js +4 -0
- package/dist/playground/javaRuntimeErrors.d.ts +9 -0
- package/dist/playground/javaRuntimeErrors.js +93 -0
- package/dist/playground/lean4Assets.d.ts +1 -25
- package/dist/playground/lean4Assets.js +2 -123
- package/dist/playground/lfortran.js +10 -0
- package/dist/playground/lfortranAssets.d.ts +2 -2
- package/dist/playground/lldbSession.d.ts +23 -2
- package/dist/playground/lldbSession.js +278 -58
- package/dist/playground/lldbSourcePath.d.ts +2 -0
- package/dist/playground/lldbSourcePath.js +9 -0
- package/dist/playground/lua.d.ts +20 -1
- package/dist/playground/lua.js +190 -8
- package/dist/playground/luaDebug.d.ts +1 -0
- package/dist/playground/luaDebug.js +1 -0
- package/dist/playground/nim.d.ts +1 -0
- package/dist/playground/nim.js +30 -0
- package/dist/playground/objectivec.d.ts +10 -6
- package/dist/playground/objectivec.js +85 -10
- package/dist/playground/options.d.ts +12 -4
- package/dist/playground/python.d.ts +8 -1
- package/dist/playground/python.js +89 -6
- package/dist/playground/ruby.d.ts +10 -1
- package/dist/playground/ruby.js +66 -1
- package/dist/playground/rubyDebug.d.ts +21 -0
- package/dist/playground/rubyDebug.js +128 -0
- package/dist/playground/runtimeAssetCache.d.ts +1 -32
- package/dist/playground/runtimeAssetCache.js +1 -193
- package/dist/playground/runtimeSession.d.ts +2 -2
- package/dist/playground/runtimeSession.js +11 -11
- package/dist/playground/rust.d.ts +10 -0
- package/dist/playground/rust.js +230 -51
- package/dist/playground/sandbox.d.ts +8 -1
- package/dist/playground/staticStdinRing.d.ts +2 -1
- package/dist/playground/staticStdinRing.js +21 -10
- package/dist/playground/staticWorkerRuntime.d.ts +15 -1
- package/dist/playground/staticWorkerRuntime.js +127 -19
- package/dist/playground/stdinBuffer.d.ts +1 -1
- package/dist/playground/stdinBuffer.js +19 -2
- package/dist/playground/tinygo.d.ts +3 -0
- package/dist/playground/tinygo.js +88 -6
- package/dist/playground/typescript.js +8 -0
- package/dist/playground/v.d.ts +6 -2
- package/dist/playground/v.js +76 -13
- package/dist/playground/wasmAwkVersion.d.ts +13 -13
- package/dist/playground/wasmAwkVersion.js +4 -4
- package/dist/playground/wasmBqnVersion.d.ts +2 -2
- package/dist/playground/wasmBqnVersion.js +2 -2
- package/dist/playground/wasmC3Version.d.ts +2 -2
- package/dist/playground/wasmC3Version.js +2 -2
- package/dist/playground/wasmClojureScriptVersion.d.ts +2 -2
- package/dist/playground/wasmClojureScriptVersion.js +2 -2
- package/dist/playground/wasmCommonLispVersion.d.ts +2 -2
- package/dist/playground/wasmCommonLispVersion.js +2 -2
- package/dist/playground/wasmDIntegrity.d.ts +5 -5
- package/dist/playground/wasmDIntegrity.js +5 -5
- package/dist/playground/wasmDVersion.d.ts +16 -1
- package/dist/playground/wasmDVersion.js +16 -1
- package/dist/playground/wasmDebugVersion.d.ts +1 -1
- package/dist/playground/wasmDebugVersion.js +1 -1
- package/dist/playground/wasmForthVersion.d.ts +2 -2
- package/dist/playground/wasmForthVersion.js +2 -2
- package/dist/playground/wasmGleamVersion.d.ts +2 -2
- package/dist/playground/wasmGleamVersion.js +2 -2
- package/dist/playground/wasmGrainVersion.d.ts +2 -2
- package/dist/playground/wasmGrainVersion.js +2 -2
- package/dist/playground/wasmJVersion.d.ts +2 -2
- package/dist/playground/wasmJVersion.js +2 -2
- package/dist/playground/wasmJanetVersion.js +2 -2
- package/dist/playground/wasmJuliaVersion.js +2 -2
- package/dist/playground/wasmLean4Version.d.ts +1 -110
- package/dist/playground/wasmLean4Version.js +2 -111
- package/dist/playground/wasmLfortranVersion.d.ts +2 -2
- package/dist/playground/wasmLfortranVersion.js +2 -2
- package/dist/playground/wasmLuaVersion.d.ts +1 -1
- package/dist/playground/wasmLuaVersion.js +1 -1
- package/dist/playground/wasmNimVersion.d.ts +9 -9
- package/dist/playground/wasmNimVersion.js +6 -6
- package/dist/playground/wasmOcamlVersion.d.ts +2 -2
- package/dist/playground/wasmOcamlVersion.js +1 -1
- package/dist/playground/wasmPascalVersion.js +2 -2
- package/dist/playground/wasmPerlVersion.js +2 -2
- package/dist/playground/wasmPrologVersion.d.ts +2 -2
- package/dist/playground/wasmPrologVersion.js +2 -2
- package/dist/playground/wasmReScriptVersion.d.ts +2 -2
- package/dist/playground/wasmReScriptVersion.js +2 -2
- package/dist/playground/wasmRustVersion.d.ts +26 -38
- package/dist/playground/wasmRustVersion.js +25 -40
- package/dist/playground/wasmTclVersion.d.ts +4 -4
- package/dist/playground/wasmTclVersion.js +2 -2
- package/dist/playground/wasmTinyGoVersion.d.ts +14 -14
- package/dist/playground/wasmTinyGoVersion.js +14 -14
- package/dist/playground/wasmTypeScriptVersion.d.ts +5 -5
- package/dist/playground/wasmTypeScriptVersion.js +5 -5
- package/dist/playground/worker/cachedClangCompile.js +52 -4
- package/dist/playground/worker/cachedClangExecute.js +6 -5
- package/dist/playground/worker/clang.js +33 -11
- package/dist/playground/worker/cobol.js +3 -3
- package/dist/playground/worker/d.js +63 -7
- package/dist/playground/worker/fortran.js +3 -3
- package/dist/playground/worker/go.js +16 -5
- package/dist/playground/worker/haskell.js +10 -7
- package/dist/playground/worker/java.js +263 -45
- package/dist/playground/worker/javaDebugEvaluator.d.ts +16 -0
- package/dist/playground/worker/javaDebugEvaluator.js +44 -0
- package/dist/playground/worker/javaDebugRuntime.d.ts +24 -0
- package/dist/playground/worker/javaDebugRuntime.js +315 -0
- package/dist/playground/worker/javaOutput.d.ts +11 -0
- package/dist/playground/worker/javaOutput.js +54 -0
- package/dist/playground/worker/lua.js +15 -3
- package/dist/playground/worker/php.js +15 -9
- package/dist/playground/worker/python.js +102 -30
- package/dist/playground/worker/pythonExecution.js +17 -1
- package/dist/playground/worker/pythonWorkspaceFiles.d.ts +6 -0
- package/dist/playground/worker/pythonWorkspaceFiles.js +26 -0
- package/dist/playground/worker/pythonWorkspaceImports.d.ts +4 -0
- package/dist/playground/worker/pythonWorkspaceImports.js +129 -0
- package/dist/playground/worker/r.js +13 -6
- package/dist/playground/worker/ruby.js +13 -0
- package/dist/playground/worker/rubyDebugPreview.d.ts +2 -0
- package/dist/playground/worker/rubyDebugPreview.js +80 -0
- package/dist/playground/worker/rubyTracepoint.d.ts +16 -0
- package/dist/playground/worker/rubyTracepoint.js +162 -0
- package/dist/playground/worker/rust.js +38 -3
- package/dist/playground/worker/sqlite.js +4 -3
- package/dist/playground/worker/tinygo.js +11 -3
- package/dist/playground/worker/typescript.js +2 -3
- package/dist/playground/worker/v.js +32 -7
- package/dist/playground/worker/wat.js +2 -3
- package/dist/playground/worker/zig.js +20 -6
- package/dist/playground/workerSession.d.ts +1 -28
- package/dist/playground/workerSession.js +1 -164
- package/dist/playground/zig.js +4 -0
- package/package.json +66 -15
- package/dist/playground/worker/python.runtime.cases.d.ts +0 -1
- package/dist/playground/worker/python.runtime.cases.js +0 -374
|
@@ -0,0 +1,561 @@
|
|
|
1
|
+
import { resolveSandboxExecutionArgs } from '../playground/options.js';
|
|
2
|
+
import { CLANG_WASI_DRIVER_TARGET, resolveClangLanguageArgs } from '@wasm-idle/llvm-core/core/clang-profile';
|
|
3
|
+
import { CLANG_SIMD_INCLUDE_PATH } from '@wasm-idle/llvm-core/core/clang-simd';
|
|
4
|
+
/** The Run button uses the same real Clang command and artifacts as Bash. Debug uses its qualified backend. */
|
|
5
|
+
export class ContainerClangRuntime {
|
|
6
|
+
profile;
|
|
7
|
+
files;
|
|
8
|
+
emit;
|
|
9
|
+
#active = new Map();
|
|
10
|
+
#prepared = new Map();
|
|
11
|
+
#input = new Map();
|
|
12
|
+
#header;
|
|
13
|
+
#disposed = false;
|
|
14
|
+
constructor(profile, files, emit) {
|
|
15
|
+
this.profile = profile;
|
|
16
|
+
this.files = files;
|
|
17
|
+
this.emit = emit;
|
|
18
|
+
}
|
|
19
|
+
queue(language) {
|
|
20
|
+
let value = this.#input.get(language);
|
|
21
|
+
if (!value)
|
|
22
|
+
this.#input.set(language, (value = { chunks: [], size: 0, eof: false }));
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
25
|
+
key(language, code, args, options) {
|
|
26
|
+
return JSON.stringify([
|
|
27
|
+
language,
|
|
28
|
+
code,
|
|
29
|
+
resolveSandboxExecutionArgs(language, args, options).compileArgs,
|
|
30
|
+
options.cppVersion,
|
|
31
|
+
options.cVersion,
|
|
32
|
+
options.activePath,
|
|
33
|
+
options.env
|
|
34
|
+
]);
|
|
35
|
+
}
|
|
36
|
+
currentProgram(program, key, artifact) {
|
|
37
|
+
const fs = this.profile.container.fs;
|
|
38
|
+
if (!program || program.key !== key || program.generation !== fs.generation)
|
|
39
|
+
return;
|
|
40
|
+
try {
|
|
41
|
+
if (fs.captureFileVersion(artifact) === program.version &&
|
|
42
|
+
fs.stat(artifact).mode & 0o111)
|
|
43
|
+
return program;
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
/* A removed or replaced artifact requires a new build. */
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
async spawn(job, command, args, options) {
|
|
50
|
+
job.signal.throwIfAborted();
|
|
51
|
+
const process = await this.profile.container.spawn(command, args, {
|
|
52
|
+
...options,
|
|
53
|
+
signal: job.signal
|
|
54
|
+
});
|
|
55
|
+
job.process = process;
|
|
56
|
+
// Stop may arrive while Container is still admitting the process. Keep the
|
|
57
|
+
// returned handle inside the same cancellation boundary before using it.
|
|
58
|
+
if (job.signal.aborted) {
|
|
59
|
+
await process.terminate().catch(() => { });
|
|
60
|
+
job.signal.throwIfAborted();
|
|
61
|
+
}
|
|
62
|
+
return process;
|
|
63
|
+
}
|
|
64
|
+
async drain(process, language, requestId, diagnostics = false, quiet = false, emit = this.emit) {
|
|
65
|
+
const output = async (stream, stderr) => {
|
|
66
|
+
const reader = stream.getReader(), decoder = new TextDecoder();
|
|
67
|
+
let pending = '';
|
|
68
|
+
try {
|
|
69
|
+
for (;;) {
|
|
70
|
+
const { value, done } = await reader.read();
|
|
71
|
+
const text = done ? decoder.decode() : decoder.decode(value, { stream: true });
|
|
72
|
+
if (text && !quiet)
|
|
73
|
+
emit({ language, name: 'output', data: text, requestId });
|
|
74
|
+
if (stderr && diagnostics) {
|
|
75
|
+
pending += text;
|
|
76
|
+
const lines = pending.split('\n');
|
|
77
|
+
pending = lines.pop() ?? '';
|
|
78
|
+
for (const line of lines) {
|
|
79
|
+
// Preserve terminal styling in output while editor diagnostics use plain text.
|
|
80
|
+
const plain = line.replace(/\x1b\[[0-?]*[ -/]*[@-~]/g, '');
|
|
81
|
+
const match = /^(.*?):(\d+):(\d+): (fatal error|error|warning|note): (.*)$/.exec(plain);
|
|
82
|
+
if (match)
|
|
83
|
+
emit({
|
|
84
|
+
language,
|
|
85
|
+
name: 'diagnostic',
|
|
86
|
+
requestId,
|
|
87
|
+
data: {
|
|
88
|
+
fileName: match[1].replace(`/workspace/${language}/`, '/workspace/'),
|
|
89
|
+
lineNumber: Number(match[2]),
|
|
90
|
+
columnNumber: Number(match[3]),
|
|
91
|
+
severity: match[4].includes('error')
|
|
92
|
+
? 'error'
|
|
93
|
+
: match[4] === 'warning'
|
|
94
|
+
? 'warning'
|
|
95
|
+
: 'other',
|
|
96
|
+
message: match[5]
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
if (pending.length > 65536)
|
|
101
|
+
pending = '';
|
|
102
|
+
}
|
|
103
|
+
if (done)
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
finally {
|
|
108
|
+
reader.releaseLock();
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
const [result] = await Promise.all([
|
|
112
|
+
process.exited,
|
|
113
|
+
output(process.stdout, false),
|
|
114
|
+
output(process.stderr, true)
|
|
115
|
+
]);
|
|
116
|
+
return result;
|
|
117
|
+
}
|
|
118
|
+
success(result, signal) {
|
|
119
|
+
if (signal.aborted)
|
|
120
|
+
throw signal.reason ?? new DOMException('Command cancelled', 'AbortError');
|
|
121
|
+
if (result.reason !== 'exited')
|
|
122
|
+
throw new Error(`Container command ended with ${result.reason}`);
|
|
123
|
+
return result.code === 0;
|
|
124
|
+
}
|
|
125
|
+
headerKey(language, code, compileArgs, options, cwd, standard) {
|
|
126
|
+
// A textual prefix and the fixed system headers are the only admitted inputs.
|
|
127
|
+
// Custom arguments, environment paths, or workspace headers can alter declarations.
|
|
128
|
+
if (language !== 'CPP' ||
|
|
129
|
+
compileArgs.length ||
|
|
130
|
+
Object.keys(options.env ?? {}).length ||
|
|
131
|
+
options.workspaceFiles?.some((file) => file.path !== options.activePath && !/\.(?:c|cc|cpp|cxx|wasm)$/i.test(file.path)))
|
|
132
|
+
return;
|
|
133
|
+
let rest = code.replace(/^\uFEFF/, '');
|
|
134
|
+
for (;;) {
|
|
135
|
+
rest = rest.replace(/^\s+/, '');
|
|
136
|
+
if (rest.startsWith('//')) {
|
|
137
|
+
const end = rest.indexOf('\n');
|
|
138
|
+
if (end < 0 || rest.slice(0, end).trimEnd().endsWith('\\'))
|
|
139
|
+
return;
|
|
140
|
+
rest = rest.slice(end + 1);
|
|
141
|
+
}
|
|
142
|
+
else if (rest.startsWith('/*')) {
|
|
143
|
+
const end = rest.indexOf('*/', 2);
|
|
144
|
+
if (end < 0)
|
|
145
|
+
return;
|
|
146
|
+
rest = rest.slice(end + 2);
|
|
147
|
+
}
|
|
148
|
+
else
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
if (!/^#[ \t]*include[ \t]*<bits\/stdc\+\+\.h>/.test(rest))
|
|
152
|
+
return;
|
|
153
|
+
return JSON.stringify([cwd, standard, '-fno-exceptions']);
|
|
154
|
+
}
|
|
155
|
+
discardHeader(source) {
|
|
156
|
+
if (!this.#header)
|
|
157
|
+
return;
|
|
158
|
+
try {
|
|
159
|
+
this.profile.container.fs.withMutationSource(source, () => this.profile.container.fs.unlink(this.#header.path));
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
/* Already removed. */
|
|
163
|
+
}
|
|
164
|
+
this.#header = undefined;
|
|
165
|
+
}
|
|
166
|
+
async prepare(language, code, args, options, signal, progress, requestId) {
|
|
167
|
+
if (this.#disposed)
|
|
168
|
+
throw new Error('Clang runtime has been disposed');
|
|
169
|
+
if (this.#active.has(language))
|
|
170
|
+
throw new Error(`${language} already has an active command`);
|
|
171
|
+
const controller = new AbortController();
|
|
172
|
+
signal = AbortSignal.any([signal, controller.signal]);
|
|
173
|
+
const job = { language, phase: 'prepare', controller, signal };
|
|
174
|
+
let mutations;
|
|
175
|
+
this.#active.set(language, job);
|
|
176
|
+
const previous = this.#prepared.get(language);
|
|
177
|
+
this.#prepared.delete(language);
|
|
178
|
+
try {
|
|
179
|
+
signal.throwIfAborted();
|
|
180
|
+
const input = this.files.inputs(language, code, options);
|
|
181
|
+
const cwd = this.files.directory(language), artifact = `${cwd}/program.wasm`, key = this.key(language, input.code, args, input.options);
|
|
182
|
+
const cached = this.currentProgram(previous, key, artifact);
|
|
183
|
+
if (cached?.reusable) {
|
|
184
|
+
progress?.report?.({
|
|
185
|
+
kind: 'activity',
|
|
186
|
+
phase: 'starting',
|
|
187
|
+
label: 'Reusing the compiled program'
|
|
188
|
+
});
|
|
189
|
+
const current = () => {
|
|
190
|
+
signal.throwIfAborted();
|
|
191
|
+
return this.currentProgram(cached, key, artifact);
|
|
192
|
+
};
|
|
193
|
+
// Progress/output callbacks may synchronously edit the workspace. Replay
|
|
194
|
+
// outputs first so an invalidation cannot leave old editor diagnostics
|
|
195
|
+
// alongside the rebuild's diagnostics. Delivered output cannot be retracted.
|
|
196
|
+
const replay = [
|
|
197
|
+
...cached.diagnostics.filter((event) => event.name !== 'diagnostic'),
|
|
198
|
+
...cached.diagnostics.filter((event) => event.name === 'diagnostic')
|
|
199
|
+
];
|
|
200
|
+
for (const event of replay) {
|
|
201
|
+
if (!current())
|
|
202
|
+
break;
|
|
203
|
+
this.emit({ ...structuredClone(event), requestId });
|
|
204
|
+
}
|
|
205
|
+
if (current()) {
|
|
206
|
+
this.#prepared.set(language, cached);
|
|
207
|
+
return true;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
progress?.report?.({
|
|
211
|
+
kind: 'activity',
|
|
212
|
+
phase: 'initializing',
|
|
213
|
+
label: 'Preparing the Clang toolchain'
|
|
214
|
+
});
|
|
215
|
+
const { compileArgs } = resolveSandboxExecutionArgs(language, args, options);
|
|
216
|
+
const sysrootProfile = this.profile.sysrootProfile === 'c-base-v1' &&
|
|
217
|
+
language === 'C' &&
|
|
218
|
+
input.options.activePath?.endsWith('.c') &&
|
|
219
|
+
!compileArgs.length &&
|
|
220
|
+
!Object.keys(options.env ?? {}).length
|
|
221
|
+
? 'c'
|
|
222
|
+
: 'full';
|
|
223
|
+
let sysrootArgs = [];
|
|
224
|
+
if (this.profile.prepareClang)
|
|
225
|
+
sysrootArgs = await this.profile.prepareClang(sysrootProfile, signal);
|
|
226
|
+
else
|
|
227
|
+
await this.profile.prepareCommand({
|
|
228
|
+
path: '/usr/local/bin/clang',
|
|
229
|
+
argv: ['clang'],
|
|
230
|
+
env: Object.entries(options.env ?? {}).map(([name, value]) => new TextEncoder().encode(`${name}=${value}`)),
|
|
231
|
+
cwd,
|
|
232
|
+
stdin: null,
|
|
233
|
+
stdout: null,
|
|
234
|
+
stderr: null
|
|
235
|
+
}, signal);
|
|
236
|
+
signal.throwIfAborted();
|
|
237
|
+
// Opaque flags/response files can request additional outputs or driver side effects.
|
|
238
|
+
// They retain only the immediate prepare-to-run handoff, not cross-Run reuse.
|
|
239
|
+
let reusable = compileArgs.length === 0 && Object.keys(options.env ?? {}).length === 0;
|
|
240
|
+
const mutationSources = new Set([job]);
|
|
241
|
+
try {
|
|
242
|
+
mutations = this.profile.container.fs.observeMutations();
|
|
243
|
+
}
|
|
244
|
+
catch {
|
|
245
|
+
reusable = false; /* Optional reuse must not prevent ordinary compilation. */
|
|
246
|
+
}
|
|
247
|
+
let diagnosticBytes = 0;
|
|
248
|
+
const diagnostics = [];
|
|
249
|
+
const emit = (event) => {
|
|
250
|
+
diagnosticBytes += new TextEncoder().encode(JSON.stringify(event)).byteLength;
|
|
251
|
+
if (diagnostics.length < 256 && diagnosticBytes <= 64 * 1024)
|
|
252
|
+
diagnostics.push(structuredClone(event));
|
|
253
|
+
else
|
|
254
|
+
reusable = false;
|
|
255
|
+
this.emit(event);
|
|
256
|
+
};
|
|
257
|
+
let oldVersion;
|
|
258
|
+
try {
|
|
259
|
+
oldVersion = this.profile.container.fs.captureFileVersion(artifact);
|
|
260
|
+
}
|
|
261
|
+
catch {
|
|
262
|
+
/* No previous output. */
|
|
263
|
+
}
|
|
264
|
+
const filenameCpp = /\.(?:cc|cpp|cxx|C)$/.test(input.options.activePath ?? '');
|
|
265
|
+
const filenameLanguage = language === 'CPP' || filenameCpp ? 'CPP' : 'C';
|
|
266
|
+
let sourceLanguage = filenameLanguage;
|
|
267
|
+
let omitBuiltinIncludes = false;
|
|
268
|
+
for (let index = 0; index < compileArgs.length; index++) {
|
|
269
|
+
const argument = compileArgs[index];
|
|
270
|
+
// Paths and forwarded arguments can themselves start with -x or @.
|
|
271
|
+
if ([
|
|
272
|
+
'-I',
|
|
273
|
+
'-isystem',
|
|
274
|
+
'-iquote',
|
|
275
|
+
'-idirafter',
|
|
276
|
+
'-include',
|
|
277
|
+
'-include-pch',
|
|
278
|
+
'-imacros',
|
|
279
|
+
'-isysroot',
|
|
280
|
+
'--sysroot',
|
|
281
|
+
'-resource-dir',
|
|
282
|
+
'-D',
|
|
283
|
+
'-U',
|
|
284
|
+
'-o',
|
|
285
|
+
'-L',
|
|
286
|
+
'-l',
|
|
287
|
+
'-B',
|
|
288
|
+
'-target',
|
|
289
|
+
'--target',
|
|
290
|
+
'-Xclang',
|
|
291
|
+
'-Xpreprocessor',
|
|
292
|
+
'-Xassembler',
|
|
293
|
+
'-Xlinker',
|
|
294
|
+
'-mllvm',
|
|
295
|
+
'-MF',
|
|
296
|
+
'-MT',
|
|
297
|
+
'-MQ'
|
|
298
|
+
].includes(argument)) {
|
|
299
|
+
index++;
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
if (argument === '-nostdinc' || argument === '-nobuiltininc')
|
|
303
|
+
omitBuiltinIncludes = true;
|
|
304
|
+
if (argument.startsWith('@'))
|
|
305
|
+
sourceLanguage = undefined;
|
|
306
|
+
else if (argument.startsWith('-x')) {
|
|
307
|
+
const value = argument === '-x' ? compileArgs[++index] : argument.slice(2);
|
|
308
|
+
if (value === 'none')
|
|
309
|
+
sourceLanguage = filenameLanguage;
|
|
310
|
+
else if (value === 'c' || value === 'c-header' || value === 'cpp-output')
|
|
311
|
+
sourceLanguage = 'C';
|
|
312
|
+
else if (value === 'c++' ||
|
|
313
|
+
value === 'c++-header' ||
|
|
314
|
+
value === 'c++-cpp-output')
|
|
315
|
+
sourceLanguage = 'CPP';
|
|
316
|
+
else
|
|
317
|
+
sourceLanguage = undefined;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
// Opaque response files choose their own source mode and standard.
|
|
321
|
+
const standard = sourceLanguage
|
|
322
|
+
? resolveClangLanguageArgs(sourceLanguage, options).standardArg.slice('-std='.length)
|
|
323
|
+
: undefined;
|
|
324
|
+
// An opaque response file or -x override can select C++ for a C editor.
|
|
325
|
+
// Keep its default source mode C while enabling the C++ driver's link libraries.
|
|
326
|
+
const override = compileArgs.some((arg) => arg.startsWith('@') || arg.startsWith('-x'));
|
|
327
|
+
const profileArgs = [];
|
|
328
|
+
for (let index = 0; index < sysrootArgs.length; index++) {
|
|
329
|
+
// The compatibility headers follow the driver's builtin-header policy.
|
|
330
|
+
// Keep target flags and the macro guard when builtin lookup is disabled.
|
|
331
|
+
if (omitBuiltinIncludes &&
|
|
332
|
+
sysrootArgs[index] === '-isystem' &&
|
|
333
|
+
sysrootArgs[index + 1]?.endsWith(`/${CLANG_SIMD_INCLUDE_PATH}`)) {
|
|
334
|
+
index++;
|
|
335
|
+
continue;
|
|
336
|
+
}
|
|
337
|
+
profileArgs.push(sysrootArgs[index]);
|
|
338
|
+
}
|
|
339
|
+
const common = [
|
|
340
|
+
...profileArgs,
|
|
341
|
+
`--target=${CLANG_WASI_DRIVER_TARGET}`,
|
|
342
|
+
...(sourceLanguage !== 'C' ? ['--driver-mode=g++'] : []),
|
|
343
|
+
...(language === 'C' && override && !filenameCpp ? ['-x', 'c'] : []),
|
|
344
|
+
...(standard ? [`-std=${standard}`] : []),
|
|
345
|
+
'-fcolor-diagnostics',
|
|
346
|
+
'-fno-exceptions',
|
|
347
|
+
...compileArgs
|
|
348
|
+
];
|
|
349
|
+
const compile = async (argv, quiet = false) => {
|
|
350
|
+
job.process = await this.spawn(job, 'clang', argv, {
|
|
351
|
+
cwd,
|
|
352
|
+
env: options.env
|
|
353
|
+
});
|
|
354
|
+
mutationSources.add(job.process.mutationSource ?? job.process.pid);
|
|
355
|
+
const drained = this.drain(job.process, language, requestId, !quiet, quiet, emit);
|
|
356
|
+
await job.process.stdin.getWriter().close();
|
|
357
|
+
const result = await drained;
|
|
358
|
+
return this.success(result, job.signal);
|
|
359
|
+
};
|
|
360
|
+
const headerKey = this.headerKey(language, input.code, compileArgs, input.options, cwd, standard);
|
|
361
|
+
if (headerKey && this.#header?.key !== headerKey)
|
|
362
|
+
this.discardHeader(job);
|
|
363
|
+
let header = headerKey === this.#header?.key ? this.#header : undefined;
|
|
364
|
+
if (header) {
|
|
365
|
+
// Restore owned bytes instead of trusting a writable temporary file.
|
|
366
|
+
const ownedHeader = header;
|
|
367
|
+
try {
|
|
368
|
+
this.profile.container.fs.withMutationSource(job, () => {
|
|
369
|
+
try {
|
|
370
|
+
this.profile.container.fs.unlink(ownedHeader.path);
|
|
371
|
+
}
|
|
372
|
+
catch {
|
|
373
|
+
/* Already removed. */
|
|
374
|
+
}
|
|
375
|
+
this.profile.container.fs.writeFile(ownedHeader.path, ownedHeader.bytes);
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
catch {
|
|
379
|
+
this.discardHeader(job);
|
|
380
|
+
header = undefined;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
let okay = await compile([
|
|
384
|
+
...common,
|
|
385
|
+
...(header ? ['-include-pch', header.path] : []),
|
|
386
|
+
input.options.activePath,
|
|
387
|
+
'-lc-printscan-long-double',
|
|
388
|
+
'-o',
|
|
389
|
+
artifact
|
|
390
|
+
]);
|
|
391
|
+
if (header && okay)
|
|
392
|
+
progress?.set?.(95, 'Compiled with precompiled <bits/stdc++.h>');
|
|
393
|
+
else if (header) {
|
|
394
|
+
this.discardHeader(job);
|
|
395
|
+
okay = await compile([
|
|
396
|
+
...common,
|
|
397
|
+
input.options.activePath,
|
|
398
|
+
'-lc-printscan-long-double',
|
|
399
|
+
'-o',
|
|
400
|
+
artifact
|
|
401
|
+
]);
|
|
402
|
+
}
|
|
403
|
+
if (okay && headerKey && !this.#header) {
|
|
404
|
+
const path = `/tmp/wasm-idle-stdcpp-${crypto.randomUUID()}.pch`;
|
|
405
|
+
try {
|
|
406
|
+
if (await compile([
|
|
407
|
+
...common,
|
|
408
|
+
'-c',
|
|
409
|
+
'-x',
|
|
410
|
+
'c++-header',
|
|
411
|
+
'/usr/share/wasm-sysroot/include/bits/stdc++.h',
|
|
412
|
+
'-o',
|
|
413
|
+
path
|
|
414
|
+
], true)) {
|
|
415
|
+
const bytes = this.profile.container.fs.readFile(path);
|
|
416
|
+
if (bytes.byteLength > 0 && bytes.byteLength <= 32 * 1024 * 1024)
|
|
417
|
+
this.#header = { key: headerKey, path, bytes };
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
catch (error) {
|
|
421
|
+
if (job.signal.aborted)
|
|
422
|
+
throw error;
|
|
423
|
+
}
|
|
424
|
+
finally {
|
|
425
|
+
if (this.#header?.path !== path) {
|
|
426
|
+
try {
|
|
427
|
+
this.profile.container.fs.withMutationSource(job, () => this.profile.container.fs.unlink(path));
|
|
428
|
+
}
|
|
429
|
+
catch {
|
|
430
|
+
/* No usable header. */
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
if (okay) {
|
|
436
|
+
if (mutations?.hasChangesOutside(mutationSources))
|
|
437
|
+
reusable = false;
|
|
438
|
+
signal.throwIfAborted();
|
|
439
|
+
const fs = this.profile.container.fs;
|
|
440
|
+
try {
|
|
441
|
+
const version = fs.captureFileVersion(artifact), stat = fs.stat(artifact);
|
|
442
|
+
if (version === oldVersion || !stat.size || !(stat.mode & 0o111))
|
|
443
|
+
return false;
|
|
444
|
+
this.#prepared.set(language, {
|
|
445
|
+
key,
|
|
446
|
+
generation: fs.generation,
|
|
447
|
+
version,
|
|
448
|
+
reusable,
|
|
449
|
+
diagnostics
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
catch {
|
|
453
|
+
return false;
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
return okay;
|
|
457
|
+
}
|
|
458
|
+
finally {
|
|
459
|
+
mutations?.dispose();
|
|
460
|
+
if (this.#active.get(language) === job)
|
|
461
|
+
this.#active.delete(language);
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
async run(language, code, args, options, signal, progress, requestId) {
|
|
465
|
+
if (this.#disposed)
|
|
466
|
+
throw new Error('Clang runtime has been disposed');
|
|
467
|
+
signal.throwIfAborted();
|
|
468
|
+
const input = this.files.inputs(language, code, options);
|
|
469
|
+
const cwd = this.files.directory(language), artifact = `${cwd}/program.wasm`;
|
|
470
|
+
if (!this.currentProgram(this.#prepared.get(language), this.key(language, input.code, args, input.options), artifact) &&
|
|
471
|
+
!(await this.prepare(language, code, args, options, signal, progress, requestId)))
|
|
472
|
+
return false;
|
|
473
|
+
const program = this.currentProgram(this.#prepared.get(language), this.key(language, input.code, args, input.options), artifact);
|
|
474
|
+
if (!program)
|
|
475
|
+
return false;
|
|
476
|
+
if (!program.reusable)
|
|
477
|
+
this.#prepared.delete(language);
|
|
478
|
+
const controller = new AbortController();
|
|
479
|
+
signal = AbortSignal.any([signal, controller.signal]);
|
|
480
|
+
const job = { language, phase: 'run', controller, signal };
|
|
481
|
+
if (this.#active.has(language))
|
|
482
|
+
throw new Error(`${language} already has an active command`);
|
|
483
|
+
this.#active.set(language, job);
|
|
484
|
+
try {
|
|
485
|
+
const { programArgs } = resolveSandboxExecutionArgs(language, args, options);
|
|
486
|
+
job.process = await this.spawn(job, 'wasm-run', [`${cwd}/program.wasm`, ...programArgs], { cwd, env: options.env });
|
|
487
|
+
job.writer = job.process.stdin.getWriter();
|
|
488
|
+
progress?.report?.({
|
|
489
|
+
kind: 'ready',
|
|
490
|
+
state: 'running',
|
|
491
|
+
reason: 'started',
|
|
492
|
+
label: 'Container program started'
|
|
493
|
+
});
|
|
494
|
+
const drained = this.drain(job.process, language, requestId);
|
|
495
|
+
if (options.stdin !== undefined) {
|
|
496
|
+
await job.writer.write(new TextEncoder().encode(options.stdin));
|
|
497
|
+
await job.writer.close();
|
|
498
|
+
}
|
|
499
|
+
else
|
|
500
|
+
await this.flush(language);
|
|
501
|
+
return this.success(await drained, job.signal);
|
|
502
|
+
}
|
|
503
|
+
finally {
|
|
504
|
+
job.writer?.releaseLock();
|
|
505
|
+
this.#input.delete(language);
|
|
506
|
+
if (this.#active.get(language) === job)
|
|
507
|
+
this.#active.delete(language);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
async flush(language) {
|
|
511
|
+
const writer = this.#active.get(language)?.writer;
|
|
512
|
+
if (!writer)
|
|
513
|
+
return;
|
|
514
|
+
const queue = this.queue(language);
|
|
515
|
+
for (const text of queue.chunks.splice(0)) {
|
|
516
|
+
const bytes = new TextEncoder().encode(text);
|
|
517
|
+
await writer.write(bytes);
|
|
518
|
+
queue.size -= bytes.byteLength;
|
|
519
|
+
}
|
|
520
|
+
if (queue.eof) {
|
|
521
|
+
queue.eof = false;
|
|
522
|
+
await writer.close();
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
async write(language, text) {
|
|
526
|
+
const q = this.queue(language);
|
|
527
|
+
q.size += new TextEncoder().encode(text).length;
|
|
528
|
+
if (q.size > 8 * 1024 * 1024)
|
|
529
|
+
throw new Error('Pending stdin exceeds its limit');
|
|
530
|
+
q.chunks.push(text);
|
|
531
|
+
await this.flush(language);
|
|
532
|
+
}
|
|
533
|
+
async eof(language) {
|
|
534
|
+
this.queue(language).eof = true;
|
|
535
|
+
await this.flush(language);
|
|
536
|
+
}
|
|
537
|
+
async stop(language) {
|
|
538
|
+
this.#input.delete(language);
|
|
539
|
+
const job = this.#active.get(language);
|
|
540
|
+
job?.controller.abort(new DOMException('Command cancelled', 'AbortError'));
|
|
541
|
+
await job?.process?.terminate();
|
|
542
|
+
}
|
|
543
|
+
clear(language) {
|
|
544
|
+
this.#input.delete(language);
|
|
545
|
+
}
|
|
546
|
+
inspect() {
|
|
547
|
+
return [...this.#active.values()].map((job) => ({
|
|
548
|
+
language: job.language,
|
|
549
|
+
phase: job.phase,
|
|
550
|
+
pid: job.process?.pid,
|
|
551
|
+
filesystem: 'live'
|
|
552
|
+
}));
|
|
553
|
+
}
|
|
554
|
+
async dispose() {
|
|
555
|
+
this.#disposed = true;
|
|
556
|
+
this.#prepared.clear();
|
|
557
|
+
await Promise.allSettled([...this.#active.keys()].map((language) => this.stop(language)));
|
|
558
|
+
this.discardHeader();
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
export { ContainerClangRuntime as WebsiteClangRuntime };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type ContainerWorkerCallOptions, type ContainerWorkerEvent, type ContainerWorkerInitialization } from './protocol.js';
|
|
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
|
+
export type ContainerOwnerWorker = Pick<Worker, 'postMessage' | 'addEventListener' | 'removeEventListener' | 'terminate'>;
|
|
8
|
+
export interface ContainerWorkerClientOptions extends ContainerWorkerInitialization {
|
|
9
|
+
/** The application owns and bundles the worker entry module. */
|
|
10
|
+
workerFactory: () => ContainerOwnerWorker;
|
|
11
|
+
initializationTimeoutMs?: number;
|
|
12
|
+
cleanupDeadlineMs?: number;
|
|
13
|
+
}
|
|
14
|
+
export declare function createContainerWorkerClient(options: ContainerWorkerClientOptions): ContainerWorkerClient;
|