44 * This function will be stringified and then injected into every rewired module.
55 * Then you can set private variables by calling myModule.__set__("myPrivateVar", newValue);
66 *
7+ * All variables within this function are namespaced in the arguments array because every
8+ * var declaration could possibly clash with a variable in the module scope.
9+ *
710 * @param {!String|!Object } varName name of the variable to set
811 * @param {String } varValue new value
912 * @throws {TypeError }
1013 * @return {* }
1114 */
12- module . exports = function __set__ ( varName , varValue ) {
13- var key ,
14- env ,
15- src = "" ;
16-
17- function checkExistsSrc ( varName ) {
15+ module . exports = function __set__ ( ) {
16+ arguments . varName = arguments [ 0 ] ;
17+ arguments . varValue = arguments [ 1 ] ;
18+ arguments . src = "" ;
19+ arguments . checkExistsSrc = function ( varName ) {
1820 return "if (typeof " + varName + " === 'undefined') { throw new ReferenceError('" + varName + " is not defined');} " ;
19- }
21+ } ;
2022
21- if ( typeof varName === "object" ) {
22- env = varName ;
23- if ( ! env || Array . isArray ( env ) ) {
23+ if ( typeof arguments [ 0 ] === "object" ) {
24+ arguments . env = arguments . varName ;
25+ if ( ! arguments . env || Array . isArray ( arguments . env ) ) {
2426 throw new TypeError ( "__set__ expects an object as env" ) ;
2527 }
26- for ( key in env ) {
27- if ( env . hasOwnProperty ( key ) ) {
28- src += checkExistsSrc ( key ) + key + " = env." + key + ";" ;
28+ for ( arguments . key in arguments . env ) {
29+ if ( arguments . env . hasOwnProperty ( arguments . key ) ) {
30+ arguments . src += arguments . checkExistsSrc ( arguments . key ) + arguments . key + " = arguments. env." + arguments . key + ";" ;
2931 }
3032 }
31- } else if ( typeof varName === "string" ) {
32- if ( ! varName ) {
33+ } else if ( typeof arguments . varName === "string" ) {
34+ if ( ! arguments . varName ) {
3335 throw new TypeError ( "__set__ expects a non-empty string as a variable name" ) ;
3436 }
35- src = checkExistsSrc ( varName ) + varName + " = varValue;"
37+ arguments . src = arguments . checkExistsSrc ( arguments . varName ) + arguments . varName + " = arguments. varValue;" ;
3638 } else {
3739 throw new TypeError ( "__set__ expects an environment object or a non-empty string as a variable name" ) ;
3840 }
3941
40- eval ( src ) ;
42+ eval ( arguments . src ) ;
4143} ;
0 commit comments