| 1 | import os |
| 2 | |
| 3 | const x11_alias_vexe = @VEXE |
| 4 | const x11_alias_tests_dir = os.dir(@FILE) |
| 5 | const x11_alias_v3_dir = os.dir(x11_alias_tests_dir) |
| 6 | const x11_alias_vlib_dir = os.dir(x11_alias_v3_dir) |
| 7 | const x11_alias_v3_src = os.join_path(x11_alias_v3_dir, 'v3.v') |
| 8 | |
| 9 | fn x11_alias_build_v3() string { |
| 10 | v3_bin := os.join_path(os.temp_dir(), 'v3_x11_alias_test_${os.getpid()}') |
| 11 | os.rm(v3_bin) or {} |
| 12 | build := |
| 13 | os.execute('${x11_alias_vexe} -gc none -path "${x11_alias_vlib_dir}|@vlib|@vmodules" -o ${v3_bin} ${x11_alias_v3_src}') |
| 14 | assert build.exit_code == 0, build.output |
| 15 | return v3_bin |
| 16 | } |
| 17 | |
| 18 | fn x11_alias_write_project() string { |
| 19 | root := os.join_path(os.temp_dir(), 'v3_x11_alias_${os.getpid()}') |
| 20 | os.rmdir_all(root) or {} |
| 21 | os.mkdir_all(root) or { panic(err) } |
| 22 | os.write_file(os.join_path(root, 'native_abi.h'), '#ifndef V3_X11_ALIAS_NATIVE_ABI_H |
| 23 | #define V3_X11_ALIAS_NATIVE_ABI_H |
| 24 | typedef unsigned long NativeAtom; |
| 25 | typedef unsigned long NativeKeySym; |
| 26 | typedef unsigned long NativeULong; |
| 27 | typedef unsigned long NativeWindow; |
| 28 | #endif |
| 29 | ') or { |
| 30 | panic(err) |
| 31 | } |
| 32 | os.write_file(os.join_path(root, 'main.v'), 'module main |
| 33 | |
| 34 | #include "@DIR/native_abi.h" |
| 35 | |
| 36 | pub type Atom = C.NativeAtom |
| 37 | pub type KeySym = C.NativeKeySym |
| 38 | pub type NativeULong = C.NativeULong |
| 39 | pub type Window = C.NativeWindow |
| 40 | pub type LocalId = u64 |
| 41 | |
| 42 | @[typedef] |
| 43 | struct C.Display {} |
| 44 | |
| 45 | @[typedef] |
| 46 | struct C.XKeyEvent {} |
| 47 | |
| 48 | fn C.XLookupString(event &C.XKeyEvent, buf &char, buf_len int, keysym &KeySym, status voidptr) int |
| 49 | fn C.XSetWMProtocols(display &C.Display, window Window, protocols &Atom, count int) int |
| 50 | fn C.XGetWindowProperty(display &C.Display, window Window, property Atom, offset i64, length i64, delete int, req_type Atom, actual &Atom, actual_format &int, count &NativeULong, bytes &NativeULong, prop &&u8) int |
| 51 | |
| 52 | fn helper(display &C.Display, window Window, value &&u8) usize { |
| 53 | mut actual_type := Atom(0) |
| 54 | mut item_count := NativeULong(0) |
| 55 | mut bytes_after := NativeULong(0) |
| 56 | mut actual_format := 0 |
| 57 | property := Atom(0) |
| 58 | C.XGetWindowProperty(display, window, property, 0, 1, 0, property, &actual_type, |
| 59 | &actual_format, &item_count, &bytes_after, value) |
| 60 | return usize(item_count) |
| 61 | } |
| 62 | |
| 63 | fn main() { |
| 64 | display := &C.Display(unsafe { nil }) |
| 65 | mut event := C.XKeyEvent{} |
| 66 | mut keysym := KeySym(0) |
| 67 | C.XLookupString(&event, nil, 0, &keysym, nil) |
| 68 | mut protocols := [1]Atom{} |
| 69 | protocols[0] = Atom(1) |
| 70 | window := Window(0) |
| 71 | C.XSetWMProtocols(display, window, &protocols[0], 1) |
| 72 | mut value := &u8(unsafe { nil }) |
| 73 | count := helper(display, window, &&u8(&value)) |
| 74 | mut local_id := LocalId(9) |
| 75 | score := int(count) + int(local_id) |
| 76 | println(int_str(score)) |
| 77 | } |
| 78 | ') or { |
| 79 | panic(err) |
| 80 | } |
| 81 | return root |
| 82 | } |
| 83 | |
| 84 | fn test_c_typedef_aliases_are_preserved_for_x11_abi_shapes() { |
| 85 | v3_bin := x11_alias_build_v3() |
| 86 | root := x11_alias_write_project() |
| 87 | c_path := os.join_path(root, 'out.c') |
| 88 | compile := os.execute('${v3_bin} ${root} -b c -o ${c_path}') |
| 89 | assert compile.exit_code == 0, compile.output |
| 90 | |
| 91 | c_code := os.read_file(c_path) or { panic(err) } |
| 92 | compact := c_code.replace('\t', '').replace(' ', '').replace('\n', '') |
| 93 | assert compact.contains('NativeKeySymkeysym'), c_code |
| 94 | assert compact.contains('NativeAtomprotocols[1]'), c_code |
| 95 | assert compact.contains('NativeWindowwindow'), c_code |
| 96 | assert compact.contains('NativeAtomactual_type'), c_code |
| 97 | assert compact.contains('NativeULongitem_count'), c_code |
| 98 | assert compact.contains('NativeULongbytes_after'), c_code |
| 99 | assert compact.contains('XLookupString(&event,NULL,0,&keysym,NULL);'), c_code |
| 100 | assert compact.contains('XSetWMProtocols(display,window,&protocols[0],1);'), c_code |
| 101 | assert compact.contains('XGetWindowProperty(display,window,property,0,1,0,property,&actual_type,&actual_format,&item_count,&bytes_after,value);'), c_code |
| 102 | |
| 103 | assert !compact.contains('u64keysym'), c_code |
| 104 | assert !compact.contains('u64protocols[1]'), c_code |
| 105 | assert !compact.contains('u64actual_type'), c_code |
| 106 | assert !compact.contains('u64item_count'), c_code |
| 107 | assert !compact.contains('u64bytes_after'), c_code |
| 108 | assert !compact.contains('0&(u8)(value)'), c_code |
| 109 | assert compact.contains('u64local_id'), c_code |
| 110 | assert !compact.contains('LocalIdlocal_id'), c_code |
| 111 | } |
| 112 | |