1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 'use strict'; 6 7 8 /** 9 * The global object. 10 * @type {!Object} 11 * @const 12 */ 13 var global = this; 14 15 16 /** Platform, package, object property, and Event support. */ 17 this.base = (function() { 18 19 function mLog(text, opt_indentLevel) { 20 if (true) 21 return; 22 23 var spacing = ''; 24 var indentLevel = opt_indentLevel || 0; 25 for (var i = 0; i < indentLevel; i++) 26 spacing += ' '; 27 console.log(spacing + text); 28 } 29 30 /** 31 * Builds an object structure for the provided namespace path, 32 * ensuring that names that already exist are not overwritten. For 33 * example: 34 * 'a.b.c' -> a = {};a.b={};a.b.c={}; 35 * @param {string} name Name of the object that this file defines. 36 * @param {*=} opt_object The object to expose at the end of the path. 37 * @param {Object=} opt_objectToExportTo The object to add the path to; 38 * default is {@code global}. 39 * @private 40 */ 41 function exportPath(name, opt_object, opt_objectToExportTo) { 42 var parts = name.split('.'); 43 var cur = opt_objectToExportTo || global; 44 45 for (var part; parts.length && (part = parts.shift());) { 46 if (!parts.length && opt_object !== undefined) { 47 // last part and we have an object; use it 48 cur[part] = opt_object; 49 } else if (part in cur) { 50 cur = cur[part]; 51 } else { 52 cur = cur[part] = {}; 53 } 54 } 55 return cur; 56 } 57 58 function exportTo(namespace, fn) { 59 var obj = exportPath(namespace); 60 try { 61 var exports = fn(); 62 } catch (e) { 63 console.log('While running exports for ', name, ':'); 64 console.log(e.stack || e); 65 return; 66 } 67 68 for (var propertyName in exports) { 69 // Maybe we should check the prototype chain here? The current usage 70 // pattern is always using an object literal so we only care about own 71 // properties. 72 var propertyDescriptor = Object.getOwnPropertyDescriptor(exports, 73 propertyName); 74 if (propertyDescriptor) { 75 Object.defineProperty(obj, propertyName, propertyDescriptor); 76 mLog(' +' + propertyName); 77 } 78 } 79 } 80 81 return { 82 exportTo: exportTo 83 }; 84 })();