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