brsh 3.7.0 → 3.8.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 +128 -0
- package/dist/shell.es.js +633 -458
- package/dist/shell.es.js.map +1 -1
- package/dist/shell.min.js +18 -18
- package/dist/shell.min.js.map +1 -1
- package/dist/wbt.es.js +626 -451
- package/dist/wbt.es.js.map +1 -1
- package/dist/wbt.min.js +12 -12
- package/dist/wbt.min.js.map +1 -1
- package/lib/Command.js +3 -2
- package/lib/Readme.md +23 -1
- package/lib/TtyStream.js +12 -2
- package/lib/WasmCommand.js +380 -37
- package/package.json +1 -1
- package/test/wasm-cli.test.js +64 -0
- package/test/wbt.test.js +43 -4
package/lib/Command.js
CHANGED
|
@@ -29,7 +29,7 @@ class Command extends EventEmitter {
|
|
|
29
29
|
onWrite: (chunk) => this.emit('stderr', chunk)
|
|
30
30
|
});
|
|
31
31
|
|
|
32
|
-
this.
|
|
32
|
+
this._onStdinStreamData = (chunk) => {
|
|
33
33
|
if (!this.captureInput) return;
|
|
34
34
|
const keys = parseVtKeys(chunk);
|
|
35
35
|
for (const k of keys) {
|
|
@@ -40,7 +40,8 @@ class Command extends EventEmitter {
|
|
|
40
40
|
}
|
|
41
41
|
this.onInput(k);
|
|
42
42
|
}
|
|
43
|
-
}
|
|
43
|
+
};
|
|
44
|
+
this.stdinStream.on('data', this._onStdinStreamData);
|
|
44
45
|
}
|
|
45
46
|
|
|
46
47
|
get name() {
|
package/lib/Readme.md
CHANGED
|
@@ -113,8 +113,30 @@ Wrap any WASI `.wasm` binary or ESM/Emscripten module into a `brsh` command:
|
|
|
113
113
|
```js
|
|
114
114
|
const MyWasmTool = Shell.createWasmCommand({
|
|
115
115
|
name: 'mytool',
|
|
116
|
-
wasmUrl: 'https://example.com/mytool.wasm'
|
|
116
|
+
wasmUrl: 'https://example.com/mytool.wasm',
|
|
117
|
+
interactive: true // Set to true for full-screen TUIs / games (enables alternate screen buffer & raw TTY input)
|
|
117
118
|
});
|
|
118
119
|
|
|
119
120
|
shell.context.setCommand(MyWasmTool);
|
|
120
121
|
```
|
|
122
|
+
|
|
123
|
+
### Options for `Shell.createWasmCommand`
|
|
124
|
+
|
|
125
|
+
| Option | Type | Description |
|
|
126
|
+
| --- | --- | --- |
|
|
127
|
+
| `name` | `String` | Command name invoked in the shell |
|
|
128
|
+
| `wasmUrl` | `String` | URL to fetch the `.wasm` binary from |
|
|
129
|
+
| `wasmPath` | `String` | Optional path to `.wasm` binary stored in the virtual filesystem (`this.fs`) |
|
|
130
|
+
| `interactive` | `Boolean` | When `true`, automatically enters the alternate screen buffer (`\x1b[?1049h`) and routes live raw keyboard events to `stdin` (`fd 0`) and `stderr` (`fd 2`) |
|
|
131
|
+
| `bin` | `String` | Optional binary name override |
|
|
132
|
+
| `type` | `String` | `'wasm'` / `'custom-wasm'` for raw WASI, or omit for generic ESM/Emscripten |
|
|
133
|
+
|
|
134
|
+
### Compiling C / C++ Applications for `brsh`
|
|
135
|
+
|
|
136
|
+
`brsh` supports WASI Preview 1 binaries with non-blocking I/O (`FDFLAGS_NONBLOCK`), cooperative sleep/timers via Asyncify (`poll_oneoff`, `clock_nanosleep`), and terminal dimension queries.
|
|
137
|
+
|
|
138
|
+
* For POSIX terminal raw mode, baud rates, and `TIOCGWINSZ`, use [**`wasi-termios-brsh`**](https://github.com/eliotstocker/wasi-termios-brsh).
|
|
139
|
+
* For a complete real-world reference implementation of an interactive C TUI editor with ncurses targeting `brsh`, inspect [**`nano-wasm`**](https://github.com/eliotstocker/nano-wasm).
|
|
140
|
+
* For complete step-by-step instructions on compiling with `wasi-sdk`, configuring `termios`, and instrumenting with `wasm-opt --asyncify`, see [Compiling C / C++ Applications in the main Readme](../Readme.md#compiling-c--c-applications-for-brsh).
|
|
141
|
+
|
|
142
|
+
|
package/lib/TtyStream.js
CHANGED
|
@@ -103,7 +103,12 @@ class TtyStream extends EventEmitter {
|
|
|
103
103
|
if (typeof this._onWrite === 'function') {
|
|
104
104
|
this._onWrite(chunk);
|
|
105
105
|
}
|
|
106
|
-
this.
|
|
106
|
+
this._isPushing = true;
|
|
107
|
+
try {
|
|
108
|
+
this.emit('data', this._formatChunk(chunk));
|
|
109
|
+
} finally {
|
|
110
|
+
this._isPushing = false;
|
|
111
|
+
}
|
|
107
112
|
if (typeof encoding === 'function') encoding();
|
|
108
113
|
else if (typeof callback === 'function') callback();
|
|
109
114
|
return true;
|
|
@@ -111,7 +116,12 @@ class TtyStream extends EventEmitter {
|
|
|
111
116
|
|
|
112
117
|
pushData(chunk) {
|
|
113
118
|
if (chunk == null) return;
|
|
114
|
-
this.
|
|
119
|
+
this._isPushing = true;
|
|
120
|
+
try {
|
|
121
|
+
this.emit('data', this._formatChunk(chunk));
|
|
122
|
+
} finally {
|
|
123
|
+
this._isPushing = false;
|
|
124
|
+
}
|
|
115
125
|
}
|
|
116
126
|
|
|
117
127
|
_formatChunk(chunk) {
|
package/lib/WasmCommand.js
CHANGED
|
@@ -5,12 +5,14 @@ const { BinaryFile } = require('./FileSystem');
|
|
|
5
5
|
|
|
6
6
|
// WASI errno constants
|
|
7
7
|
const ERRNO_SUCCESS = 0;
|
|
8
|
+
const ERRNO_AGAIN = 6;
|
|
8
9
|
const ERRNO_BADF = 8;
|
|
9
10
|
const ERRNO_EXIST = 20;
|
|
10
11
|
const ERRNO_INVAL = 28;
|
|
11
12
|
const ERRNO_ISDIR = 31;
|
|
12
13
|
const ERRNO_NOENT = 44;
|
|
13
14
|
const ERRNO_NOTDIR = 54;
|
|
15
|
+
const ERRNO_NOSYS = 52;
|
|
14
16
|
const ERRNO_SPIPE = 70;
|
|
15
17
|
|
|
16
18
|
// WASI filetype constants
|
|
@@ -27,6 +29,10 @@ const OFLAGS_TRUNC = 1 << 3;
|
|
|
27
29
|
|
|
28
30
|
// WASI fdflags
|
|
29
31
|
const FDFLAGS_APPEND = 1 << 0;
|
|
32
|
+
const FDFLAGS_DSYNC = 1 << 1;
|
|
33
|
+
const FDFLAGS_NONBLOCK = 1 << 2;
|
|
34
|
+
const FDFLAGS_RSYNC = 1 << 3;
|
|
35
|
+
const FDFLAGS_SYNC = 1 << 4;
|
|
30
36
|
|
|
31
37
|
class WasmCommand extends ScriptCommand {
|
|
32
38
|
// this.script = filepath (set by ScriptCommand parent constructor)
|
|
@@ -104,6 +110,9 @@ class WasmCommand extends ScriptCommand {
|
|
|
104
110
|
|
|
105
111
|
onInput(key) {
|
|
106
112
|
if (this._handlingStreamData) return;
|
|
113
|
+
if (this.stdinStream && this.stdinStream._isPushing) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
107
116
|
if (typeof this._externalKeyHandler === 'function') {
|
|
108
117
|
this._externalKeyHandler(key);
|
|
109
118
|
return;
|
|
@@ -216,11 +225,31 @@ class WasmCommand extends ScriptCommand {
|
|
|
216
225
|
let onResize = null;
|
|
217
226
|
let onStreamData = null;
|
|
218
227
|
|
|
219
|
-
const
|
|
220
|
-
|
|
221
|
-
|
|
228
|
+
const isTtyEnvironment = (this.stdin === undefined || this.stdin === null || this.stdin === this.stdinStream || (this.stdin && this.stdin.isTTY));
|
|
229
|
+
|
|
230
|
+
let isInteractive = Boolean(
|
|
231
|
+
this.interactive && isTtyEnvironment
|
|
222
232
|
);
|
|
223
233
|
|
|
234
|
+
const promoteToInteractive = () => {
|
|
235
|
+
if (isInteractive || !isTtyEnvironment) return;
|
|
236
|
+
isInteractive = true;
|
|
237
|
+
this._isInteractive = true;
|
|
238
|
+
|
|
239
|
+
if (outBuf[1]) {
|
|
240
|
+
if (this.stdout && typeof this.stdout.write === 'function') this.stdout.write(outBuf[1]);
|
|
241
|
+
outBuf[1] = '';
|
|
242
|
+
}
|
|
243
|
+
if (outBuf[2]) {
|
|
244
|
+
if (this.stderr && typeof this.stderr.write === 'function') this.stderr.write(outBuf[2]);
|
|
245
|
+
outBuf[2] = '';
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if (this.stdinStream && typeof this.stdinStream.setRawMode === 'function') {
|
|
249
|
+
this.stdinStream.setRawMode(true);
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
|
|
224
253
|
const writeFd = (fd, text) => {
|
|
225
254
|
if (isInteractive && (fd === 1 || fd === 2)) {
|
|
226
255
|
if (fd === 1 && this.stdout) {
|
|
@@ -290,18 +319,21 @@ class WasmCommand extends ScriptCommand {
|
|
|
290
319
|
if (b) enqueueBytes(b);
|
|
291
320
|
};
|
|
292
321
|
|
|
293
|
-
if (
|
|
322
|
+
if (this._onStdinStreamData && this.stdinStream && typeof this.stdinStream.off === 'function') {
|
|
323
|
+
this.stdinStream.off('data', this._onStdinStreamData);
|
|
324
|
+
}
|
|
325
|
+
if (isTtyEnvironment && this.stdinStream && typeof this.stdinStream.on === 'function') {
|
|
294
326
|
this.stdinStream.on('data', onStreamData);
|
|
295
327
|
}
|
|
296
|
-
if (
|
|
328
|
+
if (isTtyEnvironment && this.stdin && this.stdin !== this.stdinStream && typeof this.stdin.on === 'function') {
|
|
297
329
|
this.stdin.on('data', onStreamData);
|
|
298
330
|
}
|
|
299
331
|
|
|
300
|
-
const stdinBytes = (!
|
|
332
|
+
const stdinBytes = (!isTtyEnvironment && typeof this.stdin === 'string')
|
|
301
333
|
? encoder.encode(this.stdin)
|
|
302
|
-
: (!
|
|
334
|
+
: (!isTtyEnvironment && this.stdin instanceof Uint8Array
|
|
303
335
|
? this.stdin
|
|
304
|
-
: (!
|
|
336
|
+
: (!isTtyEnvironment && typeof Buffer !== 'undefined' && Buffer.isBuffer(this.stdin) ? new Uint8Array(this.stdin) : new Uint8Array(0)));
|
|
305
337
|
let stdinPos = 0;
|
|
306
338
|
|
|
307
339
|
// ── Virtual WASI file descriptor table backed by this.context.fs ──
|
|
@@ -320,6 +352,7 @@ class WasmCommand extends ScriptCommand {
|
|
|
320
352
|
[3, { type: 'preopen', path: '/', guestName: '/' }],
|
|
321
353
|
[4, { type: 'preopen', path: cwd, guestName: '.' }]
|
|
322
354
|
]);
|
|
355
|
+
const fdFlagsMap = new Map([[0, 0], [1, 0], [2, 0]]);
|
|
323
356
|
|
|
324
357
|
const getParentPath = (p) => {
|
|
325
358
|
const parts = p.split('/').filter(Boolean);
|
|
@@ -410,7 +443,30 @@ class WasmCommand extends ScriptCommand {
|
|
|
410
443
|
}
|
|
411
444
|
|
|
412
445
|
const entry = fdTable.get(fd);
|
|
413
|
-
if (!entry
|
|
446
|
+
if (!entry) return ERRNO_BADF;
|
|
447
|
+
|
|
448
|
+
if (entry.type === 'dev_null' || entry.type === 'dev_zero') {
|
|
449
|
+
let total = 0;
|
|
450
|
+
for (let i = 0; i < iovs_len; i++) {
|
|
451
|
+
total += view.getUint32(iovs + i * 8 + 4, true);
|
|
452
|
+
}
|
|
453
|
+
view.setUint32(nwritten_ptr, total, true);
|
|
454
|
+
return ERRNO_SUCCESS;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
if (entry.type === 'dev_tty') {
|
|
458
|
+
let total = 0;
|
|
459
|
+
for (let i = 0; i < iovs_len; i++) {
|
|
460
|
+
const ptr = view.getUint32(iovs + i * 8, true);
|
|
461
|
+
const len = view.getUint32(iovs + i * 8 + 4, true);
|
|
462
|
+
if (len > 0) writeFd(1, decoder.decode(new Uint8Array(memRef.buffer, ptr, len)));
|
|
463
|
+
total += len;
|
|
464
|
+
}
|
|
465
|
+
view.setUint32(nwritten_ptr, total, true);
|
|
466
|
+
return ERRNO_SUCCESS;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
if (entry.type !== 'file') return ERRNO_BADF;
|
|
414
470
|
|
|
415
471
|
let total = 0;
|
|
416
472
|
for (let i = 0; i < iovs_len; i++) {
|
|
@@ -438,8 +494,18 @@ class WasmCommand extends ScriptCommand {
|
|
|
438
494
|
if (!memRef) return ERRNO_BADF;
|
|
439
495
|
const view = new DataView(memRef.buffer);
|
|
440
496
|
|
|
441
|
-
|
|
442
|
-
|
|
497
|
+
const entry = fdTable.get(fd);
|
|
498
|
+
const isDevTty = Boolean(entry && entry.type === 'dev_tty');
|
|
499
|
+
const isTtyInput = (fd === 0) || (isInteractive && fd === 2) || isDevTty;
|
|
500
|
+
|
|
501
|
+
if (isTtyInput) {
|
|
502
|
+
if (isDevTty || fd === 2) {
|
|
503
|
+
promoteToInteractive();
|
|
504
|
+
}
|
|
505
|
+
const flags = fdFlagsMap.get(fd) || 0;
|
|
506
|
+
const isNonBlocking = Boolean(flags & FDFLAGS_NONBLOCK);
|
|
507
|
+
|
|
508
|
+
if (hasAsyncify) {
|
|
443
509
|
const state = (typeof instance?.exports?.asyncify_get_state === 'function')
|
|
444
510
|
? instance.exports.asyncify_get_state()
|
|
445
511
|
: 0;
|
|
@@ -473,6 +539,11 @@ class WasmCommand extends ScriptCommand {
|
|
|
473
539
|
return ERRNO_SUCCESS;
|
|
474
540
|
}
|
|
475
541
|
|
|
542
|
+
if (isNonBlocking) {
|
|
543
|
+
view.setUint32(nread_ptr, 0, true);
|
|
544
|
+
return ERRNO_AGAIN;
|
|
545
|
+
}
|
|
546
|
+
|
|
476
547
|
instance.exports.asyncify_start_unwind(asyncifyDataAddr);
|
|
477
548
|
asyncifyPromise = new Promise(resolve => {
|
|
478
549
|
pendingReadResolve = resolve;
|
|
@@ -481,6 +552,20 @@ class WasmCommand extends ScriptCommand {
|
|
|
481
552
|
return ERRNO_SUCCESS;
|
|
482
553
|
}
|
|
483
554
|
|
|
555
|
+
if (inputQueue.length > 0) {
|
|
556
|
+
let total = 0;
|
|
557
|
+
for (let i = 0; i < iovs_len && inputQueue.length > 0; i++) {
|
|
558
|
+
const ptr = view.getUint32(iovs + i * 8, true);
|
|
559
|
+
const len = view.getUint32(iovs + i * 8 + 4, true);
|
|
560
|
+
const chunk = Math.min(len, inputQueue.length);
|
|
561
|
+
const slice = inputQueue.splice(0, chunk);
|
|
562
|
+
new Uint8Array(memRef.buffer).set(slice, ptr);
|
|
563
|
+
total += chunk;
|
|
564
|
+
}
|
|
565
|
+
view.setUint32(nread_ptr, total, true);
|
|
566
|
+
return ERRNO_SUCCESS;
|
|
567
|
+
}
|
|
568
|
+
|
|
484
569
|
let total = 0;
|
|
485
570
|
for (let i = 0; i < iovs_len && stdinPos < stdinBytes.length; i++) {
|
|
486
571
|
const ptr = view.getUint32(iovs + i * 8, true);
|
|
@@ -490,12 +575,51 @@ class WasmCommand extends ScriptCommand {
|
|
|
490
575
|
stdinPos += chunk;
|
|
491
576
|
total += chunk;
|
|
492
577
|
}
|
|
578
|
+
if (total === 0 && isNonBlocking && stdinPos >= stdinBytes.length) {
|
|
579
|
+
view.setUint32(nread_ptr, 0, true);
|
|
580
|
+
return ERRNO_AGAIN;
|
|
581
|
+
}
|
|
493
582
|
view.setUint32(nread_ptr, total, true);
|
|
494
583
|
return ERRNO_SUCCESS;
|
|
495
584
|
}
|
|
496
585
|
|
|
497
|
-
|
|
498
|
-
|
|
586
|
+
if (!entry) return ERRNO_BADF;
|
|
587
|
+
|
|
588
|
+
if (entry.type === 'dev_null') {
|
|
589
|
+
view.setUint32(nread_ptr, 0, true);
|
|
590
|
+
return ERRNO_SUCCESS;
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
if (entry.type === 'dev_zero') {
|
|
594
|
+
let total = 0;
|
|
595
|
+
for (let i = 0; i < iovs_len; i++) {
|
|
596
|
+
const ptr = view.getUint32(iovs + i * 8, true);
|
|
597
|
+
const len = view.getUint32(iovs + i * 8 + 4, true);
|
|
598
|
+
new Uint8Array(memRef.buffer, ptr, len).fill(0);
|
|
599
|
+
total += len;
|
|
600
|
+
}
|
|
601
|
+
view.setUint32(nread_ptr, total, true);
|
|
602
|
+
return ERRNO_SUCCESS;
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
if (entry.type === 'dev_urandom') {
|
|
606
|
+
let total = 0;
|
|
607
|
+
for (let i = 0; i < iovs_len; i++) {
|
|
608
|
+
const ptr = view.getUint32(iovs + i * 8, true);
|
|
609
|
+
const len = view.getUint32(iovs + i * 8 + 4, true);
|
|
610
|
+
const targetBuf = new Uint8Array(memRef.buffer, ptr, len);
|
|
611
|
+
if (typeof crypto !== 'undefined' && crypto.getRandomValues) {
|
|
612
|
+
crypto.getRandomValues(targetBuf);
|
|
613
|
+
} else {
|
|
614
|
+
for (let j = 0; j < len; j++) targetBuf[j] = Math.floor(Math.random() * 256);
|
|
615
|
+
}
|
|
616
|
+
total += len;
|
|
617
|
+
}
|
|
618
|
+
view.setUint32(nread_ptr, total, true);
|
|
619
|
+
return ERRNO_SUCCESS;
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
if (entry.type !== 'file') return ERRNO_BADF;
|
|
499
623
|
|
|
500
624
|
let total = 0;
|
|
501
625
|
for (let i = 0; i < iovs_len && entry.pos < entry.bytes.length; i++) {
|
|
@@ -513,7 +637,11 @@ class WasmCommand extends ScriptCommand {
|
|
|
513
637
|
if (!memRef) return ERRNO_BADF;
|
|
514
638
|
if (fd <= 2) return ERRNO_SPIPE;
|
|
515
639
|
const entry = fdTable.get(fd);
|
|
516
|
-
if (!entry
|
|
640
|
+
if (!entry) return ERRNO_BADF;
|
|
641
|
+
if (entry.type === 'dev_tty' || entry.type === 'dev_null' || entry.type === 'dev_zero' || entry.type === 'dev_urandom') {
|
|
642
|
+
return ERRNO_SPIPE;
|
|
643
|
+
}
|
|
644
|
+
if (entry.type !== 'file') return ERRNO_BADF;
|
|
517
645
|
|
|
518
646
|
// Support both BigInt (i64) and split 32-bit calls
|
|
519
647
|
let off;
|
|
@@ -553,8 +681,9 @@ class WasmCommand extends ScriptCommand {
|
|
|
553
681
|
if (fd <= 2) return ERRNO_SUCCESS;
|
|
554
682
|
const entry = fdTable.get(fd);
|
|
555
683
|
if (!entry) return ERRNO_BADF;
|
|
556
|
-
flushOpenFile(entry);
|
|
684
|
+
if (entry.type === 'file') flushOpenFile(entry);
|
|
557
685
|
fdTable.delete(fd);
|
|
686
|
+
fdFlagsMap.delete(fd);
|
|
558
687
|
return ERRNO_SUCCESS;
|
|
559
688
|
},
|
|
560
689
|
fd_sync: (fd) => {
|
|
@@ -595,18 +724,36 @@ class WasmCommand extends ScriptCommand {
|
|
|
595
724
|
} else {
|
|
596
725
|
const entry = fdTable.get(fd);
|
|
597
726
|
if (!entry) return ERRNO_BADF;
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
727
|
+
if (entry.type === 'dev_null' || entry.type === 'dev_zero' || entry.type === 'dev_urandom' || entry.type === 'dev_tty') {
|
|
728
|
+
filetype = FILETYPE_CHARACTER_DEVICE;
|
|
729
|
+
} else {
|
|
730
|
+
filetype = (entry.type === 'preopen' || entry.type === 'dir')
|
|
731
|
+
? FILETYPE_DIRECTORY
|
|
732
|
+
: FILETYPE_REGULAR_FILE;
|
|
733
|
+
}
|
|
601
734
|
}
|
|
602
735
|
// struct fdstat (24 bytes): fs_filetype (u8), fs_flags (u16), rights_base (u64), rights_inheriting (u64)
|
|
603
736
|
for (let i = 0; i < 24; i++) view.setUint8(stat_ptr + i, 0);
|
|
604
737
|
view.setUint8(stat_ptr, filetype);
|
|
738
|
+
const flags = fdFlagsMap.get(fd) || (fdTable.get(fd)?.append ? FDFLAGS_APPEND : 0);
|
|
739
|
+
view.setUint16(stat_ptr + 2, flags, true);
|
|
605
740
|
view.setBigUint64(stat_ptr + 8, 0xffffffffffffffffn, true);
|
|
606
741
|
view.setBigUint64(stat_ptr + 16, 0xffffffffffffffffn, true);
|
|
607
742
|
return ERRNO_SUCCESS;
|
|
608
743
|
},
|
|
609
|
-
fd_fdstat_set_flags: () =>
|
|
744
|
+
fd_fdstat_set_flags: (fd, flags) => {
|
|
745
|
+
if (!memRef) return ERRNO_BADF;
|
|
746
|
+
if (fd > 2 && !fdTable.has(fd)) return ERRNO_BADF;
|
|
747
|
+
fdFlagsMap.set(fd, flags);
|
|
748
|
+
const entry = fdTable.get(fd);
|
|
749
|
+
if (entry) {
|
|
750
|
+
entry.append = Boolean(flags & FDFLAGS_APPEND);
|
|
751
|
+
}
|
|
752
|
+
if ((fd === 0 || fd === 2 || (entry && entry.type === 'dev_tty')) && (flags & FDFLAGS_NONBLOCK)) {
|
|
753
|
+
promoteToInteractive();
|
|
754
|
+
}
|
|
755
|
+
return ERRNO_SUCCESS;
|
|
756
|
+
},
|
|
610
757
|
fd_filestat_get: (fd, buf_ptr) => {
|
|
611
758
|
if (!memRef) return ERRNO_BADF;
|
|
612
759
|
const view = new DataView(memRef.buffer);
|
|
@@ -617,6 +764,11 @@ class WasmCommand extends ScriptCommand {
|
|
|
617
764
|
}
|
|
618
765
|
const entry = fdTable.get(fd);
|
|
619
766
|
if (!entry) return ERRNO_BADF;
|
|
767
|
+
if (entry.type === 'dev_null' || entry.type === 'dev_zero' || entry.type === 'dev_urandom' || entry.type === 'dev_tty') {
|
|
768
|
+
view.setUint8(buf_ptr + 16, FILETYPE_CHARACTER_DEVICE);
|
|
769
|
+
view.setBigUint64(buf_ptr + 24, 1n, true); // nlink
|
|
770
|
+
return ERRNO_SUCCESS;
|
|
771
|
+
}
|
|
620
772
|
const isDir = entry.type === 'preopen' || entry.type === 'dir';
|
|
621
773
|
view.setUint8(buf_ptr + 16, isDir ? FILETYPE_DIRECTORY : FILETYPE_REGULAR_FILE);
|
|
622
774
|
view.setBigUint64(buf_ptr + 24, 1n, true); // nlink
|
|
@@ -629,6 +781,33 @@ class WasmCommand extends ScriptCommand {
|
|
|
629
781
|
const absPath = resolveGuestPath(dirfd, relPath);
|
|
630
782
|
if (!absPath) return ERRNO_BADF;
|
|
631
783
|
|
|
784
|
+
const cleanPath = absPath.replace(/\/+/g, '/').replace(/\/$/, '') || '/';
|
|
785
|
+
if (cleanPath === '/dev/null') {
|
|
786
|
+
const newFd = nextFd++;
|
|
787
|
+
fdTable.set(newFd, { type: 'dev_null', path: '/dev/null' });
|
|
788
|
+
new DataView(memRef.buffer).setUint32(opened_fd_ptr, newFd, true);
|
|
789
|
+
return ERRNO_SUCCESS;
|
|
790
|
+
}
|
|
791
|
+
if (cleanPath === '/dev/zero') {
|
|
792
|
+
const newFd = nextFd++;
|
|
793
|
+
fdTable.set(newFd, { type: 'dev_zero', path: '/dev/zero' });
|
|
794
|
+
new DataView(memRef.buffer).setUint32(opened_fd_ptr, newFd, true);
|
|
795
|
+
return ERRNO_SUCCESS;
|
|
796
|
+
}
|
|
797
|
+
if (cleanPath === '/dev/urandom' || cleanPath === '/dev/random') {
|
|
798
|
+
const newFd = nextFd++;
|
|
799
|
+
fdTable.set(newFd, { type: 'dev_urandom', path: cleanPath });
|
|
800
|
+
new DataView(memRef.buffer).setUint32(opened_fd_ptr, newFd, true);
|
|
801
|
+
return ERRNO_SUCCESS;
|
|
802
|
+
}
|
|
803
|
+
if (cleanPath === '/dev/tty') {
|
|
804
|
+
promoteToInteractive();
|
|
805
|
+
const newFd = nextFd++;
|
|
806
|
+
fdTable.set(newFd, { type: 'dev_tty', path: '/dev/tty' });
|
|
807
|
+
new DataView(memRef.buffer).setUint32(opened_fd_ptr, newFd, true);
|
|
808
|
+
return ERRNO_SUCCESS;
|
|
809
|
+
}
|
|
810
|
+
|
|
632
811
|
const st = fs.stat(absPath);
|
|
633
812
|
const exists = st !== null;
|
|
634
813
|
|
|
@@ -681,6 +860,14 @@ class WasmCommand extends ScriptCommand {
|
|
|
681
860
|
const relPath = decoder.decode(new Uint8Array(memRef.buffer, path_ptr, path_len));
|
|
682
861
|
const absPath = resolveGuestPath(dirfd, relPath);
|
|
683
862
|
if (!absPath) return ERRNO_BADF;
|
|
863
|
+
const cleanPath = absPath.replace(/\/+/g, '/').replace(/\/$/, '') || '/';
|
|
864
|
+
if (cleanPath === '/dev/null' || cleanPath === '/dev/zero' || cleanPath === '/dev/urandom' || cleanPath === '/dev/random' || cleanPath === '/dev/tty') {
|
|
865
|
+
const view = new DataView(memRef.buffer);
|
|
866
|
+
for (let i = 0; i < 64; i++) view.setUint8(buf_ptr + i, 0);
|
|
867
|
+
view.setUint8(buf_ptr + 16, FILETYPE_CHARACTER_DEVICE);
|
|
868
|
+
view.setBigUint64(buf_ptr + 24, 1n, true);
|
|
869
|
+
return ERRNO_SUCCESS;
|
|
870
|
+
}
|
|
684
871
|
const st = fs.stat(absPath);
|
|
685
872
|
if (!st) return ERRNO_NOENT;
|
|
686
873
|
const view = new DataView(memRef.buffer);
|
|
@@ -810,6 +997,154 @@ class WasmCommand extends ScriptCommand {
|
|
|
810
997
|
}
|
|
811
998
|
return ERRNO_SUCCESS;
|
|
812
999
|
},
|
|
1000
|
+
clock_nanosleep: (id, flags, timeout_ptr, precision, rem_ptr) => {
|
|
1001
|
+
if (!memRef) return ERRNO_BADF;
|
|
1002
|
+
const view = new DataView(memRef.buffer);
|
|
1003
|
+
const timeoutNs = view.getBigUint64(timeout_ptr, true);
|
|
1004
|
+
let sleepMs = 0;
|
|
1005
|
+
if (flags & 1) { // TIMER_ABSTIME
|
|
1006
|
+
const nowNs = BigInt(Date.now()) * 1_000_000n;
|
|
1007
|
+
sleepMs = timeoutNs > nowNs ? Number((timeoutNs - nowNs) / 1_000_000n) : 0;
|
|
1008
|
+
} else {
|
|
1009
|
+
sleepMs = Number(timeoutNs / 1_000_000n);
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
if (hasAsyncify && sleepMs > 0) {
|
|
1013
|
+
const state = (typeof instance?.exports?.asyncify_get_state === 'function')
|
|
1014
|
+
? instance.exports.asyncify_get_state()
|
|
1015
|
+
: 0;
|
|
1016
|
+
|
|
1017
|
+
if (state === 2) {
|
|
1018
|
+
instance.exports.asyncify_stop_rewind();
|
|
1019
|
+
return ERRNO_SUCCESS;
|
|
1020
|
+
}
|
|
1021
|
+
|
|
1022
|
+
instance.exports.asyncify_start_unwind(asyncifyDataAddr);
|
|
1023
|
+
asyncifyPromise = new Promise(resolve => setTimeout(resolve, Math.max(1, sleepMs)));
|
|
1024
|
+
return ERRNO_SUCCESS;
|
|
1025
|
+
}
|
|
1026
|
+
return ERRNO_SUCCESS;
|
|
1027
|
+
},
|
|
1028
|
+
poll_oneoff: (in_ptr, out_ptr, nsubscriptions, nevents_ptr) => {
|
|
1029
|
+
if (!memRef) return ERRNO_BADF;
|
|
1030
|
+
const view = new DataView(memRef.buffer);
|
|
1031
|
+
|
|
1032
|
+
if (hasAsyncify) {
|
|
1033
|
+
const state = (typeof instance?.exports?.asyncify_get_state === 'function')
|
|
1034
|
+
? instance.exports.asyncify_get_state()
|
|
1035
|
+
: 0;
|
|
1036
|
+
|
|
1037
|
+
if (state === 2) {
|
|
1038
|
+
instance.exports.asyncify_stop_rewind();
|
|
1039
|
+
let nevents = 0;
|
|
1040
|
+
for (let i = 0; i < nsubscriptions; i++) {
|
|
1041
|
+
const subPtr = in_ptr + i * 48;
|
|
1042
|
+
const userdata = view.getBigUint64(subPtr, true);
|
|
1043
|
+
const type = view.getUint8(subPtr + 8);
|
|
1044
|
+
const outEventPtr = out_ptr + nevents * 32;
|
|
1045
|
+
|
|
1046
|
+
view.setBigUint64(outEventPtr, userdata, true);
|
|
1047
|
+
view.setUint16(outEventPtr + 8, ERRNO_SUCCESS, true);
|
|
1048
|
+
view.setUint8(outEventPtr + 10, type);
|
|
1049
|
+
for (let b = 11; b < 32; b++) view.setUint8(outEventPtr + b, 0);
|
|
1050
|
+
|
|
1051
|
+
if (type === 1) { // FD_READ
|
|
1052
|
+
view.setBigUint64(outEventPtr + 16, BigInt(inputQueue.length), true);
|
|
1053
|
+
}
|
|
1054
|
+
nevents++;
|
|
1055
|
+
}
|
|
1056
|
+
view.setUint32(nevents_ptr, nevents, true);
|
|
1057
|
+
return ERRNO_SUCCESS;
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
let immediateCount = 0;
|
|
1061
|
+
let minTimeoutMs = Infinity;
|
|
1062
|
+
|
|
1063
|
+
for (let i = 0; i < nsubscriptions; i++) {
|
|
1064
|
+
const subPtr = in_ptr + i * 48;
|
|
1065
|
+
const type = view.getUint8(subPtr + 8);
|
|
1066
|
+
|
|
1067
|
+
if (type === 0) { // EVENTTYPE_CLOCK
|
|
1068
|
+
const timeoutNs = view.getBigUint64(subPtr + 24, true);
|
|
1069
|
+
const flags = view.getUint16(subPtr + 40, true);
|
|
1070
|
+
let sleepMs = 0;
|
|
1071
|
+
if (flags & 1) { // ABSTIME
|
|
1072
|
+
const nowNs = BigInt(Date.now()) * 1_000_000n;
|
|
1073
|
+
sleepMs = timeoutNs > nowNs ? Number((timeoutNs - nowNs) / 1_000_000n) : 0;
|
|
1074
|
+
} else {
|
|
1075
|
+
sleepMs = Number(timeoutNs / 1_000_000n);
|
|
1076
|
+
}
|
|
1077
|
+
if (sleepMs < minTimeoutMs) minTimeoutMs = sleepMs;
|
|
1078
|
+
} else if (type === 1) { // EVENTTYPE_FD_READ
|
|
1079
|
+
const fd = view.getUint32(subPtr + 16, true);
|
|
1080
|
+
const entry = fdTable.get(fd);
|
|
1081
|
+
const isDevTty = Boolean(entry && entry.type === 'dev_tty');
|
|
1082
|
+
if (fd === 0 || fd === 2 || isDevTty) {
|
|
1083
|
+
promoteToInteractive();
|
|
1084
|
+
}
|
|
1085
|
+
if ((fd === 0 || (isInteractive && fd === 2) || isDevTty) && inputQueue.length > 0) {
|
|
1086
|
+
immediateCount++;
|
|
1087
|
+
}
|
|
1088
|
+
} else if (type === 2) { // EVENTTYPE_FD_WRITE
|
|
1089
|
+
immediateCount++;
|
|
1090
|
+
}
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
if (immediateCount > 0 || minTimeoutMs === 0) {
|
|
1094
|
+
let nevents = 0;
|
|
1095
|
+
for (let i = 0; i < nsubscriptions; i++) {
|
|
1096
|
+
const subPtr = in_ptr + i * 48;
|
|
1097
|
+
const userdata = view.getBigUint64(subPtr, true);
|
|
1098
|
+
const type = view.getUint8(subPtr + 8);
|
|
1099
|
+
let isReady = false;
|
|
1100
|
+
|
|
1101
|
+
if (type === 0 && minTimeoutMs === 0) isReady = true;
|
|
1102
|
+
else if (type === 1) {
|
|
1103
|
+
const fd = view.getUint32(subPtr + 16, true);
|
|
1104
|
+
const entry = fdTable.get(fd);
|
|
1105
|
+
const isDevTty = Boolean(entry && entry.type === 'dev_tty');
|
|
1106
|
+
if ((fd === 0 || (isInteractive && fd === 2) || isDevTty) && inputQueue.length > 0) isReady = true;
|
|
1107
|
+
} else if (type === 2) isReady = true;
|
|
1108
|
+
|
|
1109
|
+
if (isReady) {
|
|
1110
|
+
const outEventPtr = out_ptr + nevents * 32;
|
|
1111
|
+
view.setBigUint64(outEventPtr, userdata, true);
|
|
1112
|
+
view.setUint16(outEventPtr + 8, ERRNO_SUCCESS, true);
|
|
1113
|
+
view.setUint8(outEventPtr + 10, type);
|
|
1114
|
+
for (let b = 11; b < 32; b++) view.setUint8(outEventPtr + b, 0);
|
|
1115
|
+
if (type === 1) {
|
|
1116
|
+
view.setBigUint64(outEventPtr + 16, BigInt(inputQueue.length), true);
|
|
1117
|
+
}
|
|
1118
|
+
nevents++;
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
view.setUint32(nevents_ptr, nevents, true);
|
|
1122
|
+
return ERRNO_SUCCESS;
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
instance.exports.asyncify_start_unwind(asyncifyDataAddr);
|
|
1126
|
+
asyncifyPromise = new Promise(resolve => {
|
|
1127
|
+
let timer = null;
|
|
1128
|
+
const onKey = () => {
|
|
1129
|
+
if (timer) clearTimeout(timer);
|
|
1130
|
+
pendingReadResolve = null;
|
|
1131
|
+
resolve();
|
|
1132
|
+
};
|
|
1133
|
+
pendingReadResolve = onKey;
|
|
1134
|
+
if (minTimeoutMs !== Infinity) {
|
|
1135
|
+
timer = setTimeout(() => {
|
|
1136
|
+
pendingReadResolve = null;
|
|
1137
|
+
resolve();
|
|
1138
|
+
}, Math.max(1, minTimeoutMs));
|
|
1139
|
+
}
|
|
1140
|
+
});
|
|
1141
|
+
view.setUint32(nevents_ptr, 0, true);
|
|
1142
|
+
return ERRNO_SUCCESS;
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
view.setUint32(nevents_ptr, 0, true);
|
|
1146
|
+
return ERRNO_SUCCESS;
|
|
1147
|
+
},
|
|
813
1148
|
};
|
|
814
1149
|
|
|
815
1150
|
const wasiProxy = new Proxy(wasi, { get(t, k) { return k in t ? t[k] : () => 0; } });
|
|
@@ -885,27 +1220,32 @@ class WasmCommand extends ScriptCommand {
|
|
|
885
1220
|
new Int32Array(memRef.buffer, asyncifyDataAddr, 2).set([dataStart, dataEnd]);
|
|
886
1221
|
}
|
|
887
1222
|
|
|
1223
|
+
if (typeof instance.exports.set_terminal_size === 'function') {
|
|
1224
|
+
promoteToInteractive();
|
|
1225
|
+
}
|
|
1226
|
+
|
|
888
1227
|
if (isInteractive) {
|
|
889
1228
|
this._enterAltScreen();
|
|
890
|
-
|
|
891
|
-
const rows = (this.stdout && this.stdout.rows) || (this.context && typeof this.context.getVar === 'function' && parseInt(this.context.getVar('LINES'), 10)) || 24;
|
|
892
|
-
if (typeof instance.exports.set_terminal_size === 'function') {
|
|
893
|
-
try { instance.exports.set_terminal_size(cols, rows); } catch {}
|
|
894
|
-
}
|
|
1229
|
+
}
|
|
895
1230
|
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
this.stdinStream.on('resize', onResize);
|
|
1231
|
+
const cols = (this.stdout && this.stdout.columns) || (this.context && typeof this.context.getVar === 'function' && parseInt(this.context.getVar('COLUMNS'), 10)) || 80;
|
|
1232
|
+
const rows = (this.stdout && this.stdout.rows) || (this.context && typeof this.context.getVar === 'function' && parseInt(this.context.getVar('LINES'), 10)) || 24;
|
|
1233
|
+
if (typeof instance.exports.set_terminal_size === 'function') {
|
|
1234
|
+
try { instance.exports.set_terminal_size(cols, rows); } catch {}
|
|
1235
|
+
}
|
|
1236
|
+
|
|
1237
|
+
onResize = () => {
|
|
1238
|
+
const c = (this.stdout && this.stdout.columns) || (this.context && typeof this.context.getVar === 'function' && parseInt(this.context.getVar('COLUMNS'), 10)) || 80;
|
|
1239
|
+
const r = (this.stdout && this.stdout.rows) || (this.context && typeof this.context.getVar === 'function' && parseInt(this.context.getVar('LINES'), 10)) || 24;
|
|
1240
|
+
if (typeof instance?.exports?.set_terminal_size === 'function') {
|
|
1241
|
+
try { instance.exports.set_terminal_size(c, r); } catch {}
|
|
908
1242
|
}
|
|
1243
|
+
};
|
|
1244
|
+
if (this.stdout && typeof this.stdout.on === 'function') {
|
|
1245
|
+
this.stdout.on('resize', onResize);
|
|
1246
|
+
}
|
|
1247
|
+
if (this.stdinStream && typeof this.stdinStream.on === 'function') {
|
|
1248
|
+
this.stdinStream.on('resize', onResize);
|
|
909
1249
|
}
|
|
910
1250
|
|
|
911
1251
|
const start = instance.exports._start
|
|
@@ -953,10 +1293,13 @@ class WasmCommand extends ScriptCommand {
|
|
|
953
1293
|
if (isInteractive) {
|
|
954
1294
|
this._exitAltScreen();
|
|
955
1295
|
}
|
|
956
|
-
if (
|
|
1296
|
+
if (onStreamData) {
|
|
957
1297
|
if (this.stdinStream && typeof this.stdinStream.off === 'function') this.stdinStream.off('data', onStreamData);
|
|
958
1298
|
if (this.stdin && typeof this.stdin.off === 'function') this.stdin.off('data', onStreamData);
|
|
959
1299
|
}
|
|
1300
|
+
if (this._onStdinStreamData && this.stdinStream && typeof this.stdinStream.on === 'function') {
|
|
1301
|
+
this.stdinStream.on('data', this._onStdinStreamData);
|
|
1302
|
+
}
|
|
960
1303
|
if (onResize) {
|
|
961
1304
|
if (this.stdout && typeof this.stdout.off === 'function') this.stdout.off('resize', onResize);
|
|
962
1305
|
if (this.stdinStream && typeof this.stdinStream.off === 'function') this.stdinStream.off('resize', onResize);
|