Basic introduction to the QuickJS asynchronous approach
All we know something about the event loop, in the NodeJS case, the asynchronous I/O is being powered by libuv, but it’s not the same for QuickJS.
Since QuickJS was created to be embedded in other systems, tries to reduce the external dependencies to the fullest, so it uses the
select system call.
There are several important drawbacks about this approach, but I think, the intention behind is to keep it as much simple as possible.
I’m not going to dive in the
select/poll internals, instead, I’m going to show how can we develop C modules with the this asynchronous model in mind.
C Module for running external processes
Our experiment includes a C module for running external processes from scripts compiled with QuickJS, here’s our C external module, it exposes 2 functions:
Since this is an external C module, we don’t need to modify the compiler itself, will be statically linked to the final binary.
These will run the corresponding process opening/closing a stream:
JS script for calling external processes
We want to download a JSON file by calling
curl (we assume it’s installed)
fd will stores the file descriptor returned by
open and then we setup a read handler by using
os module exposed in QuickJS API.