Skip to content

Commit 42785df

Browse files
authored
Support setting domain id for Context (#1164)
This PR adds support for setting the domain id for the Context, ensuring that the domain id is handled and returned as a BigInt throughout the API. Key changes include: - Adjusting type assertions in tests to expect BigInt for domain ids. - Updating the C++ bindings to accept an optional BigInt argument and return a BigInt. - Modifying the Context API and initialization code in JavaScript to handle the new domain id. Fix: #1163
1 parent 4cff06a commit 42785df

File tree

7 files changed

+45
-11
lines changed

7 files changed

+45
-11
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ let rcl = {
255255
throw new TypeError('argv elements must be strings (and not null).');
256256
}
257257

258-
rclnodejs.init(context.handle, argv);
258+
rclnodejs.init(context.handle, argv, context._domainId);
259259

260260
if (_rosVersionChecked) {
261261
// no further processing required

lib/context.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,13 @@ class Context {
6363
* Call rcl.init(context) to initialize this context state for
6464
* use in creating nodes, etc.
6565
* @constructor
66+
* @param {bigint} - Optional, The domain ID of this context.
6667
*/
67-
constructor() {
68+
constructor(domainId) {
6869
this._handle = rclnodejs.createContext();
6970
this._isShutdown = false;
7071
this._nodes = [];
72+
this._domainId = domainId;
7173
Context._instances.push(this);
7274
}
7375

@@ -222,7 +224,7 @@ class Context {
222224

223225
/**
224226
* Get the domain ID of this context.
225-
* @returns {Number} domain ID of this context
227+
* @returns {bigint} domain ID of this context
226228
*/
227229
get domainId() {
228230
return rclnodejs.getDomainId(this.handle);

src/rcl_context_bindings.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include <rcl/rcl.h>
1919

2020
#include <cstdio>
21+
#include <rcpputils/scope_exit.hpp>
22+
// NOLINTNEXTLINE
2123
#include <string>
2224

2325
#include "macros.h"
@@ -35,6 +37,15 @@ Napi::Value Init(const Napi::CallbackInfo& info) {
3537
rcl_init_options_init(&init_options, allocator),
3638
rcl_get_error_string().str);
3739

40+
RCPPUTILS_SCOPE_EXIT({
41+
rcl_ret_t fini_ret = rcl_init_options_fini(&init_options);
42+
if (RCL_RET_OK != fini_ret) {
43+
Napi::Error::New(env, rcl_get_error_string().str)
44+
.ThrowAsJavaScriptException();
45+
rcl_reset_error();
46+
}
47+
});
48+
3849
// Preprocess Context
3950
RclHandle* context_handle = RclHandle::Unwrap(info[0].As<Napi::Object>());
4051
rcl_context_t* context =
@@ -54,6 +65,18 @@ Napi::Value Init(const Napi::CallbackInfo& info) {
5465
snprintf(argv[i], len, "%s", arg.c_str());
5566
}
5667
}
68+
// Set up the domain id.
69+
size_t domain_id = RCL_DEFAULT_DOMAIN_ID;
70+
if (info.Length() > 2 && info[2].IsBigInt()) {
71+
bool lossless;
72+
domain_id = info[2].As<Napi::BigInt>().Uint64Value(&lossless);
73+
}
74+
rcl_ret_t ret = rcl_init_options_set_domain_id(&init_options, domain_id);
75+
if (RCL_RET_OK != ret) {
76+
Napi::Error::New(env, "failed to set domain id to init options")
77+
.ThrowAsJavaScriptException();
78+
return env.Undefined();
79+
}
5780

5881
THROW_ERROR_IF_NOT_EQUAL(
5982
RCL_RET_OK,
@@ -141,7 +164,7 @@ Napi::Value GetDomainId(const Napi::CallbackInfo& info) {
141164
return env.Undefined();
142165
}
143166

144-
return Napi::Number::New(env, domain_id);
167+
return Napi::BigInt::New(env, domain_id);
145168
}
146169

147170
Napi::Object InitContextBindings(Napi::Env env, Napi::Object exports) {

test/test-context.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,16 @@ describe('context test suite', function () {
8181
assert.strictEqual(context.nodes.length, 0);
8282
});
8383

84-
it('context number id', async function () {
84+
it('context domain id', async function () {
85+
let context = new rclnodejs.Context(BigInt(123));
86+
await rclnodejs.init(context);
87+
assert.strictEqual(typeof context.domainId, 'bigint');
88+
assert.strictEqual(context.domainId, BigInt(123));
89+
});
90+
91+
it('context with default domain id', async function () {
8592
let context = new rclnodejs.Context();
8693
await rclnodejs.init(context);
87-
assert.strictEqual(typeof context.domainId, 'number');
94+
assert.strictEqual(typeof context.domainId, 'bigint');
8895
});
8996
});

test/types/index.test-d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ expectType<string | undefined>(rclnodejs.DistroUtils.getDistroName(2105));
2828

2929
// ---- Context -----
3030
expectType<rclnodejs.Context>(rclnodejs.Context.defaultContext());
31-
expectType<number>(rclnodejs.Context.defaultContext().domainId());
31+
expectType<rclnodejs.Context>(new rclnodejs.Context(123n));
32+
expectType<bigint>(rclnodejs.Context.defaultContext().domainId());
3233

3334
// ---- NodeOptions ----
3435
const nodeOptions = new rclnodejs.NodeOptions();

tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"compilerOptions": {
33
"module": "commonjs",
44
"moduleResolution": "node",
5-
"target": "es6",
5+
"target": "es2020",
66
/* Strict Type-Checking Options */
77
"strict": true,
88
/* Additional Checks */
@@ -11,7 +11,7 @@
1111
"noUnusedParameters": true,
1212
"noImplicitReturns": true,
1313
"noFallthroughCasesInSwitch": true,
14-
"lib": ["es2017"]
14+
"lib": ["es2020"]
1515
},
1616
"include": [
1717
"types/**/*"

types/context.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ declare module 'rclnodejs' {
3838
* Create a new instance in uninitialized state.
3939
* Call rcl.init(context) to initialize this context state for
4040
* use in creating nodes, etc.
41+
* @param {bigint} - Optional, The domain ID of this context.
4142
*/
42-
constructor();
43+
constructor(domainId?: bigint);
4344

4445
/**
4546
* Test if this context has not been initialized by rcl.init(context).
@@ -92,6 +93,6 @@ declare module 'rclnodejs' {
9293
* Get the domain ID of this context.
9394
* @returns domain ID of this context
9495
*/
95-
domainId(): number;
96+
domainId(): bigint;
9697
}
9798
}

0 commit comments

Comments
 (0)