1 The `cdata.get()` and `cdata.set()` methods read and write struct fields and array elements. 2 3 Struct field access with get/set. 4 5 -- Testcase -- 6 {% 7 import * as ffi from 'ffi'; 8 9 ffi.cdef('typedef struct { int x; float y; } point;'); 10 let p = ffi.ctype('point', 0, 0.0); 11 12 p.set('x', 100); 13 p.set('y', 2.71); 14 print("x: ", p.get('x'), " y: ", p.get('y'), "\n"); 15 -- End -- 16 17 -- Expect stdout -- 18 x: 100 y: 2.710000038147 19 -- End -- 20 21 22 Array element access. 23 24 -- Testcase -- 25 {% 26 import * as ffi from 'ffi'; 27 28 ffi.cdef('typedef int intarr[3];'); 29 let a = ffi.ctype('intarr', 0, 0, 0); 30 31 a.set(0, 10); 32 a.set(1, 20); 33 a.set(2, 30); 34 print("array: ", a.get(0), " ", a.get(1), " ", a.get(2), "\n"); 35 -- End -- 36 37 -- Expect stdout -- 38 array: 10 20 30 39 -- End -- 40 41 42 index() returns cdata reference for further manipulation. 43 44 -- Testcase -- 45 {% 46 import * as ffi from 'ffi'; 47 48 ffi.cdef('typedef int intarr[3];'); 49 let a = ffi.ctype('intarr', 10, 20, 30); 50 51 // index() returns cdata, get() converts to value 52 let ref = a.index(0); 53 print("type: ", type(ref), "\n"); 54 print("value: ", ref.get(), "\n"); 55 56 // Chain index().set() for modification 57 a.index(1).set(99); 58 print("after set: ", a.get(1), "\n"); 59 -- End -- 60 61 -- Expect stdout -- 62 type: resource 63 value: 10 64 after set: 99 65 -- End -- 66 67
This page was automatically generated by LXR 0.3.1. • OpenWrt