shithub: libmujs

Download patch

ref: 3da05690f11c1476cf7981c0709aed828c80e44d
parent: 48e3fdb718707b564c2a24d5ee796b3a9a6c703a
author: Tor Andersson <[email protected]>
date: Sun Jan 19 09:24:21 EST 2014

Implement Function.prototype.toString()

--- a/jsbfunction.c
+++ b/jsbfunction.c
@@ -1,4 +1,5 @@
 #include "js.h"
+#include "jscompile.h"
 #include "jsobject.h"
 #include "jsrun.h"
 #include "jsstate.h"
@@ -12,6 +13,39 @@
 	return 1;
 }
 
+static int Fp_toString(js_State *J, int nargs)
+{
+	js_Object *self = js_toobject(J, 0);
+	char *s;
+	int i, n;
+
+	if (!js_iscallable(J, 0))
+		jsR_error(J, "TypeError");
+
+	if (self->type == JS_CFUNCTION || self->type == JS_CSCRIPT) {
+		js_Function *F = self->u.f.function;
+		n = strlen("function () { ... }");
+		n += strlen(F->name);
+		for (i = 0; i < F->numparams; i++)
+			n += strlen(F->params[i]) + 1;
+		s = malloc(n);
+		strcpy(s, "function ");
+		strcat(s, F->name);
+		strcat(s, "(");
+		for (i = 0; i < F->numparams; i++) {
+			if (i > 0) strcat(s, ",");
+			strcat(s, F->params[i]);
+		}
+		strcat(s, ") { ... }");
+		js_pushstring(J, s);
+		free(s);
+	} else {
+		js_pushliteral(J, "function () { ... }");
+	}
+
+	return 1;
+}
+
 static int Fp_apply(js_State *J, int n)
 {
 	int i, argc;
@@ -72,8 +106,8 @@
 		{
 			js_copy(J, -2);
 			js_setproperty(J, -2, "constructor");
-			// jsB_propf(J, "toString", Fp_toString, 2);
-			// jsB_propf(J, "apply", Fp_apply, 2);
+			jsB_propf(J, "toString", Fp_toString, 2);
+			jsB_propf(J, "apply", Fp_apply, 2);
 			jsB_propf(J, "call", Fp_call, 1);
 		}
 		js_setproperty(J, -2, "prototype");
--- a/jscompile.c
+++ b/jscompile.c
@@ -724,7 +724,7 @@
 		emitfunction(J, F, F);
 		emitstring(J, F, OP_FUNDEC, name->string);
 	} else {
-		F->name = "<anonymous>";
+		F->name = "";
 	}
 
 	cparams(J, F, params);