55 * All variables within this function are namespaced in the arguments array because every
66 * var declaration could possibly clash with a variable in the module scope.
77 *
8- * @param {! String|! Object } varName name of the variable to set
8+ * @param {String|Object } varName name of the variable to set
99 * @param {String } varValue new value
1010 * @throws {TypeError }
1111 * @throws {ReferenceError } When the variable is unknown
1212 * @return {* }
1313 */
14-
1514function __set__ ( ) {
1615 arguments . varName = arguments [ 0 ] ;
1716 arguments . varValue = arguments [ 1 ] ;
1817 arguments . src = "" ;
19- var snapshot = { } ;
18+ arguments . snapshot = { } ;
2019
2120 if ( typeof arguments [ 0 ] === "object" && arguments . length === 1 ) {
2221 arguments . env = arguments . varName ;
@@ -27,40 +26,44 @@ function __set__() {
2726 if ( arguments . env . hasOwnProperty ( arguments . varName ) ) {
2827 arguments . varValue = arguments . env [ arguments . varName ] ;
2928 arguments . src += arguments . varName + " = arguments.env." + arguments . varName + "; " ;
30- snapshot [ arguments . varName ] = eval ( arguments . varName ) ;
29+ arguments . snapshot [ arguments . varName ] = eval ( arguments . varName ) ;
3130 }
3231 }
3332 } else if ( typeof arguments . varName === "string" && arguments . length === 2 ) {
3433 if ( ! arguments . varName ) {
3534 throw new TypeError ( "__set__ expects a non-empty string as a variable name" ) ;
3635 }
3736 arguments . src = arguments . varName + " = arguments.varValue;" ;
38- snapshot [ arguments . varName ] = eval ( arguments . varName ) ;
37+ arguments . snapshot [ arguments . varName ] = eval ( arguments . varName ) ;
3938 } else {
4039 throw new TypeError ( "__set__ expects an environment object or a non-empty string as a variable name" ) ;
4140 }
4241
4342 eval ( arguments . src ) ;
44- return function ( ) {
45- __set__ ( snapshot ) ;
46- } ;
43+
44+ return function ( snapshot ) {
45+ __set__ ( snapshot ) ;
46+ } . bind ( null , arguments . snapshot ) ;
4747}
4848
4949function __with__ ( ) {
50- var args = arguments ;
51- return function ( callback ) {
52- if ( typeof callback !== "function" ) {
53- throw new TypeError ( "__with__ expects a callback function" )
54- }
50+ var args = arguments ;
5551
56- var undo = __set__ . apply ( null , args )
57- try {
58- callback ( ) ;
59- }
60- finally {
61- undo ( ) ;
52+ return function ( callback ) {
53+ var undo ;
54+
55+ if ( typeof callback !== "function" ) {
56+ throw new TypeError ( "__with__ expects a callback function" )
57+ }
58+
59+ undo = __set__ . apply ( null , args ) ;
60+
61+ try {
62+ callback ( ) ;
63+ } finally {
64+ undo ( ) ;
65+ }
6266 }
63- }
6467}
6568
66- module . exports = { "__set__" : __set__ , "__with__" : __with__ }
69+ module . exports = { "__set__" : __set__ , "__with__" : __with__ } ;
0 commit comments