0

I am trying to basically imitate the cin functionality in a Wasm module taking the input of the user from an `.

I am approach the problem defining an extern "C" function which I will then import into my c++ code from JS using the --js-library flag.

I do manage to get the input of the user from an <input> but what I really cannot achieve is to let the c++ program to wait for the user to enter a value.

Another cue came to me from this project. In src/Main_Maze.cpp the author achieves to get the input of the user and exiting the main function using:

LOOP_SPEED = 60;
INF_LOOP = true;
void main(){
    /* CODE */
    emscripten_set_main_loop(start_prompt, LOOP_SPEED, INF_LOOP);
    ...
}

As far as I understood start_prompt() gets loaded into the main thread an it runs forever until a user enters a value.

void start_prompt(void) {

    int start_bool = 0;
    // Inline JS - Get the value entered into the faux CLI
    start_bool = EM_ASM_INT({
        return input;
    }, start_bool);

    // Select 1 to start
    if (start_bool == 1) {
        /* CODE */
        emscripten_cancel_main_loop();
        emscripten_set_main_loop(start_room, LOOP_SPEED, INF_LOOP);
    }
}

when the users inputs 1 the function calls emscripten_cancel_main_loop() and recall emscripten_set_main_loop(start_room, LOOP_SPEED, INF_LOOP) to move into another function.

Now my question is:

How do I pause/exit the main() function, wait for the user input and RESUME the main()?

Looking at the emscripten documentation, I can see that there are many functions that block or pauses the execution of the main thread syncronously, like:

emscripten_push_uncounted_main_loop_blocker(...)
emscripten_pause_main_loop()

but nothing is working. Could you please point me to an example? Please tell me if I should post my code.

Thank you

ddgg
  • 145
  • 1
  • 10

0 Answers0