Skip to content

Commit 4f39499

Browse files
committed
src: don't create js string twice on error
Rewrite ErrnoException() so that it doesn't turn the file path into a string twice. PR-URL: #1148 Reviewed-By: Trevor Norris <trev.norris@gmail.com>
1 parent eb995d6 commit 4f39499

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

src/node.cc

+15-14
Original file line numberDiff line numberDiff line change
@@ -724,28 +724,29 @@ Local<Value> ErrnoException(Isolate* isolate,
724724
}
725725
Local<String> message = OneByteString(env->isolate(), msg);
726726

727-
Local<String> cons1 =
727+
Local<String> cons =
728728
String::Concat(estring, FIXED_ONE_BYTE_STRING(env->isolate(), ", "));
729-
Local<String> cons2 = String::Concat(cons1, message);
729+
cons = String::Concat(cons, message);
730730

731-
if (path) {
732-
Local<String> cons3 =
733-
String::Concat(cons2, FIXED_ONE_BYTE_STRING(env->isolate(), " '"));
734-
Local<String> cons4 =
735-
String::Concat(cons3, String::NewFromUtf8(env->isolate(), path));
736-
Local<String> cons5 =
737-
String::Concat(cons4, FIXED_ONE_BYTE_STRING(env->isolate(), "'"));
738-
e = Exception::Error(cons5);
739-
} else {
740-
e = Exception::Error(cons2);
731+
Local<String> path_string;
732+
if (path != nullptr) {
733+
// FIXME(bnoordhuis) It's questionable to interpret the file path as UTF-8.
734+
path_string = String::NewFromUtf8(env->isolate(), path);
741735
}
742736

737+
if (path_string.IsEmpty() == false) {
738+
cons = String::Concat(cons, FIXED_ONE_BYTE_STRING(env->isolate(), " '"));
739+
cons = String::Concat(cons, path_string);
740+
cons = String::Concat(cons, FIXED_ONE_BYTE_STRING(env->isolate(), "'"));
741+
}
742+
e = Exception::Error(cons);
743+
743744
Local<Object> obj = e->ToObject(env->isolate());
744745
obj->Set(env->errno_string(), Integer::New(env->isolate(), errorno));
745746
obj->Set(env->code_string(), estring);
746747

747-
if (path != nullptr) {
748-
obj->Set(env->path_string(), String::NewFromUtf8(env->isolate(), path));
748+
if (path_string.IsEmpty() == false) {
749+
obj->Set(env->path_string(), path_string);
749750
}
750751

751752
if (syscall != nullptr) {

0 commit comments

Comments
 (0)