xref: /PHP-8.3/ext/ffi/tests/bug_gh9090.phpt (revision 8cf9c2f2)
1--TEST--
2GH-9090 (Support assigning function pointers via FFI)
3--EXTENSIONS--
4ffi
5zend_test
6--FILE--
7<?php
8
9require_once 'utils.inc';
10$h = <<<'EOD'
11void (*bug_gh9090_void_none_ptr)();
12void (*bug_gh9090_void_int_char_ptr)(int, char *);
13void (*bug_gh9090_void_int_char_var_ptr)(int, char *, ...);
14void (*bug_gh9090_void_char_int_ptr)(char *, int);
15int (*bug_gh9090_int_int_char_ptr)(int, char *);
16
17void bug_gh9090_void_none();
18void bug_gh9090_void_int_char(int i, char *s);
19void bug_gh9090_void_int_char_var(int i, char *fmt, ...);
20EOD;
21
22if (PHP_OS_FAMILY !== 'Windows') {
23    $ffi = FFI::cdef($h);
24} else {
25    try {
26        $ffi = FFI::cdef($h, 'php_zend_test.dll');
27    } catch (FFI\Exception $ex) {
28        $ffi = FFI::cdef($h, ffi_get_php_dll_name());
29    }
30}
31
32$func_ptrs = [
33    'bug_gh9090_void_none_ptr',
34    'bug_gh9090_void_int_char_ptr',
35    'bug_gh9090_void_int_char_var_ptr',
36    'bug_gh9090_void_char_int_ptr',
37    'bug_gh9090_int_int_char_ptr',
38];
39
40$func_argvs = [
41    [ 'bug_gh9090_void_none',         [ ]                           ],
42    [ 'bug_gh9090_void_int_char',     [ 42, "hello" ]               ],
43    [ 'bug_gh9090_void_int_char_var', [ 42, "d=%d s=%s", -1, "ok" ] ],
44];
45
46foreach ($func_ptrs as $func_ptr) {
47    foreach ($func_argvs as $func_argv) {
48        [ $func, $argv ] = $func_argv;
49
50        $ok = true;
51        try {
52            $ffi->$func_ptr = $ffi->$func;
53            call_user_func_array($ffi->$func_ptr, $argv);
54        } catch (FFI\Exception $e) {
55            $ok = false;
56        }
57
58        printf("%-36s = %-36s ? %s\n", $func_ptr, $func, $ok ? 'yes' : 'no');
59    }
60}
61?>
62--EXPECT--
63bug_gh9090_none
64bug_gh9090_void_none_ptr             = bug_gh9090_void_none                 ? yes
65bug_gh9090_void_none_ptr             = bug_gh9090_void_int_char             ? no
66bug_gh9090_void_none_ptr             = bug_gh9090_void_int_char_var         ? no
67bug_gh9090_void_int_char_ptr         = bug_gh9090_void_none                 ? no
68bug_gh9090_int_char 42 hello
69bug_gh9090_void_int_char_ptr         = bug_gh9090_void_int_char             ? yes
70bug_gh9090_void_int_char_ptr         = bug_gh9090_void_int_char_var         ? no
71bug_gh9090_void_int_char_var_ptr     = bug_gh9090_void_none                 ? no
72bug_gh9090_void_int_char_var_ptr     = bug_gh9090_void_int_char             ? no
73bug_gh9090_void_int_char_var d=-1 s=ok
74bug_gh9090_void_int_char_var_ptr     = bug_gh9090_void_int_char_var         ? yes
75bug_gh9090_void_char_int_ptr         = bug_gh9090_void_none                 ? no
76bug_gh9090_void_char_int_ptr         = bug_gh9090_void_int_char             ? no
77bug_gh9090_void_char_int_ptr         = bug_gh9090_void_int_char_var         ? no
78bug_gh9090_int_int_char_ptr          = bug_gh9090_void_none                 ? no
79bug_gh9090_int_int_char_ptr          = bug_gh9090_void_int_char             ? no
80bug_gh9090_int_int_char_ptr          = bug_gh9090_void_int_char_var         ? no
81