Brandon Philips
brandon@ifup.org
June 26th, 2012
Luvit is a platform for building your app
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/")
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()
for (;;) {
nfds = poll(fds, maxfd, next_timer());
if (nfds == 0)
timer_callback();
for(n = 0; n < nfds; ++n) {
callbacks[n]();
}
}
git clone git://github.com/luvit/luvit.git
cd luvit
./configure
make -C out
tools/build.py test
./out/Debug/luvit
Welcome to the Luvit repl
>
make
make test
./build/luvit
Welcome to the Luvit repl
>
https://ifup.org/slides/luvit-osb/examples
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())
-- hello.lua
local hello = {}
hello.world = function()
print("Hello World")
end
return hello
-- run.lua
local hello = require('hello')
hello.world()
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)
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()
Call C functions without writing C
local ffi = require("ffi") ffi.cdef[[ int printf(const char *fmt, ...); ]] ffi.C.printf("Hello %s!\n", "world")