Skip to content

Commit 341bce2

Browse files
victorgomesV8 LUCI CQ
authored andcommitted
[api] Add Error.cause to V8 API
Bug: chromium:1192162 Change-Id: I3423f8d58ea5eb0c6fbe82775cbc75606e7e9b8b Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/4898682 Commit-Queue: Victor Gomes <victorgomes@chromium.org> Reviewed-by: Camillo Bruni <cbruni@chromium.org> Cr-Commit-Position: refs/heads/main@{#90180}
1 parent 09d22d6 commit 341bce2

4 files changed

Lines changed: 40 additions & 25 deletions

File tree

include/v8-exception.h

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,21 @@ class ThreadLocalTop;
3030
*/
3131
class V8_EXPORT Exception {
3232
public:
33-
static Local<Value> RangeError(Local<String> message);
34-
static Local<Value> ReferenceError(Local<String> message);
35-
static Local<Value> SyntaxError(Local<String> message);
36-
static Local<Value> TypeError(Local<String> message);
37-
static Local<Value> WasmCompileError(Local<String> message);
38-
static Local<Value> WasmLinkError(Local<String> message);
39-
static Local<Value> WasmRuntimeError(Local<String> message);
40-
static Local<Value> Error(Local<String> message);
33+
static Local<Value> RangeError(Local<String> message,
34+
Local<Value> options = {});
35+
static Local<Value> ReferenceError(Local<String> message,
36+
Local<Value> options = {});
37+
static Local<Value> SyntaxError(Local<String> message,
38+
Local<Value> options = {});
39+
static Local<Value> TypeError(Local<String> message,
40+
Local<Value> options = {});
41+
static Local<Value> WasmCompileError(Local<String> message,
42+
Local<Value> options = {});
43+
static Local<Value> WasmLinkError(Local<String> message,
44+
Local<Value> options = {});
45+
static Local<Value> WasmRuntimeError(Local<String> message,
46+
Local<Value> options = {});
47+
static Local<Value> Error(Local<String> message, Local<Value> options = {});
4148

4249
/**
4350
* Creates an error message for the given exception.

src/api/api.cc

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10566,20 +10566,25 @@ String::Value::Value(v8::Isolate* v8_isolate, v8::Local<v8::Value> obj)
1056610566

1056710567
String::Value::~Value() { i::DeleteArray(str_); }
1056810568

10569-
#define DEFINE_ERROR(NAME, name) \
10570-
Local<Value> Exception::NAME(v8::Local<v8::String> raw_message) { \
10571-
i::Isolate* i_isolate = i::Isolate::Current(); \
10572-
API_RCS_SCOPE(i_isolate, NAME, New); \
10573-
ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate); \
10574-
i::Tagged<i::Object> error; \
10575-
{ \
10576-
i::HandleScope scope(i_isolate); \
10577-
i::Handle<i::String> message = Utils::OpenHandle(*raw_message); \
10578-
i::Handle<i::JSFunction> constructor = i_isolate->name##_function(); \
10579-
error = *i_isolate->factory()->NewError(constructor, message); \
10580-
} \
10581-
i::Handle<i::Object> result(error, i_isolate); \
10582-
return Utils::ToLocal(result); \
10569+
#define DEFINE_ERROR(NAME, name) \
10570+
Local<Value> Exception::NAME(v8::Local<v8::String> raw_message, \
10571+
v8::Local<v8::Value> raw_options) { \
10572+
i::Isolate* i_isolate = i::Isolate::Current(); \
10573+
API_RCS_SCOPE(i_isolate, NAME, New); \
10574+
ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate); \
10575+
i::Tagged<i::Object> error; \
10576+
{ \
10577+
i::HandleScope scope(i_isolate); \
10578+
i::Handle<i::Object> options; \
10579+
if (!raw_options.IsEmpty()) { \
10580+
options = Utils::OpenHandle(*raw_options); \
10581+
} \
10582+
i::Handle<i::String> message = Utils::OpenHandle(*raw_message); \
10583+
i::Handle<i::JSFunction> constructor = i_isolate->name##_function(); \
10584+
error = *i_isolate->factory()->NewError(constructor, message, options); \
10585+
} \
10586+
i::Handle<i::Object> result(error, i_isolate); \
10587+
return Utils::ToLocal(result); \
1058310588
}
1058410589

1058510590
DEFINE_ERROR(RangeError, range_error)

src/heap/factory.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2452,13 +2452,15 @@ Handle<JSObject> Factory::NewError(Handle<JSFunction> constructor,
24522452
}
24532453

24542454
Handle<JSObject> Factory::NewError(Handle<JSFunction> constructor,
2455-
Handle<String> message) {
2455+
Handle<String> message,
2456+
Handle<Object> options) {
24562457
// Construct a new error object. If an exception is thrown, use the exception
24572458
// as the result.
24582459

24592460
Handle<Object> no_caller;
2461+
if (options.is_null()) options = undefined_value();
24602462
return ErrorUtils::Construct(isolate(), constructor, constructor, message,
2461-
undefined_value(), SKIP_NONE, no_caller,
2463+
options, SKIP_NONE, no_caller,
24622464
ErrorUtils::StackTraceCollection::kEnabled)
24632465
.ToHandleChecked();
24642466
}

src/heap/factory.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,8 @@ class V8_EXPORT_PRIVATE Factory : public FactoryBase<Factory> {
802802

803803
// Interface for creating error objects.
804804
Handle<JSObject> NewError(Handle<JSFunction> constructor,
805-
Handle<String> message);
805+
Handle<String> message,
806+
Handle<Object> options = Handle<Object>());
806807

807808
Handle<Object> NewInvalidStringLengthError();
808809

0 commit comments

Comments
 (0)