You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
1.7 KiB
PHP

<?php
namespace Cijber\GraphicsToolkit;
use FFI;
class Utils {
static $ffi;
private static ?FFI\CData $null;
static function ffi(): FFI {
if (static::$ffi === null) {
static::$ffi = FFI::cdef("");
}
return static::$ffi;
}
static function arrayOf($type, $items): FFI\CData {
$ffi = static::ffi();
$items = array_values($items);
if (is_string($type)) {
$type = $ffi->type($type);
}
$arrType = FFI::arrayType($type, [count($items)]);
$arr = $ffi->new($arrType);
for ($i = 0; $i < count($items); $i++) {
$arr[$i] = $items[$i];
}
return $arr;
}
public static function string(string $input): FFI\CData {
$ffi = static::ffi();
$data = $ffi->new('char[' . strlen($input) . ']');
for ($i = 0; $i < strlen($input); $i++) {
$data[$i] = $input[$i];
}
return $data;
}
public static function constString(string $input): FFI\CData {
$ffi = static::ffi();
$data = $ffi->new('char[' . strlen($input) . ']', false);
for ($i = 0; $i < strlen($input); $i++) {
$data[$i] = $input[$i];
}
return $ffi->cast('const char*', $data);
}
public static function sizeOf($type): int {
$ffi = static::ffi();
if (is_string($type)) {
$type = $ffi->type($type);
}
return FFI::sizeof($type);
}
public static function null(): FFI\CData {
if (static::$null === null) {
$val = FFI::new("usize");
$val->cdata = 0;
static::$null = FFI::cast('void *', $val);
}
return static::$null;
}
}