From 0feb630124c74afff1d21473060c3f37f74e6e54 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Sun, 19 Jan 2014 13:28:01 +0100 Subject: [PATCH] Implement Function.prototype.apply() --- jsbfunction.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/jsbfunction.c b/jsbfunction.c index ca98c5c..16549b0 100644 --- a/jsbfunction.c +++ b/jsbfunction.c @@ -12,6 +12,33 @@ static int jsB_Function_prototype(js_State *J, int n) return 1; } +static int Fp_apply(js_State *J, int n) +{ + int i, argc; + char name[20]; + + if (!js_iscallable(J, 0)) + jsR_error(J, "TypeError"); + js_copy(J, 0); + + if (js_isundefined(J, 1) || js_isnull(J, 1)) + js_pushglobal(J); + else + js_copy(J, 1); + + js_getproperty(J, 2, "length"); + argc = js_tonumber(J, -1); + js_pop(J, 1); + + for (i = 0; i < argc; ++i) { + sprintf(name, "%d", i); + js_getproperty(J, 2, name); + } + + js_call(J, argc); + return 1; +} + static int Fp_call(js_State *J, int n) { int i;