rclnodejs 0.32.5 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/README.md +3 -3
  2. package/binding.gyp +17 -2
  3. package/lib/client.js +2 -3
  4. package/lib/context.js +8 -0
  5. package/lib/logging.js +26 -9
  6. package/lib/node.js +68 -1
  7. package/lib/publisher.js +8 -0
  8. package/lib/service.js +9 -2
  9. package/lib/subscription.js +8 -0
  10. package/lib/timer.js +32 -0
  11. package/package.json +4 -4
  12. package/rostsd_gen/index.js +167 -67
  13. package/scripts/config.js +1 -0
  14. package/scripts/npmjs-readme.md +3 -3
  15. package/src/addon.cpp +54 -53
  16. package/src/executor.cpp +19 -10
  17. package/src/{executor.hpp → executor.h} +7 -5
  18. package/src/handle_manager.cpp +30 -56
  19. package/src/{handle_manager.hpp → handle_manager.h} +8 -7
  20. package/src/{macros.hpp → macros.h} +7 -5
  21. package/src/rcl_action_client_bindings.cpp +231 -0
  22. package/src/{rcl_action_bindings.hpp → rcl_action_client_bindings.h} +6 -11
  23. package/src/rcl_action_goal_bindings.cpp +117 -0
  24. package/src/rcl_action_goal_bindings.h +26 -0
  25. package/src/rcl_action_server_bindings.cpp +470 -0
  26. package/src/rcl_action_server_bindings.h +26 -0
  27. package/src/rcl_bindings.cpp +42 -1979
  28. package/src/{rcl_bindings.hpp → rcl_bindings.h} +5 -25
  29. package/src/rcl_client_bindings.cpp +183 -0
  30. package/src/rcl_client_bindings.h +26 -0
  31. package/src/rcl_context_bindings.cpp +156 -0
  32. package/src/rcl_context_bindings.h +26 -0
  33. package/src/rcl_graph_bindings.cpp +280 -0
  34. package/src/rcl_graph_bindings.h +26 -0
  35. package/src/rcl_guard_condition_bindings.cpp +75 -0
  36. package/src/rcl_guard_condition_bindings.h +26 -0
  37. package/src/rcl_handle.cpp +41 -57
  38. package/src/{rcl_handle.hpp → rcl_handle.h} +18 -17
  39. package/src/rcl_lifecycle_bindings.cpp +135 -114
  40. package/src/{rcl_lifecycle_bindings.hpp → rcl_lifecycle_bindings.h} +5 -7
  41. package/src/rcl_logging_bindings.cpp +96 -0
  42. package/src/rcl_logging_bindings.h +26 -0
  43. package/src/rcl_names_bindings.cpp +255 -0
  44. package/src/rcl_names_bindings.h +26 -0
  45. package/src/rcl_node_bindings.cpp +447 -0
  46. package/src/rcl_node_bindings.h +26 -0
  47. package/src/rcl_publisher_bindings.cpp +141 -0
  48. package/src/rcl_publisher_bindings.h +26 -0
  49. package/src/rcl_service_bindings.cpp +185 -0
  50. package/src/rcl_service_bindings.h +26 -0
  51. package/src/rcl_subscription_bindings.cpp +335 -0
  52. package/src/rcl_subscription_bindings.h +26 -0
  53. package/src/rcl_time_point_bindings.cpp +194 -0
  54. package/src/rcl_time_point_bindings.h +26 -0
  55. package/src/rcl_timer_bindings.cpp +237 -0
  56. package/src/rcl_timer_bindings.h +26 -0
  57. package/src/rcl_utilities.cpp +166 -1
  58. package/src/{rcl_utilities.hpp → rcl_utilities.h} +21 -3
  59. package/src/shadow_node.cpp +56 -75
  60. package/src/{shadow_node.hpp → shadow_node.h} +18 -17
  61. package/types/context.d.ts +6 -0
  62. package/types/interfaces.d.ts +4377 -0
  63. package/types/node.d.ts +53 -0
  64. package/types/publisher.d.ts +6 -0
  65. package/types/service.d.ts +6 -0
  66. package/types/subscription.d.ts +6 -0
  67. package/types/timer.d.ts +18 -0
  68. package/src/rcl_action_bindings.cpp +0 -826
@@ -0,0 +1,280 @@
1
+ // Copyright (c) 2025, The Robot Web Tools Contributors
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ #include "rcl_graph_bindings.h"
16
+
17
+ #include <rcl/error_handling.h>
18
+ #include <rcl/graph.h>
19
+ #include <rcl/rcl.h>
20
+
21
+ #include <rcpputils/scope_exit.hpp>
22
+ // NOLINTNEXTLINE
23
+ #include <string>
24
+
25
+ #include "macros.h"
26
+ #include "rcl_handle.h"
27
+ #include "rcl_utilities.h"
28
+
29
+ namespace rclnodejs {
30
+
31
+ typedef rcl_ret_t (*rcl_get_info_by_topic_func_t)(
32
+ const rcl_node_t* node, rcutils_allocator_t* allocator,
33
+ const char* topic_name, bool no_mangle,
34
+ rcl_topic_endpoint_info_array_t* info_array);
35
+
36
+ Napi::Value GetPublisherNamesAndTypesByNode(const Napi::CallbackInfo& info) {
37
+ Napi::Env env = info.Env();
38
+
39
+ RclHandle* node_handle = RclHandle::Unwrap(info[0].As<Napi::Object>());
40
+ rcl_node_t* node = reinterpret_cast<rcl_node_t*>(node_handle->ptr());
41
+ std::string node_name = info[1].As<Napi::String>().Utf8Value();
42
+ std::string node_namespace = info[2].As<Napi::String>().Utf8Value();
43
+ bool no_demangle = info[3].As<Napi::Boolean>();
44
+
45
+ rcl_names_and_types_t topic_names_and_types =
46
+ rcl_get_zero_initialized_names_and_types();
47
+ rcl_allocator_t allocator = rcl_get_default_allocator();
48
+ THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK,
49
+ rcl_get_publisher_names_and_types_by_node(
50
+ node, &allocator, no_demangle, node_name.c_str(),
51
+ node_namespace.c_str(), &topic_names_and_types),
52
+ "Failed to get_publisher_names_and_types.");
53
+
54
+ Napi::Array result_list =
55
+ Napi::Array::New(env, topic_names_and_types.names.size);
56
+ ExtractNamesAndTypes(topic_names_and_types, &result_list);
57
+
58
+ THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK,
59
+ rcl_names_and_types_fini(&topic_names_and_types),
60
+ "Failed to destroy topic_names_and_types");
61
+
62
+ return result_list;
63
+ }
64
+
65
+ Napi::Value GetSubscriptionNamesAndTypesByNode(const Napi::CallbackInfo& info) {
66
+ Napi::Env env = info.Env();
67
+
68
+ RclHandle* node_handle = RclHandle::Unwrap(info[0].As<Napi::Object>());
69
+ rcl_node_t* node = reinterpret_cast<rcl_node_t*>(node_handle->ptr());
70
+ std::string node_name = info[1].As<Napi::String>().Utf8Value();
71
+ std::string node_namespace = info[2].As<Napi::String>().Utf8Value();
72
+ bool no_demangle = info[3].As<Napi::Boolean>();
73
+
74
+ rcl_names_and_types_t topic_names_and_types =
75
+ rcl_get_zero_initialized_names_and_types();
76
+ rcl_allocator_t allocator = rcl_get_default_allocator();
77
+ THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK,
78
+ rcl_get_subscriber_names_and_types_by_node(
79
+ node, &allocator, no_demangle, node_name.c_str(),
80
+ node_namespace.c_str(), &topic_names_and_types),
81
+ "Failed to get_publisher_names_and_types.");
82
+
83
+ Napi::Array result_list =
84
+ Napi::Array::New(env, topic_names_and_types.names.size);
85
+ ExtractNamesAndTypes(topic_names_and_types, &result_list);
86
+
87
+ THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK,
88
+ rcl_names_and_types_fini(&topic_names_and_types),
89
+ "Failed to destroy topic_names_and_types");
90
+
91
+ return result_list;
92
+ }
93
+
94
+ Napi::Value GetServiceNamesAndTypesByNode(const Napi::CallbackInfo& info) {
95
+ Napi::Env env = info.Env();
96
+
97
+ RclHandle* node_handle = RclHandle::Unwrap(info[0].As<Napi::Object>());
98
+ rcl_node_t* node = reinterpret_cast<rcl_node_t*>(node_handle->ptr());
99
+ std::string node_name = info[1].As<Napi::String>().Utf8Value();
100
+ std::string node_namespace = info[2].As<Napi::String>().Utf8Value();
101
+
102
+ rcl_names_and_types_t service_names_and_types =
103
+ rcl_get_zero_initialized_names_and_types();
104
+ rcl_allocator_t allocator = rcl_get_default_allocator();
105
+ THROW_ERROR_IF_NOT_EQUAL(
106
+ RCL_RET_OK,
107
+ rcl_get_service_names_and_types_by_node(
108
+ node, &allocator, node_name.c_str(), node_namespace.c_str(),
109
+ &service_names_and_types),
110
+ "Failed to get_service_names_and_types.");
111
+
112
+ Napi::Array result_list =
113
+ Napi::Array::New(env, service_names_and_types.names.size);
114
+ ExtractNamesAndTypes(service_names_and_types, &result_list);
115
+
116
+ THROW_ERROR_IF_NOT_EQUAL(
117
+ RCL_RET_OK, rcl_names_and_types_fini(&service_names_and_types),
118
+ "Failed to destroy rcl_get_zero_initialized_names_and_types");
119
+
120
+ return result_list;
121
+ }
122
+
123
+ Napi::Value GetClientNamesAndTypesByNode(const Napi::CallbackInfo& info) {
124
+ Napi::Env env = info.Env();
125
+
126
+ RclHandle* node_handle = RclHandle::Unwrap(info[0].As<Napi::Object>());
127
+ rcl_node_t* node = reinterpret_cast<rcl_node_t*>(node_handle->ptr());
128
+ std::string node_name = info[1].As<Napi::String>().Utf8Value();
129
+ std::string node_namespace = info[2].As<Napi::String>().Utf8Value();
130
+
131
+ rcl_names_and_types_t client_names_and_types =
132
+ rcl_get_zero_initialized_names_and_types();
133
+ rcl_allocator_t allocator = rcl_get_default_allocator();
134
+ THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK,
135
+ rcl_get_client_names_and_types_by_node(
136
+ node, &allocator, node_name.c_str(),
137
+ node_namespace.c_str(), &client_names_and_types),
138
+ "Failed to get_client_names_and_types.");
139
+
140
+ Napi::Array result_list =
141
+ Napi::Array::New(env, client_names_and_types.names.size);
142
+ ExtractNamesAndTypes(client_names_and_types, &result_list);
143
+
144
+ THROW_ERROR_IF_NOT_EQUAL(
145
+ RCL_RET_OK, rcl_names_and_types_fini(&client_names_and_types),
146
+ "Failed to destroy rcl_get_zero_initialized_names_and_types");
147
+
148
+ return result_list;
149
+ }
150
+
151
+ Napi::Value GetTopicNamesAndTypes(const Napi::CallbackInfo& info) {
152
+ Napi::Env env = info.Env();
153
+
154
+ RclHandle* node_handle = RclHandle::Unwrap(info[0].As<Napi::Object>());
155
+ rcl_node_t* node = reinterpret_cast<rcl_node_t*>(node_handle->ptr());
156
+ bool no_demangle = info[1].As<Napi::Boolean>();
157
+ rcl_names_and_types_t topic_names_and_types =
158
+ rcl_get_zero_initialized_names_and_types();
159
+ rcl_allocator_t allocator = rcl_get_default_allocator();
160
+
161
+ THROW_ERROR_IF_NOT_EQUAL(
162
+ RCL_RET_OK,
163
+ rcl_get_topic_names_and_types(node, &allocator, no_demangle,
164
+ &topic_names_and_types),
165
+ "Failed to get_publisher_names_and_types.");
166
+
167
+ Napi::Array result_list =
168
+ Napi::Array::New(env, topic_names_and_types.names.size);
169
+ ExtractNamesAndTypes(topic_names_and_types, &result_list);
170
+
171
+ THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK,
172
+ rcl_names_and_types_fini(&topic_names_and_types),
173
+ "Failed to destroy topic_names_and_types");
174
+
175
+ return result_list;
176
+ }
177
+
178
+ Napi::Value GetServiceNamesAndTypes(const Napi::CallbackInfo& info) {
179
+ Napi::Env env = info.Env();
180
+
181
+ RclHandle* node_handle = RclHandle::Unwrap(info[0].As<Napi::Object>());
182
+ rcl_node_t* node = reinterpret_cast<rcl_node_t*>(node_handle->ptr());
183
+ rcl_names_and_types_t service_names_and_types =
184
+ rcl_get_zero_initialized_names_and_types();
185
+ rcl_allocator_t allocator = rcl_get_default_allocator();
186
+
187
+ THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK,
188
+ rcl_get_service_names_and_types(
189
+ node, &allocator, &service_names_and_types),
190
+ "Failed to get_publisher_names_and_types.");
191
+
192
+ Napi::Array result_list =
193
+ Napi::Array::New(env, service_names_and_types.names.size);
194
+ ExtractNamesAndTypes(service_names_and_types, &result_list);
195
+
196
+ THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK,
197
+ rcl_names_and_types_fini(&service_names_and_types),
198
+ "Failed to destroy topic_names_and_types");
199
+
200
+ return result_list;
201
+ }
202
+
203
+ Napi::Value GetInfoByTopic(Napi::Env env, rcl_node_t* node,
204
+ const char* topic_name, bool no_mangle,
205
+ const char* type,
206
+ rcl_get_info_by_topic_func_t rcl_get_info_by_topic) {
207
+ rcutils_allocator_t allocator = rcutils_get_default_allocator();
208
+ rcl_topic_endpoint_info_array_t info_array =
209
+ rcl_get_zero_initialized_topic_endpoint_info_array();
210
+
211
+ RCPPUTILS_SCOPE_EXIT({
212
+ rcl_ret_t fini_ret =
213
+ rcl_topic_endpoint_info_array_fini(&info_array, &allocator);
214
+ if (RCL_RET_OK != fini_ret) {
215
+ Napi::Error::New(env, rcl_get_error_string().str)
216
+ .ThrowAsJavaScriptException();
217
+ rcl_reset_error();
218
+ }
219
+ });
220
+
221
+ rcl_ret_t ret = rcl_get_info_by_topic(node, &allocator, topic_name, no_mangle,
222
+ &info_array);
223
+ if (RCL_RET_OK != ret) {
224
+ if (RCL_RET_UNSUPPORTED == ret) {
225
+ Napi::Error::New(
226
+ env, std::string("Failed to get information by topic for ") + type +
227
+ ": function not supported by RMW_IMPLEMENTATION")
228
+ .ThrowAsJavaScriptException();
229
+ return env.Undefined();
230
+ }
231
+ Napi::Error::New(
232
+ env, std::string("Failed to get information by topic for ") + type)
233
+ .ThrowAsJavaScriptException();
234
+ return env.Undefined();
235
+ }
236
+
237
+ return ConvertToJSTopicEndpointInfoList(env, &info_array);
238
+ }
239
+
240
+ Napi::Value GetPublishersInfoByTopic(const Napi::CallbackInfo& info) {
241
+ RclHandle* node_handle = RclHandle::Unwrap(info[0].As<Napi::Object>());
242
+ rcl_node_t* node = reinterpret_cast<rcl_node_t*>(node_handle->ptr());
243
+ std::string topic_name = info[1].As<Napi::String>().Utf8Value();
244
+ bool no_mangle = info[2].As<Napi::Boolean>();
245
+
246
+ return GetInfoByTopic(info.Env(), node, topic_name.c_str(), no_mangle,
247
+ "publishers", rcl_get_publishers_info_by_topic);
248
+ }
249
+
250
+ Napi::Value GetSubscriptionsInfoByTopic(const Napi::CallbackInfo& info) {
251
+ RclHandle* node_handle = RclHandle::Unwrap(info[0].As<Napi::Object>());
252
+ rcl_node_t* node = reinterpret_cast<rcl_node_t*>(node_handle->ptr());
253
+ std::string topic_name = info[1].As<Napi::String>().Utf8Value();
254
+ bool no_mangle = info[2].As<Napi::Boolean>();
255
+
256
+ return GetInfoByTopic(info.Env(), node, topic_name.c_str(), no_mangle,
257
+ "subscriptions", rcl_get_subscriptions_info_by_topic);
258
+ }
259
+
260
+ Napi::Object InitGraphBindings(Napi::Env env, Napi::Object exports) {
261
+ exports.Set("getPublisherNamesAndTypesByNode",
262
+ Napi::Function::New(env, GetPublisherNamesAndTypesByNode));
263
+ exports.Set("getSubscriptionNamesAndTypesByNode",
264
+ Napi::Function::New(env, GetSubscriptionNamesAndTypesByNode));
265
+ exports.Set("getServiceNamesAndTypesByNode",
266
+ Napi::Function::New(env, GetServiceNamesAndTypesByNode));
267
+ exports.Set("getClientNamesAndTypesByNode",
268
+ Napi::Function::New(env, GetClientNamesAndTypesByNode));
269
+ exports.Set("getTopicNamesAndTypes",
270
+ Napi::Function::New(env, GetTopicNamesAndTypes));
271
+ exports.Set("getServiceNamesAndTypes",
272
+ Napi::Function::New(env, GetServiceNamesAndTypes));
273
+ exports.Set("getPublishersInfoByTopic",
274
+ Napi::Function::New(env, GetPublishersInfoByTopic));
275
+ exports.Set("getSubscriptionsInfoByTopic",
276
+ Napi::Function::New(env, GetSubscriptionsInfoByTopic));
277
+ return exports;
278
+ }
279
+
280
+ } // namespace rclnodejs
@@ -0,0 +1,26 @@
1
+ // Copyright (c) 2025, The Robot Web Tools Contributors
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ #ifndef SRC_RCL_GRAPH_BINDINGS_H_
16
+ #define SRC_RCL_GRAPH_BINDINGS_H_
17
+
18
+ #include <napi.h>
19
+
20
+ namespace rclnodejs {
21
+
22
+ Napi::Object InitGraphBindings(Napi::Env env, Napi::Object exports);
23
+
24
+ }
25
+
26
+ #endif // SRC_RCL_GRAPH_BINDINGS_H_
@@ -0,0 +1,75 @@
1
+ // Copyright (c) 2025, The Robot Web Tools Contributors
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ #include "rcl_guard_condition_bindings.h"
16
+
17
+ #include <rcl/error_handling.h>
18
+ #include <rcl/rcl.h>
19
+
20
+ #include "macros.h"
21
+ #include "rcl_handle.h"
22
+ #include "rcl_utilities.h"
23
+
24
+ namespace rclnodejs {
25
+
26
+ Napi::Value CreateGuardCondition(const Napi::CallbackInfo& info) {
27
+ Napi::Env env = info.Env();
28
+
29
+ RclHandle* context_handle = RclHandle::Unwrap(info[0].As<Napi::Object>());
30
+ rcl_context_t* context =
31
+ reinterpret_cast<rcl_context_t*>(context_handle->ptr());
32
+
33
+ rcl_guard_condition_t* gc = reinterpret_cast<rcl_guard_condition_t*>(
34
+ malloc(sizeof(rcl_guard_condition_t)));
35
+
36
+ *gc = rcl_get_zero_initialized_guard_condition();
37
+ rcl_guard_condition_options_t gc_options =
38
+ rcl_guard_condition_get_default_options();
39
+
40
+ THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK,
41
+ rcl_guard_condition_init(gc, context, gc_options),
42
+ rcl_get_error_string().str);
43
+
44
+ auto handle = RclHandle::NewInstance(env, gc, nullptr, [](void* ptr) {
45
+ rcl_guard_condition_t* gc = reinterpret_cast<rcl_guard_condition_t*>(ptr);
46
+ rcl_ret_t ret = rcl_guard_condition_fini(gc);
47
+ free(ptr);
48
+ THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK, ret, rcl_get_error_string().str);
49
+ });
50
+
51
+ return handle;
52
+ }
53
+
54
+ Napi::Value TriggerGuardCondition(const Napi::CallbackInfo& info) {
55
+ Napi::Env env = info.Env();
56
+
57
+ RclHandle* gc_handle = RclHandle::Unwrap(info[0].As<Napi::Object>());
58
+ rcl_guard_condition_t* gc =
59
+ reinterpret_cast<rcl_guard_condition_t*>(gc_handle->ptr());
60
+
61
+ THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK, rcl_trigger_guard_condition(gc),
62
+ rcl_get_error_string().str);
63
+
64
+ return env.Undefined();
65
+ }
66
+
67
+ Napi::Object InitGuardConditionBindings(Napi::Env env, Napi::Object exports) {
68
+ exports.Set("createGuardCondition",
69
+ Napi::Function::New(env, CreateGuardCondition));
70
+ exports.Set("triggerGuardCondition",
71
+ Napi::Function::New(env, TriggerGuardCondition));
72
+ return exports;
73
+ }
74
+
75
+ } // namespace rclnodejs
@@ -0,0 +1,26 @@
1
+ // Copyright (c) 2025, The Robot Web Tools Contributors
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ #ifndef SRC_RCL_GUARD_CONDITION_BINDINGS_H_
16
+ #define SRC_RCL_GUARD_CONDITION_BINDINGS_H_
17
+
18
+ #include <napi.h>
19
+
20
+ namespace rclnodejs {
21
+
22
+ Napi::Object InitGuardConditionBindings(Napi::Env env, Napi::Object exports);
23
+
24
+ }
25
+
26
+ #endif // SRC_RCL_GUARD_CONDITION_BINDINGS_H_
@@ -12,91 +12,75 @@
12
12
  // See the License for the specific language governing permissions and
13
13
  // limitations under the License.
14
14
 
15
- #include "rcl_handle.hpp"
15
+ #include "rcl_handle.h"
16
16
 
17
17
  #include <rcl/error_handling.h>
18
18
  #include <rcl/rcl.h>
19
19
 
20
+ #include "rcl_utilities.h"
21
+
20
22
  namespace rclnodejs {
21
23
 
22
- Nan::Persistent<v8::Function> RclHandle::constructor;
24
+ Napi::FunctionReference RclHandle::constructor;
23
25
 
24
- RclHandle::RclHandle() : pointer_(nullptr), parent_(nullptr) {}
26
+ RclHandle::RclHandle(const Napi::CallbackInfo& info)
27
+ : Napi::ObjectWrap<RclHandle>(info), pointer_(nullptr), parent_(nullptr) {}
25
28
 
26
29
  RclHandle::~RclHandle() {
27
30
  if (pointer_) Reset();
28
31
  }
29
32
 
30
- void RclHandle::Init(v8::Local<v8::Object> exports) {
31
- v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);
32
- tpl->SetClassName(Nan::New("RclHandle").ToLocalChecked());
33
- tpl->InstanceTemplate()->SetInternalFieldCount(1);
33
+ Napi::Object RclHandle::Init(Napi::Env env, Napi::Object exports) {
34
+ Napi::HandleScope scope(env);
34
35
 
35
- Nan::SetAccessor(tpl->InstanceTemplate(),
36
- Nan::New("properties").ToLocalChecked(), PropertiesGetter);
37
- Nan::SetPrototypeMethod(tpl, "release", Release);
38
- Nan::SetPrototypeMethod(tpl, "dismiss", Dismiss);
36
+ Napi::Function func = DefineClass(
37
+ env, "RclHandle",
38
+ {InstanceMethod("release", &RclHandle::Release),
39
+ InstanceMethod("dismiss", &RclHandle::Dismiss),
40
+ InstanceAccessor("properties", &RclHandle::PropertiesGetter, nullptr)});
39
41
 
40
- v8::Local<v8::Context> context = exports->GetIsolate()->GetCurrentContext();
42
+ constructor = Napi::Persistent(func);
43
+ constructor.SuppressDestruct();
44
+ exports.Set("RclHandle", func);
41
45
 
42
- constructor.Reset(tpl->GetFunction(context).ToLocalChecked());
43
- Nan::Set(exports, Nan::New("RclHandle").ToLocalChecked(),
44
- tpl->GetFunction(context).ToLocalChecked());
45
- }
46
-
47
- void RclHandle::New(const Nan::FunctionCallbackInfo<v8::Value>& info) {
48
- if (info.IsConstructCall()) {
49
- RclHandle* obj = new RclHandle();
50
- obj->Wrap(info.This());
51
- info.GetReturnValue().Set(info.This());
52
- }
46
+ return exports;
53
47
  }
54
48
 
55
49
  void RclHandle::SyncProperties() {
56
- auto obj = v8::Object::New(v8::Isolate::GetCurrent());
57
-
58
- for (auto it = properties_.begin(); it != properties_.end(); it++) {
59
- Nan::Set(obj, Nan::New(it->first).ToLocalChecked(), Nan::New(it->second));
50
+ Napi::Env env = rclnodejs::GetEnv();
51
+ Napi::HandleScope scope(env);
52
+ Napi::Object obj = Napi::Object::New(env);
53
+ for (auto& pair : properties_) {
54
+ obj.Set(pair.first, Napi::Boolean::New(env, pair.second));
60
55
  }
61
-
62
- properties_obj_ = obj;
56
+ properties_obj_ = Napi::Persistent(obj);
63
57
  }
64
58
 
65
- NAN_GETTER(RclHandle::PropertiesGetter) {
66
- auto* me = RclHandle::Unwrap<RclHandle>(info.Holder());
67
-
68
- if (!me->properties_obj_.IsEmpty())
69
- info.GetReturnValue().Set(me->properties_obj_);
70
- else
71
- info.GetReturnValue().Set(Nan::Undefined());
59
+ Napi::Value RclHandle::PropertiesGetter(const Napi::CallbackInfo& info) {
60
+ return !properties_obj_.IsEmpty() ? properties_obj_.Value()
61
+ : info.Env().Undefined();
72
62
  }
73
63
 
74
- NAN_METHOD(RclHandle::Release) {
75
- auto* me = Nan::ObjectWrap::Unwrap<RclHandle>(info.Holder());
76
- if (me->ptr()) me->Reset();
77
-
78
- info.GetReturnValue().Set(Nan::Undefined());
64
+ Napi::Value RclHandle::Release(const Napi::CallbackInfo& info) {
65
+ Napi::Env env = info.Env();
66
+ if (ptr()) Reset();
67
+ return env.Undefined();
79
68
  }
80
69
 
81
- NAN_METHOD(RclHandle::Dismiss) {
82
- auto* me = Nan::ObjectWrap::Unwrap<RclHandle>(info.Holder());
83
- if (me) me->set_ptr(nullptr);
84
-
85
- info.GetReturnValue().Set(Nan::Undefined());
70
+ Napi::Value RclHandle::Dismiss(const Napi::CallbackInfo& info) {
71
+ Napi::Env env = info.Env();
72
+ set_ptr(nullptr);
73
+ return env.Undefined();
86
74
  }
87
75
 
88
- v8::Local<v8::Object> RclHandle::NewInstance(
89
- void* handle, RclHandle* parent, std::function<void(void*)> deleter) {
90
- Nan::EscapableHandleScope scope;
91
-
92
- v8::Local<v8::Function> cons = Nan::New<v8::Function>(constructor);
93
- v8::Local<v8::Context> context =
94
- v8::Isolate::GetCurrent()->GetCurrentContext();
76
+ Napi::Object RclHandle::NewInstance(Napi::Env env, void* handle,
77
+ RclHandle* parent,
78
+ std::function<void(void*)> deleter) {
79
+ Napi::EscapableHandleScope scope(env);
95
80
 
96
- v8::Local<v8::Object> instance =
97
- cons->NewInstance(context, 0, nullptr).ToLocalChecked();
81
+ Napi::Object instance = constructor.New({});
98
82
 
99
- auto* rcl_handle = Nan::ObjectWrap::Unwrap<RclHandle>(instance);
83
+ RclHandle* rcl_handle = Napi::ObjectWrap<RclHandle>::Unwrap(instance);
100
84
  rcl_handle->set_ptr(handle);
101
85
  rcl_handle->set_deleter(deleter);
102
86
  if (parent) {
@@ -104,7 +88,7 @@ v8::Local<v8::Object> RclHandle::NewInstance(
104
88
  parent->AddChild(rcl_handle);
105
89
  }
106
90
 
107
- return scope.Escape(instance);
91
+ return scope.Escape(instance).As<Napi::Object>();
108
92
  }
109
93
 
110
94
  void RclHandle::Reset() {
@@ -12,10 +12,10 @@
12
12
  // See the License for the specific language governing permissions and
13
13
  // limitations under the License.
14
14
 
15
- #ifndef SRC_RCL_HANDLE_HPP_
16
- #define SRC_RCL_HANDLE_HPP_
15
+ #ifndef SRC_RCL_HANDLE_H_
16
+ #define SRC_RCL_HANDLE_H_
17
17
 
18
- #include <nan.h>
18
+ #include <napi.h>
19
19
 
20
20
  #include <functional>
21
21
  #include <map>
@@ -24,11 +24,15 @@
24
24
 
25
25
  namespace rclnodejs {
26
26
 
27
- class RclHandle : public Nan::ObjectWrap {
27
+ class RclHandle : public Napi::ObjectWrap<RclHandle> {
28
28
  public:
29
- static void Init(v8::Local<v8::Object> exports);
30
- static v8::Local<v8::Object> NewInstance(void* handle, RclHandle* parent,
31
- std::function<void(void*)> deleter);
29
+ static Napi::Object Init(Napi::Env env, Napi::Object exports);
30
+ static Napi::Object NewInstance(Napi::Env env, void* handle,
31
+ RclHandle* parent,
32
+ std::function<void(void*)> deleter);
33
+
34
+ explicit RclHandle(const Napi::CallbackInfo& info);
35
+ ~RclHandle();
32
36
 
33
37
  void set_deleter(std::function<void(void*)> deleter) { deleter_ = deleter; }
34
38
 
@@ -47,25 +51,22 @@ class RclHandle : public Nan::ObjectWrap {
47
51
  void SyncProperties();
48
52
 
49
53
  private:
50
- RclHandle();
51
- ~RclHandle();
52
-
53
- static Nan::Persistent<v8::Function> constructor;
54
- static void New(const Nan::FunctionCallbackInfo<v8::Value>& info);
55
- static NAN_METHOD(Release);
56
- static NAN_METHOD(Dismiss);
57
- static NAN_GETTER(PropertiesGetter);
54
+ Napi::Value Release(const Napi::CallbackInfo& info);
55
+ Napi::Value Dismiss(const Napi::CallbackInfo& info);
56
+ Napi::Value PropertiesGetter(const Napi::CallbackInfo& info);
58
57
 
59
58
  private:
60
59
  void* pointer_;
61
60
  RclHandle* parent_;
62
61
  std::map<std::string, bool> properties_;
63
- v8::Local<v8::Object> properties_obj_;
62
+ Napi::ObjectReference properties_obj_;
64
63
 
65
64
  std::function<void(void*)> deleter_;
66
65
  std::set<RclHandle*> children_;
66
+
67
+ static Napi::FunctionReference constructor;
67
68
  };
68
69
 
69
70
  } // namespace rclnodejs
70
71
 
71
- #endif // SRC_RCL_HANDLE_HPP_
72
+ #endif // SRC_RCL_HANDLE_H_