1 ----------------------------------------------------------------------
\r
2 ----------------------------------------------------------------------
\r
4 -- Base library extension
\r
6 ----------------------------------------------------------------------
\r
7 ----------------------------------------------------------------------
\r
9 if not metalua then rawset(getfenv(), 'metalua', { }) end
\r
10 metalua.version = "v-0.4"
\r
11 metalua.ext_compiler_prefix = 'extension-compiler.'
\r
12 metalua.ext_runtime_prefix = 'extension-runtime.'
\r
14 if not rawpairs then
\r
15 rawpairs, rawipairs, rawtype = pairs, ipairs, type
\r
19 assert(type(x)=='table', 'pairs() expects a table')
\r
20 local mt = getmetatable(x)
\r
22 local mtp = mt.__pairs
\r
23 if mtp then return mtp(x) end
\r
29 assert(type(x)=='table', 'ipairs() expects a table')
\r
30 local mt = getmetatable(x)
\r
32 local mti = mt.__ipairs
\r
33 if mti then return mti(x) end
\r
40 local mt = getmetatable(x)
\r
42 local mtt = mt.__type
\r
43 if mtt then return mtt end
\r
49 function min (a, ...)
\r
50 for n in values{...} do if n<a then a=n end end
\r
54 function max (a, ...)
\r
55 for n in values{...} do if n>a then a=n end end
\r
61 local function g (...)
\r
62 local result = {...}
\r
63 for i=#args, 1, -1 do result = {args[i](unpack(result))} end
\r
64 return unpack (result)
\r
69 function id (...) return ... end
\r
70 function const (k) return function () return k end end
\r
72 function printf(...) return print(string.format(...)) end
\r
74 function ivalues (x)
\r
75 assert(type(x)=='table', 'ivalues() expects a table')
\r
77 local function iterator ()
\r
78 local r = x[i]; i=i+1; return r
\r
84 function values (x)
\r
85 assert(type(x)=='table', 'values() expects a table')
\r
86 local function iterator (state)
\r
88 state.content, it = next(state.list, state.content)
\r
91 return iterator, { list = x }
\r
95 assert(type(x)=='table', 'keys() expects a table')
\r
96 local function iterator (state)
\r
97 local it = next(state.list, state.content)
\r
101 return iterator, { list = x }
\r