Skip to content

Commit

Permalink
Improved implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Utkarsh Gupta authored and Utkarsh Gupta committed Mar 7, 2024
1 parent d8c118f commit cd45a26
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lib/node_modules/@stdlib/napi/argv-complex128/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; \
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lib/node_modules/@stdlib/napi/argv-complex128/lib/main.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
6 changes: 1 addition & 5 deletions lib/node_modules/@stdlib/napi/argv-complex128/lib/native.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

// MODULES //

var isComplex128 = require( '@stdlib/assert/is-complex128' );
var addon = require( './../src/addon.node' );


Expand All @@ -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 );
}

Expand Down
4 changes: 2 additions & 2 deletions lib/node_modules/@stdlib/napi/argv-complex128/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
Expand Down
58 changes: 53 additions & 5 deletions lib/node_modules/@stdlib/napi/argv-complex128/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ tape( 'the function throws an error if provided argument which is not a Complex1
null,
void 0,
[],

Check failure on line 56 in lib/node_modules/@stdlib/napi/argv-complex128/test/test.native.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected trailing comma
{ 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 ] );
Expand All @@ -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 },

Check failure on line 79 in lib/node_modules/@stdlib/napi/argv-complex128/test/test.native.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Expected a line break after this opening brace

Check failure on line 79 in lib/node_modules/@stdlib/napi/argv-complex128/test/test.native.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unquoted property 're' found

Check failure on line 79 in lib/node_modules/@stdlib/napi/argv-complex128/test/test.native.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Extra space after key 're'

Check failure on line 79 in lib/node_modules/@stdlib/napi/argv-complex128/test/test.native.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Object properties must go on a new line

Check failure on line 79 in lib/node_modules/@stdlib/napi/argv-complex128/test/test.native.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unquoted property 'im' found

Check failure on line 79 in lib/node_modules/@stdlib/napi/argv-complex128/test/test.native.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Expected a line break before this closing brace
new Complex64( 5.0, 6.0 ),

Check failure on line 80 in lib/node_modules/@stdlib/napi/argv-complex128/test/test.native.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected trailing comma
];
expected = [

Check failure on line 82 in lib/node_modules/@stdlib/napi/argv-complex128/test/test.native.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

'expected' is not defined
{ re: 1.0, im: 2.0 },

Check failure on line 83 in lib/node_modules/@stdlib/napi/argv-complex128/test/test.native.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Expected a line break after this opening brace
{ 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 ] );
Expand Down

0 comments on commit cd45a26

Please sign in to comment.