wasm-idle 0.4.0 → 0.5.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 +111 -2
- package/dist/clang/app.d.ts +4 -0
- package/dist/clang/app.js +330 -15
- package/dist/clang/index.d.ts +10 -0
- package/dist/clang/index.js +284 -25
- package/dist/debug/controller.svelte.d.ts +43 -0
- package/dist/debug/controller.svelte.js +268 -0
- package/dist/debug/editor/index.d.ts +2 -1
- package/dist/debug/editor/index.js +1 -1
- package/dist/debug/editor/monaco.d.ts +8 -0
- package/dist/debug/editor/monaco.js +23 -0
- package/dist/debug/expression.d.ts +24 -0
- package/dist/debug/expression.js +307 -138
- package/dist/debug/index.d.ts +4 -1
- package/dist/debug/index.js +2 -1
- package/dist/index.d.ts +6 -3
- package/dist/index.js +3 -3
- package/dist/playground/assetBridge.d.ts +20 -0
- package/dist/playground/assetBridge.js +192 -0
- package/dist/playground/assets.d.ts +37 -0
- package/dist/playground/assets.js +105 -0
- package/dist/playground/clang.d.ts +11 -1
- package/dist/playground/clang.js +58 -4
- package/dist/playground/index.d.ts +5 -5
- package/dist/playground/index.js +48 -6
- package/dist/playground/java.d.ts +7 -2
- package/dist/playground/java.js +31 -5
- package/dist/playground/javaStdin.js +235 -2
- package/dist/playground/options.d.ts +18 -0
- package/dist/playground/options.js +15 -1
- package/dist/playground/python.d.ts +7 -1
- package/dist/playground/python.js +50 -5
- package/dist/playground/rust.d.ts +32 -0
- package/dist/playground/rust.js +175 -0
- package/dist/playground/sandbox.d.ts +19 -4
- package/dist/playground/wasmRustVersion.d.ts +1 -0
- package/dist/playground/wasmRustVersion.js +1 -0
- package/dist/playground/worker/assets.d.ts +12 -0
- package/dist/playground/worker/assets.js +257 -0
- package/dist/playground/worker/clang.js +22 -6
- package/dist/playground/worker/java.js +15 -35
- package/dist/playground/worker/python.js +13 -33
- package/dist/playground/worker/rust.d.ts +1 -0
- package/dist/playground/worker/rust.js +159 -0
- package/dist/terminal/Terminal.svelte +121 -18
- package/dist/terminal/Terminal.svelte.d.ts +4 -0
- package/dist/terminal/theme.js +2 -2
- package/dist/terminal/types.d.ts +4 -0
- package/package.json +19 -13
package/dist/playground/java.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import { resolveSandboxExecutionArgs } from './options';
|
|
2
|
+
import { WorkerAssetBridge } from './assetBridge';
|
|
3
|
+
import { resolveRuntimeAssetConfig } from './assets';
|
|
1
4
|
import { flushBufferedEof, flushQueuedStdin, resetBufferedStdin } from './stdinBuffer';
|
|
2
|
-
import { resolveTeaVmBaseUrl } from './teavmConfig';
|
|
3
5
|
class Java {
|
|
4
6
|
output = null;
|
|
5
7
|
worker = null;
|
|
@@ -14,23 +16,43 @@ class Java {
|
|
|
14
16
|
oncompilerdiagnostic;
|
|
15
17
|
waitingForInput = false;
|
|
16
18
|
pendingEof = false;
|
|
17
|
-
|
|
19
|
+
assetBridge = null;
|
|
20
|
+
load(runtimeAssets = '', _code = '', _log = true, _args = [], _options = {}, progress) {
|
|
18
21
|
return new Promise(async (resolve, reject) => {
|
|
19
22
|
this.pendingInput = [];
|
|
20
23
|
this.waitingForInput = false;
|
|
21
24
|
this.pendingEof = false;
|
|
22
|
-
|
|
25
|
+
const assetConfig = resolveRuntimeAssetConfig('java', runtimeAssets, typeof window !== 'undefined' ? window.location.href : '');
|
|
26
|
+
this.baseUrl = assetConfig.baseUrl;
|
|
27
|
+
const needsWorkerReset = !this.worker || !this.assetBridge || !this.assetBridge.matches(assetConfig);
|
|
28
|
+
if (needsWorkerReset && this.worker) {
|
|
29
|
+
this.worker.terminate();
|
|
30
|
+
delete this.worker;
|
|
31
|
+
this.assetBridge = null;
|
|
32
|
+
}
|
|
23
33
|
if (!this.worker) {
|
|
24
34
|
this.worker = new (await import('./worker/java?worker')).default();
|
|
35
|
+
this.assetBridge = new WorkerAssetBridge(this.worker, 'java', assetConfig, progress);
|
|
25
36
|
this.worker.onmessage = (event) => {
|
|
37
|
+
if (this.assetBridge?.handleMessage(event))
|
|
38
|
+
return;
|
|
26
39
|
if (event.data?.load)
|
|
27
40
|
resolve();
|
|
28
41
|
if (event.data?.error)
|
|
29
42
|
reject(event.data.error);
|
|
30
43
|
};
|
|
31
|
-
this.worker.postMessage({
|
|
44
|
+
this.worker.postMessage({
|
|
45
|
+
load: true,
|
|
46
|
+
assets: {
|
|
47
|
+
baseUrl: assetConfig.baseUrl,
|
|
48
|
+
useAssetBridge: assetConfig.useAssetBridge
|
|
49
|
+
}
|
|
50
|
+
});
|
|
32
51
|
}
|
|
33
52
|
else {
|
|
53
|
+
if (!this.assetBridge)
|
|
54
|
+
return reject('Worker asset bridge unavailable');
|
|
55
|
+
this.assetBridge.rebind(this.worker, assetConfig, progress);
|
|
34
56
|
resolve();
|
|
35
57
|
}
|
|
36
58
|
});
|
|
@@ -62,9 +84,12 @@ class Java {
|
|
|
62
84
|
return new Promise((resolve, reject) => {
|
|
63
85
|
if (!this.worker)
|
|
64
86
|
return reject('Worker not loaded');
|
|
87
|
+
const { programArgs } = resolveSandboxExecutionArgs('JAVA', args, options);
|
|
65
88
|
const _uid = ++this.uid;
|
|
66
89
|
this.activeReject = reject;
|
|
67
90
|
const handler = (event) => {
|
|
91
|
+
if (this.assetBridge?.handleMessage(event))
|
|
92
|
+
return;
|
|
68
93
|
if (!this.worker)
|
|
69
94
|
return reject('Worker not loaded');
|
|
70
95
|
if (_uid !== this.uid)
|
|
@@ -101,7 +126,7 @@ class Java {
|
|
|
101
126
|
code,
|
|
102
127
|
prepare,
|
|
103
128
|
buffer: this.buffer,
|
|
104
|
-
args,
|
|
129
|
+
args: programArgs,
|
|
105
130
|
stdin: options.stdin || '',
|
|
106
131
|
baseUrl: this.baseUrl
|
|
107
132
|
});
|
|
@@ -118,6 +143,7 @@ class Java {
|
|
|
118
143
|
this.uid += 1;
|
|
119
144
|
this.worker?.terminate?.();
|
|
120
145
|
delete this.worker;
|
|
146
|
+
this.assetBridge = null;
|
|
121
147
|
this.exit = true;
|
|
122
148
|
}
|
|
123
149
|
async clear() {
|
|
@@ -1,11 +1,238 @@
|
|
|
1
1
|
const JAVA_STDIN_HELPER_CLASS = 'WasmIdleStdin';
|
|
2
|
+
const JAVA_SCANNER_COMPAT_SENTINEL = '// wasm-idle Scanner compatibility shim';
|
|
3
|
+
const JAVA_SCANNER_COMPAT_SHIM = `${JAVA_SCANNER_COMPAT_SENTINEL}
|
|
4
|
+
final class Scanner implements AutoCloseable {
|
|
5
|
+
private final java.io.InputStream input;
|
|
6
|
+
private int bufferedChar = Integer.MIN_VALUE;
|
|
7
|
+
private String bufferedToken = null;
|
|
8
|
+
private boolean closed = false;
|
|
9
|
+
|
|
10
|
+
Scanner(java.io.InputStream input) {
|
|
11
|
+
this.input = input;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
Scanner(String source) {
|
|
15
|
+
this(
|
|
16
|
+
new java.io.ByteArrayInputStream(
|
|
17
|
+
source != null ? source.getBytes() : new byte[0]
|
|
18
|
+
)
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
private void ensureOpen() {
|
|
23
|
+
if (closed) {
|
|
24
|
+
throw new IllegalStateException("Scanner closed");
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
private int readByteInternal() {
|
|
29
|
+
try {
|
|
30
|
+
return input.read();
|
|
31
|
+
} catch (java.io.IOException error) {
|
|
32
|
+
throw new RuntimeException(error);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
private int readByte() {
|
|
37
|
+
ensureOpen();
|
|
38
|
+
if (bufferedChar != Integer.MIN_VALUE) {
|
|
39
|
+
int value = bufferedChar;
|
|
40
|
+
bufferedChar = Integer.MIN_VALUE;
|
|
41
|
+
return value;
|
|
42
|
+
}
|
|
43
|
+
return readByteInternal();
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
private int peekByte() {
|
|
47
|
+
ensureOpen();
|
|
48
|
+
if (bufferedChar == Integer.MIN_VALUE) {
|
|
49
|
+
bufferedChar = readByteInternal();
|
|
50
|
+
}
|
|
51
|
+
return bufferedChar;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private boolean isWhitespace(int value) {
|
|
55
|
+
return value == ' ' || value == '\\n' || value == '\\r' || value == '\\t' || value == '\\f';
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
private boolean skipWhitespace() {
|
|
59
|
+
int value = peekByte();
|
|
60
|
+
while (value != -1 && isWhitespace(value)) {
|
|
61
|
+
readByte();
|
|
62
|
+
value = peekByte();
|
|
63
|
+
}
|
|
64
|
+
return value != -1;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
private String readTokenValue() {
|
|
68
|
+
java.lang.StringBuilder token = new java.lang.StringBuilder();
|
|
69
|
+
int value = peekByte();
|
|
70
|
+
while (value != -1 && !isWhitespace(value)) {
|
|
71
|
+
token.append((char) readByte());
|
|
72
|
+
value = peekByte();
|
|
73
|
+
}
|
|
74
|
+
return token.toString();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
public boolean hasNext() {
|
|
78
|
+
ensureOpen();
|
|
79
|
+
if (bufferedToken != null) {
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
if (!skipWhitespace()) {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
bufferedToken = readTokenValue();
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
public String next() {
|
|
90
|
+
ensureOpen();
|
|
91
|
+
if (bufferedToken != null) {
|
|
92
|
+
String token = bufferedToken;
|
|
93
|
+
bufferedToken = null;
|
|
94
|
+
return token;
|
|
95
|
+
}
|
|
96
|
+
if (!skipWhitespace()) {
|
|
97
|
+
throw new RuntimeException("No more tokens");
|
|
98
|
+
}
|
|
99
|
+
return readTokenValue();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
public String nextLine() {
|
|
103
|
+
ensureOpen();
|
|
104
|
+
java.lang.StringBuilder line = new java.lang.StringBuilder();
|
|
105
|
+
if (bufferedToken != null) {
|
|
106
|
+
line.append(bufferedToken);
|
|
107
|
+
bufferedToken = null;
|
|
108
|
+
}
|
|
109
|
+
int value = peekByte();
|
|
110
|
+
if (line.length() == 0 && value == -1) {
|
|
111
|
+
return "";
|
|
112
|
+
}
|
|
113
|
+
if (line.length() == 0 && value == '\\r') {
|
|
114
|
+
readByte();
|
|
115
|
+
if (peekByte() == '\\n') {
|
|
116
|
+
readByte();
|
|
117
|
+
}
|
|
118
|
+
return "";
|
|
119
|
+
}
|
|
120
|
+
if (line.length() == 0 && value == '\\n') {
|
|
121
|
+
readByte();
|
|
122
|
+
return "";
|
|
123
|
+
}
|
|
124
|
+
while (true) {
|
|
125
|
+
value = readByte();
|
|
126
|
+
if (value == -1 || value == '\\n' || value == '\\r') {
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
line.append((char) value);
|
|
130
|
+
}
|
|
131
|
+
if (value == '\\r' && peekByte() == '\\n') {
|
|
132
|
+
readByte();
|
|
133
|
+
}
|
|
134
|
+
return line.toString();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
public int nextInt() {
|
|
138
|
+
return Integer.parseInt(next());
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
public long nextLong() {
|
|
142
|
+
return Long.parseLong(next());
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
public float nextFloat() {
|
|
146
|
+
return Float.parseFloat(next());
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
public double nextDouble() {
|
|
150
|
+
return Double.parseDouble(next());
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
public boolean hasNextInt() {
|
|
154
|
+
if (!hasNext()) {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
try {
|
|
158
|
+
Integer.parseInt(bufferedToken);
|
|
159
|
+
return true;
|
|
160
|
+
} catch (RuntimeException error) {
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
public boolean hasNextLong() {
|
|
166
|
+
if (!hasNext()) {
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
try {
|
|
170
|
+
Long.parseLong(bufferedToken);
|
|
171
|
+
return true;
|
|
172
|
+
} catch (RuntimeException error) {
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
public boolean hasNextFloat() {
|
|
178
|
+
if (!hasNext()) {
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
try {
|
|
182
|
+
Float.parseFloat(bufferedToken);
|
|
183
|
+
return true;
|
|
184
|
+
} catch (RuntimeException error) {
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
public boolean hasNextDouble() {
|
|
190
|
+
if (!hasNext()) {
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
193
|
+
try {
|
|
194
|
+
Double.parseDouble(bufferedToken);
|
|
195
|
+
return true;
|
|
196
|
+
} catch (RuntimeException error) {
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
public void close() {
|
|
202
|
+
if (closed) {
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
closed = true;
|
|
206
|
+
try {
|
|
207
|
+
input.close();
|
|
208
|
+
} catch (java.io.IOException error) {
|
|
209
|
+
throw new RuntimeException(error);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}`;
|
|
2
213
|
export const prepareJavaStdinInjection = (code, stdin) => {
|
|
3
214
|
const usesStdin = code.includes('System.in');
|
|
215
|
+
const usesScanner = /\bScanner\b/.test(code) &&
|
|
216
|
+
!code.includes(JAVA_SCANNER_COMPAT_SENTINEL) &&
|
|
217
|
+
!/\b(?:class|interface|enum|record)\s+Scanner\b/.test(code) &&
|
|
218
|
+
!/^[ \t]*import[ \t]+(?!java\.util\.Scanner\b)(?!static\b)[\w.]+\.Scanner[ \t]*;[ \t]*$/m.test(code);
|
|
4
219
|
if (!usesStdin) {
|
|
220
|
+
if (!usesScanner) {
|
|
221
|
+
return {
|
|
222
|
+
usesStdin: false,
|
|
223
|
+
stdinCacheKey: '',
|
|
224
|
+
transformedCode: code,
|
|
225
|
+
helperSourcePath: null,
|
|
226
|
+
helperSource: null
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
const transformedCode = code
|
|
230
|
+
.replace(/^[ \t]*import[ \t]+java\.util\.Scanner[ \t]*;[ \t]*$/gm, (match) => match.replace(/[^\r\n]/g, ' '))
|
|
231
|
+
.replaceAll(/\bjava\.util\.Scanner\b/g, 'Scanner');
|
|
5
232
|
return {
|
|
6
233
|
usesStdin: false,
|
|
7
234
|
stdinCacheKey: '',
|
|
8
|
-
transformedCode:
|
|
235
|
+
transformedCode: `${transformedCode.trimEnd()}\n\n${JAVA_SCANNER_COMPAT_SHIM}\n`,
|
|
9
236
|
helperSourcePath: null,
|
|
10
237
|
helperSource: null
|
|
11
238
|
};
|
|
@@ -15,7 +242,13 @@ export const prepareJavaStdinInjection = (code, stdin) => {
|
|
|
15
242
|
const helperSourcePath = packageName
|
|
16
243
|
? `${packageName.replaceAll('.', '/')}/${JAVA_STDIN_HELPER_CLASS}.java`
|
|
17
244
|
: `${JAVA_STDIN_HELPER_CLASS}.java`;
|
|
18
|
-
|
|
245
|
+
let transformedCode = code.replaceAll('System.in', `${JAVA_STDIN_HELPER_CLASS}.open()`);
|
|
246
|
+
if (usesScanner) {
|
|
247
|
+
transformedCode = transformedCode
|
|
248
|
+
.replace(/^[ \t]*import[ \t]+java\.util\.Scanner[ \t]*;[ \t]*$/gm, (match) => match.replace(/[^\r\n]/g, ' '))
|
|
249
|
+
.replaceAll(/\bjava\.util\.Scanner\b/g, 'Scanner');
|
|
250
|
+
transformedCode = `${transformedCode.trimEnd()}\n\n${JAVA_SCANNER_COMPAT_SHIM}\n`;
|
|
251
|
+
}
|
|
19
252
|
const encodedBytes = [...new TextEncoder().encode(stdin)].join(', ');
|
|
20
253
|
return {
|
|
21
254
|
usesStdin: true,
|
|
@@ -13,8 +13,14 @@ export interface CompilerDiagnostic {
|
|
|
13
13
|
severity: CompilerDiagnosticSeverity;
|
|
14
14
|
message: string;
|
|
15
15
|
}
|
|
16
|
+
export type RustTargetTriple = 'wasm32-wasip1' | 'wasm32-wasip2' | 'wasm32-wasip3';
|
|
16
17
|
export type DebugVariableKind = 'number' | 'bool' | 'array' | 'text';
|
|
17
18
|
export type DebugArrayElementKind = 'int' | 'float' | 'double' | 'bool' | 'char';
|
|
19
|
+
export interface DebugStructFieldMetadata {
|
|
20
|
+
name: string;
|
|
21
|
+
kind: DebugArrayElementKind;
|
|
22
|
+
offset: number;
|
|
23
|
+
}
|
|
18
24
|
export interface DebugVariableMetadata {
|
|
19
25
|
slot: number;
|
|
20
26
|
name: string;
|
|
@@ -24,6 +30,8 @@ export interface DebugVariableMetadata {
|
|
|
24
30
|
elementKind?: DebugArrayElementKind;
|
|
25
31
|
length?: number;
|
|
26
32
|
dimensions?: number[];
|
|
33
|
+
structFields?: DebugStructFieldMetadata[];
|
|
34
|
+
structSize?: number;
|
|
27
35
|
}
|
|
28
36
|
export interface DebugFrame {
|
|
29
37
|
functionName: string;
|
|
@@ -46,4 +54,14 @@ export interface SandboxExecutionOptions {
|
|
|
46
54
|
breakpoints?: number[];
|
|
47
55
|
pauseOnEntry?: boolean;
|
|
48
56
|
stdin?: string;
|
|
57
|
+
compileArgs?: string[];
|
|
58
|
+
programArgs?: string[];
|
|
59
|
+
cppVersion?: string;
|
|
60
|
+
cVersion?: string;
|
|
61
|
+
rustTargetTriple?: RustTargetTriple;
|
|
62
|
+
}
|
|
63
|
+
export interface ResolvedSandboxExecutionArgs {
|
|
64
|
+
compileArgs: string[];
|
|
65
|
+
programArgs: string[];
|
|
49
66
|
}
|
|
67
|
+
export declare function resolveSandboxExecutionArgs(language: string, args?: string[], options?: SandboxExecutionOptions): ResolvedSandboxExecutionArgs;
|
|
@@ -1 +1,15 @@
|
|
|
1
|
-
|
|
1
|
+
function cloneArgs(value) {
|
|
2
|
+
return Array.isArray(value) ? [...value] : [];
|
|
3
|
+
}
|
|
4
|
+
export function resolveSandboxExecutionArgs(language, args = [], options = {}) {
|
|
5
|
+
if (language === 'C' || language === 'CPP') {
|
|
6
|
+
return {
|
|
7
|
+
compileArgs: cloneArgs(options.compileArgs ?? args),
|
|
8
|
+
programArgs: cloneArgs(options.programArgs)
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
return {
|
|
12
|
+
compileArgs: cloneArgs(options.compileArgs),
|
|
13
|
+
programArgs: cloneArgs(options.programArgs ?? args)
|
|
14
|
+
};
|
|
15
|
+
}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import type { DebugCommand, DebugSessionEvent, SandboxExecutionOptions } from './options';
|
|
2
|
+
import { WorkerAssetBridge } from './assetBridge';
|
|
3
|
+
import { type PlaygroundRuntimeAssets } from './assets';
|
|
2
4
|
import type { Sandbox } from './sandbox';
|
|
3
5
|
declare class Python implements Sandbox {
|
|
4
6
|
ts: number;
|
|
@@ -22,7 +24,11 @@ declare class Python implements Sandbox {
|
|
|
22
24
|
waitingForInput: boolean;
|
|
23
25
|
pendingEof: boolean;
|
|
24
26
|
exit: boolean;
|
|
25
|
-
|
|
27
|
+
assetBridge: WorkerAssetBridge | null;
|
|
28
|
+
activeReject: ((reason?: string) => void) | null;
|
|
29
|
+
load(runtimeAssets?: string | PlaygroundRuntimeAssets, code?: string, log?: boolean, _args?: string[], _options?: SandboxExecutionOptions, progress?: {
|
|
30
|
+
set?: (value: number) => void;
|
|
31
|
+
} | import('svelte/store').Writable<number>): Promise<void>;
|
|
26
32
|
write(input: string): void;
|
|
27
33
|
eof(): void;
|
|
28
34
|
private flushPendingInput;
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { WorkerAssetBridge } from './assetBridge';
|
|
2
|
+
import { resolveRuntimeAssetConfig } from './assets';
|
|
1
3
|
import { bufferedSequence, flushBufferedEof, flushQueuedStdin, resetBufferedStdin, waitForBufferedSequenceChange } from './stdinBuffer';
|
|
2
4
|
class Python {
|
|
3
5
|
ts = Date.now();
|
|
@@ -17,17 +19,45 @@ class Python {
|
|
|
17
19
|
waitingForInput = false;
|
|
18
20
|
pendingEof = false;
|
|
19
21
|
exit = true;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
+
assetBridge = null;
|
|
23
|
+
activeReject = null;
|
|
24
|
+
load(runtimeAssets = '', code = '', log = true, _args = [], _options = {}, progress) {
|
|
25
|
+
return new Promise(async (resolve, reject) => {
|
|
22
26
|
this.pendingInput = [];
|
|
23
27
|
this.waitingForInput = false;
|
|
24
28
|
this.pendingEof = false;
|
|
29
|
+
const assetConfig = resolveRuntimeAssetConfig('python', runtimeAssets, typeof window !== 'undefined' ? window.location.href : '');
|
|
30
|
+
const needsWorkerReset = !this.worker || !this.assetBridge || !this.assetBridge.matches(assetConfig);
|
|
31
|
+
if (needsWorkerReset && this.worker) {
|
|
32
|
+
this.worker.terminate();
|
|
33
|
+
delete this.worker;
|
|
34
|
+
this.assetBridge = null;
|
|
35
|
+
}
|
|
25
36
|
if (!this.worker) {
|
|
26
37
|
this.worker = new (await import('./worker/python?worker')).default();
|
|
27
|
-
this.
|
|
28
|
-
this.worker.
|
|
38
|
+
this.assetBridge = new WorkerAssetBridge(this.worker, 'python', assetConfig, progress);
|
|
39
|
+
this.worker.onmessage = (event) => {
|
|
40
|
+
if (this.assetBridge?.handleMessage(event))
|
|
41
|
+
return;
|
|
42
|
+
if (event.data?.load)
|
|
43
|
+
resolve();
|
|
44
|
+
if (event.data?.error)
|
|
45
|
+
reject(event.data.error);
|
|
46
|
+
};
|
|
47
|
+
this.worker.postMessage({
|
|
48
|
+
load: true,
|
|
49
|
+
log,
|
|
50
|
+
code,
|
|
51
|
+
assets: {
|
|
52
|
+
baseUrl: assetConfig.baseUrl,
|
|
53
|
+
useAssetBridge: assetConfig.useAssetBridge
|
|
54
|
+
}
|
|
55
|
+
});
|
|
29
56
|
}
|
|
30
57
|
else {
|
|
58
|
+
if (!this.assetBridge)
|
|
59
|
+
return reject('Worker asset bridge unavailable');
|
|
60
|
+
this.assetBridge.rebind(this.worker, assetConfig, progress);
|
|
31
61
|
this.worker.postMessage({ log });
|
|
32
62
|
resolve();
|
|
33
63
|
}
|
|
@@ -60,8 +90,11 @@ class Python {
|
|
|
60
90
|
return new Promise(async (resolve, reject) => {
|
|
61
91
|
if (!this.worker)
|
|
62
92
|
return reject('Worker not loaded');
|
|
93
|
+
this.activeReject = reject;
|
|
63
94
|
const interrupt = new Uint8Array(this.interruptBuffer), _uid = ++this.uid;
|
|
64
95
|
const handler = (event) => {
|
|
96
|
+
if (this.assetBridge?.handleMessage(event))
|
|
97
|
+
return;
|
|
65
98
|
if (!this.worker)
|
|
66
99
|
return reject('Worker not loaded');
|
|
67
100
|
if (_uid !== this.uid)
|
|
@@ -82,6 +115,7 @@ class Python {
|
|
|
82
115
|
this.exit = true;
|
|
83
116
|
this.waitingForInput = false;
|
|
84
117
|
this.pendingEof = false;
|
|
118
|
+
this.activeReject = null;
|
|
85
119
|
this.ondebug?.({ type: 'stop' });
|
|
86
120
|
resolve(results);
|
|
87
121
|
}
|
|
@@ -92,6 +126,7 @@ class Python {
|
|
|
92
126
|
this.exit = true;
|
|
93
127
|
this.waitingForInput = false;
|
|
94
128
|
this.pendingEof = false;
|
|
129
|
+
this.activeReject = null;
|
|
95
130
|
this.ondebug?.({ type: 'stop' });
|
|
96
131
|
reject(error);
|
|
97
132
|
}
|
|
@@ -131,17 +166,26 @@ class Python {
|
|
|
131
166
|
Atomics.store(control, 1, 5);
|
|
132
167
|
Atomics.add(control, 0, 1);
|
|
133
168
|
Atomics.notify(control, 0);
|
|
134
|
-
return (await waitForBufferedSequenceChange(this.watchResultBuffer, previousSequence, 5000)) ??
|
|
169
|
+
return ((await waitForBufferedSequenceChange(this.watchResultBuffer, previousSequence, 5000)) ??
|
|
170
|
+
'?');
|
|
135
171
|
}
|
|
136
172
|
kill() {
|
|
137
173
|
this.terminate();
|
|
138
174
|
}
|
|
139
175
|
terminate() {
|
|
176
|
+
this.activeReject?.('Process terminated');
|
|
177
|
+
this.activeReject = null;
|
|
178
|
+
this.waitingForInput = false;
|
|
140
179
|
this.pendingEof = false;
|
|
180
|
+
this.uid += 1;
|
|
141
181
|
new Uint8Array(this.interruptBuffer)[0] = 2;
|
|
142
182
|
const control = new Int32Array(this.debugBuffer);
|
|
143
183
|
Atomics.add(control, 0, 1);
|
|
144
184
|
Atomics.notify(control, 0);
|
|
185
|
+
this.worker?.terminate?.();
|
|
186
|
+
delete this.worker;
|
|
187
|
+
this.assetBridge = null;
|
|
188
|
+
this.exit = true;
|
|
145
189
|
}
|
|
146
190
|
async clear() {
|
|
147
191
|
this.terminate();
|
|
@@ -159,6 +203,7 @@ class Python {
|
|
|
159
203
|
if (!this.exit) {
|
|
160
204
|
this.worker?.terminate?.();
|
|
161
205
|
delete this.worker;
|
|
206
|
+
this.assetBridge = null;
|
|
162
207
|
this.exit = true;
|
|
163
208
|
}
|
|
164
209
|
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type PlaygroundRuntimeAssets } from './assets';
|
|
2
|
+
import { type CompilerDiagnostic, type SandboxExecutionOptions } from './options';
|
|
3
|
+
import type { Sandbox } from './sandbox';
|
|
4
|
+
declare class Rust implements Sandbox {
|
|
5
|
+
output: any;
|
|
6
|
+
worker?: Worker;
|
|
7
|
+
buffer: SharedArrayBuffer;
|
|
8
|
+
pendingInput: string[];
|
|
9
|
+
begin: number;
|
|
10
|
+
elapse: number;
|
|
11
|
+
uid: number;
|
|
12
|
+
exit: boolean;
|
|
13
|
+
compilerUrl: string;
|
|
14
|
+
assetPath: string;
|
|
15
|
+
activeReject: ((reason: string) => void) | null;
|
|
16
|
+
oncompilerdiagnostic?: (diagnostic: CompilerDiagnostic) => void;
|
|
17
|
+
waitingForInput: boolean;
|
|
18
|
+
pendingEof: boolean;
|
|
19
|
+
load(runtimeAssets?: string | PlaygroundRuntimeAssets, _code?: string, _log?: boolean, _args?: string[], _options?: SandboxExecutionOptions, progress?: {
|
|
20
|
+
set?: (value: number) => void;
|
|
21
|
+
} | import('svelte/store').Writable<number>): Promise<void>;
|
|
22
|
+
write(input: string): void;
|
|
23
|
+
eof(): void;
|
|
24
|
+
private flushPendingInput;
|
|
25
|
+
run(code: string, prepare: boolean, _log?: boolean, _prog?: {
|
|
26
|
+
set?: (value: number) => void;
|
|
27
|
+
} | import('svelte/store').Writable<number>, args?: string[], options?: SandboxExecutionOptions): Promise<boolean | string>;
|
|
28
|
+
kill(): void;
|
|
29
|
+
terminate(): void;
|
|
30
|
+
clear(): Promise<void>;
|
|
31
|
+
}
|
|
32
|
+
export default Rust;
|