You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
xterm-pty is an addon for xterm.js to work as a PTY layer. By connecting its "slave" interface of the PTY with the TTY interface of the Emscripten runtime, some real-world CUI programs like VIM works on recent browsers such as Google Chrome and Firefox.
To create this prototype, I had to hook some system calls and events in the Emscripten runtime: ioctl, select, and TTY read/write. Currently, the prototype uses very dirty hack to achieve these hooks.
As the hack highly depends on the internal of Emscripten runtime, it is a bit tough to maintain outside of Emscripten project. Is there any chance that Emscripten could import this kind of functionality?
Alternatively, I would appreciate it if Emscripten could provide some necessary Module APIs for the hooks, but the API design might be a bit tricky.
ioctl syscall accepts a pointer to the heap, and read/write the heap depending on its request type. It's not enough to have a hook that just notifies when ioctl is called.
select syscall needs to support timeout for real-world CUI programs. We may need to create an API for a polling mechanism.
I would hope you can do a lot of what you needs using a library JS file. e.g. library_xterm.js .. then anyone who wants to could grab you code and use it via --js-library library_xterm.js
I took a stab at a proof-of-concept (#17371) in my free time. I think there's still much to do. Unfortunately, I don't have tons of experience writing good JavaScript, and practically zero experience with implementing tests for JavaScript.
Some goals I have in mind are:
Must have: Developers should be able to provide alternative implementations of basic TTY operations before stdin, stdout, and stderr are initialized, so that those streams use the supplied operations instead.
Should have: Existing code should be unaffected by the changes to stream setup.
Could have: Registering a TTY could, by default, create a device node for it under /dev, so developers don't have to bother
Should have: Developers should be able to register their own device implementations and create /dev/tty{,1} with them, in case they want to do something very unusual.
Developers should be able to supply their own devices for /dev/stdin, /dev/stdout, and /dev/stderr
As a first step, I sent a PR #19499 to expose the syscall hooks that we would like Emscripten runtime to provide at a minimum.
We are still a long way from getting it to work with --js-library library_xterm.js, but in the meantime, it will save xterm-pty from using the awful dirty hack.
@kjvalencik xterm-pty looks really great. I hope we can improve the integration over time to make it even smoother.
Perhaps we could even add some kind of integrations directly into emscripten as an alternative/option for the default out.html for emscripten programs?
A couple of notes from looking through the README. -sNO_EXIT_RUNTIME=1 (AKA -sEXIT_RUNTIME=0) is the default in emscripten so I don't think you need to ask folks to specify that settings. Also, I think the acronym normally used to refer to command line apps is CLI rather then CUI (I could wrong but that certainly seems to show a lot more on the web).
I spent some time reading over xterm-pty and the generated emscripten code and have a better understanding.
For my use case, I need a TTY with lower level control and ran into the same issue as @mame. In the generated code, there's no way to override stdin and still having it as a TTY.
I don't actually need super deep control and would like to defer to most of emscripten's implementation (e.g., UTF8 handling). I only need:
Override getChar (instead of using window.prompt) to read from my app's input buffer
Override poll so that select doesn't always return ready (causes an infinite loop)
Editing the generated code to look for overrides on Module, this was sufficient for my use case:
constbufs: string[]=[];awaitinit({stdin: {getChar(){returnbufs.shift();},poll(){// NOTE: This implementation is incomplete, but sufficient for my appreturn(bufs.length||stream.tty.input.length) ? 5 : 0;}},});
For my use case, I need a TTY with lower level control and ran into the same issue as @mame. In the generated code, there's no way to override stdin and still having it as a TTY.
@kjvalencik If you need to replace only getchar and poll, there is a way, though it is not so good:
In fact, xterm-pty overrides those functions in such a way.
It would be nice if there was a less-hacky way to override them, and I think it would be possible for just getchar and poll. But a more realistic program would need to intercept more complicated syscalls like ioctl. I think it would be difficult for emscripten to expose the API to allow to hook all IO-related syscalls. Rather, it would be more realistic to integrate xterm-pty itself into emscripten, if emscripten authors accept its integration.
(I'm not quite sure what specific problem you are facing, so sorry if my comment is misguided.)
The demo is really cool, but failed to launch even if follow the instructions at "Emscripten integration" section, with errors below:
emscriptenHack.ts:201 failed to overwrite __syscall_ioctl of emscripten
e.emscriptenHack @ emscriptenHack.ts:201
emscriptenHack.ts:205 failed to overwrite __syscall_newselect of emscripten
e.emscriptenHack @ emscriptenHack.ts:205
emscriptenHack.ts:208 Uncaught TypeError: Cannot read properties of undefined (reading 'dummy')
at e.emscriptenHack (emscriptenHack.ts:208:30)
at onmessage (worker.js:6:3)
With some simple debug, I found that asmLibraryArg in invalid in emscriptenHack.ts.
Is xterm-pty still compatible with the latest emscripten?
The demo is really cool, but failed to launch even if follow the instructions at "Emscripten integration" section, with errors below: emscriptenHack.ts:201 failed to overwrite __syscall_ioctl of emscripten e.emscriptenHack @ emscriptenHack.ts:201 emscriptenHack.ts:205 failed to overwrite __syscall_newselect of emscripten e.emscriptenHack @ emscriptenHack.ts:205 emscriptenHack.ts:208 Uncaught TypeError: Cannot read properties of undefined (reading 'dummy') at e.emscriptenHack (emscriptenHack.ts:208:30) at onmessage (worker.js:6:3)
With some simple debug, I found that asmLibraryArg in invalid in emscriptenHack.ts.
Is xterm-pty still compatible with the latest emscripten?
because of #19499 getting merged, I don't think you need xterm-pty anymore.
Activity
sbc100 commentedon Feb 22, 2022
This feature sounds great!
I would hope you can do a lot of what you needs using a library JS file. e.g.
library_xterm.js.. then anyone who wants to could grab you code and use it via--js-library library_xterm.jskjperkin commentedon Jul 5, 2022
I took a stab at a proof-of-concept (#17371) in my free time. I think there's still much to do. Unfortunately, I don't have tons of experience writing good JavaScript, and practically zero experience with implementing tests for JavaScript.
Some goals I have in mind are:
stdin,stdout, andstderrare initialized, so that those streams use the supplied operations instead./dev, so developers don't have to bother/dev/tty{,1}with them, in case they want to do something very unusual./dev/stdin,/dev/stdout, and/dev/stderriliakan commentedon Feb 15, 2023
@mame Is everything good on xterm side?
In other words, if Emscripten is adapted, will the integration be all-good and fluent?
I saw your demos, they look awesome, did you manage to fully integrate it, without hanging bugs/side-effects?
mame commentedon Jun 1, 2023
As a first step, I sent a PR #19499 to expose the syscall hooks that we would like Emscripten runtime to provide at a minimum.
We are still a long way from getting it to work with
--js-library library_xterm.js, but in the meantime, it will save xterm-pty from using the awful dirty hack.kjvalencik commentedon Jun 30, 2023
This is really awesome and the demos look cool.
I tried out
xterm-ptywith tintin but, it doesn't seem to be quite working. Sequences like up/down arrows are being echoed instead of handled.Since that's not the case with the
vimdemo, I was wondering what might be causing that?mame commentedon Jul 1, 2023
@kjvalencik That should be discussed in https://github.com/mame/xterm-pty, not here. Could you file an issue with how to reproduce your problem, the expected result, and the actual result at https://github.com/mame/xterm-pty/issues/new?
sbc100 commentedon Jul 1, 2023
@kjvalencik xterm-pty looks really great. I hope we can improve the integration over time to make it even smoother.
Perhaps we could even add some kind of integrations directly into emscripten as an alternative/option for the default
out.htmlfor emscripten programs?A couple of notes from looking through the README.
-sNO_EXIT_RUNTIME=1(AKA-sEXIT_RUNTIME=0) is the default in emscripten so I don't think you need to ask folks to specify that settings. Also, I think the acronym normally used to refer to command line apps is CLI rather then CUI (I could wrong but that certainly seems to show a lot more on the web).kjvalencik commentedon Jul 3, 2023
I spent some time reading over xterm-pty and the generated emscripten code and have a better understanding.
For my use case, I need a TTY with lower level control and ran into the same issue as @mame. In the generated code, there's no way to override
stdinand still having it as a TTY.I don't actually need super deep control and would like to defer to most of emscripten's implementation (e.g., UTF8 handling). I only need:
getChar(instead of usingwindow.prompt) to read from my app's input bufferpollso thatselectdoesn't always return ready (causes an infinite loop)Editing the generated code to look for overrides on
Module, this was sufficient for my use case:mame commentedon Jul 6, 2023
@kjvalencik If you need to replace only getchar and poll, there is a way, though it is not so good:
In fact, xterm-pty overrides those functions in such a way.
It would be nice if there was a less-hacky way to override them, and I think it would be possible for just getchar and poll. But a more realistic program would need to intercept more complicated syscalls like ioctl. I think it would be difficult for emscripten to expose the API to allow to hook all IO-related syscalls. Rather, it would be more realistic to integrate xterm-pty itself into emscripten, if emscripten authors accept its integration.
(I'm not quite sure what specific problem you are facing, so sorry if my comment is misguided.)
versaloon commentedon Aug 6, 2023
The demo is really cool, but failed to launch even if follow the instructions at "Emscripten integration" section, with errors below:
emscriptenHack.ts:201 failed to overwrite __syscall_ioctl of emscripten
e.emscriptenHack @ emscriptenHack.ts:201
emscriptenHack.ts:205 failed to overwrite __syscall_newselect of emscripten
e.emscriptenHack @ emscriptenHack.ts:205
emscriptenHack.ts:208 Uncaught TypeError: Cannot read properties of undefined (reading 'dummy')
at e.emscriptenHack (emscriptenHack.ts:208:30)
at onmessage (worker.js:6:3)
With some simple debug, I found that asmLibraryArg in invalid in emscriptenHack.ts.
Is xterm-pty still compatible with the latest emscripten?
leahanderson1 commentedon Nov 17, 2023
because of #19499 getting merged, I don't think you need xterm-pty anymore.
leahanderson1 commentedon Nov 17, 2023
nevermind