-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
1343 lines (1160 loc) · 39.5 KB
/
Copy pathinit.lua
File metadata and controls
1343 lines (1160 loc) · 39.5 KB
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[
Normalized Lua API for Lua 5.1, 5.2, 5.3, 5.4 & 5.5
Copyright (C) 2002-2026 std.normalize authors
]]
--[[--
Normalize API differences between supported Lua implementations.
Respecting the values set in the `std._debug` settings module, inject
deterministic identically behaving cross-implementation low-level
functions into the callers environment.
Writing Lua libraries that target several Lua implementations can be a
frustrating exercise in working around lots of small differences in APIs
and semantics they share (or rename, or omit). _normalize_ provides the
means to simply access deterministic implementations of those APIs that
have the the same semantics across all supported host Lua
implementations. Each function is as thin and fast an implementation as
is possible within that host Lua environment, evaluating to the Lua C
implementation with no overhead where host semantics allow.
The core of this module is to transparently set the environment up with
a single API (as opposed to requiring caching functions from a module
table into module locals):
local _ENV = require 'std.normalize' {
'package',
'std.prototype',
strict = 'std.strict',
}
It is not yet complete, and in contrast to the kepler project
lua-compat libraries, neither does it attempt to provide you with as
nearly compatible an API as is possible relative to some specific Lua
implementation - rather it provides a variation of the "lowest common
denominator" that can be implemented relatively efficiently in the
supported Lua implementations, all in pure Lua.
At the moment, only the functionality used by stdlib is implemented.
@module std.normalize
]]
--[[ ====================== ]]--
--[[ Load optional modules. ]]--
--[[ ====================== ]]--
local _debug = (function()
local ok, r = pcall(require, 'std._debug')
if not ok then
r = setmetatable({
-- If this module was required, but there's no std._debug, safe to
-- assume we do want runtime argchecks!
argcheck = true,
-- Similarly, if std.strict is available, but there's no _std.debug,
-- then apply strict global symbol checks to this module!
strict = true,
}, {
__call = function(self, x)
self.argscheck = (x ~= false)
end,
})
end
return r
end)()
local strict = (function()
local setfenv = rawget(_G, 'setfenv') or function() end
-- No strict global symbol checks with no std.strict module, even
-- if we found std._debug and requested that!
local r = function(env, level)
setfenv(1 + (level or 1), env)
return env
end
if _debug.strict then
-- Specify `.init` submodule to make sure we only accept
-- lua-stdlib/strict, and not the old strict module from
-- lua-stdlib/lua-stdlib.
local ok, m = pcall(require, 'std.strict.init')
if ok then
r = m
end
end
return r
end)()
local typecheck = (function()
local format = string.format
local ok, r = pcall(require, 'typecheck')
if ok then
return r
end
return {
ARGCHECK_FRAME = 0,
-- Return `inner` untouched, for no runtime overhead!
argscheck = function(decl, inner)
return inner or setmetatable({}, {
__concat = function(_, inner)
return inner
end,
})
end,
argerror = function(name, i, extramsg, level)
level = level or 1
local s = format("bad argument #%d to '%s'", i, name)
if extramsg ~= nil then
s = s .. ' (' .. extramsg .. ')'
end
error(s, level > 0 and level + 2 or 0)
end,
}
end)()
local _ENV = strict(_G)
local ARGCHECK_FRAME = typecheck.ARGCHECK_FRAME
local argerror = typecheck.argerror
local argscheck = typecheck.argscheck
local concat = table.concat
local config = package.config
local debug_getfenv = debug.getfenv or false
local debug_getinfo = debug.getinfo
local debug_getupvalue = debug.getupvalue
local debug_setfenv = debug.setfenv or false
local debug_setupvalue = debug.setupvalue
local debug_upvaluejoin = debug.upvaluejoin
local exit = os.exit
local format = string.format
local getfenv = rawget(_G, 'getfenv') or false
local gmatch = string.gmatch
local gsub = string.gsub
local loadstring = rawget(_G, 'loadstring') or load
local match = string.match
local open = io.open
local remove = table.remove
local searchpath = package.searchpath or false
local setfenv = rawget(_G, 'setfenv') or false
local sort = table.sort
local unpack = table.unpack or unpack
local upper = string.upper
--[[ =============== ]]--
--[[ Implementation. ]]--
--[[ =============== ]]--
-- At this point, only the locals imported above are visible (even in
-- Lua 5.1). If 'std.strict' is available, we'll also get a runtime
-- error if any of the code below tries to use an undeclared variable.
local dirsep, pathsep, pathmark, execdir, igmark =
match(config, '^([^\n]+)\n([^\n]+)\n([^\n]+)\n([^\n]+)\n([^\n]+)')
local function callable(x)
-- Careful here!
-- Most versions of Lua don't recurse functables, so make sure you
-- always put a real function in __call metamethods. Consequently,
-- no reason to recurse here.
-- func=function() print 'called' end
-- func() --> 'called'
-- functable=setmetatable({}, {__call=func})
-- functable() --> 'called'
-- nested=setmetatable({}, {__call=function(self, ...) return functable(...)end})
-- nested() -> 'called'
-- notnested=setmetatable({}, {__call=functable})
-- notnested()
-- --> stdin:1: attempt to call global 'nested' (a table value)
-- --> stack traceback:
-- --> stdin:1: in main chunk
-- --> [C]: in ?
if type(x) == 'function' or (getmetatable(x) or {}).__call then
return x
end
end
local tointeger = (function(f)
if f == nil then
-- No host tointeger implementationm use our own.
local floor = math.floor
return function(x)
if type(x) == 'number' and x - floor(x) == 0.0 then
return x
end
end
elseif f '1' ~= nil then
-- Don't perform implicit string-to-number conversion!
return function(x)
if type(x) == 'number' then
return f(x)
end
end
end
-- Host tointeger is good!
return f
end)(math.tointeger)
-- It's hard to test at require-time whether the host `os.exit` handles
-- boolean argument properly (ostensibly to defer to it in that case).
-- We're shutting down anyway, so sacrifice a bit of speed for timely
-- diagnosis of float and nil valued argument (with the argscheck
-- annotation, later in the file), since that probably indicates a bug
-- in your code!
local _exit = exit
local function exit(...)
local n, status = select('#', ...), ...
if tointeger(n) == 0 or status == true then
_exit(0)
elseif status == false then
_exit(1)
end
_exit(status)
end
local normalize_getfenv
if debug_getfenv then
normalize_getfenv = function(fn)
local n = tointeger(fn or 1)
if n then
if n > 0 then
-- Adjust for this function's stack frame, if fn is non-zero.
n = n + 1 + ARGCHECK_FRAME
end
-- Return an additional nil result to defeat tail call elimination
-- which would remove a stack frame and break numeric *fn* count.
return getfenv(n), nil
end
if type(fn) ~= 'function' then
-- Unwrap functors:
-- No need to recurse because Lua doesn't support nested functors.
-- __call can only (sensibly) be a function, so no need to adjust
-- stack frame offset either.
fn =(getmetatable(fn) or {}).__call or fn
end
-- In Lua 5.1, only debug.getfenv works on C functions; but it
-- does not work on stack counts.
return debug_getfenv(fn)
end
else
-- Thanks to http://lua-users.org/lists/lua-l/2010-06/msg00313.html
normalize_getfenv = function(fn)
if fn == 0 then
return _G
end
local n = tointeger(fn or 1)
if n then
fn = debug_getinfo(n + 1 + ARGCHECK_FRAME, 'f').func
elseif type(fn) ~= 'function' then
fn = (getmetatable(fn) or {}).__call or fn
end
local name, env
local up = 0
repeat
up = up + 1
name, env = debug_getupvalue(fn, up)
until name == '_ENV' or name == nil
return env
end
end
local function getmetamethod(x, n)
return callable((getmetatable(x) or {})[n])
end
local function rawlen(x)
-- Lua 5.1 does not implement rawlen, and while # operator ignores
-- __len metamethod, `nil` in sequence is handled inconsistently.
if type(x) ~= 'table' then
return #x
end
local n = #x
for i = 1, n do
if x[i] == nil then
return i -1
end
end
return n
end
local function len(x)
return (getmetamethod(x, '__len') or rawlen)(x)
end
local function ipairs(l)
if getmetamethod(l, '__len') then
-- Use a closure to capture len metamethod result if necessary.
local n = len(l)
return function(l, i)
i = i + 1
if i <= n then
return i, l[i]
end
end, l, 0
end
-- ...otherwise, find the last item as we go without calling `len()`.
return function(l, i)
i = i + 1
if l[i] ~= nil then
return i, l[i]
end
end, l, 0
end
local load = (function(ok)
if not ok then
return function(...)
if type(...) == 'string' then
return loadstring(...)
end
return _G.load(...)
end
end
return _G.load
end)(pcall(load, '_=1'))
local function normalize_load(chunk, chunkname)
local m = getmetamethod(chunk, '__call')
if m then
chunk = m
elseif getmetamethod(chunk, '__tostring') then
chunk = tostring(chunk)
end
if getmetamethod(chunkname, '__tostring') then
chunkname = tostring(chunkname)
end
return load(chunk, chunkname)
end
local function merge(t, r)
r = r or {}
for k, v in next, t do
r[k] = r[k] or v
end
return r
end
local pack = (function(f)
local pack_mt = {
__len = function(self)
return self.n
end,
}
local pack_fn = f or function(...)
return {n=select('#', ...), ...}
end
return function(...)
return setmetatable(pack_fn(...), pack_mt)
end
end)(rawget(_G, "pack"))
local pairs = (function(b)
if b then
-- Add support for __pairs when missing.
return function (t)
return (getmetamethod(t, '__pairs') or pairs)(t)
end
end
return _G.pairs
end)(not not pairs(setmetatable({},{__pairs=function() return false end})))
local function keys(t)
local r = {}
for k in pairs(t) do
r[#r + 1] = k
end
return r
end
local pathmatch_patt = '[^' .. pathsep .. ']+'
local searchpath = searchpath or function(name, path, sep, rep)
name = gsub(name, sep or '%.', rep or dirsep)
local errbuf = {}
for template in gmatch(path, pathmatch_patt) do
local filename = gsub(template, pathmark, name)
local fh = open(filename, 'r')
if fh then
fh:close()
return filename
end
errbuf[#errbuf + 1] = "\tno file '" .. filename .. "'"
end
return nil, concat(errbuf, '\n')
end
local normalize_setfenv
if debug_setfenv then
normalize_setfenv = function(fn, env)
local n = tointeger(fn or 1)
if n then
if n > 0 then
n = n + 1 + ARGCHECK_FRAME
end
return setfenv(n, env), nil
end
if type(fn) ~= 'function' then
fn =(getmetatable(fn) or {}).__call or fn
end
return debug_setfenv(fn, env)
end
else
-- Thanks to http://lua-users.org/lists/lua-l/2010-06/msg00313.html
normalize_setfenv = function(fn, env)
local n = tointeger(fn or 1)
if n then
if n > 0 then
n = n + 1 + ARGCHECK_FRAME
end
fn = debug_getinfo(n, 'f').func
elseif type(fn) ~= 'function' then
fn =(getmetatable(fn) or {}).__call or fn
end
local up, name = 0
repeat
up = up + 1
name = debug_getupvalue(fn, up)
until name == '_ENV' or name == nil
if name then
debug_upvaluejoin(fn, up, function() return name end, 1)
debug_setupvalue(fn, up, env)
end
return n ~= 0 and fn or nil
end
end
local shallow_copy = merge
local function render(x, vfns, roots)
if vfns.term(x) then
return vfns.elem(x)
end
roots = roots or {}
local function stop_roots(x)
return roots[x] or render(x, vfns, shallow_copy(roots))
end
local buf, pair, sep = {vfns.open(x)}, vfns.pair, vfns.sep
roots[x] = vfns.elem(x) -- recursion protection
local seqp, kp, vp -- proper sequence?, previous key and value
local keylist = vfns.sort(keys(x))
for i, k in ipairs(keylist) do
local v = x[k]
buf[#buf + 1] = sep(x, kp, vp, k, v, seqp) -- buffer << separator
if k == 1 then
seqp = true
else
seqp = seqp and type(kp) == 'number' and k == kp + 1
end
buf[#buf + 1] = pair(x, kp, vp, k, v, stop_roots(k), stop_roots(v), seqp)
kp, vp = k, v
end
buf[#buf + 1] = sep(x, kp, vp) -- buffer << trailing separator
buf[#buf + 1] = vfns.close(x) -- buffer << table close
return concat(buf) -- stringify buffer
end
local function always(x)
return function(...) return x end
end
local function keysort(a, b)
if type(a) == 'number' then
return type(b) ~= 'number' or a < b
else
return type(b) ~= 'number' and tostring(a) < tostring(b)
end
end
local strvtable = {
open = always '{',
close = always '}',
elem = setmetatable({
['\a'] = [[\a]],
['\b'] = [[\b]],
['\t'] = [[\t]],
['\n'] = [[\n]],
['\v'] = [[\v]],
['\f'] = [[\f]],
['\r'] = [[\r]],
['\\'] = [[\\]],
}, {
__call = function(map, x)
return gsub(tostring(x), '[\a\b\t\n\v\f\r]', function(c)
return map[c]
end)
end,
}),
pair = function(x, kp, vp, k, v, kstr, vstr, seqp)
if seqp then
return vstr
end
return kstr .. '=' .. vstr
end,
sep = function(x, kp, vp, k, v, seqp)
if kp == nil or k == nil then
return ''
elseif seqp and type(kp) == 'number' and k ~= kp + 1 then
return '; '
end
return ', '
end,
sort = function(keys)
sort(keys, keysort)
return keys
end,
term = function(x)
return type(x) ~= 'table' or getmetamethod(x, '__tostring')
end,
}
local function str(x)
return render(x, strvtable)
end
local function math_type(x)
if type(x) ~= 'number' then
return nil
end
return tointeger(x) and 'integer' or 'float'
end
local _unpack = unpack
local function unpack(t, i, j)
return _unpack(t, tointeger(i) or 1, tointeger(j) or len(t))
end
do
local have_xpcall_args = false
local function catch(arg) have_xpcall_args = arg end
xpcall(catch, function() end, true)
if not have_xpcall_args then
local _xpcall = xpcall
xpcall = function(fn, errh, ...)
local argu = pack(...)
return _xpcall(function()
return fn(unpack(argu, 1, argu.n))
end, errh)
end
end
end
--[[ ================= ]]--
--[[ Public Interface. ]]--
--[[ ================= ]]--
local F = {
_VERSION = _G._VERSION,
arg = _G.arg,
--- Raise a bad argument error.
-- Equivalent to luaL_argerror in the Lua C API. This function does not
-- return. The `level` argument behaves just like the core `error`
-- function.
-- @function argerror
-- @string name function to callout in error message
-- @int i argument number
-- @string[opt] extramsg additional text to append to message inside
-- parentheses
-- @int[opt=1] level call stack level to blame for the error
-- @usage
-- local function slurp(file)
-- local h, err = input_handle(file)
-- if h == nil then
-- argerror('std.io.slurp', 1, err, 2)
-- end
-- ...
argerror = argerror,
assert = _G.assert,
collectgarbage = _G.collectgarbage,
dofile = _G.dofile,
error = _G.error,
--- Get a function or functor environment.
--
-- This version of getfenv works on all supported Lua versions, and
-- knows how to unwrap functors (table's with a function valued
-- `__call` metamethod).
-- @function getfenv
-- @tparam[opt=1] function|int fn stack level, C or Lua function or
-- functor to act on
-- @treturn table the execution environment of *fn*
-- @usage
-- callers_environment = getfenv(1)
getfenv = argscheck 'getfenv([callable|integer])'
.. normalize_getfenv,
--- Return named metamethod, if callable, otherwise `nil`.
-- @function getmetamethod
-- @param x item to act on
-- @string n name of metamethod to look up
-- @treturn function|nil metamethod function, or `nil` if no
-- metamethod
-- @usage
-- normalize = getmetamethod(require 'std.normalize', '__call')
getmetamethod = argscheck 'getmetamethod(?any, string)'
.. getmetamethod,
getmetatable = _G.getmetatable,
--- Iterate over elements of a sequence, until the first `nil` value.
--
-- Returns successive key-value pairs with integer keys starting at 1,
-- up to the index returned by the `__len` metamethod if any, or else
-- up to last non-`nil` value.
--
-- Unlike Lua 5.1, any `__index` metamethod is respected.
--
-- Unlike Lua 5.2+, any `__ipairs` metamethod is **ignored**!
-- @function ipairs
-- @tparam table t table to iterate on
-- @treturn function iterator function
-- @treturn table *t* the table being iterated over
-- @treturn int the previous iteration index
-- @usage
-- t, u = {}, {}
-- for i, v in ipairs {1, 2, nil, 4} do t[i] = v end
-- assert(len(t) == 2)
--
-- for i, v in ipairs(pack(1, 2, nil, 4)) do u[i] = v end
-- assert(len(u) == 4)
ipairs = argscheck 'ipairs(table)'
.. ipairs,
--- Deterministic, functional version of core Lua `#` operator.
--
-- Respects `__len` metamethod (like Lua 5.2+), or else if there is
-- a `__tostring` metamethod return the length of the string it
-- returns. Otherwise, always return one less than the lowest
-- integer index with a `nil` value in *x*, where the `#` operator
-- implementation might return the size of the array part of a table.
-- @function len
-- @param x item to act on
-- @treturn int the length of *x*
-- @usage
-- x = {1, 2, 3, nil, 5}
-- --> 5 3
-- print(#x, len(x))
len = argscheck 'len(string|table)'
.. len,
--- Load a string or a function, just like Lua 5.2+.
-- @function load
-- @tparam string|function ld chunk to load
-- @string source name of the source of *ld*
-- @treturn function a Lua function to execute *ld* in global scope.
-- @usage
-- assert(load 'print "woo"')()
load = argscheck 'load(callable|string, [string])'
.. normalize_load,
loadfile = _G.loadfile,
next = _G.next,
--- Return a list of given arguments, with field `n` set to the length.
--
-- The returned table also has a `__len` metamethod that returns `n`, so
-- `ipairs` and `unpack` behave sanely when there are `nil` valued elements.
-- @function pack
-- @param ... tuple to act on
-- @treturn table packed list of *...* values, with field `n` set to
-- number of tuple elements (including any explicit `nil` elements)
-- @see unpack
-- @usage
-- --> 5
-- len(pack(nil, 2, 5, nil, nil))
pack = pack,
--- Like Lua `pairs` iterator, but respect `__pairs` even in Lua 5.1.
-- @function pairs
-- @tparam table t table to act on
-- @treturn function iterator function
-- @treturn table *t*, the table being iterated over
-- @return the previous iteration key
-- @usage
-- for k, v in pairs {'a', b='c', foo=42} do process(k, v) end
pairs = argscheck 'pairs(table)'
.. pairs,
pcall = _G.pcall,
print = _G.print,
rawequal = _G.rawequal,
rawget = _G.rawget,
--- Length of a string or table object without using any metamethod.
-- @function rawlen
-- @tparam string|table x object to act on
-- @treturn int raw length of *x*
-- @usage
-- --> 0
-- rawlen(setmetatable({}, {__len=function() return 42}))
rawlen = argscheck 'rawlen(string|table)'
.. rawlen,
rawset = _G.rawset,
select = _G.select,
--- Set a function or functor environment.
--
-- This version of setfenv works on all supported Lua versions, and
-- knows how to unwrap functors.
-- @function setfenv
-- @tparam function|int fn stack level, C or Lua function or functor
-- to act on
-- @tparam table env new execution environment for *fn*
-- @treturn function function acted upon
-- @usage
-- function clearenv(fn) return setfenv(fn, {}) end
setfenv = argscheck 'setfenv(integer|callable, table)'
.. normalize_setfenv,
setmetatable = _G.setmetatable,
--- Return a compact stringified representation of argument.
-- @function str
-- @param x item to act on
-- @treturn string compact string representing *x*
-- @usage
-- -- {baz,5,foo=bar}
-- print(str{foo='bar','baz', 5})
str = str,
tonumber = _G.tonumber,
tostring = _G.tostring,
type = _G.type,
--- Either `table.unpack` in newer-, or `unpack` in older Lua implementations.
-- @function unpack
-- @tparam table t table to act on
-- @int[opt=1] i first index to unpack
-- @int[opt=len(t)] j last index to unpack
-- @return ... values of numeric indices of *t*
-- @see pack
-- @usage
-- local a, b, c = unpack(pack(nil, 2, nil))
-- assert(a == nil and b == 2 and c == nil)
unpack = argscheck'unpack(table, [?integer], [integer])'
.. unpack,
--- Support arguments to a protected function call, even on Lua 5.1.
-- @function xpcall
-- @tparam function f protect this function call
-- @tparam function errh error object handler callback if *f* raises
-- an error
-- @param ... arguments to pass to *f*
-- @treturn[1] boolean `false` when `f(...)` raised an error
-- @treturn[1] string error message
-- @treturn[2] boolean `true` when `f(...)` succeeded
-- @return ... all return values from *f* follow
-- @usage
-- -- Use errh to get a backtrack after curses exits abnormally
-- xpcall(main, errh, arg, opt)
xpcall = argscheck 'xpcall(callable, callable, [?any...])'
.. xpcall,
}
local G = {
coroutine = {
create = _G.coroutine.create,
resume = _G.coroutine.resume,
running = _G.coroutine.running,
status = _G.coroutine.status,
wrap = _G.coroutine.wrap,
yield = _G.coroutine.yield,
},
debug = {
debug = _G.debug.debug,
gethook = _G.debug.gethook,
getinfo = _G.debug.getinfo,
getlocal = _G.debug.getlocal,
getmetatable = _G.debug.getmetatable,
getregistry = _G.debug.getregistry,
getupvalue = _G.debug.getupvalue,
getuservalue = _G.debug.getuservalue,
sethook = _G.debug.sethook,
setmetatable = _G.debug.setmetatable,
setupvalue = _G.debug.setupvalue,
setuservalue = _G.debug.setuservalue,
traceback = _G.debug.traceback,
upvalueid = _G.debug.upvalueid,
upvaluejoin = _G.debug.upvaluejoin,
},
io = {
close = _G.io.close,
flush = _G.io.flush,
input = _G.io.input,
lines = _G.io.lines,
open = _G.io.open,
output = _G.io.output,
popen = _G.io.popen,
read = _G.io.read,
stderr = _G.io.stderr,
stdin = _G.io.stdin,
stdout = _G.io.stdout,
tmpfile = _G.io.tmpfile,
type = _G.io.type,
write = _G.io.write,
},
math = {
abs = _G.math.abs,
acos = _G.math.acos,
asin = _G.math.asin,
atan = _G.math.atan,
ceil = _G.math.ceil,
cos = _G.math.cos,
deg = _G.math.deg,
exp = _G.math.exp,
floor = _G.math.floor,
fmod = _G.math.fmod,
huge = _G.math.huge,
log = _G.math.log,
max = _G.math.max,
min = _G.math.min,
modf = _G.math.modf,
pi = _G.math.pi,
rad = _G.math.rad,
random = _G.math.random,
randomseed = _G.math.randomseed,
sin = _G.math.sin,
sqrt = _G.math.sqrt,
tan = _G.math.tan,
--- Convert to an integer and return if possible, otherwise `nil`.
-- @function math.tointeger
-- @param x object to act on
-- @treturn[1] integer *x* converted to an integer if possible
-- @return[2] `nil` otherwise
tointeger = argscheck 'math.tointeger(?any)'
.. tointeger,
--- Return 'integer', 'float' or `nil` according to argument type.
--
-- To ensure the same behaviour on all host Lua implementations,
-- this function returns 'float' for integer-equivalent floating
-- values, even on Lua 5.3.
-- @function math.type
-- @param x object to act on
-- @treturn[1] string 'integer', if *x* is a whole number
-- @treturn[2] string 'float', for other numbers
-- @return[3] `nil` otherwise
type = argscheck 'math.type(?any)'
.. math_type,
},
os = {
clock = _G.os.clock,
date = _G.os.date,
difftime = _G.os.difftime,
execute = _G.os.execute,
--- Exit the program.
-- @function os.exit
-- @tparam bool|number[opt=true] status report back to parent process
-- @usage
-- exit(len(records.processed) > 0)
exit = argscheck 'os.exit([boolean|integer])'
.. exit,
getenv = _G.os.getenv,
remove = _G.os.remove,
rename = _G.os.rename,
setlocale = _G.os.setlocale,
time = _G.os.time,
tmpname = _G.os.tmpname,
},
package = {
config = _G.package.config,
cpath = _G.package.cpath,
--- Package module constants for `package.config` substrings.
-- @table package
-- @string dirsep directory separator in path elements
-- @string execdir replaced by the executable's directory in a path
-- @string igmark ignore everything before this when building
-- `luaopen_` function name
-- @string pathmark mark substitution points in a path template
-- @string pathsep element separator in a path template
dirsep = dirsep,
execdir = execdir,
igmark = igmark,
pathmark = pathmark,
pathsep = pathsep,
loadlib = _G.package.loadlib,
path = _G.package.path,
preload = _G.package.preload,
searchers = _G.package.searchers or _G.package.loaders,
--- Searches for a named file in a given path.
--
-- For each `package.pathsep` delimited template in the given path,
-- search for an readable file made by first substituting for *sep*
-- with `package.dirsep`, and then replacing any
-- `package.pathmark` with the result. The first such file, if any
-- is returned.
-- @function package.searchpath
-- @string name name of search file
-- @string path `package.pathsep` delimited list of full path templates
-- @string[opt='.'] sep *name* component separator
-- @string[opt=`package.dirsep`] rep *sep* replacement in template
-- @treturn[1] string first template substitution that names a file
-- that can be opened in read mode
-- @return[2] `nil`
-- @treturn[2] string error message listing all failed paths
searchpath = argscheck(
'package.searchpath(string, string, [?string], [string])'
) .. searchpath,
},
string = {
byte = _G.string.byte,
char = _G.string.char,
dump = _G.string.dump,
find = _G.string.find,
format = _G.string.format,
gmatch = _G.string.gmatch,
gsub = _G.string.gsub,
lower = _G.string.lower,
match = _G.string.match,
rep = _G.string.rep,
--- Low-level recursive data to string rendering.
-- @function string.render
-- @param x data to be renedered
-- @tparam RenderFns vfns table of virtual functions to control rendering
-- @tparam[opt] table roots used internally for cycle detection
-- @treturn string a text recursive rendering of *x* using *vfns*
-- @usage
-- function printarray(x)
-- return render(x, arrayvfns)
-- end