From e634a400dc0466a6d4fca0f7213a17249befbca0 Mon Sep 17 00:00:00 2001 From: Minggang Wang Date: Mon, 16 Jun 2025 13:49:02 +0800 Subject: [PATCH 1/5] Support setting domain id for Context --- index.js | 2 +- lib/context.js | 6 ++++-- src/rcl_context_bindings.cpp | 24 +++++++++++++++++++++++- test/test-context.js | 11 +++++++++-- test/types/index.test-d.ts | 3 ++- types/context.d.ts | 5 +++-- 6 files changed, 42 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index f32e69b1..5e65c6ac 100644 --- a/index.js +++ b/index.js @@ -255,7 +255,7 @@ let rcl = { throw new TypeError('argv elements must be strings (and not null).'); } - rclnodejs.init(context.handle, argv); + rclnodejs.init(context.handle, argv, context._domainId); if (_rosVersionChecked) { // no further processing required diff --git a/lib/context.js b/lib/context.js index 0865a86b..f36acf2a 100644 --- a/lib/context.js +++ b/lib/context.js @@ -63,11 +63,13 @@ class Context { * Call rcl.init(context) to initialize this context state for * use in creating nodes, etc. * @constructor + * @param {bigint} - Optional, The domain ID of this context. */ - constructor() { + constructor(domainId) { this._handle = rclnodejs.createContext(); this._isShutdown = false; this._nodes = []; + this._domainId = domainId; Context._instances.push(this); } @@ -222,7 +224,7 @@ class Context { /** * Get the domain ID of this context. - * @returns {Number} domain ID of this context + * @returns {bigint} domain ID of this context */ get domainId() { return rclnodejs.getDomainId(this.handle); diff --git a/src/rcl_context_bindings.cpp b/src/rcl_context_bindings.cpp index 5e1c22db..88f2203d 100644 --- a/src/rcl_context_bindings.cpp +++ b/src/rcl_context_bindings.cpp @@ -18,6 +18,7 @@ #include #include +#include #include #include "macros.h" @@ -35,6 +36,15 @@ Napi::Value Init(const Napi::CallbackInfo& info) { rcl_init_options_init(&init_options, allocator), rcl_get_error_string().str); + RCPPUTILS_SCOPE_EXIT({ + rcl_ret_t fini_ret = rcl_init_options_fini(&init_options); + if (RCL_RET_OK != fini_ret) { + Napi::Error::New(env, rcl_get_error_string().str) + .ThrowAsJavaScriptException(); + rcl_reset_error(); + } + }); + // Preprocess Context RclHandle* context_handle = RclHandle::Unwrap(info[0].As()); rcl_context_t* context = @@ -54,6 +64,18 @@ Napi::Value Init(const Napi::CallbackInfo& info) { snprintf(argv[i], len, "%s", arg.c_str()); } } + // Set up the domain id. + size_t domain_id = RCL_DEFAULT_DOMAIN_ID; + if (info.Length() > 2 && info[2].IsBigInt()) { + bool lossless; + domain_id = info[2].As().Uint64Value(&lossless); + } + rcl_ret_t ret = rcl_init_options_set_domain_id(&init_options, domain_id); + if (RCL_RET_OK != ret) { + Napi::Error::New(env, "failed to set domain id to init options") + .ThrowAsJavaScriptException(); + return env.Undefined(); + } THROW_ERROR_IF_NOT_EQUAL( RCL_RET_OK, @@ -141,7 +163,7 @@ Napi::Value GetDomainId(const Napi::CallbackInfo& info) { return env.Undefined(); } - return Napi::Number::New(env, domain_id); + return Napi::BigInt::New(env, domain_id); } Napi::Object InitContextBindings(Napi::Env env, Napi::Object exports) { diff --git a/test/test-context.js b/test/test-context.js index e1c6bfd4..e995f021 100644 --- a/test/test-context.js +++ b/test/test-context.js @@ -81,9 +81,16 @@ describe('context test suite', function () { assert.strictEqual(context.nodes.length, 0); }); - it('context number id', async function () { + it('context domain id', async function () { + let context = new rclnodejs.Context(BigInt(123)); + await rclnodejs.init(context); + assert.strictEqual(typeof context.domainId, 'bigint'); + assert.strictEqual(context.domainId, BigInt(123)); + }); + + it('context with default domain id', async function () { let context = new rclnodejs.Context(); await rclnodejs.init(context); - assert.strictEqual(typeof context.domainId, 'number'); + assert.strictEqual(typeof context.domainId, 'bigint'); }); }); diff --git a/test/types/index.test-d.ts b/test/types/index.test-d.ts index 33cd0caa..69ef573e 100644 --- a/test/types/index.test-d.ts +++ b/test/types/index.test-d.ts @@ -28,7 +28,8 @@ expectType(rclnodejs.DistroUtils.getDistroName(2105)); // ---- Context ----- expectType(rclnodejs.Context.defaultContext()); -expectType(rclnodejs.Context.defaultContext().domainId()); +expectType(new rclnodejs.Context(123n)); +expectType(rclnodejs.Context.defaultContext().domainId()); // ---- NodeOptions ---- const nodeOptions = new rclnodejs.NodeOptions(); diff --git a/types/context.d.ts b/types/context.d.ts index 07a73b17..713720a2 100644 --- a/types/context.d.ts +++ b/types/context.d.ts @@ -38,8 +38,9 @@ declare module 'rclnodejs' { * Create a new instance in uninitialized state. * Call rcl.init(context) to initialize this context state for * use in creating nodes, etc. + * @param {bigint} - Optional, The domain ID of this context. */ - constructor(); + constructor(domainId?: bigint); /** * Test if this context has not been initialized by rcl.init(context). @@ -92,6 +93,6 @@ declare module 'rclnodejs' { * Get the domain ID of this context. * @returns domain ID of this context */ - domainId(): number; + domainId(): bigint; } } From 13a1c8bce47a748e9de6626e6ab766bb6387174c Mon Sep 17 00:00:00 2001 From: Minggang Wang Date: Mon, 16 Jun 2025 14:03:53 +0800 Subject: [PATCH 2/5] Fix lint error --- src/rcl_graph_bindings.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/rcl_graph_bindings.cpp b/src/rcl_graph_bindings.cpp index 476b561d..51c81528 100644 --- a/src/rcl_graph_bindings.cpp +++ b/src/rcl_graph_bindings.cpp @@ -19,7 +19,6 @@ #include #include -// NOLINTNEXTLINE #include #include "macros.h" From 5a8d100d17809bf0a1d93221d589a700979c756b Mon Sep 17 00:00:00 2001 From: Minggang Wang Date: Mon, 16 Jun 2025 14:18:06 +0800 Subject: [PATCH 3/5] Fix lint error --- src/rcl_context_bindings.cpp | 1 + src/rcl_graph_bindings.cpp | 1 + 2 files changed, 2 insertions(+) diff --git a/src/rcl_context_bindings.cpp b/src/rcl_context_bindings.cpp index 88f2203d..8c6c5952 100644 --- a/src/rcl_context_bindings.cpp +++ b/src/rcl_context_bindings.cpp @@ -19,6 +19,7 @@ #include #include +// NOLINTNEXTLINE #include #include "macros.h" diff --git a/src/rcl_graph_bindings.cpp b/src/rcl_graph_bindings.cpp index 51c81528..476b561d 100644 --- a/src/rcl_graph_bindings.cpp +++ b/src/rcl_graph_bindings.cpp @@ -19,6 +19,7 @@ #include #include +// NOLINTNEXTLINE #include #include "macros.h" From dfd79d8e8ede9433da9b626f467e191d47722843 Mon Sep 17 00:00:00 2001 From: Minggang Wang Date: Mon, 16 Jun 2025 14:38:59 +0800 Subject: [PATCH 4/5] Upgrade to ES2020i for ts --- tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index 84ecd4a4..73795956 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,7 +11,7 @@ "noUnusedParameters": true, "noImplicitReturns": true, "noFallthroughCasesInSwitch": true, - "lib": ["es2017"] + "lib": ["es2020"] }, "include": [ "types/**/*" From 9b100696708e13a3f946f8769304950cddf446cc Mon Sep 17 00:00:00 2001 From: Minggang Wang Date: Mon, 16 Jun 2025 15:10:57 +0800 Subject: [PATCH 5/5] Set target to es2020 --- tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index 73795956..8670aca6 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,7 +2,7 @@ "compilerOptions": { "module": "commonjs", "moduleResolution": "node", - "target": "es6", + "target": "es2020", /* Strict Type-Checking Options */ "strict": true, /* Additional Checks */