Closed Bug 1478936 Opened 6 years ago Closed 6 years ago

JS_GetFunctionArity's comment doesn't match to the body

Categories

(Core :: JavaScript Engine, enhancement, P3)

enhancement

Tracking

()

RESOLVED FIXED
mozilla63
Tracking Status
firefox63 --- fixed

People

(Reporter: arai, Assigned: arai)

Details

Attachments

(1 file)

JSFunction::nargs() is not `function.length`, and JS_GetFunctionArity comment is wrong, and the consumer expects `function.length`.

https://searchfox.org/mozilla-central/rev/d47c829065767b6f36d29303d650bffb7c4f94a3/js/src/vm/JSFunction.h#123-124
> class JSFunction : public js::NativeObject
> {
> ...
>     uint16_t        nargs_;       /* number of formal arguments
>                                      (including defaults and the rest parameter unlike f.length) */

https://searchfox.org/mozilla-central/rev/d47c829065767b6f36d29303d650bffb7c4f94a3/js/src/vm/JSFunction.h#221-223
> class JSFunction : public js::NativeObject
> {
> ...
>     size_t nargs() const {
>         return nargs_;
>     }

https://searchfox.org/mozilla-central/rev/d47c829065767b6f36d29303d650bffb7c4f94a3/js/src/jsapi.h#3090-3094
> /*
>  * Return the arity (length) of fun.
>  */
> extern JS_PUBLIC_API(uint16_t)
> JS_GetFunctionArity(JSFunction* fun);

https://searchfox.org/mozilla-central/rev/d47c829065767b6f36d29303d650bffb7c4f94a3/js/src/jsapi.cpp#3666-3670
> JS_PUBLIC_API(uint16_t)
> JS_GetFunctionArity(JSFunction* fun)
> {
>     return fun->nargs();
> }

https://searchfox.org/mozilla-central/rev/d47c829065767b6f36d29303d650bffb7c4f94a3/js/xpconnect/wrappers/XrayWrapper.cpp#533-538
>         } else if (key == JSProto_Function) {
>             if (id == GetJSIDByIndex(cx, XPCJSContext::IDX_LENGTH)) {
>                 FillPropertyDescriptor(desc, wrapper, JSPROP_PERMANENT | JSPROP_READONLY,
>                                        NumberValue(JS_GetFunctionArity(JS_GetObjectFunction(target))));
>                 return true;
>             }
Summary: JS_GetFunctionArity shouldn't return JSFunction::nargs() → JS_GetFunctionArity's comment doesn't match to the body
JS_GetFunctionArity returns nargs, that is the number of comma-separated components in parameter, including default parameter and rest parameter.
JS_GetFunctionLength returns .length property of the function object.
Assignee: nobody → arai.unmht
Status: NEW → ASSIGNED
Attachment #8998405 - Flags: review?(jdemooij)
Comment on attachment 8998405 [details] [diff] [review]
Fix the comment for JS_GetFunctionArity, and add JS_GetFunctionLength which matches to the original comment, and fixed consumer.

Review of attachment 8998405 [details] [diff] [review]:
-----------------------------------------------------------------

Nice :)
Attachment #8998405 - Flags: review?(jdemooij) → review+
Sorry I forgot this before: could you add assertSameCompartment(cx, fun); in JS_GetFunctionLength? Thanks!
(In reply to Jan de Mooij [:jandem] from comment #3)
> Sorry I forgot this before: could you add assertSameCompartment(cx, fun); in
> JS_GetFunctionLength? Thanks!

unfortunately it fails
https://treeherder.mozilla.org/logviewer.html#?job_id=192933763&repo=try&lineNumber=8446

I'll investigate it.
maybe we should wrap it either in caller or in JS_GetFunctionLength.
Yeah I think the caller should just use JSAutoRealm around the call.

It's not strictly necessary because when we delazify, we also enter the function's realm. However I think it's good if all APIs that take a cx + object assertSameCompartment so it's easier to reason about the code.
okay, I'll land with the fix.
https://hg.mozilla.org/integration/mozilla-inbound/rev/60a38b319b323f9b8c44040eedecb49715a129b0
Bug 1478936 - Fix the comment for JS_GetFunctionArity, and add JS_GetFunctionLength which matches to the original comment, and fixed consumer. r=jandem
https://hg.mozilla.org/mozilla-central/rev/60a38b319b32
Status: ASSIGNED → RESOLVED
Closed: 6 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla63
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: