File: binding.cc

package info (click to toggle)
nodejs 20.19.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 219,072 kB
  • sloc: cpp: 1,277,408; javascript: 565,332; ansic: 129,476; python: 58,536; sh: 3,841; makefile: 2,725; asm: 1,732; perl: 248; lisp: 222; xml: 42
file content (52 lines) | stat: -rw-r--r-- 1,173 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <node.h>
#include <v8.h>

#ifndef _WIN32

#include <dlfcn.h>

extern "C"
__attribute__((visibility("default")))
const char* dlopen_pong(void) {
  return "pong";
}

namespace {

using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;

typedef const char* (*ping)(void);

static ping ping_func;

void LoadLibrary(const FunctionCallbackInfo<Value>& args) {
  const String::Utf8Value filename(args.GetIsolate(), args[0]);
  void* handle = dlopen(*filename, RTLD_LAZY);
  if (handle == nullptr) fprintf(stderr, "%s\n", dlerror());
  assert(handle != nullptr);
  ping_func = reinterpret_cast<ping>(dlsym(handle, "dlopen_ping"));
  assert(ping_func != nullptr);
}

void Ping(const FunctionCallbackInfo<Value>& args) {
  Isolate* isolate = args.GetIsolate();
  assert(ping_func != nullptr);
  args.GetReturnValue().Set(String::NewFromUtf8(
        isolate, ping_func()).ToLocalChecked());
}

void init(Local<Object> exports) {
  NODE_SET_METHOD(exports, "load", LoadLibrary);
  NODE_SET_METHOD(exports, "ping", Ping);
}

NODE_MODULE(NODE_GYP_MODULE_NAME, init)

}  // anonymous namespace

#endif  // _WIN32