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