Repository navigation
Add low-level APIs for running Emscripten programs in xterm.js - #19499
Conversation
When an Emscripten program calls `ioctl(fd, TCGETS, argp)` for TTY,
`stream.tty.ops.ioctl_tcgets` is called if defined. This allows a user
to hook this event by defining `TTY.default_tty_ops.ioctl_tcgets`.
`stream.tty.ops.ioctl_tcgets` should return an object like the following
format:
```
{
c_iflag: number,
c_oflag: number,
c_cflag: number,
c_lflag: number,
c_cc: number[32],
}
```
When an Emscripten program calls `ioctl(fd, TCSETS, argp)` for TTY,
`stream.tty.ops.ioctl_tcsets` is called if defined. This allows a user
to hook this event by defining `TTY.default_tty_ops.ioctl_tcsets`.
`TCSETSW` and `TCSETSF` are handled in the same way.
`stream.tty.ops.ioctl_tcsets` should accept the command (`""` for
`TCSETS`, `"W"` for `TCSETSW`, and `"F"` for `TCSETSF`) and a termios
object like the following format
```
{
c_iflag: number,
c_oflag: number,
c_cflag: number,
c_lflag: number,
c_cc: number[32],
}
```
Also, `stream.tty.ops.ioctl_tcsets` should return 0 on success (or -1 on
error).
When an Emscripten program calls `ioctl(fd, TIOCGWINSZ, argp)` for TTY, `stream.tty.ops.ioctl_tiocgwinsz` is called if defined. This allows a user to hook this event by defining `TTY.default_tty_ops.ioctl_tiocgwinsz`. `stream.tty.ops.ioctl_tiocgwinsz` should return [row, col].
Previously, the timeout argument of select syscall was just ignored. This changeset passes the argument to `poll` hook, which allows a user to handle the argument. This is useful for [xterm-pty](https://github.com/mame/xterm-pty/).
|
Note that this PR contains two more minor changes for xterm-pty.
Please let me know if you prefer to separate the PRs. |
|
I have not written the test yet because I have no idea how to write it. Please let me know if there is an existing test that might be helpful to write it. |
sbc100
left a comment
There was a problem hiding this comment.
Looks great!
Is there some way we can test this here in the emscripten repo to avoid regressions?
| ioctl_tcgets: function(tty) { | ||
| // typical setting | ||
| return { | ||
| c_iflag: 0x6500, // ICRNL | IXON | IMAXBEL | IUTF8 |
There was a problem hiding this comment.
Can you use {{{ cDefs.ICRNL | cDefs.IXON | cDefs.IMAXBEL | cDefs.IUTF8 }}} here instead of the hex number? Or is that harder to read?
| var timeoutInMillis = -1; | ||
| if (timeout) { | ||
| var timeoutLow = (readfds ? {{{ makeGetValue('timeout', 0, 'i32') }}} : 0), | ||
| timeoutHigh = (readfds ? {{{ makeGetValue('timeout', 4, 'i32') }}} : 0); |
There was a problem hiding this comment.
I think you can use var timeout = {{{ makeGetValue('timeout', 4, 'i53') }}} instead of reading the hi/low pair yourself.
There was a problem hiding this comment.
Oh sorry, this is intentional here. The code reads the following two fields of struct timeval.
struct timeval {
long tv_sec; // seconds
long tv_usec; // microseconds
};
The names timeoutLow and timeoutHigh were bad. I'll change them as timeoutSeconds and timeoutMicroseconds.
There was a problem hiding this comment.
In that case I wonder if its worth using timeval.tv_sec and timeval.tv_usec here rather than hardcoding 0 and 4? Maybe not worth it?
There was a problem hiding this comment.
You are absolutely right. Why didn't I notice it? Done at 8fe4bb8
| case {{{ cDefs.TCSETSF }}}: { | ||
| if (!stream.tty) return -{{{ cDefs.ENOTTY }}}; | ||
| if (stream.tty.ops.ioctl_tcsets) { | ||
| var optional_actions = op == {{{ cDefs.TCSETS }}} ? "" : op == {{{ cDefs.TCSETSW }}} ? "W" : "F"; |
There was a problem hiding this comment.
Just single quotes in JS code
There was a problem hiding this comment.
What do these "W" and "F" strings mean?
Can you move this line down to line 258 where optional_actions is first used?
There was a problem hiding this comment.
It is an argument to distinguish the event types: "" stands for TCSETS, "W" does for TCSETSW, and "F" does for TCSETSF.
I am open to change the values. Do you like to pass the numbers like cDefs.TCSETS or cDefs.TCSETSW? Or pass a string like "TCSETS" or "TCSETSW"?
There was a problem hiding this comment.
Maybe just stick with cDefs.TCSETS since they already exist?
There was a problem hiding this comment.
Maybe just stick with cDefs.TCSETS since they already exist?
There was a problem hiding this comment.
Just FYI: The reason why I did something a little different is that the constants such as
cDefs.TCSETScannot be referred from xterm-pty which is outside of Emscripten. But, the values of the constants probably won't change easily, so it's not a big deal. I think we should aim to incorporate xterm-pty into Emscripten in the end.
BYW if you write the glue code between xterm.js and emscripten as a JS library file you can access all the macros. e.g. --js-library=library_xterm.js .. can code than interpret the emscripten requests and make calls into the external xterm.js library.
There was a problem hiding this comment.
Yes, I would like to challenge that next.
However, I think it may be difficult to get everything working nice with just --js-library=library_xterm.js.
This is because getchar function needs to synchronously wait for user input. The default implementation of getchar waits synchornously by using prompt function, but we can't do the same for xterm.js. So, xterm-pty requires to run the Emscripten runtime in a Web Worker, and waits for user input via SharedArrayBuffer.
Therefore, emcc needs to generate two JS files, one for the main thread and one for the Web Worker. I wonder if this is beyond the scope of the --js-library=library_xterm.js option.
There was a problem hiding this comment.
We have an option called PROXY_TO_THREAD that puts the main application code in worker. When getchar is called from a worker it should be able to block.. We also have -sASYNCIFY and -sASYNCIFY=2 (A.K.A JSPI) which allow functions to suspend/block, even when running on the main thread.
There was a problem hiding this comment.
Great to know PROXY_TO_THREAD, thanks! I will read the code.
We also have
-sASYNCIFYand-sASYNCIFY=2(A.K.A JSPI) which allow functions to suspend/block, even when running on the main thread.
I'm not sure about this. As long as getchar is not an async function, I thought it could not wait synchronously in the main thread. Maybe I need to learn JSPI.
There was a problem hiding this comment.
-sASYNCIFY and -sASYNCIFY=2 allow the wasm module to suspend its computation and resume later. For example, after some async work is done. From the POV of the code being suspended it appears like a blocking call.
... by ./tools/gen_struct_info.py --wasm64
Thank you!
Sure, I guess the last additional commit should fix the issue. (It may be good to write "run |
I needed to run not only `./tools/gen_struct_info.py` but also `./tools/gen_struct_info.py --wasm64`. emscripten-core#19499 (comment)
I needed to run not only `./tools/gen_struct_info.py` but also `./tools/gen_struct_info.py --wasm64`. emscripten-core#19499 (comment)
|
@sbc100 Thank you, I think I've addressed all your review comments and the CI has passed. Do you want me to tidy up the commits by rebase? Please feel free to let me know. |
|
In terms of testing, perhaps you could modify |
I needed to run not only `./tools/gen_struct_info.py` but also `./tools/gen_struct_info.py --wasm64`. #19499 (comment)
... because ioctl syscall is not well implemented yet on wasmfs
Thanks! I have added |
Head branch was pushed to by a user without write access
|
Excited to see this land. Looks like this was ready to auto-merge then the force push cancelled it. |
Background: #16352
The main purpose of this changeset is to make (some kinds of)
ioctlsyscall hookable:ioctl(TCGETS)callsTTY.default_tty_ops.ioctl_tcgetsioctl(TCSETS),ioctl(TCSETSW)andioctl(TCSETSF)callTTY.default_tty_ops.ioctl_tcsetsioctl(TCIOCGWINSZ)callsTTY.default_tty_ops.ioctl_tiocgwinszSo we can hook the events by defining
TTY.default_tty_ops.ioctl_tcgets, etc.These hooks serve as low-level APIs to run an Emscripten CUI program on xterm.js.
Currently, xterm-pty uses very dirty hack to hook the syscall (by redefining
wasmImports).This hack is very fragile and will stop working if the Emscripten runtime changes (for example,
asmLibraryArgwas renamed towasmImports).The proposed ioctl hooks allow (relatively) clean integration between xterm-pty and the Emscripten runtime.
What do you think?