Commit 77131a2f authored by Logan Smyth's avatar Logan Smyth
Browse files

Bug 1609426 - Part 3: Automatically generate the list of pure functions. r=jlast

Differential Revision: https://phabricator.services.mozilla.com/D61965

--HG--
extra : moz-landing-system : lando
parent 5308aa16
Loading
Loading
Loading
Loading
+1 −0
Original line number Original line Diff line number Diff line
@@ -14,5 +14,6 @@ DevToolsModules(
    'eval-with-debugger.js',
    'eval-with-debugger.js',
    'message-manager-mock.js',
    'message-manager-mock.js',
    'utils.js',
    'utils.js',
    'webidl-pure-whitelist.js',
    'worker-listeners.js',
    'worker-listeners.js',
)
)
+68 −0
Original line number Original line Diff line number Diff line
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// This file is automatically generated by the GeneratePureDOMFunctions.py
// script. Do not modify it manually.
"use strict";

module.exports = {
  DOMTokenList: [["prototype", "item"], ["prototype", "contains"]],
  Document: [
    ["prototype", "getElementsByTagName"],
    ["prototype", "getElementsByTagNameNS"],
    ["prototype", "getElementsByClassName"],
    ["prototype", "getElementById"],
    ["prototype", "getElementsByName"],
    ["prototype", "querySelector"],
    ["prototype", "querySelectorAll"],
    ["prototype", "createNSResolver"],
  ],
  Element: [
    ["prototype", "getAttributeNames"],
    ["prototype", "getAttribute"],
    ["prototype", "getAttributeNS"],
    ["prototype", "hasAttribute"],
    ["prototype", "hasAttributeNS"],
    ["prototype", "hasAttributes"],
    ["prototype", "closest"],
    ["prototype", "matches"],
    ["prototype", "webkitMatchesSelector"],
    ["prototype", "getElementsByTagName"],
    ["prototype", "getElementsByTagNameNS"],
    ["prototype", "getElementsByClassName"],
    ["prototype", "mozMatchesSelector"],
    ["prototype", "querySelector"],
    ["prototype", "querySelectorAll"],
    ["prototype", "getAsFlexContainer"],
    ["prototype", "getGridFragments"],
    ["prototype", "getElementsWithGrid"],
  ],
  FormData: [
    ["prototype", "entries"],
    ["prototype", "keys"],
    ["prototype", "values"],
  ],
  Headers: [
    ["prototype", "entries"],
    ["prototype", "keys"],
    ["prototype", "values"],
  ],
  Node: [
    ["prototype", "getRootNode"],
    ["prototype", "hasChildNodes"],
    ["prototype", "isSameNode"],
    ["prototype", "isEqualNode"],
    ["prototype", "compareDocumentPosition"],
    ["prototype", "contains"],
    ["prototype", "lookupPrefix"],
    ["prototype", "lookupNamespaceURI"],
    ["prototype", "isDefaultNamespace"],
  ],
  Performance: [["prototype", "now"]],
  URLSearchParams: [
    ["prototype", "entries"],
    ["prototype", "keys"],
    ["prototype", "values"],
  ],
};
+93 −0
Original line number Original line Diff line number Diff line
"""
This script parses mozilla-central's WebIDL bindings and writes a JSON-formatted
subset of the function bindings to the file
"devtools/server/actors/webconsole/webidl-pure-whitelist.js" so that they may
be used by the devtools for eager evaluation processing.

Run this script via

> ./mach python devtools/shared/webconsole/GeneratePureDOMFunctions.py

with a mozconfig that references a built non-artifact build, and then run

> ./mach eslint --fix devtools/server/actors/webconsole/webidl-pure-whitelist.js

to format the file properly.
"""

from __future__ import absolute_import, unicode_literals
from os import path
import json
import WebIDL
import buildconfig

# This is an explicit whitelist of interfaces to load [Pure] and [Constant]
# annotation for. There are a bunch of things that are pure in other interfaces
# that we don't care about in the context of the devtools.
INTERFACE_WHITELIST = set([
  "Document",
  "Node",
  "DOMTokenList",
  "Element",
  "Performance",
  "URLSearchParams",
  "FormData",
  "Headers"
])

FILE_TEMPLATE = """\
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// This file is automatically generated by the GeneratePureDOMFunctions.py
// script. Do not modify it manually.
"use strict";

module.exports = %(pure_data)s;
"""

output_file = path.join(
  buildconfig.topsrcdir,
  "devtools/server/actors/webconsole/webidl-pure-whitelist.js"
)

input_file = path.join(buildconfig.topobjdir, "dom/bindings/file-lists.json")

if not path.isfile(input_file):
  raise Exception(
    "Script must be run with a mozconfig referencing a non-artifact OBJDIR")

file_list = json.load(open(input_file))

parser = WebIDL.Parser()
for filepath in file_list["webidls"]:
  with open(filepath, 'r') as f:
    parser.parse(f.read(), filepath)
results = parser.finish()

output = {}
for result in results:
  if isinstance(result, WebIDL.IDLInterface):
    iface = result.identifier.name

    for member in result.members:
      # We only care about methods because eager evaluation assumes that
      # all getter functions are side-effect-free.
      if member.isMethod() and member.affects == "Nothing":
        name = member.identifier.name

        if ((INTERFACE_WHITELIST and not iface in INTERFACE_WHITELIST) or
            name.startswith("_")):
          continue
        if not iface in output:
          output[iface] = []
        if member.isStatic():
          output[iface].append([name])
        else:
          output[iface].append(["prototype", name])

with open(output_file, "w") as f:
  f.write(FILE_TEMPLATE % {
    'pure_data': json.dumps(output, indent=2, sort_keys=True)
  })