-
Notifications
You must be signed in to change notification settings - Fork 1
/
ref.h
66 lines (52 loc) · 1.61 KB
/
ref.h
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
// Source: @tootallnate https://gist.github.com/TooTallNate/3987725
// Maintain an up-to-date copy of this file when necessary.
#pragma once
#include <nan.h>
/*
* Called when the "pointer" is garbage collected.
*/
// UNUSED: however, inline functions don't generate code.
inline static void wrap_pointer_cb(char *data, void *hint) {
// fprintf(stderr, "wrap_pointer_cb\n");
}
/*
* Wraps "ptr" into a new SlowBuffer instance with size "length".
*/
inline static v8::Local<v8::Value> WrapPointer(void *ptr, size_t length) {
return Nan::NewBuffer(static_cast<char *>(ptr), length, wrap_pointer_cb, 0)
.ToLocalChecked();
}
/*
* Wraps "ptr" into a new SlowBuffer instance with length 0.
*/
inline static v8::Local<v8::Value> WrapPointer(void *ptr) {
return WrapPointer(ptr, 0);
}
/*
* Unwraps Buffer instance "buffer" to a C `char *` with the offset specified.
*/
inline static char *UnwrapPointer(v8::Local<v8::Value> buffer, int64_t offset) {
if (node::Buffer::HasInstance(buffer)) {
return node::Buffer::Data(buffer.As<v8::Object>()) + offset;
} else {
return 0;
}
}
/*
* Unwraps Buffer instance "buffer" to a C `char *` (no offset applied).
*/
inline static char *UnwrapPointer(v8::Local<v8::Value> buffer) {
if (node::Buffer::HasInstance(buffer)) {
return node::Buffer::Data(buffer.As<v8::Object>());
} else {
return 0;
}
}
/**
* Templated version of UnwrapPointer that does a reinterpret_cast() on the
* pointer before returning it.
*/
template <typename Type>
inline static Type UnwrapPointer(v8::Local<v8::Value> buffer) {
return reinterpret_cast<Type>(UnwrapPointer(buffer));
}