Repository navigation
Add support for preMainLoop/postMainLoop functions - #22621
Conversation
5b8d810 to
c73154e
Compare
| clearInterval(id); | ||
| }, | ||
|
|
||
| $registerPostMainLoop: (f) => { |
There was a problem hiding this comment.
Maybe add a comment that as an optimization, this does not include MainLoop but instead checks for it at runtime and does nothing?
Separately, I wonder if we can do better than this. We could in theory add a "weak linking" mechanism, that would add some code only if MainLoop was actually included. Though if it's complicated it might not be worth it.
There was a problem hiding this comment.
Sadly I don't think we have such a weak linking mechanism today. The way I did it here I think is the closest thing we have to weak linking in JS code.
c73154e to
43ca757
Compare
Rather than having code from e.g SDL, etc, in event loop code use a callback registration system.
43ca757 to
f094bf2
Compare
|
|
||
| $registerPostMainLoop: (f) => { | ||
| // Does nothing unless $MainLoop is included/used. | ||
| typeof MainLoop != 'undefined' && MainLoop.postMainLoop.push(f); |
There was a problem hiding this comment.
| typeof MainLoop != 'undefined' && MainLoop.postMainLoop.push(f); | |
| MainLoop?.postMainLoop.push(f); |
There was a problem hiding this comment.
Sadly I don't think the ? operator works like that (i.e. it doesn't work on top level things):
$ node
Welcome to Node.js v18.20.3.
Type ".help" for more information.
> a?.foo
Uncaught ReferenceError: a is not defined
>
There was a problem hiding this comment.
Indeed from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining:
Optional chaining cannot be used on a non-declared root object
There was a problem hiding this comment.
Oh sorry. That is unfortunate, I wonder why it has that restriction.
With -sOFFSCREEN_FRAMEBUFFER, -pthread and -sGL_SUPPORT_EXPLICIT_SWAP_CONTROL, a WebGL context that is proxied to the main thread and created with explicitSwapControl=false is supposed to be presented by the runtime at each main loop iteration. Three earlier changes each broke a part of this: - emscripten-core#22621 moved the swap out of the main loop runner into a hook on _emscripten_proxied_gl_context_activated_from_main_browser_thread, but attached it with a `__postjs` suffix. That is not a JS library decorator, so the hook was never emitted. The key was instead treated as a library symbol of its own, which showed up in builds that include the whole JS library (-sINCLUDE_FULL_LIBRARY, -sMAIN_MODULE=1). Use `__postset`. - emscripten-core#22621 left the dependency on the native emscripten_webgl_commit_frame() behind on $setMainLoop, and emscripten-core#23855 then removed it from there as unused. Since then nothing has exported that function to JS for the hook to call. Make the hook depend on it. Also drop the hook's `$registerPreMainLoop` dependency: since emscripten-core#27995 it is detected automatically, and listing it is reported as redundant. - emscripten-core#22157 narrowed the write of the explicitSwapControl flag in the context handle from i32 to i8 along with the EM_BOOL struct fields. The handle field is not an EM_BOOL: the hook reads it as an i32, and the WebGLContextHandle struct in webgl1.c declares it as uint32_t. The handle is malloc()ed, so the upper bytes were left uninitialized. Write it as an i32 again. No test passed -sGL_SUPPORT_EXPLICIT_SWAP_CONTROL, and the one test of a proxied implicit-swap context (test_emscripten_animate_canvas_element_size) only checks the exit code. Add a browser test that reads back the canvas to check that the frame was presented. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Rather than having code from e.g SDL, etc, in event loop code use a callback registration system.