This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #[wasm_bindgen(start)] | |
| pub fn run() { | |
| log(&format!("Hello, {}!", name())); | |
| let x = MyClass::new(); | |
| assert_eq!(x.number(), 42); | |
| x.set_number(10); | |
| log(&x.render()); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #[wasm_bindgen] | |
| extern "C" { | |
| #[wasm_bindgen(js_namespace = console)] | |
| fn log(s: &str); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extern "C" { | |
| fn name() -> String; | |
| type MyClass; | |
| #[wasm_bindgen(constructor)] | |
| fn new() -> MyClass; | |
| #[wasm_bindgen(method, getter)] | |
| fn number(this: &MyClass) -> u32; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use wasm_bindgen::prelude::*; | |
| #[wasm_bindgen(module = "/defined-in-js.js")] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export function name() { | |
| return 'World'; | |
| } | |
| export class MyClass { | |
| constructor() { | |
| this._number = 42; | |
| } | |
| get number() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export const loadPageInto = (main) => { | |
| main.innerText = 'Video game page'; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| link.addEventListener('click', async (event) => { | |
| event.preventDefault(); | |
| try { | |
| const module = await import(`/${link.dataset.entryModule}.mjs`); | |
| // The module exports a function named `loadPageInto`. | |
| module.loadPageInto(main); | |
| } catch (error) { | |
| main.textContent = error.message; | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <nav> | |
| <a href="books.html" data-entry-module="books">Books</a> | |
| <a href="movies.html" data-entry-module="movies">Movies</a> | |
| <a href="video-games.html" data-entry-module="video-games">Video Games</a> | |
| </nav> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const module = await import(`/${link.dataset.entryModule}.mjs`); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <script type="module"> | |
| import * as module from './utils.mjs'; | |
| module.default(); | |
| // → logs 'Hi from the default export!' | |
| module.doStuff(); | |
| // → logs 'Doing stuff…' | |
| </script> |