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: Clean up test Created April 25, 2018, 8:20 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 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 122
123 if (character == ";" && call.length > 0) 123 if (character == ";" && call.length > 0)
124 { 124 {
125 tree.push(call); 125 tree.push(call);
126 call = []; 126 call = [];
127 } 127 }
128 } 128 }
129 } 129 }
130 130
131 return tree; 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 `;
132 } 164 }
133 }; 165 };
134 166
135 exports.Snippets = Snippets; 167 exports.Snippets = Snippets;
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