• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef SRC_NODE_BINDING_H_
2 #define SRC_NODE_BINDING_H_
3 
4 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5 
6 #if defined(__POSIX__)
7 #include <dlfcn.h>
8 #endif
9 
10 #include "node.h"
11 #define JSVM_EXPERIMENTAL
12 #include "jsvm_node_api.h"
13 #include "uv.h"
14 
15 enum {
16   NM_F_BUILTIN = 1 << 0,  // Unused.
17   NM_F_LINKED = 1 << 1,
18   NM_F_INTERNAL = 1 << 2,
19   NM_F_DELETEME = 1 << 3,
20 };
21 
22 // Make sure our internal values match the public API's values.
23 static_assert(static_cast<int>(NM_F_LINKED) ==
24                   static_cast<int>(node::ModuleFlags::kLinked),
25               "NM_F_LINKED != node::ModuleFlags::kLinked");
26 
27 #define NODE_BINDINGS_WITH_PER_ISOLATE_INIT(V) V(builtins)
28 
29 #define NODE_BINDING_CONTEXT_AWARE_CPP(modname, regfunc, priv, flags)          \
30   static node::node_module _module = {                                         \
31       NODE_MODULE_VERSION,                                                     \
32       flags,                                                                   \
33       nullptr,                                                                 \
34       __FILE__,                                                                \
35       nullptr,                                                                 \
36       (node::addon_context_register_func)(regfunc),                            \
37       NODE_STRINGIFY(modname),                                                 \
38       priv,                                                                    \
39       nullptr};                                                                \
40   void _register_##modname() { node_module_register(&_module); }
41 
42 void jsvm_module_register_by_symbol(
43     v8::Local<v8::Object> exports,
44     v8::Local<v8::Value> module,
45     v8::Local<v8::Context> context,
46     jsvm_addon_register_func init,
47     int32_t module_api_version = NODE_API_DEFAULT_MODULE_API_VERSION);
48 
49 node::addon_context_register_func get_node_api_context_register_func(
50     node::Environment* node_env,
51     const char* module_name,
52     int32_t module_api_version);
53 
54 namespace node {
55 
56 // Define a node internal binding that may be loaded in a context of
57 // a node::Environment.
58 // If an internal binding needs initializing per-isolate templates, define
59 // with NODE_BINDING_PER_ISOLATE_INIT too.
60 #define NODE_BINDING_CONTEXT_AWARE_INTERNAL(modname, regfunc)                  \
61   NODE_BINDING_CONTEXT_AWARE_CPP(modname, regfunc, nullptr, NM_F_INTERNAL)
62 
63 // Define a per-isolate initialization function for a node internal binding.
64 #define NODE_BINDING_PER_ISOLATE_INIT(modname, per_isolate_func)               \
65   void _register_isolate_##modname(node::IsolateData* isolate_data,            \
66                                    v8::Local<v8::FunctionTemplate> target) {   \
67     per_isolate_func(isolate_data, target);                                    \
68   }
69 
70 // Globals per process
71 // This is set by node::Init() which is used by embedders
72 extern bool node_is_initialized;
73 
74 namespace binding {
75 
76 class DLib {
77  public:
78 #ifdef __POSIX__
79   static const int kDefaultFlags = RTLD_LAZY;
80 #else
81   static const int kDefaultFlags = 0;
82 #endif
83 
84   DLib(const char* filename, int flags);
85 
86   bool Open();
87   void Close();
88   void* GetSymbolAddress(const char* name);
89   void SaveInGlobalHandleMap(node_module* mp);
90   node_module* GetSavedModuleFromGlobalHandleMap();
91 
92   const std::string filename_;
93   const int flags_;
94   std::string errmsg_;
95   void* handle_;
96 #ifndef __POSIX__
97   uv_lib_t lib_;
98 #endif
99   bool has_entry_in_global_handle_map_ = false;
100 
101   DLib(const DLib&) = delete;
102   DLib& operator=(const DLib&) = delete;
103 };
104 
105 // Call _register<module_name> functions for all of
106 // the built-in bindings. Because built-in bindings don't
107 // use the __attribute__((constructor)). Need to
108 // explicitly call the _register* functions.
109 void RegisterBuiltinBindings();
110 // Create per-isolate templates for the internal bindings.
111 void CreateInternalBindingTemplates(IsolateData* isolate_data);
112 void GetInternalBinding(const v8::FunctionCallbackInfo<v8::Value>& args);
113 void GetLinkedBinding(const v8::FunctionCallbackInfo<v8::Value>& args);
114 void DLOpen(const v8::FunctionCallbackInfo<v8::Value>& args);
115 
116 }  // namespace binding
117 
118 }  // namespace node
119 
120 #endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
121 #endif  // SRC_NODE_BINDING_H_
122