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

Unified Diff: lib/snippets.js

Issue 29761612: Issue 6538, 6781 - Implement script compilation for snippets (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created April 25, 2018, 8:03 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | test/snippets.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/snippets.js
===================================================================
--- a/lib/snippets.js
+++ b/lib/snippets.js
@@ -124,12 +124,44 @@
{
tree.push(call);
call = [];
}
}
}
return tree;
+ },
+
+ /**
+ * Compiles a script against a given list of libraries into executable code
+ * @param {string} script
+ * @param {string[]} libraries
+ * @return {string}
+ */
+ compileScript(script, libraries)
+ {
+ return `
+ "use strict";
+ {
+ const libraries = ${JSON.stringify(libraries)};
+
+ const script = ${JSON.stringify(Snippets.parseScript(script))};
+
+ let imports = Object.create(null);
+ for (let library of libraries)
+ new Function("exports", library)(imports);
+
+ for (let [name, ...args] of script)
+ {
+ if (Object.prototype.hasOwnProperty.call(imports, name))
+ {
+ let value = imports[name];
+ if (typeof value == "function")
+ value(...args);
+ }
+ }
+ }
+ `;
}
};
exports.Snippets = Snippets;
« 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