aboutsummaryrefslogtreecommitdiff
path: root/ltests.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2009-12-10 16:21:28 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2009-12-10 16:21:28 -0200
commit1375435e4a9ad5a7625a332934251945586aceca (patch)
treee5e861da2bab443321a4f93beb228811e31801a0 /ltests.c
parent2bb19ccf08381c7755015dfa61e2eb4675637c78 (diff)
downloadlua-1375435e4a9ad5a7625a332934251945586aceca.tar.gz
lua-1375435e4a9ad5a7625a332934251945586aceca.tar.bz2
lua-1375435e4a9ad5a7625a332934251945586aceca.zip
several new features in testC to allow better testing of lua_resume
at the C API level
Diffstat (limited to 'ltests.c')
-rw-r--r--ltests.c89
1 files changed, 62 insertions, 27 deletions
diff --git a/ltests.c b/ltests.c
index 8410e08e..29908c1c 100644
--- a/ltests.c
+++ b/ltests.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltests.c,v 2.80 2009/11/27 15:39:31 roberto Exp roberto $ 2** $Id: ltests.c,v 2.81 2009/12/01 16:49:48 roberto Exp roberto $
3** Internal Module for Debugging of the Lua Implementation 3** Internal Module for Debugging of the Lua Implementation
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -825,13 +825,13 @@ static void skip (const char **pc) {
825 } 825 }
826} 826}
827 827
828static int getnum_aux (lua_State *L, const char **pc) { 828static int getnum_aux (lua_State *L, lua_State *L1, const char **pc) {
829 int res = 0; 829 int res = 0;
830 int sig = 1; 830 int sig = 1;
831 skip(pc); 831 skip(pc);
832 if (**pc == '.') { 832 if (**pc == '.') {
833 res = cast_int(lua_tonumber(L, -1)); 833 res = lua_tointeger(L1, -1);
834 lua_pop(L, 1); 834 lua_pop(L1, 1);
835 (*pc)++; 835 (*pc)++;
836 return res; 836 return res;
837 } 837 }
@@ -845,7 +845,7 @@ static int getnum_aux (lua_State *L, const char **pc) {
845 return sig*res; 845 return sig*res;
846} 846}
847 847
848static const char *getname_aux (lua_State *L, char *buff, const char **pc) { 848static const char *getstring_aux (lua_State *L, char *buff, const char **pc) {
849 int i = 0; 849 int i = 0;
850 skip(pc); 850 skip(pc);
851 if (**pc == '"' || **pc == '\'') { /* quoted string? */ 851 if (**pc == '"' || **pc == '\'') { /* quoted string? */
@@ -865,22 +865,30 @@ static const char *getname_aux (lua_State *L, char *buff, const char **pc) {
865} 865}
866 866
867 867
868static int getindex_aux (lua_State *L, const char **pc) { 868static int getindex_aux (lua_State *L, lua_State *L1, const char **pc) {
869 skip(pc); 869 skip(pc);
870 switch (*(*pc)++) { 870 switch (*(*pc)++) {
871 case 'R': return LUA_REGISTRYINDEX; 871 case 'R': return LUA_REGISTRYINDEX;
872 case 'G': return LUA_GLOBALSINDEX; 872 case 'G': return LUA_GLOBALSINDEX;
873 case 'E': return LUA_ENVIRONINDEX; 873 case 'E': return LUA_ENVIRONINDEX;
874 case 'U': return lua_upvalueindex(getnum_aux(L, pc)); 874 case 'U': return lua_upvalueindex(getnum_aux(L, L1, pc));
875 default: (*pc)--; return getnum_aux(L, pc); 875 default: (*pc)--; return getnum_aux(L, L1, pc);
876 } 876 }
877} 877}
878 878
879
880static void pushcode (lua_State *L, int code) {
881 static const char *const codes[] = {"OK", "YIELD", "ERRRUN",
882 "ERRSYNTAX", "ERRMEM", "ERRGCMM", "ERRERR"};
883 lua_pushstring(L, codes[code]);
884}
885
886
879#define EQ(s1) (strcmp(s1, inst) == 0) 887#define EQ(s1) (strcmp(s1, inst) == 0)
880 888
881#define getnum (getnum_aux(L, &pc)) 889#define getnum (getnum_aux(L, L1, &pc))
882#define getname (getname_aux(L, buff, &pc)) 890#define getstring (getstring_aux(L, buff, &pc))
883#define getindex (getindex_aux(L, &pc)) 891#define getindex (getindex_aux(L, L1, &pc))
884 892
885 893
886static int testC (lua_State *L); 894static int testC (lua_State *L);
@@ -888,9 +896,10 @@ static int Cfunck (lua_State *L);
888 896
889static int runC (lua_State *L, lua_State *L1, const char *pc) { 897static int runC (lua_State *L, lua_State *L1, const char *pc) {
890 char buff[300]; 898 char buff[300];
899 int status = 0;
891 if (pc == NULL) return luaL_error(L, "attempt to runC null script"); 900 if (pc == NULL) return luaL_error(L, "attempt to runC null script");
892 for (;;) { 901 for (;;) {
893 const char *inst = getname; 902 const char *inst = getstring;
894 if EQ("") return 0; 903 if EQ("") return 0;
895 else if EQ("isnumber") { 904 else if EQ("isnumber") {
896 lua_pushboolean(L1, lua_isnumber(L1, getindex)); 905 lua_pushboolean(L1, lua_isnumber(L1, getindex));
@@ -938,7 +947,13 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
938 lua_pushlightuserdata(L1, &func); 947 lua_pushlightuserdata(L1, &func);
939 } 948 }
940 else if EQ("return") { 949 else if EQ("return") {
941 return getnum; 950 int n = getnum;
951 if (L1 != L) {
952 int i;
953 for (i = 0; i < n; i++)
954 lua_pushstring(L, lua_tostring(L1, -(n - i)));
955 }
956 return n;
942 } 957 }
943 else if EQ("gettop") { 958 else if EQ("gettop") {
944 lua_pushinteger(L1, lua_gettop(L1)); 959 lua_pushinteger(L1, lua_gettop(L1));
@@ -953,7 +968,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
953 lua_pushinteger(L1, getnum); 968 lua_pushinteger(L1, getnum);
954 } 969 }
955 else if EQ("pushstring") { 970 else if EQ("pushstring") {
956 lua_pushstring(L1, getname); 971 lua_pushstring(L1, getstring);
957 } 972 }
958 else if EQ("pushnil") { 973 else if EQ("pushnil") {
959 lua_pushnil(L1); 974 lua_pushnil(L1);
@@ -997,7 +1012,11 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
997 } 1012 }
998 else if EQ("getfield") { 1013 else if EQ("getfield") {
999 int t = getindex; 1014 int t = getindex;
1000 lua_getfield(L1, t, getname); 1015 lua_getfield(L1, t, getstring);
1016 }
1017 else if EQ("setfield") {
1018 int t = getindex;
1019 lua_setfield(L1, t, getstring);
1001 } 1020 }
1002 else if EQ("rawgeti") { 1021 else if EQ("rawgeti") {
1003 int t = getindex; 1022 int t = getindex;
@@ -1033,7 +1052,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
1033 int op; 1052 int op;
1034 skip(&pc); 1053 skip(&pc);
1035 op = strchr(ops, *pc++) - ops; 1054 op = strchr(ops, *pc++) - ops;
1036 lua_arith(L, op); 1055 lua_arith(L1, op);
1037 } 1056 }
1038 else if EQ("compare") { 1057 else if EQ("compare") {
1039 int a = getindex; 1058 int a = getindex;
@@ -1048,13 +1067,13 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
1048 else if EQ("pcall") { 1067 else if EQ("pcall") {
1049 int narg = getnum; 1068 int narg = getnum;
1050 int nres = getnum; 1069 int nres = getnum;
1051 lua_pcall(L1, narg, nres, 0); 1070 status = lua_pcall(L1, narg, nres, 0);
1052 } 1071 }
1053 else if EQ("pcallk") { 1072 else if EQ("pcallk") {
1054 int narg = getnum; 1073 int narg = getnum;
1055 int nres = getnum; 1074 int nres = getnum;
1056 int i = getindex; 1075 int i = getindex;
1057 lua_pcallk(L1, narg, nres, 0, i, Cfunck); 1076 status = lua_pcallk(L1, narg, nres, 0, i, Cfunck);
1058 } 1077 }
1059 else if EQ("callk") { 1078 else if EQ("callk") {
1060 int narg = getnum; 1079 int narg = getnum;
@@ -1070,6 +1089,25 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
1070 int i = getindex; 1089 int i = getindex;
1071 return lua_yieldk(L1, nres, i, Cfunck); 1090 return lua_yieldk(L1, nres, i, Cfunck);
1072 } 1091 }
1092 else if EQ("newthread") {
1093 lua_newthread(L1);
1094 }
1095 else if EQ("resume") {
1096 int i = getindex;
1097 status = lua_resume(lua_tothread(L1, i), getnum);
1098 }
1099 else if EQ("pushstatus") {
1100 pushcode(L1, status);
1101 }
1102 else if EQ("xmove") {
1103 int f = getindex;
1104 int t = getindex;
1105 lua_State *fs = (f == 0) ? L1 : lua_tothread(L1, f);
1106 lua_State *ts = (t == 0) ? L1 : lua_tothread(L1, t);
1107 int n = getnum;
1108 if (n == 0) n = lua_gettop(fs);
1109 lua_xmove(fs, ts, n);
1110 }
1073 else if EQ("loadstring") { 1111 else if EQ("loadstring") {
1074 size_t sl; 1112 size_t sl;
1075 const char *s = luaL_checklstring(L1, getnum, &sl); 1113 const char *s = luaL_checklstring(L1, getnum, &sl);
@@ -1098,12 +1136,9 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
1098 lua_rawseti(L1, t, i + 1); 1136 lua_rawseti(L1, t, i + 1);
1099 } 1137 }
1100 else if EQ("getctx") { 1138 else if EQ("getctx") {
1101 static const char *const codes[] = {"OK", "YIELD", "ERRRUN",
1102 "ERRSYNTAX", "ERRMEM", "ERRGCMM", "ERRERR"};
1103
1104 int i = 0; 1139 int i = 0;
1105 int s = lua_getctx(L1, &i); 1140 int s = lua_getctx(L1, &i);
1106 lua_pushstring(L1, codes[s]); 1141 pushcode(L1, s);
1107 lua_pushinteger(L1, i); 1142 lua_pushinteger(L1, i);
1108 } 1143 }
1109 else if EQ("checkstack") { 1144 else if EQ("checkstack") {
@@ -1111,22 +1146,22 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
1111 luaL_error(L, "C stack overflow"); 1146 luaL_error(L, "C stack overflow");
1112 } 1147 }
1113 else if EQ("newmetatable") { 1148 else if EQ("newmetatable") {
1114 lua_pushboolean(L1, luaL_newmetatable(L1, getname)); 1149 lua_pushboolean(L1, luaL_newmetatable(L1, getstring));
1115 } 1150 }
1116 else if EQ("testudata") { 1151 else if EQ("testudata") {
1117 int i = getindex; 1152 int i = getindex;
1118 lua_pushboolean(L1, luaL_testudata(L1, i, getname) != NULL); 1153 lua_pushboolean(L1, luaL_testudata(L1, i, getstring) != NULL);
1119 } 1154 }
1120 else if EQ("gsub") { 1155 else if EQ("gsub") {
1121 int a = getnum; int b = getnum; int c = getnum; 1156 int a = getnum; int b = getnum; int c = getnum;
1122 luaL_gsub(L1, lua_tostring(L1, a), 1157 luaL_gsub(L1, lua_tostring(L1, a),
1123 lua_tostring(L, b), 1158 lua_tostring(L1, b),
1124 lua_tostring(L, c)); 1159 lua_tostring(L1, c));
1125 } 1160 }
1126 else if EQ("sethook") { 1161 else if EQ("sethook") {
1127 int mask = getnum; 1162 int mask = getnum;
1128 int count = getnum; 1163 int count = getnum;
1129 sethookaux(L1, mask, count, getname); 1164 sethookaux(L1, mask, count, getstring);
1130 } 1165 }
1131 else if EQ("throw") { 1166 else if EQ("throw") {
1132#if defined(__cplusplus) 1167#if defined(__cplusplus)