static
int
tslice (lua_State
*
L) {
int
i, stackpos;
const TValue
*
src,
*
dst;
lua_Integer
len
, start, end, newlen;
/
*
Get table size
*
/
len
=
aux_getn(L,
1
, TAB_RW);
/
/
first argument
is
the length of table
luaL_argcheck(L,
len
< INT_MAX,
1
,
"array too big"
);
/
*
Get start
and
end position
*
/
start
=
luaL_checkinteger(L,
2
);
/
/
second argument
is
the start position
end
=
luaL_optinteger(L,
3
,
len
);
/
/
get third argument(
if
has)
is
the end position
if
(lua_isnoneornil(L,
3
))
end
=
len
+
1
;
else
end
=
luaL_checkinteger(L,
3
);
/
*
Check start
and
end position
*
/
if
(start <
=
0
) start
=
1
;
else
if
(start >
len
) start
=
len
;
if
(end <
=
0
) end
=
1
;
else
if
(end >
len
+
1
) end
=
len
+
1
;
luaL_argcheck(L, start <
=
end,
2
,
"invalid slice range"
);
newlen
=
end
-
start;
stackpos
=
lua_gettop(L)
+
1
;
/
*
Create a new array
*
/
lua_createtable(L, newlen,
0
);
if
(
len
>
0
&& newlen >
0
) {
src
=
&(L
-
>ci
-
>func
+
1
)
-
>val;
dst
=
&(L
-
>ci
-
>func
+
stackpos)
-
>val;
for
(i
=
end
-
1
; i >
=
start; i
-
-
) {
hvalue(dst)
-
>array[i
-
start]
=
hvalue(src)
-
>array[i
-
1
];
TValue
*
tv
=
&((Table
*
)src
-
>value_.p)
-
>array[i
-
1
];
printf(
"src: 0x%x 0x%x\n"
, tv
-
>value_, tv
-
>tt_);
}
}
return
1
;
}