Skip to content

Commit ee4bdad

Browse files
committed
Migrate project from NAN to Node-API for improved compatibility
1 parent ff07c86 commit ee4bdad

File tree

5 files changed

+469
-515
lines changed

5 files changed

+469
-515
lines changed

src/addon.cpp

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include <nan.h>
15+
#include <node_api.h>
1616

1717
#include "macros.hpp"
1818
#include "rcl_action_bindings.hpp"
@@ -23,18 +23,20 @@
2323
#include "rcutils/macros.h"
2424
#include "shadow_node.hpp"
2525

26-
bool IsRunningInElectronRenderer() {
27-
auto global = Nan::GetCurrentContext()->Global();
28-
auto process =
29-
Nan::To<v8::Object>(Nan::Get(global, Nan::New("process").ToLocalChecked())
30-
.ToLocalChecked())
31-
.ToLocalChecked();
32-
auto process_type =
33-
Nan::Get(process, Nan::New("type").ToLocalChecked()).ToLocalChecked();
34-
return process_type->StrictEquals(Nan::New("renderer").ToLocalChecked());
26+
bool IsRunningInElectronRenderer(napi_env env) {
27+
napi_value global, process, process_type;
28+
napi_get_global(env, &global);
29+
napi_get_named_property(env, global, "process", &process);
30+
napi_get_named_property(env, process, "type", &process_type);
31+
32+
bool is_renderer;
33+
napi_value renderer_str;
34+
napi_create_string_utf8(env, "renderer", NAPI_AUTO_LENGTH, &renderer_str);
35+
napi_strict_equals(env, process_type, renderer_str, &is_renderer);
36+
return is_renderer;
3537
}
3638

37-
void InitModule(v8::Local<v8::Object> exports) {
39+
void InitModule(napi_env env, napi_value exports) {
3840
// workaround process name mangling by chromium
3941
//
4042
// rcl logging uses `program_invocation_name` to determine the log file,
@@ -43,42 +45,33 @@ void InitModule(v8::Local<v8::Object> exports) {
4345
// occurence of ' -' with the null terminator. see:
4446
// https://unix.stackexchange.com/questions/432419/unexpected-non-null-encoding-of-proc-pid-cmdline
4547
#if defined(__linux__) && defined(__GLIBC__)
46-
if (IsRunningInElectronRenderer()) {
48+
if (IsRunningInElectronRenderer(env)) {
4749
auto prog_name = program_invocation_name;
4850
auto end = strstr(prog_name, " -");
4951
assert(end);
5052
prog_name[end - prog_name] = 0;
5153
}
5254
#endif
5355

54-
v8::Local<v8::Context> context = exports->GetIsolate()->GetCurrentContext();
56+
napi_value context;
57+
napi_get_value_string_utf8(env, exports, "context", &context, NULL, NULL);
5558

5659
for (uint32_t i = 0; i < rclnodejs::binding_methods.size(); i++) {
57-
Nan::Set(
58-
exports, Nan::New(rclnodejs::binding_methods[i].name).ToLocalChecked(),
59-
Nan::New<v8::FunctionTemplate>(rclnodejs::binding_methods[i].function)
60-
->GetFunction(context)
61-
.ToLocalChecked());
60+
napi_value func;
61+
napi_create_function(env, NULL, 0, rclnodejs::binding_methods[i].function, NULL, &func);
62+
napi_set_named_property(env, exports, rclnodejs::binding_methods[i].name, func);
6263
}
6364

6465
for (uint32_t i = 0; i < rclnodejs::action_binding_methods.size(); i++) {
65-
Nan::Set(
66-
exports,
67-
Nan::New(rclnodejs::action_binding_methods[i].name).ToLocalChecked(),
68-
Nan::New<v8::FunctionTemplate>(
69-
rclnodejs::action_binding_methods[i].function)
70-
->GetFunction(context)
71-
.ToLocalChecked());
66+
napi_value func;
67+
napi_create_function(env, NULL, 0, rclnodejs::action_binding_methods[i].function, NULL, &func);
68+
napi_set_named_property(env, exports, rclnodejs::action_binding_methods[i].name, func);
7269
}
7370

7471
for (uint32_t i = 0; i < rclnodejs::lifecycle_binding_methods.size(); i++) {
75-
Nan::Set(
76-
exports,
77-
Nan::New(rclnodejs::lifecycle_binding_methods[i].name).ToLocalChecked(),
78-
Nan::New<v8::FunctionTemplate>(
79-
rclnodejs::lifecycle_binding_methods[i].function)
80-
->GetFunction(context)
81-
.ToLocalChecked());
72+
napi_value func;
73+
napi_create_function(env, NULL, 0, rclnodejs::lifecycle_binding_methods[i].function, NULL, &func);
74+
napi_set_named_property(env, exports, rclnodejs::lifecycle_binding_methods[i].name, func);
8275
}
8376

8477
rclnodejs::ShadowNode::Init(exports);

src/rcl_action_bindings.cpp

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -95,21 +95,16 @@ NAN_METHOD(ActionCreateClient) {
9595
}
9696
}
9797

98-
NAN_METHOD(ActionCreateServer) {
99-
v8::Local<v8::Context> currentContent = Nan::GetCurrentContext();
100-
RclHandle* node_handle = RclHandle::Unwrap<RclHandle>(
101-
Nan::To<v8::Object>(info[0]).ToLocalChecked());
98+
Napi::Value ActionCreateServer(const Napi::CallbackInfo& info) {
99+
Napi::Env env = info.Env();
100+
RclHandle* node_handle = RclHandle::Unwrap<RclHandle>(info[0].As<Napi::Object>());
102101
rcl_node_t* node = reinterpret_cast<rcl_node_t*>(node_handle->ptr());
103-
RclHandle* clock_handle = RclHandle::Unwrap<RclHandle>(
104-
Nan::To<v8::Object>(info[1]).ToLocalChecked());
102+
RclHandle* clock_handle = RclHandle::Unwrap<RclHandle>(info[1].As<Napi::Object>());
105103
rcl_clock_t* clock = reinterpret_cast<rcl_clock_t*>(clock_handle->ptr());
106-
std::string action_name(
107-
*Nan::Utf8String(info[2]->ToString(currentContent).ToLocalChecked()));
108-
std::string interface_name(
109-
*Nan::Utf8String(info[3]->ToString(currentContent).ToLocalChecked()));
110-
std::string package_name(
111-
*Nan::Utf8String(info[4]->ToString(currentContent).ToLocalChecked()));
112-
int64_t result_timeout = info[10]->IntegerValue(currentContent).FromJust();
104+
std::string action_name = info[2].As<Napi::String>().Utf8Value();
105+
std::string interface_name = info[3].As<Napi::String>().Utf8Value();
106+
std::string package_name = info[4].As<Napi::String>().Utf8Value();
107+
int64_t result_timeout = info[10].As<Napi::Number>().Int64Value();
113108

114109
const rosidl_action_type_support_t* ts =
115110
GetActionTypeSupport(package_name, interface_name);
@@ -160,9 +155,10 @@ NAN_METHOD(ActionCreateServer) {
160155
THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK, ret, rcl_get_error_string().str);
161156
});
162157

163-
info.GetReturnValue().Set(js_obj);
158+
return js_obj;
164159
} else {
165-
Nan::ThrowError(GetErrorMessageAndClear().c_str());
160+
Napi::Error::New(env, GetErrorMessageAndClear()).ThrowAsJavaScriptException();
161+
return env.Undefined();
166162
}
167163
}
168164

0 commit comments

Comments
 (0)