forked from browserify/static-eval
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
James Halliday
committed
Jul 21, 2013
1 parent
71e0b9e
commit 77bce8f
Showing
5 changed files
with
122 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,7 @@ | ||
var evaluate = require('../'); | ||
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 src = process.argv.slice(2).join(' '); | ||
var ast = parse(src).body[0].expression; | ||
|
||
console.log(evaluate(ast, { | ||
n: 6, | ||
foo: function (x) { return x * 100 }, | ||
obj: { x: { y: 555 } } | ||
})); | ||
console.log(evaluate(ast)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
var 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 } } | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# static-eval | ||
|
||
evaluate statically-analyzable expressions | ||
|
||
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
var 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,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.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); | ||
}); |