Implement wasm bindgen initial version
This commit is contained in:
parent
0edb09c632
commit
7b88580fe7
@ -5,11 +5,16 @@
|
||||
<title>hello-wasm example</title>
|
||||
</head>
|
||||
<body>
|
||||
<script type="module">
|
||||
import init, { greet } from "./pkg/this_algorithm_wasm.js";
|
||||
init().then(() => {
|
||||
greet("WebAssembly");
|
||||
});
|
||||
<input id="address-lat" type="text"></input>
|
||||
<input id="address-lon" type="text"></input>
|
||||
<p id="address-result">(None)</p>
|
||||
<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>
|
||||
</body>
|
||||
</html>
|
||||
|
23
this_algorithm-wasm/index.js
Normal file
23
this_algorithm-wasm/index.js
Normal 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();
|
||||
});
|
@ -1,11 +1,21 @@
|
||||
use this_algorithm::Address;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[wasm_bindgen]
|
||||
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]
|
||||
pub fn greet(name: &str) {
|
||||
alert(&format!("Hello, {}!", name));
|
||||
pub fn address_lat_lon(lat: f64, lon: f64) -> Result<String, String> {
|
||||
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>< {
|
||||
|
||||
// }
|
||||
|
Loading…
Reference in New Issue
Block a user