summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2021-04-12 16:50:34 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2021-04-12 16:50:34 -0300
commit5148954eed7550dcac587fce0214b470442efa0d (patch)
tree0190c45e592de8f0eb362c8c4ff369058a7d94dd
parentd205f3a4847bc8b835fda91f51ba1cf45b796baf (diff)
downloadlua-github-5148954eed7550dcac587fce0214b470442efa0d.tar.gz
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".
-rw-r--r--ldebug.c48
1 files changed, 37 insertions, 11 deletions
diff --git a/ldebug.c b/ldebug.c
index 1feaab22..433a8759 100644
--- a/ldebug.c
+++ b/ldebug.c
@@ -675,9 +675,21 @@ static const char *getupvalname (CallInfo *ci, const TValue *o,
}
+static const char *formatvarinfo (lua_State *L, const char *kind,
+ const char *name) {
+ if (kind == NULL)
+ return ""; /* no information */
+ else
+ return luaO_pushfstring(L, " (%s '%s')", kind, name);
+}
+
+/*
+** Build a string with a "description" for the value 'o', such as
+** "variable 'x'" or "upvalue 'y'".
+*/
static const char *varinfo (lua_State *L, const TValue *o) {
- const char *name = NULL; /* to avoid warnings */
CallInfo *ci = L->ci;
+ const char *name = NULL; /* to avoid warnings */
const char *kind = NULL;
if (isLua(ci)) {
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) {
kind = getobjname(ci_func(ci)->p, currentpc(ci),
cast_int(cast(StkId, o) - (ci->func + 1)), &name);
}
- return (kind) ? luaO_pushfstring(L, " (%s '%s')", kind, name) : "";
+ return formatvarinfo(L, kind, name);
}
-l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
+/*
+** Raise a type error
+*/
+static l_noret typeerror (lua_State *L, const TValue *o, const char *op,
+ const char *extra) {
const char *t = luaT_objtypename(L, o);
- luaG_runerror(L, "attempt to %s a %s value%s", op, t, varinfo(L, o));
+ luaG_runerror(L, "attempt to %s a %s value%s", op, t, extra);
}
+/*
+** Raise a type error with "standard" information about the faulty
+** object 'o' (using 'varinfo').
+*/
+l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
+ typeerror(L, o, op, varinfo(L, o));
+}
+
+
+/*
+** Raise an error for calling a non-callable object. Try to find
+** a name for the object based on the code that made the call
+** ('funcnamefromcode'); if it cannot get a name there, try 'varinfo'.
+*/
l_noret luaG_callerror (lua_State *L, const TValue *o) {
CallInfo *ci = L->ci;
const char *name = NULL; /* to avoid warnings */
- const char *what = (isLua(ci)) ? funcnamefromcode(L, ci, &name) : NULL;
- if (what != NULL) {
- const char *t = luaT_objtypename(L, o);
- luaG_runerror(L, "%s '%s' is not callable (a %s value)", what, name, t);
- }
- else
- luaG_typeerror(L, o, "call");
+ const char *kind = (isLua(ci)) ? funcnamefromcode(L, ci, &name) : NULL;
+ const char *extra = kind ? formatvarinfo(L, kind, name) : varinfo(L, o);
+ typeerror(L, o, "call", extra);
}