diff --git a/src/node_wasi.cc b/src/node_wasi.cc index 5bbf7d9871fd98..347b49e9adbe61 100644 --- a/src/node_wasi.cc +++ b/src/node_wasi.cc @@ -1503,6 +1503,37 @@ void WASI::SchedYield(const FunctionCallbackInfo& args) { args.GetReturnValue().Set(err); } +void WASI::SockAccept(const FunctionCallbackInfo& args) { + WASI* wasi; + uint32_t sock; + uint32_t flags; + uint32_t fd_ptr; + char* memory; + size_t mem_size; + RETURN_IF_BAD_ARG_COUNT(args, 3); + CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, sock); + CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, flags); + CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, fd_ptr); + ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This()); + Debug(wasi, + "sock_accept(%d, %d, %d)\n", + sock, + flags, + fd_ptr); + GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size); + CHECK_BOUNDS_OR_RETURN(args, mem_size, fd_ptr, UVWASI_SERDES_SIZE_fd_t); + + 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, fd_ptr, fd); + + args.GetReturnValue().Set(err); +} void WASI::SockRecv(const FunctionCallbackInfo& args) { WASI* wasi; @@ -1720,6 +1751,7 @@ static void Initialize(Local target, SetProtoMethod(isolate, tmpl, "proc_raise", WASI::ProcRaise); SetProtoMethod(isolate, tmpl, "random_get", WASI::RandomGet); SetProtoMethod(isolate, tmpl, "sched_yield", WASI::SchedYield); + SetProtoMethod(isolate, tmpl, "sock_accept", WASI::SockAccept); SetProtoMethod(isolate, tmpl, "sock_recv", WASI::SockRecv); SetProtoMethod(isolate, tmpl, "sock_send", WASI::SockSend); SetProtoMethod(isolate, tmpl, "sock_shutdown", WASI::SockShutdown); diff --git a/src/node_wasi.h b/src/node_wasi.h index b3814ddc31033a..8345dd979777e2 100644 --- a/src/node_wasi.h +++ b/src/node_wasi.h @@ -71,6 +71,7 @@ class WASI : public BaseObject, static void ProcRaise(const v8::FunctionCallbackInfo& args); static void RandomGet(const v8::FunctionCallbackInfo& args); static void SchedYield(const v8::FunctionCallbackInfo& args); + static void SockAccept(const v8::FunctionCallbackInfo& args); static void SockRecv(const v8::FunctionCallbackInfo& args); static void SockSend(const v8::FunctionCallbackInfo& args); static void SockShutdown(const v8::FunctionCallbackInfo& args); 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 949fc77e97a7a3..9c1451b93e0ccc 100644 --- a/test/wasi/test-wasi.js +++ b/test/wasi/test-wasi.js @@ -92,6 +92,7 @@ if (process.argv[2] === 'wasi-child') { 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