• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ucode/tests/custom/17_lib_ffi/30_property_access

  1 Cdata instances support `__get__` / `__set__`, so struct fields and array
  2 elements can be read and written with plain property syntax instead of the
  3 explicit get() / set() methods.
  4 
  5 Struct field access with property syntax.
  6 
  7 -- Testcase --
  8 {%
  9 import * as ffi from 'ffi';
 10 
 11 ffi.cdef('typedef struct { int x; int y; } point;');
 12 let p = ffi.ctype('point');
 13 
 14 p.x = 10;
 15 p.y = 20;
 16 print("x: ", p.x, " y: ", p.y, "\n");
 17 -- End --
 18 
 19 -- Expect stdout --
 20 x: 10 y: 20
 21 -- End --
 22 
 23 
 24 Array element access with index syntax.
 25 
 26 -- Testcase --
 27 {%
 28 import * as ffi from 'ffi';
 29 
 30 ffi.cdef('typedef int intarr[3];');
 31 let a = ffi.ctype('intarr');
 32 
 33 a[0] = 10;
 34 a[1] = 20;
 35 a[2] = 30;
 36 print("array: ", a[0], " ", a[1], " ", a[2], "\n");
 37 -- End --
 38 
 39 -- Expect stdout --
 40 array: 10 20 30
 41 -- End --
 42 
 43 
 44 Nested struct fields are live references, so chained access works.
 45 
 46 -- Testcase --
 47 {%
 48 import * as ffi from 'ffi';
 49 
 50 ffi.cdef('typedef struct { int x; int y; } point;');
 51 ffi.cdef('typedef struct { point min; point max; } rect;');
 52 let r = ffi.ctype('rect');
 53 
 54 r.min.x = 1;
 55 r.min.y = 2;
 56 r.max.x = 100;
 57 r.max.y = 200;
 58 print("min.x: ", r.min.x, " max.y: ", r.max.y, "\n");
 59 -- End --
 60 
 61 -- Expect stdout --
 62 min.x: 1 max.y: 200
 63 -- End --
 64 
 65 
 66 Pointer fields are live references too, so they can be dereferenced further.
 67 
 68 -- Testcase --
 69 {%
 70 import * as ffi from 'ffi';
 71 
 72 ffi.cdef('typedef struct { int x; int y; } point;');
 73 ffi.cdef('typedef struct { point *p; } holder;');
 74 let h = ffi.ctype('holder');
 75 let pt = ffi.ctype('point');
 76 
 77 pt.x = 7;
 78 pt.y = 8;
 79 h.p = pt.ptr();
 80 
 81 print("p.x: ", h.p.x, " p.y: ", h.p.y, "\n");
 82 
 83 h.p.x = 99;
 84 print("after write: ", h.p.x, "\n");
 85 -- End --
 86 
 87 -- Expect stdout --
 88 p.x: 7 p.y: 8
 89 after write: 99
 90 -- End --
 91 
 92 
 93 Compound assignment works through the metamethods.
 94 
 95 -- Testcase --
 96 {%
 97 import * as ffi from 'ffi';
 98 
 99 ffi.cdef('typedef struct { int x; int y; } point;');
100 let p = ffi.ctype('point');
101 
102 p.x = 5;
103 p.x += 10;
104 p.y = 1;
105 p.y *= 4;
106 print("x: ", p.x, " y: ", p.y, "\n");
107 -- End --
108 
109 -- Expect stdout --
110 x: 15 y: 4
111 -- End --
112 
113 
114 Arrays of structs support indexed struct-field access.
115 
116 -- Testcase --
117 {%
118 import * as ffi from 'ffi';
119 
120 ffi.cdef('typedef struct { int a; int b; } pair;');
121 let pairs = ffi.ctype('pair[3]');
122 
123 pairs[0].a = 1;
124 pairs[0].b = 2;
125 pairs[2].a = 3;
126 pairs[2].b = 4;
127 print("pairs[0].a: ", pairs[0].a, " pairs[2].b: ", pairs[2].b, "\n");
128 -- End --
129 
130 -- Expect stdout --
131 pairs[0].a: 1 pairs[2].b: 4
132 -- End --
133 
134 
135 The explicit get() / set() / index() methods keep working alongside the
136 property syntax.
137 
138 -- Testcase --
139 {%
140 import * as ffi from 'ffi';
141 
142 ffi.cdef('typedef struct { int x; int y; } point;');
143 let p = ffi.ctype('point');
144 
145 p.x = 1;
146 p.set('y', 2);
147 print("get: ", p.get('x'), " ", p.get('y'), "\n");
148 print("index type: ", type(p.index('x')), "\n");
149 -- End --
150 
151 -- Expect stdout --
152 get: 1 2
153 index type: resource
154 -- End --

This page was automatically generated by LXR 0.3.1.  •  OpenWrt