main.c (4859B)
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/math/base/napi/unary.h" 20 #include <node_api.h> 21 #include <stdint.h> 22 #include <assert.h> 23 24 /** 25 * Invokes a unary function accepting and returning double-precision floating-point numbers. 26 * 27 * ## Notes 28 * 29 * - This function expects that the callback `info` argument provides access to the following JavaScript arguments: 30 * 31 * - `x`: input value. 32 * 33 * @param env environment under which the function is invoked 34 * @param info callback data 35 * @param fcn unary function 36 * @return function return value as a Node-API double-precision floating-point number 37 */ 38 napi_value stdlib_math_base_napi_d_d( napi_env env, napi_callback_info info, double (*fcn)( double ) ) { 39 napi_status status; 40 41 size_t argc = 1; 42 napi_value argv[ 1 ]; 43 status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL ); 44 assert( status == napi_ok ); 45 46 if ( argc < 1 ) { 47 status = napi_throw_error( env, NULL, "invalid invocation. Must provide a number." ); 48 assert( status == napi_ok ); 49 return NULL; 50 } 51 52 napi_valuetype vtype0; 53 status = napi_typeof( env, argv[ 0 ], &vtype0 ); 54 assert( status == napi_ok ); 55 if ( vtype0 != napi_number ) { 56 status = napi_throw_type_error( env, NULL, "invalid argument. Must provide a number." ); 57 assert( status == napi_ok ); 58 return NULL; 59 } 60 61 double x; 62 status = napi_get_value_double( env, argv[ 0 ], &x ); 63 assert( status == napi_ok ); 64 65 napi_value v; 66 status = napi_create_double( env, fcn( x ), &v ); 67 assert( status == napi_ok ); 68 69 return v; 70 } 71 72 /** 73 * Invokes a unary function accepting and returning single-precision floating-point numbers. 74 * 75 * ## Notes 76 * 77 * - This function expects that the callback `info` argument provides access to the following JavaScript arguments: 78 * 79 * - `x`: input value. 80 * 81 * @param env environment under which the function is invoked 82 * @param info callback data 83 * @param fcn unary function 84 * @return function return value as a Node-API double-precision floating-point number 85 */ 86 napi_value stdlib_math_base_napi_f_f( napi_env env, napi_callback_info info, float (*fcn)( float ) ) { 87 napi_status status; 88 89 size_t argc = 1; 90 napi_value argv[ 1 ]; 91 status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL ); 92 assert( status == napi_ok ); 93 94 if ( argc < 1 ) { 95 status = napi_throw_error( env, NULL, "invalid invocation. Must provide a number." ); 96 assert( status == napi_ok ); 97 return NULL; 98 } 99 100 napi_valuetype vtype0; 101 status = napi_typeof( env, argv[ 0 ], &vtype0 ); 102 assert( status == napi_ok ); 103 if ( vtype0 != napi_number ) { 104 status = napi_throw_type_error( env, NULL, "invalid argument. Must provide a number." ); 105 assert( status == napi_ok ); 106 return NULL; 107 } 108 109 double x; 110 status = napi_get_value_double( env, argv[ 0 ], &x ); 111 assert( status == napi_ok ); 112 113 napi_value v; 114 status = napi_create_double( env, (double)fcn( (float)x ), &v ); 115 assert( status == napi_ok ); 116 117 return v; 118 } 119 120 /** 121 * Invokes a unary function accepting and returning 32-bit signed integers. 122 * 123 * ## Notes 124 * 125 * - This function expects that the callback `info` argument provides access to the following JavaScript arguments: 126 * 127 * - `x`: input value. 128 * 129 * @param env environment under which the function is invoked 130 * @param info callback data 131 * @param fcn unary function 132 * @return function return value as a Node-API 32-bit signed integer 133 */ 134 napi_value stdlib_math_base_napi_i_i( napi_env env, napi_callback_info info, int32_t (*fcn)( int32_t ) ) { 135 napi_status status; 136 137 size_t argc = 1; 138 napi_value argv[ 1 ]; 139 status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL ); 140 assert( status == napi_ok ); 141 142 if ( argc < 1 ) { 143 status = napi_throw_error( env, NULL, "invalid invocation. Must provide a number." ); 144 assert( status == napi_ok ); 145 return NULL; 146 } 147 148 napi_valuetype vtype0; 149 status = napi_typeof( env, argv[ 0 ], &vtype0 ); 150 assert( status == napi_ok ); 151 if ( vtype0 != napi_number ) { 152 status = napi_throw_type_error( env, NULL, "invalid argument. Must provide a number." ); 153 assert( status == napi_ok ); 154 return NULL; 155 } 156 157 int32_t x; 158 status = napi_get_value_int32( env, argv[ 0 ], &x ); 159 assert( status == napi_ok ); 160 161 napi_value v; 162 status = napi_create_int32( env, fcn( x ), &v ); 163 assert( status == napi_ok ); 164 165 return v; 166 }