Implement wasm bindgen initial version

This commit is contained in:
Austen Adler 2023-02-25 14:12:18 -05:00
parent 0edb09c632
commit 7b88580fe7
3 changed files with 46 additions and 8 deletions

View File

@ -5,11 +5,16 @@
<title>hello-wasm example</title> <title>hello-wasm example</title>
</head> </head>
<body> <body>
<script type="module"> <input id="address-lat" type="text"></input>
import init, { greet } from "./pkg/this_algorithm_wasm.js"; <input id="address-lon" type="text"></input>
init().then(() => { <p id="address-result">(None)</p>
greet("WebAssembly"); <script>
}); const addressResult = document.getElementById("address-result");
const addressLat = document.getElementById("address-lat");
const addressLon = document.getElementById("address-lon");
var lta;
</script>
<script type="module" src="index.js">
</script> </script>
</body> </body>
</html> </html>

View File

@ -0,0 +1,23 @@
// import init, { log_to_address, greet } from "./pkg/this_algorithm_wasm.js";
// init().then(() => {
// console.log(greet());
// });
import init, { address_lat_lon } from "./pkg/this_algorithm_wasm.js";
init().then(() => {
let update = () => {
let lat = parseFloat(addressLat.value);
let lon = parseFloat(addressLon.value);
if (!(lat || lat == 0.0) && !(lon || lon == 0.0)) {
return;
}
let ret = address_lat_lon(lat, lon);
addressResult.textContent = ret;
};
addressLat.addEventListener("input", update);
addressLon.addEventListener("input", update);
update();
});

View File

@ -1,11 +1,21 @@
use this_algorithm::Address;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
#[wasm_bindgen] #[wasm_bindgen]
extern "C" { extern "C" {
pub fn alert(s: &str); // fn alert(s: &str);
#[wasm_bindgen(js_namespace = console, js_name = log)]
fn log_str(s: &str);
} }
#[wasm_bindgen] #[wasm_bindgen]
pub fn greet(name: &str) { pub fn address_lat_lon(lat: f64, lon: f64) -> Result<String, String> {
alert(&format!("Hello, {}!", name)); Address::from_lat_lon(lat, lon)
.map(|a| a.to_string())
.map_err(|e| e.to_string())
} }
// #[wasm_bindgen]
// pub fn to_lat_lon(address: &str) -> Result>< {
// }