time-to-botec

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

main.js (2009B)


      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 'use strict';
     20 
     21 // MODULES //
     22 
     23 var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
     24 var getThis = require( './codegen.js' );
     25 var Self = require( './self.js' );
     26 var Win = require( './window.js' );
     27 var Global = require( './global.js' );
     28 
     29 
     30 // MAIN //
     31 
     32 /**
     33 * Returns the global object.
     34 *
     35 * ## Notes
     36 *
     37 * -   Using code generation is the **most** reliable way to resolve the global object; however, doing so is likely to violate content security policies (CSPs) in, e.g., Chrome Apps and elsewhere.
     38 *
     39 * @param {boolean} [codegen=false] - boolean indicating whether to use code generation to resolve the global object
     40 * @throws {TypeError} must provide a boolean
     41 * @throws {Error} unable to resolve global object
     42 * @returns {Object} global object
     43 *
     44 * @example
     45 * var g = getGlobal();
     46 * // returns {...}
     47 */
     48 function getGlobal( codegen ) {
     49 	if ( arguments.length ) {
     50 		if ( !isBoolean( codegen ) ) {
     51 			throw new TypeError( 'invalid argument. Must provide a boolean primitive. Value: `'+codegen+'`.' );
     52 		}
     53 		if ( codegen ) {
     54 			return getThis();
     55 		}
     56 		// Fall through...
     57 	}
     58 	// Case: browsers and web workers
     59 	if ( Self ) {
     60 		return Self;
     61 	}
     62 	// Case: browsers
     63 	if ( Win ) {
     64 		return Win;
     65 	}
     66 	// Case: Node.js
     67 	if ( Global ) {
     68 		return Global;
     69 	}
     70 	// Case: unknown
     71 	throw new Error( 'unexpected error. Unable to resolve global object.' );
     72 }
     73 
     74 
     75 // EXPORTS //
     76 
     77 module.exports = getGlobal;