diff --git a/package-lock.json b/package-lock.json index cbc91ff867..eba5b8fdb1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6315,6 +6315,11 @@ "integrity": "sha512-6Ey4Xy2xvmuQu7z7YQtMsaMV0EHJRpVxIDOd5GRrm04/I3nkTKIutELfECsLp6le+b3SSa3cXhPiw6PgqzxYWA==", "dev": true }, + "expr-eval": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/expr-eval/-/expr-eval-2.0.2.tgz", + "integrity": "sha512-4EMSHGOPSwAfBiibw3ndnP0AvjDWLsMvGOvWEZ2F96IGk0bIVdjQisOHxReSkE13mHcfbuCiXw+G4y0zv6N8Eg==" + }, "ext-list": { "version": "2.2.2", "resolved": "https://registry.npmjs.org/ext-list/-/ext-list-2.2.2.tgz", diff --git a/package.json b/package.json index 83ee009058..25693ff8da 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "data-uri-to-buffer": "^3.0.0", "downloadjs": "^1.4.7", "eslint": "^6.1.0", + "expr-eval": "^2.0.2", "fisheyegl": "^0.1.2", "font-awesome": "~4.7.0", "geotiff": "^1.0.0-beta.6", diff --git a/src/modules/Dynamic/Module.js b/src/modules/Dynamic/Module.js index 9234f790a6..3e78196b05 100644 --- a/src/modules/Dynamic/Module.js +++ b/src/modules/Dynamic/Module.js @@ -14,20 +14,18 @@ module.exports = function Dynamic(options, UI) { options.blue = options.blue || defaults.blue; options.green = options.green || defaults.green; + const Parser = require('expr-eval').Parser; function generator(expression) { - var func = 'f = function (r, g, b, a) { var R = r, G = g, B = b, A = a; return ' + expression + ';}'; - var f; - eval(func); - return f; + let expr = Parser.parse('R = r; G = g; B = b; A = a; ' + expression); + return expr.toJSFunction("r,g,b,a,R,G,B,A"); } - + var channels = ['red', 'green', 'blue', 'alpha']; channels.forEach(function(channel) { if (channel === 'alpha'){ options['alpha_function'] = function() { return 255; }; - } - else{ + } else { options[channel + '_function'] = generator(options[channel]); } }); diff --git a/test/core/templates/options-test.js b/test/core/templates/options-test.js index c1f72d98d0..548a21e831 100644 --- a/test/core/templates/options-test.js +++ b/test/core/templates/options-test.js @@ -37,7 +37,7 @@ module.exports = (moduleName, options, benchmark, input) => { looksSame(result, benchmark[0], function(err, res) { if (err) console.log(err); - t.equal(res.equal, true, `${moduleName} module works correctly with initial option ${options[0][moduleName]}`); + t.equal(res.equal, true, `${moduleName} module works correctly with initial option ${JSON.stringify(options[0])}`); }); // Change the option of the given module. sequencer.steps[1].setOptions(options[1]); @@ -54,7 +54,7 @@ module.exports = (moduleName, options, benchmark, input) => { looksSame(newResult, benchmark[1], function(err, res) { if (err) console.log(err); - t.equal(res.equal, true, `${moduleName} module works correctly when the option is changed to ${options[1][moduleName]}`); + t.equal(res.equal, true, `${moduleName} module works correctly when the option is changed to ${JSON.stringify(options[1])}`); sequencer = null; t.end(); });