Skip to content

WIP: Improve Emscripten support - #5259

Draft
kinke wants to merge 13 commits into
ldc-developers:masterfrom
kinke:emscripten
Draft

WIP: Improve Emscripten support#5259
kinke wants to merge 13 commits into
ldc-developers:masterfrom
kinke:emscripten

Conversation

@kinke

@kinke kinke commented Aug 10, 2026

Copy link
Copy Markdown
Member

Based on WASI.

@kinke

kinke commented Aug 10, 2026

Copy link
Copy Markdown
Member Author

@QuantumSegfault: This seems like a viable approach - basing Emscripten on your WASI work, after reading https://v8.dev/blog/emscripten-standalone-wasm and noting that they apparently try to use WASI APIs as much as possible, even in non-standalone-wasm mode.

To get the druntime test runners to link, I only had to avoid a single undefined _CLOCK_MONOTONIC symbol. And a single module fails its unittests (core.sync.event): https://github.com/ldc-developers/ldc/actions/runs/31392028311/job/93465932716?pr=5259

@QuantumSegfault

Copy link
Copy Markdown
Contributor

I'm not sure this makes sense to do like this.

Emscripten use a more vanilla Musl, without all of the differences to the headers WASI-libc made. They opted to stub out functions, rather than remove them, and otherwise use the unmodified types, constants, etc. (other than where architecture specific differences are concerned). A lot of the CRuntime_WASI blocks, particularly in Phobos, may be unnecessary.

WASI is also iffy, as that's an internal implementation detail. Emscripten also provides fuller emulation of things like threads, that we disable tests for on WASI. Also they provide their own filesystem, which isn't subject to all the quirks of WASI (e.g. the inability to have absolute symlinks; though that's largely a Wasmtime limitation).

@kinke

kinke commented Aug 10, 2026

Copy link
Copy Markdown
Member Author

Yeah the importc_compare test shows a number of incompatible declarations, seemingly matching your points. - I'll pursue this a bit further, trying to get it more or less green (edit: i.e., the druntime/Phobos parts tested for WASI too). Then different avenues (e.g., based on CRuntime_Musl alone instead of WASI, WASIp1 and CRuntime_WASI) can be explored as well.

I'd hope that Emscripten would eventually be based on wasi-libc...

@kinke
kinke force-pushed the emscripten branch 7 times, most recently from dc050c1 to b03eb55 Compare August 13, 2026 17:52
@kinke

kinke commented Aug 13, 2026

Copy link
Copy Markdown
Member Author

Okay, this is now based on CRuntime_Musl with a few Emscripten special-cases (instead of CRuntime_WASI earlier), but still uses WASI[p1] too (mainly to inherit the disabled parts and unitests). importc_compare is truly green; druntime still fails for a single core.sync.event unittest (depending on pthread_cond_timedwait), and Phobos just fails 2 std.datetime modules unittests, apparently just because I haven't found a way to make the node process use the /usr/share/zoneinfo host timezone files.

I'm okay with depending/building on predefined WASI, at least for the time being; supporting the few potential Posix API parts being stubbed/emulated on Emscripten and not available for WASI are IMO not of big interest. What's of interest to me is that Emscripten support is at a similar level to wasi-libc, so that wasm users can target both platforms with little to no hassle.

@QuantumSegfault

Copy link
Copy Markdown
Contributor

I'm okay with depending/building on predefined WASI, at least for the time being; supporting the few potential Posix API parts being stubbed/emulated on Emscripten and not available for WASI are IMO not of big interest. What's of interest to me is that Emscripten support is at a similar level to wasi-libc, so that wasm users can target both platforms with little to no hassle.

Threading is a notable difference. Emscripten actually DOES support threading (over web workers; with some caveats).

https://emscripten.org/docs/porting/pthreads.html

Signals still not, other than for pthread_kill

Though I wouldn't mind holding off on the threading discussion until I figure out WASIp3 (with wasi-sdk 34 and LLVM 23 coming up; which will support cooperative threading [like Emscripten]; no preemptive threading yet though).


Other than that I think the rest of the WASI route-arounds should be fine, if not always necessary.

@kinke

kinke commented Aug 13, 2026

Copy link
Copy Markdown
Member Author

Though I wouldn't mind holding off on the threading discussion until I figure out WASIp3

Yeah that sounds good to me. [And note that I've just started with WASIp1, no idea if WASIp2 might be a better match for Emscripten.] - In the long run, if Emscripten is to survive, I can definitely imagine it being based on wasi-libc and its Posix emulation layer in the future. So the distinction might go away, and duplicating all current Emscripten divergences potentially not worth the effort.

The compiler has a special case wrt. TLS globals for wasm in general IIRC, emitting them as thread-global; so the compiler will probably have to be adapted for wasm threading too.

@QuantumSegfault

Copy link
Copy Markdown
Contributor

Yeah that sounds good to me. [And note that I've just started with WASIp1, no idea if WASIp2 might be a better match for Emscripten.]

I'm fairly certain that WASIp1 is the only valid option for Emscripten.

They provide a subset of WASIp1 for the web (but not p2+ AFAICT)

https://github.com/emscripten-core/emscripten/blob/main/src/lib/libwasi.js

The compiler has a special case wrt. TLS globals for wasm in general IIRC, emitting them as thread-global; so the compiler will probably have to be adapted for wasm threading too.

Good to know.

enum B_NO_TRANSLATOR = (B_TRANSLATION_ERROR_BASE + 1);
enum B_ILLEGAL_DATA = (B_TRANSLATION_ERROR_BASE + 2);
}
else version (CRuntime_WASI)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah here I was lazy and didn't want to duplicate the existing CRuntime_WASI block, after seeing that the Emscripten headers forward the WASI codes. [And I don't really care about the extra ones; if people relied on them, their code wouldn't work with WASI.]

At least a comment wrt. Emscripten would be good though; there's some polishing left to do, hence the draft state and WIP title. :)

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