Skip to content

Commit d2c44f5

Browse files
committed
NOT TO MERGE
1 parent ff07c86 commit d2c44f5

21 files changed

+1682
-1517
lines changed

binding.gyp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
],
3030
'include_dirs': [
3131
'.',
32-
"<!(node -e \"require('nan')\")",
3332
'<(ros_include_root)',
33+
"<!@(node -p \"require('node-addon-api').include\")",
3434
],
3535
'cflags!': [
3636
'-fno-exceptions'

lib/action/server.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ class ActionServer extends Entity {
130130
this.qos.statusSubQosProfile,
131131
this.options.resultTimeout
132132
);
133-
134133
node._addActionServer(this);
135134
}
136135

lib/logging.js

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,31 @@ let LoggingSeverity = {
3939

4040
class Caller {
4141
constructor() {
42-
this._info = {};
43-
let frame = new Error().stack.split('\n').slice(4, 5)[0];
44-
let results = frame.match(/at\s+(.*)\s+\((.*):(\d*):(\d*)\)/i);
45-
46-
if (results && results.length === 5) {
47-
this._info['functionName'] = results[1];
48-
this._info['lineNumber'] = results[3];
49-
this._info['fileName'] = path.basename(results[2]);
42+
this._info = {
43+
functionName: 'unknown',
44+
fileName: 'unknown',
45+
lineNumber: 'unknown',
46+
};
47+
48+
const stackLines = new Error().stack.split('\n');
49+
50+
// Adjust the index (usually 3 or 4) to correctly point to the caller frame
51+
const callerFrame = stackLines[4] || stackLines[3];
52+
// Match both named and anonymous function stack frames
53+
const frameRegex = /^\s*at\s+(?:(.+)\s+\()?(.+):(\d+):(\d+)\)?$/;
54+
const match = callerFrame.match(frameRegex);
55+
if (match && match.length === 5) {
56+
this._info.functionName = match[1] || '(anonymous)';
57+
this._info.fileName = path.basename(match[2]);
58+
this._info.lineNumber = match[3];
59+
} else {
60+
// Handle anonymous functions or different stack formats
61+
const altMatch = callerFrame.match(/at\s+(.*):(\d+):(\d+)/i);
62+
if (altMatch && altMatch.length >= 4) {
63+
this._info.functionName = '(anonymous)';
64+
this._info.fileName = path.basename(altMatch[1]);
65+
this._info.lineNumber = altMatch[2];
66+
}
5067
}
5168
}
5269

@@ -156,7 +173,7 @@ class Logging {
156173
severity,
157174
message,
158175
caller.functionName,
159-
caller.lineNumber,
176+
parseInt(caller.lineNumber),
160177
caller.fileName
161178
);
162179
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@
7979
"fs-extra": "^11.2.0",
8080
"is-close": "^1.3.3",
8181
"json-bigint": "^1.0.0",
82-
"nan": "^2.22.0",
8382
"terser": "^5.39.0",
84-
"walk": "^2.3.15"
83+
"walk": "^2.3.15",
84+
"node-addon-api": "^5.0.0"
8585
},
8686
"husky": {
8787
"hooks": {

src/addon.cpp

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
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"
@@ -23,18 +24,18 @@
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)

src/executor.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@ struct RclResult {
4141
std::string error_msg;
4242
};
4343

44-
Executor::Executor(HandleManager* handle_manager, Delegate* delegate)
44+
Executor::Executor(Napi::Env env, HandleManager* handle_manager,
45+
Delegate* delegate)
4546
: async_(nullptr),
4647
main_thread_(uv_thread_self()),
4748
handle_manager_(handle_manager),
4849
delegate_(delegate),
49-
context_(nullptr) {
50+
context_(nullptr),
51+
env_(env) {
5052
running_.store(false);
5153
}
5254

@@ -74,20 +76,27 @@ void Executor::SpinOnce(rcl_context_t* context, int32_t time_out) {
7476
rcl_wait_set_t wait_set = rcl_get_zero_initialized_wait_set();
7577
rcl_ret_t ret = rcl_wait_set_init(&wait_set, 0, 0, 0, 0, 0, 0, context,
7678
rcl_get_default_allocator());
77-
if (ret != RCL_RET_OK) Nan::ThrowError(rcl_get_error_string().str);
79+
if (ret != RCL_RET_OK) {
80+
Napi::Error::New(env_, rcl_get_error_string().str)
81+
.ThrowAsJavaScriptException();
82+
return;
83+
}
7884

7985
RclResult wait_result = WaitForReadyCallbacks(&wait_set, time_out);
8086

81-
if (wait_result.ret != RCL_RET_OK)
82-
Nan::ThrowError(wait_result.error_msg.c_str());
87+
if (wait_result.ret != RCL_RET_OK) {
88+
Napi::Error::New(env_, wait_result.error_msg.c_str())
89+
.ThrowAsJavaScriptException();
90+
return;
91+
}
8392

8493
if (handle_manager_->ready_handles_count() > 0) ExecuteReadyHandles();
8594

8695
if (rcl_wait_set_fini(&wait_set) != RCL_RET_OK) {
8796
std::string error_message =
8897
std::string("Failed to destroy guard waitset:") +
8998
std::string(rcl_get_error_string().str);
90-
Nan::ThrowError(error_message.c_str());
99+
Napi::Error::New(env_, error_message.c_str()).ThrowAsJavaScriptException();
91100
}
92101
}
93102

src/executor.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#ifndef SRC_EXECUTOR_HPP_
1616
#define SRC_EXECUTOR_HPP_
1717

18+
#include <napi.h>
1819
#include <rcl/wait.h>
1920
#include <uv.h>
2021

@@ -37,7 +38,7 @@ class Executor {
3738
virtual void CatchException(std::exception_ptr e_ptr) = 0;
3839
};
3940

40-
Executor(HandleManager* handle_manager, Delegate* delegate);
41+
Executor(Napi::Env env, HandleManager* handle_manager, Delegate* delegate);
4142
~Executor();
4243

4344
void Start(rcl_context_t* context, int32_t time_out);
@@ -68,6 +69,7 @@ class Executor {
6869
Delegate* delegate_;
6970
rcl_context_t* context_;
7071
int32_t time_out_;
72+
Napi::Env env_;
7173

7274
std::atomic_bool running_;
7375
};

0 commit comments

Comments
 (0)