On Mon, Apr 8, 2019 at 3:40 AM <[email protected]> wrote: > I really cannot believe that it is not possible to compile such > a simple interactive program with emcc.
Believe it. :-) The JavaScript hosts like Node.js and browsers weren't designed for synchronous I/O. In a browser you can use Window.prompt() and alert()...both of which are too annoying to be practical. But other than that you won't see any I/O or impacts of DOM manipulations. JavaScript programmers have just learned to live with it. For I/O to process you have to yield to the main loop...which means your C code can't still be running (on the main thread) when it does. The only options for C code that wants to do synchronous I/O while remaining on the stack are if that code is running on a worker thread, or if you use the emterpreter: https://github.com/emscripten-core/emscripten/wiki/Emterpreter Threads aren't an option yet for running under Node.js, as Emscripten does not currently have pthreads support (though Node.js seemingly has the necessary features where it *could* be implemented): https://github.com/emscripten-core/emscripten/issues/6332 So this means that you pretty much can only preserve your C stack state if you're running in a bytecode interpreter, vs. directly on WASM or asm.js. It's not really emscripten's fault, it's just the way things are. There are probably too many variances in the meaning of what a "console app" in the browser would look like (what kind of DOM tree, CSS, etc.) to canonize one of those into the emscripten build yet. A Node.js console app may seem more clearly defined, but it would still be non-trivial...and since interactive console-based node.js programs are uncommon, it's probably just not a priority. And as mentioned, it would force you to use the bytecode interpreter at this time. Since your example is a programming language, you might find some of the following work to be of interest--we've been building an interactive REPL for the Rebol language: https://forum.rebol.info/t/on-giving-librebol-js-more-powers-than-javascript/849 https://github.com/hostilefork/replpad-js/ https://github.com/metaeducation/ren-c/tree/master/extensions/javascript Best --Brian On Mon, Apr 8, 2019 at 3:40 AM <[email protected]> wrote: > Hello, > I have problems with emcc and node.ja when I test > a small interactive program: > > ---------- begin test program tst261.c ---------- > #include <stdio.h> > > int main(int argc, char *argv[]) > { > char buf[100]; > printf("Your input? "); > fflush(stdout); > while (fgets(buf, sizeof(buf), stdin) != NULL && > buf[0] != '!') { > printf("You typed: %s", buf); > printf("Your input (Use ! to terminate)? "); > fflush(stdout); > } > return 0; > } > ---------- end test program tst261.c ---------- > > When I use gcc and run the program afterwards it looks like follows: > > myPrompt> gcc tst261.c -o tst261 > myPrompt> ./tst261 > Your input? test > You typed: test > Your input (Use ! to terminate)? another test > You typed: another test > Your input (Use ! to terminate)? ! > myPrompt> > > As you can see input and output can alternate. > With emcc and node.js it looks like follows: > > myPrompt> emcc tst261.c -o tst261.js > myPrompt> node tst261.js > test > another test > ! > <<< Here I typed cntl-d >>> > Your input? You typed: test > Your input (Use ! to terminate)? You typed: another test > stdio streams had content in them that was not flushed. you should set > EXIT_RUNTIME to 1 (see the FAQ), or make sure to emit a newline when you > printf etc. > myPrompt> > > As you can see: After the start of the program all the > input must be typed in followed by cntl-d at the beginnig of a line. > Afterwards the program processes all given input in one batch. > > Interestingly it is also possible to press cntl-d several times: > > myPrompt> node tst261.js > test > <<< Here I typed cntl-d >>> > Your input? You typed: test > another test > <<< Here I typed cntl-d >>> > Your input (Use ! to terminate)? You typed: another test > ! > <<< Here I typed cntl-d >>> > stdio streams had content in them that was not flushed. you should set > EXIT_RUNTIME to 1 (see the FAQ), or make sure to emit a newline when you > printf etc. > myPrompt> > > I know that Javascript is single threaded, uses an event loop > and works with asynchronous I/O. But I think that it should be > possible to base a synchonous I/O on an asynchronous one. > > All JavaScript "solutions" I found in the internet suggest, that > the program must register some callback (or use a Promise) an then > needs to terminate. Afterwards the event loop would call the callback > respecively Promise. In any such case the program is terminated > and executes in a callback function. > > In the days when Pascal and other programming languages experimented > with cooperative multitasking it was always possible to write > such a simple interactive program like above. The sychronous > input function entered the event loop (where other events would be > handled just like the Javascript event loop does) and it came back, > when a keyboard input had been typed in. > > I really cannot believe that it is not possible to compile such > a simple interactive program with emcc. > > I have just started to compile my project (Seed7) with emcc. > Several things already work good, but interactive things do not. > > I also need a synchronous solution to read single keypresses > without echo and a function to detect if a key has been pressed. > > First I target node.js, but later the browsers are also a target. > > Many thanks in advance for your help. > > Regards, > Thomas Mertes > > -- > Seed7 Homepage: http://seed7.sourceforge.net > Seed7 - The extensible programming language: User defined statements > and operators, abstract data types, templates without special > syntax, OO with interfaces and multiple dispatch, statically typed, > interpreted or compiled, portable, runs under linux/unix/windows. > -- You received this message because you are subscribed to the Google Groups "emscripten-discuss" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
