File: attrib.lua

package info (click to toggle)
golang-github-yuin-gopher-lua 0.0~git20170915.0.eb1c729-3
  • links: PTS, VCS
  • area: main
  • in suites: buster, buster-backports
  • size: 844 kB
  • sloc: yacc: 479; python: 73; makefile: 18
file content (341 lines) | stat: -rw-r--r-- 8,357 bytes parent folder | download | duplicates (3)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
do --[

print "testing require"

assert(require"string" == string)
assert(require"math" == math)
assert(require"table" == table)
assert(require"io" == io)
assert(require"os" == os)
assert(require"debug" == debug)
assert(require"coroutine" == coroutine)

assert(type(package.path) == "string")
assert(type(package.cpath) == "string")
assert(type(package.loaded) == "table")
assert(type(package.preload) == "table")


local DIR = "libs/"

local function createfiles (files, preextras, posextras)
  for n,c in pairs(files) do
    io.output(DIR..n)
    io.write(string.format(preextras, n))
    io.write(c)
    io.write(string.format(posextras, n))
    io.close(io.output())
  end
end

function removefiles (files)
  for n in pairs(files) do
    os.remove(DIR..n)
  end
end

local files = {
  ["A.lua"] = "",
  ["B.lua"] = "assert(...=='B');require 'A'",
  ["A.lc"] = "",
  ["A"] = "",
  ["L"] = "",
  ["XXxX"] = "",
  ["C.lua"] = "package.loaded[...] = 25; require'C'"
}

AA = nil
local extras = [[
NAME = '%s'
REQUIRED = ...
return AA]]

createfiles(files, "", extras)


local oldpath = package.path

package.path = string.gsub("D/?.lua;D/?.lc;D/?;D/??x?;D/L", "D/", DIR)

local try = function (p, n, r)
  NAME = nil
  local rr = require(p)
  assert(NAME == n)
  assert(REQUIRED == p)
  assert(rr == r)
end

assert(require"C" == 25)
assert(require"C" == 25)
AA = nil
try('B', 'B.lua', true)
assert(package.loaded.B)
assert(require"B" == true)
assert(package.loaded.A)
package.loaded.A = nil
try('B', nil, true)   -- should not reload package
try('A', 'A.lua', true)
package.loaded.A = nil
os.remove(DIR..'A.lua')
AA = {}
try('A', 'A.lc', AA)  -- now must find second option
assert(require("A") == AA)
AA = false
try('K', 'L', false)     -- default option
try('K', 'L', false)     -- default option (should reload it)
assert(rawget(_G, "_REQUIREDNAME") == nil)

AA = "x"
try("X", "XXxX", AA)


removefiles(files)


-- testing require of sub-packages

package.path = string.gsub("D/?.lua;D/?/init.lua", "D/", DIR)

files = {
  ["P1/init.lua"] = "AA = 10",
  ["P1/xuxu.lua"] = "AA = 20",
}

createfiles(files, "module(..., package.seeall)\n", "")
AA = 0

local m = assert(require"P1")
assert(m == P1 and m._NAME == "P1" and AA == 0 and m.AA == 10)
assert(require"P1" == P1 and P1 == m)
assert(require"P1" == P1)
assert(P1._PACKAGE == "")

local m = assert(require"P1.xuxu")
assert(m == P1.xuxu and m._NAME == "P1.xuxu" and AA == 0 and m.AA == 20)
assert(require"P1.xuxu" == P1.xuxu and P1.xuxu == m)
assert(require"P1.xuxu" == P1.xuxu)
assert(require"P1" == P1)
assert(P1.xuxu._PACKAGE == "P1.")
assert(P1.AA == 10 and P1._PACKAGE == "")
assert(P1._G == _G and P1.xuxu._G == _G)



removefiles(files)


package.path = ""
assert(not pcall(require, "file_does_not_exist"))
package.path = "??\0?"
assert(not pcall(require, "file_does_not_exist1"))

package.path = oldpath

-- check 'require' error message
-- local fname = "file_does_not_exist2"
-- local m, err = pcall(require, fname)
-- for t in string.gmatch(package.path..";"..package.cpath, "[^;]+") do
--   t = string.gsub(t, "?", fname)
--   print(t, err)
--   assert(string.find(err, t, 1, true))
-- end


local function import(...)
  local f = {...}
  return function (m)
    for i=1, #f do m[f[i]] = _G[f[i]] end
  end
end

local assert, module, package = assert, module, package
X = nil; x = 0; assert(_G.x == 0)   -- `x' must be a global variable
module"X"; x = 1; assert(_M.x == 1)
module"X.a.b.c"; x = 2; assert(_M.x == 2)
module("X.a.b", package.seeall); x = 3
assert(X._NAME == "X" and X.a.b.c._NAME == "X.a.b.c" and X.a.b._NAME == "X.a.b")
assert(X._M == X and X.a.b.c._M == X.a.b.c and X.a.b._M == X.a.b)
assert(X.x == 1 and X.a.b.c.x == 2 and X.a.b.x == 3)
assert(X._PACKAGE == "" and X.a.b.c._PACKAGE == "X.a.b." and
       X.a.b._PACKAGE == "X.a.")
assert(_PACKAGE.."c" == "X.a.c")
assert(X.a._NAME == nil and X.a._M == nil)
module("X.a", import("X")) ; x = 4
assert(X.a._NAME == "X.a" and X.a.x == 4 and X.a._M == X.a)
module("X.a.b", package.seeall); assert(x == 3); x = 5
assert(_NAME == "X.a.b" and X.a.b.x == 5)

assert(X._G == nil and X.a._G == nil and X.a.b._G == _G and X.a.b.c._G == nil)

setfenv(1, _G)
assert(x == 0)

assert(not pcall(module, "x"))
assert(not pcall(module, "math.sin"))


-- testing C libraries


local p = ""   -- On Mac OS X, redefine this to "_"

-- assert(loadlib == package.loadlib)   -- only for compatibility
-- local f, err, when = package.loadlib("libs/lib1.so", p.."luaopen_lib1")
local f = nil
if not f then
  (Message or print)('\a\n >>> cannot load dynamic library <<<\n\a')
  print(err, when)
else
  f()   -- open library
  assert(require("lib1") == lib1)
  collectgarbage()
  assert(lib1.id("x") == "x")
  f = assert(package.loadlib("libs/lib1.so", p.."anotherfunc"))
  assert(f(10, 20) == "1020\n")
  f, err, when = package.loadlib("libs/lib1.so", p.."xuxu")
  assert(not f and type(err) == "string" and when == "init")
  package.cpath = "libs/?.so"
  require"lib2"
  assert(lib2.id("x") == "x")
  local fs = require"lib1.sub"
  assert(fs == lib1.sub and next(lib1.sub) == nil)
  module("lib2", package.seeall)
  f = require"-lib2"
  assert(f.id("x") == "x" and _M == f and _NAME == "lib2")
  module("lib1.sub", package.seeall)
  assert(_M == fs)
  setfenv(1, _G)
 
end
-- f, err, when = package.loadlib("donotexist", p.."xuxu")
-- assert(not f and type(err) == "string" and (when == "open" or when == "absent"))


-- testing preload

do
  local p = package
  package = {}
  p.preload.pl = function (...)
    module(...)
    function xuxu (x) return x+20 end
  end

  require"pl"
  assert(require"pl" == pl)
  assert(pl.xuxu(10) == 30)

  package = p
  assert(type(package.path) == "string")
end



end  --]

print('+')

print("testing assignments, logical operators, and constructors")

local res, res2 = 27

a, b = 1, 2+3
assert(a==1 and b==5)
a={}
function f() return 10, 11, 12 end
a.x, b, a[1] = 1, 2, f()
assert(a.x==1 and b==2 and a[1]==10)
a[f()], b, a[f()+3] = f(), a, 'x'
assert(a[10] == 10 and b == a and a[13] == 'x')

do
  local f = function (n) local x = {}; for i=1,n do x[i]=i end;
                         return unpack(x) end;
  local a,b,c
  a,b = 0, f(1)
  assert(a == 0 and b == 1)
  A,b = 0, f(1)
  assert(A == 0 and b == 1)
  a,b,c = 0,5,f(4)
  assert(a==0 and b==5 and c==1)
  a,b,c = 0,5,f(0)
  assert(a==0 and b==5 and c==nil)
end


a, b, c, d = 1 and nil, 1 or nil, (1 and (nil or 1)), 6
assert(not a and b and c and d==6)

d = 20
a, b, c, d = f()
assert(a==10 and b==11 and c==12 and d==nil)
a,b = f(), 1, 2, 3, f()
assert(a==10 and b==1)

assert(a<b == false and a>b == true)
assert((10 and 2) == 2)
assert((10 or 2) == 10)
assert((10 or assert(nil)) == 10)
assert(not (nil and assert(nil)))
assert((nil or "alo") == "alo")
assert((nil and 10) == nil)
assert((false and 10) == false)
assert((true or 10) == true)
assert((false or 10) == 10)
assert(false ~= nil)
assert(nil ~= false)
assert(not nil == true)
assert(not not nil == false)
assert(not not 1 == true)
assert(not not a == true)
assert(not not (6 or nil) == true)
assert(not not (nil and 56) == false)
assert(not not (nil and true) == false)
print('+')

a = {}
a[true] = 20
a[false] = 10
assert(a[1<2] == 20 and a[1>2] == 10)

function f(a) return a end

local a = {}
for i=3000,-3000,-1 do a[i] = i; end
a[10e30] = "alo"; a[true] = 10; a[false] = 20
assert(a[10e30] == 'alo' and a[not 1] == 20 and a[10<20] == 10)
for i=3000,-3000,-1 do assert(a[i] == i); end
a[print] = assert
a[f] = print
a[a] = a
assert(a[a][a][a][a][print] == assert)
a[print](a[a[f]] == a[print])
a = nil

a = {10,9,8,7,6,5,4,3,2; [-3]='a', [f]=print, a='a', b='ab'}
a, a.x, a.y = a, a[-3]
assert(a[1]==10 and a[-3]==a.a and a[f]==print and a.x=='a' and not a.y)
a[1], f(a)[2], b, c = {['alo']=assert}, 10, a[1], a[f], 6, 10, 23, f(a), 2
a[1].alo(a[2]==10 and b==10 and c==print)

a[2^31] = 10; a[2^31+1] = 11; a[-2^31] = 12;
a[2^32] = 13; a[-2^32] = 14; a[2^32+1] = 15; a[10^33] = 16;

assert(a[2^31] == 10 and a[2^31+1] == 11 and a[-2^31] == 12 and
       a[2^32] == 13 and a[-2^32] == 14 and a[2^32+1] == 15 and
       a[10^33] == 16)

a = nil


-- do
--   local a,i,j,b
--   a = {'a', 'b'}; i=1; j=2; b=a
--   i, a[i], a, j, a[j], a[i+j] = j, i, i, b, j, i
--   assert(i == 2 and b[1] == 1 and a == 1 and j == b and b[2] == 2 and
--          b[3] == 1)
-- end

print('OK')

return res