@@ -18,10 +18,22 @@ function processExports(exports, test, cached, parentKeyName) {
1818 // Return early if this object has already been processed.
1919 if ( cached . indexOf ( exports ) > - 1 ) {
2020 return exports ;
21+ } else if ( typeof exports === "function" ) {
22+ // For functions, cache the original and wrapped version, else non-wrapped
23+ // functions end up being given back when encountered multiple times.
24+ var cacheResult = cached . filter ( function ( c ) {
25+ return c . original === exports ;
26+ } ) ;
27+
28+ if ( cacheResult . length ) {
29+ return cacheResult [ 0 ] . wrapped ;
30+ }
2131 }
2232
23- // Record this object in the cache.
24- cached . push ( exports ) ;
33+ // Record this object in the cache, if it is not a function.
34+ if ( typeof exports != "function" ) {
35+ cached . push ( exports ) ;
36+ }
2537
2638 // Pass through if not an object or function.
2739 if ( typeof exports != "object" && typeof exports != "function" ) {
@@ -32,14 +44,21 @@ function processExports(exports, test, cached, parentKeyName) {
3244
3345 // If a function, simply return it wrapped.
3446 if ( typeof exports === "function" ) {
47+ // Assign the new function in place.
48+ var wrapped = Promise . denodeify ( exports ) ;
49+
50+ // Push the wrapped function onto the cache before processing properties,
51+ // else a cyclical function property causes a stack overflow.
52+ cached . push ( {
53+ original : exports ,
54+ wrapped : wrapped
55+ } ) ;
56+
3557 // Find properties added to functions.
3658 for ( var keyName in exports ) {
3759 exports [ keyName ] = processExports ( exports [ keyName ] , test , cached , name ) ;
3860 }
3961
40- // Assign the new function in place.
41- var wrapped = Promise . denodeify ( exports ) ;
42-
4362 // Find methods on the prototype, if there are any.
4463 if ( Object . keys ( exports . prototype ) . length ) {
4564 processExports ( exports . prototype , test , cached , name ) ;
0 commit comments