wasm-idle 1.3.0 → 1.4.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 +183 -147
- package/dist/container/clang-runtime.js +32 -8
- package/dist/container/runtime-adapter.d.ts +2 -0
- package/dist/container/runtime-adapter.js +37 -8
- package/dist/playground/assetBridge.js +10 -8
- package/dist/playground/assets.d.ts +4 -0
- package/dist/playground/assets.js +9 -4
- package/dist/playground/c3.js +1 -1
- package/dist/playground/compiledLldb.d.ts +2 -0
- package/dist/playground/compiledLldb.js +37 -2
- package/dist/playground/d.d.ts +1 -0
- package/dist/playground/d.js +18 -8
- package/dist/playground/index.d.ts +1 -1
- package/dist/playground/index.js +7 -0
- package/dist/playground/{java-debug-HUZVAK4F.js → java-debug-64JGCLTC.js} +1 -1
- package/dist/playground/java-debug-notices.txt +26 -1
- package/dist/playground/java.js +3 -2
- package/dist/playground/javaDebugBridge.js +42 -7
- package/dist/playground/javaDebugSource.js +4 -4
- package/dist/playground/javaMain.d.ts +3 -0
- package/dist/playground/javaMain.js +2 -0
- package/dist/playground/javaRuntimeErrors.d.ts +2 -1
- package/dist/playground/javaRuntimeErrors.js +9 -2
- package/dist/playground/javaSource.d.ts +1 -1
- package/dist/playground/javaSource.js +7 -2
- package/dist/playground/lldbSession.d.ts +1 -0
- package/dist/playground/lldbSession.js +15 -2
- package/dist/playground/objectivec.js +2 -1
- package/dist/playground/options.d.ts +1 -1
- package/dist/playground/pythonCompileOptions.d.ts +7 -4
- package/dist/playground/pythonCompileOptions.js +2 -2
- package/dist/playground/scala.d.ts +34 -0
- package/dist/playground/scala.js +397 -0
- package/dist/playground/scalaAssets.d.ts +16 -0
- package/dist/playground/scalaAssets.js +51 -0
- package/dist/playground/scalaDiagnostics.d.ts +3 -0
- package/dist/playground/scalaDiagnostics.js +29 -0
- package/dist/playground/staticWorkerRuntime.d.ts +2 -0
- package/dist/playground/staticWorkerRuntime.js +85 -11
- package/dist/playground/tinygo.js +21 -8
- package/dist/playground/tinygoExecutableGraph.js +1 -1
- package/dist/playground/v.d.ts +1 -0
- package/dist/playground/v.js +17 -7
- package/dist/playground/wasmC3Version.d.ts +8 -8
- package/dist/playground/wasmC3Version.js +8 -8
- package/dist/playground/wasmDotnetVersion.d.ts +1 -1
- package/dist/playground/wasmDotnetVersion.js +1 -1
- package/dist/playground/wasmGoVersion.d.ts +1 -1
- package/dist/playground/wasmGoVersion.js +1 -1
- package/dist/playground/wasmLispVersion.d.ts +5 -0
- package/dist/playground/wasmLispVersion.js +5 -0
- package/dist/playground/wasmOcamlVersion.d.ts +4 -4
- package/dist/playground/wasmOcamlVersion.js +3 -3
- package/dist/playground/wasmTinyGoVersion.d.ts +57 -57
- package/dist/playground/wasmTinyGoVersion.js +57 -57
- package/dist/playground/wasmZigVersion.d.ts +7 -7
- package/dist/playground/wasmZigVersion.js +7 -7
- package/dist/playground/worker/java.js +18 -4
- package/dist/playground/worker/javaDebugEvaluator.js +2 -2
- package/dist/playground/worker/javaDebugRuntime.js +8 -1
- package/dist/playground/worker/javaStreaming.js +4 -2
- package/dist/playground/worker/ocaml.js +11 -10
- package/dist/playground/worker/python.js +21 -164
- package/dist/playground/worker/pythonCompile.js +5 -0
- package/dist/playground/worker/pythonDebug.d.ts +20 -0
- package/dist/playground/worker/pythonDebug.js +205 -0
- package/dist/playground/worker/scala/UPSTREAM-LICENSE.txt +201 -0
- package/dist/playground/worker/scala/UPSTREAM-NOTICE.txt +108 -0
- package/dist/playground/worker/scala/asset-bootstrap.d.ts +6 -0
- package/dist/playground/worker/scala/asset-bootstrap.js +58 -0
- package/dist/playground/worker/scala/asset-receipts.d.ts +7 -0
- package/dist/playground/worker/scala/asset-receipts.js +58 -0
- package/dist/playground/worker/scala/compiler.worker.d.ts +1 -0
- package/dist/playground/worker/scala/compiler.worker.js +204 -0
- package/dist/playground/worker/scala/execution.worker.d.ts +1 -0
- package/dist/playground/worker/scala/execution.worker.js +89 -0
- package/dist/playground/worker/scala/memory-fs.d.ts +29 -0
- package/dist/playground/worker/scala/memory-fs.js +333 -0
- package/dist/playground/worker/scala/runner.d.ts +1 -0
- package/dist/playground/worker/scala/runner.js +36 -0
- package/dist/playground/worker/zig.d.ts +2 -1
- package/dist/playground/worker/zig.js +212 -86
- package/dist/playground/worker/zigDebug.d.ts +39 -0
- package/dist/playground/worker/zigDebug.js +156 -0
- package/dist/playground/zig.d.ts +27 -4
- package/dist/playground/zig.js +266 -23
- package/dist/playground/zigDebugValues.d.ts +20 -0
- package/dist/playground/zigDebugValues.js +248 -0
- package/package.json +17 -10
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
import { BusyError, CancelledError, CompileError, DiagnosticLimitError, OutputLimitError, ProtocolError, RuntimeConfigurationError, RuntimeExecutionError, TimeoutError, UnsupportedBrowserFeatureError, resolveExecutionLimits, validateWorkspaceFiles } from '@wasm-idle/core';
|
|
2
|
+
import { resolveSandboxExecutionArgs } from './options';
|
|
3
|
+
import { createScalaCompilerPayload } from './scalaAssets';
|
|
4
|
+
import { parseScalaDiagnostics } from './scalaDiagnostics';
|
|
5
|
+
import { reportWorkerProgress } from './workerProgress';
|
|
6
|
+
const encoder = new TextEncoder();
|
|
7
|
+
const HARD_SOURCE_BYTES = 1024 * 1024;
|
|
8
|
+
/** Scala 3 through the genuine browser compiler and Scala.js linker. */
|
|
9
|
+
export default class Scala {
|
|
10
|
+
output;
|
|
11
|
+
oncompilerdiagnostic;
|
|
12
|
+
elapse = 0;
|
|
13
|
+
compiler = null;
|
|
14
|
+
execution = null;
|
|
15
|
+
active = null;
|
|
16
|
+
disposed = false;
|
|
17
|
+
sequence = 0;
|
|
18
|
+
payload = null;
|
|
19
|
+
payloadKey = '';
|
|
20
|
+
baseUrl;
|
|
21
|
+
cache;
|
|
22
|
+
pendingInput = '';
|
|
23
|
+
begin(signal) {
|
|
24
|
+
if (this.disposed)
|
|
25
|
+
throw new RuntimeConfigurationError('Scala sandbox is disposed', {
|
|
26
|
+
runtimeId: 'SCALA'
|
|
27
|
+
});
|
|
28
|
+
if (this.active)
|
|
29
|
+
throw new BusyError('Scala sandbox is busy', { runtimeId: 'SCALA' });
|
|
30
|
+
if (signal?.aborted)
|
|
31
|
+
throw new CancelledError('Scala request aborted', {
|
|
32
|
+
runtimeId: 'SCALA',
|
|
33
|
+
cause: signal.reason
|
|
34
|
+
});
|
|
35
|
+
const controller = new AbortController();
|
|
36
|
+
const operation = {
|
|
37
|
+
controller,
|
|
38
|
+
signal,
|
|
39
|
+
abort: () => controller.abort(signal?.reason)
|
|
40
|
+
};
|
|
41
|
+
signal?.addEventListener('abort', operation.abort, { once: true });
|
|
42
|
+
this.active = operation;
|
|
43
|
+
return operation;
|
|
44
|
+
}
|
|
45
|
+
check(operation) {
|
|
46
|
+
if (this.disposed || this.active !== operation || operation.controller.signal.aborted)
|
|
47
|
+
throw new CancelledError('Scala request cancelled', {
|
|
48
|
+
runtimeId: 'SCALA',
|
|
49
|
+
cause: operation.controller.signal.reason
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
end(operation) {
|
|
53
|
+
operation.signal?.removeEventListener('abort', operation.abort);
|
|
54
|
+
if (this.active === operation)
|
|
55
|
+
this.active = null;
|
|
56
|
+
}
|
|
57
|
+
validateOptions(options) {
|
|
58
|
+
if ((options.debugMode && options.debugMode !== 'none') ||
|
|
59
|
+
options.debug ||
|
|
60
|
+
options.interactive ||
|
|
61
|
+
options.compileArgs?.length ||
|
|
62
|
+
Object.keys(options.env ?? {}).length)
|
|
63
|
+
throw new RuntimeConfigurationError('Scala.js supports buffered stdin and program arguments; debugging, interactive input, compiler options and environment overrides are unavailable.', { runtimeId: 'SCALA' });
|
|
64
|
+
for (const feature of ['JSTag', 'Suspending', 'promising']) {
|
|
65
|
+
if (typeof globalThis.WebAssembly?.[feature] === 'undefined')
|
|
66
|
+
throw new UnsupportedBrowserFeatureError(`WebAssembly.${feature} (Scala browser compiler)`, { runtimeId: 'SCALA' });
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
files(code, options, limits) {
|
|
70
|
+
const activePath = options.activePath ?? 'Main.scala';
|
|
71
|
+
const files = (options.workspaceFiles?.length
|
|
72
|
+
? options.workspaceFiles
|
|
73
|
+
: [{ path: activePath, content: code }]).map((file) => ({
|
|
74
|
+
path: file.path,
|
|
75
|
+
content: file.path === activePath ? code : file.content
|
|
76
|
+
}));
|
|
77
|
+
if (!files.some((file) => file.path === activePath))
|
|
78
|
+
files.push({ path: activePath, content: code });
|
|
79
|
+
const validated = validateWorkspaceFiles(files, {
|
|
80
|
+
...options.workspaceLimits,
|
|
81
|
+
maxFiles: Math.min(options.workspaceLimits?.maxFiles ?? 64, 64),
|
|
82
|
+
maxFileBytes: Math.min(options.workspaceLimits?.maxFileBytes ?? HARD_SOURCE_BYTES, HARD_SOURCE_BYTES),
|
|
83
|
+
maxTotalBytes: Math.min(options.workspaceLimits?.maxTotalBytes ?? HARD_SOURCE_BYTES, HARD_SOURCE_BYTES, limits.maxWorkspaceBytes)
|
|
84
|
+
});
|
|
85
|
+
for (const file of validated) {
|
|
86
|
+
if (!/^(?:[A-Za-z0-9_-]+\/)*[A-Za-z0-9_-]+\.scala$/.test(file.path) ||
|
|
87
|
+
file.path === 'WasmIdleScalaRunner.scala')
|
|
88
|
+
throw new RuntimeConfigurationError(`Unsupported Scala source path: ${file.path}`, {
|
|
89
|
+
runtimeId: 'SCALA'
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
return validated;
|
|
93
|
+
}
|
|
94
|
+
retire(worker) {
|
|
95
|
+
worker.terminate();
|
|
96
|
+
if (this.compiler === worker)
|
|
97
|
+
this.compiler = null;
|
|
98
|
+
if (this.execution === worker)
|
|
99
|
+
this.execution = null;
|
|
100
|
+
}
|
|
101
|
+
request(worker, payload, operation, phase, timeoutMs, onOutput, progress) {
|
|
102
|
+
this.check(operation);
|
|
103
|
+
return new Promise((resolve, reject) => {
|
|
104
|
+
const id = ++this.sequence;
|
|
105
|
+
let settled = false;
|
|
106
|
+
const finish = (error, reply) => {
|
|
107
|
+
if (settled)
|
|
108
|
+
return;
|
|
109
|
+
settled = true;
|
|
110
|
+
clearTimeout(timer);
|
|
111
|
+
operation.controller.signal.removeEventListener('abort', abort);
|
|
112
|
+
worker.onmessage = null;
|
|
113
|
+
worker.onerror = null;
|
|
114
|
+
worker.onmessageerror = null;
|
|
115
|
+
if (error) {
|
|
116
|
+
this.retire(worker);
|
|
117
|
+
reject(error);
|
|
118
|
+
}
|
|
119
|
+
else
|
|
120
|
+
resolve(reply);
|
|
121
|
+
};
|
|
122
|
+
const abort = () => finish(new CancelledError('Scala request cancelled', { runtimeId: 'SCALA', phase }));
|
|
123
|
+
const timer = setTimeout(() => finish(new TimeoutError(`Scala ${phase} timed out`, {
|
|
124
|
+
runtimeId: 'SCALA',
|
|
125
|
+
phase,
|
|
126
|
+
timeoutMs
|
|
127
|
+
})), timeoutMs);
|
|
128
|
+
operation.controller.signal.addEventListener('abort', abort, { once: true });
|
|
129
|
+
worker.onmessage = ({ data }) => {
|
|
130
|
+
if (data.id !== id)
|
|
131
|
+
return;
|
|
132
|
+
try {
|
|
133
|
+
if (data.type === 'execution-ready') {
|
|
134
|
+
reportWorkerProgress(progress, data.progress);
|
|
135
|
+
}
|
|
136
|
+
else if (data.type === 'stdout' || data.type === 'stderr') {
|
|
137
|
+
if (typeof data.data !== 'string')
|
|
138
|
+
throw new ProtocolError('Invalid Scala output');
|
|
139
|
+
onOutput?.(data.type, data.data);
|
|
140
|
+
}
|
|
141
|
+
else if (['ready', 'compiled', 'exit', 'error'].includes(data.type))
|
|
142
|
+
finish(undefined, data);
|
|
143
|
+
}
|
|
144
|
+
catch (error) {
|
|
145
|
+
finish(error);
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
worker.onerror = (event) => finish(new RuntimeExecutionError(event.message || 'Scala worker failed', {
|
|
149
|
+
runtimeId: 'SCALA',
|
|
150
|
+
phase
|
|
151
|
+
}));
|
|
152
|
+
worker.onmessageerror = () => finish(new ProtocolError('Scala worker message cannot be decoded', {
|
|
153
|
+
runtimeId: 'SCALA'
|
|
154
|
+
}));
|
|
155
|
+
try {
|
|
156
|
+
worker.postMessage({ id, ...payload });
|
|
157
|
+
}
|
|
158
|
+
catch (error) {
|
|
159
|
+
finish(error);
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
async ensureCompiler(operation, limits, options, progress) {
|
|
164
|
+
this.check(operation);
|
|
165
|
+
const baseUrl = this.baseUrl;
|
|
166
|
+
const cache = options.persistentCache ?? this.cache;
|
|
167
|
+
const key = JSON.stringify([baseUrl, cache]);
|
|
168
|
+
if (this.payload &&
|
|
169
|
+
(this.payloadKey !== key ||
|
|
170
|
+
Object.values(this.payload).some((bytes) => bytes.length > limits.maxAssetBytes))) {
|
|
171
|
+
if (this.compiler)
|
|
172
|
+
this.retire(this.compiler);
|
|
173
|
+
this.payload = null;
|
|
174
|
+
}
|
|
175
|
+
if (this.compiler)
|
|
176
|
+
return;
|
|
177
|
+
if (!this.payload) {
|
|
178
|
+
const controller = new AbortController();
|
|
179
|
+
const abort = () => controller.abort(operation.controller.signal.reason);
|
|
180
|
+
const timer = setTimeout(() => controller.abort(new TimeoutError('Scala asset loading timed out', {
|
|
181
|
+
runtimeId: 'SCALA',
|
|
182
|
+
phase: 'asset',
|
|
183
|
+
timeoutMs: limits.assetTimeoutMs
|
|
184
|
+
})), limits.assetTimeoutMs);
|
|
185
|
+
operation.controller.signal.addEventListener('abort', abort, { once: true });
|
|
186
|
+
try {
|
|
187
|
+
progress?.set?.(0.05, 'Downloading verified Scala compiler assets');
|
|
188
|
+
this.payload = await createScalaCompilerPayload({
|
|
189
|
+
baseUrl,
|
|
190
|
+
persistentCache: cache,
|
|
191
|
+
signal: controller.signal,
|
|
192
|
+
maxAssetBytes: limits.maxAssetBytes
|
|
193
|
+
});
|
|
194
|
+
this.payloadKey = key;
|
|
195
|
+
}
|
|
196
|
+
catch (error) {
|
|
197
|
+
if (operation.controller.signal.aborted)
|
|
198
|
+
throw new CancelledError('Scala asset loading cancelled', {
|
|
199
|
+
runtimeId: 'SCALA',
|
|
200
|
+
phase: 'asset'
|
|
201
|
+
});
|
|
202
|
+
if (controller.signal.aborted)
|
|
203
|
+
throw (controller.signal.reason ??
|
|
204
|
+
new CancelledError('Scala asset download cancelled'));
|
|
205
|
+
throw error;
|
|
206
|
+
}
|
|
207
|
+
finally {
|
|
208
|
+
clearTimeout(timer);
|
|
209
|
+
operation.controller.signal.removeEventListener('abort', abort);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
this.check(operation);
|
|
213
|
+
progress?.set?.(0.25, 'Initializing Scala 3 compiler');
|
|
214
|
+
this.check(operation);
|
|
215
|
+
const worker = new Worker(new URL('./worker/scala/compiler.worker.js', import.meta.url), {
|
|
216
|
+
type: 'module'
|
|
217
|
+
});
|
|
218
|
+
this.compiler = worker;
|
|
219
|
+
const reply = await this.request(worker, { type: 'initialize', runtimeAssets: this.payload }, operation, 'startup', limits.startupTimeoutMs);
|
|
220
|
+
this.check(operation);
|
|
221
|
+
if (reply.type !== 'ready') {
|
|
222
|
+
this.retire(worker);
|
|
223
|
+
throw new RuntimeExecutionError(reply.message ?? 'Scala initialization failed', {
|
|
224
|
+
runtimeId: 'SCALA',
|
|
225
|
+
phase: 'startup'
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
async load(runtimeAssets = '', _code = '', _log = true, _args = [], options = {}, progress) {
|
|
230
|
+
const operation = this.begin(options.signal);
|
|
231
|
+
try {
|
|
232
|
+
this.validateOptions(options);
|
|
233
|
+
const config = typeof runtimeAssets === 'object' ? runtimeAssets.scala : undefined;
|
|
234
|
+
const baseUrl = config?.baseUrl ??
|
|
235
|
+
(typeof runtimeAssets === 'string' && runtimeAssets
|
|
236
|
+
? `${runtimeAssets.replace(/\/$/, '')}/wasm-scala/`
|
|
237
|
+
: undefined);
|
|
238
|
+
if (baseUrl !== this.baseUrl) {
|
|
239
|
+
if (this.compiler)
|
|
240
|
+
this.retire(this.compiler);
|
|
241
|
+
this.payload = null;
|
|
242
|
+
}
|
|
243
|
+
this.baseUrl = baseUrl;
|
|
244
|
+
this.cache =
|
|
245
|
+
typeof runtimeAssets === 'object' ? runtimeAssets.persistentCache : undefined;
|
|
246
|
+
await this.ensureCompiler(operation, resolveExecutionLimits(options.limits), options, progress);
|
|
247
|
+
}
|
|
248
|
+
finally {
|
|
249
|
+
this.end(operation);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
async run(code, _prepare = false, _log = true, progress, args = [], options = {}) {
|
|
253
|
+
const operation = this.begin(options.signal);
|
|
254
|
+
let outcome = 'failed';
|
|
255
|
+
const start = performance.now();
|
|
256
|
+
try {
|
|
257
|
+
this.validateOptions(options);
|
|
258
|
+
const requestedLimits = resolveExecutionLimits(options.limits);
|
|
259
|
+
const limits = {
|
|
260
|
+
...requestedLimits,
|
|
261
|
+
maxOutputBytes: Math.min(requestedLimits.maxOutputBytes, 1024 * 1024)
|
|
262
|
+
};
|
|
263
|
+
const files = this.files(code, options, limits);
|
|
264
|
+
const { programArgs } = resolveSandboxExecutionArgs('SCALA', args, options);
|
|
265
|
+
if (encoder.encode(programArgs.join('')).length > HARD_SOURCE_BYTES)
|
|
266
|
+
throw new RuntimeConfigurationError('Scala program arguments exceed 1 MiB', {
|
|
267
|
+
runtimeId: 'SCALA'
|
|
268
|
+
});
|
|
269
|
+
const stdin = options.stdin ?? this.pendingInput;
|
|
270
|
+
this.pendingInput = '';
|
|
271
|
+
if (encoder.encode(stdin).length > HARD_SOURCE_BYTES)
|
|
272
|
+
throw new RuntimeConfigurationError('Scala stdin exceeds 1 MiB', {
|
|
273
|
+
runtimeId: 'SCALA'
|
|
274
|
+
});
|
|
275
|
+
await this.ensureCompiler(operation, limits, options, progress);
|
|
276
|
+
this.check(operation);
|
|
277
|
+
progress?.set?.(0.4, 'Compiling Scala and linking Scala.js');
|
|
278
|
+
const compiled = await this.request(this.compiler, {
|
|
279
|
+
files,
|
|
280
|
+
limits: {
|
|
281
|
+
maxDiagnostics: limits.maxDiagnostics,
|
|
282
|
+
maxDiagnosticBytes: limits.maxOutputBytes
|
|
283
|
+
}
|
|
284
|
+
}, operation, 'compile', limits.compileTimeoutMs);
|
|
285
|
+
this.check(operation);
|
|
286
|
+
if (compiled.failureKind === 'diagnostic-limit') {
|
|
287
|
+
if (this.compiler)
|
|
288
|
+
this.retire(this.compiler);
|
|
289
|
+
throw new DiagnosticLimitError(compiled.message ?? 'Scala diagnostic limit exceeded', {
|
|
290
|
+
runtimeId: 'SCALA',
|
|
291
|
+
limit: compiled.limit ?? limits.maxDiagnostics,
|
|
292
|
+
actual: compiled.actual ?? limits.maxDiagnostics + 1
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
const lines = compiled.diagnostics ?? [];
|
|
296
|
+
if (!Array.isArray(lines) || lines.some((line) => typeof line !== 'string'))
|
|
297
|
+
throw new ProtocolError('Invalid Scala diagnostics');
|
|
298
|
+
if (encoder.encode(lines.join('\n')).length > limits.maxOutputBytes)
|
|
299
|
+
throw new DiagnosticLimitError('Scala diagnostics exceed byte budget', {
|
|
300
|
+
runtimeId: 'SCALA',
|
|
301
|
+
limit: limits.maxOutputBytes,
|
|
302
|
+
actual: encoder.encode(lines.join('\n')).length
|
|
303
|
+
});
|
|
304
|
+
const diagnostics = parseScalaDiagnostics(lines, limits.maxDiagnostics);
|
|
305
|
+
for (const diagnostic of diagnostics)
|
|
306
|
+
this.oncompilerdiagnostic?.(diagnostic);
|
|
307
|
+
if (compiled.type !== 'compiled' || typeof compiled.code !== 'string') {
|
|
308
|
+
if (this.compiler)
|
|
309
|
+
this.retire(this.compiler);
|
|
310
|
+
throw new CompileError(lines.join('\n').replace(/\x1b\[[0-9;]*m/g, '') ||
|
|
311
|
+
compiled.message ||
|
|
312
|
+
'Scala compilation failed', { runtimeId: 'SCALA' });
|
|
313
|
+
}
|
|
314
|
+
if (encoder.encode(compiled.code).length > 32 * 1024 * 1024)
|
|
315
|
+
throw new CompileError('Scala generated program exceeds 32 MiB', {
|
|
316
|
+
runtimeId: 'SCALA'
|
|
317
|
+
});
|
|
318
|
+
if (limits.maxWorkers < 2 && this.compiler)
|
|
319
|
+
this.retire(this.compiler);
|
|
320
|
+
this.check(operation);
|
|
321
|
+
const execution = new Worker(new URL('./worker/scala/execution.worker.js', import.meta.url), { type: 'module' });
|
|
322
|
+
this.execution = execution;
|
|
323
|
+
let outputBytes = 0;
|
|
324
|
+
const result = await this.request(execution, {
|
|
325
|
+
code: compiled.code,
|
|
326
|
+
stdin,
|
|
327
|
+
args: programArgs,
|
|
328
|
+
maxOutputBytes: limits.maxOutputBytes
|
|
329
|
+
}, operation, 'execute', limits.runTimeoutMs, (_stream, text) => {
|
|
330
|
+
outputBytes += encoder.encode(text).length;
|
|
331
|
+
if (outputBytes > limits.maxOutputBytes)
|
|
332
|
+
throw new OutputLimitError('Scala output limit exceeded', {
|
|
333
|
+
runtimeId: 'SCALA',
|
|
334
|
+
limit: limits.maxOutputBytes,
|
|
335
|
+
actual: outputBytes
|
|
336
|
+
});
|
|
337
|
+
this.output?.(text);
|
|
338
|
+
}, progress);
|
|
339
|
+
this.check(operation);
|
|
340
|
+
if (result.type !== 'exit' || result.exitCode !== 0) {
|
|
341
|
+
if (/output exceeds/.test(result.message ?? ''))
|
|
342
|
+
throw new OutputLimitError(result.message, {
|
|
343
|
+
runtimeId: 'SCALA',
|
|
344
|
+
limit: limits.maxOutputBytes,
|
|
345
|
+
actual: limits.maxOutputBytes + 1
|
|
346
|
+
});
|
|
347
|
+
throw new RuntimeExecutionError(result.message ?? 'Scala program failed', {
|
|
348
|
+
runtimeId: 'SCALA'
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
outcome = 'completed';
|
|
352
|
+
return true;
|
|
353
|
+
}
|
|
354
|
+
catch (error) {
|
|
355
|
+
if (operation.controller.signal.aborted || error instanceof CancelledError)
|
|
356
|
+
outcome = 'cancelled';
|
|
357
|
+
else if (error instanceof TimeoutError)
|
|
358
|
+
outcome = 'timed-out';
|
|
359
|
+
throw error;
|
|
360
|
+
}
|
|
361
|
+
finally {
|
|
362
|
+
if (this.execution)
|
|
363
|
+
this.retire(this.execution);
|
|
364
|
+
this.elapse = performance.now() - start;
|
|
365
|
+
this.end(operation);
|
|
366
|
+
progress?.report?.({
|
|
367
|
+
kind: 'settled',
|
|
368
|
+
outcome
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
write(data) {
|
|
373
|
+
// The terminal may submit late keystrokes; they cannot extend buffered execution.
|
|
374
|
+
if (this.active || this.disposed)
|
|
375
|
+
return;
|
|
376
|
+
if (encoder.encode(this.pendingInput + data).length > HARD_SOURCE_BYTES)
|
|
377
|
+
throw new RuntimeConfigurationError('Scala stdin exceeds 1 MiB');
|
|
378
|
+
this.pendingInput += data;
|
|
379
|
+
}
|
|
380
|
+
eof() {
|
|
381
|
+
/* Every supplied input buffer ends in EOF; execution is never replayed. */
|
|
382
|
+
}
|
|
383
|
+
terminate() {
|
|
384
|
+
this.active?.controller.abort(new CancelledError('Scala execution stopped', { runtimeId: 'SCALA' }));
|
|
385
|
+
}
|
|
386
|
+
async clear() {
|
|
387
|
+
this.terminate();
|
|
388
|
+
if (this.compiler)
|
|
389
|
+
this.retire(this.compiler);
|
|
390
|
+
this.pendingInput = '';
|
|
391
|
+
}
|
|
392
|
+
async dispose() {
|
|
393
|
+
this.disposed = true;
|
|
394
|
+
await this.clear();
|
|
395
|
+
this.payload = null;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type RuntimeAssetCacheOptions } from '@wasm-idle/core';
|
|
2
|
+
import { SCALA_ASSET_BASE_URL, SCALA_ASSET_RECEIPTS, SCALA_ASSET_REVISION } from './worker/scala/asset-receipts.js';
|
|
3
|
+
export { SCALA_ASSET_BASE_URL, SCALA_ASSET_RECEIPTS, SCALA_ASSET_REVISION };
|
|
4
|
+
export type ScalaCompilerPayload = Record<string, Uint8Array>;
|
|
5
|
+
export interface ScalaCompilerAssetOptions {
|
|
6
|
+
baseUrl?: string;
|
|
7
|
+
persistentCache?: RuntimeAssetCacheOptions;
|
|
8
|
+
signal?: AbortSignal;
|
|
9
|
+
maxAssetBytes?: number;
|
|
10
|
+
fetch?: typeof globalThis.fetch;
|
|
11
|
+
onProgress?: (loaded: number, total: number) => void;
|
|
12
|
+
}
|
|
13
|
+
/** Capability preflight precedes every cache/network load of the large compiler. */
|
|
14
|
+
export declare function assertScalaBrowserSupport(wasm?: typeof WebAssembly | undefined): void;
|
|
15
|
+
/** Fetches exactly the pinned graph; verified bytes, never remote URLs, cross into the Worker. */
|
|
16
|
+
export declare function createScalaCompilerPayload(options?: ScalaCompilerAssetOptions): Promise<ScalaCompilerPayload>;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { fetchPinnedRuntimeAsset, RuntimeConfigurationError } from '@wasm-idle/core';
|
|
2
|
+
import { SCALA_ASSET_BASE_URL, SCALA_ASSET_RECEIPTS, SCALA_ASSET_REVISION } from './worker/scala/asset-receipts.js';
|
|
3
|
+
export { SCALA_ASSET_BASE_URL, SCALA_ASSET_RECEIPTS, SCALA_ASSET_REVISION };
|
|
4
|
+
/** Capability preflight precedes every cache/network load of the large compiler. */
|
|
5
|
+
export function assertScalaBrowserSupport(wasm = globalThis.WebAssembly) {
|
|
6
|
+
const features = wasm;
|
|
7
|
+
const missing = ['JSTag', 'Suspending', 'promising'].filter((name) => name === 'JSTag' ? features?.[name] === undefined : typeof features?.[name] !== 'function');
|
|
8
|
+
if (missing.length)
|
|
9
|
+
throw new RuntimeConfigurationError(`Scala 3 requires WebAssembly ${missing.join(', ')}. This browser cannot run the Scala.js compiler.`, { runtimeId: 'SCALA' });
|
|
10
|
+
}
|
|
11
|
+
/** Fetches exactly the pinned graph; verified bytes, never remote URLs, cross into the Worker. */
|
|
12
|
+
export async function createScalaCompilerPayload(options = {}) {
|
|
13
|
+
assertScalaBrowserSupport();
|
|
14
|
+
options.signal?.throwIfAborted();
|
|
15
|
+
let base;
|
|
16
|
+
try {
|
|
17
|
+
base = new URL(options.baseUrl ?? SCALA_ASSET_BASE_URL, globalThis.location?.href);
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
throw new RuntimeConfigurationError('Scala asset base URL is invalid.', {
|
|
21
|
+
runtimeId: 'SCALA'
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
if (!['http:', 'https:'].includes(base.protocol) ||
|
|
25
|
+
base.username ||
|
|
26
|
+
base.password ||
|
|
27
|
+
base.search ||
|
|
28
|
+
base.hash)
|
|
29
|
+
throw new RuntimeConfigurationError('Scala assets require an uncredentialed HTTP(S) base URL without query or fragment.', { runtimeId: 'SCALA' });
|
|
30
|
+
if (!base.pathname.endsWith('/'))
|
|
31
|
+
base.pathname += '/';
|
|
32
|
+
const total = SCALA_ASSET_RECEIPTS.reduce((sum, receipt) => sum + receipt.bytes, 0);
|
|
33
|
+
let loaded = 0;
|
|
34
|
+
const payload = {};
|
|
35
|
+
// Sequential loading bounds transient buffers and makes cancellation deterministic.
|
|
36
|
+
for (const receipt of SCALA_ASSET_RECEIPTS) {
|
|
37
|
+
const bytes = await fetchPinnedRuntimeAsset({
|
|
38
|
+
url: new URL(receipt.path, base).href,
|
|
39
|
+
receipt: { bytes: receipt.bytes, sha256: receipt.sha256 },
|
|
40
|
+
persistentCache: options.persistentCache,
|
|
41
|
+
signal: options.signal,
|
|
42
|
+
maxAssetBytes: options.maxAssetBytes,
|
|
43
|
+
fetch: options.fetch,
|
|
44
|
+
onProgress: (size) => options.onProgress?.(loaded + size, total)
|
|
45
|
+
});
|
|
46
|
+
options.signal?.throwIfAborted();
|
|
47
|
+
payload[receipt.path] = bytes;
|
|
48
|
+
loaded += receipt.bytes;
|
|
49
|
+
}
|
|
50
|
+
return payload;
|
|
51
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { DiagnosticLimitError } from '@wasm-idle/core';
|
|
2
|
+
/** Convert the real Scala reporter's terminal headings into editor locations. */
|
|
3
|
+
export function parseScalaDiagnostics(lines, maxDiagnostics = 1000) {
|
|
4
|
+
const diagnostics = [];
|
|
5
|
+
const text = lines.join('\n').replace(/\x1b\[[0-9;]*m/g, '');
|
|
6
|
+
const headings = [
|
|
7
|
+
...text.matchAll(/^-- (?:\[\w+\] )?(.+?): \/workspace\/(.+\.scala):(\d+):(\d+)\s*-*\s*$/gm)
|
|
8
|
+
];
|
|
9
|
+
for (let index = 0; index < headings.length; index++) {
|
|
10
|
+
const heading = headings[index];
|
|
11
|
+
if (diagnostics.length >= maxDiagnostics)
|
|
12
|
+
throw new DiagnosticLimitError('Scala diagnostic limit exceeded', {
|
|
13
|
+
runtimeId: 'SCALA',
|
|
14
|
+
limit: maxDiagnostics,
|
|
15
|
+
actual: diagnostics.length + 1
|
|
16
|
+
});
|
|
17
|
+
const body = text
|
|
18
|
+
.slice(heading.index + heading[0].length, headings[index + 1]?.index ?? text.length)
|
|
19
|
+
.trim();
|
|
20
|
+
diagnostics.push({
|
|
21
|
+
fileName: heading[2],
|
|
22
|
+
lineNumber: Number(heading[3]),
|
|
23
|
+
columnNumber: Number(heading[4]),
|
|
24
|
+
severity: /warning/i.test(heading[1]) ? 'warning' : 'error',
|
|
25
|
+
message: `${heading[1]}${body ? `\n${body}` : ''}`
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
return diagnostics;
|
|
29
|
+
}
|
|
@@ -144,6 +144,8 @@ export declare class StaticWorkerRuntimeSandbox implements Sandbox {
|
|
|
144
144
|
private createBootstrapUrl;
|
|
145
145
|
private revokeBootstrapUrl;
|
|
146
146
|
private ensureWorkerStarted;
|
|
147
|
+
private preflightWorkerAssets;
|
|
148
|
+
private prepareWorkerAssets;
|
|
147
149
|
private startWorker;
|
|
148
150
|
private handleWorkerMessage;
|
|
149
151
|
private formatWorkerError;
|
|
@@ -1016,7 +1016,7 @@ self.postMessage = (message, transferOrOptions) => {
|
|
|
1016
1016
|
});
|
|
1017
1017
|
return startPromise;
|
|
1018
1018
|
}
|
|
1019
|
-
async
|
|
1019
|
+
async preflightWorkerAssets(generation, progress, controls) {
|
|
1020
1020
|
let runtimePreflight;
|
|
1021
1021
|
if (this.config.preflightRuntimeAssets) {
|
|
1022
1022
|
const urls = this.resolvedRuntimeUrls;
|
|
@@ -1036,8 +1036,30 @@ self.postMessage = (message, transferOrOptions) => {
|
|
|
1036
1036
|
runtimeId: this.config.languageId
|
|
1037
1037
|
};
|
|
1038
1038
|
this.reportProgress(progress, 0.03, `Preflighting ${this.config.displayName} runtime`);
|
|
1039
|
+
let rejectAborted;
|
|
1040
|
+
const aborted = new Promise((_resolve, reject) => {
|
|
1041
|
+
rejectAborted = reject;
|
|
1042
|
+
});
|
|
1043
|
+
const abortPreflight = () => {
|
|
1044
|
+
operation.active = false;
|
|
1045
|
+
if (operation.claimedTicket)
|
|
1046
|
+
this.retireRuntimePreflight(operation.claimedTicket);
|
|
1047
|
+
rejectAborted(new CancelledError(`${this.config.displayName} runtime preflight cancelled`, {
|
|
1048
|
+
phase: 'asset',
|
|
1049
|
+
runtimeId: this.config.languageId,
|
|
1050
|
+
cause: controls.signal?.reason
|
|
1051
|
+
}));
|
|
1052
|
+
};
|
|
1053
|
+
// A custom preflight may ignore cancellation. Retire its claimed buffers
|
|
1054
|
+
// immediately and settle without waiting for its late callback result.
|
|
1055
|
+
void aborted.catch(() => { });
|
|
1056
|
+
controls.signal?.addEventListener('abort', abortPreflight, { once: true });
|
|
1039
1057
|
try {
|
|
1040
|
-
|
|
1058
|
+
if (controls.signal?.aborted) {
|
|
1059
|
+
abortPreflight();
|
|
1060
|
+
return await aborted;
|
|
1061
|
+
}
|
|
1062
|
+
const pending = this.config.preflightRuntimeAssets(urls, {
|
|
1041
1063
|
limits: controls.limits,
|
|
1042
1064
|
persistentCache: controls.persistentCache,
|
|
1043
1065
|
signal: controls.signal,
|
|
@@ -1112,6 +1134,7 @@ self.postMessage = (message, transferOrOptions) => {
|
|
|
1112
1134
|
return ticket;
|
|
1113
1135
|
}
|
|
1114
1136
|
});
|
|
1137
|
+
runtimePreflight = await Promise.race([pending, aborted]);
|
|
1115
1138
|
}
|
|
1116
1139
|
catch (error) {
|
|
1117
1140
|
operation.active = false;
|
|
@@ -1120,6 +1143,9 @@ self.postMessage = (message, transferOrOptions) => {
|
|
|
1120
1143
|
}
|
|
1121
1144
|
throw error;
|
|
1122
1145
|
}
|
|
1146
|
+
finally {
|
|
1147
|
+
controls.signal?.removeEventListener('abort', abortPreflight);
|
|
1148
|
+
}
|
|
1123
1149
|
operation.active = false;
|
|
1124
1150
|
const returnedDelivery = runtimePreflight !== null && typeof runtimePreflight === 'object'
|
|
1125
1151
|
? ownedDeliveryStateByTicket.get(runtimePreflight)
|
|
@@ -1156,18 +1182,66 @@ self.postMessage = (message, transferOrOptions) => {
|
|
|
1156
1182
|
throw error;
|
|
1157
1183
|
}
|
|
1158
1184
|
}
|
|
1185
|
+
return runtimePreflight;
|
|
1186
|
+
}
|
|
1187
|
+
async prepareWorkerAssets(generation, progress, controls) {
|
|
1188
|
+
const controller = new AbortController();
|
|
1189
|
+
const abort = () => controller.abort(controls.signal?.reason);
|
|
1190
|
+
const preparationControls = { ...controls, signal: controller.signal };
|
|
1191
|
+
let active = true;
|
|
1192
|
+
let lastProgress = 0;
|
|
1193
|
+
const preparationProgress = {
|
|
1194
|
+
set: (value, stage) => {
|
|
1195
|
+
if (!active ||
|
|
1196
|
+
controller.signal.aborted ||
|
|
1197
|
+
this.disposed ||
|
|
1198
|
+
generation !== this.workerGeneration)
|
|
1199
|
+
return;
|
|
1200
|
+
lastProgress = Math.max(lastProgress, Number.isFinite(value) ? value : 0);
|
|
1201
|
+
this.reportProgress(progress, lastProgress, stage);
|
|
1202
|
+
}
|
|
1203
|
+
};
|
|
1204
|
+
let runtimePreflight;
|
|
1205
|
+
controls.signal?.addEventListener('abort', abort, { once: true });
|
|
1206
|
+
try {
|
|
1207
|
+
if (controls.signal?.aborted)
|
|
1208
|
+
abort();
|
|
1209
|
+
// The runner script is independent of the verified runtime payload. Only
|
|
1210
|
+
// downloads and verification overlap; the runner is created after both finish.
|
|
1211
|
+
const [, verifiedWorkerBytes] = await Promise.all([
|
|
1212
|
+
this.preflightWorkerAssets(generation, preparationProgress, preparationControls).then((value) => {
|
|
1213
|
+
// The helper has already removed its abort listener. A sibling failure
|
|
1214
|
+
// can still invalidate this last handoff before we retain its ticket.
|
|
1215
|
+
if (!active || controller.signal.aborted) {
|
|
1216
|
+
this.retireRuntimePreflight(value);
|
|
1217
|
+
throw new CancelledError(`${this.config.displayName} runtime preparation cancelled`, {
|
|
1218
|
+
phase: 'startup',
|
|
1219
|
+
runtimeId: this.config.languageId,
|
|
1220
|
+
cause: controller.signal.reason
|
|
1221
|
+
});
|
|
1222
|
+
}
|
|
1223
|
+
runtimePreflight = value;
|
|
1224
|
+
}),
|
|
1225
|
+
this.preloadWorkerScript(preparationProgress, preparationControls)
|
|
1226
|
+
]);
|
|
1227
|
+
return { runtimePreflight, verifiedWorkerBytes };
|
|
1228
|
+
}
|
|
1229
|
+
catch (error) {
|
|
1230
|
+
active = false;
|
|
1231
|
+
controller.abort(error);
|
|
1232
|
+
this.retireRuntimePreflight(runtimePreflight);
|
|
1233
|
+
throw error;
|
|
1234
|
+
}
|
|
1235
|
+
finally {
|
|
1236
|
+
active = false;
|
|
1237
|
+
controls.signal?.removeEventListener('abort', abort);
|
|
1238
|
+
}
|
|
1239
|
+
}
|
|
1240
|
+
async startWorker(generation, progress, controls) {
|
|
1241
|
+
const { runtimePreflight, verifiedWorkerBytes } = await this.prepareWorkerAssets(generation, progress, controls);
|
|
1159
1242
|
let worker;
|
|
1160
1243
|
let workerCreated = false;
|
|
1161
1244
|
try {
|
|
1162
|
-
if (this.disposed)
|
|
1163
|
-
throw this.getDisposeReason();
|
|
1164
|
-
if (generation !== this.workerGeneration) {
|
|
1165
|
-
throw new CancelledError(`${this.config.displayName} worker startup terminated`, {
|
|
1166
|
-
phase: 'startup',
|
|
1167
|
-
runtimeId: this.config.languageId
|
|
1168
|
-
});
|
|
1169
|
-
}
|
|
1170
|
-
const verifiedWorkerBytes = await this.preloadWorkerScript(progress, controls);
|
|
1171
1245
|
if (this.disposed)
|
|
1172
1246
|
throw this.getDisposeReason();
|
|
1173
1247
|
if (generation !== this.workerGeneration) {
|