diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2021-04-12 16:50:34 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2021-04-12 16:50:34 -0300 |
commit | 5148954eed7550dcac587fce0214b470442efa0d (patch) | |
tree | 0190c45e592de8f0eb362c8c4ff369058a7d94dd /ldebug.c | |
parent | d205f3a4847bc8b835fda91f51ba1cf45b796baf (diff) | |
download | lua-5148954eed7550dcac587fce0214b470442efa0d.tar.gz lua-5148954eed7550dcac587fce0214b470442efa0d.tar.bz2 lua-5148954eed7550dcac587fce0214b470442efa0d.zip |
Align error messages for calling non-callable values
'pcall(foo)' message was "attempt to call a table value", while
'pcall(function () foo() end) message was "global 'foo' is not callable".
Diffstat (limited to 'ldebug.c')
-rw-r--r-- | ldebug.c | 48 |
1 files changed, 37 insertions, 11 deletions
@@ -675,9 +675,21 @@ static const char *getupvalname (CallInfo *ci, const TValue *o, | |||
675 | } | 675 | } |
676 | 676 | ||
677 | 677 | ||
678 | static const char *formatvarinfo (lua_State *L, const char *kind, | ||
679 | const char *name) { | ||
680 | if (kind == NULL) | ||
681 | return ""; /* no information */ | ||
682 | else | ||
683 | return luaO_pushfstring(L, " (%s '%s')", kind, name); | ||
684 | } | ||
685 | |||
686 | /* | ||
687 | ** Build a string with a "description" for the value 'o', such as | ||
688 | ** "variable 'x'" or "upvalue 'y'". | ||
689 | */ | ||
678 | static const char *varinfo (lua_State *L, const TValue *o) { | 690 | static const char *varinfo (lua_State *L, const TValue *o) { |
679 | const char *name = NULL; /* to avoid warnings */ | ||
680 | CallInfo *ci = L->ci; | 691 | CallInfo *ci = L->ci; |
692 | const char *name = NULL; /* to avoid warnings */ | ||
681 | const char *kind = NULL; | 693 | const char *kind = NULL; |
682 | if (isLua(ci)) { | 694 | if (isLua(ci)) { |
683 | kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */ | 695 | kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */ |
@@ -685,26 +697,40 @@ static const char *varinfo (lua_State *L, const TValue *o) { | |||
685 | kind = getobjname(ci_func(ci)->p, currentpc(ci), | 697 | kind = getobjname(ci_func(ci)->p, currentpc(ci), |
686 | cast_int(cast(StkId, o) - (ci->func + 1)), &name); | 698 | cast_int(cast(StkId, o) - (ci->func + 1)), &name); |
687 | } | 699 | } |
688 | return (kind) ? luaO_pushfstring(L, " (%s '%s')", kind, name) : ""; | 700 | return formatvarinfo(L, kind, name); |
689 | } | 701 | } |
690 | 702 | ||
691 | 703 | ||
692 | l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) { | 704 | /* |
705 | ** Raise a type error | ||
706 | */ | ||
707 | static l_noret typeerror (lua_State *L, const TValue *o, const char *op, | ||
708 | const char *extra) { | ||
693 | const char *t = luaT_objtypename(L, o); | 709 | const char *t = luaT_objtypename(L, o); |
694 | luaG_runerror(L, "attempt to %s a %s value%s", op, t, varinfo(L, o)); | 710 | luaG_runerror(L, "attempt to %s a %s value%s", op, t, extra); |
695 | } | 711 | } |
696 | 712 | ||
697 | 713 | ||
714 | /* | ||
715 | ** Raise a type error with "standard" information about the faulty | ||
716 | ** object 'o' (using 'varinfo'). | ||
717 | */ | ||
718 | l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) { | ||
719 | typeerror(L, o, op, varinfo(L, o)); | ||
720 | } | ||
721 | |||
722 | |||
723 | /* | ||
724 | ** Raise an error for calling a non-callable object. Try to find | ||
725 | ** a name for the object based on the code that made the call | ||
726 | ** ('funcnamefromcode'); if it cannot get a name there, try 'varinfo'. | ||
727 | */ | ||
698 | l_noret luaG_callerror (lua_State *L, const TValue *o) { | 728 | l_noret luaG_callerror (lua_State *L, const TValue *o) { |
699 | CallInfo *ci = L->ci; | 729 | CallInfo *ci = L->ci; |
700 | const char *name = NULL; /* to avoid warnings */ | 730 | const char *name = NULL; /* to avoid warnings */ |
701 | const char *what = (isLua(ci)) ? funcnamefromcode(L, ci, &name) : NULL; | 731 | const char *kind = (isLua(ci)) ? funcnamefromcode(L, ci, &name) : NULL; |
702 | if (what != NULL) { | 732 | const char *extra = kind ? formatvarinfo(L, kind, name) : varinfo(L, o); |
703 | const char *t = luaT_objtypename(L, o); | 733 | typeerror(L, o, "call", extra); |
704 | luaG_runerror(L, "%s '%s' is not callable (a %s value)", what, name, t); | ||
705 | } | ||
706 | else | ||
707 | luaG_typeerror(L, o, "call"); | ||
708 | } | 734 | } |
709 | 735 | ||
710 | 736 | ||