]> git.lizzy.rs Git - metalua.git/blob - src/lib/extension-runtime/lazy.mlua
added a try...catch...with extension
[metalua.git] / src / lib / extension-runtime / lazy.mlua
1 module("lazy", package.seeall)
2
3 local THUNK_MT = { }
4
5 function thunk (f)
6    return setmetatable ({raw=f}, THUNK_MT)
7 end
8
9 is_thunk = |th| getmetatable(th) == THUNK_MT
10
11 function force (th)
12    if not is_thunk(th) then return th 
13    elseif th.raw then th.value=th.raw(); th.raw=nil; return th.value
14    else return th.value end
15 end
16
17 function table (t)
18    local mt = { __rawtable = t }
19    function mt.__index(_, key) return force(t[key]) end 
20    function mt.__newindex(_, key, val) t[key]=val end
21    return setmetatable({}, mt)
22 end