Bug 1478936 - Fix the comment for JS_GetFunctionArity, and add JS_GetFunctionLength which matches to the original comment, and fixed consumer. r=jandem
authorTooru Fujisawa <arai_a@mac.com>
Fri, 10 Aug 2018 07:49:17 +0900
changeset 486015 60a38b319b323f9b8c44040eedecb49715a129b0
parent 486014 e51868670f3cd7c2c029cd0843e9919a5664db78
child 486016 e20a86a9b5756d9bf2ec3c48b1c7ddbdb95a9eba
push id9719
push userffxbld-merge
push dateFri, 24 Aug 2018 17:49:46 +0000
treeherdermozilla-beta@719ec98fba77 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjandem
bugs1478936
milestone63.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1478936 - Fix the comment for JS_GetFunctionArity, and add JS_GetFunctionLength which matches to the original comment, and fixed consumer. r=jandem
js/src/jsapi.cpp
js/src/jsapi.h
js/xpconnect/wrappers/XrayWrapper.cpp
--- a/js/src/jsapi.cpp
+++ b/js/src/jsapi.cpp
@@ -3686,16 +3686,23 @@ JS_GetFunctionDisplayId(JSFunction* fun)
 
 JS_PUBLIC_API(uint16_t)
 JS_GetFunctionArity(JSFunction* fun)
 {
     return fun->nargs();
 }
 
 JS_PUBLIC_API(bool)
+JS_GetFunctionLength(JSContext* cx, HandleFunction fun, uint16_t* length)
+{
+    assertSameCompartment(cx, fun);
+    return JSFunction::getLength(cx, fun, length);
+}
+
+JS_PUBLIC_API(bool)
 JS_ObjectIsFunction(JSContext* cx, JSObject* obj)
 {
     return obj->is<JSFunction>();
 }
 
 JS_PUBLIC_API(bool)
 JS_IsNativeFunction(JSObject* funobj, JSNative call)
 {
--- a/js/src/jsapi.h
+++ b/js/src/jsapi.h
@@ -3132,21 +3132,28 @@ JS_GetFunctionId(JSFunction* fun);
  * engine in the case that the function was defined to be anonymous. This can
  * still return nullptr if a useful display name could not be inferred. The
  * same restrictions on rooting as those in JS_GetFunctionId apply.
  */
 extern JS_PUBLIC_API(JSString*)
 JS_GetFunctionDisplayId(JSFunction* fun);
 
 /*
- * Return the arity (length) of fun.
+ * Return the arity of fun, which includes default parameters and rest
+ * parameter.  This can be used as `nargs` parameter for other functions.
  */
 extern JS_PUBLIC_API(uint16_t)
 JS_GetFunctionArity(JSFunction* fun);
 
+/*
+ * Return the length of fun, which is the original value of .length property.
+ */
+JS_PUBLIC_API(bool)
+JS_GetFunctionLength(JSContext* cx, JS::HandleFunction fun, uint16_t* length);
+
 /**
  * Infallible predicate to test whether obj is a function object (faster than
  * comparing obj's class name to "Function", but equivalent unless someone has
  * overwritten the "Function" identifier with a different constructor and then
  * created instances using that constructor that might be passed in as obj).
  */
 extern JS_PUBLIC_API(bool)
 JS_ObjectIsFunction(JSContext* cx, JSObject* obj);
--- a/js/xpconnect/wrappers/XrayWrapper.cpp
+++ b/js/xpconnect/wrappers/XrayWrapper.cpp
@@ -531,18 +531,25 @@ JSXrayTraits::resolveOwnProperty(JSConte
                 }
                 JS_ReportErrorASCII(cx, "Accessing TypedArray data over Xrays is slow, and forbidden "
                                     "in order to encourage performant code. To copy TypedArrays "
                                     "across origin boundaries, consider using Components.utils.cloneInto().");
                 return false;
             }
         } else if (key == JSProto_Function) {
             if (id == GetJSIDByIndex(cx, XPCJSContext::IDX_LENGTH)) {
+                uint16_t length;
+                RootedFunction fun(cx, JS_GetObjectFunction(target));
+                {
+                    JSAutoRealm ar(cx, target);
+                    if (!JS_GetFunctionLength(cx, fun, &length))
+                        return false;
+                }
                 FillPropertyDescriptor(desc, wrapper, JSPROP_PERMANENT | JSPROP_READONLY,
-                                       NumberValue(JS_GetFunctionArity(JS_GetObjectFunction(target))));
+                                       NumberValue(length));
                 return true;
             }
             if (id == GetJSIDByIndex(cx, XPCJSContext::IDX_NAME)) {
                 RootedString fname(cx, JS_GetFunctionId(JS_GetObjectFunction(target)));
                 if (fname)
                     JS_MarkCrossZoneIdValue(cx, StringValue(fname));
                 FillPropertyDescriptor(desc, wrapper, JSPROP_PERMANENT | JSPROP_READONLY,
                                        fname ? StringValue(fname) : JS_GetEmptyStringValue(cx));