js/src/tests/ecma_3/Object/shell.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/ecma_3/Object/shell.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,71 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/*
    1.10 + * Date: 14 Mar 2001
    1.11 + *
    1.12 + * SUMMARY: Utility functions for testing objects -
    1.13 + *
    1.14 + * Suppose obj is an instance of a native type, e.g. Number.
    1.15 + * Then obj.toString() invokes Number.prototype.toString().
    1.16 + * We would also like to access Object.prototype.toString().
    1.17 + *
    1.18 + * The difference is this: suppose obj = new Number(7).
    1.19 + * Invoking Number.prototype.toString() on this just returns 7.
    1.20 + * Object.prototype.toString() on this returns '[object Number]'.
    1.21 + *
    1.22 + * The getJSType() function below will return '[object Number]' for us.
    1.23 + * The getJSClass() function returns 'Number', the [[Class]] property of obj.
    1.24 + * See ECMA-262 Edition 3,  13-Oct-1999,  Section 8.6.2 
    1.25 + */
    1.26 +//-----------------------------------------------------------------------------
    1.27 +
    1.28 +
    1.29 +var cnNoObject = 'Unexpected Error!!! Parameter to this function must be an object';
    1.30 +var cnNoClass = 'Unexpected Error!!! Cannot find Class property';
    1.31 +var cnObjectToString = Object.prototype.toString;
    1.32 +var GLOBAL = 'global';
    1.33 +
    1.34 +// checks that it's safe to call findType()
    1.35 +function getJSType(obj)
    1.36 +{
    1.37 +  if (isObject(obj))
    1.38 +    return findType(obj);
    1.39 +  return cnNoObject;
    1.40 +}
    1.41 +
    1.42 +
    1.43 +// checks that it's safe to call findType()
    1.44 +function getJSClass(obj)
    1.45 +{
    1.46 +  if (isObject(obj))
    1.47 +    return findClass(findType(obj));
    1.48 +  return cnNoObject;
    1.49 +}
    1.50 +
    1.51 +
    1.52 +function findType(obj)
    1.53 +{
    1.54 +  return cnObjectToString.apply(obj);
    1.55 +}
    1.56 +
    1.57 +
    1.58 +// given '[object Number]',  return 'Number'
    1.59 +function findClass(sType)
    1.60 +{
    1.61 +  var re =  /^\[.*\s+(\w+)\s*\]$/;
    1.62 +  var a = sType.match(re);
    1.63 + 
    1.64 +  if (a && a[1])
    1.65 +    return a[1];
    1.66 +  return cnNoClass;
    1.67 +}
    1.68 +
    1.69 +
    1.70 +function isObject(obj)
    1.71 +{
    1.72 +  return obj instanceof Object;
    1.73 +}
    1.74 +

mercurial