brsh 3.10.2 → 3.10.3

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/index.js CHANGED
@@ -260,9 +260,25 @@ class Shell extends EventEmitter {
260
260
  }
261
261
 
262
262
  onInput(char) {
263
- if(this.runningCommand && this.runningCommand.onInput && this.runningCommand.captureInput) {
264
- this.runningCommand.onInput(char);
265
- return true;
263
+ if (this.runningCommand) {
264
+ const cmd = this.runningCommand;
265
+ if (char === '^C' || char === '\x03') {
266
+ const cmdIsRaw = Boolean(cmd.isRaw || (cmd.stdinStream && cmd.stdinStream.isRaw));
267
+ if (!cmdIsRaw) {
268
+ const wasAltOrInteractive = Boolean(cmd.interactive || cmd._inAltScreen);
269
+ if (typeof cmd.kill === 'function') {
270
+ cmd.kill('SIGINT');
271
+ }
272
+ if (!wasAltOrInteractive) {
273
+ this.stdout.write('^C\r\n');
274
+ }
275
+ return true;
276
+ }
277
+ }
278
+ if (cmd.onInput && cmd.captureInput) {
279
+ cmd.onInput(char);
280
+ return true;
281
+ }
266
282
  }
267
283
  }
268
284
 
@@ -1046,10 +1062,11 @@ class Shell extends EventEmitter {
1046
1062
 
1047
1063
  const cmdIsRaw = Boolean(cmd.isRaw || (cmd.stdinStream && cmd.stdinStream.isRaw));
1048
1064
  if (isCtrlC && !cmdIsRaw) {
1065
+ const wasAltOrInteractive = Boolean(cmd.interactive || cmd._inAltScreen);
1049
1066
  if (typeof cmd.kill === 'function') {
1050
1067
  cmd.kill('SIGINT');
1051
1068
  }
1052
- if (!cmd.interactive && !cmd._inAltScreen) {
1069
+ if (!wasAltOrInteractive) {
1053
1070
  this.stdout.write('^C\r\n');
1054
1071
  }
1055
1072
  return;
package/lib/Command.js CHANGED
@@ -33,11 +33,12 @@ class Command extends EventEmitter {
33
33
  if (!this.captureInput) return;
34
34
  const keys = parseVtKeys(chunk);
35
35
  for (const k of keys) {
36
- if (k === '^C' && !(this.stdinStream && this.stdinStream.isRaw)) {
36
+ const isRaw = Boolean(this.isRaw || (this.stdinStream && this.stdinStream.isRaw));
37
+ if (k === '^C' && !isRaw) {
37
38
  this.kill('SIGINT');
38
39
  return;
39
40
  }
40
- if (!this.interactive && !this.stdinStream.isRaw) {
41
+ if (!this.interactive && !isRaw) {
41
42
  if (k === 'Enter') this.stdout.write('\r\n');
42
43
  else if (k === 'Backspace') this.stdout.write('\b \b');
43
44
  else if (k.length === 1) this.stdout.write(k);
@@ -39,13 +39,11 @@ class ScriptCommand extends LocalCommand {
39
39
  }
40
40
 
41
41
  get isRaw() {
42
- return Boolean(
43
- (this.stdinStream && this.stdinStream.isRaw)
44
- || (this.sandbox
45
- && this.sandbox.runningCommand
46
- && (this.sandbox.runningCommand.isRaw
47
- || (this.sandbox.runningCommand.stdinStream && this.sandbox.runningCommand.stdinStream.isRaw)))
48
- );
42
+ if (this.sandbox && this.sandbox.runningCommand) {
43
+ const child = this.sandbox.runningCommand;
44
+ return Boolean(child.isRaw || (child.stdinStream && child.stdinStream.isRaw));
45
+ }
46
+ return Boolean(this.stdinStream && this.stdinStream.isRaw);
49
47
  }
50
48
 
51
49
  get _inAltScreen() {
@@ -62,12 +60,17 @@ class ScriptCommand extends LocalCommand {
62
60
  }
63
61
 
64
62
  onInput(char) {
63
+ if ((char === '^C' || char === '\x03') && !this.isRaw) {
64
+ this.kill('SIGINT');
65
+ return;
66
+ }
65
67
  if (this.sandbox) {
66
68
  this.sandbox.onInput(char);
67
69
  }
68
70
  }
69
71
 
70
72
  kill(signal = 'SIGINT') {
73
+ this._killed = true;
71
74
  if (this.sandbox && this.sandbox.runningCommand && typeof this.sandbox.runningCommand.kill === 'function') {
72
75
  this.sandbox.runningCommand.kill(signal);
73
76
  }
@@ -120,6 +123,14 @@ class ScriptCommand extends LocalCommand {
120
123
  const onParentStdin = (chunk) => {
121
124
  if (!this.sandbox || !this.sandbox.runningCommand) return;
122
125
  const child = this.sandbox.runningCommand;
126
+ const childIsRaw = Boolean(child.isRaw || (child.stdinStream && child.stdinStream.isRaw));
127
+ const isCtrlC = (typeof chunk === 'string' && chunk === '\x03')
128
+ || (chunk instanceof Uint8Array && chunk.length === 1 && chunk[0] === 3)
129
+ || ((typeof Buffer !== 'undefined' && Buffer.isBuffer(chunk)) && chunk.length === 1 && chunk[0] === 3);
130
+ if (isCtrlC && !childIsRaw) {
131
+ this.kill('SIGINT');
132
+ return;
133
+ }
123
134
  const hasStreamListeners = hasCustomStdinListener(child);
124
135
  if (child.interactive || child._inAltScreen || hasStreamListeners) {
125
136
  if (child.stdinStream) {
@@ -128,7 +139,6 @@ class ScriptCommand extends LocalCommand {
128
139
  return;
129
140
  }
130
141
  if (child.captureInput && typeof child.onInput === 'function') {
131
- const childIsRaw = Boolean(child.isRaw || (child.stdinStream && child.stdinStream.isRaw));
132
142
  for (const k of parseVtKeys(chunk)) {
133
143
  if (k === '^C' && !childIsRaw) {
134
144
  this.kill('SIGINT');
@@ -477,11 +487,15 @@ class ScriptCommand extends LocalCommand {
477
487
 
478
488
  async _executeNodes(nodes) {
479
489
  for (const node of nodes) {
490
+ if (this._killed || (this.sandbox && this.sandbox.destroyed)) break;
480
491
  await this._executeNode(node);
481
492
  }
482
493
  }
483
494
 
484
495
  _executeNode(node) {
496
+ if (this._killed || (this.sandbox && this.sandbox.destroyed)) {
497
+ return Promise.resolve();
498
+ }
485
499
  if (node.type === 'function') {
486
500
  const funcName = node.name;
487
501
  const funcBody = node.bodyLines;
@@ -568,19 +582,22 @@ class ScriptCommand extends LocalCommand {
568
582
 
569
583
  async _executeIf(node) {
570
584
  for (const clause of node.clauses) {
585
+ if (this._killed || (this.sandbox && this.sandbox.destroyed)) return;
571
586
  await this.sandbox.onCommand(clause.condition, false);
587
+ if (this._killed || (this.sandbox && this.sandbox.destroyed)) return;
572
588
  if (this.sandbox.lastCode === 0) {
573
589
  return this._executeNodes(this._parseLines(clause.bodyLines));
574
590
  }
575
591
  }
576
- if (node.elseLines.length > 0) {
592
+ if (node.elseLines.length > 0 && !this._killed && !(this.sandbox && this.sandbox.destroyed)) {
577
593
  return this._executeNodes(this._parseLines(node.elseLines));
578
594
  }
579
595
  }
580
596
 
581
597
  async _executeWhile(node) {
582
- while (true) {
598
+ while (!this._killed && !(this.sandbox && this.sandbox.destroyed)) {
583
599
  await this.sandbox.onCommand(node.condition, false);
600
+ if (this._killed || (this.sandbox && this.sandbox.destroyed)) break;
584
601
  if (this.sandbox.lastCode !== 0) break;
585
602
  try {
586
603
  await this._executeNodes(this._parseLines(node.bodyLines));
@@ -611,6 +628,7 @@ class ScriptCommand extends LocalCommand {
611
628
  items = expanded.split(/\s+/).filter(Boolean);
612
629
  }
613
630
  for (const value of items) {
631
+ if (this._killed || (this.sandbox && this.sandbox.destroyed)) break;
614
632
  this.sandbox.context.setVar(node.variable, value);
615
633
  try {
616
634
  await this._executeNodes(this._parseLines(node.bodyLines));
@@ -35,6 +35,9 @@ const FDFLAGS_NONBLOCK = 1 << 2;
35
35
  const FDFLAGS_RSYNC = 1 << 3;
36
36
  const FDFLAGS_SYNC = 1 << 4;
37
37
 
38
+ // POSIX termios c_lflag ISIG bit (0000001)
39
+ const TERMIOS_ISIG = 1;
40
+
38
41
  class WasmCommand extends ScriptCommand {
39
42
  // this.script = filepath (set by ScriptCommand parent constructor)
40
43
  constructor(script, args, context) {
@@ -74,6 +77,44 @@ class WasmCommand extends ScriptCommand {
74
77
  return this.interactive;
75
78
  }
76
79
 
80
+ get isRaw() {
81
+ if (typeof this._getTermiosLflag === 'function') {
82
+ try {
83
+ const raw = ((this._getTermiosLflag() >>> 0) & TERMIOS_ISIG) === 0;
84
+ if (this._inAltScreen && this.stdinStream && typeof this.stdinStream.setRawMode === 'function' && this.stdinStream.isRaw !== raw) {
85
+ this.stdinStream.setRawMode(raw);
86
+ }
87
+ return raw;
88
+ } catch (_) {}
89
+ }
90
+ const meta = this._ensurePkgMeta();
91
+ if (meta && (meta.raw !== undefined || meta.wasm?.raw !== undefined)) {
92
+ return Boolean(meta.raw ?? meta.wasm?.raw);
93
+ }
94
+ return Boolean(this.stdinStream && this.stdinStream.isRaw);
95
+ }
96
+
97
+ _syncRawMode() {
98
+ if (!this.stdinStream || typeof this.stdinStream.setRawMode !== 'function') return;
99
+ if (!this._inAltScreen && !this._isInteractive) {
100
+ if (this.stdinStream.isRaw) this.stdinStream.setRawMode(false);
101
+ return;
102
+ }
103
+ if (typeof this._getTermiosLflag === 'function') {
104
+ try {
105
+ const raw = ((this._getTermiosLflag() >>> 0) & TERMIOS_ISIG) === 0;
106
+ this.stdinStream.setRawMode(raw);
107
+ return;
108
+ } catch (_) {}
109
+ }
110
+ const meta = this._ensurePkgMeta();
111
+ if (meta && (meta.raw !== undefined || meta.wasm?.raw !== undefined)) {
112
+ this.stdinStream.setRawMode(Boolean(meta.raw ?? meta.wasm?.raw));
113
+ return;
114
+ }
115
+ this.stdinStream.setRawMode(false);
116
+ }
117
+
77
118
  kill(signal = 'SIGINT') {
78
119
  this._killed = true;
79
120
  this.exitCode = 130;
@@ -93,9 +134,7 @@ class WasmCommand extends ScriptCommand {
93
134
  _enterAltScreen() {
94
135
  if (this._inAltScreen) return;
95
136
  this._inAltScreen = true;
96
- if (this.stdinStream && typeof this.stdinStream.setRawMode === 'function') {
97
- this.stdinStream.setRawMode(true);
98
- }
137
+ this._syncRawMode();
99
138
  if (this.stdout && typeof this.stdout.write === 'function') {
100
139
  this.stdout.write('\x1b[?1049h\x1b[2J\x1b[H');
101
140
  }
@@ -343,9 +382,7 @@ class WasmCommand extends ScriptCommand {
343
382
  outBuf[2] = '';
344
383
  }
345
384
 
346
- if (this.stdinStream && typeof this.stdinStream.setRawMode === 'function') {
347
- this.stdinStream.setRawMode(true);
348
- }
385
+ this._syncRawMode();
349
386
  };
350
387
 
351
388
  const writeFd = (fd, text) => {
@@ -385,7 +422,12 @@ class WasmCommand extends ScriptCommand {
385
422
 
386
423
  const enqueueBytes = (b) => {
387
424
  if (!b || b.length === 0) return;
425
+ const raw = this.isRaw;
388
426
  for (let i = 0; i < b.length; i++) {
427
+ if (b[i] === 3 && !raw) {
428
+ this.kill('SIGINT');
429
+ return;
430
+ }
389
431
  inputQueue.push(b[i]);
390
432
  }
391
433
  if (pendingReadResolve) {
@@ -614,6 +656,7 @@ class WasmCommand extends ScriptCommand {
614
656
  if (isDevTty || fd === 2) {
615
657
  promoteToInteractive();
616
658
  }
659
+ this._syncRawMode();
617
660
  const flags = fdFlagsMap.get(fd) || 0;
618
661
  const isNonBlocking = Boolean(flags & FDFLAGS_NONBLOCK);
619
662
 
@@ -1168,7 +1211,17 @@ class WasmCommand extends ScriptCommand {
1168
1211
  }
1169
1212
 
1170
1213
  instance.exports.asyncify_start_unwind(asyncifyDataAddr);
1171
- asyncifyPromise = new Promise(resolve => setTimeout(resolve, Math.max(1, sleepMs)));
1214
+ asyncifyPromise = new Promise((resolve, reject) => {
1215
+ const timer = setTimeout(() => {
1216
+ this._asyncifyReject = null;
1217
+ resolve();
1218
+ }, Math.max(1, sleepMs));
1219
+ this._asyncifyReject = (err) => {
1220
+ clearTimeout(timer);
1221
+ this._asyncifyReject = null;
1222
+ reject(err);
1223
+ };
1224
+ });
1172
1225
  return ERRNO_SUCCESS;
1173
1226
  }
1174
1227
  return ERRNO_SUCCESS;
@@ -1176,6 +1229,7 @@ class WasmCommand extends ScriptCommand {
1176
1229
  poll_oneoff: (in_ptr, out_ptr, nsubscriptions, nevents_ptr) => {
1177
1230
  if (!memRef) return ERRNO_BADF;
1178
1231
  const view = new DataView(memRef.buffer);
1232
+ this._syncRawMode();
1179
1233
 
1180
1234
  if (hasAsyncify) {
1181
1235
  const state = (typeof instance?.exports?.asyncify_get_state === 'function')
@@ -1298,7 +1352,14 @@ class WasmCommand extends ScriptCommand {
1298
1352
  instance.exports.asyncify_start_unwind(asyncifyDataAddr);
1299
1353
  asyncifyPromise = new Promise((resolve, reject) => {
1300
1354
  let timer = null;
1301
- this._asyncifyReject = reject;
1355
+ this._asyncifyReject = (err) => {
1356
+ if (timer) clearTimeout(timer);
1357
+ pendingReadResolve = null;
1358
+ pendingResizeInterrupt = null;
1359
+ this._pendingReadResolve = null;
1360
+ this._asyncifyReject = null;
1361
+ reject(err);
1362
+ };
1302
1363
  const onKey = () => {
1303
1364
  if (timer) clearTimeout(timer);
1304
1365
  pollTimedOut = false;
@@ -1404,6 +1465,10 @@ class WasmCommand extends ScriptCommand {
1404
1465
  // Prefer the module's own exported memory
1405
1466
  if (instance.exports.memory) memRef = instance.exports.memory;
1406
1467
 
1468
+ this._getTermiosLflag = typeof instance.exports.get_termios_lflag === 'function'
1469
+ ? instance.exports.get_termios_lflag
1470
+ : null;
1471
+
1407
1472
  hasAsyncify = Boolean(instance.exports.asyncify_start_unwind && instance.exports.asyncify_stop_unwind);
1408
1473
  if (hasAsyncify && memRef) {
1409
1474
  try {
@@ -1423,6 +1488,7 @@ class WasmCommand extends ScriptCommand {
1423
1488
 
1424
1489
  if (isInteractive) {
1425
1490
  this._enterAltScreen();
1491
+ this._syncRawMode();
1426
1492
  }
1427
1493
 
1428
1494
  const cols = (this.stdout && this.stdout.columns)
@@ -1563,6 +1629,7 @@ class WasmCommand extends ScriptCommand {
1563
1629
  this.stdinStream.off('resize', onStdinResize);
1564
1630
  }
1565
1631
  this._externalKeyHandler = null;
1632
+ this._getTermiosLflag = null;
1566
1633
 
1567
1634
  // Flush any open dirty WASI file descriptors back to the brsh filesystem
1568
1635
  for (const entry of fdTable.values()) {
@@ -1907,10 +1974,10 @@ function createGenericWasmCommand(pkgMeta, config = {}, binNameOverride = null)
1907
1974
  }
1908
1975
 
1909
1976
  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
- );
1977
+ if (this._subCommand) {
1978
+ return Boolean(this._subCommand.isRaw || (this._subCommand.stdinStream && this._subCommand.stdinStream.isRaw));
1979
+ }
1980
+ return Boolean(this.stdinStream && this.stdinStream.isRaw);
1914
1981
  }
1915
1982
 
1916
1983
  get _inAltScreen() {
package/lib/wbt/index.js CHANGED
@@ -130,10 +130,28 @@ class Wbt extends Command {
130
130
  }
131
131
 
132
132
  get isRaw() {
133
- return Boolean(
134
- (this.stdinStream && this.stdinStream.isRaw)
135
- || (this._activeSubCommand && (this._activeSubCommand.isRaw || (this._activeSubCommand.stdinStream && this._activeSubCommand.stdinStream.isRaw)))
136
- );
133
+ if (this._activeSubCommand) {
134
+ return Boolean(
135
+ this._activeSubCommand.isRaw
136
+ || (this._activeSubCommand.stdinStream && this._activeSubCommand.stdinStream.isRaw)
137
+ );
138
+ }
139
+ return Boolean(this.stdinStream && this.stdinStream.isRaw);
140
+ }
141
+
142
+ get _inAltScreen() {
143
+ return Boolean(this.__inAltScreen || (this._activeSubCommand && this._activeSubCommand._inAltScreen));
144
+ }
145
+
146
+ set _inAltScreen(val) {
147
+ this.__inAltScreen = Boolean(val);
148
+ }
149
+
150
+ kill(signal = 'SIGINT') {
151
+ if (this._activeSubCommand && typeof this._activeSubCommand.kill === 'function') {
152
+ this._activeSubCommand.kill(signal);
153
+ }
154
+ super.kill(signal);
137
155
  }
138
156
 
139
157
  onInput(key) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "brsh",
3
- "version": "3.10.2",
3
+ "version": "3.10.3",
4
4
  "description": "A unix like shell written in javascript",
5
5
  "main": "./dist/shell.min.js",
6
6
  "module": "./dist/shell.es.js",
@@ -281,5 +281,199 @@ describe('WASM requirements auto-discovery', () => {
281
281
  await p;
282
282
  expect(shell.lastCode).toBe(0);
283
283
  });
284
+
285
+ function buildTermiosTestWasm(initialLflag) {
286
+ const uleb = (n) => {
287
+ const out = [];
288
+ do {
289
+ let b = n & 0x7f;
290
+ n >>>= 7;
291
+ if (n) b |= 0x80;
292
+ out.push(b);
293
+ } while (n);
294
+ return out;
295
+ };
296
+ const sleb = (n) => {
297
+ const out = [];
298
+ let more = true;
299
+ while (more) {
300
+ let b = n & 0x7f;
301
+ n >>= 7;
302
+ if ((n === 0 && (b & 0x40) === 0) || (n === -1 && (b & 0x40) !== 0)) more = false;
303
+ else b |= 0x80;
304
+ out.push(b);
305
+ }
306
+ return out;
307
+ };
308
+ const str = (s) => {
309
+ const b = Buffer.from(s, 'utf8');
310
+ return [...uleb(b.length), ...b];
311
+ };
312
+ const sec = (id, body) => [id, ...uleb(body.length), ...body];
313
+ const vec = (items) => [...uleb(items.length), ...items.flat()];
314
+ const fnBody = (locals, instrs) => {
315
+ const b = [...vec(locals), ...instrs, 0x0b];
316
+ return [...uleb(b.length), ...b];
317
+ };
318
+
319
+ const types = sec(1, vec([
320
+ [0x60, 0, 0],
321
+ [0x60, 0, 1, 0x7f],
322
+ [0x60, 1, 0x7f, 0],
323
+ [0x60, 2, 0x7f, 0x7f, 0],
324
+ [0x60, 4, 0x7f, 0x7f, 0x7f, 0x7f, 1, 0x7f],
325
+ ]));
326
+ const imports = sec(2, vec([
327
+ [...str('wasi_snapshot_preview1'), ...str('clock_nanosleep'), 0x00, 4],
328
+ [...str('wasi_snapshot_preview1'), ...str('fd_read'), 0x00, 4],
329
+ [...str('wasi_snapshot_preview1'), ...str('fd_write'), 0x00, 4],
330
+ ]));
331
+ const funcs = sec(3, vec([3, 1, 2, 0, 2, 0, 1, 0]));
332
+ const mem = sec(5, vec([[0x00, 1]]));
333
+ const globals = sec(6, vec([
334
+ [0x7f, 1, 0x41, ...sleb(0), 0x0b],
335
+ [0x7f, 1, 0x41, ...sleb(initialLflag), 0x0b],
336
+ ]));
337
+ const exports = sec(7, vec([
338
+ [...str('memory'), 0x02, 0],
339
+ [...str('set_terminal_size'), 0x00, 3],
340
+ [...str('get_termios_lflag'), 0x00, 4],
341
+ [...str('asyncify_start_unwind'), 0x00, 5],
342
+ [...str('asyncify_stop_unwind'), 0x00, 6],
343
+ [...str('asyncify_start_rewind'), 0x00, 7],
344
+ [...str('asyncify_stop_rewind'), 0x00, 8],
345
+ [...str('asyncify_get_state'), 0x00, 9],
346
+ [...str('_start'), 0x00, 10],
347
+ ]));
348
+
349
+ const startInstrs = initialLflag === 1
350
+ ? [
351
+ 0x41, ...sleb(256), 0x42, ...sleb(5000000000), 0x37, 3, 0,
352
+ 0x41, 0, 0x41, 0, 0x41, ...sleb(256), 0x41, ...sleb(264), 0x10, 0, 0x1a
353
+ ]
354
+ : [
355
+ 0x03, 0x40,
356
+ 0x41, ...sleb(128), 0x41, ...sleb(160), 0x36, 2, 0,
357
+ 0x41, ...sleb(132), 0x41, 1, 0x36, 2, 0,
358
+ 0x41, 0, 0x41, ...sleb(128), 0x41, 1, 0x41, ...sleb(136), 0x10, 1, 0x1a,
359
+ 0x23, 0, 0x41, 1, 0x46, 0x04, 0x40, 0x0f, 0x0b,
360
+ 0x41, ...sleb(160), 0x2d, 0, 0, 0x21, 0,
361
+ 0x20, 0, 0x41, 0x18, 0x46, 0x04, 0x40, 0x0f, 0x0b,
362
+ 0x20, 0, 0x41, ...sleb(0x73), 0x46, 0x04, 0x40, 0x41, 1, 0x24, 1, 0x0b,
363
+ 0x20, 0, 0x41, 3, 0x46, 0x04, 0x40,
364
+ 0x41, ...sleb(300), 0x41, ...sleb(0x585445), 0x36, 2, 0,
365
+ 0x41, ...sleb(140), 0x41, ...sleb(300), 0x36, 2, 0,
366
+ 0x41, ...sleb(144), 0x41, 3, 0x36, 2, 0,
367
+ 0x41, 1, 0x41, ...sleb(140), 0x41, 1, 0x41, ...sleb(148), 0x10, 2, 0x1a,
368
+ 0x0b,
369
+ 0x0c, 0,
370
+ 0x0b
371
+ ];
372
+
373
+ const code = sec(10, vec([
374
+ fnBody([], []),
375
+ fnBody([], [0x23, 1]),
376
+ fnBody([], [0x41, 1, 0x24, 0]),
377
+ fnBody([], [0x41, 0, 0x24, 0]),
378
+ fnBody([], [0x41, 2, 0x24, 0]),
379
+ fnBody([], [0x41, 0, 0x24, 0]),
380
+ fnBody([], [0x23, 0]),
381
+ fnBody([[1, 0x7f]], startInstrs),
382
+ ]));
383
+
384
+ return new Uint8Array([0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, ...types, ...imports, ...funcs, ...mem, ...globals, ...exports, ...code]);
385
+ }
386
+
387
+ it('terminates cbreak WASM command (ISIG enabled) on Ctrl+C immediately, restores screen, and aborts parent launcher script', async () => {
388
+ const cbreakWasm = buildTermiosTestWasm(1);
389
+ const shell = await createShell({
390
+ filesystem: {
391
+ bin: {
392
+ 'cbreak-game': cbreakWasm,
393
+ 'launch-game': '#!/bin/sh\ncbreak-game\necho "AFTER_GAME" > /after.txt\n'
394
+ }
395
+ },
396
+ permissions: {
397
+ '/bin/cbreak-game': 0o755,
398
+ '/bin/launch-game': 0o755
399
+ }
400
+ });
401
+
402
+ // 1. Direct WASM execution: Ctrl+C via stream write (\x03) cancels 5s clock_nanosleep immediately
403
+ const out1 = [];
404
+ const onOut1 = c => out1.push(String(c));
405
+ shell.stdout.on('data', onOut1);
406
+
407
+ const t0 = Date.now();
408
+ const p1 = shell.onCommand('cbreak-game');
409
+ await new Promise(r => setTimeout(r, 30));
410
+
411
+ shell.write('\x03');
412
+ await p1;
413
+ const elapsed = Date.now() - t0;
414
+ shell.stdout.off('data', onOut1);
415
+
416
+ expect(shell.lastCode).toBe(130);
417
+ expect(elapsed).toBeLessThan(1000);
418
+ expect(out1.join('')).toContain('\x1b[?1049h');
419
+ expect(out1.join('')).toContain('\x1b[?1049l');
420
+ expect(out1.join('')).not.toContain('^C\r\n');
421
+
422
+ // 2. Via shell script wrapper (like freedoom): Ctrl+C kills child + script without running subsequent lines
423
+ const p2 = shell.onCommand('launch-game');
424
+ await new Promise(r => setTimeout(r, 40));
425
+
426
+ shell.write('\x03');
427
+ await p2;
428
+
429
+ expect(shell.lastCode).toBe(130);
430
+ expect(shell.context.fs.exists('/after.txt')).toBe(false);
431
+
432
+ // 3. Ctrl+C via shell.onInput('^C') also delivers SIGINT cleanly
433
+ const p3 = shell.onCommand('cbreak-game');
434
+ await new Promise(r => setTimeout(r, 30));
435
+ shell.onInput('^C');
436
+ await p3;
437
+ expect(shell.lastCode).toBe(130);
438
+ });
439
+
440
+ it('passes Ctrl+C (\\x03) as raw input when WASM clears ISIG (~ISIG) and honors dynamic ISIG re-enablement', async () => {
441
+ const rawWasm = buildTermiosTestWasm(0);
442
+ const shell = await createShell({
443
+ filesystem: {
444
+ bin: {
445
+ 'raw-editor': rawWasm
446
+ }
447
+ },
448
+ permissions: {
449
+ '/bin/raw-editor': 0o755
450
+ }
451
+ });
452
+
453
+ const chunks = [];
454
+ const onData = c => chunks.push(String(c));
455
+ shell.stdout.on('data', onData);
456
+
457
+ const p = shell.onCommand('raw-editor');
458
+ await new Promise(r => setTimeout(r, 30));
459
+
460
+ // While lflag == 0 (~ISIG), Ctrl+C is delivered as byte 0x03 ("ETX" written to stdout)
461
+ shell.write('\x03');
462
+ await new Promise(r => setTimeout(r, 30));
463
+ expect(chunks.join('')).toContain('ETX');
464
+ expect(shell.runningCommand).toBeDefined();
465
+
466
+ // Sending 's' toggles lflag to 1 (re-enabling ISIG dynamically at runtime)
467
+ shell.write('s');
468
+ await new Promise(r => setTimeout(r, 20));
469
+
470
+ // Subsequent Ctrl+C now terminates with SIGINT (130)
471
+ shell.write('\x03');
472
+ await p;
473
+ shell.stdout.off('data', onData);
474
+
475
+ expect(shell.lastCode).toBe(130);
476
+ });
284
477
  });
285
478
 
479
+
package/test/doom.test.js CHANGED
@@ -4,18 +4,24 @@ import path from 'path';
4
4
  import { createShell } from './helpers.js';
5
5
 
6
6
  const DOOM_WASM_CANDIDATES = [
7
+ path.resolve(import.meta.dirname, '../../doom-wasm/dist/doom.wasm'),
7
8
  '/home/antigravity/eliotstocker/doom-wasm/dist/doom.wasm',
8
9
  '/home/antigravity/doom-ascii/dist/doom.wasm',
9
10
  '/tmp/doom-asyncify.wasm'
10
11
  ];
12
+ const DOOM_WAD_CANDIDATES = [
13
+ '/tmp/doom1.wad',
14
+ '/tmp/freedoom1.wad'
15
+ ];
11
16
  const DOOM_WASM_PATH = DOOM_WASM_CANDIDATES.find(p => fs.existsSync(p)) || DOOM_WASM_CANDIDATES[0];
12
- const DOOM_WAD_PATH = '/tmp/doom1.wad';
17
+ const DOOM_WAD_PATH = DOOM_WAD_CANDIDATES.find(p => fs.existsSync(p)) || DOOM_WAD_CANDIDATES[0];
13
18
  const hasDoom = fs.existsSync(DOOM_WASM_PATH) && fs.existsSync(DOOM_WAD_PATH);
19
+ const hasDoom1Demo = fs.existsSync(DOOM_WASM_PATH) && fs.existsSync('/tmp/doom1.wad');
14
20
 
15
21
  describe('Doom in brsh', () => {
16
- it.skipIf(!hasDoom)('executes doom timedemo benchmark cleanly to completion', async () => {
22
+ it.skipIf(!hasDoom1Demo)('executes doom timedemo benchmark cleanly to completion', async () => {
17
23
  const doomWasm = fs.readFileSync(DOOM_WASM_PATH);
18
- const doomWad = fs.readFileSync(DOOM_WAD_PATH);
24
+ const doomWad = fs.readFileSync('/tmp/doom1.wad');
19
25
  const shell = await createShell({
20
26
  filesystem: {
21
27
  bin: { 'doom': doomWasm },
@@ -36,4 +42,57 @@ describe('Doom in brsh', () => {
36
42
  expect(allOut).toContain('DOOM ASCII');
37
43
  expect(allOut).toMatch(/timed \d+ gametics/);
38
44
  }, 30000);
45
+
46
+ it.skipIf(!hasDoom)('exits interactive doom and freedoom cleanly on Ctrl+C (SIGINT, exit code 130)', async () => {
47
+ const doomWasm = new Uint8Array(fs.readFileSync(DOOM_WASM_PATH));
48
+ const doomWad = new Uint8Array(fs.readFileSync(DOOM_WAD_PATH));
49
+ const mod = new WebAssembly.Module(doomWasm);
50
+ expect(WebAssembly.Module.exports(mod).map(e => e.name)).toContain('get_termios_lflag');
51
+
52
+ const shell = await createShell({
53
+ filesystem: {
54
+ usr: {
55
+ lib: {
56
+ doom: {
57
+ 'doom.wasm': doomWasm,
58
+ 'freedoom': '#!/bin/sh\ndoom -iwad /usr/share/games/doom/game.wad -nosound\n',
59
+ 'package.json': JSON.stringify({ name: 'doom', bin: 'doom', interactive: true })
60
+ }
61
+ },
62
+ share: {
63
+ games: {
64
+ doom: {
65
+ 'game.wad': doomWad
66
+ }
67
+ }
68
+ }
69
+ }
70
+ }
71
+ });
72
+ shell.context.fs.createSymlink('/usr/lib/doom/doom.wasm', '/bin/doom');
73
+ shell.context.fs.setMode('/usr/lib/doom/doom.wasm', 0o755);
74
+ shell.context.fs.createSymlink('/usr/lib/doom/freedoom', '/bin/freedoom');
75
+ shell.context.fs.setMode('/usr/lib/doom/freedoom', 0o755);
76
+
77
+ const chunks = [];
78
+ const onData = chunk => chunks.push(String(chunk));
79
+ shell.stdout.on('data', onData);
80
+
81
+ const runPromise = shell.onCommand('freedoom');
82
+ await new Promise(r => setTimeout(r, 350));
83
+
84
+ expect(shell.runningCommand).toBeDefined();
85
+ expect(shell.runningCommand.isRaw).toBe(false);
86
+ expect(shell.runningCommand._inAltScreen).toBe(true);
87
+
88
+ shell.write('\x03');
89
+ await runPromise;
90
+ shell.stdout.off('data', onData);
91
+
92
+ const out = chunks.join('');
93
+ expect(shell.lastCode).toBe(130);
94
+ expect(out).toContain('\x1b[?1049h');
95
+ expect(out).toContain('\x1b[?1049l\x1b[?25h');
96
+ expect(out).not.toContain('^C\r\n');
97
+ }, 30000);
39
98
  });
Binary file