diff options
| author | Brian Cully <bjc@spork.org> | 2025-08-07 12:05:51 -0400 |
|---|---|---|
| committer | Brian Cully <bjc@spork.org> | 2025-08-07 12:05:51 -0400 |
| commit | 58f6fbe0afb828571463a22737603a037b059909 (patch) | |
| tree | 5a2027883c4970f1876164b9e45d0691988ccf66 | |
| parent | b2e411de770a437dbdf31408b877e2952759e57a (diff) | |
| download | addem-rust-wasm-58f6fbe0afb828571463a22737603a037b059909.tar.gz addem-rust-wasm-58f6fbe0afb828571463a22737603a037b059909.zip | |
add imports from javascript
‘alert’ and ‘console.debug’
| -rw-r--r-- | index.html | 2 | ||||
| -rw-r--r-- | main.mjs | 6 | ||||
| -rw-r--r-- | src/lib.rs | 23 |
3 files changed, 22 insertions, 9 deletions
@@ -13,6 +13,8 @@ result: <span id='result'></span> </p> + <button onclick='testalert()'>alert</button> + <script src='./main.mjs' type='module'></script> </body> </html> @@ -4,6 +4,7 @@ async function run() { console.debug('run'); const foo = await init(); console.debug('init done', foo); + window.calculate = _ => { console.debug('calc'); const inp1 = document.getElementById('number-input1').value; @@ -11,6 +12,11 @@ async function run() { const res = foo.add(parseInt(inp1), parseInt(inp2)); document.getElementById('result').textContent = res; } + + window.testalert = _ => { + console.debug('testalert'); + foo.test_alert('from js'); + } } document.addEventListener('DOMContentLoaded', run); @@ -1,17 +1,22 @@ use wasm_bindgen::prelude::wasm_bindgen; #[wasm_bindgen] +extern { + fn alert(s: &str); + + // set the namespace so we can use ‘console.debug’ + #[wasm_bindgen(js_namespace = console)] + fn debug(s: &str); +} + +#[wasm_bindgen] pub fn add(left: u32, right: u32) -> u32 { + debug(&format!("adding the numbers {} and {}", + left, right)); left + right } -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); - } +#[wasm_bindgen] +pub fn test_alert(s: &str) { + alert(s) } |
