main.c (3387B)
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/unary.h" 20 #include "stdlib/strided/base/function_object.h" 21 #include "stdlib/strided/napi/addon_arguments.h" 22 #include <node_api.h> 23 #include <stdint.h> 24 #include <assert.h> 25 26 /** 27 * Invokes a strided array interface which applies a unary callback to an input strided array based on provided JavaScript arguments. 28 * 29 * ## Notes 30 * 31 * - This function expects that the callback `info` argument provides access to the following JavaScript arguments: 32 * 33 * - `N`: number of indexed elements 34 * - `X`: input array 35 * - `strideX`: `X` stride length 36 * - `Y`: destination array 37 * - `strideY`: `Y` stride length 38 * 39 * @param env environment under which the function is invoked 40 * @param info callback data 41 * @param obj strided array function object 42 */ 43 void stdlib_strided_napi_unary( napi_env env, napi_callback_info info, const struct StridedFunctionObject *obj ) { 44 napi_status status; 45 46 // Total number of input arguments: 47 int64_t nargs = 5; 48 49 // Number of input strided array arguments: 50 int64_t nin = 1; 51 52 // Get callback arguments: 53 size_t argc = 5; 54 napi_value argv[ 5 ]; 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 uint8_t *arrays[ 2 ]; 72 int64_t strides[ 2 ]; 73 int64_t shape[ 1 ]; 74 int32_t types[ 2 ]; 75 76 napi_value err; 77 status = stdlib_strided_napi_addon_arguments( env, argv, nargs, nin, arrays, shape, strides, types, &err ); 78 assert( status == napi_ok ); 79 80 // Check whether processing was successful: 81 if ( err != NULL ) { 82 status = napi_throw( env, err ); 83 assert( status == napi_ok ); 84 return; 85 } 86 // Resolve the strided array function satisfying the input array types: 87 int64_t idx = stdlib_strided_function_dispatch_index_of( obj, types ); 88 89 // Check whether we were able to successfully resolve a strided array function: 90 if ( idx < 0 ) { 91 status = napi_throw_type_error( env, NULL, "invalid arguments. Unable to resolve a strided array function supporting the provided array argument data types." ); 92 assert( status == napi_ok ); 93 return; 94 } 95 // Retrieve the strided array function: 96 StridedArrayFcn fcn = obj->functions[ idx ]; 97 98 // Retrieve the associated function data: 99 void *clbk = obj->data[ idx ]; 100 101 // Evaluate the strided array function: 102 fcn( arrays, shape, strides, clbk ); 103 104 return; 105 }