Basic introduction to the QuickJS asynchronous approach
Since I’ve been reading the QuickJS engine code, I’ve came up with a simple question as an average javascript developer: ¿How the asynchronous code work in a low level?
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: open
and close
Since this is an external C module, we don’t need to modify the compiler itself, will be statically linked to the final binary.