-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathErrorEvents.cpp
More file actions
182 lines (165 loc) · 7.25 KB
/
Copy pathErrorEvents.cpp
File metadata and controls
182 lines (165 loc) · 7.25 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "ErrorEvents.h"
#include "BuiltinLoader.h"
#include "Caches.h"
#include "Helpers.h"
#include "NativeScriptException.h"
#include "RuntimeConfig.h"
using namespace v8;
namespace tns {
// Native function handed to the error-events builtin as
// `binding.nativeReportFatal(error, stackString)`. It runs the terminal tail
// (shim + fatal log) WITHOUT
// re-dispatching an event: reportError and listener-thrown errors have already
// gone through JS dispatch, so dispatching again here would recurse.
static void NativeReportFatalCallback(const FunctionCallbackInfo<Value>& info) {
Isolate* isolate = info.GetIsolate();
Local<Value> error =
info.Length() > 0 ? info[0] : v8::Undefined(isolate).As<Value>();
std::string stack = info.Length() > 1 ? tns::ToString(isolate, info[1]) : "";
NativeScriptException::ReportFatalTail(isolate, error, Local<v8::Message>(),
stack, "");
}
void ErrorEvents::Init(Local<Context> context) {
// WHATWG error-events layer (internal/error-events.js), layered on top of
// the generic event primitives installed by Events::Init. The builtin
// receives — via its binding bag — the internal EventTarget backing the
// global (so native dispatch survives app code overwriting
// globalThis.dispatchEvent) and the native nativeReportFatal(error, stack)
// function that runs the terminal tail, and returns three closures bound to
// that backing store. ErrorEvent/PromiseRejectionEvent subclass the Event
// captured off globalThis at init time, which runs before any user code.
Isolate* isolate = v8::Isolate::GetCurrent();
auto cache = Caches::Get(isolate);
tns::Assert(cache != nullptr && cache->GlobalEventTarget != nullptr, isolate);
Local<Object> globalTarget = cache->GlobalEventTarget->Get(isolate);
Local<v8::Function> nativeReportFatal;
bool success = v8::Function::New(context, NativeReportFatalCallback)
.ToLocal(&nativeReportFatal);
tns::Assert(success, isolate);
Local<Object> binding = Object::New(isolate);
success =
binding
->Set(context, tns::ToV8String(isolate, "globalTarget"), globalTarget)
.FromMaybe(false) &&
binding
->Set(context, tns::ToV8String(isolate, "nativeReportFatal"),
nativeReportFatal)
.FromMaybe(false) &&
binding
->Set(context, tns::ToV8String(isolate, "isDebug"),
v8::Boolean::New(isolate, RuntimeConfig.IsDebug))
.FromMaybe(false);
tns::Assert(success, isolate);
Local<Value> result;
success = BuiltinLoader::RunBuiltin(context, BuiltinId::kErrorEvents, binding)
.ToLocal(&result);
tns::Assert(success && result->IsArray(), isolate);
Local<v8::Array> closures = result.As<v8::Array>();
Local<Value> errorFn, rejectionFn, handledFn;
tns::Assert(
closures->Get(context, 0).ToLocal(&errorFn) && errorFn->IsFunction(),
isolate);
tns::Assert(closures->Get(context, 1).ToLocal(&rejectionFn) &&
rejectionFn->IsFunction(),
isolate);
tns::Assert(
closures->Get(context, 2).ToLocal(&handledFn) && handledFn->IsFunction(),
isolate);
cache->DispatchErrorEventFunc = std::make_unique<Persistent<v8::Function>>(
isolate, errorFn.As<v8::Function>());
cache->DispatchUnhandledRejectionFunc =
std::make_unique<Persistent<v8::Function>>(
isolate, rejectionFn.As<v8::Function>());
cache->DispatchRejectionHandledFunc =
std::make_unique<Persistent<v8::Function>>(isolate,
handledFn.As<v8::Function>());
Local<Value> releasedFn;
tns::Assert(closures->Get(context, 3).ToLocal(&releasedFn) &&
releasedFn->IsFunction(),
isolate);
cache->DispatchReleasedNativeAccessFunc =
std::make_unique<Persistent<v8::Function>>(isolate,
releasedFn.As<v8::Function>());
}
// Dispatches the cancelable `error` ErrorEvent through the JS listener store.
// Returns true when a listener called preventDefault(). A dispatch that itself
// throws is logged and treated as unprevented so an error is never lost.
bool ErrorEvents::DispatchError(Isolate* isolate, Local<Value> error,
const std::string& messageString,
const std::string& stack) {
auto cache = Caches::Get(isolate);
if (cache == nullptr || cache->DispatchErrorEventFunc == nullptr) {
return false;
}
Local<Context> context = isolate->GetCurrentContext();
Local<v8::Function> dispatch = cache->DispatchErrorEventFunc->Get(isolate);
Local<Value> args[] = {error, tns::ToV8String(isolate, messageString),
tns::ToV8String(isolate, stack)};
Local<Value> result;
TryCatch tc(isolate);
bool success =
dispatch->Call(context, context->Global(), 3, args).ToLocal(&result);
if (tc.HasCaught()) {
tns::LogError(isolate, tc);
return false;
}
return success && !result.IsEmpty() && result->BooleanValue(isolate);
}
bool ErrorEvents::DispatchUnhandledRejection(Isolate* isolate,
Local<Promise> promise,
Local<Value> reason) {
auto cache = Caches::Get(isolate);
if (cache == nullptr || cache->DispatchUnhandledRejectionFunc == nullptr) {
return false;
}
Local<Context> context = isolate->GetCurrentContext();
Local<v8::Function> dispatch =
cache->DispatchUnhandledRejectionFunc->Get(isolate);
Local<Value> args[] = {promise, reason};
Local<Value> result;
TryCatch tc(isolate);
bool success =
dispatch->Call(context, context->Global(), 2, args).ToLocal(&result);
if (tc.HasCaught()) {
tns::LogError(isolate, tc);
return false;
}
return success && !result.IsEmpty() && result->BooleanValue(isolate);
}
void ErrorEvents::DispatchRejectionHandled(Isolate* isolate,
Local<Promise> promise,
Local<Value> reason) {
auto cache = Caches::Get(isolate);
if (cache == nullptr || cache->DispatchRejectionHandledFunc == nullptr) {
return;
}
Local<Context> context = isolate->GetCurrentContext();
Local<v8::Function> dispatch =
cache->DispatchRejectionHandledFunc->Get(isolate);
Local<Value> args[] = {promise, reason};
Local<Value> result;
TryCatch tc(isolate);
if (!dispatch->Call(context, context->Global(), 2, args).ToLocal(&result) &&
tc.HasCaught()) {
tns::LogError(isolate, tc);
}
}
void ErrorEvents::DispatchReleasedNativeAccess(Isolate* isolate,
Local<Value> error,
const std::string& operation) {
auto cache = Caches::Get(isolate);
if (cache == nullptr || cache->DispatchReleasedNativeAccessFunc == nullptr) {
return;
}
Local<Context> context = isolate->GetCurrentContext();
Local<v8::Function> dispatch =
cache->DispatchReleasedNativeAccessFunc->Get(isolate);
Local<Value> args[] = {error, tns::ToV8String(isolate, operation)};
Local<Value> result;
TryCatch tc(isolate);
if (!dispatch->Call(context, context->Global(), 2, args).ToLocal(&result) &&
tc.HasCaught()) {
tns::LogError(isolate, tc);
}
}
} // namespace tns