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