ffi: accept safe integer numbers for int64 and uint64 arguments
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 25/100
- Loại issue
- Tính năng
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Đình trệ
- Công nghệ
- javascript, node.js
- Lĩnh vực
- backend-api-design
Hướng nghiên cứu
Start by reviewing related PR #66197 and the FFI argument conversion paths it changes. Check the Fast API, SharedBuffer, and generic argument conversion tests described in the issue. Done means safe integer numbers are accepted for int64 and uint64 arguments with the stated range checks, while bigint behavior, return values, and existing tests remain correct.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
What is the problem this feature will solve?
Currently, int64 and uint64 FFI arguments require JavaScript bigint values. Passing a number is rejected even when it is a small integer that can be represented exactly by both JavaScript and the native type.
This adds an explicit conversion at the call site when the value is already available as a number. A common example is passing a buffer and its length to a native function:
uint64_t sum_buffer(const uint8_t* buffer, uint64_t length);
const { functions } = ffi.dlopen(libraryPath, {
sum_buffer: {
arguments: ['buffer', 'u64'],
return: 'u64',
},
});
const bytes = new Uint8Array([1, 2, 3]);
functions.sum_buffer(bytes, bytes.byteLength); // ERR_INVALID_ARG_VALUE
functions.sum_buffer(bytes, BigInt(bytes.byteLength)); // 6n
bytes.byteLength is 3, but the caller still needs to wrap it in BigInt() because the native parameter is declared as uint64_t.
The same issue applies to arguments unrelated to buffers, such as counters or offsets:
const { functions } = ffi.dlopen(libraryPath, {
set_counter: {
arguments: ['u64'],
return: 'void',
},
});
const count = 100;
functions.set_count(count); // ERR_INVALID_ARG_VALUE
functions.set_counter(BigInt(count)); // 100
Requiring bigint makes sense when a value is outside JavaScript's safe integer range. For values already within that range, however, it seems useful to let the runtime validate and convert them rather than requiring callers to do so explicitly.
What is the feature you are proposing to solve the problem?
Allow safe integer numbers as int64 and uint64 FFI arguments, alongside the existing bigint support.
The accepted number ranges would be:
- int64 / i64:
Number.MIN_SAFE_INTEGERthroughNumber.MAX_SAFE_INTEGER. - uint64 / u64: 0 through
Number.MAX_SAFE_INTEGER.
Node.js would validate the number before converting it internally. Fractional numbers, NaN, infinities, and numbers outside the accepted range would throw ERR_INVALID_ARG_VALUE. Negative numbers would also be rejected for uint64.
functions.sum_buffer(bytes, bytes.byteLength);
functions.set_counter(100);
Existing bigint arguments would continue to work, including values beyond the safe integer range that fit within the declared native type:
functions.set_counter(100); // Accepted
functions.set_counter(100n); // Still accepted
functions.set_counter((2n ** 64n) - 1n); // Still accepted
functions.set_counter(-1); // Rejected
functions.set_counter(1.5); // Rejected
functions.set_counter(Number.MAX_SAFE_INTEGER + 1); // Rejected
functions.set_counter(2n ** 64n); // Rejected
The number restriction is intentional: converting an unsafe number to bigint cannot recover precision that may already have been lost. Callers would still need bigint for integers outside the safe integer range.
This proposal only changes which values are accepted as native function arguments. It does not change 64-bit return values, which would remain bigint, or infer a buffer length automatically.
I've opened a related PR #66197 with the implementation, documentation, and tests covering the Fast API, SharedBuffer, and generic argument conversion paths. A review of the proposed behavior and implementation would be appreciated.
What alternatives have you considered?
I haven't identified another approach that addresses this without changing the accepted argument types. Callers can continue using BigInt() explicitly, or put those conversions in their own wrapper functions.
Both approaches work, but callers still need to handle the conversion whenever an existing number is passed to an int64 or uint64 parameter. For example, passing buffer.byteLength requires an extra BigInt() call even though the length is already a suitable integer.
Accepting safe integer numbers would remove that extra step for common calls while retaining explicit range checks and bigint support for larger values.
- Ngôn ngữ chính
- JavaScript
- Star
- 122k
- Fork
- 37.4k
- Merge trung bình
- 4 ngày 3 giờ
- Pull request đã merge (30 ngày)
- 279
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của nodejs/node
-
doc
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
-
build
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
-
feature request
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
-
Theme loads third-party resources on every page (jsdelivr web font, cdnjs Font Awesome) – GDPR Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
docToolchain/docToolchain#1705 ·
-
Timezone select lists one option per character; duplicate "Failed" reason; shared tracker popover id Đang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
nightscout/nocturne#1414 ·
-
bug v2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
modelcontextprotocol/inspector#2458 · 1 bình luận ·
-
Mend: dependency security vulnerability status: needs triage 🕵️♀️
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
carbon-design-system/ibm-products#9907 ·