11cb0ef41Sopenharmony_ci#ifndef SRC_NODE_BINDING_H_
21cb0ef41Sopenharmony_ci#define SRC_NODE_BINDING_H_
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci#if defined(__POSIX__)
71cb0ef41Sopenharmony_ci#include <dlfcn.h>
81cb0ef41Sopenharmony_ci#endif
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci#include "node.h"
111cb0ef41Sopenharmony_ci#define JSVM_EXPERIMENTAL
121cb0ef41Sopenharmony_ci#include "jsvm_node_api.h"
131cb0ef41Sopenharmony_ci#include "uv.h"
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_cienum {
161cb0ef41Sopenharmony_ci  NM_F_BUILTIN = 1 << 0,  // Unused.
171cb0ef41Sopenharmony_ci  NM_F_LINKED = 1 << 1,
181cb0ef41Sopenharmony_ci  NM_F_INTERNAL = 1 << 2,
191cb0ef41Sopenharmony_ci  NM_F_DELETEME = 1 << 3,
201cb0ef41Sopenharmony_ci};
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci// Make sure our internal values match the public API's values.
231cb0ef41Sopenharmony_cistatic_assert(static_cast<int>(NM_F_LINKED) ==
241cb0ef41Sopenharmony_ci                  static_cast<int>(node::ModuleFlags::kLinked),
251cb0ef41Sopenharmony_ci              "NM_F_LINKED != node::ModuleFlags::kLinked");
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci#define NODE_BINDINGS_WITH_PER_ISOLATE_INIT(V) V(builtins)
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci#define NODE_BINDING_CONTEXT_AWARE_CPP(modname, regfunc, priv, flags)          \
301cb0ef41Sopenharmony_ci  static node::node_module _module = {                                         \
311cb0ef41Sopenharmony_ci      NODE_MODULE_VERSION,                                                     \
321cb0ef41Sopenharmony_ci      flags,                                                                   \
331cb0ef41Sopenharmony_ci      nullptr,                                                                 \
341cb0ef41Sopenharmony_ci      __FILE__,                                                                \
351cb0ef41Sopenharmony_ci      nullptr,                                                                 \
361cb0ef41Sopenharmony_ci      (node::addon_context_register_func)(regfunc),                            \
371cb0ef41Sopenharmony_ci      NODE_STRINGIFY(modname),                                                 \
381cb0ef41Sopenharmony_ci      priv,                                                                    \
391cb0ef41Sopenharmony_ci      nullptr};                                                                \
401cb0ef41Sopenharmony_ci  void _register_##modname() { node_module_register(&_module); }
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_civoid jsvm_module_register_by_symbol(
431cb0ef41Sopenharmony_ci    v8::Local<v8::Object> exports,
441cb0ef41Sopenharmony_ci    v8::Local<v8::Value> module,
451cb0ef41Sopenharmony_ci    v8::Local<v8::Context> context,
461cb0ef41Sopenharmony_ci    jsvm_addon_register_func init,
471cb0ef41Sopenharmony_ci    int32_t module_api_version = NODE_API_DEFAULT_MODULE_API_VERSION);
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_cinode::addon_context_register_func get_node_api_context_register_func(
501cb0ef41Sopenharmony_ci    node::Environment* node_env,
511cb0ef41Sopenharmony_ci    const char* module_name,
521cb0ef41Sopenharmony_ci    int32_t module_api_version);
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_cinamespace node {
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci// Define a node internal binding that may be loaded in a context of
571cb0ef41Sopenharmony_ci// a node::Environment.
581cb0ef41Sopenharmony_ci// If an internal binding needs initializing per-isolate templates, define
591cb0ef41Sopenharmony_ci// with NODE_BINDING_PER_ISOLATE_INIT too.
601cb0ef41Sopenharmony_ci#define NODE_BINDING_CONTEXT_AWARE_INTERNAL(modname, regfunc)                  \
611cb0ef41Sopenharmony_ci  NODE_BINDING_CONTEXT_AWARE_CPP(modname, regfunc, nullptr, NM_F_INTERNAL)
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci// Define a per-isolate initialization function for a node internal binding.
641cb0ef41Sopenharmony_ci#define NODE_BINDING_PER_ISOLATE_INIT(modname, per_isolate_func)               \
651cb0ef41Sopenharmony_ci  void _register_isolate_##modname(node::IsolateData* isolate_data,            \
661cb0ef41Sopenharmony_ci                                   v8::Local<v8::FunctionTemplate> target) {   \
671cb0ef41Sopenharmony_ci    per_isolate_func(isolate_data, target);                                    \
681cb0ef41Sopenharmony_ci  }
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci// Globals per process
711cb0ef41Sopenharmony_ci// This is set by node::Init() which is used by embedders
721cb0ef41Sopenharmony_ciextern bool node_is_initialized;
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_cinamespace binding {
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ciclass DLib {
771cb0ef41Sopenharmony_ci public:
781cb0ef41Sopenharmony_ci#ifdef __POSIX__
791cb0ef41Sopenharmony_ci  static const int kDefaultFlags = RTLD_LAZY;
801cb0ef41Sopenharmony_ci#else
811cb0ef41Sopenharmony_ci  static const int kDefaultFlags = 0;
821cb0ef41Sopenharmony_ci#endif
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci  DLib(const char* filename, int flags);
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci  bool Open();
871cb0ef41Sopenharmony_ci  void Close();
881cb0ef41Sopenharmony_ci  void* GetSymbolAddress(const char* name);
891cb0ef41Sopenharmony_ci  void SaveInGlobalHandleMap(node_module* mp);
901cb0ef41Sopenharmony_ci  node_module* GetSavedModuleFromGlobalHandleMap();
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci  const std::string filename_;
931cb0ef41Sopenharmony_ci  const int flags_;
941cb0ef41Sopenharmony_ci  std::string errmsg_;
951cb0ef41Sopenharmony_ci  void* handle_;
961cb0ef41Sopenharmony_ci#ifndef __POSIX__
971cb0ef41Sopenharmony_ci  uv_lib_t lib_;
981cb0ef41Sopenharmony_ci#endif
991cb0ef41Sopenharmony_ci  bool has_entry_in_global_handle_map_ = false;
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci  DLib(const DLib&) = delete;
1021cb0ef41Sopenharmony_ci  DLib& operator=(const DLib&) = delete;
1031cb0ef41Sopenharmony_ci};
1041cb0ef41Sopenharmony_ci
1051cb0ef41Sopenharmony_ci// Call _register<module_name> functions for all of
1061cb0ef41Sopenharmony_ci// the built-in bindings. Because built-in bindings don't
1071cb0ef41Sopenharmony_ci// use the __attribute__((constructor)). Need to
1081cb0ef41Sopenharmony_ci// explicitly call the _register* functions.
1091cb0ef41Sopenharmony_civoid RegisterBuiltinBindings();
1101cb0ef41Sopenharmony_ci// Create per-isolate templates for the internal bindings.
1111cb0ef41Sopenharmony_civoid CreateInternalBindingTemplates(IsolateData* isolate_data);
1121cb0ef41Sopenharmony_civoid GetInternalBinding(const v8::FunctionCallbackInfo<v8::Value>& args);
1131cb0ef41Sopenharmony_civoid GetLinkedBinding(const v8::FunctionCallbackInfo<v8::Value>& args);
1141cb0ef41Sopenharmony_civoid DLOpen(const v8::FunctionCallbackInfo<v8::Value>& args);
1151cb0ef41Sopenharmony_ci
1161cb0ef41Sopenharmony_ci}  // namespace binding
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ci}  // namespace node
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
1211cb0ef41Sopenharmony_ci#endif  // SRC_NODE_BINDING_H_
122