JSFunctionSpec::nargs in 1.60

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

JSFunctionSpec::nargs in 1.60

Donny Viszneki
Am I misunderstanding the documentation for JSFunctionSpec::nargs?
When I specify a value of 1 with this field, the following code still
receives multiple arguments:

static JSBool
add_all(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval
*rval)
{
    uintN i;
    jsdouble n, r=0;

    for (i = 0; i < argc; i++) {
        if (!JS_ValueToNumber(cx, argv[i], &n)) return JS_FALSE;
        r += n;
    }

    if (!JS_NewNumberValue(cx, r, rval)) return JS_FALSE;

    return JS_TRUE;
}

JSFunctionSpec spidermonkey_functions[] = {
    {"add_all",     add_all,    1},
    {0}
};

js> add_all(1,2,3)
6


Thanks in advance for any helpful responses

_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: JSFunctionSpec::nargs in 1.60

James Ross
Donny wrote:
> Am I misunderstanding the documentation for JSFunctionSpec::nargs?
> When I specify a value of 1 with this field, the following code still
> receives multiple arguments:

According to what I can find (mostly the source code, unfortunately),
the nargs value specifies the minimum number of items in the argv array
when your function is called.

E.g. if you specify nargs as 2, argv[0] and argv[1] will always exist,
even if the caller didn't specify them. argc will reflect the actual
number passed by the caller (which could be less, the same or more than
nargs). Any extra values will be JSVAL_VOID.

--
James Ross <[hidden email]>
ChatZilla and Venkman Developer
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: JSFunctionSpec::nargs in 1.60

Mike Shaver
On 3/24/07, James Ross <[hidden email]> wrote:

> Donny wrote:
> > Am I misunderstanding the documentation for JSFunctionSpec::nargs?
> > When I specify a value of 1 with this field, the following code still
> > receives multiple arguments:
>
> According to what I can find (mostly the source code, unfortunately),
> the nargs value specifies the minimum number of items in the argv array
> when your function is called.
>
> E.g. if you specify nargs as 2, argv[0] and argv[1] will always exist,
> even if the caller didn't specify them. argc will reflect the actual
> number passed by the caller (which could be less, the same or more than
> nargs). Any extra values will be JSVAL_VOID.

That's exactly correct, yes.  I've added your explanation and example
to http://developer.mozilla.org/en/docs/JSFunctionSpec for future
reference; thanks!

Mike
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine