time-to-botec

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

addon.c (2716B)


      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/strided/napi/addon_arguments.h"
     20 #include <node_api.h>
     21 #include <stdint.h>
     22 #include <assert.h>
     23 
     24 /**
     25 * Receives JavaScript callback invocation data.
     26 *
     27 * @private
     28 * @param env    environment under which the function is invoked
     29 * @param info   callback data
     30 * @return       Node-API value
     31 */
     32 static napi_value addon( napi_env env, napi_callback_info info ) {
     33 	napi_status status;
     34 
     35 	// Total number of input arguments:
     36 	int64_t nargs = 5;
     37 
     38 	// Number of input strided array arguments:
     39 	int64_t nin = 1;
     40 
     41 	// Get callback arguments:
     42 	size_t argc = 5;
     43 	napi_value argv[ 5 ];
     44 	status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
     45 	assert( status == napi_ok );
     46 
     47 	// Check whether we were provided the correct number of arguments:
     48 	int64_t argc64 = (int64_t)argc;
     49 	if ( argc64 < nargs ) {
     50 		status = napi_throw_error( env, NULL, "invalid invocation. Insufficient arguments." );
     51 		assert( status == napi_ok );
     52 		return NULL;
     53 	}
     54 	if ( argc64 > nargs ) {
     55 		status = napi_throw_error( env, NULL, "invalid invocation. Too many arguments." );
     56 		assert( status == napi_ok );
     57 		return NULL;
     58 	}
     59 	// Process the provided arguments:
     60 	uint8_t *arrays[ 2 ];
     61 	int64_t strides[ 2 ];
     62 	int64_t shape[ 1 ];
     63 	int32_t types[ 2 ];
     64 
     65 	napi_value err;
     66 	status = stdlib_strided_napi_addon_arguments( env, argv, nargs, nin, arrays, shape, strides, types, &err );
     67 	assert( status == napi_ok );
     68 
     69 	// Check whether processing was successful:
     70 	if ( err != NULL ) {
     71 		status = napi_throw( env, err );
     72 		assert( status == napi_ok );
     73 		return NULL;
     74 	}
     75 	// Normally, one would then do something with the processed arguments; here, we just return...
     76 	return NULL;
     77 }
     78 
     79 /**
     80 * Initializes a Node-API module.
     81 *
     82 * @private
     83 * @param env      environment under which the function is invoked
     84 * @param exports  exports object
     85 * @return         main export
     86 */
     87 static napi_value init( napi_env env, napi_value exports ) {
     88 	napi_value fcn;
     89 	napi_status status = napi_create_function( env, "exports", NAPI_AUTO_LENGTH, addon, NULL, &fcn );
     90 	assert( status == napi_ok );
     91 	return fcn;
     92 }
     93 
     94 NAPI_MODULE( NODE_GYP_MODULE_NAME, init )