I was trying to create my web assembly module using Rust`s wasm-pack and use it within the .html file. I tried to use replit.com to generate the .wasm .
I created a new blank repl, installed rustup and wasm-pack, added wasm32-unknown-unknown target, ran cargo init
, created src/lib.rs and put this code in there:
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn sum_3(p1: u8, p2: u8, p3: u8) -> u8 {
p1 + p2 + p3
}
Here is how my Cargo.toml file looked like:
[package]
name = "test"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
[dependencies]
target = "2.1.0"
wasm-bindgen = "0.2.92"
Then I ran these two commands:
rustup default stable
wasm-pack build --target web
I got the wasm file and put it into the new folder where this html was located too:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Wasm</title>
</head>
<body>
<script>
function process(e) {
}
WebAssembly.instantiateStreaming(fetch("./my.wasm", {mode: 'no-cors'}), {}).then(result => {
process(result.instance.exports);
})
</script>
</body>
</html>
The html was served with the running of this python file:
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
app = FastAPI()
app.mount("/", StaticFiles(directory="myf"), name="stat")
with this command: fastapi dev server.py
.
After opening html in the browser with the console I saw: Uncaught (in promise) CompileError: wasm validation error: at offset 184: function body too big
.
The .wasm file weights like 13kb.
Here is the link to my repl: https://replit.com/@benjaminaugustb/testnew. You can fork it and test everything yourself.
Here is the link to a zip with the folder I served: https://github.com/user-attachments/files/16354465/myf.zip.
Am I doing something wrong? Does web assembly js api have any limits related to size?