Skip to content

Commit 00e73fd

Browse files
committed
feat: add support for BigInt callback arguments
--- 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: passed - 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: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 850600f commit 00e73fd

3 files changed

Lines changed: 37 additions & 3 deletions

File tree

lib/node_modules/@stdlib/napi/argv-int64/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ The function accepts the following arguments:
137137
- **value**: `[in] napi_value` Node-API value.
138138
- **out**: `[out] int64_t*` destination for storing output value.
139139
- **message**: `[in] char*` error message.
140-
- **err**: `[out] napi_value*` pointer for storing a JavaScript error. If not provided a number, the function sets `err` with a JavaScript error; otherwise, `err` is set to `NULL`.
140+
- **err**: `[out] napi_value*` pointer for storing a JavaScript error. If not provided a number or `BigInt`, the function sets `err` with a JavaScript error; otherwise, `err` is set to `NULL`.
141141
142142
```c
143143
napi_status stdlib_napi_argv_int64( const napi_env env, const napi_value value, int64_t *out, const char *message, napi_value *err );
@@ -189,6 +189,10 @@ The macro expects the following arguments:
189189
190190
<section class="notes">
191191
192+
## Notes
193+
194+
- The function supports the conversion of both `number` and `BigInt` add-on callback arguments.
195+
192196
</section>
193197
194198
<!-- /.notes -->

lib/node_modules/@stdlib/napi/argv-int64/src/main.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "stdlib/assert/napi/is_type.h"
2222
#include <node_api.h>
2323
#include <stdint.h>
24+
#include <stdbool.h>
2425

2526
/**
2627
* Converts a Node-API value to a signed 64-bit integer.
@@ -55,10 +56,18 @@
5556
* }
5657
*/
5758
napi_status stdlib_napi_argv_int64( const napi_env env, const napi_value value, int64_t *out, const char *message, napi_value *err ) {
59+
bool lossless = true;
60+
5861
stdlib_assert_napi_value_is_type( env, value, napi_number, message, err );
59-
if ( *err != NULL ) {
62+
if ( *err == NULL ) {
63+
STDLIB_ASSERT_NAPI_STATUS_OK_RET_VALUE( env, napi_get_value_int64( env, value, out ), "", napi_ok )
64+
return napi_ok;
65+
}
66+
*err = NULL;
67+
stdlib_assert_napi_value_is_type( env, value, napi_bigint, message, err );
68+
if ( *err == NULL ) {
69+
STDLIB_ASSERT_NAPI_STATUS_OK_RET_VALUE( env, napi_get_value_bigint_int64( env, value, out, &lossless ), "", napi_ok )
6070
return napi_ok;
6171
}
62-
STDLIB_ASSERT_NAPI_STATUS_OK_RET_VALUE( env, napi_get_value_int64( env, value, out ), "", napi_ok )
6372
return napi_ok;
6473
}

lib/node_modules/@stdlib/napi/argv-int64/test/test.native.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222

2323
var resolve = require( 'path' ).resolve;
2424
var tape = require( 'tape' );
25+
var Number = require( '@stdlib/number/ctor' );
26+
var BigInt = require( '@stdlib/bigint/ctor' );
27+
var hasBigIntSupport = require( '@stdlib/assert/has-bigint-support' );
2528
var tryRequire = require( '@stdlib/utils/try-require' );
2629

2730

@@ -82,3 +85,21 @@ tape( 'the function does not throw an error if provided a number', opts, functio
8285
}
8386
t.end();
8487
});
88+
89+
opts.skip = opts.skip || !hasBigIntSupport();
90+
tape( 'the function does not throw an error if provided a BigInt', opts, function test( t ) {
91+
var values;
92+
var v;
93+
var i;
94+
95+
values = [
96+
BigInt( 5 ),
97+
BigInt( -5 ),
98+
BigInt( 0 )
99+
];
100+
for ( i = 0; i < values.length; i++ ) {
101+
v = addon( values[ i ] );
102+
t.strictEqual( v, Number( values[ i ] ), 'returns expected value when provided '+values[ i ] );
103+
}
104+
t.end();
105+
});

0 commit comments

Comments
 (0)