Imported from ../lua-5.0.2.tar.gz.
[lua.git] / etc / compat.lua
blob2a7f3731e0d3636bf21aff14b852420c70802da0
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
18 -------------------------------------------------------------------
19 -- collectgarbage
20 -- gcinfo
22 -- globals
24 -- call -> protect(f, err)
25 -- loadfile
26 -- loadstring
28 -- rawget
29 -- rawset
31 -- getargs = Main.getargs ??
34 function do_ (f, err)
35 if not f then print(err); return end
36 local a,b = pcall(f)
37 if not a then print(b); return nil
38 else return b or true
39 end
40 end
42 function dostring(s) return do_(loadstring(s)) end
43 -- function dofile(s) return do_(loadfile(s)) end
45 -------------------------------------------------------------------
46 -- Table library
47 local tab = table
48 foreach = tab.foreach
49 foreachi = tab.foreachi
50 getn = tab.getn
51 tinsert = tab.insert
52 tremove = tab.remove
53 sort = tab.sort
55 -------------------------------------------------------------------
56 -- Debug library
57 local dbg = debug
58 getinfo = dbg.getinfo
59 getlocal = dbg.getlocal
60 setcallhook = function () error"`setcallhook' is deprecated" end
61 setlinehook = function () error"`setlinehook' is deprecated" end
62 setlocal = dbg.setlocal
64 -------------------------------------------------------------------
65 -- math library
66 local math = math
67 abs = math.abs
68 acos = function (x) return math.deg(math.acos(x)) end
69 asin = function (x) return math.deg(math.asin(x)) end
70 atan = function (x) return math.deg(math.atan(x)) end
71 atan2 = function (x,y) return math.deg(math.atan2(x,y)) end
72 ceil = math.ceil
73 cos = function (x) return math.cos(math.rad(x)) end
74 deg = math.deg
75 exp = math.exp
76 floor = math.floor
77 frexp = math.frexp
78 ldexp = math.ldexp
79 log = math.log
80 log10 = math.log10
81 max = math.max
82 min = math.min
83 mod = math.mod
84 PI = math.pi
85 --??? pow = math.pow
86 rad = math.rad
87 random = math.random
88 randomseed = math.randomseed
89 sin = function (x) return math.sin(math.rad(x)) end
90 sqrt = math.sqrt
91 tan = function (x) return math.tan(math.rad(x)) end
93 -------------------------------------------------------------------
94 -- string library
95 local str = string
96 strbyte = str.byte
97 strchar = str.char
98 strfind = str.find
99 format = str.format
100 gsub = str.gsub
101 strlen = str.len
102 strlower = str.lower
103 strrep = str.rep
104 strsub = str.sub
105 strupper = str.upper
107 -------------------------------------------------------------------
108 -- os library
109 clock = os.clock
110 date = os.date
111 difftime = os.difftime
112 execute = os.execute --?
113 exit = os.exit
114 getenv = os.getenv
115 remove = os.remove
116 rename = os.rename
117 setlocale = os.setlocale
118 time = os.time
119 tmpname = os.tmpname
121 -------------------------------------------------------------------
122 -- compatibility only
123 getglobal = function (n) return _G[n] end
124 setglobal = function (n,v) _G[n] = v end
126 -------------------------------------------------------------------
128 local io, tab = io, table
130 -- IO library (files)
131 _STDIN = io.stdin
132 _STDERR = io.stderr
133 _STDOUT = io.stdout
134 _INPUT = io.stdin
135 _OUTPUT = io.stdout
136 seek = io.stdin.seek -- sick ;-)
137 tmpfile = io.tmpfile
138 closefile = io.close
139 openfile = io.open
141 function flush (f)
142 if f then f:flush()
143 else _OUTPUT:flush()
147 function readfrom (name)
148 if name == nil then
149 local f, err, cod = io.close(_INPUT)
150 _INPUT = io.stdin
151 return f, err, cod
152 else
153 local f, err, cod = io.open(name, "r")
154 _INPUT = f or _INPUT
155 return f, err, cod
159 function writeto (name)
160 if name == nil then
161 local f, err, cod = io.close(_OUTPUT)
162 _OUTPUT = io.stdout
163 return f, err, cod
164 else
165 local f, err, cod = io.open(name, "w")
166 _OUTPUT = f or _OUTPUT
167 return f, err, cod
171 function appendto (name)
172 local f, err, cod = io.open(name, "a")
173 _OUTPUT = f or _OUTPUT
174 return f, err, cod
177 function read (...)
178 local f = _INPUT
179 if type(arg[1]) == 'userdata' then
180 f = tab.remove(arg, 1)
182 return f:read(unpack(arg))
185 function write (...)
186 local f = _OUTPUT
187 if type(arg[1]) == 'userdata' then
188 f = tab.remove(arg, 1)
190 return f:write(unpack(arg))