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

- This is a reland of d435eaa5e4
- 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/+/2826534
Reviewed-by: Dan Elphick <delphick@chromium.org>
Reviewed-by: Yang Guo <yangguo@chromium.org>
Reviewed-by: Marja Hölttä <marja@chromium.org>
Commit-Queue: Camillo Bruni <cbruni@chromium.org>
Cr-Commit-Position: refs/heads/master@{#73993}
This commit is contained in:
Camillo Bruni 2021-04-15 10:10:06 +02:00 committed by Commit Bot
parent 03f52964ea
commit c685df3226
16 changed files with 154 additions and 189 deletions

View File

@ -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

View File

@ -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;
}
}

View File

@ -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);

View File

@ -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>();
}

View File

@ -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,

View File

@ -159,8 +159,8 @@ class ExecArgs {
bool Init(Isolate* isolate, Local<Value> arg0, Local<Array> command_args) {
String::Utf8Value prog(isolate, arg0);
if (*prog == nullptr) {
isolate->ThrowException(String::NewFromUtf8Literal(
isolate, "os.system(): String conversion of program name failed"));
isolate->ThrowError(
"os.system(): String conversion of program name failed");
return false;
}
int len = prog.length() + 3;
@ -176,8 +176,8 @@ class ExecArgs {
String::Utf8Value utf8_arg(isolate, arg);
if (*utf8_arg == nullptr) {
exec_args_[i] = nullptr; // Consistent state for destructor.
isolate->ThrowException(String::NewFromUtf8Literal(
isolate, "os.system(): String conversion of argument failed."));
isolate->ThrowError(
"os.system(): String conversion of argument failed.");
return false;
}
int len = utf8_arg.length() + 1;
@ -214,8 +214,7 @@ static bool GetTimeouts(const v8::FunctionCallbackInfo<v8::Value>& args,
->Int32Value(args.GetIsolate()->GetCurrentContext())
.FromJust();
} else {
args.GetIsolate()->ThrowException(String::NewFromUtf8Literal(
args.GetIsolate(), "system: Argument 4 must be a number"));
args.GetIsolate()->ThrowError("system: Argument 4 must be a number");
return false;
}
}
@ -225,14 +224,19 @@ static bool GetTimeouts(const v8::FunctionCallbackInfo<v8::Value>& args,
->Int32Value(args.GetIsolate()->GetCurrentContext())
.FromJust();
} else {
args.GetIsolate()->ThrowException(String::NewFromUtf8Literal(
args.GetIsolate(), "system: Argument 3 must be a number"));
args.GetIsolate()->ThrowError("system: Argument 3 must be a number");
return false;
}
}
return true;
}
namespace {
v8::Local<v8::String> v8_strerror(v8::Isolate* isolate, int err) {
return v8::String::NewFromUtf8(isolate, strerror(err)).ToLocalChecked();
}
} // namespace
static const int kReadFD = 0;
static const int kWriteFD = 1;
@ -267,8 +271,7 @@ static bool ChildLaunchedOK(Isolate* isolate, int* exec_error_fds) {
bytes_read = read(exec_error_fds[kReadFD], &err, sizeof(err));
} while (bytes_read == -1 && errno == EINTR);
if (bytes_read != 0) {
isolate->ThrowException(
String::NewFromUtf8(isolate, strerror(err)).ToLocalChecked());
isolate->ThrowError(v8_strerror(isolate, err));
return false;
}
return true;
@ -286,8 +289,7 @@ static Local<Value> GetStdout(Isolate* isolate, int child_fd,
char buffer[kStdoutReadBufferSize];
if (fcntl(child_fd, F_SETFL, O_NONBLOCK) != 0) {
return isolate->ThrowException(
String::NewFromUtf8(isolate, strerror(errno)).ToLocalChecked());
return isolate->ThrowError(v8_strerror(isolate, errno));
}
int bytes_read;
@ -298,8 +300,7 @@ static Local<Value> GetStdout(Isolate* isolate, int child_fd,
if (errno == EAGAIN) {
if (!WaitOnFD(child_fd, read_timeout, total_timeout, start_time) ||
(TimeIsOut(start_time, total_timeout))) {
return isolate->ThrowException(String::NewFromUtf8Literal(
isolate, "Timed out waiting for output"));
return isolate->ThrowError("Timed out waiting for output");
}
continue;
} else if (errno == EINTR) {
@ -357,8 +358,7 @@ static bool WaitForChild(Isolate* isolate, int pid,
if (useconds < 1000000) useconds <<= 1;
if ((read_timeout != -1 && useconds / 1000 > read_timeout) ||
(TimeIsOut(start_time, total_timeout))) {
isolate->ThrowException(String::NewFromUtf8Literal(
isolate, "Timed out waiting for process to terminate"));
isolate->ThrowError("Timed out waiting for process to terminate");
kill(pid, SIGINT);
return false;
}
@ -367,16 +367,14 @@ static bool WaitForChild(Isolate* isolate, int pid,
char message[999];
snprintf(message, sizeof(message), "Child killed by signal %d",
child_info.si_status);
isolate->ThrowException(
String::NewFromUtf8(isolate, message).ToLocalChecked());
isolate->ThrowError(message);
return false;
}
if (child_info.si_code == CLD_EXITED && child_info.si_status != 0) {
char message[999];
snprintf(message, sizeof(message), "Child exited with status %d",
child_info.si_status);
isolate->ThrowException(
String::NewFromUtf8(isolate, message).ToLocalChecked());
isolate->ThrowError(message);
return false;
}
@ -389,8 +387,7 @@ static bool WaitForChild(Isolate* isolate, int pid,
char message[999];
snprintf(message, sizeof(message), "Child killed by signal %d",
WTERMSIG(child_status));
isolate->ThrowException(
String::NewFromUtf8(isolate, message).ToLocalChecked());
isolate->ThrowError(message);
return false;
}
if (WEXITSTATUS(child_status) != 0) {
@ -398,8 +395,7 @@ static bool WaitForChild(Isolate* isolate, int pid,
int exit_status = WEXITSTATUS(child_status);
snprintf(message, sizeof(message), "Child exited with status %d",
exit_status);
isolate->ThrowException(
String::NewFromUtf8(isolate, message).ToLocalChecked());
isolate->ThrowError(message);
return false;
}
@ -419,8 +415,7 @@ void Shell::System(const v8::FunctionCallbackInfo<v8::Value>& args) {
Local<Array> command_args;
if (args.Length() > 1) {
if (!args[1]->IsArray()) {
args.GetIsolate()->ThrowException(String::NewFromUtf8Literal(
args.GetIsolate(), "system: Argument 2 must be an array"));
args.GetIsolate()->ThrowError("system: Argument 2 must be an array");
return;
}
command_args = args[1].As<Array>();
@ -428,13 +423,11 @@ void Shell::System(const v8::FunctionCallbackInfo<v8::Value>& args) {
command_args = Array::New(args.GetIsolate(), 0);
}
if (command_args->Length() > ExecArgs::kMaxArgs) {
args.GetIsolate()->ThrowException(String::NewFromUtf8Literal(
args.GetIsolate(), "Too many arguments to system()"));
args.GetIsolate()->ThrowError("Too many arguments to system()");
return;
}
if (args.Length() < 1) {
args.GetIsolate()->ThrowException(String::NewFromUtf8Literal(
args.GetIsolate(), "Too few arguments to system()"));
args.GetIsolate()->ThrowError("Too few arguments to system()");
return;
}
@ -449,13 +442,11 @@ void Shell::System(const v8::FunctionCallbackInfo<v8::Value>& args) {
int stdout_fds[2];
if (pipe(exec_error_fds) != 0) {
args.GetIsolate()->ThrowException(
String::NewFromUtf8Literal(args.GetIsolate(), "pipe syscall failed."));
args.GetIsolate()->ThrowError("pipe syscall failed.");
return;
}
if (pipe(stdout_fds) != 0) {
args.GetIsolate()->ThrowException(
String::NewFromUtf8Literal(args.GetIsolate(), "pipe syscall failed."));
args.GetIsolate()->ThrowError("pipe syscall failed.");
return;
}
@ -493,29 +484,24 @@ void Shell::System(const v8::FunctionCallbackInfo<v8::Value>& args) {
void Shell::ChangeDirectory(const v8::FunctionCallbackInfo<v8::Value>& args) {
if (args.Length() != 1) {
args.GetIsolate()->ThrowException(String::NewFromUtf8Literal(
args.GetIsolate(), "chdir() takes one argument"));
args.GetIsolate()->ThrowError("chdir() takes one argument");
return;
}
String::Utf8Value directory(args.GetIsolate(), args[0]);
if (*directory == nullptr) {
args.GetIsolate()->ThrowException(String::NewFromUtf8Literal(
args.GetIsolate(),
"os.chdir(): String conversion of argument failed."));
args.GetIsolate()->ThrowError(
"os.chdir(): String conversion of argument failed.");
return;
}
if (chdir(*directory) != 0) {
args.GetIsolate()->ThrowException(
String::NewFromUtf8(args.GetIsolate(), strerror(errno))
.ToLocalChecked());
args.GetIsolate()->ThrowError(v8_strerror(args.GetIsolate(), errno));
return;
}
}
void Shell::SetUMask(const v8::FunctionCallbackInfo<v8::Value>& args) {
if (args.Length() != 1) {
args.GetIsolate()->ThrowException(String::NewFromUtf8Literal(
args.GetIsolate(), "umask() takes one argument"));
args.GetIsolate()->ThrowError("umask() takes one argument");
return;
}
if (args[0]->IsNumber()) {
@ -524,8 +510,7 @@ void Shell::SetUMask(const v8::FunctionCallbackInfo<v8::Value>& args) {
args.GetReturnValue().Set(previous);
return;
} else {
args.GetIsolate()->ThrowException(String::NewFromUtf8Literal(
args.GetIsolate(), "umask() argument must be numeric"));
args.GetIsolate()->ThrowError("umask() argument must be numeric");
return;
}
}
@ -534,13 +519,11 @@ static bool CheckItsADirectory(Isolate* isolate, char* directory) {
struct stat stat_buf;
int stat_result = stat(directory, &stat_buf);
if (stat_result != 0) {
isolate->ThrowException(
String::NewFromUtf8(isolate, strerror(errno)).ToLocalChecked());
isolate->ThrowError(v8_strerror(isolate, errno));
return false;
}
if ((stat_buf.st_mode & S_IFDIR) != 0) return true;
isolate->ThrowException(
String::NewFromUtf8(isolate, strerror(EEXIST)).ToLocalChecked());
isolate->ThrowError(v8_strerror(isolate, EEXIST));
return false;
}
@ -554,8 +537,7 @@ static bool mkdirp(Isolate* isolate, char* directory, mode_t mask) {
} else if (errno == ENOENT) { // Intermediate path element is missing.
char* last_slash = strrchr(directory, '/');
if (last_slash == nullptr) {
isolate->ThrowException(
String::NewFromUtf8(isolate, strerror(errno)).ToLocalChecked());
isolate->ThrowError(v8_strerror(isolate, errno));
return false;
}
*last_slash = 0;
@ -566,12 +548,10 @@ static bool mkdirp(Isolate* isolate, char* directory, mode_t mask) {
if (errno == EEXIST) {
return CheckItsADirectory(isolate, directory);
}
isolate->ThrowException(
String::NewFromUtf8(isolate, strerror(errno)).ToLocalChecked());
isolate->ThrowError(v8_strerror(isolate, errno));
return false;
} else {
isolate->ThrowException(
String::NewFromUtf8(isolate, strerror(errno)).ToLocalChecked());
isolate->ThrowError(v8_strerror(isolate, errno));
return false;
}
}
@ -584,20 +564,17 @@ void Shell::MakeDirectory(const v8::FunctionCallbackInfo<v8::Value>& args) {
->Int32Value(args.GetIsolate()->GetCurrentContext())
.FromJust();
} else {
args.GetIsolate()->ThrowException(String::NewFromUtf8Literal(
args.GetIsolate(), "mkdirp() second argument must be numeric"));
args.GetIsolate()->ThrowError("mkdirp() second argument must be numeric");
return;
}
} else if (args.Length() != 1) {
args.GetIsolate()->ThrowException(String::NewFromUtf8Literal(
args.GetIsolate(), "mkdirp() takes one or two arguments"));
args.GetIsolate()->ThrowError("mkdirp() takes one or two arguments");
return;
}
String::Utf8Value directory(args.GetIsolate(), args[0]);
if (*directory == nullptr) {
args.GetIsolate()->ThrowException(String::NewFromUtf8Literal(
args.GetIsolate(),
"os.mkdirp(): String conversion of argument failed."));
args.GetIsolate()->ThrowError(
"os.mkdirp(): String conversion of argument failed.");
return;
}
mkdirp(args.GetIsolate(), *directory, mask);
@ -605,15 +582,13 @@ void Shell::MakeDirectory(const v8::FunctionCallbackInfo<v8::Value>& args) {
void Shell::RemoveDirectory(const v8::FunctionCallbackInfo<v8::Value>& args) {
if (args.Length() != 1) {
args.GetIsolate()->ThrowException(String::NewFromUtf8Literal(
args.GetIsolate(), "rmdir() takes one or two arguments"));
args.GetIsolate()->ThrowError("rmdir() takes one or two arguments");
return;
}
String::Utf8Value directory(args.GetIsolate(), args[0]);
if (*directory == nullptr) {
args.GetIsolate()->ThrowException(String::NewFromUtf8Literal(
args.GetIsolate(),
"os.rmdir(): String conversion of argument failed."));
args.GetIsolate()->ThrowError(
"os.rmdir(): String conversion of argument failed.");
return;
}
rmdir(*directory);
@ -621,22 +596,19 @@ void Shell::RemoveDirectory(const v8::FunctionCallbackInfo<v8::Value>& args) {
void Shell::SetEnvironment(const v8::FunctionCallbackInfo<v8::Value>& args) {
if (args.Length() != 2) {
args.GetIsolate()->ThrowException(String::NewFromUtf8Literal(
args.GetIsolate(), "setenv() takes two arguments"));
args.GetIsolate()->ThrowError("setenv() takes two arguments");
return;
}
String::Utf8Value var(args.GetIsolate(), args[0]);
String::Utf8Value value(args.GetIsolate(), args[1]);
if (*var == nullptr) {
args.GetIsolate()->ThrowException(String::NewFromUtf8Literal(
args.GetIsolate(),
"os.setenv(): String conversion of variable name failed."));
args.GetIsolate()->ThrowError(
"os.setenv(): String conversion of variable name failed.");
return;
}
if (*value == nullptr) {
args.GetIsolate()->ThrowException(String::NewFromUtf8Literal(
args.GetIsolate(),
"os.setenv(): String conversion of variable contents failed."));
args.GetIsolate()->ThrowError(
"os.setenv(): String conversion of variable contents failed.");
return;
}
setenv(*var, *value, 1);
@ -644,15 +616,13 @@ void Shell::SetEnvironment(const v8::FunctionCallbackInfo<v8::Value>& args) {
void Shell::UnsetEnvironment(const v8::FunctionCallbackInfo<v8::Value>& args) {
if (args.Length() != 1) {
args.GetIsolate()->ThrowException(String::NewFromUtf8Literal(
args.GetIsolate(), "unsetenv() takes one argument"));
args.GetIsolate()->ThrowError("unsetenv() takes one argument");
return;
}
String::Utf8Value var(args.GetIsolate(), args[0]);
if (*var == nullptr) {
args.GetIsolate()->ThrowException(String::NewFromUtf8Literal(
args.GetIsolate(),
"os.setenv(): String conversion of variable name failed."));
args.GetIsolate()->ThrowError(
"os.setenv(): String conversion of variable name failed.");
return;
}
unsetenv(*var);

View File

@ -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();

View File

@ -333,11 +333,6 @@ class MultiMappedAllocator : public ArrayBufferAllocatorBase {
v8::Platform* g_default_platform;
std::unique_ptr<v8::Platform> g_platform;
static Local<Value> Throw(Isolate* isolate, const char* message) {
return isolate->ThrowException(v8::Exception::Error(
String::NewFromUtf8(isolate, message).ToLocalChecked()));
}
static MaybeLocal<Value> TryGetValue(v8::Isolate* isolate,
Local<Context> context,
Local<v8::Object> object,
@ -355,13 +350,13 @@ static Local<Value> GetValue(v8::Isolate* isolate, Local<Context> context,
std::shared_ptr<Worker> GetWorkerFromInternalField(Isolate* isolate,
Local<Object> object) {
if (object->InternalFieldCount() != 1) {
Throw(isolate, "this is not a Worker");
isolate->ThrowError("this is not a Worker");
return nullptr;
}
i::Handle<i::Object> handle = Utils::OpenHandle(*object->GetInternalField(0));
if (handle->IsSmi()) {
Throw(isolate, "Worker is defunct because main thread is terminating");
isolate->ThrowError("Worker is defunct because main thread is terminating");
return nullptr;
}
auto managed = i::Handle<i::Managed<Worker>>::cast(handle);
@ -737,7 +732,7 @@ bool Shell::ExecuteString(Isolate* isolate, Local<String> source,
if (options.web_snapshot_config) {
std::vector<std::string> exports;
if (!ReadLines(options.web_snapshot_config, exports)) {
Throw(isolate, "Web snapshots: unable to read config");
isolate->ThrowError("Web snapshots: unable to read config");
CHECK(try_catch.HasCaught());
ReportException(isolate, &try_catch);
return false;
@ -974,7 +969,8 @@ MaybeLocal<Module> Shell::FetchModuleTree(Local<Module> referrer,
CHECK(specifier_it != d->module_to_specifier_map.end());
msg += "\n imported by " + specifier_it->second;
}
Throw(isolate, msg.c_str());
isolate->ThrowError(
v8::String::NewFromUtf8(isolate, msg.c_str()).ToLocalChecked());
return MaybeLocal<Module>();
}
ScriptOrigin origin(
@ -1033,7 +1029,7 @@ MaybeLocal<Module> Shell::FetchModuleTree(Local<Module> referrer,
context, import_assertions, true);
if (request_module_type == ModuleType::kInvalid) {
Throw(isolate, "Invalid module type was asserted");
isolate->ThrowError("Invalid module type was asserted");
return MaybeLocal<Module>();
}
@ -1220,7 +1216,7 @@ void Shell::DoHostImportModuleDynamically(void* import_data) {
try_catch.SetVerbose(true);
if (module_type == ModuleType::kInvalid) {
Throw(isolate, "Invalid module type was asserted");
isolate->ThrowError("Invalid module type was asserted");
CHECK(try_catch.HasCaught());
resolver->Reject(realm, try_catch.Exception()).ToChecked();
return;
@ -1367,7 +1363,7 @@ bool Shell::ExecuteWebSnapshot(Isolate* isolate, const char* file_name) {
std::unique_ptr<uint8_t[]> snapshot_data(
reinterpret_cast<uint8_t*>(ReadChars(absolute_path.c_str(), &length)));
if (length == 0) {
Throw(isolate, "Error reading the web snapshot");
isolate->ThrowError("Error reading the web snapshot");
DCHECK(try_catch.HasCaught());
ReportException(isolate, &try_catch);
return false;
@ -1509,14 +1505,14 @@ int PerIsolateData::RealmFind(Local<Context> context) {
int PerIsolateData::RealmIndexOrThrow(
const v8::FunctionCallbackInfo<v8::Value>& args, int arg_offset) {
if (args.Length() < arg_offset || !args[arg_offset]->IsNumber()) {
Throw(args.GetIsolate(), "Invalid argument");
args.GetIsolate()->ThrowError("Invalid argument");
return -1;
}
int index = args[arg_offset]
->Int32Value(args.GetIsolate()->GetCurrentContext())
.FromMaybe(-1);
if (index < 0 || index >= realm_count_ || realms_[index].IsEmpty()) {
Throw(args.GetIsolate(), "Invalid realm index");
args.GetIsolate()->ThrowError("Invalid realm index");
return -1;
}
return index;
@ -1574,7 +1570,7 @@ void Shell::RealmOwner(const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate* isolate = args.GetIsolate();
PerIsolateData* data = PerIsolateData::Get(isolate);
if (args.Length() < 1 || !args[0]->IsObject()) {
Throw(args.GetIsolate(), "Invalid argument");
args.GetIsolate()->ThrowError("Invalid argument");
return;
}
Local<Object> object =
@ -1586,7 +1582,7 @@ void Shell::RealmOwner(const v8::FunctionCallbackInfo<v8::Value>& args) {
}
Local<Context> creation_context;
if (!object->GetCreationContext().ToLocal(&creation_context)) {
Throw(args.GetIsolate(), "object doesn't have creation context");
args.GetIsolate()->ThrowError("object doesn't have creation context");
return;
}
int index = data->RealmFind(creation_context);
@ -1670,7 +1666,7 @@ void Shell::RealmNavigate(const v8::FunctionCallbackInfo<v8::Value>& args) {
if (index == -1) return;
if (index == 0 || index == data->realm_current_ ||
index == data->realm_switch_) {
Throw(args.GetIsolate(), "Invalid realm index");
args.GetIsolate()->ThrowError("Invalid realm index");
return;
}
@ -1699,7 +1695,7 @@ void Shell::RealmDetachGlobal(const v8::FunctionCallbackInfo<v8::Value>& args) {
if (index == -1) return;
if (index == 0 || index == data->realm_current_ ||
index == data->realm_switch_) {
Throw(args.GetIsolate(), "Invalid realm index");
args.GetIsolate()->ThrowError("Invalid realm index");
return;
}
@ -1716,7 +1712,7 @@ void Shell::RealmDispose(const v8::FunctionCallbackInfo<v8::Value>& args) {
if (index == -1) return;
if (index == 0 || index == data->realm_current_ ||
index == data->realm_switch_) {
Throw(args.GetIsolate(), "Invalid realm index");
args.GetIsolate()->ThrowError("Invalid realm index");
return;
}
DisposeRealm(args, index);
@ -1738,7 +1734,7 @@ void Shell::RealmEval(const v8::FunctionCallbackInfo<v8::Value>& args) {
int index = data->RealmIndexOrThrow(args, 0);
if (index == -1) return;
if (args.Length() < 2 || !args[1]->IsString()) {
Throw(args.GetIsolate(), "Invalid argument");
args.GetIsolate()->ThrowError("Invalid argument");
return;
}
ScriptOrigin origin(isolate,
@ -1789,18 +1785,18 @@ void Shell::LogGetAndStop(const v8::FunctionCallbackInfo<v8::Value>& args) {
std::string file_name = i_isolate->logger()->file_name();
if (!i::Log::IsLoggingToTemporaryFile(file_name)) {
Throw(isolate, "Only capturing from temporary files is supported.");
isolate->ThrowError("Only capturing from temporary files is supported.");
return;
}
if (!i_isolate->logger()->is_logging()) {
Throw(isolate, "Logging not enabled.");
isolate->ThrowError("Logging not enabled.");
return;
}
std::string raw_log;
FILE* log_file = i_isolate->logger()->TearDownAndGetLogFile();
if (!log_file) {
Throw(isolate, "Log file does not exist.");
isolate->ThrowError("Log file does not exist.");
return;
}
@ -1809,7 +1805,7 @@ void Shell::LogGetAndStop(const v8::FunctionCallbackInfo<v8::Value>& args) {
base::Fclose(log_file);
if (!exists) {
Throw(isolate, "Unable to read log file.");
isolate->ThrowError("Unable to read log file.");
return;
}
Local<String> result =
@ -1825,13 +1821,13 @@ void Shell::TestVerifySourcePositions(
Isolate* isolate = args.GetIsolate();
// Check if the argument is a valid function.
if (args.Length() != 1) {
Throw(isolate, "Expected function as single argument.");
isolate->ThrowError("Expected function as single argument.");
return;
}
auto arg_handle = Utils::OpenHandle(*args[0]);
if (!arg_handle->IsHeapObject() || !i::Handle<i::HeapObject>::cast(arg_handle)
->IsJSFunctionOrBoundFunction()) {
Throw(isolate, "Expected function as single argument.");
isolate->ThrowError("Expected function as single argument.");
return;
}
@ -1848,7 +1844,7 @@ void Shell::TestVerifySourcePositions(
i::Handle<i::JSFunction> function = i::Handle<i::JSFunction>::cast(callable);
if (!function->shared().HasBytecodeArray()) {
Throw(isolate, "Function has no BytecodeArray attached.");
isolate->ThrowError("Function has no BytecodeArray attached.");
return;
}
i::Handle<i::BytecodeArray> bytecodes =
@ -1874,7 +1870,7 @@ void Shell::TestVerifySourcePositions(
if (has_baseline) {
if (offset_iterator->current_bytecode_offset() !=
bytecode_iterator.current_offset()) {
Throw(isolate, "Baseline bytecode offset mismatch.");
isolate->ThrowError("Baseline bytecode offset mismatch.");
return;
}
// Check that we map every address to this bytecode correctly.
@ -1886,7 +1882,8 @@ void Shell::TestVerifySourcePositions(
pc_lookup.AdvanceToPCOffset(pc);
if (pc_lookup.current_bytecode_offset() !=
bytecode_iterator.current_offset()) {
Throw(isolate, "Baseline bytecode offset mismatch for PC lookup.");
isolate->ThrowError(
"Baseline bytecode offset mismatch for PC lookup.");
return;
}
}
@ -1894,14 +1891,14 @@ void Shell::TestVerifySourcePositions(
bytecode_iterator.Advance();
if (has_baseline && !bytecode_iterator.done()) {
if (offset_iterator->done()) {
Throw(isolate, "Missing bytecode(s) in baseline offset mapping.");
isolate->ThrowError("Missing bytecode(s) in baseline offset mapping.");
return;
}
offset_iterator->Advance();
}
}
if (has_baseline && !offset_iterator->done()) {
Throw(isolate, "Excess offsets in baseline offset mapping.");
isolate->ThrowError("Excess offsets in baseline offset mapping.");
return;
}
}
@ -1987,7 +1984,7 @@ void Shell::Write(const v8::FunctionCallbackInfo<v8::Value>& args) {
void Shell::Read(const v8::FunctionCallbackInfo<v8::Value>& args) {
String::Utf8Value file(args.GetIsolate(), args[0]);
if (*file == nullptr) {
Throw(args.GetIsolate(), "Error loading file");
args.GetIsolate()->ThrowError("Error loading file");
return;
}
if (args.Length() == 2) {
@ -1999,7 +1996,7 @@ void Shell::Read(const v8::FunctionCallbackInfo<v8::Value>& args) {
}
Local<String> source = ReadFile(args.GetIsolate(), *file);
if (source.IsEmpty()) {
Throw(args.GetIsolate(), "Error loading file");
args.GetIsolate()->ThrowError("Error loading file");
return;
}
args.GetReturnValue().Set(source);
@ -2047,12 +2044,12 @@ void Shell::Load(const v8::FunctionCallbackInfo<v8::Value>& args) {
HandleScope handle_scope(args.GetIsolate());
String::Utf8Value file(args.GetIsolate(), args[i]);
if (*file == nullptr) {
Throw(args.GetIsolate(), "Error loading file");
args.GetIsolate()->ThrowError("Error loading file");
return;
}
Local<String> source = ReadFile(args.GetIsolate(), *file);
if (source.IsEmpty()) {
Throw(args.GetIsolate(), "Error loading file");
args.GetIsolate()->ThrowError("Error loading file");
return;
}
if (!ExecuteString(
@ -2061,7 +2058,7 @@ void Shell::Load(const v8::FunctionCallbackInfo<v8::Value>& args) {
kNoPrintResult,
options.quiet_load ? kNoReportExceptions : kReportExceptions,
kNoProcessMessageQueue)) {
Throw(args.GetIsolate(), "Error executing file");
args.GetIsolate()->ThrowError("Error executing file");
return;
}
}
@ -2124,7 +2121,7 @@ bool FunctionAndArgumentsToString(Local<Function> function,
function->FunctionProtoToString(context);
Local<String> function_string;
if (!maybe_function_string.ToLocal(&function_string)) {
Throw(isolate, "Failed to convert function to string");
isolate->ThrowError("Failed to convert function to string");
return false;
}
*source = String::NewFromUtf8Literal(isolate, "(");
@ -2133,7 +2130,7 @@ bool FunctionAndArgumentsToString(Local<Function> function,
*source = String::Concat(isolate, *source, middle);
if (!arguments.IsEmpty() && !arguments->IsUndefined()) {
if (!arguments->IsArray()) {
Throw(isolate, "'arguments' must be an array");
isolate->ThrowError("'arguments' must be an array");
return false;
}
Local<String> comma = String::NewFromUtf8Literal(isolate, ",");
@ -2145,12 +2142,12 @@ bool FunctionAndArgumentsToString(Local<Function> function,
MaybeLocal<Value> maybe_argument = array->Get(context, i);
Local<Value> argument;
if (!maybe_argument.ToLocal(&argument)) {
Throw(isolate, "Failed to get argument");
isolate->ThrowError("Failed to get argument");
return false;
}
Local<String> argument_string;
if (!JSON::Stringify(context, argument).ToLocal(&argument_string)) {
Throw(isolate, "Failed to convert argument to string");
isolate->ThrowError("Failed to convert argument to string");
return false;
}
*source = String::Concat(isolate, *source, argument_string);
@ -2165,7 +2162,7 @@ void Shell::WorkerNew(const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate* isolate = args.GetIsolate();
HandleScope handle_scope(isolate);
if (args.Length() < 1 || (!args[0]->IsString() && !args[0]->IsFunction())) {
Throw(isolate, "1st argument must be a string or a function");
isolate->ThrowError("1st argument must be a string or a function");
return;
}
@ -2179,7 +2176,7 @@ void Shell::WorkerNew(const v8::FunctionCallbackInfo<v8::Value>& args) {
Local<Value> arguments;
ReadWorkerTypeAndArguments(args, &worker_type, &arguments);
if (worker_type != WorkerType::kFunction) {
Throw(isolate, "Invalid or missing worker type");
isolate->ThrowError("Invalid or missing worker type");
return;
}
@ -2198,7 +2195,7 @@ void Shell::WorkerNew(const v8::FunctionCallbackInfo<v8::Value>& args) {
load_from_file = false;
} else if (worker_type != WorkerType::kNone &&
worker_type != WorkerType::kClassic) {
Throw(isolate, "Invalid worker type");
isolate->ThrowError("Invalid worker type");
return;
}
@ -2206,7 +2203,7 @@ void Shell::WorkerNew(const v8::FunctionCallbackInfo<v8::Value>& args) {
String::Utf8Value filename(isolate, args[0]);
source = ReadFile(isolate, *filename);
if (source.IsEmpty()) {
Throw(args.GetIsolate(), "Error loading worker script");
args.GetIsolate()->ThrowError("Error loading worker script");
return;
}
} else {
@ -2215,7 +2212,7 @@ void Shell::WorkerNew(const v8::FunctionCallbackInfo<v8::Value>& args) {
}
if (!args.IsConstructCall()) {
Throw(isolate, "Worker must be constructed with new");
isolate->ThrowError("Worker must be constructed with new");
return;
}
@ -2232,7 +2229,7 @@ void Shell::WorkerNew(const v8::FunctionCallbackInfo<v8::Value>& args) {
String::Utf8Value script(isolate, source);
if (!*script) {
Throw(isolate, "Can't get worker script");
isolate->ThrowError("Can't get worker script");
return;
}
@ -2246,7 +2243,7 @@ void Shell::WorkerNew(const v8::FunctionCallbackInfo<v8::Value>& args) {
i_isolate, kWorkerSizeEstimate, worker);
args.Holder()->SetInternalField(0, Utils::ToLocal(managed));
if (!Worker::StartWorkerThread(std::move(worker))) {
Throw(isolate, "Can't start thread");
isolate->ThrowError("Can't start thread");
return;
}
}
@ -2257,7 +2254,7 @@ void Shell::WorkerPostMessage(const v8::FunctionCallbackInfo<v8::Value>& args) {
HandleScope handle_scope(isolate);
if (args.Length() < 1) {
Throw(isolate, "Invalid argument");
isolate->ThrowError("Invalid argument");
return;
}
@ -3196,13 +3193,13 @@ void Shell::ReadBuffer(const v8::FunctionCallbackInfo<v8::Value>& args) {
String::Utf8Value filename(isolate, args[0]);
int length;
if (*filename == nullptr) {
Throw(isolate, "Error loading file");
isolate->ThrowError("Error loading file");
return;
}
uint8_t* data = reinterpret_cast<uint8_t*>(ReadChars(*filename, &length));
if (data == nullptr) {
Throw(isolate, "Error reading file");
isolate->ThrowError("Error reading file");
return;
}
std::unique_ptr<v8::BackingStore> backing_store =
@ -3869,7 +3866,7 @@ void Worker::PostMessageOut(const v8::FunctionCallbackInfo<v8::Value>& args) {
HandleScope handle_scope(isolate);
if (args.Length() < 1) {
Throw(isolate, "Invalid argument");
isolate->ThrowError("Invalid argument");
return;
}
@ -4447,7 +4444,8 @@ class Serializer : public ValueSerializer::Delegate {
Local<Value> element;
if (transfer_array->Get(context, i).ToLocal(&element)) {
if (!element->IsArrayBuffer()) {
Throw(isolate_, "Transfer array elements must be an ArrayBuffer");
isolate_->ThrowError(
"Transfer array elements must be an ArrayBuffer");
return Nothing<bool>();
}
@ -4455,8 +4453,8 @@ class Serializer : public ValueSerializer::Delegate {
if (std::find(array_buffers_.begin(), array_buffers_.end(),
array_buffer) != array_buffers_.end()) {
Throw(isolate_,
"ArrayBuffer occurs in the transfer array more than once");
isolate_->ThrowError(
"ArrayBuffer occurs in the transfer array more than once");
return Nothing<bool>();
}
@ -4471,7 +4469,7 @@ class Serializer : public ValueSerializer::Delegate {
} else if (transfer->IsUndefined()) {
return Just(true);
} else {
Throw(isolate_, "Transfer list must be an Array or undefined");
isolate_->ThrowError("Transfer list must be an Array or undefined");
return Nothing<bool>();
}
}
@ -4481,7 +4479,7 @@ class Serializer : public ValueSerializer::Delegate {
Local<ArrayBuffer> array_buffer =
Local<ArrayBuffer>::New(isolate_, global_array_buffer);
if (!array_buffer->IsDetachable()) {
Throw(isolate_, "ArrayBuffer could not be transferred");
isolate_->ThrowError("ArrayBuffer could not be transferred");
return Nothing<bool>();
}

View File

@ -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;
}

View File

@ -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 =

View File

@ -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());
}

View File

@ -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 =

View File

@ -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());
}
}

View File

@ -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 =

View File

@ -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(

View File

@ -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(