xref: /PHP-8.4/ext/ffi/tests/020.phpt (revision 4acf0084)
1--TEST--
2FFI 020: read-only
3--EXTENSIONS--
4ffi
5--INI--
6ffi.enable=1
7--FILE--
8<?php
9$ffi = FFI::cdef();
10
11try {
12    $p = $ffi->new("struct {int x; const int y;}");
13    $p->x = 1;
14    $p->y = 1;
15    echo "ok\n";
16} catch (Throwable $e) {
17    echo get_class($e) . ": " . $e->getMessage()."\n";
18}
19try {
20    $p = $ffi->new("struct {const int x; int y;}");
21    $p->y = 1;
22    $p->x = 1;
23    echo "ok\n";
24} catch (Throwable $e) {
25    echo get_class($e) . ": " . $e->getMessage()."\n";
26}
27try {
28    $p = $ffi->new("const struct {int x; int y;}");
29    $p->x = 1;
30    echo "ok\n";
31} catch (Throwable $e) {
32    echo get_class($e) . ": " . $e->getMessage()."\n";
33}
34try {
35    $p = $ffi->new("const int[10]");
36    $p[1] = 1;
37    echo "ok\n";
38} catch (Throwable $e) {
39    echo get_class($e) . ": " . $e->getMessage()."\n";
40}
41try {
42    $p = $ffi->new("const int * [1]");
43    $p[0] = null;
44    echo "ok\n";
45} catch (Throwable $e) {
46    echo get_class($e) . ": " . $e->getMessage()."\n";
47}
48try {
49    $p = $ffi->new("int * const [1]");
50    $p[0] = null;
51    echo "ok\n";
52} catch (Throwable $e) {
53    echo get_class($e) . ": " . $e->getMessage()."\n";
54}
55try {
56    $f = FFI::cdef("typedef int * const t[1];");
57    $p = $f->new("t");
58    $p[0] = null;
59    echo "ok\n";
60} catch (Throwable $e) {
61    echo get_class($e) . ": " . $e->getMessage()."\n";
62}
63?>
64ok
65--EXPECT--
66FFI\Exception: Attempt to assign read-only field 'y'
67FFI\Exception: Attempt to assign read-only field 'x'
68FFI\Exception: Attempt to assign read-only location
69FFI\Exception: Attempt to assign read-only location
70ok
71FFI\Exception: Attempt to assign read-only location
72FFI\Exception: Attempt to assign read-only location
73ok
74