forked from google/pprof-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcode-event-record.cc
More file actions
127 lines (108 loc) · 4.17 KB
/
code-event-record.cc
File metadata and controls
127 lines (108 loc) · 4.17 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
118
119
120
121
122
123
124
125
126
127
/*
* Copyright 2023 Datadog, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <node.h>
#include "code-event-record.hh"
namespace dd {
CodeEventRecord::CodeEventRecord(uintptr_t address,
uintptr_t previousAddress,
size_t size,
int line,
int column,
std::string comment,
std::string functionName,
std::string scriptName)
: address(address),
previousAddress(previousAddress),
size(size),
line(line),
column(column),
comment(comment),
functionName(functionName),
scriptName(scriptName) {}
std::string safe_string(const char* maybe_string) {
return maybe_string == nullptr ? "" : maybe_string;
}
std::string safe_string(v8::Isolate* isolate,
v8::Local<v8::String> maybe_string) {
auto len = maybe_string->Utf8Length(isolate);
std::string buffer(len + 1, 0);
maybe_string->WriteUtf8(isolate, &buffer[0], len + 1);
return std::string(buffer.c_str());
}
CodeEventRecord::CodeEventRecord(v8::Isolate* isolate,
v8::CodeEvent* code_event)
: CodeEventRecord(code_event->GetCodeStartAddress(),
// CodeEvent::GetPreviousCodeStartAddress didn't exist until Node.js 13.
#if NODE_MODULE_VERSION > 79
code_event->GetPreviousCodeStartAddress(),
#else
0,
#endif
code_event->GetCodeSize(),
code_event->GetScriptLine(),
code_event->GetScriptColumn(),
safe_string(code_event->GetComment()),
safe_string(isolate, code_event->GetFunctionName()),
safe_string(isolate, code_event->GetScriptName())) {
}
void CodeEventRecord::SetScriptId(int _scriptId) {
scriptId = _scriptId;
}
v8::Local<v8::Integer> CodeEventRecord::GetScriptId(v8::Isolate* isolate) {
return v8::Integer::New(isolate, scriptId);
}
v8::Local<v8::Integer> CodeEventRecord::GetAddress(v8::Isolate* isolate) {
return v8::Integer::NewFromUnsigned(isolate, address);
}
v8::Local<v8::Integer> CodeEventRecord::GetPreviousAddress(
v8::Isolate* isolate) {
return v8::Integer::NewFromUnsigned(isolate, previousAddress);
}
v8::Local<v8::Integer> CodeEventRecord::GetSize(v8::Isolate* isolate) {
return v8::Integer::NewFromUnsigned(isolate, size);
}
v8::Local<v8::Value> CodeEventRecord::GetFunctionName(v8::Isolate* isolate) {
if (functionName.empty()) {
return v8::Undefined(isolate);
}
return Nan::New(functionName).ToLocalChecked();
}
v8::Local<v8::Value> CodeEventRecord::GetScriptName(v8::Isolate* isolate) {
if (scriptName.empty()) {
return v8::Undefined(isolate);
}
return Nan::New(scriptName).ToLocalChecked();
}
v8::Local<v8::Integer> CodeEventRecord::GetLine(v8::Isolate* isolate) {
return v8::Integer::New(isolate, line);
}
v8::Local<v8::Integer> CodeEventRecord::GetColumn(v8::Isolate* isolate) {
return v8::Integer::New(isolate, column);
}
v8::Local<v8::Value> CodeEventRecord::GetComment(v8::Isolate* isolate) {
if (comment.empty()) {
return v8::Undefined(isolate);
}
return Nan::New(comment).ToLocalChecked();
}
bool CodeEventRecord::Equal(const CodeEventRecord* rhs) const {
return scriptId == rhs->scriptId && address == rhs->address &&
previousAddress == rhs->previousAddress && size == rhs->size &&
line == rhs->line && column == rhs->column &&
comment == rhs->comment && functionName == rhs->functionName &&
scriptName == rhs->scriptName;
}
}; // namespace dd