Ivan Enderlin announced today a new experimental PHP-ext-WASM extension that will let you natively execute WASM binary in PHP through Rust. The code is more a proof of concept, but more fun could be coming like import functions and import memory !

For example the code below in Rust :

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

By compiling this code to WASM using just compile-toy will give you a binary file toy.wasm

This code could be then called from PHP using :

<?php
require_once dirname(__DIR__) . '/lib/WASM.php';
$instance = new WASM\Instance(__DIR__ . '/toy.wasm');
$result = $instance->sum(5, 37);
var_dump($result);

Then you can run it using :

$ php -d extension=wasm tests/toy.php
int(42)

Notice that it’s a “6 hours project, so be nice with the existing code please” tweeted Ivan.

Released under a BSD-3-clause license, more information at https://github.com/Hywan/php-ext-wasm

LEAVE A REPLY

Please enter your comment!
Please enter your name here