From 692afcb69e47de99d9675571503e6cc7a73dd1a7 Mon Sep 17 00:00:00 2001 From: Neeraj Pathak Date: Fri, 27 Dec 2024 14:30:14 +0530 Subject: [PATCH 1/3] refactor: update `stats/base/dnanmeanpn` native addon from C++ to C PR-URL: https://github.com/stdlib-js/stdlib/pull/4110 Reviewed-by: Athan Reines --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: passed - task: run_c_examples status: passed - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: passed - task: run_c_benchmarks status: passed - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: passed - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: passed --- --- .../stats/base/dnanmeanpn/include.gypi | 2 +- .../stats/base/dnanmeanpn/manifest.json | 39 +++++- .../@stdlib/stats/base/dnanmeanpn/src/addon.c | 43 +++++++ .../stats/base/dnanmeanpn/src/addon.cpp | 117 ------------------ 4 files changed, 82 insertions(+), 119 deletions(-) create mode 100644 lib/node_modules/@stdlib/stats/base/dnanmeanpn/src/addon.c delete mode 100644 lib/node_modules/@stdlib/stats/base/dnanmeanpn/src/addon.cpp diff --git a/lib/node_modules/@stdlib/stats/base/dnanmeanpn/include.gypi b/lib/node_modules/@stdlib/stats/base/dnanmeanpn/include.gypi index 868c5c12e852..26476a8c2655 100644 --- a/lib/node_modules/@stdlib/stats/base/dnanmeanpn/include.gypi +++ b/lib/node_modules/@stdlib/stats/base/dnanmeanpn/include.gypi @@ -36,7 +36,7 @@ # Source files: 'src_files': [ - '<(src_dir)/addon.cpp', + '<(src_dir)/addon.c', ' + +/** +* Receives JavaScript callback invocation data. +* +* @param env environment under which the function is invoked +* @param info callback data +* @return Node-API value +*/ +static napi_value addon( napi_env env, napi_callback_info info ) { + STDLIB_NAPI_ARGV( env, info, argv, argc, 3 ); + STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 ); + STDLIB_NAPI_ARGV_INT64( env, stride, argv, 2 ); + STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, X, N, stride, argv, 1 ); + STDLIB_NAPI_CREATE_DOUBLE( env, stdlib_strided_dnanmeanpn( N, X, stride ), v ); + return v; +} + +STDLIB_NAPI_MODULE_EXPORT_FCN( addon ) diff --git a/lib/node_modules/@stdlib/stats/base/dnanmeanpn/src/addon.cpp b/lib/node_modules/@stdlib/stats/base/dnanmeanpn/src/addon.cpp deleted file mode 100644 index 8c46b762906b..000000000000 --- a/lib/node_modules/@stdlib/stats/base/dnanmeanpn/src/addon.cpp +++ /dev/null @@ -1,117 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2020 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -#include "stdlib/stats/base/dnanmeanpn.h" -#include -#include -#include -#include -#include - -/** -* Add-on namespace. -*/ -namespace stdlib_stats_base_dnanmeanpn { - - /** - * Computes the arithmetic mean of a double-precision floating-point strided array, ignoring `NaN` values and using a two-pass error correction algorithm. - * - * ## Notes - * - * - When called from JavaScript, the function expects three arguments: - * - * - `N`: number of indexed elements - * - `X`: input array - * - `stride`: stride length - */ - napi_value node_dnanmeanpn( napi_env env, napi_callback_info info ) { - napi_status status; - - size_t argc = 3; - napi_value argv[ 3 ]; - status = napi_get_cb_info( env, info, &argc, argv, nullptr, nullptr ); - assert( status == napi_ok ); - - if ( argc < 3 ) { - napi_throw_error( env, nullptr, "invalid invocation. Must provide 3 arguments." ); - return nullptr; - } - - napi_valuetype vtype0; - status = napi_typeof( env, argv[ 0 ], &vtype0 ); - assert( status == napi_ok ); - if ( vtype0 != napi_number ) { - napi_throw_type_error( env, nullptr, "invalid argument. First argument must be a number." ); - return nullptr; - } - - bool res; - status = napi_is_typedarray( env, argv[ 1 ], &res ); - assert( status == napi_ok ); - if ( res == false ) { - napi_throw_type_error( env, nullptr, "invalid argument. Second argument must be a Float64Array." ); - return nullptr; - } - - napi_valuetype vtype2; - status = napi_typeof( env, argv[ 2 ], &vtype2 ); - assert( status == napi_ok ); - if ( vtype2 != napi_number ) { - napi_throw_type_error( env, nullptr, "invalid argument. Third argument must be a number." ); - return nullptr; - } - - int64_t N; - status = napi_get_value_int64( env, argv[ 0 ], &N ); - assert( status == napi_ok ); - - int64_t stride; - status = napi_get_value_int64( env, argv[ 2 ], &stride ); - assert( status == napi_ok ); - - napi_typedarray_type vtype1; - size_t xlen; - void *X; - status = napi_get_typedarray_info( env, argv[ 1 ], &vtype1, &xlen, &X, nullptr, nullptr ); - assert( status == napi_ok ); - if ( vtype1 != napi_float64_array ) { - napi_throw_type_error( env, nullptr, "invalid argument. Second argument must be a Float64Array." ); - return nullptr; - } - if ( (N-1)*llabs(stride) >= (int64_t)xlen ) { - napi_throw_range_error( env, nullptr, "invalid argument. Second argument has insufficient elements based on the associated stride and the number of indexed elements." ); - return nullptr; - } - - napi_value v; - status = napi_create_double( env, stdlib_strided_dnanmeanpn( N, (double *)X, stride ), &v ); - assert( status == napi_ok ); - - return v; - } - - napi_value Init( napi_env env, napi_value exports ) { - napi_status status; - napi_value fcn; - status = napi_create_function( env, "exports", NAPI_AUTO_LENGTH, node_dnanmeanpn, NULL, &fcn ); - assert( status == napi_ok ); - return fcn; - } - - NAPI_MODULE( NODE_GYP_MODULE_NAME, Init ) -} // end namespace stdlib_stats_base_dnanmeanpn From a85317788dba4b2ec3d117911ee51284dc6a6b3b Mon Sep 17 00:00:00 2001 From: JoyBoy <144602492+0PrashantYadav0@users.noreply.github.com> Date: Sat, 4 Jan 2025 11:07:26 +0530 Subject: [PATCH 2/3] feat: add ID_D interface in binary --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na --- --- .../@stdlib/math/base/napi/binary/README.md | 23 +++++++ .../include/stdlib/math/base/napi/binary.h | 47 ++++++++++++++ .../@stdlib/math/base/napi/binary/src/main.c | 62 +++++++++++++++++++ 3 files changed, 132 insertions(+) diff --git a/lib/node_modules/@stdlib/math/base/napi/binary/README.md b/lib/node_modules/@stdlib/math/base/napi/binary/README.md index e1ff3cd3af7a..290e0e760314 100644 --- a/lib/node_modules/@stdlib/math/base/napi/binary/README.md +++ b/lib/node_modules/@stdlib/math/base/napi/binary/README.md @@ -820,6 +820,29 @@ The macro expects the following arguments: When used, this macro should be used **instead of** `NAPI_MODULE`. The macro includes `NAPI_MODULE`, thus ensuring Node-API module registration. +#### STDLIB_MATH_BASE_NAPI_MODULE_ID_D( fcn ) + +Macro for registering a Node-API module exporting an interface invoking a binary function accepting a signed 32-bit integer and a double-precision floating-point number and returning a double-precision floating-point number. + +```c +#include + +static double mul( const int32_t x, const double y ) { + return x * y; +} + +// ... + +// Register a Node-API module: +STDLIB_MATH_BASE_NAPI_MODULE_ID_D( mul ); +``` + +The macro expects the following arguments: + +- **fcn**: `double (*fcn)( int32_t, double )` binary function. + +When used, this macro should be used **instead of** `NAPI_MODULE`. The macro includes `NAPI_MODULE`, thus ensuring Node-API module registration. + #### STDLIB_MATH_BASE_NAPI_MODULE_FI_F( fcn ) Macro for registering a Node-API module exporting an interface invoking a binary function accepting a single-precision floating-point number and a signed 32-bit integer and returning a single-precision floating-point number. diff --git a/lib/node_modules/@stdlib/math/base/napi/binary/include/stdlib/math/base/napi/binary.h b/lib/node_modules/@stdlib/math/base/napi/binary/include/stdlib/math/base/napi/binary.h index 4315f1072eac..13328645083a 100644 --- a/lib/node_modules/@stdlib/math/base/napi/binary/include/stdlib/math/base/napi/binary.h +++ b/lib/node_modules/@stdlib/math/base/napi/binary/include/stdlib/math/base/napi/binary.h @@ -342,6 +342,48 @@ }; \ NAPI_MODULE( NODE_GYP_MODULE_NAME, stdlib_math_base_napi_di_d_init ) +/** +* Macro for registering a Node-API module exporting an interface invoking a binary function accepting a signed 32-bit integer and a double-precision floating-point number and returning a double-precision floating-point number. +* +* @param fcn binary function +* +* @example +* #include +* +* static double mul( const int32_t n, const double x ) { +* return x * n; +* } +* +* // ... +* +* // Register a Node-API module: +* STDLIB_MATH_BASE_NAPI_MODULE_ID_D( mul ); +*/ +#define STDLIB_MATH_BASE_NAPI_MODULE_ID_D( fcn ) \ + static napi_value stdlib_math_base_napi_id_d_wrapper( \ + napi_env env, \ + napi_callback_info info \ + ) { \ + return stdlib_math_base_napi_id_d( env, info, fcn ); \ + }; \ + static napi_value stdlib_math_base_napi_id_d_init( \ + napi_env env, \ + napi_value exports \ + ) { \ + napi_value fcn; \ + napi_status status = napi_create_function( \ + env, \ + "exports", \ + NAPI_AUTO_LENGTH, \ + stdlib_math_base_napi_id_d_wrapper, \ + NULL, \ + &fcn \ + ); \ + assert( status == napi_ok ); \ + return fcn; \ + }; \ + NAPI_MODULE( NODE_GYP_MODULE_NAME, stdlib_math_base_napi_id_d_init ) + /** * Macro for registering a Node-API module exporting an interface invoking a binary function accepting a single-precision floating-point number and a signed 32-bit integer and returning a single-precision floating-point number. * @@ -690,6 +732,11 @@ napi_value stdlib_math_base_napi_cc_c( napi_env env, napi_callback_info info, st */ napi_value stdlib_math_base_napi_di_d( napi_env env, napi_callback_info info, double (*fcn)( double, int32_t ) ); +/** +* Invokes a binary function accepting a signed 32-bit integer and a double-precision floating-point number and returning a double-precision floating-point number. +*/ +napi_value stdlib_math_base_napi_id_d( napi_env env, napi_callback_info info, double (*fcn)( int32_t, double ) ); + /** * Invokes a binary function accepting signed 32-bit integers and returning a double-precision floating-point number. */ diff --git a/lib/node_modules/@stdlib/math/base/napi/binary/src/main.c b/lib/node_modules/@stdlib/math/base/napi/binary/src/main.c index 07d3b89c5f6e..b6de9eb38b48 100644 --- a/lib/node_modules/@stdlib/math/base/napi/binary/src/main.c +++ b/lib/node_modules/@stdlib/math/base/napi/binary/src/main.c @@ -524,6 +524,68 @@ napi_value stdlib_math_base_napi_di_d( napi_env env, napi_callback_info info, do return v; } +/** +* Invokes a binary function accepting a signed 32-bit integer and a double-precision floating-point number and returning a double-precision floating-point number. +* +* ## Notes +* +* - This function expects that the callback `info` argument provides access to the following JavaScript arguments: +* +* - `x`: input value. +* - `y`: input value. +* +* @param env environment under which the function is invoked +* @param info callback data +* @param fcn binary function +* @return function return value as a Node-API double-precision floating-point number +*/ +napi_value stdlib_math_base_napi_id_d( napi_env env, napi_callback_info info, double (*fcn)( int32_t, double ) ) { + napi_status status; + + size_t argc = 2; + napi_value argv[ 2 ]; + status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL ); + assert( status == napi_ok ); + + if ( argc < 2 ) { + status = napi_throw_error( env, NULL, "invalid invocation. Must provide two numbers." ); + assert( status == napi_ok ); + return NULL; + } + + napi_valuetype vtype0; + status = napi_typeof( env, argv[ 0 ], &vtype0 ); + assert( status == napi_ok ); + if ( vtype0 != napi_number ) { + status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a number." ); + assert( status == napi_ok ); + return NULL; + } + + napi_valuetype vtype1; + status = napi_typeof( env, argv[ 1 ], &vtype1 ); + assert( status == napi_ok ); + if ( vtype1 != napi_number ) { + status = napi_throw_type_error( env, NULL, "invalid argument. Second argument must be a number." ); + assert( status == napi_ok ); + return NULL; + } + + int32_t x; + status = napi_get_value_int32( env, argv[ 0 ], &x ); + assert( status == napi_ok ); + + double y; + status = napi_get_value_double( env, argv[ 1 ], &y ); + assert( status == napi_ok ); + + napi_value v; + status = napi_create_double( env, fcn( x, y ), &v ); + assert( status == napi_ok ); + + return v; +} + /** * Invokes a binary function accepting signed 32-bit integers and returning a double-precision floating-point number. * From 8fb4eee67e64e41c07c0905aa6d0eeba7b12013d Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Mon, 6 Jan 2025 01:48:05 -0800 Subject: [PATCH 3/3] docs: add API documentation --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na --- --- .../@stdlib/math/base/napi/binary/README.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/lib/node_modules/@stdlib/math/base/napi/binary/README.md b/lib/node_modules/@stdlib/math/base/napi/binary/README.md index 290e0e760314..f92b10cb0563 100644 --- a/lib/node_modules/@stdlib/math/base/napi/binary/README.md +++ b/lib/node_modules/@stdlib/math/base/napi/binary/README.md @@ -331,6 +331,46 @@ The function accepts the following arguments: void stdlib_math_base_napi_di_d( napi_env env, napi_callback_info info, double (*fcn)( double, int32_t ) ); ``` +#### stdlib_math_base_napi_id_d( env, info, fcn ) + +Invokes a binary function accepting a signed 32-bit integer and a double-precision floating-point number and returning a double-precision floating-point number. + +```c +#include +#include + +// ... + +static double mul( const int32_t x, const double y ) { + return x * y; +} + +// ... + +/** +* Receives JavaScript callback invocation data. +* +* @param env environment under which the function is invoked +* @param info callback data +* @return Node-API value +*/ +napi_value addon( napi_env env, napi_callback_info info ) { + return stdlib_math_base_napi_id_d( env, info, mul ); +} + +// ... +``` + +The function accepts the following arguments: + +- **env**: `[in] napi_env` environment under which the function is invoked. +- **info**: `[in] napi_callback_info` callback data. +- **fcn**: `[in] double (*fcn)( int32_t, double )` binary function. + +```c +void stdlib_math_base_napi_id_d( napi_env env, napi_callback_info info, double (*fcn)( int32_t, double ) ); +``` + #### stdlib_math_base_napi_ii_i( env, info, fcn ) Invokes a binary function accepting and returning signed 32-bit integers.