@@ -40,14 +40,24 @@ namespace rclnodejs {
4040std::unique_ptr<rmw_qos_profile_t > GetQoSProfile (v8::Local<v8::Value> qos);
4141
4242NAN_METHOD (Init) {
43+ rcl_allocator_t allocator = rcl_get_default_allocator ();
44+ rcl_init_options_t init_options = rcl_get_zero_initialized_init_options ();
45+ rcl_ret_t ret = rcl_init_options_init (&init_options, allocator);
46+
47+ RclHandle* context_handle = RclHandle::Unwrap<RclHandle>(info[0 ]->ToObject ());
48+ rcl_context_t * context =
49+ reinterpret_cast <rcl_context_t *>(context_handle->ptr ());
4350 THROW_ERROR_IF_NOT_EQUAL (RCL_RET_OK,
44- rcl_init (0 , nullptr , rcl_get_default_allocator () ),
51+ rcl_init (0 , nullptr , &init_options, context ),
4552 rcl_get_error_string ().str );
4653}
4754
4855NAN_METHOD (CreateNode) {
4956 std::string node_name (*v8::String::Utf8Value (info[0 ]));
5057 std::string name_space (*v8::String::Utf8Value (info[1 ]));
58+ RclHandle* context_handle = RclHandle::Unwrap<RclHandle>(info[2 ]->ToObject ());
59+ rcl_context_t * context =
60+ reinterpret_cast <rcl_context_t *>(context_handle->ptr ());
5161
5262 rcl_node_t * node = reinterpret_cast <rcl_node_t *>(malloc (sizeof (rcl_node_t )));
5363
@@ -56,7 +66,8 @@ NAN_METHOD(CreateNode) {
5666
5767 THROW_ERROR_IF_NOT_EQUAL (
5868 RCL_RET_OK,
59- rcl_node_init (node, node_name.c_str (), name_space.c_str (), &options),
69+ rcl_node_init (node, node_name.c_str (), name_space.c_str (), context,
70+ &options),
6071 rcl_get_error_string ().str );
6172
6273 auto handle = RclHandle::NewInstance (node, nullptr ,
@@ -66,6 +77,9 @@ NAN_METHOD(CreateNode) {
6677
6778NAN_METHOD (CreateTimer) {
6879 int64_t period_ms = info[0 ]->IntegerValue ();
80+ RclHandle* context_handle = RclHandle::Unwrap<RclHandle>(info[1 ]->ToObject ());
81+ rcl_context_t * context =
82+ reinterpret_cast <rcl_context_t *>(context_handle->ptr ());
6983 rcl_timer_t * timer =
7084 reinterpret_cast <rcl_timer_t *>(malloc (sizeof (rcl_timer_t )));
7185 *timer = rcl_get_zero_initialized_timer ();
@@ -78,8 +92,9 @@ NAN_METHOD(CreateTimer) {
7892 &allocator),
7993 rcl_get_error_string ().str );
8094 THROW_ERROR_IF_NOT_EQUAL (RCL_RET_OK,
81- rcl_timer_init (timer, clock, RCL_MS_TO_NS (period_ms),
82- nullptr , rcl_get_default_allocator ()),
95+ rcl_timer_init (timer, clock, context,
96+ RCL_MS_TO_NS (period_ms), nullptr ,
97+ rcl_get_default_allocator ()),
8398 rcl_get_error_string ().str );
8499
85100 auto js_obj = RclHandle::NewInstance (
@@ -893,8 +908,24 @@ std::unique_ptr<rmw_qos_profile_t> GetQoSProfile(v8::Local<v8::Value> qos) {
893908 return qos_profile;
894909}
895910
911+ int DestroyContext (rcl_context_t * context) {
912+ rcl_ret_t ret = RCL_RET_OK;
913+ if (context->impl ) {
914+ if (rcl_context_is_valid (context)) {
915+ if (RCL_RET_OK != rcl_shutdown (context)) {
916+ Nan::ThrowError (rcl_get_error_string ().str );
917+ }
918+ ret = rcl_context_fini (context);
919+ }
920+ }
921+ return ret;
922+ }
923+
896924NAN_METHOD (Shutdown) {
897- THROW_ERROR_IF_NOT_EQUAL (rcl_shutdown (), RCL_RET_OK,
925+ RclHandle* context_handle = RclHandle::Unwrap<RclHandle>(info[0 ]->ToObject ());
926+ rcl_context_t * context =
927+ reinterpret_cast <rcl_context_t *>(context_handle->ptr ());
928+ THROW_ERROR_IF_NOT_EQUAL (rcl_shutdown (context), RCL_RET_OK,
898929 rcl_get_error_string ().str );
899930 info.GetReturnValue ().Set (Nan::Undefined ());
900931}
@@ -1002,6 +1033,24 @@ NAN_METHOD(IsEnableFor) {
10021033 info.GetReturnValue ().Set (Nan::New (enabled));
10031034}
10041035
1036+ NAN_METHOD (CreateContext) {
1037+ rcl_context_t * context =
1038+ reinterpret_cast <rcl_context_t *>(malloc (sizeof (rcl_context_t )));
1039+ *context = rcl_get_zero_initialized_context ();
1040+ auto js_obj = RclHandle::NewInstance (context, nullptr ,
1041+ std::bind (DestroyContext, context));
1042+
1043+ info.GetReturnValue ().Set (js_obj);
1044+ }
1045+
1046+ NAN_METHOD (IsContextValid) {
1047+ RclHandle* context_handle = RclHandle::Unwrap<RclHandle>(info[0 ]->ToObject ());
1048+ rcl_context_t * context =
1049+ reinterpret_cast <rcl_context_t *>(context_handle->ptr ());
1050+ bool is_valid = rcl_context_is_valid (context);
1051+ info.GetReturnValue ().Set (Nan::New (is_valid));
1052+ }
1053+
10051054uint32_t GetBindingMethodsCount (BindingMethod* methods) {
10061055 uint32_t count = 0 ;
10071056 while (methods[count].function ) {
@@ -1058,6 +1107,8 @@ BindingMethod binding_methods[] = {
10581107 {" getLoggerEffectiveLevel" , GetLoggerEffectiveLevel},
10591108 {" log" , Log},
10601109 {" isEnableFor" , IsEnableFor},
1110+ {" createContext" , CreateContext},
1111+ {" ok" , IsContextValid},
10611112 {" " , nullptr }};
10621113
10631114} // namespace rclnodejs
0 commit comments