xref: /PHP-8.1/ext/ffi/tests/041.phpt (revision bd9f4fa6)
1--TEST--
2FFI 041: Type memory management
3--EXTENSIONS--
4ffi
5--INI--
6ffi.enable=1
7--FILE--
8<?php
9function test_new_ownership($str, $transfer) {
10    if ($transfer) {
11        return FFI::new(FFI::type($str));
12    } else {
13        $type = FFI::type($str);
14        return FFI::new($type);
15    }
16}
17var_dump(test_new_ownership("int", 1));
18var_dump(test_new_ownership("int[2]", 1));
19var_dump(test_new_ownership("int", 0));
20var_dump(test_new_ownership("int[2]", 0));
21
22function test_type_ownership($str, $transfer) {
23    if ($transfer) {
24        return FFI::typeof(FFI::new($str));
25    } else {
26        $data = FFI::new($str);
27        return FFI::typeof($data);
28    }
29}
30var_dump(test_type_ownership("int", 1));
31var_dump(test_type_ownership("int[2]", 1));
32var_dump(test_type_ownership("int", 0));
33var_dump(test_type_ownership("int[2]", 0));
34
35function test_cast_ownership($str, $transfer) {
36    if ($transfer) {
37        return FFI::cast(FFI::type($str), FFI::new($str));
38    } else {
39        $type = FFI::type($str);
40        return FFI::cast($type, FFI::new($str));
41    }
42}
43var_dump(test_cast_ownership("int", 1));
44var_dump(test_cast_ownership("int[2]", 1));
45var_dump(test_cast_ownership("int", 0));
46var_dump(test_cast_ownership("int[2]", 0));
47
48function test_array_ownership($str, $transfer) {
49    if ($transfer) {
50        return FFI::arrayType(FFI::type($str), [2]);
51    } else {
52        $type = FFI::type($str);
53        return FFI::arrayType($type, [2]);
54    }
55}
56var_dump(test_array_ownership("int", 1));
57var_dump(test_array_ownership("int[2]", 1));
58var_dump(test_array_ownership("int", 0));
59var_dump(test_array_ownership("int[2]", 0));
60?>
61--EXPECTF--
62object(FFI\CData:int32_t)#%d (1) {
63  ["cdata"]=>
64  int(0)
65}
66object(FFI\CData:int32_t[2])#%d (2) {
67  [0]=>
68  int(0)
69  [1]=>
70  int(0)
71}
72object(FFI\CData:int32_t)#%d (1) {
73  ["cdata"]=>
74  int(0)
75}
76object(FFI\CData:int32_t[2])#%d (2) {
77  [0]=>
78  int(0)
79  [1]=>
80  int(0)
81}
82object(FFI\CType:int32_t)#%d (0) {
83}
84object(FFI\CType:int32_t[2])#%d (0) {
85}
86object(FFI\CType:int32_t)#%d (0) {
87}
88object(FFI\CType:int32_t[2])#%d (0) {
89}
90object(FFI\CData:int32_t)#%d (1) {
91  ["cdata"]=>
92  int(0)
93}
94object(FFI\CData:int32_t[2])#%d (2) {
95  [0]=>
96  int(0)
97  [1]=>
98  int(0)
99}
100object(FFI\CData:int32_t)#%d (1) {
101  ["cdata"]=>
102  int(0)
103}
104object(FFI\CData:int32_t[2])#%d (2) {
105  [0]=>
106  int(0)
107  [1]=>
108  int(0)
109}
110object(FFI\CType:int32_t[2])#%s (0) {
111}
112object(FFI\CType:int32_t[2][2])#%d (0) {
113}
114object(FFI\CType:int32_t[2])#%d (0) {
115}
116object(FFI\CType:int32_t[2][2])#%d (0) {
117}
118