Skip to content

FFI extension add FFI\CDef class and other update #8585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 33 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
e3f29b4
Merge pull request #1 from php/master
chopins Sep 2, 2020
f97a395
Merge pull request #2 from php/master
chopins Sep 8, 2020
9850245
Merge pull request #3 from php/master
chopins Sep 12, 2020
728d914
Merge pull request #4 from php/master
chopins Sep 16, 2020
ee2ba03
Merge pull request #5 from php/master
chopins Sep 20, 2020
ee22aa4
Merge pull request #6 from php/master
chopins Oct 1, 2020
86e41ca
Merge pull request #7 from php/master
chopins Oct 3, 2020
dd61db7
Merge pull request #8 from php/master
chopins Nov 12, 2020
725f0ef
Merge pull request #9 from php/master
chopins Dec 17, 2020
2032ee3
Merge pull request #10 from php/master
chopins Nov 16, 2021
7fa87be
Merge pull request #11 from php/master
chopins Nov 21, 2021
d4b78af
Merge pull request #12 from php/master
chopins Dec 6, 2021
e80e7c1
Merge pull request #13 from php/master
chopins Dec 24, 2021
a29017a
Merge pull request #14 from php/master
chopins Dec 26, 2021
adf3789
Merge pull request #15 from php/master
chopins Mar 24, 2022
b510ebd
Merge pull request #16 from php/master
chopins Apr 10, 2022
bda9813
Merge pull request #17 from php/master
chopins May 16, 2022
c5259a4
update
chopins May 16, 2022
87f7f8c
update
chopins May 16, 2022
3e064df
update
chopins May 16, 2022
f4e5686
updatE
chopins May 16, 2022
56ac3e2
updatE
chopins May 16, 2022
1d12aa9
update
chopins May 16, 2022
ea37a33
update
chopins May 20, 2022
d8340f6
update
chopins May 20, 2022
a5dc8b6
update
chopins May 20, 2022
c84c53f
update
chopins May 20, 2022
8604991
update
chopins May 20, 2022
63fe549
update
chopins May 20, 2022
2167335
update
chopins May 20, 2022
eae05ff
update
chopins May 20, 2022
02d022a
Merge pull request #18 from php/master
chopins May 21, 2022
8d7aad6
Merge branch 'master' into ffi-cdef-class
chopins May 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
226 changes: 170 additions & 56 deletions ext/ffi/ffi.c

Large diffs are not rendered by default.

19 changes: 13 additions & 6 deletions ext/ffi/ffi.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
/** @not-serializable */
final class FFI
{
public static function cdef(string $code = "", ?string $lib = null): FFI {}
public static function cdef(string $code = "", ?string $lib = null): FFI\CDef {}

public static function load(string $filename): ?FFI {}
public static function load(string $filename): ?FFI\CDef {}

public static function scope(string $name): FFI {}
public static function scope(string $name): FFI\CDef {}

public static function new(FFI\CType|string $type, bool $owned = true, bool $persistent = false): ?FFI\CData {}
public static function new(FFI\CType|string $type, bool $owned = true, bool $persistent = false, ?FFI\CDef $cdef = null): ?FFI\CData {}

/** @prefer-ref $ptr */
public static function free(FFI\CData $ptr): void {}
Expand All @@ -22,9 +22,9 @@ public static function free(FFI\CData $ptr): void {}
* @param FFI\CData|int|float|bool|null $ptr
* @prefer-ref $ptr
*/
public static function cast(FFI\CType|string $type, $ptr): ?FFI\CData {}
public static function cast(FFI\CType|string $type, $ptr, ?FFI\CDef $cdef = null): ?FFI\CData {}

public static function type(string $type): ?FFI\CType {}
public static function type(string $type, ?FFI\CDef $cdef = null): ?FFI\CType {}

/** @prefer-ref $ptr */
public static function typeof(FFI\CData $ptr): FFI\CType {}
Expand Down Expand Up @@ -63,11 +63,18 @@ public static function string(FFI\CData $ptr, ?int $size = null): string {}

/** @prefer-ref $ptr */
public static function isNull(FFI\CData $ptr): bool {}

public static function hasSymbol(FFI\CDef $cdef, string $symbol, ?int $symbol_kind = null): bool {}

public static function getSymbols(FFI\CDef $cdef, ?int $symbol_kind = null): array {}
}

}

namespace FFI {
/** @not-serializable */
final class CDef {
}

/** @not-serializable */
final class CData {
Expand Down
42 changes: 38 additions & 4 deletions ext/ffi/ffi_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: d9dd3b93c0d1623fe61ea0bd8ee9d4c3a359bf78 */
* Stub hash: 754463b45c9c4a1313e2bd690c84656d5d6ec853 */

ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_FFI_cdef, 0, 0, FFI, 0)
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_FFI_cdef, 0, 0, FFI\\CDef, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, code, IS_STRING, 0, "\"\"")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, lib, IS_STRING, 1, "null")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_FFI_load, 0, 1, FFI, 1)
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_FFI_load, 0, 1, FFI\\CDef, 1)
ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_FFI_scope, 0, 1, FFI, 0)
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_FFI_scope, 0, 1, FFI\\CDef, 0)
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_FFI_new, 0, 1, FFI\\CData, 1)
ZEND_ARG_OBJ_TYPE_MASK(0, type, FFI\\CType, MAY_BE_STRING, NULL)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, owned, _IS_BOOL, 0, "true")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, persistent, _IS_BOOL, 0, "false")
ZEND_ARG_OBJ_INFO_WITH_DEFAULT_VALUE(0, cdef, FFI\\CDef, 1, "null")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_FFI_free, 0, 1, IS_VOID, 0)
Expand All @@ -27,10 +28,12 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_FFI_cast, 0, 2, FFI\\CData, 1)
ZEND_ARG_OBJ_TYPE_MASK(0, type, FFI\\CType, MAY_BE_STRING, NULL)
ZEND_ARG_INFO(ZEND_SEND_PREFER_REF, ptr)
ZEND_ARG_OBJ_INFO_WITH_DEFAULT_VALUE(0, cdef, FFI\\CDef, 1, "null")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_FFI_type, 0, 1, FFI\\CType, 1)
ZEND_ARG_TYPE_INFO(0, type, IS_STRING, 0)
ZEND_ARG_OBJ_INFO_WITH_DEFAULT_VALUE(0, cdef, FFI\\CDef, 1, "null")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_FFI_typeof, 0, 1, FFI\\CType, 0)
Expand Down Expand Up @@ -79,6 +82,17 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_FFI_isNull, 0, 1, _IS_BOOL
ZEND_ARG_OBJ_INFO(ZEND_SEND_PREFER_REF, ptr, FFI\\CData, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_FFI_hasSymbol, 0, 2, _IS_BOOL, 0)
ZEND_ARG_OBJ_INFO(0, cdef, FFI\\CDef, 0)
ZEND_ARG_TYPE_INFO(0, symbol, IS_STRING, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, symbol_kind, IS_LONG, 1, "null")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_FFI_getSymbols, 0, 1, IS_ARRAY, 0)
ZEND_ARG_OBJ_INFO(0, cdef, FFI\\CDef, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, symbol_kind, IS_LONG, 1, "null")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_FFI_CType_getName, 0, 0, IS_STRING, 0)
ZEND_END_ARG_INFO()

Expand Down Expand Up @@ -139,6 +153,8 @@ ZEND_METHOD(FFI, memcmp);
ZEND_METHOD(FFI, memset);
ZEND_METHOD(FFI, string);
ZEND_METHOD(FFI, isNull);
ZEND_METHOD(FFI, hasSymbol);
ZEND_METHOD(FFI, getSymbols);
ZEND_METHOD(FFI_CType, getName);
ZEND_METHOD(FFI_CType, getKind);
ZEND_METHOD(FFI_CType, getSize);
Expand Down Expand Up @@ -175,6 +191,13 @@ static const zend_function_entry class_FFI_methods[] = {
ZEND_ME(FFI, memset, arginfo_class_FFI_memset, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
ZEND_ME(FFI, string, arginfo_class_FFI_string, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
ZEND_ME(FFI, isNull, arginfo_class_FFI_isNull, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
ZEND_ME(FFI, hasSymbol, arginfo_class_FFI_hasSymbol, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
ZEND_ME(FFI, getSymbols, arginfo_class_FFI_getSymbols, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
ZEND_FE_END
};


static const zend_function_entry class_FFI_CDef_methods[] = {
ZEND_FE_END
};

Expand Down Expand Up @@ -225,6 +248,17 @@ static zend_class_entry *register_class_FFI(void)
return class_entry;
}

static zend_class_entry *register_class_FFI_CDef(void)
{
zend_class_entry ce, *class_entry;

INIT_NS_CLASS_ENTRY(ce, "FFI", "CDef", class_FFI_CDef_methods);
class_entry = zend_register_internal_class_ex(&ce, NULL);
class_entry->ce_flags |= ZEND_ACC_FINAL|ZEND_ACC_NOT_SERIALIZABLE;

return class_entry;
}

static zend_class_entry *register_class_FFI_CData(void)
{
zend_class_entry ce, *class_entry;
Expand Down
12 changes: 6 additions & 6 deletions ext/ffi/tests/003.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ struct _f;
typedef struct _f f;
EOF
);
var_dump($ffi->new("struct _a"));
var_dump($ffi->new("struct _b"));
var_dump($ffi->new("c"));
var_dump($ffi->new("d"));
var_dump(FFI::new("struct _a", cdef : $ffi));
var_dump(FFI::new("struct _b", cdef : $ffi));
var_dump(FFI::new("c", cdef : $ffi));
var_dump(FFI::new("d", cdef : $ffi));
try {
var_dump($ffi->new("struct _e"));
var_dump(FFI::new("struct _e", cdef : $ffi));
} catch (Throwable $e) {
echo get_class($e) . ": " . $e->getMessage()."\n";
}
try {
var_dump($ffi->new("f"));
var_dump(FFI::new("f", cdef : $ffi));
} catch (Throwable $e) {
echo get_class($e) . ": " . $e->getMessage()."\n";
}
Expand Down
18 changes: 9 additions & 9 deletions ext/ffi/tests/004.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ enum _g {
};
EOF
);
var_dump($ffi->new("enum _a"));
var_dump($ffi->new("enum _b"));
var_dump($ffi->new("c"));
var_dump($ffi->new("d"));
var_dump($ffi->new("int[_c2]"));
var_dump($ffi->new("int[_c3]"));
var_dump($ffi->new("int[_c4]"));
var_dump(FFI::new("enum _a", cdef : $ffi));
var_dump(FFI::new("enum _b", cdef : $ffi));
var_dump(FFI::new("c", cdef : $ffi));
var_dump(FFI::new("d", cdef : $ffi));
var_dump(FFI::new("int[_c2]", cdef : $ffi));
var_dump(FFI::new("int[_c3]", cdef : $ffi));
var_dump(FFI::new("int[_c4]", cdef : $ffi));
try {
var_dump($ffi->new("enum _e"));
var_dump(FFI::new("enum _e", cdef : $ffi));
} catch (Throwable $e) {
echo get_class($e) . ": " . $e->getMessage()."\n";
}
try {
var_dump($ffi->new("f"));
var_dump(FFI::new("f", cdef : $ffi));
} catch (Throwable $e) {
echo get_class($e) . ": " . $e->getMessage()."\n";
}
Expand Down
2 changes: 1 addition & 1 deletion ext/ffi/tests/020.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ try {
}
try {
$f = FFI::cdef("typedef int * const t[1];");
$p = $f->new("t");
$p = FFI::new("t", cdef : $f);
$p[0] = null;
echo "ok\n";
} catch (Throwable $e) {
Expand Down
4 changes: 2 additions & 2 deletions ext/ffi/tests/029.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ $ffi = FFI::cdef("
typedef char t1;
typedef char _Alignas(int) t2;
");
var_dump(FFI::sizeof($ffi->new("struct {char a; t1 b;}")));
var_dump(FFI::sizeof($ffi->new("struct {char a; t2 b;}")));
var_dump(FFI::sizeof(FFI::new("struct {char a; t1 b;}", cdef : $ffi)));
var_dump(FFI::sizeof(FFI::new("struct {char a; t2 b;}", cdef : $ffi)));
?>
--EXPECT--
int(2)
Expand Down
20 changes: 10 additions & 10 deletions ext/ffi/tests/044.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ typedef unsigned int ud __attribute__ ((__mode__ (__DI__)));
typedef float e __attribute__ ((__mode__ (__SF__)));
typedef float f __attribute__ ((__mode__ (__DF__)));
");
var_dump(FFI::sizeof($ffi->new("a")));
var_dump(FFI::sizeof($ffi->new("ua")));
var_dump(FFI::sizeof($ffi->new("b")));
var_dump(FFI::sizeof($ffi->new("ub")));
var_dump(FFI::sizeof($ffi->new("c")));
var_dump(FFI::sizeof($ffi->new("uc")));
var_dump(FFI::sizeof($ffi->new("d")));
var_dump(FFI::sizeof($ffi->new("ud")));
var_dump(FFI::sizeof($ffi->new("e")));
var_dump(FFI::sizeof($ffi->new("f")));
var_dump(FFI::sizeof(FFI::new("a", cdef : $ffi)));
var_dump(FFI::sizeof(FFI::new("ua", cdef : $ffi)));
var_dump(FFI::sizeof(FFI::new("b", cdef : $ffi)));
var_dump(FFI::sizeof(FFI::new("ub", cdef : $ffi)));
var_dump(FFI::sizeof(FFI::new("c", cdef : $ffi)));
var_dump(FFI::sizeof(FFI::new("uc", cdef : $ffi)));
var_dump(FFI::sizeof(FFI::new("d", cdef : $ffi)));
var_dump(FFI::sizeof(FFI::new("ud", cdef : $ffi)));
var_dump(FFI::sizeof(FFI::new("e", cdef : $ffi)));
var_dump(FFI::sizeof(FFI::new("f", cdef : $ffi)));
?>
--EXPECT--
int(1)
Expand Down
26 changes: 26 additions & 0 deletions ext/ffi/tests/047.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
FFI 047: FFI::hasSymbol()
--EXTENSIONS--
ffi
--INI--
ffi.enable=1
--FILE--
<?php


$ffi = FFI::cdef(<<<EOF
/* allowed storage classes */
typedef int type1;
EOF
);

var_dump(FFI::hasSymbol($ffi, 'type1'));
var_dump(FFI::hasSymbol($ffi, 'type1', FFI::SYM_TYPE));
var_dump(FFI::hasSymbol($ffi, 'type1', FFI::SYM_FUNC));
?>
ok
--EXPECT--
bool(true)
bool(true)
bool(false)
ok
76 changes: 76 additions & 0 deletions ext/ffi/tests/048.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
--TEST--
FFI 048: FFI::getSymbols()
--EXTENSIONS--
ffi
--INI--
ffi.enable=1
--FILE--
<?php


$ffi = FFI::cdef(<<<EOF
/* allowed storage classes */
typedef int type1;
typedef char type2;
enum _g {
_c1,
_c2,
_c3 = 1,
_c4,
};

EOF
);

var_dump(FFI::getSymbols($ffi));
var_dump(FFI::getSymbols($ffi, FFI::SYM_TYPE));
var_dump(FFI::getSymbols($ffi, FFI::SYM_FUNC));
var_dump(FFI::getSymbols($ffi, FFI::SYM_CONST));
?>
ok
--EXPECTF--
array(6) {
["type1"]=>
object(FFI\CType:int32_t)#%d (0) {
}
["type2"]=>
object(FFI\CType:char)#%d (0) {
}
["_c1"]=>
object(FFI\CType:uint32_t)#%d (0) {
}
["_c2"]=>
object(FFI\CType:uint32_t)#%d (0) {
}
["_c3"]=>
object(FFI\CType:uint32_t)#%d (0) {
}
["_c4"]=>
object(FFI\CType:uint32_t)#%d (0) {
}
}
array(2) {
["type1"]=>
object(FFI\CType:int32_t)#%d (0) {
}
["type2"]=>
object(FFI\CType:char)#%d (0) {
}
}
array(0) {
}
array(4) {
["_c1"]=>
object(FFI\CType:uint32_t)#%d (0) {
}
["_c2"]=>
object(FFI\CType:uint32_t)#%d (0) {
}
["_c3"]=>
object(FFI\CType:uint32_t)#%d (0) {
}
["_c4"]=>
object(FFI\CType:uint32_t)#%d (0) {
}
}
ok
2 changes: 1 addition & 1 deletion ext/ffi/tests/100.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var_dump(($zend->zend_printf)("Hello %s!\n", "World"));

var_dump($zend->zend_hash_func("file", strlen("file")));

$str = $zend->new("char[16]");
$str = FFI::new("char[16]", cdef : $zend);
FFI::memcpy($str, "Hello World!", strlen("Hello World!"));
$zend->zend_str_tolower($str, strlen("Hello World!"));
var_dump(FFI::string($str));
Expand Down
2 changes: 1 addition & 1 deletion ext/ffi/tests/101.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var_dump(($zend->zend_printf)("Hello %s!\n", "World"));
$f = $zend->zend_hash_func;
var_dump($f("file", strlen("file")));

$str = $zend->new("char[16]");
$str = FFI::new("char[16]", cdef : $zend);
FFI::memcpy($str, "Hello World!", strlen("Hello World!"));
$f = $zend->zend_str_tolower;
$f($str, strlen("Hello World!"));
Expand Down
2 changes: 1 addition & 1 deletion ext/ffi/tests/bug77768.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ int printf(const char *format, ...);
int printf(const char *format, ...);
");

var_dump(FFI::sizeof($x->new("uint8_t")));
var_dump(FFI::sizeof(FFI::new("uint8_t", cdef : $x)));
var_dump(FFI::sizeof(FFI::new("uint8_t")));
?>
--EXPECT--
Expand Down
2 changes: 1 addition & 1 deletion ext/ffi/tests/bug78543.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ffi
--FILE--
<?php
$ffi = FFI::cdef(' struct test { int dummy; }; ');
$test = $ffi->new('struct test');
$test = FFI::new('struct test', cdef : $ffi);
var_dump(is_callable($test));
?>
--EXPECT--
Expand Down
Loading