aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto I <roberto@inf.puc-rio.br>2025-08-20 13:59:08 -0300
committerRoberto I <roberto@inf.puc-rio.br>2025-08-20 13:59:08 -0300
commitdd095677e38a104d0fd073f31530e08c9f5286fc (patch)
treee46888706748091a63d2f2331235746caad97b43
parent53dc5a3bbadac166a8b40904790f91b351e55dd9 (diff)
downloadlua-dd095677e38a104d0fd073f31530e08c9f5286fc.tar.gz
lua-dd095677e38a104d0fd073f31530e08c9f5286fc.tar.bz2
lua-dd095677e38a104d0fd073f31530e08c9f5286fc.zip
Small cleaning services
-rw-r--r--lfunc.c3
-rw-r--r--lmathlib.c19
-rw-r--r--lvm.c54
3 files changed, 46 insertions, 30 deletions
diff --git a/lfunc.c b/lfunc.c
index da7c6239..b6fd9ceb 100644
--- a/lfunc.c
+++ b/lfunc.c
@@ -196,8 +196,7 @@ void luaF_unlinkupval (UpVal *uv) {
196*/ 196*/
197void luaF_closeupval (lua_State *L, StkId level) { 197void luaF_closeupval (lua_State *L, StkId level) {
198 UpVal *uv; 198 UpVal *uv;
199 StkId upl; /* stack index pointed by 'uv' */ 199 while ((uv = L->openupval) != NULL && uplevel(uv) >= level) {
200 while ((uv = L->openupval) != NULL && (upl = uplevel(uv)) >= level) {
201 TValue *slot = &uv->u.value; /* new position for value */ 200 TValue *slot = &uv->u.value; /* new position for value */
202 lua_assert(uplevel(uv) < L->top.p); 201 lua_assert(uplevel(uv) < L->top.p);
203 luaF_unlinkupval(uv); /* remove upvalue from 'openupval' list */ 202 luaF_unlinkupval(uv); /* remove upvalue from 'openupval' list */
diff --git a/lmathlib.c b/lmathlib.c
index b030f97e..2f0f3d1b 100644
--- a/lmathlib.c
+++ b/lmathlib.c
@@ -38,31 +38,37 @@ static int math_abs (lua_State *L) {
38 return 1; 38 return 1;
39} 39}
40 40
41
41static int math_sin (lua_State *L) { 42static int math_sin (lua_State *L) {
42 lua_pushnumber(L, l_mathop(sin)(luaL_checknumber(L, 1))); 43 lua_pushnumber(L, l_mathop(sin)(luaL_checknumber(L, 1)));
43 return 1; 44 return 1;
44} 45}
45 46
47
46static int math_cos (lua_State *L) { 48static int math_cos (lua_State *L) {
47 lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1))); 49 lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1)));
48 return 1; 50 return 1;
49} 51}
50 52
53
51static int math_tan (lua_State *L) { 54static int math_tan (lua_State *L) {
52 lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1))); 55 lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1)));
53 return 1; 56 return 1;
54} 57}
55 58
59
56static int math_asin (lua_State *L) { 60static int math_asin (lua_State *L) {
57 lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1))); 61 lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1)));
58 return 1; 62 return 1;
59} 63}
60 64
65
61static int math_acos (lua_State *L) { 66static int math_acos (lua_State *L) {
62 lua_pushnumber(L, l_mathop(acos)(luaL_checknumber(L, 1))); 67 lua_pushnumber(L, l_mathop(acos)(luaL_checknumber(L, 1)));
63 return 1; 68 return 1;
64} 69}
65 70
71
66static int math_atan (lua_State *L) { 72static int math_atan (lua_State *L) {
67 lua_Number y = luaL_checknumber(L, 1); 73 lua_Number y = luaL_checknumber(L, 1);
68 lua_Number x = luaL_optnumber(L, 2, 1); 74 lua_Number x = luaL_optnumber(L, 2, 1);
@@ -167,6 +173,7 @@ static int math_ult (lua_State *L) {
167 return 1; 173 return 1;
168} 174}
169 175
176
170static int math_log (lua_State *L) { 177static int math_log (lua_State *L) {
171 lua_Number x = luaL_checknumber(L, 1); 178 lua_Number x = luaL_checknumber(L, 1);
172 lua_Number res; 179 lua_Number res;
@@ -188,28 +195,34 @@ static int math_log (lua_State *L) {
188 return 1; 195 return 1;
189} 196}
190 197
198
191static int math_exp (lua_State *L) { 199static int math_exp (lua_State *L) {
192 lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1))); 200 lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1)));
193 return 1; 201 return 1;
194} 202}
195 203
204
196static int math_deg (lua_State *L) { 205static int math_deg (lua_State *L) {
197 lua_pushnumber(L, luaL_checknumber(L, 1) * (l_mathop(180.0) / PI)); 206 lua_pushnumber(L, luaL_checknumber(L, 1) * (l_mathop(180.0) / PI));
198 return 1; 207 return 1;
199} 208}
200 209
210
201static int math_rad (lua_State *L) { 211static int math_rad (lua_State *L) {
202 lua_pushnumber(L, luaL_checknumber(L, 1) * (PI / l_mathop(180.0))); 212 lua_pushnumber(L, luaL_checknumber(L, 1) * (PI / l_mathop(180.0)));
203 return 1; 213 return 1;
204} 214}
205 215
216
206static int math_frexp (lua_State *L) { 217static int math_frexp (lua_State *L) {
207 int e; 218 lua_Number x = luaL_checknumber(L, 1);
208 lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e)); 219 int ep;
209 lua_pushinteger(L, e); 220 lua_pushnumber(L, l_mathop(frexp)(x, &ep));
221 lua_pushinteger(L, ep);
210 return 2; 222 return 2;
211} 223}
212 224
225
213static int math_ldexp (lua_State *L) { 226static int math_ldexp (lua_State *L) {
214 lua_Number x = luaL_checknumber(L, 1); 227 lua_Number x = luaL_checknumber(L, 1);
215 int ep = (int)luaL_checkinteger(L, 2); 228 int ep = (int)luaL_checkinteger(L, 2);
diff --git a/lvm.c b/lvm.c
index cfdcf97a..bde850ea 100644
--- a/lvm.c
+++ b/lvm.c
@@ -910,6 +910,10 @@ void luaV_finishOp (lua_State *L) {
910/* 910/*
911** {================================================================== 911** {==================================================================
912** Macros for arithmetic/bitwise/comparison opcodes in 'luaV_execute' 912** Macros for arithmetic/bitwise/comparison opcodes in 'luaV_execute'
913**
914** All these macros are to be used exclusively inside the main
915** iterpreter loop (function luaV_execute) and may access directly
916** the local variables of that function (L, i, pc, ci, etc.).
913** =================================================================== 917** ===================================================================
914*/ 918*/
915 919
@@ -931,17 +935,17 @@ void luaV_finishOp (lua_State *L) {
931** operation, 'fop' is the float operation. 935** operation, 'fop' is the float operation.
932*/ 936*/
933#define op_arithI(L,iop,fop) { \ 937#define op_arithI(L,iop,fop) { \
934 StkId ra = RA(i); \ 938 TValue *ra = vRA(i); \
935 TValue *v1 = vRB(i); \ 939 TValue *v1 = vRB(i); \
936 int imm = GETARG_sC(i); \ 940 int imm = GETARG_sC(i); \
937 if (ttisinteger(v1)) { \ 941 if (ttisinteger(v1)) { \
938 lua_Integer iv1 = ivalue(v1); \ 942 lua_Integer iv1 = ivalue(v1); \
939 pc++; setivalue(s2v(ra), iop(L, iv1, imm)); \ 943 pc++; setivalue(ra, iop(L, iv1, imm)); \
940 } \ 944 } \
941 else if (ttisfloat(v1)) { \ 945 else if (ttisfloat(v1)) { \
942 lua_Number nb = fltvalue(v1); \ 946 lua_Number nb = fltvalue(v1); \
943 lua_Number fimm = cast_num(imm); \ 947 lua_Number fimm = cast_num(imm); \
944 pc++; setfltvalue(s2v(ra), fop(L, nb, fimm)); \ 948 pc++; setfltvalue(ra, fop(L, nb, fimm)); \
945 }} 949 }}
946 950
947 951
@@ -952,6 +956,7 @@ void luaV_finishOp (lua_State *L) {
952#define op_arithf_aux(L,v1,v2,fop) { \ 956#define op_arithf_aux(L,v1,v2,fop) { \
953 lua_Number n1; lua_Number n2; \ 957 lua_Number n1; lua_Number n2; \
954 if (tonumberns(v1, n1) && tonumberns(v2, n2)) { \ 958 if (tonumberns(v1, n1) && tonumberns(v2, n2)) { \
959 StkId ra = RA(i); \
955 pc++; setfltvalue(s2v(ra), fop(L, n1, n2)); \ 960 pc++; setfltvalue(s2v(ra), fop(L, n1, n2)); \
956 }} 961 }}
957 962
@@ -960,7 +965,6 @@ void luaV_finishOp (lua_State *L) {
960** Arithmetic operations over floats and others with register operands. 965** Arithmetic operations over floats and others with register operands.
961*/ 966*/
962#define op_arithf(L,fop) { \ 967#define op_arithf(L,fop) { \
963 StkId ra = RA(i); \
964 TValue *v1 = vRB(i); \ 968 TValue *v1 = vRB(i); \
965 TValue *v2 = vRC(i); \ 969 TValue *v2 = vRC(i); \
966 op_arithf_aux(L, v1, v2, fop); } 970 op_arithf_aux(L, v1, v2, fop); }
@@ -970,7 +974,6 @@ void luaV_finishOp (lua_State *L) {
970** Arithmetic operations with K operands for floats. 974** Arithmetic operations with K operands for floats.
971*/ 975*/
972#define op_arithfK(L,fop) { \ 976#define op_arithfK(L,fop) { \
973 StkId ra = RA(i); \
974 TValue *v1 = vRB(i); \ 977 TValue *v1 = vRB(i); \
975 TValue *v2 = KC(i); lua_assert(ttisnumber(v2)); \ 978 TValue *v2 = KC(i); lua_assert(ttisnumber(v2)); \
976 op_arithf_aux(L, v1, v2, fop); } 979 op_arithf_aux(L, v1, v2, fop); }
@@ -980,8 +983,8 @@ void luaV_finishOp (lua_State *L) {
980** Arithmetic operations over integers and floats. 983** Arithmetic operations over integers and floats.
981*/ 984*/
982#define op_arith_aux(L,v1,v2,iop,fop) { \ 985#define op_arith_aux(L,v1,v2,iop,fop) { \
983 StkId ra = RA(i); \
984 if (ttisinteger(v1) && ttisinteger(v2)) { \ 986 if (ttisinteger(v1) && ttisinteger(v2)) { \
987 StkId ra = RA(i); \
985 lua_Integer i1 = ivalue(v1); lua_Integer i2 = ivalue(v2); \ 988 lua_Integer i1 = ivalue(v1); lua_Integer i2 = ivalue(v2); \
986 pc++; setivalue(s2v(ra), iop(L, i1, i2)); \ 989 pc++; setivalue(s2v(ra), iop(L, i1, i2)); \
987 } \ 990 } \
@@ -1010,12 +1013,12 @@ void luaV_finishOp (lua_State *L) {
1010** Bitwise operations with constant operand. 1013** Bitwise operations with constant operand.
1011*/ 1014*/
1012#define op_bitwiseK(L,op) { \ 1015#define op_bitwiseK(L,op) { \
1013 StkId ra = RA(i); \
1014 TValue *v1 = vRB(i); \ 1016 TValue *v1 = vRB(i); \
1015 TValue *v2 = KC(i); \ 1017 TValue *v2 = KC(i); \
1016 lua_Integer i1; \ 1018 lua_Integer i1; \
1017 lua_Integer i2 = ivalue(v2); \ 1019 lua_Integer i2 = ivalue(v2); \
1018 if (tointegerns(v1, &i1)) { \ 1020 if (tointegerns(v1, &i1)) { \
1021 StkId ra = RA(i); \
1019 pc++; setivalue(s2v(ra), op(i1, i2)); \ 1022 pc++; setivalue(s2v(ra), op(i1, i2)); \
1020 }} 1023 }}
1021 1024
@@ -1024,11 +1027,11 @@ void luaV_finishOp (lua_State *L) {
1024** Bitwise operations with register operands. 1027** Bitwise operations with register operands.
1025*/ 1028*/
1026#define op_bitwise(L,op) { \ 1029#define op_bitwise(L,op) { \
1027 StkId ra = RA(i); \
1028 TValue *v1 = vRB(i); \ 1030 TValue *v1 = vRB(i); \
1029 TValue *v2 = vRC(i); \ 1031 TValue *v2 = vRC(i); \
1030 lua_Integer i1; lua_Integer i2; \ 1032 lua_Integer i1; lua_Integer i2; \
1031 if (tointegerns(v1, &i1) && tointegerns(v2, &i2)) { \ 1033 if (tointegerns(v1, &i1) && tointegerns(v2, &i2)) { \
1034 StkId ra = RA(i); \
1032 pc++; setivalue(s2v(ra), op(i1, i2)); \ 1035 pc++; setivalue(s2v(ra), op(i1, i2)); \
1033 }} 1036 }}
1034 1037
@@ -1039,18 +1042,18 @@ void luaV_finishOp (lua_State *L) {
1039** integers. 1042** integers.
1040*/ 1043*/
1041#define op_order(L,opi,opn,other) { \ 1044#define op_order(L,opi,opn,other) { \
1042 StkId ra = RA(i); \ 1045 TValue *ra = vRA(i); \
1043 int cond; \ 1046 int cond; \
1044 TValue *rb = vRB(i); \ 1047 TValue *rb = vRB(i); \
1045 if (ttisinteger(s2v(ra)) && ttisinteger(rb)) { \ 1048 if (ttisinteger(ra) && ttisinteger(rb)) { \
1046 lua_Integer ia = ivalue(s2v(ra)); \ 1049 lua_Integer ia = ivalue(ra); \
1047 lua_Integer ib = ivalue(rb); \ 1050 lua_Integer ib = ivalue(rb); \
1048 cond = opi(ia, ib); \ 1051 cond = opi(ia, ib); \
1049 } \ 1052 } \
1050 else if (ttisnumber(s2v(ra)) && ttisnumber(rb)) \ 1053 else if (ttisnumber(ra) && ttisnumber(rb)) \
1051 cond = opn(s2v(ra), rb); \ 1054 cond = opn(ra, rb); \
1052 else \ 1055 else \
1053 Protect(cond = other(L, s2v(ra), rb)); \ 1056 Protect(cond = other(L, ra, rb)); \
1054 docondjump(); } 1057 docondjump(); }
1055 1058
1056 1059
@@ -1059,19 +1062,19 @@ void luaV_finishOp (lua_State *L) {
1059** always small enough to have an exact representation as a float.) 1062** always small enough to have an exact representation as a float.)
1060*/ 1063*/
1061#define op_orderI(L,opi,opf,inv,tm) { \ 1064#define op_orderI(L,opi,opf,inv,tm) { \
1062 StkId ra = RA(i); \ 1065 TValue *ra = vRA(i); \
1063 int cond; \ 1066 int cond; \
1064 int im = GETARG_sB(i); \ 1067 int im = GETARG_sB(i); \
1065 if (ttisinteger(s2v(ra))) \ 1068 if (ttisinteger(ra)) \
1066 cond = opi(ivalue(s2v(ra)), im); \ 1069 cond = opi(ivalue(ra), im); \
1067 else if (ttisfloat(s2v(ra))) { \ 1070 else if (ttisfloat(ra)) { \
1068 lua_Number fa = fltvalue(s2v(ra)); \ 1071 lua_Number fa = fltvalue(ra); \
1069 lua_Number fim = cast_num(im); \ 1072 lua_Number fim = cast_num(im); \
1070 cond = opf(fa, fim); \ 1073 cond = opf(fa, fim); \
1071 } \ 1074 } \
1072 else { \ 1075 else { \
1073 int isf = GETARG_C(i); \ 1076 int isf = GETARG_C(i); \
1074 Protect(cond = luaT_callorderiTM(L, s2v(ra), im, inv, isf, tm)); \ 1077 Protect(cond = luaT_callorderiTM(L, ra, im, inv, isf, tm)); \
1075 } \ 1078 } \
1076 docondjump(); } 1079 docondjump(); }
1077 1080
@@ -1090,6 +1093,7 @@ void luaV_finishOp (lua_State *L) {
1090 1093
1091 1094
1092#define RA(i) (base+GETARG_A(i)) 1095#define RA(i) (base+GETARG_A(i))
1096#define vRA(i) s2v(RA(i))
1093#define RB(i) (base+GETARG_B(i)) 1097#define RB(i) (base+GETARG_B(i))
1094#define vRB(i) s2v(RB(i)) 1098#define vRB(i) s2v(RB(i))
1095#define KB(i) (k+GETARG_B(i)) 1099#define KB(i) (k+GETARG_B(i))
@@ -1130,14 +1134,14 @@ void luaV_finishOp (lua_State *L) {
1130/* 1134/*
1131** Correct global 'pc'. 1135** Correct global 'pc'.
1132*/ 1136*/
1133#define savepc(L) (ci->u.l.savedpc = pc) 1137#define savepc(ci) (ci->u.l.savedpc = pc)
1134 1138
1135 1139
1136/* 1140/*
1137** Whenever code can raise errors, the global 'pc' and the global 1141** Whenever code can raise errors, the global 'pc' and the global
1138** 'top' must be correct to report occasional errors. 1142** 'top' must be correct to report occasional errors.
1139*/ 1143*/
1140#define savestate(L,ci) (savepc(L), L->top.p = ci->top.p) 1144#define savestate(L,ci) (savepc(ci), L->top.p = ci->top.p)
1141 1145
1142 1146
1143/* 1147/*
@@ -1147,7 +1151,7 @@ void luaV_finishOp (lua_State *L) {
1147#define Protect(exp) (savestate(L,ci), (exp), updatetrap(ci)) 1151#define Protect(exp) (savestate(L,ci), (exp), updatetrap(ci))
1148 1152
1149/* special version that does not change the top */ 1153/* special version that does not change the top */
1150#define ProtectNT(exp) (savepc(L), (exp), updatetrap(ci)) 1154#define ProtectNT(exp) (savepc(ci), (exp), updatetrap(ci))
1151 1155
1152/* 1156/*
1153** Protect code that can only raise errors. (That is, it cannot change 1157** Protect code that can only raise errors. (That is, it cannot change
@@ -1165,7 +1169,7 @@ void luaV_finishOp (lua_State *L) {
1165 1169
1166/* 'c' is the limit of live values in the stack */ 1170/* 'c' is the limit of live values in the stack */
1167#define checkGC(L,c) \ 1171#define checkGC(L,c) \
1168 { luaC_condGC(L, (savepc(L), L->top.p = (c)), \ 1172 { luaC_condGC(L, (savepc(ci), L->top.p = (c)), \
1169 updatetrap(ci)); \ 1173 updatetrap(ci)); \
1170 luai_threadyield(L); } 1174 luai_threadyield(L); }
1171 1175
@@ -1714,7 +1718,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1714 if (b != 0) /* fixed number of arguments? */ 1718 if (b != 0) /* fixed number of arguments? */
1715 L->top.p = ra + b; /* top signals number of arguments */ 1719 L->top.p = ra + b; /* top signals number of arguments */
1716 /* else previous instruction set top */ 1720 /* else previous instruction set top */
1717 savepc(L); /* in case of errors */ 1721 savepc(ci); /* in case of errors */
1718 if ((newci = luaD_precall(L, ra, nresults)) == NULL) 1722 if ((newci = luaD_precall(L, ra, nresults)) == NULL)
1719 updatetrap(ci); /* C call; nothing else to be done */ 1723 updatetrap(ci); /* C call; nothing else to be done */
1720 else { /* Lua call: run function in this same C frame */ 1724 else { /* Lua call: run function in this same C frame */