Skip to content

Commit d7f65a2

Browse files
juntyralexcrichton
andauthored
Extend description of Emscripten and contrast with other WASM targets
Co-authored-by: Alex Crichton <[email protected]>
1 parent a1ed63b commit d7f65a2

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

src/doc/rustc/src/platform-support/wasm32-unknown-emscripten.md

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,36 @@
33
**Tier: 2**
44

55
The `wasm32-unknown-emscripten` target is a WebAssembly compilation target which
6-
uses the [Emscripten](https://emscripten.org/) compiler toolchain. Unlike the
7-
[`wasm32-unknown-unknown`](./wasm32-unknown-unknown.md) target, which only has
8-
partial support for the Rust standard library, `wasm32-unknown-emscripten`
9-
integrates with JavaScipt on the web to provide a more featureful standard library.
10-
This target is often used when compiling for the web, accessing web APIs, or
11-
interoperating with C and C++ code.
6+
uses the [Emscripten](https://emscripten.org/) compiler toolchain. Emscripten is
7+
a C/C++ toolchain used for running C/C++ code on the web. It provides POSIX `libc`
8+
and `libstd` implementations, access to the OpenGL and SDL APIs, and the ability
9+
to run arbitrary JavaScript code, all based on web APIs using JS glue code. With
10+
the `wasm32-unknown-emscripten` target, Rust code can interoperate with
11+
Emscripten's ecosystem, C/C++ and JS code, and web APIs.
1212

1313
One existing user of this target is the
1414
[`pyodide` project](https://pyodide.org/) which provides a Python runtime in
1515
WebAssembly using Emscripten and compiles Python extension modules written in Rust
1616
to the `wasm32-unknown-emscripten` target.
1717

18-
If you want to target the web but no not need interop with C/C++ or you want to
19-
produce a bare-bones WebAssembly binary and you do not require access to the host
20-
environment (e.g. stdio, file access, networking, etc.), consider using the
21-
[`wasm32-unknown-unknown`](./wasm32-unknown-unknown.md) target instead.
22-
23-
If you need access to the host environment, consider using one of the WASI targets
24-
([`wasm32-wasip1`](./wasm32-wasip1.md) or [`wasm32-wasip2`](./wasm32-wasip2.md)).
18+
If you want to generate a standalone bare-bones WebAssembly binary that does not
19+
require JS glue code and access to the web APIs and can thus run outside the web,
20+
the [`wasm32-unknown-unknown`](./wasm32-unknown-unknown.md) target may be better
21+
suited for you. However, [`wasm32-unknown-unknown`](./wasm32-unknown-unknown.md)
22+
only has partial support for the Rust standard library and does not (easily)
23+
support interop with C/C++ code.
24+
25+
Like Emscripten, the WASI targets [`wasm32-wasip1`](./wasm32-wasip1.md) and
26+
[`wasm32-wasip2`](./wasm32-wasip2.md) also provide access to the host environment,
27+
support interop with C/C++ (and other languages), and support most of the Rust
28+
standard library. While the WASI targets are portable across different hosts
29+
(web and non-web), WASI has no standard way of accessing web APIs, whereas
30+
Emscripten has the ability to run arbitrary JS from WASM and access many web APIs.
31+
If you are only targeting the web and need to access web APIs, the
32+
`wasm32-unknown-emscripten` target may be preferable.
2533

2634
## Target maintainers
2735

28-
When this target was added to the compiler, platform-specific documentation here
29-
was not maintained at that time. This means that the list below is not
30-
exhaustive, and there are more interested parties in this target. That being
31-
said, those interested in maintaining this target are:
32-
3336
- Hood Chatham, https://github.com/hoodmane
3437
- Sam Clegg, https://github.com/sbc100
3538
- Juniper Tyree, https://github.com/juntyr
@@ -99,11 +102,15 @@ It's recommended to conditionally compile code for this target with:
99102
#[cfg(all(target_family = "wasm", target_os = "emscripten"))]
100103
```
101104

102-
Note that there is no way to tell via `#[cfg]` whether code will be running on
103-
the web or not.
104-
105105
## Enabled WebAssembly features
106106

107107
WebAssembly is an evolving standard which adds new features such as new
108-
instructions over time. The `wasm32-unknown-emscripten` target inherits the
109-
default settings of Emscripten.
108+
instructions over time. This target's default set of supported WebAssembly
109+
features will additionally change over time. The `wasm32-unknown-emscripten` target
110+
inherits the default settings of LLVM which typically, but not necessarily, matches
111+
the default settings of Emscripten as well.
112+
113+
Please refer to the [`wasm32-unknown-unknown`](./wasm32-unknown-unknown.md)
114+
target's documentation on which WebAssembly features Rust enables by default, how
115+
features can be disabled, and how Rust code can be conditionally compiled based on
116+
which features are enabled.

0 commit comments

Comments
 (0)