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 | }; |
| 9 | const methodsByName = { |
| 10 | __proto__: null |
| 11 | }; |
| 12 | const program = |
| 13 | ts.createProgram([path.join(__dirname, 'node_modules', 'typescript', 'lib', 'lib.esnext.d.ts')], {noLib: true}); |
| 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) { |
| 18 | if (member.kind === ts.SyntaxKind.MethodSignature) |
| 19 | parseTSFunction(member, node); |
| 20 | } |
| 21 | } |
| 22 | if (node.kind === ts.SyntaxKind.FunctionDeclaration) |
| 23 | parseTSFunction(node, {name: {text: 'Window'}}); |
| 24 | |
| 25 | }); |
| 26 | } |
| 27 | |
| 28 | function parseTSFunction(func, node) { |
| 29 | if (!func.name.escapedText) |
| 30 | return; |
| 31 | |
| 32 | const args = func.parameters |
| 33 | .map(p => { |
| 34 | let text = p.name.escapedText; |
| 35 | if (p.questionToken) |
| 36 | text = '?' + text; |
| 37 | if (p.dotDotDotToken) |
| 38 | text = '...' + text; |
| 39 | return text; |
| 40 | }) |
| 41 | .filter(x => x !== 'this'); |
| 42 | storeMethod(node.name.text, func.name.escapedText, args); |
| 43 | } |
| 44 | |
| 45 | const files = glob('../../../+(core|modules)/**/*.idl', {cwd: __dirname}, function(er, files) { |
| 46 | for (const file of files) { |
| 47 | if (file.includes('testing')) |
| 48 | continue; |
| 49 | const data = fs.readFileSync(path.join(__dirname, file), 'utf8'); |
| 50 | const lines = data.split('\n'); |
| 51 | const newLines = []; |
| 52 | for (line of lines) { |
| 53 | if (!line.includes(' attribute ')) |
| 54 | newLines.push(line); |
| 55 | } |
| 56 | |
| 57 | try { |
| 58 | WebIDL2.parse(newLines.join('\n')).forEach(walk); |
| 59 | } catch (e) { |
| 60 | // console.error(file); |
| 61 | } |
| 62 | } |
| 63 | WebIDL2 |
| 64 | .parse(` |
| 65 | namespace console { |
| 66 | void assert(optional boolean condition = false, any... data); |
| 67 | void clear(); |
| 68 | void count(optional DOMString label = "default"); |
| 69 | void debug(any... data); |
| 70 | void dir(any item, optional object? options); |
| 71 | void dirxml(any... data); |
| 72 | void error(any... data); |
| 73 | void group(any... data); |
| 74 | void groupCollapsed(any... data); |
| 75 | void groupEnd(); |
| 76 | void info(any... data); |
| 77 | void log(any... data); |
| 78 | void profile(optional DOMString title); |
| 79 | void profileEnd(optional DOMString title); |
| 80 | void table(any... tabularData); |
| 81 | void time(optional DOMString label); |
| 82 | void timeEnd(optional DOMString label); |
| 83 | void timeStamp(optional DOMString name); |
| 84 | void trace(any... data); |
| 85 | void warn(any... data); |
| 86 | }; |
| 87 | `).forEach(walk); |
| 88 | postProcess(); |
| 89 | }); |
| 90 | |
| 91 | function walk(thing, parent) { |
| 92 | if (thing.type === 'interface') { |
| 93 | const constructor = thing.extAttrs.find(extAttr => extAttr.name === 'Constructor'); |
| 94 | if (constructor && constructor.arguments && thing.extAttrs.find(extAttr => extAttr.name === 'Exposed')) |
| 95 | storeMethod('Window', thing.name, constructor.arguments.map(argName)); |
| 96 | |
| 97 | const namedConstructor = thing.extAttrs.find(extAttr => extAttr.name === 'NamedConstructor'); |
| 98 | if (namedConstructor && namedConstructor.arguments) |
| 99 | storeMethod('Window', namedConstructor.rhs.value, namedConstructor.arguments.map(argName)); |
| 100 | } |
| 101 | if (thing.type.includes('operation')) { |
| 102 | storeMethod(thing.static ? (parent.name + 'Constructor') : parent.name, thing.name, thing.arguments.map(argName)); |
| 103 | return; |
| 104 | } |
| 105 | if (thing.members) { |
| 106 | for (const member of thing.members) |
| 107 | walk(member, thing); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | function argName(a) { |
| 112 | let name = a.name; |
| 113 | if (a.optional) |
| 114 | name = '?' + name; |
| 115 | if (a.variadic) |
| 116 | name = '...' + name; |
| 117 | return name; |
| 118 | } |
| 119 | |
| 120 | function storeMethod(parent, name, args) { |
| 121 | if (!methods[name]) |
| 122 | methods[name] = {__proto__: null}; |
| 123 | if (!methods[name][parent]) |
| 124 | methods[name][parent] = []; |
| 125 | methods[name][parent].push(args); |
| 126 | } |
| 127 | |
| 128 | function postProcess() { |
| 129 | for (const name in methods) { |
| 130 | const jsonParents = new Set(); |
| 131 | for (const parent in methods[name]) { |
| 132 | const signatures = methods[name][parent]; |
| 133 | signatures.sort((a, b) => a.length - b.length); |
| 134 | const filteredSignatures = []; |
| 135 | for (const signature of signatures) { |
| 136 | const smallerIndex = filteredSignatures.findIndex(smaller => startsThesame(smaller, signature)); |
| 137 | if (smallerIndex !== -1) { |
| 138 | filteredSignatures[smallerIndex] = (signature.map((arg, index) => { |
| 139 | if (index < filteredSignatures[smallerIndex].length) |
| 140 | return arg; |
| 141 | if (arg.startsWith('?') || arg.startsWith('...')) |
| 142 | return arg; |
| 143 | return '?' + arg; |
| 144 | })); |
| 145 | } else { |
| 146 | filteredSignatures.push(signature); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | function startsThesame(smaller, bigger) { |
| 151 | for (let i = 0; i < smaller.length; i++) { |
| 152 | if (smaller[i] !== bigger[i]) |
| 153 | return false; |
| 154 | } |
| 155 | return true; |
| 156 | } |
| 157 | |
| 158 | methods[name][parent] = filteredSignatures; |
| 159 | jsonParents.add(JSON.stringify(filteredSignatures)); |
| 160 | } |
| 161 | if (jsonParents.size === 1) { |
| 162 | methods[name] = {'*': JSON.parse(jsonParents.values().next().value)}; |
| 163 | } |
| 164 | for (const parent in methods[name]) { |
| 165 | const signatures = methods[name][parent]; |
| 166 | if (signatures.length === 1 && !signatures[0].length) |
| 167 | delete methods[name][parent]; |
| 168 | } |
| 169 | if (!Object.keys(methods[name]).length) |
| 170 | delete methods[name]; |
| 171 | } |
| 172 | const functions = []; |
| 173 | for (const name in methods) { |
| 174 | if (methods[name]['*']) { |
| 175 | functions.push({name, signatures: methods[name]['*']}); |
| 176 | } else { |
| 177 | for (const parent in methods[name]) { |
| 178 | if (parent.endsWith('Constructor')) |
| 179 | functions.push({name, signatures: methods[name][parent], static: true, receiver: constructor}); |
| 180 | else |
| 181 | functions.push({name, signatures: methods[name][parent], receiver: parent}); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | fs.writeFileSync( |
| 187 | path.join(__dirname, '..', '..', 'front_end', 'javascript_metadata', 'NativeFunctions.js'), |
| 188 | `// Generated from ${path.relative(path.join(__dirname, '..', '..'), __filename)} |
| 189 | JavaScriptMetadata.NativeFunctions = ${JSON.stringify(functions)};`); |
| 190 | } |