Skip to content

Commit e6d345b

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 <mdawson@devrus.com>
1 parent 9e7093f commit e6d345b

File tree

5 files changed

+42
-0
lines changed

5 files changed

+42
-0
lines changed

src/node_wasi.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,6 +1234,28 @@ uint32_t WASI::SockShutdown(WASI& wasi,
12341234
return uvwasi_sock_shutdown(&wasi.uvw_, sock, how);
12351235
}
12361236

1237+
uint32_t WASI::SockAccept(WASI& wasi,
1238+
WasmMemory memory,
1239+
uint32_t sock,
1240+
uint32_t flags,
1241+
uint32_t fd_ptr) {
1242+
Debug(wasi,
1243+
"sock_accept(%d, %d, %d)\n",
1244+
sock,
1245+
flags,
1246+
fd_ptr);
1247+
uvwasi_fd_t fd;
1248+
uvwasi_errno_t err = uvwasi_sock_accept(&wasi.uvw_,
1249+
sock,
1250+
flags,
1251+
&fd);
1252+
1253+
if (err == UVWASI_ESUCCESS)
1254+
uvwasi_serdes_write_size_t(memory.data, fd_ptr, fd);
1255+
1256+
return err;
1257+
}
1258+
12371259
void WASI::_SetMemory(const FunctionCallbackInfo<Value>& args) {
12381260
WASI* wasi;
12391261
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
@@ -1306,6 +1328,7 @@ static void Initialize(Local<Object> target,
13061328
V(SockRecv, "sock_recv")
13071329
V(SockSend, "sock_send")
13081330
V(SockShutdown, "sock_shutdown")
1331+
V(SockAccept, "sock_accept")
13091332
#undef V
13101333

13111334
SetInstanceMethod(isolate, tmpl, "_setMemory", WASI::_SetMemory);

src/node_wasi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ class WASI : public BaseObject,
142142
static uint32_t SockSend(
143143
WASI&, WasmMemory, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t);
144144
static uint32_t SockShutdown(WASI&, WasmMemory, uint32_t, uint32_t);
145+
static uint32_t SockAccept(WASI&, WasmMemory, uint32_t, uint32_t, uint32_t);
145146

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

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+
int main(void) {
8+
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 == 58);
15+
16+
return 0;
17+
}

test/wasi/test-wasi.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ if (process.argv[2] === 'wasi-child') {
9898
stdout: `hello from input.txt${checkoutEOL}hello from input.txt${checkoutEOL}`,
9999
});
100100
runWASI({ test: 'stat' });
101+
runWASI({ test: 'sock' });
101102
runWASI({ test: 'write_file' });
102103

103104
// Tests that are currently unsupported on Windows.

test/wasi/wasm/sock.wasm

18.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)