 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: | 
| OLD | NEW | 
|---|---|
| 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 /* eslint no-new-func: "off" */ | |
| 
Manish Jethani
2018/04/26 13:46:00
ESLint won't let me use new Function(...) even tho
 | |
| 19 | |
| 18 "use strict"; | 20 "use strict"; | 
| 19 | 21 | 
| 20 const {createSandbox} = require("./_common"); | 22 const {createSandbox} = require("./_common"); | 
| 21 | 23 | 
| 22 let SnippetFilter = null; | 24 let SnippetFilter = null; | 
| 23 let Snippets = null; | 25 let Snippets = null; | 
| 24 let ElemHide = null; | 26 let ElemHide = null; | 
| 25 let Filter = null; | 27 let Filter = null; | 
| 26 | 28 | 
| 27 exports.setUp = function(callback) | 29 exports.setUp = function(callback) | 
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 183 "fo\\'\\ \\ \\\t\\\n\\;o 1 2 3; 'b a r' 1 2", | 185 "fo\\'\\ \\ \\\t\\\n\\;o 1 2 3; 'b a r' 1 2", | 
| 184 [["fo' \t\n;o", "1", "2", "3"], ["b a r", "1", "2"]]); | 186 [["fo' \t\n;o", "1", "2", "3"], ["b a r", "1", "2"]]); | 
| 185 checkParsedScript("Script containing Unicode composite characters", | 187 checkParsedScript("Script containing Unicode composite characters", | 
| 186 "f\ud83d\ude42\ud83d\ude42 b\ud83d\ude02r", | 188 "f\ud83d\ude42\ud83d\ude42 b\ud83d\ude02r", | 
| 187 [["f\ud83d\ude42\ud83d\ude42", "b\ud83d\ude02r"]]); | 189 [["f\ud83d\ude42\ud83d\ude42", "b\ud83d\ude02r"]]); | 
| 188 checkParsedScript("Script with no-op commands", "foo; ;;; ; ; bar 1", | 190 checkParsedScript("Script with no-op commands", "foo; ;;; ; ; bar 1", | 
| 189 [["foo"], ["bar", "1"]]); | 191 [["foo"], ["bar", "1"]]); | 
| 190 | 192 | 
| 191 test.done(); | 193 test.done(); | 
| 192 }; | 194 }; | 
| 195 | |
| 196 exports.testScriptCompilation = function(test) | |
| 197 { | |
| 198 let {compileScript, parseScript} = Snippets; | |
| 199 | |
| 200 let libraries = [ | |
| 201 ` | |
| 202 let foo = 0; | |
| 203 | |
| 204 exports.setFoo = function(value) | |
| 205 { | |
| 206 foo = value; | |
| 207 }; | |
| 208 | |
| 209 exports.assertFoo = function(expected) | |
| 210 { | |
| 211 if (foo != expected) | |
| 212 throw new Error("Value mismatch"); | |
| 213 }; | |
| 214 ` | |
| 215 ]; | |
| 216 | |
| 217 let template = ` | |
| 218 "use strict"; | |
| 219 { | |
| 220 const libraries = ${JSON.stringify(libraries)}; | |
| 221 | |
| 222 const script = {{{script}}}; | |
| 223 | |
| 224 let imports = Object.create(null); | |
| 225 for (let library of libraries) | |
| 226 new Function("exports", library)(imports); | |
| 227 | |
| 228 for (let [name, ...args] of script) | |
| 229 { | |
| 230 if (Object.prototype.hasOwnProperty.call(imports, name)) | |
| 231 { | |
| 232 let value = imports[name]; | |
| 233 if (typeof value == "function") | |
| 234 value(...args); | |
| 235 } | |
| 236 } | |
| 237 } | |
| 238 `; | |
| 239 | |
| 240 function verifyExecutable(script) | |
| 
Manish Jethani
2018/04/26 13:46:00
I don't know how valuable this is because "templat
 | |
| 241 { | |
| 242 let actual = compileScript(script, libraries); | |
| 243 let expected = template.replace("{{{script}}}", | |
| 244 JSON.stringify(parseScript(script))); | |
| 245 | |
| 246 // Trim surrounding spaces because of possible difference in indentation. | |
| 247 test.equal(expected.trim(), actual.trim()); | |
| 248 } | |
| 249 | |
| 250 verifyExecutable("hello 'How are you?'"); | |
| 251 | |
| 252 // Test script execution. | |
| 253 new Function(compileScript("setFoo 123; assertFoo 123", libraries))(); | |
| 254 | |
| 255 // Override setFoo in a second library, without overriding assertFoo. A | |
| 256 // couple of things to note here: (1) each library has its own variables; | |
| 257 // (2) script execution is stateless, i.e. the values are not retained | |
| 258 // between executions. In the example below, assertFoo does not find 456 but | |
| 259 // it doesn't find 123 either. It's the initial value 0. | |
| 260 new Function( | |
| 261 compileScript("setFoo 456; assertFoo 0", [ | |
| 262 ...libraries, "let foo = 1; exports.setFoo = value => { foo = value; };" | |
| 263 ]) | |
| 264 )(); | |
| 265 | |
| 266 test.done(); | |
| 267 }; | |
| OLD | NEW |