michael@0: // Copyright 2012 Mozilla Corporation. All rights reserved. michael@0: // This code is governed by the BSD license found in the LICENSE file. michael@0: michael@0: /** michael@0: * @description Tests that obj meets the requirements for built-in objects michael@0: * defined by the introduction of chapter 15 of the ECMAScript Language Specification. michael@0: * @param {Object} obj the object to be tested. michael@0: * @param {boolean} isFunction whether the specification describes obj as a function. michael@0: * @param {boolean} isConstructor whether the specification describes obj as a constructor. michael@0: * @param {String[]} properties an array with the names of the built-in properties of obj, michael@0: * excluding length, prototype, or properties with non-default attributes. michael@0: * @param {number} length for functions only: the length specified for the function michael@0: * or derived from the argument list. michael@0: * @author Norbert Lindenberg michael@0: */ michael@0: michael@0: function testBuiltInObject(obj, isFunction, isConstructor, properties, length) { michael@0: michael@0: if (obj === undefined) { michael@0: $ERROR("Object being tested is undefined."); michael@0: } michael@0: michael@0: var objString = Object.prototype.toString.call(obj); michael@0: if (isFunction) { michael@0: if (objString !== "[object Function]") { michael@0: $ERROR("The [[Class]] internal property of a built-in function must be " + michael@0: "\"Function\", but toString() returns " + objString); michael@0: } michael@0: } else { michael@0: if (objString !== "[object Object]") { michael@0: $ERROR("The [[Class]] internal property of a built-in non-function object must be " + michael@0: "\"Object\", but toString() returns " + objString); michael@0: } michael@0: } michael@0: michael@0: if (!Object.isExtensible(obj)) { michael@0: $ERROR("Built-in objects must be extensible."); michael@0: } michael@0: michael@0: if (isFunction && Object.getPrototypeOf(obj) !== Function.prototype) { michael@0: $ERROR("Built-in functions must have Function.prototype as their prototype."); michael@0: } michael@0: michael@0: if (isConstructor && Object.getPrototypeOf(obj.prototype) !== Object.prototype) { michael@0: $ERROR("Built-in prototype objects must have Object.prototype as their prototype."); michael@0: } michael@0: michael@0: // verification of the absence of the [[Construct]] internal property has michael@0: // been moved to the end of the test michael@0: michael@0: // verification of the absence of the prototype property has michael@0: // been moved to the end of the test michael@0: michael@0: if (isFunction) { michael@0: michael@0: if (typeof obj.length !== "number" || obj.length !== Math.floor(obj.length)) { michael@0: $ERROR("Built-in functions must have a length property with an integer value."); michael@0: } michael@0: michael@0: if (obj.length !== length) { michael@0: $ERROR("Function's length property doesn't have specified value; expected " + michael@0: length + ", got " + obj.length + "."); michael@0: } michael@0: michael@0: var desc = Object.getOwnPropertyDescriptor(obj, "length"); michael@0: if (desc.writable) { michael@0: $ERROR("The length property of a built-in function must not be writable."); michael@0: } michael@0: if (desc.enumerable) { michael@0: $ERROR("The length property of a built-in function must not be enumerable."); michael@0: } michael@0: if (desc.configurable) { michael@0: $ERROR("The length property of a built-in function must not be configurable."); michael@0: } michael@0: } michael@0: michael@0: properties.forEach(function(prop) { michael@0: var desc = Object.getOwnPropertyDescriptor(obj, prop); michael@0: if (desc === undefined) { michael@0: $ERROR("Missing property " + prop + "."); michael@0: } michael@0: // accessor properties don't have writable attribute michael@0: if (desc.hasOwnProperty("writable") && !desc.writable) { michael@0: $ERROR("The " + prop + " property of this built-in function must be writable."); michael@0: } michael@0: if (desc.enumerable) { michael@0: $ERROR("The " + prop + " property of this built-in function must not be enumerable."); michael@0: } michael@0: if (!desc.configurable) { michael@0: $ERROR("The " + prop + " property of this built-in function must be configurable."); michael@0: } michael@0: }); michael@0: michael@0: // The remaining sections have been moved to the end of the test because michael@0: // unbound non-constructor functions written in JavaScript cannot possibly michael@0: // pass them, and we still want to test JavaScript implementations as much michael@0: // as possible. michael@0: michael@0: var exception; michael@0: if (isFunction && !isConstructor) { michael@0: // this is not a complete test for the presence of [[Construct]]: michael@0: // if it's absent, the exception must be thrown, but it may also michael@0: // be thrown if it's present and just has preconditions related to michael@0: // arguments or the this value that this statement doesn't meet. michael@0: try { michael@0: /*jshint newcap:false*/ michael@0: var instance = new obj(); michael@0: } catch (e) { michael@0: exception = e; michael@0: } michael@0: if (exception === undefined || exception.name !== "TypeError") { michael@0: $ERROR("Built-in functions that aren't constructors must throw TypeError when " + michael@0: "used in a \"new\" statement."); michael@0: } michael@0: } michael@0: michael@0: if (isFunction && !isConstructor && obj.hasOwnProperty("prototype")) { michael@0: $ERROR("Built-in functions that aren't constructors must not have a prototype property."); michael@0: } michael@0: michael@0: // passed the complete test! michael@0: return true; michael@0: } michael@0: