summaryrefslogtreecommitdiffstats
path: root/devtools/shared/webconsole/GenerateDataFromWebIdls.py
blob: c02f747c2a9f50ecae13b63fd908963db6218602 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""
This script parses mozilla-central's WebIDL bindings and writes a JSON-formatted
subset of the function bindings to several files:
- "devtools/server/actors/webconsole/webidl-pure-allowlist.js" (for eager evaluation processing)
- "devtools/server/actors/webconsole/webidl-deprecated-list.js"

Run this script via

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

with a mozconfig that references a built non-artifact build.
"""

from __future__ import absolute_import, unicode_literals, print_function
from os import path, remove, system
import json
import WebIDL
import buildconfig

# This is an explicit list 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.
PURE_INTERFACE_ALLOWLIST = set(
    [
        "Document",
        "Node",
        "DOMTokenList",
        "Element",
        "Performance",
        "URLSearchParams",
        "FormData",
        "Headers",
    ]
)

# This is an explicit list of interfaces to exclude.
DEPRECATED_INTERFACE__EXCLUDE_LIST = set(
    [
        "External",
        "TestExampleInterface",
        "TestInterface",
        "TestJSImplInterface",
        "TestingDeprecatedInterface",
    ]
)

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 GenerateDataFromWebIdls.py
// script. Do not modify it manually.
"use strict";

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

pure_output_file = path.join(
    buildconfig.topsrcdir, "devtools/server/actors/webconsole/webidl-pure-allowlist.js"
)
deprecated_output_file = path.join(
    buildconfig.topsrcdir, "devtools/server/actors/webconsole/webidl-deprecated-list.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", encoding="utf8") as f:
        parser.parse(f.read(), filepath)
results = parser.finish()

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

        for member in result.members:
            name = member.identifier.name

            # We only care about methods because eager evaluation assumes that
            # all getter functions are side-effect-free.
            if member.isMethod() and member.affects == "Nothing":
                if (
                    PURE_INTERFACE_ALLOWLIST and not iface in PURE_INTERFACE_ALLOWLIST
                ) or name.startswith("_"):
                    continue
                if not iface in pure_output:
                    pure_output[iface] = []
                if member.isStatic():
                    pure_output[iface].append([name])
                else:
                    pure_output[iface].append(["prototype", name])
            if (
                not iface in DEPRECATED_INTERFACE__EXCLUDE_LIST
                and (member.isMethod() or member.isAttr())
                and member.getExtendedAttribute("Deprecated")
            ):
                if not iface in deprecated_output:
                    deprecated_output[iface] = []
                if member.isStatic():
                    deprecated_output[iface].append([name])
                else:
                    deprecated_output[iface].append(["prototype", name])

with open(pure_output_file, "w") as f:
    f.write(FILE_TEMPLATE % {"data": json.dumps(pure_output, indent=2, sort_keys=True)})
print("Successfully generated", pure_output_file)

with open(deprecated_output_file, "w") as f:
    f.write(
        FILE_TEMPLATE
        % {"data": json.dumps(deprecated_output, indent=2, sort_keys=True)}
    )
print("Successfully generated", deprecated_output_file)

print("Formatting files...")
system("./mach eslint --fix " + pure_output_file + " " + deprecated_output_file)
print("Files are now properly formatted")

# Parsing the idls generate a parser.out file that we don't have any use of.
if path.exists("parser.out"):
    remove("parser.out")
print("DONE")