Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | /* |
michael@0 | 7 | * Date: 14 Mar 2001 |
michael@0 | 8 | * |
michael@0 | 9 | * SUMMARY: Utility functions for testing objects - |
michael@0 | 10 | * |
michael@0 | 11 | * Suppose obj is an instance of a native type, e.g. Number. |
michael@0 | 12 | * Then obj.toString() invokes Number.prototype.toString(). |
michael@0 | 13 | * We would also like to access Object.prototype.toString(). |
michael@0 | 14 | * |
michael@0 | 15 | * The difference is this: suppose obj = new Number(7). |
michael@0 | 16 | * Invoking Number.prototype.toString() on this just returns 7. |
michael@0 | 17 | * Object.prototype.toString() on this returns '[object Number]'. |
michael@0 | 18 | * |
michael@0 | 19 | * The getJSType() function below will return '[object Number]' for us. |
michael@0 | 20 | * The getJSClass() function returns 'Number', the [[Class]] property of obj. |
michael@0 | 21 | * See ECMA-262 Edition 3, 13-Oct-1999, Section 8.6.2 |
michael@0 | 22 | */ |
michael@0 | 23 | //----------------------------------------------------------------------------- |
michael@0 | 24 | |
michael@0 | 25 | |
michael@0 | 26 | var cnNoObject = 'Unexpected Error!!! Parameter to this function must be an object'; |
michael@0 | 27 | var cnNoClass = 'Unexpected Error!!! Cannot find Class property'; |
michael@0 | 28 | var cnObjectToString = Object.prototype.toString; |
michael@0 | 29 | var GLOBAL = 'global'; |
michael@0 | 30 | |
michael@0 | 31 | // checks that it's safe to call findType() |
michael@0 | 32 | function getJSType(obj) |
michael@0 | 33 | { |
michael@0 | 34 | if (isObject(obj)) |
michael@0 | 35 | return findType(obj); |
michael@0 | 36 | return cnNoObject; |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | |
michael@0 | 40 | // checks that it's safe to call findType() |
michael@0 | 41 | function getJSClass(obj) |
michael@0 | 42 | { |
michael@0 | 43 | if (isObject(obj)) |
michael@0 | 44 | return findClass(findType(obj)); |
michael@0 | 45 | return cnNoObject; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | |
michael@0 | 49 | function findType(obj) |
michael@0 | 50 | { |
michael@0 | 51 | return cnObjectToString.apply(obj); |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | |
michael@0 | 55 | // given '[object Number]', return 'Number' |
michael@0 | 56 | function findClass(sType) |
michael@0 | 57 | { |
michael@0 | 58 | var re = /^\[.*\s+(\w+)\s*\]$/; |
michael@0 | 59 | var a = sType.match(re); |
michael@0 | 60 | |
michael@0 | 61 | if (a && a[1]) |
michael@0 | 62 | return a[1]; |
michael@0 | 63 | return cnNoClass; |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | |
michael@0 | 67 | function isObject(obj) |
michael@0 | 68 | { |
michael@0 | 69 | return obj instanceof Object; |
michael@0 | 70 | } |
michael@0 | 71 |