Luvit 2.5.6 Documentation


Core object model for luvit using simple prototypes and inheritance. We support single inheritance for classes.

core.instanceof(obj, class)#

Given an object which inherits from a class, and a class, returns whether the object is an instance of that class.

> em = core.Emitter:new()
> core.instanceof(em, core.Emitter)

Class: core.Object#

The base object class. It provides simple prototypal inheritance and inheritable constructors. All other objects inherit from this.


Creates a new instance of the base object


Creates a new instance and calls obj:initialize(...) if it exists.

local Rectangle = Object:extend()
function Rectangle:initialize(w, h)
  self.w = w
  self.h = h
function Rectangle:getArea()
  return self.w * self.h
local rect = Rectangle:new(3, 4)


Creates a new sub-class.

local Square = Rectangle:extend()
function Square:initialize(w)
  self.w = w
  self.h = h

Class: core.Emitter#

This class can be used directly whenever an event emitter is needed.

local emitter = Emitter:new()
emitter:on('foo', p)
emitter:emit('foo', 1, 2, 3)

Also it can easily be sub-classed.

local Custom = Emitter:extend()
local c = Custom:new()
c:on('bar', onBar)

Unlike EventEmitter in node.js, Emitter class doesn't auto binds self reference. This means, if a callback handler is expecting a self reference, utils.bind() should be used, and the callback handler should have a self at the beginning its parameter list.

function some_func(self, a, b, c) end
emitter:on('end', utils.bind(some_func, emitter))
emitter:emit('end', 'a', 'b', 'c')

Emitter:on(name, callback)#

Adds an event listener (callback) for the named event name.

em = Emitter:new()
em:on('data', function(data) print(data) end)

Emitter:once(name, callback)#

Same as Emitter:on except it de-registers itself after the first event.


Returns the count of the listeners bound to an event with the name name

Emitter:emit(name, ...)#

Emit a named event to all listeners with optional data arguments

> em = Emitter:new()
> em:on('data', function(data) print(data) end)
> em:emit('data', 'foo')

Emitter:removeListener(name, callback)#

Removes a listener so it no longer catches events

> em = Emitter:new()
> em:on('data', function(data) print(data) end)
> em:emit('data', 'foo')
> em:removeListener('data', function() em:emit('data', 'foo') end)
> -- nothing printed


Removes all listeners. Name is optional and if supplied will make it act like removeListener


Returns listeners for the event with name name


Utility that binds the named method self[name] for use as a callback. The first argument (err) is re-routed to the "error" event instead.

local Joystick = Emitter:extend()
function Joystick:initialize(device)
  self:wrap("onOpen"), self.onOpen)
function Joystick:onOpen(fd)
  -- and so forth

Emitter:propagate(eventName, target)#

Propagate the event to another emitter.

Class: core.Error#

This is for code that wants structured error messages.


How to handle strings