Skip to content

Commit 18a8e36

Browse files
committed
wip make select() async when using JSPI
1 parent 5f59cb6 commit 18a8e36

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

src/lib/libcore.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2617,7 +2617,11 @@ function wrapSyscallFunction(x, library, isWasi) {
26172617
post = handler + post;
26182618

26192619
if (pre || post) {
2620-
t = modifyJSFunction(t, (args, body) => `function (${args}) {\n${pre}${body}${post}}\n`);
2620+
if (library[x + '__async']) {
2621+
t = modifyJSFunction(t, (args, body) => `async function (${args}) {\n${pre}${body}${post}}\n`);
2622+
} else {
2623+
t = modifyJSFunction(t, (args, body) => `function (${args}) {\n${pre}${body}${post}}\n`);
2624+
}
26212625
}
26222626

26232627
library[x] = eval('(' + t + ')');

src/lib/libeventloop.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,9 @@ LibraryJSEventLoop = {
378378
emscripten_set_main_loop__deps: ['$setMainLoop'],
379379
emscripten_set_main_loop: (func, fps, simulateInfiniteLoop) => {
380380
var iterFunc = {{{ makeDynCall('v', 'func') }}};
381+
#if JSPI
382+
iterFunc = WebAssembly.promising(iterFunc);
383+
#endif
381384
setMainLoop(iterFunc, fps, simulateInfiniteLoop);
382385
},
383386

src/lib/libsockfs.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ addToLibrary({
104104
},
105105
// node and stream ops are backend agnostic
106106
stream_ops: {
107-
poll(stream) {
107+
poll(stream, timeout) {
108108
var sock = stream.node.sock;
109-
return sock.sock_ops.poll(sock);
109+
return sock.sock_ops.poll(sock, timeout);
110110
},
111111
ioctl(stream, request, varargs) {
112112
var sock = stream.node.sock;
@@ -375,7 +375,7 @@ addToLibrary({
375375
//
376376
// actual sock ops
377377
//
378-
poll(sock) {
378+
poll(sock, timeout) {
379379
if (sock.type === {{{ cDefs.SOCK_STREAM }}} && sock.server) {
380380
// listen sockets should only say they're available for reading
381381
// if there are pending clients.

src/lib/libsyscall.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,12 @@ var SyscallsLibrary = {
606606
'_emscripten_proxy_newselect',
607607
#endif
608608
],
609+
#if ASYNCIFY
610+
__syscall__newselect__async: true,
611+
__syscall__newselect: async (nfds, readfds, writefds, exceptfds, timeoutInMillis) => {
612+
#else
609613
__syscall__newselect: (nfds, readfds, writefds, exceptfds, timeoutInMillis) => {
614+
#endif
610615
#if PTHREADS
611616
if (ENVIRONMENT_IS_PTHREAD) {
612617
return __emscripten_proxy_newselect(nfds,
@@ -631,7 +636,12 @@ var SyscallsLibrary = {
631636
'_emscripten_proxy_newselect_finish',
632637
#endif
633638
],
639+
#if ASYNCIFY
640+
_newselect_js__async: true,
641+
_newselect_js: async (ctx, arg, nfds, readfds, writefds, exceptfds, timeoutInMillis) => {
642+
#else
634643
_newselect_js: (ctx, arg, nfds, readfds, writefds, exceptfds, timeoutInMillis) => {
644+
#endif
635645
// readfds are supported,
636646
// writefds checks socket open status
637647
// exceptfds are supported, although on web, such exceptional conditions never arise in web sockets
@@ -701,6 +711,11 @@ var SyscallsLibrary = {
701711
#endif
702712
return stream.stream_ops.poll(stream, timeoutInMillis);
703713
})();
714+
715+
#if ASYNCIFY
716+
/* poll is possibly a promise */
717+
flags = await flags;
718+
#endif
704719
} else {
705720
#if ASSERTIONS
706721
if (timeoutInMillis != 0) warnOnce('non-zero select() timeout not supported: ' + timeoutInMillis)

0 commit comments

Comments
 (0)