0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/addons/dlopen-ping-pong/binding.cc
Ben Noordhuis cc0748f509 test: export public symbols in addons tests
Upcoming changes to node-gyp will turn on `-fvisibility=hidden` on
macOS. Ensure that public symbols that are dlsym'd have default
visibility.

Refs: https://github.com/nodejs/node/pull/28647
Refs: https://github.com/nodejs/node-gyp/pull/1828

PR-URL: https://github.com/nodejs/node/pull/28717
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2020-01-17 18:51:50 -08:00

54 lines
1.2 KiB
C++

#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::NewStringType;
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(), NewStringType::kNormal).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