js: Simpler and more robust startup procedure

Previously, we initialised all of the JavaScript event handlers as soon
at the DOM was loaded, and then called main() ourselves once the
Emscripten runtime was ready.  This was slightly dangerous since it
depended on none of those event handlers' being called before main().
In practice this was difficult because most of the elements the event
handlers were attached to were invisible, but it did limit what event
handlers could safely be used.

Now, the event handlers are initialised from main().  This makes things
work in a sufficiently conventional way that we can just let the
Emscripten run-time call main() in its usual way, rather than involving
ourselves in the minutiae of Emscripten's startup.
This commit is contained in:
Ben Harris
2022-12-09 13:56:12 +00:00
parent 420663d477
commit 9d7b044c01
5 changed files with 92 additions and 79 deletions

12
emcc.c
View File

@ -43,6 +43,8 @@
/*
* Extern references to Javascript functions provided in emcclib.js.
*/
extern void js_init_puzzle(void);
extern void js_post_init(void);
extern void js_debug(const char *);
extern void js_error_box(const char *message);
extern void js_remove_type_dropdown(void);
@ -936,6 +938,11 @@ int main(int argc, char **argv)
float *colours;
int i;
/*
* Initialise JavaScript event handlers.
*/
js_init_puzzle();
/*
* Instantiate a midend.
*/
@ -1040,6 +1047,11 @@ int main(int argc, char **argv)
if (param_err)
js_error_box(param_err);
/*
* Reveal the puzzle!
*/
js_post_init();
/*
* Done. Return to JS, and await callbacks!
*/