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: Rebase Created May 23, 2018, 4:18 a.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
@@ -148,8 +148,42 @@
}
}
}
return tree;
}
exports.parseScript = parseScript;
+
+/**
+ * Compiles a script against a given list of libraries into executable code
+ * @param {string} script
+ * @param {string[]} libraries
+ * @return {string}
+ */
+function compileScript(script, libraries)
+{
+ return `
+ "use strict";
+ {
+ const libraries = ${JSON.stringify(libraries)};
+
+ const script = ${JSON.stringify(parseScript(script))};
+
+ let imports = Object.create(null);
+ for (let library of libraries)
+ 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
+
+ for (let [name, ...args] of script)
+ {
+ 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.
+ {
+ let value = imports[name];
+ 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.
+ value(...args);
+ }
+ }
+ }
+ `;
+}
+
+exports.compileScript = compileScript;
« no previous file with comments | « no previous file | test/snippets.js » ('j') | test/snippets.js » ('J')

Powered by Google App Engine
This is Rietveld