fixed wasm

This commit is contained in:
dongho
2024-09-03 14:50:27 +09:00
parent 5cadfc22da
commit f16a721d2d
8 changed files with 87 additions and 1197 deletions

Binary file not shown.

3
Cargo.lock generated
View File

@ -177,9 +177,12 @@ checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
name = "ekstrahMap"
version = "0.1.0"
dependencies = [
"console_error_panic_hook",
"console_log",
"js-sys",
"osmpbf",
"wasm-bindgen",
"web-sys",
"yew",
]

View File

@ -8,6 +8,18 @@ console_log = "1.0.0"
osmpbf = "0.3.4"
wasm-bindgen = "0.2.93"
yew = { git = "https://github.com/yewstack/yew/", features = ["csr"] }
console_error_panic_hook = "0.1.7"
js-sys = "0.3.70"
[dependencies.web-sys]
version = "0.3.4"
features = [
'Document',
'Element',
'HtmlElement',
'Node',
'Window',
]
[lib]
crate-type = ["cdylib", "rlib"]

View File

@ -1,13 +1,56 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Yew App</title>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
</head>
<body>
<!-- Note the usage of `type=module` here as this is an ES6 module -->
<script type="module">
import init from "yew-wasm/pkg/yew-wasm.js";
init();
// Use ES module import syntax to import functionality from the module
// that we have compiled.
//
// Note that the `default` import is an initialization function which
// will "boot" the module and make it ready to use. Currently browsers
// don't support natively imported WebAssembly as an ES module, but
// eventually the manual initialization won't be required!
import init, { add } from './pkg/ekstrahMap.js';
async function run() {
// First up we need to actually load the wasm file, so we use the
// default export to inform it where the wasm file is located on the
// server, and then we wait on the returned promise to wait for the
// wasm to be loaded.
//
// It may look like this: `await init('./pkg/without_a_bundler_bg.wasm');`,
// but there is also a handy default inside `init` function, which uses
// `import.meta` to locate the wasm file relatively to js file.
//
// Note that instead of a string you can also pass in any of the
// following things:
//
// * `WebAssembly.Module`
//
// * `ArrayBuffer`
//
// * `Response`
//
// * `Promise` which returns any of the above, e.g. `fetch("./path/to/wasm")`
//
// This gives you complete control over how the module is loaded
// and compiled.
//
// Also note that the promise, when resolved, yields the wasm module's
// exports which is the same as importing the `*_bg` module in other
// modes
await init();
// And afterwards we can use all the functionality defined in wasm.
const result = add(1, 2);
console.log(`1 + 2 = ${result}`);
if (result !== 3)
throw new Error("wasm addition doesn't work!");
}
run();
</script>
</body>
</html>

24
src/lib.rs Normal file
View File

@ -0,0 +1,24 @@
use wasm_bindgen::prelude::*;
// Called when the wasm module is instantiated
#[wasm_bindgen(start)]
fn main() -> Result<(), JsValue> {
// Use `web_sys`'s global `window` function to get a handle on the global
// window object.
let window = web_sys::window().expect("no global `window` exists");
let document = window.document().expect("should have a document on window");
let body = document.body().expect("document should have a body");
// Manufacture the element we're gonna append
let val = document.create_element("p")?;
val.set_inner_html("Hello from Rust!");
body.append_child(&val)?;
Ok(())
}
#[wasm_bindgen]
pub fn add(a: u32, b: u32) -> u32 {
a + b
}

1173
yew-wasm/Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,13 +0,0 @@
[package]
name = "yew-wasm"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
[dependencies]
console_error_panic_hook = "0.1.7"
console_log = "1.0.0"
wasm-bindgen = "0.2.93"
yew = { git = "https://github.com/yewstack/yew/", features = ["csr"] }

View File

@ -1,6 +0,0 @@
use wasm_bindgen::prelude::*;
#[wasm_bindgen(start)]
fn start() {
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
}