node's Ziggy Stardust

Brandon Philips
brandon@ifup.org
June 26th, 2012

About me

Orientation

Untechnical Overview

Luvit is a platform for building your app

Technical Overview

HTTP Server Example

local http = require("http")

http.createServer(function (req, res)
  local body = "Hello world\n"
  res:writeHead(200, {
    ["Content-Type"] = "text/plain",
    ["Content-Length"] = #body
  })
  res:finish(body)
end):listen(8080)

print("Server listening at http://localhost:8080/")

Community

History of luvit

Motivations

Community motivations

Comparison to node

Cloudkick Agent

Foundations

lua

History

Lua - Javascript's Long Lost Brazilian Cousin

Example code

GroundControl = {}

function GroundControl.new()
  obj = {}
  obj.heard_major_tom = false
  setmetatable(obj, { __index = GroundControl })
  return obj
end

function GroundControl:heard()
  print(self.heard_major_tom)
end

a = GroundControl.new()

a:heard()
a.heard()
      

luajit

Features

libuv

Basic idea

Event loop pseudo code

for (;;) {
  nfds = poll(fds, maxfd, next_timer());
  if (nfds == 0)
     timer_callback();

  for(n = 0; n < nfds; ++n) {
     callbacks[n]();
  }
}
      

Other platforms built on libuv

building luvit

Follow along at home

git clone git://github.com/luvit/luvit.git
cd luvit

gyp (all platforms)

./configure
make -C out
tools/build.py test
./out/Debug/luvit
Welcome to the Luvit repl
>

make (linux, embedded)

make
make test
./build/luvit
Welcome to the Luvit repl
>

luvit code practices

https://ifup.org/slides/luvit-osb/examples

Object system

local Object = require('core').Object

local Rectangle = Object:extend()
function Rectangle:initialize(w, h)
  self.w = w
  self.h = h
end
function Rectangle:getArea()
  return self.w * self.h
end

local rect = Rectangle:new(3, 4)
p(rect:getArea())
      

Module System

-- hello.lua
local hello = {}
hello.world = function()
  print("Hello World")
end
return hello

-- run.lua
local hello = require('hello')
hello.world()
      

JSON Example

local JSON = require('json')
local value = JSON.parse([[
{
  "author": "Brandon Philips <brandon@ifup.org>",
  "name": "luvit - node's Ziggy Stardust",
  "description": "a talk about luvit"
}
]])

local json = JSON.stringify(value, {beautify=true,indent_string="  "});
print(json)
      

HTTP Client

local http = require('http')

      local options = {
        host = "luvit.io",
        port = 80,
        path = "/"
      }

      local req = http.request(options, function (res)
        res:on('data', function (chunk)
          p("ondata", {chunk=chunk})
        end)
      end)

      req:done()
      

FFI is awesome! And Fast.

Call C functions without writing C

local ffi = require("ffi")

ffi.cdef[[
  int printf(const char *fmt, ...);
]]

ffi.C.printf("Hello %s!\n", "world")

Users

Modules

Applications

Future work

ifup.org/slides

Thanks! - fork on luvit.io - contact me: brandon@ifup.org

Source: Octodex