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; |