jcbhmr/hello-world-wasmer-lib
Public
wasmer run jcbhmr/hello-world-wasmer-lib

"Hello world!" Wasmer library

πŸŸͺ A Rust library compiled to WASM for use with Wasmer

console.log(hello_world_wasmer_lib.add(1, 2));
//=> 3
console.log(hello_world_wasmer_lib.greet("Alan Turing"));
//=> 'Hello Alan Turing!'

const calc = Calculator.new(hello_world_wasmer_lib, 15);
calc.add(5);
console.log(calc.currentValue());
//=> 20
print(hello_world_wasmer_lib.add(1, 2))
#=> 3
print(hello_world_wasmer_lib.greet("Alan Turing"))
#=> 'Hello Alan Turing!'

calc = Calculator.new(hello_world_wasmer_lib, 15)
calc.add(5)
print(calc.current_value())
#=> 20

πŸš€ Quickstart template for Wasmer-based WASM libraries
πŸ¦€ Written in Rust
πŸ“– Follows the Wasmer Pack tutorial series
πŸ“₯ Imports some host functions
🟨 Can be used with JavaScript
🐍 Can be used with Python
πŸ‘©β€βš–οΈ 0BSD licensed template

Installation

Wasmer npm Yarn pnpm pip

You can install this package using npm through Wasmer's npm redistribution CDN using the Wasmer CLI.

wasmer add jcbhmr/hello-world-wasmer-lib --npm

You can also use --yarn or --pnpm to install the package using Yarn or pnpm respectively. See wasmer add --help for more info.

⚠️ wasmer add doesn't support targeting browser runtimes. It uses node:fs/promises readFile().

If you're using Python you can install it using pip:

wasmer add jcbhmr/hello-world-wasmer-lib --pip

Usage

Node.js Deno Bun Python

Imports (in order): fib, io
Exports: hello-world-wasmer-lib

Here's an example of how you would import and use this package using Node.js:

import { bindings } from "@jcbhmr/hello-world-wasmer-lib";
import { Calculator } from "@jcbhmr/hello-world-wasmer-lib/src/bindings/hello_world_wasmer_lib/hello_world_wasmer_lib.js";
import * as io from "./io.js";
import * as fib from "./fib.js";
const hello_world_wasmer_lib = await bindings.hello_world_wasmer_lib(fib, io);

console.log(hello_world_wasmer_lib.add(1, 2));
//=> 3
console.log(hello_world_wasmer_lib.greet("Alan Turing"));
//=> 'Hello Alan Turing!'

const calc = Calculator.new(hello_world_wasmer_lib, 15);
calc.add(5);
console.log(calc.currentValue());
//=> 20

β„Ή To use the Calculator class you need to construct it using Calculator.new(), not new Calculator(). Note that we also need to pass it the exports bindings context.

To use this package with Python this is what you would do:

from hello_world_wasmer_lib import bindings
from hello_world_wasmer_lib.bindings.hello_world_wasmer_lib import Calculator
import .fib
import .io
hello_world_wasmer_lib = bindings.hello_world_wasmer_lib(fib, io)

print(hello_world_wasmer_lib.add(1, 2))
#=> 3
print(hello_world_wasmer_lib.greet("Alan Turing"))
#=> 'Hello Alan Turing!'

calc = Calculator.new(hello_world_wasmer_lib, 15)
calc.add(5)
print(calc.current_value())
#=> 20

Development

Wasmer Rust

You can make sure everything looks good like this:

cargo wasmer --dry-run --debug --out-dir=./out
mv ./out/wasmer.toml ./out/wapm.toml
wasmer-pack js ./out --out-dir=./out/js
wasmer-pack py ./out --out-dir=./out/py

You can get the wasmer-pack CLI via cargo install wasmer-pack-cli. You can install cargo-wasmer via cargo install cargo-wasmer.

πŸŸͺ A Rust library compiled to WASM and distributed with Wasmer


Github
More packages
wasmer/wasmer-hello website
wasmer/wasmer-edge-nuxt website
The WebAssembly interface to wasmer-pack.
Wasmer Compiler
ptitseb avatar
ptitseb
ptitseb avatar
ptitseb
The Wasmer frontend