Debian Codegraph @

3rdparty/toluapp/src/bin/lua/compat.lua

raw
1 -------------------------------------------------------------------
2 -- Real globals
3 -- _ALERT
4 -- _ERRORMESSAGE
5 -- _VERSION
6 -- _G
7 -- assert
8 -- error
9 -- metatable
10 -- next
11 -- print
12 -- require
13 -- tonumber
14 -- tostring
15 -- type
16 -- unpack
17
18 -------------------------------------------------------------------
19 -- collectgarbage
20 -- gcinfo
21
22 -- globals
23
24 -- call -> protect(f, err)
25 -- loadfile
26 -- loadstring
27
28 -- rawget
29 -- rawset
30
31 -- getargs = Main.getargs ??
32
33 rawtype = type
34
35 function do_ (f, err)
36 if not f then print(err); return end
37 local a,b = pcall(f)
38 if not a then print(b); return nil
39 else return b or true
40 end
41 end
42
43 function dostring(s) return do_(load(s)) end
44 -- function dofile(s) return do_(loadfile(s)) end
45
46 -------------------------------------------------------------------
47 -- Table library
48 local tab = table
49 getn = function (tab) return #tab end
50 tinsert = tab.insert
51 tremove = tab.remove
52 sort = tab.sort
53
54 -------------------------------------------------------------------
55 -- Debug library
56 local dbg = debug
57 getinfo = dbg.getinfo
58 getlocal = dbg.getlocal
59 setcallhook = function () error"`setcallhook' is deprecated" end
60 setlinehook = function () error"`setlinehook' is deprecated" end
61 setlocal = dbg.setlocal
62
63 -------------------------------------------------------------------
64 -- math library
65 local math = math
66 abs = math.abs
67 acos = function (x) return math.deg(math.acos(x)) end
68 asin = function (x) return math.deg(math.asin(x)) end
69 atan = function (x) return math.deg(math.atan(x)) end
70 atan2 = function (x,y) return math.deg(math.atan2(x,y)) end
71 ceil = math.ceil
72 cos = function (x) return math.cos(math.rad(x)) end
73 deg = math.deg
74 exp = math.exp
75 floor = math.floor
76 frexp = math.frexp
77 ldexp = math.ldexp
78 log = math.log
79 log10 = function(val) return math.log(10, val) end
80 max = math.max
81 min = math.min
82 mod = math.mod
83 PI = math.pi
84 --??? pow = math.pow
85 rad = math.rad
86 random = math.random
87 randomseed = math.randomseed
88 sin = function (x) return math.sin(math.rad(x)) end
89 sqrt = math.sqrt
90 tan = function (x) return math.tan(math.rad(x)) end
91
92 -------------------------------------------------------------------
93 -- string library
94 local str = string
95 strbyte = str.byte
96 strchar = str.char
97 strfind = str.find
98 format = str.format
99 gsub = str.gsub
100 strlen = str.len
101 strlower = str.lower
102 strrep = str.rep
103 strsub = str.sub
104 strupper = str.upper
105
106 -------------------------------------------------------------------
107 -- os library
108 clock = os.clock
109 date = os.date
110 difftime = os.difftime
111 execute = os.execute --?
112 exit = os.exit
113 getenv = os.getenv
114 remove = os.remove
115 rename = os.rename
116 setlocale = os.setlocale
117 time = os.time
118 tmpname = os.tmpname
119
120 -------------------------------------------------------------------
121 -- compatibility only
122 getglobal = function (n) return _G[n] end
123 setglobal = function (n,v) _G[n] = v end
124
125 -------------------------------------------------------------------
126
127 local io, tab = io, table
128
129 -- IO library (files)
130 _STDIN = io.stdin
131 _STDERR = io.stderr
132 _STDOUT = io.stdout
133 _INPUT = io.stdin
134 _OUTPUT = io.stdout
135 seek = io.stdin.seek -- sick ;-)
136 tmpfile = io.tmpfile
137 closefile = io.close
138 openfile = io.open
139
140 function flush (f)
141 if f then f:flush()
142 else _OUTPUT:flush()
143 end
144 end
145
146 function readfrom (name)
147 if name == nil then
148 local f, err, cod = io.close(_INPUT)
149 _INPUT = io.stdin
150 return f, err, cod
151 else
152 local f, err, cod = io.open(name, "r")
153 _INPUT = f or _INPUT
154 return f, err, cod
155 end
156 end
157
158 function writeto (name)
159 if name == nil then
160 local f, err, cod = io.close(_OUTPUT)
161 _OUTPUT = io.stdout
162 return f, err, cod
163 else
164 local f, err, cod = io.open(name, "w")
165 _OUTPUT = f or _OUTPUT
166 return f, err, cod
167 end
168 end
169
170 function appendto (name)
171 local f, err, cod = io.open(name, "a")
172 _OUTPUT = f or _OUTPUT
173 return f, err, cod
174 end
175
176 function read (...)
177 local f = _INPUT
178 local arg = {...}
179 if rawtype(arg[1]) == 'userdata' then
180 f = tab.remove(arg, 1)
181 end
182 return f:read(table.unpack(arg))
183 end
184
185 function write (...)
186 local f = _OUTPUT
187 local arg = {...}
188 if rawtype(arg[1]) == 'userdata' then
189 f = tab.remove(arg, 1)
190 end
191 return f:write(table.unpack(arg))
192 end
193