| 1 | module modc |
| 2 | |
| 3 | #flag @VMODROOT/modc/impl.o |
| 4 | #include "header.h" |
| 5 | |
| 6 | pub struct C.Atype { |
| 7 | } |
| 8 | |
| 9 | // Note: @[trusted] below, means that the C function, can be safely called outside unsafe{} blocks. |
| 10 | // |
| 11 | // By default, all C. functions are NOT trusted, and all V functions are by default trusted. |
| 12 | // |
| 13 | // Relatedly, if you want to mark a V function as unsafe, use [unsafe]. |
| 14 | // |
| 15 | // The V compiler forces all calls of unsafe functions to be wrapped in `unsafe{...}` blocks. |
| 16 | pub struct Vtype { |
| 17 | pub mut: |
| 18 | p &C.Atype |
| 19 | } |
| 20 | |
| 21 | fn C.new_atype(i32) voidptr |
| 22 | |
| 23 | @[trusted] |
| 24 | fn C.handle_array(voidptr, i32) |
| 25 | |
| 26 | fn todo_remove_me() { |
| 27 | // TODO: remove this dummy function, when the vfmt bug of @[trusted] after a void C function is fixed |
| 28 | } |
| 29 | |
| 30 | @[trusted] |
| 31 | fn C.handle_array2(voidptr, i32) |
| 32 | |
| 33 | fn C.destroy_atype(voidptr) |
| 34 | |
| 35 | pub fn new_vtype(n int) Vtype { |
| 36 | t := C.new_atype(n) |
| 37 | return Vtype{t} |
| 38 | } |
| 39 | |
| 40 | pub fn call_with_array_param(arr []Vtype) { |
| 41 | mut carr := []C.Atype{} |
| 42 | for t in arr { |
| 43 | carr << *t.p |
| 44 | } |
| 45 | // TODO: make it safe |
| 46 | C.handle_array(carr.data, carr.len) |
| 47 | C.handle_array2(carr.data, carr.len) |
| 48 | } |
| 49 | |
| 50 | pub fn destroy_vtype(t Vtype) { |
| 51 | C.destroy_atype(t.p) |
| 52 | } |
| 53 | |
| 54 | pub enum MyEnum { |
| 55 | unknown = C.UNKNOWN |
| 56 | name1 = C.NAME1 |
| 57 | name2 = C.NAME2 |
| 58 | name3 = C.NAME3 |
| 59 | name4 = C.NAME4 |
| 60 | name5 = C.NAME5 |
| 61 | name6 = C.NAME6 |
| 62 | common_name1 = C.COMMON_NAME1 |
| 63 | common_name2 = C.COMMON_NAME2 |
| 64 | common_name3 = C.COMMON_NAME3 |
| 65 | } |
| 66 | |