AndrewSC208 / wasm_hello_world

learning how wasm works

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

wasm_hello_world

A small litte tutorial to start learning rust and WebAssembly

Setup

  1. install wasm-pack
cargo install wasm-pack
  1. Since we will be publishing a new npm module, we will need to signin to npm
npm adduser

Tutorial

  1. Create new lib
cargo new --lib hello-wasm
  1. Add code
# src/lib.rs
extern crate wasm_bindgen;

use wasm_bindgen::prelude::*;

#[wasm_bindgen]
extern {
    pub fn alert(s: &str);
}

#[wasm_bindgen]
pub fn greet(name: &str) {
    alert(&format!("Hello, {}!", name));
}
[package]
name = "hello-wasm"
version = "0.1.0"
authors = ["Your Name <you@example.com>"]
description = "A sample project with wasm-pack"
license = "MIT/Apache-2.0"
repository = "https://github.com/yourgithubusername/hello-wasm"

[lib]
crate-type = ["cdylib"]

[dependencies]
wasm-bindgen = "0.2"
  1. Build Package
wasm-pack build --scope mynpmusername
  1. Publish Package
cd pkg
npm publish --access=public

Test

I am not going to review how to build the web server, or anything involved on that side of things. So you can run npm install then npm run serve to run the demo and navigate to localhost.8080 to test out the alert function.

About

learning how wasm works


Languages

Language:Rust 45.2%Language:JavaScript 35.0%Language:HTML 19.8%