1212// See the License for the specific language governing permissions and
1313// limitations under the License.
1414
15- #include < nan.h>
15+ #include < napi.h>
16+ #include < node_api.h>
1617
1718#include " macros.hpp"
1819#include " rcl_action_bindings.hpp"
2324#include " rcutils/macros.h"
2425#include " shadow_node.hpp"
2526
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 ());
27+ bool IsRunningInElectronRenderer (const Napi::Env& env) {
28+ Napi::Object global = env.Global ();
29+ Napi::Object process = global.Get (" process" ).As <Napi::Object>();
30+ Napi::Value processType = process.Get (" type" );
31+ return processType.StrictEquals (Napi::String::New (env, " renderer" ));
3532}
3633
37- void InitModule (v8::Local<v8::Object> exports) {
34+ void Cleanup (Napi::Env env, void * data) {
35+ rclnodejs::RclHandle::CleanupThreadSafeFunction ();
36+ }
37+
38+ Napi::Object InitModule (Napi::Env env, Napi::Object exports) {
3839// workaround process name mangling by chromium
3940//
4041// rcl logging uses `program_invocation_name` to determine the log file,
@@ -43,52 +44,45 @@ void InitModule(v8::Local<v8::Object> exports) {
4344// occurence of ' -' with the null terminator. see:
4445// https://unix.stackexchange.com/questions/432419/unexpected-non-null-encoding-of-proc-pid-cmdline
4546#if defined(__linux__) && defined(__GLIBC__)
46- if (IsRunningInElectronRenderer ()) {
47+ if (IsRunningInElectronRenderer (env )) {
4748 auto prog_name = program_invocation_name;
4849 auto end = strstr (prog_name, " -" );
4950 assert (end);
5051 prog_name[end - prog_name] = 0 ;
5152 }
5253#endif
53-
54- v8::Local<v8::Context> context = exports->GetIsolate ()->GetCurrentContext ();
55-
56- 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 ());
62- }
63-
64- 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 ());
72- }
54+ rclnodejs::StoreEnv (env);
55+ // for (uint32_t i = 0; i < rclnodejs::binding_methods.size(); i++) {
56+ // exports.Set(rclnodejs::binding_methods[i].name,
57+ // Napi::Function::New(env,
58+ // rclnodejs::binding_methods[i].function));
59+ // }
60+ rclnodejs::InitBindings (env, exports);
61+ // for (uint32_t i = 0; i < rclnodejs::action_binding_methods.size(); i++) {
62+ // exports.Set(rclnodejs::action_binding_methods[i].name,
63+ // Napi::Function::New(env,
64+ // rclnodejs::action_binding_methods[i].function));
65+ // }
66+ rclnodejs::InitAction (env, exports);
7367
7468 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 ());
69+ exports.Set (rclnodejs::lifecycle_binding_methods[i].name ,
70+ Napi::Function::New (
71+ env, rclnodejs::lifecycle_binding_methods[i].function ));
8272 }
8373
84- rclnodejs::ShadowNode::Init (exports);
85- rclnodejs::RclHandle::Init (exports);
86-
74+ rclnodejs::ShadowNode::Init (env, exports);
75+ rclnodejs::RclHandle::Init (env, exports);
76+ // Initialize thread-safe function
77+ // rclnodejs::RclHandle::InitThreadSafeFunction(env);
8778#ifdef DEBUG_ON
8879 int result = rcutils_logging_set_logger_level (PACKAGE_NAME,
8980 RCUTILS_LOG_SEVERITY_DEBUG);
9081 RCUTILS_UNUSED (result);
9182#endif
83+ // Register cleanup handler
84+ // env.SetInstanceData<void>(nullptr, Cleanup);
85+ return exports;
9286}
9387
94- NODE_MODULE (rclnodejs, InitModule);
88+ NODE_API_MODULE (rclnodejs, InitModule)
0 commit comments