Skip to content

Commit 34794d2

Browse files
committed
fixup! src,lib: initial ffi implementation
1 parent c6f3f88 commit 34794d2

4 files changed

Lines changed: 25 additions & 7 deletions

File tree

src/node_binding.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "node_binding.h"
2+
#include <dlfcn.h>
23
#include <atomic>
34
#include "env-inl.h"
45
#include "node_builtins.h"
@@ -328,10 +329,15 @@ static struct global_handle_map_t {
328329
} global_handle_map;
329330

330331
DLib::DLib(const char* filename, int flags)
331-
: filename_(filename), flags_(flags), handle_(nullptr) {}
332+
: filename_(filename), flags_(flags), handle_(nullptr), is_self_(false) {}
333+
DLib::DLib(int flags) : flags_(flags), handle_(nullptr), is_self_(true) {}
332334

333335
#ifdef __POSIX__
334336
bool DLib::Open() {
337+
if (is_self_) {
338+
handle_ = RTLD_DEFAULT;
339+
return true;
340+
}
335341
handle_ = dlopen(filename_.c_str(), flags_);
336342
if (handle_ != nullptr) return true;
337343
errmsg_ = dlerror();
@@ -363,6 +369,7 @@ void* DLib::GetSymbolAddress(const char* name) {
363369
}
364370
#else // !__POSIX__
365371
bool DLib::Open() {
372+
if (is_self_) return true;
366373
int ret = uv_dlopen(filename_.c_str(), &lib_);
367374
if (ret == 0) {
368375
handle_ = static_cast<void*>(lib_.handle);

src/node_binding.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class DLib {
7777
#endif
7878

7979
DLib(const char* filename, int flags);
80+
DLib(int flags);
8081

8182
bool Open();
8283
void Close();
@@ -92,6 +93,7 @@ class DLib {
9293
uv_lib_t lib_;
9394
#endif
9495
bool has_entry_in_global_handle_map_ = false;
96+
bool is_self_;
9597

9698
DLib(const DLib&) = delete;
9799
DLib& operator=(const DLib&) = delete;

src/node_ffi.cc

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,15 @@ void GetLibrary(const v8::FunctionCallbackInfo<Value>& args) {
4242
if (libraries[fname] != nullptr) {
4343
lib = libraries[fname];
4444
} else {
45-
lib = new binding::DLib(fname.c_str(), binding::DLib::kDefaultFlags);
45+
if (isNull) {
46+
lib = new binding::DLib(binding::DLib::kDefaultFlags);
47+
} else {
48+
lib = new binding::DLib(fname.c_str(), binding::DLib::kDefaultFlags);
49+
}
4650
libraries[fname] = lib;
47-
if (!isNull) {
48-
bool success = lib->Open();
49-
if (!success) {
50-
// TODO(bengl) what now?
51-
}
51+
bool success = lib->Open();
52+
if (!success) {
53+
// TODO(bengl) what now?
5254
}
5355
}
5456
ReturnBigInt(env->isolate(), lib);

test/parallel/test-ffi-self.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
const ffi = require('node:ffi');
5+
6+
const getpid = ffi.getNativeFunction(null, 'uv_os_getpid', 'int', []);
7+
assert.strictEqual(process.pid, getpid());

0 commit comments

Comments
 (0)