1 Path-based access allows dot notation and array indexing in get/set calls. 2 3 Nested struct field access with paths. 4 5 -- Testcase -- 6 {% 7 import * as ffi from 'ffi'; 8 9 ffi.cdef('struct point { int x; int y; }; struct rect { struct point min; struct point max; };'); 10 let r = ffi.ctype('struct rect', { 11 min: {x: 1, y: 2}, 12 max: {x: 3, y: 4} 13 }); 14 15 print("min.x: ", r.get('min.x'), "\n"); 16 print("max.y: ", r.get('max.y'), "\n"); 17 -- End -- 18 19 -- Expect stdout -- 20 min.x: 1 21 max.y: 4 22 -- End -- 23 24 25 Array element access with paths. 26 27 -- Testcase -- 28 {% 29 import * as ffi from 'ffi'; 30 31 ffi.cdef('struct arr { int data[5]; };'); 32 let a = ffi.ctype('struct arr', {data: [1, 2, 3, 4, 5]}); 33 34 print("data[0]: ", a.get('data[0]'), "\n"); 35 print("data[2]: ", a.get('data[2]'), "\n"); 36 -- End -- 37 38 -- Expect stdout -- 39 data[0]: 1 40 data[2]: 3 41 -- End -- 42 43 44 Set with path syntax. 45 46 -- Testcase -- 47 {% 48 import * as ffi from 'ffi'; 49 50 ffi.cdef('struct arr { int data[5]; };'); 51 let a = ffi.ctype('struct arr', {data: [1, 2, 3, 4, 5]}); 52 53 a.set('data[1]', 99); 54 print("data[1] after set: ", a.get('data[1]'), "\n"); 55 -- End -- 56 57 -- Expect stdout -- 58 data[1] after set: 99 59 -- End -- 60 61 62 index() with path notation returns cdata reference. 63 64 -- Testcase -- 65 {% 66 import * as ffi from 'ffi'; 67 68 ffi.cdef('struct point { int x; int y; }; struct rect { struct point min; struct point max; };'); 69 let r = ffi.ctype('struct rect', { 70 min: {x: 1, y: 2}, 71 max: {x: 3, y: 4} 72 }); 73 74 // index() with path returns cdata reference 75 let ref = r.index('min.x'); 76 print("type: ", type(ref), "\n"); 77 print("value: ", ref.get(), "\n"); 78 79 // Modify through path reference 80 r.index('max.y').set(999); 81 print("max.y after set: ", r.get('max.y'), "\n"); 82 -- End -- 83 84 -- Expect stdout -- 85 type: resource 86 value: 1 87 max.y after set: 999 88 -- End -- 89 90
This page was automatically generated by LXR 0.3.1. • OpenWrt