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: Move compileScript to top level Created April 26, 2018, 2:34 p.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') | no next file with comments »
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 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 tree.push(call); 128 tree.push(call);
129 call = []; 129 call = [];
130 } 130 }
131 } 131 }
132 } 132 }
133 133
134 return tree; 134 return tree;
135 } 135 }
136 136
137 exports.parseScript = parseScript; 137 exports.parseScript = parseScript;
138
139 /**
140 * Compiles a script against a given list of libraries into executable code
141 * @param {string} script
142 * @param {string[]} libraries
143 * @return {string}
144 */
145 function compileScript(script, libraries)
146 {
147 return `
148 "use strict";
149 {
150 const libraries = ${JSON.stringify(libraries)};
151
152 const script = ${JSON.stringify(parseScript(script))};
153
154 let imports = Object.create(null);
155 for (let library of libraries)
156 new Function("exports", library)(imports);
157
158 for (let [name, ...args] of script)
159 {
160 if (Object.prototype.hasOwnProperty.call(imports, name))
161 {
162 let value = imports[name];
163 if (typeof value == "function")
164 value(...args);
165 }
166 }
167 }
168 `;
169 }
170
171 exports.compileScript = compileScript;
OLDNEW
« no previous file with comments | « no previous file | test/snippets.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld