Skip to content

Commit

Permalink
wasi: add wasi socki_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 <mdawson@devrus.com>
  • Loading branch information
mhdawson committed Jan 30, 2023
1 parent 917512d commit 6f3a82a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 19 deletions.
27 changes: 9 additions & 18 deletions src/node_wasi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1234,36 +1234,26 @@ uint32_t WASI::SockShutdown(WASI& wasi,
return uvwasi_sock_shutdown(&wasi.uvw_, sock, how);
}

void WASI::SockAccept(const FunctionCallbackInfo<Value>& 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());
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);
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_,
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);
uvwasi_serdes_write_size_t(memory.data, fd_ptr, fd);

args.GetReturnValue().Set(err);
return err;
}

void WASI::_SetMemory(const FunctionCallbackInfo<Value>& args) {
Expand Down Expand Up @@ -1338,6 +1328,7 @@ static void Initialize(Local<Object> target,
V(SockRecv, "sock_recv")
V(SockSend, "sock_send")
V(SockShutdown, "sock_shutdown")
V(SockAccept, "sock_accept")
#undef V

SetInstanceMethod(isolate, tmpl, "_setMemory", WASI::_SetMemory);
Expand Down
2 changes: 1 addition & 1 deletion src/node_wasi.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class WASI : public BaseObject,
static uint32_t SockSend(
WASI&, WasmMemory, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t);
static uint32_t SockShutdown(WASI&, WasmMemory, uint32_t, uint32_t);
static void SockAccept(const v8::FunctionCallbackInfo<v8::Value>& args);
static uint32_t SockAccept(WASI&, WasmMemory, uint32_t, uint32_t, uint32_t);

static void _SetMemory(const v8::FunctionCallbackInfo<v8::Value>& args);

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>

int main(void) {

int fd = 0 ;
socklen_t addrlen = 0;
int flags = 0;
int ret = accept(0, NULL, &addrlen);
assert(ret == -1);
assert(errno == 58);

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 @@ -98,6 +98,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.
Expand Down
Binary file added test/wasi/wasm/sock.wasm
Binary file not shown.

0 comments on commit 6f3a82a

Please sign in to comment.