Commit 1e758861 authored by Gus Caplan's avatar Gus Caplan Committed by Commit Bot

[api] Redesign the Isolate PrepareStackTrace API

This CL replaces the stack trace parameter with a the array that is
usually passed to the JS prepareStackTrace callback. This allows two
important goals to be realized: 1) we can easily stringify individual
frames and 2) we can (if needed) call back into JS from this callback
with a usable structure. If, as is sometimes the case, a v8::StackTrace
is needed, |v8::Exception::GetStackTrace| can be used on the exception
that is passed to PrepareStackTraceCallback.

Bug: v8:7637

Cq-Include-Trybots: luci.chromium.try:linux_chromium_rel_ng
Change-Id: I57fa1f2b4552cc7f69351fe0918f4e59e3f5fce1
Reviewed-on: https://chromium-review.googlesource.com/c/1266698Reviewed-by: 's avatarYang Guo <yangguo@chromium.org>
Reviewed-by: 's avatarJakob Gruber <jgruber@chromium.org>
Commit-Queue: Jakob Gruber <jgruber@chromium.org>
Cr-Commit-Position: refs/heads/master@{#56880}
parent 727003c6
......@@ -6487,10 +6487,12 @@ typedef void (*HostInitializeImportMetaObjectCallback)(Local<Context> context,
* PrepareStackTraceCallback is called when the stack property of an error is
* first accessed. The return value will be used as the stack value. If this
* callback is registed, the |Error.prepareStackTrace| API will be disabled.
* |sites| is an array of call sites, specified in
* https://github.com/v8/v8/wiki/Stack-Trace-API
*/
typedef MaybeLocal<Value> (*PrepareStackTraceCallback)(Local<Context> context,
Local<Value> error,
Local<StackTrace> trace);
Local<Array> sites);
/**
* PromiseHook with type kInit is called when a new promise is
......
......@@ -4024,16 +4024,14 @@ void Isolate::SetHostInitializeImportMetaObjectCallback(
}
MaybeHandle<Object> Isolate::RunPrepareStackTraceCallback(
Handle<Context> context, Handle<JSObject> error) {
Handle<Context> context, Handle<JSObject> error, Handle<JSArray> sites) {
v8::Local<v8::Context> api_context = Utils::ToLocal(context);
v8::Local<StackTrace> trace =
Utils::StackTraceToLocal(GetDetailedStackTrace(error));
v8::Local<v8::Value> stack;
ASSIGN_RETURN_ON_SCHEDULED_EXCEPTION_VALUE(
this, stack,
prepare_stack_trace_callback_(api_context, Utils::ToLocal(error), trace),
prepare_stack_trace_callback_(api_context, Utils::ToLocal(error),
Utils::ToLocal(sites)),
MaybeHandle<Object>());
return Utils::OpenHandle(*stack);
}
......
......@@ -1556,7 +1556,8 @@ class Isolate : private HiddenFactory {
void SetPrepareStackTraceCallback(PrepareStackTraceCallback callback);
MaybeHandle<Object> RunPrepareStackTraceCallback(Handle<Context>,
Handle<JSObject> Error);
Handle<JSObject> Error,
Handle<JSArray> sites);
bool HasPrepareStackTraceCallback() const;
void SetRAILMode(RAILMode rail_mode);
......
......@@ -963,10 +963,15 @@ MaybeHandle<Object> ErrorUtils::FormatStackTrace(Isolate* isolate,
DCHECK(!error_context.is_null() && error_context->IsNativeContext());
PrepareStackTraceScope scope(isolate);
Handle<JSArray> sites;
ASSIGN_RETURN_ON_EXCEPTION(isolate, sites, GetStackFrames(isolate, elems),
Object);
Handle<Object> result;
ASSIGN_RETURN_ON_EXCEPTION(
isolate, result,
isolate->RunPrepareStackTraceCallback(error_context, error), Object);
isolate->RunPrepareStackTraceCallback(error_context, error, sites),
Object);
return result;
} else {
Handle<JSFunction> global_error = isolate->error_function();
......
......@@ -8999,13 +8999,13 @@ THREADED_TEST(ToArrayIndex) {
static v8::MaybeLocal<Value> PrepareStackTrace42(v8::Local<Context> context,
v8::Local<Value> error,
v8::Local<StackTrace> trace) {
v8::Local<Array> trace) {
return v8::Number::New(context->GetIsolate(), 42);
}
static v8::MaybeLocal<Value> PrepareStackTraceThrow(
v8::Local<Context> context, v8::Local<Value> error,
v8::Local<StackTrace> trace) {
static v8::MaybeLocal<Value> PrepareStackTraceThrow(v8::Local<Context> context,
v8::Local<Value> error,
v8::Local<Array> trace) {
v8::Isolate* isolate = context->GetIsolate();
v8::Local<String> message = v8_str("42");
isolate->ThrowException(v8::Exception::Error(message));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment