Skip to content

Commit

Permalink
wasi: add wasi sock_accept stub
Browse files Browse the repository at this point in the history
Refs: nodejs/uvwasi#185

Add stub for sock_accept so that we have stubs
for all of the sock methods in wasi_snapshot_preview1.
Its a bit awkward as the method was added after the
initial definitial of wasi_snapshot-preview1 but I
think it should be semver minor at most to add
the method.

Depends on nodejs/uvwasi#185
being landed in uvwasi first and an updated version
of uvwasi that includes that being pulled into
Node.js

Signed-off-by: Michael Dawson <[email protected]>

PR-URL: #46434
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
  • Loading branch information
mhdawson authored and targos committed Mar 14, 2023
1 parent 7ab7f97 commit b2a80d7
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/node_wasi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -1303,6 +1318,7 @@ static void InitializePreview1(Local<Object> 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")
Expand Down
1 change: 1 addition & 0 deletions src/node_wasi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions test/wasi/c/sock.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <sys/socket.h>
#include <stddef.h>
#include <errno.h>
#include <assert.h>
#include <stdio.h>

// 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;
}
1 change: 1 addition & 0 deletions test/wasi/test-wasi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Binary file added test/wasi/wasm/sock.wasm
Binary file not shown.

0 comments on commit b2a80d7

Please sign in to comment.