Files
logos-messaging-nim/scripts/patch_wasm_brokers.py
T
Ivan FBandClaude Opus 5 6eb524d245 build(wasm): brokers copy that can be imported with --threads:off
The edge build is --threads:off. brokers' EventBroker / RequestBroker /
SignalBroker macros each carry a

    when not compileOption("threads"):
      {.error: "... requires --threads:on".}

inside the macro's OWN body. A `{.error.}` pragma is evaluated when the macro
is semchecked -- that is, on a plain `import` -- so threads-off the modules
cannot be imported at all, not merely used in mt mode. Gating the macro call
sites is therefore not sufficient; roughly a dozen modules in the waku tree
import request_broker/event_broker without ever invoking them threads-off.

So wasm-deps/brokers is bumped from the old 3.1.1 snapshot to the pinned 3.3.0
with those six pragmas rewritten as `macros.error(...)` calls, which fire only
if the macro is actually invoked in that mode. That is the correct behaviour
and worth upstreaming; scripts/patch_wasm_brokers.py reapplies it after a bump.

config.nims switches the override in under -d:emscripten, after the Nimble
block: Nim prepends each --path as it is registered, so the last one wins, and
config-file paths are registered after the command line. Passing --path in the
build script does not work for this reason -- the nimble copy still wins.

backend_comm.nim additionally gates its own mt broker declarations, which
genuinely cannot exist threads-off, and defines just the response types the
persistency facade names. Those are inert: the facade's threads-off path never
reaches a provider, and the browser build has no persistence.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012qDYE5r2t2dMry5XpWWySu
2026-08-08 01:45:09 +02:00

32 lines
1.0 KiB
Python

import re, glob
# Turn the macros' compile-time `{.error: "...".}` pragma into a macro-time
# `macros.error("...")` call. The pragma form is evaluated when the macro body
# is semchecked -- i.e. on plain `import brokers/request_broker` -- so under
# --threads:off the module cannot be imported at all, even by code that never
# invokes the mt mode. The call form fires only if the macro is actually used
# in that mode, which is the intended behaviour.
PAT = re.compile(
r'( *)\{\.\n'
r' *error:\n'
r'((?: *"[^\n]*\n)+)'
r' *\.\}\n'
)
total = 0
for path in sorted(glob.glob("wasm-deps/brokers/brokers/*.nim")):
src = open(path).read()
def repl(m):
indent, body = m.group(1), m.group(2)
parts = [ln.strip() for ln in body.strip().splitlines()]
joined = ("\n" + indent + " ").join(parts)
return f"{indent}macros.error(\n{indent} {joined}\n{indent})\n"
out, n = PAT.subn(repl, src)
if n:
open(path, "w").write(out)
total += n
print(f"{path}: {n} site(s)")
print("total", total)