Top Related Projects
A fast and secure runtime for WebAssembly
Optimizer and compiler/toolchain library for WebAssembly
Emscripten: An LLVM-to-WebAssembly Compiler
A TypeScript-like language for WebAssembly.
Facilitating high-level interactions between Wasm modules and JavaScript
Cranelift code generator
Quick Overview
Wasmer is an open-source runtime for WebAssembly, enabling the execution of WebAssembly modules outside of the browser. It allows developers to run WebAssembly binaries on various platforms and integrate them into different programming languages, providing a universal runtime for running WebAssembly anywhere.
Pros
- Cross-platform compatibility: Runs on multiple operating systems and architectures
- Language agnostic: Supports integration with various programming languages
- High performance: Offers near-native speed execution of WebAssembly modules
- Sandboxed execution: Provides a secure environment for running untrusted code
Cons
- Learning curve: Requires understanding of WebAssembly concepts
- Limited ecosystem: Compared to traditional native libraries, fewer resources available
- Potential overhead: May introduce some performance overhead compared to native code in certain scenarios
Code Examples
- Running a WebAssembly module:
use wasmer::{Store, Module, Instance};
use wasmer_compiler_cranelift::Cranelift;
// Load and compile the WebAssembly module
let module = Module::from_file(&Store::new(Cranelift::default()), "path/to/module.wasm")?;
// Instantiate the module
let instance = Instance::new(&module, &imports! {})?;
// Get an exported function and call it
let sum = instance.exports.get_function("sum")?;
let result = sum.call(&[Value::I32(5), Value::I32(37)])?;
println!("Result: {:?}", result);
- Passing memory between host and WebAssembly:
use wasmer::{Memory, MemoryType, Store, Instance, Module};
// Create a memory instance
let memory = Memory::new(&Store::default(), MemoryType::new(1, None, false)).unwrap();
// Pass memory to the WebAssembly instance
let instance = Instance::new(&module, &imports! {
"env" => {
"memory" => memory.clone(),
},
})?;
// Write data to the memory
let data = b"Hello, WebAssembly!";
memory.view()[0..data.len()].copy_from_slice(data);
// Call a WebAssembly function that uses the memory
let process_data = instance.exports.get_function("process_data")?;
process_data.call(&[Value::I32(0), Value::I32(data.len() as i32)])?;
- Using WASI (WebAssembly System Interface):
use wasmer::{Store, Module, Instance};
use wasmer_wasi::WasiState;
// Create a WASI environment
let wasi_env = WasiState::new("command_name")
.args(&["arg1", "arg2"])
.env("KEY", "VALUE")
.finalize()?;
// Instantiate the module with WASI imports
let instance = wasi_env.instantiate(&module)?;
// Get the WASI start function and call it
let start = instance.exports.get_function("_start")?;
start.call(&[])?;
Getting Started
To get started with Wasmer, follow these steps:
- Install Wasmer:
curl https://get.wasmer.io -sSfL | sh
- Create a new Rust project:
cargo new wasmer_example && cd wasmer_example
- Add Wasmer to your
Cargo.toml
:[dependencies] wasmer = "3.0" wasmer-compiler-cranelift = "3.0"
- Use the code examples above to start running WebAssembly modules with Wasmer.
Competitor Comparisons
A fast and secure runtime for WebAssembly
Pros of Wasmtime
- More extensive and active community support
- Better integration with WASI (WebAssembly System Interface)
- Stronger focus on security and sandboxing features
Cons of Wasmtime
- Slightly steeper learning curve for beginners
- Less extensive language support compared to Wasmer
Code Comparison
Wasmtime (Rust):
use wasmtime::*;
let engine = Engine::default();
let module = Module::from_file(&engine, "example.wasm")?;
let store = Store::new(&engine, ());
let instance = Instance::new(&store, &module, &[])?;
Wasmer (Rust):
use wasmer::*;
let store = Store::default();
let module = Module::from_file(&store, "example.wasm")?;
let instance = Instance::new(&module, &imports! {})?;
Both Wasmtime and Wasmer are popular WebAssembly runtimes with their own strengths. Wasmtime offers robust WASI support and a strong security focus, while Wasmer provides broader language support and an easier entry point for beginners. The code examples demonstrate similar usage patterns, with minor differences in API structure and naming conventions.
Optimizer and compiler/toolchain library for WebAssembly
Pros of Binaryen
- More comprehensive toolchain for WebAssembly, including optimization and compilation tools
- Broader language support, including C/C++ and Rust
- Extensive optimization capabilities for WebAssembly modules
Cons of Binaryen
- Primarily focused on compilation and optimization, not runtime execution
- Steeper learning curve due to its broader scope and functionality
- Less emphasis on seamless integration with host languages compared to Wasmer
Code Comparison
Binaryen (C++ API):
Module module;
Expression* expr = module.makeConst(Literal(1337));
Function* func = module.addFunction("get", Signature(), {}, expr);
Wasmer (Rust API):
let module = Module::new(&store, wasm_bytes)?;
let import_object = imports! {};
let instance = Instance::new(&module, &import_object)?;
Summary
Binaryen is a comprehensive WebAssembly toolchain focusing on compilation, optimization, and transformation. It offers broader language support and extensive optimization capabilities. Wasmer, on the other hand, is primarily a WebAssembly runtime that emphasizes easy integration with host languages and provides a more straightforward API for executing WebAssembly modules. While Binaryen excels in the compilation and optimization phase, Wasmer shines in runtime execution and language integration.
Emscripten: An LLVM-to-WebAssembly Compiler
Pros of Emscripten
- Mature and widely adopted toolchain for compiling C/C++ to WebAssembly
- Extensive support for porting existing C/C++ codebases to the web
- Provides a complete runtime environment, including emulated POSIX APIs
Cons of Emscripten
- Larger runtime overhead due to its comprehensive emulation layer
- Primarily focused on web-based WebAssembly use cases
- Steeper learning curve for developers new to WebAssembly
Code Comparison
Emscripten (compiling C to WebAssembly):
#include <stdio.h>
int main() {
printf("Hello, WebAssembly!\n");
return 0;
}
Wasmer (running WebAssembly):
use wasmer::{Store, Module, Instance};
let module = Module::from_file(&store, "hello.wasm")?;
let instance = Instance::new(&module, &imports)?;
let result = instance.exports.get_function("main")?.call(&[])?;
Emscripten focuses on compiling C/C++ to WebAssembly, while Wasmer is a runtime for executing WebAssembly modules across different platforms. Emscripten is better suited for porting existing C/C++ projects to the web, whereas Wasmer provides a more flexible and lightweight solution for running WebAssembly in various environments, including server-side applications.
A TypeScript-like language for WebAssembly.
Pros of AssemblyScript
- Easier learning curve for JavaScript/TypeScript developers
- Compiles to optimized WebAssembly modules
- Provides a familiar development environment with TypeScript-like syntax
Cons of AssemblyScript
- Limited to WebAssembly as the target platform
- Smaller ecosystem and community compared to Wasmer
- Less flexibility in terms of language features and optimizations
Code Comparison
AssemblyScript:
export function add(a: i32, b: i32): i32 {
return a + b;
}
Wasmer (using Rust):
#[no_mangle]
pub extern "C" fn add(a: i32, b: i32) -> i32 {
a + b
}
Key Differences
- AssemblyScript focuses on WebAssembly compilation from TypeScript-like code
- Wasmer is a WebAssembly runtime that can execute modules from various languages
- AssemblyScript is more accessible for web developers, while Wasmer offers broader language support
- Wasmer provides a runtime environment, whereas AssemblyScript is primarily a compiler
Use Cases
- AssemblyScript: Web applications requiring high-performance WebAssembly modules
- Wasmer: Cross-platform applications, serverless functions, and embedding WebAssembly in various environments
Community and Ecosystem
- AssemblyScript has a growing community focused on web development
- Wasmer has a larger ecosystem with support for multiple languages and platforms
Facilitating high-level interactions between Wasm modules and JavaScript
Pros of wasm-bindgen
- Specifically designed for Rust-to-WebAssembly interoperability
- Provides high-level abstractions for working with JavaScript APIs
- Generates TypeScript definitions for improved type safety in JavaScript
Cons of wasm-bindgen
- Limited to Rust-WebAssembly integration, less versatile than Wasmer
- Requires additional tooling and build steps in Rust projects
- May have a steeper learning curve for developers new to WebAssembly
Code Comparison
wasm-bindgen:
#[wasm_bindgen]
pub fn greet(name: &str) -> String {
format!("Hello, {}!", name)
}
Wasmer:
fn greet(name: &str) -> String {
format!("Hello, {}!", name)
}
Summary
wasm-bindgen is tailored for Rust-to-WebAssembly integration, offering seamless JavaScript interoperability and TypeScript support. It excels in web-focused projects but is limited to Rust. Wasmer, on the other hand, is a more versatile WebAssembly runtime that supports multiple languages and environments. While wasm-bindgen requires additional setup, it provides powerful abstractions for web development. Wasmer offers a simpler approach but may require more manual work for JavaScript integration. The choice between them depends on the specific project requirements and target environment.
Cranelift code generator
Pros of Cranelift
- More focused on being a code generator and compiler backend
- Potentially faster compilation times for some use cases
- Designed for use in various projects beyond WebAssembly
Cons of Cranelift
- Less comprehensive WebAssembly runtime features compared to Wasmer
- Narrower scope, focusing primarily on code generation
- May require additional components for a full WebAssembly environment
Code Comparison
Cranelift (function definition):
pub fn define_function(
func: &mut Function,
sig: &Signature,
builder_context: &mut FunctionBuilderContext,
entry_block: Block,
) -> FunctionBuilder {
FunctionBuilder::new(func, builder_context, entry_block)
}
Wasmer (module instantiation):
let module = Module::new(&store, wasm_bytes)?;
let import_object = imports! {};
let instance = Instance::new(&module, &import_object)?;
Both projects serve different purposes within the WebAssembly ecosystem. Cranelift focuses on being a code generator and compiler backend, while Wasmer provides a more comprehensive WebAssembly runtime environment. The choice between them depends on specific project requirements and use cases.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Wasmer is a blazing fast and secure WebAssembly runtime that enables incredibly lightweight containers to run anywhere: from Desktop to the Cloud, Edge and your browser.
- Secure by default. No file, network, or environment access, unless explicitly enabled.
- Pluggable. supports WASIX, WASI and Emscripten out of the box.
- Incredibly Fast. Run WebAssembly at near-native speeds.
- Embeddable anywhere via Wasmer SDKs
Install Wasmer
curl https://get.wasmer.io -sSfL | sh
Other installation options (Powershell, Brew, Cargo, ...)
Wasmer can be installed from various package managers. Choose the one that fits best for your environment:
- Powershell (Windows)
iwr https://win.wasmer.io -useb | iex
-
Homebrew (macOS, Linux)
brew install wasmer
-
Scoop (Windows)
scoop install wasmer
-
Chocolatey (Windows)
choco install wasmer
-
cargo binstall wasmer-cli
-
Note: All the available features are described in the
wasmer-cli
crate docscargo install wasmer-cli
Looking for more installation options? See the
wasmer-install
repository to learn more!
Note: You can also try Wasmer online in wasmer.sh
Quickstart
You can start by running Cowsay:
$ wasmer run cowsay "hello world"
_____________
< hello world >
-------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
There are many more available packages, such as
wasmer/python
orquickjs
. Create your own package, or explore packages from the community: https://wasmer.io/explore
Here is what you can do next:
Wasmer SDK
You can use the Wasmer runtime embedded in different languages with the Wasmer SDK:
Language | Package | Documentation | |
---|---|---|---|
Rust | wasmer Rust crate | Learn | |
C | wasm.h header | Learn | |
C++ | wasm.hh header | Learn | |
C# | WasmerSharp NuGet package | Learn | |
D | wasmer Dub package | Learn | |
Zig | wasmer Zig package | Learn | |
Python | wasmer PyPI package | Learn | |
Javascript | @wasmerio NPM packages | Learn | |
Go | wasmer Go package | Learn | |
PHP | wasm PECL package | Learn | |
Ruby | wasmer Ruby Gem | Learn | |
Java | wasmer/wasmer-jni Bintray package | Learn | |
R | no published package | Learn | |
Postgres | no published package | Learn | |
Swift | no published package | ||
Dart | wasm pub package | ||
Crystal | no published package | Learn | |
Lisp | no published package | ||
Julia | no published package | ||
V | no published package | ||
OCaml | wasmer OCaml package |
Develop
We have different guides to help you develop Wasmer:
Contribute
We appreciate your help! ð
We recommend reading the following guide on how to contribute into a complex project successfully: https://mitchellh.com/writing/contributing-to-complex-projects
Check our docs on how to build Wasmer from source or test your changes.
Community
Wasmer has an amazing community of developers and contributors. Welcome, please join us! ð
README also in: ð¨ð³ ä¸ æ -Chinese ⢠ð©ðª Deutsch-German ⢠ðªð¸ Español-Spanish ⢠ð«ð· Français-French ⢠ð¯ðµ æ¥æ¬ èª -Japanese ⢠ð°ð· íêµì´ -Korean.
Top Related Projects
A fast and secure runtime for WebAssembly
Optimizer and compiler/toolchain library for WebAssembly
Emscripten: An LLVM-to-WebAssembly Compiler
A TypeScript-like language for WebAssembly.
Facilitating high-level interactions between Wasm modules and JavaScript
Cranelift code generator
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot