time-to-botec

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

addon.c (3401B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2020 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 #include "stdlib/number/float64/base/to_words.h"
     20 #include <node_api.h>
     21 #include <stdint.h>
     22 #include <stdbool.h>
     23 #include <assert.h>
     24 
     25 /**
     26 * Receives JavaScript callback invocation data.
     27 *
     28 * @private
     29 * @param env    environment under which the function is invoked
     30 * @param info   callback data
     31 * @return       Node-API value
     32 */
     33 static napi_value addon( napi_env env, napi_callback_info info ) {
     34 	napi_status status;
     35 
     36 	// Get callback arguments:
     37 	size_t argc = 2;
     38 	napi_value argv[ 2 ];
     39 	status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
     40 	assert( status == napi_ok );
     41 
     42 	// Check whether we were provided the correct number of arguments:
     43 	if ( argc < 2 ) {
     44 		status = napi_throw_error( env, NULL, "invalid invocation. Insufficient arguments." );
     45 		assert( status == napi_ok );
     46 		return NULL;
     47 	}
     48 	if ( argc > 2 ) {
     49 		status = napi_throw_error( env, NULL, "invalid invocation. Too many arguments." );
     50 		assert( status == napi_ok );
     51 		return NULL;
     52 	}
     53 
     54 	bool res;
     55 	status = napi_is_typedarray( env, argv[ 0 ], &res );
     56 	assert( status == napi_ok );
     57 	if ( res == false ) {
     58 		status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a Uint32Array." );
     59 		assert( status == napi_ok );
     60 		return NULL;
     61 	}
     62 
     63 	napi_valuetype vtype1;
     64 	status = napi_typeof( env, argv[ 1 ], &vtype1 );
     65 	assert( status == napi_ok );
     66 	if ( vtype1 != napi_number ) {
     67 		status = napi_throw_type_error( env, NULL, "invalid argument. Second argument must be a number." );
     68 		assert( status == napi_ok );
     69 		return NULL;
     70 	}
     71 
     72 	napi_typedarray_type vtype0;
     73 	size_t len;
     74 	void *Out;
     75 	status = napi_get_typedarray_info( env, argv[ 0 ], &vtype0, &len, &Out, NULL, NULL );
     76 	assert( status == napi_ok );
     77 	if ( vtype0 != napi_uint32_array ) {
     78 		status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a Uint32Array." );
     79 		assert( status == napi_ok );
     80 		return NULL;
     81 	}
     82 	if ( len != 2 ) {
     83 		status = napi_throw_range_error( env, NULL, "invalid argument. First argument must have 2 elements." );
     84 		assert( status == napi_ok );
     85 		return NULL;
     86 	}
     87 
     88 	double value;
     89 	status = napi_get_value_double( env, argv[ 1 ], &value );
     90 	assert( status == napi_ok );
     91 
     92 	uint32_t high;
     93 	uint32_t low;
     94 	stdlib_base_float64_to_words( value, &high, &low );
     95 
     96 	uint32_t *op = (uint32_t *)Out;
     97 	op[ 0 ] = high;
     98 	op[ 1 ] = low;
     99 
    100 	return NULL;
    101 }
    102 
    103 /**
    104 * Initializes a Node-API module.
    105 *
    106 * @private
    107 * @param env      environment under which the function is invoked
    108 * @param exports  exports object
    109 * @return         main export
    110 */
    111 static napi_value init( napi_env env, napi_value exports ) {
    112 	napi_value fcn;
    113 	napi_status status = napi_create_function( env, "exports", NAPI_AUTO_LENGTH, addon, NULL, &fcn );
    114 	assert( status == napi_ok );
    115 	return fcn;
    116 }
    117 
    118 NAPI_MODULE( NODE_GYP_MODULE_NAME, init )