This repository has been archived by the owner on Sep 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuture.lua
99 lines (89 loc) · 2.05 KB
/
future.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
--[[ future
backports features from Lua 5.3
]]
if _VERSION<'Lua 5.2' then
-- os.execute returns a boolean before the value
local exe=os.execute
function os.execute(a)
local v=exe(a)
return v==0 or nil, 'exit', v
end
-- math.log takes an optional base
local ln=math.log
function math.log(a, b)
if not b then
return ln(a)
end
return ln(a)/ln(b)
end
-- unpack goes in table lib
table.unpack=unpack
-- string.rep accepts a separator
local rep=string.rep
function string.rep(str, n, sep)
if not sep then
return rep(str, n)
end
return string.sub(rep(str..sep, n), 1, -#sep-1)
end
-- xpcall accepts arguments
local xp=xpcall
function xpcall(fn, msgh, ...)
if select('#', ...)==0 then
return xp(fn, msgh)
end
local n=select('#', ...)
local args={...}
return xp(function()
return fn(unpack(args, 1, n))
end, msgh)
end
-- file:write returns file
local filemeta=getmetatable(io.stdout)
local write=filemeta.write
function filemeta:write(...)
local function ret(val, ...)
if val then
return self
end
return val, ...
end
return ret(write(self, ...))
end
--TODO give arguments to io.lines and file:lines
--TODO give a mode to load and loadfile
--TODO give an environement to load and loadfile
--TODO create package.searchpath
--TODO io.popen():close() returns status
--TODO io.read '*L'
-- __pairs metamethod
function pairs(a)
local ok, meta=pcall(getmetatable, a)
if ok and meta and meta.__pairs then
return meta.__pairs(a)
end
return next, a, nil
end
-- __ipairs metamethod
local inext=ipairs({})
function ipairs(a)
local ok, meta=pcall(getmetatable, a)
if ok and meta and meta.__pairs then
return meta.__pairs(a)
end
return inext, a, 0
end
-- load takes mode and env; string chunks are this chunk
local realload=load
function load(chunk, source, mode, env)
--FIXME handle mode
local loaded, err
if type(chunk)=='string' then
loaded, err=loadstring(chunk, source)
else
loaded, err=realload(chunk, source)
end
--FIXME set env
return loaded, err
end
end