Commit c685df32 authored by Camillo Bruni's avatar Camillo Bruni Committed by Commit Bot

Reland "[api] Add v8::Isolate::ThrowError helper"

- This is a reland of d435eaa5
- Fix vtunedomain

Original change's description:
> [api] Add v8::Isolate::ThrowError helper
>
> Add a ThrowError helper to encourage throwing full Error objects
> instead of just v8::Strings.
>
> Bug: v8:11195
> Change-Id: I15d75b1d39b817de3b9026a836b57a70d7c16a28
> Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2811738
> Commit-Queue: Camillo Bruni <cbruni@chromium.org>
> Reviewed-by: Dan Elphick <delphick@chromium.org>
> Reviewed-by: Yang Guo <yangguo@chromium.org>
> Reviewed-by: Marja Hölttä <marja@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#73958}

Bug: v8:11195
Change-Id: I3cffaa4f122d74705476c3f8791b549f85d8c87b
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2826534Reviewed-by: 's avatarDan Elphick <delphick@chromium.org>
Reviewed-by: 's avatarYang Guo <yangguo@chromium.org>
Reviewed-by: 's avatarMarja Hölttä <marja@chromium.org>
Commit-Queue: Camillo Bruni <cbruni@chromium.org>
Cr-Commit-Position: refs/heads/master@{#73993}
parent 03f52964
......@@ -8869,6 +8869,17 @@ class V8_EXPORT Isolate {
*/
Local<Context> GetIncumbentContext();
/**
* Schedules a v8::Exception::Error with the given message.
* See ThrowException for more details. Templatized to provide compile-time
* errors in case of too long strings (see v8::String::NewFromUtf8Literal).
*/
template <int N>
Local<Value> ThrowError(const char (&message)[N]) {
return ThrowError(String::NewFromUtf8Literal(this, message));
}
Local<Value> ThrowError(Local<String> message);
/**
* Schedules an exception to be thrown when returning to JavaScript. When an
* exception has been scheduled it is illegal to invoke any JavaScript
......
......@@ -147,20 +147,17 @@ void Print(const v8::FunctionCallbackInfo<v8::Value>& args) {
// the argument into a JavaScript string.
void Read(const v8::FunctionCallbackInfo<v8::Value>& args) {
if (args.Length() != 1) {
args.GetIsolate()->ThrowException(
v8::String::NewFromUtf8Literal(args.GetIsolate(), "Bad parameters"));
args.GetIsolate()->ThrowError("Bad parameters");
return;
}
v8::String::Utf8Value file(args.GetIsolate(), args[0]);
if (*file == NULL) {
args.GetIsolate()->ThrowException(v8::String::NewFromUtf8Literal(
args.GetIsolate(), "Error loading file"));
args.GetIsolate()->ThrowError("Error loading file");
return;
}
v8::Local<v8::String> source;
if (!ReadFile(args.GetIsolate(), *file).ToLocal(&source)) {
args.GetIsolate()->ThrowException(v8::String::NewFromUtf8Literal(
args.GetIsolate(), "Error loading file"));
args.GetIsolate()->ThrowError("Error loading file");
return;
}
......@@ -175,19 +172,16 @@ void Load(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::HandleScope handle_scope(args.GetIsolate());
v8::String::Utf8Value file(args.GetIsolate(), args[i]);
if (*file == NULL) {
args.GetIsolate()->ThrowException(v8::String::NewFromUtf8Literal(
args.GetIsolate(), "Error loading file"));
args.GetIsolate()->ThrowError("Error loading file");
return;
}
v8::Local<v8::String> source;
if (!ReadFile(args.GetIsolate(), *file).ToLocal(&source)) {
args.GetIsolate()->ThrowException(v8::String::NewFromUtf8Literal(
args.GetIsolate(), "Error loading file"));
args.GetIsolate()->ThrowError("Error loading file");
return;
}
if (!ExecuteString(args.GetIsolate(), source, args[i], false, false)) {
args.GetIsolate()->ThrowException(v8::String::NewFromUtf8Literal(
args.GetIsolate(), "Error executing file"));
args.GetIsolate()->ThrowError("Error executing file");
return;
}
}
......
......@@ -8012,6 +8012,10 @@ v8::Local<v8::Context> Isolate::GetIncumbentContext() {
return Utils::ToLocal(context);
}
v8::Local<Value> Isolate::ThrowError(v8::Local<v8::String> message) {
return ThrowException(v8::Exception::Error(message));
}
v8::Local<Value> Isolate::ThrowException(v8::Local<v8::Value> value) {
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
ENTER_V8_DO_NOT_USE(isolate);
......
......@@ -47,8 +47,7 @@ static AsyncHooksWrap* UnwrapHook(
AsyncHooks* hooks = PerIsolateData::Get(isolate)->GetAsyncHooks();
if (!hooks->async_hook_ctor.Get(isolate)->HasInstance(hook)) {
isolate->ThrowException(String::NewFromUtf8Literal(
isolate, "Invalid 'this' passed instead of AsyncHooks instance"));
isolate->ThrowError("Invalid 'this' passed instead of AsyncHooks instance");
return nullptr;
}
......@@ -87,8 +86,7 @@ Local<Object> AsyncHooks::CreateHook(
Local<Context> currentContext = isolate->GetCurrentContext();
if (args.Length() != 1 || !args[0]->IsObject()) {
isolate->ThrowException(String::NewFromUtf8Literal(
isolate, "Invalid arguments passed to createHook"));
isolate->ThrowError("Invalid arguments passed to createHook");
return Local<Object>();
}
......
......@@ -43,8 +43,7 @@ void D8Console::Assert(const debug::ConsoleCallArguments& args,
// false-ish.
if (args.Length() > 0 && args[0]->BooleanValue(isolate_)) return;
WriteToFile("console.assert", stdout, isolate_, args);
isolate_->ThrowException(v8::Exception::Error(
v8::String::NewFromUtf8Literal(isolate_, "console.assert failed")));
isolate_->ThrowError("console.assert failed");
}
void D8Console::Log(const debug::ConsoleCallArguments& args,
......
This diff is collapsed.
......@@ -170,10 +170,8 @@ thread_local FastCApiObject kFastCApiObject;
// TODO(mslekova): Rename the fast_c_api helper to FastCAPI.
void CreateObject(const FunctionCallbackInfo<Value>& info) {
if (!info.IsConstructCall()) {
info.GetIsolate()->ThrowException(
v8::Exception::Error(String::NewFromUtf8Literal(
info.GetIsolate(),
"FastCAPI helper must be constructed with new.")));
info.GetIsolate()->ThrowError(
"FastCAPI helper must be constructed with new.");
return;
}
Local<Object> api_object = info.Holder();
......
This diff is collapsed.
......@@ -16,9 +16,8 @@ CpuTraceMarkExtension::GetNativeFunctionTemplate(v8::Isolate* isolate,
void CpuTraceMarkExtension::Mark(
const v8::FunctionCallbackInfo<v8::Value>& args) {
if (args.Length() < 1 || !args[0]->IsUint32()) {
args.GetIsolate()->ThrowException(v8::String::NewFromUtf8Literal(
args.GetIsolate(),
"First parameter to cputracemark() must be a unsigned int32."));
args.GetIsolate()->ThrowError(
"First parameter to cputracemark() must be a unsigned int32.");
return;
}
......
......@@ -59,9 +59,8 @@ ExternalizeStringExtension::GetNativeFunctionTemplate(
void ExternalizeStringExtension::Externalize(
const v8::FunctionCallbackInfo<v8::Value>& args) {
if (args.Length() < 1 || !args[0]->IsString()) {
args.GetIsolate()->ThrowException(v8::String::NewFromUtf8Literal(
args.GetIsolate(),
"First parameter to externalizeString() must be a string."));
args.GetIsolate()->ThrowError(
"First parameter to externalizeString() must be a string.");
return;
}
bool force_two_byte = false;
......@@ -69,17 +68,15 @@ void ExternalizeStringExtension::Externalize(
if (args[1]->IsBoolean()) {
force_two_byte = args[1]->BooleanValue(args.GetIsolate());
} else {
args.GetIsolate()->ThrowException(v8::String::NewFromUtf8Literal(
args.GetIsolate(),
"Second parameter to externalizeString() must be a boolean."));
args.GetIsolate()->ThrowError(
"Second parameter to externalizeString() must be a boolean.");
return;
}
}
bool result = false;
Handle<String> string = Utils::OpenHandle(*args[0].As<v8::String>());
if (!string->SupportsExternalization()) {
args.GetIsolate()->ThrowException(v8::String::NewFromUtf8Literal(
args.GetIsolate(), "string does not support externalization."));
args.GetIsolate()->ThrowError("string does not support externalization.");
return;
}
if (string->IsOneByteRepresentation() && !force_two_byte) {
......@@ -98,8 +95,7 @@ void ExternalizeStringExtension::Externalize(
if (!result) delete resource;
}
if (!result) {
args.GetIsolate()->ThrowException(v8::String::NewFromUtf8Literal(
args.GetIsolate(), "externalizeString() failed."));
args.GetIsolate()->ThrowError("externalizeString() failed.");
return;
}
}
......@@ -108,9 +104,8 @@ void ExternalizeStringExtension::Externalize(
void ExternalizeStringExtension::IsOneByte(
const v8::FunctionCallbackInfo<v8::Value>& args) {
if (args.Length() != 1 || !args[0]->IsString()) {
args.GetIsolate()->ThrowException(v8::String::NewFromUtf8Literal(
args.GetIsolate(),
"isOneByteString() requires a single string argument."));
args.GetIsolate()->ThrowError(
"isOneByteString() requires a single string argument.");
return;
}
bool is_one_byte =
......
......@@ -109,10 +109,9 @@ void VTuneDomainSupportExtension::Mark(
const v8::FunctionCallbackInfo<v8::Value>& args) {
if (args.Length() != 3 || !args[0]->IsString() || !args[1]->IsString() ||
!args[2]->IsString()) {
args.GetIsolate()->ThrowException(v8::String::NewFromUtf8Literal(
args.GetIsolate(),
args.GetIsolate()->ThrowError(
"Parameter number should be exactly three, first domain name"
"second task name, third start/end"));
"second task name, third start/end");
return;
}
......@@ -130,7 +129,7 @@ void VTuneDomainSupportExtension::Mark(
int r = 0;
if ((r = libvtune::invoke(params.str().c_str())) != 0) {
args.GetIsolate()->ThrowException(
args.GetIsolate()->ThrowError(
v8::String::NewFromUtf8(args.GetIsolate(), std::to_string(r).c_str())
.ToLocalChecked());
}
......
......@@ -743,8 +743,8 @@ void V8RuntimeAgentImpl::bindingCallback(
const v8::FunctionCallbackInfo<v8::Value>& info) {
v8::Isolate* isolate = info.GetIsolate();
if (info.Length() != 1 || !info[0]->IsString()) {
info.GetIsolate()->ThrowException(toV8String(
isolate, "Invalid arguments: should be exactly one string."));
info.GetIsolate()->ThrowError(
"Invalid arguments: should be exactly one string.");
return;
}
V8InspectorImpl* inspector =
......
......@@ -34,8 +34,8 @@ void WebSnapshotSerializerDeserializer::Throw(const char* message) {
error_message_ = message;
if (!isolate_->has_pending_exception()) {
v8::Isolate* v8_isolate = reinterpret_cast<v8::Isolate*>(isolate_);
v8_isolate->ThrowException(v8::Exception::Error(
v8::String::NewFromUtf8(v8_isolate, message).ToLocalChecked()));
v8_isolate->ThrowError(
v8::String::NewFromUtf8(v8_isolate, message).ToLocalChecked());
}
}
......
......@@ -3957,7 +3957,7 @@ struct FastApiReceiver {
static void SlowCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
v8::Object* receiver_obj = v8::Object::Cast(*info.Holder());
if (!IsValidUnwrapObject(receiver_obj)) {
info.GetIsolate()->ThrowException(v8_str("Called with a non-object."));
info.GetIsolate()->ThrowError("Called with a non-object.");
return;
}
FastApiReceiver* receiver =
......
......@@ -439,14 +439,14 @@ class InspectorExtension : public IsolateData::SetupGlobalTask {
static void AccessorGetter(v8::Local<v8::String> property,
const v8::PropertyCallbackInfo<v8::Value>& info) {
v8::Isolate* isolate = info.GetIsolate();
isolate->ThrowException(ToV8String(isolate, "Getter is called"));
isolate->ThrowError("Getter is called");
}
static void AccessorSetter(v8::Local<v8::String> property,
v8::Local<v8::Value> value,
const v8::PropertyCallbackInfo<void>& info) {
v8::Isolate* isolate = info.GetIsolate();
isolate->ThrowException(ToV8String(isolate, "Setter is called"));
isolate->ThrowError("Setter is called");
}
static void StoreCurrentStackTrace(
......
......@@ -171,7 +171,7 @@ class UtilsExtension : public IsolateData::SetupGlobalTask {
std::string filename(*str, str.length());
*chars = v8::internal::ReadFile(filename.c_str(), &exists);
if (!exists) {
isolate->ThrowException(ToV8String(isolate, "Error reading file"));
isolate->ThrowError("Error reading file");
return false;
}
return true;
......@@ -632,14 +632,14 @@ class InspectorExtension : public IsolateData::SetupGlobalTask {
static void AccessorGetter(v8::Local<v8::String> property,
const v8::PropertyCallbackInfo<v8::Value>& info) {
v8::Isolate* isolate = info.GetIsolate();
isolate->ThrowException(ToV8String(isolate, "Getter is called"));
isolate->ThrowError("Getter is called");
}
static void AccessorSetter(v8::Local<v8::String> property,
v8::Local<v8::Value> value,
const v8::PropertyCallbackInfo<void>& info) {
v8::Isolate* isolate = info.GetIsolate();
isolate->ThrowException(ToV8String(isolate, "Setter is called"));
isolate->ThrowError("Setter is called");
}
static void StoreCurrentStackTrace(
......
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