OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "xfa/fxfa/fm2js/xfa_simpleexpression.h" | 7 #include "xfa/fxfa/fm2js/xfa_simpleexpression.h" |
8 | 8 |
| 9 #include <utility> |
| 10 |
9 #include "core/fxcrt/fx_ext.h" | 11 #include "core/fxcrt/fx_ext.h" |
10 | 12 |
11 namespace { | 13 namespace { |
12 | 14 |
13 const FX_WCHAR* const gs_lpStrExpFuncName[] = { | 15 const FX_WCHAR* const gs_lpStrExpFuncName[] = { |
14 L"foxit_xfa_formcalc_runtime.assign_value_operator", | 16 L"foxit_xfa_formcalc_runtime.assign_value_operator", |
15 L"foxit_xfa_formcalc_runtime.logical_or_operator", | 17 L"foxit_xfa_formcalc_runtime.logical_or_operator", |
16 L"foxit_xfa_formcalc_runtime.logical_and_operator", | 18 L"foxit_xfa_formcalc_runtime.logical_and_operator", |
17 L"foxit_xfa_formcalc_runtime.equality_operator", | 19 L"foxit_xfa_formcalc_runtime.equality_operator", |
18 L"foxit_xfa_formcalc_runtime.notequality_operator", | 20 L"foxit_xfa_formcalc_runtime.notequality_operator", |
(...skipping 451 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
470 void CXFA_FMNotExpression::ToJavaScript(CFX_WideTextBuf& javascript) { | 472 void CXFA_FMNotExpression::ToJavaScript(CFX_WideTextBuf& javascript) { |
471 javascript << gs_lpStrExpFuncName[NOT]; | 473 javascript << gs_lpStrExpFuncName[NOT]; |
472 javascript << FX_WSTRC(L"("); | 474 javascript << FX_WSTRC(L"("); |
473 m_pExp->ToJavaScript(javascript); | 475 m_pExp->ToJavaScript(javascript); |
474 javascript << FX_WSTRC(L")"); | 476 javascript << FX_WSTRC(L")"); |
475 } | 477 } |
476 | 478 |
477 CXFA_FMCallExpression::CXFA_FMCallExpression( | 479 CXFA_FMCallExpression::CXFA_FMCallExpression( |
478 uint32_t line, | 480 uint32_t line, |
479 CXFA_FMSimpleExpression* pExp, | 481 CXFA_FMSimpleExpression* pExp, |
480 CFX_ArrayTemplate<CXFA_FMSimpleExpression*>* pArguments, | 482 std::vector<std::unique_ptr<CXFA_FMSimpleExpression>>&& pArguments, |
481 bool bIsSomMethod) | 483 bool bIsSomMethod) |
482 : CXFA_FMUnaryExpression(line, TOKcall, pExp), | 484 : CXFA_FMUnaryExpression(line, TOKcall, pExp), |
483 m_bIsSomMethod(bIsSomMethod), | 485 m_bIsSomMethod(bIsSomMethod), |
484 m_pArguments(pArguments) {} | 486 m_Arguments(std::move(pArguments)) {} |
485 | 487 |
486 CXFA_FMCallExpression::~CXFA_FMCallExpression() { | 488 CXFA_FMCallExpression::~CXFA_FMCallExpression() {} |
487 if (m_pArguments) { | |
488 for (int i = 0; i < m_pArguments->GetSize(); ++i) | |
489 delete m_pArguments->GetAt(i); | |
490 | |
491 delete m_pArguments; | |
492 } | |
493 } | |
494 | 489 |
495 bool CXFA_FMCallExpression::IsBuildInFunc(CFX_WideTextBuf* funcName) { | 490 bool CXFA_FMCallExpression::IsBuildInFunc(CFX_WideTextBuf* funcName) { |
496 uint32_t uHash = FX_HashCode_GetW(funcName->AsStringC(), true); | 491 uint32_t uHash = FX_HashCode_GetW(funcName->AsStringC(), true); |
497 const XFA_FMBuildInFunc* pEnd = g_BuildInFuncs + FX_ArraySize(g_BuildInFuncs); | 492 const XFA_FMBuildInFunc* pEnd = g_BuildInFuncs + FX_ArraySize(g_BuildInFuncs); |
498 const XFA_FMBuildInFunc* pFunc = | 493 const XFA_FMBuildInFunc* pFunc = |
499 std::lower_bound(g_BuildInFuncs, pEnd, uHash, | 494 std::lower_bound(g_BuildInFuncs, pEnd, uHash, |
500 [](const XFA_FMBuildInFunc& func, uint32_t hash) { | 495 [](const XFA_FMBuildInFunc& func, uint32_t hash) { |
501 return func.m_uHash < hash; | 496 return func.m_uHash < hash; |
502 }); | 497 }); |
503 if (pFunc < pEnd && uHash == pFunc->m_uHash) { | 498 if (pFunc < pEnd && uHash == pFunc->m_uHash) { |
(...skipping 25 matching lines...) Expand all Loading... |
529 } | 524 } |
530 } while (iStart <= iEnd); | 525 } while (iStart <= iEnd); |
531 return parameters; | 526 return parameters; |
532 } | 527 } |
533 | 528 |
534 void CXFA_FMCallExpression::ToJavaScript(CFX_WideTextBuf& javascript) { | 529 void CXFA_FMCallExpression::ToJavaScript(CFX_WideTextBuf& javascript) { |
535 CFX_WideTextBuf funcName; | 530 CFX_WideTextBuf funcName; |
536 m_pExp->ToJavaScript(funcName); | 531 m_pExp->ToJavaScript(funcName); |
537 if (m_bIsSomMethod) { | 532 if (m_bIsSomMethod) { |
538 javascript << funcName; | 533 javascript << funcName; |
539 javascript << FX_WSTRC(L"("); | 534 javascript << L"("; |
540 if (m_pArguments) { | 535 uint32_t methodPara = IsMethodWithObjParam(funcName.AsStringC()); |
541 uint32_t methodPara = IsMethodWithObjParam(funcName.AsStringC()); | 536 if (methodPara > 0) { |
542 if (methodPara > 0) { | 537 for (size_t i = 0; i < m_Arguments.size(); ++i) { |
543 for (int i = 0; i < m_pArguments->GetSize(); ++i) { | 538 // Currently none of our expressions use objects for a parameter over |
544 // Currently none of our expressions use objects for a parameter over | 539 // the 6th. Make sure we don't overflow the shift when doing this |
545 // the 6th. Make sure we don't overflow the shift when doing this | 540 // check. If we ever need more the 32 object params we can revisit. |
546 // check. If we ever need more the 32 object params we can revisit. | 541 if (i < 32 && (methodPara & (0x01 << i)) > 0) { |
547 if (i < 32 && (methodPara & (0x01 << i)) > 0) { | 542 javascript << gs_lpStrExpFuncName[GETFMJSOBJ]; |
548 javascript << gs_lpStrExpFuncName[GETFMJSOBJ]; | 543 } else { |
549 } else { | 544 javascript << gs_lpStrExpFuncName[GETFMVALUE]; |
550 javascript << gs_lpStrExpFuncName[GETFMVALUE]; | |
551 } | |
552 javascript << FX_WSTRC(L"("); | |
553 CXFA_FMSimpleExpression* e = m_pArguments->GetAt(i); | |
554 e->ToJavaScript(javascript); | |
555 javascript << FX_WSTRC(L")"); | |
556 if (i + 1 < m_pArguments->GetSize()) { | |
557 javascript << FX_WSTRC(L", "); | |
558 } | |
559 } | 545 } |
560 } else { | 546 javascript << L"("; |
561 for (int i = 0; i < m_pArguments->GetSize(); ++i) { | 547 const auto& expr = m_Arguments[i]; |
562 javascript << gs_lpStrExpFuncName[GETFMVALUE]; | 548 expr->ToJavaScript(javascript); |
563 javascript << FX_WSTRC(L"("); | 549 javascript << L")"; |
564 CXFA_FMSimpleExpression* e = m_pArguments->GetAt(i); | 550 if (i + 1 < m_Arguments.size()) { |
565 e->ToJavaScript(javascript); | 551 javascript << L", "; |
566 javascript << FX_WSTRC(L")"); | |
567 if (i + 1 < m_pArguments->GetSize()) { | |
568 javascript << FX_WSTRC(L", "); | |
569 } | |
570 } | 552 } |
571 } | 553 } |
| 554 } else { |
| 555 for (const auto& expr : m_Arguments) { |
| 556 javascript << gs_lpStrExpFuncName[GETFMVALUE]; |
| 557 javascript << L"("; |
| 558 expr->ToJavaScript(javascript); |
| 559 javascript << L")"; |
| 560 if (expr != m_Arguments.back()) |
| 561 javascript << L", "; |
| 562 } |
572 } | 563 } |
573 javascript << FX_WSTRC(L")"); | 564 javascript << L")"; |
574 } else { | 565 } else { |
575 bool isEvalFunc = false; | 566 bool isEvalFunc = false; |
576 bool isExistsFunc = false; | 567 bool isExistsFunc = false; |
577 if (IsBuildInFunc(&funcName)) { | 568 if (IsBuildInFunc(&funcName)) { |
578 if (funcName.AsStringC() == FX_WSTRC(L"Eval")) { | 569 if (funcName.AsStringC() == FX_WSTRC(L"Eval")) { |
579 isEvalFunc = true; | 570 isEvalFunc = true; |
580 javascript << FX_WSTRC(L"eval.call(this, "); | 571 javascript << FX_WSTRC(L"eval.call(this, "); |
581 javascript << gs_lpStrExpFuncName[CALL]; | 572 javascript << gs_lpStrExpFuncName[CALL]; |
582 javascript << FX_WSTRC(L"Translate"); | 573 javascript << FX_WSTRC(L"Translate"); |
583 } else if (funcName.AsStringC() == FX_WSTRC(L"Exists")) { | 574 } else if (funcName.AsStringC() == FX_WSTRC(L"Exists")) { |
584 isExistsFunc = true; | 575 isExistsFunc = true; |
585 javascript << gs_lpStrExpFuncName[CALL]; | 576 javascript << gs_lpStrExpFuncName[CALL]; |
586 javascript << funcName; | 577 javascript << funcName; |
587 } else { | 578 } else { |
588 javascript << gs_lpStrExpFuncName[CALL]; | 579 javascript << gs_lpStrExpFuncName[CALL]; |
589 javascript << funcName; | 580 javascript << funcName; |
590 } | 581 } |
591 } else { | 582 } else { |
592 javascript << funcName; | 583 javascript << funcName; |
593 } | 584 } |
594 javascript << FX_WSTRC(L"("); | 585 javascript << FX_WSTRC(L"("); |
595 if (isExistsFunc) { | 586 if (isExistsFunc) { |
596 javascript << FX_WSTRC(L"\n(\nfunction ()\n{\ntry\n{\n"); | 587 javascript << FX_WSTRC(L"\n(\nfunction ()\n{\ntry\n{\n"); |
597 if (m_pArguments && m_pArguments->GetSize() > 0) { | 588 if (!m_Arguments.empty()) { |
598 CXFA_FMSimpleExpression* e = m_pArguments->GetAt(0); | 589 const auto& expr = m_Arguments[0]; |
599 javascript << FX_WSTRC(L"return "); | 590 javascript << FX_WSTRC(L"return "); |
600 e->ToJavaScript(javascript); | 591 expr->ToJavaScript(javascript); |
601 javascript << FX_WSTRC(L";\n}\n"); | 592 javascript << FX_WSTRC(L";\n}\n"); |
602 } else { | 593 } else { |
603 javascript << FX_WSTRC(L"return 0;\n}\n"); | 594 javascript << FX_WSTRC(L"return 0;\n}\n"); |
604 } | 595 } |
605 javascript << FX_WSTRC( | 596 javascript << FX_WSTRC( |
606 L"catch(accessExceptions)\n{\nreturn 0;\n}\n}\n).call(this)\n"); | 597 L"catch(accessExceptions)\n{\nreturn 0;\n}\n}\n).call(this)\n"); |
607 } else if (m_pArguments) { | 598 } else { |
608 for (int i = 0; i < m_pArguments->GetSize(); ++i) { | 599 for (const auto& expr : m_Arguments) { |
609 CXFA_FMSimpleExpression* e = m_pArguments->GetAt(i); | 600 expr->ToJavaScript(javascript); |
610 e->ToJavaScript(javascript); | 601 if (expr != m_Arguments.back()) |
611 if (i + 1 < m_pArguments->GetSize()) { | 602 javascript << L", "; |
612 javascript << FX_WSTRC(L", "); | |
613 } | |
614 } | 603 } |
615 } | 604 } |
616 javascript << FX_WSTRC(L")"); | 605 javascript << L")"; |
617 if (isEvalFunc) { | 606 if (isEvalFunc) { |
618 javascript << FX_WSTRC(L")"); | 607 javascript << L")"; |
619 } | 608 } |
620 } | 609 } |
621 } | 610 } |
622 | 611 |
623 CXFA_FMDotAccessorExpression::CXFA_FMDotAccessorExpression( | 612 CXFA_FMDotAccessorExpression::CXFA_FMDotAccessorExpression( |
624 uint32_t line, | 613 uint32_t line, |
625 CXFA_FMSimpleExpression* pAccessor, | 614 CXFA_FMSimpleExpression* pAccessor, |
626 XFA_FM_TOKEN op, | 615 XFA_FM_TOKEN op, |
627 CFX_WideStringC wsIdentifier, | 616 CFX_WideStringC wsIdentifier, |
628 CXFA_FMSimpleExpression* pIndexExp) | 617 CXFA_FMSimpleExpression* pIndexExp) |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
746 L"for(var index = accessor_object.length - 1; index > 1; index--)\n{\n"); | 735 L"for(var index = accessor_object.length - 1; index > 1; index--)\n{\n"); |
747 javascript << FX_WSTRC(L"method_return_value = accessor_object[index]."); | 736 javascript << FX_WSTRC(L"method_return_value = accessor_object[index]."); |
748 m_pExp2->ToJavaScript(javascript); | 737 m_pExp2->ToJavaScript(javascript); |
749 javascript << FX_WSTRC(L";\n}\n}\n"); | 738 javascript << FX_WSTRC(L";\n}\n}\n"); |
750 javascript << FX_WSTRC(L"else\n{\nmethod_return_value = accessor_object."); | 739 javascript << FX_WSTRC(L"else\n{\nmethod_return_value = accessor_object."); |
751 m_pExp2->ToJavaScript(javascript); | 740 m_pExp2->ToJavaScript(javascript); |
752 javascript << FX_WSTRC(L";\n}\n"); | 741 javascript << FX_WSTRC(L";\n}\n"); |
753 javascript << FX_WSTRC(L"return method_return_value;\n"); | 742 javascript << FX_WSTRC(L"return method_return_value;\n"); |
754 javascript << FX_WSTRC(L"}\n).call(this)"); | 743 javascript << FX_WSTRC(L"}\n).call(this)"); |
755 } | 744 } |
OLD | NEW |