Skip to content

Commit 74a82ea

Browse files
committed
wasi: add wasi sock_accept stub
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: nodejs#46434 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent e588b63 commit 74a82ea

File tree

5 files changed

+51
-0
lines changed

5 files changed

+51
-0
lines changed

src/node_wasi.cc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,6 +1503,37 @@ void WASI::SchedYield(const FunctionCallbackInfo<Value>& args) {
15031503
args.GetReturnValue().Set(err);
15041504
}
15051505

1506+
void WASI::SockAccept(const FunctionCallbackInfo<Value>& args) {
1507+
WASI* wasi;
1508+
uint32_t sock;
1509+
uint32_t flags;
1510+
uint32_t fd_ptr;
1511+
char* memory;
1512+
size_t mem_size;
1513+
RETURN_IF_BAD_ARG_COUNT(args, 3);
1514+
CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, sock);
1515+
CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, flags);
1516+
CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, fd_ptr);
1517+
ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
1518+
Debug(wasi,
1519+
"sock_accept(%d, %d, %d)\n",
1520+
sock,
1521+
flags,
1522+
fd_ptr);
1523+
GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
1524+
CHECK_BOUNDS_OR_RETURN(args, mem_size, fd_ptr, UVWASI_SERDES_SIZE_fd_t);
1525+
1526+
uvwasi_fd_t fd;
1527+
uvwasi_errno_t err = uvwasi_sock_accept(&wasi->uvw_,
1528+
sock,
1529+
flags,
1530+
&fd);
1531+
1532+
if (err == UVWASI_ESUCCESS)
1533+
uvwasi_serdes_write_size_t(memory, fd_ptr, fd);
1534+
1535+
args.GetReturnValue().Set(err);
1536+
}
15061537

15071538
void WASI::SockRecv(const FunctionCallbackInfo<Value>& args) {
15081539
WASI* wasi;
@@ -1720,6 +1751,7 @@ static void Initialize(Local<Object> target,
17201751
SetProtoMethod(isolate, tmpl, "proc_raise", WASI::ProcRaise);
17211752
SetProtoMethod(isolate, tmpl, "random_get", WASI::RandomGet);
17221753
SetProtoMethod(isolate, tmpl, "sched_yield", WASI::SchedYield);
1754+
SetProtoMethod(isolate, tmpl, "sock_accept", WASI::SockAccept);
17231755
SetProtoMethod(isolate, tmpl, "sock_recv", WASI::SockRecv);
17241756
SetProtoMethod(isolate, tmpl, "sock_send", WASI::SockSend);
17251757
SetProtoMethod(isolate, tmpl, "sock_shutdown", WASI::SockShutdown);

src/node_wasi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class WASI : public BaseObject,
7171
static void ProcRaise(const v8::FunctionCallbackInfo<v8::Value>& args);
7272
static void RandomGet(const v8::FunctionCallbackInfo<v8::Value>& args);
7373
static void SchedYield(const v8::FunctionCallbackInfo<v8::Value>& args);
74+
static void SockAccept(const v8::FunctionCallbackInfo<v8::Value>& args);
7475
static void SockRecv(const v8::FunctionCallbackInfo<v8::Value>& args);
7576
static void SockSend(const v8::FunctionCallbackInfo<v8::Value>& args);
7677
static void SockShutdown(const v8::FunctionCallbackInfo<v8::Value>& args);

test/wasi/c/sock.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <sys/socket.h>
2+
#include <stddef.h>
3+
#include <errno.h>
4+
#include <assert.h>
5+
#include <stdio.h>
6+
7+
// TODO(mhdawson): Update once sock_accept is implemented in uvwasi
8+
int main(void) {
9+
int fd = 0 ;
10+
socklen_t addrlen = 0;
11+
int flags = 0;
12+
int ret = accept(0, NULL, &addrlen);
13+
assert(ret == -1);
14+
assert(errno == ENOTSUP);
15+
16+
return 0;
17+
}

test/wasi/test-wasi.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ if (process.argv[2] === 'wasi-child') {
9292
stdout: `hello from input.txt${checkoutEOL}hello from input.txt${checkoutEOL}`,
9393
});
9494
runWASI({ test: 'stat' });
95+
runWASI({ test: 'sock' });
9596
runWASI({ test: 'write_file' });
9697

9798
// Tests that are currently unsupported on Windows.

test/wasi/wasm/sock.wasm

18.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)