shithub: libmujs

Download patch

ref: 8cb771cd7c5d2a21084a42236f8f62efd450eee3
parent: 7159bcd774d074453268bc227cd971ab189b30b5
author: Tor Andersson <tor@ccxvii.net>
date: Sat Jan 25 10:45:43 EST 2014

Print a better error message when trying to call a non-function.

--- a/jsrun.c
+++ b/jsrun.c
@@ -92,7 +92,9 @@
 {
 	const js_Value *v = stackidx(J, idx);
 	if (v->type == JS_TOBJECT)
-		return v->u.object->type == JS_CFUNCTION || v->u.object->type == JS_CCFUNCTION;
+		return v->u.object->type == JS_CFUNCTION ||
+			v->u.object->type == JS_CSCRIPT ||
+			v->u.object->type == JS_CCFUNCTION;
 	return 0;
 }
 
@@ -557,9 +559,17 @@
 
 void js_call(js_State *J, int n)
 {
-	js_Object *obj = js_toobject(J, -n - 2);
-	int savebot = BOT;
+	js_Object *obj;
+	int savebot;
+
+	if (!js_iscallable(J, -n - 2))
+		js_typeerror(J, "called object is not a function");
+
+	obj = js_toobject(J, -n - 2);
+
+	savebot = BOT;
 	BOT = TOP - n - 1;
+
 	if (obj->type == JS_CFUNCTION)
 		jsR_callfunction(J, n, obj->u.f.function, obj->u.f.scope);
 	else if (obj->type == JS_CSCRIPT)
@@ -566,16 +576,20 @@
 		jsR_callscript(J, n, obj->u.f.function);
 	else if (obj->type == JS_CCFUNCTION)
 		jsR_callcfunction(J, n, obj->u.c.function);
-	else
-		js_typeerror(J, "call: not a function");
+
 	BOT = savebot;
 }
 
 void js_construct(js_State *J, int n)
 {
-	js_Object *obj = js_toobject(J, -n - 1);
+	js_Object *obj;
 	js_Object *prototype;
 	js_Object *newobj;
+
+	if (!js_iscallable(J, -n - 1))
+		js_typeerror(J, "called object is not a function");
+
+	obj = js_toobject(J, -n - 1);
 
 	/* built-in constructors create their own objects, give them a 'null' this */
 	if (obj->type == JS_CCFUNCTION && obj->u.c.constructor) {