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