Skip to content

Commit

Permalink
Merge pull request #141 from wasmerio/change-ns
Browse files Browse the repository at this point in the history
fix: Move everything from Wasm\Module to Wasm\
  • Loading branch information
jubianchi authored Feb 23, 2021
2 parents 89094bb + f5d9724 commit c94931f
Show file tree
Hide file tree
Showing 18 changed files with 143 additions and 160 deletions.
4 changes: 2 additions & 2 deletions examples/errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
$module = Wasm\Module::new($store, $wasmBytes);

echo 'Instantiating module...'.PHP_EOL;
$instance = Wasm\Module\Instance::new($store, $module);
$instance = Wasm\Instance::new($store, $module);

// Extracting export...
$exports = $instance->exports();
$divByZero = (new Wasm\Module\Extern($exports[0]))->asFunc();
$divByZero = (new Wasm\Extern($exports[0]))->asFunc();

echo 'Calling `div_by_zero` function...'.PHP_EOL;

Expand Down
10 changes: 5 additions & 5 deletions examples/exports-function.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@
$module = Wasm\Module::new($store, $wasmBytes);

echo 'Instantiating module...'.PHP_EOL;
$instance = Wasm\Module\Instance::new($store, $module);
$instance = Wasm\Instance::new($store, $module);

// Extracting export...
$exports = $instance->exports();
$sum = (new Wasm\Module\Extern($exports[0]))->asFunc();
$sum = (new Wasm\Extern($exports[0]))->asFunc();

$firstArg = Wasm\Module\Val::newI32(1);
$secondArg = Wasm\Module\Val::newI32(2);
$firstArg = Wasm\Val::newI32(1);
$secondArg = Wasm\Val::newI32(2);
$args = new Wasm\Vec\Val([$firstArg->inner(), $secondArg->inner()]);

echo 'Calling `sum` function...'.PHP_EOL;
$result = $sum($args);

echo 'Results of `sum`: '.((new Wasm\Module\Val($result[0]))->value()).PHP_EOL;
echo 'Results of `sum`: '.((new Wasm\Val($result[0]))->value()).PHP_EOL;
17 changes: 8 additions & 9 deletions examples/exports-global.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

declare(strict_types=1);

use Wasm\Module\Val;
use Wasm\Type\GlobalType;

require_once __DIR__.'/../vendor/autoload.php';
Expand Down Expand Up @@ -31,12 +30,12 @@
$module = Wasm\Module::new($store, $wasmBytes);

echo 'Instantiating module...'.PHP_EOL;
$instance = Wasm\Module\Instance::new($store, $module);
$instance = Wasm\Instance::new($store, $module);

// Extracting export...
$exports = $instance->exports();
$one = (new Wasm\Module\Extern($exports[0]))->asGlobal();
$some = (new Wasm\Module\Extern($exports[1]))->asGlobal();
$one = (new Wasm\Extern($exports[0]))->asGlobal();
$some = (new Wasm\Extern($exports[1]))->asGlobal();

echo 'Getting globals types information...'.PHP_EOL;
$oneType = $one->type();
Expand All @@ -46,10 +45,10 @@
echo '`some` type: '.(GlobalType::MUTABILITY_CONST === $someType->mutability() ? 'CONST' : 'VAR').' '.$someType->content()->kind().PHP_EOL;

echo 'Getting global values...'.PHP_EOL;
$getOne = (new Wasm\Module\Extern($exports[2]))->asFunc();
$getOne = (new Wasm\Extern($exports[2]))->asFunc();

$results = $getOne();
$oneValue = (new Val($results[0]))->value();
$oneValue = (new Wasm\Val($results[0]))->value();

$someValue = $some->get()->value();

Expand All @@ -69,13 +68,13 @@
}

$results = $getOne();
$oneValue = (new Val($results[0]))->value();
$oneValue = (new Wasm\Val($results[0]))->value();

echo '`one` value: '.$oneValue.PHP_EOL;

$setSome = (new Wasm\Module\Extern($exports[4]))->asFunc();
$setSome = (new Wasm\Extern($exports[4]))->asFunc();

$arg = Wasm\Module\Val::newF32(21.0);
$arg = Wasm\Val::newF32(21.0);
$args = new Wasm\Vec\Val([$arg->inner()]);
$setSome($args);

Expand Down
10 changes: 5 additions & 5 deletions examples/imports-function-early-exit.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@ function earlyExit()

// Create an import object with the expected function.
$funcType = Wasm\Type\FuncType::new(new Wasm\Vec\ValType(), new Wasm\Vec\ValType());
$func = Wasm\Module\Func::new($store, $funcType, 'earlyExit');
$func = Wasm\Func::new($store, $funcType, 'earlyExit');
$extern = $func->asExtern();
$externs = new Wasm\Vec\Extern([$extern->inner()]);

echo 'Instantiating module...'.PHP_EOL;
$instance = Wasm\Module\Instance::new($store, $module, $externs);
$instance = Wasm\Instance::new($store, $module, $externs);

// Extracting export...
$exports = $instance->exports();
$run = (new Wasm\Module\Extern($exports[0]))->asFunc();
$run = (new Wasm\Extern($exports[0]))->asFunc();

$firstArg = Wasm\Module\Val::newI32(1);
$secondArg = Wasm\Module\Val::newI32(7);
$firstArg = Wasm\Val::newI32(1);
$secondArg = Wasm\Val::newI32(7);
$args = new Wasm\Vec\Val([$firstArg->inner(), $secondArg->inner()]);

echo 'Calling `run` function...'.PHP_EOL;
Expand Down
8 changes: 4 additions & 4 deletions examples/instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@
$module = Wasm\Module::new($store, $wasmBytes);

echo 'Instantiating module...'.PHP_EOL;
$instance = Wasm\Module\Instance::new($store, $module);
$instance = Wasm\Instance::new($store, $module);

// Extracting export...
$exports = $instance->exports();
$addOne = (new Wasm\Module\Extern($exports[0]))->asFunc();
$addOne = (new Wasm\Extern($exports[0]))->asFunc();

$arg = Wasm\Module\Val::newI32(1);
$arg = Wasm\Val::newI32(1);
$args = new Wasm\Vec\Val([$arg->inner()]);

echo 'Calling `add_one` function...'.PHP_EOL;
$result = $addOne($args);

echo 'Results of `add_one`: '.((new Wasm\Module\Val($result[0]))->value()).PHP_EOL;
echo 'Results of `add_one`: '.((new Wasm\Val($result[0]))->value()).PHP_EOL;
3 changes: 1 addition & 2 deletions src/Module/Extern.php → src/Extern.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

declare(strict_types=1);

namespace Wasm\Module;
namespace Wasm;

use Wasm\Exception;
use Wasm\Type\ExternType;

/**
Expand Down
4 changes: 1 addition & 3 deletions src/Module/Func.php → src/Func.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

declare(strict_types=1);

namespace Wasm\Module;
namespace Wasm;

use Wasm\Exception;
use Wasm\Store;
use Wasm\Type\FuncType;
use Wasm\Vec\Val;

Expand Down
4 changes: 1 addition & 3 deletions src/Module/Globl.php → src/Globl.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

declare(strict_types=1);

namespace Wasm\Module;
namespace Wasm;

use Wasm\Exception;
use Wasm\Store;
use Wasm\Type\GlobalType;
use Wasm\Type\ValType;

Expand Down
7 changes: 1 addition & 6 deletions src/Module/Instance.php → src/Instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@

declare(strict_types=1);

namespace Wasm\Module;

use Wasm\Exception;
use Wasm\Module;
use Wasm\Store;
use Wasm\Vec;
namespace Wasm;

/**
* @api
Expand Down
4 changes: 1 addition & 3 deletions src/Module/Val.php → src/Val.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

declare(strict_types=1);

namespace Wasm\Module;

use Wasm\Exception;
namespace Wasm;

/**
* @api
Expand Down
16 changes: 7 additions & 9 deletions tests/examples/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
namespace Wasm\Examples;

use Wasm;
use Wasm\Module\Extern;
use Wasm\Module\Val;
use Wasm\Wat;

/**
Expand Down Expand Up @@ -38,28 +36,28 @@ public function main()
new Wasm\Vec\ValType([$arg->inner()]),
new Wasm\Vec\ValType([$result->inner()])
);
$print = Wasm\Module\Func::new($store, $printType, [self::class, 'print_callback']);
$print = Wasm\Func::new($store, $printType, [self::class, 'print_callback']);

$result = Wasm\Type\ValType::new(Wasm\Type\ValType::KIND_I32);
$closureType = Wasm\Type\FuncType::new(
new Wasm\Vec\ValType(),
new Wasm\Vec\ValType([$result->inner()])
);
$closure = Wasm\Module\Func::new($store, $closureType, [self::class, 'closure']);
$closure = Wasm\Func::new($store, $closureType, [self::class, 'closure']);

// Instantiating module...
$printExtern = $print->asExtern();
$closureExtern = $closure->asExtern();
$externs = new Wasm\Vec\Extern([$printExtern->inner(), $closureExtern->inner()]);
$instance = Wasm\Module\Instance::new($store, $module, $externs);
$instance = Wasm\Instance::new($store, $module, $externs);

// Extracting export...
$exports = $instance->exports();
$run = (new Extern($exports[0]))->asFunc();
$run = (new Wasm\Extern($exports[0]))->asFunc();

// Calling export...
$first = Wasm\Module\Val::newI32(3);
$second = Wasm\Module\Val::newI32(4);
$first = Wasm\Val::newI32(3);
$second = Wasm\Val::newI32(4);
$args = new Wasm\Vec\Val([
$first->inner(),
$second->inner(),
Expand All @@ -68,7 +66,7 @@ public function main()
$results = $run($args);

// Printing result...
$result = new Val($results[0]);
$result = new Wasm\Val($results[0]);

self::assertEquals(49, $result->value());
}
Expand Down
35 changes: 17 additions & 18 deletions tests/examples/Globl.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Wasm\Examples;

use Wasm;
use Wasm\Module;
use Wasm\Type;

/**
Expand Down Expand Up @@ -37,14 +36,14 @@ public function main()
$var_f32_type = Type\GlobalType::new(Type\ValType::new(Type\ValType::KIND_F32), Type\GlobalType::MUTABILITY_VAR);
$var_i64_type = Type\GlobalType::new(Type\ValType::new(Type\ValType::KIND_I64), Type\GlobalType::MUTABILITY_VAR);

$val_f32_1 = Module\Val::newF32((float) 1);
$const_f32_import = Module\Globl::new($store, $const_f32_type, $val_f32_1);
$val_i64_2 = Module\Val::newI64(2);
$const_i64_import = Module\Globl::new($store, $const_i64_type, $val_i64_2);
$val_f32_3 = Module\Val::newF32((float) 3);
$var_f32_import = Module\Globl::new($store, $var_f32_type, $val_f32_3);
$val_i64_4 = Module\Val::newI64(4);
$var_i64_import = Module\Globl::new($store, $var_i64_type, $val_i64_4);
$val_f32_1 = Wasm\Val::newF32((float) 1);
$const_f32_import = Wasm\Globl::new($store, $const_f32_type, $val_f32_1);
$val_i64_2 = Wasm\Val::newI64(2);
$const_i64_import = Wasm\Globl::new($store, $const_i64_type, $val_i64_2);
$val_f32_3 = Wasm\Val::newF32((float) 3);
$var_f32_import = Wasm\Globl::new($store, $var_f32_type, $val_f32_3);
$val_i64_4 = Wasm\Val::newI64(4);
$var_i64_import = Wasm\Globl::new($store, $var_i64_type, $val_i64_4);

// Instantiating module...
$const_f32_extern = $const_f32_import->asExtern();
Expand All @@ -58,7 +57,7 @@ public function main()
$var_f32_extern->inner(),
$var_i64_extern->inner(),
]);
$instance = Module\Instance::new($store, $module, $externs);
$instance = Wasm\Instance::new($store, $module, $externs);

// Extracting export...
$exports = $instance->exports();
Expand Down Expand Up @@ -140,37 +139,37 @@ public function main()
self::check_call($get_var_i64_export, 78);
}

public static function check(Module\Val $val, int | float $expected): void
public static function check(Wasm\Val $val, int | float $expected): void
{
$actual = $val->value();

self::assertTrue($actual === $expected, sprintf('%s !== %s', var_export($actual, true), var_export($expected, true)));
}

public static function check_global(Module\Globl $global, int | float $expected): void
public static function check_global(Wasm\Globl $global, int | float $expected): void
{
self::check($global->get(), $expected);
}

public static function check_call(Module\Func $func, $expected): void
public static function check_call(Wasm\Func $func, $expected): void
{
$args = new Wasm\Vec\Val();
$results = $func($args);

self::check(Module\Val::new($results[0]), $expected);
self::check(Wasm\Val::new($results[0]), $expected);
}

public static function get_export_global(Wasm\Vec\Extern $externs, int $i): Module\Globl
public static function get_export_global(Wasm\Vec\Extern $externs, int $i): Wasm\Globl
{
self::assertTrue(count($externs) > $i);

return (new Module\Extern($externs[$i]))->asGlobal();
return (new Wasm\Extern($externs[$i]))->asGlobal();
}

public static function get_export_func(Wasm\Vec\Extern $externs, int $i): Module\Func
public static function get_export_func(Wasm\Vec\Extern $externs, int $i): Wasm\Func
{
self::assertTrue(count($externs) > $i);

return (new Module\Extern($externs[$i]))->asFunc();
return (new Wasm\Extern($externs[$i]))->asFunc();
}
}
6 changes: 3 additions & 3 deletions tests/examples/Hello.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ public function main()
$module = Wasm\Module::new($store, $wasm);

$functype = Wasm\Type\FuncType::new(new Wasm\Vec\ValType(), new Wasm\Vec\ValType());
$func = Wasm\Module\Func::new($store, $functype, [self::class, 'hello_callback']);
$func = Wasm\Func::new($store, $functype, [self::class, 'hello_callback']);

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

// Extracting export...
$exports = $instance->exports();
$run = (new Module\Extern($exports[0]))->asFunc();
$run = (new Wasm\Extern($exports[0]))->asFunc();

// Calling export...
$args = new Wasm\Vec\Val();
Expand Down
Loading

0 comments on commit c94931f

Please sign in to comment.