Skip to content

Commit 3083b17

Browse files
committed
Refactor: Migrate NAN methods to Node-API in rcl_action_bindings.cpp and fix binding.gyp syntax error
1 parent ee4bdad commit 3083b17

File tree

4 files changed

+31
-40
lines changed

4 files changed

+31
-40
lines changed

binding.gyp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828
'./src/shadow_node.cpp',
2929
],
3030
'include_dirs': [
31-
'.',
32-
"<!(node -e \"require('nan')\")",
31+
'<!@(node -p "require(\"node-addon-api\").include")',
3332
'<(ros_include_root)',
3433
],
3534
'cflags!': [
@@ -129,7 +128,11 @@
129128
"defines": ["NODE_RUNTIME_ELECTRON=1"]
130129
}
131130
],
132-
]
131+
],
132+
'dependencies': [
133+
'scripts/config.js',
134+
'<!(node -p "require(\"node-addon-api\").gyp")',
135+
],
133136
}
134137
]
135138
}

src/rcl_action_bindings.cpp

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -162,23 +162,17 @@ Napi::Value ActionCreateServer(const Napi::CallbackInfo& info) {
162162
}
163163
}
164164

165-
NAN_METHOD(ActionServerIsAvailable) {
166-
RclHandle* node_handle = RclHandle::Unwrap<RclHandle>(
167-
Nan::To<v8::Object>(info[0]).ToLocalChecked());
168-
rcl_node_t* node = reinterpret_cast<rcl_node_t*>(node_handle->ptr());
169-
RclHandle* action_client_handle = RclHandle::Unwrap<RclHandle>(
170-
Nan::To<v8::Object>(info[1]).ToLocalChecked());
171-
rcl_action_client_t* action_client =
172-
reinterpret_cast<rcl_action_client_t*>(action_client_handle->ptr());
165+
Napi::Value ActionServerIsAvailable(const Napi::CallbackInfo& info) {
166+
Napi::Env env = info.Env();
167+
RclHandle* action_server_handle = RclHandle::Unwrap<RclHandle>(info[0].As<Napi::Object>());
168+
rcl_action_server_t* action_server =
169+
reinterpret_cast<rcl_action_server_t*>(action_server_handle->ptr());
170+
rcl_action_goal_info_t* buffer = reinterpret_cast<rcl_action_goal_info_t*>(
171+
node::Buffer::Data(info[1].As<Napi::Object>()));
173172

174-
bool is_available;
175-
THROW_ERROR_IF_NOT_EQUAL(
176-
RCL_RET_OK,
177-
rcl_action_server_is_available(node, action_client, &is_available),
178-
rcl_get_error_string().str);
173+
bool exists = rcl_action_server_goal_exists(action_server, buffer);
179174

180-
v8::Local<v8::Boolean> result = Nan::New<v8::Boolean>(is_available);
181-
info.GetReturnValue().Set(result);
175+
return Napi::Boolean::New(env, exists);
182176
}
183177

184178
NAN_METHOD(ActionSendGoalRequest) {
@@ -683,35 +677,31 @@ NAN_METHOD(ActionServerGoalExists) {
683677
info.GetReturnValue().Set(result);
684678
}
685679

686-
NAN_METHOD(ActionExpireGoals) {
687-
v8::Local<v8::Context> currentContent = Nan::GetCurrentContext();
688-
RclHandle* action_server_handle = RclHandle::Unwrap<RclHandle>(
689-
Nan::To<v8::Object>(info[0]).ToLocalChecked());
680+
Napi::Value ActionExpireGoals(const Napi::CallbackInfo& info) {
681+
Napi::Env env = info.Env();
682+
RclHandle* action_server_handle = RclHandle::Unwrap<RclHandle>(info[0].As<Napi::Object>());
690683
rcl_action_server_t* action_server =
691684
reinterpret_cast<rcl_action_server_t*>(action_server_handle->ptr());
692-
int64_t max_num_goals = info[1]->IntegerValue(currentContent).FromJust();
685+
int64_t max_num_goals = info[1].As<Napi::Number>().Int64Value();
693686
rcl_action_goal_info_t* buffer = reinterpret_cast<rcl_action_goal_info_t*>(
694-
node::Buffer::Data(Nan::To<v8::Object>(info[2]).ToLocalChecked()));
687+
node::Buffer::Data(info[2].As<Napi::Object>()));
695688

696689
size_t num_expired;
697690
THROW_ERROR_IF_NOT_EQUAL(rcl_action_expire_goals(action_server, buffer,
698691
max_num_goals, &num_expired),
699692
RCL_RET_OK, rcl_get_error_string().str);
700693

701-
v8::Local<v8::Integer> result =
702-
Nan::New<v8::Integer>(static_cast<int32_t>(num_expired));
703-
info.GetReturnValue().Set(result);
694+
return Napi::Number::New(env, static_cast<int32_t>(num_expired));
704695
}
705696

706-
NAN_METHOD(ActionGetClientNamesAndTypesByNode) {
707-
v8::Local<v8::Context> currentContent = Nan::GetCurrentContext();
708-
RclHandle* node_handle = RclHandle::Unwrap<RclHandle>(
709-
Nan::To<v8::Object>(info[0]).ToLocalChecked());
697+
Napi::Value ActionGetClientNamesAndTypesByNode(const Napi::CallbackInfo& info) {
698+
Napi::Env env = info.Env();
699+
RclHandle* node_handle = RclHandle::Unwrap<RclHandle>(info[0].As<Napi::Object>());
710700
rcl_node_t* node = reinterpret_cast<rcl_node_t*>(node_handle->ptr());
711701
std::string node_name =
712-
*Nan::Utf8String(info[1]->ToString(currentContent).ToLocalChecked());
702+
info[1].As<Napi::String>().Utf8Value();
713703
std::string node_namespace =
714-
*Nan::Utf8String(info[2]->ToString(currentContent).ToLocalChecked());
704+
info[2].As<Napi::String>().Utf8Value();
715705

716706
rcl_names_and_types_t names_and_types =
717707
rcl_get_zero_initialized_names_and_types();
@@ -722,15 +712,14 @@ NAN_METHOD(ActionGetClientNamesAndTypesByNode) {
722712
node_namespace.c_str(), &names_and_types),
723713
"Failed to action client names and types.");
724714

725-
v8::Local<v8::Array> result_list =
726-
Nan::New<v8::Array>(names_and_types.names.size);
727-
ExtractNamesAndTypes(names_and_types, &result_list);
715+
Napi::Array result_list = Napi::Array::New(env, names_and_types.names.size);
716+
ExtractNamesAndTypes(env, names_and_types, result_list);
728717

729718
THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK,
730719
rcl_names_and_types_fini(&names_and_types),
731720
"Failed to destroy names_and_types");
732721

733-
info.GetReturnValue().Set(result_list);
722+
return result_list;
734723
}
735724

736725
NAN_METHOD(ActionGetServerNamesAndTypesByNode) {

src/rcl_bindings.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,6 @@ Napi::Value ClearContentFilter(const Napi::CallbackInfo& info) {
813813
rcl_get_error_string().str);
814814

815815
return Napi::Boolean::New(env, true);
816-
#endif
817816
}
818817

819818
Napi::Value CreatePublisher(const Napi::CallbackInfo& info) {

src/rcl_bindings.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#ifndef SRC_RCL_BINDINGS_HPP_
1616
#define SRC_RCL_BINDINGS_HPP_
1717

18-
#include <nan.h>
18+
#include <node_api.h>
1919
#include <rcl/graph.h>
2020
#include <rcl/rcl.h>
2121

@@ -25,7 +25,7 @@
2525

2626
namespace rclnodejs {
2727

28-
typedef void (*JsCFuntcion)(const Nan::FunctionCallbackInfo<v8::Value>&);
28+
typedef void (*JsCFuntcion)(const Napi::CallbackInfo& info);
2929

3030
typedef struct {
3131
const char* name;

0 commit comments

Comments
 (0)