From cd45a264cf4c1b17c100024b7422c4bda6c4667a Mon Sep 17 00:00:00 2001 From: Utkarsh Gupta Date: Thu, 7 Mar 2024 07:19:52 +0530 Subject: [PATCH] Improved implementation --- .../@stdlib/napi/argv-complex128/README.md | 2 +- .../include/stdlib/napi/argv_complex128.h | 2 +- .../napi/argv-complex128/lib/browser.js | 2 +- .../@stdlib/napi/argv-complex128/lib/main.js | 2 +- .../napi/argv-complex128/lib/native.js | 6 +- .../napi/argv-complex128/manifest.json | 4 +- .../@stdlib/napi/argv-complex128/src/main.c | 58 +++++++++++++++++-- .../napi/argv-complex128/test/test.native.js | 10 ++-- 8 files changed, 66 insertions(+), 20 deletions(-) diff --git a/lib/node_modules/@stdlib/napi/argv-complex128/README.md b/lib/node_modules/@stdlib/napi/argv-complex128/README.md index cd19b3bdc3ed..2ac241e20e4f 100644 --- a/lib/node_modules/@stdlib/napi/argv-complex128/README.md +++ b/lib/node_modules/@stdlib/napi/argv-complex128/README.md @@ -119,7 +119,7 @@ static napi_value addon( napi_env env, napi_callback_info info ) { stdlib_complex128_t out; napi_value err; - napi_status status = stdlib_napi_argv_complex128( env, value, &out, "Must be a Complex128 instance.", &err ); + napi_status status = stdlib_napi_argv_complex128( env, value, &out, "Must be a Complex128 number.", &err ); assert( status == napi_ok ); if ( err != NULL ) { assert( napi_throw( env, err ) == napi_ok ); diff --git a/lib/node_modules/@stdlib/napi/argv-complex128/include/stdlib/napi/argv_complex128.h b/lib/node_modules/@stdlib/napi/argv-complex128/include/stdlib/napi/argv_complex128.h index 093261b58c74..6946a44c33dc 100644 --- a/lib/node_modules/@stdlib/napi/argv-complex128/include/stdlib/napi/argv_complex128.h +++ b/lib/node_modules/@stdlib/napi/argv-complex128/include/stdlib/napi/argv_complex128.h @@ -59,7 +59,7 @@ #define STDLIB_NAPI_ARGV_COMPLEX128( env, name, argv, index ) \ napi_value __STDLIB_NAPI_ARGV_COMPLEX128_ERR_ ## name; \ stdlib_complex128_t name; \ - stdlib_napi_argv_complex128( env, argv[ index ], &name, "invalid argument. " STDLIB_NAPI_ARGV_INDEX2ORDINAL( index ) " argument must be a Complex128 instance.", &__STDLIB_NAPI_ARGV_COMPLEX128_ERR_ ## name ); \ + stdlib_napi_argv_complex128( env, argv[ index ], &name, "invalid argument. " STDLIB_NAPI_ARGV_INDEX2ORDINAL( index ) " argument must be a Complex128 object.", &__STDLIB_NAPI_ARGV_COMPLEX128_ERR_ ## name ); \ if ( __STDLIB_NAPI_ARGV_COMPLEX128_ERR_ ## name != NULL ) { \ STDLIB_ASSERT_NAPI_STATUS_OK_RET_NULL( env, napi_throw( env, __STDLIB_NAPI_ARGV_COMPLEX128_ERR_ ## name ), "" ) \ return NULL; \ diff --git a/lib/node_modules/@stdlib/napi/argv-complex128/lib/browser.js b/lib/node_modules/@stdlib/napi/argv-complex128/lib/browser.js index 10019834de32..42a55a142b79 100644 --- a/lib/node_modules/@stdlib/napi/argv-complex128/lib/browser.js +++ b/lib/node_modules/@stdlib/napi/argv-complex128/lib/browser.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2022 The Stdlib Authors. +* Copyright (c) 2024 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. diff --git a/lib/node_modules/@stdlib/napi/argv-complex128/lib/main.js b/lib/node_modules/@stdlib/napi/argv-complex128/lib/main.js index 3507ad752a6f..3650c95099cc 100644 --- a/lib/node_modules/@stdlib/napi/argv-complex128/lib/main.js +++ b/lib/node_modules/@stdlib/napi/argv-complex128/lib/main.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2022 The Stdlib Authors. +* Copyright (c) 2024 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. diff --git a/lib/node_modules/@stdlib/napi/argv-complex128/lib/native.js b/lib/node_modules/@stdlib/napi/argv-complex128/lib/native.js index 7a80498ca984..94196579ccb5 100644 --- a/lib/node_modules/@stdlib/napi/argv-complex128/lib/native.js +++ b/lib/node_modules/@stdlib/napi/argv-complex128/lib/native.js @@ -20,7 +20,6 @@ // MODULES // -var isComplex128 = require( '@stdlib/assert/is-complex128' ); var addon = require( './../src/addon.node' ); @@ -38,12 +37,9 @@ var addon = require( './../src/addon.node' ); * * var x = new Complex128( 5.0, 3.0 ); * -* wrapper( x ); +* wrapper( x ); */ function wrapper( v ) { - if ( isComplex128( v ) ) { - v = { re: v.re, im: v.im }; - } return addon( v ); } diff --git a/lib/node_modules/@stdlib/napi/argv-complex128/manifest.json b/lib/node_modules/@stdlib/napi/argv-complex128/manifest.json index c4d6bb328e87..441cb2e0c457 100644 --- a/lib/node_modules/@stdlib/napi/argv-complex128/manifest.json +++ b/lib/node_modules/@stdlib/napi/argv-complex128/manifest.json @@ -33,8 +33,8 @@ "libraries": [], "libpath": [], "dependencies": [ - "@stdlib/assert/napi/is-complex128", - "@stdlib/assert/napi/status-ok", + "@stdlib/assert/napi/status-ok", + "@stdlib/complex/float64", "@stdlib/napi/argv" ] } diff --git a/lib/node_modules/@stdlib/napi/argv-complex128/src/main.c b/lib/node_modules/@stdlib/napi/argv-complex128/src/main.c index 7004dbcf90f3..9788d8027fa4 100644 --- a/lib/node_modules/@stdlib/napi/argv-complex128/src/main.c +++ b/lib/node_modules/@stdlib/napi/argv-complex128/src/main.c @@ -44,7 +44,7 @@ * * stdlib_complex128_t out; * napi_value err; -* napi_status status = stdlib_napi_argv_complex128( env, value, &out, "Must be a Complex128 instance.", &err ); +* napi_status status = stdlib_napi_argv_complex128( env, value, &out, "Must be a Complex128 number.", &err ); * assert( status == napi_ok ); * if ( err != NULL ) { * assert( napi_throw( env, err ) == napi_ok ); @@ -55,15 +55,63 @@ * } */ napi_status stdlib_napi_argv_complex128( const napi_env env, const napi_value value, stdlib_complex128_t *out, const char *message, napi_value *err ) { - double real; - double imaginary; stdlib_assert_napi_value_is_type( env, value, napi_object, message, err ); if ( *err != NULL ) { return napi_ok; } - STDLIB_ASSERT_NAPI_STATUS_OK_RET_VALUE( env, napi_get_property( env, value, re, &real ), "", napi_ok ) - STDLIB_ASSERT_NAPI_STATUS_OK_RET_VALUE( env, napi_get_property( env, value, im, &imaginary ), "", napi_ok ) + + bool hprop; + status = napi_has_named_property( env, value, "re", &hprop ); + assert( status == napi_ok ); + if ( !hprop ) { + status = napi_throw_type_error( env, NULL, "Invalid argument. The Complex128 object must have a real component." ); + assert( status == napi_ok ); + return NULL; + } + + napi_value re0; + status = napi_get_named_property( env, value, "re", &re0 ); + assert( status == napi_ok ); + + napi_valuetype retype; + status = napi_typeof( env, re0, &retype ); + assert( status == napi_ok ); + if ( retype != napi_number ) { + status = napi_throw_type_error( env, NULL, "Invalid argument. The Complex128 object must have a real component which is a number." ); + assert( status == napi_ok ); + return NULL; + } + + status = napi_has_named_property( env, value, "im", &hprop ); + assert( status == napi_ok ); + if ( !hprop ) { + status = napi_throw_type_error( env, NULL, "Invalid argument. The Complex128 object must have an imaginary component." ); + assert( status == napi_ok ); + return NULL; + } + + napi_value im0; + status = napi_get_named_property( env, argv[ 0 ], "im", &im0 ); + assert( status == napi_ok ); + + napi_valuetype imtype; + status = napi_typeof( env, im0, &imtype ); + assert( status == napi_ok ); + if ( imtype != napi_number ) { + status = napi_throw_type_error( env, NULL, "Invalid argument. The Complex128 object must have an imaginary component which a number." ); + assert( status == napi_ok ); + return NULL; + } + + double real; + status = napi_get_value_double( env, re0, &real ); + assert( status == napi_ok ); + + double imaginary; + status = napi_get_value_double( env, im0, &imaginary ); + assert( status == napi_ok ); + *out = stdlib_complex128( real, imaginary ); return napi_ok; } diff --git a/lib/node_modules/@stdlib/napi/argv-complex128/test/test.native.js b/lib/node_modules/@stdlib/napi/argv-complex128/test/test.native.js index 5f4670346c23..0ab55e4dc065 100644 --- a/lib/node_modules/@stdlib/napi/argv-complex128/test/test.native.js +++ b/lib/node_modules/@stdlib/napi/argv-complex128/test/test.native.js @@ -54,8 +54,6 @@ tape( 'the function throws an error if provided argument which is not a Complex1 null, void 0, [], - { re : 2.0, im: 3.0 }, - new Complex64( 2.0, 3.0 ), ]; for ( i = 0; i < values.length; i++ ) { t.throws( badValue( values[ i ] ), Error, 'throws an error when provided '+values[ i ] ); @@ -77,12 +75,16 @@ tape( 'the function does not throw an error if provided a Complex128 instance', values = [ new Complex128( 1.0, 2.0 ), new Complex128( 2.0, 3.0 ), - new Complex128( 3.0, 4.0 ) + new Complex128( 3.0, 4.0 ), + { re : 4.0, im: 5.0 }, + new Complex64( 5.0, 6.0 ), ]; expected = [ { re: 1.0, im: 2.0 }, { re: 2.0, im: 3.0 }, - { re: 3.0, im: 4.0 }, + { re: 3.0, im: 4.0 }, + { re: 4.0, im: 5.0 }, + { re: 5.0, im: 6.0 }, ]; for ( i = 0; i < values.length; i++ ) { v = wrapper( values[ i ] );