main.c (3577B)
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/unary.h" 20 #include "stdlib/ndarray/base/function_object.h" 21 #include "stdlib/ndarray/base/napi/addon_arguments.h" 22 #include "stdlib/ndarray/ctor.h" 23 #include <node_api.h> 24 #include <stdint.h> 25 #include <assert.h> 26 27 /** 28 * Invokes an ndarray interface which applies a unary callback to an input ndarray based on provided JavaScript arguments. 29 * 30 * ## Notes 31 * 32 * - This function expects that the callback `info` argument provides access to the following JavaScript arguments: 33 * 34 * - `X`: input ndarray data buffer (i.e., typed array) 35 * - `metaX`: `X` serialized meta data 36 * - `Y`: destination ndarray data buffer (i.e., typed array) 37 * - `metaY`: `Y` serialized meta data 38 * 39 * @param env environment under which the function is invoked 40 * @param info callback data 41 * @param obj ndarray function object 42 */ 43 void stdlib_ndarray_napi_unary( napi_env env, napi_callback_info info, const struct ndarrayFunctionObject *obj ) { 44 napi_status status; 45 46 // Total number of input arguments: 47 int64_t nargs = 4; 48 49 // Number of input ndarray arguments: 50 int64_t nin = 1; 51 52 // Get callback arguments: 53 size_t argc = 4; 54 napi_value argv[ 4 ]; 55 status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL ); 56 assert( status == napi_ok ); 57 58 // Check whether we were provided the correct number of arguments: 59 int64_t argc64 = (int64_t)argc; 60 if ( argc64 < nargs ) { 61 status = napi_throw_error( env, NULL, "invalid invocation. Insufficient arguments." ); 62 assert( status == napi_ok ); 63 return; 64 } 65 if ( argc64 > nargs ) { 66 status = napi_throw_error( env, NULL, "invalid invocation. Too many arguments." ); 67 assert( status == napi_ok ); 68 return; 69 } 70 // Process the provided arguments: 71 struct ndarray *arrays[ 2 ]; 72 napi_value err; 73 status = stdlib_ndarray_napi_addon_arguments( env, argv, nargs, nin, arrays, &err ); 74 assert( status == napi_ok ); 75 76 // Check whether processing was successful: 77 if ( err != NULL ) { 78 status = napi_throw( env, err ); 79 assert( status == napi_ok ); 80 return; 81 } 82 // Extract the ndarray data types: 83 int32_t types[] = { 84 stdlib_ndarray_dtype( arrays[ 0 ] ), 85 stdlib_ndarray_dtype( arrays[ 1 ] ) 86 }; 87 // Resolve the ndarray function satisfying the input array types: 88 int64_t idx = stdlib_ndarray_function_dispatch_index_of( obj, types ); 89 90 // Check whether we were able to successfully resolve an ndarray function: 91 if ( idx < 0 ) { 92 status = napi_throw_type_error( env, NULL, "invalid arguments. Unable to resolve an ndarray function supporting the provided array argument data types." ); 93 assert( status == napi_ok ); 94 return; 95 } 96 // Retrieve the ndarray function: 97 ndarrayFcn fcn = obj->functions[ idx ]; 98 99 // Retrieve the associated function data: 100 void *clbk = obj->data[ idx ]; 101 102 // Evaluate the ndarray function: 103 fcn( arrays, clbk ); 104 105 // Free allocated memory: 106 for ( int64_t i = 0; i < 2; i++ ) { 107 stdlib_ndarray_free( arrays[ i ] ); 108 arrays[ i ] = NULL; 109 } 110 return; 111 }