Index: lib/snippets.js |
=================================================================== |
--- a/lib/snippets.js |
+++ b/lib/snippets.js |
@@ -130,8 +130,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); |
+ |
+ for (let [name, ...args] of script) |
+ { |
+ if (Object.prototype.hasOwnProperty.call(imports, name)) |
+ { |
+ let value = imports[name]; |
+ if (typeof value == "function") |
+ value(...args); |
+ } |
+ } |
+ } |
+ `; |
+} |
+ |
+exports.compileScript = compileScript; |