From d276a52cd5061e0d4ee4aa79b8e89bbeb0a89da3 Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Mon, 19 Feb 2018 18:13:23 +0000 Subject: 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 Commit-Queue: dsinclair --- xfa/fxfa/fm2js/cxfa_fmexpression.cpp | 369 ++++++++--------- xfa/fxfa/fm2js/cxfa_fmsimpleexpression.cpp | 627 ++++++++++++++--------------- 2 files changed, 483 insertions(+), 513 deletions(-) diff --git a/xfa/fxfa/fm2js/cxfa_fmexpression.cpp b/xfa/fxfa/fm2js/cxfa_fmexpression.cpp index 9381e7f8a1..97652bcd37 100644 --- a/xfa/fxfa/fm2js/cxfa_fmexpression.cpp +++ b/xfa/fxfa/fm2js/cxfa_fmexpression.cpp @@ -27,10 +27,9 @@ CXFA_FMExpression::CXFA_FMExpression(uint32_t line) CXFA_FMExpression::CXFA_FMExpression(uint32_t line, XFA_FM_EXPTYPE type) : m_type(type), m_line(line) {} -bool CXFA_FMExpression::ToJavaScript(CFX_WideTextBuf& javascript, - ReturnType type) { +bool CXFA_FMExpression::ToJavaScript(CFX_WideTextBuf& js, ReturnType type) { CXFA_FMToJavaScriptDepth depthManager; - return !CXFA_IsTooBig(javascript) && depthManager.IsWithinMaxDepth(); + return !CXFA_IsTooBig(js) && depthManager.IsWithinMaxDepth(); } CXFA_FMFunctionDefinition::CXFA_FMFunctionDefinition( @@ -47,68 +46,66 @@ CXFA_FMFunctionDefinition::CXFA_FMFunctionDefinition( CXFA_FMFunctionDefinition::~CXFA_FMFunctionDefinition() {} -bool CXFA_FMFunctionDefinition::ToJavaScript(CFX_WideTextBuf& javascript, +bool CXFA_FMFunctionDefinition::ToJavaScript(CFX_WideTextBuf& js, ReturnType type) { CXFA_FMToJavaScriptDepth depthManager; - if (type == ReturnType::kImplied) { - return !CXFA_IsTooBig(javascript) && depthManager.IsWithinMaxDepth(); - } + if (type == ReturnType::kImplied) + return !CXFA_IsTooBig(js) && depthManager.IsWithinMaxDepth(); - if (CXFA_IsTooBig(javascript) || !depthManager.IsWithinMaxDepth()) + if (CXFA_IsTooBig(js) || !depthManager.IsWithinMaxDepth()) return false; if (m_isGlobal && m_pExpressions.empty()) { - javascript << L"// comments only"; - return !CXFA_IsTooBig(javascript); - } - if (m_isGlobal) { - javascript << L"(\n"; + js << L"// comments only"; + return !CXFA_IsTooBig(js); } - javascript << L"function "; + if (m_isGlobal) + js << L"(\n"; + + js << L"function "; if (!m_wsName.IsEmpty() && m_wsName[0] == L'!') { WideString tempName = L"pfm__excl__" + m_wsName.Right(m_wsName.GetLength() - 1); - javascript << tempName; + js << tempName; } else { - javascript << m_wsName; + js << m_wsName; } - javascript << L"("; + + js << L"("; bool bNeedComma = false; for (const auto& identifier : m_pArguments) { if (bNeedComma) - javascript << L", "; + js << L", "; if (identifier[0] == L'!') { WideString tempIdentifier = L"pfm__excl__" + identifier.Right(identifier.GetLength() - 1); - javascript << tempIdentifier; + js << tempIdentifier; } else { - javascript << identifier; + js << identifier; } bNeedComma = true; } - javascript << L")\n{\n"; - javascript << L"var pfm_ret = null;\n"; - for (const auto& expr : m_pExpressions) { - bool ret; - if (expr == m_pExpressions.back()) - ret = expr->ToJavaScript(javascript, ReturnType::kImplied); - else - ret = expr->ToJavaScript(javascript, ReturnType::kInfered); + js << L")\n{\n"; - if (!ret) + js << L"var pfm_ret = null;\n"; + for (const auto& expr : m_pExpressions) { + ReturnType ret_type = expr == m_pExpressions.back() ? ReturnType::kImplied + : ReturnType::kInfered; + if (!expr->ToJavaScript(js, ret_type)) return false; } - javascript << L"return "; + + js << L"return "; if (m_isGlobal) - javascript << L"pfm_rt.get_val(pfm_ret)"; + js << L"pfm_rt.get_val(pfm_ret);\n"; else - javascript << L"pfm_ret"; + js << L"pfm_ret;\n"; - javascript << L";\n}\n"; - if (m_isGlobal) { - javascript << L").call(this);\n"; - } - return !CXFA_IsTooBig(javascript); + js << L"}\n"; + if (m_isGlobal) + js << L").call(this);\n"; + + return !CXFA_IsTooBig(js); } CXFA_FMVarExpression::CXFA_FMVarExpression( @@ -121,36 +118,29 @@ CXFA_FMVarExpression::CXFA_FMVarExpression( CXFA_FMVarExpression::~CXFA_FMVarExpression() {} -bool CXFA_FMVarExpression::ToJavaScript(CFX_WideTextBuf& javascript, - ReturnType type) { +bool CXFA_FMVarExpression::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"var "; WideString tempName(m_wsName); - if (m_wsName[0] == L'!') { + if (m_wsName[0] == L'!') tempName = L"pfm__excl__" + m_wsName.Right(m_wsName.GetLength() - 1); - } - javascript << tempName; - javascript << L" = "; + + js << L"var " << tempName << L" = "; if (m_pInit) { - if (!m_pInit->ToJavaScript(javascript, ReturnType::kInfered)) + if (!m_pInit->ToJavaScript(js, ReturnType::kInfered)) return false; - javascript << tempName; - javascript << L" = pfm_rt.var_filter("; - javascript << tempName; - javascript << L");\n"; + + js << tempName << L" = pfm_rt.var_filter(" << tempName << L");\n"; } else { - javascript << L"\"\";\n"; + js << L"\"\";\n"; } - if (type == ReturnType::kImplied) { - javascript << L"pfm_ret = "; - javascript << tempName; - javascript << L";\n"; - } - return !CXFA_IsTooBig(javascript); + if (type == ReturnType::kImplied) + js << L"pfm_ret = " << tempName << L";\n"; + + return !CXFA_IsTooBig(js); } CXFA_FMExpExpression::CXFA_FMExpExpression( @@ -161,39 +151,41 @@ CXFA_FMExpExpression::CXFA_FMExpExpression( CXFA_FMExpExpression::~CXFA_FMExpExpression() {} -bool CXFA_FMExpExpression::ToJavaScript(CFX_WideTextBuf& javascript, - ReturnType type) { +bool CXFA_FMExpExpression::ToJavaScript(CFX_WideTextBuf& js, ReturnType type) { CXFA_FMToJavaScriptDepth depthManager; - if (CXFA_IsTooBig(javascript) || !depthManager.IsWithinMaxDepth()) + if (CXFA_IsTooBig(js) || !depthManager.IsWithinMaxDepth()) return false; if (type == ReturnType::kInfered) { - bool ret = m_pExpression->ToJavaScript(javascript, ReturnType::kInfered); + bool ret = m_pExpression->ToJavaScript(js, ReturnType::kInfered); if (m_pExpression->GetOperatorToken() != TOKassign) - javascript << L";\n"; + js << L";\n"; + return ret; } if (m_pExpression->GetOperatorToken() == TOKassign) - return m_pExpression->ToJavaScript(javascript, ReturnType::kImplied); + return m_pExpression->ToJavaScript(js, ReturnType::kImplied); if (m_pExpression->GetOperatorToken() == TOKstar || m_pExpression->GetOperatorToken() == TOKdotstar || m_pExpression->GetOperatorToken() == TOKdotscream || m_pExpression->GetOperatorToken() == TOKdotdot || m_pExpression->GetOperatorToken() == TOKdot) { - javascript << L"pfm_ret = pfm_rt.get_val("; - if (!m_pExpression->ToJavaScript(javascript, ReturnType::kInfered)) + js << L"pfm_ret = pfm_rt.get_val("; + if (!m_pExpression->ToJavaScript(js, ReturnType::kInfered)) return false; - javascript << L");\n"; - return !CXFA_IsTooBig(javascript); + + js << L");\n"; + return !CXFA_IsTooBig(js); } - javascript << L"pfm_ret = "; - if (!m_pExpression->ToJavaScript(javascript, ReturnType::kInfered)) + js << L"pfm_ret = "; + if (!m_pExpression->ToJavaScript(js, ReturnType::kInfered)) return false; - javascript << L";\n"; - return !CXFA_IsTooBig(javascript); + + js << L";\n"; + return !CXFA_IsTooBig(js); } CXFA_FMBlockExpression::CXFA_FMBlockExpression( @@ -204,30 +196,28 @@ CXFA_FMBlockExpression::CXFA_FMBlockExpression( CXFA_FMBlockExpression::~CXFA_FMBlockExpression() {} -bool CXFA_FMBlockExpression::ToJavaScript(CFX_WideTextBuf& javascript, +bool CXFA_FMBlockExpression::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"{\n"; + js << L"{\n"; for (const auto& expr : m_ExpressionList) { if (type == ReturnType::kInfered) { - if (!expr->ToJavaScript(javascript, ReturnType::kInfered)) + if (!expr->ToJavaScript(js, ReturnType::kInfered)) return false; } else { - bool ret; - if (expr == m_ExpressionList.back()) - ret = expr->ToJavaScript(javascript, ReturnType::kImplied); - else - ret = expr->ToJavaScript(javascript, ReturnType::kInfered); - - if (!ret) + ReturnType ret_type = expr == m_ExpressionList.back() + ? ReturnType::kImplied + : ReturnType::kInfered; + if (!expr->ToJavaScript(js, ret_type)) return false; } } - javascript << L"}\n"; - return !CXFA_IsTooBig(javascript); + js << L"}\n"; + + return !CXFA_IsTooBig(js); } CXFA_FMDoExpression::CXFA_FMDoExpression( @@ -237,13 +227,12 @@ CXFA_FMDoExpression::CXFA_FMDoExpression( CXFA_FMDoExpression::~CXFA_FMDoExpression() {} -bool CXFA_FMDoExpression::ToJavaScript(CFX_WideTextBuf& javascript, - ReturnType type) { +bool CXFA_FMDoExpression::ToJavaScript(CFX_WideTextBuf& js, ReturnType type) { CXFA_FMToJavaScriptDepth depthManager; - if (CXFA_IsTooBig(javascript) || !depthManager.IsWithinMaxDepth()) + if (CXFA_IsTooBig(js) || !depthManager.IsWithinMaxDepth()) return false; - return m_pList->ToJavaScript(javascript, type); + return m_pList->ToJavaScript(js, type); } CXFA_FMIfExpression::CXFA_FMIfExpression( @@ -258,55 +247,55 @@ CXFA_FMIfExpression::CXFA_FMIfExpression( CXFA_FMIfExpression::~CXFA_FMIfExpression() {} -bool CXFA_FMIfExpression::ToJavaScript(CFX_WideTextBuf& javascript, - ReturnType type) { +bool CXFA_FMIfExpression::ToJavaScript(CFX_WideTextBuf& js, ReturnType type) { CXFA_FMToJavaScriptDepth depthManager; - if (CXFA_IsTooBig(javascript) || !depthManager.IsWithinMaxDepth()) + if (CXFA_IsTooBig(js) || !depthManager.IsWithinMaxDepth()) return false; if (type == ReturnType::kImplied) - javascript << L"pfm_ret = 0;\n"; + js << L"pfm_ret = 0;\n"; - javascript << L"if ("; + js << L"if ("; if (m_pExpression) { - javascript << L"pfm_rt.get_val("; - if (!m_pExpression->ToJavaScript(javascript, ReturnType::kInfered)) + js << L"pfm_rt.get_val("; + if (!m_pExpression->ToJavaScript(js, ReturnType::kInfered)) return false; - javascript << L")"; + + js << L")"; } - javascript << L")\n"; - if (CXFA_IsTooBig(javascript)) + js << L")\n"; + + if (CXFA_IsTooBig(js)) return false; if (m_pIfExpression) { - if (!m_pIfExpression->ToJavaScript(javascript, type)) + if (!m_pIfExpression->ToJavaScript(js, type)) return false; - if (CXFA_IsTooBig(javascript)) + if (CXFA_IsTooBig(js)) return false; } if (m_pElseExpression) { if (m_pElseExpression->GetExpType() == XFA_FM_EXPTYPE_IF) { - javascript << L"else\n"; - javascript << L"{\n"; - if (!m_pElseExpression->ToJavaScript(javascript, type)) + js << L"else\n{\n"; + if (!m_pElseExpression->ToJavaScript(js, type)) return false; - javascript << L"}\n"; + + js << L"}\n"; } else { - javascript << L"else\n"; - if (!m_pElseExpression->ToJavaScript(javascript, type)) + js << L"else\n"; + if (!m_pElseExpression->ToJavaScript(js, type)) return false; } } - return !CXFA_IsTooBig(javascript); + return !CXFA_IsTooBig(js); } CXFA_FMLoopExpression::~CXFA_FMLoopExpression() {} -bool CXFA_FMLoopExpression::ToJavaScript(CFX_WideTextBuf& javascript, - ReturnType type) { +bool CXFA_FMLoopExpression::ToJavaScript(CFX_WideTextBuf& js, ReturnType type) { CXFA_FMToJavaScriptDepth depthManager; - return !CXFA_IsTooBig(javascript) && depthManager.IsWithinMaxDepth(); + return !CXFA_IsTooBig(js) && depthManager.IsWithinMaxDepth(); } CXFA_FMWhileExpression::CXFA_FMWhileExpression( @@ -319,25 +308,27 @@ CXFA_FMWhileExpression::CXFA_FMWhileExpression( CXFA_FMWhileExpression::~CXFA_FMWhileExpression() {} -bool CXFA_FMWhileExpression::ToJavaScript(CFX_WideTextBuf& javascript, +bool CXFA_FMWhileExpression::ToJavaScript(CFX_WideTextBuf& js, ReturnType type) { CXFA_FMToJavaScriptDepth depthManager; - if (CXFA_IsTooBig(javascript) || !depthManager.IsWithinMaxDepth()) + if (CXFA_IsTooBig(js) || !depthManager.IsWithinMaxDepth()) return false; if (type == ReturnType::kImplied) - javascript << L"pfm_ret = 0;\n"; + js << L"pfm_ret = 0;\n"; - javascript << L"while ("; - if (!m_pCondition->ToJavaScript(javascript, ReturnType::kInfered)) + js << L"while ("; + if (!m_pCondition->ToJavaScript(js, ReturnType::kInfered)) return false; - javascript << L")\n"; - if (CXFA_IsTooBig(javascript)) + + js << L")\n"; + if (CXFA_IsTooBig(js)) return false; - if (!m_pExpression->ToJavaScript(javascript, type)) + if (!m_pExpression->ToJavaScript(js, type)) return false; - return !CXFA_IsTooBig(javascript); + + return !CXFA_IsTooBig(js); } CXFA_FMBreakExpression::CXFA_FMBreakExpression(uint32_t line) @@ -345,15 +336,14 @@ CXFA_FMBreakExpression::CXFA_FMBreakExpression(uint32_t line) CXFA_FMBreakExpression::~CXFA_FMBreakExpression() {} -bool CXFA_FMBreakExpression::ToJavaScript(CFX_WideTextBuf& javascript, +bool CXFA_FMBreakExpression::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_ret = 0;\n"; - javascript << L"break;\n"; - return !CXFA_IsTooBig(javascript); + js << L"pfm_ret = 0;\nbreak;\n"; + return !CXFA_IsTooBig(js); } CXFA_FMContinueExpression::CXFA_FMContinueExpression(uint32_t line) @@ -361,15 +351,14 @@ CXFA_FMContinueExpression::CXFA_FMContinueExpression(uint32_t line) CXFA_FMContinueExpression::~CXFA_FMContinueExpression() {} -bool CXFA_FMContinueExpression::ToJavaScript(CFX_WideTextBuf& javascript, +bool CXFA_FMContinueExpression::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_ret = 0;\n"; - javascript << L"continue;\n"; - return !CXFA_IsTooBig(javascript); + js << L"pfm_ret = 0;\ncontinue;\n"; + return !CXFA_IsTooBig(js); } CXFA_FMForExpression::CXFA_FMForExpression( @@ -390,61 +379,54 @@ CXFA_FMForExpression::CXFA_FMForExpression( CXFA_FMForExpression::~CXFA_FMForExpression() {} -bool CXFA_FMForExpression::ToJavaScript(CFX_WideTextBuf& javascript, - ReturnType type) { +bool CXFA_FMForExpression::ToJavaScript(CFX_WideTextBuf& js, ReturnType type) { CXFA_FMToJavaScriptDepth depthManager; - if (CXFA_IsTooBig(javascript) || !depthManager.IsWithinMaxDepth()) + if (CXFA_IsTooBig(js) || !depthManager.IsWithinMaxDepth()) return false; if (type == ReturnType::kImplied) - javascript << L"pfm_ret = 0;\n"; - - javascript << L"{\nvar "; - WideString tempVariant; - if (m_wsVariant[0] == L'!') { - tempVariant = - L"pfm__excl__" + m_wsVariant.Right(m_wsVariant.GetLength() - 1); - javascript << tempVariant; - } else { - tempVariant = m_wsVariant; - javascript << m_wsVariant; - } - javascript << L" = null;\n"; - javascript << L"for ("; - javascript << tempVariant; - javascript << L" = pfm_rt.get_val("; - if (!m_pAssignment->ToJavaScript(javascript, ReturnType::kInfered)) - return false; - javascript << L"); "; - javascript << tempVariant; + js << L"pfm_ret = 0;\n"; + + js << L"{\n"; + + WideString tmpName; + if (m_wsVariant[0] == L'!') + tmpName = L"pfm__excl__" + m_wsVariant.Right(m_wsVariant.GetLength() - 1); + else + tmpName = m_wsVariant; - javascript << (m_bDirection ? kLessEqual : kGreaterEqual); - javascript << L"pfm_rt.get_val("; - if (!m_pAccessor->ToJavaScript(javascript, ReturnType::kInfered)) + js << L"var " << tmpName << L" = null;\n"; + + CFX_WideTextBuf assign_txt; + if (!m_pAssignment->ToJavaScript(assign_txt, ReturnType::kInfered)) return false; - javascript << L"); "; - javascript << tempVariant; - javascript << L" += "; - javascript << (m_bDirection ? kPlusEqual : kMinusEqual); - if (CXFA_IsTooBig(javascript)) + + CFX_WideTextBuf accessor_txt; + if (!m_pAccessor->ToJavaScript(accessor_txt, ReturnType::kInfered)) return false; + js << L"for (" << tmpName << L" = pfm_rt.get_val(" << assign_txt << L"); "; + js << tmpName << (m_bDirection ? kLessEqual : kGreaterEqual); + js << L"pfm_rt.get_val(" << accessor_txt << L"); "; + js << tmpName << (m_bDirection ? kPlusEqual : kMinusEqual); if (m_pStep) { - javascript << L"pfm_rt.get_val("; - if (!m_pStep->ToJavaScript(javascript, ReturnType::kInfered)) + CFX_WideTextBuf step_txt; + if (!m_pStep->ToJavaScript(step_txt, ReturnType::kInfered)) return false; - javascript << L")"; + + js << L"pfm_rt.get_val(" << step_txt << L")"; } else { - javascript << L"1"; + js << L"1"; } - if (CXFA_IsTooBig(javascript)) + js << L")\n"; + if (CXFA_IsTooBig(js)) return false; - javascript << L")\n"; - if (!m_pList->ToJavaScript(javascript, type)) + if (!m_pList->ToJavaScript(js, type)) return false; - javascript << L"}\n"; - return !CXFA_IsTooBig(javascript); + + js << L"}\n"; + return !CXFA_IsTooBig(js); } CXFA_FMForeachExpression::CXFA_FMForeachExpression( @@ -459,47 +441,42 @@ CXFA_FMForeachExpression::CXFA_FMForeachExpression( CXFA_FMForeachExpression::~CXFA_FMForeachExpression() {} -bool CXFA_FMForeachExpression::ToJavaScript(CFX_WideTextBuf& javascript, +bool CXFA_FMForeachExpression::ToJavaScript(CFX_WideTextBuf& js, ReturnType type) { CXFA_FMToJavaScriptDepth depthManager; - if (CXFA_IsTooBig(javascript) || !depthManager.IsWithinMaxDepth()) + if (CXFA_IsTooBig(js) || !depthManager.IsWithinMaxDepth()) return false; if (type == ReturnType::kImplied) - javascript << L"pfm_ret = 0;\n"; + js << L"pfm_ret = 0;\n"; + + js << L"{\n"; - javascript << L"{\n"; - javascript << L"var "; + WideString tmpName; if (m_wsIdentifier[0] == L'!') { - WideString tempIdentifier = + tmpName = L"pfm__excl__" + m_wsIdentifier.Right(m_wsIdentifier.GetLength() - 1); - javascript << tempIdentifier; } else { - javascript << m_wsIdentifier; + tmpName = m_wsIdentifier; } - javascript << L" = null;\n"; - javascript << L"var pfm_ary = pfm_rt.concat_obj("; + js << L"var " << tmpName << L" = null;\n"; + js << L"var pfm_ary = pfm_rt.concat_obj("; for (const auto& expr : m_pAccessors) { - if (!expr->ToJavaScript(javascript, ReturnType::kInfered)) + if (!expr->ToJavaScript(js, ReturnType::kInfered)) return false; if (expr != m_pAccessors.back()) - javascript << L", "; + js << L", "; } - javascript << L");\n"; - javascript << L"var pfm_ary_idx = 0;\n"; - javascript << L"while(pfm_ary_idx < pfm_ary.length)\n{\n"; - if (m_wsIdentifier[0] == L'!') { - WideString tempIdentifier = - L"pfm__excl__" + m_wsIdentifier.Right(m_wsIdentifier.GetLength() - 1); - javascript << tempIdentifier; - } else { - javascript << m_wsIdentifier; - } - javascript << L" = pfm_ary[pfm_ary_idx++];\n"; - if (!m_pList->ToJavaScript(javascript, type)) + js << L");\n"; + + js << L"var pfm_ary_idx = 0;\n"; + js << L"while(pfm_ary_idx < pfm_ary.length)\n{\n"; + js << tmpName << L" = pfm_ary[pfm_ary_idx++];\n"; + if (!m_pList->ToJavaScript(js, type)) return false; - javascript << L"}\n"; - javascript << L"}\n"; - return !CXFA_IsTooBig(javascript); + js << L"}\n"; // while + + js << L"}\n"; // block + return !CXFA_IsTooBig(js); } 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 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 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 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 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 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 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 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 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 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 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) { -- cgit v1.2.3