Bug 1375829 part 4. Enforce the spec restrictions on methods named toJSON(). r=qdot
authorBoris Zbarsky <bzbarsky@mit.edu>
Thu, 17 May 2018 23:43:55 -0400
changeset 418798 23c967ee8611
parent 418797 3db98d12d700
child 418799 1f3dbe030f57
push id34013
push userdluca@mozilla.com
push date2018-05-18 09:56 +0000
treeherdermozilla-central@11ee70f24ea5 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersqdot
bugs1375829
milestone62.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 1375829 part 4. Enforce the spec restrictions on methods named toJSON(). r=qdot There are two restrictions: such methods must take no arguments and must return a JSON type.
dom/bindings/parser/WebIDL.py
dom/bindings/parser/tests/test_toJSON.py
--- a/dom/bindings/parser/WebIDL.py
+++ b/dom/bindings/parser/WebIDL.py
@@ -4973,16 +4973,29 @@ class IDLMethod(IDLInterfaceMember, IDLS
                               [overloadWithPromiseReturnType.location])
 
         if self.getExtendedAttribute("StaticClassOverride") and not \
            (self.identifier.scope.isJSImplemented() and self.isStatic()):
             raise WebIDLError("StaticClassOverride can be applied to static"
                               " methods on JS-implemented classes only.",
                               [self.location])
 
+        # Ensure that toJSON methods satisfy the spec constraints on them.
+        if self.identifier.name == "toJSON":
+            if len(self.signatures()) != 1:
+                raise WebIDLError("toJSON method has multiple overloads",
+                                  [self._overloads[0].location,
+                                   self._overloads[1].location])
+            if len(self.signatures()[0][1]) != 0:
+                raise WebIDLError("toJSON method has arguments",
+                                  [self.location])
+            if not self.signatures()[0][0].isJSONType():
+                raise WebIDLError("toJSON method has non-JSON return type",
+                                  [self.location])
+
     def overloadsForArgCount(self, argc):
         return [overload for overload in self._overloads if
                 len(overload.arguments) == argc or
                 (len(overload.arguments) > argc and
                  all(arg.optional for arg in overload.arguments[argc:])) or
                 (len(overload.arguments) < argc and
                  len(overload.arguments) > 0 and
                  overload.arguments[-1].variadic)]
new file mode 100644
--- /dev/null
+++ b/dom/bindings/parser/tests/test_toJSON.py
@@ -0,0 +1,59 @@
+def WebIDLTest(parser, harness):
+    threw = False
+    try:
+        parser.parse(
+            """
+            interface Test {
+              object toJSON();
+            };
+            """)
+        results = parser.finish()
+    except:
+        threw = True
+    harness.ok(not threw, "Should allow a toJSON method.")
+
+    parser = parser.reset()
+    threw = False
+    try:
+        parser.parse(
+            """
+            interface Test {
+              object toJSON(object arg);
+              object toJSON(long arg);
+            };
+            """)
+        results = parser.finish()
+    except:
+        threw = True
+    harness.ok(threw, "Should not allow overloads of a toJSON method.")
+
+    parser = parser.reset()
+    threw = False
+    try:
+        parser.parse(
+            """
+            interface Test {
+              object toJSON(object arg);
+            };
+            """)
+        results = parser.finish()
+    except:
+        threw = True
+    harness.ok(threw, "Should not allow a toJSON method with arguments.")
+
+    parser = parser.reset()
+    threw = False
+    try:
+        parser.parse(
+            """
+            interface Test {
+              any toJSON();
+            };
+            """)
+        results = parser.finish()
+    except:
+        threw = True
+    harness.ok(threw, "Should not allow a toJSON method with a non-JSON return type.")
+
+    # We should probably write some tests here about what types are
+    # considered JSON types.  Bug 1462537.