create a complete example of a webpage with an entry form for a number and a wasm program that calculates the square of the number entered and then prints the result for the user. the wasm program can be done with assemblyscript.
Assistant
To create a webpage that takes a number input, calculates its square using WebAssembly (WASM) with AssemblyScript, and then displays the result, you'll need the following components:
HTML file: The webpage with an entry form.
AssemblyScript file: The WASM program that calculates the square.
JavaScript glue code: To integrate the WASM module with the webpage.
HTML file: The webpage with an entry form.
AssemblyScript file: The WASM program that calculates the square.
JavaScript glue code: To integrate the WASM module with the webpage.
I'll walk through the steps to create this.
Web Page (HTML + JavaScript)
This webpage will have an input field for entering a number, a button to trigger the calculation, and a space to display the result. It will also load the WebAssembly module and interact with it.
typescriptCopy code// AssemblyScript function to calculate the square of a number
export function square(x: i32): i32 {
return x * x;
}
Compile AssemblyScript to WASM
To compile the AssemblyScript file into WebAssembly, you'll need to have AssemblyScript installed and set up. If you don't have it installed, you can do so by following these steps:
Install AssemblyScript globally using npm:bashCopy codenpm install -g assemblyscript
Create a project folder, then inside the folder, initialize it with:bashCopy codenpm init -y
Install AssemblyScript as a local dependency:bashCopy codenpm install --save-dev assemblyscript
Create a folder for the AssemblyScript code and WASM output:bashCopy codemkdirassembly
Place thesquare.tsfile inside theassemblyfolder.
Compile the AssemblyScript code to WebAssembly:bashCopy codenpx asc assembly/square.ts --target release --outFile square.wasm
graphqlCopy codeproject/
│
├── index.html # Your main webpage
├── square.wasm # The WebAssembly binary (generated from AssemblyScript)
── assembly/
└── square.ts # AssemblyScript code for calculating the square
Running the Web Page
Start a local server in the project directory. You can use any simple HTTP server for this. For example, if you have Python installed, you can run:bashCopy -m http.server 8080
Open your browser and go tohttp://localhost:8080.
You should see the webpage where you can input a number, hit "", and see the result computed using WebAssembly.
Start a local server in the project directory. You can use any simple HTTP server for this. For example, if you have Python installed, you can run:
You should see the webpage where you can input a number, hit "", and see the result computed using WebAssembly.
Summary of How it Works
TheHTML formcollects the user's input.
TheJavaScripthandles the form submission, loads the WebAssembly module, and calls thesquarefunction exported from WASM.
TheAssemblyScriptcode defines thesquarefunction, which is compiled to WebAssembly.
The WebAssembly module is loaded asynchronously in JavaScript and is used to compute the square of the number entered by the user.
TheHTML formcollects the user's input.
TheJavaScripthandles the form submission, loads the WebAssembly module, and calls thesquarefunction exported from WASM.
TheAssemblyScriptcode defines thesquarefunction, which is compiled to WebAssembly.
The WebAssembly module is loaded asynchronously in JavaScript and is used to compute the square of the number entered by the user.
Let me know if you need more details or adjustments!
Cite this conversation
“create a complete example of a webpage with an entry form for a number and a wasm….” Conversation with ChatGPT (version unspecified), from the ShareChat — Chatbot Conversations in the Wild dataset. The Chat Archive, 2026. https://thechatarchive.com/c/01m84vvp4h6rs2wr14120eqarh