• R/O
  • SSH

quipu: 提交

quipu mercurial repository


Commit MetaInfo

修訂c4788dd4bc3fc29d1beda2cec07c089d0e935947 (tree)
時間2018-07-13 13:44:06
作者Agustina Arzille <avarzille@rise...>
CommiterAgustina Arzille

Log Message

Fix argument check in functions with kwargs

Change Summary

差異

diff -r ed020b7852ee -r c4788dd4bc3f compiler.cpp
--- a/compiler.cpp Thu Jul 12 18:41:45 2018 -0300
+++ b/compiler.cpp Fri Jul 13 04:44:06 2018 +0000
@@ -73,9 +73,10 @@
7373 public:
7474 enum
7575 {
76- flg_outer_ref = 0x01,
77- flg_captured = 0x02,
78- flg_toplevel = 0x04
76+ flg_outer_ref = 0x01,
77+ flg_captured = 0x02,
78+ flg_toplevel = 0x04,
79+ flg_kwargs = 0x08
7980 };
8081
8182 class frame_data
@@ -1777,6 +1778,7 @@
17771778 (void)this->index (make_kwtab (interp, pa.kw, pa.opt.n));
17781779 this->emit (OPX_(KWARGS), intobj (nreq), intobj (pa.kw.n),
17791780 intobj (atail == NIL ? nargs : -nargs));
1781+ this->rflags |= flg_kwargs;
17801782 }
17811783
17821784 cons *first_opt = pa.opt.ptr ? pa.opt.ptr : pa.kw.ptr;
@@ -1860,7 +1862,9 @@
18601862 else if (r != EVR_IRET)
18611863 this->emit (OPX_(RET));
18621864
1863- function *retp = as_fct (alloc_fct (this->interp, flags));
1865+ function *retp = as_fct (alloc_fct (this->interp, flags |
1866+ ((this->rflags & flg_kwargs) ? function::kwargs_flag : 0)));
1867+
18641868 this->interp->push (this->interp->alval);
18651869 retp->bcode = this->encode ();
18661870 retp->vals = this->idxvec ();
diff -r ed020b7852ee -r c4788dd4bc3f defs.h
--- a/defs.h Thu Jul 12 18:41:45 2018 -0300
+++ b/defs.h Fri Jul 13 04:44:06 2018 +0000
@@ -236,6 +236,7 @@
236236 public:
237237 local_varobj<T> ()
238238 {
239+ this->full = 0;
239240 this->init_vo ();
240241 }
241242 };
diff -r ed020b7852ee -r c4788dd4bc3f eval.cpp
--- a/eval.cpp Thu Jul 12 18:41:45 2018 -0300
+++ b/eval.cpp Fri Jul 13 04:44:06 2018 +0000
@@ -5,10 +5,14 @@
55
66 static uint32_t
77 process_keys (interpreter *interp, object kwtab, uint32_t nreq,
8- uint32_t nkw, uint32_t nopt, uint32_t bp, uint32_t nargs)
8+ uint32_t nkw, uint32_t nopt, uint32_t bp, uint32_t nargs, bool va)
99 {
1010 uint32_t extra = nopt + nkw;
1111 uint32_t total = nreq + extra;
12+
13+ if (nargs < nreq)
14+ interp->raise_nargs (nreq, total, nargs);
15+
1216 QP_TMARK (interp);
1317
1418 object *argv = (object *)QP_TALLOC (interp, extra * sizeof (*argv));
@@ -79,6 +83,16 @@
7983
8084 done:
8185 uint32_t nrest = nargs - ix;
86+ if (qp_unlikely (!va && nrest > 0))
87+ {
88+ local_varobj<string> ts;
89+ ts.type = typecode::STR;
90+ ts.data = (unsigned char *)fct_sname (interp->caller ());
91+ ts.nbytes = ustrlen (ts.data, &ts.len);
92+ interp->raise2 ("arg-error", io_sprintf (interp, "apply: excess "
93+ "arguments found after keyword arguments in call to %Q",
94+ ts.as_obj ()));
95+ }
8296
8397 nargs = total + nrest;
8498 move_objs (interp->stack + bp + total,
@@ -776,7 +790,7 @@
776790 }
777791
778792 nargs = process_keys (interp, *fvals, ix,
779- n, abs (sx) - ix - n, bp, nargs);
793+ n, abs (sx) - ix - n, bp, nargs, sx < 0);
780794 lastf = interp->cur_frame;
781795 NEXT_OP;
782796
@@ -846,6 +860,7 @@
846860 function *fp = as_fct (alloc_fct (interp));
847861 const auto infct = as_fct (r_stkend (1));
848862
863+ fp->full = infct->full;
849864 fp->max_sp = infct->max_sp;
850865 fp->min_argc = infct->min_argc;
851866 fp->max_argc = infct->max_argc;
diff -r ed020b7852ee -r c4788dd4bc3f function.h
--- a/function.h Thu Jul 12 18:41:45 2018 -0300
+++ b/function.h Fri Jul 13 04:44:06 2018 +0000
@@ -44,6 +44,7 @@
4444 {
4545 public:
4646 static const int artificial_flag = 1u << 17;
47+ static const int kwargs_flag = 1u << 18;
4748
4849 int max_sp;
4950 int min_argc;
@@ -57,7 +58,8 @@
5758
5859 void test_nargs (interpreter *__interp, uint32_t __n) const
5960 {
60- if (__n < (uint32_t)this->min_argc || __n > (uint32_t)this->max_argc)
61+ if (!this->flagged_p (kwargs_flag) &&
62+ (__n < (uint32_t)this->min_argc || __n > (uint32_t)this->max_argc))
6163 __interp->raise_nargs (fct_sname (this->as_obj ()),
6264 this->min_argc, this->max_argc, __n);
6365 }
diff -r ed020b7852ee -r c4788dd4bc3f io.h
--- a/io.h Thu Jul 12 18:41:45 2018 -0300
+++ b/io.h Fri Jul 13 04:44:06 2018 +0000
@@ -33,9 +33,8 @@
3333 object io_sprintf (interpreter *__interp, const char *__fmt, Args... __args)
3434 {
3535 valref __tmp (__interp, symval (intern (__interp, "%fmt-str", 8)));
36- string __sf;
36+ local_varobj<string> __sf;
3737
38- __sf.full = 0;
3938 __sf.type = typecode::STR;
4039 __sf.data = (unsigned char *)__fmt;
4140 __sf.nbytes = ustrlen (__fmt, &__sf.len);
diff -r ed020b7852ee -r c4788dd4bc3f str.cpp
--- a/str.cpp Thu Jul 12 18:41:45 2018 -0300
+++ b/str.cpp Fri Jul 13 04:44:06 2018 +0000
@@ -34,14 +34,14 @@
3434 s + maxlen ? (int)(p - s) : maxlen);
3535 }
3636
37-int ustrlen (const char *s, int *lenp)
37+int ustrlen (const void *xs, int *lenp)
3838 {
3939 int i;
40- const char *p;
40+ const uint8_t *s = (const uint8_t *)xs, *p;
4141
4242 for (i = 0, p = s; *p != 0; ++i)
4343 {
44- int off = UTF8_SKIP[(uint8_t)*p];
44+ int off = UTF8_SKIP[*p];
4545 if (off == 0)
4646 return (-1);
4747
@@ -52,14 +52,14 @@
5252 return ((int)(p - s));
5353 }
5454
55-int ustrnlen (const char *s, int maxlen)
55+int ustrnlen (const void *xs, int maxlen)
5656 {
5757 int i;
58- const char *end = s + maxlen;
58+ const uint8_t *s = (const uint8_t *)xs, *end = s + maxlen;
5959
6060 for (i = 0; s < end; ++i)
6161 {
62- int off = UTF8_SKIP[(uint8_t)*s];
62+ int off = UTF8_SKIP[*s];
6363 if (off == 0)
6464 return (-1);
6565
diff -r ed020b7852ee -r c4788dd4bc3f str.h
--- a/str.h Thu Jul 12 18:41:45 2018 -0300
+++ b/str.h Fri Jul 13 04:44:06 2018 +0000
@@ -138,9 +138,9 @@
138138
139139 QP_EXPORT int utf8min (const char *__s, int __n);
140140
141-QP_EXPORT int ustrlen (const char *__s, int *__lenp);
141+QP_EXPORT int ustrlen (const void *__s, int *__lenp);
142142
143-QP_EXPORT int ustrnlen (const char *__s, int __bytes);
143+QP_EXPORT int ustrnlen (const void *__s, int __bytes);
144144
145145 QP_EXPORT int ustroff (const unsigned char *__p, int __idx);
146146
Show on old repository browser