Skip to content

Commit f3d5c5a

Browse files
committed
do some optimize
Signed-off-by: johnlanni <[email protected]>
1 parent 499887f commit f3d5c5a

File tree

2 files changed

+16
-88
lines changed

2 files changed

+16
-88
lines changed

include/proxy-wasm/exports.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,14 @@ Word wasi_unstable_path_remove_directory(Word, Word, Word);
170170
Word wasi_unstable_path_rename(Word, Word, Word, Word, Word);
171171
Word wasi_unstable_path_symlink(Word, Word, Word, Word);
172172
Word wasi_unstable_path_unlink_file(Word, Word, Word);
173+
Word wasi_unstable_path_filestat_get(Word fd, Word flags, Word path, Word path_len, Word buf);
173174
Word wasi_unstable_sock_accept(Word, Word, Word);
174175
Word wasi_unstable_sock_recv(Word, Word, Word, Word, Word, Word);
175176
Word wasi_unstable_sock_send(Word, Word, Word, Word, Word);
176177
Word wasi_unstable_sock_shutdown(Word, Word);
177178
Word wasi_unstable_random_get(Word, Word);
178179
Word pthread_equal(Word left, Word right);
179180
void emscripten_notify_memory_growth(Word);
180-
Word wasi_unstable_path_filestat_get(Word fd, Word flags, Word path, Word path_len, Word buf);
181181

182182
// Support for embedders, not exported to Wasm.
183183

src/exports.cc

Lines changed: 15 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -923,30 +923,19 @@ Word wasi_unstable_fd_advise(Word fd, uint64_t offset, uint64_t len, Word advice
923923
// __wasi_errno_t __wasi_fd_allocate(__wasi_fd_t fd, __wasi_filesize_t offset, __wasi_filesize_t len);
924924
Word wasi_unstable_fd_allocate(Word fd, uint64_t offset, uint64_t len) {
925925
// fd_allocate is used to ensure that space is allocated for a file.
926-
// Since we don't have a real file system in proxy-wasm, we can just return success without doing anything.
927-
// This is similar to how other file-related functions are implemented in this codebase.
928-
929-
// We only support stdout and stderr in proxy-wasm, which don't need allocation
930-
if (fd != 1 /* stdout */ && fd != 2 /* stderr */) {
931-
return 8; // __WASI_ERRNO_BADF - Bad file descriptor
932-
}
926+
// This operation doesn't make sense for stdout/stderr streams, so we return ENOTSUP.
927+
// Unlike regular files, stdout/stderr don't need space allocation.
933928

934-
return 0; // __WASI_ESUCCESS
929+
return 58; // __WASI_ENOTSUP - Not supported
935930
}
936931

937932
// __wasi_errno_t __wasi_fd_datasync(__wasi_fd_t fd);
938933
Word wasi_unstable_fd_datasync(Word fd) {
939934
// fd_datasync is used to synchronize the data of a file to disk.
940-
// Since we don't have a real file system in proxy-wasm, we can just return success for stdout/stderr
941-
// and an error for other file descriptors.
935+
// This operation doesn't really make sense for stdout/stderr streams.
936+
// We return ENOTSUP since syncing streams is not a meaningful operation.
942937

943-
// We only support stdout and stderr in proxy-wasm
944-
if (fd != 1 /* stdout */ && fd != 2 /* stderr */) {
945-
return 8; // __WASI_ERRNO_BADF - Bad file descriptor
946-
}
947-
948-
// For stdout and stderr, there's no need to sync as they're handled by the host system
949-
return 0; // __WASI_ESUCCESS
938+
return 58; // __WASI_ENOTSUP - Not supported
950939
}
951940

952941
// __wasi_errno_t __wasi_fd_fdstat_set_rights(__wasi_fd_t fd, __wasi_rights_t fs_rights_base, __wasi_rights_t fs_rights_inheriting);
@@ -968,33 +957,19 @@ Word wasi_unstable_fd_fdstat_set_rights(Word fd, uint64_t fs_rights_base, uint64
968957
// __wasi_errno_t __wasi_fd_filestat_set_size(__wasi_fd_t fd, __wasi_filesize_t size);
969958
Word wasi_unstable_fd_filestat_set_size(Word fd, uint64_t size) {
970959
// fd_filestat_set_size is used to adjust the size of a file, similar to ftruncate.
971-
// Since we don't have a real file system in proxy-wasm, we can just return success for stdout/stderr
972-
// and an error for other file descriptors.
960+
// This operation doesn't make sense for stdout/stderr streams.
961+
// Streams don't have a settable size.
973962

974-
// We only support stdout and stderr in proxy-wasm
975-
if (fd != 1 /* stdout */ && fd != 2 /* stderr */) {
976-
return 8; // __WASI_ERRNO_BADF - Bad file descriptor
977-
}
978-
979-
// For stdout and stderr, we don't actually change any size, but we can pretend it succeeded
980-
// This is similar to how other file-related functions are implemented in this codebase
981-
return 0; // __WASI_ESUCCESS
963+
return 58; // __WASI_ENOTSUP - Not supported
982964
}
983965

984966
// __wasi_errno_t __wasi_fd_filestat_set_times(__wasi_fd_t fd, __wasi_timestamp_t atim, __wasi_timestamp_t mtim, __wasi_fstflags_t fst_flags);
985967
Word wasi_unstable_fd_filestat_set_times(Word fd, uint64_t atim, uint64_t mtim, Word fst_flags) {
986968
// fd_filestat_set_times is used to set the access and modification times of a file.
987-
// Since we don't have a real file system in proxy-wasm, we can just return success for stdout/stderr
988-
// and an error for other file descriptors.
969+
// This operation doesn't make sense for stdout/stderr streams.
970+
// Streams don't have access/modification times.
989971

990-
// We only support stdout and stderr in proxy-wasm
991-
if (fd != 1 /* stdout */ && fd != 2 /* stderr */) {
992-
return 8; // __WASI_ERRNO_BADF - Bad file descriptor
993-
}
994-
995-
// For stdout and stderr, we don't actually change any times, but we can pretend it succeeded
996-
// This is similar to how other file-related functions are implemented in this codebase
997-
return 0; // __WASI_ESUCCESS
972+
return 58; // __WASI_ENOTSUP - Not supported
998973
}
999974

1000975
// __wasi_errno_t __wasi_fd_pread(__wasi_fd_t fd, const __wasi_iovec_t *iovs, size_t iovs_len, __wasi_filesize_t offset, __wasi_size_t *retptr0);
@@ -1037,17 +1012,9 @@ Word wasi_unstable_fd_pwrite(Word fd, Word iovs_ptr, Word iovs_len, uint64_t off
10371012

10381013
// __wasi_errno_t __wasi_fd_readdir(__wasi_fd_t fd, uint8_t *buf, __wasi_size_t buf_len, __wasi_dircookie_t cookie, __wasi_size_t *retptr0);
10391014
Word wasi_unstable_fd_readdir(Word fd, Word buf_ptr, Word buf_len, uint64_t cookie, Word nread_ptr) {
1040-
auto *context = contextOrEffectiveContext();
1041-
10421015
// fd_readdir is used to read directory entries from a directory.
10431016
// Since we don't have a real file system in proxy-wasm, we can just return an error.
10441017

1045-
// Set the number of bytes read to 0
1046-
if (!context->wasmVm()->setWord(nread_ptr, Word(0))) {
1047-
return 21; // __WASI_EFAULT
1048-
}
1049-
1050-
// Return ENOTDIR (Not a directory) error
10511018
return 20; // __WASI_ENOTDIR
10521019
}
10531020

@@ -1069,16 +1036,10 @@ Word wasi_unstable_fd_renumber(Word fd, Word to) {
10691036
// __wasi_errno_t __wasi_fd_sync(__wasi_fd_t fd);
10701037
Word wasi_unstable_fd_sync(Word fd) {
10711038
// fd_sync is used to synchronize a file's in-core state with the storage device.
1072-
// Since we don't have a real file system in proxy-wasm, we can just return success for stdout/stderr
1073-
// and an error for other file descriptors.
1039+
// This operation doesn't really make sense for stdout/stderr streams.
1040+
// We return ENOTSUP since syncing streams is not a meaningful operation.
10741041

1075-
// We only support stdout and stderr in proxy-wasm
1076-
if (fd != 1 /* stdout */ && fd != 2 /* stderr */) {
1077-
return 8; // __WASI_ERRNO_BADF - Bad file descriptor
1078-
}
1079-
1080-
// For stdout and stderr, there's no need to sync as they're handled by the host system
1081-
return 0; // __WASI_ESUCCESS
1042+
return 58; // __WASI_ENOTSUP - Not supported
10821043
}
10831044

10841045
// __wasi_errno_t __wasi_fd_tell(__wasi_fd_t fd, __wasi_filesize_t *retptr0);
@@ -1127,16 +1088,9 @@ Word wasi_unstable_path_link(Word old_fd, Word old_flags, Word old_path_ptr, Wor
11271088

11281089
// __wasi_errno_t __wasi_path_readlink(__wasi_fd_t fd, const char *path, uint8_t *buf, __wasi_size_t buf_len, __wasi_size_t *retptr0);
11291090
Word wasi_unstable_path_readlink(Word fd, Word path_ptr, Word path_len, Word buf_ptr, Word buf_len, Word retptr0) {
1130-
auto *context = contextOrEffectiveContext();
1131-
11321091
// path_readlink is used to read the contents of a symbolic link.
11331092
// Since we don't have a real file system in proxy-wasm, we can just return an error.
11341093

1135-
// Set the number of bytes read to 0
1136-
if (!context->wasmVm()->setWord(retptr0, Word(0))) {
1137-
return 21; // __WASI_EFAULT
1138-
}
1139-
11401094
return 58; // __WASI_ENOTSUP - Not supported
11411095
}
11421096

@@ -1174,51 +1128,25 @@ Word wasi_unstable_path_unlink_file(Word fd, Word path_ptr, Word path_len) {
11741128

11751129
// __wasi_errno_t __wasi_sock_accept(__wasi_fd_t fd, __wasi_fdflags_t flags, __wasi_fd_t *retptr0);
11761130
Word wasi_unstable_sock_accept(Word fd, Word flags, Word retptr0) {
1177-
auto *context = contextOrEffectiveContext();
1178-
11791131
// sock_accept is used to accept a new connection on a socket.
11801132
// Since we don't have socket support in proxy-wasm, we can just return an error.
11811133

1182-
// Set the returned file descriptor to an invalid value
1183-
if (!context->wasm()->setDatatype(retptr0, uint32_t(0))) {
1184-
return 21; // __WASI_EFAULT
1185-
}
1186-
11871134
return 58; // __WASI_ENOTSUP - Not supported
11881135
}
11891136

11901137
// __wasi_errno_t __wasi_sock_recv(__wasi_fd_t fd, const __wasi_iovec_t *ri_data, size_t ri_data_len, __wasi_riflags_t ri_flags, __wasi_size_t *retptr0, __wasi_roflags_t *retptr1);
11911138
Word wasi_unstable_sock_recv(Word fd, Word ri_data_ptr, Word ri_data_len, Word ri_flags, Word retptr0, Word retptr1) {
1192-
auto *context = contextOrEffectiveContext();
1193-
11941139
// sock_recv is used to receive data from a socket.
11951140
// Since we don't have socket support in proxy-wasm, we can just return an error.
11961141

1197-
// Set the number of bytes received to 0
1198-
if (!context->wasmVm()->setWord(retptr0, Word(0))) {
1199-
return 21; // __WASI_EFAULT
1200-
}
1201-
1202-
// Set the output flags to 0
1203-
if (!context->wasm()->setDatatype(retptr1, uint16_t(0))) {
1204-
return 21; // __WASI_EFAULT
1205-
}
1206-
12071142
return 58; // __WASI_ENOTSUP - Not supported
12081143
}
12091144

12101145
// __wasi_errno_t __wasi_sock_send(__wasi_fd_t fd, const __wasi_ciovec_t *si_data, size_t si_data_len, __wasi_siflags_t si_flags, __wasi_size_t *retptr0);
12111146
Word wasi_unstable_sock_send(Word fd, Word si_data_ptr, Word si_data_len, Word si_flags, Word retptr0) {
1212-
auto *context = contextOrEffectiveContext();
1213-
12141147
// sock_send is used to send data on a socket.
12151148
// Since we don't have socket support in proxy-wasm, we can just return an error.
12161149

1217-
// Set the number of bytes sent to 0
1218-
if (!context->wasmVm()->setWord(retptr0, Word(0))) {
1219-
return 21; // __WASI_EFAULT
1220-
}
1221-
12221150
return 58; // __WASI_ENOTSUP - Not supported
12231151
}
12241152

0 commit comments

Comments
 (0)