Skip to content

Instantly share code, notes, and snippets.

@Deco
Deco / geometry.lua
Created February 2, 2012 08:30 — forked from Deco/geometry.lua
Luvit module example (using local _M table and self-referencing)
local OO = require"oo"
local _M = {}
do _M.Shape = OO.class({
})
function _M.Shape:__init()
error("attempt to instantiate abstract shape")
end
@Deco
Deco / geometry.lua
Created February 2, 2012 08:18 — forked from Deco/geometry.lua
Luvit module example (using local _M table) (my personal choice)
local OO = require"oo"
local _M = {}
do _M.Shape = OO.class({
})
function _M.Shape:__init()
error("attempt to instantiate abstract shape")
end
@Deco
Deco / geometry.lua
Created February 2, 2012 08:12 — forked from Deco/geometry.lua
Luvit module example (using exported locals)
local OO = require"oo"
local Shape, Rectangle, Circle
do Shape = OO.class({
})
function Shape:__init()
error("attempt to instantiate abstract shape")
end
@Deco
Deco / geometry.lua
Created February 2, 2012 08:11 — forked from Deco/geometry.lua
Luvit module example (using setfenv to emulate module function)
local OO = require"oo"
local setmetatable = setmetatable
local error, assert = error, assert
local type = type
local newproxy = newproxy
local math = require"math"
local tonumber = tonumber
local getfenv=getfenv setfenv(1, {})
@Deco
Deco / geometry.lua
Created February 2, 2012 08:09 — forked from Deco/geometry.lua
Luvit module example (using module)
local OO = require"oo"
local setmetatable = setmetatable
local error, assert = error, assert
local type = type
local newproxy = newproxy
local math = require"math"
local tonumber = tonumber
module("geometry")
@Deco
Deco / geometry.lua
Created February 2, 2012 08:09
Luvit module example (using _G)
require"oo"
geometry = {}
do geometry.Shape = OO.class({
})
function geometry.Shape:__init()
error("attempt to instantiate abstract shape")
end
@Deco
Deco / ObjectOrientation.lua
Created February 2, 2012 06:08
Object Orientation model for Lua
local setmetatable = setmetatable
local table, debug = table, debug
do --_G.class
local function class_new(self, ...)
return setmetatable({}, self)
end
_G.class = function(...)
local supers = {...}
local class = table.remove(supers, #supers)
@Deco
Deco / Results Lua 5.1.4
Created January 30, 2012 03:20
select(arg_i, ...) vs ({...})[arg_i]
select: 684000 1.1670000000013
tables: 684000 0.61299999999756
select: 684000 1.1729999999952
tables: 684000 0.64100000000326
select: 684000 1.176999999996
tables: 684000 0.63500000000931
select: 684000 1.1710000000021
tables: 684000 0.63799999999173
select: 684000 1.1719999999914
tables: 684000 0.63199999999779
@Deco
Deco / table.lua
Created January 29, 2012 10:35
table.weakref
do table.weakref -- protect against evil anti-GC indirect self-references
--[[ sample usage:
a = {}
b = {a = a}
a.b = table.weakref(b)
a, b = nil, nil
-- a and b *should* be collected
-- to use b from a.b:
print(a.b() == b)
]]
@Deco
Deco / util.lua
Created January 24, 2012 20:43
Extensions to the default Lua functions for flexibility through metamethods
do -- base function reimplementations
do _G.rawtype = _G.rawtype or _G.type
local meta, override
function _G.type(v)
local vtype = rawtype(v)
if vtype ~= "table" and vtype ~= "userdata" then return vtype end
meta = debug.getmetatable(v)
if meta then
override = rawget(meta, "__type")