-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtut.lua
115 lines (93 loc) · 1.48 KB
/
tut.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
who="Lua user"
print("hello " .. who)
print(1 ~= 0)
-- user data
-- foreign to Lua, obj in C
a, b = 1, 2
print(a, b)
a, b = b, a
print(a, b)
print(math.pi)
x = tonumber("123") + 25
print(x)
x = 100 + "7"
print(x)
x = "Green bottles: " .. 10
print(x)
-- slow
local s = ''
for i=1,10000 do
s = s .. math.random() .. ','
end
-- io.stdout:write(s)
-- fast
for i=1, 1000 do
tostring(math.random())
end
local t = {}
for i=1,10000 do
t[i] = tostring(math.random())
end
x = string.byte("ABCDE", 2)
print(x)
x = string.char(65,66,67,68,69)
print(x)
x, y = string.find("hello Lua user", "Lua")
print(x, y)
list = {{3}, {5}, {2}, {-1}}
table.sort(list, function(a, b) return a[1] < b[1] end)
for i, v in ipairs(list) do
print(v[1])
end
-- variable args
f = function(x, ...)
x(...)
end
f(print, "1 2 3", "abc")
-- select
f = function(...)
print(select('#', ...))
print(select(3, ...))
end
f(1,2,3,4,5)
-- ...->table
f = function(...)
tbl = {...}
print(_VERSION)
-- print(table.unpack(tbl))
end
f("a", "b", "c")
a = {b={}}
function a.b.f(...)
print(...)
end
a.b.f(1,2,3)
-- closure
local x = 5
local function f()
print(x)
end
f()
x = 6
f()
-- env in Lua5.1
-- each func has env table
-- getfenv/setfenv
-- * stack lvel
-- 1: curr func
-- 2: func that called curr func
print(getfenv(1) == _G)
a = 1
b = nil
local function f(t)
local print=print
setfenv(1, t)
print(getmetatable)
a=2
b=3
end
local t = {}
f(t)
print(a, b)
print(t.a, t.b)
--