Skip to content

Commit cbaae4c

Browse files
committed
feat: add DZ_Z macro
--- 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: na - 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 ---
1 parent d2164b1 commit cbaae4c

File tree

4 files changed

+233
-0
lines changed

4 files changed

+233
-0
lines changed

lib/node_modules/@stdlib/math/base/napi/binary/include/stdlib/math/base/napi/binary.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "stdlib/math/base/napi/binary/ci_c.h"
2626
#include "stdlib/math/base/napi/binary/dd_d.h"
2727
#include "stdlib/math/base/napi/binary/di_d.h"
28+
#include "stdlib/math/base/napi/binary/dz_z.h"
2829
#include "stdlib/math/base/napi/binary/ff_f.h"
2930
#include "stdlib/math/base/napi/binary/fi_f.h"
3031
#include "stdlib/math/base/napi/binary/id_d.h"
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
#ifndef STDLIB_MATH_BASE_NAPI_BINARY_DZ_Z_H
20+
#define STDLIB_MATH_BASE_NAPI_BINARY_DZ_Z_H
21+
22+
#include "stdlib/complex/float64/ctor.h"
23+
#include <node_api.h>
24+
#include <assert.h>
25+
26+
/**
27+
* Macro for registering a Node-API module exporting an interface invoking a binary function accepting a double-precision floating-point number and a double-precision complex floating-point number and returning a double-precision complex floating-point number.
28+
*
29+
* @param fcn binary function
30+
*
31+
* @example
32+
* #include "stdlib/complex/float64/ctor.h"
33+
* #include "stdlib/complex/float64/reim.h"
34+
*
35+
* static stdlib_complex128_t mul( const double n, const stdlib_complex128_t x ) {
36+
* double re;
37+
* double im;
38+
*
39+
* stdlib_complex128_reim( x, &re, &im );
40+
* return stdlib_complex128( re*n, im*n );
41+
* }
42+
*
43+
* // ...
44+
*
45+
* // Register a Node-API module:
46+
* STDLIB_MATH_BASE_NAPI_MODULE_DZ_Z( mul );
47+
*/
48+
#define STDLIB_MATH_BASE_NAPI_MODULE_DZ_Z( fcn ) \
49+
static napi_value stdlib_math_base_napi_dz_z_wrapper( \
50+
napi_env env, \
51+
napi_callback_info info \
52+
) { \
53+
return stdlib_math_base_napi_dz_z( env, info, fcn ); \
54+
}; \
55+
static napi_value stdlib_math_base_napi_dz_z_init( \
56+
napi_env env, \
57+
napi_value exports \
58+
) { \
59+
napi_value fcn; \
60+
napi_status status = napi_create_function( \
61+
env, \
62+
"exports", \
63+
NAPI_AUTO_LENGTH, \
64+
stdlib_math_base_napi_dz_z_wrapper, \
65+
NULL, \
66+
&fcn \
67+
); \
68+
assert( status == napi_ok ); \
69+
return fcn; \
70+
}; \
71+
NAPI_MODULE( NODE_GYP_MODULE_NAME, stdlib_math_base_napi_dz_z_init )
72+
73+
/*
74+
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
75+
*/
76+
#ifdef __cplusplus
77+
extern "C" {
78+
#endif
79+
80+
/**
81+
* Invokes a binary function accepting a double-precision floating-point number and a double-precision complex floating-point number and returning a double-precision complex floating-point number.
82+
*/
83+
napi_value stdlib_math_base_napi_dz_z( napi_env env, napi_callback_info info, stdlib_complex128_t (*fcn)( double, stdlib_complex128_t ) );
84+
85+
#ifdef __cplusplus
86+
}
87+
#endif
88+
89+
#endif // !STDLIB_MATH_BASE_NAPI_BINARY_DZ_Z_H

lib/node_modules/@stdlib/math/base/napi/binary/manifest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"./src/ci_c.c",
3131
"./src/dd_d.c",
3232
"./src/di_d.c",
33+
"./src/dz_z.c",
3334
"./src/ff_f.c",
3435
"./src/fi_f.c",
3536
"./src/id_d.c",
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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/dz_z.h"
20+
#include "stdlib/complex/float64/ctor.h"
21+
#include "stdlib/complex/float64/reim.h"
22+
#include <node_api.h>
23+
#include <assert.h>
24+
25+
/**
26+
* Invokes a binary function accepting a double-precision floating-point number and a double-precision complex floating-point number and returning a double-precision complex floating-point number.
27+
*
28+
* ## Notes
29+
*
30+
* - This function expects that the callback `info` argument provides access to the following JavaScript arguments:
31+
*
32+
* - `x`: input value.
33+
* - `y`: input value.
34+
*
35+
* @param env environment under which the function is invoked
36+
* @param info callback data
37+
* @param fcn binary function
38+
* @return function return value as a Node-API complex-like object
39+
*/
40+
napi_value stdlib_math_base_napi_dz_z( napi_env env, napi_callback_info info, stdlib_complex128_t (*fcn)( double, stdlib_complex128_t ) ) {
41+
napi_status status;
42+
43+
size_t argc = 2;
44+
napi_value argv[ 2 ];
45+
status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
46+
assert( status == napi_ok );
47+
48+
if ( argc < 2 ) {
49+
status = napi_throw_error( env, NULL, "invalid invocation. Must provide two arguments." );
50+
assert( status == napi_ok );
51+
return NULL;
52+
}
53+
54+
napi_valuetype vtype0;
55+
status = napi_typeof( env, argv[ 0 ], &vtype0 );
56+
assert( status == napi_ok );
57+
if ( vtype0 != napi_number ) {
58+
status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a number." );
59+
assert( status == napi_ok );
60+
return NULL;
61+
}
62+
63+
bool hprop;
64+
status = napi_has_named_property( env, argv[ 1 ], "re", &hprop );
65+
assert( status == napi_ok );
66+
if ( !hprop ) {
67+
status = napi_throw_type_error( env, NULL, "invalid argument. Second argument must have a real component." );
68+
assert( status == napi_ok );
69+
return NULL;
70+
}
71+
72+
napi_value xre;
73+
status = napi_get_named_property( env, argv[ 1 ], "re", &xre );
74+
assert( status == napi_ok );
75+
76+
napi_valuetype xretype;
77+
status = napi_typeof( env, xre, &xretype );
78+
assert( status == napi_ok );
79+
if ( xretype != napi_number ) {
80+
status = napi_throw_type_error( env, NULL, "invalid argument. Second argument must have a real component which is a number." );
81+
assert( status == napi_ok );
82+
return NULL;
83+
}
84+
85+
status = napi_has_named_property( env, argv[ 1 ], "im", &hprop );
86+
assert( status == napi_ok );
87+
if ( !hprop ) {
88+
status = napi_throw_type_error( env, NULL, "invalid argument. Second argument must have an imaginary component." );
89+
assert( status == napi_ok );
90+
return NULL;
91+
}
92+
93+
napi_value xim;
94+
status = napi_get_named_property( env, argv[ 1 ], "im", &xim );
95+
assert( status == napi_ok );
96+
97+
napi_valuetype ximtype;
98+
status = napi_typeof( env, xim, &ximtype );
99+
assert( status == napi_ok );
100+
if ( ximtype != napi_number ) {
101+
status = napi_throw_type_error( env, NULL, "invalid argument. Second argument must have an imaginary component which a number." );
102+
assert( status == napi_ok );
103+
return NULL;
104+
}
105+
106+
double y;
107+
status = napi_get_value_double( env, argv[ 0 ], &y );
108+
assert( status == napi_ok );
109+
110+
double re0;
111+
status = napi_get_value_double( env, xre, &re0 );
112+
assert( status == napi_ok );
113+
114+
double im0;
115+
status = napi_get_value_double( env, xim, &im0 );
116+
assert( status == napi_ok );
117+
118+
stdlib_complex128_t v = fcn( y, stdlib_complex128( re0, im0 ) );
119+
double re;
120+
double im;
121+
stdlib_complex128_reim( v, &re, &im );
122+
123+
napi_value obj;
124+
status = napi_create_object( env, &obj );
125+
assert( status == napi_ok );
126+
127+
napi_value vre;
128+
status = napi_create_double( env, re, &vre );
129+
assert( status == napi_ok );
130+
131+
status = napi_set_named_property( env, obj, "re", vre );
132+
assert( status == napi_ok );
133+
134+
napi_value vim;
135+
status = napi_create_double( env, im, &vim );
136+
assert( status == napi_ok );
137+
138+
status = napi_set_named_property( env, obj, "im", vim );
139+
assert( status == napi_ok );
140+
141+
return obj;
142+
}

0 commit comments

Comments
 (0)