Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 1 | const WebIDL2 = require('webidl2'); |
| 2 | const fs = require('fs'); |
| 3 | const path = require('path'); |
| 4 | const ts = require('typescript'); |
| 5 | const glob = require('glob'); |
| 6 | const methods = { |
| 7 | __proto__: null |
| 8 | }; |
Tim van der Lippe | e6a9868 | 2020-01-15 14:23:08 +0000 | [diff] [blame] | 9 | const program = ts.createProgram( |
| 10 | [ |
| 11 | path.join(__dirname, 'node_modules', 'typescript', 'lib', 'lib.esnext.d.ts'), |
| 12 | ], |
| 13 | {noLib: true}); |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 14 | for (const file of program.getSourceFiles()) { |
| 15 | ts.forEachChild(file, node => { |
| 16 | if (node.kind === ts.SyntaxKind.InterfaceDeclaration) { |
| 17 | for (const member of node.members) { |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 18 | if (member.kind === ts.SyntaxKind.MethodSignature) { |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 19 | parseTSFunction(member, node); |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 20 | } |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 21 | } |
| 22 | } |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 23 | if (node.kind === ts.SyntaxKind.FunctionDeclaration) { |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 24 | parseTSFunction(node, {name: {text: 'Window'}}); |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 25 | } |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 26 | |
| 27 | }); |
| 28 | } |
| 29 | |
| 30 | function parseTSFunction(func, node) { |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 31 | if (!func.name.escapedText) { |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 32 | return; |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 33 | } |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 34 | |
| 35 | const args = func.parameters |
| 36 | .map(p => { |
| 37 | let text = p.name.escapedText; |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 38 | if (p.questionToken) { |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 39 | text = '?' + text; |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 40 | } |
| 41 | if (p.dotDotDotToken) { |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 42 | text = '...' + text; |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 43 | } |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 44 | return text; |
| 45 | }) |
| 46 | .filter(x => x !== 'this'); |
| 47 | storeMethod(node.name.text, func.name.escapedText, args); |
| 48 | } |
| 49 | |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 50 | glob('../../../../blink/renderer/+(core|modules)/**/*.idl', {cwd: process.env.PWD}, function(er, files) { |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 51 | for (const file of files) { |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 52 | if (file.includes('testing')) { |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 53 | continue; |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 54 | } |
Tim van der Lippe | e6a9868 | 2020-01-15 14:23:08 +0000 | [diff] [blame] | 55 | const data = fs.readFileSync(path.join(process.env.PWD, file), 'utf8'); |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 56 | const lines = data.split('\n'); |
| 57 | const newLines = []; |
| 58 | for (line of lines) { |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 59 | if (!line.includes(' attribute ')) { |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 60 | newLines.push(line); |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 61 | } |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | try { |
| 65 | WebIDL2.parse(newLines.join('\n')).forEach(walk); |
| 66 | } catch (e) { |
| 67 | // console.error(file); |
| 68 | } |
| 69 | } |
| 70 | WebIDL2 |
| 71 | .parse(` |
| 72 | namespace console { |
| 73 | void assert(optional boolean condition = false, any... data); |
| 74 | void clear(); |
| 75 | void count(optional DOMString label = "default"); |
| 76 | void debug(any... data); |
| 77 | void dir(any item, optional object? options); |
| 78 | void dirxml(any... data); |
| 79 | void error(any... data); |
| 80 | void group(any... data); |
| 81 | void groupCollapsed(any... data); |
| 82 | void groupEnd(); |
| 83 | void info(any... data); |
| 84 | void log(any... data); |
| 85 | void profile(optional DOMString title); |
| 86 | void profileEnd(optional DOMString title); |
| 87 | void table(any... tabularData); |
| 88 | void time(optional DOMString label); |
| 89 | void timeEnd(optional DOMString label); |
| 90 | void timeStamp(optional DOMString name); |
| 91 | void trace(any... data); |
| 92 | void warn(any... data); |
| 93 | }; |
| 94 | `).forEach(walk); |
| 95 | postProcess(); |
| 96 | }); |
| 97 | |
| 98 | function walk(thing, parent) { |
| 99 | if (thing.type === 'interface') { |
| 100 | const constructor = thing.extAttrs.find(extAttr => extAttr.name === 'Constructor'); |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 101 | if (constructor && constructor.arguments && thing.extAttrs.find(extAttr => extAttr.name === 'Exposed')) { |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 102 | storeMethod('Window', thing.name, constructor.arguments.map(argName)); |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 103 | } |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 104 | |
| 105 | const namedConstructor = thing.extAttrs.find(extAttr => extAttr.name === 'NamedConstructor'); |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 106 | if (namedConstructor && namedConstructor.arguments) { |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 107 | storeMethod('Window', namedConstructor.rhs.value, namedConstructor.arguments.map(argName)); |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 108 | } |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 109 | } |
| 110 | if (thing.type.includes('operation')) { |
| 111 | storeMethod(thing.static ? (parent.name + 'Constructor') : parent.name, thing.name, thing.arguments.map(argName)); |
| 112 | return; |
| 113 | } |
| 114 | if (thing.members) { |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 115 | for (const member of thing.members) { |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 116 | walk(member, thing); |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 117 | } |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| 121 | function argName(a) { |
| 122 | let name = a.name; |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 123 | if (a.optional) { |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 124 | name = '?' + name; |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 125 | } |
| 126 | if (a.variadic) { |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 127 | name = '...' + name; |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 128 | } |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 129 | return name; |
| 130 | } |
| 131 | |
| 132 | function storeMethod(parent, name, args) { |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 133 | if (!methods[name]) { |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 134 | methods[name] = {__proto__: null}; |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 135 | } |
| 136 | if (!methods[name][parent]) { |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 137 | methods[name][parent] = []; |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 138 | } |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 139 | methods[name][parent].push(args); |
| 140 | } |
| 141 | |
| 142 | function postProcess() { |
| 143 | for (const name in methods) { |
| 144 | const jsonParents = new Set(); |
| 145 | for (const parent in methods[name]) { |
| 146 | const signatures = methods[name][parent]; |
| 147 | signatures.sort((a, b) => a.length - b.length); |
| 148 | const filteredSignatures = []; |
| 149 | for (const signature of signatures) { |
| 150 | const smallerIndex = filteredSignatures.findIndex(smaller => startsThesame(smaller, signature)); |
| 151 | if (smallerIndex !== -1) { |
| 152 | filteredSignatures[smallerIndex] = (signature.map((arg, index) => { |
Joel Einbinder | 705daf0 | 2018-05-16 23:57:41 +0000 | [diff] [blame] | 153 | const otherArg = filteredSignatures[smallerIndex][index]; |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 154 | if (otherArg) { |
Joel Einbinder | 705daf0 | 2018-05-16 23:57:41 +0000 | [diff] [blame] | 155 | return otherArg.length > arg.length ? otherArg : arg; |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 156 | } |
| 157 | if (arg.startsWith('?') || arg.startsWith('...')) { |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 158 | return arg; |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 159 | } |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 160 | return '?' + arg; |
| 161 | })); |
| 162 | } else { |
| 163 | filteredSignatures.push(signature); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | function startsThesame(smaller, bigger) { |
| 168 | for (let i = 0; i < smaller.length; i++) { |
Joel Einbinder | 705daf0 | 2018-05-16 23:57:41 +0000 | [diff] [blame] | 169 | const withoutQuestion = str => /[\?]?(.*)/.exec(str)[1]; |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 170 | if (withoutQuestion(smaller[i]) !== withoutQuestion(bigger[i])) { |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 171 | return false; |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 172 | } |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 173 | } |
| 174 | return true; |
| 175 | } |
| 176 | |
| 177 | methods[name][parent] = filteredSignatures; |
| 178 | jsonParents.add(JSON.stringify(filteredSignatures)); |
| 179 | } |
| 180 | if (jsonParents.size === 1) { |
| 181 | methods[name] = {'*': JSON.parse(jsonParents.values().next().value)}; |
| 182 | } |
| 183 | for (const parent in methods[name]) { |
| 184 | const signatures = methods[name][parent]; |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 185 | if (signatures.length === 1 && !signatures[0].length) { |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 186 | delete methods[name][parent]; |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 187 | } |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 188 | } |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 189 | if (!Object.keys(methods[name]).length) { |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 190 | delete methods[name]; |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 191 | } |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 192 | } |
| 193 | const functions = []; |
| 194 | for (const name in methods) { |
| 195 | if (methods[name]['*']) { |
| 196 | functions.push({name, signatures: methods[name]['*']}); |
| 197 | } else { |
| 198 | for (const parent in methods[name]) { |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 199 | if (parent.endsWith('Constructor')) { |
| 200 | functions.push({ |
| 201 | name, |
| 202 | signatures: methods[name][parent], |
| 203 | static: true, |
| 204 | receiver: parent.substring(0, parent.length - 'Constructor'.length) |
| 205 | }); |
| 206 | } else { |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 207 | functions.push({name, signatures: methods[name][parent], receiver: parent}); |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 +0000 | [diff] [blame^] | 208 | } |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | fs.writeFileSync( |
| 214 | path.join(__dirname, '..', '..', 'front_end', 'javascript_metadata', 'NativeFunctions.js'), |
Brandon Goddard | 53faba1 | 2020-01-24 16:55:04 +0000 | [diff] [blame] | 215 | `// Copyright 2020 The Chromium Authors. All rights reserved. |
| 216 | // Use of this source code is governed by a BSD-style license that can be |
| 217 | // found in the LICENSE file. |
| 218 | // Generated from ${path.relative(path.join(__dirname, '..', '..'), __filename)} |
Paul Lewis | ea12f14 | 2019-11-26 17:00:09 +0000 | [diff] [blame] | 219 | export const NativeFunctions = ${JSON.stringify(functions)}; |
Paul Lewis | ea12f14 | 2019-11-26 17:00:09 +0000 | [diff] [blame] | 220 | `); |
Joel Einbinder | 3f23eb2 | 2018-05-14 23:27:51 +0000 | [diff] [blame] | 221 | } |