import { MyApi } from "learn_rust_wasm";
export async function debug_all() {
// console.log('----------------- debug_wasm() -----------------');
// await debug_wasm();
}
export function getApi() {
let api = MyApi.new("lol");
debugger;
return api;
}
let x = getApi();
window.api = x;
console.log(await window.api.get_name());
console.log(await window.api.do_stuff());
web/bootstrap.js
// A dependency graph that contains any wasm must all be imported
// asynchronously. This `bootstrap.js` file does the single async import, so
// that no one else needs to worry about it again.
import("./index.js")
.catch(e => console.error("Error importing `index.js`:", e));
let api = import("learn_rust_wasm");
web/index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Hello wasm-pack!</title>
</head>
<body>
<noscript>This page contains webassembly and javascript content, please enable javascript in your browser.</noscript>
<script src="./bootstrap.js"></script>
</body>
</html>
wasm-pack build; (cd web && npm i && npm run start)
When you go to http://localhost:8080 and then run this, it will fail in this way:
>>> window.api.add_game_to_cart({})
Uncaught Error: missing field `data` at line 1 column 2
__wbindgen_throw learn_rust_wasm_bg.js:537
add_game_to_cart learn_rust_wasm_bg.js:384
<anonymous> debugger eval code:1
[learn_rust_wasm_bg.js:537](webpack://create-wasm-app/pkg/learn_rust_wasm_bg.js)
__wbindgen_throw learn_rust_wasm_bg.js:537
<anonymous> 3456e6f9d69608745039.module.wasm:91904
<anonymous> 3456e6f9d69608745039.module.wasm:65348
add_game_to_cart learn_rust_wasm_bg.js:384
<anonymous> debugger eval code:1
>>> window.api.add_game_to_cart({})
Uncaught Error: recursive use of an object detected which would lead to unsafe aliasing in rust
__wbindgen_throw learn_rust_wasm_bg.js:537
add_game_to_cart learn_rust_wasm_bg.js:384
<anonymous> debugger eval code:1
[learn_rust_wasm_bg.js:537](webpack://create-wasm-app/pkg/learn_rust_wasm_bg.js)
>>> window.api.get_stuff({})
Uncaught Error: recursive use of an object detected which would lead to unsafe aliasing in rust
__wbindgen_throw learn_rust_wasm_bg.js:537
__wbg_adapter_22 learn_rust_wasm_bg.js:216
real learn_rust_wasm_bg.js:201
__wbg_queueMicrotask_118eeb525d584d9a learn_rust_wasm_bg.js:437
__wbg_adapter_49 learn_rust_wasm_bg.js:227
cb0 learn_rust_wasm_bg.js:501
__wbg_new_1d93771b84541aa5 learn_rust_wasm_bg.js:506
get_stuff learn_rust_wasm_bg.js:376
<anonymous> debugger eval code:1
[learn_rust_wasm_bg.js:537](webpack://create-wasm-app/pkg/learn_rust_wasm_bg.js)
Here is a minimal example.
Make a new project. Use these files:
lib.rs:
Cargo.toml
.cargo/config.toml
web/index.js
web/bootstrap.js
web/index.html
web/package.json
web/webpack.config.js
then run:
When you go to
http://localhost:8080
and then run this, it will fail in this way: