11var unparse = require ( 'escodegen' ) . generate ;
22
3- module . exports = function ( ast , vars ) {
3+ module . exports = function ( ast , vars , opts ) {
4+ if ( ! opts ) opts = { } ;
5+ var rejectAccessToMethodsOnFunctions = ! opts . allowAccessToMethodsOnFunctions ;
6+
47 if ( ! vars ) vars = { } ;
58 var FAIL = { } ;
6-
9+
710 var result = ( function walk ( node , noExecute ) {
811 if ( node . type === 'Literal' ) {
912 return node . value ;
@@ -63,7 +66,7 @@ module.exports = function (ast, vars) {
6366 if ( l === FAIL ) return FAIL ;
6467 var r = walk ( node . right , noExecute ) ;
6568 if ( r === FAIL ) return FAIL ;
66-
69+
6770 if ( op === '==' ) return l == r ;
6871 if ( op === '===' ) return l === r ;
6972 if ( op === '!=' ) return l != r ;
@@ -80,7 +83,7 @@ module.exports = function (ast, vars) {
8083 if ( op === '|' ) return l | r ;
8184 if ( op === '&' ) return l & r ;
8285 if ( op === '^' ) return l ^ r ;
83-
86+
8487 return FAIL ;
8588 }
8689 else if ( node . type === 'Identifier' ) {
@@ -100,7 +103,7 @@ module.exports = function (ast, vars) {
100103 if ( callee === FAIL ) return FAIL ;
101104 if ( typeof callee !== 'function' ) return FAIL ;
102105
103-
106+
104107 var ctx = node . callee . object ? walk ( node . callee . object , noExecute ) : FAIL ;
105108 if ( ctx === FAIL ) ctx = null ;
106109
@@ -119,8 +122,9 @@ module.exports = function (ast, vars) {
119122 }
120123 else if ( node . type === 'MemberExpression' ) {
121124 var obj = walk ( node . object , noExecute ) ;
122- // do not allow access to methods on Function
123- if ( ( obj === FAIL ) || ( typeof obj == 'function' ) ) {
125+ if ( ( obj === FAIL ) || (
126+ ( typeof obj == 'function' ) && rejectAccessToMethodsOnFunctions
127+ ) ) {
124128 return FAIL ;
125129 }
126130 if ( node . property . type === 'Identifier' && ! node . computed ) {
@@ -147,7 +151,7 @@ module.exports = function (ast, vars) {
147151 }
148152 else if ( node . type === 'FunctionExpression' ) {
149153 var bodies = node . body . body ;
150-
154+
151155 // Create a "scope" for our arguments
152156 var oldVars = { } ;
153157 Object . keys ( vars ) . forEach ( function ( element ) {
@@ -168,7 +172,7 @@ module.exports = function (ast, vars) {
168172 }
169173 // restore the vars and scope after we walk
170174 vars = oldVars ;
171-
175+
172176 var keys = Object . keys ( vars ) ;
173177 var vals = keys . map ( function ( key ) {
174178 return vars [ key ] ;
@@ -196,7 +200,7 @@ module.exports = function (ast, vars) {
196200 }
197201 else return FAIL ;
198202 } ) ( ast ) ;
199-
203+
200204 return result === FAIL ? undefined : result ;
201205} ;
202206
0 commit comments