You've already forked comprehensive-rust
mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-06-16 14:17:34 +02:00
expose-method
This commit is contained in:
@ -301,6 +301,7 @@
|
|||||||
----
|
----
|
||||||
- [WebAssembly basics](webassembly.md)
|
- [WebAssembly basics](webassembly.md)
|
||||||
- [Load a WASM module](webassembly/load-wasm-module.md)
|
- [Load a WASM module](webassembly/load-wasm-module.md)
|
||||||
|
- [Expose a method](webassembly/expose-method.md)
|
||||||
|
|
||||||
# Final Words
|
# Final Words
|
||||||
|
|
||||||
|
@ -87,7 +87,7 @@
|
|||||||
This repository contains the minimum amount of code needed to experiment with WebAssembly. Including a web server to serve the HTML and WASM as well as the javascript boilerplate needed to load WASM.
|
This repository contains the minimum amount of code needed to experiment with WebAssembly. Including a web server to serve the HTML and WASM as well as the javascript boilerplate needed to load WASM.
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
Edit <a href="">server/files/index.mjs</a> to edit the Javascript.
|
Edit <a href="https://github.com/google/comprehensive-rust/tree/main/src/rust-wasm-template/server/files/index.mjs">server/files/index.mjs</a> to edit the Javascript.
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
WASM output: <span id="wasmoutput" />
|
WASM output: <span id="wasmoutput" />
|
||||||
|
23
src/webassembly/expose-method.md
Normal file
23
src/webassembly/expose-method.md
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
# Expose a method
|
||||||
|
|
||||||
|
The first thing you will want to do with WebAssembly is expose your methods to Javascript.
|
||||||
|
This is straightforward using the `#[wasm_bindgen]` procedural macro.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn set_panic_hook() {
|
||||||
|
// Generates better error messages if our code ever panics.
|
||||||
|
//
|
||||||
|
// For more details see
|
||||||
|
// https://github.com/rustwasm/console_error_panic_hook#readme
|
||||||
|
console_error_panic_hook::set_once();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Exposes the `add` method from the previous slide
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn add(a: i32, b: i32) -> i32 {
|
||||||
|
a + b
|
||||||
|
}
|
||||||
|
```
|
@ -1,7 +1,29 @@
|
|||||||
# Load a WASM module
|
# Load a WASM module
|
||||||
|
|
||||||
|
## Commands to run:
|
||||||
|
|
||||||
|
You can compile the basic WASM library provided in [rust-wasm-template](https://github.com/google/comprehensive-rust/tree/main/src/rust-wasm-template) with this command:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
cd src/rust-wasm-template/project
|
||||||
|
|
||||||
|
wasm-pack build --target web && cp -r pkg ../server
|
||||||
|
```
|
||||||
|
|
||||||
|
You can start the web server provided in [rust-wasm-template](https://github.com/google/comprehensive-rust/tree/main/src/rust-wasm-template) with this command:
|
||||||
|
|
||||||
|
|
||||||
|
```shell
|
||||||
|
cd src/rust-wasm-template/server
|
||||||
|
|
||||||
|
cargo run
|
||||||
|
```
|
||||||
|
|
||||||
|
Open the web page on port `8080`. HTML and JS files are provided at [rust-wasm-template/server](https://github.com/google/comprehensive-rust/tree/main/src/rust-wasm-template/server/files).
|
||||||
|
|
||||||
|
## Javascript
|
||||||
|
|
||||||
Once you have compiled your WebAssembly module, you want to call it from your Web application.
|
Once you have compiled your WebAssembly module, you want to call it from your Web application.
|
||||||
This chapter will cover minimum amount of Javascript required to load a WASM module into a Web application.
|
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
// Import the module and the exported method `add`
|
// Import the module and the exported method `add`
|
||||||
|
Reference in New Issue
Block a user