| ... | ... | @@ -178,6 +178,12 @@ struct wasi_ciovec { |
| 178 | 178 | uint32_t len; |
| 179 | 179 | }; |
| 180 | 180 | |
| 181 | enum wasi_whence { |
| 182 | wasi_whence_set = 0, |
| 183 | wasi_whence_cur = 1, |
| 184 | wasi_whence_end = 2, |
| 185 | }; |
| 186 | |
| 181 | 187 | extern uint8_t **const wasm_memory; |
| 182 | 188 | extern void wasm__start(void); |
| 183 | 189 | |
| ... | ... | @@ -946,6 +952,43 @@ uint32_t wasi_snapshot_preview1_fd_pread(uint32_t fd, uint32_t iovs, uint32_t io |
| 946 | 952 | return wasi_errno_success; |
| 947 | 953 | } |
| 948 | 954 | |
| 955 | uint32_t wasi_snapshot_preview1_fd_seek(uint32_t fd, uint64_t in_offset, uint32_t whence, uint32_t res_filesize) { |
| 956 | uint8_t *const m = *wasm_memory; |
| 957 | int64_t offset = (int64_t)in_offset; |
| 958 | uint64_t *res_filesize_ptr = (uint64_t *)&m[res_filesize]; |
| 959 | #if LOG_TRACE |
| 960 | fprintf(stderr, "wasi_snapshot_preview1_fd_seek(%u, 0x%lld, %u)\n", fd, (long long)offset, whence); |
| 961 | #endif |
| 962 | |
| 963 | if (fd >= fd_len || fds[fd].de >= de_len) return wasi_errno_badf; |
| 964 | switch (des[fds[fd].de].filetype) { |
| 965 | case wasi_filetype_character_device: break; |
| 966 | case wasi_filetype_regular_file: break; |
| 967 | case wasi_filetype_directory: return wasi_errno_inval; |
| 968 | default: panic("unimplemented"); |
| 969 | } |
| 970 | |
| 971 | int seek_whence; |
| 972 | switch (whence) { |
| 973 | case wasi_whence_set: |
| 974 | seek_whence = SEEK_SET; |
| 975 | break; |
| 976 | case wasi_whence_cur: |
| 977 | seek_whence = SEEK_CUR; |
| 978 | break; |
| 979 | case wasi_whence_end: |
| 980 | seek_whence = SEEK_END; |
| 981 | break; |
| 982 | default: |
| 983 | return wasi_errno_inval; |
| 984 | } |
| 985 | if (fseek(fds[fd].stream, offset, seek_whence) < 0) return wasi_errno_io; |
| 986 | long res_offset = ftell(fds[fd].stream); |
| 987 | if (res_offset < 0) return wasi_errno_io; |
| 988 | *res_filesize_ptr = (uint64_t)res_offset; |
| 989 | return wasi_errno_success; |
| 990 | } |
| 991 | |
| 949 | 992 | uint32_t wasi_snapshot_preview1_poll_oneoff(uint32_t in, uint32_t out, uint32_t nsubscriptions, uint32_t res_nevents) { |
| 950 | 993 | (void)in; |
| 951 | 994 | (void)out; |
| ... | ... | @@ -959,7 +1002,6 @@ uint32_t wasi_snapshot_preview1_poll_oneoff(uint32_t in, uint32_t out, uint32_t |
| 959 | 1002 | return wasi_errno_success; |
| 960 | 1003 | } |
| 961 | 1004 | |
| 962 | | |
| 963 | 1005 | void wasi_snapshot_preview1_debug(uint32_t string, uint64_t x) { |
| 964 | 1006 | uint8_t *const m = *wasm_memory; |
| 965 | 1007 | const char *string_ptr = (const char *)&m[string]; |