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

Sources/ucode/tests/custom/17_lib_ffi/27_tostring

  1 The `ffi.ctype` cdata objects provide a `tostring()` method that serializes
  2 the C type along with its value for debugging purposes.
  3 
  4 -- Testcase --
  5 {%
  6 import * as ffi from 'ffi';
  7 
  8 // Scalar types
  9 let i = ffi.ctype('int', 42);
 10 print(i.tostring(), "\n");
 11 
 12 let f = ffi.ctype('float', 3.14);
 13 print(f.tostring(), "\n");
 14 
 15 let d = ffi.ctype('double', 2.71828);
 16 print(d.tostring(), "\n");
 17 
 18 // Character arrays with string content
 19 let buf = ffi.ctype('char[6]', "hello");
 20 print(buf.tostring(), "\n");
 21 
 22 // Pointer types
 23 let pn = ffi.ctype('void *');
 24 print(pn.tostring(), "\n");
 25 
 26 // Struct types
 27 ffi.cdef('struct point { int x; int y; };');
 28 let p = ffi.ctype('struct point', 10, 20);
 29 print(p.tostring(), "\n");
 30 
 31 // Array types
 32 let arr = ffi.ctype('int[5]', [1, 2, 3, 4, 5]);
 33 print(arr.tostring(), "\n");
 34 
 35 // Complex numbers
 36 let c = ffi.ctype('float complex', 1.0, 2.0);
 37 print(c.tostring(), "\n");
 38 
 39 // Unsigned types
 40 let u = ffi.ctype('uint32_t', 4294967295);
 41 print(u.tostring(), "\n");
 42 
 43 // Signed types
 44 let s = ffi.ctype('int8_t', -128);
 45 print(s.tostring(), "\n");
 46 -- End --
 47 
 48 -- Expect stdout --
 49 int: 42
 50 float: 3.1400001049042
 51 double: 2.71828
 52 char [6]: "hello"
 53 void *: NULL
 54 struct point: { "x": 10, "y": 20 }
 55 int [5] (len=5)
 56 complex float: 1+2i
 57 unsigned int: 4294967295
 58 char: -128
 59 -- End --

This page was automatically generated by LXR 0.3.1.  •  OpenWrt