Skip to content

Implement cbool for emscripten_set_main_loop to reflect recent emsdk change #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

maxpicklez
Copy link

@maxpicklez maxpicklez commented Feb 20, 2025

This is related to and resolves issue #129 for me.

Emsdk recently changed the type signature for emscripten_set_main_loop specifically changing the argument from int to bool. There exists an issue when linking emscripten in regards to the definition of a ctype for bool. Nim language has proper support for the bool (boolean) C99 type however the function definitions use custom defined types to match the fact that WASM thunks everything to int32? Either way by adding a cbool type which corresponds to the "bool" declared in emsdk function definition the linking now doesnt fail.

emsdk breaking change

I spoke to the nimlang developers in matrix chat that pointed me to the proper definition for bool internal to nim language so it lead me to believe that its related to nico. though adding the same cbool declaration to nim/system/ctypes.nim also alleviated the issue I feel a bit out of my depth here in the sense that I cant fully articulate where the problem is.

Also spoke to the emsdk developer who made the change. Investigating how to write tests for this or where to look.

@@ -1829,9 +1829,10 @@ proc getRecordSeconds*(): int =
return 0

when defined(emscripten):
type cbool* {.importc: "bool", nodecl.} = uint8
Copy link

@mratsim mratsim Feb 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
type cbool* {.importc: "bool", nodecl.} = uint8
type cbool* {.importc: "bool", header:"<stdbool>".} = uint8

This will fail if stdbool is not imported, nodecl is used for compiler builtins, though it's possible that the compiler auto-imports stdbool and issues a warning. See https://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf p253
image

type em_callback_func* = proc() {.cdecl.}
{.push importc.}
proc emscripten_set_main_loop*(f: em_callback_func, fps, simulate_infinite_loop: cint)
proc emscripten_set_main_loop*(f: em_callback_func, fps: cint, simulate_infinite_loop: cbool)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
proc emscripten_set_main_loop*(f: em_callback_func, fps: cint, simulate_infinite_loop: cbool)
proc emscripten_set_main_loop*(f: em_callback_func, fps: cint, simulate_infinite_loop: bool)

Given that cbool and Nim bool both defer to _Bool it might be okay to reuse Nim bool here and it might be the reason why cbool doesn't exist in Nim.

I leave that choice to the maintainers of the library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants