[fm2js] Fixup initialized variable conversion

When converting a Var statement from formcalc to JS we would neglect to
add the ; if the variable was initialized. This generated invalid JS as
we'd end up with two statements smushed together.

FormCalc:
  var s = ""

Previous JS:
  var s = ""s = pfm....

New JS:
  var s = "";
  s = pfm ...

Bug: pdfium:1097
Change-Id: I8a869f07374cac68a06a487dace89699a4e5540c
Reviewed-on: https://pdfium-review.googlesource.com/34110
Commit-Queue: dsinclair <dsinclair@chromium.org>
Reviewed-by: Ryan Harrison <rharrison@chromium.org>
Reviewed-by: Henrique Nakashima <hnakashima@chromium.org>
diff --git a/BUILD.gn b/BUILD.gn
index d9db5a7..d88c1d6 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -2945,6 +2945,7 @@
       "xfa/fwl/cfx_barcode_unittest.cpp",
       "xfa/fxfa/cxfa_ffbarcode_unittest.cpp",
       "xfa/fxfa/cxfa_textparser_unittest.cpp",
+      "xfa/fxfa/fm2js/cxfa_fmexpression_unittest.cpp",
       "xfa/fxfa/fm2js/cxfa_fmlexer_unittest.cpp",
       "xfa/fxfa/fm2js/cxfa_fmparser_unittest.cpp",
       "xfa/fxfa/fm2js/cxfa_fmsimpleexpression_unittest.cpp",
diff --git a/xfa/fxfa/fm2js/cxfa_fmexpression.cpp b/xfa/fxfa/fm2js/cxfa_fmexpression.cpp
index 9ff043a..244030e 100644
--- a/xfa/fxfa/fm2js/cxfa_fmexpression.cpp
+++ b/xfa/fxfa/fm2js/cxfa_fmexpression.cpp
@@ -131,6 +131,7 @@
     if (!m_pInit->ToJavaScript(js, ReturnType::kInfered))
       return false;
 
+    *js << L";\n";
     *js << tempName << L" = pfm_rt.var_filter(" << tempName << L");\n";
   } else {
     *js << L"\"\";\n";
diff --git a/xfa/fxfa/fm2js/cxfa_fmexpression_unittest.cpp b/xfa/fxfa/fm2js/cxfa_fmexpression_unittest.cpp
new file mode 100644
index 0000000..defe563
--- /dev/null
+++ b/xfa/fxfa/fm2js/cxfa_fmexpression_unittest.cpp
@@ -0,0 +1,48 @@
+// Copyright 2018 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "xfa/fxfa/fm2js/cxfa_fmexpression.h"
+
+#include <memory>
+#include <utility>
+
+#include "core/fxcrt/cfx_widetextbuf.h"
+#include "core/fxcrt/fx_string.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "testing/test_support.h"
+#include "third_party/base/ptr_util.h"
+#include "xfa/fxfa/fm2js/cxfa_fmtojavascriptdepth.h"
+
+TEST(CXFA_FMExpressionTest, VarExpressionInitBlank) {
+  CXFA_FMToJavaScriptDepth::Reset();
+  CFX_WideTextBuf accumulator;
+
+  auto init = pdfium::MakeUnique<CXFA_FMStringExpression>(L"\"\"");
+  CXFA_FMVarExpression(L"s", std::move(init))
+      .ToJavaScript(&accumulator, ReturnType::kInfered);
+  EXPECT_STREQ(L"var s = \"\";\ns = pfm_rt.var_filter(s);\n",
+               accumulator.MakeString().c_str());
+}
+
+TEST(CXFA_FMExpressionTest, VarExpressionInitString) {
+  CXFA_FMToJavaScriptDepth::Reset();
+  CFX_WideTextBuf accumulator;
+
+  auto init = pdfium::MakeUnique<CXFA_FMStringExpression>(L"\"foo\"");
+  CXFA_FMVarExpression(L"s", std::move(init))
+      .ToJavaScript(&accumulator, ReturnType::kInfered);
+  EXPECT_STREQ(L"var s = \"foo\";\ns = pfm_rt.var_filter(s);\n",
+               accumulator.MakeString().c_str());
+}
+
+TEST(CXFA_FMExpressionTest, VarExpressionInitNumeric) {
+  CXFA_FMToJavaScriptDepth::Reset();
+  CFX_WideTextBuf accumulator;
+
+  auto init = pdfium::MakeUnique<CXFA_FMNumberExpression>(L"112");
+  CXFA_FMVarExpression(L"s", std::move(init))
+      .ToJavaScript(&accumulator, ReturnType::kInfered);
+  EXPECT_STREQ(L"var s = 112;\ns = pfm_rt.var_filter(s);\n",
+               accumulator.MakeString().c_str());
+}
diff --git a/xfa/fxfa/fm2js/cxfa_fmparser_unittest.cpp b/xfa/fxfa/fm2js/cxfa_fmparser_unittest.cpp
index 9a9db21..5afacc0 100644
--- a/xfa/fxfa/fm2js/cxfa_fmparser_unittest.cpp
+++ b/xfa/fxfa/fm2js/cxfa_fmparser_unittest.cpp
@@ -371,3 +371,34 @@
   EXPECT_TRUE(ast->ToJavaScript(&buf));
   EXPECT_EQ(ret, buf.AsStringView());
 }
+
+TEST(CXFA_FMParserTest, ParseVar) {
+  const wchar_t input[] = L"var s = \"\"\n";
+  const wchar_t ret[] =
+      L"(function() {\n"
+      L"let pfm_method_runner = function(obj, cb) {\n"
+      L"  if (pfm_rt.is_ary(obj)) {\n"
+      L"    let pfm_method_return = null;\n"
+      L"    for (var idx = obj.length -1; idx > 1; idx--) {\n"
+      L"      pfm_method_return = cb(obj[idx]);\n"
+      L"    }\n"
+      L"    return pfm_method_return;\n"
+      L"  }\n"
+      L"  return cb(obj);\n"
+      L"};\n"
+      L"var pfm_ret = null;\n"
+      L"var s = \"\";\n"
+      L"s = pfm_rt.var_filter(s);\n"
+      L"pfm_ret = s;\n"
+      L"return pfm_rt.get_val(pfm_ret);\n"
+      L"}).call(this);";
+
+  auto parser = pdfium::MakeUnique<CXFA_FMParser>(input);
+  std::unique_ptr<CXFA_FMAST> ast = parser->Parse();
+  EXPECT_FALSE(parser->HasError());
+
+  CXFA_FMToJavaScriptDepth::Reset();
+  CFX_WideTextBuf buf;
+  EXPECT_TRUE(ast->ToJavaScript(&buf));
+  EXPECT_STREQ(ret, buf.MakeString().c_str());
+}