-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathdtrace_argument.cc
94 lines (71 loc) · 2.26 KB
/
dtrace_argument.cc
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
#include "dtrace_provider.h"
#include <nan.h>
namespace node {
using namespace v8;
// Integer Argument
#ifdef __x86_64__
# define INTMETHOD int64_t
#else
# define INTMETHOD int32_t
#endif
void * DTraceIntegerArgument::ArgumentValue(v8::Local<Value> value) {
if (value->IsUndefined())
return 0;
else
return (void *)(long) Nan::To<INTMETHOD>(value).FromJust();
}
void DTraceIntegerArgument::FreeArgument(void *arg) {
}
const char * DTraceIntegerArgument::Type() {
return "int";
}
// String Argument
void * DTraceStringArgument::ArgumentValue(v8::Local<Value> value) {
if (value->IsUndefined())
return (void *) strdup("undefined");
Nan::Utf8String str(value);
return (void *) strdup(*str);
}
void DTraceStringArgument::FreeArgument(void *arg) {
free(arg);
}
const char * DTraceStringArgument::Type() {
return "char *";
}
// JSON Argument
DTraceJsonArgument::DTraceJsonArgument() {
Nan::HandleScope scope;
v8::Local<Context> context = Nan::GetCurrentContext();
v8::Local<Object> global = context->Global();
v8::Local<String> json = Nan::New<String>("JSON").ToLocalChecked();
v8::Local<Object> l_JSON = Nan::To<v8::Object>(global->Get(json)).ToLocalChecked();
v8::Local<Function> l_JSON_stringify
= v8::Local<Function>::Cast(l_JSON->Get(Nan::New<String>("stringify").ToLocalChecked()));
JSON.Reset(l_JSON);
JSON_stringify.Reset(l_JSON_stringify);
}
DTraceJsonArgument::~DTraceJsonArgument() {
JSON.Reset();
JSON_stringify.Reset();
}
void * DTraceJsonArgument::ArgumentValue(v8::Local<Value> value) {
Nan::HandleScope scope;
if (value->IsUndefined())
return (void *) strdup("undefined");
v8::Local<Value> info[1];
info[0] = value;
v8::Local<Function> cb = Nan::New<Function>(JSON_stringify);
v8::Local<Object> obj = Nan::New<Object>(JSON);
Local<Value> j = Nan::Call(cb, obj, 1, info).ToLocalChecked();
if (*j == NULL)
return (void *) strdup("{ \"error\": \"stringify failed\" }");
Nan::Utf8String json(j);
return (void *) strdup(*json);
}
void DTraceJsonArgument::FreeArgument(void *arg) {
free(arg);
}
const char * DTraceJsonArgument::Type() {
return "char *";
}
} // namespace node