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 33rawtype = type 34 35function 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 41end 42 43function dostring(s) return do_(load(s)) end 44-- function dofile(s) return do_(loadfile(s)) end 45 46------------------------------------------------------------------- 47-- Table library 48local tab = table 49getn = function (tab) return #tab end 50tinsert = tab.insert 51tremove = tab.remove 52sort = tab.sort 53 54------------------------------------------------------------------- 55-- Debug library 56local dbg = debug 57getinfo = dbg.getinfo 58getlocal = dbg.getlocal 59setcallhook = function () error"`setcallhook' is deprecated" end 60setlinehook = function () error"`setlinehook' is deprecated" end 61setlocal = dbg.setlocal 62 63------------------------------------------------------------------- 64-- math library 65local math = math 66abs = math.abs 67acos = function (x) return math.deg(math.acos(x)) end 68asin = function (x) return math.deg(math.asin(x)) end 69atan = function (x) return math.deg(math.atan(x)) end 70atan2 = function (x,y) return math.deg(math.atan2(x,y)) end 71ceil = math.ceil 72cos = function (x) return math.cos(math.rad(x)) end 73deg = math.deg 74exp = math.exp 75floor = math.floor 76frexp = math.frexp 77ldexp = math.ldexp 78log = math.log 79log10 = function(val) return math.log(10, val) end 80max = math.max 81min = math.min 82mod = math.mod 83PI = math.pi 84--??? pow = math.pow 85rad = math.rad 86random = math.random 87randomseed = math.randomseed 88sin = function (x) return math.sin(math.rad(x)) end 89sqrt = math.sqrt 90tan = function (x) return math.tan(math.rad(x)) end 91 92------------------------------------------------------------------- 93-- string library 94local str = string 95strbyte = str.byte 96strchar = str.char 97strfind = str.find 98format = str.format 99gsub = str.gsub 100strlen = str.len 101strlower = str.lower 102strrep = str.rep 103strsub = str.sub 104strupper = str.upper 105 106------------------------------------------------------------------- 107-- os library 108clock = os.clock 109date = os.date 110difftime = os.difftime 111execute = os.execute --? 112exit = os.exit 113getenv = os.getenv 114remove = os.remove 115rename = os.rename 116setlocale = os.setlocale 117time = os.time 118tmpname = os.tmpname 119 120------------------------------------------------------------------- 121-- compatibility only 122getglobal = function (n) return _G[n] end 123setglobal = function (n,v) _G[n] = v end 124 125------------------------------------------------------------------- 126 127local 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 135seek = io.stdin.seek -- sick ;-) 136tmpfile = io.tmpfile 137closefile = io.close 138openfile = io.open 139 140function flush (f) 141 if f then f:flush() 142 else _OUTPUT:flush() 143 end 144end 145 146function 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 156end 157 158function 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 168end 169 170function appendto (name) 171 local f, err, cod = io.open(name, "a") 172 _OUTPUT = f or _OUTPUT 173 return f, err, cod 174end 175 176function 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)) 183end 184 185function 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)) 192end 193 194