diff --git a/src/node_wasi.cc b/src/node_wasi.cc index 6f8ceb3bb67308..061de8e8b21281 100644 --- a/src/node_wasi.cc +++ b/src/node_wasi.cc @@ -1148,6 +1148,21 @@ uint32_t WASI::SchedYield(WASI& wasi, WasmMemory) { return uvwasi_sched_yield(&wasi.uvw_); } +uint32_t WASI::SockAccept(WASI& wasi, + WasmMemory memory, + uint32_t sock, + uint32_t flags, + uint32_t fd_ptr) { + Debug(wasi, "sock_accept(%d, %d, %d)\n", sock, flags, fd_ptr); + uvwasi_fd_t fd; + uvwasi_errno_t err = uvwasi_sock_accept(&wasi.uvw_, sock, flags, &fd); + + if (err == UVWASI_ESUCCESS) + uvwasi_serdes_write_size_t(memory.data, fd_ptr, fd); + + return err; +} + uint32_t WASI::SockRecv(WASI& wasi, WasmMemory memory, uint32_t sock, @@ -1303,6 +1318,7 @@ static void InitializePreview1(Local target, V(ProcRaise, "proc_raise") V(RandomGet, "random_get") V(SchedYield, "sched_yield") + V(SockAccept, "sock_accept") V(SockRecv, "sock_recv") V(SockSend, "sock_send") V(SockShutdown, "sock_shutdown") diff --git a/src/node_wasi.h b/src/node_wasi.h index a28bdd8ad1bfa6..25551936e6be36 100644 --- a/src/node_wasi.h +++ b/src/node_wasi.h @@ -131,6 +131,7 @@ class WASI : public BaseObject, static uint32_t ProcRaise(WASI&, WasmMemory, uint32_t); static uint32_t RandomGet(WASI&, WasmMemory, uint32_t, uint32_t); static uint32_t SchedYield(WASI&, WasmMemory); + static uint32_t SockAccept(WASI&, WasmMemory, uint32_t, uint32_t, uint32_t); static uint32_t SockRecv(WASI&, WasmMemory, uint32_t, diff --git a/test/wasi/c/sock.c b/test/wasi/c/sock.c new file mode 100644 index 00000000000000..de4a3ccc5f95a6 --- /dev/null +++ b/test/wasi/c/sock.c @@ -0,0 +1,17 @@ +#include +#include +#include +#include +#include + +// TODO(mhdawson): Update once sock_accept is implemented in uvwasi +int main(void) { + int fd = 0 ; + socklen_t addrlen = 0; + int flags = 0; + int ret = accept(0, NULL, &addrlen); + assert(ret == -1); + assert(errno == ENOTSUP); + + return 0; +} diff --git a/test/wasi/test-wasi.js b/test/wasi/test-wasi.js index 15ca9d6c5cc47b..b9f7cb9be7b2c6 100644 --- a/test/wasi/test-wasi.js +++ b/test/wasi/test-wasi.js @@ -137,6 +137,7 @@ if (process.argv[2] === 'wasi-child-default') { stdout: `hello from input.txt${checkoutEOL}hello from input.txt${checkoutEOL}`, }); runWASI({ test: 'stat' }); + runWASI({ test: 'sock' }); runWASI({ test: 'write_file' }); // Tests that are currently unsupported on Windows. diff --git a/test/wasi/wasm/sock.wasm b/test/wasi/wasm/sock.wasm new file mode 100755 index 00000000000000..78e7b8e430f911 Binary files /dev/null and b/test/wasi/wasm/sock.wasm differ