Repository navigation
Expand file tree
/
Copy patherrors.cc
More file actions
143 lines (127 loc) · 4.75 KB
/
Copy patherrors.cc
File metadata and controls
143 lines (127 loc) · 4.75 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <stdlib.h>
#include <sstream>
#include "deps/include/v8-debug.h"
#include "deps/include/v8-exception.h"
#include "deps/include/v8-isolate.h"
#include "deps/include/v8-message.h"
#include "deps/include/v8-primitive.h"
#include "errors.h"
#include "isolate.h"
#include "utils.h"
using namespace v8;
// The serialization format is parsed by parseMessage in errors.go.
// Integers are 32 bits, little-endian. Strings are a length, followed by
// UTF-8 bytes.
static void AppendInt(std::string& buf, int32_t v) {
// A single append, which a byte-by-byte push_back loop isn't optimized
// into. The swap is a no-op on all our targets.
uint32_t le = static_cast<uint32_t>(v);
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
le = __builtin_bswap32(le);
#endif
buf.append(reinterpret_cast<const char*>(&le), sizeof(le));
}
static void AppendString(std::string& buf, Isolate* iso, Local<Value> v) {
if (v.IsEmpty() || v->IsUndefined()) {
AppendInt(buf, 0);
return;
}
String::Utf8Value s(iso, v);
AppendInt(buf, s.length());
buf.append(*s, s.length());
}
static std::string SerializeMessage(Isolate* iso,
Local<Context> ctx,
Local<Message> msg) {
std::string buf;
// Avoids growing the buffer repeatedly. The header is 36 bytes of
// lengths and integers, plus the text, script name and source line,
// e.g. 60, 40 and 150 bytes. A larger message only costs reallocations.
buf.reserve(256);
AppendString(buf, iso, msg->Get());
AppendString(buf, iso, msg->GetScriptResourceName());
Local<String> source_line;
AppendString(buf, iso,
msg->GetSourceLine(ctx).ToLocal(&source_line)
? source_line.As<Value>()
: Local<Value>());
AppendInt(buf, msg->GetLineNumber(ctx).FromMaybe(0));
AppendInt(buf, msg->GetStartPosition());
AppendInt(buf, msg->GetEndPosition());
AppendInt(buf, msg->GetStartColumn());
AppendInt(buf, msg->GetEndColumn());
AppendInt(buf, msg->GetWasmFunctionIndex());
// Captured because of SetCaptureStackTraceForUncaughtExceptions.
Local<StackTrace> trace = msg->GetStackTrace();
int num_frames = trace.IsEmpty() ? 0 : trace->GetFrameCount();
// The count, and per frame 20 bytes of lengths and integers, plus the
// script and function names, e.g. 30 and 14 bytes.
buf.reserve(buf.size() + 4 + num_frames * 64);
AppendInt(buf, num_frames);
for (int i = 0; i < num_frames; i++) {
Local<StackFrame> frame = trace->GetFrame(iso, i);
AppendString(buf, iso, frame->GetScriptName());
AppendString(buf, iso, frame->GetFunctionName());
AppendInt(buf, frame->GetLineNumber());
AppendInt(buf, frame->GetColumn());
AppendInt(buf, (frame->IsEval() ? 1 : 0) |
(frame->IsConstructor() ? 2 : 0) |
(frame->IsWasm() ? 4 : 0) |
(frame->IsUserJavaScript() ? 8 : 0));
}
return buf;
}
RtnError ExceptionError(TryCatch& try_catch, Isolate* iso, Local<Context> ctx) {
HandleScope handle_scope(iso);
RtnError rtn = {};
if (try_catch.HasTerminated()) {
if (IsolateTakeHeapLimitReached(iso)) {
// The script has unwound, so its garbage can be collected. This
// makes AutomaticallyRestoreInitialHeapLimit restore the limit,
// which it only does when the heap is small enough. Otherwise, the
// next script reaching the limit would raise it further.
iso->LowMemoryNotification();
rtn.msg = CopyString("ExecutionTerminated: heap limit reached");
rtn.heap_limit_reached = 1;
} else {
rtn.msg = CopyString(
"ExecutionTerminated: script execution has been terminated");
}
return rtn;
}
String::Utf8Value exception(iso, try_catch.Exception());
rtn.msg = CopyString(exception);
Local<Message> msg = try_catch.Message();
if (!msg.IsEmpty()) {
String::Utf8Value origin(iso, msg->GetScriptOrigin().ResourceName());
std::ostringstream sb;
sb << *origin;
Maybe<int> line = try_catch.Message()->GetLineNumber(ctx);
if (line.IsJust()) {
sb << ":" << line.ToChecked();
}
Maybe<int> start = try_catch.Message()->GetStartColumn(ctx);
if (start.IsJust()) {
sb << ":"
<< start.ToChecked() + 1; // + 1 to match output from stack trace
}
rtn.location = CopyString(sb.str());
if (IsolateExceptionMessages(iso)) {
std::string buf = SerializeMessage(iso, ctx, msg);
rtn.message = CopyString(buf);
rtn.message_length = buf.size();
}
}
Local<Value> mstack;
if (try_catch.StackTrace(ctx).ToLocal(&mstack)) {
String::Utf8Value stack(iso, mstack);
rtn.stack = CopyString(stack);
}
return rtn;
}
void ErrorRelease(RtnError err) {
free(err.msg);
free(err.location);
free(err.stack);
free(err.message);
}