diff options
Diffstat (limited to '')
-rw-r--r-- | ltests.c | 134 |
1 files changed, 92 insertions, 42 deletions
@@ -327,37 +327,40 @@ void lua_printobj (lua_State *L, struct GCObject *o) { | |||
327 | 327 | ||
328 | 328 | ||
329 | void lua_printvalue (TValue *v) { | 329 | void lua_printvalue (TValue *v) { |
330 | switch (ttype(v)) { | 330 | switch (ttypetag(v)) { |
331 | case LUA_TNUMBER: { | 331 | case LUA_VNUMINT: case LUA_VNUMFLT: { |
332 | char buff[LUA_N2SBUFFSZ]; | 332 | char buff[LUA_N2SBUFFSZ]; |
333 | unsigned len = luaO_tostringbuff(v, buff); | 333 | unsigned len = luaO_tostringbuff(v, buff); |
334 | buff[len] = '\0'; | 334 | buff[len] = '\0'; |
335 | printf("%s", buff); | 335 | printf("%s", buff); |
336 | break; | 336 | break; |
337 | } | 337 | } |
338 | case LUA_TSTRING: { | 338 | case LUA_VSHRSTR: |
339 | printf("'%s'", getstr(tsvalue(v))); | 339 | printf("'%s'", getstr(tsvalue(v))); break; |
340 | break; | 340 | case LUA_VLNGSTR: |
341 | } | 341 | printf("'%.30s...'", getstr(tsvalue(v))); break; |
342 | case LUA_TBOOLEAN: { | 342 | case LUA_VFALSE: |
343 | printf("%s", (!l_isfalse(v) ? "true" : "false")); | 343 | printf("%s", "false"); break; |
344 | break; | 344 | case LUA_VTRUE: |
345 | } | 345 | printf("%s", "true"); break; |
346 | case LUA_TLIGHTUSERDATA: { | 346 | case LUA_VLIGHTUSERDATA: |
347 | printf("light udata: %p", pvalue(v)); | 347 | printf("light udata: %p", pvalue(v)); break; |
348 | break; | 348 | case LUA_VUSERDATA: |
349 | } | 349 | printf("full udata: %p", uvalue(v)); break; |
350 | case LUA_TNIL: { | 350 | case LUA_VNIL: |
351 | printf("nil"); | 351 | printf("nil"); break; |
352 | break; | 352 | case LUA_VLCF: |
353 | } | 353 | printf("light C function: %p", fvalue(v)); break; |
354 | default: { | 354 | case LUA_VCCL: |
355 | if (ttislcf(v)) | 355 | printf("C closure: %p", clCvalue(v)); break; |
356 | printf("light C function: %p", fvalue(v)); | 356 | case LUA_VLCL: |
357 | else /* must be collectable */ | 357 | printf("Lua function: %p", clLvalue(v)); break; |
358 | printf("%s: %p", ttypename(ttype(v)), gcvalue(v)); | 358 | case LUA_VTHREAD: |
359 | break; | 359 | printf("thread: %p", thvalue(v)); break; |
360 | } | 360 | case LUA_VTABLE: |
361 | printf("table: %p", hvalue(v)); break; | ||
362 | default: | ||
363 | lua_assert(0); | ||
361 | } | 364 | } |
362 | } | 365 | } |
363 | 366 | ||
@@ -872,6 +875,28 @@ void lua_printstack (lua_State *L) { | |||
872 | } | 875 | } |
873 | 876 | ||
874 | 877 | ||
878 | int lua_printallstack (lua_State *L) { | ||
879 | StkId p; | ||
880 | int i = 1; | ||
881 | CallInfo *ci = &L->base_ci; | ||
882 | printf("stack: >>\n"); | ||
883 | for (p = L->stack.p; p < L->top.p; p++) { | ||
884 | if (ci != NULL && p == ci->func.p) { | ||
885 | printf(" ---\n"); | ||
886 | if (ci == L->ci) | ||
887 | ci = NULL; /* printed last frame */ | ||
888 | else | ||
889 | ci = ci->next; | ||
890 | } | ||
891 | printf("%3d: ", i++); | ||
892 | lua_printvalue(s2v(p)); | ||
893 | printf("\n"); | ||
894 | } | ||
895 | printf("<<\n"); | ||
896 | return 0; | ||
897 | } | ||
898 | |||
899 | |||
875 | static int get_limits (lua_State *L) { | 900 | static int get_limits (lua_State *L) { |
876 | lua_createtable(L, 0, 5); | 901 | lua_createtable(L, 0, 5); |
877 | setnameval(L, "IS32INT", LUAI_IS32INT); | 902 | setnameval(L, "IS32INT", LUAI_IS32INT); |
@@ -885,9 +910,9 @@ static int get_limits (lua_State *L) { | |||
885 | 910 | ||
886 | static int mem_query (lua_State *L) { | 911 | static int mem_query (lua_State *L) { |
887 | if (lua_isnone(L, 1)) { | 912 | if (lua_isnone(L, 1)) { |
888 | lua_pushinteger(L, cast(lua_Integer, l_memcontrol.total)); | 913 | lua_pushinteger(L, cast_Integer(l_memcontrol.total)); |
889 | lua_pushinteger(L, cast(lua_Integer, l_memcontrol.numblocks)); | 914 | lua_pushinteger(L, cast_Integer(l_memcontrol.numblocks)); |
890 | lua_pushinteger(L, cast(lua_Integer, l_memcontrol.maxmem)); | 915 | lua_pushinteger(L, cast_Integer(l_memcontrol.maxmem)); |
891 | return 3; | 916 | return 3; |
892 | } | 917 | } |
893 | else if (lua_isnumber(L, 1)) { | 918 | else if (lua_isnumber(L, 1)) { |
@@ -901,7 +926,7 @@ static int mem_query (lua_State *L) { | |||
901 | int i; | 926 | int i; |
902 | for (i = LUA_NUMTYPES - 1; i >= 0; i--) { | 927 | for (i = LUA_NUMTYPES - 1; i >= 0; i--) { |
903 | if (strcmp(t, ttypename(i)) == 0) { | 928 | if (strcmp(t, ttypename(i)) == 0) { |
904 | lua_pushinteger(L, cast(lua_Integer, l_memcontrol.objcount[i])); | 929 | lua_pushinteger(L, cast_Integer(l_memcontrol.objcount[i])); |
905 | return 1; | 930 | return 1; |
906 | } | 931 | } |
907 | } | 932 | } |
@@ -982,7 +1007,7 @@ static int gc_printobj (lua_State *L) { | |||
982 | } | 1007 | } |
983 | 1008 | ||
984 | 1009 | ||
985 | static const char *statenames[] = { | 1010 | static const char *const statenames[] = { |
986 | "propagate", "enteratomic", "atomic", "sweepallgc", "sweepfinobj", | 1011 | "propagate", "enteratomic", "atomic", "sweepallgc", "sweepfinobj", |
987 | "sweeptobefnz", "sweepend", "callfin", "pause", ""}; | 1012 | "sweeptobefnz", "sweepend", "callfin", "pause", ""}; |
988 | 1013 | ||
@@ -1041,15 +1066,19 @@ static int tracegc (lua_State *L) { | |||
1041 | 1066 | ||
1042 | static int hash_query (lua_State *L) { | 1067 | static int hash_query (lua_State *L) { |
1043 | if (lua_isnone(L, 2)) { | 1068 | if (lua_isnone(L, 2)) { |
1069 | TString *ts; | ||
1044 | luaL_argcheck(L, lua_type(L, 1) == LUA_TSTRING, 1, "string expected"); | 1070 | luaL_argcheck(L, lua_type(L, 1) == LUA_TSTRING, 1, "string expected"); |
1045 | lua_pushinteger(L, cast_int(tsvalue(obj_at(L, 1))->hash)); | 1071 | ts = tsvalue(obj_at(L, 1)); |
1072 | if (ts->tt == LUA_VLNGSTR) | ||
1073 | luaS_hashlongstr(ts); /* make sure long string has a hash */ | ||
1074 | lua_pushinteger(L, cast_int(ts->hash)); | ||
1046 | } | 1075 | } |
1047 | else { | 1076 | else { |
1048 | TValue *o = obj_at(L, 1); | 1077 | TValue *o = obj_at(L, 1); |
1049 | Table *t; | 1078 | Table *t; |
1050 | luaL_checktype(L, 2, LUA_TTABLE); | 1079 | luaL_checktype(L, 2, LUA_TTABLE); |
1051 | t = hvalue(obj_at(L, 2)); | 1080 | t = hvalue(obj_at(L, 2)); |
1052 | lua_pushinteger(L, cast(lua_Integer, luaH_mainposition(t, o) - t->node)); | 1081 | lua_pushinteger(L, cast_Integer(luaH_mainposition(t, o) - t->node)); |
1053 | } | 1082 | } |
1054 | return 1; | 1083 | return 1; |
1055 | } | 1084 | } |
@@ -1057,9 +1086,9 @@ static int hash_query (lua_State *L) { | |||
1057 | 1086 | ||
1058 | static int stacklevel (lua_State *L) { | 1087 | static int stacklevel (lua_State *L) { |
1059 | int a = 0; | 1088 | int a = 0; |
1060 | lua_pushinteger(L, cast(lua_Integer, L->top.p - L->stack.p)); | 1089 | lua_pushinteger(L, cast_Integer(L->top.p - L->stack.p)); |
1061 | lua_pushinteger(L, stacksize(L)); | 1090 | lua_pushinteger(L, stacksize(L)); |
1062 | lua_pushinteger(L, cast(lua_Integer, L->nCcalls)); | 1091 | lua_pushinteger(L, cast_Integer(L->nCcalls)); |
1063 | lua_pushinteger(L, L->nci); | 1092 | lua_pushinteger(L, L->nci); |
1064 | lua_pushinteger(L, (lua_Integer)(size_t)&a); | 1093 | lua_pushinteger(L, (lua_Integer)(size_t)&a); |
1065 | return 5; | 1094 | return 5; |
@@ -1074,9 +1103,9 @@ static int table_query (lua_State *L) { | |||
1074 | t = hvalue(obj_at(L, 1)); | 1103 | t = hvalue(obj_at(L, 1)); |
1075 | asize = t->asize; | 1104 | asize = t->asize; |
1076 | if (i == -1) { | 1105 | if (i == -1) { |
1077 | lua_pushinteger(L, cast(lua_Integer, asize)); | 1106 | lua_pushinteger(L, cast_Integer(asize)); |
1078 | lua_pushinteger(L, cast(lua_Integer, allocsizenode(t))); | 1107 | lua_pushinteger(L, cast_Integer(allocsizenode(t))); |
1079 | lua_pushinteger(L, cast(lua_Integer, asize > 0 ? *lenhint(t) : 0)); | 1108 | lua_pushinteger(L, cast_Integer(asize > 0 ? *lenhint(t) : 0)); |
1080 | return 3; | 1109 | return 3; |
1081 | } | 1110 | } |
1082 | else if (cast_uint(i) < asize) { | 1111 | else if (cast_uint(i) < asize) { |
@@ -1132,7 +1161,7 @@ static int test_codeparam (lua_State *L) { | |||
1132 | static int test_applyparam (lua_State *L) { | 1161 | static int test_applyparam (lua_State *L) { |
1133 | lua_Integer p = luaL_checkinteger(L, 1); | 1162 | lua_Integer p = luaL_checkinteger(L, 1); |
1134 | lua_Integer x = luaL_checkinteger(L, 2); | 1163 | lua_Integer x = luaL_checkinteger(L, 2); |
1135 | lua_pushinteger(L, cast(lua_Integer, luaO_applyparam(cast_byte(p), x))); | 1164 | lua_pushinteger(L, cast_Integer(luaO_applyparam(cast_byte(p), x))); |
1136 | return 1; | 1165 | return 1; |
1137 | } | 1166 | } |
1138 | 1167 | ||
@@ -1232,7 +1261,7 @@ static int pushuserdata (lua_State *L) { | |||
1232 | 1261 | ||
1233 | 1262 | ||
1234 | static int udataval (lua_State *L) { | 1263 | static int udataval (lua_State *L) { |
1235 | lua_pushinteger(L, cast(lua_Integer, cast(size_t, lua_touserdata(L, 1)))); | 1264 | lua_pushinteger(L, cast_st2S(cast_sizet(lua_touserdata(L, 1)))); |
1236 | return 1; | 1265 | return 1; |
1237 | } | 1266 | } |
1238 | 1267 | ||
@@ -1269,7 +1298,7 @@ static int num2int (lua_State *L) { | |||
1269 | 1298 | ||
1270 | 1299 | ||
1271 | static int makeseed (lua_State *L) { | 1300 | static int makeseed (lua_State *L) { |
1272 | lua_pushinteger(L, cast(lua_Integer, luaL_makeseed(L))); | 1301 | lua_pushinteger(L, cast_Integer(luaL_makeseed(L))); |
1273 | return 1; | 1302 | return 1; |
1274 | } | 1303 | } |
1275 | 1304 | ||
@@ -1613,7 +1642,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { | |||
1613 | } | 1642 | } |
1614 | else if EQ("func2num") { | 1643 | else if EQ("func2num") { |
1615 | lua_CFunction func = lua_tocfunction(L1, getindex); | 1644 | lua_CFunction func = lua_tocfunction(L1, getindex); |
1616 | lua_pushinteger(L1, cast(lua_Integer, cast(size_t, func))); | 1645 | lua_pushinteger(L1, cast_st2S(cast_sizet(func))); |
1617 | } | 1646 | } |
1618 | else if EQ("getfield") { | 1647 | else if EQ("getfield") { |
1619 | int t = getindex; | 1648 | int t = getindex; |
@@ -1986,7 +2015,7 @@ static int Cfunc (lua_State *L) { | |||
1986 | static int Cfunck (lua_State *L, int status, lua_KContext ctx) { | 2015 | static int Cfunck (lua_State *L, int status, lua_KContext ctx) { |
1987 | lua_pushstring(L, statcodes[status]); | 2016 | lua_pushstring(L, statcodes[status]); |
1988 | lua_setglobal(L, "status"); | 2017 | lua_setglobal(L, "status"); |
1989 | lua_pushinteger(L, cast(lua_Integer, ctx)); | 2018 | lua_pushinteger(L, cast_Integer(ctx)); |
1990 | lua_setglobal(L, "ctx"); | 2019 | lua_setglobal(L, "ctx"); |
1991 | return runC(L, L, lua_tostring(L, cast_int(ctx))); | 2020 | return runC(L, L, lua_tostring(L, cast_int(ctx))); |
1992 | } | 2021 | } |
@@ -2081,6 +2110,25 @@ static int coresume (lua_State *L) { | |||
2081 | } | 2110 | } |
2082 | } | 2111 | } |
2083 | 2112 | ||
2113 | #if !defined(LUA_USE_POSIX) | ||
2114 | |||
2115 | #define nonblock NULL | ||
2116 | |||
2117 | #else | ||
2118 | |||
2119 | #include <unistd.h> | ||
2120 | #include <fcntl.h> | ||
2121 | |||
2122 | static int nonblock (lua_State *L) { | ||
2123 | FILE *f = cast(luaL_Stream*, luaL_checkudata(L, 1, LUA_FILEHANDLE))->f; | ||
2124 | int fd = fileno(f); | ||
2125 | int flags = fcntl(fd, F_GETFL, 0); | ||
2126 | flags |= O_NONBLOCK; | ||
2127 | fcntl(fd, F_SETFL, flags); | ||
2128 | return 0; | ||
2129 | } | ||
2130 | #endif | ||
2131 | |||
2084 | /* }====================================================== */ | 2132 | /* }====================================================== */ |
2085 | 2133 | ||
2086 | 2134 | ||
@@ -2102,6 +2150,7 @@ static const struct luaL_Reg tests_funcs[] = { | |||
2102 | {"limits", get_limits}, | 2150 | {"limits", get_limits}, |
2103 | {"listcode", listcode}, | 2151 | {"listcode", listcode}, |
2104 | {"printcode", printcode}, | 2152 | {"printcode", printcode}, |
2153 | {"printallstack", lua_printallstack}, | ||
2105 | {"listk", listk}, | 2154 | {"listk", listk}, |
2106 | {"listabslineinfo", listabslineinfo}, | 2155 | {"listabslineinfo", listabslineinfo}, |
2107 | {"listlocals", listlocals}, | 2156 | {"listlocals", listlocals}, |
@@ -2133,6 +2182,7 @@ static const struct luaL_Reg tests_funcs[] = { | |||
2133 | {"upvalue", upvalue}, | 2182 | {"upvalue", upvalue}, |
2134 | {"externKstr", externKstr}, | 2183 | {"externKstr", externKstr}, |
2135 | {"externstr", externstr}, | 2184 | {"externstr", externstr}, |
2185 | {"nonblock", nonblock}, | ||
2136 | {NULL, NULL} | 2186 | {NULL, NULL} |
2137 | }; | 2187 | }; |
2138 | 2188 | ||