michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * Date: 14 Mar 2001 michael@0: * michael@0: * SUMMARY: Utility functions for testing objects - michael@0: * michael@0: * Suppose obj is an instance of a native type, e.g. Number. michael@0: * Then obj.toString() invokes Number.prototype.toString(). michael@0: * We would also like to access Object.prototype.toString(). michael@0: * michael@0: * The difference is this: suppose obj = new Number(7). michael@0: * Invoking Number.prototype.toString() on this just returns 7. michael@0: * Object.prototype.toString() on this returns '[object Number]'. michael@0: * michael@0: * The getJSType() function below will return '[object Number]' for us. michael@0: * The getJSClass() function returns 'Number', the [[Class]] property of obj. michael@0: * See ECMA-262 Edition 3, 13-Oct-1999, Section 8.6.2 michael@0: */ michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: michael@0: var cnNoObject = 'Unexpected Error!!! Parameter to this function must be an object'; michael@0: var cnNoClass = 'Unexpected Error!!! Cannot find Class property'; michael@0: var cnObjectToString = Object.prototype.toString; michael@0: var GLOBAL = 'global'; michael@0: michael@0: // checks that it's safe to call findType() michael@0: function getJSType(obj) michael@0: { michael@0: if (isObject(obj)) michael@0: return findType(obj); michael@0: return cnNoObject; michael@0: } michael@0: michael@0: michael@0: // checks that it's safe to call findType() michael@0: function getJSClass(obj) michael@0: { michael@0: if (isObject(obj)) michael@0: return findClass(findType(obj)); michael@0: return cnNoObject; michael@0: } michael@0: michael@0: michael@0: function findType(obj) michael@0: { michael@0: return cnObjectToString.apply(obj); michael@0: } michael@0: michael@0: michael@0: // given '[object Number]', return 'Number' michael@0: function findClass(sType) michael@0: { michael@0: var re = /^\[.*\s+(\w+)\s*\]$/; michael@0: var a = sType.match(re); michael@0: michael@0: if (a && a[1]) michael@0: return a[1]; michael@0: return cnNoClass; michael@0: } michael@0: michael@0: michael@0: function isObject(obj) michael@0: { michael@0: return obj instanceof Object; michael@0: } michael@0: