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: Test script execution as well Created April 25, 2018, 9:07 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') | test/snippets.js » ('J')
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);
Manish Jethani 2018/04/26 13:46:00 There's an imports object and it gets populated by
+ 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];
Manish Jethani 2018/04/26 13:46:00 Note that the function must exist directly on the
+ if (typeof value == "function")
+ value(...args);
Manish Jethani 2018/04/26 13:46:00 Also note that the function is called without a "t
+ }
+ }
+ }
+ `;
}
};
exports.Snippets = Snippets;
« no previous file with comments | « no previous file | test/snippets.js » ('j') | test/snippets.js » ('J')

Powered by Google App Engine
This is Rietveld