 Issue 29761612:
  Issue 6538, 6781 - Implement script compilation for snippets  (Closed) 
  Base URL: https://hg.adblockplus.org/adblockpluscore/
    
  
    Issue 29761612:
  Issue 6538, 6781 - Implement script compilation for snippets  (Closed) 
  Base URL: https://hg.adblockplus.org/adblockpluscore/| Left: | ||
| Right: | 
| LEFT | RIGHT | 
|---|---|
| 1 /* | 1 /* | 
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 
| 3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH | 
| 4 * | 4 * | 
| 5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify | 
| 6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as | 
| 7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. | 
| 8 * | 8 * | 
| 9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, | 
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
| 12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. | 
| 13 * | 13 * | 
| 14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License | 
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 
| 16 */ | 16 */ | 
| 17 | 17 | 
| 18 "use strict"; | 18 "use strict"; | 
| 19 | 19 | 
| 20 /** | 20 /** | 
| 21 * @fileOverview Snippets implementation. | 21 * @fileOverview Snippets implementation. | 
| 22 */ | 22 */ | 
| 23 | 23 | 
| 24 const {ElemHide} = require("./elemHide"); | |
| 25 const {Filter} = require("./filterClasses"); | 24 const {Filter} = require("./filterClasses"); | 
| 26 | 25 | 
| 27 let filters = new Set(); | 26 let filters = new Set(); | 
| 28 | 27 | 
| 29 /** | 28 /** | 
| 30 * Container for snippet filters | 29 * Container for snippet filters | 
| 31 * @class | 30 * @class | 
| 32 */ | 31 */ | 
| 33 let Snippets = { | 32 let Snippets = { | 
| 34 /** | 33 /** | 
| (...skipping 18 matching lines...) Expand all Loading... | |
| 53 * @param {SnippetFilter} filter | 52 * @param {SnippetFilter} filter | 
| 54 */ | 53 */ | 
| 55 remove(filter) | 54 remove(filter) | 
| 56 { | 55 { | 
| 57 filters.delete(filter.text); | 56 filters.delete(filter.text); | 
| 58 }, | 57 }, | 
| 59 | 58 | 
| 60 /** | 59 /** | 
| 61 * Returns a list of all scripts active on a particular domain | 60 * Returns a list of all scripts active on a particular domain | 
| 62 * @param {string} domain | 61 * @param {string} domain | 
| 63 * @return {SnippetFilter[]} | 62 * @return {string[]} | 
| 64 */ | 63 */ | 
| 65 getScriptsForDomain(domain) | 64 getScriptsForDomain(domain) | 
| 66 { | 65 { | 
| 67 let result = []; | 66 let result = []; | 
| 68 for (let text of filters) | 67 for (let text of filters) | 
| 69 { | 68 { | 
| 70 let filter = Filter.fromText(text); | 69 let filter = Filter.fromText(text); | 
| 71 if (filter.isActiveOnDomain(domain) && | 70 if (filter.isActiveOnDomain(domain)) | 
| 72 !ElemHide.getException(filter, domain)) | 71 result.push(filter.script); | 
| 73 { | |
| 74 result.push(filter); | |
| 75 } | |
| 76 } | 72 } | 
| 77 return result; | 73 return result; | 
| 78 }, | |
| 79 | |
| 80 /** | |
| 81 * Parses a script and returns a list of all its commands and their arguments | |
| 82 * @param {string} script | |
| 83 * @return {Array.<string[]>} | |
| 84 */ | |
| 85 parseScript(script) | |
| 86 { | |
| 87 let tree = []; | |
| 88 | |
| 89 let escape = false; | |
| 90 let literal = false; | |
| 91 | |
| 92 let call = []; | |
| 93 let argument = ""; | |
| 94 | |
| 95 for (let character of [...script.trim(), ";"]) | |
| 96 { | |
| 97 if (escape) | |
| 98 { | |
| 99 escape = false; | |
| 100 | |
| 101 argument += character; | |
| 102 } | |
| 103 else if (character == "\\") | |
| 104 { | |
| 105 escape = true; | |
| 106 } | |
| 107 else if (character == "'") | |
| 108 { | |
| 109 literal = !literal; | |
| 110 } | |
| 111 else if (literal || character != ";" && !/\s/u.test(character)) | |
| 112 { | |
| 113 argument += character; | |
| 114 } | |
| 115 else | |
| 116 { | |
| 117 if (argument) | |
| 118 { | |
| 119 call.push(argument); | |
| 120 argument = ""; | |
| 121 } | |
| 122 | |
| 123 if (character == ";" && call.length > 0) | |
| 124 { | |
| 125 tree.push(call); | |
| 126 call = []; | |
| 127 } | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 return tree; | |
| 132 }, | |
| 133 | |
| 134 /** | |
| 135 * Compiles a script against a given list of libraries into executable code | |
| 136 * @param {string} script | |
| 137 * @param {string[]} libraries | |
| 138 * @return {string} | |
| 139 */ | |
| 140 compileScript(script, libraries) | |
| 141 { | |
| 142 return ` | |
| 143 "use strict"; | |
| 144 { | |
| 145 const libraries = ${JSON.stringify(libraries)}; | |
| 146 | |
| 147 const script = ${JSON.stringify(Snippets.parseScript(script))}; | |
| 148 | |
| 149 let imports = Object.create(null); | |
| 150 for (let library of libraries) | |
| 151 new Function("exports", library)(imports); | |
| 152 | |
| 153 for (let [name, ...args] of script) | |
| 154 { | |
| 155 if (Object.prototype.hasOwnProperty.call(imports, name)) | |
| 156 { | |
| 157 let value = imports[name]; | |
| 158 if (typeof value == "function") | |
| 159 value(...args); | |
| 160 } | |
| 161 } | |
| 162 } | |
| 163 `; | |
| 164 } | 74 } | 
| 165 }; | 75 }; | 
| 166 | 76 | 
| 167 exports.Snippets = Snippets; | 77 exports.Snippets = Snippets; | 
| 78 | |
| 79 /** | |
| 80 * Parses a script and returns a list of all its commands and their arguments | |
| 81 * @param {string} script | |
| 82 * @return {Array.<string[]>} | |
| 83 */ | |
| 84 function parseScript(script) | |
| 85 { | |
| 86 const singleCharacterEscapes = new Map([ | |
| 87 ["n", "\n"], ["r", "\r"], ["t", "\t"] | |
| 88 ]); | |
| 89 | |
| 90 let tree = []; | |
| 91 | |
| 92 let escape = false; | |
| 93 let literal = false; | |
| 94 | |
| 95 let unicodeEscape = null; | |
| 96 | |
| 97 let call = []; | |
| 98 let argument = ""; | |
| 99 | |
| 100 for (let character of [...script.trim(), ";"]) | |
| 101 { | |
| 102 if (unicodeEscape != null) | |
| 103 { | |
| 104 unicodeEscape += character; | |
| 105 | |
| 106 if (unicodeEscape.length == 4) | |
| 107 { | |
| 108 let codePoint = parseInt(unicodeEscape, 16); | |
| 109 if (!isNaN(codePoint)) | |
| 110 argument += String.fromCodePoint(codePoint); | |
| 111 | |
| 112 unicodeEscape = null; | |
| 113 } | |
| 114 } | |
| 115 else if (escape) | |
| 116 { | |
| 117 escape = false; | |
| 118 | |
| 119 if (character == "u") | |
| 120 unicodeEscape = ""; | |
| 121 else | |
| 122 argument += singleCharacterEscapes.get(character) || character; | |
| 123 } | |
| 124 else if (character == "\\") | |
| 125 { | |
| 126 escape = true; | |
| 127 } | |
| 128 else if (character == "'") | |
| 129 { | |
| 130 literal = !literal; | |
| 131 } | |
| 132 else if (literal || character != ";" && !/\s/u.test(character)) | |
| 133 { | |
| 134 argument += character; | |
| 135 } | |
| 136 else | |
| 137 { | |
| 138 if (argument) | |
| 139 { | |
| 140 call.push(argument); | |
| 141 argument = ""; | |
| 142 } | |
| 143 | |
| 144 if (character == ";" && call.length > 0) | |
| 145 { | |
| 146 tree.push(call); | |
| 147 call = []; | |
| 148 } | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 return tree; | |
| 153 } | |
| 154 | |
| 155 exports.parseScript = parseScript; | |
| 156 | |
| 157 /** | |
| 158 * Compiles a script against a given list of libraries into executable code | |
| 159 * @param {string} script | |
| 160 * @param {string[]} libraries | |
| 161 * @return {string} | |
| 162 */ | |
| 163 function compileScript(script, libraries) | |
| 164 { | |
| 165 return ` | |
| 166 "use strict"; | |
| 167 { | |
| 168 const libraries = ${JSON.stringify(libraries)}; | |
| 169 | |
| 170 const script = ${JSON.stringify(parseScript(script))}; | |
| 171 | |
| 172 let imports = Object.create(null); | |
| 173 for (let library of libraries) | |
| 174 new Function("exports", library)(imports); | |
| 
kzar
2018/07/10 14:35:15
I wonder why we take include all libraries, instea
 
kzar
2018/07/10 14:35:15
If I got it right, `compileScript` has a string re
 
Manish Jethani
2018/07/12 09:48:31
The idea is that there will be a local library, wh
 
Manish Jethani
2018/07/12 09:48:32
libraries is an array of strings. The only way to
 
kzar
2018/07/12 10:21:43
I still don't see the point of stringifying it, ju
 
Manish Jethani
2018/07/12 11:05:29
The JSON.stringify is what converts your first sni
 
kzar
2018/07/12 11:19:55
It's really not, but nevermind.
 
Manish Jethani
2018/07/12 11:35:16
OK, perhaps I am misunderstanding. If you can rewr
 
Sebastian Noack
2018/07/16 19:14:53
So we are generating code that generates code just
 
Manish Jethani
2018/07/17 19:57:15
The library looks like this:
https://github.com/a
 | |
| 175 | |
| 176 for (let [name, ...args] of script) | |
| 177 { | |
| 178 if (Object.prototype.hasOwnProperty.call(imports, name)) | |
| 
kzar
2018/07/10 14:35:15
Why not `name in imports`, you created the `import
 
Manish Jethani
2018/07/12 09:48:31
It's just safer this way. Right now it's a null pr
 
kzar
2018/07/12 10:21:43
Alright, I disagree but won't insist.
 
Manish Jethani
2018/07/12 11:05:29
Acknowledged.
 | |
| 179 { | |
| 180 let value = imports[name]; | |
| 181 if (typeof value == "function") | |
| 
kzar
2018/07/10 14:35:15
Could this check ever fail?
 
Manish Jethani
2018/07/12 09:48:31
Yes, a library could export a "constant" for whate
 
kzar
2018/07/12 10:21:43
Acknowledged.
 | |
| 182 value(...args); | |
| 183 } | |
| 184 } | |
| 185 } | |
| 186 `; | |
| 187 } | |
| 188 | |
| 189 exports.compileScript = compileScript; | |
| LEFT | RIGHT |