guide: Add working with pointers example to types section

This commit is contained in:
Nick Fitzgerald 2018-08-14 15:24:43 -07:00
parent 79f9d966e5
commit 602b63c1b0
4 changed files with 35 additions and 0 deletions

View File

@ -0,0 +1,12 @@
import {
take_pointer_by_value,
return_pointer,
} from './guide_supported_types_examples';
import { memory } from './guide_supported_types_examples_bg';
let ptr = return_pointer();
let buf = new Uint8Array(memory.buffer);
let value = buf[ptr];
console.log(`The byte at the ${ptr} address is ${value}`);
take_pointer_by_value(ptr);

View File

@ -11,3 +11,4 @@ pub mod char;
pub mod bool;
pub mod js_value;
pub mod boxed_js_value_slice;
pub mod pointers;

View File

@ -0,0 +1,10 @@
use std::ptr;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn take_pointer_by_value(x: *mut u8) {}
#[wasm_bindgen]
pub fn return_pointer() -> *mut u8 {
ptr::null_mut()
}

View File

@ -163,6 +163,18 @@ garbage-collected heap and the Wasm linear memory with `TextDecoder` and
|:---:|:---:|:---:|:---:|:---:|:---:|:---:|
| Yes | No | No | Yes | No | No | A JavaScript number value |
### Example Rust Usage
```rust
{{#include ../../../examples/guide-supported-types-examples/src/pointers.rs}}
```
### Example JavaScript Usage
```js
{{#include ../../../examples/guide-supported-types-examples/pointers.js}}
```
## `u8` `i8` `u16` `i16` `u64` `i64` `isize` `size`
| `T` parameter | `&T` parameter | `&mut T` parameter | `T` return value | `Option<T>` parameter | `Option<T>` return value | JavaScript representation |