Skip to content

Commit 0c116e3

Browse files
Kaushikgtmkgryte
andauthored
feat: add napi/create-int64 and napi/create-uint64
PR-URL: #11428 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: #10922
1 parent 1588bf2 commit 0c116e3

38 files changed

Lines changed: 2468 additions & 0 deletions
Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# create_int64
22+
23+
> Convert a signed 64-bit integer to a Node-API value.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var headerDir = require( '@stdlib/napi/create-int64' );
41+
```
42+
43+
#### headerDir
44+
45+
Absolute file path for the directory containing header files for C APIs.
46+
47+
```javascript
48+
var dir = headerDir;
49+
// returns <string>
50+
```
51+
52+
</section>
53+
54+
<!-- /.usage -->
55+
56+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
57+
58+
<section class="notes">
59+
60+
</section>
61+
62+
<!-- /.notes -->
63+
64+
<!-- Package usage examples. -->
65+
66+
<section class="examples">
67+
68+
## Examples
69+
70+
```javascript
71+
var headerDir = require( '@stdlib/napi/create-int64' );
72+
73+
console.log( headerDir );
74+
// => <string>
75+
```
76+
77+
</section>
78+
79+
<!-- /.examples -->
80+
81+
<!-- C interface documentation. -->
82+
83+
* * *
84+
85+
<section class="c">
86+
87+
## C APIs
88+
89+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
90+
91+
<section class="intro">
92+
93+
</section>
94+
95+
<!-- /.intro -->
96+
97+
<!-- C usage documentation. -->
98+
99+
<section class="usage">
100+
101+
### Usage
102+
103+
```c
104+
#include "stdlib/napi/create_int64.h"
105+
```
106+
107+
#### stdlib_napi_create_int64( env, value, \*out )
108+
109+
Converts a signed 64-bit integer to a Node-API value.
110+
111+
```c
112+
#include "stdlib/napi/create_int64.h"
113+
#include <node_api.h>
114+
115+
static napi_value addon( napi_env env, napi_callback_info info ) {
116+
117+
// ...
118+
119+
napi_value value;
120+
napi_status status = stdlib_napi_create_int64( env, 1, &value );
121+
assert( status == napi_ok );
122+
if ( err != NULL ) {
123+
assert( napi_throw( env, err ) == napi_ok );
124+
return NULL;
125+
}
126+
127+
// ...
128+
}
129+
```
130+
131+
The function accepts the following arguments:
132+
133+
- **env**: `[in] napi_env` environment under which the function is invoked.
134+
- **value**: `[in] int64_t` signed 64-bit integer.
135+
- **out**: `[out] napi_value*` destination for storing output value.
136+
137+
```c
138+
napi_status stdlib_napi_create_int64( const napi_env env, const int64_t value, napi_value *out );
139+
```
140+
141+
The function returns a `napi_status` status code indicating success or failure (returns `napi_ok` if success).
142+
143+
#### STDLIB_NAPI_CREATE_INT64( env, expression, name )
144+
145+
Macro for converting a signed 64-bit integer to a Node-API value.
146+
147+
```c
148+
#include "stdlib/napi/create_int64.h"
149+
#include "stdlib/napi/argv_int64.h"
150+
#include "stdlib/napi/argv.h"
151+
#include <node_api.h>
152+
#include <stdint.h>
153+
154+
static int64_t fcn( const int64_t v ) {
155+
return v;
156+
}
157+
158+
// ...
159+
160+
static napi_value addon( napi_env env, napi_callback_info info ) {
161+
// Retrieve add-on callback arguments:
162+
STDLIB_NAPI_ARGV( env, info, argv, argc, 1 );
163+
164+
// Convert the first argument to a C type:
165+
STDLIB_NAPI_ARGV_INT64( env, value, argv, 0 );
166+
167+
// ...
168+
169+
// Convert a value having a C type to a Node-API value:
170+
STDLIB_NAPI_CREATE_INT64( env, fcn( value ), out );
171+
172+
return out;
173+
}
174+
```
175+
176+
The macro expects the following arguments:
177+
178+
- **env**: environment under which the callback is invoked.
179+
- **expression**: expression returning a signed 64-bit integer.
180+
- **name**: output variable name.
181+
182+
</section>
183+
184+
<!-- /.usage -->
185+
186+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
187+
188+
<section class="notes">
189+
190+
- The generated JavaScript value is a `BigInt` (N-API Version 6+).
191+
192+
</section>
193+
194+
<!-- /.notes -->
195+
196+
<!-- C API usage examples. -->
197+
198+
<section class="examples">
199+
200+
</section>
201+
202+
<!-- /.examples -->
203+
204+
</section>
205+
206+
<!-- /.c -->
207+
208+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
209+
210+
<section class="references">
211+
212+
</section>
213+
214+
<!-- /.references -->
215+
216+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
217+
218+
<section class="related">
219+
220+
</section>
221+
222+
<!-- /.related -->
223+
224+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
225+
226+
<section class="links">
227+
228+
</section>
229+
230+
<!-- /.links -->

0 commit comments

Comments
 (0)