time-to-botec

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

main.js (3676B)


      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 isArrayBuffer = require( '@stdlib/assert/is-arraybuffer' );
     24 var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
     25 var Buffer = require( './../../ctor' );
     26 var version = require( './node_version.js' );
     27 
     28 
     29 // MAIN //
     30 
     31 /**
     32 * Allocates a buffer from an `ArrayBuffer`.
     33 *
     34 * ## Notes
     35 *
     36 * The behavior of this function varies across Node.js versions due to changes in the underlying Node.js APIs:
     37 *
     38 * -   `<6.0.0`: if provided an empty ArrayBuffer, the function returns an empty Buffer which is **not** an ArrayBuffer view.
     39 * -   otherwise, the function returns a view of an ArrayBuffer without copying the underlying memory.
     40 *
     41 *
     42 * @param {ArrayBuffer} buf - ArrayBuffer instance
     43 * @param {NonNegativeInteger} [byteOffset=0] - index specifying the location of the first buffer byte
     44 * @param {NonNegativeInteger} [length=buf.byteLength] - number of buffer bytes
     45 * @throws {TypeError} first argument must be an ArrayBuffer
     46 * @throws {TypeError} second argument must be a nonnegative integer
     47 * @throws {RangeError} second argument must not exceed number of bytes in input ArrayBuffer
     48 * @throws {TypeError} last argument must be a nonnegative integer
     49 * @throws {RangeError} last argument must not exceed number of bytes in input ArrayBuffer
     50 * @returns {Buffer} new `Buffer` instance
     51 *
     52 * @example
     53 * var ArrayBuffer = require( '@stdlib/array/buffer' );
     54 * var ab = new ArrayBuffer( 10 );
     55 *
     56 * var buf = fromArrayBuffer( ab );
     57 * // returns <Buffer>
     58 *
     59 * @example
     60 * var ArrayBuffer = require( '@stdlib/array/buffer' );
     61 * var ab = new ArrayBuffer( 10 );
     62 *
     63 * var buf = fromArrayBuffer( ab, 2, 4 );
     64 * // returns <Buffer>
     65 */
     66 function fromArrayBuffer( buf, byteOffset, length ) {
     67 	var offset;
     68 	var len;
     69 	if ( !isArrayBuffer( buf ) ) {
     70 		throw new TypeError( 'invalid argument. First argument must be an ArrayBuffer. Value: `' + buf + '`' );
     71 	}
     72 	if ( arguments.length > 1 ) {
     73 		if ( !isNonNegativeInteger( byteOffset ) ) {
     74 			throw new TypeError( 'invalid argument. Second argument must be a nonnegative integer. Value: `' + byteOffset + '`.' );
     75 		}
     76 		if ( byteOffset > buf.byteLength ) {
     77 			throw new RangeError( 'invalid argument. Second argument must not exceed the number of bytes in the input ArrayBuffer. Value: `' + byteOffset + '`.' );
     78 		}
     79 		offset = byteOffset;
     80 		if ( arguments.length > 2 ) {
     81 			if ( !isNonNegativeInteger( length ) ) {
     82 				throw new TypeError( 'invalid argument. Last argument must be a nonnegative integer. Value: `' + length + '`.' );
     83 			}
     84 			if ( length > buf.byteLength ) {
     85 				throw new RangeError( 'invalid argument. Last argument must not exceed the number of bytes in the input ArrayBuffer. Value: `' + length + '`.' );
     86 			}
     87 			len = length;
     88 		} else {
     89 			len = buf.byteLength - offset;
     90 		}
     91 	} else {
     92 		offset = 0;
     93 		len = buf.byteLength;
     94 	}
     95 	// Address Node v5.x where providing an empty ArrayBuffer throws an error:
     96 	if ( len === 0 && version < 6 ) {
     97 		return Buffer.from( [] );
     98 	}
     99 	return Buffer.from( buf, offset, len );
    100 }
    101 
    102 
    103 // EXPORTS //
    104 
    105 module.exports = fromArrayBuffer;