brsh 3.6.2 → 3.8.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/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ # [3.8.0](https://github.com/eliotstocker/Brsh-JS/compare/v3.7.0...v3.8.0) (2026-11-10)
2
+
3
+
4
+ ### Features
5
+
6
+ * **wasm:** add non-blocking read, interactive stderr tty, and poll_oneoff timer support ([#31](https://github.com/eliotstocker/Brsh-JS/issues/31)) ([6dadca6](https://github.com/eliotstocker/Brsh-JS/commit/6dadca6f7b4b50150921b7bf1050ede673f09ec4))
7
+
8
+ # [3.7.0](https://github.com/eliotstocker/Brsh-JS/compare/v3.6.2...v3.7.0) (2026-11-09)
9
+
10
+
11
+ ### Features
12
+
13
+ * **terminal:** implement screen buffer reflow on terminal resize ([7e3c648](https://github.com/eliotstocker/Brsh-JS/commit/7e3c6480fa949cecb8857d4e77bbc5f317543c44))
14
+
1
15
  ## [3.6.2](https://github.com/eliotstocker/Brsh-JS/compare/v3.6.1...v3.6.2) (2026-11-09)
2
16
 
3
17
 
package/Readme.md CHANGED
@@ -359,6 +359,134 @@ mytool
359
359
  wbt remove sqlite3
360
360
  ```
361
361
 
362
+ ## Compiling C / C++ Applications for `brsh`
363
+
364
+ `brsh` includes built-in runtime support for WebAssembly binaries targeting WASI Preview 1 (`wasm32-wasip1` / `wasm32-wasi`). Any standard C or C++ codebase can be compiled with `wasi-sdk` or Clang and executed directly inside the shell with full virtual filesystem access, environment variables, standard streams, and terminal features.
365
+
366
+ ### Prerequisites & Toolchains
367
+
368
+ * **[wasi-sdk](https://github.com/WebAssembly/wasi-sdk)** (recommended): Complete Clang/LLVM toolchain with `wasi-libc`.
369
+ * **[Binaryen](https://github.com/WebAssembly/binaryen)** (`wasm-opt`): Required for instrumenting binaries with Asyncify for cooperative multitasking.
370
+ * **[wasi-termios-brsh](https://github.com/eliotstocker/wasi-termios-brsh)**: Drop-in POSIX `<termios.h>` and `<sys/ioctl.h>` compatibility library for interactive terminal applications and games.
371
+ * **[nano-wasm](https://github.com/eliotstocker/nano-wasm)**: Real-world reference repository demonstrating a complete C build setup (wasi-sdk, static ncurses 6.5, compiled-in terminfo fallbacks, and build scripts).
372
+
373
+ ---
374
+
375
+ ### 1. Compiling Standard CLI Utilities
376
+
377
+ For standard command-line tools that run to completion, read arguments, process files, and produce output:
378
+
379
+ ```c
380
+ // hello.c
381
+ #include <stdio.h>
382
+
383
+ int main(int argc, char **argv) {
384
+ const char *name = argc > 1 ? argv[1] : "world";
385
+ printf("Hello, %s from WASI in brsh!\n", name);
386
+ return 0;
387
+ }
388
+ ```
389
+
390
+ Compile with `wasi-sdk`:
391
+
392
+ ```bash
393
+ $WASI_SDK_PATH/bin/clang -O3 -o hello.wasm hello.c
394
+ ```
395
+
396
+ Place `hello.wasm` into `/bin` in your `brsh` virtual filesystem or register it as a command:
397
+ ```bash
398
+ hello.wasm Alice
399
+ # Hello, Alice from WASI in brsh!
400
+ ```
401
+
402
+ ---
403
+
404
+ ### 2. Interactive Terminal Apps & Games (`termios` & Non-blocking I/O)
405
+
406
+ Many C terminal programs (such as text editors, roguelikes, games like Doom, or `ncurses` tools) rely on POSIX terminal controls:
407
+ * Entering raw / unbuffered mode (`tcgetattr`, `tcsetattr`, disabling `ICANON` and `ECHO`)
408
+ * Polling keyboard input without blocking (`c_cc[VMIN] = 0`, `c_cc[VTIME] = 0` or `O_NONBLOCK`)
409
+ * Querying terminal dimensions (`ioctl(STDIN_FILENO, TIOCGWINSZ, &ws)`)
410
+
411
+ Because `wasi-libc` omits `<termios.h>`, use [**`wasi-termios-brsh`**](https://github.com/eliotstocker/wasi-termios-brsh) as a drop-in header and implementation:
412
+
413
+ 1. Clone or add `wasi-termios-brsh` to your project:
414
+ ```bash
415
+ git clone https://github.com/eliotstocker/wasi-termios-brsh.git
416
+ ```
417
+
418
+ 2. Include the headers and source file when compiling:
419
+ ```bash
420
+ $WASI_SDK_PATH/bin/clang -O3 -std=c99 \
421
+ -I/path/to/wasi-termios-brsh/include \
422
+ app.c /path/to/wasi-termios-brsh/src/termios.c \
423
+ -o app.wasm
424
+ ```
425
+
426
+ `wasi-termios-brsh` maps POSIX terminal operations directly to `brsh`'s WASI runtime:
427
+ * Setting `VMIN = 0` and `VTIME = 0` switches the descriptor flags to `FDFLAGS_NONBLOCK` via `fcntl()` / `fd_fdstat_set_flags`. In non-blocking mode, empty reads immediately return `EAGAIN` / `EWOULDBLOCK` instead of suspending execution.
428
+ * Calling `ioctl(..., TIOCGWINSZ, &ws)` reads `ws_col` and `ws_row` from the `$COLUMNS` and `$LINES` environment variables automatically exported by `brsh`.
429
+ * Interactive keyboard reads can target `stdin` (`fd 0`) or `stderr` (`fd 2`).
430
+
431
+ ---
432
+
433
+ ### 3. Cooperative Multitasking with Asyncify
434
+
435
+ In a browser, WebAssembly execution runs on the main JavaScript thread. If a game loop calls `nanosleep()` / `usleep()` or waits on a blocking `read()`, it would block the browser event loop, preventing DOM rendering and keyboard input dispatch.
436
+
437
+ `brsh` solves this using Binaryen's **Asyncify** in `WasmCommand`. To enable non-blocking timers and cooperative yielding, post-process your `.wasm` binary with `wasm-opt`:
438
+
439
+ ```bash
440
+ wasm-opt -O3 --asyncify \
441
+ --pass-arg=asyncify-imports@wasi_snapshot_preview1.poll_oneoff,wasi_snapshot_preview1.clock_nanosleep,wasi_snapshot_preview1.fd_read \
442
+ app.wasm -o app-asyncify.wasm
443
+ ```
444
+
445
+ When the WASM binary calls `poll_oneoff`, `clock_nanosleep`, or a blocking `fd_read`, `brsh` saves the execution state, yields control back to the browser's event loop, and resumes execution seamlessly when the timer fires or input arrives.
446
+
447
+ ---
448
+
449
+ ### 4. Running WebAssembly Binaries in `brsh`
450
+
451
+ There are three ways to execute your compiled `.wasm` binary:
452
+
453
+ #### A. Place into `/bin` in the Virtual Filesystem
454
+ Any `.wasm` binary placed in `/bin` or `/usr/bin` (via `exportFileSystem` or runtime `fs.setFileByPath`) is automatically executed as a command:
455
+ ```bash
456
+ mytool.wasm arg1 arg2
457
+ ```
458
+
459
+ #### B. Register via `Shell.createWasmCommand`
460
+ Create an explicit command wrapper with custom metadata and interactive TTY flags:
461
+ ```js
462
+ import Shell from 'brsh';
463
+
464
+ const MyGame = Shell.createWasmCommand({
465
+ name: 'mygame',
466
+ wasmUrl: 'https://example.com/mygame.wasm',
467
+ interactive: true // Enters alternate screen buffer (\x1b[?1049h) & raw keyboard mode
468
+ });
469
+
470
+ shell.context.setCommand(MyGame);
471
+ ```
472
+
473
+ #### C. Install via `wbt` (Web Binary Tool)
474
+ If `wbt` is enabled in the shell:
475
+ ```bash
476
+ wbt install mygame https://example.com/mygame.wasm
477
+ mygame
478
+ ```
479
+
480
+ ---
481
+
482
+ ### Real-World Example: `nano-wasm`
483
+
484
+ For a complete, production reference implementation of an interactive C terminal application compiled to WebAssembly for `brsh`, check out [**eliotstocker/nano-wasm**](https://github.com/eliotstocker/nano-wasm). It demonstrates:
485
+ * Compiling a complex C codebase (`GNU nano 8.0`) targeting `wasm32-wasip1` with `wasi-sdk`.
486
+ * Statically linking wide-character `ncurses 6.5` with built-in terminfo fallbacks (`xterm-256color`, `vt100`, etc.) without requiring external terminfo files.
487
+ * Handling dynamic terminal window resizing (`TIOCGWINSZ`) and raw terminal input.
488
+ * Automated build scripts downloading toolchains and generating stripped standalone `.wasm` bundles.
489
+
362
490
  ## Binary Filesystem Container (`.brfs`) & Client-Side Persistence
363
491
 
364
492
  While `brsh` continues to support standard JS object filesystems (and compressed `.js` + `.brfs` loader bundles), it also supports an **Indexed Binary Container (`.brfs`)** format (`BRFS` magic header + JSON metadata/permissions index + contiguous raw binary payload with zero base64 overhead) as well as **IndexedDB client-side persistence**.