kambo/webassembly

Run web assembly in PHP

v0.1.0-alpha.1 2025-01-11 20:51 UTC

This package is auto-updated.

Last update: 2025-01-11 20:55:24 UTC


README

An quick POC (done in ~3 hours) for running WebAssembly modules using Wasmer.

Latest Version on Packagist Tests Total Downloads

Installation

You can install the package via composer:

composer require kambo/webassembly

Usage

Please see the examples directory for usage examples.

$engine = new Kambo\WebAssembly\Engine();
$store = new Kambo\WebAssembly\Store($engine);

// Example WAT (WebAssembly Text) code
$wat = '(module
    (func $sum (param i32 i32) (result i32)
        local.get 0
        local.get 1
        i32.add)
    (export "sum" (func $sum)))';

$wasmBytes = WasmerFFI::watToWasm($wat);
$module = new Module($store, $wasmBytes);
$instance = new Instance($store, $module);

$args = Collection::from([10, 32], Value::KIND_I32);
$result = $instance->callFunction('sum', $args);
echo $result[0]->asI32(); // Outputs: 42

Current limitations

  • ๐Ÿ”ง Needs FFI extension to be enabled.
  • โš ๏ธ Just a POC, can segfault at any moment.
  • ๐Ÿงน It's probably leaking memory.
  • ๐Ÿ“ฆ Wasmer runtime is bundled with the library - not a good idea for production.
  • โšก Limited support for some WebAssembly features.
  • ๐ŸŽ Does not support Mac OS (I forget password to my Mac ).

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

License

The MIT License (MIT). Please see License File for more information.