noob Q: set main loop under nodejs not working as expected.

41 views
Skip to first unread message

Dave Nicponski

unread,
Jan 28, 2014, 4:51:12 PM1/28/14
to emscripte...@googlegroups.com
Sorry for the noob question, but here goes.

# Source code
$ cat headless_main_loop.cc

#include <stdio.h>

#include "emscripten/emscripten.h"

int foo = 0;

void dummy_loop() {
  printf("dummy loop: %d\n", foo++);
  if (foo >= 5)
    emscripten_cancel_main_loop();
}

int main(int argc, char **argv) {
  printf("Start\n");

  emscripten_set_main_loop(dummy_loop, 0, true);  // XXX

  printf("Main done.\n");
  return 0;
}  


# Build command for nodejs
$ ~/src/emscripen/emscripten/emcc -Qunused-arguments -Wall -Wextra -Werror -Wno-unused-parameter -fexceptions -std=c++11 -x c++ -iquote ./src -iquote ~/src/emscripen/emscripten/system/include/ -I . -DNDEBUG -O2 src/trash/poc/headless_main_loop.cc -o build/trash/poc/headless_main_loop.js -s HEADLESS=1


# Try with nodejs
$ (dev-test_pb) nodejs trash/poc/headless_main_loop.js
Start

# Change line XXX from true to false (infinite loop), rebuild
# Try again
$ (dev-test_pb) nodejs trash/poc/headless_main_loop.js
Start
Main done.


In both cases, it works as expected if i compile to html and run in a browser (obviously, without the HEADLESS=1 option).
Specifically, I see this in the console:

Start
Main done.
dummy loop: 0
dummy loop: 1
dummy loop: 2
dummy loop: 3
dummy loop: 4


...which is exactly what i'd expect from both node and browser.

Am I crazy, or shouldn't this work?

Note that this is both with the default ubuntu install (1.4.9, old) and the version i'm actually using from head (1.9.0 (commit 7dc8c2ff08e46c9d9a88ba44bf221a404eeb1e5e))

Thanks,
    -dave-

Alon Zakai

unread,
Jan 28, 2014, 7:28:51 PM1/28/14
to emscripte...@googlegroups.com
The HEADLESS option is not much tested, and used for testing purposes mainly. But I think that you need to call window.runEventLoop() manually. Even then I imagine there would need to be some minor fixes.

If there is a use case that we should support here though, we can consider that - why do you want to run headless in node?

- Alon



--
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 emscripten-disc...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Dave Nicponski

unread,
Jan 28, 2014, 10:45:31 PM1/28/14
to emscripte...@googlegroups.com
Hello,

Thanks for the response.


On Tue, Jan 28, 2014 at 7:28 PM, Alon Zakai <alon...@gmail.com> wrote:
The HEADLESS option is not much tested, and used for testing purposes mainly. But I think that you need to call window.runEventLoop() manually. Even then I imagine there would need to be some minor fixes.


Hrm...  OK i can do this, but doing this conditionally for nodejs from C++ code seems strange.
 
If there is a use case that we should support here though, we can consider that - why do you want to run headless in node?

Basically, i've got a C++ application, some of which is compiled to javascript and run in a client browser.  However, much of what's being executed isn't browser-related.  Being able to factor out the browser element for code testing is the idea.  It's easier to make assertions about code correctness when the application components are run in nodejs headless than when they are executed in a browser, or so it seems to me.

I don't want an entire browser to be emulated in headless mode obviously, but basic infrastructure functionality like "reasonably correctly emulate main event loops and web worker threads in headless mode" would be very useful to have.

Thanks,
   -dave-


--
You received this message because you are subscribed to a topic in the Google Groups "emscripten-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/emscripten-discuss/2-rxY9ob3aY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to emscripten-disc...@googlegroups.com.

Dave Nicponski

unread,
Jan 28, 2014, 11:40:54 PM1/28/14
to emscripte...@googlegroups.com
On Tue, Jan 28, 2014 at 10:45 PM, Dave Nicponski <dave.ni...@gmail.com> wrote:
Hello,

Thanks for the response.


On Tue, Jan 28, 2014 at 7:28 PM, Alon Zakai <alon...@gmail.com> wrote:
The HEADLESS option is not much tested, and used for testing purposes mainly. But I think that you need to call window.runEventLoop() manually. Even then I imagine there would need to be some minor fixes.


Hrm...  OK i can do this, but doing this conditionally for nodejs from C++ code seems strange.

Yeah, this doesn't work out-of-the-box.
Adding:

emscripten_run_script("window.runEventLoop()");

at the end of main fails because headless.js 's runEventLoop triggers:

exception thrown: TypeError: Object function Date() { [native code] } has no method 'realNow',TypeError: Object function Date() { [native code] } has no method 'realNow'
    at Object.window.runEventLoop (/home/daven/src/jsminer/jsminer/jsminer/build/trash/poc/headless_main_loop.js:5671:24)

[...]

I'll go with a different approach for the moment, and try to loop back to help fixing this when i have some free time.  This is a pretty simple repro / use case which should probably be supported, and i can't imagine this test case would be too hard to fix.

Thanks for looking in to this.

        -dave-

Dave Nicponski

unread,
Jan 29, 2014, 3:30:11 AM1/29/14
to emscripte...@googlegroups.com
You know what?  Let me humbly apologize for the noise here.
I first tried this with the ubuntu-default version (1.4.9) _without_ the HEADLESS=1 option, and that failed at runtime due to a slew of things not being defined (i vaguely recall things like "print", "read", etc, not being defined), which lead me to use the HEADLESS=1 option.  In this old version, that got things to not fail at runtime pretty quickly, but exhibited the behaviour i described above (not getting in to loop).

After upgrading to head (1.9.0), i failed to retry the test without the HEADLESS=1 option.  Had I done so (as i have now), i'd have seen that everything works perfectly under nodejs as expected.

Thanks, and sorry again for the noise here.  =)

       -dave-

Reply all
Reply to author
Forward
0 new messages