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