wasmerio/wasmer-php

πŸ˜πŸ•ΈοΈ WebAssembly runtime for PHP

PHP

1,050

422 commits

updated Feb 27, 2024

See the code

README

Wasmer logo

Wasmer PHP

Tests Status Nightly Status License

Website β€’ Docs β€’ Slack Channel


A complete and mature WebAssembly runtime for PHP based on Wasmer.

Features

  • 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.

Install

To install the library, follow the classical:

git clone https://github.com/wasmerio/wasmer-php
cd wasmer-php/ext
phpize
./configure --enable-wasmer
make
make test
make install

Note: Wasmer doesn't work on Windows yet.

Examples

Procedural API
<?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 Wasm\Vec\ValType(), new Wasm\Vec\ValType());
$func = wasm_func_new($store, $functype, 'hello_callback');
wasm_functype_delete($functype);

$extern = wasm_func_as_extern($func);
$externs = new Wasm\Vec\Extern([$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 Wasm\Vec\Val());

wasm_store_delete($store);
wasm_engine_delete($engine);
Object-oriented API
<?php

declare(strict_types=1);

use Wasm;

require_once __DIR__.'/../vendor/autoload.php';

$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 Wasm\Vec\ValType(), new Wasm\Vec\ValType());
$func = Wasm\Module\Func::new($store, $functype, 'hello_callback');

$extern = $func->asExtern();
$externs = new Wasm\Vec\Extern([$extern->inner()]);
$instance = Wasm\Module\Instance::new($store, $module, $externs);

$exports = $instance->exports();
$run = $exports[0]->asFunc();

$args = new Wasm\Vec\Val();
$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:

Supported platforms and features

Platforms

PlatformArchitectureStatus
Linuxamd64βœ…
Linuxaarch64❌
Windowsamd64❌
Darwinamd64βœ…
Darwinaarch64❌
PHPStatus
8.0βœ…
7.4❌
7.3❌

Features

Compilers and engines

CompilerStatus
Craneliftβœ…
LLVM❌
Singlepass❌
EngineStatus
Nativeβœ…
JITβœ…
Object File❌

Runtime

ObjectStatus
configβœ…
engineβœ…
storeβœ…

Types

TypeStatus
valtypeβœ…
functypeβœ…
globaltypeβœ…
tabletypeβœ…
memorytypeβœ…
externtypeβœ…
importtypeβœ…
exporttypeβœ…

Objects

ObjectStatus
valβœ…
frameβœ…
trapβœ…
foreignβœ…
moduleβœ…
funcβœ…
globalβœ…
tableπŸ§‘β€πŸ’»
memoryβœ…
externβœ…
instanceβœ…

Misc

FeatureStatus
WATβœ…
WASI❌
Cross Compilation❌

License

The entire project is under the MIT License. Please read the LICENSE file.

php
php-extension
rust
wasm
wasmer
webassembly

Significant stargazers

Peter Kokot

1,192 followers Β· starred Nov 2018

Romain Menke

148 followers Β· starred Nov 2018

Nazar Mokrynskyi

830 followers Β· starred Dec 2018

Niklas Mollenhauer

145 followers Β· starred Jan 2024

wasmerio/wasmer-php

πŸ˜πŸ•ΈοΈ WebAssembly runtime for PHP

PHP

1,050

422 commits

updated Feb 27, 2024

See the code

README

Wasmer logo

Wasmer PHP

Tests Status Nightly Status License

Website β€’ Docs β€’ Slack Channel


A complete and mature WebAssembly runtime for PHP based on Wasmer.

Features

  • 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.

Install

To install the library, follow the classical:

git clone https://github.com/wasmerio/wasmer-php
cd wasmer-php/ext
phpize
./configure --enable-wasmer
make
make test
make install

Note: Wasmer doesn't work on Windows yet.

Examples

Procedural API
<?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 Wasm\Vec\ValType(), new Wasm\Vec\ValType());
$func = wasm_func_new($store, $functype, 'hello_callback');
wasm_functype_delete($functype);

$extern = wasm_func_as_extern($func);
$externs = new Wasm\Vec\Extern([$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 Wasm\Vec\Val());

wasm_store_delete($store);
wasm_engine_delete($engine);
Object-oriented API
<?php

declare(strict_types=1);

use Wasm;

require_once __DIR__.'/../vendor/autoload.php';

$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 Wasm\Vec\ValType(), new Wasm\Vec\ValType());
$func = Wasm\Module\Func::new($store, $functype, 'hello_callback');

$extern = $func->asExtern();
$externs = new Wasm\Vec\Extern([$extern->inner()]);
$instance = Wasm\Module\Instance::new($store, $module, $externs);

$exports = $instance->exports();
$run = $exports[0]->asFunc();

$args = new Wasm\Vec\Val();
$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:

Supported platforms and features

Platforms

PlatformArchitectureStatus
Linuxamd64βœ…
Linuxaarch64❌
Windowsamd64❌
Darwinamd64βœ…
Darwinaarch64❌
PHPStatus
8.0βœ…
7.4❌
7.3❌

Features

Compilers and engines

CompilerStatus
Craneliftβœ…
LLVM❌
Singlepass❌
EngineStatus
Nativeβœ…
JITβœ…
Object File❌

Runtime

ObjectStatus
configβœ…
engineβœ…
storeβœ…

Types

TypeStatus
valtypeβœ…
functypeβœ…
globaltypeβœ…
tabletypeβœ…
memorytypeβœ…
externtypeβœ…
importtypeβœ…
exporttypeβœ…

Objects

ObjectStatus
valβœ…
frameβœ…
trapβœ…
foreignβœ…
moduleβœ…
funcβœ…
globalβœ…
tableπŸ§‘β€πŸ’»
memoryβœ…
externβœ…
instanceβœ…

Misc

FeatureStatus
WATβœ…
WASI❌
Cross Compilation❌

License

The entire project is under the MIT License. Please read the LICENSE file.

php
php-extension
rust
wasm
wasmer
webassembly

Significant stargazers

Peter Kokot

1,192 followers Β· starred Nov 2018

Romain Menke

148 followers Β· starred Nov 2018

Nazar Mokrynskyi

830 followers Β· starred Dec 2018

Niklas Mollenhauer

145 followers Β· starred Jan 2024

Languages

PHP

52.1%

C

44.1%

Makefile

1.8%

WebAssembly

1.1%