xref: /PHP-8.3/ext/ffi/tests/101.phpt (revision bd9f4fa6)
1--TEST--
2FFI 101: PHP symbols (function address)
3--EXTENSIONS--
4ffi
5--SKIPIF--
6<?php require_once('utils.inc'); ?>
7<?php
8try {
9    ffi_cdef("extern void *zend_printf;", ffi_get_php_dll_name());
10} catch (Throwable $e) {
11    die('skip PHP symbols not available');
12}
13?>
14--INI--
15ffi.enable=1
16--FILE--
17<?php
18require_once('utils.inc');
19$fastcall = ffi_get_fastcall_specifier();
20$zend = ffi_cdef("
21    const char *get_zend_version(void);
22    //char *get_zend_version(void);
23    extern size_t (*zend_printf)(const char *format, ...);
24
25    unsigned long $fastcall zend_hash_func(const char *str, size_t len);
26
27    void $fastcall zend_str_tolower(char *str, size_t length);
28
29", ffi_get_php_dll_name());
30$f = $zend->get_zend_version;
31var_dump(trim(explode("\n",$f())[0]));
32//var_dump(trim(FFI::string($zend->get_zend_version())));
33var_dump($zend->zend_printf);
34var_dump(($zend->zend_printf)("Hello %s!\n", "World"));
35
36$f = $zend->zend_hash_func;
37var_dump($f("file", strlen("file")));
38
39$str = $zend->new("char[16]");
40FFI::memcpy($str, "Hello World!", strlen("Hello World!"));
41$f = $zend->zend_str_tolower;
42$f($str, strlen("Hello World!"));
43var_dump(FFI::string($str));
44
45?>
46--EXPECTF--
47string(%d) "Zend Engine %s"
48object(FFI\CData:uint%d_t(*)())#%d (1) {
49  [0]=>
50  object(FFI\CData:uint%d_t())#%d (0) {
51  }
52}
53Hello World!
54int(13)
55int(%i)
56string(12) "hello world!"
57