Skip to content

Commit 89fef8c

Browse files
Added more documentation for bool, char, and numeric types (rustwasm#4245)
1 parent 1f739b6 commit 89fef8c

File tree

5 files changed

+68
-4
lines changed

5 files changed

+68
-4
lines changed

guide/src/reference/types/bool.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
|:---:|:---:|:---:|:---:|:---:|:---:|:---:|
55
| Yes | No | No | Yes | Yes | Yes | A JavaScript boolean value |
66

7+
> **Note**: Only [JavaScript `Boolean`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean) values (`true` or `false`) are supported when calling into Rust. If you want to pass truthy or falsy values to Rust, convert them to a boolean using `Boolean(value)` first.
8+
>
9+
> If you are using TypeScript, you don't have to worry about this, as TypeScript will emit a compiler error if you try to pass a non-`boolean` value.
10+
711
## Example Rust Usage
812

913
```rust

guide/src/reference/types/boxed-number-slices.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
|:---:|:---:|:---:|:---:|:---:|:---:|:---:|
55
| Yes | No | No | Yes | Yes | Yes | A JavaScript `TypedArray` of the appropriate type (`Int32Array`, `Uint8Array`, etc...) |
66

7-
Note that the contents of the slice are copied into the JavaScript `TypedArray`
7+
> **Note:** The contents of the slice are copied into a JavaScript [`TypedArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray)
88
from the Wasm linear memory when returning a boxed slice to JavaScript, and vice
99
versa when receiving a JavaScript `TypedArray` as a boxed slice in Rust.
1010

guide/src/reference/types/char.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
|:---:|:---:|:---:|:---:|:---:|:---:|:---:|
55
| Yes | No | No | Yes | No | No | A JavaScript string value |
66

7+
Since JavaScript doesn't have a character type, `char` is represented as a JavaScript string with one Unicode code point.
8+
9+
> **Note**: [JavaScript strings uses UTF-16 encoding](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#utf-16_characters_unicode_code_points_and_grapheme_clusters). This means that a single `char` may be represented by a string of length 1 or 2 in JavaScript, depending on the Unicode code point. See [`String.fromCodePoint`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint) for more information.
10+
11+
When passed into Rust, the `char` value of a JavaScript string is determined using [`codePointAt(0)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/codePointAt). If the JavaScript string is empty or starts with an unpaired surrogate, a runtime error will be thrown.
12+
13+
> **Note**: For more information about unpaired surrogates, see the [documentation for `str`](str.html).
14+
715
## Example Rust Usage
816

917
```rust

guide/src/reference/types/exported-rust-types.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
|:---:|:---:|:---:|:---:|:---:|:---:|:---:|
55
| Yes | Yes | Yes | Yes | Yes | Yes | Instances of a `wasm-bindgen`-generated JavaScript `class Whatever { ... }` |
66

7-
> **Note**: Public fields implementing Copy have automatically generated getters/setters.
8-
> To generate getters/setters for non-Copy public fields, use #[wasm_bindgen(getter_with_clone)] for the struct
7+
> **Note**: Public fields implementing `Copy` have automatically generated getters/setters.
8+
> To generate getters/setters for non-`Copy` public fields, use `#[wasm_bindgen(getter_with_clone)]` for the struct
99
> or [implement getters/setters manually](https://rustwasm.github.io/wasm-bindgen/reference/attributes/on-rust-exports/getter-and-setter.html).
10+
1011
## Example Rust Usage
1112

1213
```rust

guide/src/reference/types/numbers.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,58 @@
22

33
| `T` parameter | `&T` parameter | `&mut T` parameter | `T` return value | `Option<T>` parameter | `Option<T>` return value | JavaScript representation |
44
|:---:|:---:|:---:|:---:|:---:|:---:|:---:|
5-
| Yes | No | No | Yes | Yes | Yes | A JavaScript number value |
5+
| Yes | No | No | Yes | Yes | Yes | A JavaScript number or bigint value |
6+
7+
[JavaScript `Number`s](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number#number_encoding) are 64-bit floating point value under the hood and cannot accurately represent all of Rust's numeric types. `wasm-bindgen` will automatically use either [`BigInt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) or `Number` to accurately represent Rust's numeric types in JavaScript:
8+
9+
- `u8`, `i8`, `u16`, `i16`, `u32`, `i32`, `isize`, `usize`, `f32`, and `f64` will be represented as `Number` in JavaScript.
10+
- `u64` and `i64` will be represented as `BigInt` in JavaScript.
11+
12+
> **Note**: Wasm is currently a 32-bit architecture, so `isize` and `usize` are 32-bit integers and "fit" into a JavaScript `Number`.
13+
14+
## Converting from JavaScript to Rust
15+
16+
`wasm-bindgen` will automatically handle the conversion of JavaScript numbers to Rust numeric types. The conversion rules are as follows:
17+
18+
### `Number` to `u8`, `i8`, `u16`, `i16`, `u32`, `i32`, `isize`, and `usize`
19+
20+
If the JavaScript number is `Infinity`, `-Infinity`, or `NaN`, then the Rust value will be 0. Otherwise, the JavaScript number will rounded towards zero (see [`Math.trunc`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/trunc) or [`f64::trunc`](https://doc.rust-lang.org/std/primitive.f64.html#method.trunc)). If the rounded number is too large or too small for the target integer type, it will wrap around.
21+
22+
For example, if the target type is `i8`, Rust will see the following values for the following inputs:
23+
24+
| JS input number | Rust value (`i8`) |
25+
| --------------: | :---------------- |
26+
| 42 | 42 |
27+
| -42 | -42 |
28+
| 1.999 | 1 |
29+
| -1.999 | -1 |
30+
| 127 | 127 |
31+
| 128 | -128 |
32+
| 255 | -1 |
33+
| 256 | 0 |
34+
| -0 | 0 |
35+
| `±Infinity` | 0 |
36+
| `NaN` | 0 |
37+
38+
This is the same behavior as assigning the JavaScript `Number` to a [typed array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) of the appropriate integer type in JavaScript, i.e. `new Uint8Array([value])[0]`.
39+
40+
Except for the handling of `Infinity` and `-Infinity`, this is the same behavior as [casting](https://doc.rust-lang.org/reference/expressions/operator-expr.html#numeric-cast) `f64` to the appropriate integer type in Rust, i.e. `value_f64 as u32`.
41+
42+
### `BigInt` to `u64` and `i64`
43+
44+
If the JavaScript `BigInt` is too large or too small for the target integer type, it will wrap around.
45+
46+
This is the same behavior as assigning the JavaScript `BigInt` to a [typed array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) of the appropriate integer type in JavaScript, i.e. `new Int64Array([value])[0]`.
47+
48+
### `Number` to `f32`
49+
50+
The JavaScript `Number` is converted to a Rust `f32` using the same rules as [casting](https://doc.rust-lang.org/reference/expressions/operator-expr.html#numeric-cast) `f64` to `f32` in Rust, i.e. `value_f64 as f32`.
51+
52+
This is the same behavior as [`Math.fround`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/fround) or assigning the JavaScript `Number` to a [`Float32Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float32Array) in JavaScript, i.e. `new Float32Array([value])[0]`.
53+
54+
### `Number` to `f64`
55+
56+
Since JavaScript numbers are 64-bit floating point values, converting a JavaScript `Number` to a Rust `f64` is a no-op.
657

758
## Example Rust Usage
859

0 commit comments

Comments
 (0)