1 // ==========================================================================
  2 // Project:   SproutCore - JavaScript Application Framework
  3 // Copyright: ©2006-2011 Strobe Inc. and contributors.
  4 //            Portions ©2008-2011 Apple Inc. All rights reserved.
  5 // License:   Licensed under MIT license (see license.js)
  6 // ==========================================================================
  7 
  8 // test
  9 window.SC = window.SC || {};
 10 
 11 // Note:  We won't use SC.T_* here because those constants might not yet be
 12 //        defined.
 13 SC._mapDisplayNamesUseHashForSeenTypes = ['object', 'number', 'boolean', 'array', 'string', 'function', 'class', 'undefined', 'error'];  // 'hash' causes problems
 14 
 15 
 16 SC.mapDisplayNames = function(obj, level, path, seenHash, seenArray) {
 17   if (SC.browser.engine !== SC.ENGINE.webkit) return ;
 18 
 19   // Lazily instantiate the hash of types we'll use a hash for the "have we
 20   // seen this before?" structure.  (Some types are not safe to put in a hash
 21   // in this manner, so we'll use the hash for its algorithmic advantage when
 22   // possible, but fall back to an array using `indexOf()` when necessary.)
 23   if (!SC._mapDisplayNamesUseHashForSeenTypesHash) {
 24     var types = SC._mapDisplayNamesUseHashForSeenTypes ;
 25     var typesHash = {} ;
 26     var len = types.length ;
 27     for (var i = 0;  i < len;  ++i) {
 28       var type = types[i] ;
 29       typesHash[type] = true ;
 30     }
 31     SC._mapDisplayNamesUseHashForSeenTypesHash = typesHash ;
 32   }
 33 
 34 
 35   if (obj === undefined) obj = window ;
 36   if (level === undefined) level = 0 ;
 37   if (path === undefined) path = [] ;
 38   if (seenHash === undefined) seenHash = {} ;
 39   if (seenArray === undefined) seenArray = [] ;
 40 
 41   if (level > 5) return ;
 42 
 43   var useHash = !!SC._mapDisplayNamesUseHashForSeenTypesHash[SC.typeOf(obj)] ;
 44 
 45   var hash;
 46   var arrayToCheck;
 47   if (useHash) {
 48     hash = SC.hashFor(obj) ;
 49     arrayToCheck = seenHash[hash];
 50   }
 51   else {
 52     arrayToCheck = seenArray;
 53   }
 54 
 55   if (arrayToCheck  &&  arrayToCheck.indexOf(obj) !== -1) return ;
 56 
 57   if (arrayToCheck) {
 58     arrayToCheck.push(obj) ;
 59   }
 60   else if (useHash) {
 61     seenHash[hash] = [obj] ;
 62   }
 63 
 64   var loc = path.length, str, val, t;
 65   path[loc] = '';
 66 
 67   for(var key in obj) {
 68     if (obj.hasOwnProperty && !obj.hasOwnProperty(key)) continue ;
 69     if (!isNaN(Number(key))) continue ; // skip array indexes
 70     if (key === "constructor") continue ;
 71     if (key === "superclass") continue ;
 72     if (key === "document") continue ;
 73 
 74     // Avoid TypeError's in WebKit based browsers
 75     if (obj.type && obj.type === 'file') {
 76       if (key === 'selectionStart' || key === 'selectionEnd') continue;
 77     }
 78 
 79     try{
 80       val = obj[key];
 81     }catch(e){
 82       //This object might be special this get called when an app
 83      // using webView adds an static C object to JS.
 84       continue;
 85     }
 86     if (key === "SproutCore") key = "SC";
 87     t   = SC.typeOf(val);
 88     if (t === SC.T_FUNCTION) {
 89       if (!val.displayName) { // only name the first time it is encountered
 90         path[loc] = key ;
 91         str = path.join('.').replace('.prototype.', '#');
 92         val.displayName = str;
 93       }
 94 
 95       // handle constructor-style
 96       if (val.prototype) {
 97         path.push("prototype");
 98         SC.mapDisplayNames(val.prototype, level+1, path, seenHash, seenArray);
 99         path.pop();
100       }
101 
102     } else if (t === SC.T_CLASS) {
103       path[loc] = key ;
104       SC.mapDisplayNames(val, level+1, path, seenHash, seenArray);
105 
106     } else if ((key.indexOf('_')!==0) && (t===SC.T_OBJECT || t===SC.T_HASH)) {
107       path[loc] = key ;
108       SC.mapDisplayNames(val, level+1, path, seenHash, seenArray);
109     }
110   }
111 
112   path.pop();
113 };
114 
115