Skip to content

Commit

Permalink
type definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
yuichiis committed Apr 25, 2024
1 parent 004629a commit 21b7040
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
13 changes: 11 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
name: tests

on: [push]
on:
push:
branches:
- main

jobs:
no_ext_tests:
Expand All @@ -21,11 +24,17 @@ jobs:
php_version: ${{ matrix.php }}
php_extensions: ffi

- name: PHP Static Analysys
uses: php-actions/phpstan@v3
with:
php_version: ${{ matrix.php }}
path: src/

- name: PHPUnit Tests
uses: php-actions/phpunit@v3
with:
configuration: tests/phpunit.xml
version: 10.5
php_version: ${{ matrix.php }}
args: --filter ReleaseTest
php_extensions: ffi

21 changes: 21 additions & 0 deletions phpstan-interop.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
parameters:
ignoreErrors:

# -
# message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\.*[Device\\|Linear]*Buffer\\:\\:dtype\\(\\)#"
# -
# message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\.*[Device\\|Linear]*Buffer\\:\\:addr\\(\\)#"
# -
# message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\.*[Device\\|Linear]*Buffer\\:\\:dump\\(\\)#"
# -
# message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\.*[Device\\|Linear]*Buffer\\:\\:value_size\\(\\)#"
# -
# message: "#function count expects array|Countable, Interop\\\\Polite\\\\Math\\\\Matrix\\\\NDArray given#"
# -
# message: "#^Argument of an invalid type Interop\\\\Polite\\\\Math\\\\Matrix\\\\NDArray supplied for foreach, only iterables are supported#"
# -
# message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\DeviceBuffer\\:\\:bytes\\(\\)#"
# -
# message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\DeviceBuffer\\:\\:copy\\(\\)#"
# -
# message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\DeviceBuffer\\:\\:read\\(\\)#"
4 changes: 4 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
level: 6
paths:
- src
14 changes: 11 additions & 3 deletions src/Buffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@
use LogicException;
use FFI;

class complex_t {
public float $real;
public float $imag;
}

class Buffer implements LinearBuffer
{
const MAX_BYTES = 2147483648; // 2**31
static protected ?FFI $ffi = null;

/** @var array<int,string> $typeString */
protected static $typeString = [
NDArray::bool => 'uint8_t',
NDArray::int8 => 'int8_t',
Expand All @@ -33,6 +39,7 @@ class Buffer implements LinearBuffer
NDArray::complex64 => 'rindow_complex_float',
NDArray::complex128 => 'rindow_complex_double',
];
/** @var array<int,int> $valueSize */
protected static $valueSize = [
NDArray::bool => 1,
NDArray::int8 => 1,
Expand Down Expand Up @@ -76,7 +83,7 @@ public function __construct(int $size, int $dtype)
$this->data = self::$ffi->new("{$declaration}[{$size}]");
}

protected function assertOffset($method, mixed $offset) : void
protected function assertOffset(string $method, mixed $offset) : void
{
if(!is_int($offset)) {
throw new TypeError($method.'(): Argument #1 ($offset) must be of type int');
Expand All @@ -86,14 +93,14 @@ protected function assertOffset($method, mixed $offset) : void
}
}

protected function assertOffsetIsInt($method, mixed $offset) : void
protected function assertOffsetIsInt(string $method, mixed $offset) : void
{
if(!is_int($offset)) {
throw new TypeError($method.'(): Argument #1 ($offset) must be of type int');
}
}

protected function isComplex($dtype=null) : bool
protected function isComplex(int $dtype=null) : bool
{
$dtype = $dtype ?? $this->dtype;
return $dtype==NDArray::complex64||$dtype==NDArray::complex128;
Expand Down Expand Up @@ -148,6 +155,7 @@ public function offsetSet(mixed $offset, mixed $value): void
$type = gettype($value);
throw new InvalidArgumentException("Cannot convert to complex number.: ".$type);
}
/** @var complex_t $value */
$value = self::$ffi->new(self::$typeString[$this->dtype]);
$value->real = $real;
$value->imag = $imag;
Expand Down

0 comments on commit 21b7040

Please sign in to comment.