Skip to content

Commit

Permalink
feat: More types implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
jubianchi committed Feb 18, 2021
1 parent 4dadeca commit 65aad69
Show file tree
Hide file tree
Showing 18 changed files with 461 additions and 71 deletions.
2 changes: 2 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

$finder = PhpCsFixer\Finder::create()
->notName('wasmer_vec.stub.php')
->in('examples')
->in('ext/examples')
->in('ext/src')
->in('src')
;
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ unit: vendor/atoum/atoum/bin/atoum
lint: vendor/friendsofphp/php-cs-fixer/php-cs-fixer
$(PHP_EXECUTABLE) $< fix --dry-run --allow-risky=yes

.phpdoc/build/index.html: vendor/phpdocumentor/phpdocumentor/bin/phpdoc ext/src/wasmer_*.stub.php src/*.php
$(PHP_EXECUTABLE) $<
.phpdoc/build/index.html: vendor/phpdocumentor/phpdocumentor/bin/phpdoc phpdoc.dist.xml ext/src/wasmer_*.stub.php src/*.php src/Exception/*.php src/Type/*.php
$<

vendor/phpdocumentor/phpdocumentor/bin/phpdoc vendor/friendsofphp/php-cs-fixer/php-cs-fixer vendor/atoum/atoum/bin/atoum: composer.lock
composer install
Expand Down
8 changes: 4 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 24 additions & 20 deletions ext/examples/callback.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
<?php declare(strict_types=1);
<?php

echo 'Initializing...' . PHP_EOL;
declare(strict_types=1);

echo 'Initializing...'.PHP_EOL;
$engine = wasm_engine_new();
$store = wasm_store_new($engine);

echo 'Loading WAT...' . PHP_EOL;
$wat = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . basename(__FILE__, '.php') . '.wat');
echo 'Loading WAT...'.PHP_EOL;
$wat = file_get_contents(__DIR__.DIRECTORY_SEPARATOR.basename(__FILE__, '.php').'.wat');

echo 'Loading binary...' . PHP_EOL;
echo 'Loading binary...'.PHP_EOL;
$wasm = wat2wasm($wat);

echo 'Compiling module...' . PHP_EOL;
echo 'Compiling module...'.PHP_EOL;
$module = wasm_module_new($store, $wasm);

echo 'Creating callback...' . PHP_EOL;
function print_callback(int $i32): int {
echo 'Calling back...' . PHP_EOL . '> ' . $i32 . PHP_EOL;
echo 'Creating callback...'.PHP_EOL;
function print_callback(int $i32): int
{
echo 'Calling back...'.PHP_EOL.'> '.$i32.PHP_EOL;

assert($i32 === 7);
assert(7 === $i32);

return $i32;
}

function closure(): int {
echo 'Calling back closure...' . PHP_EOL;
function closure(): int
{
echo 'Calling back closure...'.PHP_EOL;

return 42;
}
Expand All @@ -42,33 +46,33 @@ function closure(): int {
$closure = wasm_func_new($store, $closureType, 'closure');
wasm_functype_delete($closureType);

echo 'Instantiating module...' . PHP_EOL;
echo 'Instantiating module...'.PHP_EOL;
$externs = new Wasm\Vec\Extern([wasm_func_as_extern($print), wasm_func_as_extern($closure)]);
$instance = wasm_instance_new($store, $module, $externs);

wasm_func_delete($print);
wasm_func_delete($closure);

echo 'Extracting export...' . PHP_EOL;
echo 'Extracting export...'.PHP_EOL;
$exports = wasm_instance_exports($instance);
$run = wasm_extern_as_func($exports[0]);

wasm_module_delete($module);
wasm_instance_delete($instance);

echo 'Calling export...' . PHP_EOL;
echo 'Calling export...'.PHP_EOL;
$args = new Wasm\Vec\Val([
wasm_val_i32(3),
wasm_val_i32(4),
]);

$results = wasm_func_call($run, $args);

echo 'Printing result...' . PHP_EOL;
echo 'Printing result...'.PHP_EOL;
$result = wasm_val_value($results[0]);
echo '> ' . $result . PHP_EOL;
assert($result === 49);
echo '> '.$result.PHP_EOL;
assert(49 === $result);

echo 'Shutting down...' . PHP_EOL;
echo 'Shutting down...'.PHP_EOL;
wasm_store_delete($store);
wasm_engine_delete($engine);
wasm_engine_delete($engine);
29 changes: 16 additions & 13 deletions ext/examples/hello.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
<?php declare(strict_types=1);
<?php

echo 'Initializing...' . PHP_EOL;
declare(strict_types=1);

echo 'Initializing...'.PHP_EOL;
$engine = wasm_engine_new();
$store = wasm_store_new($engine);

echo 'Loading binary...' . PHP_EOL;
$wasm = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'hello.wasm');
echo 'Loading binary...'.PHP_EOL;
$wasm = file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'hello.wasm');

echo 'Compiling module...' . PHP_EOL;
echo 'Compiling module...'.PHP_EOL;
$module = wasm_module_new($store, $wasm);

echo 'Creating callback...' . PHP_EOL;
function hello_callback() {
echo 'Calling back...' . PHP_EOL;
echo '> Hello World!' . PHP_EOL;
echo 'Creating callback...'.PHP_EOL;
function hello_callback()
{
echo 'Calling back...'.PHP_EOL;
echo '> Hello World!'.PHP_EOL;

return null;
}
Expand All @@ -22,24 +25,24 @@ function hello_callback() {
$func = wasm_func_new($store, $functype, 'hello_callback');
wasm_functype_delete($functype);

echo 'Instantiating module...' . PHP_EOL;
echo 'Instantiating module...'.PHP_EOL;
$extern = wasm_func_as_extern($func);
$externs = new Wasm\Vec\Extern([$extern]);
$instance = wasm_instance_new($store, $module, $externs);

wasm_func_delete($func);

echo 'Extracting export...' . PHP_EOL;
echo 'Extracting export...'.PHP_EOL;
$exports = wasm_instance_exports($instance);
$run = wasm_extern_as_func($exports[0]);

wasm_module_delete($module);
wasm_instance_delete($instance);

echo 'Calling export...' . PHP_EOL;
echo 'Calling export...'.PHP_EOL;
$args = new Wasm\Vec\Val();
$results = wasm_func_call($run, $args);

echo 'Shutting down...' . PHP_EOL;
echo 'Shutting down...'.PHP_EOL;
wasm_store_delete($store);
wasm_engine_delete($engine);
8 changes: 6 additions & 2 deletions phpdoc.dist.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
configVersion="3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://www.phpdoc.org"
xsi:noNamespaceSchemaLocation="https://docs.phpdoc.org/latest/phpdoc.xsd"
xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/phpDocumentor/phpDocumentor/master/data/xsd/phpdoc.xsd"
>
<title>Wasmer PHP</title>
<version number="1.0.0-beta1">
<api>
<api format="php">
<source dsn=".">
<path>ext/src</path>
<path>src</path>
</source>
<visibility>public</visibility>
<markers>
<marker>TODO</marker>
</markers>
</api>
</version>
</phpdocumentor>
59 changes: 55 additions & 4 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Wasm;

/**
* @api
*/
final class Config
{
public const COMPILER_CRANELIFT = WASM_COMPILER_CRANELIFT;
Expand All @@ -17,15 +20,31 @@ final class Config
private static $engines = [self::ENGINE_JIT, self::ENGINE_NATIVE, self::ENGINE_OBJECT_FILE];

/**
* @var resource
* @var resource The inner `wasm_config_t` resource
*/
private $inner;

public function __construct()
/**
* Create a Wasm\Config from a `wasm_config_t` resource.
*
* @param $config ?resource a `wasm_config_t` resource
*
* @throw Exception\InvalidArgumentException If the `$config` argument is not a valid `wasm_config_t` resource
*/
public function __construct($config = null)
{
$this->inner = \wasm_config_new();
$config = $config ?? \wasm_config_new();

if (false === is_resource($config) || 'wasm_config_t' !== get_resource_type($config)) {
throw new Exception\InvalidArgumentException();
}

$this->inner = $config;
}

/**
* @ignore
*/
public function __destruct()
{
try {
Expand All @@ -38,13 +57,20 @@ public function __destruct()
}

/**
* @return resource
* Return the inner config resource.
*
* @return resource A `wasm_config_t` resource
*/
public function inner()
{
return $this->inner;
}

/**
* Set the compiler for the current configuration.
*
* @throw Exception\InvalidArgumentException If the `$compiler` is not a valid compiler
*/
public function setCompiler(int $compiler): bool
{
if (false === in_array($compiler, self::$compilers, true)) {
Expand All @@ -54,6 +80,11 @@ public function setCompiler(int $compiler): bool
return \wasm_config_set_compiler($this->inner, $compiler);
}

/**
* Set the engine for the current configuration.
*
* @throw Exception\InvalidArgumentException If the `$engine` is not a valid compiler
*/
public function setEngine(int $engine): bool
{
if (false === in_array($engine, self::$engines, true)) {
Expand All @@ -62,4 +93,24 @@ public function setEngine(int $engine): bool

return \wasm_config_set_engine($this->inner, $engine);
}

/**
* @api
*
* @throw Exception\InvalidArgumentException If the `$compiler` or `$engine` arguments are not valid, respectively,compilers and engines.
*/
public static function new(?int $compiler = null, ?int $engine = null): self
{
$config = new self(\wasm_config_new());

if (null !== $compiler) {
$config->setCompiler($compiler);
}

if (null !== $engine) {
$config->setEngine($engine);
}

return $config;
}
}
26 changes: 24 additions & 2 deletions src/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,28 @@

namespace Wasm;

/**
* @api
*/
final class Engine
{
/**
* @var resource
*/
private $inner;

public function __construct(?Config $config = null)
/**
* @param ?resource $engine
*/
public function __construct($engine = null)
{
$this->inner = null === $config ? \wasm_engine_new() : \wasm_engine_new_with_config($config->inner());
$engine = $engine ?? \wasm_engine_new();

if (false === is_resource($engine) || 'wasm_engine_t' !== get_resource_type($engine)) {
throw new Exception\InvalidArgumentException();
}

$this->inner = $engine;
}

public function __destruct()
Expand All @@ -34,4 +46,14 @@ public function inner()
{
return $this->inner;
}

/**
* @api
*
* @throw Exception\InvalidArgumentException If the `$kind` is not a valid value kind
*/
public static function new(?Config $config = null): self
{
return new self($config ? \wasm_engine_new_with_config($config->inner()) : \wasm_engine_new());
}
}
3 changes: 3 additions & 0 deletions src/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Wasm;

/**
* @api
*/
final class Store
{
/**
Expand Down
Loading

0 comments on commit 65aad69

Please sign in to comment.