| 1 | import os |
| 2 | |
| 3 | const wait_header_vexe = @VEXE |
| 4 | const wait_header_tests_dir = os.dir(@FILE) |
| 5 | const wait_header_v3_dir = os.dir(wait_header_tests_dir) |
| 6 | const wait_header_vlib_dir = os.dir(wait_header_v3_dir) |
| 7 | const wait_header_v3_src = os.join_path(wait_header_v3_dir, 'v3.v') |
| 8 | |
| 9 | struct WaitHeaderProgram { |
| 10 | c_code string |
| 11 | out string |
| 12 | } |
| 13 | |
| 14 | fn wait_header_build_v3() string { |
| 15 | pid := os.getpid() |
| 16 | v3_bin := os.join_path(os.temp_dir(), 'v3_wait_header_test_${pid}') |
| 17 | os.rm(v3_bin) or {} |
| 18 | build := |
| 19 | os.execute('${wait_header_vexe} -gc none -path "${wait_header_vlib_dir}|@vlib|@vmodules" -o ${v3_bin} ${wait_header_v3_src}') |
| 20 | assert build.exit_code == 0, build.output |
| 21 | return v3_bin |
| 22 | } |
| 23 | |
| 24 | fn wait_header_compile(v3_bin string, name string, source string) WaitHeaderProgram { |
| 25 | pid := os.getpid() |
| 26 | src := os.join_path(os.temp_dir(), 'v3_wait_header_${name}_${pid}.v') |
| 27 | out := os.join_path(os.temp_dir(), 'v3_wait_header_${name}_${pid}') |
| 28 | os.write_file(src, source) or { panic(err) } |
| 29 | os.rm(out) or {} |
| 30 | os.rm(out + '.c') or {} |
| 31 | compile := os.execute('${v3_bin} ${src} -b c -o ${out}') |
| 32 | assert compile.exit_code == 0, compile.output |
| 33 | return WaitHeaderProgram{ |
| 34 | c_code: os.read_file(out + '.c') or { panic(err) } |
| 35 | out: out |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | fn wait_header_gen_c(v3_bin string, name string, source string) string { |
| 40 | pid := os.getpid() |
| 41 | src := os.join_path(os.temp_dir(), 'v3_wait_header_${name}_${pid}.v') |
| 42 | c_path := os.join_path(os.temp_dir(), 'v3_wait_header_${name}_${pid}.c') |
| 43 | os.write_file(src, source) or { panic(err) } |
| 44 | os.rm(c_path) or {} |
| 45 | compile := os.execute('${v3_bin} ${src} -b c -o ${c_path}') |
| 46 | assert compile.exit_code == 0, compile.output |
| 47 | return os.read_file(c_path) or { panic(err) } |
| 48 | } |
| 49 | |
| 50 | fn wait_header_has_include_directive(c_code string) bool { |
| 51 | for line in c_code.split_into_lines() { |
| 52 | if line.trim_space().starts_with('#include') { |
| 53 | return true |
| 54 | } |
| 55 | } |
| 56 | return false |
| 57 | } |
| 58 | |
| 59 | fn test_os_import_uses_waitpid_without_headers() { |
| 60 | $if windows { |
| 61 | return |
| 62 | } |
| 63 | v3_bin := wait_header_build_v3() |
| 64 | with_os := wait_header_compile(v3_bin, 'with_os_execute', 'module main |
| 65 | |
| 66 | import os |
| 67 | |
| 68 | fn main() { |
| 69 | result := os.execute(\'/bin/sh -c "printf waitpid-ok"\') |
| 70 | assert result.exit_code == 0 |
| 71 | stat_info := os.stat(@FILE) or { panic(err) } |
| 72 | assert stat_info.size > 0 |
| 73 | uname_info := os.uname() |
| 74 | assert uname_info.sysname.len > 0 |
| 75 | entries := os.ls(os.dir(@FILE)) or { panic(err) } |
| 76 | usage := os.disk_usage(os.dir(@FILE)) or { panic(err) } |
| 77 | os.signal_ignore(.pipe) |
| 78 | signals_ok := C.SIGSTOP > 0 && C.SIGCONT > 0 && C.SIGTERM == 15 && C.SIGKILL == 9 |
| 79 | println(result.output) |
| 80 | println((entries.len > 0 && usage.total > 0 && signals_ok).str()) |
| 81 | } |
| 82 | ') |
| 83 | assert !wait_header_has_include_directive(with_os.c_code), with_os.c_code |
| 84 | assert with_os.c_code.contains('waitpid('), with_os.c_code |
| 85 | assert with_os.c_code.contains('int close(int fd);'), with_os.c_code |
| 86 | assert with_os.c_code.contains('typedef _Bool bool;'), with_os.c_code |
| 87 | assert with_os.c_code.contains('typedef unsigned char bool;'), with_os.c_code |
| 88 | assert with_os.c_code.contains('#define __bool_true_false_are_defined 1'), with_os.c_code |
| 89 | assert with_os.c_code.contains('#if !defined(__FILE_defined) && !defined(_FILE_DEFINED) && !defined(_FILEDEFED) && !defined(__DEFINED_FILE) && !defined(_FILE_DECLARED) && !defined(__FILE_DECLARED)'), with_os.c_code |
| 90 | assert with_os.c_code.contains('typedef intptr_t ssize_t;'), with_os.c_code |
| 91 | assert with_os.c_code.contains('int setenv(const char* name, const char* value, int overwrite);'), with_os.c_code |
| 92 | assert with_os.c_code.contains('void abort(void);'), with_os.c_code |
| 93 | assert with_os.c_code.contains('void* memset(void* s, int c, size_t n);'), with_os.c_code |
| 94 | assert with_os.c_code.contains('void* memcpy(void* dest, const void* src, size_t n);'), with_os.c_code |
| 95 | assert with_os.c_code.contains('void* memmove(void* dest, const void* src, size_t n);'), with_os.c_code |
| 96 | assert with_os.c_code.contains('int memcmp(const void* s1, const void* s2, size_t n);'), with_os.c_code |
| 97 | assert with_os.c_code.contains('size_t strlen(const char* s);'), with_os.c_code |
| 98 | assert !with_os.c_code.contains('i32 strlen(char* s);'), with_os.c_code |
| 99 | assert with_os.c_code.contains('int strcmp(const char* s1, const char* s2);'), with_os.c_code |
| 100 | assert with_os.c_code.contains('int strncmp(const char* s1, const char* s2, size_t n);'), with_os.c_code |
| 101 | assert with_os.c_code.contains('char* strncpy(char* dest, const char* src, size_t n);'), with_os.c_code |
| 102 | assert with_os.c_code.contains('double floor(double x);'), with_os.c_code |
| 103 | assert with_os.c_code.contains('double ceil(double x);'), with_os.c_code |
| 104 | assert with_os.c_code.contains('float floorf(float x);'), with_os.c_code |
| 105 | assert with_os.c_code.contains('float ceilf(float x);'), with_os.c_code |
| 106 | assert with_os.c_code.contains('double sqrt(double x);'), with_os.c_code |
| 107 | assert with_os.c_code.contains('double pow(double x, double y);'), with_os.c_code |
| 108 | assert with_os.c_code.contains('double ldexp(double x, int exp);'), with_os.c_code |
| 109 | assert with_os.c_code.contains('double fmod(double x, double y);'), with_os.c_code |
| 110 | assert with_os.c_code.contains('double cos(double x);'), with_os.c_code |
| 111 | assert with_os.c_code.contains('double acos(double x);'), with_os.c_code |
| 112 | assert with_os.c_code.contains('double fabs(double x);'), with_os.c_code |
| 113 | assert with_os.c_code.contains('int open(const char* path, int flags, ...);'), with_os.c_code |
| 114 | assert with_os.c_code.contains('ssize_t read(int fd, void* buf, size_t count);'), with_os.c_code |
| 115 | assert with_os.c_code.contains('int fork(void);'), with_os.c_code |
| 116 | assert with_os.c_code.contains('int dup2(int oldfd, int newfd);'), with_os.c_code |
| 117 | assert with_os.c_code.contains('int execlp(const char* file, const char* arg, ...);'), with_os.c_code |
| 118 | assert with_os.c_code.contains('int execvp(const char* file, char* const argv[]);'), with_os.c_code |
| 119 | assert with_os.c_code.contains('void _exit(int status);'), with_os.c_code |
| 120 | ssize_typedef_idx := with_os.c_code.index('typedef intptr_t ssize_t;') or { -1 } |
| 121 | read_proto_idx := with_os.c_code.index('ssize_t read(int fd, void* buf, size_t count);') or { |
| 122 | -1 |
| 123 | } |
| 124 | assert ssize_typedef_idx >= 0 && ssize_typedef_idx < read_proto_idx, with_os.c_code |
| 125 | assert with_os.c_code.contains('int access(const char* path, int mode);'), with_os.c_code |
| 126 | assert with_os.c_code.contains('char* realpath(const char* path, char* resolved_path);'), with_os.c_code |
| 127 | assert with_os.c_code.contains('char* strrchr(const char* s, int c);'), with_os.c_code |
| 128 | assert with_os.c_code.contains('char* strstr(const char* haystack, const char* needle);'), with_os.c_code |
| 129 | assert with_os.c_code.contains('int snprintf(char* str, size_t size, const char* format, ...);'), with_os.c_code |
| 130 | assert with_os.c_code.contains('#elif defined(__ANDROID__)'), with_os.c_code |
| 131 | assert with_os.c_code.contains('int* __errno(void);'), with_os.c_code |
| 132 | assert with_os.c_code.contains('#define errno (*__errno())'), with_os.c_code |
| 133 | assert with_os.c_code.contains('int* __errno_location(void);'), with_os.c_code |
| 134 | assert with_os.c_code.contains('#define errno (*__errno_location())'), with_os.c_code |
| 135 | assert with_os.c_code.contains('#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__DragonFly__)'), with_os.c_code |
| 136 | assert with_os.c_code.contains('#define stdout __stdoutp'), with_os.c_code |
| 137 | assert with_os.c_code.contains('#elif defined(__OpenBSD__)'), with_os.c_code |
| 138 | assert with_os.c_code.contains('extern struct __sFstub __stdout[];'), with_os.c_code |
| 139 | assert with_os.c_code.contains('#define stdout ((FILE*)__stdout)'), with_os.c_code |
| 140 | assert with_os.c_code.contains('#elif defined(__NetBSD__)'), with_os.c_code |
| 141 | assert with_os.c_code.contains('struct __netbsd_FILE_stub { unsigned char _opaque[152]; };'), with_os.c_code |
| 142 | assert with_os.c_code.contains('#define stdout ((FILE*)&__sF[1])'), with_os.c_code |
| 143 | assert with_os.c_code.contains('typedef union { unsigned char _opaque[128]; long long _align; } pthread_mutex_t;'), with_os.c_code |
| 144 | assert with_os.c_code.contains('typedef union { unsigned char _opaque[256]; long long _align; } pthread_rwlock_t;'), with_os.c_code |
| 145 | assert with_os.c_code.contains('#define PTHREAD_MUTEX_INITIALIZER'), with_os.c_code |
| 146 | assert with_os.c_code.contains('int pthread_mutex_lock(void* mutex);'), with_os.c_code |
| 147 | assert with_os.c_code.contains('int pthread_mutex_unlock(void* mutex);'), with_os.c_code |
| 148 | assert with_os.c_code.contains('typedef union { unsigned char _opaque[128]; long long _align; } sigset_t;'), with_os.c_code |
| 149 | assert with_os.c_code.contains('struct winsize { unsigned short ws_row; unsigned short ws_col; unsigned short ws_xpixel; unsigned short ws_ypixel; };'), with_os.c_code |
| 150 | assert with_os.c_code.contains('typedef union epoll_data { void* ptr; int fd; u32 u32; u64 u64; } epoll_data_t;'), with_os.c_code |
| 151 | assert with_os.c_code.contains('struct epoll_event { u32 events; epoll_data_t data; } __attribute__((packed));'), with_os.c_code |
| 152 | assert with_os.c_code.contains('#elif defined(__linux__) && (defined(__i386__) || defined(__arm__))'), with_os.c_code |
| 153 | assert with_os.c_code.contains('struct dirent { unsigned long d_ino; long d_off; unsigned short d_reclen; unsigned char d_type; char d_name[256]; };'), with_os.c_code |
| 154 | assert with_os.c_code.contains('struct dirent { u64 d_ino; i64 d_off; unsigned short d_reclen; unsigned char d_type; char d_name[256]; };'), with_os.c_code |
| 155 | assert with_os.c_code.contains('struct dirent { u64 d_ino; u64 d_seekoff; u16 d_reclen; u16 d_namlen; u8 d_type; char d_name[1024]; };'), with_os.c_code |
| 156 | assert with_os.c_code.contains('struct dirent { u64 d_ino; i64 d_seekoff; u16 d_reclen; u8 d_type; u8 __pad0; u16 d_namlen; u16 __pad1; char d_name[256]; };'), with_os.c_code |
| 157 | assert with_os.c_code.contains('struct statvfs { unsigned long f_flag; unsigned long f_bsize; unsigned long f_frsize; unsigned long f_iosize; u64 f_blocks;'), with_os.c_code |
| 158 | assert with_os.c_code.contains('char f_mntfromlabel[1024];'), with_os.c_code |
| 159 | assert with_os.c_code.contains('struct statvfs { unsigned long f_bsize; unsigned long f_frsize; unsigned long f_blocks; unsigned long f_bfree; unsigned long f_bavail;'), with_os.c_code |
| 160 | assert with_os.c_code.contains('#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)'), with_os.c_code |
| 161 | assert with_os.c_code.contains('struct utsname { char sysname[256]; char nodename[256]; char release[256]; char version[256]; char machine[256]; };'), with_os.c_code |
| 162 | assert with_os.c_code.contains('struct utsname { char sysname[65]; char nodename[65]; char release[65]; char version[65]; char machine[65]; char domainname[65]; };'), with_os.c_code |
| 163 | assert with_os.c_code.contains('#if defined(__x86_64__) && !defined(__ILP32__)'), with_os.c_code |
| 164 | assert with_os.c_code.contains('struct stat { u64 st_dev; u64 st_ino; u64 st_nlink; u32 st_mode; u32 st_uid; u32 st_gid; int __pad0; u64 st_rdev; i64 st_size; i64 st_blksize; i64 st_blocks; i64 st_atime; i64 st_atimensec; i64 st_mtime; i64 st_mtimensec; i64 st_ctime; i64 st_ctimensec; i64 __glibc_reserved[3]; };'), with_os.c_code |
| 165 | assert with_os.c_code.contains('#elif defined(__aarch64__) || (defined(__riscv) && __riscv_xlen == 64) || defined(__loongarch_lp64)'), with_os.c_code |
| 166 | assert with_os.c_code.contains('#elif defined(__i386__) || defined(__arm__)'), with_os.c_code |
| 167 | assert with_os.c_code.contains('struct stat { u64 st_dev; unsigned short __pad1; unsigned long st_ino; u32 st_mode; unsigned long st_nlink;'), with_os.c_code |
| 168 | assert with_os.c_code.contains('#error unsupported Linux struct stat layout for this architecture'), with_os.c_code |
| 169 | assert with_os.c_code.contains('i32 st_atim_ext; i64 st_atime; long st_atimensec;'), with_os.c_code |
| 170 | assert with_os.c_code.contains('struct stat { u64 st_dev; u64 st_ino; u64 st_nlink; u16 st_mode; i16 st_bsdflags;'), with_os.c_code |
| 171 | assert with_os.c_code.contains('struct stat { u32 st_mode; i32 st_dev; u64 st_ino; u32 st_nlink;'), with_os.c_code |
| 172 | assert with_os.c_code.contains('struct stat { u64 st_dev; u32 st_mode; u64 st_ino; u32 st_nlink;'), with_os.c_code |
| 173 | assert with_os.c_code.contains('struct stat { u64 st_ino; u32 st_nlink; u32 st_dev; u16 st_mode;'), with_os.c_code |
| 174 | assert with_os.c_code.contains('#elif defined(__sun)'), with_os.c_code |
| 175 | assert with_os.c_code.contains('struct stat { u64 st_dev; u64 st_ino; u32 st_mode; u32 st_nlink; u32 st_uid; u32 st_gid;'), with_os.c_code |
| 176 | assert with_os.c_code.contains('char st_fstype[16];'), with_os.c_code |
| 177 | assert with_os.c_code.contains('#elif defined(__QNX__) || defined(__QNXNTO__)'), with_os.c_code |
| 178 | assert with_os.c_code.contains('#if _FILE_OFFSET_BITS - 0 == 64'), with_os.c_code |
| 179 | assert with_os.c_code.contains('struct stat { u64 st_ino; i64 st_size; u64 st_dev; u64 st_rdev;'), with_os.c_code |
| 180 | assert with_os.c_code.contains('#elif defined(__BIGENDIAN__)'), with_os.c_code |
| 181 | assert with_os.c_code.contains('#error unsupported headerless Unix struct stat layout for this platform'), with_os.c_code |
| 182 | assert with_os.c_code.contains('i64 st_birthtime;'), with_os.c_code |
| 183 | assert with_os.c_code.contains('struct rusage { struct timeval ru_utime; struct timeval ru_stime; long ru_maxrss; long ru_ixrss; long ru_idrss;'), with_os.c_code |
| 184 | assert with_os.c_code.contains('#if !defined(_STRUCT_TIMESPEC) && !defined(_TIMESPEC_DEFINED) && !defined(_TIMESPEC_DECLARED) && !defined(__timespec_defined)'), with_os.c_code |
| 185 | assert with_os.c_code.contains('typedef struct timespec timespec;'), with_os.c_code |
| 186 | assert !with_os.c_code.contains('typedef struct stat stat;'), with_os.c_code |
| 187 | assert !with_os.c_code.contains('typedef struct sigset_t sigset_t;'), with_os.c_code |
| 188 | assert !with_os.c_code.contains('struct winsize {\n\tws_row'), with_os.c_code |
| 189 | assert !with_os.c_code.contains('struct rusage {\n\tru_maxrss int'), with_os.c_code |
| 190 | assert !with_os.c_code.contains('typedef union epoll_data_t epoll_data_t;'), with_os.c_code |
| 191 | assert !with_os.c_code.contains('u64 d_seekoff;\n\tu64 d_reclen;'), with_os.c_code |
| 192 | assert with_os.c_code.contains('#define SIGSTOP'), with_os.c_code |
| 193 | assert with_os.c_code.contains('#define SIGCONT'), with_os.c_code |
| 194 | assert with_os.c_code.contains('#define SIGTERM 15'), with_os.c_code |
| 195 | assert with_os.c_code.contains('#define SIGKILL 9'), with_os.c_code |
| 196 | assert with_os.c_code.contains('#define SIGPIPE 13'), with_os.c_code |
| 197 | assert with_os.c_code.contains('#define SIG_IGN ((void*)1)'), with_os.c_code |
| 198 | assert with_os.c_code.contains('#define SIG_BLOCK'), with_os.c_code |
| 199 | assert with_os.c_code.contains('#define PTRACE_ATTACH 16'), with_os.c_code |
| 200 | assert with_os.c_code.contains('#define PTRACE_DETACH 17'), with_os.c_code |
| 201 | assert with_os.c_code.contains('#define PT_TRACE_ME 0'), with_os.c_code |
| 202 | assert with_os.c_code.contains('#define PT_ATTACH 10'), with_os.c_code |
| 203 | assert with_os.c_code.contains('#define PT_DETACH 11'), with_os.c_code |
| 204 | assert with_os.c_code.contains('#define WNOHANG 1'), with_os.c_code |
| 205 | assert with_os.c_code.contains('#define ENOENT 2'), with_os.c_code |
| 206 | assert with_os.c_code.contains('#define RUSAGE_SELF 0'), with_os.c_code |
| 207 | assert with_os.c_code.contains('#define EACCES 13'), with_os.c_code |
| 208 | assert with_os.c_code.contains('#define EIO 5'), with_os.c_code |
| 209 | assert with_os.c_code.contains('#define EBADF 9'), with_os.c_code |
| 210 | assert with_os.c_code.contains('#define ENOMEM 12'), with_os.c_code |
| 211 | assert with_os.c_code.contains('#define EFAULT 14'), with_os.c_code |
| 212 | assert with_os.c_code.contains('#define ESPIPE 29'), with_os.c_code |
| 213 | assert with_os.c_code.contains('#define EOVERFLOW 75'), with_os.c_code |
| 214 | assert with_os.c_code.contains('#define _SC_PAGESIZE 0x0027'), with_os.c_code |
| 215 | assert with_os.c_code.contains('#define _SC_NPROCESSORS_ONLN 0x0061'), with_os.c_code |
| 216 | assert with_os.c_code.contains('#define _SC_PHYS_PAGES 0x0062'), with_os.c_code |
| 217 | assert with_os.c_code.contains('#define _SC_AVPHYS_PAGES 0x0063'), with_os.c_code |
| 218 | assert with_os.c_code.contains('#define _SC_PAGESIZE 30'), with_os.c_code |
| 219 | assert with_os.c_code.contains('#define _SC_NPROCESSORS_ONLN 84'), with_os.c_code |
| 220 | assert with_os.c_code.contains('#define _SC_PHYS_PAGES 85'), with_os.c_code |
| 221 | assert with_os.c_code.contains('#define _SC_AVPHYS_PAGES 86'), with_os.c_code |
| 222 | assert with_os.c_code.contains('#define PROT_READ 0x1'), with_os.c_code |
| 223 | assert with_os.c_code.contains('#define PROT_WRITE 0x2'), with_os.c_code |
| 224 | assert with_os.c_code.contains('#define PROT_EXEC 0x4'), with_os.c_code |
| 225 | assert with_os.c_code.contains('#define MAP_PRIVATE 0x0002'), with_os.c_code |
| 226 | assert with_os.c_code.contains('#define MAP_ANONYMOUS 0x20'), with_os.c_code |
| 227 | assert with_os.c_code.contains('#define MAP_ANONYMOUS 0x1000'), with_os.c_code |
| 228 | assert with_os.c_code.contains('#define MAP_FAILED ((void*)-1)'), with_os.c_code |
| 229 | assert with_os.c_code.contains('#define SYS_getrandom 318'), with_os.c_code |
| 230 | assert with_os.c_code.contains('#define SYS_getrandom 355'), with_os.c_code |
| 231 | assert with_os.c_code.contains('#define SYS_getrandom 384'), with_os.c_code |
| 232 | assert with_os.c_code.contains('#define SYS_getrandom 278'), with_os.c_code |
| 233 | assert with_os.c_code.contains('#define CTL_KERN 1'), with_os.c_code |
| 234 | assert with_os.c_code.contains('#define CTL_VM 2'), with_os.c_code |
| 235 | assert with_os.c_code.contains('#define KERN_PROC_PATHNAME 12'), with_os.c_code |
| 236 | assert with_os.c_code.contains('#define KERN_PROC_INC_THREAD 0x10'), with_os.c_code |
| 237 | assert with_os.c_code.contains('#define KERN_PROC_ARGS 55'), with_os.c_code |
| 238 | assert with_os.c_code.contains('#define KERN_PROC_ARGV 1'), with_os.c_code |
| 239 | assert with_os.c_code.contains('#define VM_UVMEXP 4'), with_os.c_code |
| 240 | assert with_os.c_code.contains('__atomic_load_4((u32*)ptr, 5)'), with_os.c_code |
| 241 | assert with_os.c_code.contains('__atomic_load_8((u64*)ptr, 5)'), with_os.c_code |
| 242 | assert with_os.c_code.contains('__atomic_load_n((void**)ptr, 5)'), with_os.c_code |
| 243 | assert !with_os.c_code.contains('*(void* volatile*)ptr'), with_os.c_code |
| 244 | assert with_os.c_code.contains('#define SOCK_NONBLOCK 04000'), with_os.c_code |
| 245 | assert with_os.c_code.contains('#define SOCK_NONBLOCK 0x20000000'), with_os.c_code |
| 246 | assert with_os.c_code.contains('#define SO_REUSEPORT 15'), with_os.c_code |
| 247 | assert with_os.c_code.contains('#define TCP_QUICKACK 12'), with_os.c_code |
| 248 | assert with_os.c_code.contains('#define TCP_DEFER_ACCEPT 9'), with_os.c_code |
| 249 | assert with_os.c_code.contains('#define TCP_FASTOPEN 23'), with_os.c_code |
| 250 | assert with_os.c_code.contains('#define SOMAXCONN 4096'), with_os.c_code |
| 251 | assert with_os.c_code.contains('#define MEM_COMMIT 0x00001000U'), with_os.c_code |
| 252 | assert with_os.c_code.contains('#define MEM_RESERVE 0x00002000U'), with_os.c_code |
| 253 | assert with_os.c_code.contains('#define PAGE_READWRITE 0x04U'), with_os.c_code |
| 254 | assert with_os.c_code.contains('#define PAGE_EXECUTE_READ 0x20U'), with_os.c_code |
| 255 | assert with_os.c_code.contains('#define TLS_OUT_OF_INDEXES 0xffffffffU'), with_os.c_code |
| 256 | assert with_os.c_code.contains('#define SOCKET_ERROR (-1)'), with_os.c_code |
| 257 | assert with_os.c_code.contains('#define WSAEWOULDBLOCK 10035'), with_os.c_code |
| 258 | run := os.execute(with_os.out) |
| 259 | assert run.exit_code == 0, run.output |
| 260 | assert run.output.trim_space() == 'waitpid-ok\ntrue', run.output |
| 261 | |
| 262 | hello := wait_header_compile(v3_bin, 'hello', "module main |
| 263 | |
| 264 | fn main() { |
| 265 | println('hello') |
| 266 | } |
| 267 | ") |
| 268 | assert !wait_header_has_include_directive(hello.c_code), hello.c_code |
| 269 | } |
| 270 | |
| 271 | fn test_user_c_decl_emits_extern_prototype_without_headers() { |
| 272 | $if windows { |
| 273 | return |
| 274 | } |
| 275 | v3_bin := wait_header_build_v3() |
| 276 | program := wait_header_compile(v3_bin, 'user_c_getppid', 'module main |
| 277 | |
| 278 | #flag -Werror=implicit-function-declaration |
| 279 | |
| 280 | fn C.getppid() int |
| 281 | |
| 282 | fn main() { |
| 283 | println(C.getppid().str()) |
| 284 | } |
| 285 | ') |
| 286 | assert !wait_header_has_include_directive(program.c_code), program.c_code |
| 287 | assert program.c_code.contains('int getppid(void);'), program.c_code |
| 288 | run := os.execute(program.out) |
| 289 | assert run.exit_code == 0, run.output |
| 290 | assert run.output.trim_space().int() > 0, run.output |
| 291 | } |
| 292 | |
| 293 | fn test_windows_crt_underscore_c_decls_emit_extern_prototypes() { |
| 294 | v3_bin := wait_header_build_v3() |
| 295 | c_code := wait_header_gen_c(v3_bin, 'windows_crt_underscore_decls', 'module main |
| 296 | |
| 297 | fn C._wfopen(&u16, &u16) voidptr |
| 298 | fn C._wsystem(&u16) int |
| 299 | fn C._wgetenv(&u16) voidptr |
| 300 | fn C._waccess(&u16, int) int |
| 301 | fn C._wchdir(&u16) int |
| 302 | fn C._chsize_s(voidptr, u64) int |
| 303 | |
| 304 | fn main() { |
| 305 | p := &u16(unsafe { nil }) |
| 306 | h := voidptr(0) |
| 307 | _ = C._wfopen(p, p) |
| 308 | _ = C._wsystem(p) |
| 309 | _ = C._wgetenv(p) |
| 310 | _ = C._waccess(p, 0) |
| 311 | _ = C._wchdir(p) |
| 312 | _ = C._chsize_s(h, u64(0)) |
| 313 | } |
| 314 | ') |
| 315 | assert !wait_header_has_include_directive(c_code), c_code |
| 316 | assert c_code.contains('#ifdef _MSC_VER'), c_code |
| 317 | assert c_code.contains('typedef unsigned __int64 size_t;'), c_code |
| 318 | assert c_code.contains('typedef __int64 ptrdiff_t;'), c_code |
| 319 | assert c_code.contains('typedef unsigned __int64 uintptr_t;'), c_code |
| 320 | assert c_code.contains('typedef __int64 intptr_t;'), c_code |
| 321 | assert c_code.contains('#elif defined(_WIN32)'), c_code |
| 322 | assert c_code.contains('int* _errno(void);'), c_code |
| 323 | assert c_code.contains('#define errno (*_errno())'), c_code |
| 324 | assert c_code.contains('void* _wfopen(u16*, u16*);'), c_code |
| 325 | assert c_code.contains('int _wsystem(u16*);'), c_code |
| 326 | assert c_code.contains('void* _wgetenv(u16*);'), c_code |
| 327 | assert c_code.contains('int _waccess(u16*, int);'), c_code |
| 328 | assert c_code.contains('int _wchdir(u16*);'), c_code |
| 329 | assert c_code.contains('int _chsize_s(void*, u64);'), c_code |
| 330 | } |
| 331 | |
| 332 | fn test_windows_sdk_types_are_emitted_before_extern_prototypes() { |
| 333 | v3_bin := wait_header_build_v3() |
| 334 | c_code := wait_header_gen_c(v3_bin, 'windows_sdk_type_order', 'module main |
| 335 | |
| 336 | @[typedef] |
| 337 | struct C.SECURITY_ATTRIBUTES {} |
| 338 | |
| 339 | fn C.CreateHardLinkW(&u16, &u16, &C.SECURITY_ATTRIBUTES) int |
| 340 | |
| 341 | fn main() { |
| 342 | path := &u16(unsafe { nil }) |
| 343 | attrs := &C.SECURITY_ATTRIBUTES(unsafe { nil }) |
| 344 | _ = C.CreateHardLinkW(path, path, attrs) |
| 345 | } |
| 346 | ') |
| 347 | security_typedef := 'typedef struct SECURITY_ATTRIBUTES { DWORD nLength; void* lpSecurityDescriptor; BOOL bInheritHandle; } SECURITY_ATTRIBUTES;' |
| 348 | security_typedef_idx := c_code.index(security_typedef) or { -1 } |
| 349 | prototype_idx := c_code.index('int WINAPI CreateHardLinkW(u16*, u16*, SECURITY_ATTRIBUTES*);') or { |
| 350 | -1 |
| 351 | } |
| 352 | assert security_typedef_idx >= 0, c_code |
| 353 | assert prototype_idx >= 0, c_code |
| 354 | assert security_typedef_idx < prototype_idx, c_code |
| 355 | assert !c_code.contains('typedef struct SECURITY_ATTRIBUTES SECURITY_ATTRIBUTES;'), c_code |
| 356 | } |
| 357 | |
| 358 | fn test_winapi_extern_prototypes_use_calling_convention() { |
| 359 | v3_bin := wait_header_build_v3() |
| 360 | c_code := wait_header_gen_c(v3_bin, 'winapi_calling_convention', 'module main |
| 361 | |
| 362 | fn C.GetStdHandle(u32) voidptr |
| 363 | fn C.CreateFileW(&u16, u32, u32, voidptr, u32, u32, voidptr) voidptr |
| 364 | |
| 365 | fn main() { |
| 366 | path := &u16(unsafe { nil }) |
| 367 | _ = C.GetStdHandle(u32(0)) |
| 368 | _ = C.CreateFileW(path, 0, 0, voidptr(0), 0, 0, voidptr(0)) |
| 369 | } |
| 370 | ') |
| 371 | assert c_code.contains('#ifndef WINAPI'), c_code |
| 372 | assert c_code.contains('#define WINAPI __stdcall'), c_code |
| 373 | assert c_code.contains('void* WINAPI GetStdHandle(u32);'), c_code |
| 374 | assert c_code.contains('void* WINAPI CreateFileW(u16*, u32, u32, void*, u32, u32, void*);'), c_code |
| 375 | assert !c_code.contains('void* GetStdHandle(u32);'), c_code |
| 376 | assert !c_code.contains('void* CreateFileW(u16*, u32, u32, void*, u32, u32, void*);'), c_code |
| 377 | } |
| 378 | |
| 379 | fn test_unsuffixed_winapi_decls_use_wide_exports() { |
| 380 | v3_bin := wait_header_build_v3() |
| 381 | c_code := wait_header_gen_c(v3_bin, 'winapi_unsuffixed_wide_exports', 'module main |
| 382 | |
| 383 | fn C.GetModuleFileName(voidptr, &u16, u32) u32 |
| 384 | fn C.CreateFile(&u16, u32, u32, voidptr, u32, u32, voidptr) voidptr |
| 385 | fn C.LoadLibrary(&u16) voidptr |
| 386 | fn C.DefWindowProc(voidptr, u32, usize, isize) isize |
| 387 | |
| 388 | fn main() { |
| 389 | path := &u16(unsafe { nil }) |
| 390 | _ = C.GetModuleFileName(voidptr(0), path, 0) |
| 391 | _ = C.CreateFile(path, 0, 0, voidptr(0), 0, 0, voidptr(0)) |
| 392 | _ = C.LoadLibrary(path) |
| 393 | _ = voidptr(&C.DefWindowProc) |
| 394 | } |
| 395 | ') |
| 396 | assert c_code.contains('u32 WINAPI GetModuleFileNameW(void*, u16*, u32);'), c_code |
| 397 | assert c_code.contains('void* WINAPI CreateFileW(u16*, u32, u32, void*, u32, u32, void*);'), c_code |
| 398 | assert c_code.contains('void* WINAPI LoadLibraryW(u16*);'), c_code |
| 399 | assert c_code.contains('ptrdiff_t WINAPI DefWindowProcW(void*, u32, size_t, ptrdiff_t);'), c_code |
| 400 | assert c_code.contains('GetModuleFileNameW('), c_code |
| 401 | assert c_code.contains('CreateFileW('), c_code |
| 402 | assert c_code.contains('LoadLibraryW('), c_code |
| 403 | assert c_code.contains('&DefWindowProcW'), c_code |
| 404 | assert !c_code.contains('GetModuleFileName('), c_code |
| 405 | assert !c_code.contains('CreateFile('), c_code |
| 406 | assert !c_code.contains('LoadLibrary('), c_code |
| 407 | assert !c_code.contains('&DefWindowProc)'), c_code |
| 408 | } |
| 409 | |
| 410 | fn test_synthesized_c_extern_prototypes_preserve_const_pointer_params() { |
| 411 | v3_bin := wait_header_build_v3() |
| 412 | c_code := wait_header_gen_c(v3_bin, 'c_extern_const_pointer_params', 'module main |
| 413 | |
| 414 | fn C.SSL_CTX_load_verify_locations(ctx voidptr, const_file &char, const_ca_path &char) int |
| 415 | |
| 416 | fn main() { |
| 417 | path := &char(unsafe { nil }) |
| 418 | _ = C.SSL_CTX_load_verify_locations(voidptr(0), path, path) |
| 419 | } |
| 420 | ') |
| 421 | assert c_code.contains('int SSL_CTX_load_verify_locations(void* ctx, const char* const_file, const char* const_ca_path);'), c_code |
| 422 | assert !c_code.contains('int SSL_CTX_load_verify_locations(void* ctx, char* const_file'), c_code |
| 423 | } |
| 424 | |
| 425 | fn test_windows_sync_structs_use_headerless_preamble_storage() { |
| 426 | v3_bin := wait_header_build_v3() |
| 427 | c_code := wait_header_gen_c(v3_bin, 'windows_sync_struct_storage', 'module main |
| 428 | |
| 429 | @[typedef] |
| 430 | struct C.SRWLOCK {} |
| 431 | |
| 432 | @[typedef] |
| 433 | struct C.CONDITION_VARIABLE {} |
| 434 | |
| 435 | struct SyncHolder { |
| 436 | rw C.SRWLOCK |
| 437 | cv C.CONDITION_VARIABLE |
| 438 | } |
| 439 | |
| 440 | fn main() { |
| 441 | _ := SyncHolder{} |
| 442 | } |
| 443 | ') |
| 444 | assert !wait_header_has_include_directive(c_code), c_code |
| 445 | assert c_code.contains('typedef struct SRWLOCK { void* Ptr; } SRWLOCK;'), c_code |
| 446 | assert c_code.contains('typedef struct CONDITION_VARIABLE { void* Ptr; } CONDITION_VARIABLE;'), c_code |
| 447 | assert c_code.contains('SRWLOCK rw;'), c_code |
| 448 | assert c_code.contains('CONDITION_VARIABLE cv;'), c_code |
| 449 | assert !c_code.contains('typedef struct SRWLOCK SRWLOCK;'), c_code |
| 450 | assert !c_code.contains('typedef struct CONDITION_VARIABLE CONDITION_VARIABLE;'), c_code |
| 451 | } |
| 452 | |
| 453 | fn test_epoll_data_tag_uses_headerless_preamble_definition() { |
| 454 | v3_bin := wait_header_build_v3() |
| 455 | c_code := wait_header_gen_c(v3_bin, 'epoll_data_tag_storage', 'module main |
| 456 | |
| 457 | @[typedef] |
| 458 | union C.epoll_data { |
| 459 | mut: |
| 460 | ptr voidptr |
| 461 | fd int |
| 462 | u32 u32 |
| 463 | u64 u64 |
| 464 | } |
| 465 | |
| 466 | @[packed] |
| 467 | struct C.epoll_event { |
| 468 | events u32 |
| 469 | data C.epoll_data |
| 470 | } |
| 471 | |
| 472 | fn main() { |
| 473 | _ := C.epoll_event{} |
| 474 | } |
| 475 | ') |
| 476 | assert c_code.contains('typedef union epoll_data { void* ptr; int fd; u32 u32; u64 u64; } epoll_data_t;'), c_code |
| 477 | assert !c_code.contains('union epoll_data {\n'), c_code |
| 478 | } |
| 479 | |
| 480 | fn test_windows_console_records_use_headerless_preamble_definitions() { |
| 481 | v3_bin := wait_header_build_v3() |
| 482 | c_code := wait_header_gen_c(v3_bin, 'windows_console_records', 'module main |
| 483 | |
| 484 | pub union C.Event { |
| 485 | KeyEvent C.KEY_EVENT_RECORD |
| 486 | MouseEvent C.MOUSE_EVENT_RECORD |
| 487 | WindowBufferSizeEvent C.WINDOW_BUFFER_SIZE_RECORD |
| 488 | MenuEvent C.MENU_EVENT_RECORD |
| 489 | FocusEvent C.FOCUS_EVENT_RECORD |
| 490 | } |
| 491 | |
| 492 | @[typedef] |
| 493 | pub struct C.INPUT_RECORD { |
| 494 | EventType u16 |
| 495 | Event C.Event |
| 496 | } |
| 497 | |
| 498 | pub union C.uChar { |
| 499 | mut: |
| 500 | UnicodeChar rune |
| 501 | AsciiChar u8 |
| 502 | } |
| 503 | |
| 504 | @[typedef] |
| 505 | pub struct C.KEY_EVENT_RECORD { |
| 506 | bKeyDown int |
| 507 | wRepeatCount u16 |
| 508 | wVirtualKeyCode u16 |
| 509 | wVirtualScanCode u16 |
| 510 | uChar C.uChar |
| 511 | dwControlKeyState u32 |
| 512 | } |
| 513 | |
| 514 | @[typedef] |
| 515 | pub struct C.MOUSE_EVENT_RECORD { |
| 516 | dwMousePosition C.COORD |
| 517 | dwButtonState u32 |
| 518 | dwControlKeyState u32 |
| 519 | dwEventFlags u32 |
| 520 | } |
| 521 | |
| 522 | @[typedef] |
| 523 | pub struct C.WINDOW_BUFFER_SIZE_RECORD { |
| 524 | dwSize C.COORD |
| 525 | } |
| 526 | |
| 527 | @[typedef] |
| 528 | pub struct C.MENU_EVENT_RECORD { |
| 529 | dwCommandId u32 |
| 530 | } |
| 531 | |
| 532 | @[typedef] |
| 533 | pub struct C.FOCUS_EVENT_RECORD { |
| 534 | bSetFocus int |
| 535 | } |
| 536 | |
| 537 | @[typedef] |
| 538 | pub struct C.COORD { |
| 539 | X i16 |
| 540 | Y i16 |
| 541 | } |
| 542 | |
| 543 | @[typedef] |
| 544 | pub struct C.SMALL_RECT { |
| 545 | Left u16 |
| 546 | Top u16 |
| 547 | Right u16 |
| 548 | Bottom u16 |
| 549 | } |
| 550 | |
| 551 | @[typedef] |
| 552 | pub struct C.CONSOLE_SCREEN_BUFFER_INFO { |
| 553 | dwSize C.COORD |
| 554 | dwCursorPosition C.COORD |
| 555 | wAttributes u16 |
| 556 | srWindow C.SMALL_RECT |
| 557 | dwMaximumWindowSize C.COORD |
| 558 | } |
| 559 | |
| 560 | @[typedef] |
| 561 | pub struct C.CHAR_INFO { |
| 562 | Char C.uChar |
| 563 | Attributes u16 |
| 564 | } |
| 565 | |
| 566 | fn main() { |
| 567 | _ := C.INPUT_RECORD{} |
| 568 | _ := C.CHAR_INFO{} |
| 569 | } |
| 570 | ') |
| 571 | assert c_code.contains('typedef union uChar { u16 UnicodeChar; u8 AsciiChar; } uChar;'), c_code |
| 572 | assert c_code.contains('typedef struct KEY_EVENT_RECORD { int bKeyDown; u16 wRepeatCount; u16 wVirtualKeyCode; u16 wVirtualScanCode; uChar uChar; u32 dwControlKeyState; } KEY_EVENT_RECORD;'), c_code |
| 573 | assert c_code.contains('typedef struct INPUT_RECORD { u16 EventType; Event Event; } INPUT_RECORD;'), c_code |
| 574 | assert !c_code.contains('union uChar {\n'), c_code |
| 575 | assert !c_code.contains('struct uChar'), c_code |
| 576 | assert !c_code.contains('struct KEY_EVENT_RECORD {\n'), c_code |
| 577 | } |
| 578 | |
| 579 | fn test_time_import_uses_platform_tm_layout_without_headers() { |
| 580 | $if windows { |
| 581 | return |
| 582 | } |
| 583 | v3_bin := wait_header_build_v3() |
| 584 | program := wait_header_compile(v3_bin, 'time_tm_layout', 'module main |
| 585 | |
| 586 | import time |
| 587 | |
| 588 | fn main() { |
| 589 | now := time.now() |
| 590 | println((now.year > 0).str()) |
| 591 | } |
| 592 | ') |
| 593 | assert !wait_header_has_include_directive(program.c_code), program.c_code |
| 594 | assert program.c_code.contains('struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; const char* tm_zone; };'), program.c_code |
| 595 | assert program.c_code.contains('typedef struct tm tm;'), program.c_code |
| 596 | assert !program.c_code.contains('int tm_gmtoff;'), program.c_code |
| 597 | run := os.execute(program.out) |
| 598 | assert run.exit_code == 0, run.output |
| 599 | assert run.output.trim_space() == 'true', run.output |
| 600 | } |
| 601 | |
| 602 | fn test_term_import_uses_platform_termios_layout_without_headers() { |
| 603 | $if windows { |
| 604 | return |
| 605 | } |
| 606 | v3_bin := wait_header_build_v3() |
| 607 | program := wait_header_compile(v3_bin, 'term_termios', 'module main |
| 608 | |
| 609 | import term |
| 610 | import term.termios |
| 611 | |
| 612 | fn main() { |
| 613 | mut t := termios.Termios{} |
| 614 | t.disable_echo() |
| 615 | width, height := term.get_terminal_size() |
| 616 | println((width > 0 && height > 0).str()) |
| 617 | } |
| 618 | ') |
| 619 | assert !wait_header_has_include_directive(program.c_code), program.c_code |
| 620 | assert program.c_code.contains('struct termios { size_t c_iflag; size_t c_oflag; size_t c_cflag; size_t c_lflag; u8 c_cc[20]; size_t c_ispeed; size_t c_ospeed; };'), program.c_code |
| 621 | assert program.c_code.contains('struct termios { int c_iflag; int c_oflag; int c_cflag; int c_lflag; u8 c_line; u8 c_cc[32]; int c_ispeed; int c_ospeed; };'), program.c_code |
| 622 | assert program.c_code.contains('struct termios { int c_iflag; int c_oflag; int c_cflag; int c_lflag; u8 c_cc[20]; };'), program.c_code |
| 623 | assert program.c_code.contains('struct termios { int c_iflag; int c_oflag; int c_cflag; int c_lflag; u8 c_cc[20]; u32 reserved[3]; int c_ispeed; int c_ospeed; };'), program.c_code |
| 624 | assert program.c_code.contains('struct termios { int c_iflag; int c_oflag; int c_cflag; int c_lflag; u8 c_cc[20]; int c_ispeed; int c_ospeed; };'), program.c_code |
| 625 | assert program.c_code.contains('#define VMIN'), program.c_code |
| 626 | assert program.c_code.contains('#define TIOCGWINSZ'), program.c_code |
| 627 | run := os.execute(program.out) |
| 628 | assert run.exit_code == 0, run.output |
| 629 | assert run.output.trim_space() == 'true', run.output |
| 630 | } |
| 631 | |
| 632 | fn test_filelock_uses_headerless_fcntl_helpers() { |
| 633 | $if windows { |
| 634 | return |
| 635 | } |
| 636 | v3_bin := wait_header_build_v3() |
| 637 | program := wait_header_compile(v3_bin, 'filelock_helpers', "module main |
| 638 | |
| 639 | import os |
| 640 | |
| 641 | fn C.open(&char, i32, i32) i32 |
| 642 | fn C.close(i32) i32 |
| 643 | fn C.v_filelock_lock(i32, i32, i32, u64, u64) i32 |
| 644 | fn C.v_filelock_unlock(i32, u64, u64) i32 |
| 645 | |
| 646 | fn main() { |
| 647 | path := os.join_path(os.temp_dir(), 'v3_headerless_filelock_target') |
| 648 | os.write_file(path, 'abcdef') or { panic(err) } |
| 649 | fd := C.open(&char(path.str), C.O_RDWR, 0) |
| 650 | println(fd) |
| 651 | lock_result := C.v_filelock_lock(fd, 1, 1, u64(0), u64(3)) |
| 652 | unlock_result := C.v_filelock_unlock(fd, u64(0), u64(3)) |
| 653 | println(lock_result) |
| 654 | println(unlock_result) |
| 655 | C.close(fd) |
| 656 | os.rm(path) or {} |
| 657 | } |
| 658 | ") |
| 659 | assert !wait_header_has_include_directive(program.c_code), program.c_code |
| 660 | assert program.c_code.contains('#define LOCK_EX 2'), program.c_code |
| 661 | assert program.c_code.contains('int open(const char* path, int flags, ...);'), program.c_code |
| 662 | assert !program.c_code.contains('int open(char*'), program.c_code |
| 663 | assert program.c_code.contains('#elif defined(_WIN32)'), program.c_code |
| 664 | assert program.c_code.contains('#define GENERIC_READ 0x80000000U'), program.c_code |
| 665 | assert program.c_code.contains('#define OPEN_ALWAYS 4'), program.c_code |
| 666 | assert program.c_code.contains('static inline int v_filelock_lock(int fd, int exclusive, int immediate, u64 start, u64 len) { struct flock fl;'), program.c_code |
| 667 | assert program.c_code.contains('return fcntl(fd, immediate ? F_SETLK : F_SETLKW, &fl);'), program.c_code |
| 668 | assert program.c_code.contains('typedef struct SECURITY_ATTRIBUTES { DWORD nLength; void* lpSecurityDescriptor; BOOL bInheritHandle; } SECURITY_ATTRIBUTES;'), program.c_code |
| 669 | assert program.c_code.contains('BOOL LockFileEx(HANDLE handle, DWORD flags, DWORD reserved, DWORD low, DWORD high, OVERLAPPED* overlap);'), program.c_code |
| 670 | assert program.c_code.contains('return LockFileEx(handle, flags, 0, low, high, &overlap) ? 0 : -1;'), program.c_code |
| 671 | assert program.c_code.contains('return UnlockFileEx(handle, 0, low, high, &overlap) ? 0 : -1;'), program.c_code |
| 672 | run := os.execute(program.out) |
| 673 | assert run.exit_code == 0, run.output |
| 674 | lines := run.output.trim_space().split_into_lines() |
| 675 | assert lines.len == 3, run.output |
| 676 | assert lines[0].int() != -1, run.output |
| 677 | assert lines[1] == '0', run.output |
| 678 | assert lines[2] == '0', run.output |
| 679 | } |
| 680 | |
| 681 | fn test_net_import_uses_headerless_socket_constants() { |
| 682 | $if windows { |
| 683 | return |
| 684 | } |
| 685 | v3_bin := wait_header_build_v3() |
| 686 | program := wait_header_compile(v3_bin, 'net_socket_constants', 'module main |
| 687 | |
| 688 | import net |
| 689 | |
| 690 | fn C.getaddrinfo(&char, &char, &C.addrinfo, &&C.addrinfo) int |
| 691 | fn C.freeaddrinfo(&C.addrinfo) |
| 692 | |
| 693 | fn main() { |
| 694 | println((int(net.SocketType.tcp) > 0).str()) |
| 695 | println((int(net.AddrFamily.ip) > 0).str()) |
| 696 | host := "127.0.0.1" |
| 697 | service := "80" |
| 698 | mut hints := C.addrinfo{} |
| 699 | unsafe { vmemset(&hints, 0, int(sizeof(hints))) } |
| 700 | hints.ai_family = C.AF_INET |
| 701 | hints.ai_socktype = C.SOCK_STREAM |
| 702 | hints.ai_flags = C.AI_PASSIVE |
| 703 | mut results := &C.addrinfo(unsafe { nil }) |
| 704 | code := C.getaddrinfo(&char(host.str), &char(service.str), &hints, &results) |
| 705 | if code != 0 { |
| 706 | println("false") |
| 707 | return |
| 708 | } |
| 709 | ok := !isnil(results) && results.ai_family == C.AF_INET |
| 710 | C.freeaddrinfo(results) |
| 711 | println(ok.str()) |
| 712 | } |
| 713 | ') |
| 714 | assert !wait_header_has_include_directive(program.c_code), program.c_code |
| 715 | assert program.c_code.contains('typedef unsigned short wchar_t;'), program.c_code |
| 716 | assert program.c_code.contains('typedef unsigned int wchar_t;'), program.c_code |
| 717 | assert program.c_code.contains('typedef uintptr_t SOCKET;'), program.c_code |
| 718 | assert program.c_code.contains('struct fd_set { unsigned int fd_count; SOCKET fd_array[FD_SETSIZE]; };'), program.c_code |
| 719 | assert program.c_code.contains('static inline void v_fd_set(SOCKET fd, fd_set* set)'), program.c_code |
| 720 | assert program.c_code.contains('#elif defined(__APPLE__)'), program.c_code |
| 721 | assert program.c_code.contains('#define __V_FD_BITS 32'), program.c_code |
| 722 | assert program.c_code.contains('struct fd_set { unsigned int fds_bits[FD_SETSIZE / __V_FD_BITS]; };'), program.c_code |
| 723 | assert program.c_code.contains('struct fd_set { unsigned long fds_bits[FD_SETSIZE / __V_FD_BITS]; };'), program.c_code |
| 724 | assert program.c_code.contains('struct kevent { uintptr_t ident; u32 filter; u32 flags; u32 fflags; i64 data; void* udata; u64 ext[4]; };'), program.c_code |
| 725 | assert program.c_code.contains('struct kevent { uintptr_t ident; i16 filter; u16 flags; u32 fflags; intptr_t data; void* udata; };'), program.c_code |
| 726 | assert program.c_code.contains('struct winsize { unsigned short ws_row; unsigned short ws_col; unsigned short ws_xpixel; unsigned short ws_ypixel; };'), program.c_code |
| 727 | assert program.c_code.contains('struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; size_t ai_addrlen; char* ai_canonname; void* ai_addr; struct addrinfo* ai_next; };'), program.c_code |
| 728 | assert program.c_code.contains('struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; char* ai_canonname; void* ai_addr; struct addrinfo* ai_next; };'), program.c_code |
| 729 | assert program.c_code.contains('struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; void* ai_addr; char* ai_canonname; struct addrinfo* ai_next; };'), program.c_code |
| 730 | assert program.c_code.contains('typedef struct addrinfo addrinfo;'), program.c_code |
| 731 | assert !program.c_code.contains('int ai_family; int ai_socktype; int ai_flags;'), program.c_code |
| 732 | assert program.c_code.contains('struct sockaddr { u8 sa_len; u8 sa_family; char sa_data[14]; };'), program.c_code |
| 733 | assert program.c_code.contains('struct sockaddr_in { u8 sin_len; u8 sin_family; u16 sin_port; u32 sin_addr; char sin_zero[8]; };'), program.c_code |
| 734 | assert program.c_code.contains('struct sockaddr_in6 { u8 sin6_len; u8 sin6_family; u16 sin6_port; u32 sin6_flowinfo; u8 sin6_addr[16]; u32 sin6_scope_id; };'), program.c_code |
| 735 | assert program.c_code.contains('struct sockaddr { u16 sa_family; char sa_data[14]; };'), program.c_code |
| 736 | assert program.c_code.contains('struct sockaddr_in { u16 sin_family; u16 sin_port; u32 sin_addr; char sin_zero[8]; };'), program.c_code |
| 737 | assert program.c_code.contains('struct sockaddr_in6 { u16 sin6_family; u16 sin6_port; u32 sin6_flowinfo; u8 sin6_addr[16]; u32 sin6_scope_id; };'), program.c_code |
| 738 | assert !program.c_code.contains('struct sockaddr_in6 {\n\tu16 sin6_family;\n\tu16 sin6_port;\n\tu32 sin6_addr[4];'), program.c_code |
| 739 | assert program.c_code.contains('#define FLT_EPSILON 1.19209290e-7F'), program.c_code |
| 740 | assert program.c_code.contains('#define DBL_EPSILON 2.2204460492503131e-16'), program.c_code |
| 741 | assert program.c_code.contains('#define FLT_MAX __FLT_MAX__'), program.c_code |
| 742 | assert program.c_code.contains('#define DBL_MAX __DBL_MAX__'), program.c_code |
| 743 | assert program.c_code.contains('#define KEY_EVENT 0x0001'), program.c_code |
| 744 | assert program.c_code.contains('#define MOUSE_MOVED 0x0001'), program.c_code |
| 745 | assert program.c_code.contains('#define DOUBLE_CLICK 0x0002'), program.c_code |
| 746 | assert program.c_code.contains('#define MOUSE_WHEELED 0x0004'), program.c_code |
| 747 | assert program.c_code.contains('#define VK_BACK 0x08'), program.c_code |
| 748 | assert program.c_code.contains('#define VK_RETURN 0x0d'), program.c_code |
| 749 | assert program.c_code.contains('struct timeval { long tv_sec; long tv_usec; };'), program.c_code |
| 750 | assert program.c_code.contains('typedef struct timeval timeval;'), program.c_code |
| 751 | assert program.c_code.contains('struct rusage { struct timeval ru_utime; struct timeval ru_stime; long ru_maxrss; long ru_ixrss; long ru_idrss;'), program.c_code |
| 752 | assert !program.c_code.contains('u64 tv_sec;'), program.c_code |
| 753 | assert program.c_code.contains('struct timespec { i64 tv_sec; long tv_nsec; };'), program.c_code |
| 754 | assert program.c_code.contains('struct timespec { long tv_sec; long tv_nsec; };'), program.c_code |
| 755 | assert program.c_code.contains('typedef struct timespec timespec;'), program.c_code |
| 756 | assert !program.c_code.contains('struct timespec {\n\ttv_sec'), program.c_code |
| 757 | assert program.c_code.contains('#define F_GETFL 3'), program.c_code |
| 758 | assert program.c_code.contains('#define F_SETFL 4'), program.c_code |
| 759 | assert program.c_code.contains('#define SOCK_STREAM 1'), program.c_code |
| 760 | assert program.c_code.contains('#define AF_INET 2'), program.c_code |
| 761 | assert program.c_code.contains('#define SOL_SOCKET'), program.c_code |
| 762 | assert program.c_code.contains('#define EV_SET(kevp, a, b, c, d, e, f) do'), program.c_code |
| 763 | assert program.c_code.contains('#define EVFILT_READ (-1)'), program.c_code |
| 764 | assert program.c_code.contains('#define EVFILT_MACHPORT (-8)'), program.c_code |
| 765 | assert program.c_code.contains('#define EV_ADD 0x0001'), program.c_code |
| 766 | assert program.c_code.contains('#define EV_CLEAR 0x0020'), program.c_code |
| 767 | assert program.c_code.contains('#define EV_ERROR 0x4000'), program.c_code |
| 768 | assert program.c_code.contains('#elif defined(__FreeBSD__)'), program.c_code |
| 769 | assert program.c_code.contains('#define O_CLOEXEC 0x00100000'), program.c_code |
| 770 | assert program.c_code.contains('#define F_SETLK 12'), program.c_code |
| 771 | assert program.c_code.contains('#define AF_INET6 28'), program.c_code |
| 772 | assert program.c_code.contains('#elif defined(__OpenBSD__)'), program.c_code |
| 773 | assert program.c_code.contains('#define O_CLOEXEC 0x10000'), program.c_code |
| 774 | assert program.c_code.contains('#define AF_INET6 24'), program.c_code |
| 775 | assert program.c_code.contains('#elif defined(__sun)'), program.c_code |
| 776 | assert program.c_code.contains('#define O_NONBLOCK 0x80'), program.c_code |
| 777 | assert program.c_code.contains('#define SOCK_STREAM 2'), program.c_code |
| 778 | assert program.c_code.contains('#define AI_PASSIVE 0x0008'), program.c_code |
| 779 | assert program.c_code.contains('#elif defined(__QNX__) || defined(__QNXNTO__)'), program.c_code |
| 780 | |
| 781 | assert program.c_code.contains('#define O_NONBLOCK 000200'), program.c_code |
| 782 | assert program.c_code.contains('#define EINPROGRESS 236'), program.c_code |
| 783 | assert program.c_code.contains('#elif defined(__linux__) || defined(__ANDROID__)'), program.c_code |
| 784 | assert program.c_code.contains('#define EPOLLIN 0x001'), program.c_code |
| 785 | assert program.c_code.contains('#define EPOLLET (1U << 31)'), program.c_code |
| 786 | assert program.c_code.contains('#define EPOLL_CTL_ADD 1'), program.c_code |
| 787 | |
| 788 | assert program.c_code.contains('#error unsupported headerless C platform constants'), program.c_code |
| 789 | |
| 790 | assert program.c_code.contains('#define TCP_NODELAY 1'), program.c_code |
| 791 | run := os.execute(program.out) |
| 792 | assert run.exit_code == 0, run.output |
| 793 | assert run.output.trim_space() == 'true\ntrue\ntrue', run.output |
| 794 | } |
| 795 | |