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