time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

polyfill.js (4792B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2018 The Stdlib Authors.
      5 *
      6 * Licensed under the Apache License, Version 2.0 (the "License");
      7 * you may not use this file except in compliance with the License.
      8 * You may obtain a copy of the License at
      9 *
     10 *    http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing, software
     13 * distributed under the License is distributed on an "AS IS" BASIS,
     14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15 * See the License for the specific language governing permissions and
     16 * limitations under the License.
     17 */
     18 
     19 /* eslint-disable no-underscore-dangle, no-proto */
     20 
     21 'use strict';
     22 
     23 // VARIABLES //
     24 
     25 var objectProtoype = Object.prototype;
     26 var toStr = objectProtoype.toString;
     27 var defineGetter = objectProtoype.__defineGetter__;
     28 var defineSetter = objectProtoype.__defineSetter__;
     29 var lookupGetter = objectProtoype.__lookupGetter__;
     30 var lookupSetter = objectProtoype.__lookupSetter__;
     31 
     32 
     33 // MAIN //
     34 
     35 /**
     36 * Defines (or modifies) an object property.
     37 *
     38 * ## Notes
     39 *
     40 * -   Property descriptors come in two flavors: **data descriptors** and **accessor descriptors**. A data descriptor is a property that has a value, which may or may not be writable. An accessor descriptor is a property described by a getter-setter function pair. A descriptor must be one of these two flavors and cannot be both.
     41 *
     42 * @param {Object} obj - object on which to define the property
     43 * @param {string} prop - property name
     44 * @param {Object} descriptor - property descriptor
     45 * @param {boolean} [descriptor.configurable=false] - boolean indicating if property descriptor can be changed and if the property can be deleted from the provided object
     46 * @param {boolean} [descriptor.enumerable=false] - boolean indicating if the property shows up when enumerating object properties
     47 * @param {boolean} [descriptor.writable=false] - boolean indicating if the value associated with the property can be changed with an assignment operator
     48 * @param {*} [descriptor.value] - property value
     49 * @param {(Function|void)} [descriptor.get=undefined] - function which serves as a getter for the property, or, if no getter, undefined. When the property is accessed, a getter function is called without arguments and with the `this` context set to the object through which the property is accessed (which may not be the object on which the property is defined due to inheritance). The return value will be used as the property value.
     50 * @param {(Function|void)} [descriptor.set=undefined] - function which serves as a setter for the property, or, if no setter, undefined. When assigning a property value, a setter function is called with one argument (the value being assigned to the property) and with the `this` context set to the object through which the property is assigned.
     51 * @throws {TypeError} first argument must be an object
     52 * @throws {TypeError} third argument must be an object
     53 * @throws {Error} property descriptor cannot have both a value and a setter and/or getter
     54 * @returns {Object} object with added property
     55 *
     56 * @example
     57 * var obj = {};
     58 *
     59 * defineProperty( obj, 'foo', {
     60 *     'value': 'bar'
     61 * });
     62 *
     63 * var str = obj.foo;
     64 * // returns 'bar'
     65 */
     66 function defineProperty( obj, prop, descriptor ) {
     67 	var prototype;
     68 	var hasValue;
     69 	var hasGet;
     70 	var hasSet;
     71 
     72 	if ( typeof obj !== 'object' || obj === null || toStr.call( obj ) === '[object Array]' ) {
     73 		throw new TypeError( 'invalid argument. First argument must be an object. Value: `' + obj + '`.' );
     74 	}
     75 	if ( typeof descriptor !== 'object' || descriptor === null || toStr.call( descriptor ) === '[object Array]' ) {
     76 		throw new TypeError( 'invalid argument. Property descriptor must be an object. Value: `' + descriptor + '`.' );
     77 	}
     78 	hasValue = ( 'value' in descriptor );
     79 	if ( hasValue ) {
     80 		if (
     81 			lookupGetter.call( obj, prop ) ||
     82 			lookupSetter.call( obj, prop )
     83 		) {
     84 			// Override `__proto__` to avoid touching inherited accessors:
     85 			prototype = obj.__proto__;
     86 			obj.__proto__ = objectProtoype;
     87 
     88 			// Delete property as existing getters/setters prevent assigning value to specified property:
     89 			delete obj[ prop ];
     90 			obj[ prop ] = descriptor.value;
     91 
     92 			// Restore original prototype:
     93 			obj.__proto__ = prototype;
     94 		} else {
     95 			obj[ prop ] = descriptor.value;
     96 		}
     97 	}
     98 	hasGet = ( 'get' in descriptor );
     99 	hasSet = ( 'set' in descriptor );
    100 
    101 	if ( hasValue && ( hasGet || hasSet ) ) {
    102 		throw new Error( 'invalid argument. Cannot specify one or more accessors and a value or writable attribute in the property descriptor.' );
    103 	}
    104 
    105 	if ( hasGet && defineGetter ) {
    106 		defineGetter.call( obj, prop, descriptor.get );
    107 	}
    108 	if ( hasSet && defineSetter ) {
    109 		defineSetter.call( obj, prop, descriptor.set );
    110 	}
    111 	return obj;
    112 }
    113 
    114 
    115 // EXPORTS //
    116 
    117 module.exports = defineProperty;