Skip to content

add WASM/Emscripten build support via shim window port#1489

Open
apowers313 wants to merge 4 commits intoNetHack:NetHack-3.6from
apowers313:wasm-3.6.7
Open

add WASM/Emscripten build support via shim window port#1489
apowers313 wants to merge 4 commits intoNetHack:NetHack-3.6from
apowers313:wasm-3.6.7

Conversation

@apowers313
Copy link
Copy Markdown
Contributor

Backport of the WASM/Emscripten cross-compilation support from 3.7
(#385, #1331, #1328) to the 3.6 branch so the current production
release can run in the browser or Node.js.

Same approach as 3.7: a shim window port (win/shim/winshim.c) bridges
the C engine to JavaScript via an async callback, and a library main
(sys/libnh/libnhmain.c) replaces unixmain.c. A hints file
(sys/unix/hints/linux-wasm) wires up the Emscripten flags.

The one wrinkle specific to 3.6 is a cross-compilation data mismatch.
On LP64 hosts (Linux/macOS x86_64) unsigned long is 8 bytes, but in
WASM it's 4. The binary data files (dungeon, levels, quest text) are
written by native utilities and read by the WASM game, so the struct
layouts have to agree. A CROSS_TO_WASM guard in include/global.h
typedefs the relevant types to int/unsigned int when building for WASM
and leaves them as long/unsigned long otherwise. This doesn't come up
in 3.7 because the data file format was reworked there.

Build instructions and API docs are in sys/libnh/README.md.

I've also published this as npm packages for anyone who wants to embed
NetHack in a JS project without setting up the C toolchain:

@neth4ck/wasm-367 -- https://www.npmjs.com/package/@neth4ck/wasm-367
@neth4ck/neth4ck -- https://www.npmjs.com/package/@neth4ck/neth4ck

@nhmall nhmall added the 3.6 label Feb 18, 2026
apowers313 and others added 3 commits February 18, 2026 17:19
WASM builds can fail passwd lookup, which incorrectly blocks wizard authorization before check_user_string can accept wildcard sysconf values.

Use the wizard list check directly so wildcard authorization works as configured in sys/libnh/sysconf.
libnh (wasm-3.6.7): allow wizard mode when WIZARDS includes wildcard
@nhmall
Copy link
Copy Markdown
Contributor

nhmall commented Apr 2, 2026

Interesting approach, that of using typedefs for related struct fields when building host-side utilities for the cross-compile that causes the same data sizes to be written as those that the target cross-compiler expects.

Beyond data type size matches, I would have been a little concerned about struct padding differences between the two compilers, as well as bitfield layouts between them (perhaps the involved structs for this avoid the use of any bitfields, I didn't actually take a look).

The embedding of the data for retrieval is also noteworthy.

I hope to look into this pull request more deeply at a later date.

EDIT: a couple of typo corrections

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants