time-to-botec

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

addon.c (2960B)


      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/set_high_word.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 	napi_valuetype vtype0;
     55 	status = napi_typeof( env, argv[ 0 ], &vtype0 );
     56 	assert( status == napi_ok );
     57 	if ( vtype0 != napi_number ) {
     58 		status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a number." );
     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 	double x;
     73 	status = napi_get_value_double( env, argv[ 0 ], &x );
     74 	assert( status == napi_ok );
     75 
     76 	uint32_t high;
     77 	status = napi_get_value_uint32( env, argv[ 1 ], &high );
     78 	assert( status == napi_ok );
     79 
     80 	stdlib_base_float64_set_high_word( high, &x );
     81 
     82 	napi_value v;
     83 	status = napi_create_double( env, x, &v );
     84 	assert( status == napi_ok );
     85 
     86 	return v;
     87 }
     88 
     89 /**
     90 * Initializes a Node-API module.
     91 *
     92 * @private
     93 * @param env      environment under which the function is invoked
     94 * @param exports  exports object
     95 * @return         main export
     96 */
     97 static napi_value init( napi_env env, napi_value exports ) {
     98 	napi_value fcn;
     99 	napi_status status = napi_create_function( env, "exports", NAPI_AUTO_LENGTH, addon, NULL, &fcn );
    100 	assert( status == napi_ok );
    101 	return fcn;
    102 }
    103 
    104 NAPI_MODULE( NODE_GYP_MODULE_NAME, init )