|
| 1 | +// Copyright 2012 Mozilla Corporation. All rights reserved. |
| 2 | +// This code is governed by the BSD license found in the LICENSE file. |
| 3 | + |
| 4 | +/** |
| 5 | + * @description Tests that obj meets the requirements for built-in objects |
| 6 | + * defined by the introduction of chapter 15 of the ECMAScript Language Specification. |
| 7 | + * @param {Object} obj the object to be tested. |
| 8 | + * @param {boolean} isFunction whether the specification describes obj as a function. |
| 9 | + * @param {boolean} isConstructor whether the specification describes obj as a constructor. |
| 10 | + * @param {String[]} properties an array with the names of the built-in properties of obj, |
| 11 | + * excluding length, prototype, or properties with non-default attributes. |
| 12 | + * @param {number} length for functions only: the length specified for the function |
| 13 | + * or derived from the argument list. |
| 14 | + * @author Norbert Lindenberg |
| 15 | + */ |
| 16 | + |
| 17 | +function testBuiltInObject(obj, isFunction, isConstructor, properties, length) { |
| 18 | + |
| 19 | + if (obj === undefined) { |
| 20 | + $ERROR("Object being tested is undefined."); |
| 21 | + } |
| 22 | + |
| 23 | + var objString = Object.prototype.toString.call(obj); |
| 24 | + if (isFunction) { |
| 25 | + if (objString !== "[object Function]") { |
| 26 | + $ERROR("The [[Class]] internal property of a built-in function must be " + |
| 27 | + "\"Function\", but toString() returns " + objString); |
| 28 | + } |
| 29 | + } else { |
| 30 | + if (objString !== "[object Object]") { |
| 31 | + $ERROR("The [[Class]] internal property of a built-in non-function object must be " + |
| 32 | + "\"Object\", but toString() returns " + objString); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + if (!Object.isExtensible(obj)) { |
| 37 | + $ERROR("Built-in objects must be extensible."); |
| 38 | + } |
| 39 | + |
| 40 | + if (isFunction && Object.getPrototypeOf(obj) !== Function.prototype) { |
| 41 | + $ERROR("Built-in functions must have Function.prototype as their prototype."); |
| 42 | + } |
| 43 | + |
| 44 | + if (isConstructor && Object.getPrototypeOf(obj.prototype) !== Object.prototype) { |
| 45 | + $ERROR("Built-in prototype objects must have Object.prototype as their prototype."); |
| 46 | + } |
| 47 | + |
| 48 | + // verification of the absence of the [[Construct]] internal property has |
| 49 | + // been moved to the end of the test |
| 50 | + |
| 51 | + // verification of the absence of the prototype property has |
| 52 | + // been moved to the end of the test |
| 53 | + |
| 54 | + if (isFunction) { |
| 55 | + |
| 56 | + if (typeof obj.length !== "number" || obj.length !== Math.floor(obj.length)) { |
| 57 | + $ERROR("Built-in functions must have a length property with an integer value."); |
| 58 | + } |
| 59 | + |
| 60 | + if (obj.length !== length) { |
| 61 | + $ERROR("Function's length property doesn't have specified value; expected " + |
| 62 | + length + ", got " + obj.length + "."); |
| 63 | + } |
| 64 | + |
| 65 | + var desc = Object.getOwnPropertyDescriptor(obj, "length"); |
| 66 | + if (desc.writable) { |
| 67 | + $ERROR("The length property of a built-in function must not be writable."); |
| 68 | + } |
| 69 | + if (desc.enumerable) { |
| 70 | + $ERROR("The length property of a built-in function must not be enumerable."); |
| 71 | + } |
| 72 | + if (desc.configurable) { |
| 73 | + $ERROR("The length property of a built-in function must not be configurable."); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + properties.forEach(function(prop) { |
| 78 | + var desc = Object.getOwnPropertyDescriptor(obj, prop); |
| 79 | + if (desc === undefined) { |
| 80 | + $ERROR("Missing property " + prop + "."); |
| 81 | + } |
| 82 | + // accessor properties don't have writable attribute |
| 83 | + if (desc.hasOwnProperty("writable") && !desc.writable) { |
| 84 | + $ERROR("The " + prop + " property of this built-in function must be writable."); |
| 85 | + } |
| 86 | + if (desc.enumerable) { |
| 87 | + $ERROR("The " + prop + " property of this built-in function must not be enumerable."); |
| 88 | + } |
| 89 | + if (!desc.configurable) { |
| 90 | + $ERROR("The " + prop + " property of this built-in function must be configurable."); |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + // The remaining sections have been moved to the end of the test because |
| 95 | + // unbound non-constructor functions written in JavaScript cannot possibly |
| 96 | + // pass them, and we still want to test JavaScript implementations as much |
| 97 | + // as possible. |
| 98 | + |
| 99 | + var exception; |
| 100 | + if (isFunction && !isConstructor) { |
| 101 | + // this is not a complete test for the presence of [[Construct]]: |
| 102 | + // if it's absent, the exception must be thrown, but it may also |
| 103 | + // be thrown if it's present and just has preconditions related to |
| 104 | + // arguments or the this value that this statement doesn't meet. |
| 105 | + try { |
| 106 | + /*jshint newcap:false*/ |
| 107 | + var instance = new obj(); |
| 108 | + } catch (e) { |
| 109 | + exception = e; |
| 110 | + } |
| 111 | + if (exception === undefined || exception.name !== "TypeError") { |
| 112 | + $ERROR("Built-in functions that aren't constructors must throw TypeError when " + |
| 113 | + "used in a \"new\" statement."); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + if (isFunction && !isConstructor && obj.hasOwnProperty("prototype")) { |
| 118 | + $ERROR("Built-in functions that aren't constructors must not have a prototype property."); |
| 119 | + } |
| 120 | + |
| 121 | + // passed the complete test! |
| 122 | + return true; |
| 123 | +} |
| 124 | + |
0 commit comments