time-to-botec

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

addon.c (2795B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2021 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/ndarray/base/napi/addon_arguments.h"
     20 #include "stdlib/ndarray/ctor.h"
     21 #include <node_api.h>
     22 #include <stdint.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 	// Total number of arguments:
     37 	int64_t nargs = 4;
     38 
     39 	// Number of input ndarrays:
     40 	int64_t nin = 1;
     41 
     42 	// Get callback arguments:
     43 	size_t argc = 4;
     44 	napi_value argv[ 4 ];
     45 	status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
     46 	assert( status == napi_ok );
     47 
     48 	// Check whether we were provided the correct number of arguments:
     49 	int64_t argc64 = (int64_t)argc;
     50 	if ( argc64 < nargs ) {
     51 		status = napi_throw_error( env, NULL, "invalid invocation. Insufficient arguments." );
     52 		assert( status == napi_ok );
     53 		return NULL;
     54 	}
     55 	if ( argc64 > nargs ) {
     56 		status = napi_throw_error( env, NULL, "invalid invocation. Too many arguments." );
     57 		assert( status == napi_ok );
     58 		return NULL;
     59 	}
     60 	// Process the provided arguments:
     61 	struct ndarray *arrays[ 2 ];
     62 	napi_value err;
     63 	status = stdlib_ndarray_napi_addon_arguments( env, argv, nargs, nin, arrays, &err );
     64 	assert( status == napi_ok );
     65 
     66 	// Check whether processing was successful:
     67 	if ( err != NULL ) {
     68 		status = napi_throw( env, err );
     69 		assert( status == napi_ok );
     70 		return NULL;
     71 	}
     72 	// Normally, one would then do something with the processed arguments; here, we just free memory and return...
     73 
     74 	// Free allocated memory:
     75 	for ( int64_t i = 0; i < 2; i++ ) {
     76 		stdlib_ndarray_free( arrays[ i ] );
     77 		arrays[ i ] = NULL;
     78 	}
     79 	return NULL;
     80 }
     81 
     82 /**
     83 * Initializes a Node-API module.
     84 *
     85 * @private
     86 * @param env      environment under which the function is invoked
     87 * @param exports  exports object
     88 * @return         main export
     89 */
     90 static napi_value init( napi_env env, napi_value exports ) {
     91 	napi_value fcn;
     92 	napi_status status = napi_create_function( env, "exports", NAPI_AUTO_LENGTH, addon, NULL, &fcn );
     93 	assert( status == napi_ok );
     94 	return fcn;
     95 }
     96 
     97 NAPI_MODULE( NODE_GYP_MODULE_NAME, init )