A complete and mature WebAssembly runtime for PHP based on Wasmer.
Easy to use: The wasmer API mimics the standard WebAssembly C API,
Fast: wasmer executes the WebAssembly modules as fast as possible, close to native speed,
Safe: All calls to WebAssembly will be fast, but more importantly, completely safe and sandboxed.
To install the library, follow the classical:
git clone https://github.com/wasmerio/wasmer-phpcd wasmer-php/ext phpize ./configure --enable-wasmer make make testmake install
Note: Wasmer doesn't work on Windows yet.
<?php declare(strict_types=1);$engine = wasm_engine_new();$store = wasm_store_new($engine);$wasm = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'hello.wasm');$module = wasm_module_new($store, $wasm);function hello_callback() {echo 'Calling back...' . PHP_EOL;echo '> Hello World!' . PHP_EOL;return null;
}$functype = wasm_functype_new(new WasmVecValType(), new WasmVecValType());$func = wasm_func_new($store, $functype, 'hello_callback');wasm_functype_delete($functype);$extern = wasm_func_as_extern($func);$externs = new WasmVecExtern([$extern]);$instance = wasm_instance_new($store, $module, $externs);wasm_func_delete($func);$exports = wasm_instance_exports($instance);$run = wasm_extern_as_func($exports[0]);wasm_module_delete($module);wasm_instance_delete($instance);$results = wasm_func_call($run, new WasmVecVal());wasm_store_delete($store);wasm_engine_delete($engine);<?phpdeclare(strict_types=1);use Wasm;require_once __DIR__.'/../vendor/autoload.php';$engine = WasmEngine::new();$store = WasmStore::new($engine);$wasm = file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'hello.wasm');$module = WasmModule::new($store, $wasm);function hello_callback()
{echo 'Calling back...'.PHP_EOL;echo '> Hello World!'.PHP_EOL;return null;
}$functype = WasmFunctype::new(new WasmVecValType(), new WasmVecValType());$func = WasmModuleFunc::new($store, $functype, 'hello_callback');$extern = $func->asExtern();$externs = new WasmVecExtern([$extern->inner()]);$instance = WasmModuleInstance::new($store, $module, $externs);$exports = $instance->exports();$run = $exports[0]->asFunc();$args = new WasmVecVal();$results = $run($args);This example covers the most basic Wasm use case: we take a Wasm module (in its text representation form), create an instance from it, get an exported function and run it.
You can go through more advanced examples in the dedicated directories:
Procedural API
Object-oriented API
| Platform | Architecture | Status |
|---|---|---|
| Linux | amd64 | |
| Linux | aarch64 | |
| Windows | amd64 | |
| Darwin | amd64 | |
| Darwin | aarch64 |
| PHP | Status |
|---|---|
| 8.0 | |
| 7.4 | |
| 7.3 |
| Compiler | Status |
|---|---|
| Cranelift | |
| LLVM | |
| Singlepass |
| Engine | Status |
|---|---|
| Native | |
| JIT | |
| Object File |
| Object | Status |
|---|---|
| config | |
| engine | |
| store |
| Type | Status |
|---|---|
| valtype | |
| functype | |
| globaltype | |
| tabletype | |
| memorytype | |
| externtype | |
| importtype | |
| exporttype |
| Object | Status |
|---|---|
| val | |
| frame | |
| trap | |
| foreign | |
| module | |
| func | |
| global | |
| table | |
| memory | |
| extern | |
| instance |
| Feature | Status |
|---|---|
| WAT | |
| WASI | |
| Cross Compilation |
The entire project is under the MIT License. Please read theLICENSE file.