brsh 3.9.0 → 3.10.1
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/CHANGELOG.md +14 -0
- package/Readme.md +22 -4
- package/bin/Readme.md +33 -3
- package/bin/exportFileSystem.js +127 -110
- package/bin/wbt.js +118 -0
- package/dist/shell.es.js +4074 -3717
- package/dist/shell.es.js.map +1 -1
- package/dist/shell.min.js +26 -25
- package/dist/shell.min.js.map +1 -1
- package/dist/terminal.cjs.js +3 -3
- package/dist/terminal.cjs.js.map +1 -1
- package/dist/terminal.es.js +55 -33
- package/dist/terminal.es.js.map +1 -1
- package/dist/terminal.min.js +3 -3
- package/dist/terminal.min.js.map +1 -1
- package/dist/wbt.es.js +3082 -2710
- package/dist/wbt.es.js.map +1 -1
- package/dist/wbt.min.js +16 -12
- package/dist/wbt.min.js.map +1 -1
- package/index.js +102 -80
- package/lib/BaseFileSystem.js +57 -2
- package/lib/BinaryContainer.js +1 -1
- package/lib/Command.js +1 -1
- package/lib/Context.js +4 -3
- package/lib/FileSystem.js +4 -2
- package/lib/InteractiveCommand.js +9 -8
- package/lib/JsCommand.js +113 -0
- package/lib/NodeFileSystem.js +164 -30
- package/lib/Readme.md +1 -0
- package/lib/ScriptCommand.js +148 -8
- package/lib/Terminal.js +60 -6
- package/lib/TtyStream.js +36 -2
- package/lib/WasmCommand.js +313 -66
- package/lib/buildFilesystemStructure.js +38 -24
- package/lib/isUtf8Text.js +47 -0
- package/lib/local/which.js +8 -3
- package/lib/wbt/index.js +473 -175
- package/lib/wbt/registry.js +21 -1
- package/package.json +3 -2
- package/test/devices-signals.test.js +112 -0
- package/test/doom.test.js +7 -4
- package/test/exportFileSystem.test.js +37 -15
- package/test/js-command.test.js +125 -0
- package/test/node-filesystem.test.js +7 -0
- package/test/scripts.test.js +30 -0
- package/test/terminal.test.js +148 -0
- package/test/wasm-cli.test.js +76 -0
- package/test/wbt.test.js +418 -10
- package/test/wbtCli.test.js +160 -0
package/lib/WasmCommand.js
CHANGED
|
@@ -8,6 +8,7 @@ const ERRNO_SUCCESS = 0;
|
|
|
8
8
|
const ERRNO_AGAIN = 6;
|
|
9
9
|
const ERRNO_BADF = 8;
|
|
10
10
|
const ERRNO_EXIST = 20;
|
|
11
|
+
const ERRNO_INTR = 27;
|
|
11
12
|
const ERRNO_INVAL = 28;
|
|
12
13
|
const ERRNO_ISDIR = 31;
|
|
13
14
|
const ERRNO_NOENT = 44;
|
|
@@ -36,15 +37,35 @@ const FDFLAGS_SYNC = 1 << 4;
|
|
|
36
37
|
|
|
37
38
|
class WasmCommand extends ScriptCommand {
|
|
38
39
|
// this.script = filepath (set by ScriptCommand parent constructor)
|
|
40
|
+
constructor(script, args, context) {
|
|
41
|
+
super(script, args, context);
|
|
42
|
+
this._ensurePkgMeta();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
_ensurePkgMeta() {
|
|
46
|
+
if (this._pkgMeta === undefined && this.script) {
|
|
47
|
+
const fs = (this.context && this.context.fs) || this.fs;
|
|
48
|
+
if (fs) {
|
|
49
|
+
this._pkgMeta = readSiblingPkgMeta(fs, this.script);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return this._pkgMeta || null;
|
|
53
|
+
}
|
|
39
54
|
|
|
40
|
-
get name() {
|
|
55
|
+
get name() {
|
|
56
|
+
const meta = this._ensurePkgMeta();
|
|
57
|
+
if (meta && meta.bin) return String(meta.bin).toLowerCase();
|
|
58
|
+
if (this.script) return this.script.split('/').pop().replace(/\.(wasm|wat)$/i, '').toLowerCase();
|
|
59
|
+
return 'wasm';
|
|
60
|
+
}
|
|
41
61
|
get requiresFilesystem() { return true; }
|
|
42
62
|
|
|
43
63
|
get interactive() {
|
|
44
64
|
const isHelpOrVersion = (this.arguments || []).some(a => a === '--help' || a === '-h' || a === '--version' || a === '-V' || a === '-v');
|
|
45
65
|
if (isHelpOrVersion) return false;
|
|
46
66
|
if (this._isInteractive !== undefined) return Boolean(this._isInteractive);
|
|
47
|
-
|
|
67
|
+
const meta = this._ensurePkgMeta();
|
|
68
|
+
if (meta && (meta.interactive || meta.wasm?.interactive)) return true;
|
|
48
69
|
if (this._inspectedWasm && this._inspectedWasm.isInteractive) return true;
|
|
49
70
|
return false;
|
|
50
71
|
}
|
|
@@ -87,7 +108,7 @@ class WasmCommand extends ScriptCommand {
|
|
|
87
108
|
this.stdinStream.setRawMode(false);
|
|
88
109
|
}
|
|
89
110
|
if (this.stdout && typeof this.stdout.write === 'function') {
|
|
90
|
-
this.stdout.write('\x1b[?1049l\x1b[?25h');
|
|
111
|
+
this.stdout.write('\x1b[0m\x1b[?1l\x1b[?1000l\x1b[?1002l\x1b[?1003l\x1b[?1006l\x1b[?1049l\x1b[?25h');
|
|
91
112
|
}
|
|
92
113
|
}
|
|
93
114
|
|
|
@@ -97,6 +118,9 @@ class WasmCommand extends ScriptCommand {
|
|
|
97
118
|
if (key instanceof Uint8Array) return key;
|
|
98
119
|
return new Uint8Array([Number(key) & 0xff]);
|
|
99
120
|
}
|
|
121
|
+
if (['Control', 'Alt', 'Shift', 'Meta', 'CapsLock', 'NumLock', 'ScrollLock', 'Dead', 'Unidentified'].includes(key)) {
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
100
124
|
const ctrlMatch = key.match(/^\^([A-Za-z@\[\\\]^_?])$/);
|
|
101
125
|
if (ctrlMatch) {
|
|
102
126
|
const ch = ctrlMatch[1].toUpperCase();
|
|
@@ -106,19 +130,40 @@ class WasmCommand extends ScriptCommand {
|
|
|
106
130
|
const map = {
|
|
107
131
|
'Enter': '\r',
|
|
108
132
|
'Return': '\r',
|
|
133
|
+
'Space': ' ',
|
|
134
|
+
'Spacebar': ' ',
|
|
109
135
|
'Backspace': '\x7f',
|
|
110
136
|
'Tab': '\t',
|
|
137
|
+
'BackTab': '\x1b[Z',
|
|
111
138
|
'Escape': '\x1b',
|
|
139
|
+
'Esc': '\x1b',
|
|
112
140
|
'ArrowUp': '\x1b[A',
|
|
141
|
+
'Up': '\x1b[A',
|
|
113
142
|
'ArrowDown': '\x1b[B',
|
|
143
|
+
'Down': '\x1b[B',
|
|
114
144
|
'ArrowRight': '\x1b[C',
|
|
145
|
+
'Right': '\x1b[C',
|
|
115
146
|
'ArrowLeft': '\x1b[D',
|
|
147
|
+
'Left': '\x1b[D',
|
|
116
148
|
'Home': '\x1b[H',
|
|
117
149
|
'End': '\x1b[F',
|
|
118
150
|
'PageUp': '\x1b[5~',
|
|
119
151
|
'PageDown': '\x1b[6~',
|
|
120
152
|
'Delete': '\x1b[3~',
|
|
153
|
+
'Del': '\x1b[3~',
|
|
121
154
|
'Insert': '\x1b[2~',
|
|
155
|
+
'F1': '\x1bOP',
|
|
156
|
+
'F2': '\x1bOQ',
|
|
157
|
+
'F3': '\x1bOR',
|
|
158
|
+
'F4': '\x1bOS',
|
|
159
|
+
'F5': '\x1b[15~',
|
|
160
|
+
'F6': '\x1b[17~',
|
|
161
|
+
'F7': '\x1b[18~',
|
|
162
|
+
'F8': '\x1b[19~',
|
|
163
|
+
'F9': '\x1b[20~',
|
|
164
|
+
'F10': '\x1b[21~',
|
|
165
|
+
'F11': '\x1b[23~',
|
|
166
|
+
'F12': '\x1b[24~',
|
|
122
167
|
};
|
|
123
168
|
if (map[key]) return new TextEncoder().encode(map[key]);
|
|
124
169
|
return new TextEncoder().encode(key);
|
|
@@ -151,7 +196,15 @@ class WasmCommand extends ScriptCommand {
|
|
|
151
196
|
const nodeFs = (() => { try { return require('fs'); } catch { return null; } })();
|
|
152
197
|
|
|
153
198
|
return this._loadBytes(filepath, nodeFs)
|
|
154
|
-
.then(({ bytes, isWat }) =>
|
|
199
|
+
.then(({ bytes, isWat }) => {
|
|
200
|
+
if (this.interactive) {
|
|
201
|
+
this.emit('interactive');
|
|
202
|
+
if (this.context && typeof this.context.emit === 'function') {
|
|
203
|
+
this.context.emit('interactive');
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return isWat ? this._runWat(bytes) : this._runWasm(bytes);
|
|
207
|
+
})
|
|
155
208
|
.catch(e => {
|
|
156
209
|
this.stdErr = e.message || String(e);
|
|
157
210
|
this.exitCode = 1;
|
|
@@ -249,7 +302,12 @@ class WasmCommand extends ScriptCommand {
|
|
|
249
302
|
let asyncifyDataAddr = 0;
|
|
250
303
|
let asyncifyPromise = null;
|
|
251
304
|
let pendingReadResolve = null;
|
|
252
|
-
let
|
|
305
|
+
let pendingResizeInterrupt = null;
|
|
306
|
+
let pollTimedOut = false;
|
|
307
|
+
let pollInterrupted = false;
|
|
308
|
+
let readInterrupted = false;
|
|
309
|
+
let onStdoutResize = null;
|
|
310
|
+
let onStdinResize = null;
|
|
253
311
|
let onStreamData = null;
|
|
254
312
|
|
|
255
313
|
const isTtyEnvironment = (this.stdin === undefined || this.stdin === null || this.stdin === this.stdinStream || (this.stdin && this.stdin.isTTY));
|
|
@@ -258,10 +316,21 @@ class WasmCommand extends ScriptCommand {
|
|
|
258
316
|
this.interactive && isTtyEnvironment
|
|
259
317
|
);
|
|
260
318
|
|
|
319
|
+
if (isInteractive) {
|
|
320
|
+
this.emit('interactive');
|
|
321
|
+
if (this.context && typeof this.context.emit === 'function') {
|
|
322
|
+
this.context.emit('interactive');
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
261
326
|
const promoteToInteractive = () => {
|
|
262
327
|
if (isInteractive || !isTtyEnvironment) return;
|
|
263
328
|
isInteractive = true;
|
|
264
329
|
this._isInteractive = true;
|
|
330
|
+
this.emit('interactive');
|
|
331
|
+
if (this.context && typeof this.context.emit === 'function') {
|
|
332
|
+
this.context.emit('interactive');
|
|
333
|
+
}
|
|
265
334
|
|
|
266
335
|
this._enterAltScreen();
|
|
267
336
|
|
|
@@ -348,7 +417,13 @@ class WasmCommand extends ScriptCommand {
|
|
|
348
417
|
if (b) enqueueBytes(b);
|
|
349
418
|
};
|
|
350
419
|
|
|
351
|
-
|
|
420
|
+
const hadDefaultStdinListener = Boolean(
|
|
421
|
+
this._onStdinStreamData
|
|
422
|
+
&& this.stdinStream
|
|
423
|
+
&& typeof this.stdinStream.listeners === 'function'
|
|
424
|
+
&& this.stdinStream.listeners('data').includes(this._onStdinStreamData)
|
|
425
|
+
);
|
|
426
|
+
if (hadDefaultStdinListener && typeof this.stdinStream.off === 'function') {
|
|
352
427
|
this.stdinStream.off('data', this._onStdinStreamData);
|
|
353
428
|
}
|
|
354
429
|
if (isTtyEnvironment && this.stdinStream && typeof this.stdinStream.on === 'function') {
|
|
@@ -444,13 +519,21 @@ class WasmCommand extends ScriptCommand {
|
|
|
444
519
|
}
|
|
445
520
|
};
|
|
446
521
|
|
|
447
|
-
// Environment variables from context.vars with terminal
|
|
522
|
+
// Environment variables from context.vars with live terminal dimensions taking precedence
|
|
523
|
+
const initCols = (this.stdout && this.stdout.columns)
|
|
524
|
+
|| (this.stdinStream && this.stdinStream.columns)
|
|
525
|
+
|| (this.context && typeof this.context.getVar === 'function' && parseInt(this.context.getVar('COLUMNS'), 10))
|
|
526
|
+
|| 80;
|
|
527
|
+
const initRows = (this.stdout && this.stdout.rows)
|
|
528
|
+
|| (this.stdinStream && this.stdinStream.rows)
|
|
529
|
+
|| (this.context && typeof this.context.getVar === 'function' && parseInt(this.context.getVar('LINES'), 10))
|
|
530
|
+
|| 24;
|
|
448
531
|
const termVars = {
|
|
449
532
|
TERM: 'xterm-256color',
|
|
450
533
|
COLORTERM: 'truecolor',
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
534
|
+
...((this.context && this.context.vars) || {}),
|
|
535
|
+
COLUMNS: String(initCols),
|
|
536
|
+
LINES: String(initRows)
|
|
454
537
|
};
|
|
455
538
|
const envPairs = Object.entries(termVars).map(([k, v]) => `${k}=${v}`);
|
|
456
539
|
|
|
@@ -541,6 +624,12 @@ class WasmCommand extends ScriptCommand {
|
|
|
541
624
|
|
|
542
625
|
if (state === 2) {
|
|
543
626
|
instance.exports.asyncify_stop_rewind();
|
|
627
|
+
if (readInterrupted && inputQueue.length === 0) {
|
|
628
|
+
readInterrupted = false;
|
|
629
|
+
view.setUint32(nread_ptr, 0, true);
|
|
630
|
+
return ERRNO_INTR;
|
|
631
|
+
}
|
|
632
|
+
readInterrupted = false;
|
|
544
633
|
let total = 0;
|
|
545
634
|
for (let i = 0; i < iovs_len && inputQueue.length > 0; i++) {
|
|
546
635
|
const ptr = view.getUint32(iovs + i * 8, true);
|
|
@@ -573,10 +662,27 @@ class WasmCommand extends ScriptCommand {
|
|
|
573
662
|
return ERRNO_AGAIN;
|
|
574
663
|
}
|
|
575
664
|
|
|
665
|
+
readInterrupted = false;
|
|
576
666
|
instance.exports.asyncify_start_unwind(asyncifyDataAddr);
|
|
577
667
|
asyncifyPromise = new Promise((resolve, reject) => {
|
|
578
|
-
|
|
579
|
-
|
|
668
|
+
const onReadReady = () => {
|
|
669
|
+
readInterrupted = false;
|
|
670
|
+
pendingReadResolve = null;
|
|
671
|
+
pendingResizeInterrupt = null;
|
|
672
|
+
this._pendingReadResolve = null;
|
|
673
|
+
this._asyncifyReject = null;
|
|
674
|
+
resolve();
|
|
675
|
+
};
|
|
676
|
+
pendingReadResolve = onReadReady;
|
|
677
|
+
this._pendingReadResolve = onReadReady;
|
|
678
|
+
pendingResizeInterrupt = () => {
|
|
679
|
+
readInterrupted = true;
|
|
680
|
+
pendingReadResolve = null;
|
|
681
|
+
pendingResizeInterrupt = null;
|
|
682
|
+
this._pendingReadResolve = null;
|
|
683
|
+
this._asyncifyReject = null;
|
|
684
|
+
resolve();
|
|
685
|
+
};
|
|
580
686
|
this._asyncifyReject = reject;
|
|
581
687
|
});
|
|
582
688
|
view.setUint32(nread_ptr, 0, true);
|
|
@@ -1025,7 +1131,9 @@ class WasmCommand extends ScriptCommand {
|
|
|
1025
1131
|
},
|
|
1026
1132
|
clock_time_get: (id, prec, time_ptr) => {
|
|
1027
1133
|
if (memRef) {
|
|
1028
|
-
const ns =
|
|
1134
|
+
const ns = (id === 1 && typeof performance !== 'undefined' && typeof performance.now === 'function')
|
|
1135
|
+
? BigInt(Math.round(performance.now() * 1e6))
|
|
1136
|
+
: BigInt(Date.now()) * 1_000_000n;
|
|
1029
1137
|
new DataView(memRef.buffer).setBigUint64(time_ptr, ns, true);
|
|
1030
1138
|
}
|
|
1031
1139
|
return ERRNO_SUCCESS;
|
|
@@ -1076,28 +1184,50 @@ class WasmCommand extends ScriptCommand {
|
|
|
1076
1184
|
|
|
1077
1185
|
if (state === 2) {
|
|
1078
1186
|
instance.exports.asyncify_stop_rewind();
|
|
1187
|
+
if (pollInterrupted && inputQueue.length === 0) {
|
|
1188
|
+
pollInterrupted = false;
|
|
1189
|
+
view.setUint32(nevents_ptr, 0, true);
|
|
1190
|
+
return ERRNO_INTR;
|
|
1191
|
+
}
|
|
1192
|
+
pollInterrupted = false;
|
|
1079
1193
|
let nevents = 0;
|
|
1080
1194
|
for (let i = 0; i < nsubscriptions; i++) {
|
|
1081
1195
|
const subPtr = in_ptr + i * 48;
|
|
1082
1196
|
const userdata = view.getBigUint64(subPtr, true);
|
|
1083
1197
|
const type = view.getUint8(subPtr + 8);
|
|
1084
|
-
|
|
1198
|
+
let isReady = false;
|
|
1085
1199
|
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1200
|
+
if (type === 0) { // EVENTTYPE_CLOCK
|
|
1201
|
+
isReady = pollTimedOut;
|
|
1202
|
+
} else if (type === 1) { // EVENTTYPE_FD_READ
|
|
1203
|
+
const fd = view.getUint32(subPtr + 16, true);
|
|
1204
|
+
const entry = fdTable.get(fd);
|
|
1205
|
+
const isDevTty = Boolean(entry && entry.type === 'dev_tty');
|
|
1206
|
+
if ((fd === 0 || (isInteractive && fd === 2) || isDevTty) && inputQueue.length > 0) {
|
|
1207
|
+
isReady = true;
|
|
1208
|
+
}
|
|
1209
|
+
} else if (type === 2) { // EVENTTYPE_FD_WRITE
|
|
1210
|
+
isReady = true;
|
|
1211
|
+
}
|
|
1090
1212
|
|
|
1091
|
-
if (
|
|
1092
|
-
|
|
1213
|
+
if (isReady) {
|
|
1214
|
+
const outEventPtr = out_ptr + nevents * 32;
|
|
1215
|
+
view.setBigUint64(outEventPtr, userdata, true);
|
|
1216
|
+
view.setUint16(outEventPtr + 8, ERRNO_SUCCESS, true);
|
|
1217
|
+
view.setUint8(outEventPtr + 10, type);
|
|
1218
|
+
for (let b = 11; b < 32; b++) view.setUint8(outEventPtr + b, 0);
|
|
1219
|
+
if (type === 1) {
|
|
1220
|
+
view.setBigUint64(outEventPtr + 16, BigInt(inputQueue.length), true);
|
|
1221
|
+
}
|
|
1222
|
+
nevents++;
|
|
1093
1223
|
}
|
|
1094
|
-
nevents++;
|
|
1095
1224
|
}
|
|
1096
1225
|
view.setUint32(nevents_ptr, nevents, true);
|
|
1097
1226
|
return ERRNO_SUCCESS;
|
|
1098
1227
|
}
|
|
1099
1228
|
|
|
1100
1229
|
let immediateCount = 0;
|
|
1230
|
+
let hasFdReadSub = false;
|
|
1101
1231
|
let minTimeoutMs = Infinity;
|
|
1102
1232
|
|
|
1103
1233
|
for (let i = 0; i < nsubscriptions; i++) {
|
|
@@ -1116,6 +1246,7 @@ class WasmCommand extends ScriptCommand {
|
|
|
1116
1246
|
}
|
|
1117
1247
|
if (sleepMs < minTimeoutMs) minTimeoutMs = sleepMs;
|
|
1118
1248
|
} else if (type === 1) { // EVENTTYPE_FD_READ
|
|
1249
|
+
hasFdReadSub = true;
|
|
1119
1250
|
const fd = view.getUint32(subPtr + 16, true);
|
|
1120
1251
|
const entry = fdTable.get(fd);
|
|
1121
1252
|
const isDevTty = Boolean(entry && entry.type === 'dev_tty');
|
|
@@ -1162,22 +1293,42 @@ class WasmCommand extends ScriptCommand {
|
|
|
1162
1293
|
return ERRNO_SUCCESS;
|
|
1163
1294
|
}
|
|
1164
1295
|
|
|
1296
|
+
pollTimedOut = false;
|
|
1297
|
+
pollInterrupted = false;
|
|
1165
1298
|
instance.exports.asyncify_start_unwind(asyncifyDataAddr);
|
|
1166
1299
|
asyncifyPromise = new Promise((resolve, reject) => {
|
|
1167
1300
|
let timer = null;
|
|
1168
1301
|
this._asyncifyReject = reject;
|
|
1169
1302
|
const onKey = () => {
|
|
1170
1303
|
if (timer) clearTimeout(timer);
|
|
1304
|
+
pollTimedOut = false;
|
|
1305
|
+
pollInterrupted = false;
|
|
1171
1306
|
pendingReadResolve = null;
|
|
1307
|
+
pendingResizeInterrupt = null;
|
|
1172
1308
|
this._pendingReadResolve = null;
|
|
1173
1309
|
this._asyncifyReject = null;
|
|
1174
1310
|
resolve();
|
|
1175
1311
|
};
|
|
1176
|
-
|
|
1177
|
-
|
|
1312
|
+
if (hasFdReadSub) {
|
|
1313
|
+
pendingReadResolve = onKey;
|
|
1314
|
+
this._pendingReadResolve = onKey;
|
|
1315
|
+
pendingResizeInterrupt = () => {
|
|
1316
|
+
if (timer) clearTimeout(timer);
|
|
1317
|
+
pollTimedOut = false;
|
|
1318
|
+
pollInterrupted = true;
|
|
1319
|
+
pendingReadResolve = null;
|
|
1320
|
+
pendingResizeInterrupt = null;
|
|
1321
|
+
this._pendingReadResolve = null;
|
|
1322
|
+
this._asyncifyReject = null;
|
|
1323
|
+
resolve();
|
|
1324
|
+
};
|
|
1325
|
+
}
|
|
1178
1326
|
if (minTimeoutMs !== Infinity) {
|
|
1179
1327
|
timer = setTimeout(() => {
|
|
1328
|
+
pollTimedOut = true;
|
|
1329
|
+
pollInterrupted = false;
|
|
1180
1330
|
pendingReadResolve = null;
|
|
1331
|
+
pendingResizeInterrupt = null;
|
|
1181
1332
|
this._pendingReadResolve = null;
|
|
1182
1333
|
this._asyncifyReject = null;
|
|
1183
1334
|
resolve();
|
|
@@ -1274,24 +1425,64 @@ class WasmCommand extends ScriptCommand {
|
|
|
1274
1425
|
this._enterAltScreen();
|
|
1275
1426
|
}
|
|
1276
1427
|
|
|
1277
|
-
const cols = (this.stdout && this.stdout.columns)
|
|
1278
|
-
|
|
1428
|
+
const cols = (this.stdout && this.stdout.columns)
|
|
1429
|
+
|| (this.stdinStream && this.stdinStream.columns)
|
|
1430
|
+
|| (this.context && typeof this.context.getVar === 'function' && parseInt(this.context.getVar('COLUMNS'), 10))
|
|
1431
|
+
|| 80;
|
|
1432
|
+
const rows = (this.stdout && this.stdout.rows)
|
|
1433
|
+
|| (this.stdinStream && this.stdinStream.rows)
|
|
1434
|
+
|| (this.context && typeof this.context.getVar === 'function' && parseInt(this.context.getVar('LINES'), 10))
|
|
1435
|
+
|| 24;
|
|
1279
1436
|
if (typeof instance.exports.set_terminal_size === 'function') {
|
|
1280
1437
|
try { instance.exports.set_terminal_size(cols, rows); } catch {}
|
|
1281
1438
|
}
|
|
1282
1439
|
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1440
|
+
let lastAppliedCols = cols;
|
|
1441
|
+
let lastAppliedRows = rows;
|
|
1442
|
+
const applyResize = (c, r) => {
|
|
1443
|
+
if (!c || !r) return;
|
|
1444
|
+
if (this.stdout && (this.stdout.columns !== c || this.stdout.rows !== r)) {
|
|
1445
|
+
this.stdout.columns = c;
|
|
1446
|
+
this.stdout.rows = r;
|
|
1447
|
+
}
|
|
1448
|
+
if (this.stdinStream && (this.stdinStream.columns !== c || this.stdinStream.rows !== r)) {
|
|
1449
|
+
this.stdinStream.columns = c;
|
|
1450
|
+
this.stdinStream.rows = r;
|
|
1451
|
+
}
|
|
1452
|
+
if (this.context && typeof this.context.setVar === 'function') {
|
|
1453
|
+
try {
|
|
1454
|
+
this.context.setVar('COLUMNS', String(c));
|
|
1455
|
+
this.context.setVar('LINES', String(r));
|
|
1456
|
+
} catch {}
|
|
1457
|
+
}
|
|
1458
|
+
if (c === lastAppliedCols && r === lastAppliedRows) return;
|
|
1459
|
+
lastAppliedCols = c;
|
|
1460
|
+
lastAppliedRows = r;
|
|
1286
1461
|
if (typeof instance?.exports?.set_terminal_size === 'function') {
|
|
1287
1462
|
try { instance.exports.set_terminal_size(c, r); } catch {}
|
|
1288
1463
|
}
|
|
1464
|
+
if (pendingResizeInterrupt) {
|
|
1465
|
+
const intr = pendingResizeInterrupt;
|
|
1466
|
+
pendingResizeInterrupt = null;
|
|
1467
|
+
intr();
|
|
1468
|
+
}
|
|
1469
|
+
};
|
|
1470
|
+
|
|
1471
|
+
onStdoutResize = () => {
|
|
1472
|
+
const c = (this.stdout && this.stdout.columns) || (this.context && typeof this.context.getVar === 'function' && parseInt(this.context.getVar('COLUMNS'), 10)) || 80;
|
|
1473
|
+
const r = (this.stdout && this.stdout.rows) || (this.context && typeof this.context.getVar === 'function' && parseInt(this.context.getVar('LINES'), 10)) || 24;
|
|
1474
|
+
applyResize(c, r);
|
|
1475
|
+
};
|
|
1476
|
+
onStdinResize = () => {
|
|
1477
|
+
const c = (this.stdinStream && this.stdinStream.columns) || (this.stdout && this.stdout.columns) || (this.context && typeof this.context.getVar === 'function' && parseInt(this.context.getVar('COLUMNS'), 10)) || 80;
|
|
1478
|
+
const r = (this.stdinStream && this.stdinStream.rows) || (this.stdout && this.stdout.rows) || (this.context && typeof this.context.getVar === 'function' && parseInt(this.context.getVar('LINES'), 10)) || 24;
|
|
1479
|
+
applyResize(c, r);
|
|
1289
1480
|
};
|
|
1290
1481
|
if (this.stdout && typeof this.stdout.on === 'function') {
|
|
1291
|
-
this.stdout.on('resize',
|
|
1482
|
+
this.stdout.on('resize', onStdoutResize);
|
|
1292
1483
|
}
|
|
1293
|
-
if (this.stdinStream && typeof this.stdinStream.on === 'function') {
|
|
1294
|
-
this.stdinStream.on('resize',
|
|
1484
|
+
if (this.stdinStream && this.stdinStream !== this.stdout && typeof this.stdinStream.on === 'function') {
|
|
1485
|
+
this.stdinStream.on('resize', onStdinResize);
|
|
1295
1486
|
}
|
|
1296
1487
|
|
|
1297
1488
|
const start = instance.exports._start
|
|
@@ -1362,12 +1553,14 @@ class WasmCommand extends ScriptCommand {
|
|
|
1362
1553
|
if (this.stdinStream && typeof this.stdinStream.off === 'function') this.stdinStream.off('data', onStreamData);
|
|
1363
1554
|
if (this.stdin && typeof this.stdin.off === 'function') this.stdin.off('data', onStreamData);
|
|
1364
1555
|
}
|
|
1365
|
-
if (this._onStdinStreamData && this.stdinStream && typeof this.stdinStream.on === 'function') {
|
|
1556
|
+
if (hadDefaultStdinListener && this._onStdinStreamData && this.stdinStream && typeof this.stdinStream.on === 'function') {
|
|
1366
1557
|
this.stdinStream.on('data', this._onStdinStreamData);
|
|
1367
1558
|
}
|
|
1368
|
-
if (
|
|
1369
|
-
|
|
1370
|
-
|
|
1559
|
+
if (onStdoutResize && this.stdout && typeof this.stdout.off === 'function') {
|
|
1560
|
+
this.stdout.off('resize', onStdoutResize);
|
|
1561
|
+
}
|
|
1562
|
+
if (onStdinResize && this.stdinStream && typeof this.stdinStream.off === 'function') {
|
|
1563
|
+
this.stdinStream.off('resize', onStdinResize);
|
|
1371
1564
|
}
|
|
1372
1565
|
this._externalKeyHandler = null;
|
|
1373
1566
|
|
|
@@ -1385,12 +1578,13 @@ class WasmCommand extends ScriptCommand {
|
|
|
1385
1578
|
const args = this.arguments || [];
|
|
1386
1579
|
const hasOutput = this.result && this.result.getStdOutput && this.result.getStdOutput().length > 0;
|
|
1387
1580
|
if (!hasOutput && (this.exitCode === 0 || this.exitCode === undefined)) {
|
|
1388
|
-
const
|
|
1581
|
+
const pkgMeta = this._ensurePkgMeta();
|
|
1582
|
+
const binLabel = (pkgMeta && pkgMeta.bin) || (this.script || 'wasm').split('/').pop();
|
|
1389
1583
|
const meta = Object.assign({
|
|
1390
|
-
name: binLabel,
|
|
1391
|
-
version: 'wasi',
|
|
1584
|
+
name: (pkgMeta && pkgMeta.name) || binLabel,
|
|
1585
|
+
version: (pkgMeta && pkgMeta.version) || 'wasi',
|
|
1392
1586
|
description: `WebAssembly WASI binary (${this.script})`
|
|
1393
|
-
},
|
|
1587
|
+
}, pkgMeta || {});
|
|
1394
1588
|
if (args.includes('--help') || args.includes('-h')) {
|
|
1395
1589
|
const inspected = inspectWasmBytes(bytes);
|
|
1396
1590
|
for (const line of formatWasmHelp(binLabel, meta, inspected)) {
|
|
@@ -1458,24 +1652,13 @@ function readBytesFromFs(fs, filepath) {
|
|
|
1458
1652
|
*/
|
|
1459
1653
|
function writeBytesToFs(fs, filepath, bytes, mimeType = 'application/octet-stream') {
|
|
1460
1654
|
const uint8 = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes);
|
|
1655
|
+
if (fs && fs.isRealFilesystem) {
|
|
1656
|
+
fs.setFileByPath(filepath, uint8, true);
|
|
1657
|
+
return;
|
|
1658
|
+
}
|
|
1461
1659
|
const copy = new Uint8Array(uint8.byteLength);
|
|
1462
1660
|
copy.set(uint8);
|
|
1463
1661
|
const bf = new BinaryFile(copy.buffer, mimeType);
|
|
1464
|
-
if (fs.isRealFilesystem) {
|
|
1465
|
-
try {
|
|
1466
|
-
const nodeFs = require('fs');
|
|
1467
|
-
const nodePath = require('path');
|
|
1468
|
-
const abs = fs.absolutePath(filepath);
|
|
1469
|
-
nodeFs.mkdirSync(nodePath.dirname(abs), { recursive: true });
|
|
1470
|
-
nodeFs.writeFileSync(abs, Buffer.from(uint8.buffer, uint8.byteOffset, uint8.byteLength));
|
|
1471
|
-
return;
|
|
1472
|
-
} catch {
|
|
1473
|
-
if (typeof fs._setFileRaw === 'function') {
|
|
1474
|
-
fs._setFileRaw(filepath, bf, true);
|
|
1475
|
-
return;
|
|
1476
|
-
}
|
|
1477
|
-
}
|
|
1478
|
-
}
|
|
1479
1662
|
if (typeof fs._setFileRaw === 'function' && filepath.startsWith('/var/lib/wbt/')) {
|
|
1480
1663
|
fs._setFileRaw(filepath, bf, true);
|
|
1481
1664
|
return;
|
|
@@ -1702,10 +1885,10 @@ function formatWasmHelp(binName, pkgMeta = {}, inspected = null) {
|
|
|
1702
1885
|
* which extends `createGenericWasmCommand`.
|
|
1703
1886
|
*/
|
|
1704
1887
|
function createGenericWasmCommand(pkgMeta, config = {}, binNameOverride = null) {
|
|
1705
|
-
const
|
|
1888
|
+
const Command = require('./Command');
|
|
1706
1889
|
const binName = (binNameOverride || pkgMeta.bin || pkgMeta.name).toLowerCase();
|
|
1707
1890
|
|
|
1708
|
-
class GenericWasmCommand extends
|
|
1891
|
+
class GenericWasmCommand extends Command {
|
|
1709
1892
|
static get name() { return binName; }
|
|
1710
1893
|
get name() { return binName; }
|
|
1711
1894
|
get requiresFilesystem() { return true; }
|
|
@@ -1719,9 +1902,25 @@ function createGenericWasmCommand(pkgMeta, config = {}, binNameOverride = null)
|
|
|
1719
1902
|
}
|
|
1720
1903
|
|
|
1721
1904
|
get captureInput() {
|
|
1905
|
+
if (this._subCommand) return Boolean(this._subCommand.captureInput);
|
|
1722
1906
|
return this.interactive;
|
|
1723
1907
|
}
|
|
1724
1908
|
|
|
1909
|
+
get isRaw() {
|
|
1910
|
+
return Boolean(
|
|
1911
|
+
(this.stdinStream && this.stdinStream.isRaw)
|
|
1912
|
+
|| (this._subCommand && (this._subCommand.isRaw || (this._subCommand.stdinStream && this._subCommand.stdinStream.isRaw)))
|
|
1913
|
+
);
|
|
1914
|
+
}
|
|
1915
|
+
|
|
1916
|
+
get _inAltScreen() {
|
|
1917
|
+
return Boolean(this.__inAltScreen || (this._subCommand && this._subCommand._inAltScreen));
|
|
1918
|
+
}
|
|
1919
|
+
|
|
1920
|
+
set _inAltScreen(val) {
|
|
1921
|
+
this.__inAltScreen = Boolean(val);
|
|
1922
|
+
}
|
|
1923
|
+
|
|
1725
1924
|
kill(signal = 'SIGINT') {
|
|
1726
1925
|
if (this._subCommand && typeof this._subCommand.kill === 'function') {
|
|
1727
1926
|
this._subCommand.kill(signal);
|
|
@@ -1739,9 +1938,10 @@ function createGenericWasmCommand(pkgMeta, config = {}, binNameOverride = null)
|
|
|
1739
1938
|
const args = [...(this.arguments || [])];
|
|
1740
1939
|
const isHelp = args.includes('--help') || args.includes('-h');
|
|
1741
1940
|
const isVersion = !isHelp && (args.includes('--version') || args.includes('-V') || args.includes('-v'));
|
|
1941
|
+
const fs = this.fs || (this.context && this.context.fs);
|
|
1742
1942
|
this._inspectedWasm = null;
|
|
1743
1943
|
this._persistedWasmBytes = pkgMeta.wasmPath
|
|
1744
|
-
? readBytesFromFs(
|
|
1944
|
+
? readBytesFromFs(fs, pkgMeta.wasmPath)
|
|
1745
1945
|
: null;
|
|
1746
1946
|
if (isHelp && this._persistedWasmBytes && this._persistedWasmBytes.byteLength > 0) {
|
|
1747
1947
|
this._inspectedWasm = inspectWasmBytes(this._persistedWasmBytes);
|
|
@@ -1809,7 +2009,7 @@ function createGenericWasmCommand(pkgMeta, config = {}, binNameOverride = null)
|
|
|
1809
2009
|
await this._runRawWasmBytes(bytes, args);
|
|
1810
2010
|
} else if (typeof target === 'function') {
|
|
1811
2011
|
// 3. Factory / constructor / CLI function (`export default function(opts)` or class)
|
|
1812
|
-
await this._runFunctionOrEmscripten(target, args, isHelp);
|
|
2012
|
+
await this._runFunctionOrEmscripten(target, args, isHelp, isVersion);
|
|
1813
2013
|
} else if (target && typeof target === 'object') {
|
|
1814
2014
|
// 4. Standard CLI module object (`main`, `run`, `callMain`, `exec`)
|
|
1815
2015
|
if (target.ready) await target.ready;
|
|
@@ -1919,14 +2119,32 @@ function createGenericWasmCommand(pkgMeta, config = {}, binNameOverride = null)
|
|
|
1919
2119
|
wasmCmd.context = this.context;
|
|
1920
2120
|
wasmCmd.fs = this.fs;
|
|
1921
2121
|
wasmCmd._pkgMeta = pkgMeta;
|
|
1922
|
-
|
|
1923
|
-
|
|
2122
|
+
wasmCmd.stdinStream = this.stdinStream;
|
|
2123
|
+
wasmCmd.stdout = this.stdout;
|
|
2124
|
+
wasmCmd.stderr = this.stderr;
|
|
2125
|
+
wasmCmd.stdin = (this.stdin && !this.stdin.isTTY) ? this.stdin : this.stdinStream;
|
|
2126
|
+
if (this._onStdinStreamData && this.stdinStream && typeof this.stdinStream.off === 'function') {
|
|
2127
|
+
this.stdinStream.off('data', this._onStdinStreamData);
|
|
1924
2128
|
}
|
|
2129
|
+
const onSubInteractive = () => {
|
|
2130
|
+
this.emit('interactive');
|
|
2131
|
+
if (this.context && typeof this.context.emit === 'function') {
|
|
2132
|
+
this.context.emit('interactive');
|
|
2133
|
+
}
|
|
2134
|
+
};
|
|
2135
|
+
wasmCmd.on('interactive', onSubInteractive);
|
|
1925
2136
|
this._subCommand = wasmCmd;
|
|
1926
2137
|
try {
|
|
1927
2138
|
await wasmCmd._runWasm(bytes);
|
|
1928
2139
|
} finally {
|
|
2140
|
+
wasmCmd.removeListener('interactive', onSubInteractive);
|
|
2141
|
+
if (wasmCmd._inAltScreen) {
|
|
2142
|
+
wasmCmd._exitAltScreen();
|
|
2143
|
+
}
|
|
1929
2144
|
this._subCommand = null;
|
|
2145
|
+
if (this._onStdinStreamData && this.stdinStream && typeof this.stdinStream.on === 'function') {
|
|
2146
|
+
this.stdinStream.on('data', this._onStdinStreamData);
|
|
2147
|
+
}
|
|
1930
2148
|
}
|
|
1931
2149
|
for (const line of wasmCmd.result.getStdOutput()) {
|
|
1932
2150
|
if (line.type === 'out') this.stdOut = line.string;
|
|
@@ -1978,8 +2196,7 @@ function createGenericWasmCommand(pkgMeta, config = {}, binNameOverride = null)
|
|
|
1978
2196
|
}
|
|
1979
2197
|
if (targetPath.endsWith('.wasm') || targetPath.endsWith('.wat')) {
|
|
1980
2198
|
try {
|
|
1981
|
-
|
|
1982
|
-
else this.fs.setMode(targetPath, 0o755);
|
|
2199
|
+
this.fs.setMode(targetPath, 0o755);
|
|
1983
2200
|
} catch { /* ignore */ }
|
|
1984
2201
|
}
|
|
1985
2202
|
},
|
|
@@ -1997,7 +2214,7 @@ function createGenericWasmCommand(pkgMeta, config = {}, binNameOverride = null)
|
|
|
1997
2214
|
};
|
|
1998
2215
|
}
|
|
1999
2216
|
|
|
2000
|
-
async _runFunctionOrEmscripten(fn, args, isHelp = false) {
|
|
2217
|
+
async _runFunctionOrEmscripten(fn, args, isHelp = false, isVersion = false) {
|
|
2001
2218
|
const outLines = [];
|
|
2002
2219
|
const errLines = [];
|
|
2003
2220
|
const stdinBytes = new TextEncoder().encode(this.stdin || '');
|
|
@@ -2275,8 +2492,7 @@ function createGenericWasmCommand(pkgMeta, config = {}, binNameOverride = null)
|
|
|
2275
2492
|
writeBytesToFs(this.fs, arg, data, mime);
|
|
2276
2493
|
if (arg.endsWith('.wasm') || arg.endsWith('.wat')) {
|
|
2277
2494
|
try {
|
|
2278
|
-
|
|
2279
|
-
else this.fs.setMode(arg, 0o755);
|
|
2495
|
+
this.fs.setMode(arg, 0o755);
|
|
2280
2496
|
} catch { /* ignore */ }
|
|
2281
2497
|
}
|
|
2282
2498
|
}
|
|
@@ -2290,6 +2506,38 @@ function createGenericWasmCommand(pkgMeta, config = {}, binNameOverride = null)
|
|
|
2290
2506
|
return GenericWasmCommand;
|
|
2291
2507
|
}
|
|
2292
2508
|
|
|
2509
|
+
function isWasmFile(filepath, content) {
|
|
2510
|
+
if (filepath && (filepath.endsWith('.wasm') || filepath.endsWith('.wat'))) return true;
|
|
2511
|
+
if (content instanceof Uint8Array) {
|
|
2512
|
+
return content.length >= 4 && content[0] === 0x00 && content[1] === 0x61 && content[2] === 0x73 && content[3] === 0x6d;
|
|
2513
|
+
}
|
|
2514
|
+
if (typeof content !== 'string') return false;
|
|
2515
|
+
// Binary WASM: magic bytes \0asm
|
|
2516
|
+
if (content.charCodeAt(0) === 0 && content.slice(1, 4) === 'asm') return true;
|
|
2517
|
+
// WAT text format
|
|
2518
|
+
return content.trimStart().startsWith('(module');
|
|
2519
|
+
}
|
|
2520
|
+
|
|
2521
|
+
function readSiblingPkgMeta(fs, filepath) {
|
|
2522
|
+
if (!fs || !filepath || typeof fs.exists !== 'function' || typeof fs.getFileByPath !== 'function') {
|
|
2523
|
+
return null;
|
|
2524
|
+
}
|
|
2525
|
+
try {
|
|
2526
|
+
const dirParts = filepath.split('/').filter(Boolean);
|
|
2527
|
+
dirParts.pop();
|
|
2528
|
+
const pkgJsonPath = (dirParts.length ? '/' + dirParts.join('/') : '') + '/package.json';
|
|
2529
|
+
if (fs.exists(pkgJsonPath)) {
|
|
2530
|
+
const rawJson = fs.getFileByPath(pkgJsonPath);
|
|
2531
|
+
if (typeof rawJson === 'string') {
|
|
2532
|
+
return JSON.parse(rawJson);
|
|
2533
|
+
}
|
|
2534
|
+
}
|
|
2535
|
+
} catch (_) {}
|
|
2536
|
+
return null;
|
|
2537
|
+
}
|
|
2538
|
+
|
|
2539
|
+
WasmCommand.isWasmFile = isWasmFile;
|
|
2540
|
+
WasmCommand.readSiblingPkgMeta = readSiblingPkgMeta;
|
|
2293
2541
|
WasmCommand.readBytesFromFs = readBytesFromFs;
|
|
2294
2542
|
WasmCommand.writeBytesToFs = writeBytesToFs;
|
|
2295
2543
|
WasmCommand.loadModule = loadModule;
|
|
@@ -2300,4 +2548,3 @@ WasmCommand.createGenericWasmCommand = createGenericWasmCommand;
|
|
|
2300
2548
|
WasmCommand.createCommand = createGenericWasmCommand;
|
|
2301
2549
|
|
|
2302
2550
|
module.exports = WasmCommand;
|
|
2303
|
-
|