Kerneljs is a minimalist javascript interpreter with a nonblocking API in the style of the browser and nodejs. It began as an exercise exploring the nodejs code base, and out of frustration with some of its design choices, including:
requires and the commonjs API, which I wanted to avoid.Buffer object, which is intended to handle raw byte streams. As javascript was meant to work only on unicode, I wanted to leave binary data out.Buffer objects. I wanted true abstraction between C and javascript.The goal was to have a sufficient subset of nodejs with only three major components:
The kernel executable provides the following global functions:
setTimeout(callback, milliseconds)window.setTimeout in the browser, except that it returns an object on which you call .clear() instead of using clearTimeout().listen(callback, port, host)port and host (optional; defaults to 127.0.0.1). To stop listening, call .close() on the object that is returned. When a connection is established, issues callback with a connection object as its first argument. The connection object's reader member can be set to a function that will be called with a string argument when it is received from the client. It has a write function that sends a message to the client, and a close function that closes the connection.connect(callback, port, host)listen to connect to a server. host is also optional, and the callback is only issued once with the same connection object.print(string)string to STDOUT.The kerneljs library can be embedded into a C++ program. Here is a simple program that adds the same global functions available in the kernel executable:
#include <kernel.h>
using namespace kernel;
int main(int argc, char *argv[]) {
HandleScope scope;
Kernel kernel;
kernel->Set("setTimeout", FunctionTemplate::New(Timer::New));
kernel->Set("listen", FunctionTemplate::New(Server::New));
kernel->Set("connect", FunctionTemplate::New(Client::New));
kernel->Set("print", FunctionTemplate::New(Kernel::Print));
return kernel.Run(argc, argv);
}
Dereferencing the kernel object simply returns the global ObjectTemplate. Your code can refer to ev_loop Kernel::loop when using the libev API and Kernel::RunAsync in place of eio_custom.