11cb0ef41Sopenharmony_ci#include <node.h>
21cb0ef41Sopenharmony_ci#include <v8.h>
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci#ifndef _WIN32
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci#include <dlfcn.h>
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciextern "C"
91cb0ef41Sopenharmony_ci__attribute__((visibility("default")))
101cb0ef41Sopenharmony_ciconst char* dlopen_pong(void) {
111cb0ef41Sopenharmony_ci  return "pong";
121cb0ef41Sopenharmony_ci}
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_cinamespace {
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciusing v8::FunctionCallbackInfo;
171cb0ef41Sopenharmony_ciusing v8::Isolate;
181cb0ef41Sopenharmony_ciusing v8::Local;
191cb0ef41Sopenharmony_ciusing v8::Object;
201cb0ef41Sopenharmony_ciusing v8::String;
211cb0ef41Sopenharmony_ciusing v8::Value;
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_citypedef const char* (*ping)(void);
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_cistatic ping ping_func;
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_civoid LoadLibrary(const FunctionCallbackInfo<Value>& args) {
281cb0ef41Sopenharmony_ci  const String::Utf8Value filename(args.GetIsolate(), args[0]);
291cb0ef41Sopenharmony_ci  void* handle = dlopen(*filename, RTLD_LAZY);
301cb0ef41Sopenharmony_ci  if (handle == nullptr) fprintf(stderr, "%s\n", dlerror());
311cb0ef41Sopenharmony_ci  assert(handle != nullptr);
321cb0ef41Sopenharmony_ci  ping_func = reinterpret_cast<ping>(dlsym(handle, "dlopen_ping"));
331cb0ef41Sopenharmony_ci  assert(ping_func != nullptr);
341cb0ef41Sopenharmony_ci}
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_civoid Ping(const FunctionCallbackInfo<Value>& args) {
371cb0ef41Sopenharmony_ci  Isolate* isolate = args.GetIsolate();
381cb0ef41Sopenharmony_ci  assert(ping_func != nullptr);
391cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(String::NewFromUtf8(
401cb0ef41Sopenharmony_ci        isolate, ping_func()).ToLocalChecked());
411cb0ef41Sopenharmony_ci}
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_civoid init(Local<Object> exports) {
441cb0ef41Sopenharmony_ci  NODE_SET_METHOD(exports, "load", LoadLibrary);
451cb0ef41Sopenharmony_ci  NODE_SET_METHOD(exports, "ping", Ping);
461cb0ef41Sopenharmony_ci}
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ciNODE_MODULE(NODE_GYP_MODULE_NAME, init)
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci}  // anonymous namespace
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci#endif  // _WIN32
53