summaryrefslogtreecommitdiff
path: root/xfa/fxfa/fm2js/cxfa_fmsimpleexpression.cpp
diff options
context:
space:
mode:
authordan sinclair <dsinclair@chromium.org>2018-02-19 18:13:23 +0000
committerChromium commit bot <commit-bot@chromium.org>2018-02-19 18:13:23 +0000
commitd276a52cd5061e0d4ee4aa79b8e89bbeb0a89da3 (patch)
tree8286e2c53070ad6ed4f5e7ca00b0dd0039c6c92a /xfa/fxfa/fm2js/cxfa_fmsimpleexpression.cpp
parent636259244a298b09aab0195258e76b51784766ff (diff)
downloadpdfium-d276a52cd5061e0d4ee4aa79b8e89bbeb0a89da3.tar.xz
Cleanup JS generation in formcalc.
This CL cleans up some of the JS generation code to make it easier to see what JavaScript is begin created. Change-Id: I4265226cca0e4de400d05cc0fea3f8c76608115e Reviewed-on: https://pdfium-review.googlesource.com/27250 Reviewed-by: Ryan Harrison <rharrison@chromium.org> Commit-Queue: dsinclair <dsinclair@chromium.org>
Diffstat (limited to 'xfa/fxfa/fm2js/cxfa_fmsimpleexpression.cpp')
-rw-r--r--xfa/fxfa/fm2js/cxfa_fmsimpleexpression.cpp627
1 files changed, 310 insertions, 317 deletions
diff --git a/xfa/fxfa/fm2js/cxfa_fmsimpleexpression.cpp b/xfa/fxfa/fm2js/cxfa_fmsimpleexpression.cpp
index 76f780f962..8354072b4d 100644
--- a/xfa/fxfa/fm2js/cxfa_fmsimpleexpression.cpp
+++ b/xfa/fxfa/fm2js/cxfa_fmsimpleexpression.cpp
@@ -74,10 +74,10 @@ const XFA_FMSOMMethod gs_FMSomMethods[] = {
CXFA_FMSimpleExpression::CXFA_FMSimpleExpression(uint32_t line, XFA_FM_TOKEN op)
: m_line(line), m_op(op) {}
-bool CXFA_FMSimpleExpression::ToJavaScript(CFX_WideTextBuf& javascript,
+bool CXFA_FMSimpleExpression::ToJavaScript(CFX_WideTextBuf& js,
ReturnType type) {
CXFA_FMToJavaScriptDepth depthManager;
- return !CXFA_IsTooBig(javascript) && depthManager.IsWithinMaxDepth();
+ return !CXFA_IsTooBig(js) && depthManager.IsWithinMaxDepth();
}
XFA_FM_TOKEN CXFA_FMSimpleExpression::GetOperatorToken() const {
return m_op;
@@ -86,14 +86,13 @@ XFA_FM_TOKEN CXFA_FMSimpleExpression::GetOperatorToken() const {
CXFA_FMNullExpression::CXFA_FMNullExpression(uint32_t line)
: CXFA_FMSimpleExpression(line, TOKnull) {}
-bool CXFA_FMNullExpression::ToJavaScript(CFX_WideTextBuf& javascript,
- ReturnType type) {
+bool CXFA_FMNullExpression::ToJavaScript(CFX_WideTextBuf& js, ReturnType type) {
CXFA_FMToJavaScriptDepth depthManager;
- if (CXFA_IsTooBig(javascript) || !depthManager.IsWithinMaxDepth())
+ if (CXFA_IsTooBig(js) || !depthManager.IsWithinMaxDepth())
return false;
- javascript << L"null";
- return !CXFA_IsTooBig(javascript);
+ js << L"null";
+ return !CXFA_IsTooBig(js);
}
CXFA_FMNumberExpression::CXFA_FMNumberExpression(uint32_t line,
@@ -102,14 +101,14 @@ CXFA_FMNumberExpression::CXFA_FMNumberExpression(uint32_t line,
CXFA_FMNumberExpression::~CXFA_FMNumberExpression() {}
-bool CXFA_FMNumberExpression::ToJavaScript(CFX_WideTextBuf& javascript,
+bool CXFA_FMNumberExpression::ToJavaScript(CFX_WideTextBuf& js,
ReturnType type) {
CXFA_FMToJavaScriptDepth depthManager;
- if (CXFA_IsTooBig(javascript) || !depthManager.IsWithinMaxDepth())
+ if (CXFA_IsTooBig(js) || !depthManager.IsWithinMaxDepth())
return false;
- javascript << m_wsNumber;
- return !CXFA_IsTooBig(javascript);
+ js << m_wsNumber;
+ return !CXFA_IsTooBig(js);
}
CXFA_FMStringExpression::CXFA_FMStringExpression(uint32_t line,
@@ -118,37 +117,38 @@ CXFA_FMStringExpression::CXFA_FMStringExpression(uint32_t line,
CXFA_FMStringExpression::~CXFA_FMStringExpression() {}
-bool CXFA_FMStringExpression::ToJavaScript(CFX_WideTextBuf& javascript,
+bool CXFA_FMStringExpression::ToJavaScript(CFX_WideTextBuf& js,
ReturnType type) {
CXFA_FMToJavaScriptDepth depthManager;
- if (CXFA_IsTooBig(javascript) || !depthManager.IsWithinMaxDepth())
+ if (CXFA_IsTooBig(js) || !depthManager.IsWithinMaxDepth())
return false;
WideString tempStr(m_wsString);
if (tempStr.GetLength() <= 2) {
- javascript << tempStr;
- return !CXFA_IsTooBig(javascript);
+ js << tempStr;
+ return !CXFA_IsTooBig(js);
}
- javascript.AppendChar(L'\"');
+
+ js << L"\"";
for (size_t i = 1; i < tempStr.GetLength() - 1; i++) {
wchar_t oneChar = tempStr[i];
switch (oneChar) {
case L'\"':
- i++;
- javascript << L"\\\"";
+ ++i;
+ js << L"\\\"";
break;
case 0x0d:
break;
case 0x0a:
- javascript << L"\\n";
+ js << L"\\n";
break;
default:
- javascript.AppendChar(oneChar);
+ js.AppendChar(oneChar);
break;
}
}
- javascript.AppendChar(L'\"');
- return !CXFA_IsTooBig(javascript);
+ js << L"\"";
+ return !CXFA_IsTooBig(js);
}
CXFA_FMIdentifierExpression::CXFA_FMIdentifierExpression(
@@ -159,34 +159,34 @@ CXFA_FMIdentifierExpression::CXFA_FMIdentifierExpression(
CXFA_FMIdentifierExpression::~CXFA_FMIdentifierExpression() {}
-bool CXFA_FMIdentifierExpression::ToJavaScript(CFX_WideTextBuf& javascript,
+bool CXFA_FMIdentifierExpression::ToJavaScript(CFX_WideTextBuf& js,
ReturnType type) {
CXFA_FMToJavaScriptDepth depthManager;
- if (CXFA_IsTooBig(javascript) || !depthManager.IsWithinMaxDepth())
- return false;
-
- WideString tempStr(m_wsIdentifier);
- if (tempStr == L"$") {
- tempStr = L"this";
- } else if (tempStr == L"!") {
- tempStr = L"xfa.datasets";
- } else if (tempStr == L"$data") {
- tempStr = L"xfa.datasets.data";
- } else if (tempStr == L"$event") {
- tempStr = L"xfa.event";
- } else if (tempStr == L"$form") {
- tempStr = L"xfa.form";
- } else if (tempStr == L"$host") {
- tempStr = L"xfa.host";
- } else if (tempStr == L"$layout") {
- tempStr = L"xfa.layout";
- } else if (tempStr == L"$template") {
- tempStr = L"xfa.template";
- } else if (tempStr[0] == L'!') {
- tempStr = L"pfm__excl__" + tempStr.Right(tempStr.GetLength() - 1);
- }
- javascript << tempStr;
- return !CXFA_IsTooBig(javascript);
+ if (CXFA_IsTooBig(js) || !depthManager.IsWithinMaxDepth())
+ return false;
+
+ if (m_wsIdentifier == L"$")
+ js << L"this";
+ else if (m_wsIdentifier == L"!")
+ js << L"xfa.datasets";
+ else if (m_wsIdentifier == L"$data")
+ js << L"xfa.datasets.data";
+ else if (m_wsIdentifier == L"$event")
+ js << L"xfa.event";
+ else if (m_wsIdentifier == L"$form")
+ js << L"xfa.form";
+ else if (m_wsIdentifier == L"$host")
+ js << L"xfa.host";
+ else if (m_wsIdentifier == L"$layout")
+ js << L"xfa.layout";
+ else if (m_wsIdentifier == L"$template")
+ js << L"xfa.template";
+ else if (m_wsIdentifier[0] == L'!')
+ js << L"pfm__excl__" + m_wsIdentifier.Right(m_wsIdentifier.GetLength() - 1);
+ else
+ js << m_wsIdentifier;
+
+ return !CXFA_IsTooBig(js);
}
CXFA_FMUnaryExpression::CXFA_FMUnaryExpression(
@@ -197,10 +197,10 @@ CXFA_FMUnaryExpression::CXFA_FMUnaryExpression(
CXFA_FMUnaryExpression::~CXFA_FMUnaryExpression() {}
-bool CXFA_FMUnaryExpression::ToJavaScript(CFX_WideTextBuf& javascript,
+bool CXFA_FMUnaryExpression::ToJavaScript(CFX_WideTextBuf& js,
ReturnType type) {
CXFA_FMToJavaScriptDepth depthManager;
- return !CXFA_IsTooBig(javascript) && depthManager.IsWithinMaxDepth();
+ return !CXFA_IsTooBig(js) && depthManager.IsWithinMaxDepth();
}
CXFA_FMBinExpression::CXFA_FMBinExpression(
@@ -214,10 +214,9 @@ CXFA_FMBinExpression::CXFA_FMBinExpression(
CXFA_FMBinExpression::~CXFA_FMBinExpression() {}
-bool CXFA_FMBinExpression::ToJavaScript(CFX_WideTextBuf& javascript,
- ReturnType type) {
+bool CXFA_FMBinExpression::ToJavaScript(CFX_WideTextBuf& js, ReturnType type) {
CXFA_FMToJavaScriptDepth depthManager;
- return !CXFA_IsTooBig(javascript) && depthManager.IsWithinMaxDepth();
+ return !CXFA_IsTooBig(js) && depthManager.IsWithinMaxDepth();
}
CXFA_FMAssignExpression::CXFA_FMAssignExpression(
@@ -227,45 +226,37 @@ CXFA_FMAssignExpression::CXFA_FMAssignExpression(
std::unique_ptr<CXFA_FMSimpleExpression> pExp2)
: CXFA_FMBinExpression(line, op, std::move(pExp1), std::move(pExp2)) {}
-bool CXFA_FMAssignExpression::ToJavaScript(CFX_WideTextBuf& javascript,
+bool CXFA_FMAssignExpression::ToJavaScript(CFX_WideTextBuf& js,
ReturnType type) {
CXFA_FMToJavaScriptDepth depthManager;
- if (CXFA_IsTooBig(javascript) || !depthManager.IsWithinMaxDepth())
+ if (CXFA_IsTooBig(js) || !depthManager.IsWithinMaxDepth())
return false;
- javascript << L"if (pfm_rt.is_obj(";
CFX_WideTextBuf tempExp1;
if (!m_pExp1->ToJavaScript(tempExp1, ReturnType::kInfered))
return false;
- javascript << tempExp1;
- javascript << L"))\n{\n";
+ js << L"if (pfm_rt.is_obj(" << tempExp1 << L"))\n{\n";
if (type == ReturnType::kImplied)
- javascript << L"pfm_ret = ";
-
- javascript << L"pfm_rt.asgn_val_op(";
- javascript << tempExp1;
- javascript << L", ";
+ js << L"pfm_ret = ";
CFX_WideTextBuf tempExp2;
if (!m_pExp2->ToJavaScript(tempExp2, ReturnType::kInfered))
return false;
- javascript << tempExp2;
- javascript << L");\n}\n";
+
+ js << L"pfm_rt.asgn_val_op(" << tempExp1 << L", " << tempExp2 << L");\n}\n";
+
if (m_pExp1->GetOperatorToken() == TOKidentifier &&
tempExp1.AsStringView() != L"this") {
- javascript << L"else\n{\n";
+ js << L"else\n{\n";
if (type == ReturnType::kImplied)
- javascript << L"pfm_ret = ";
-
- javascript << tempExp1;
- javascript << L" = pfm_rt.asgn_val_op(";
- javascript << tempExp1;
- javascript << L", ";
- javascript << tempExp2;
- javascript << L");\n}\n";
+ js << L"pfm_ret = ";
+
+ js << tempExp1 << L" = pfm_rt.asgn_val_op";
+ js << L"(" << tempExp1 << L", " << tempExp2 << L");\n";
+ js << L"}\n";
}
- return !CXFA_IsTooBig(javascript);
+ return !CXFA_IsTooBig(js);
}
CXFA_FMLogicalOrExpression::CXFA_FMLogicalOrExpression(
@@ -275,20 +266,22 @@ CXFA_FMLogicalOrExpression::CXFA_FMLogicalOrExpression(
std::unique_ptr<CXFA_FMSimpleExpression> pExp2)
: CXFA_FMBinExpression(line, op, std::move(pExp1), std::move(pExp2)) {}
-bool CXFA_FMLogicalOrExpression::ToJavaScript(CFX_WideTextBuf& javascript,
+bool CXFA_FMLogicalOrExpression::ToJavaScript(CFX_WideTextBuf& js,
ReturnType type) {
CXFA_FMToJavaScriptDepth depthManager;
- if (CXFA_IsTooBig(javascript) || !depthManager.IsWithinMaxDepth())
+ if (CXFA_IsTooBig(js) || !depthManager.IsWithinMaxDepth())
return false;
- javascript << L"pfm_rt.log_or_op(";
- if (!m_pExp1->ToJavaScript(javascript, ReturnType::kInfered))
+ CFX_WideTextBuf exp1_txt;
+ if (!m_pExp1->ToJavaScript(exp1_txt, ReturnType::kInfered))
return false;
- javascript << L", ";
- if (!m_pExp2->ToJavaScript(javascript, ReturnType::kInfered))
+
+ CFX_WideTextBuf exp2_txt;
+ if (!m_pExp2->ToJavaScript(exp2_txt, ReturnType::kInfered))
return false;
- javascript << L")";
- return !CXFA_IsTooBig(javascript);
+
+ js << L"pfm_rt.log_or_op(" << exp1_txt << L", " << exp2_txt << L")";
+ return !CXFA_IsTooBig(js);
}
CXFA_FMLogicalAndExpression::CXFA_FMLogicalAndExpression(
@@ -298,20 +291,22 @@ CXFA_FMLogicalAndExpression::CXFA_FMLogicalAndExpression(
std::unique_ptr<CXFA_FMSimpleExpression> pExp2)
: CXFA_FMBinExpression(line, op, std::move(pExp1), std::move(pExp2)) {}
-bool CXFA_FMLogicalAndExpression::ToJavaScript(CFX_WideTextBuf& javascript,
+bool CXFA_FMLogicalAndExpression::ToJavaScript(CFX_WideTextBuf& js,
ReturnType type) {
CXFA_FMToJavaScriptDepth depthManager;
- if (CXFA_IsTooBig(javascript) || !depthManager.IsWithinMaxDepth())
+ if (CXFA_IsTooBig(js) || !depthManager.IsWithinMaxDepth())
return false;
- javascript << L"pfm_rt.log_and_op(";
- if (!m_pExp1->ToJavaScript(javascript, ReturnType::kInfered))
+ CFX_WideTextBuf exp1_txt;
+ if (!m_pExp1->ToJavaScript(exp1_txt, ReturnType::kInfered))
return false;
- javascript << L", ";
- if (!m_pExp2->ToJavaScript(javascript, ReturnType::kInfered))
+
+ CFX_WideTextBuf exp2_txt;
+ if (!m_pExp2->ToJavaScript(exp2_txt, ReturnType::kInfered))
return false;
- javascript << L")";
- return !CXFA_IsTooBig(javascript);
+
+ js << L"pfm_rt.log_and_op(" << exp1_txt << L", " << exp2_txt << L")";
+ return !CXFA_IsTooBig(js);
}
CXFA_FMEqualityExpression::CXFA_FMEqualityExpression(
@@ -321,33 +316,36 @@ CXFA_FMEqualityExpression::CXFA_FMEqualityExpression(
std::unique_ptr<CXFA_FMSimpleExpression> pExp2)
: CXFA_FMBinExpression(line, op, std::move(pExp1), std::move(pExp2)) {}
-bool CXFA_FMEqualityExpression::ToJavaScript(CFX_WideTextBuf& javascript,
+bool CXFA_FMEqualityExpression::ToJavaScript(CFX_WideTextBuf& js,
ReturnType type) {
CXFA_FMToJavaScriptDepth depthManager;
- if (CXFA_IsTooBig(javascript) || !depthManager.IsWithinMaxDepth())
+ if (CXFA_IsTooBig(js) || !depthManager.IsWithinMaxDepth())
return false;
switch (m_op) {
case TOKeq:
case TOKkseq:
- javascript << L"pfm_rt.eq_op";
+ js << L"pfm_rt.eq_op";
break;
case TOKne:
case TOKksne:
- javascript << L"pfm_rt.neq_op";
+ js << L"pfm_rt.neq_op";
break;
default:
NOTREACHED();
break;
}
- javascript << L"(";
- if (!m_pExp1->ToJavaScript(javascript, ReturnType::kInfered))
+
+ CFX_WideTextBuf exp1_txt;
+ if (!m_pExp1->ToJavaScript(exp1_txt, ReturnType::kInfered))
return false;
- javascript << L", ";
- if (!m_pExp2->ToJavaScript(javascript, ReturnType::kInfered))
+
+ CFX_WideTextBuf exp2_txt;
+ if (!m_pExp2->ToJavaScript(exp2_txt, ReturnType::kInfered))
return false;
- javascript << L")";
- return !CXFA_IsTooBig(javascript);
+
+ js << L"(" << exp1_txt << L", " << exp2_txt << L")";
+ return !CXFA_IsTooBig(js);
}
CXFA_FMRelationalExpression::CXFA_FMRelationalExpression(
@@ -357,41 +355,44 @@ CXFA_FMRelationalExpression::CXFA_FMRelationalExpression(
std::unique_ptr<CXFA_FMSimpleExpression> pExp2)
: CXFA_FMBinExpression(line, op, std::move(pExp1), std::move(pExp2)) {}
-bool CXFA_FMRelationalExpression::ToJavaScript(CFX_WideTextBuf& javascript,
+bool CXFA_FMRelationalExpression::ToJavaScript(CFX_WideTextBuf& js,
ReturnType type) {
CXFA_FMToJavaScriptDepth depthManager;
- if (CXFA_IsTooBig(javascript) || !depthManager.IsWithinMaxDepth())
+ if (CXFA_IsTooBig(js) || !depthManager.IsWithinMaxDepth())
return false;
switch (m_op) {
case TOKlt:
case TOKkslt:
- javascript << L"pfm_rt.lt_op";
+ js << L"pfm_rt.lt_op";
break;
case TOKgt:
case TOKksgt:
- javascript << L"pfm_rt.gt_op";
+ js << L"pfm_rt.gt_op";
break;
case TOKle:
case TOKksle:
- javascript << L"pfm_rt.le_op";
+ js << L"pfm_rt.le_op";
break;
case TOKge:
case TOKksge:
- javascript << L"pfm_rt.ge_op";
+ js << L"pfm_rt.ge_op";
break;
default:
NOTREACHED();
break;
}
- javascript << L"(";
- if (!m_pExp1->ToJavaScript(javascript, ReturnType::kInfered))
+
+ CFX_WideTextBuf exp1_txt;
+ if (!m_pExp1->ToJavaScript(exp1_txt, ReturnType::kInfered))
return false;
- javascript << L", ";
- if (!m_pExp2->ToJavaScript(javascript, ReturnType::kInfered))
+
+ CFX_WideTextBuf exp2_txt;
+ if (!m_pExp2->ToJavaScript(exp2_txt, ReturnType::kInfered))
return false;
- javascript << L")";
- return !CXFA_IsTooBig(javascript);
+
+ js << L"(" << exp1_txt << L", " << exp2_txt << L")";
+ return !CXFA_IsTooBig(js);
}
CXFA_FMAdditiveExpression::CXFA_FMAdditiveExpression(
@@ -401,31 +402,34 @@ CXFA_FMAdditiveExpression::CXFA_FMAdditiveExpression(
std::unique_ptr<CXFA_FMSimpleExpression> pExp2)
: CXFA_FMBinExpression(line, op, std::move(pExp1), std::move(pExp2)) {}
-bool CXFA_FMAdditiveExpression::ToJavaScript(CFX_WideTextBuf& javascript,
+bool CXFA_FMAdditiveExpression::ToJavaScript(CFX_WideTextBuf& js,
ReturnType type) {
CXFA_FMToJavaScriptDepth depthManager;
- if (CXFA_IsTooBig(javascript) || !depthManager.IsWithinMaxDepth())
+ if (CXFA_IsTooBig(js) || !depthManager.IsWithinMaxDepth())
return false;
switch (m_op) {
case TOKplus:
- javascript << L"pfm_rt.plus_op";
+ js << L"pfm_rt.plus_op";
break;
case TOKminus:
- javascript << L"pfm_rt.minus_op";
+ js << L"pfm_rt.minus_op";
break;
default:
NOTREACHED();
break;
}
- javascript << L"(";
- if (!m_pExp1->ToJavaScript(javascript, ReturnType::kInfered))
+
+ CFX_WideTextBuf exp1_txt;
+ if (!m_pExp1->ToJavaScript(exp1_txt, ReturnType::kInfered))
return false;
- javascript << L", ";
- if (!m_pExp2->ToJavaScript(javascript, ReturnType::kInfered))
+
+ CFX_WideTextBuf exp2_txt;
+ if (!m_pExp2->ToJavaScript(exp2_txt, ReturnType::kInfered))
return false;
- javascript << L")";
- return !CXFA_IsTooBig(javascript);
+
+ js << L"(" << exp1_txt << L", " << exp2_txt << L")";
+ return !CXFA_IsTooBig(js);
}
CXFA_FMMultiplicativeExpression::CXFA_FMMultiplicativeExpression(
@@ -435,31 +439,34 @@ CXFA_FMMultiplicativeExpression::CXFA_FMMultiplicativeExpression(
std::unique_ptr<CXFA_FMSimpleExpression> pExp2)
: CXFA_FMBinExpression(line, op, std::move(pExp1), std::move(pExp2)) {}
-bool CXFA_FMMultiplicativeExpression::ToJavaScript(CFX_WideTextBuf& javascript,
+bool CXFA_FMMultiplicativeExpression::ToJavaScript(CFX_WideTextBuf& js,
ReturnType type) {
CXFA_FMToJavaScriptDepth depthManager;
- if (CXFA_IsTooBig(javascript) || !depthManager.IsWithinMaxDepth())
+ if (CXFA_IsTooBig(js) || !depthManager.IsWithinMaxDepth())
return false;
switch (m_op) {
case TOKmul:
- javascript << L"pfm_rt.mul_op";
+ js << L"pfm_rt.mul_op";
break;
case TOKdiv:
- javascript << L"pfm_rt.div_op";
+ js << L"pfm_rt.div_op";
break;
default:
NOTREACHED();
break;
}
- javascript << L"(";
- if (!m_pExp1->ToJavaScript(javascript, ReturnType::kInfered))
+
+ CFX_WideTextBuf exp1_txt;
+ if (!m_pExp1->ToJavaScript(exp1_txt, ReturnType::kInfered))
return false;
- javascript << L", ";
- if (!m_pExp2->ToJavaScript(javascript, ReturnType::kInfered))
+
+ CFX_WideTextBuf exp2_txt;
+ if (!m_pExp2->ToJavaScript(exp2_txt, ReturnType::kInfered))
return false;
- javascript << L")";
- return !CXFA_IsTooBig(javascript);
+
+ js << L"(" << exp1_txt << L", " << exp2_txt << L")";
+ return !CXFA_IsTooBig(js);
}
CXFA_FMPosExpression::CXFA_FMPosExpression(
@@ -467,18 +474,17 @@ CXFA_FMPosExpression::CXFA_FMPosExpression(
std::unique_ptr<CXFA_FMSimpleExpression> pExp)
: CXFA_FMUnaryExpression(line, TOKplus, std::move(pExp)) {}
-bool CXFA_FMPosExpression::ToJavaScript(CFX_WideTextBuf& javascript,
- ReturnType type) {
+bool CXFA_FMPosExpression::ToJavaScript(CFX_WideTextBuf& js, ReturnType type) {
CXFA_FMToJavaScriptDepth depthManager;
- if (CXFA_IsTooBig(javascript) || !depthManager.IsWithinMaxDepth())
+ if (CXFA_IsTooBig(js) || !depthManager.IsWithinMaxDepth())
return false;
- javascript << L"pfm_rt.pos_op";
- javascript << L"(";
- if (!m_pExp->ToJavaScript(javascript, ReturnType::kInfered))
+ CFX_WideTextBuf exp_txt;
+ if (!m_pExp->ToJavaScript(exp_txt, ReturnType::kInfered))
return false;
- javascript << L")";
- return !CXFA_IsTooBig(javascript);
+
+ js << L"pfm_rt.pos_op(" << exp_txt << L")";
+ return !CXFA_IsTooBig(js);
}
CXFA_FMNegExpression::CXFA_FMNegExpression(
@@ -486,18 +492,17 @@ CXFA_FMNegExpression::CXFA_FMNegExpression(
std::unique_ptr<CXFA_FMSimpleExpression> pExp)
: CXFA_FMUnaryExpression(line, TOKminus, std::move(pExp)) {}
-bool CXFA_FMNegExpression::ToJavaScript(CFX_WideTextBuf& javascript,
- ReturnType type) {
+bool CXFA_FMNegExpression::ToJavaScript(CFX_WideTextBuf& js, ReturnType type) {
CXFA_FMToJavaScriptDepth depthManager;
- if (CXFA_IsTooBig(javascript) || !depthManager.IsWithinMaxDepth())
+ if (CXFA_IsTooBig(js) || !depthManager.IsWithinMaxDepth())
return false;
- javascript << L"pfm_rt.neg_op";
- javascript << L"(";
- if (!m_pExp->ToJavaScript(javascript, ReturnType::kInfered))
+ CFX_WideTextBuf exp_txt;
+ if (!m_pExp->ToJavaScript(exp_txt, ReturnType::kInfered))
return false;
- javascript << L")";
- return !CXFA_IsTooBig(javascript);
+
+ js << L"pfm_rt.neg_op(" << exp_txt << L")";
+ return !CXFA_IsTooBig(js);
}
CXFA_FMNotExpression::CXFA_FMNotExpression(
@@ -505,18 +510,17 @@ CXFA_FMNotExpression::CXFA_FMNotExpression(
std::unique_ptr<CXFA_FMSimpleExpression> pExp)
: CXFA_FMUnaryExpression(line, TOKksnot, std::move(pExp)) {}
-bool CXFA_FMNotExpression::ToJavaScript(CFX_WideTextBuf& javascript,
- ReturnType type) {
+bool CXFA_FMNotExpression::ToJavaScript(CFX_WideTextBuf& js, ReturnType type) {
CXFA_FMToJavaScriptDepth depthManager;
- if (CXFA_IsTooBig(javascript) || !depthManager.IsWithinMaxDepth())
+ if (CXFA_IsTooBig(js) || !depthManager.IsWithinMaxDepth())
return false;
- javascript << L"pfm_rt.log_not_op";
- javascript << L"(";
- if (!m_pExp->ToJavaScript(javascript, ReturnType::kInfered))
+ CFX_WideTextBuf exp_txt;
+ if (!m_pExp->ToJavaScript(exp_txt, ReturnType::kInfered))
return false;
- javascript << L")";
- return !CXFA_IsTooBig(javascript);
+
+ js << L"pfm_rt.log_not_op(" << exp_txt << L")";
+ return !CXFA_IsTooBig(js);
}
CXFA_FMCallExpression::CXFA_FMCallExpression(
@@ -534,12 +538,12 @@ bool CXFA_FMCallExpression::IsBuiltInFunc(CFX_WideTextBuf* funcName) {
if (funcName->GetLength() > g_BuiltInFuncsMaxLen)
return false;
- auto cmpFunc = [](const wchar_t* iter, const WideString& val) -> bool {
- return val.CompareNoCase(iter) > 0;
- };
WideString str = funcName->MakeString();
const wchar_t* const* pMatchResult = std::lower_bound(
- std::begin(g_BuiltInFuncs), std::end(g_BuiltInFuncs), str, cmpFunc);
+ std::begin(g_BuiltInFuncs), std::end(g_BuiltInFuncs), str,
+ [](const wchar_t* iter, const WideString& val) -> bool {
+ return val.CompareNoCase(iter) > 0;
+ });
if (pMatchResult != std::end(g_BuiltInFuncs) &&
!str.CompareNoCase(*pMatchResult)) {
funcName->Clear();
@@ -551,12 +555,11 @@ bool CXFA_FMCallExpression::IsBuiltInFunc(CFX_WideTextBuf* funcName) {
uint32_t CXFA_FMCallExpression::IsMethodWithObjParam(
const WideString& methodName) {
- auto cmpFunc = [](const XFA_FMSOMMethod iter, const WideString& val) {
- return val.Compare(iter.m_wsSomMethodName) > 0;
- };
- const XFA_FMSOMMethod* result =
- std::lower_bound(std::begin(gs_FMSomMethods), std::end(gs_FMSomMethods),
- methodName, cmpFunc);
+ const XFA_FMSOMMethod* result = std::lower_bound(
+ std::begin(gs_FMSomMethods), std::end(gs_FMSomMethods), methodName,
+ [](const XFA_FMSOMMethod iter, const WideString& val) {
+ return val.Compare(iter.m_wsSomMethodName) > 0;
+ });
if (result != std::end(gs_FMSomMethods) &&
!methodName.Compare(result->m_wsSomMethodName)) {
return result->m_dParameters;
@@ -564,100 +567,97 @@ uint32_t CXFA_FMCallExpression::IsMethodWithObjParam(
return 0;
}
-bool CXFA_FMCallExpression::ToJavaScript(CFX_WideTextBuf& javascript,
- ReturnType type) {
+bool CXFA_FMCallExpression::ToJavaScript(CFX_WideTextBuf& js, ReturnType type) {
CXFA_FMToJavaScriptDepth depthManager;
- if (CXFA_IsTooBig(javascript) || !depthManager.IsWithinMaxDepth())
+ if (CXFA_IsTooBig(js) || !depthManager.IsWithinMaxDepth())
return false;
CFX_WideTextBuf funcName;
if (!m_pExp->ToJavaScript(funcName, ReturnType::kInfered))
return false;
+
if (m_bIsSomMethod) {
- javascript << funcName;
- javascript << L"(";
+ js << funcName << L"(";
uint32_t methodPara = IsMethodWithObjParam(funcName.MakeString());
if (methodPara > 0) {
for (size_t i = 0; i < m_Arguments.size(); ++i) {
+ CFX_WideTextBuf expr_txt;
+ if (!m_Arguments[i]->ToJavaScript(expr_txt, ReturnType::kInfered))
+ return false;
+
// Currently none of our expressions use objects for a parameter over
// the 6th. Make sure we don't overflow the shift when doing this
// check. If we ever need more the 32 object params we can revisit.
- if (i < 32 && (methodPara & (0x01 << i)) > 0) {
- javascript << L"pfm_rt.get_jsobj";
- } else {
- javascript << L"pfm_rt.get_val";
- }
- javascript << L"(";
- const auto& expr = m_Arguments[i];
- if (!expr->ToJavaScript(javascript, ReturnType::kInfered))
- return false;
- javascript << L")";
- if (i + 1 < m_Arguments.size()) {
- javascript << L", ";
- }
+ if (i < 32 && (methodPara & (0x01 << i)) > 0)
+ js << L"pfm_rt.get_jsobj(" << expr_txt << L")";
+ else
+ js << L"pfm_rt.get_val(" << expr_txt << L")";
+
+ if (i + 1 < m_Arguments.size())
+ js << L", ";
}
} else {
for (const auto& expr : m_Arguments) {
- javascript << L"pfm_rt.get_val";
- javascript << L"(";
- if (!expr->ToJavaScript(javascript, ReturnType::kInfered))
+ CFX_WideTextBuf expr_txt;
+ if (!expr->ToJavaScript(expr_txt, ReturnType::kInfered))
return false;
- javascript << L")";
+
+ js << L"pfm_rt.get_val(" << expr_txt << L")";
if (expr != m_Arguments.back())
- javascript << L", ";
+ js << L", ";
}
}
- javascript << L")";
+ js << L")";
+ return !CXFA_IsTooBig(js);
+ }
+
+ bool isEvalFunc = false;
+ bool isExistsFunc = false;
+ if (!IsBuiltInFunc(&funcName)) {
+ // If a function is not a SomMethod or a built-in then the input was
+ // invalid, so failing. The scanner/lexer should catch this, but currently
+ // doesn't. This failure will bubble up to the top-level and cause the
+ // transpile to fail.
+ return false;
+ }
+
+ if (funcName.AsStringView() == L"Eval") {
+ isEvalFunc = true;
+ js << L"eval.call(this, pfm_rt.Translate";
} else {
- bool isEvalFunc = false;
- bool isExistsFunc = false;
- if (IsBuiltInFunc(&funcName)) {
- if (funcName.AsStringView() == L"Eval") {
- isEvalFunc = true;
- javascript << L"eval.call(this, pfm_rt.Translate";
- } else if (funcName.AsStringView() == L"Exists") {
- isExistsFunc = true;
- javascript << L"pfm_rt.";
- javascript << funcName;
- } else {
- javascript << L"pfm_rt.";
- javascript << funcName;
- }
- } else {
- // If a function is not a SomMethod or a built-in then the input was
- // invalid, so failing. The scanner/lexer should catch this, but currently
- // doesn't. This failure will bubble up to the top-level and cause the
- // transpile to fail.
- return false;
- }
- javascript << L"(";
- if (isExistsFunc) {
- javascript << L"\n(\nfunction ()\n{\ntry\n{\n";
- if (!m_Arguments.empty()) {
- const auto& expr = m_Arguments[0];
- javascript << L"return ";
- if (!expr->ToJavaScript(javascript, ReturnType::kInfered))
- return false;
- javascript << L";\n}\n";
- } else {
- javascript << L"return 0;\n}\n";
- }
- javascript << L"catch(accessExceptions)\n";
- javascript << L"{\nreturn 0;\n}\n}\n).call(this)\n";
+ if (funcName.AsStringView() == L"Exists")
+ isExistsFunc = true;
+
+ js << L"pfm_rt." << funcName;
+ }
+
+ js << L"(";
+ if (isExistsFunc) {
+ js << L"\n(\nfunction ()\n{\ntry\n{\n";
+ if (!m_Arguments.empty()) {
+ CFX_WideTextBuf expr_txt;
+ if (!m_Arguments[0]->ToJavaScript(expr_txt, ReturnType::kInfered))
+ return false;
+
+ js << L"return " << expr_txt << L";\n}\n";
} else {
- for (const auto& expr : m_Arguments) {
- if (!expr->ToJavaScript(javascript, ReturnType::kInfered))
- return false;
- if (expr != m_Arguments.back())
- javascript << L", ";
- }
+ js << L"return 0;\n}\n";
}
- javascript << L")";
- if (isEvalFunc) {
- javascript << L")";
+ js << L"catch(accessExceptions)\n";
+ js << L"{\nreturn 0;\n}\n}\n).call(this)\n";
+ } else {
+ for (const auto& expr : m_Arguments) {
+ if (!expr->ToJavaScript(js, ReturnType::kInfered))
+ return false;
+ if (expr != m_Arguments.back())
+ js << L", ";
}
}
- return !CXFA_IsTooBig(javascript);
+ js << L")";
+ if (isEvalFunc)
+ js << L")";
+
+ return !CXFA_IsTooBig(js);
}
CXFA_FMDotAccessorExpression::CXFA_FMDotAccessorExpression(
@@ -674,45 +674,43 @@ CXFA_FMDotAccessorExpression::CXFA_FMDotAccessorExpression(
CXFA_FMDotAccessorExpression::~CXFA_FMDotAccessorExpression() {}
-bool CXFA_FMDotAccessorExpression::ToJavaScript(CFX_WideTextBuf& javascript,
+bool CXFA_FMDotAccessorExpression::ToJavaScript(CFX_WideTextBuf& js,
ReturnType type) {
CXFA_FMToJavaScriptDepth depthManager;
- if (CXFA_IsTooBig(javascript) || !depthManager.IsWithinMaxDepth())
+ if (CXFA_IsTooBig(js) || !depthManager.IsWithinMaxDepth())
return false;
- javascript << L"pfm_rt.dot_acc";
- javascript << L"(";
+ js << L"pfm_rt.dot_acc(";
+
CFX_WideTextBuf tempExp1;
if (m_pExp1) {
if (!m_pExp1->ToJavaScript(tempExp1, ReturnType::kInfered))
return false;
- javascript << tempExp1;
+
+ js << tempExp1;
} else {
- javascript << L"null";
+ js << L"null";
}
- javascript << L", ";
- javascript << L"\"";
+ js << L", \"";
if (m_pExp1 && m_pExp1->GetOperatorToken() == TOKidentifier)
- javascript << tempExp1;
- javascript << L"\", ";
- if (m_op == TOKdotscream) {
- javascript << L"\"#";
- javascript << m_wsIdentifier;
- javascript << L"\", ";
- } else if (m_op == TOKdotstar) {
- javascript << L"\"*\", ";
- } else if (m_op == TOKcall) {
- javascript << L"\"\", ";
- } else {
- javascript << L"\"";
- javascript << m_wsIdentifier;
- javascript << L"\", ";
- }
- if (!m_pExp2->ToJavaScript(javascript, ReturnType::kInfered))
+ js << tempExp1;
+
+ js << L"\", ";
+ if (m_op == TOKdotscream)
+ js << L"\"#" << m_wsIdentifier << L"\", ";
+ else if (m_op == TOKdotstar)
+ js << L"\"*\", ";
+ else if (m_op == TOKcall)
+ js << L"\"\", ";
+ else
+ js << L"\"" << m_wsIdentifier << L"\", ";
+
+ if (!m_pExp2->ToJavaScript(js, ReturnType::kInfered))
return false;
- javascript << L")";
- return !CXFA_IsTooBig(javascript);
+
+ js << L")";
+ return !CXFA_IsTooBig(js);
}
CXFA_FMIndexExpression::CXFA_FMIndexExpression(
@@ -724,38 +722,39 @@ CXFA_FMIndexExpression::CXFA_FMIndexExpression(
m_accessorIndex(accessorIndex),
m_bIsStarIndex(bIsStarIndex) {}
-bool CXFA_FMIndexExpression::ToJavaScript(CFX_WideTextBuf& javascript,
+bool CXFA_FMIndexExpression::ToJavaScript(CFX_WideTextBuf& js,
ReturnType type) {
CXFA_FMToJavaScriptDepth depthManager;
- if (CXFA_IsTooBig(javascript) || !depthManager.IsWithinMaxDepth())
+ if (CXFA_IsTooBig(js) || !depthManager.IsWithinMaxDepth())
return false;
switch (m_accessorIndex) {
case ACCESSOR_NO_INDEX:
- javascript << L"0";
+ js << L"0";
break;
case ACCESSOR_NO_RELATIVEINDEX:
- javascript << L"1";
+ js << L"1";
break;
case ACCESSOR_POSITIVE_INDEX:
- javascript << L"2";
+ js << L"2";
break;
case ACCESSOR_NEGATIVE_INDEX:
- javascript << L"3";
+ js << L"3";
break;
default:
- javascript << L"0";
+ js << L"0";
}
- if (!m_bIsStarIndex) {
- javascript << L", ";
- if (m_pExp) {
- if (!m_pExp->ToJavaScript(javascript, ReturnType::kInfered))
- return false;
- } else {
- javascript << L"0";
- }
+ if (m_bIsStarIndex)
+ return !CXFA_IsTooBig(js);
+
+ js << L", ";
+ if (m_pExp) {
+ if (!m_pExp->ToJavaScript(js, ReturnType::kInfered))
+ return false;
+ } else {
+ js << L"0";
}
- return !CXFA_IsTooBig(javascript);
+ return !CXFA_IsTooBig(js);
}
CXFA_FMDotDotAccessorExpression::CXFA_FMDotDotAccessorExpression(
@@ -772,31 +771,26 @@ CXFA_FMDotDotAccessorExpression::CXFA_FMDotDotAccessorExpression(
CXFA_FMDotDotAccessorExpression::~CXFA_FMDotDotAccessorExpression() {}
-bool CXFA_FMDotDotAccessorExpression::ToJavaScript(CFX_WideTextBuf& javascript,
+bool CXFA_FMDotDotAccessorExpression::ToJavaScript(CFX_WideTextBuf& js,
ReturnType type) {
CXFA_FMToJavaScriptDepth depthManager;
- if (CXFA_IsTooBig(javascript) || !depthManager.IsWithinMaxDepth())
+ if (CXFA_IsTooBig(js) || !depthManager.IsWithinMaxDepth())
return false;
- javascript << L"pfm_rt.dotdot_acc";
- javascript << L"(";
- CFX_WideTextBuf tempExp1;
- if (!m_pExp1->ToJavaScript(tempExp1, ReturnType::kInfered))
+ CFX_WideTextBuf exp1_txt;
+ if (!m_pExp1->ToJavaScript(exp1_txt, ReturnType::kInfered))
+ return false;
+
+ CFX_WideTextBuf exp2_txt;
+ if (!m_pExp2->ToJavaScript(exp2_txt, ReturnType::kInfered))
return false;
- javascript << tempExp1;
- javascript << L", ";
- javascript << L"\"";
+ js << L"pfm_rt.dotdot_acc(" << exp1_txt << L", " << L"\"";
if (m_pExp1->GetOperatorToken() == TOKidentifier)
- javascript << tempExp1;
- javascript << L"\", ";
- javascript << L"\"";
- javascript << m_wsIdentifier;
- javascript << L"\", ";
- if (!m_pExp2->ToJavaScript(javascript, ReturnType::kInfered))
- return false;
- javascript << L")";
- return !CXFA_IsTooBig(javascript);
+ js << exp1_txt;
+
+ js << L"\", \"" << m_wsIdentifier << L"\", " << exp2_txt << L")";
+ return !CXFA_IsTooBig(js);
}
CXFA_FMMethodCallExpression::CXFA_FMMethodCallExpression(
@@ -808,34 +802,33 @@ CXFA_FMMethodCallExpression::CXFA_FMMethodCallExpression(
std::move(pAccessorExp1),
std::move(pCallExp)) {}
-bool CXFA_FMMethodCallExpression::ToJavaScript(CFX_WideTextBuf& javascript,
+bool CXFA_FMMethodCallExpression::ToJavaScript(CFX_WideTextBuf& js,
ReturnType type) {
CXFA_FMToJavaScriptDepth depthManager;
- if (CXFA_IsTooBig(javascript) || !depthManager.IsWithinMaxDepth())
+ if (CXFA_IsTooBig(js) || !depthManager.IsWithinMaxDepth())
return false;
- javascript << L"(\nfunction ()\n{\n";
- javascript << L"var method_return_value = null;\n";
- javascript << L"var accessor_object = ";
- if (!m_pExp1->ToJavaScript(javascript, ReturnType::kInfered))
+ CFX_WideTextBuf exp1_txt;
+ if (!m_pExp1->ToJavaScript(exp1_txt, ReturnType::kInfered))
return false;
- javascript << L";\n";
- javascript << L"if (pfm_rt.is_ary(accessor_object))\n{\n";
- javascript << L"for(var index = accessor_object.length - 1; index > 1; "
- L"index--)\n{\n";
- javascript << L"method_return_value = accessor_object[index].";
- CFX_WideTextBuf tempExp2;
- if (!m_pExp2->ToJavaScript(tempExp2, ReturnType::kInfered))
+ CFX_WideTextBuf exp2_txt;
+ if (!m_pExp2->ToJavaScript(exp2_txt, ReturnType::kInfered))
return false;
- javascript << tempExp2;
- javascript << L";\n}\n}\n";
- javascript << L"else\n{\nmethod_return_value = accessor_object.";
- javascript << tempExp2;
- javascript << L";\n}\n";
- javascript << L"return method_return_value;\n";
- javascript << L"}\n).call(this)";
- return !CXFA_IsTooBig(javascript);
+
+ js << L"(\nfunction ()\n{\n";
+ js << L"var method_return_value = null;\n";
+ js << L"var accessor_object = " << exp1_txt << L";\n";
+ js << L"if (pfm_rt.is_ary(accessor_object))\n{\n";
+ js << L"for(var index = accessor_object.length - 1; index > 1; "
+ L"index--)\n{\n";
+ js << L"method_return_value = accessor_object[index]." << exp2_txt << L";\n";
+ js << L"}\n}\nelse\n{\n";
+ js << L"method_return_value = accessor_object." << exp2_txt << L";\n";
+ js << L"}\n";
+ js << L"return method_return_value;\n";
+ js << L"}\n).call(this)";
+ return !CXFA_IsTooBig(js);
}
bool CXFA_IsTooBig(const CFX_WideTextBuf& javascript) {