time-to-botec

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

sync.js (2338B)


      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 isObject = require( '@stdlib/assert/is-plain-object' );
     24 var isUint8Array = require( '@stdlib/assert/is-uint8array' );
     25 var instanceOf = require( '@stdlib/assert/instance-of' );
     26 var readFileSync = require( './../../read-file' ).sync;
     27 var Uint8Array = require( '@stdlib/array/uint8' );
     28 
     29 
     30 // MAIN //
     31 
     32 /**
     33 * Synchronously reads the entire contents of a WebAssembly file.
     34 *
     35 * @param {(string|Buffer|integer)} file - file path or file descriptor
     36 * @param {Options} [options] - options
     37 * @param {string} [options.flag] - file status flag
     38 * @throws {TypeError} options argument must be an object
     39 * @returns {(Uint8Array|Error)} file contents or an error
     40 *
     41 * @example
     42 * var join = require( 'path' ).join;
     43 * var instanceOf = require( '@stdlib/assert/instance-of' );
     44 *
     45 * var fpath = join( __dirname, 'foo.wasm' );
     46 * var out = readWASMSync( fpath );
     47 * if ( instanceOf( out, Error ) ) {
     48 *     throw out;
     49 * }
     50 * console.log( out );
     51 */
     52 function readWASMSync( file, options ) {
     53 	var opts;
     54 	var out;
     55 	var f;
     56 	var i;
     57 	if ( arguments.length > 1 ) {
     58 		if ( !isObject( options ) ) {
     59 			throw new TypeError( 'invalid argument. Options argument must be an object. Value: `' + options + '`.' );
     60 		}
     61 		opts = options;
     62 	} else {
     63 		opts = {};
     64 	}
     65 	// Always override setting the encoding option, as wasm is a binary file format:
     66 	opts.encoding = null;
     67 	f = readFileSync( file, opts );
     68 	if ( instanceOf( f, Error ) ) {
     69 		return f;
     70 	}
     71 	if ( isUint8Array( f ) ) {
     72 		return f;
     73 	}
     74 	// Handle older Node.js environments where Buffer objects are not Uint8Arrays...
     75 	out = new Uint8Array( f.length );
     76 	for ( i = 0; i < f.length; i++ ) {
     77 		out[ i ] = f[ i ];
     78 	}
     79 	return out;
     80 }
     81 
     82 
     83 // EXPORTS //
     84 
     85 module.exports = readWASMSync;