forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_dlopen.cc
More file actions
117 lines (85 loc) · 3.26 KB
/
Copy pathnode_dlopen.cc
File metadata and controls
117 lines (85 loc) · 3.26 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "node.h"
#include "node_buffer.h"
#include "v8.h"
#include "env.h"
#include "env-inl.h"
namespace node {
namespace dlopen {
using v8::Boolean;
using v8::Context;
using v8::FunctionCallbackInfo;
using v8::Handle;
using v8::Integer;
using v8::Local;
using v8::Number;
using v8::Object;
using v8::String;
using v8::Value;
using v8::Uint32;
static void Dlopen(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
const char* filename;
if (args[0]->IsNull()) {
filename = nullptr;
} else if (args[0]->IsString()) {
node::Utf8Value name(env->isolate(), args[0]);
filename = *name;
} else {
return env->ThrowTypeError(
"expected a string filename or null as first argument");
}
if (!Buffer::HasInstance(args[1]))
return env->ThrowTypeError("expected a Buffer instance as second argument");
Local<Object> buf = args[1].As<Object>();
uv_lib_t* lib = reinterpret_cast<uv_lib_t*>(Buffer::Data(buf));
int r = uv_dlopen(filename, lib);
args.GetReturnValue().Set(r);
}
static void Dlclose(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
if (!Buffer::HasInstance(args[0]))
return env->ThrowTypeError("expected a Buffer instance as first argument");
Local<Object> buf = args[0].As<Object>();
uv_lib_t* lib = reinterpret_cast<uv_lib_t*>(Buffer::Data(buf));
uv_dlclose(lib);
}
static void Dlsym(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
if (!Buffer::HasInstance(args[0]))
return env->ThrowTypeError("expected a Buffer instance as first argument");
if (!args[1]->IsString())
return env->ThrowTypeError("expected a string as second argument");
if (!Buffer::HasInstance(args[2]))
return env->ThrowTypeError("expected a Buffer instance as third argument");
Local<Object> buf = args[0].As<Object>();
uv_lib_t* lib = reinterpret_cast<uv_lib_t*>(Buffer::Data(buf));
void* sym;
node::Utf8Value name(env->isolate(), args[1]);
int r = uv_dlsym(lib, *name, &sym);
Local<Object> sym_buf = args[2].As<Object>();
memcpy(Buffer::Data(sym_buf), &sym, sizeof(sym));
args.GetReturnValue().Set(r);
}
static void Dlerror(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
if (!Buffer::HasInstance(args[0]))
return env->ThrowTypeError("expected a Buffer instance as first argument");
Local<Object> buf = args[0].As<Object>();
uv_lib_t* lib = reinterpret_cast<uv_lib_t*>(Buffer::Data(buf));
args.GetReturnValue().Set(OneByteString(env->isolate(), uv_dlerror(lib)));
}
void Initialize(Handle<Object> target,
Handle<Value> unused,
Handle<Context> context) {
Environment* env = Environment::GetCurrent(context);
env->SetMethod(target, "dlopen", Dlopen);
env->SetMethod(target, "dlclose", Dlclose);
env->SetMethod(target, "dlsym", Dlsym);
env->SetMethod(target, "dlerror", Dlerror);
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "sizeof_uv_lib_t"),
Uint32::NewFromUnsigned(env->isolate(),
static_cast<uint32_t>(sizeof(uv_lib_t))));
}
} // namespace dlopen
} // namespace node
NODE_MODULE_CONTEXT_AWARE_BUILTIN(dlopen, node::dlopen::Initialize)