binji / wasm-clang

Running Clang/LLD in WebAssembly Demo
https://binji.github.io/wasm-clang
Apache License 2.0
259 stars 27 forks source link

Support of stdin? #5

Closed leoleoasd closed 4 years ago

leoleoasd commented 4 years ago

It will be awsome if input to the WASM program is supported. What is stopping us against achieving it?

leoleoasd commented 4 years ago

It seems that the memfs module handled write to stdout and stderr in the fd_write function, but I havn't found similar code in the fd_read funciton.

binji commented 4 years ago

I avoided implementing stdin, since generally people want to use it for reading input from the user. That would require blocking, which doesn't work well on the web. It's possible to do if the wasm code was always running on a Web Worker, though. It's also possible to support stdin as a regular file, where the user is required to provide all input ahead of time. That's probably the easiest way to implement it for now.

leoleoasd commented 4 years ago

I'm trying to implement a simple online ide for beginners to run simple C++ programs, which doesn't require an interactive interface. Providing all input ahead of time is acceptable for me since it just can't read from stdin for now (Always get EOF). This would be an awesome feature.

If you don't have time to implement it, maybe I'll try to make a PR of this when I have time.

This is an awesome project, thanks a lot for your work.

binji commented 4 years ago

I'm trying to implement a simple online ide for beginners to run simple C++ programs, which doesn't require an interactive interface.

Awesome, this was my motivating use case too :-)

If you don't have time to implement it, maybe I'll try to make a PR of this when I have time.

I don't currently, but I'm happy to review a PR.

HKalbasi commented 3 years ago

@leoleoasd I have problem with stdin in web demo (https://binji.github.io/wasm-clang/), How can I extend your work to the web demo?

With this code, I get a random number every time I run it:

#include <iostream>

using namespace std;

int main() {
    int x, y;
    cin >> x >> y;
    cout << x+y;
}
leoleoasd commented 3 years ago

@leoleoasd I have problem with stdin in web demo (https://binji.github.io/wasm-clang/), How can I extend your work to the web demo?

With this code, I get a random number every time I run it:

#include <iostream>

using namespace std;

int main() {
    int x, y;
    cin >> x >> y;
    cout << x+y;
}

@HKalbasi The web demo doesn't utilized the srdin feature. You need to change https://github.com/binji/wasm-clang/blob/master/shared.js#L676 , add a "stdinStr" option when instancing MemFS. We don't support interactive reading right now, means that you can only set stdinStr before running the App.