Skip to content

Commit f3f46e9

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 00e73fd commit f3f46e9

3 files changed

Lines changed: 39 additions & 3 deletions

File tree

lib/node_modules/@stdlib/napi/argv-uint32/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] uint32_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_uint32( const napi_env env, const napi_value value, uint32_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-uint32/src/main.c

Lines changed: 13 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 an unsigned 32-bit integer.
@@ -55,10 +56,20 @@
5556
* }
5657
*/
5758
napi_status stdlib_napi_argv_uint32( const napi_env env, const napi_value value, uint32_t *out, const char *message, napi_value *err ) {
59+
bool lossless = true;
60+
uint64_t tmp = 0;
61+
5862
stdlib_assert_napi_value_is_type( env, value, napi_number, message, err );
59-
if ( *err != NULL ) {
63+
if ( *err == NULL ) {
64+
STDLIB_ASSERT_NAPI_STATUS_OK_RET_VALUE( env, napi_get_value_uint32( env, value, out ), "", napi_ok )
65+
return napi_ok;
66+
}
67+
*err = NULL;
68+
stdlib_assert_napi_value_is_type( env, value, napi_bigint, message, err );
69+
if ( *err == NULL ) {
70+
STDLIB_ASSERT_NAPI_STATUS_OK_RET_VALUE( env, napi_get_value_bigint_uint64( env, value, &tmp, &lossless ), "", napi_ok )
71+
*out = (uint32_t)(tmp);
6072
return napi_ok;
6173
}
62-
STDLIB_ASSERT_NAPI_STATUS_OK_RET_VALUE( env, napi_get_value_uint32( env, value, out ), "", napi_ok )
6374
return napi_ok;
6475
}

lib/node_modules/@stdlib/napi/argv-uint32/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( 15 ),
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)