Prefer single-byte form of CFX_V8::NewString() with literals.

Otherwise, we store 3 extra bytes of zeros per character, which
we then throw away at runtime when we turn it back into a bytestring
before entering V8.

Add small test for equivalence of L"123" vs. "123".

Change-Id: I18d4a3677924372297ea62e36ee96489bad7c264
Reviewed-on: https://pdfium-review.googlesource.com/c/44893
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/fxjs/cfx_v8_unittest.cpp b/fxjs/cfx_v8_unittest.cpp
index f8e0be6..91188c3 100644
--- a/fxjs/cfx_v8_unittest.cpp
+++ b/fxjs/cfx_v8_unittest.cpp
@@ -118,7 +118,7 @@
   v8::HandleScope handle_scope(isolate());
   v8::Context::Scope context_scope(v8::Context::New(isolate()));
 
-  auto str = cfx_v8()->NewString(L"123");
+  auto str = cfx_v8()->NewString("123");
   EXPECT_TRUE(cfx_v8()->ToBoolean(str));
   EXPECT_EQ(123, cfx_v8()->ToInt32(str));
   EXPECT_EQ(123, cfx_v8()->ToDouble(str));
@@ -126,6 +126,15 @@
   EXPECT_EQ(L"123", cfx_v8()->ToWideString(str));
   EXPECT_TRUE(cfx_v8()->ToObject(str).IsEmpty());
   EXPECT_TRUE(cfx_v8()->ToArray(str).IsEmpty());
+
+  auto str2 = cfx_v8()->NewString(L"123");
+  EXPECT_TRUE(cfx_v8()->ToBoolean(str2));
+  EXPECT_EQ(123, cfx_v8()->ToInt32(str2));
+  EXPECT_EQ(123, cfx_v8()->ToDouble(str2));
+  EXPECT_EQ("123", cfx_v8()->ToByteString(str2));
+  EXPECT_EQ(L"123", cfx_v8()->ToWideString(str2));
+  EXPECT_TRUE(cfx_v8()->ToObject(str2).IsEmpty());
+  EXPECT_TRUE(cfx_v8()->ToArray(str2).IsEmpty());
 }
 
 TEST_F(FXV8UnitTest, NewDate) {
diff --git a/fxjs/cjs_color.cpp b/fxjs/cjs_color.cpp
index 0d4065e..e51cf54 100644
--- a/fxjs/cjs_color.cpp
+++ b/fxjs/cjs_color.cpp
@@ -56,23 +56,23 @@
   switch (color.nColorType) {
     case CFX_Color::kTransparent:
       array = pRuntime->NewArray();
-      pRuntime->PutArrayElement(array, 0, pRuntime->NewString(L"T"));
+      pRuntime->PutArrayElement(array, 0, pRuntime->NewString("T"));
       break;
     case CFX_Color::kGray:
       array = pRuntime->NewArray();
-      pRuntime->PutArrayElement(array, 0, pRuntime->NewString(L"G"));
+      pRuntime->PutArrayElement(array, 0, pRuntime->NewString("G"));
       pRuntime->PutArrayElement(array, 1, pRuntime->NewNumber(color.fColor1));
       break;
     case CFX_Color::kRGB:
       array = pRuntime->NewArray();
-      pRuntime->PutArrayElement(array, 0, pRuntime->NewString(L"RGB"));
+      pRuntime->PutArrayElement(array, 0, pRuntime->NewString("RGB"));
       pRuntime->PutArrayElement(array, 1, pRuntime->NewNumber(color.fColor1));
       pRuntime->PutArrayElement(array, 2, pRuntime->NewNumber(color.fColor2));
       pRuntime->PutArrayElement(array, 3, pRuntime->NewNumber(color.fColor3));
       break;
     case CFX_Color::kCMYK:
       array = pRuntime->NewArray();
-      pRuntime->PutArrayElement(array, 0, pRuntime->NewString(L"CMYK"));
+      pRuntime->PutArrayElement(array, 0, pRuntime->NewString("CMYK"));
       pRuntime->PutArrayElement(array, 1, pRuntime->NewNumber(color.fColor1));
       pRuntime->PutArrayElement(array, 2, pRuntime->NewNumber(color.fColor2));
       pRuntime->PutArrayElement(array, 3, pRuntime->NewNumber(color.fColor3));
diff --git a/fxjs/cjs_document.cpp b/fxjs/cjs_document.cpp
index 8f60d13..33d1087 100644
--- a/fxjs/cjs_document.cpp
+++ b/fxjs/cjs_document.cpp
@@ -1002,7 +1002,7 @@
   if (i > 0 && i < wsFilePath.GetLength())
     return CJS_Result::Success(pRuntime->NewString(wsFilePath.c_str() + i));
 
-  return CJS_Result::Success(pRuntime->NewString(L""));
+  return CJS_Result::Success(pRuntime->NewString(""));
 }
 
 CJS_Result CJS_Document::set_document_file_name(CJS_Runtime* pRuntime,
diff --git a/fxjs/cjs_field.cpp b/fxjs/cjs_field.cpp
index 3676717..eb594cf 100644
--- a/fxjs/cjs_field.cpp
+++ b/fxjs/cjs_field.cpp
@@ -665,13 +665,13 @@
 
   switch (pFormControl->GetControlAlignment()) {
     case 0:
-      return CJS_Result::Success(pRuntime->NewString(L"left"));
+      return CJS_Result::Success(pRuntime->NewString("left"));
     case 1:
-      return CJS_Result::Success(pRuntime->NewString(L"center"));
+      return CJS_Result::Success(pRuntime->NewString("center"));
     case 2:
-      return CJS_Result::Success(pRuntime->NewString(L"right"));
+      return CJS_Result::Success(pRuntime->NewString("right"));
   }
-  return CJS_Result::Success(pRuntime->NewString(L""));
+  return CJS_Result::Success(pRuntime->NewString(""));
 }
 
 CJS_Result CJS_Field::set_alignment(CJS_Runtime* pRuntime,
@@ -697,17 +697,17 @@
 
   switch (pWidget->GetBorderStyle()) {
     case BorderStyle::SOLID:
-      return CJS_Result::Success(pRuntime->NewString(L"solid"));
+      return CJS_Result::Success(pRuntime->NewString("solid"));
     case BorderStyle::DASH:
-      return CJS_Result::Success(pRuntime->NewString(L"dashed"));
+      return CJS_Result::Success(pRuntime->NewString("dashed"));
     case BorderStyle::BEVELED:
-      return CJS_Result::Success(pRuntime->NewString(L"beveled"));
+      return CJS_Result::Success(pRuntime->NewString("beveled"));
     case BorderStyle::INSET:
-      return CJS_Result::Success(pRuntime->NewString(L"inset"));
+      return CJS_Result::Success(pRuntime->NewString("inset"));
     case BorderStyle::UNDERLINE:
-      return CJS_Result::Success(pRuntime->NewString(L"underline"));
+      return CJS_Result::Success(pRuntime->NewString("underline"));
   }
-  return CJS_Result::Success(pRuntime->NewString(L""));
+  return CJS_Result::Success(pRuntime->NewString(""));
 }
 
 CJS_Result CJS_Field::set_border_style(CJS_Runtime* pRuntime,
@@ -1374,15 +1374,15 @@
   int eHM = pFormControl->GetHighlightingMode();
   switch (eHM) {
     case CPDF_FormControl::None:
-      return CJS_Result::Success(pRuntime->NewString(L"none"));
+      return CJS_Result::Success(pRuntime->NewString("none"));
     case CPDF_FormControl::Push:
-      return CJS_Result::Success(pRuntime->NewString(L"push"));
+      return CJS_Result::Success(pRuntime->NewString("push"));
     case CPDF_FormControl::Invert:
-      return CJS_Result::Success(pRuntime->NewString(L"invert"));
+      return CJS_Result::Success(pRuntime->NewString("invert"));
     case CPDF_FormControl::Outline:
-      return CJS_Result::Success(pRuntime->NewString(L"outline"));
+      return CJS_Result::Success(pRuntime->NewString("outline"));
     case CPDF_FormControl::Toggle:
-      return CJS_Result::Success(pRuntime->NewString(L"toggle"));
+      return CJS_Result::Success(pRuntime->NewString("toggle"));
   }
   return CJS_Result::Success();
 }
@@ -2024,23 +2024,23 @@
 
   switch (pFormField->GetFieldType()) {
     case FormFieldType::kUnknown:
-      return CJS_Result::Success(pRuntime->NewString(L"unknown"));
+      return CJS_Result::Success(pRuntime->NewString("unknown"));
     case FormFieldType::kPushButton:
-      return CJS_Result::Success(pRuntime->NewString(L"button"));
+      return CJS_Result::Success(pRuntime->NewString("button"));
     case FormFieldType::kCheckBox:
-      return CJS_Result::Success(pRuntime->NewString(L"checkbox"));
+      return CJS_Result::Success(pRuntime->NewString("checkbox"));
     case FormFieldType::kRadioButton:
-      return CJS_Result::Success(pRuntime->NewString(L"radiobutton"));
+      return CJS_Result::Success(pRuntime->NewString("radiobutton"));
     case FormFieldType::kComboBox:
-      return CJS_Result::Success(pRuntime->NewString(L"combobox"));
+      return CJS_Result::Success(pRuntime->NewString("combobox"));
     case FormFieldType::kListBox:
-      return CJS_Result::Success(pRuntime->NewString(L"listbox"));
+      return CJS_Result::Success(pRuntime->NewString("listbox"));
     case FormFieldType::kTextField:
-      return CJS_Result::Success(pRuntime->NewString(L"text"));
+      return CJS_Result::Success(pRuntime->NewString("text"));
     case FormFieldType::kSignature:
-      return CJS_Result::Success(pRuntime->NewString(L"signature"));
+      return CJS_Result::Success(pRuntime->NewString("signature"));
     default:
-      return CJS_Result::Success(pRuntime->NewString(L"unknown"));
+      return CJS_Result::Success(pRuntime->NewString("unknown"));
   }
 }
 
@@ -2113,7 +2113,7 @@
         }
       }
       if (!bFind)
-        ret = pRuntime->NewString(L"Off");
+        ret = pRuntime->NewString("Off");
 
       break;
     }
@@ -2171,12 +2171,12 @@
             pFormField->GetControl(i)->GetExportValue().AsStringView()));
       }
     }
-    return CJS_Result::Success(pRuntime->NewString(L"Off"));
+    return CJS_Result::Success(pRuntime->NewString("Off"));
   }
 
   if (pFormField->GetFieldType() == FormFieldType::kListBox &&
       (pFormField->CountSelectedItems() > 1)) {
-    return CJS_Result::Success(pRuntime->NewString(L""));
+    return CJS_Result::Success(pRuntime->NewString(""));
   }
   return CJS_Result::Success(
       pRuntime->NewString(pFormField->GetValue().AsStringView()));
diff --git a/fxjs/cjs_publicmethods.cpp b/fxjs/cjs_publicmethods.cpp
index e412c70..38dac89 100644
--- a/fxjs/cjs_publicmethods.cpp
+++ b/fxjs/cjs_publicmethods.cpp
@@ -965,7 +965,7 @@
     if (iNegStyle == 1 || iNegStyle == 3) {
       if (CJS_Field* fTarget = pEvent->Target_Field()) {
         v8::Local<v8::Array> arColor = pRuntime->NewArray();
-        pRuntime->PutArrayElement(arColor, 0, pRuntime->NewString(L"RGB"));
+        pRuntime->PutArrayElement(arColor, 0, pRuntime->NewString("RGB"));
         pRuntime->PutArrayElement(arColor, 1, pRuntime->NewNumber(1));
         pRuntime->PutArrayElement(arColor, 2, pRuntime->NewNumber(0));
         pRuntime->PutArrayElement(arColor, 3, pRuntime->NewNumber(0));
@@ -976,7 +976,7 @@
     if (iNegStyle == 1 || iNegStyle == 3) {
       if (CJS_Field* fTarget = pEvent->Target_Field()) {
         v8::Local<v8::Array> arColor = pRuntime->NewArray();
-        pRuntime->PutArrayElement(arColor, 0, pRuntime->NewString(L"RGB"));
+        pRuntime->PutArrayElement(arColor, 0, pRuntime->NewString("RGB"));
         pRuntime->PutArrayElement(arColor, 1, pRuntime->NewNumber(0));
         pRuntime->PutArrayElement(arColor, 2, pRuntime->NewNumber(0));
         pRuntime->PutArrayElement(arColor, 3, pRuntime->NewNumber(0));
diff --git a/fxjs/cjs_publicmethods_embeddertest.cpp b/fxjs/cjs_publicmethods_embeddertest.cpp
index 93c526a..56f04aa 100644
--- a/fxjs/cjs_publicmethods_embeddertest.cpp
+++ b/fxjs/cjs_publicmethods_embeddertest.cpp
@@ -193,9 +193,8 @@
   runtime.GetCurrentEventContext()->GetEventHandler()->m_pValue = &result;
 
   auto ary = runtime.NewArray();
-
-  runtime.PutArrayElement(ary, 0, runtime.NewString(L"Calc1_A"));
-  runtime.PutArrayElement(ary, 1, runtime.NewString(L"Calc1_B"));
+  runtime.PutArrayElement(ary, 0, runtime.NewString("Calc1_A"));
+  runtime.PutArrayElement(ary, 1, runtime.NewString("Calc1_B"));
 
   std::vector<v8::Local<v8::Value>> params;
   params.push_back(runtime.NewString("SUM"));
@@ -239,8 +238,8 @@
   handler->SetSelEnd(0);
 
   std::vector<v8::Local<v8::Value>> params;
-  params.push_back(runtime.NewString(L"-10"));
-  params.push_back(runtime.NewString(L""));
+  params.push_back(runtime.NewString("-10"));
+  params.push_back(runtime.NewString(""));
 
   CJS_Result ret = CJS_PublicMethods::AFNumber_Keystroke(&runtime, params);
   EXPECT_TRUE(valid);