pax_global_header00006660000000000000000000000064127727640000014517gustar00rootroot0000000000000052 comment=ab81134a43431457508bc2ead500f2d93ac0a71d static-eval-1.1.1/000077500000000000000000000000001277276400000137335ustar00rootroot00000000000000static-eval-1.1.1/.travis.yml000066400000000000000000000000601277276400000160400ustar00rootroot00000000000000language: node_js node_js: - "0.8" - "0.10" static-eval-1.1.1/LICENSE000066400000000000000000000020611277276400000147370ustar00rootroot00000000000000This software is released under the MIT license: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. static-eval-1.1.1/example/000077500000000000000000000000001277276400000153665ustar00rootroot00000000000000static-eval-1.1.1/example/eval.js000066400000000000000000000002771277276400000166610ustar00rootroot00000000000000var evaluate = require('static-eval'); var parse = require('esprima').parse; var src = process.argv.slice(2).join(' '); var ast = parse(src).body[0].expression; console.log(evaluate(ast)); static-eval-1.1.1/example/vars.js000066400000000000000000000004231277276400000166760ustar00rootroot00000000000000var evaluate = require('../'); var parse = require('esprima').parse; var src = '[1,2,3+4*10+n,foo(3+5),obj[""+"x"].y]'; var ast = parse(src).body[0].expression; console.log(evaluate(ast, { n: 6, foo: function (x) { return x * 100 }, obj: { x: { y: 555 } } })); static-eval-1.1.1/index.js000066400000000000000000000122301277276400000153760ustar00rootroot00000000000000var unparse = require('escodegen').generate; module.exports = function (ast, vars) { if (!vars) vars = {}; var FAIL = {}; var result = (function walk (node) { if (node.type === 'Literal') { return node.value; } else if (node.type === 'UnaryExpression'){ var val = walk(node.argument) if (node.operator === '+') return +val if (node.operator === '-') return -val if (node.operator === '~') return ~val if (node.operator === '!') return !val return FAIL } else if (node.type === 'ArrayExpression') { var xs = []; for (var i = 0, l = node.elements.length; i < l; i++) { var x = walk(node.elements[i]); if (x === FAIL) return FAIL; xs.push(x); } return xs; } else if (node.type === 'ObjectExpression') { var obj = {}; for (var i = 0; i < node.properties.length; i++) { var prop = node.properties[i]; var value = prop.value === null ? prop.value : walk(prop.value) ; if (value === FAIL) return FAIL; obj[prop.key.value || prop.key.name] = value; } return obj; } else if (node.type === 'BinaryExpression' || node.type === 'LogicalExpression') { var l = walk(node.left); if (l === FAIL) return FAIL; var r = walk(node.right); if (r === FAIL) return FAIL; var op = node.operator; if (op === '==') return l == r; if (op === '===') return l === r; if (op === '!=') return l != r; if (op === '!==') return l !== r; if (op === '+') return l + r; if (op === '-') return l - r; if (op === '*') return l * r; if (op === '/') return l / r; if (op === '%') return l % r; if (op === '<') return l < r; if (op === '<=') return l <= r; if (op === '>') return l > r; if (op === '>=') return l >= r; if (op === '|') return l | r; if (op === '&') return l & r; if (op === '^') return l ^ r; if (op === '&&') return l && r; if (op === '||') return l || r; return FAIL; } else if (node.type === 'Identifier') { if ({}.hasOwnProperty.call(vars, node.name)) { return vars[node.name]; } else return FAIL; } else if (node.type === 'ThisExpression') { if ({}.hasOwnProperty.call(vars, 'this')) { return vars['this']; } else return FAIL; } else if (node.type === 'CallExpression') { var callee = walk(node.callee); if (callee === FAIL) return FAIL; if (typeof callee !== 'function') return FAIL; var ctx = node.callee.object ? walk(node.callee.object) : FAIL; if (ctx === FAIL) ctx = null; var args = []; for (var i = 0, l = node.arguments.length; i < l; i++) { var x = walk(node.arguments[i]); if (x === FAIL) return FAIL; args.push(x); } return callee.apply(ctx, args); } else if (node.type === 'MemberExpression') { var obj = walk(node.object); if (obj === FAIL) return FAIL; if (node.property.type === 'Identifier') { return obj[node.property.name]; } var prop = walk(node.property); if (prop === FAIL) return FAIL; return obj[prop]; } else if (node.type === 'ConditionalExpression') { var val = walk(node.test) if (val === FAIL) return FAIL; return val ? walk(node.consequent) : walk(node.alternate) } else if (node.type === 'FunctionExpression') { var keys = Object.keys(vars); var vals = keys.map(function(key) { return vars[key]; }); return Function(keys.join(', '), 'return ' + unparse(node)).apply(null, vals); } else if (node.type === 'TemplateLiteral') { var str = ''; for (var i = 0; i < node.expressions.length; i++) { str += walk(node.quasis[i]); str += walk(node.expressions[i]); } str += walk(node.quasis[i]); return str; } else if (node.type === 'TaggedTemplateExpression') { var tag = walk(node.tag); var quasi = node.quasi; var strings = quasi.quasis.map(walk); var values = quasi.expressions.map(walk); return tag.apply(null, [strings].concat(values)); } else if (node.type === 'TemplateElement') { return node.value.cooked; } else return FAIL; })(ast); return result === FAIL ? undefined : result; }; static-eval-1.1.1/package.json000066400000000000000000000016361277276400000162270ustar00rootroot00000000000000{ "name": "static-eval", "version": "1.1.1", "description": "evaluate statically-analyzable expressions", "main": "index.js", "dependencies": { "escodegen": "^1.8.1" }, "devDependencies": { "esprima": "^2.7.3", "tape": "^4.6.0" }, "scripts": { "test": "tape test/*.js" }, "testling": { "files": "test/*.js", "browsers": [ "ie/8..latest", "ff/latest", "chrome/latest", "opera/latest", "safari/latest" ] }, "repository": { "type": "git", "url": "git://github.com/substack/static-eval.git" }, "homepage": "https://github.com/substack/static-eval", "keywords": [ "static", "eval", "expression", "esprima", "ast", "abstract", "syntax", "tree", "analysis" ], "author": { "name": "James Halliday", "email": "mail@substack.net", "url": "http://substack.net" }, "license": "MIT" } static-eval-1.1.1/readme.markdown000066400000000000000000000033201277276400000167320ustar00rootroot00000000000000# static-eval evaluate statically-analyzable expressions [![testling badge](https://ci.testling.com/substack/static-eval.png)](https://ci.testling.com/substack/static-eval) [![build status](https://secure.travis-ci.org/substack/static-eval.png)](http://travis-ci.org/substack/static-eval) # example ``` js var evaluate = require('static-eval'); var parse = require('esprima').parse; var src = process.argv[2]; var ast = parse(src).body[0].expression; console.log(evaluate(ast)); ``` If you stick to simple expressions, the result is statically analyzable: ``` $ node '7*8+9' 65 $ node eval.js '[1,2,3+4*5-(5*11)]' [ 1, 2, -32 ] ``` but if you use statements, undeclared identifiers, or syntax, the result is no longer statically analyzable and `evaluate()` returns `undefined`: ``` $ node eval.js '1+2+3*n' undefined $ node eval.js 'x=5; x*2' undefined $ node eval.js '5-4*3' -7 ``` You can also declare variables and functions to use in the static evaluation: ``` js var evaluate = require('static-eval'); var parse = require('esprima').parse; var src = '[1,2,3+4*10+n,foo(3+5),obj[""+"x"].y]'; var ast = parse(src).body[0].expression; console.log(evaluate(ast, { n: 6, foo: function (x) { return x * 100 }, obj: { x: { y: 555 } } })); ``` # methods ``` js var evaluate = require('static-eval'); ``` ## evaluate(ast, vars={}) Evaluate the [esprima](https://npmjs.org/package/esprima)-parsed abstract syntax tree object `ast` with an optional collection of variables `vars` to use in the static expression resolution. If the expression contained in `ast` can't be statically resolved, `evaluate()` returns undefined. # install With [npm](https://npmjs.org) do: ``` npm install static-eval ``` # license MIT static-eval-1.1.1/test/000077500000000000000000000000001277276400000147125ustar00rootroot00000000000000static-eval-1.1.1/test/eval.js000066400000000000000000000031601277276400000161770ustar00rootroot00000000000000var test = require('tape'); var evaluate = require('../'); var parse = require('esprima').parse; test('resolved', function (t) { t.plan(1); var src = '[1,2,3+4*10+(n||6),foo(3+5),obj[""+"x"].y]'; var ast = parse(src).body[0].expression; var res = evaluate(ast, { n: false, foo: function (x) { return x * 100 }, obj: { x: { y: 555 } } }); t.deepEqual(res, [ 1, 2, 49, 800, 555 ]); }); test('unresolved', function (t) { t.plan(1); var src = '[1,2,3+4*10*z+n,foo(3+5),obj[""+"x"].y]'; var ast = parse(src).body[0].expression; var res = evaluate(ast, { n: 6, foo: function (x) { return x * 100 }, obj: { x: { y: 555 } } }); t.equal(res, undefined); }); test('boolean', function (t) { t.plan(1); var src = '[ 1===2+3-16/4, [2]==2, [2]!==2, [2]!==[2] ]'; var ast = parse(src).body[0].expression; t.deepEqual(evaluate(ast), [ true, true, true, true ]); }); test('array methods', function(t) { t.plan(1); var src = '[1, 2, 3].map(function(n) { return n * 2 })'; var ast = parse(src).body[0].expression; t.deepEqual(evaluate(ast), [2, 4, 6]); }); test('array methods with vars', function(t) { t.plan(1); var src = '[1, 2, 3].map(function(n) { return n * x })'; var ast = parse(src).body[0].expression; t.deepEqual(evaluate(ast, {x: 2}), [2, 4, 6]); }); test('evaluate this', function(t) { t.plan(1); var src = 'this.x + this.y.z'; var ast = parse(src).body[0].expression; var res = evaluate(ast, { 'this': { x: 1, y: { z: 100 } } }); t.equal(res, 101); }); static-eval-1.1.1/test/prop.js000066400000000000000000000007071277276400000162340ustar00rootroot00000000000000var test = require('tape'); var evaluate = require('../'); var parse = require('esprima').parse; test('function property', function (t) { t.plan(1); var src = '[1,2,3+4*10+n,beep.boop(3+5),obj[""+"x"].y]'; var ast = parse(src).body[0].expression; var res = evaluate(ast, { n: 6, beep: { boop: function (x) { return x * 100 } }, obj: { x: { y: 555 } } }); t.deepEqual(res, [ 1, 2, 49, 800, 555 ]); }); static-eval-1.1.1/test/template-strings.js000066400000000000000000000014621277276400000205550ustar00rootroot00000000000000var test = require('tape'); var evaluate = require('../'); var parse = require('esprima').parse; test('untagged template strings', function (t) { t.plan(1); var src = '`${1},${2 + n},${`4,5`}`'; var ast = parse(src).body[0].expression; var res = evaluate(ast, { n: 6 }); t.deepEqual(res, '1,8,4,5'); }); test('tagged template strings', function (t) { t.plan(3); var src = 'template`${1},${2 + n},${`4,5`}`'; var ast = parse(src).body[0].expression; var res = evaluate(ast, { template: function (strings) { t.deepEqual(strings, ['', ',', ',', '']); var values = [].slice.call(arguments, 1); t.deepEqual(values, [1, 8, '4,5']); return 'foo'; }, n: 6 }); t.deepEqual(res, 'foo'); })