1 <!DOCTYPE html> 2 <!-- 3 Copyright (c) 2014 The Chromium Authors. All rights reserved. 4 Use of this source code is governed by a BSD-style license that can be 5 found in the LICENSE file. 6 --> 7 8 <link rel="import" href="/base/base.html"> 9 10 <script> 11 'use strict'; 12 13 tr.exportTo('tr.b', function() { 14 /** 15 * Adds a {@code getInstance} static method that always return the same 16 * instance object. 17 * @param {!Function} ctor The constructor for the class to add the static 18 * method to. 19 */ 20 function addSingletonGetter(ctor) { 21 ctor.getInstance = function() { 22 return ctor.instance_ || (ctor.instance_ = new ctor()); 23 }; 24 } 25 26 function normalizeException(e) { 27 if (e === undefined || e === null) { 28 return { 29 message: 'Unknown: null or undefined exception', 30 stack: 'Unknown' 31 }; 32 } 33 34 if (typeof(e) == 'string') { 35 return { 36 message: e, 37 stack: [e] 38 }; 39 } 40 41 var msg = e.message ? e.message : 'Unknown'; 42 return { 43 message: msg, 44 stack: e.stack ? e.stack : [msg] 45 }; 46 } 47 48 function stackTrace() { 49 var stack = new Error().stack + ''; 50 stack = stack.split('\n'); 51 return stack.slice(2); 52 } 53 54 function getUsingPath(path, from_dict) { 55 var parts = path.split('.'); 56 var cur = from_dict; 57 58 for (var part; parts.length && (part = parts.shift());) { 59 if (!parts.length) { 60 return cur[part]; 61 } else if (part in cur) { 62 cur = cur[part]; 63 } else { 64 return undefined; 65 } 66 } 67 return undefined; 68 } 69 70 return { 71 addSingletonGetter: addSingletonGetter, 72 73 normalizeException: normalizeException, 74 stackTrace: stackTrace, 75 76 getUsingPath: getUsingPath 77 }; 78 }); 79 </script> 80