You certainly know Docker, but much less Wasmer, a container format based on WebAssembly which compile many programming languages such as GoLang, Rust, C/C++, D, Python, Javascript, PHP, Ruby, Java, R… to universal binaries running in a secure sandbox so you can run it almost everywhere from Desktop to the Cloud and IoT devices, and also embedded in any programming language.

Wasmer is fast and safe, it runs WebAssembly at near-native speed in a fully sandboxed environment. It supports different compilation frameworks to best suit your needs (LLVM, Cranelift…). Wasmer can run on in any platform (macOS, Linux and Windows) and chipset and it’s Standards compliant, as the runtime passes official WebAssembly test suite supporting WASI and Emscripten.

Wasmer runtime can be used as a library embedded in different languages, so you can use WebAssembly anywhere. In the Wasmer-PHP repository there is a a simple program in examples/simple.rs, written in Rust (or any other language that compiles to WebAssembly):

#[no_mangle]
pub extern fn sum(x: i32, y: i32) -> i32 {
    x + y
}

After compilation to WebAssembly, the examples/simple.wasm binary file is generated. Then, we can execute it in PHP (!) with the examples/simple.php file:

$instance = new Wasm\Instance(__DIR__ . '/simple.wasm');

var_dump(
    $instance->sum(5, 37) // 42!
);

And then run it :

$ php -d extension=wasm examples/simple.php
int(42)

Wasmer-PHP repository provides two things : wasmer-php extension, and the Wasm library. The wasmer-php extension provides a raw API around WebAssembly, while the Wasm library is a layer on top of wasmer-php to provide more safety and a more user-friendly API. to compile the entire project run the following commands :

$ just build
$ php -d extension=wasm examples/simple.php

This is a great opportunity not only to have your code running everywhere, but also to have the ability to include in your PHP application any other code written in any of the supported languages securely and fast ! We’ll be back on this with more detailed benchmarks with native code and docker, but so far the extension provides a faster execution than PHP itself. With the nbody benchmark, the wasmer-php is 9.5 times faster than pure PHP according to benchmark available on the repository.

Wasmer is quiet interesting alternative that you should explore, the security and speed of execution that it provides are very intriguing.

More information about Wasmer at https://wasmer.io/, released under an MIT license.

LEAVE A REPLY

Please enter your comment!
Please enter your name here