From ffe0ca7d7f47dc11d8912b11daa388e66be358bb Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Mon, 18 Mar 2019 12:56:42 +0100 Subject: [PATCH] Issue 95: Improve error message when trying to call a non-callable. --- jsdate.c | 2 +- jsrun.c | 6 +++--- mujs.h | 1 + 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/jsdate.c b/jsdate.c index 5313250..9216c44 100644 --- a/jsdate.c +++ b/jsdate.c @@ -741,7 +741,7 @@ static void Dp_toJSON(js_State *J) js_getproperty(J, 0, "toISOString"); if (!js_iscallable(J, -1)) - js_typeerror(J, "Date.prototype.toJSON: this.toISOString not a function"); + js_typeerror(J, "this.toISOString is not a function"); js_copy(J, 0); js_call(J, 0); } diff --git a/jsrun.c b/jsrun.c index 49e08b5..6474a7f 100644 --- a/jsrun.c +++ b/jsrun.c @@ -230,7 +230,7 @@ int js_iserror(js_State *J, int idx) return v->type == JS_TOBJECT && v->u.object->type == JS_CERROR; } -static const char *js_typeof(js_State *J, int idx) +const char *js_typeof(js_State *J, int idx) { js_Value *v = stackidx(J, idx); switch (v->type) { @@ -1056,7 +1056,7 @@ void js_call(js_State *J, int n) int savebot; if (!js_iscallable(J, -n-2)) - js_typeerror(J, "called object is not a function"); + js_typeerror(J, "%s is not callable", js_typeof(J, -n-2)); obj = js_toobject(J, -n-2); @@ -1090,7 +1090,7 @@ void js_construct(js_State *J, int n) js_Object *newobj; if (!js_iscallable(J, -n-1)) - js_typeerror(J, "called object is not a function"); + js_typeerror(J, "%s is not callable", js_typeof(J, -n-1)); obj = js_toobject(J, -n-1); diff --git a/mujs.h b/mujs.h index d5dcc93..7c133dd 100644 --- a/mujs.h +++ b/mujs.h @@ -213,6 +213,7 @@ int js_compare(js_State *J, int *okay); int js_equal(js_State *J); int js_strictequal(js_State *J); int js_instanceof(js_State *J); +const char *js_typeof(js_State *J, int idx); #ifdef __cplusplus }