7 static void jsB_Function(js_State *J)
9 unsigned int i, top = js_gettop(J);
17 for (i = 1; i < top - 1; ++i) {
20 js_puts(J, &sb, js_tostring(J, i));
26 body = js_isdefined(J, top - 1) ? js_tostring(J, top - 1) : "";
34 parse = jsP_parsefunction(J, "[string]", sb ? sb->s : NULL, body);
35 fun = jsC_compilefunction(J, parse);
41 js_newfunction(J, fun, J->GE);
44 static void jsB_Function_prototype(js_State *J)
49 static void Fp_toString(js_State *J)
51 js_Object *self = js_toobject(J, 0);
55 if (!js_iscallable(J, 0))
56 js_typeerror(J, "not a function");
58 if (self->type == JS_CFUNCTION || self->type == JS_CSCRIPT) {
59 js_Function *F = self->u.f.function;
60 n = strlen("function () { ... }");
62 for (i = 0; i < F->numparams; ++i)
63 n += strlen(F->vartab[i]) + 1;
65 strcpy(s, "function ");
68 for (i = 0; i < F->numparams; ++i) {
69 if (i > 0) strcat(s, ",");
70 strcat(s, F->vartab[i]);
72 strcat(s, ") { ... }");
81 js_pushliteral(J, "function () { ... }");
85 static void Fp_apply(js_State *J)
89 if (!js_iscallable(J, 0))
90 js_typeerror(J, "not a function");
95 n = js_getlength(J, 2);
96 for (i = 0; i < n; ++i)
102 static void Fp_call(js_State *J)
104 unsigned int i, top = js_gettop(J);
106 if (!js_iscallable(J, 0))
107 js_typeerror(J, "not a function");
109 for (i = 0; i < top; ++i)
115 static void callbound(js_State *J)
117 unsigned int top = js_gettop(J);
118 unsigned int i, fun, args, n;
121 js_currentfunction(J);
122 js_getproperty(J, fun, "__TargetFunction__");
123 js_getproperty(J, fun, "__BoundThis__");
126 js_getproperty(J, fun, "__BoundArguments__");
127 n = js_getlength(J, args);
128 for (i = 0; i < n; ++i)
129 js_getindex(J, args, i);
132 for (i = 1; i < top; ++i)
135 js_call(J, n + top - 1);
138 static void constructbound(js_State *J)
140 unsigned int top = js_gettop(J);
141 unsigned int i, fun, args, n;
144 js_currentfunction(J);
145 js_getproperty(J, fun, "__TargetFunction__");
148 js_getproperty(J, fun, "__BoundArguments__");
149 n = js_getlength(J, args);
150 for (i = 0; i < n; ++i)
151 js_getindex(J, args, i);
154 for (i = 1; i < top; ++i)
157 js_construct(J, n + top - 1);
160 static void Fp_bind(js_State *J)
162 unsigned int i, top = js_gettop(J);
165 if (!js_iscallable(J, 0))
166 js_typeerror(J, "not a function");
168 n = js_getlength(J, 0);
174 js_newcconstructor(J, callbound, constructbound, "[bind]", n);
176 /* Reuse target function's prototype for HasInstance check. */
177 js_getproperty(J, 0, "prototype");
178 js_defproperty(J, -2, "prototype", JS_READONLY | JS_DONTENUM | JS_DONTCONF);
180 /* target function */
182 js_defproperty(J, -2, "__TargetFunction__", JS_READONLY | JS_DONTENUM | JS_DONTCONF);
186 js_defproperty(J, -2, "__BoundThis__", JS_READONLY | JS_DONTENUM | JS_DONTCONF);
188 /* bound arguments */
190 for (i = 2; i < top; ++i) {
192 js_setindex(J, -2, i - 2);
194 js_defproperty(J, -2, "__BoundArguments__", JS_READONLY | JS_DONTENUM | JS_DONTCONF);
197 void jsB_initfunction(js_State *J)
199 J->Function_prototype->u.c.function = jsB_Function_prototype;
200 J->Function_prototype->u.c.constructor = NULL;
202 js_pushobject(J, J->Function_prototype);
204 jsB_propf(J, "toString", Fp_toString, 2);
205 jsB_propf(J, "apply", Fp_apply, 2);
206 jsB_propf(J, "call", Fp_call, 1);
207 jsB_propf(J, "bind", Fp_bind, 1);
209 js_newcconstructor(J, jsB_Function, jsB_Function, "Function", 1);
210 js_defglobal(J, "Function", JS_DONTENUM);