We Want to Swap Engines
-As of node.js v0.6.x, node is just V8 bindings to libuv right?
-So let’s just swap V8 with another engine. LuaJit looks nice.
-This should be quick and easy. Let’s port the boat!
Boating in Lake JavaScript
C Libraries
libuv – Provides non-blocking, callback based I/O and timers.
http_parser – Fast event-based HTTP protocol parser.
openssl – Provides crypto primitives.
zlib – Provides compression and decompression.
Scripting Language Virtual Machine
google V8 – Runs JavaScript code.
Navigating The Strait of Lua
C Libraries
libuv – Provides non-blocking, callback based I/O and timers.
http_parser – Fast event-based HTTP protocol parse
openssl – Provides crypto primitives.
zlib – Provides compression and decompression.
Scripting Language Virtual Machine
LuaJit – Runs Lua code
Why Bother Porting the Canoe?
LuaJit is much lighter than V8 in embedded situations.
I don’t like C++ for addons, I prefer straight C.
Lua has coroutines! (an alternative to callbacks)
LuaJit has really fast FFI built-in
I wanted to make other things than websites.
Learning libUV
Cloned the repo at https://github.com/joyen libuv.git
Read include/uv.h
Joined #libuv on freenode IRC.
Work with @piscisaureus and @bnoordhuis.
TCP Server
int main() {
/* Initialize the tcp server handle */ uv_tcp_t* server = malloc(sizeof(uv_tcp_t)); uv_tcp_init(uv_default_loop(), server);
/* Bind to port 8080 on "0.0.0.0" */
printf("Binding to port 8080\n");
if (uv_tcp_bind(server, uv_ip4_addr("0.0.0.0", 8080))) {
error("bind");
}
/* Listen for connections */
printf("Listening for connections\n");
if (uv_listen((uv_stream_t*)server, 128, on_connection)) {
error("listen");
}
/* Block in the main loop */ uv_run(uv_default_loop()); return 0;TCP Server Continued
/* Callback on data chunk from client */
static void on_read(uv_stream_t* stream,
ssize_t nread, uv_buf_t buf) {
if (nread >= 0) {
printf("chunk: %.*s", (int)nread, buf.base);
} else {
uv_err_t err = uv_last_error(uv_default_loop());
if (err.code == UV_EOF) {
printf("eof\n");
uv_close((uv_handle_t*)stream, on_close);
} else {
fprintf(stderr, "read: %s\n", uv_strerror(err));
exit(-1);
}
}
free(buf.base);The Journey from Lake JavaScript to The Strait of Lua (465.52 KO) (Cours PDF)