Skip to content

Conversation

@minggangw
Copy link
Member

@minggangw minggangw commented Feb 20, 2025

Per current implementation, the Time/Duration in rclnodejs module may be initialized with two parts, which are seconds and nanoseconds using type of number or string, this is because the range limitation of integer that JavaScript can represents. While, after BigInt introduced, we can change it to type of bigint only, which aligns with other ROS2 clients, like rclpy.

This patch implements:

  1. Leverage BigInt to initialize Time/Duration in nanoseconds from JavaScript side.
  2. Change Clock and TimeSource accordingly.
  3. Get the period from v8::BigInt object as int64_t and pass it to rcl from C++ side.
  4. Update the related unit tests accordingly.
  5. Remove dependency, int64-napi, which was used to pass int64_t from JavaScript to C++.

Fix: #1040

@minggangw minggangw requested a review from Copilot February 20, 2025 13:59
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 12 out of 12 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (6)

lib/duration.js:40

  • While the upper bound is checked, consider whether negative duration values should also be validated against a lower bound (if the underlying C time representation has one) or be documented as allowed.
if (total >= 2n ** 63n) {

lib/time.js:99

  • Verify that using BigInt division in the secondsAndNanoseconds getter yields the correct integer seconds when dividing by S_TO_NS, particularly for very large nanosecond values.
const nanoseconds = this._nanoseconds;

lib/node.js:1474

  • Converting the BigInt seconds value to a Number may lose precision for unexpectedly large time values; please confirm that this conversion is acceptable in this context.
sec: Number(secondsAndNanos.seconds),

lib/action/server.js:271

  • Ensure that converting the BigInt seconds from secondsAndNanos to a Number does not introduce precision loss when interfacing with the underlying C time point, especially in edge cases.
sec: Number(secondsAndNanos.seconds),

test/test-time-source.js:75

  • [nitpick] Consider including an additional test to verify the behavior when the difference exceeds the 1e9 nanoseconds threshold, ensuring that the comparison logic remains robust for larger time differences.
assert.ok(sysNow.nanoseconds - now.nanoseconds < 10n ** 9n);

lib/time_source.js:65

  • The conditional is incorrectly checking the node type; replace it with 'if (!(node instanceof rclnodejs.ShadowNode))' to properly validate that the provided node is of the correct type.
if ((!node) instanceof rclnodejs.ShadowNode) {

@minggangw minggangw changed the title Levrage BigInt for Time/TimeSource/Duration Leverage BigInt for Time/TimeSource/Duration Feb 21, 2025
@minggangw minggangw requested a review from Copilot February 24, 2025 03:22
@minggangw minggangw changed the title Leverage BigInt for Time/TimeSource/Duration Leverage BigInt for Time/Duration Feb 24, 2025
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 12 out of 12 changed files in this pull request and generated no comments.

@minggangw minggangw merged commit 1412425 into RobotWebTools:develop Feb 24, 2025
6 checks passed
minggangw added a commit that referenced this pull request Feb 25, 2025
This patch updates the `.d.ts` files according to the following PRs:

1. Change timer period to type of bigint in nanoseconds #1038
2. Leverage BigInt for Time/Duration #1039

Fix: #1041
minggangw added a commit that referenced this pull request Feb 25, 2025
Per current implementation, the `Time`/`Duration` in rclnodejs module may be initialized with two parts, which are `seconds` and `nanoseconds` using type of `number` or `string`, this is because the range limitation of integer that JavaScript can [represents](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER). While, after [`BigInt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) introduced, we can change it to type of `bigint` only, which aligns with other ROS2 clients, like `rclpy`.

This patch implements:

1. Leverage `BigInt` to initialize `Time`/`Duration` in nanoseconds from JavaScript side.
2. Change `Clock` and `TimeSource` accordingly.
3. Get the period from `v8::BigInt` object as `int64_t` and pass it to `rcl` from C++ side.
4. Update the related unit tests accordingly.
5. Remove dependency, `int64-napi`, which was used to pass `int64_t` from JavaScript to C++.

Fix: #1040
minggangw added a commit that referenced this pull request Feb 25, 2025
This patch updates the `.d.ts` files according to the following PRs:

1. Change timer period to type of bigint in nanoseconds #1038
2. Leverage BigInt for Time/Duration #1039

Fix: #1041
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Leverage BigInt for Time/TimeSource/Duration

1 participant