|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2025 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/d_f.h" |
| 20 | +#include <node_api.h> |
| 21 | +#include <assert.h> |
| 22 | + |
| 23 | +/** |
| 24 | +* Invokes a unary function accepting a double-precision floating-point number and returning a single-precision floating-point number. |
| 25 | +* |
| 26 | +* ## Notes |
| 27 | +* |
| 28 | +* - This function expects that the callback `info` argument provides access to the following JavaScript arguments: |
| 29 | +* |
| 30 | +* - `x`: input value. |
| 31 | +* |
| 32 | +* @param env environment under which the function is invoked |
| 33 | +* @param info callback data |
| 34 | +* @param fcn unary function |
| 35 | +* @return function return value as a Node-API double-precision floating-point number |
| 36 | +*/ |
| 37 | +napi_value stdlib_math_base_napi_d_f( napi_env env, napi_callback_info info, float (*fcn)( double ) ) { |
| 38 | + napi_status status; |
| 39 | + |
| 40 | + size_t argc = 1; |
| 41 | + napi_value argv[ 1 ]; |
| 42 | + status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL ); |
| 43 | + assert( status == napi_ok ); |
| 44 | + |
| 45 | + if ( argc < 1 ) { |
| 46 | + status = napi_throw_error( env, NULL, "invalid invocation. Must provide a number." ); |
| 47 | + assert( status == napi_ok ); |
| 48 | + return NULL; |
| 49 | + } |
| 50 | + |
| 51 | + napi_valuetype vtype0; |
| 52 | + status = napi_typeof( env, argv[ 0 ], &vtype0 ); |
| 53 | + assert( status == napi_ok ); |
| 54 | + if ( vtype0 != napi_number ) { |
| 55 | + status = napi_throw_type_error( env, NULL, "invalid argument. Must provide a number." ); |
| 56 | + assert( status == napi_ok ); |
| 57 | + return NULL; |
| 58 | + } |
| 59 | + |
| 60 | + double x; |
| 61 | + status = napi_get_value_double( env, argv[ 0 ], &x ); |
| 62 | + assert( status == napi_ok ); |
| 63 | + |
| 64 | + napi_value v; |
| 65 | + status = napi_create_double( env, (double)fcn( x ), &v ); |
| 66 | + assert( status == napi_ok ); |
| 67 | + |
| 68 | + return v; |
| 69 | +} |
0 commit comments