Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/snippets.js

Issue 29761612: Issue 6538, 6781 - Implement script compilation for snippets (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Rebase Created May 23, 2018, 4:18 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | test/snippets.js » ('j') | test/snippets.js » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 tree.push(call); 146 tree.push(call);
147 call = []; 147 call = [];
148 } 148 }
149 } 149 }
150 } 150 }
151 151
152 return tree; 152 return tree;
153 } 153 }
154 154
155 exports.parseScript = parseScript; 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;
OLDNEW
« no previous file with comments | « no previous file | test/snippets.js » ('j') | test/snippets.js » ('J')

Powered by Google App Engine
This is Rietveld