|
| 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